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