2 * Generic hugetlb support.
3 * (C) William Irwin, April 2004
6 #include <linux/list.h>
7 #include <linux/init.h>
8 #include <linux/module.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>
19 #include <asm/pgtable.h>
21 #include <linux/hugetlb.h>
24 const unsigned long hugetlb_zero
= 0, hugetlb_infinity
= ~0UL;
25 static unsigned long nr_huge_pages
, free_huge_pages
, resv_huge_pages
;
26 static unsigned long surplus_huge_pages
;
27 static unsigned long nr_overcommit_huge_pages
;
28 unsigned long max_huge_pages
;
29 unsigned long sysctl_overcommit_huge_pages
;
30 static struct list_head hugepage_freelists
[MAX_NUMNODES
];
31 static unsigned int nr_huge_pages_node
[MAX_NUMNODES
];
32 static unsigned int free_huge_pages_node
[MAX_NUMNODES
];
33 static unsigned int surplus_huge_pages_node
[MAX_NUMNODES
];
34 static gfp_t htlb_alloc_mask
= GFP_HIGHUSER
;
35 unsigned long hugepages_treat_as_movable
;
36 static int hugetlb_next_nid
;
39 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
41 static DEFINE_SPINLOCK(hugetlb_lock
);
44 * These helpers are used to track how many pages are reserved for
45 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
46 * is guaranteed to have their future faults succeed.
48 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
49 * the reserve counters are updated with the hugetlb_lock held. It is safe
50 * to reset the VMA at fork() time as it is not in use yet and there is no
51 * chance of the global counters getting corrupted as a result of the values.
53 static unsigned long vma_resv_huge_pages(struct vm_area_struct
*vma
)
55 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
56 if (!(vma
->vm_flags
& VM_SHARED
))
57 return (unsigned long)vma
->vm_private_data
;
61 static void set_vma_resv_huge_pages(struct vm_area_struct
*vma
,
62 unsigned long reserve
)
64 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
65 VM_BUG_ON(vma
->vm_flags
& VM_SHARED
);
67 vma
->vm_private_data
= (void *)reserve
;
70 /* Decrement the reserved pages in the hugepage pool by one */
71 static void decrement_hugepage_resv_vma(struct vm_area_struct
*vma
)
73 if (vma
->vm_flags
& VM_SHARED
) {
74 /* Shared mappings always use reserves */
78 * Only the process that called mmap() has reserves for
81 if (vma_resv_huge_pages(vma
)) {
83 reserve
= (unsigned long)vma
->vm_private_data
- 1;
84 vma
->vm_private_data
= (void *)reserve
;
89 void reset_vma_resv_huge_pages(struct vm_area_struct
*vma
)
91 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
92 if (!(vma
->vm_flags
& VM_SHARED
))
93 vma
->vm_private_data
= (void *)0;
96 /* Returns true if the VMA has associated reserve pages */
97 static int vma_has_private_reserves(struct vm_area_struct
*vma
)
99 if (vma
->vm_flags
& VM_SHARED
)
101 if (!vma_resv_huge_pages(vma
))
106 static void clear_huge_page(struct page
*page
, unsigned long addr
)
111 for (i
= 0; i
< (HPAGE_SIZE
/PAGE_SIZE
); i
++) {
113 clear_user_highpage(page
+ i
, addr
+ i
* PAGE_SIZE
);
117 static void copy_huge_page(struct page
*dst
, struct page
*src
,
118 unsigned long addr
, struct vm_area_struct
*vma
)
123 for (i
= 0; i
< HPAGE_SIZE
/PAGE_SIZE
; i
++) {
125 copy_user_highpage(dst
+ i
, src
+ i
, addr
+ i
*PAGE_SIZE
, vma
);
129 static void enqueue_huge_page(struct page
*page
)
131 int nid
= page_to_nid(page
);
132 list_add(&page
->lru
, &hugepage_freelists
[nid
]);
134 free_huge_pages_node
[nid
]++;
137 static struct page
*dequeue_huge_page(void)
140 struct page
*page
= NULL
;
142 for (nid
= 0; nid
< MAX_NUMNODES
; ++nid
) {
143 if (!list_empty(&hugepage_freelists
[nid
])) {
144 page
= list_entry(hugepage_freelists
[nid
].next
,
146 list_del(&page
->lru
);
148 free_huge_pages_node
[nid
]--;
155 static struct page
*dequeue_huge_page_vma(struct vm_area_struct
*vma
,
156 unsigned long address
)
159 struct page
*page
= NULL
;
160 struct mempolicy
*mpol
;
161 nodemask_t
*nodemask
;
162 struct zonelist
*zonelist
= huge_zonelist(vma
, address
,
163 htlb_alloc_mask
, &mpol
, &nodemask
);
168 * A child process with MAP_PRIVATE mappings created by their parent
169 * have no page reserves. This check ensures that reservations are
170 * not "stolen". The child may still get SIGKILLed
172 if (!vma_has_private_reserves(vma
) &&
173 free_huge_pages
- resv_huge_pages
== 0)
176 for_each_zone_zonelist_nodemask(zone
, z
, zonelist
,
177 MAX_NR_ZONES
- 1, nodemask
) {
178 nid
= zone_to_nid(zone
);
179 if (cpuset_zone_allowed_softwall(zone
, htlb_alloc_mask
) &&
180 !list_empty(&hugepage_freelists
[nid
])) {
181 page
= list_entry(hugepage_freelists
[nid
].next
,
183 list_del(&page
->lru
);
185 free_huge_pages_node
[nid
]--;
186 decrement_hugepage_resv_vma(vma
);
195 static void update_and_free_page(struct page
*page
)
199 nr_huge_pages_node
[page_to_nid(page
)]--;
200 for (i
= 0; i
< (HPAGE_SIZE
/ PAGE_SIZE
); i
++) {
201 page
[i
].flags
&= ~(1 << PG_locked
| 1 << PG_error
| 1 << PG_referenced
|
202 1 << PG_dirty
| 1 << PG_active
| 1 << PG_reserved
|
203 1 << PG_private
| 1<< PG_writeback
);
205 set_compound_page_dtor(page
, NULL
);
206 set_page_refcounted(page
);
207 arch_release_hugepage(page
);
208 __free_pages(page
, HUGETLB_PAGE_ORDER
);
211 static void free_huge_page(struct page
*page
)
213 int nid
= page_to_nid(page
);
214 struct address_space
*mapping
;
216 mapping
= (struct address_space
*) page_private(page
);
217 set_page_private(page
, 0);
218 BUG_ON(page_count(page
));
219 INIT_LIST_HEAD(&page
->lru
);
221 spin_lock(&hugetlb_lock
);
222 if (surplus_huge_pages_node
[nid
]) {
223 update_and_free_page(page
);
224 surplus_huge_pages
--;
225 surplus_huge_pages_node
[nid
]--;
227 enqueue_huge_page(page
);
229 spin_unlock(&hugetlb_lock
);
231 hugetlb_put_quota(mapping
, 1);
235 * Increment or decrement surplus_huge_pages. Keep node-specific counters
236 * balanced by operating on them in a round-robin fashion.
237 * Returns 1 if an adjustment was made.
239 static int adjust_pool_surplus(int delta
)
245 VM_BUG_ON(delta
!= -1 && delta
!= 1);
247 nid
= next_node(nid
, node_online_map
);
248 if (nid
== MAX_NUMNODES
)
249 nid
= first_node(node_online_map
);
251 /* To shrink on this node, there must be a surplus page */
252 if (delta
< 0 && !surplus_huge_pages_node
[nid
])
254 /* Surplus cannot exceed the total number of pages */
255 if (delta
> 0 && surplus_huge_pages_node
[nid
] >=
256 nr_huge_pages_node
[nid
])
259 surplus_huge_pages
+= delta
;
260 surplus_huge_pages_node
[nid
] += delta
;
263 } while (nid
!= prev_nid
);
269 static struct page
*alloc_fresh_huge_page_node(int nid
)
273 page
= alloc_pages_node(nid
,
274 htlb_alloc_mask
|__GFP_COMP
|__GFP_THISNODE
|
275 __GFP_REPEAT
|__GFP_NOWARN
,
278 if (arch_prepare_hugepage(page
)) {
279 __free_pages(page
, HUGETLB_PAGE_ORDER
);
282 set_compound_page_dtor(page
, free_huge_page
);
283 spin_lock(&hugetlb_lock
);
285 nr_huge_pages_node
[nid
]++;
286 spin_unlock(&hugetlb_lock
);
287 put_page(page
); /* free it into the hugepage allocator */
293 static int alloc_fresh_huge_page(void)
300 start_nid
= hugetlb_next_nid
;
303 page
= alloc_fresh_huge_page_node(hugetlb_next_nid
);
307 * Use a helper variable to find the next node and then
308 * copy it back to hugetlb_next_nid afterwards:
309 * otherwise there's a window in which a racer might
310 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
311 * But we don't need to use a spin_lock here: it really
312 * doesn't matter if occasionally a racer chooses the
313 * same nid as we do. Move nid forward in the mask even
314 * if we just successfully allocated a hugepage so that
315 * the next caller gets hugepages on the next node.
317 next_nid
= next_node(hugetlb_next_nid
, node_online_map
);
318 if (next_nid
== MAX_NUMNODES
)
319 next_nid
= first_node(node_online_map
);
320 hugetlb_next_nid
= next_nid
;
321 } while (!page
&& hugetlb_next_nid
!= start_nid
);
324 count_vm_event(HTLB_BUDDY_PGALLOC
);
326 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL
);
331 static struct page
*alloc_buddy_huge_page(struct vm_area_struct
*vma
,
332 unsigned long address
)
338 * Assume we will successfully allocate the surplus page to
339 * prevent racing processes from causing the surplus to exceed
342 * This however introduces a different race, where a process B
343 * tries to grow the static hugepage pool while alloc_pages() is
344 * called by process A. B will only examine the per-node
345 * counters in determining if surplus huge pages can be
346 * converted to normal huge pages in adjust_pool_surplus(). A
347 * won't be able to increment the per-node counter, until the
348 * lock is dropped by B, but B doesn't drop hugetlb_lock until
349 * no more huge pages can be converted from surplus to normal
350 * state (and doesn't try to convert again). Thus, we have a
351 * case where a surplus huge page exists, the pool is grown, and
352 * the surplus huge page still exists after, even though it
353 * should just have been converted to a normal huge page. This
354 * does not leak memory, though, as the hugepage will be freed
355 * once it is out of use. It also does not allow the counters to
356 * go out of whack in adjust_pool_surplus() as we don't modify
357 * the node values until we've gotten the hugepage and only the
358 * per-node value is checked there.
360 spin_lock(&hugetlb_lock
);
361 if (surplus_huge_pages
>= nr_overcommit_huge_pages
) {
362 spin_unlock(&hugetlb_lock
);
366 surplus_huge_pages
++;
368 spin_unlock(&hugetlb_lock
);
370 page
= alloc_pages(htlb_alloc_mask
|__GFP_COMP
|
371 __GFP_REPEAT
|__GFP_NOWARN
,
374 spin_lock(&hugetlb_lock
);
377 * This page is now managed by the hugetlb allocator and has
378 * no users -- drop the buddy allocator's reference.
380 put_page_testzero(page
);
381 VM_BUG_ON(page_count(page
));
382 nid
= page_to_nid(page
);
383 set_compound_page_dtor(page
, free_huge_page
);
385 * We incremented the global counters already
387 nr_huge_pages_node
[nid
]++;
388 surplus_huge_pages_node
[nid
]++;
389 __count_vm_event(HTLB_BUDDY_PGALLOC
);
392 surplus_huge_pages
--;
393 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL
);
395 spin_unlock(&hugetlb_lock
);
401 * Increase the hugetlb pool such that it can accomodate a reservation
404 static int gather_surplus_pages(int delta
)
406 struct list_head surplus_list
;
407 struct page
*page
, *tmp
;
409 int needed
, allocated
;
411 needed
= (resv_huge_pages
+ delta
) - free_huge_pages
;
413 resv_huge_pages
+= delta
;
418 INIT_LIST_HEAD(&surplus_list
);
422 spin_unlock(&hugetlb_lock
);
423 for (i
= 0; i
< needed
; i
++) {
424 page
= alloc_buddy_huge_page(NULL
, 0);
427 * We were not able to allocate enough pages to
428 * satisfy the entire reservation so we free what
429 * we've allocated so far.
431 spin_lock(&hugetlb_lock
);
436 list_add(&page
->lru
, &surplus_list
);
441 * After retaking hugetlb_lock, we need to recalculate 'needed'
442 * because either resv_huge_pages or free_huge_pages may have changed.
444 spin_lock(&hugetlb_lock
);
445 needed
= (resv_huge_pages
+ delta
) - (free_huge_pages
+ allocated
);
450 * The surplus_list now contains _at_least_ the number of extra pages
451 * needed to accomodate the reservation. Add the appropriate number
452 * of pages to the hugetlb pool and free the extras back to the buddy
453 * allocator. Commit the entire reservation here to prevent another
454 * process from stealing the pages as they are added to the pool but
455 * before they are reserved.
458 resv_huge_pages
+= delta
;
461 /* Free the needed pages to the hugetlb pool */
462 list_for_each_entry_safe(page
, tmp
, &surplus_list
, lru
) {
465 list_del(&page
->lru
);
466 enqueue_huge_page(page
);
469 /* Free unnecessary surplus pages to the buddy allocator */
470 if (!list_empty(&surplus_list
)) {
471 spin_unlock(&hugetlb_lock
);
472 list_for_each_entry_safe(page
, tmp
, &surplus_list
, lru
) {
473 list_del(&page
->lru
);
475 * The page has a reference count of zero already, so
476 * call free_huge_page directly instead of using
477 * put_page. This must be done with hugetlb_lock
478 * unlocked which is safe because free_huge_page takes
479 * hugetlb_lock before deciding how to free the page.
481 free_huge_page(page
);
483 spin_lock(&hugetlb_lock
);
490 * When releasing a hugetlb pool reservation, any surplus pages that were
491 * allocated to satisfy the reservation must be explicitly freed if they were
494 static void return_unused_surplus_pages(unsigned long unused_resv_pages
)
498 unsigned long nr_pages
;
501 * We want to release as many surplus pages as possible, spread
502 * evenly across all nodes. Iterate across all nodes until we
503 * can no longer free unreserved surplus pages. This occurs when
504 * the nodes with surplus pages have no free pages.
506 unsigned long remaining_iterations
= num_online_nodes();
508 /* Uncommit the reservation */
509 resv_huge_pages
-= unused_resv_pages
;
511 nr_pages
= min(unused_resv_pages
, surplus_huge_pages
);
513 while (remaining_iterations
-- && nr_pages
) {
514 nid
= next_node(nid
, node_online_map
);
515 if (nid
== MAX_NUMNODES
)
516 nid
= first_node(node_online_map
);
518 if (!surplus_huge_pages_node
[nid
])
521 if (!list_empty(&hugepage_freelists
[nid
])) {
522 page
= list_entry(hugepage_freelists
[nid
].next
,
524 list_del(&page
->lru
);
525 update_and_free_page(page
);
527 free_huge_pages_node
[nid
]--;
528 surplus_huge_pages
--;
529 surplus_huge_pages_node
[nid
]--;
531 remaining_iterations
= num_online_nodes();
536 static struct page
*alloc_huge_page(struct vm_area_struct
*vma
,
540 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
541 struct inode
*inode
= mapping
->host
;
542 unsigned int chg
= 0;
545 * Processes that did not create the mapping will have no reserves and
546 * will not have accounted against quota. Check that the quota can be
547 * made before satisfying the allocation
549 if (!vma_has_private_reserves(vma
)) {
551 if (hugetlb_get_quota(inode
->i_mapping
, chg
))
552 return ERR_PTR(-ENOSPC
);
555 spin_lock(&hugetlb_lock
);
556 page
= dequeue_huge_page_vma(vma
, addr
);
557 spin_unlock(&hugetlb_lock
);
560 page
= alloc_buddy_huge_page(vma
, addr
);
562 hugetlb_put_quota(inode
->i_mapping
, chg
);
563 return ERR_PTR(-VM_FAULT_OOM
);
567 set_page_refcounted(page
);
568 set_page_private(page
, (unsigned long) mapping
);
573 static int __init
hugetlb_init(void)
577 if (HPAGE_SHIFT
== 0)
580 for (i
= 0; i
< MAX_NUMNODES
; ++i
)
581 INIT_LIST_HEAD(&hugepage_freelists
[i
]);
583 hugetlb_next_nid
= first_node(node_online_map
);
585 for (i
= 0; i
< max_huge_pages
; ++i
) {
586 if (!alloc_fresh_huge_page())
589 max_huge_pages
= free_huge_pages
= nr_huge_pages
= i
;
590 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages
);
593 module_init(hugetlb_init
);
595 static int __init
hugetlb_setup(char *s
)
597 if (sscanf(s
, "%lu", &max_huge_pages
) <= 0)
601 __setup("hugepages=", hugetlb_setup
);
603 static unsigned int cpuset_mems_nr(unsigned int *array
)
608 for_each_node_mask(node
, cpuset_current_mems_allowed
)
615 #ifdef CONFIG_HIGHMEM
616 static void try_to_free_low(unsigned long count
)
620 for (i
= 0; i
< MAX_NUMNODES
; ++i
) {
621 struct page
*page
, *next
;
622 list_for_each_entry_safe(page
, next
, &hugepage_freelists
[i
], lru
) {
623 if (count
>= nr_huge_pages
)
625 if (PageHighMem(page
))
627 list_del(&page
->lru
);
628 update_and_free_page(page
);
630 free_huge_pages_node
[page_to_nid(page
)]--;
635 static inline void try_to_free_low(unsigned long count
)
640 #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
641 static unsigned long set_max_huge_pages(unsigned long count
)
643 unsigned long min_count
, ret
;
646 * Increase the pool size
647 * First take pages out of surplus state. Then make up the
648 * remaining difference by allocating fresh huge pages.
650 * We might race with alloc_buddy_huge_page() here and be unable
651 * to convert a surplus huge page to a normal huge page. That is
652 * not critical, though, it just means the overall size of the
653 * pool might be one hugepage larger than it needs to be, but
654 * within all the constraints specified by the sysctls.
656 spin_lock(&hugetlb_lock
);
657 while (surplus_huge_pages
&& count
> persistent_huge_pages
) {
658 if (!adjust_pool_surplus(-1))
662 while (count
> persistent_huge_pages
) {
664 * If this allocation races such that we no longer need the
665 * page, free_huge_page will handle it by freeing the page
666 * and reducing the surplus.
668 spin_unlock(&hugetlb_lock
);
669 ret
= alloc_fresh_huge_page();
670 spin_lock(&hugetlb_lock
);
677 * Decrease the pool size
678 * First return free pages to the buddy allocator (being careful
679 * to keep enough around to satisfy reservations). Then place
680 * pages into surplus state as needed so the pool will shrink
681 * to the desired size as pages become free.
683 * By placing pages into the surplus state independent of the
684 * overcommit value, we are allowing the surplus pool size to
685 * exceed overcommit. There are few sane options here. Since
686 * alloc_buddy_huge_page() is checking the global counter,
687 * though, we'll note that we're not allowed to exceed surplus
688 * and won't grow the pool anywhere else. Not until one of the
689 * sysctls are changed, or the surplus pages go out of use.
691 min_count
= resv_huge_pages
+ nr_huge_pages
- free_huge_pages
;
692 min_count
= max(count
, min_count
);
693 try_to_free_low(min_count
);
694 while (min_count
< persistent_huge_pages
) {
695 struct page
*page
= dequeue_huge_page();
698 update_and_free_page(page
);
700 while (count
< persistent_huge_pages
) {
701 if (!adjust_pool_surplus(1))
705 ret
= persistent_huge_pages
;
706 spin_unlock(&hugetlb_lock
);
710 int hugetlb_sysctl_handler(struct ctl_table
*table
, int write
,
711 struct file
*file
, void __user
*buffer
,
712 size_t *length
, loff_t
*ppos
)
714 proc_doulongvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
715 max_huge_pages
= set_max_huge_pages(max_huge_pages
);
719 int hugetlb_treat_movable_handler(struct ctl_table
*table
, int write
,
720 struct file
*file
, void __user
*buffer
,
721 size_t *length
, loff_t
*ppos
)
723 proc_dointvec(table
, write
, file
, buffer
, length
, ppos
);
724 if (hugepages_treat_as_movable
)
725 htlb_alloc_mask
= GFP_HIGHUSER_MOVABLE
;
727 htlb_alloc_mask
= GFP_HIGHUSER
;
731 int hugetlb_overcommit_handler(struct ctl_table
*table
, int write
,
732 struct file
*file
, void __user
*buffer
,
733 size_t *length
, loff_t
*ppos
)
735 proc_doulongvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
736 spin_lock(&hugetlb_lock
);
737 nr_overcommit_huge_pages
= sysctl_overcommit_huge_pages
;
738 spin_unlock(&hugetlb_lock
);
742 #endif /* CONFIG_SYSCTL */
744 int hugetlb_report_meminfo(char *buf
)
747 "HugePages_Total: %5lu\n"
748 "HugePages_Free: %5lu\n"
749 "HugePages_Rsvd: %5lu\n"
750 "HugePages_Surp: %5lu\n"
751 "Hugepagesize: %5lu kB\n",
759 int hugetlb_report_node_meminfo(int nid
, char *buf
)
762 "Node %d HugePages_Total: %5u\n"
763 "Node %d HugePages_Free: %5u\n"
764 "Node %d HugePages_Surp: %5u\n",
765 nid
, nr_huge_pages_node
[nid
],
766 nid
, free_huge_pages_node
[nid
],
767 nid
, surplus_huge_pages_node
[nid
]);
770 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
771 unsigned long hugetlb_total_pages(void)
773 return nr_huge_pages
* (HPAGE_SIZE
/ PAGE_SIZE
);
776 static int hugetlb_acct_memory(long delta
)
780 spin_lock(&hugetlb_lock
);
782 * When cpuset is configured, it breaks the strict hugetlb page
783 * reservation as the accounting is done on a global variable. Such
784 * reservation is completely rubbish in the presence of cpuset because
785 * the reservation is not checked against page availability for the
786 * current cpuset. Application can still potentially OOM'ed by kernel
787 * with lack of free htlb page in cpuset that the task is in.
788 * Attempt to enforce strict accounting with cpuset is almost
789 * impossible (or too ugly) because cpuset is too fluid that
790 * task or memory node can be dynamically moved between cpusets.
792 * The change of semantics for shared hugetlb mapping with cpuset is
793 * undesirable. However, in order to preserve some of the semantics,
794 * we fall back to check against current free page availability as
795 * a best attempt and hopefully to minimize the impact of changing
796 * semantics that cpuset has.
799 if (gather_surplus_pages(delta
) < 0)
802 if (delta
> cpuset_mems_nr(free_huge_pages_node
)) {
803 return_unused_surplus_pages(delta
);
810 return_unused_surplus_pages((unsigned long) -delta
);
813 spin_unlock(&hugetlb_lock
);
817 static void hugetlb_vm_op_close(struct vm_area_struct
*vma
)
819 unsigned long reserve
= vma_resv_huge_pages(vma
);
821 hugetlb_acct_memory(-reserve
);
825 * We cannot handle pagefaults against hugetlb pages at all. They cause
826 * handle_mm_fault() to try to instantiate regular-sized pages in the
827 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
830 static int hugetlb_vm_op_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
836 struct vm_operations_struct hugetlb_vm_ops
= {
837 .fault
= hugetlb_vm_op_fault
,
838 .close
= hugetlb_vm_op_close
,
841 static pte_t
make_huge_pte(struct vm_area_struct
*vma
, struct page
*page
,
848 pte_mkwrite(pte_mkdirty(mk_pte(page
, vma
->vm_page_prot
)));
850 entry
= huge_pte_wrprotect(mk_pte(page
, vma
->vm_page_prot
));
852 entry
= pte_mkyoung(entry
);
853 entry
= pte_mkhuge(entry
);
858 static void set_huge_ptep_writable(struct vm_area_struct
*vma
,
859 unsigned long address
, pte_t
*ptep
)
863 entry
= pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep
)));
864 if (huge_ptep_set_access_flags(vma
, address
, ptep
, entry
, 1)) {
865 update_mmu_cache(vma
, address
, entry
);
870 int copy_hugetlb_page_range(struct mm_struct
*dst
, struct mm_struct
*src
,
871 struct vm_area_struct
*vma
)
873 pte_t
*src_pte
, *dst_pte
, entry
;
874 struct page
*ptepage
;
878 cow
= (vma
->vm_flags
& (VM_SHARED
| VM_MAYWRITE
)) == VM_MAYWRITE
;
880 for (addr
= vma
->vm_start
; addr
< vma
->vm_end
; addr
+= HPAGE_SIZE
) {
881 src_pte
= huge_pte_offset(src
, addr
);
884 dst_pte
= huge_pte_alloc(dst
, addr
);
888 /* If the pagetables are shared don't copy or take references */
889 if (dst_pte
== src_pte
)
892 spin_lock(&dst
->page_table_lock
);
893 spin_lock_nested(&src
->page_table_lock
, SINGLE_DEPTH_NESTING
);
894 if (!huge_pte_none(huge_ptep_get(src_pte
))) {
896 huge_ptep_set_wrprotect(src
, addr
, src_pte
);
897 entry
= huge_ptep_get(src_pte
);
898 ptepage
= pte_page(entry
);
900 set_huge_pte_at(dst
, addr
, dst_pte
, entry
);
902 spin_unlock(&src
->page_table_lock
);
903 spin_unlock(&dst
->page_table_lock
);
911 void __unmap_hugepage_range(struct vm_area_struct
*vma
, unsigned long start
,
914 struct mm_struct
*mm
= vma
->vm_mm
;
915 unsigned long address
;
921 * A page gathering list, protected by per file i_mmap_lock. The
922 * lock is used to avoid list corruption from multiple unmapping
923 * of the same page since we are using page->lru.
925 LIST_HEAD(page_list
);
927 WARN_ON(!is_vm_hugetlb_page(vma
));
928 BUG_ON(start
& ~HPAGE_MASK
);
929 BUG_ON(end
& ~HPAGE_MASK
);
931 spin_lock(&mm
->page_table_lock
);
932 for (address
= start
; address
< end
; address
+= HPAGE_SIZE
) {
933 ptep
= huge_pte_offset(mm
, address
);
937 if (huge_pmd_unshare(mm
, &address
, ptep
))
940 pte
= huge_ptep_get_and_clear(mm
, address
, ptep
);
941 if (huge_pte_none(pte
))
944 page
= pte_page(pte
);
946 set_page_dirty(page
);
947 list_add(&page
->lru
, &page_list
);
949 spin_unlock(&mm
->page_table_lock
);
950 flush_tlb_range(vma
, start
, end
);
951 list_for_each_entry_safe(page
, tmp
, &page_list
, lru
) {
952 list_del(&page
->lru
);
957 void unmap_hugepage_range(struct vm_area_struct
*vma
, unsigned long start
,
961 * It is undesirable to test vma->vm_file as it should be non-null
962 * for valid hugetlb area. However, vm_file will be NULL in the error
963 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
964 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
965 * to clean up. Since no pte has actually been setup, it is safe to
966 * do nothing in this case.
969 spin_lock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
970 __unmap_hugepage_range(vma
, start
, end
);
971 spin_unlock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
975 static int hugetlb_cow(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
976 unsigned long address
, pte_t
*ptep
, pte_t pte
)
978 struct page
*old_page
, *new_page
;
981 old_page
= pte_page(pte
);
983 /* If no-one else is actually using this page, avoid the copy
984 * and just make the page writable */
985 avoidcopy
= (page_count(old_page
) == 1);
987 set_huge_ptep_writable(vma
, address
, ptep
);
991 page_cache_get(old_page
);
992 new_page
= alloc_huge_page(vma
, address
);
994 if (IS_ERR(new_page
)) {
995 page_cache_release(old_page
);
996 return -PTR_ERR(new_page
);
999 spin_unlock(&mm
->page_table_lock
);
1000 copy_huge_page(new_page
, old_page
, address
, vma
);
1001 __SetPageUptodate(new_page
);
1002 spin_lock(&mm
->page_table_lock
);
1004 ptep
= huge_pte_offset(mm
, address
& HPAGE_MASK
);
1005 if (likely(pte_same(huge_ptep_get(ptep
), pte
))) {
1007 huge_ptep_clear_flush(vma
, address
, ptep
);
1008 set_huge_pte_at(mm
, address
, ptep
,
1009 make_huge_pte(vma
, new_page
, 1));
1010 /* Make the old page be freed below */
1011 new_page
= old_page
;
1013 page_cache_release(new_page
);
1014 page_cache_release(old_page
);
1018 static int hugetlb_no_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1019 unsigned long address
, pte_t
*ptep
, int write_access
)
1021 int ret
= VM_FAULT_SIGBUS
;
1025 struct address_space
*mapping
;
1028 mapping
= vma
->vm_file
->f_mapping
;
1029 idx
= ((address
- vma
->vm_start
) >> HPAGE_SHIFT
)
1030 + (vma
->vm_pgoff
>> (HPAGE_SHIFT
- PAGE_SHIFT
));
1033 * Use page lock to guard against racing truncation
1034 * before we get page_table_lock.
1037 page
= find_lock_page(mapping
, idx
);
1039 size
= i_size_read(mapping
->host
) >> HPAGE_SHIFT
;
1042 page
= alloc_huge_page(vma
, address
);
1044 ret
= -PTR_ERR(page
);
1047 clear_huge_page(page
, address
);
1048 __SetPageUptodate(page
);
1050 if (vma
->vm_flags
& VM_SHARED
) {
1052 struct inode
*inode
= mapping
->host
;
1054 err
= add_to_page_cache(page
, mapping
, idx
, GFP_KERNEL
);
1062 spin_lock(&inode
->i_lock
);
1063 inode
->i_blocks
+= BLOCKS_PER_HUGEPAGE
;
1064 spin_unlock(&inode
->i_lock
);
1069 spin_lock(&mm
->page_table_lock
);
1070 size
= i_size_read(mapping
->host
) >> HPAGE_SHIFT
;
1075 if (!huge_pte_none(huge_ptep_get(ptep
)))
1078 new_pte
= make_huge_pte(vma
, page
, ((vma
->vm_flags
& VM_WRITE
)
1079 && (vma
->vm_flags
& VM_SHARED
)));
1080 set_huge_pte_at(mm
, address
, ptep
, new_pte
);
1082 if (write_access
&& !(vma
->vm_flags
& VM_SHARED
)) {
1083 /* Optimization, do the COW without a second fault */
1084 ret
= hugetlb_cow(mm
, vma
, address
, ptep
, new_pte
);
1087 spin_unlock(&mm
->page_table_lock
);
1093 spin_unlock(&mm
->page_table_lock
);
1099 int hugetlb_fault(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1100 unsigned long address
, int write_access
)
1105 static DEFINE_MUTEX(hugetlb_instantiation_mutex
);
1107 ptep
= huge_pte_alloc(mm
, address
);
1109 return VM_FAULT_OOM
;
1112 * Serialize hugepage allocation and instantiation, so that we don't
1113 * get spurious allocation failures if two CPUs race to instantiate
1114 * the same page in the page cache.
1116 mutex_lock(&hugetlb_instantiation_mutex
);
1117 entry
= huge_ptep_get(ptep
);
1118 if (huge_pte_none(entry
)) {
1119 ret
= hugetlb_no_page(mm
, vma
, address
, ptep
, write_access
);
1120 mutex_unlock(&hugetlb_instantiation_mutex
);
1126 spin_lock(&mm
->page_table_lock
);
1127 /* Check for a racing update before calling hugetlb_cow */
1128 if (likely(pte_same(entry
, huge_ptep_get(ptep
))))
1129 if (write_access
&& !pte_write(entry
))
1130 ret
= hugetlb_cow(mm
, vma
, address
, ptep
, entry
);
1131 spin_unlock(&mm
->page_table_lock
);
1132 mutex_unlock(&hugetlb_instantiation_mutex
);
1137 int follow_hugetlb_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1138 struct page
**pages
, struct vm_area_struct
**vmas
,
1139 unsigned long *position
, int *length
, int i
,
1142 unsigned long pfn_offset
;
1143 unsigned long vaddr
= *position
;
1144 int remainder
= *length
;
1146 spin_lock(&mm
->page_table_lock
);
1147 while (vaddr
< vma
->vm_end
&& remainder
) {
1152 * Some archs (sparc64, sh*) have multiple pte_ts to
1153 * each hugepage. We have to make * sure we get the
1154 * first, for the page indexing below to work.
1156 pte
= huge_pte_offset(mm
, vaddr
& HPAGE_MASK
);
1158 if (!pte
|| huge_pte_none(huge_ptep_get(pte
)) ||
1159 (write
&& !pte_write(huge_ptep_get(pte
)))) {
1162 spin_unlock(&mm
->page_table_lock
);
1163 ret
= hugetlb_fault(mm
, vma
, vaddr
, write
);
1164 spin_lock(&mm
->page_table_lock
);
1165 if (!(ret
& VM_FAULT_ERROR
))
1174 pfn_offset
= (vaddr
& ~HPAGE_MASK
) >> PAGE_SHIFT
;
1175 page
= pte_page(huge_ptep_get(pte
));
1179 pages
[i
] = page
+ pfn_offset
;
1189 if (vaddr
< vma
->vm_end
&& remainder
&&
1190 pfn_offset
< HPAGE_SIZE
/PAGE_SIZE
) {
1192 * We use pfn_offset to avoid touching the pageframes
1193 * of this compound page.
1198 spin_unlock(&mm
->page_table_lock
);
1199 *length
= remainder
;
1205 void hugetlb_change_protection(struct vm_area_struct
*vma
,
1206 unsigned long address
, unsigned long end
, pgprot_t newprot
)
1208 struct mm_struct
*mm
= vma
->vm_mm
;
1209 unsigned long start
= address
;
1213 BUG_ON(address
>= end
);
1214 flush_cache_range(vma
, address
, end
);
1216 spin_lock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
1217 spin_lock(&mm
->page_table_lock
);
1218 for (; address
< end
; address
+= HPAGE_SIZE
) {
1219 ptep
= huge_pte_offset(mm
, address
);
1222 if (huge_pmd_unshare(mm
, &address
, ptep
))
1224 if (!huge_pte_none(huge_ptep_get(ptep
))) {
1225 pte
= huge_ptep_get_and_clear(mm
, address
, ptep
);
1226 pte
= pte_mkhuge(pte_modify(pte
, newprot
));
1227 set_huge_pte_at(mm
, address
, ptep
, pte
);
1230 spin_unlock(&mm
->page_table_lock
);
1231 spin_unlock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
1233 flush_tlb_range(vma
, start
, end
);
1236 struct file_region
{
1237 struct list_head link
;
1242 static long region_add(struct list_head
*head
, long f
, long t
)
1244 struct file_region
*rg
, *nrg
, *trg
;
1246 /* Locate the region we are either in or before. */
1247 list_for_each_entry(rg
, head
, link
)
1251 /* Round our left edge to the current segment if it encloses us. */
1255 /* Check for and consume any regions we now overlap with. */
1257 list_for_each_entry_safe(rg
, trg
, rg
->link
.prev
, link
) {
1258 if (&rg
->link
== head
)
1263 /* If this area reaches higher then extend our area to
1264 * include it completely. If this is not the first area
1265 * which we intend to reuse, free it. */
1269 list_del(&rg
->link
);
1278 static long region_chg(struct list_head
*head
, long f
, long t
)
1280 struct file_region
*rg
, *nrg
;
1283 /* Locate the region we are before or in. */
1284 list_for_each_entry(rg
, head
, link
)
1288 /* If we are below the current region then a new region is required.
1289 * Subtle, allocate a new region at the position but make it zero
1290 * size such that we can guarantee to record the reservation. */
1291 if (&rg
->link
== head
|| t
< rg
->from
) {
1292 nrg
= kmalloc(sizeof(*nrg
), GFP_KERNEL
);
1297 INIT_LIST_HEAD(&nrg
->link
);
1298 list_add(&nrg
->link
, rg
->link
.prev
);
1303 /* Round our left edge to the current segment if it encloses us. */
1308 /* Check for and consume any regions we now overlap with. */
1309 list_for_each_entry(rg
, rg
->link
.prev
, link
) {
1310 if (&rg
->link
== head
)
1315 /* We overlap with this area, if it extends futher than
1316 * us then we must extend ourselves. Account for its
1317 * existing reservation. */
1322 chg
-= rg
->to
- rg
->from
;
1327 static long region_truncate(struct list_head
*head
, long end
)
1329 struct file_region
*rg
, *trg
;
1332 /* Locate the region we are either in or before. */
1333 list_for_each_entry(rg
, head
, link
)
1336 if (&rg
->link
== head
)
1339 /* If we are in the middle of a region then adjust it. */
1340 if (end
> rg
->from
) {
1343 rg
= list_entry(rg
->link
.next
, typeof(*rg
), link
);
1346 /* Drop any remaining regions. */
1347 list_for_each_entry_safe(rg
, trg
, rg
->link
.prev
, link
) {
1348 if (&rg
->link
== head
)
1350 chg
+= rg
->to
- rg
->from
;
1351 list_del(&rg
->link
);
1357 int hugetlb_reserve_pages(struct inode
*inode
,
1359 struct vm_area_struct
*vma
)
1364 * Shared mappings base their reservation on the number of pages that
1365 * are already allocated on behalf of the file. Private mappings need
1366 * to reserve the full area even if read-only as mprotect() may be
1367 * called to make the mapping read-write. Assume !vma is a shm mapping
1369 if (!vma
|| vma
->vm_flags
& VM_SHARED
)
1370 chg
= region_chg(&inode
->i_mapping
->private_list
, from
, to
);
1373 set_vma_resv_huge_pages(vma
, chg
);
1379 if (hugetlb_get_quota(inode
->i_mapping
, chg
))
1381 ret
= hugetlb_acct_memory(chg
);
1383 hugetlb_put_quota(inode
->i_mapping
, chg
);
1386 if (!vma
|| vma
->vm_flags
& VM_SHARED
)
1387 region_add(&inode
->i_mapping
->private_list
, from
, to
);
1391 void hugetlb_unreserve_pages(struct inode
*inode
, long offset
, long freed
)
1393 long chg
= region_truncate(&inode
->i_mapping
->private_list
, offset
);
1395 spin_lock(&inode
->i_lock
);
1396 inode
->i_blocks
-= BLOCKS_PER_HUGEPAGE
* freed
;
1397 spin_unlock(&inode
->i_lock
);
1399 hugetlb_put_quota(inode
->i_mapping
, (chg
- freed
));
1400 hugetlb_acct_memory(-(chg
- freed
));