upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / i386 / mm / hugetlbpage.c
1 /*
2  * IA-32 Huge TLB Page Support for Kernel.
3  *
4  * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
5  */
6
7 #include <linux/config.h>
8 #include <linux/init.h>
9 #include <linux/fs.h>
10 #include <linux/mm.h>
11 #include <linux/hugetlb.h>
12 #include <linux/pagemap.h>
13 #include <linux/smp_lock.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/sysctl.h>
17 #include <asm/mman.h>
18 #include <asm/tlb.h>
19 #include <asm/tlbflush.h>
20
21 static pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
22 {
23         pgd_t *pgd;
24         pmd_t *pmd = NULL;
25
26         pgd = pgd_offset(mm, addr);
27         pmd = pmd_alloc(mm, pgd, addr);
28         return (pte_t *) pmd;
29 }
30
31 static pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
32 {
33         pgd_t *pgd;
34         pmd_t *pmd = NULL;
35
36         pgd = pgd_offset(mm, addr);
37         pmd = pmd_offset(pgd, addr);
38         return (pte_t *) pmd;
39 }
40
41 static void set_huge_pte(struct mm_struct *mm, struct vm_area_struct *vma, struct page *page, pte_t * page_table, int write_access)
42 {
43         pte_t entry;
44
45         // mm->rss += (HPAGE_SIZE / PAGE_SIZE);
46         vx_rsspages_add(mm, HPAGE_SIZE / PAGE_SIZE);
47         if (write_access) {
48                 entry =
49                     pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
50         } else
51                 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
52         entry = pte_mkyoung(entry);
53         mk_pte_huge(entry);
54         set_pte(page_table, entry);
55 }
56
57 /*
58  * This function checks for proper alignment of input addr and len parameters.
59  */
60 int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
61 {
62         if (len & ~HPAGE_MASK)
63                 return -EINVAL;
64         if (addr & ~HPAGE_MASK)
65                 return -EINVAL;
66         return 0;
67 }
68
69 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
70                         struct vm_area_struct *vma)
71 {
72         pte_t *src_pte, *dst_pte, entry;
73         struct page *ptepage;
74         unsigned long addr = vma->vm_start;
75         unsigned long end = vma->vm_end;
76
77         while (addr < end) {
78                 dst_pte = huge_pte_alloc(dst, addr);
79                 if (!dst_pte)
80                         goto nomem;
81                 src_pte = huge_pte_offset(src, addr);
82                 entry = *src_pte;
83                 ptepage = pte_page(entry);
84                 get_page(ptepage);
85                 set_pte(dst_pte, entry);
86                 // dst->rss += (HPAGE_SIZE / PAGE_SIZE);
87                 vx_rsspages_add(dst, HPAGE_SIZE / PAGE_SIZE);
88                 addr += HPAGE_SIZE;
89         }
90         return 0;
91
92 nomem:
93         return -ENOMEM;
94 }
95
96 int
97 follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
98                     struct page **pages, struct vm_area_struct **vmas,
99                     unsigned long *position, int *length, int i)
100 {
101         unsigned long vpfn, vaddr = *position;
102         int remainder = *length;
103
104         WARN_ON(!is_vm_hugetlb_page(vma));
105
106         vpfn = vaddr/PAGE_SIZE;
107         while (vaddr < vma->vm_end && remainder) {
108
109                 if (pages) {
110                         pte_t *pte;
111                         struct page *page;
112
113                         pte = huge_pte_offset(mm, vaddr);
114
115                         /* hugetlb should be locked, and hence, prefaulted */
116                         WARN_ON(!pte || pte_none(*pte));
117
118                         page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
119
120                         WARN_ON(!PageCompound(page));
121
122                         get_page(page);
123                         pages[i] = page;
124                 }
125
126                 if (vmas)
127                         vmas[i] = vma;
128
129                 vaddr += PAGE_SIZE;
130                 ++vpfn;
131                 --remainder;
132                 ++i;
133         }
134
135         *length = remainder;
136         *position = vaddr;
137
138         return i;
139 }
140
141 #if 0   /* This is just for testing */
142 struct page *
143 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
144 {
145         unsigned long start = address;
146         int length = 1;
147         int nr;
148         struct page *page;
149         struct vm_area_struct *vma;
150
151         vma = find_vma(mm, addr);
152         if (!vma || !is_vm_hugetlb_page(vma))
153                 return ERR_PTR(-EINVAL);
154
155         pte = huge_pte_offset(mm, address);
156
157         /* hugetlb should be locked, and hence, prefaulted */
158         WARN_ON(!pte || pte_none(*pte));
159
160         page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
161
162         WARN_ON(!PageCompound(page));
163
164         return page;
165 }
166
167 int pmd_huge(pmd_t pmd)
168 {
169         return 0;
170 }
171
172 struct page *
173 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
174                 pmd_t *pmd, int write)
175 {
176         return NULL;
177 }
178
179 #else
180
181 struct page *
182 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
183 {
184         return ERR_PTR(-EINVAL);
185 }
186
187 int pmd_huge(pmd_t pmd)
188 {
189         return !!(pmd_val(pmd) & _PAGE_PSE);
190 }
191
192 struct page *
193 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
194                 pmd_t *pmd, int write)
195 {
196         struct page *page;
197
198         page = pte_page(*(pte_t *)pmd);
199         if (page)
200                 page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
201         return page;
202 }
203 #endif
204
205 void unmap_hugepage_range(struct vm_area_struct *vma,
206                 unsigned long start, unsigned long end)
207 {
208         struct mm_struct *mm = vma->vm_mm;
209         unsigned long address;
210         pte_t pte;
211         struct page *page;
212
213         BUG_ON(start & (HPAGE_SIZE - 1));
214         BUG_ON(end & (HPAGE_SIZE - 1));
215
216         for (address = start; address < end; address += HPAGE_SIZE) {
217                 pte = ptep_get_and_clear(huge_pte_offset(mm, address));
218                 if (pte_none(pte))
219                         continue;
220                 page = pte_page(pte);
221                 put_page(page);
222         }
223         // mm->rss -= (end - start) >> PAGE_SHIFT;
224         vx_rsspages_sub(mm, (end - start) >> PAGE_SHIFT);
225         flush_tlb_range(vma, start, end);
226 }
227
228 int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
229 {
230         struct mm_struct *mm = current->mm;
231         unsigned long addr;
232         int ret = 0;
233
234         BUG_ON(vma->vm_start & ~HPAGE_MASK);
235         BUG_ON(vma->vm_end & ~HPAGE_MASK);
236
237         spin_lock(&mm->page_table_lock);
238         for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
239                 unsigned long idx;
240                 pte_t *pte = huge_pte_alloc(mm, addr);
241                 struct page *page;
242
243                 if (!pte) {
244                         ret = -ENOMEM;
245                         goto out;
246                 }
247
248                 if (!pte_none(*pte)) {
249                         pmd_t *pmd = (pmd_t *) pte;
250
251                         page = pmd_page(*pmd);
252                         pmd_clear(pmd);
253                         mm->nr_ptes--;
254                         dec_page_state(nr_page_table_pages);
255                         page_cache_release(page);
256                 }
257
258                 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
259                         + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
260                 page = find_get_page(mapping, idx);
261                 if (!page) {
262                         /* charge the fs quota first */
263                         if (hugetlb_get_quota(mapping)) {
264                                 ret = -ENOMEM;
265                                 goto out;
266                         }
267                         page = alloc_huge_page();
268                         if (!page) {
269                                 hugetlb_put_quota(mapping);
270                                 ret = -ENOMEM;
271                                 goto out;
272                         }
273                         ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
274                         if (! ret) {
275                                 unlock_page(page);
276                         } else {
277                                 hugetlb_put_quota(mapping);
278                                 free_huge_page(page);
279                                 goto out;
280                         }
281                 }
282                 set_huge_pte(mm, vma, page, pte, vma->vm_flags & VM_WRITE);
283         }
284 out:
285         spin_unlock(&mm->page_table_lock);
286         return ret;
287 }
288
289 /* x86_64 also uses this file */
290
291 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
292 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
293                 unsigned long addr, unsigned long len,
294                 unsigned long pgoff, unsigned long flags)
295 {
296         struct mm_struct *mm = current->mm;
297         struct vm_area_struct *vma;
298         unsigned long start_addr;
299
300         start_addr = mm->free_area_cache;
301
302 full_search:
303         addr = ALIGN(start_addr, HPAGE_SIZE);
304
305         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
306                 /* At this point:  (!vma || addr < vma->vm_end). */
307                 if (TASK_SIZE - len < addr) {
308                         /*
309                          * Start a new search - just in case we missed
310                          * some holes.
311                          */
312                         if (start_addr != TASK_UNMAPPED_BASE) {
313                                 start_addr = TASK_UNMAPPED_BASE;
314                                 goto full_search;
315                         }
316                         return -ENOMEM;
317                 }
318                 if (!vma || addr + len <= vma->vm_start) {
319                         mm->free_area_cache = addr + len;
320                         return addr;
321                 }
322                 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
323         }
324 }
325
326 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
327                 unsigned long addr0, unsigned long len,
328                 unsigned long pgoff, unsigned long flags)
329 {
330         struct mm_struct *mm = current->mm;
331         struct vm_area_struct *vma, *prev_vma;
332         unsigned long base = mm->mmap_base, addr = addr0;
333         int first_time = 1;
334
335         /* don't allow allocations above current base */
336         if (mm->free_area_cache > base)
337                 mm->free_area_cache = base;
338
339 try_again:
340         /* make sure it can fit in the remaining address space */
341         if (mm->free_area_cache < len)
342                 goto fail;
343
344         /* either no address requested or cant fit in requested address hole */
345         addr = (mm->free_area_cache - len) & HPAGE_MASK;
346         do {
347                 /*
348                  * Lookup failure means no vma is above this address,
349                  * i.e. return with success:
350                  */
351                 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
352                         return addr;
353
354                 /*
355                  * new region fits between prev_vma->vm_end and
356                  * vma->vm_start, use it:
357                  */
358                 if (addr + len <= vma->vm_start &&
359                                 (!prev_vma || (addr >= prev_vma->vm_end)))
360                         /* remember the address as a hint for next time */
361                         return (mm->free_area_cache = addr);
362                 else
363                         /* pull free_area_cache down to the first hole */
364                         if (mm->free_area_cache == vma->vm_end)
365                                 mm->free_area_cache = vma->vm_start;
366
367                 /* try just below the current vma->vm_start */
368                 addr = (vma->vm_start - len) & HPAGE_MASK;
369         } while (len <= vma->vm_start);
370
371 fail:
372         /*
373          * if hint left us with no space for the requested
374          * mapping then try again:
375          */
376         if (first_time) {
377                 mm->free_area_cache = base;
378                 first_time = 0;
379                 goto try_again;
380         }
381         /*
382          * A failed mmap() very likely causes application failure,
383          * so fall back to the bottom-up function here. This scenario
384          * can happen with large stack limits and large mmap()
385          * allocations.
386          */
387         mm->free_area_cache = TASK_UNMAPPED_BASE;
388         addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
389                         len, pgoff, flags);
390
391         /*
392          * Restore the topdown base:
393          */
394         mm->free_area_cache = base;
395
396         return addr;
397 }
398
399 unsigned long
400 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
401                 unsigned long len, unsigned long pgoff, unsigned long flags)
402 {
403         struct mm_struct *mm = current->mm;
404         struct vm_area_struct *vma;
405
406         if (len & ~HPAGE_MASK)
407                 return -EINVAL;
408         if (len > TASK_SIZE)
409                 return -ENOMEM;
410
411         if (addr) {
412                 addr = ALIGN(addr, HPAGE_SIZE);
413                 vma = find_vma(mm, addr);
414                 if (TASK_SIZE - len >= addr &&
415                     (!vma || addr + len <= vma->vm_start))
416                         return addr;
417         }
418         if (mm->get_unmapped_area == arch_get_unmapped_area)
419                 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
420                                 pgoff, flags);
421         else
422                 return hugetlb_get_unmapped_area_topdown(file, addr, len,
423                                 pgoff, flags);
424 }
425
426 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
427