]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Generic hugetlb support. | |
3 | * (C) William Irwin, April 2004 | |
4 | */ | |
5 | #include <linux/gfp.h> | |
6 | #include <linux/list.h> | |
7 | #include <linux/init.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/mm.h> | |
1da177e4 LT |
10 | #include <linux/sysctl.h> |
11 | #include <linux/highmem.h> | |
12 | #include <linux/nodemask.h> | |
63551ae0 | 13 | #include <linux/pagemap.h> |
5da7ca86 | 14 | #include <linux/mempolicy.h> |
aea47ff3 | 15 | #include <linux/cpuset.h> |
3935baa9 | 16 | #include <linux/mutex.h> |
5da7ca86 | 17 | |
63551ae0 DG |
18 | #include <asm/page.h> |
19 | #include <asm/pgtable.h> | |
20 | ||
21 | #include <linux/hugetlb.h> | |
7835e98b | 22 | #include "internal.h" |
1da177e4 LT |
23 | |
24 | const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL; | |
b45b5bd6 | 25 | static unsigned long nr_huge_pages, free_huge_pages, reserved_huge_pages; |
1da177e4 LT |
26 | unsigned long max_huge_pages; |
27 | static struct list_head hugepage_freelists[MAX_NUMNODES]; | |
28 | static unsigned int nr_huge_pages_node[MAX_NUMNODES]; | |
29 | static unsigned int free_huge_pages_node[MAX_NUMNODES]; | |
3935baa9 DG |
30 | /* |
31 | * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages | |
32 | */ | |
33 | static DEFINE_SPINLOCK(hugetlb_lock); | |
0bd0f9fb | 34 | |
79ac6ba4 DG |
35 | static void clear_huge_page(struct page *page, unsigned long addr) |
36 | { | |
37 | int i; | |
38 | ||
39 | might_sleep(); | |
40 | for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) { | |
41 | cond_resched(); | |
42 | clear_user_highpage(page + i, addr); | |
43 | } | |
44 | } | |
45 | ||
46 | static void copy_huge_page(struct page *dst, struct page *src, | |
47 | unsigned long addr) | |
48 | { | |
49 | int i; | |
50 | ||
51 | might_sleep(); | |
52 | for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) { | |
53 | cond_resched(); | |
54 | copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE); | |
55 | } | |
56 | } | |
57 | ||
1da177e4 LT |
58 | static void enqueue_huge_page(struct page *page) |
59 | { | |
60 | int nid = page_to_nid(page); | |
61 | list_add(&page->lru, &hugepage_freelists[nid]); | |
62 | free_huge_pages++; | |
63 | free_huge_pages_node[nid]++; | |
64 | } | |
65 | ||
5da7ca86 CL |
66 | static struct page *dequeue_huge_page(struct vm_area_struct *vma, |
67 | unsigned long address) | |
1da177e4 LT |
68 | { |
69 | int nid = numa_node_id(); | |
70 | struct page *page = NULL; | |
5da7ca86 | 71 | struct zonelist *zonelist = huge_zonelist(vma, address); |
96df9333 | 72 | struct zone **z; |
1da177e4 | 73 | |
96df9333 CL |
74 | for (z = zonelist->zones; *z; z++) { |
75 | nid = (*z)->zone_pgdat->node_id; | |
aea47ff3 CL |
76 | if (cpuset_zone_allowed(*z, GFP_HIGHUSER) && |
77 | !list_empty(&hugepage_freelists[nid])) | |
96df9333 | 78 | break; |
1da177e4 | 79 | } |
96df9333 CL |
80 | |
81 | if (*z) { | |
1da177e4 LT |
82 | page = list_entry(hugepage_freelists[nid].next, |
83 | struct page, lru); | |
84 | list_del(&page->lru); | |
85 | free_huge_pages--; | |
86 | free_huge_pages_node[nid]--; | |
87 | } | |
88 | return page; | |
89 | } | |
90 | ||
27a85ef1 DG |
91 | static void free_huge_page(struct page *page) |
92 | { | |
93 | BUG_ON(page_count(page)); | |
94 | ||
95 | INIT_LIST_HEAD(&page->lru); | |
96 | ||
97 | spin_lock(&hugetlb_lock); | |
98 | enqueue_huge_page(page); | |
99 | spin_unlock(&hugetlb_lock); | |
100 | } | |
101 | ||
a482289d | 102 | static int alloc_fresh_huge_page(void) |
1da177e4 LT |
103 | { |
104 | static int nid = 0; | |
105 | struct page *page; | |
106 | page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN, | |
107 | HUGETLB_PAGE_ORDER); | |
108 | nid = (nid + 1) % num_online_nodes(); | |
109 | if (page) { | |
a482289d | 110 | page[1].lru.next = (void *)free_huge_page; /* dtor */ |
0bd0f9fb | 111 | spin_lock(&hugetlb_lock); |
1da177e4 LT |
112 | nr_huge_pages++; |
113 | nr_huge_pages_node[page_to_nid(page)]++; | |
0bd0f9fb | 114 | spin_unlock(&hugetlb_lock); |
a482289d NP |
115 | put_page(page); /* free it into the hugepage allocator */ |
116 | return 1; | |
1da177e4 | 117 | } |
a482289d | 118 | return 0; |
1da177e4 LT |
119 | } |
120 | ||
27a85ef1 DG |
121 | static struct page *alloc_huge_page(struct vm_area_struct *vma, |
122 | unsigned long addr) | |
1da177e4 | 123 | { |
b45b5bd6 | 124 | struct inode *inode = vma->vm_file->f_dentry->d_inode; |
1da177e4 | 125 | struct page *page; |
b45b5bd6 DG |
126 | int use_reserve = 0; |
127 | unsigned long idx; | |
1da177e4 LT |
128 | |
129 | spin_lock(&hugetlb_lock); | |
b45b5bd6 DG |
130 | |
131 | if (vma->vm_flags & VM_MAYSHARE) { | |
132 | ||
133 | /* idx = radix tree index, i.e. offset into file in | |
134 | * HPAGE_SIZE units */ | |
135 | idx = ((addr - vma->vm_start) >> HPAGE_SHIFT) | |
136 | + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT)); | |
137 | ||
138 | /* The hugetlbfs specific inode info stores the number | |
139 | * of "guaranteed available" (huge) pages. That is, | |
140 | * the first 'prereserved_hpages' pages of the inode | |
141 | * are either already instantiated, or have been | |
142 | * pre-reserved (by hugetlb_reserve_for_inode()). Here | |
143 | * we're in the process of instantiating the page, so | |
144 | * we use this to determine whether to draw from the | |
145 | * pre-reserved pool or the truly free pool. */ | |
146 | if (idx < HUGETLBFS_I(inode)->prereserved_hpages) | |
147 | use_reserve = 1; | |
1da177e4 | 148 | } |
b45b5bd6 DG |
149 | |
150 | if (!use_reserve) { | |
151 | if (free_huge_pages <= reserved_huge_pages) | |
152 | goto fail; | |
153 | } else { | |
154 | BUG_ON(reserved_huge_pages == 0); | |
155 | reserved_huge_pages--; | |
156 | } | |
157 | ||
158 | page = dequeue_huge_page(vma, addr); | |
159 | if (!page) | |
160 | goto fail; | |
161 | ||
1da177e4 | 162 | spin_unlock(&hugetlb_lock); |
7835e98b | 163 | set_page_refcounted(page); |
1da177e4 | 164 | return page; |
b45b5bd6 DG |
165 | |
166 | fail: | |
167 | WARN_ON(use_reserve); /* reserved allocations shouldn't fail */ | |
168 | spin_unlock(&hugetlb_lock); | |
169 | return NULL; | |
170 | } | |
171 | ||
172 | /* hugetlb_extend_reservation() | |
173 | * | |
174 | * Ensure that at least 'atleast' hugepages are, and will remain, | |
175 | * available to instantiate the first 'atleast' pages of the given | |
176 | * inode. If the inode doesn't already have this many pages reserved | |
177 | * or instantiated, set aside some hugepages in the reserved pool to | |
178 | * satisfy later faults (or fail now if there aren't enough, rather | |
179 | * than getting the SIGBUS later). | |
180 | */ | |
181 | int hugetlb_extend_reservation(struct hugetlbfs_inode_info *info, | |
182 | unsigned long atleast) | |
183 | { | |
184 | struct inode *inode = &info->vfs_inode; | |
185 | unsigned long change_in_reserve = 0; | |
186 | int ret = 0; | |
187 | ||
188 | spin_lock(&hugetlb_lock); | |
189 | read_lock_irq(&inode->i_mapping->tree_lock); | |
190 | ||
191 | if (info->prereserved_hpages >= atleast) | |
192 | goto out; | |
193 | ||
194 | /* Because we always call this on shared mappings, none of the | |
195 | * pages beyond info->prereserved_hpages can have been | |
196 | * instantiated, so we need to reserve all of them now. */ | |
197 | change_in_reserve = atleast - info->prereserved_hpages; | |
198 | ||
199 | if ((reserved_huge_pages + change_in_reserve) > free_huge_pages) { | |
200 | ret = -ENOMEM; | |
201 | goto out; | |
202 | } | |
203 | ||
204 | reserved_huge_pages += change_in_reserve; | |
205 | info->prereserved_hpages = atleast; | |
206 | ||
207 | out: | |
208 | read_unlock_irq(&inode->i_mapping->tree_lock); | |
209 | spin_unlock(&hugetlb_lock); | |
210 | ||
211 | return ret; | |
212 | } | |
213 | ||
214 | /* hugetlb_truncate_reservation() | |
215 | * | |
216 | * This returns pages reserved for the given inode to the general free | |
217 | * hugepage pool. If the inode has any pages prereserved, but not | |
218 | * instantiated, beyond offset (atmost << HPAGE_SIZE), then release | |
219 | * them. | |
220 | */ | |
221 | void hugetlb_truncate_reservation(struct hugetlbfs_inode_info *info, | |
222 | unsigned long atmost) | |
223 | { | |
224 | struct inode *inode = &info->vfs_inode; | |
225 | struct address_space *mapping = inode->i_mapping; | |
226 | unsigned long idx; | |
227 | unsigned long change_in_reserve = 0; | |
228 | struct page *page; | |
229 | ||
230 | spin_lock(&hugetlb_lock); | |
231 | read_lock_irq(&inode->i_mapping->tree_lock); | |
232 | ||
233 | if (info->prereserved_hpages <= atmost) | |
234 | goto out; | |
235 | ||
236 | /* Count pages which were reserved, but not instantiated, and | |
237 | * which we can now release. */ | |
238 | for (idx = atmost; idx < info->prereserved_hpages; idx++) { | |
239 | page = radix_tree_lookup(&mapping->page_tree, idx); | |
240 | if (!page) | |
241 | /* Pages which are already instantiated can't | |
242 | * be unreserved (and in fact have already | |
243 | * been removed from the reserved pool) */ | |
244 | change_in_reserve++; | |
245 | } | |
246 | ||
247 | BUG_ON(reserved_huge_pages < change_in_reserve); | |
248 | reserved_huge_pages -= change_in_reserve; | |
249 | info->prereserved_hpages = atmost; | |
250 | ||
251 | out: | |
252 | read_unlock_irq(&inode->i_mapping->tree_lock); | |
253 | spin_unlock(&hugetlb_lock); | |
1da177e4 LT |
254 | } |
255 | ||
256 | static int __init hugetlb_init(void) | |
257 | { | |
258 | unsigned long i; | |
1da177e4 | 259 | |
3c726f8d BH |
260 | if (HPAGE_SHIFT == 0) |
261 | return 0; | |
262 | ||
1da177e4 LT |
263 | for (i = 0; i < MAX_NUMNODES; ++i) |
264 | INIT_LIST_HEAD(&hugepage_freelists[i]); | |
265 | ||
266 | for (i = 0; i < max_huge_pages; ++i) { | |
a482289d | 267 | if (!alloc_fresh_huge_page()) |
1da177e4 | 268 | break; |
1da177e4 LT |
269 | } |
270 | max_huge_pages = free_huge_pages = nr_huge_pages = i; | |
271 | printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages); | |
272 | return 0; | |
273 | } | |
274 | module_init(hugetlb_init); | |
275 | ||
276 | static int __init hugetlb_setup(char *s) | |
277 | { | |
278 | if (sscanf(s, "%lu", &max_huge_pages) <= 0) | |
279 | max_huge_pages = 0; | |
280 | return 1; | |
281 | } | |
282 | __setup("hugepages=", hugetlb_setup); | |
283 | ||
284 | #ifdef CONFIG_SYSCTL | |
285 | static void update_and_free_page(struct page *page) | |
286 | { | |
287 | int i; | |
288 | nr_huge_pages--; | |
289 | nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--; | |
290 | for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) { | |
291 | page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | | |
292 | 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved | | |
293 | 1 << PG_private | 1<< PG_writeback); | |
1da177e4 | 294 | } |
a482289d | 295 | page[1].lru.next = NULL; |
7835e98b | 296 | set_page_refcounted(page); |
1da177e4 LT |
297 | __free_pages(page, HUGETLB_PAGE_ORDER); |
298 | } | |
299 | ||
300 | #ifdef CONFIG_HIGHMEM | |
301 | static void try_to_free_low(unsigned long count) | |
302 | { | |
303 | int i, nid; | |
304 | for (i = 0; i < MAX_NUMNODES; ++i) { | |
305 | struct page *page, *next; | |
306 | list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) { | |
307 | if (PageHighMem(page)) | |
308 | continue; | |
309 | list_del(&page->lru); | |
310 | update_and_free_page(page); | |
311 | nid = page_zone(page)->zone_pgdat->node_id; | |
312 | free_huge_pages--; | |
313 | free_huge_pages_node[nid]--; | |
314 | if (count >= nr_huge_pages) | |
315 | return; | |
316 | } | |
317 | } | |
318 | } | |
319 | #else | |
320 | static inline void try_to_free_low(unsigned long count) | |
321 | { | |
322 | } | |
323 | #endif | |
324 | ||
325 | static unsigned long set_max_huge_pages(unsigned long count) | |
326 | { | |
327 | while (count > nr_huge_pages) { | |
a482289d | 328 | if (!alloc_fresh_huge_page()) |
1da177e4 | 329 | return nr_huge_pages; |
1da177e4 LT |
330 | } |
331 | if (count >= nr_huge_pages) | |
332 | return nr_huge_pages; | |
333 | ||
334 | spin_lock(&hugetlb_lock); | |
335 | try_to_free_low(count); | |
336 | while (count < nr_huge_pages) { | |
5da7ca86 | 337 | struct page *page = dequeue_huge_page(NULL, 0); |
1da177e4 LT |
338 | if (!page) |
339 | break; | |
340 | update_and_free_page(page); | |
341 | } | |
342 | spin_unlock(&hugetlb_lock); | |
343 | return nr_huge_pages; | |
344 | } | |
345 | ||
346 | int hugetlb_sysctl_handler(struct ctl_table *table, int write, | |
347 | struct file *file, void __user *buffer, | |
348 | size_t *length, loff_t *ppos) | |
349 | { | |
350 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); | |
351 | max_huge_pages = set_max_huge_pages(max_huge_pages); | |
352 | return 0; | |
353 | } | |
354 | #endif /* CONFIG_SYSCTL */ | |
355 | ||
356 | int hugetlb_report_meminfo(char *buf) | |
357 | { | |
358 | return sprintf(buf, | |
359 | "HugePages_Total: %5lu\n" | |
360 | "HugePages_Free: %5lu\n" | |
b45b5bd6 | 361 | "HugePages_Rsvd: %5lu\n" |
1da177e4 LT |
362 | "Hugepagesize: %5lu kB\n", |
363 | nr_huge_pages, | |
364 | free_huge_pages, | |
b45b5bd6 | 365 | reserved_huge_pages, |
1da177e4 LT |
366 | HPAGE_SIZE/1024); |
367 | } | |
368 | ||
369 | int hugetlb_report_node_meminfo(int nid, char *buf) | |
370 | { | |
371 | return sprintf(buf, | |
372 | "Node %d HugePages_Total: %5u\n" | |
373 | "Node %d HugePages_Free: %5u\n", | |
374 | nid, nr_huge_pages_node[nid], | |
375 | nid, free_huge_pages_node[nid]); | |
376 | } | |
377 | ||
1da177e4 LT |
378 | /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ |
379 | unsigned long hugetlb_total_pages(void) | |
380 | { | |
381 | return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE); | |
382 | } | |
1da177e4 LT |
383 | |
384 | /* | |
385 | * We cannot handle pagefaults against hugetlb pages at all. They cause | |
386 | * handle_mm_fault() to try to instantiate regular-sized pages in the | |
387 | * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get | |
388 | * this far. | |
389 | */ | |
390 | static struct page *hugetlb_nopage(struct vm_area_struct *vma, | |
391 | unsigned long address, int *unused) | |
392 | { | |
393 | BUG(); | |
394 | return NULL; | |
395 | } | |
396 | ||
397 | struct vm_operations_struct hugetlb_vm_ops = { | |
398 | .nopage = hugetlb_nopage, | |
399 | }; | |
400 | ||
1e8f889b DG |
401 | static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, |
402 | int writable) | |
63551ae0 DG |
403 | { |
404 | pte_t entry; | |
405 | ||
1e8f889b | 406 | if (writable) { |
63551ae0 DG |
407 | entry = |
408 | pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); | |
409 | } else { | |
410 | entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot)); | |
411 | } | |
412 | entry = pte_mkyoung(entry); | |
413 | entry = pte_mkhuge(entry); | |
414 | ||
415 | return entry; | |
416 | } | |
417 | ||
1e8f889b DG |
418 | static void set_huge_ptep_writable(struct vm_area_struct *vma, |
419 | unsigned long address, pte_t *ptep) | |
420 | { | |
421 | pte_t entry; | |
422 | ||
423 | entry = pte_mkwrite(pte_mkdirty(*ptep)); | |
424 | ptep_set_access_flags(vma, address, ptep, entry, 1); | |
425 | update_mmu_cache(vma, address, entry); | |
426 | lazy_mmu_prot_update(entry); | |
427 | } | |
428 | ||
429 | ||
63551ae0 DG |
430 | int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, |
431 | struct vm_area_struct *vma) | |
432 | { | |
433 | pte_t *src_pte, *dst_pte, entry; | |
434 | struct page *ptepage; | |
1c59827d | 435 | unsigned long addr; |
1e8f889b DG |
436 | int cow; |
437 | ||
438 | cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; | |
63551ae0 | 439 | |
1c59827d | 440 | for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) { |
c74df32c HD |
441 | src_pte = huge_pte_offset(src, addr); |
442 | if (!src_pte) | |
443 | continue; | |
63551ae0 DG |
444 | dst_pte = huge_pte_alloc(dst, addr); |
445 | if (!dst_pte) | |
446 | goto nomem; | |
c74df32c | 447 | spin_lock(&dst->page_table_lock); |
1c59827d | 448 | spin_lock(&src->page_table_lock); |
c74df32c | 449 | if (!pte_none(*src_pte)) { |
1e8f889b DG |
450 | if (cow) |
451 | ptep_set_wrprotect(src, addr, src_pte); | |
1c59827d HD |
452 | entry = *src_pte; |
453 | ptepage = pte_page(entry); | |
454 | get_page(ptepage); | |
4294621f | 455 | add_mm_counter(dst, file_rss, HPAGE_SIZE / PAGE_SIZE); |
1c59827d HD |
456 | set_huge_pte_at(dst, addr, dst_pte, entry); |
457 | } | |
458 | spin_unlock(&src->page_table_lock); | |
c74df32c | 459 | spin_unlock(&dst->page_table_lock); |
63551ae0 DG |
460 | } |
461 | return 0; | |
462 | ||
463 | nomem: | |
464 | return -ENOMEM; | |
465 | } | |
466 | ||
467 | void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, | |
468 | unsigned long end) | |
469 | { | |
470 | struct mm_struct *mm = vma->vm_mm; | |
471 | unsigned long address; | |
c7546f8f | 472 | pte_t *ptep; |
63551ae0 DG |
473 | pte_t pte; |
474 | struct page *page; | |
475 | ||
476 | WARN_ON(!is_vm_hugetlb_page(vma)); | |
477 | BUG_ON(start & ~HPAGE_MASK); | |
478 | BUG_ON(end & ~HPAGE_MASK); | |
479 | ||
508034a3 HD |
480 | spin_lock(&mm->page_table_lock); |
481 | ||
365e9c87 HD |
482 | /* Update high watermark before we lower rss */ |
483 | update_hiwater_rss(mm); | |
484 | ||
63551ae0 | 485 | for (address = start; address < end; address += HPAGE_SIZE) { |
c7546f8f | 486 | ptep = huge_pte_offset(mm, address); |
4c887265 | 487 | if (!ptep) |
c7546f8f DG |
488 | continue; |
489 | ||
490 | pte = huge_ptep_get_and_clear(mm, address, ptep); | |
63551ae0 DG |
491 | if (pte_none(pte)) |
492 | continue; | |
c7546f8f | 493 | |
63551ae0 DG |
494 | page = pte_page(pte); |
495 | put_page(page); | |
4294621f | 496 | add_mm_counter(mm, file_rss, (int) -(HPAGE_SIZE / PAGE_SIZE)); |
63551ae0 | 497 | } |
63551ae0 | 498 | |
1da177e4 | 499 | spin_unlock(&mm->page_table_lock); |
508034a3 | 500 | flush_tlb_range(vma, start, end); |
1da177e4 | 501 | } |
63551ae0 | 502 | |
1e8f889b DG |
503 | static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, |
504 | unsigned long address, pte_t *ptep, pte_t pte) | |
505 | { | |
506 | struct page *old_page, *new_page; | |
79ac6ba4 | 507 | int avoidcopy; |
1e8f889b DG |
508 | |
509 | old_page = pte_page(pte); | |
510 | ||
511 | /* If no-one else is actually using this page, avoid the copy | |
512 | * and just make the page writable */ | |
513 | avoidcopy = (page_count(old_page) == 1); | |
514 | if (avoidcopy) { | |
515 | set_huge_ptep_writable(vma, address, ptep); | |
516 | return VM_FAULT_MINOR; | |
517 | } | |
518 | ||
519 | page_cache_get(old_page); | |
5da7ca86 | 520 | new_page = alloc_huge_page(vma, address); |
1e8f889b DG |
521 | |
522 | if (!new_page) { | |
523 | page_cache_release(old_page); | |
0df420d8 | 524 | return VM_FAULT_OOM; |
1e8f889b DG |
525 | } |
526 | ||
527 | spin_unlock(&mm->page_table_lock); | |
79ac6ba4 | 528 | copy_huge_page(new_page, old_page, address); |
1e8f889b DG |
529 | spin_lock(&mm->page_table_lock); |
530 | ||
531 | ptep = huge_pte_offset(mm, address & HPAGE_MASK); | |
532 | if (likely(pte_same(*ptep, pte))) { | |
533 | /* Break COW */ | |
534 | set_huge_pte_at(mm, address, ptep, | |
535 | make_huge_pte(vma, new_page, 1)); | |
536 | /* Make the old page be freed below */ | |
537 | new_page = old_page; | |
538 | } | |
539 | page_cache_release(new_page); | |
540 | page_cache_release(old_page); | |
541 | return VM_FAULT_MINOR; | |
542 | } | |
543 | ||
86e5216f | 544 | int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, |
1e8f889b | 545 | unsigned long address, pte_t *ptep, int write_access) |
ac9b9c66 HD |
546 | { |
547 | int ret = VM_FAULT_SIGBUS; | |
4c887265 AL |
548 | unsigned long idx; |
549 | unsigned long size; | |
4c887265 AL |
550 | struct page *page; |
551 | struct address_space *mapping; | |
1e8f889b | 552 | pte_t new_pte; |
4c887265 | 553 | |
4c887265 AL |
554 | mapping = vma->vm_file->f_mapping; |
555 | idx = ((address - vma->vm_start) >> HPAGE_SHIFT) | |
556 | + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT)); | |
557 | ||
558 | /* | |
559 | * Use page lock to guard against racing truncation | |
560 | * before we get page_table_lock. | |
561 | */ | |
6bda666a CL |
562 | retry: |
563 | page = find_lock_page(mapping, idx); | |
564 | if (!page) { | |
565 | if (hugetlb_get_quota(mapping)) | |
566 | goto out; | |
567 | page = alloc_huge_page(vma, address); | |
568 | if (!page) { | |
569 | hugetlb_put_quota(mapping); | |
0df420d8 | 570 | ret = VM_FAULT_OOM; |
6bda666a CL |
571 | goto out; |
572 | } | |
79ac6ba4 | 573 | clear_huge_page(page, address); |
ac9b9c66 | 574 | |
6bda666a CL |
575 | if (vma->vm_flags & VM_SHARED) { |
576 | int err; | |
577 | ||
578 | err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); | |
579 | if (err) { | |
580 | put_page(page); | |
581 | hugetlb_put_quota(mapping); | |
582 | if (err == -EEXIST) | |
583 | goto retry; | |
584 | goto out; | |
585 | } | |
586 | } else | |
587 | lock_page(page); | |
588 | } | |
1e8f889b | 589 | |
ac9b9c66 | 590 | spin_lock(&mm->page_table_lock); |
4c887265 AL |
591 | size = i_size_read(mapping->host) >> HPAGE_SHIFT; |
592 | if (idx >= size) | |
593 | goto backout; | |
594 | ||
595 | ret = VM_FAULT_MINOR; | |
86e5216f | 596 | if (!pte_none(*ptep)) |
4c887265 AL |
597 | goto backout; |
598 | ||
599 | add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE); | |
1e8f889b DG |
600 | new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) |
601 | && (vma->vm_flags & VM_SHARED))); | |
602 | set_huge_pte_at(mm, address, ptep, new_pte); | |
603 | ||
604 | if (write_access && !(vma->vm_flags & VM_SHARED)) { | |
605 | /* Optimization, do the COW without a second fault */ | |
606 | ret = hugetlb_cow(mm, vma, address, ptep, new_pte); | |
607 | } | |
608 | ||
ac9b9c66 | 609 | spin_unlock(&mm->page_table_lock); |
4c887265 AL |
610 | unlock_page(page); |
611 | out: | |
ac9b9c66 | 612 | return ret; |
4c887265 AL |
613 | |
614 | backout: | |
615 | spin_unlock(&mm->page_table_lock); | |
616 | hugetlb_put_quota(mapping); | |
617 | unlock_page(page); | |
618 | put_page(page); | |
619 | goto out; | |
ac9b9c66 HD |
620 | } |
621 | ||
86e5216f AL |
622 | int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, |
623 | unsigned long address, int write_access) | |
624 | { | |
625 | pte_t *ptep; | |
626 | pte_t entry; | |
1e8f889b | 627 | int ret; |
3935baa9 | 628 | static DEFINE_MUTEX(hugetlb_instantiation_mutex); |
86e5216f AL |
629 | |
630 | ptep = huge_pte_alloc(mm, address); | |
631 | if (!ptep) | |
632 | return VM_FAULT_OOM; | |
633 | ||
3935baa9 DG |
634 | /* |
635 | * Serialize hugepage allocation and instantiation, so that we don't | |
636 | * get spurious allocation failures if two CPUs race to instantiate | |
637 | * the same page in the page cache. | |
638 | */ | |
639 | mutex_lock(&hugetlb_instantiation_mutex); | |
86e5216f | 640 | entry = *ptep; |
3935baa9 DG |
641 | if (pte_none(entry)) { |
642 | ret = hugetlb_no_page(mm, vma, address, ptep, write_access); | |
643 | mutex_unlock(&hugetlb_instantiation_mutex); | |
644 | return ret; | |
645 | } | |
86e5216f | 646 | |
1e8f889b DG |
647 | ret = VM_FAULT_MINOR; |
648 | ||
649 | spin_lock(&mm->page_table_lock); | |
650 | /* Check for a racing update before calling hugetlb_cow */ | |
651 | if (likely(pte_same(entry, *ptep))) | |
652 | if (write_access && !pte_write(entry)) | |
653 | ret = hugetlb_cow(mm, vma, address, ptep, entry); | |
654 | spin_unlock(&mm->page_table_lock); | |
3935baa9 | 655 | mutex_unlock(&hugetlb_instantiation_mutex); |
1e8f889b DG |
656 | |
657 | return ret; | |
86e5216f AL |
658 | } |
659 | ||
63551ae0 DG |
660 | int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, |
661 | struct page **pages, struct vm_area_struct **vmas, | |
662 | unsigned long *position, int *length, int i) | |
663 | { | |
664 | unsigned long vpfn, vaddr = *position; | |
665 | int remainder = *length; | |
666 | ||
63551ae0 | 667 | vpfn = vaddr/PAGE_SIZE; |
1c59827d | 668 | spin_lock(&mm->page_table_lock); |
63551ae0 | 669 | while (vaddr < vma->vm_end && remainder) { |
4c887265 AL |
670 | pte_t *pte; |
671 | struct page *page; | |
63551ae0 | 672 | |
4c887265 AL |
673 | /* |
674 | * Some archs (sparc64, sh*) have multiple pte_ts to | |
675 | * each hugepage. We have to make * sure we get the | |
676 | * first, for the page indexing below to work. | |
677 | */ | |
678 | pte = huge_pte_offset(mm, vaddr & HPAGE_MASK); | |
63551ae0 | 679 | |
4c887265 AL |
680 | if (!pte || pte_none(*pte)) { |
681 | int ret; | |
63551ae0 | 682 | |
4c887265 AL |
683 | spin_unlock(&mm->page_table_lock); |
684 | ret = hugetlb_fault(mm, vma, vaddr, 0); | |
685 | spin_lock(&mm->page_table_lock); | |
686 | if (ret == VM_FAULT_MINOR) | |
687 | continue; | |
63551ae0 | 688 | |
4c887265 AL |
689 | remainder = 0; |
690 | if (!i) | |
691 | i = -EFAULT; | |
692 | break; | |
693 | } | |
694 | ||
695 | if (pages) { | |
696 | page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)]; | |
63551ae0 DG |
697 | get_page(page); |
698 | pages[i] = page; | |
699 | } | |
700 | ||
701 | if (vmas) | |
702 | vmas[i] = vma; | |
703 | ||
704 | vaddr += PAGE_SIZE; | |
705 | ++vpfn; | |
706 | --remainder; | |
707 | ++i; | |
708 | } | |
1c59827d | 709 | spin_unlock(&mm->page_table_lock); |
63551ae0 DG |
710 | *length = remainder; |
711 | *position = vaddr; | |
712 | ||
713 | return i; | |
714 | } | |
8f860591 ZY |
715 | |
716 | void hugetlb_change_protection(struct vm_area_struct *vma, | |
717 | unsigned long address, unsigned long end, pgprot_t newprot) | |
718 | { | |
719 | struct mm_struct *mm = vma->vm_mm; | |
720 | unsigned long start = address; | |
721 | pte_t *ptep; | |
722 | pte_t pte; | |
723 | ||
724 | BUG_ON(address >= end); | |
725 | flush_cache_range(vma, address, end); | |
726 | ||
727 | spin_lock(&mm->page_table_lock); | |
728 | for (; address < end; address += HPAGE_SIZE) { | |
729 | ptep = huge_pte_offset(mm, address); | |
730 | if (!ptep) | |
731 | continue; | |
732 | if (!pte_none(*ptep)) { | |
733 | pte = huge_ptep_get_and_clear(mm, address, ptep); | |
734 | pte = pte_mkhuge(pte_modify(pte, newprot)); | |
735 | set_huge_pte_at(mm, address, ptep, pte); | |
736 | lazy_mmu_prot_update(pte); | |
737 | } | |
738 | } | |
739 | spin_unlock(&mm->page_table_lock); | |
740 | ||
741 | flush_tlb_range(vma, start, end); | |
742 | } | |
743 |