1 // SPDX-License-Identifier: GPL-2.0
3 * arch/metag/mm/hugetlbpage.c
5 * METAG HugeTLB page support.
9 * Cloned from sparc64 by Paul Mundt.
11 * Copyright (C) 2002, 2003 David S. Miller (davem@redhat.com)
14 #include <linux/init.h>
17 #include <linux/hugetlb.h>
18 #include <linux/pagemap.h>
19 #include <linux/sysctl.h>
22 #include <asm/pgalloc.h>
24 #include <asm/tlbflush.h>
25 #include <asm/cacheflush.h>
28 * If the arch doesn't supply something else, assume that hugepage
29 * size aligned regions are ok without further preparation.
31 int prepare_hugepage_range(struct file
*file
, unsigned long addr
,
34 struct mm_struct
*mm
= current
->mm
;
35 struct hstate
*h
= hstate_file(file
);
36 struct vm_area_struct
*vma
;
38 if (len
& ~huge_page_mask(h
))
40 if (addr
& ~huge_page_mask(h
))
42 if (TASK_SIZE
- len
< addr
)
45 vma
= find_vma(mm
, ALIGN_HUGEPT(addr
));
46 if (vma
&& !(vma
->vm_flags
& MAP_HUGETLB
))
49 vma
= find_vma(mm
, addr
);
51 if (addr
+ len
> vma
->vm_start
)
53 if (!(vma
->vm_flags
& MAP_HUGETLB
) &&
54 (ALIGN_HUGEPT(addr
+ len
) > vma
->vm_start
))
60 pte_t
*huge_pte_alloc(struct mm_struct
*mm
,
61 unsigned long addr
, unsigned long sz
)
68 pgd
= pgd_offset(mm
, addr
);
69 pud
= pud_offset(pgd
, addr
);
70 pmd
= pmd_offset(pud
, addr
);
71 pte
= pte_alloc_map(mm
, pmd
, addr
);
72 pgd
->pgd
&= ~_PAGE_SZ_MASK
;
73 pgd
->pgd
|= _PAGE_SZHUGE
;
78 pte_t
*huge_pte_offset(struct mm_struct
*mm
,
79 unsigned long addr
, unsigned long sz
)
86 pgd
= pgd_offset(mm
, addr
);
87 pud
= pud_offset(pgd
, addr
);
88 pmd
= pmd_offset(pud
, addr
);
89 pte
= pte_offset_kernel(pmd
, addr
);
94 int pmd_huge(pmd_t pmd
)
96 return pmd_page_shift(pmd
) > PAGE_SHIFT
;
99 int pud_huge(pud_t pud
)
104 struct page
*follow_huge_pmd(struct mm_struct
*mm
, unsigned long address
,
105 pmd_t
*pmd
, int write
)
110 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
113 * Look for an unmapped area starting after another hugetlb vma.
114 * There are guaranteed to be no huge pte's spare if all the huge pages are
115 * full size (4MB), so in that case compile out this search.
117 #if HPAGE_SHIFT == HUGEPT_SHIFT
118 static inline unsigned long
119 hugetlb_get_unmapped_area_existing(unsigned long len
)
125 hugetlb_get_unmapped_area_existing(unsigned long len
)
127 struct mm_struct
*mm
= current
->mm
;
128 struct vm_area_struct
*vma
;
129 unsigned long start_addr
, addr
;
132 if (mm
->context
.part_huge
) {
133 start_addr
= mm
->context
.part_huge
;
136 start_addr
= TASK_UNMAPPED_BASE
;
142 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
143 if ((!vma
&& !after_huge
) || TASK_SIZE
- len
< addr
) {
145 * Start a new search - just in case we missed
148 if (start_addr
!= TASK_UNMAPPED_BASE
) {
149 start_addr
= TASK_UNMAPPED_BASE
;
154 /* skip ahead if we've aligned right over some vmas */
155 if (vma
&& vma
->vm_end
<= addr
)
157 /* space before the next vma? */
158 if (after_huge
&& (!vma
|| ALIGN_HUGEPT(addr
+ len
)
160 unsigned long end
= addr
+ len
;
161 if (end
& HUGEPT_MASK
)
162 mm
->context
.part_huge
= end
;
163 else if (addr
== mm
->context
.part_huge
)
164 mm
->context
.part_huge
= 0;
167 if (vma
->vm_flags
& MAP_HUGETLB
) {
168 /* space after a huge vma in 2nd level page table? */
169 if (vma
->vm_end
& HUGEPT_MASK
) {
171 /* no need to align to the next PT block */
177 addr
= ALIGN_HUGEPT(vma
->vm_end
);
182 /* Do a full search to find an area without any nearby normal pages. */
184 hugetlb_get_unmapped_area_new_pmd(unsigned long len
)
186 struct vm_unmapped_area_info info
;
190 info
.low_limit
= TASK_UNMAPPED_BASE
;
191 info
.high_limit
= TASK_SIZE
;
192 info
.align_mask
= PAGE_MASK
& HUGEPT_MASK
;
193 info
.align_offset
= 0;
194 return vm_unmapped_area(&info
);
198 hugetlb_get_unmapped_area(struct file
*file
, unsigned long addr
,
199 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
201 struct hstate
*h
= hstate_file(file
);
203 if (len
& ~huge_page_mask(h
))
208 if (flags
& MAP_FIXED
) {
209 if (prepare_hugepage_range(file
, addr
, len
))
215 addr
= ALIGN(addr
, huge_page_size(h
));
216 if (!prepare_hugepage_range(file
, addr
, len
))
221 * Look for an existing hugetlb vma with space after it (this is to to
222 * minimise fragmentation caused by huge pages.
224 addr
= hugetlb_get_unmapped_area_existing(len
);
229 * Find an unmapped naturally aligned set of 4MB blocks that we can use
232 return hugetlb_get_unmapped_area_new_pmd(len
);
235 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
237 /* necessary for boot time 4MB huge page allocation */
238 static __init
int setup_hugepagesz(char *opt
)
240 unsigned long ps
= memparse(opt
, &opt
);
241 if (ps
== (1 << HPAGE_SHIFT
)) {
242 hugetlb_add_hstate(HPAGE_SHIFT
- PAGE_SHIFT
);
245 pr_err("hugepagesz: Unsupported page size %lu M\n",
251 __setup("hugepagesz=", setup_hugepagesz
);