1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/hugetlb.h> 3 #include <linux/err.h> 4 5 #ifdef CONFIG_RISCV_ISA_SVNAPOT 6 pte_t huge_ptep_get(pte_t *ptep) 7 { 8 unsigned long pte_num; 9 int i; 10 pte_t orig_pte = ptep_get(ptep); 11 12 if (!pte_present(orig_pte) || !pte_napot(orig_pte)) 13 return orig_pte; 14 15 pte_num = napot_pte_num(napot_cont_order(orig_pte)); 16 17 for (i = 0; i < pte_num; i++, ptep++) { 18 pte_t pte = ptep_get(ptep); 19 20 if (pte_dirty(pte)) 21 orig_pte = pte_mkdirty(orig_pte); 22 23 if (pte_young(pte)) 24 orig_pte = pte_mkyoung(orig_pte); 25 } 26 27 return orig_pte; 28 } 29 30 pte_t *huge_pte_alloc(struct mm_struct *mm, 31 struct vm_area_struct *vma, 32 unsigned long addr, 33 unsigned long sz) 34 { 35 unsigned long order; 36 pte_t *pte = NULL; 37 pgd_t *pgd; 38 p4d_t *p4d; 39 pud_t *pud; 40 pmd_t *pmd; 41 42 pgd = pgd_offset(mm, addr); 43 p4d = p4d_alloc(mm, pgd, addr); 44 if (!p4d) 45 return NULL; 46 47 pud = pud_alloc(mm, p4d, addr); 48 if (!pud) 49 return NULL; 50 51 if (sz == PUD_SIZE) { 52 pte = (pte_t *)pud; 53 goto out; 54 } 55 56 if (sz == PMD_SIZE) { 57 if (want_pmd_share(vma, addr) && pud_none(*pud)) 58 pte = huge_pmd_share(mm, vma, addr, pud); 59 else 60 pte = (pte_t *)pmd_alloc(mm, pud, addr); 61 goto out; 62 } 63 64 pmd = pmd_alloc(mm, pud, addr); 65 if (!pmd) 66 return NULL; 67 68 for_each_napot_order(order) { 69 if (napot_cont_size(order) == sz) { 70 pte = pte_alloc_huge(mm, pmd, addr & napot_cont_mask(order)); 71 break; 72 } 73 } 74 75 out: 76 if (pte) { 77 pte_t pteval = ptep_get_lockless(pte); 78 79 WARN_ON_ONCE(pte_present(pteval) && !pte_huge(pteval)); 80 } 81 return pte; 82 } 83 84 pte_t *huge_pte_offset(struct mm_struct *mm, 85 unsigned long addr, 86 unsigned long sz) 87 { 88 unsigned long order; 89 pte_t *pte = NULL; 90 pgd_t *pgd; 91 p4d_t *p4d; 92 pud_t *pud; 93 pmd_t *pmd; 94 95 pgd = pgd_offset(mm, addr); 96 if (!pgd_present(*pgd)) 97 return NULL; 98 99 p4d = p4d_offset(pgd, addr); 100 if (!p4d_present(*p4d)) 101 return NULL; 102 103 pud = pud_offset(p4d, addr); 104 if (sz == PUD_SIZE) 105 /* must be pud huge, non-present or none */ 106 return (pte_t *)pud; 107 108 if (!pud_present(*pud)) 109 return NULL; 110 111 pmd = pmd_offset(pud, addr); 112 if (sz == PMD_SIZE) 113 /* must be pmd huge, non-present or none */ 114 return (pte_t *)pmd; 115 116 if (!pmd_present(*pmd)) 117 return NULL; 118 119 for_each_napot_order(order) { 120 if (napot_cont_size(order) == sz) { 121 pte = pte_offset_huge(pmd, addr & napot_cont_mask(order)); 122 break; 123 } 124 } 125 return pte; 126 } 127 128 static pte_t get_clear_contig(struct mm_struct *mm, 129 unsigned long addr, 130 pte_t *ptep, 131 unsigned long pte_num) 132 { 133 pte_t orig_pte = ptep_get(ptep); 134 unsigned long i; 135 136 for (i = 0; i < pte_num; i++, addr += PAGE_SIZE, ptep++) { 137 pte_t pte = ptep_get_and_clear(mm, addr, ptep); 138 139 if (pte_dirty(pte)) 140 orig_pte = pte_mkdirty(orig_pte); 141 142 if (pte_young(pte)) 143 orig_pte = pte_mkyoung(orig_pte); 144 } 145 146 return orig_pte; 147 } 148 149 static pte_t get_clear_contig_flush(struct mm_struct *mm, 150 unsigned long addr, 151 pte_t *ptep, 152 unsigned long pte_num) 153 { 154 pte_t orig_pte = get_clear_contig(mm, addr, ptep, pte_num); 155 struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0); 156 bool valid = !pte_none(orig_pte); 157 158 if (valid) 159 flush_tlb_range(&vma, addr, addr + (PAGE_SIZE * pte_num)); 160 161 return orig_pte; 162 } 163 164 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags) 165 { 166 unsigned long order; 167 168 for_each_napot_order(order) { 169 if (shift == napot_cont_shift(order)) { 170 entry = pte_mknapot(entry, order); 171 break; 172 } 173 } 174 if (order == NAPOT_ORDER_MAX) 175 entry = pte_mkhuge(entry); 176 177 return entry; 178 } 179 180 void set_huge_pte_at(struct mm_struct *mm, 181 unsigned long addr, 182 pte_t *ptep, 183 pte_t pte) 184 { 185 int i, pte_num; 186 187 if (!pte_napot(pte)) { 188 set_pte_at(mm, addr, ptep, pte); 189 return; 190 } 191 192 pte_num = napot_pte_num(napot_cont_order(pte)); 193 for (i = 0; i < pte_num; i++, ptep++, addr += PAGE_SIZE) 194 set_pte_at(mm, addr, ptep, pte); 195 } 196 197 int huge_ptep_set_access_flags(struct vm_area_struct *vma, 198 unsigned long addr, 199 pte_t *ptep, 200 pte_t pte, 201 int dirty) 202 { 203 struct mm_struct *mm = vma->vm_mm; 204 unsigned long order; 205 pte_t orig_pte; 206 int i, pte_num; 207 208 if (!pte_napot(pte)) 209 return ptep_set_access_flags(vma, addr, ptep, pte, dirty); 210 211 order = napot_cont_order(pte); 212 pte_num = napot_pte_num(order); 213 ptep = huge_pte_offset(mm, addr, napot_cont_size(order)); 214 orig_pte = get_clear_contig_flush(mm, addr, ptep, pte_num); 215 216 if (pte_dirty(orig_pte)) 217 pte = pte_mkdirty(pte); 218 219 if (pte_young(orig_pte)) 220 pte = pte_mkyoung(pte); 221 222 for (i = 0; i < pte_num; i++, addr += PAGE_SIZE, ptep++) 223 set_pte_at(mm, addr, ptep, pte); 224 225 return true; 226 } 227 228 pte_t huge_ptep_get_and_clear(struct mm_struct *mm, 229 unsigned long addr, 230 pte_t *ptep) 231 { 232 pte_t orig_pte = ptep_get(ptep); 233 int pte_num; 234 235 if (!pte_napot(orig_pte)) 236 return ptep_get_and_clear(mm, addr, ptep); 237 238 pte_num = napot_pte_num(napot_cont_order(orig_pte)); 239 240 return get_clear_contig(mm, addr, ptep, pte_num); 241 } 242 243 void huge_ptep_set_wrprotect(struct mm_struct *mm, 244 unsigned long addr, 245 pte_t *ptep) 246 { 247 pte_t pte = ptep_get(ptep); 248 unsigned long order; 249 pte_t orig_pte; 250 int i, pte_num; 251 252 if (!pte_napot(pte)) { 253 ptep_set_wrprotect(mm, addr, ptep); 254 return; 255 } 256 257 order = napot_cont_order(pte); 258 pte_num = napot_pte_num(order); 259 ptep = huge_pte_offset(mm, addr, napot_cont_size(order)); 260 orig_pte = get_clear_contig_flush(mm, addr, ptep, pte_num); 261 262 orig_pte = pte_wrprotect(orig_pte); 263 264 for (i = 0; i < pte_num; i++, addr += PAGE_SIZE, ptep++) 265 set_pte_at(mm, addr, ptep, orig_pte); 266 } 267 268 pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, 269 unsigned long addr, 270 pte_t *ptep) 271 { 272 pte_t pte = ptep_get(ptep); 273 int pte_num; 274 275 if (!pte_napot(pte)) 276 return ptep_clear_flush(vma, addr, ptep); 277 278 pte_num = napot_pte_num(napot_cont_order(pte)); 279 280 return get_clear_contig_flush(vma->vm_mm, addr, ptep, pte_num); 281 } 282 283 void huge_pte_clear(struct mm_struct *mm, 284 unsigned long addr, 285 pte_t *ptep, 286 unsigned long sz) 287 { 288 pte_t pte = READ_ONCE(*ptep); 289 int i, pte_num; 290 291 if (!pte_napot(pte)) { 292 pte_clear(mm, addr, ptep); 293 return; 294 } 295 296 pte_num = napot_pte_num(napot_cont_order(pte)); 297 for (i = 0; i < pte_num; i++, addr += PAGE_SIZE, ptep++) 298 pte_clear(mm, addr, ptep); 299 } 300 301 static __init bool is_napot_size(unsigned long size) 302 { 303 unsigned long order; 304 305 if (!has_svnapot()) 306 return false; 307 308 for_each_napot_order(order) { 309 if (size == napot_cont_size(order)) 310 return true; 311 } 312 return false; 313 } 314 315 static __init int napot_hugetlbpages_init(void) 316 { 317 if (has_svnapot()) { 318 unsigned long order; 319 320 for_each_napot_order(order) 321 hugetlb_add_hstate(order); 322 } 323 return 0; 324 } 325 arch_initcall(napot_hugetlbpages_init); 326 327 #else 328 329 static __init bool is_napot_size(unsigned long size) 330 { 331 return false; 332 } 333 334 #endif /*CONFIG_RISCV_ISA_SVNAPOT*/ 335 336 int pud_huge(pud_t pud) 337 { 338 return pud_leaf(pud); 339 } 340 341 int pmd_huge(pmd_t pmd) 342 { 343 return pmd_leaf(pmd); 344 } 345 346 bool __init arch_hugetlb_valid_size(unsigned long size) 347 { 348 if (size == HPAGE_SIZE) 349 return true; 350 else if (IS_ENABLED(CONFIG_64BIT) && size == PUD_SIZE) 351 return true; 352 else if (is_napot_size(size)) 353 return true; 354 else 355 return false; 356 } 357 358 #ifdef CONFIG_CONTIG_ALLOC 359 static __init int gigantic_pages_init(void) 360 { 361 /* With CONTIG_ALLOC, we can allocate gigantic pages at runtime */ 362 if (IS_ENABLED(CONFIG_64BIT)) 363 hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT); 364 return 0; 365 } 366 arch_initcall(gigantic_pages_init); 367 #endif 368