1 /* 2 * arch/arm64/mm/hugetlbpage.c 3 * 4 * Copyright (C) 2013 Linaro Ltd. 5 * 6 * Based on arch/x86/mm/hugetlbpage.c. 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 */ 17 18 #include <linux/init.h> 19 #include <linux/fs.h> 20 #include <linux/mm.h> 21 #include <linux/hugetlb.h> 22 #include <linux/pagemap.h> 23 #include <linux/err.h> 24 #include <linux/sysctl.h> 25 #include <asm/mman.h> 26 #include <asm/tlb.h> 27 #include <asm/tlbflush.h> 28 #include <asm/pgalloc.h> 29 30 int pmd_huge(pmd_t pmd) 31 { 32 return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT); 33 } 34 35 int pud_huge(pud_t pud) 36 { 37 #ifndef __PAGETABLE_PMD_FOLDED 38 return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT); 39 #else 40 return 0; 41 #endif 42 } 43 44 /* 45 * Select all bits except the pfn 46 */ 47 static inline pgprot_t pte_pgprot(pte_t pte) 48 { 49 unsigned long pfn = pte_pfn(pte); 50 51 return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte)); 52 } 53 54 static int find_num_contig(struct mm_struct *mm, unsigned long addr, 55 pte_t *ptep, size_t *pgsize) 56 { 57 pgd_t *pgdp = pgd_offset(mm, addr); 58 pud_t *pudp; 59 pmd_t *pmdp; 60 61 *pgsize = PAGE_SIZE; 62 pudp = pud_offset(pgdp, addr); 63 pmdp = pmd_offset(pudp, addr); 64 if ((pte_t *)pmdp == ptep) { 65 *pgsize = PMD_SIZE; 66 return CONT_PMDS; 67 } 68 return CONT_PTES; 69 } 70 71 static inline int num_contig_ptes(unsigned long size, size_t *pgsize) 72 { 73 int contig_ptes = 0; 74 75 *pgsize = size; 76 77 switch (size) { 78 #ifdef CONFIG_ARM64_4K_PAGES 79 case PUD_SIZE: 80 #endif 81 case PMD_SIZE: 82 contig_ptes = 1; 83 break; 84 case CONT_PMD_SIZE: 85 *pgsize = PMD_SIZE; 86 contig_ptes = CONT_PMDS; 87 break; 88 case CONT_PTE_SIZE: 89 *pgsize = PAGE_SIZE; 90 contig_ptes = CONT_PTES; 91 break; 92 } 93 94 return contig_ptes; 95 } 96 97 /* 98 * Changing some bits of contiguous entries requires us to follow a 99 * Break-Before-Make approach, breaking the whole contiguous set 100 * before we can change any entries. See ARM DDI 0487A.k_iss10775, 101 * "Misprogramming of the Contiguous bit", page D4-1762. 102 * 103 * This helper performs the break step. 104 */ 105 static pte_t get_clear_flush(struct mm_struct *mm, 106 unsigned long addr, 107 pte_t *ptep, 108 unsigned long pgsize, 109 unsigned long ncontig) 110 { 111 struct vm_area_struct vma; 112 pte_t orig_pte = huge_ptep_get(ptep); 113 bool valid = pte_valid(orig_pte); 114 unsigned long i, saddr = addr; 115 116 vma_init(&vma, mm); 117 118 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) { 119 pte_t pte = ptep_get_and_clear(mm, addr, ptep); 120 121 /* 122 * If HW_AFDBM is enabled, then the HW could turn on 123 * the dirty bit for any page in the set, so check 124 * them all. All hugetlb entries are already young. 125 */ 126 if (pte_dirty(pte)) 127 orig_pte = pte_mkdirty(orig_pte); 128 } 129 130 if (valid) 131 flush_tlb_range(&vma, saddr, addr); 132 return orig_pte; 133 } 134 135 /* 136 * Changing some bits of contiguous entries requires us to follow a 137 * Break-Before-Make approach, breaking the whole contiguous set 138 * before we can change any entries. See ARM DDI 0487A.k_iss10775, 139 * "Misprogramming of the Contiguous bit", page D4-1762. 140 * 141 * This helper performs the break step for use cases where the 142 * original pte is not needed. 143 */ 144 static void clear_flush(struct mm_struct *mm, 145 unsigned long addr, 146 pte_t *ptep, 147 unsigned long pgsize, 148 unsigned long ncontig) 149 { 150 struct vm_area_struct vma; 151 unsigned long i, saddr = addr; 152 153 vma_init(&vma, mm); 154 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) 155 pte_clear(mm, addr, ptep); 156 157 flush_tlb_range(&vma, saddr, addr); 158 } 159 160 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, 161 pte_t *ptep, pte_t pte) 162 { 163 size_t pgsize; 164 int i; 165 int ncontig; 166 unsigned long pfn, dpfn; 167 pgprot_t hugeprot; 168 169 /* 170 * Code needs to be expanded to handle huge swap and migration 171 * entries. Needed for HUGETLB and MEMORY_FAILURE. 172 */ 173 WARN_ON(!pte_present(pte)); 174 175 if (!pte_cont(pte)) { 176 set_pte_at(mm, addr, ptep, pte); 177 return; 178 } 179 180 ncontig = find_num_contig(mm, addr, ptep, &pgsize); 181 pfn = pte_pfn(pte); 182 dpfn = pgsize >> PAGE_SHIFT; 183 hugeprot = pte_pgprot(pte); 184 185 clear_flush(mm, addr, ptep, pgsize, ncontig); 186 187 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) 188 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot)); 189 } 190 191 void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr, 192 pte_t *ptep, pte_t pte, unsigned long sz) 193 { 194 int i, ncontig; 195 size_t pgsize; 196 197 ncontig = num_contig_ptes(sz, &pgsize); 198 199 for (i = 0; i < ncontig; i++, ptep++) 200 set_pte(ptep, pte); 201 } 202 203 pte_t *huge_pte_alloc(struct mm_struct *mm, 204 unsigned long addr, unsigned long sz) 205 { 206 pgd_t *pgdp; 207 pud_t *pudp; 208 pmd_t *pmdp; 209 pte_t *ptep = NULL; 210 211 pgdp = pgd_offset(mm, addr); 212 pudp = pud_alloc(mm, pgdp, addr); 213 if (!pudp) 214 return NULL; 215 216 if (sz == PUD_SIZE) { 217 ptep = (pte_t *)pudp; 218 } else if (sz == (PAGE_SIZE * CONT_PTES)) { 219 pmdp = pmd_alloc(mm, pudp, addr); 220 221 WARN_ON(addr & (sz - 1)); 222 /* 223 * Note that if this code were ever ported to the 224 * 32-bit arm platform then it will cause trouble in 225 * the case where CONFIG_HIGHPTE is set, since there 226 * will be no pte_unmap() to correspond with this 227 * pte_alloc_map(). 228 */ 229 ptep = pte_alloc_map(mm, pmdp, addr); 230 } else if (sz == PMD_SIZE) { 231 if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) && 232 pud_none(READ_ONCE(*pudp))) 233 ptep = huge_pmd_share(mm, addr, pudp); 234 else 235 ptep = (pte_t *)pmd_alloc(mm, pudp, addr); 236 } else if (sz == (PMD_SIZE * CONT_PMDS)) { 237 pmdp = pmd_alloc(mm, pudp, addr); 238 WARN_ON(addr & (sz - 1)); 239 return (pte_t *)pmdp; 240 } 241 242 return ptep; 243 } 244 245 pte_t *huge_pte_offset(struct mm_struct *mm, 246 unsigned long addr, unsigned long sz) 247 { 248 pgd_t *pgdp; 249 pud_t *pudp, pud; 250 pmd_t *pmdp, pmd; 251 252 pgdp = pgd_offset(mm, addr); 253 if (!pgd_present(READ_ONCE(*pgdp))) 254 return NULL; 255 256 pudp = pud_offset(pgdp, addr); 257 pud = READ_ONCE(*pudp); 258 if (sz != PUD_SIZE && pud_none(pud)) 259 return NULL; 260 /* hugepage or swap? */ 261 if (pud_huge(pud) || !pud_present(pud)) 262 return (pte_t *)pudp; 263 /* table; check the next level */ 264 265 if (sz == CONT_PMD_SIZE) 266 addr &= CONT_PMD_MASK; 267 268 pmdp = pmd_offset(pudp, addr); 269 pmd = READ_ONCE(*pmdp); 270 if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) && 271 pmd_none(pmd)) 272 return NULL; 273 if (pmd_huge(pmd) || !pmd_present(pmd)) 274 return (pte_t *)pmdp; 275 276 if (sz == CONT_PTE_SIZE) 277 return pte_offset_kernel(pmdp, (addr & CONT_PTE_MASK)); 278 279 return NULL; 280 } 281 282 pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma, 283 struct page *page, int writable) 284 { 285 size_t pagesize = huge_page_size(hstate_vma(vma)); 286 287 if (pagesize == CONT_PTE_SIZE) { 288 entry = pte_mkcont(entry); 289 } else if (pagesize == CONT_PMD_SIZE) { 290 entry = pmd_pte(pmd_mkcont(pte_pmd(entry))); 291 } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) { 292 pr_warn("%s: unrecognized huge page size 0x%lx\n", 293 __func__, pagesize); 294 } 295 return entry; 296 } 297 298 void huge_pte_clear(struct mm_struct *mm, unsigned long addr, 299 pte_t *ptep, unsigned long sz) 300 { 301 int i, ncontig; 302 size_t pgsize; 303 304 ncontig = num_contig_ptes(sz, &pgsize); 305 306 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) 307 pte_clear(mm, addr, ptep); 308 } 309 310 pte_t huge_ptep_get_and_clear(struct mm_struct *mm, 311 unsigned long addr, pte_t *ptep) 312 { 313 int ncontig; 314 size_t pgsize; 315 pte_t orig_pte = huge_ptep_get(ptep); 316 317 if (!pte_cont(orig_pte)) 318 return ptep_get_and_clear(mm, addr, ptep); 319 320 ncontig = find_num_contig(mm, addr, ptep, &pgsize); 321 322 return get_clear_flush(mm, addr, ptep, pgsize, ncontig); 323 } 324 325 int huge_ptep_set_access_flags(struct vm_area_struct *vma, 326 unsigned long addr, pte_t *ptep, 327 pte_t pte, int dirty) 328 { 329 int ncontig, i, changed = 0; 330 size_t pgsize = 0; 331 unsigned long pfn = pte_pfn(pte), dpfn; 332 pgprot_t hugeprot; 333 pte_t orig_pte; 334 335 if (!pte_cont(pte)) 336 return ptep_set_access_flags(vma, addr, ptep, pte, dirty); 337 338 ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize); 339 dpfn = pgsize >> PAGE_SHIFT; 340 341 orig_pte = get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig); 342 if (!pte_same(orig_pte, pte)) 343 changed = 1; 344 345 /* Make sure we don't lose the dirty state */ 346 if (pte_dirty(orig_pte)) 347 pte = pte_mkdirty(pte); 348 349 hugeprot = pte_pgprot(pte); 350 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) 351 set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot)); 352 353 return changed; 354 } 355 356 void huge_ptep_set_wrprotect(struct mm_struct *mm, 357 unsigned long addr, pte_t *ptep) 358 { 359 unsigned long pfn, dpfn; 360 pgprot_t hugeprot; 361 int ncontig, i; 362 size_t pgsize; 363 pte_t pte; 364 365 if (!pte_cont(READ_ONCE(*ptep))) { 366 ptep_set_wrprotect(mm, addr, ptep); 367 return; 368 } 369 370 ncontig = find_num_contig(mm, addr, ptep, &pgsize); 371 dpfn = pgsize >> PAGE_SHIFT; 372 373 pte = get_clear_flush(mm, addr, ptep, pgsize, ncontig); 374 pte = pte_wrprotect(pte); 375 376 hugeprot = pte_pgprot(pte); 377 pfn = pte_pfn(pte); 378 379 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) 380 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot)); 381 } 382 383 void huge_ptep_clear_flush(struct vm_area_struct *vma, 384 unsigned long addr, pte_t *ptep) 385 { 386 size_t pgsize; 387 int ncontig; 388 389 if (!pte_cont(READ_ONCE(*ptep))) { 390 ptep_clear_flush(vma, addr, ptep); 391 return; 392 } 393 394 ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize); 395 clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig); 396 } 397 398 static __init int setup_hugepagesz(char *opt) 399 { 400 unsigned long ps = memparse(opt, &opt); 401 402 switch (ps) { 403 #ifdef CONFIG_ARM64_4K_PAGES 404 case PUD_SIZE: 405 #endif 406 case PMD_SIZE * CONT_PMDS: 407 case PMD_SIZE: 408 case PAGE_SIZE * CONT_PTES: 409 hugetlb_add_hstate(ilog2(ps) - PAGE_SHIFT); 410 return 1; 411 } 412 413 hugetlb_bad_size(); 414 pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10); 415 return 0; 416 } 417 __setup("hugepagesz=", setup_hugepagesz); 418 419 #ifdef CONFIG_ARM64_64K_PAGES 420 static __init int add_default_hugepagesz(void) 421 { 422 if (size_to_hstate(CONT_PTES * PAGE_SIZE) == NULL) 423 hugetlb_add_hstate(CONT_PTE_SHIFT); 424 return 0; 425 } 426 arch_initcall(add_default_hugepagesz); 427 #endif 428