blob: 39d49ecea8e89170d7ab66958c006b70c0efc50c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/sysctl.h>
11#include <linux/highmem.h>
12#include <linux/nodemask.h>
David Gibson63551ae2005-06-21 17:14:44 -070013#include <linux/pagemap.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080014#include <linux/mempolicy.h>
Christoph Lameteraea47ff2006-01-08 01:00:57 -080015#include <linux/cpuset.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080016
David Gibson63551ae2005-06-21 17:14:44 -070017#include <asm/page.h>
18#include <asm/pgtable.h>
19
20#include <linux/hugetlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
23static unsigned long nr_huge_pages, free_huge_pages;
24unsigned long max_huge_pages;
25static struct list_head hugepage_freelists[MAX_NUMNODES];
26static unsigned int nr_huge_pages_node[MAX_NUMNODES];
27static unsigned int free_huge_pages_node[MAX_NUMNODES];
Eric Paris0bd0f9f2005-11-21 21:32:28 -080028
29/*
30 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static DEFINE_SPINLOCK(hugetlb_lock);
33
34static void enqueue_huge_page(struct page *page)
35{
36 int nid = page_to_nid(page);
37 list_add(&page->lru, &hugepage_freelists[nid]);
38 free_huge_pages++;
39 free_huge_pages_node[nid]++;
40}
41
Christoph Lameter5da7ca82006-01-06 00:10:46 -080042static struct page *dequeue_huge_page(struct vm_area_struct *vma,
43 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 int nid = numa_node_id();
46 struct page *page = NULL;
Christoph Lameter5da7ca82006-01-06 00:10:46 -080047 struct zonelist *zonelist = huge_zonelist(vma, address);
Christoph Lameter96df9332006-01-06 00:10:45 -080048 struct zone **z;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Christoph Lameter96df9332006-01-06 00:10:45 -080050 for (z = zonelist->zones; *z; z++) {
51 nid = (*z)->zone_pgdat->node_id;
Christoph Lameteraea47ff2006-01-08 01:00:57 -080052 if (cpuset_zone_allowed(*z, GFP_HIGHUSER) &&
53 !list_empty(&hugepage_freelists[nid]))
Christoph Lameter96df9332006-01-06 00:10:45 -080054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
Christoph Lameter96df9332006-01-06 00:10:45 -080056
57 if (*z) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 page = list_entry(hugepage_freelists[nid].next,
59 struct page, lru);
60 list_del(&page->lru);
61 free_huge_pages--;
62 free_huge_pages_node[nid]--;
63 }
64 return page;
65}
66
Nick Piggina4822892006-03-22 00:08:08 -080067static int alloc_fresh_huge_page(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 static int nid = 0;
70 struct page *page;
71 page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN,
72 HUGETLB_PAGE_ORDER);
73 nid = (nid + 1) % num_online_nodes();
74 if (page) {
Nick Piggina4822892006-03-22 00:08:08 -080075 page[1].lru.next = (void *)free_huge_page; /* dtor */
Eric Paris0bd0f9f2005-11-21 21:32:28 -080076 spin_lock(&hugetlb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 nr_huge_pages++;
78 nr_huge_pages_node[page_to_nid(page)]++;
Eric Paris0bd0f9f2005-11-21 21:32:28 -080079 spin_unlock(&hugetlb_lock);
Nick Piggina4822892006-03-22 00:08:08 -080080 put_page(page); /* free it into the hugepage allocator */
81 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Nick Piggina4822892006-03-22 00:08:08 -080083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86void free_huge_page(struct page *page)
87{
88 BUG_ON(page_count(page));
89
90 INIT_LIST_HEAD(&page->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 spin_lock(&hugetlb_lock);
93 enqueue_huge_page(page);
94 spin_unlock(&hugetlb_lock);
95}
96
Christoph Lameter5da7ca82006-01-06 00:10:46 -080097struct page *alloc_huge_page(struct vm_area_struct *vma, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct page *page;
100 int i;
101
102 spin_lock(&hugetlb_lock);
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800103 page = dequeue_huge_page(vma, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (!page) {
105 spin_unlock(&hugetlb_lock);
106 return NULL;
107 }
108 spin_unlock(&hugetlb_lock);
109 set_page_count(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); ++i)
David Gibsona2dfef62006-02-07 12:58:25 -0800111 clear_user_highpage(&page[i], addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return page;
113}
114
115static int __init hugetlb_init(void)
116{
117 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100119 if (HPAGE_SHIFT == 0)
120 return 0;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 for (i = 0; i < MAX_NUMNODES; ++i)
123 INIT_LIST_HEAD(&hugepage_freelists[i]);
124
125 for (i = 0; i < max_huge_pages; ++i) {
Nick Piggina4822892006-03-22 00:08:08 -0800126 if (!alloc_fresh_huge_page())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 max_huge_pages = free_huge_pages = nr_huge_pages = i;
130 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
131 return 0;
132}
133module_init(hugetlb_init);
134
135static int __init hugetlb_setup(char *s)
136{
137 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
138 max_huge_pages = 0;
139 return 1;
140}
141__setup("hugepages=", hugetlb_setup);
142
143#ifdef CONFIG_SYSCTL
144static void update_and_free_page(struct page *page)
145{
146 int i;
147 nr_huge_pages--;
148 nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--;
149 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
150 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
151 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
152 1 << PG_private | 1<< PG_writeback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Nick Piggina4822892006-03-22 00:08:08 -0800154 page[1].lru.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 set_page_count(page, 1);
156 __free_pages(page, HUGETLB_PAGE_ORDER);
157}
158
159#ifdef CONFIG_HIGHMEM
160static void try_to_free_low(unsigned long count)
161{
162 int i, nid;
163 for (i = 0; i < MAX_NUMNODES; ++i) {
164 struct page *page, *next;
165 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
166 if (PageHighMem(page))
167 continue;
168 list_del(&page->lru);
169 update_and_free_page(page);
170 nid = page_zone(page)->zone_pgdat->node_id;
171 free_huge_pages--;
172 free_huge_pages_node[nid]--;
173 if (count >= nr_huge_pages)
174 return;
175 }
176 }
177}
178#else
179static inline void try_to_free_low(unsigned long count)
180{
181}
182#endif
183
184static unsigned long set_max_huge_pages(unsigned long count)
185{
186 while (count > nr_huge_pages) {
Nick Piggina4822892006-03-22 00:08:08 -0800187 if (!alloc_fresh_huge_page())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return nr_huge_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 if (count >= nr_huge_pages)
191 return nr_huge_pages;
192
193 spin_lock(&hugetlb_lock);
194 try_to_free_low(count);
195 while (count < nr_huge_pages) {
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800196 struct page *page = dequeue_huge_page(NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!page)
198 break;
199 update_and_free_page(page);
200 }
201 spin_unlock(&hugetlb_lock);
202 return nr_huge_pages;
203}
204
205int hugetlb_sysctl_handler(struct ctl_table *table, int write,
206 struct file *file, void __user *buffer,
207 size_t *length, loff_t *ppos)
208{
209 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
210 max_huge_pages = set_max_huge_pages(max_huge_pages);
211 return 0;
212}
213#endif /* CONFIG_SYSCTL */
214
215int hugetlb_report_meminfo(char *buf)
216{
217 return sprintf(buf,
218 "HugePages_Total: %5lu\n"
219 "HugePages_Free: %5lu\n"
220 "Hugepagesize: %5lu kB\n",
221 nr_huge_pages,
222 free_huge_pages,
223 HPAGE_SIZE/1024);
224}
225
226int hugetlb_report_node_meminfo(int nid, char *buf)
227{
228 return sprintf(buf,
229 "Node %d HugePages_Total: %5u\n"
230 "Node %d HugePages_Free: %5u\n",
231 nid, nr_huge_pages_node[nid],
232 nid, free_huge_pages_node[nid]);
233}
234
235int is_hugepage_mem_enough(size_t size)
236{
237 return (size + ~HPAGE_MASK)/HPAGE_SIZE <= free_huge_pages;
238}
239
240/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
241unsigned long hugetlb_total_pages(void)
242{
243 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/*
247 * We cannot handle pagefaults against hugetlb pages at all. They cause
248 * handle_mm_fault() to try to instantiate regular-sized pages in the
249 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
250 * this far.
251 */
252static struct page *hugetlb_nopage(struct vm_area_struct *vma,
253 unsigned long address, int *unused)
254{
255 BUG();
256 return NULL;
257}
258
259struct vm_operations_struct hugetlb_vm_ops = {
260 .nopage = hugetlb_nopage,
261};
262
David Gibson1e8f8892006-01-06 00:10:44 -0800263static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
264 int writable)
David Gibson63551ae2005-06-21 17:14:44 -0700265{
266 pte_t entry;
267
David Gibson1e8f8892006-01-06 00:10:44 -0800268 if (writable) {
David Gibson63551ae2005-06-21 17:14:44 -0700269 entry =
270 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
271 } else {
272 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
273 }
274 entry = pte_mkyoung(entry);
275 entry = pte_mkhuge(entry);
276
277 return entry;
278}
279
David Gibson1e8f8892006-01-06 00:10:44 -0800280static void set_huge_ptep_writable(struct vm_area_struct *vma,
281 unsigned long address, pte_t *ptep)
282{
283 pte_t entry;
284
285 entry = pte_mkwrite(pte_mkdirty(*ptep));
286 ptep_set_access_flags(vma, address, ptep, entry, 1);
287 update_mmu_cache(vma, address, entry);
288 lazy_mmu_prot_update(entry);
289}
290
291
David Gibson63551ae2005-06-21 17:14:44 -0700292int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
293 struct vm_area_struct *vma)
294{
295 pte_t *src_pte, *dst_pte, entry;
296 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -0700297 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -0800298 int cow;
299
300 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -0700301
Hugh Dickins1c598272005-10-19 21:23:43 -0700302 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
Hugh Dickinsc74df322005-10-29 18:16:23 -0700303 src_pte = huge_pte_offset(src, addr);
304 if (!src_pte)
305 continue;
David Gibson63551ae2005-06-21 17:14:44 -0700306 dst_pte = huge_pte_alloc(dst, addr);
307 if (!dst_pte)
308 goto nomem;
Hugh Dickinsc74df322005-10-29 18:16:23 -0700309 spin_lock(&dst->page_table_lock);
Hugh Dickins1c598272005-10-19 21:23:43 -0700310 spin_lock(&src->page_table_lock);
Hugh Dickinsc74df322005-10-29 18:16:23 -0700311 if (!pte_none(*src_pte)) {
David Gibson1e8f8892006-01-06 00:10:44 -0800312 if (cow)
313 ptep_set_wrprotect(src, addr, src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -0700314 entry = *src_pte;
315 ptepage = pte_page(entry);
316 get_page(ptepage);
Hugh Dickins42946212005-10-29 18:16:05 -0700317 add_mm_counter(dst, file_rss, HPAGE_SIZE / PAGE_SIZE);
Hugh Dickins1c598272005-10-19 21:23:43 -0700318 set_huge_pte_at(dst, addr, dst_pte, entry);
319 }
320 spin_unlock(&src->page_table_lock);
Hugh Dickinsc74df322005-10-29 18:16:23 -0700321 spin_unlock(&dst->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -0700322 }
323 return 0;
324
325nomem:
326 return -ENOMEM;
327}
328
329void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
330 unsigned long end)
331{
332 struct mm_struct *mm = vma->vm_mm;
333 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -0700334 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -0700335 pte_t pte;
336 struct page *page;
337
338 WARN_ON(!is_vm_hugetlb_page(vma));
339 BUG_ON(start & ~HPAGE_MASK);
340 BUG_ON(end & ~HPAGE_MASK);
341
Hugh Dickins508034a2005-10-29 18:16:30 -0700342 spin_lock(&mm->page_table_lock);
343
Hugh Dickins365e9c872005-10-29 18:16:18 -0700344 /* Update high watermark before we lower rss */
345 update_hiwater_rss(mm);
346
David Gibson63551ae2005-06-21 17:14:44 -0700347 for (address = start; address < end; address += HPAGE_SIZE) {
David Gibsonc7546f82005-08-05 11:59:35 -0700348 ptep = huge_pte_offset(mm, address);
Adam Litke4c887262005-10-29 18:16:46 -0700349 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -0700350 continue;
351
352 pte = huge_ptep_get_and_clear(mm, address, ptep);
David Gibson63551ae2005-06-21 17:14:44 -0700353 if (pte_none(pte))
354 continue;
David Gibsonc7546f82005-08-05 11:59:35 -0700355
David Gibson63551ae2005-06-21 17:14:44 -0700356 page = pte_page(pte);
357 put_page(page);
Hugh Dickins42946212005-10-29 18:16:05 -0700358 add_mm_counter(mm, file_rss, (int) -(HPAGE_SIZE / PAGE_SIZE));
David Gibson63551ae2005-06-21 17:14:44 -0700359 }
David Gibson63551ae2005-06-21 17:14:44 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_unlock(&mm->page_table_lock);
Hugh Dickins508034a2005-10-29 18:16:30 -0700362 flush_tlb_range(vma, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
David Gibson63551ae2005-06-21 17:14:44 -0700364
David Gibson1e8f8892006-01-06 00:10:44 -0800365static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
366 unsigned long address, pte_t *ptep, pte_t pte)
367{
368 struct page *old_page, *new_page;
369 int i, avoidcopy;
370
371 old_page = pte_page(pte);
372
373 /* If no-one else is actually using this page, avoid the copy
374 * and just make the page writable */
375 avoidcopy = (page_count(old_page) == 1);
376 if (avoidcopy) {
377 set_huge_ptep_writable(vma, address, ptep);
378 return VM_FAULT_MINOR;
379 }
380
381 page_cache_get(old_page);
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800382 new_page = alloc_huge_page(vma, address);
David Gibson1e8f8892006-01-06 00:10:44 -0800383
384 if (!new_page) {
385 page_cache_release(old_page);
Christoph Lameter0df420d2006-02-07 12:58:30 -0800386 return VM_FAULT_OOM;
David Gibson1e8f8892006-01-06 00:10:44 -0800387 }
388
389 spin_unlock(&mm->page_table_lock);
390 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++)
391 copy_user_highpage(new_page + i, old_page + i,
392 address + i*PAGE_SIZE);
393 spin_lock(&mm->page_table_lock);
394
395 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
396 if (likely(pte_same(*ptep, pte))) {
397 /* Break COW */
398 set_huge_pte_at(mm, address, ptep,
399 make_huge_pte(vma, new_page, 1));
400 /* Make the old page be freed below */
401 new_page = old_page;
402 }
403 page_cache_release(new_page);
404 page_cache_release(old_page);
405 return VM_FAULT_MINOR;
406}
407
Adam Litke86e52162006-01-06 00:10:43 -0800408int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
David Gibson1e8f8892006-01-06 00:10:44 -0800409 unsigned long address, pte_t *ptep, int write_access)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +0100410{
411 int ret = VM_FAULT_SIGBUS;
Adam Litke4c887262005-10-29 18:16:46 -0700412 unsigned long idx;
413 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -0700414 struct page *page;
415 struct address_space *mapping;
David Gibson1e8f8892006-01-06 00:10:44 -0800416 pte_t new_pte;
Adam Litke4c887262005-10-29 18:16:46 -0700417
Adam Litke4c887262005-10-29 18:16:46 -0700418 mapping = vma->vm_file->f_mapping;
419 idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
420 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
421
422 /*
423 * Use page lock to guard against racing truncation
424 * before we get page_table_lock.
425 */
Christoph Lameter6bda6662006-01-06 00:10:49 -0800426retry:
427 page = find_lock_page(mapping, idx);
428 if (!page) {
429 if (hugetlb_get_quota(mapping))
430 goto out;
431 page = alloc_huge_page(vma, address);
432 if (!page) {
433 hugetlb_put_quota(mapping);
Christoph Lameter0df420d2006-02-07 12:58:30 -0800434 ret = VM_FAULT_OOM;
Christoph Lameter6bda6662006-01-06 00:10:49 -0800435 goto out;
436 }
Hugh Dickinsac9b9c62005-10-20 16:24:28 +0100437
Christoph Lameter6bda6662006-01-06 00:10:49 -0800438 if (vma->vm_flags & VM_SHARED) {
439 int err;
440
441 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
442 if (err) {
443 put_page(page);
444 hugetlb_put_quota(mapping);
445 if (err == -EEXIST)
446 goto retry;
447 goto out;
448 }
449 } else
450 lock_page(page);
451 }
David Gibson1e8f8892006-01-06 00:10:44 -0800452
Hugh Dickinsac9b9c62005-10-20 16:24:28 +0100453 spin_lock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -0700454 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
455 if (idx >= size)
456 goto backout;
457
458 ret = VM_FAULT_MINOR;
Adam Litke86e52162006-01-06 00:10:43 -0800459 if (!pte_none(*ptep))
Adam Litke4c887262005-10-29 18:16:46 -0700460 goto backout;
461
462 add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE);
David Gibson1e8f8892006-01-06 00:10:44 -0800463 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
464 && (vma->vm_flags & VM_SHARED)));
465 set_huge_pte_at(mm, address, ptep, new_pte);
466
467 if (write_access && !(vma->vm_flags & VM_SHARED)) {
468 /* Optimization, do the COW without a second fault */
469 ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
470 }
471
Hugh Dickinsac9b9c62005-10-20 16:24:28 +0100472 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -0700473 unlock_page(page);
474out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +0100475 return ret;
Adam Litke4c887262005-10-29 18:16:46 -0700476
477backout:
478 spin_unlock(&mm->page_table_lock);
479 hugetlb_put_quota(mapping);
480 unlock_page(page);
481 put_page(page);
482 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +0100483}
484
Adam Litke86e52162006-01-06 00:10:43 -0800485int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
486 unsigned long address, int write_access)
487{
488 pte_t *ptep;
489 pte_t entry;
David Gibson1e8f8892006-01-06 00:10:44 -0800490 int ret;
Adam Litke86e52162006-01-06 00:10:43 -0800491
492 ptep = huge_pte_alloc(mm, address);
493 if (!ptep)
494 return VM_FAULT_OOM;
495
496 entry = *ptep;
497 if (pte_none(entry))
David Gibson1e8f8892006-01-06 00:10:44 -0800498 return hugetlb_no_page(mm, vma, address, ptep, write_access);
Adam Litke86e52162006-01-06 00:10:43 -0800499
David Gibson1e8f8892006-01-06 00:10:44 -0800500 ret = VM_FAULT_MINOR;
501
502 spin_lock(&mm->page_table_lock);
503 /* Check for a racing update before calling hugetlb_cow */
504 if (likely(pte_same(entry, *ptep)))
505 if (write_access && !pte_write(entry))
506 ret = hugetlb_cow(mm, vma, address, ptep, entry);
507 spin_unlock(&mm->page_table_lock);
508
509 return ret;
Adam Litke86e52162006-01-06 00:10:43 -0800510}
511
David Gibson63551ae2005-06-21 17:14:44 -0700512int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
513 struct page **pages, struct vm_area_struct **vmas,
514 unsigned long *position, int *length, int i)
515{
516 unsigned long vpfn, vaddr = *position;
517 int remainder = *length;
518
David Gibson63551ae2005-06-21 17:14:44 -0700519 vpfn = vaddr/PAGE_SIZE;
Hugh Dickins1c598272005-10-19 21:23:43 -0700520 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -0700521 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -0700522 pte_t *pte;
523 struct page *page;
524
525 /*
526 * Some archs (sparc64, sh*) have multiple pte_ts to
527 * each hugepage. We have to make * sure we get the
528 * first, for the page indexing below to work.
529 */
530 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
531
532 if (!pte || pte_none(*pte)) {
533 int ret;
534
535 spin_unlock(&mm->page_table_lock);
536 ret = hugetlb_fault(mm, vma, vaddr, 0);
537 spin_lock(&mm->page_table_lock);
538 if (ret == VM_FAULT_MINOR)
539 continue;
540
541 remainder = 0;
542 if (!i)
543 i = -EFAULT;
544 break;
545 }
David Gibson63551ae2005-06-21 17:14:44 -0700546
547 if (pages) {
David Gibson63551ae2005-06-21 17:14:44 -0700548 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
David Gibson63551ae2005-06-21 17:14:44 -0700549 get_page(page);
550 pages[i] = page;
551 }
552
553 if (vmas)
554 vmas[i] = vma;
555
556 vaddr += PAGE_SIZE;
557 ++vpfn;
558 --remainder;
559 ++i;
560 }
Hugh Dickins1c598272005-10-19 21:23:43 -0700561 spin_unlock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -0700562 *length = remainder;
563 *position = vaddr;
564
565 return i;
566}