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 *pgd = pgd_offset(mm, addr);
62 pud = pud_offset(pgd, addr);
63 pmd = pmd_offset(pud, addr);
64 if ((pte_t *)pmd == 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 struct vm_area_struct vma = { .vm_mm = mm };
112 pte_t orig_pte = huge_ptep_get(ptep);
113 bool valid = pte_valid(orig_pte);
114 unsigned long i, saddr = addr;
116 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
117 pte_t pte = ptep_get_and_clear(mm, addr, ptep);
120 * If HW_AFDBM is enabled, then the HW could turn on
121 * the dirty bit for any page in the set, so check
122 * them all. All hugetlb entries are already young.
125 orig_pte = pte_mkdirty(orig_pte);
129 flush_tlb_range(&vma, saddr, addr);
134 * Changing some bits of contiguous entries requires us to follow a
135 * Break-Before-Make approach, breaking the whole contiguous set
136 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
137 * "Misprogramming of the Contiguous bit", page D4-1762.
139 * This helper performs the break step for use cases where the
140 * original pte is not needed.
142 static void clear_flush(struct mm_struct *mm,
145 unsigned long pgsize,
146 unsigned long ncontig)
148 struct vm_area_struct vma = { .vm_mm = mm };
149 unsigned long i, saddr = addr;
151 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
152 pte_clear(mm, addr, ptep);
154 flush_tlb_range(&vma, saddr, addr);
157 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
158 pte_t *ptep, pte_t pte)
163 unsigned long pfn, dpfn;
167 * Code needs to be expanded to handle huge swap and migration
168 * entries. Needed for HUGETLB and MEMORY_FAILURE.
170 WARN_ON(!pte_present(pte));
172 if (!pte_cont(pte)) {
173 set_pte_at(mm, addr, ptep, pte);
177 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
179 dpfn = pgsize >> PAGE_SHIFT;
180 hugeprot = pte_pgprot(pte);
182 clear_flush(mm, addr, ptep, pgsize, ncontig);
184 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) {
185 pr_debug("%s: set pte %p to 0x%llx\n", __func__, ptep,
186 pte_val(pfn_pte(pfn, hugeprot)));
187 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
191 void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
192 pte_t *ptep, pte_t pte, unsigned long sz)
197 ncontig = num_contig_ptes(sz, &pgsize);
199 for (i = 0; i < ncontig; i++, ptep++)
203 pte_t *huge_pte_alloc(struct mm_struct *mm,
204 unsigned long addr, unsigned long sz)
210 pr_debug("%s: addr:0x%lx sz:0x%lx\n", __func__, addr, sz);
211 pgd = pgd_offset(mm, addr);
212 pud = pud_alloc(mm, pgd, addr);
216 if (sz == PUD_SIZE) {
218 } else if (sz == (PAGE_SIZE * CONT_PTES)) {
219 pmd_t *pmd = pmd_alloc(mm, pud, addr);
221 WARN_ON(addr & (sz - 1));
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
229 pte = pte_alloc_map(mm, pmd, addr);
230 } else if (sz == PMD_SIZE) {
231 if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
233 pte = huge_pmd_share(mm, addr, pud);
235 pte = (pte_t *)pmd_alloc(mm, pud, addr);
236 } else if (sz == (PMD_SIZE * CONT_PMDS)) {
239 pmd = pmd_alloc(mm, pud, addr);
240 WARN_ON(addr & (sz - 1));
244 pr_debug("%s: addr:0x%lx sz:0x%lx ret pte=%p/0x%llx\n", __func__, addr,
245 sz, pte, pte_val(*pte));
249 pte_t *huge_pte_offset(struct mm_struct *mm,
250 unsigned long addr, unsigned long sz)
256 pgd = pgd_offset(mm, addr);
257 pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
258 if (!pgd_present(*pgd))
261 pud = pud_offset(pgd, addr);
262 if (sz != PUD_SIZE && pud_none(*pud))
264 /* hugepage or swap? */
265 if (pud_huge(*pud) || !pud_present(*pud))
267 /* table; check the next level */
269 if (sz == CONT_PMD_SIZE)
270 addr &= CONT_PMD_MASK;
272 pmd = pmd_offset(pud, addr);
273 if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
276 if (pmd_huge(*pmd) || !pmd_present(*pmd))
279 if (sz == CONT_PTE_SIZE) {
280 pte_t *pte = pte_offset_kernel(pmd, (addr & CONT_PTE_MASK));
287 pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
288 struct page *page, int writable)
290 size_t pagesize = huge_page_size(hstate_vma(vma));
292 if (pagesize == CONT_PTE_SIZE) {
293 entry = pte_mkcont(entry);
294 } else if (pagesize == CONT_PMD_SIZE) {
295 entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
296 } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
297 pr_warn("%s: unrecognized huge page size 0x%lx\n",
303 void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
304 pte_t *ptep, unsigned long sz)
309 ncontig = num_contig_ptes(sz, &pgsize);
311 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
312 pte_clear(mm, addr, ptep);
315 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
316 unsigned long addr, pte_t *ptep)
320 pte_t orig_pte = huge_ptep_get(ptep);
322 if (!pte_cont(orig_pte))
323 return ptep_get_and_clear(mm, addr, ptep);
325 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
327 return get_clear_flush(mm, addr, ptep, pgsize, ncontig);
330 int huge_ptep_set_access_flags(struct vm_area_struct *vma,
331 unsigned long addr, pte_t *ptep,
332 pte_t pte, int dirty)
334 int ncontig, i, changed = 0;
336 unsigned long pfn = pte_pfn(pte), dpfn;
341 return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
343 ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
344 dpfn = pgsize >> PAGE_SHIFT;
346 orig_pte = get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
347 if (!pte_same(orig_pte, pte))
350 /* Make sure we don't lose the dirty state */
351 if (pte_dirty(orig_pte))
352 pte = pte_mkdirty(pte);
354 hugeprot = pte_pgprot(pte);
355 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
356 set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
361 void huge_ptep_set_wrprotect(struct mm_struct *mm,
362 unsigned long addr, pte_t *ptep)
364 unsigned long pfn, dpfn;
370 if (!pte_cont(*ptep)) {
371 ptep_set_wrprotect(mm, addr, ptep);
375 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
376 dpfn = pgsize >> PAGE_SHIFT;
378 pte = get_clear_flush(mm, addr, ptep, pgsize, ncontig);
379 pte = pte_wrprotect(pte);
381 hugeprot = pte_pgprot(pte);
384 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
385 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
388 void huge_ptep_clear_flush(struct vm_area_struct *vma,
389 unsigned long addr, pte_t *ptep)
394 if (!pte_cont(*ptep)) {
395 ptep_clear_flush(vma, addr, ptep);
399 ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
400 clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
403 static __init int setup_hugepagesz(char *opt)
405 unsigned long ps = memparse(opt, &opt);
408 #ifdef CONFIG_ARM64_4K_PAGES
411 case PMD_SIZE * CONT_PMDS:
413 case PAGE_SIZE * CONT_PTES:
414 hugetlb_add_hstate(ilog2(ps) - PAGE_SHIFT);
419 pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
422 __setup("hugepagesz=", setup_hugepagesz);
424 #ifdef CONFIG_ARM64_64K_PAGES
425 static __init int add_default_hugepagesz(void)
427 if (size_to_hstate(CONT_PTES * PAGE_SIZE) == NULL)
428 hugetlb_add_hstate(CONT_PTE_SHIFT);
431 arch_initcall(add_default_hugepagesz);