// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2023 ARM Ltd.3*/45#include <linux/mm.h>6#include <linux/efi.h>7#include <linux/export.h>8#include <asm/tlbflush.h>910static inline bool mm_is_user(struct mm_struct *mm)11{12/*13* Don't attempt to apply the contig bit to kernel mappings, because14* dynamically adding/removing the contig bit can cause page faults.15* These racing faults are ok for user space, since they get serialized16* on the PTL. But kernel mappings can't tolerate faults.17*/18if (unlikely(mm_is_efi(mm)))19return false;20return mm != &init_mm;21}2223static inline pte_t *contpte_align_down(pte_t *ptep)24{25return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);26}2728static void contpte_try_unfold_partial(struct mm_struct *mm, unsigned long addr,29pte_t *ptep, unsigned int nr)30{31/*32* Unfold any partially covered contpte block at the beginning and end33* of the range.34*/3536if (ptep != contpte_align_down(ptep) || nr < CONT_PTES)37contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep));3839if (ptep + nr != contpte_align_down(ptep + nr)) {40unsigned long last_addr = addr + PAGE_SIZE * (nr - 1);41pte_t *last_ptep = ptep + nr - 1;4243contpte_try_unfold(mm, last_addr, last_ptep,44__ptep_get(last_ptep));45}46}4748static void contpte_convert(struct mm_struct *mm, unsigned long addr,49pte_t *ptep, pte_t pte)50{51struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);52unsigned long start_addr;53pte_t *start_ptep;54int i;5556start_ptep = ptep = contpte_align_down(ptep);57start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);58pte = pfn_pte(ALIGN_DOWN(pte_pfn(pte), CONT_PTES), pte_pgprot(pte));5960for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE) {61pte_t ptent = __ptep_get_and_clear(mm, addr, ptep);6263if (pte_dirty(ptent))64pte = pte_mkdirty(pte);6566if (pte_young(ptent))67pte = pte_mkyoung(pte);68}6970/*71* On eliding the __tlb_flush_range() under BBML2+noabort:72*73* NOTE: Instead of using N=16 as the contiguous block length, we use74* N=4 for clarity.75*76* NOTE: 'n' and 'c' are used to denote the "contiguous bit" being77* unset and set, respectively.78*79* We worry about two cases where contiguous bit is used:80* - When folding N smaller non-contiguous ptes as 1 contiguous block.81* - When unfolding a contiguous block into N smaller non-contiguous ptes.82*83* Currently, the BBML0 folding case looks as follows:84*85* 0) Initial page-table layout:86*87* +----+----+----+----+88* |RO,n|RO,n|RO,n|RW,n| <--- last page being set as RO89* +----+----+----+----+90*91* 1) Aggregate AF + dirty flags using __ptep_get_and_clear():92*93* +----+----+----+----+94* | 0 | 0 | 0 | 0 |95* +----+----+----+----+96*97* 2) __flush_tlb_range():98*99* |____ tlbi + dsb ____|100*101* 3) __set_ptes() to repaint contiguous block:102*103* +----+----+----+----+104* |RO,c|RO,c|RO,c|RO,c|105* +----+----+----+----+106*107* 4) The kernel will eventually __flush_tlb() for changed page:108*109* |____| <--- tlbi + dsb110*111* As expected, the intermediate tlbi+dsb ensures that other PEs112* only ever see an invalid (0) entry, or the new contiguous TLB entry.113* The final tlbi+dsb will always throw away the newly installed114* contiguous TLB entry, which is a micro-optimisation opportunity,115* but does not affect correctness.116*117* In the BBML2 case, the change is avoiding the intermediate tlbi+dsb.118* This means a few things, but notably other PEs will still "see" any119* stale cached TLB entries. This could lead to a "contiguous bit120* misprogramming" issue until the final tlbi+dsb of the changed page,121* which would clear out both the stale (RW,n) entry and the new (RO,c)122* contiguous entry installed in its place.123*124* What this is saying, is the following:125*126* +----+----+----+----+127* |RO,n|RO,n|RO,n|RW,n| <--- old page tables, all non-contiguous128* +----+----+----+----+129*130* +----+----+----+----+131* |RO,c|RO,c|RO,c|RO,c| <--- new page tables, all contiguous132* +----+----+----+----+133* /\134* ||135*136* If both the old single (RW,n) and new contiguous (RO,c) TLB entries137* are present, and a write is made to this address, do we fault or138* is the write permitted (via amalgamation)?139*140* The relevant Arm ARM DDI 0487L.a requirements are RNGLXZ and RJQQTC,141* and together state that when BBML1 or BBML2 are implemented, either142* a TLB conflict abort is raised (which we expressly forbid), or will143* "produce an OA, access permissions, and memory attributes that are144* consistent with any of the programmed translation table values".145*146* That is to say, will either raise a TLB conflict, or produce one of147* the cached TLB entries, but never amalgamate.148*149* Thus, as the page tables are only considered "consistent" after150* the final tlbi+dsb (which evicts both the single stale (RW,n) TLB151* entry as well as the new contiguous (RO,c) TLB entry), omitting the152* initial tlbi+dsb is correct.153*154* It is also important to note that at the end of the BBML2 folding155* case, we are still left with potentially all N TLB entries still156* cached (the N-1 non-contiguous ptes, and the single contiguous157* block). However, over time, natural TLB pressure will cause the158* non-contiguous pte TLB entries to be flushed, leaving only the159* contiguous block TLB entry. This means that omitting the tlbi+dsb is160* not only correct, but also keeps our eventual performance benefits.161*162* For the unfolding case, BBML0 looks as follows:163*164* 0) Initial page-table layout:165*166* +----+----+----+----+167* |RW,c|RW,c|RW,c|RW,c| <--- last page being set as RO168* +----+----+----+----+169*170* 1) Aggregate AF + dirty flags using __ptep_get_and_clear():171*172* +----+----+----+----+173* | 0 | 0 | 0 | 0 |174* +----+----+----+----+175*176* 2) __flush_tlb_range():177*178* |____ tlbi + dsb ____|179*180* 3) __set_ptes() to repaint as non-contiguous:181*182* +----+----+----+----+183* |RW,n|RW,n|RW,n|RW,n|184* +----+----+----+----+185*186* 4) Update changed page permissions:187*188* +----+----+----+----+189* |RW,n|RW,n|RW,n|RO,n| <--- last page permissions set190* +----+----+----+----+191*192* 5) The kernel will eventually __flush_tlb() for changed page:193*194* |____| <--- tlbi + dsb195*196* For BBML2, we again remove the intermediate tlbi+dsb. Here, there197* are no issues, as the final tlbi+dsb covering the changed page is198* guaranteed to remove the original large contiguous (RW,c) TLB entry,199* as well as the intermediate (RW,n) TLB entry; the next access will200* install the new (RO,n) TLB entry and the page tables are only201* considered "consistent" after the final tlbi+dsb, so software must202* be prepared for this inconsistency prior to finishing the mm dance203* regardless.204*/205206if (!system_supports_bbml2_noabort())207__flush_tlb_range(&vma, start_addr, addr, PAGE_SIZE, true, 3);208209__set_ptes(mm, start_addr, start_ptep, pte, CONT_PTES);210}211212void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,213pte_t *ptep, pte_t pte)214{215/*216* We have already checked that the virtual and pysical addresses are217* correctly aligned for a contpte mapping in contpte_try_fold() so the218* remaining checks are to ensure that the contpte range is fully219* covered by a single folio, and ensure that all the ptes are valid220* with contiguous PFNs and matching prots. We ignore the state of the221* access and dirty bits for the purpose of deciding if its a contiguous222* range; the folding process will generate a single contpte entry which223* has a single access and dirty bit. Those 2 bits are the logical OR of224* their respective bits in the constituent pte entries. In order to225* ensure the contpte range is covered by a single folio, we must226* recover the folio from the pfn, but special mappings don't have a227* folio backing them. Fortunately contpte_try_fold() already checked228* that the pte is not special - we never try to fold special mappings.229* Note we can't use vm_normal_page() for this since we don't have the230* vma.231*/232233unsigned long folio_start, folio_end;234unsigned long cont_start, cont_end;235pte_t expected_pte, subpte;236struct folio *folio;237struct page *page;238unsigned long pfn;239pte_t *orig_ptep;240pgprot_t prot;241242int i;243244if (!mm_is_user(mm))245return;246247page = pte_page(pte);248folio = page_folio(page);249folio_start = addr - (page - &folio->page) * PAGE_SIZE;250folio_end = folio_start + folio_nr_pages(folio) * PAGE_SIZE;251cont_start = ALIGN_DOWN(addr, CONT_PTE_SIZE);252cont_end = cont_start + CONT_PTE_SIZE;253254if (folio_start > cont_start || folio_end < cont_end)255return;256257pfn = ALIGN_DOWN(pte_pfn(pte), CONT_PTES);258prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));259expected_pte = pfn_pte(pfn, prot);260orig_ptep = ptep;261ptep = contpte_align_down(ptep);262263for (i = 0; i < CONT_PTES; i++) {264subpte = pte_mkold(pte_mkclean(__ptep_get(ptep)));265if (!pte_same(subpte, expected_pte))266return;267expected_pte = pte_advance_pfn(expected_pte, 1);268ptep++;269}270271pte = pte_mkcont(pte);272contpte_convert(mm, addr, orig_ptep, pte);273}274EXPORT_SYMBOL_GPL(__contpte_try_fold);275276void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,277pte_t *ptep, pte_t pte)278{279/*280* We have already checked that the ptes are contiguous in281* contpte_try_unfold(), so just check that the mm is user space.282*/283if (!mm_is_user(mm))284return;285286pte = pte_mknoncont(pte);287contpte_convert(mm, addr, ptep, pte);288}289EXPORT_SYMBOL_GPL(__contpte_try_unfold);290291pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)292{293/*294* Gather access/dirty bits, which may be populated in any of the ptes295* of the contig range. We are guaranteed to be holding the PTL, so any296* contiguous range cannot be unfolded or otherwise modified under our297* feet.298*/299300pte_t pte;301int i;302303ptep = contpte_align_down(ptep);304305for (i = 0; i < CONT_PTES; i++, ptep++) {306pte = __ptep_get(ptep);307308if (pte_dirty(pte)) {309orig_pte = pte_mkdirty(orig_pte);310for (; i < CONT_PTES; i++, ptep++) {311pte = __ptep_get(ptep);312if (pte_young(pte)) {313orig_pte = pte_mkyoung(orig_pte);314break;315}316}317break;318}319320if (pte_young(pte)) {321orig_pte = pte_mkyoung(orig_pte);322i++;323ptep++;324for (; i < CONT_PTES; i++, ptep++) {325pte = __ptep_get(ptep);326if (pte_dirty(pte)) {327orig_pte = pte_mkdirty(orig_pte);328break;329}330}331break;332}333}334335return orig_pte;336}337EXPORT_SYMBOL_GPL(contpte_ptep_get);338339static inline bool contpte_is_consistent(pte_t pte, unsigned long pfn,340pgprot_t orig_prot)341{342pgprot_t prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));343344return pte_valid_cont(pte) && pte_pfn(pte) == pfn &&345pgprot_val(prot) == pgprot_val(orig_prot);346}347348pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)349{350/*351* The ptep_get_lockless() API requires us to read and return *orig_ptep352* so that it is self-consistent, without the PTL held, so we may be353* racing with other threads modifying the pte. Usually a READ_ONCE()354* would suffice, but for the contpte case, we also need to gather the355* access and dirty bits from across all ptes in the contiguous block,356* and we can't read all of those neighbouring ptes atomically, so any357* contiguous range may be unfolded/modified/refolded under our feet.358* Therefore we ensure we read a _consistent_ contpte range by checking359* that all ptes in the range are valid and have CONT_PTE set, that all360* pfns are contiguous and that all pgprots are the same (ignoring361* access/dirty). If we find a pte that is not consistent, then we must362* be racing with an update so start again. If the target pte does not363* have CONT_PTE set then that is considered consistent on its own364* because it is not part of a contpte range.365*/366367pgprot_t orig_prot;368unsigned long pfn;369pte_t orig_pte;370pte_t *ptep;371pte_t pte;372int i;373374retry:375orig_pte = __ptep_get(orig_ptep);376377if (!pte_valid_cont(orig_pte))378return orig_pte;379380orig_prot = pte_pgprot(pte_mkold(pte_mkclean(orig_pte)));381ptep = contpte_align_down(orig_ptep);382pfn = pte_pfn(orig_pte) - (orig_ptep - ptep);383384for (i = 0; i < CONT_PTES; i++, ptep++, pfn++) {385pte = __ptep_get(ptep);386387if (!contpte_is_consistent(pte, pfn, orig_prot))388goto retry;389390if (pte_dirty(pte)) {391orig_pte = pte_mkdirty(orig_pte);392for (; i < CONT_PTES; i++, ptep++, pfn++) {393pte = __ptep_get(ptep);394395if (!contpte_is_consistent(pte, pfn, orig_prot))396goto retry;397398if (pte_young(pte)) {399orig_pte = pte_mkyoung(orig_pte);400break;401}402}403break;404}405406if (pte_young(pte)) {407orig_pte = pte_mkyoung(orig_pte);408i++;409ptep++;410pfn++;411for (; i < CONT_PTES; i++, ptep++, pfn++) {412pte = __ptep_get(ptep);413414if (!contpte_is_consistent(pte, pfn, orig_prot))415goto retry;416417if (pte_dirty(pte)) {418orig_pte = pte_mkdirty(orig_pte);419break;420}421}422break;423}424}425426return orig_pte;427}428EXPORT_SYMBOL_GPL(contpte_ptep_get_lockless);429430void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,431pte_t *ptep, pte_t pte, unsigned int nr)432{433unsigned long next;434unsigned long end;435unsigned long pfn;436pgprot_t prot;437438/*439* The set_ptes() spec guarantees that when nr > 1, the initial state of440* all ptes is not-present. Therefore we never need to unfold or441* otherwise invalidate a range before we set the new ptes.442* contpte_set_ptes() should never be called for nr < 2.443*/444VM_WARN_ON(nr == 1);445446if (!mm_is_user(mm))447return __set_ptes(mm, addr, ptep, pte, nr);448449end = addr + (nr << PAGE_SHIFT);450pfn = pte_pfn(pte);451prot = pte_pgprot(pte);452453do {454next = pte_cont_addr_end(addr, end);455nr = (next - addr) >> PAGE_SHIFT;456pte = pfn_pte(pfn, prot);457458if (((addr | next | (pfn << PAGE_SHIFT)) & ~CONT_PTE_MASK) == 0)459pte = pte_mkcont(pte);460else461pte = pte_mknoncont(pte);462463__set_ptes(mm, addr, ptep, pte, nr);464465addr = next;466ptep += nr;467pfn += nr;468469} while (addr != end);470}471EXPORT_SYMBOL_GPL(contpte_set_ptes);472473void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,474pte_t *ptep, unsigned int nr, int full)475{476contpte_try_unfold_partial(mm, addr, ptep, nr);477__clear_full_ptes(mm, addr, ptep, nr, full);478}479EXPORT_SYMBOL_GPL(contpte_clear_full_ptes);480481pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm,482unsigned long addr, pte_t *ptep,483unsigned int nr, int full)484{485contpte_try_unfold_partial(mm, addr, ptep, nr);486return __get_and_clear_full_ptes(mm, addr, ptep, nr, full);487}488EXPORT_SYMBOL_GPL(contpte_get_and_clear_full_ptes);489490int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma,491unsigned long addr, pte_t *ptep)492{493/*494* ptep_clear_flush_young() technically requires us to clear the access495* flag for a _single_ pte. However, the core-mm code actually tracks496* access/dirty per folio, not per page. And since we only create a497* contig range when the range is covered by a single folio, we can get498* away with clearing young for the whole contig range here, so we avoid499* having to unfold.500*/501502int young = 0;503int i;504505ptep = contpte_align_down(ptep);506addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);507508for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)509young |= __ptep_test_and_clear_young(vma, addr, ptep);510511return young;512}513EXPORT_SYMBOL_GPL(contpte_ptep_test_and_clear_young);514515int contpte_ptep_clear_flush_young(struct vm_area_struct *vma,516unsigned long addr, pte_t *ptep)517{518int young;519520young = contpte_ptep_test_and_clear_young(vma, addr, ptep);521522if (young) {523/*524* See comment in __ptep_clear_flush_young(); same rationale for525* eliding the trailing DSB applies here.526*/527addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);528__flush_tlb_range_nosync(vma->vm_mm, addr, addr + CONT_PTE_SIZE,529PAGE_SIZE, true, 3);530}531532return young;533}534EXPORT_SYMBOL_GPL(contpte_ptep_clear_flush_young);535536void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,537pte_t *ptep, unsigned int nr)538{539/*540* If wrprotecting an entire contig range, we can avoid unfolding. Just541* set wrprotect and wait for the later mmu_gather flush to invalidate542* the tlb. Until the flush, the page may or may not be wrprotected.543* After the flush, it is guaranteed wrprotected. If it's a partial544* range though, we must unfold, because we can't have a case where545* CONT_PTE is set but wrprotect applies to a subset of the PTEs; this546* would cause it to continue to be unpredictable after the flush.547*/548549contpte_try_unfold_partial(mm, addr, ptep, nr);550__wrprotect_ptes(mm, addr, ptep, nr);551}552EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes);553554void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma,555unsigned long addr, pte_t *ptep,556unsigned int nr, cydp_t flags)557{558/*559* We can safely clear access/dirty without needing to unfold from560* the architectures perspective, even when contpte is set. If the561* range starts or ends midway through a contpte block, we can just562* expand to include the full contpte block. While this is not563* exactly what the core-mm asked for, it tracks access/dirty per564* folio, not per page. And since we only create a contpte block565* when it is covered by a single folio, we can get away with566* clearing access/dirty for the whole block.567*/568unsigned long start = addr;569unsigned long end = start + nr * PAGE_SIZE;570571if (pte_cont(__ptep_get(ptep + nr - 1)))572end = ALIGN(end, CONT_PTE_SIZE);573574if (pte_cont(__ptep_get(ptep))) {575start = ALIGN_DOWN(start, CONT_PTE_SIZE);576ptep = contpte_align_down(ptep);577}578579__clear_young_dirty_ptes(vma, start, ptep, (end - start) / PAGE_SIZE, flags);580}581EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes);582583int contpte_ptep_set_access_flags(struct vm_area_struct *vma,584unsigned long addr, pte_t *ptep,585pte_t entry, int dirty)586{587unsigned long start_addr;588pte_t orig_pte;589int i;590591/*592* Gather the access/dirty bits for the contiguous range. If nothing has593* changed, its a noop.594*/595orig_pte = pte_mknoncont(ptep_get(ptep));596if (pte_val(orig_pte) == pte_val(entry))597return 0;598599/*600* We can fix up access/dirty bits without having to unfold the contig601* range. But if the write bit is changing, we must unfold.602*/603if (pte_write(orig_pte) == pte_write(entry)) {604/*605* For HW access management, we technically only need to update606* the flag on a single pte in the range. But for SW access607* management, we need to update all the ptes to prevent extra608* faults. Avoid per-page tlb flush in __ptep_set_access_flags()609* and instead flush the whole range at the end.610*/611ptep = contpte_align_down(ptep);612start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);613614/*615* We are not advancing entry because __ptep_set_access_flags()616* only consumes access flags from entry. And since we have checked617* for the whole contpte block and returned early, pte_same()618* within __ptep_set_access_flags() is likely false.619*/620for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)621__ptep_set_access_flags(vma, addr, ptep, entry, 0);622623if (dirty)624__flush_tlb_range(vma, start_addr, addr,625PAGE_SIZE, true, 3);626} else {627__contpte_try_unfold(vma->vm_mm, addr, ptep, orig_pte);628__ptep_set_access_flags(vma, addr, ptep, entry, dirty);629}630631return 1;632}633EXPORT_SYMBOL_GPL(contpte_ptep_set_access_flags);634635636