]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/metag/mm/hugetlbpage.c
mm/hugetlb: add size parameter to huge_pte_offset()
[karo-tx-linux.git] / arch / metag / mm / hugetlbpage.c
1 /*
2  * arch/metag/mm/hugetlbpage.c
3  *
4  * METAG HugeTLB page support.
5  *
6  * Cloned from SuperH
7  *
8  * Cloned from sparc64 by Paul Mundt.
9  *
10  * Copyright (C) 2002, 2003 David S. Miller (davem@redhat.com)
11  */
12
13 #include <linux/init.h>
14 #include <linux/fs.h>
15 #include <linux/mm.h>
16 #include <linux/hugetlb.h>
17 #include <linux/pagemap.h>
18 #include <linux/sysctl.h>
19
20 #include <asm/mman.h>
21 #include <asm/pgalloc.h>
22 #include <asm/tlb.h>
23 #include <asm/tlbflush.h>
24 #include <asm/cacheflush.h>
25
26 /*
27  * If the arch doesn't supply something else, assume that hugepage
28  * size aligned regions are ok without further preparation.
29  */
30 int prepare_hugepage_range(struct file *file, unsigned long addr,
31                                                 unsigned long len)
32 {
33         struct mm_struct *mm = current->mm;
34         struct hstate *h = hstate_file(file);
35         struct vm_area_struct *vma;
36
37         if (len & ~huge_page_mask(h))
38                 return -EINVAL;
39         if (addr & ~huge_page_mask(h))
40                 return -EINVAL;
41         if (TASK_SIZE - len < addr)
42                 return -EINVAL;
43
44         vma = find_vma(mm, ALIGN_HUGEPT(addr));
45         if (vma && !(vma->vm_flags & MAP_HUGETLB))
46                 return -EINVAL;
47
48         vma = find_vma(mm, addr);
49         if (vma) {
50                 if (addr + len > vma->vm_start)
51                         return -EINVAL;
52                 if (!(vma->vm_flags & MAP_HUGETLB) &&
53                     (ALIGN_HUGEPT(addr + len) > vma->vm_start))
54                         return -EINVAL;
55         }
56         return 0;
57 }
58
59 pte_t *huge_pte_alloc(struct mm_struct *mm,
60                         unsigned long addr, unsigned long sz)
61 {
62         pgd_t *pgd;
63         pud_t *pud;
64         pmd_t *pmd;
65         pte_t *pte;
66
67         pgd = pgd_offset(mm, addr);
68         pud = pud_offset(pgd, addr);
69         pmd = pmd_offset(pud, addr);
70         pte = pte_alloc_map(mm, pmd, addr);
71         pgd->pgd &= ~_PAGE_SZ_MASK;
72         pgd->pgd |= _PAGE_SZHUGE;
73
74         return pte;
75 }
76
77 pte_t *huge_pte_offset(struct mm_struct *mm,
78                        unsigned long addr, unsigned long sz)
79 {
80         pgd_t *pgd;
81         pud_t *pud;
82         pmd_t *pmd;
83         pte_t *pte = NULL;
84
85         pgd = pgd_offset(mm, addr);
86         pud = pud_offset(pgd, addr);
87         pmd = pmd_offset(pud, addr);
88         pte = pte_offset_kernel(pmd, addr);
89
90         return pte;
91 }
92
93 int pmd_huge(pmd_t pmd)
94 {
95         return pmd_page_shift(pmd) > PAGE_SHIFT;
96 }
97
98 int pud_huge(pud_t pud)
99 {
100         return 0;
101 }
102
103 struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address,
104                              pmd_t *pmd, int write)
105 {
106         return NULL;
107 }
108
109 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
110
111 /*
112  * Look for an unmapped area starting after another hugetlb vma.
113  * There are guaranteed to be no huge pte's spare if all the huge pages are
114  * full size (4MB), so in that case compile out this search.
115  */
116 #if HPAGE_SHIFT == HUGEPT_SHIFT
117 static inline unsigned long
118 hugetlb_get_unmapped_area_existing(unsigned long len)
119 {
120         return 0;
121 }
122 #else
123 static unsigned long
124 hugetlb_get_unmapped_area_existing(unsigned long len)
125 {
126         struct mm_struct *mm = current->mm;
127         struct vm_area_struct *vma;
128         unsigned long start_addr, addr;
129         int after_huge;
130
131         if (mm->context.part_huge) {
132                 start_addr = mm->context.part_huge;
133                 after_huge = 1;
134         } else {
135                 start_addr = TASK_UNMAPPED_BASE;
136                 after_huge = 0;
137         }
138 new_search:
139         addr = start_addr;
140
141         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
142                 if ((!vma && !after_huge) || TASK_SIZE - len < addr) {
143                         /*
144                          * Start a new search - just in case we missed
145                          * some holes.
146                          */
147                         if (start_addr != TASK_UNMAPPED_BASE) {
148                                 start_addr = TASK_UNMAPPED_BASE;
149                                 goto new_search;
150                         }
151                         return 0;
152                 }
153                 /* skip ahead if we've aligned right over some vmas */
154                 if (vma && vma->vm_end <= addr)
155                         continue;
156                 /* space before the next vma? */
157                 if (after_huge && (!vma || ALIGN_HUGEPT(addr + len)
158                             <= vma->vm_start)) {
159                         unsigned long end = addr + len;
160                         if (end & HUGEPT_MASK)
161                                 mm->context.part_huge = end;
162                         else if (addr == mm->context.part_huge)
163                                 mm->context.part_huge = 0;
164                         return addr;
165                 }
166                 if (vma->vm_flags & MAP_HUGETLB) {
167                         /* space after a huge vma in 2nd level page table? */
168                         if (vma->vm_end & HUGEPT_MASK) {
169                                 after_huge = 1;
170                                 /* no need to align to the next PT block */
171                                 addr = vma->vm_end;
172                                 continue;
173                         }
174                 }
175                 after_huge = 0;
176                 addr = ALIGN_HUGEPT(vma->vm_end);
177         }
178 }
179 #endif
180
181 /* Do a full search to find an area without any nearby normal pages. */
182 static unsigned long
183 hugetlb_get_unmapped_area_new_pmd(unsigned long len)
184 {
185         struct vm_unmapped_area_info info;
186
187         info.flags = 0;
188         info.length = len;
189         info.low_limit = TASK_UNMAPPED_BASE;
190         info.high_limit = TASK_SIZE;
191         info.align_mask = PAGE_MASK & HUGEPT_MASK;
192         info.align_offset = 0;
193         return vm_unmapped_area(&info);
194 }
195
196 unsigned long
197 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
198                 unsigned long len, unsigned long pgoff, unsigned long flags)
199 {
200         struct hstate *h = hstate_file(file);
201
202         if (len & ~huge_page_mask(h))
203                 return -EINVAL;
204         if (len > TASK_SIZE)
205                 return -ENOMEM;
206
207         if (flags & MAP_FIXED) {
208                 if (prepare_hugepage_range(file, addr, len))
209                         return -EINVAL;
210                 return addr;
211         }
212
213         if (addr) {
214                 addr = ALIGN(addr, huge_page_size(h));
215                 if (!prepare_hugepage_range(file, addr, len))
216                         return addr;
217         }
218
219         /*
220          * Look for an existing hugetlb vma with space after it (this is to to
221          * minimise fragmentation caused by huge pages.
222          */
223         addr = hugetlb_get_unmapped_area_existing(len);
224         if (addr)
225                 return addr;
226
227         /*
228          * Find an unmapped naturally aligned set of 4MB blocks that we can use
229          * for huge pages.
230          */
231         return hugetlb_get_unmapped_area_new_pmd(len);
232 }
233
234 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
235
236 /* necessary for boot time 4MB huge page allocation */
237 static __init int setup_hugepagesz(char *opt)
238 {
239         unsigned long ps = memparse(opt, &opt);
240         if (ps == (1 << HPAGE_SHIFT)) {
241                 hugetlb_add_hstate(HPAGE_SHIFT - PAGE_SHIFT);
242         } else {
243                 hugetlb_bad_size();
244                 pr_err("hugepagesz: Unsupported page size %lu M\n",
245                        ps >> 20);
246                 return 0;
247         }
248         return 1;
249 }
250 __setup("hugepagesz=", setup_hugepagesz);