1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm64/mm/hugetlbpage.c
5 * Copyright (C) 2013 Linaro Ltd.
7 * Based on arch/x86/mm/hugetlbpage.c.
10 #include <linux/init.h>
13 #include <linux/hugetlb.h>
14 #include <linux/pagemap.h>
15 #include <linux/err.h>
16 #include <linux/sysctl.h>
19 #include <asm/tlbflush.h>
20 #include <asm/pgalloc.h>
22 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
23 bool arch_hugetlb_migration_supported(struct hstate
*h
)
25 size_t pagesize
= huge_page_size(h
);
28 #ifdef CONFIG_ARM64_4K_PAGES
36 pr_warn("%s: unrecognized huge page size 0x%lx\n",
42 int pmd_huge(pmd_t pmd
)
44 return pmd_val(pmd
) && !(pmd_val(pmd
) & PMD_TABLE_BIT
);
47 int pud_huge(pud_t pud
)
49 #ifndef __PAGETABLE_PMD_FOLDED
50 return pud_val(pud
) && !(pud_val(pud
) & PUD_TABLE_BIT
);
57 * Select all bits except the pfn
59 static inline pgprot_t
pte_pgprot(pte_t pte
)
61 unsigned long pfn
= pte_pfn(pte
);
63 return __pgprot(pte_val(pfn_pte(pfn
, __pgprot(0))) ^ pte_val(pte
));
66 static int find_num_contig(struct mm_struct
*mm
, unsigned long addr
,
67 pte_t
*ptep
, size_t *pgsize
)
69 pgd_t
*pgdp
= pgd_offset(mm
, addr
);
74 pudp
= pud_offset(pgdp
, addr
);
75 pmdp
= pmd_offset(pudp
, addr
);
76 if ((pte_t
*)pmdp
== ptep
) {
83 static inline int num_contig_ptes(unsigned long size
, size_t *pgsize
)
90 #ifdef CONFIG_ARM64_4K_PAGES
98 contig_ptes
= CONT_PMDS
;
102 contig_ptes
= CONT_PTES
;
110 * Changing some bits of contiguous entries requires us to follow a
111 * Break-Before-Make approach, breaking the whole contiguous set
112 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
113 * "Misprogramming of the Contiguous bit", page D4-1762.
115 * This helper performs the break step.
117 static pte_t
get_clear_flush(struct mm_struct
*mm
,
120 unsigned long pgsize
,
121 unsigned long ncontig
)
123 pte_t orig_pte
= huge_ptep_get(ptep
);
124 bool valid
= pte_valid(orig_pte
);
125 unsigned long i
, saddr
= addr
;
127 for (i
= 0; i
< ncontig
; i
++, addr
+= pgsize
, ptep
++) {
128 pte_t pte
= ptep_get_and_clear(mm
, addr
, ptep
);
131 * If HW_AFDBM is enabled, then the HW could turn on
132 * the dirty or accessed bit for any page in the set,
136 orig_pte
= pte_mkdirty(orig_pte
);
139 orig_pte
= pte_mkyoung(orig_pte
);
143 struct vm_area_struct vma
= TLB_FLUSH_VMA(mm
, 0);
144 flush_tlb_range(&vma
, saddr
, addr
);
150 * Changing some bits of contiguous entries requires us to follow a
151 * Break-Before-Make approach, breaking the whole contiguous set
152 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
153 * "Misprogramming of the Contiguous bit", page D4-1762.
155 * This helper performs the break step for use cases where the
156 * original pte is not needed.
158 static void clear_flush(struct mm_struct
*mm
,
161 unsigned long pgsize
,
162 unsigned long ncontig
)
164 struct vm_area_struct vma
= TLB_FLUSH_VMA(mm
, 0);
165 unsigned long i
, saddr
= addr
;
167 for (i
= 0; i
< ncontig
; i
++, addr
+= pgsize
, ptep
++)
168 pte_clear(mm
, addr
, ptep
);
170 flush_tlb_range(&vma
, saddr
, addr
);
173 void set_huge_pte_at(struct mm_struct
*mm
, unsigned long addr
,
174 pte_t
*ptep
, pte_t pte
)
179 unsigned long pfn
, dpfn
;
183 * Code needs to be expanded to handle huge swap and migration
184 * entries. Needed for HUGETLB and MEMORY_FAILURE.
186 WARN_ON(!pte_present(pte
));
188 if (!pte_cont(pte
)) {
189 set_pte_at(mm
, addr
, ptep
, pte
);
193 ncontig
= find_num_contig(mm
, addr
, ptep
, &pgsize
);
195 dpfn
= pgsize
>> PAGE_SHIFT
;
196 hugeprot
= pte_pgprot(pte
);
198 clear_flush(mm
, addr
, ptep
, pgsize
, ncontig
);
200 for (i
= 0; i
< ncontig
; i
++, ptep
++, addr
+= pgsize
, pfn
+= dpfn
)
201 set_pte_at(mm
, addr
, ptep
, pfn_pte(pfn
, hugeprot
));
204 void set_huge_swap_pte_at(struct mm_struct
*mm
, unsigned long addr
,
205 pte_t
*ptep
, pte_t pte
, unsigned long sz
)
210 ncontig
= num_contig_ptes(sz
, &pgsize
);
212 for (i
= 0; i
< ncontig
; i
++, ptep
++)
216 pte_t
*huge_pte_alloc(struct mm_struct
*mm
,
217 unsigned long addr
, unsigned long sz
)
224 pgdp
= pgd_offset(mm
, addr
);
225 pudp
= pud_alloc(mm
, pgdp
, addr
);
229 if (sz
== PUD_SIZE
) {
230 ptep
= (pte_t
*)pudp
;
231 } else if (sz
== (CONT_PTE_SIZE
)) {
232 pmdp
= pmd_alloc(mm
, pudp
, addr
);
234 WARN_ON(addr
& (sz
- 1));
236 * Note that if this code were ever ported to the
237 * 32-bit arm platform then it will cause trouble in
238 * the case where CONFIG_HIGHPTE is set, since there
239 * will be no pte_unmap() to correspond with this
242 ptep
= pte_alloc_map(mm
, pmdp
, addr
);
243 } else if (sz
== PMD_SIZE
) {
244 if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE
) &&
245 pud_none(READ_ONCE(*pudp
)))
246 ptep
= huge_pmd_share(mm
, addr
, pudp
);
248 ptep
= (pte_t
*)pmd_alloc(mm
, pudp
, addr
);
249 } else if (sz
== (CONT_PMD_SIZE
)) {
250 pmdp
= pmd_alloc(mm
, pudp
, addr
);
251 WARN_ON(addr
& (sz
- 1));
252 return (pte_t
*)pmdp
;
258 pte_t
*huge_pte_offset(struct mm_struct
*mm
,
259 unsigned long addr
, unsigned long sz
)
265 pgdp
= pgd_offset(mm
, addr
);
266 if (!pgd_present(READ_ONCE(*pgdp
)))
269 pudp
= pud_offset(pgdp
, addr
);
270 pud
= READ_ONCE(*pudp
);
271 if (sz
!= PUD_SIZE
&& pud_none(pud
))
273 /* hugepage or swap? */
274 if (pud_huge(pud
) || !pud_present(pud
))
275 return (pte_t
*)pudp
;
276 /* table; check the next level */
278 if (sz
== CONT_PMD_SIZE
)
279 addr
&= CONT_PMD_MASK
;
281 pmdp
= pmd_offset(pudp
, addr
);
282 pmd
= READ_ONCE(*pmdp
);
283 if (!(sz
== PMD_SIZE
|| sz
== CONT_PMD_SIZE
) &&
286 if (pmd_huge(pmd
) || !pmd_present(pmd
))
287 return (pte_t
*)pmdp
;
289 if (sz
== CONT_PTE_SIZE
)
290 return pte_offset_kernel(pmdp
, (addr
& CONT_PTE_MASK
));
295 pte_t
arch_make_huge_pte(pte_t entry
, struct vm_area_struct
*vma
,
296 struct page
*page
, int writable
)
298 size_t pagesize
= huge_page_size(hstate_vma(vma
));
300 if (pagesize
== CONT_PTE_SIZE
) {
301 entry
= pte_mkcont(entry
);
302 } else if (pagesize
== CONT_PMD_SIZE
) {
303 entry
= pmd_pte(pmd_mkcont(pte_pmd(entry
)));
304 } else if (pagesize
!= PUD_SIZE
&& pagesize
!= PMD_SIZE
) {
305 pr_warn("%s: unrecognized huge page size 0x%lx\n",
311 void huge_pte_clear(struct mm_struct
*mm
, unsigned long addr
,
312 pte_t
*ptep
, unsigned long sz
)
317 ncontig
= num_contig_ptes(sz
, &pgsize
);
319 for (i
= 0; i
< ncontig
; i
++, addr
+= pgsize
, ptep
++)
320 pte_clear(mm
, addr
, ptep
);
323 pte_t
huge_ptep_get_and_clear(struct mm_struct
*mm
,
324 unsigned long addr
, pte_t
*ptep
)
328 pte_t orig_pte
= huge_ptep_get(ptep
);
330 if (!pte_cont(orig_pte
))
331 return ptep_get_and_clear(mm
, addr
, ptep
);
333 ncontig
= find_num_contig(mm
, addr
, ptep
, &pgsize
);
335 return get_clear_flush(mm
, addr
, ptep
, pgsize
, ncontig
);
339 * huge_ptep_set_access_flags will update access flags (dirty, accesssed)
340 * and write permission.
342 * For a contiguous huge pte range we need to check whether or not write
343 * permission has to change only on the first pte in the set. Then for
344 * all the contiguous ptes we need to check whether or not there is a
345 * discrepancy between dirty or young.
347 static int __cont_access_flags_changed(pte_t
*ptep
, pte_t pte
, int ncontig
)
351 if (pte_write(pte
) != pte_write(huge_ptep_get(ptep
)))
354 for (i
= 0; i
< ncontig
; i
++) {
355 pte_t orig_pte
= huge_ptep_get(ptep
+ i
);
357 if (pte_dirty(pte
) != pte_dirty(orig_pte
))
360 if (pte_young(pte
) != pte_young(orig_pte
))
367 int huge_ptep_set_access_flags(struct vm_area_struct
*vma
,
368 unsigned long addr
, pte_t
*ptep
,
369 pte_t pte
, int dirty
)
373 unsigned long pfn
= pte_pfn(pte
), dpfn
;
378 return ptep_set_access_flags(vma
, addr
, ptep
, pte
, dirty
);
380 ncontig
= find_num_contig(vma
->vm_mm
, addr
, ptep
, &pgsize
);
381 dpfn
= pgsize
>> PAGE_SHIFT
;
383 if (!__cont_access_flags_changed(ptep
, pte
, ncontig
))
386 orig_pte
= get_clear_flush(vma
->vm_mm
, addr
, ptep
, pgsize
, ncontig
);
388 /* Make sure we don't lose the dirty or young state */
389 if (pte_dirty(orig_pte
))
390 pte
= pte_mkdirty(pte
);
392 if (pte_young(orig_pte
))
393 pte
= pte_mkyoung(pte
);
395 hugeprot
= pte_pgprot(pte
);
396 for (i
= 0; i
< ncontig
; i
++, ptep
++, addr
+= pgsize
, pfn
+= dpfn
)
397 set_pte_at(vma
->vm_mm
, addr
, ptep
, pfn_pte(pfn
, hugeprot
));
402 void huge_ptep_set_wrprotect(struct mm_struct
*mm
,
403 unsigned long addr
, pte_t
*ptep
)
405 unsigned long pfn
, dpfn
;
411 if (!pte_cont(READ_ONCE(*ptep
))) {
412 ptep_set_wrprotect(mm
, addr
, ptep
);
416 ncontig
= find_num_contig(mm
, addr
, ptep
, &pgsize
);
417 dpfn
= pgsize
>> PAGE_SHIFT
;
419 pte
= get_clear_flush(mm
, addr
, ptep
, pgsize
, ncontig
);
420 pte
= pte_wrprotect(pte
);
422 hugeprot
= pte_pgprot(pte
);
425 for (i
= 0; i
< ncontig
; i
++, ptep
++, addr
+= pgsize
, pfn
+= dpfn
)
426 set_pte_at(mm
, addr
, ptep
, pfn_pte(pfn
, hugeprot
));
429 void huge_ptep_clear_flush(struct vm_area_struct
*vma
,
430 unsigned long addr
, pte_t
*ptep
)
435 if (!pte_cont(READ_ONCE(*ptep
))) {
436 ptep_clear_flush(vma
, addr
, ptep
);
440 ncontig
= find_num_contig(vma
->vm_mm
, addr
, ptep
, &pgsize
);
441 clear_flush(vma
->vm_mm
, addr
, ptep
, pgsize
, ncontig
);
444 static void __init
add_huge_page_size(unsigned long size
)
446 if (size_to_hstate(size
))
449 hugetlb_add_hstate(ilog2(size
) - PAGE_SHIFT
);
452 static int __init
hugetlbpage_init(void)
454 #ifdef CONFIG_ARM64_4K_PAGES
455 add_huge_page_size(PUD_SIZE
);
457 add_huge_page_size(CONT_PMD_SIZE
);
458 add_huge_page_size(PMD_SIZE
);
459 add_huge_page_size(CONT_PTE_SIZE
);
463 arch_initcall(hugetlbpage_init
);
465 static __init
int setup_hugepagesz(char *opt
)
467 unsigned long ps
= memparse(opt
, &opt
);
470 #ifdef CONFIG_ARM64_4K_PAGES
476 add_huge_page_size(ps
);
481 pr_err("hugepagesz: Unsupported page size %lu K\n", ps
>> 10);
484 __setup("hugepagesz=", setup_hugepagesz
);