Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 2 | /* |
| 3 | * mm/pgtable-generic.c |
| 4 | * |
Mike Rapoport | ca5999f | 2020-06-08 21:32:38 -0700 | [diff] [blame] | 5 | * Generic pgtable methods declared in linux/pgtable.h |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 2010 Linus Torvalds |
| 8 | */ |
| 9 | |
Andrew Morton | f95ba94 | 2011-01-25 15:07:11 -0800 | [diff] [blame] | 10 | #include <linux/pagemap.h> |
Peter Zijlstra | a31acd3 | 2018-08-26 12:56:48 +0200 | [diff] [blame] | 11 | #include <linux/hugetlb.h> |
Mike Rapoport | ca5999f | 2020-06-08 21:32:38 -0700 | [diff] [blame] | 12 | #include <linux/pgtable.h> |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 13 | #include <asm/tlb.h> |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 14 | |
Joonsoo Kim | bc4b444 | 2013-09-11 14:21:28 -0700 | [diff] [blame] | 15 | /* |
| 16 | * If a p?d_bad entry is found while walking page tables, report |
| 17 | * the error, before resetting entry to p?d_none. Usually (but |
| 18 | * very seldom) called out from the p?d_none_or_clear_bad macros. |
| 19 | */ |
| 20 | |
| 21 | void pgd_clear_bad(pgd_t *pgd) |
| 22 | { |
| 23 | pgd_ERROR(*pgd); |
| 24 | pgd_clear(pgd); |
| 25 | } |
| 26 | |
Vineet Gupta | f2400ab | 2019-11-30 17:51:20 -0800 | [diff] [blame] | 27 | #ifndef __PAGETABLE_P4D_FOLDED |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 28 | void p4d_clear_bad(p4d_t *p4d) |
| 29 | { |
| 30 | p4d_ERROR(*p4d); |
| 31 | p4d_clear(p4d); |
| 32 | } |
Vineet Gupta | f2400ab | 2019-11-30 17:51:20 -0800 | [diff] [blame] | 33 | #endif |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 34 | |
Vineet Gupta | f2400ab | 2019-11-30 17:51:20 -0800 | [diff] [blame] | 35 | #ifndef __PAGETABLE_PUD_FOLDED |
Joonsoo Kim | bc4b444 | 2013-09-11 14:21:28 -0700 | [diff] [blame] | 36 | void pud_clear_bad(pud_t *pud) |
| 37 | { |
| 38 | pud_ERROR(*pud); |
| 39 | pud_clear(pud); |
| 40 | } |
Vineet Gupta | f2400ab | 2019-11-30 17:51:20 -0800 | [diff] [blame] | 41 | #endif |
Joonsoo Kim | bc4b444 | 2013-09-11 14:21:28 -0700 | [diff] [blame] | 42 | |
Vineet Gupta | f2400ab | 2019-11-30 17:51:20 -0800 | [diff] [blame] | 43 | /* |
| 44 | * Note that the pmd variant below can't be stub'ed out just as for p4d/pud |
| 45 | * above. pmd folding is special and typically pmd_* macros refer to upper |
| 46 | * level even when folded |
| 47 | */ |
Joonsoo Kim | bc4b444 | 2013-09-11 14:21:28 -0700 | [diff] [blame] | 48 | void pmd_clear_bad(pmd_t *pmd) |
| 49 | { |
| 50 | pmd_ERROR(*pmd); |
| 51 | pmd_clear(pmd); |
| 52 | } |
| 53 | |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 54 | #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS |
| 55 | /* |
Mike Rapoport | ca5999f | 2020-06-08 21:32:38 -0700 | [diff] [blame] | 56 | * Only sets the access flags (dirty, accessed), as well as write |
Rik van Riel | cef23d9 | 2012-11-06 09:56:01 +0000 | [diff] [blame] | 57 | * permission. Furthermore, we know it always gets set to a "more |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 58 | * permissive" setting, which allows most architectures to optimize |
| 59 | * this. We return whether the PTE actually changed, which in turn |
| 60 | * instructs the caller to do things like update__mmu_cache. This |
| 61 | * used to be done in the caller, but sparc needs minor faults to |
| 62 | * force that call on sun4c so we changed this macro slightly |
| 63 | */ |
| 64 | int ptep_set_access_flags(struct vm_area_struct *vma, |
| 65 | unsigned long address, pte_t *ptep, |
| 66 | pte_t entry, int dirty) |
| 67 | { |
| 68 | int changed = !pte_same(*ptep, entry); |
| 69 | if (changed) { |
| 70 | set_pte_at(vma->vm_mm, address, ptep, entry); |
Rik van Riel | cef23d9 | 2012-11-06 09:56:01 +0000 | [diff] [blame] | 71 | flush_tlb_fix_spurious_fault(vma, address); |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 72 | } |
| 73 | return changed; |
| 74 | } |
| 75 | #endif |
| 76 | |
Vineet Gupta | 52585bc | 2015-07-09 17:19:30 +0530 | [diff] [blame] | 77 | #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH |
| 78 | int ptep_clear_flush_young(struct vm_area_struct *vma, |
| 79 | unsigned long address, pte_t *ptep) |
| 80 | { |
| 81 | int young; |
| 82 | young = ptep_test_and_clear_young(vma, address, ptep); |
| 83 | if (young) |
| 84 | flush_tlb_page(vma, address); |
| 85 | return young; |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH |
| 90 | pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long address, |
| 91 | pte_t *ptep) |
| 92 | { |
| 93 | struct mm_struct *mm = (vma)->vm_mm; |
| 94 | pte_t pte; |
| 95 | pte = ptep_get_and_clear(mm, address, ptep); |
| 96 | if (pte_accessible(mm, pte)) |
| 97 | flush_tlb_page(vma, address); |
| 98 | return pte; |
| 99 | } |
| 100 | #endif |
| 101 | |
Vineet Gupta | bd5e88a | 2015-07-09 17:22:44 +0530 | [diff] [blame] | 102 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 103 | |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 104 | #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS |
| 105 | int pmdp_set_access_flags(struct vm_area_struct *vma, |
| 106 | unsigned long address, pmd_t *pmdp, |
| 107 | pmd_t entry, int dirty) |
| 108 | { |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 109 | int changed = !pmd_same(*pmdp, entry); |
| 110 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 111 | if (changed) { |
| 112 | set_pmd_at(vma->vm_mm, address, pmdp, entry); |
Vineet Gupta | 12ebc15 | 2015-02-20 10:36:28 +0530 | [diff] [blame] | 113 | flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 114 | } |
| 115 | return changed; |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 116 | } |
| 117 | #endif |
| 118 | |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 119 | #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH |
| 120 | int pmdp_clear_flush_young(struct vm_area_struct *vma, |
| 121 | unsigned long address, pmd_t *pmdp) |
| 122 | { |
| 123 | int young; |
Naoya Horiguchi | d8c37c4 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 124 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 125 | young = pmdp_test_and_clear_young(vma, address, pmdp); |
| 126 | if (young) |
Vineet Gupta | 12ebc15 | 2015-02-20 10:36:28 +0530 | [diff] [blame] | 127 | flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 128 | return young; |
| 129 | } |
| 130 | #endif |
| 131 | |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 132 | #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 133 | pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address, |
| 134 | pmd_t *pmdp) |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 135 | { |
| 136 | pmd_t pmd; |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 137 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
Hugh Dickins | 99fa8a4 | 2021-06-15 18:23:45 -0700 | [diff] [blame^] | 138 | VM_BUG_ON(pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) && |
| 139 | !pmd_devmap(*pmdp)); |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 140 | pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp); |
Vineet Gupta | 12ebc15 | 2015-02-20 10:36:28 +0530 | [diff] [blame] | 141 | flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 142 | return pmd; |
| 143 | } |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 144 | |
| 145 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
| 146 | pud_t pudp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address, |
| 147 | pud_t *pudp) |
| 148 | { |
| 149 | pud_t pud; |
| 150 | |
| 151 | VM_BUG_ON(address & ~HPAGE_PUD_MASK); |
| 152 | VM_BUG_ON(!pud_trans_huge(*pudp) && !pud_devmap(*pudp)); |
| 153 | pud = pudp_huge_get_and_clear(vma->vm_mm, address, pudp); |
| 154 | flush_pud_tlb_range(vma, address, address + HPAGE_PUD_SIZE); |
| 155 | return pud; |
| 156 | } |
| 157 | #endif |
Andrea Arcangeli | e2cda32 | 2011-01-13 15:46:40 -0800 | [diff] [blame] | 158 | #endif |
| 159 | |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 160 | #ifndef __HAVE_ARCH_PGTABLE_DEPOSIT |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 161 | void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp, |
| 162 | pgtable_t pgtable) |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 163 | { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 164 | assert_spin_locked(pmd_lockptr(mm, pmdp)); |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 165 | |
| 166 | /* FIFO */ |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 167 | if (!pmd_huge_pte(mm, pmdp)) |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 168 | INIT_LIST_HEAD(&pgtable->lru); |
| 169 | else |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 170 | list_add(&pgtable->lru, &pmd_huge_pte(mm, pmdp)->lru); |
| 171 | pmd_huge_pte(mm, pmdp) = pgtable; |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 172 | } |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 173 | #endif |
| 174 | |
| 175 | #ifndef __HAVE_ARCH_PGTABLE_WITHDRAW |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 176 | /* no "address" argument so destroys page coloring of some arch */ |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 177 | pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp) |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 178 | { |
| 179 | pgtable_t pgtable; |
| 180 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 181 | assert_spin_locked(pmd_lockptr(mm, pmdp)); |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 182 | |
| 183 | /* FIFO */ |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 184 | pgtable = pmd_huge_pte(mm, pmdp); |
Geliang Tang | 1466934 | 2016-01-14 15:19:32 -0800 | [diff] [blame] | 185 | pmd_huge_pte(mm, pmdp) = list_first_entry_or_null(&pgtable->lru, |
| 186 | struct page, lru); |
| 187 | if (pmd_huge_pte(mm, pmdp)) |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 188 | list_del(&pgtable->lru); |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 189 | return pgtable; |
| 190 | } |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 191 | #endif |
Gerald Schaefer | 46dcde7 | 2012-10-08 16:30:09 -0700 | [diff] [blame] | 192 | |
| 193 | #ifndef __HAVE_ARCH_PMDP_INVALIDATE |
Kirill A. Shutemov | d52605d | 2018-01-31 16:18:16 -0800 | [diff] [blame] | 194 | pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, |
Gerald Schaefer | 46dcde7 | 2012-10-08 16:30:09 -0700 | [diff] [blame] | 195 | pmd_t *pmdp) |
| 196 | { |
Anshuman Khandual | 86ec2da | 2020-06-03 16:03:45 -0700 | [diff] [blame] | 197 | pmd_t old = pmdp_establish(vma, address, pmdp, pmd_mkinvalid(*pmdp)); |
Vineet Gupta | 12ebc15 | 2015-02-20 10:36:28 +0530 | [diff] [blame] | 198 | flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); |
Kirill A. Shutemov | d52605d | 2018-01-31 16:18:16 -0800 | [diff] [blame] | 199 | return old; |
Gerald Schaefer | 46dcde7 | 2012-10-08 16:30:09 -0700 | [diff] [blame] | 200 | } |
Gerald Schaefer | 46dcde7 | 2012-10-08 16:30:09 -0700 | [diff] [blame] | 201 | #endif |
Aneesh Kumar K.V | f28b6ff | 2015-06-24 16:57:42 -0700 | [diff] [blame] | 202 | |
| 203 | #ifndef pmdp_collapse_flush |
Aneesh Kumar K.V | f28b6ff | 2015-06-24 16:57:42 -0700 | [diff] [blame] | 204 | pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address, |
| 205 | pmd_t *pmdp) |
| 206 | { |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 207 | /* |
| 208 | * pmd and hugepage pte format are same. So we could |
| 209 | * use the same function. |
| 210 | */ |
Aneesh Kumar K.V | f28b6ff | 2015-06-24 16:57:42 -0700 | [diff] [blame] | 211 | pmd_t pmd; |
| 212 | |
| 213 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 214 | VM_BUG_ON(pmd_trans_huge(*pmdp)); |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 215 | pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp); |
Vineet Gupta | 6a6ac72 | 2016-02-11 16:13:09 -0800 | [diff] [blame] | 216 | |
| 217 | /* collapse entails shooting down ptes not pmd */ |
| 218 | flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE); |
Aneesh Kumar K.V | f28b6ff | 2015-06-24 16:57:42 -0700 | [diff] [blame] | 219 | return pmd; |
| 220 | } |
Aneesh Kumar K.V | f28b6ff | 2015-06-24 16:57:42 -0700 | [diff] [blame] | 221 | #endif |
Vineet Gupta | bd5e88a | 2015-07-09 17:22:44 +0530 | [diff] [blame] | 222 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |