2 * arch/arm64/mm/hugetlbpage.c
4 * Copyright (C) 2013 Linaro Ltd.
6 * Based on arch/x86/mm/hugetlbpage.c.
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.
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.
18 #include <linux/init.h>
21 #include <linux/hugetlb.h>
22 #include <linux/pagemap.h>
23 #include <linux/err.h>
24 #include <linux/sysctl.h>
27 #include <asm/tlbflush.h>
28 #include <asm/pgalloc.h>
30 int pmd_huge(pmd_t pmd)
32 return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT);
35 int pud_huge(pud_t pud)
37 #ifndef __PAGETABLE_PMD_FOLDED
38 return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT);
45 * Select all bits except the pfn
47 static inline pgprot_t pte_pgprot(pte_t pte)
49 unsigned long pfn = pte_pfn(pte);
51 return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
54 static int find_num_contig(struct mm_struct *mm, unsigned long addr,
55 pte_t *ptep, size_t *pgsize)
57 pgd_t *pgdp = pgd_offset(mm, addr);
62 pudp = pud_offset(pgdp, addr);
63 pmdp = pmd_offset(pudp, addr);
64 if ((pte_t *)pmdp == ptep) {
71 static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
78 #ifdef CONFIG_ARM64_4K_PAGES
86 contig_ptes = CONT_PMDS;
90 contig_ptes = CONT_PTES;
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.
103 * This helper performs the break step.
105 static pte_t get_clear_flush(struct mm_struct *mm,
108 unsigned long pgsize,
109 unsigned long ncontig)
111 pte_t orig_pte = huge_ptep_get(ptep);
112 bool valid = pte_valid(orig_pte);
113 unsigned long i, saddr = addr;
115 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
116 pte_t pte = ptep_get_and_clear(mm, addr, ptep);
119 * If HW_AFDBM is enabled, then the HW could turn on
120 * the dirty or accessed bit for any page in the set,
124 orig_pte = pte_mkdirty(orig_pte);
127 orig_pte = pte_mkyoung(orig_pte);
131 struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
132 flush_tlb_range(&vma, saddr, addr);
138 * Changing some bits of contiguous entries requires us to follow a
139 * Break-Before-Make approach, breaking the whole contiguous set
140 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
141 * "Misprogramming of the Contiguous bit", page D4-1762.
143 * This helper performs the break step for use cases where the
144 * original pte is not needed.
146 static void clear_flush(struct mm_struct *mm,
149 unsigned long pgsize,
150 unsigned long ncontig)
152 struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
153 unsigned long i, saddr = addr;
155 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
156 pte_clear(mm, addr, ptep);
158 flush_tlb_range(&vma, saddr, addr);
161 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
162 pte_t *ptep, pte_t pte)
167 unsigned long pfn, dpfn;
171 * Code needs to be expanded to handle huge swap and migration
172 * entries. Needed for HUGETLB and MEMORY_FAILURE.
174 WARN_ON(!pte_present(pte));
176 if (!pte_cont(pte)) {
177 set_pte_at(mm, addr, ptep, pte);
181 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
183 dpfn = pgsize >> PAGE_SHIFT;
184 hugeprot = pte_pgprot(pte);
186 clear_flush(mm, addr, ptep, pgsize, ncontig);
188 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
189 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
192 void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
193 pte_t *ptep, pte_t pte, unsigned long sz)
198 ncontig = num_contig_ptes(sz, &pgsize);
200 for (i = 0; i < ncontig; i++, ptep++)
204 pte_t *huge_pte_alloc(struct mm_struct *mm,
205 unsigned long addr, unsigned long sz)
212 pgdp = pgd_offset(mm, addr);
213 pudp = pud_alloc(mm, pgdp, addr);
217 if (sz == PUD_SIZE) {
218 ptep = (pte_t *)pudp;
219 } else if (sz == (PAGE_SIZE * CONT_PTES)) {
220 pmdp = pmd_alloc(mm, pudp, addr);
224 WARN_ON(addr & (sz - 1));
226 * Note that if this code were ever ported to the
227 * 32-bit arm platform then it will cause trouble in
228 * the case where CONFIG_HIGHPTE is set, since there
229 * will be no pte_unmap() to correspond with this
232 ptep = pte_alloc_map(mm, pmdp, addr);
233 } else if (sz == PMD_SIZE) {
234 if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
235 pud_none(READ_ONCE(*pudp)))
236 ptep = huge_pmd_share(mm, addr, pudp);
238 ptep = (pte_t *)pmd_alloc(mm, pudp, addr);
239 } else if (sz == (PMD_SIZE * CONT_PMDS)) {
240 pmdp = pmd_alloc(mm, pudp, addr);
241 WARN_ON(addr & (sz - 1));
242 return (pte_t *)pmdp;
248 pte_t *huge_pte_offset(struct mm_struct *mm,
249 unsigned long addr, unsigned long sz)
255 pgdp = pgd_offset(mm, addr);
256 if (!pgd_present(READ_ONCE(*pgdp)))
259 pudp = pud_offset(pgdp, addr);
260 pud = READ_ONCE(*pudp);
261 if (sz != PUD_SIZE && pud_none(pud))
263 /* hugepage or swap? */
264 if (pud_huge(pud) || !pud_present(pud))
265 return (pte_t *)pudp;
266 /* table; check the next level */
268 if (sz == CONT_PMD_SIZE)
269 addr &= CONT_PMD_MASK;
271 pmdp = pmd_offset(pudp, addr);
272 pmd = READ_ONCE(*pmdp);
273 if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
276 if (pmd_huge(pmd) || !pmd_present(pmd))
277 return (pte_t *)pmdp;
279 if (sz == CONT_PTE_SIZE)
280 return pte_offset_kernel(pmdp, (addr & CONT_PTE_MASK));
285 pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
286 struct page *page, int writable)
288 size_t pagesize = huge_page_size(hstate_vma(vma));
290 if (pagesize == CONT_PTE_SIZE) {
291 entry = pte_mkcont(entry);
292 } else if (pagesize == CONT_PMD_SIZE) {
293 entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
294 } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
295 pr_warn("%s: unrecognized huge page size 0x%lx\n",
301 void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
302 pte_t *ptep, unsigned long sz)
307 ncontig = num_contig_ptes(sz, &pgsize);
309 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
310 pte_clear(mm, addr, ptep);
313 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
314 unsigned long addr, pte_t *ptep)
318 pte_t orig_pte = huge_ptep_get(ptep);
320 if (!pte_cont(orig_pte))
321 return ptep_get_and_clear(mm, addr, ptep);
323 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
325 return get_clear_flush(mm, addr, ptep, pgsize, ncontig);
329 * huge_ptep_set_access_flags will update access flags (dirty, accesssed)
330 * and write permission.
332 * For a contiguous huge pte range we need to check whether or not write
333 * permission has to change only on the first pte in the set. Then for
334 * all the contiguous ptes we need to check whether or not there is a
335 * discrepancy between dirty or young.
337 static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig)
341 if (pte_write(pte) != pte_write(huge_ptep_get(ptep)))
344 for (i = 0; i < ncontig; i++) {
345 pte_t orig_pte = huge_ptep_get(ptep + i);
347 if (pte_dirty(pte) != pte_dirty(orig_pte))
350 if (pte_young(pte) != pte_young(orig_pte))
357 int huge_ptep_set_access_flags(struct vm_area_struct *vma,
358 unsigned long addr, pte_t *ptep,
359 pte_t pte, int dirty)
363 unsigned long pfn = pte_pfn(pte), dpfn;
368 return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
370 ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
371 dpfn = pgsize >> PAGE_SHIFT;
373 if (!__cont_access_flags_changed(ptep, pte, ncontig))
376 orig_pte = get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
378 /* Make sure we don't lose the dirty or young state */
379 if (pte_dirty(orig_pte))
380 pte = pte_mkdirty(pte);
382 if (pte_young(orig_pte))
383 pte = pte_mkyoung(pte);
385 hugeprot = pte_pgprot(pte);
386 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
387 set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
392 void huge_ptep_set_wrprotect(struct mm_struct *mm,
393 unsigned long addr, pte_t *ptep)
395 unsigned long pfn, dpfn;
401 if (!pte_cont(READ_ONCE(*ptep))) {
402 ptep_set_wrprotect(mm, addr, ptep);
406 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
407 dpfn = pgsize >> PAGE_SHIFT;
409 pte = get_clear_flush(mm, addr, ptep, pgsize, ncontig);
410 pte = pte_wrprotect(pte);
412 hugeprot = pte_pgprot(pte);
415 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
416 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
419 void huge_ptep_clear_flush(struct vm_area_struct *vma,
420 unsigned long addr, pte_t *ptep)
425 if (!pte_cont(READ_ONCE(*ptep))) {
426 ptep_clear_flush(vma, addr, ptep);
430 ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
431 clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
434 static __init int setup_hugepagesz(char *opt)
436 unsigned long ps = memparse(opt, &opt);
439 #ifdef CONFIG_ARM64_4K_PAGES
442 case PMD_SIZE * CONT_PMDS:
444 case PAGE_SIZE * CONT_PTES:
445 hugetlb_add_hstate(ilog2(ps) - PAGE_SHIFT);
450 pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
453 __setup("hugepagesz=", setup_hugepagesz);
455 #ifdef CONFIG_ARM64_64K_PAGES
456 static __init int add_default_hugepagesz(void)
458 if (size_to_hstate(CONT_PTES * PAGE_SIZE) == NULL)
459 hugetlb_add_hstate(CONT_PTE_SHIFT);
462 arch_initcall(add_default_hugepagesz);