1 // SPDX-License-Identifier: GPL-2.0
3 * SPARC64 Huge TLB page support.
5 * Copyright (C) 2002, 2003, 2006 David S. Miller (davem@davemloft.net)
10 #include <linux/sched/mm.h>
11 #include <linux/hugetlb.h>
12 #include <linux/pagemap.h>
13 #include <linux/sysctl.h>
16 #include <asm/pgalloc.h>
17 #include <asm/pgtable.h>
19 #include <asm/tlbflush.h>
20 #include <asm/cacheflush.h>
21 #include <asm/mmu_context.h>
23 /* Slightly simplified from the non-hugepage variant because by
24 * definition we don't have to worry about any page coloring stuff
27 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file
*filp
,
33 struct hstate
*h
= hstate_file(filp
);
34 unsigned long task_size
= TASK_SIZE
;
35 struct vm_unmapped_area_info info
;
37 if (test_thread_flag(TIF_32BIT
))
38 task_size
= STACK_TOP32
;
42 info
.low_limit
= TASK_UNMAPPED_BASE
;
43 info
.high_limit
= min(task_size
, VA_EXCLUDE_START
);
44 info
.align_mask
= PAGE_MASK
& ~huge_page_mask(h
);
45 info
.align_offset
= 0;
46 addr
= vm_unmapped_area(&info
);
48 if ((addr
& ~PAGE_MASK
) && task_size
> VA_EXCLUDE_END
) {
49 VM_BUG_ON(addr
!= -ENOMEM
);
50 info
.low_limit
= VA_EXCLUDE_END
;
51 info
.high_limit
= task_size
;
52 addr
= vm_unmapped_area(&info
);
59 hugetlb_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
60 const unsigned long len
,
61 const unsigned long pgoff
,
62 const unsigned long flags
)
64 struct hstate
*h
= hstate_file(filp
);
65 struct mm_struct
*mm
= current
->mm
;
66 unsigned long addr
= addr0
;
67 struct vm_unmapped_area_info info
;
69 /* This should only ever run for 32-bit processes. */
70 BUG_ON(!test_thread_flag(TIF_32BIT
));
72 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
74 info
.low_limit
= PAGE_SIZE
;
75 info
.high_limit
= mm
->mmap_base
;
76 info
.align_mask
= PAGE_MASK
& ~huge_page_mask(h
);
77 info
.align_offset
= 0;
78 addr
= vm_unmapped_area(&info
);
81 * A failed mmap() very likely causes application failure,
82 * so fall back to the bottom-up function here. This scenario
83 * can happen with large stack limits and large mmap()
86 if (addr
& ~PAGE_MASK
) {
87 VM_BUG_ON(addr
!= -ENOMEM
);
89 info
.low_limit
= TASK_UNMAPPED_BASE
;
90 info
.high_limit
= STACK_TOP32
;
91 addr
= vm_unmapped_area(&info
);
98 hugetlb_get_unmapped_area(struct file
*file
, unsigned long addr
,
99 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
101 struct hstate
*h
= hstate_file(file
);
102 struct mm_struct
*mm
= current
->mm
;
103 struct vm_area_struct
*vma
;
104 unsigned long task_size
= TASK_SIZE
;
106 if (test_thread_flag(TIF_32BIT
))
107 task_size
= STACK_TOP32
;
109 if (len
& ~huge_page_mask(h
))
114 if (flags
& MAP_FIXED
) {
115 if (prepare_hugepage_range(file
, addr
, len
))
121 addr
= ALIGN(addr
, huge_page_size(h
));
122 vma
= find_vma(mm
, addr
);
123 if (task_size
- len
>= addr
&&
124 (!vma
|| addr
+ len
<= vm_start_gap(vma
)))
127 if (mm
->get_unmapped_area
== arch_get_unmapped_area
)
128 return hugetlb_get_unmapped_area_bottomup(file
, addr
, len
,
131 return hugetlb_get_unmapped_area_topdown(file
, addr
, len
,
135 static pte_t
sun4u_hugepage_shift_to_tte(pte_t entry
, unsigned int shift
)
140 static pte_t
sun4v_hugepage_shift_to_tte(pte_t entry
, unsigned int shift
)
142 unsigned long hugepage_size
= _PAGE_SZ4MB_4V
;
144 pte_val(entry
) = pte_val(entry
) & ~_PAGE_SZALL_4V
;
147 case HPAGE_16GB_SHIFT
:
148 hugepage_size
= _PAGE_SZ16GB_4V
;
149 pte_val(entry
) |= _PAGE_PUD_HUGE
;
151 case HPAGE_2GB_SHIFT
:
152 hugepage_size
= _PAGE_SZ2GB_4V
;
153 pte_val(entry
) |= _PAGE_PMD_HUGE
;
155 case HPAGE_256MB_SHIFT
:
156 hugepage_size
= _PAGE_SZ256MB_4V
;
157 pte_val(entry
) |= _PAGE_PMD_HUGE
;
160 pte_val(entry
) |= _PAGE_PMD_HUGE
;
162 case HPAGE_64K_SHIFT
:
163 hugepage_size
= _PAGE_SZ64K_4V
;
166 WARN_ONCE(1, "unsupported hugepage shift=%u\n", shift
);
169 pte_val(entry
) = pte_val(entry
) | hugepage_size
;
173 static pte_t
hugepage_shift_to_tte(pte_t entry
, unsigned int shift
)
175 if (tlb_type
== hypervisor
)
176 return sun4v_hugepage_shift_to_tte(entry
, shift
);
178 return sun4u_hugepage_shift_to_tte(entry
, shift
);
181 pte_t
arch_make_huge_pte(pte_t entry
, struct vm_area_struct
*vma
,
182 struct page
*page
, int writeable
)
184 unsigned int shift
= huge_page_shift(hstate_vma(vma
));
186 return hugepage_shift_to_tte(entry
, shift
);
189 static unsigned int sun4v_huge_tte_to_shift(pte_t entry
)
191 unsigned long tte_szbits
= pte_val(entry
) & _PAGE_SZALL_4V
;
194 switch (tte_szbits
) {
195 case _PAGE_SZ16GB_4V
:
196 shift
= HPAGE_16GB_SHIFT
;
199 shift
= HPAGE_2GB_SHIFT
;
201 case _PAGE_SZ256MB_4V
:
202 shift
= HPAGE_256MB_SHIFT
;
205 shift
= REAL_HPAGE_SHIFT
;
208 shift
= HPAGE_64K_SHIFT
;
217 static unsigned int sun4u_huge_tte_to_shift(pte_t entry
)
219 unsigned long tte_szbits
= pte_val(entry
) & _PAGE_SZALL_4U
;
222 switch (tte_szbits
) {
223 case _PAGE_SZ256MB_4U
:
224 shift
= HPAGE_256MB_SHIFT
;
227 shift
= REAL_HPAGE_SHIFT
;
230 shift
= HPAGE_64K_SHIFT
;
239 static unsigned int huge_tte_to_shift(pte_t entry
)
243 if (tlb_type
== hypervisor
)
244 shift
= sun4v_huge_tte_to_shift(entry
);
246 shift
= sun4u_huge_tte_to_shift(entry
);
248 if (shift
== PAGE_SHIFT
)
249 WARN_ONCE(1, "tto_to_shift: invalid hugepage tte=0x%lx\n",
255 static unsigned long huge_tte_to_size(pte_t pte
)
257 unsigned long size
= 1UL << huge_tte_to_shift(pte
);
259 if (size
== REAL_HPAGE_SIZE
)
264 pte_t
*huge_pte_alloc(struct mm_struct
*mm
,
265 unsigned long addr
, unsigned long sz
)
271 pgd
= pgd_offset(mm
, addr
);
272 pud
= pud_alloc(mm
, pgd
, addr
);
277 pmd
= pmd_alloc(mm
, pud
, addr
);
282 return pte_alloc_map(mm
, pmd
, addr
);
285 pte_t
*huge_pte_offset(struct mm_struct
*mm
,
286 unsigned long addr
, unsigned long sz
)
292 pgd
= pgd_offset(mm
, addr
);
295 pud
= pud_offset(pgd
, addr
);
298 if (is_hugetlb_pud(*pud
))
300 pmd
= pmd_offset(pud
, addr
);
303 if (is_hugetlb_pmd(*pmd
))
305 return pte_offset_map(pmd
, addr
);
308 void set_huge_pte_at(struct mm_struct
*mm
, unsigned long addr
,
309 pte_t
*ptep
, pte_t entry
)
311 unsigned int nptes
, orig_shift
, shift
;
312 unsigned long i
, size
;
315 size
= huge_tte_to_size(entry
);
318 if (size
>= PUD_SIZE
)
320 else if (size
>= PMD_SIZE
)
325 nptes
= size
>> shift
;
327 if (!pte_present(*ptep
) && pte_present(entry
))
328 mm
->context
.hugetlb_pte_count
+= nptes
;
332 orig_shift
= pte_none(orig
) ? PAGE_SHIFT
: huge_tte_to_shift(orig
);
334 for (i
= 0; i
< nptes
; i
++)
335 ptep
[i
] = __pte(pte_val(entry
) + (i
<< shift
));
337 maybe_tlb_batch_add(mm
, addr
, ptep
, orig
, 0, orig_shift
);
338 /* An HPAGE_SIZE'ed page is composed of two REAL_HPAGE_SIZE'ed pages */
339 if (size
== HPAGE_SIZE
)
340 maybe_tlb_batch_add(mm
, addr
+ REAL_HPAGE_SIZE
, ptep
, orig
, 0,
344 pte_t
huge_ptep_get_and_clear(struct mm_struct
*mm
, unsigned long addr
,
347 unsigned int i
, nptes
, orig_shift
, shift
;
352 size
= huge_tte_to_size(entry
);
355 if (size
>= PUD_SIZE
)
357 else if (size
>= PMD_SIZE
)
362 nptes
= size
>> shift
;
363 orig_shift
= pte_none(entry
) ? PAGE_SHIFT
: huge_tte_to_shift(entry
);
365 if (pte_present(entry
))
366 mm
->context
.hugetlb_pte_count
-= nptes
;
369 for (i
= 0; i
< nptes
; i
++)
370 ptep
[i
] = __pte(0UL);
372 maybe_tlb_batch_add(mm
, addr
, ptep
, entry
, 0, orig_shift
);
373 /* An HPAGE_SIZE'ed page is composed of two REAL_HPAGE_SIZE'ed pages */
374 if (size
== HPAGE_SIZE
)
375 maybe_tlb_batch_add(mm
, addr
+ REAL_HPAGE_SIZE
, ptep
, entry
, 0,
381 int pmd_huge(pmd_t pmd
)
383 return !pmd_none(pmd
) &&
384 (pmd_val(pmd
) & (_PAGE_VALID
|_PAGE_PMD_HUGE
)) != _PAGE_VALID
;
387 int pud_huge(pud_t pud
)
389 return !pud_none(pud
) &&
390 (pud_val(pud
) & (_PAGE_VALID
|_PAGE_PUD_HUGE
)) != _PAGE_VALID
;
393 static void hugetlb_free_pte_range(struct mmu_gather
*tlb
, pmd_t
*pmd
,
396 pgtable_t token
= pmd_pgtable(*pmd
);
399 pte_free_tlb(tlb
, token
, addr
);
400 atomic_long_dec(&tlb
->mm
->nr_ptes
);
403 static void hugetlb_free_pmd_range(struct mmu_gather
*tlb
, pud_t
*pud
,
404 unsigned long addr
, unsigned long end
,
405 unsigned long floor
, unsigned long ceiling
)
412 pmd
= pmd_offset(pud
, addr
);
414 next
= pmd_addr_end(addr
, end
);
417 if (is_hugetlb_pmd(*pmd
))
420 hugetlb_free_pte_range(tlb
, pmd
, addr
);
421 } while (pmd
++, addr
= next
, addr
!= end
);
431 if (end
- 1 > ceiling
- 1)
434 pmd
= pmd_offset(pud
, start
);
436 pmd_free_tlb(tlb
, pmd
, start
);
437 mm_dec_nr_pmds(tlb
->mm
);
440 static void hugetlb_free_pud_range(struct mmu_gather
*tlb
, pgd_t
*pgd
,
441 unsigned long addr
, unsigned long end
,
442 unsigned long floor
, unsigned long ceiling
)
449 pud
= pud_offset(pgd
, addr
);
451 next
= pud_addr_end(addr
, end
);
452 if (pud_none_or_clear_bad(pud
))
454 if (is_hugetlb_pud(*pud
))
457 hugetlb_free_pmd_range(tlb
, pud
, addr
, next
, floor
,
459 } while (pud
++, addr
= next
, addr
!= end
);
465 ceiling
&= PGDIR_MASK
;
469 if (end
- 1 > ceiling
- 1)
472 pud
= pud_offset(pgd
, start
);
474 pud_free_tlb(tlb
, pud
, start
);
477 void hugetlb_free_pgd_range(struct mmu_gather
*tlb
,
478 unsigned long addr
, unsigned long end
,
479 unsigned long floor
, unsigned long ceiling
)
495 if (end
- 1 > ceiling
- 1)
500 pgd
= pgd_offset(tlb
->mm
, addr
);
502 next
= pgd_addr_end(addr
, end
);
503 if (pgd_none_or_clear_bad(pgd
))
505 hugetlb_free_pud_range(tlb
, pgd
, addr
, next
, floor
, ceiling
);
506 } while (pgd
++, addr
= next
, addr
!= end
);