blob: 72acbb29d2cc68ca50161af90d3cbe042c9852aa [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>
David Gibson3935baa2006-03-22 00:08:53 -080016#include <linux/mutex.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080017
David Gibson63551ae2005-06-21 17:14:44 -070018#include <asm/page.h>
19#include <asm/pgtable.h>
20
21#include <linux/hugetlb.h>
Nick Piggin7835e982006-03-22 00:08:40 -080022#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -070025static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages;
Adam Litke7893d1d2007-10-16 01:26:18 -070026static unsigned long surplus_huge_pages;
Nishanth Aravamudan064d9ef2008-02-13 15:03:19 -080027static unsigned long nr_overcommit_huge_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028unsigned long max_huge_pages;
Nishanth Aravamudan064d9ef2008-02-13 15:03:19 -080029unsigned long sysctl_overcommit_huge_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static struct list_head hugepage_freelists[MAX_NUMNODES];
31static unsigned int nr_huge_pages_node[MAX_NUMNODES];
32static unsigned int free_huge_pages_node[MAX_NUMNODES];
Adam Litke7893d1d2007-10-16 01:26:18 -070033static unsigned int surplus_huge_pages_node[MAX_NUMNODES];
Mel Gorman396faf02007-07-17 04:03:13 -070034static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
35unsigned long hugepages_treat_as_movable;
Nishanth Aravamudan63b46132007-10-16 01:26:24 -070036static int hugetlb_next_nid;
Mel Gorman396faf02007-07-17 04:03:13 -070037
David Gibson3935baa2006-03-22 00:08:53 -080038/*
39 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
40 */
41static DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080042
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -070043/*
Andy Whitcroft96822902008-07-23 21:27:29 -070044 * Region tracking -- allows tracking of reservations and instantiated pages
45 * across the pages in a mapping.
46 */
47struct file_region {
48 struct list_head link;
49 long from;
50 long to;
51};
52
53static long region_add(struct list_head *head, long f, long t)
54{
55 struct file_region *rg, *nrg, *trg;
56
57 /* Locate the region we are either in or before. */
58 list_for_each_entry(rg, head, link)
59 if (f <= rg->to)
60 break;
61
62 /* Round our left edge to the current segment if it encloses us. */
63 if (f > rg->from)
64 f = rg->from;
65
66 /* Check for and consume any regions we now overlap with. */
67 nrg = rg;
68 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
69 if (&rg->link == head)
70 break;
71 if (rg->from > t)
72 break;
73
74 /* If this area reaches higher then extend our area to
75 * include it completely. If this is not the first area
76 * which we intend to reuse, free it. */
77 if (rg->to > t)
78 t = rg->to;
79 if (rg != nrg) {
80 list_del(&rg->link);
81 kfree(rg);
82 }
83 }
84 nrg->from = f;
85 nrg->to = t;
86 return 0;
87}
88
89static long region_chg(struct list_head *head, long f, long t)
90{
91 struct file_region *rg, *nrg;
92 long chg = 0;
93
94 /* Locate the region we are before or in. */
95 list_for_each_entry(rg, head, link)
96 if (f <= rg->to)
97 break;
98
99 /* If we are below the current region then a new region is required.
100 * Subtle, allocate a new region at the position but make it zero
101 * size such that we can guarantee to record the reservation. */
102 if (&rg->link == head || t < rg->from) {
103 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
104 if (!nrg)
105 return -ENOMEM;
106 nrg->from = f;
107 nrg->to = f;
108 INIT_LIST_HEAD(&nrg->link);
109 list_add(&nrg->link, rg->link.prev);
110
111 return t - f;
112 }
113
114 /* Round our left edge to the current segment if it encloses us. */
115 if (f > rg->from)
116 f = rg->from;
117 chg = t - f;
118
119 /* Check for and consume any regions we now overlap with. */
120 list_for_each_entry(rg, rg->link.prev, link) {
121 if (&rg->link == head)
122 break;
123 if (rg->from > t)
124 return chg;
125
126 /* We overlap with this area, if it extends futher than
127 * us then we must extend ourselves. Account for its
128 * existing reservation. */
129 if (rg->to > t) {
130 chg += rg->to - t;
131 t = rg->to;
132 }
133 chg -= rg->to - rg->from;
134 }
135 return chg;
136}
137
138static long region_truncate(struct list_head *head, long end)
139{
140 struct file_region *rg, *trg;
141 long chg = 0;
142
143 /* Locate the region we are either in or before. */
144 list_for_each_entry(rg, head, link)
145 if (end <= rg->to)
146 break;
147 if (&rg->link == head)
148 return 0;
149
150 /* If we are in the middle of a region then adjust it. */
151 if (end > rg->from) {
152 chg = rg->to - end;
153 rg->to = end;
154 rg = list_entry(rg->link.next, typeof(*rg), link);
155 }
156
157 /* Drop any remaining regions. */
158 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
159 if (&rg->link == head)
160 break;
161 chg += rg->to - rg->from;
162 list_del(&rg->link);
163 kfree(rg);
164 }
165 return chg;
166}
167
168/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700169 * Convert the address within this vma to the page offset within
170 * the mapping, in base page units.
171 */
172static pgoff_t vma_page_offset(struct vm_area_struct *vma,
173 unsigned long address)
174{
175 return ((address - vma->vm_start) >> PAGE_SHIFT) +
176 (vma->vm_pgoff >> PAGE_SHIFT);
177}
178
179/*
180 * Convert the address within this vma to the page offset within
181 * the mapping, in pagecache page units; huge pages here.
182 */
183static pgoff_t vma_pagecache_offset(struct vm_area_struct *vma,
184 unsigned long address)
185{
186 return ((address - vma->vm_start) >> HPAGE_SHIFT) +
187 (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
188}
189
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700190#define HPAGE_RESV_OWNER (1UL << (BITS_PER_LONG - 1))
191#define HPAGE_RESV_UNMAPPED (1UL << (BITS_PER_LONG - 2))
192#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Mel Gormana1e78772008-07-23 21:27:23 -0700193/*
194 * These helpers are used to track how many pages are reserved for
195 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
196 * is guaranteed to have their future faults succeed.
197 *
198 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
199 * the reserve counters are updated with the hugetlb_lock held. It is safe
200 * to reset the VMA at fork() time as it is not in use yet and there is no
201 * chance of the global counters getting corrupted as a result of the values.
202 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700203static unsigned long get_vma_private_data(struct vm_area_struct *vma)
204{
205 return (unsigned long)vma->vm_private_data;
206}
207
208static void set_vma_private_data(struct vm_area_struct *vma,
209 unsigned long value)
210{
211 vma->vm_private_data = (void *)value;
212}
213
Mel Gormana1e78772008-07-23 21:27:23 -0700214static unsigned long vma_resv_huge_pages(struct vm_area_struct *vma)
215{
216 VM_BUG_ON(!is_vm_hugetlb_page(vma));
217 if (!(vma->vm_flags & VM_SHARED))
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700218 return get_vma_private_data(vma) & ~HPAGE_RESV_MASK;
Mel Gormana1e78772008-07-23 21:27:23 -0700219 return 0;
220}
221
222static void set_vma_resv_huge_pages(struct vm_area_struct *vma,
223 unsigned long reserve)
224{
225 VM_BUG_ON(!is_vm_hugetlb_page(vma));
226 VM_BUG_ON(vma->vm_flags & VM_SHARED);
227
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700228 set_vma_private_data(vma,
229 (get_vma_private_data(vma) & HPAGE_RESV_MASK) | reserve);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700230}
231
232static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
233{
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700234 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700235 VM_BUG_ON(vma->vm_flags & VM_SHARED);
236
237 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700238}
239
240static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
241{
242 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700243
244 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700245}
246
247/* Decrement the reserved pages in the hugepage pool by one */
248static void decrement_hugepage_resv_vma(struct vm_area_struct *vma)
249{
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700250 if (vma->vm_flags & VM_NORESERVE)
251 return;
252
Mel Gormana1e78772008-07-23 21:27:23 -0700253 if (vma->vm_flags & VM_SHARED) {
254 /* Shared mappings always use reserves */
255 resv_huge_pages--;
256 } else {
257 /*
258 * Only the process that called mmap() has reserves for
259 * private mappings.
260 */
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700261 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
262 unsigned long flags, reserve;
Mel Gormana1e78772008-07-23 21:27:23 -0700263 resv_huge_pages--;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700264 flags = (unsigned long)vma->vm_private_data &
265 HPAGE_RESV_MASK;
Mel Gormana1e78772008-07-23 21:27:23 -0700266 reserve = (unsigned long)vma->vm_private_data - 1;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700267 vma->vm_private_data = (void *)(reserve | flags);
Mel Gormana1e78772008-07-23 21:27:23 -0700268 }
269 }
270}
271
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700272/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700273void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
274{
275 VM_BUG_ON(!is_vm_hugetlb_page(vma));
276 if (!(vma->vm_flags & VM_SHARED))
277 vma->vm_private_data = (void *)0;
278}
279
280/* Returns true if the VMA has associated reserve pages */
281static int vma_has_private_reserves(struct vm_area_struct *vma)
282{
283 if (vma->vm_flags & VM_SHARED)
284 return 0;
285 if (!vma_resv_huge_pages(vma))
286 return 0;
287 return 1;
288}
289
David Gibson79ac6ba2006-03-22 00:08:51 -0800290static void clear_huge_page(struct page *page, unsigned long addr)
291{
292 int i;
293
294 might_sleep();
295 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
296 cond_resched();
Ralf Baechle281e0e32007-10-01 01:20:10 -0700297 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
David Gibson79ac6ba2006-03-22 00:08:51 -0800298 }
299}
300
301static void copy_huge_page(struct page *dst, struct page *src,
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000302 unsigned long addr, struct vm_area_struct *vma)
David Gibson79ac6ba2006-03-22 00:08:51 -0800303{
304 int i;
305
306 might_sleep();
307 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
308 cond_resched();
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000309 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800310 }
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static void enqueue_huge_page(struct page *page)
314{
315 int nid = page_to_nid(page);
316 list_add(&page->lru, &hugepage_freelists[nid]);
317 free_huge_pages++;
318 free_huge_pages_node[nid]++;
319}
320
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800321static struct page *dequeue_huge_page(void)
322{
323 int nid;
324 struct page *page = NULL;
325
326 for (nid = 0; nid < MAX_NUMNODES; ++nid) {
327 if (!list_empty(&hugepage_freelists[nid])) {
328 page = list_entry(hugepage_freelists[nid].next,
329 struct page, lru);
330 list_del(&page->lru);
331 free_huge_pages--;
332 free_huge_pages_node[nid]--;
333 break;
334 }
335 }
336 return page;
337}
338
339static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700340 unsigned long address, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Nishanth Aravamudan31a5c6e2007-07-15 23:38:02 -0700342 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct page *page = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700344 struct mempolicy *mpol;
Mel Gorman19770b32008-04-28 02:12:18 -0700345 nodemask_t *nodemask;
Mel Gorman396faf02007-07-17 04:03:13 -0700346 struct zonelist *zonelist = huge_zonelist(vma, address,
Mel Gorman19770b32008-04-28 02:12:18 -0700347 htlb_alloc_mask, &mpol, &nodemask);
Mel Gormandd1a2392008-04-28 02:12:17 -0700348 struct zone *zone;
349 struct zoneref *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Mel Gormana1e78772008-07-23 21:27:23 -0700351 /*
352 * A child process with MAP_PRIVATE mappings created by their parent
353 * have no page reserves. This check ensures that reservations are
354 * not "stolen". The child may still get SIGKILLed
355 */
356 if (!vma_has_private_reserves(vma) &&
357 free_huge_pages - resv_huge_pages == 0)
358 return NULL;
359
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700360 /* If reserves cannot be used, ensure enough pages are in the pool */
361 if (avoid_reserve && free_huge_pages - resv_huge_pages == 0)
362 return NULL;
363
Mel Gorman19770b32008-04-28 02:12:18 -0700364 for_each_zone_zonelist_nodemask(zone, z, zonelist,
365 MAX_NR_ZONES - 1, nodemask) {
Mel Gorman54a6eb52008-04-28 02:12:16 -0700366 nid = zone_to_nid(zone);
367 if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
Andrew Morton3abf7af2007-07-19 01:49:08 -0700368 !list_empty(&hugepage_freelists[nid])) {
369 page = list_entry(hugepage_freelists[nid].next,
370 struct page, lru);
371 list_del(&page->lru);
372 free_huge_pages--;
373 free_huge_pages_node[nid]--;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700374
375 if (!avoid_reserve)
376 decrement_hugepage_resv_vma(vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700377
Ken Chen5ab3ee72007-07-23 18:44:00 -0700378 break;
Andrew Morton3abf7af2007-07-19 01:49:08 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700381 mpol_cond_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return page;
383}
384
Adam Litke6af2acb2007-10-16 01:26:16 -0700385static void update_and_free_page(struct page *page)
386{
387 int i;
388 nr_huge_pages--;
389 nr_huge_pages_node[page_to_nid(page)]--;
390 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
391 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
392 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
393 1 << PG_private | 1<< PG_writeback);
394 }
395 set_compound_page_dtor(page, NULL);
396 set_page_refcounted(page);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700397 arch_release_hugepage(page);
Adam Litke6af2acb2007-10-16 01:26:16 -0700398 __free_pages(page, HUGETLB_PAGE_ORDER);
399}
400
David Gibson27a85ef2006-03-22 00:08:56 -0800401static void free_huge_page(struct page *page)
402{
Adam Litke7893d1d2007-10-16 01:26:18 -0700403 int nid = page_to_nid(page);
Adam Litkec79fb752007-11-14 16:59:38 -0800404 struct address_space *mapping;
David Gibson27a85ef2006-03-22 00:08:56 -0800405
Adam Litkec79fb752007-11-14 16:59:38 -0800406 mapping = (struct address_space *) page_private(page);
Andy Whitcrofte5df70a2008-02-23 15:23:32 -0800407 set_page_private(page, 0);
Adam Litke7893d1d2007-10-16 01:26:18 -0700408 BUG_ON(page_count(page));
David Gibson27a85ef2006-03-22 00:08:56 -0800409 INIT_LIST_HEAD(&page->lru);
410
411 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700412 if (surplus_huge_pages_node[nid]) {
413 update_and_free_page(page);
414 surplus_huge_pages--;
415 surplus_huge_pages_node[nid]--;
416 } else {
417 enqueue_huge_page(page);
418 }
David Gibson27a85ef2006-03-22 00:08:56 -0800419 spin_unlock(&hugetlb_lock);
Adam Litkec79fb752007-11-14 16:59:38 -0800420 if (mapping)
Adam Litke9a119c02007-11-14 16:59:41 -0800421 hugetlb_put_quota(mapping, 1);
David Gibson27a85ef2006-03-22 00:08:56 -0800422}
423
Adam Litke7893d1d2007-10-16 01:26:18 -0700424/*
425 * Increment or decrement surplus_huge_pages. Keep node-specific counters
426 * balanced by operating on them in a round-robin fashion.
427 * Returns 1 if an adjustment was made.
428 */
429static int adjust_pool_surplus(int delta)
430{
431 static int prev_nid;
432 int nid = prev_nid;
433 int ret = 0;
434
435 VM_BUG_ON(delta != -1 && delta != 1);
436 do {
437 nid = next_node(nid, node_online_map);
438 if (nid == MAX_NUMNODES)
439 nid = first_node(node_online_map);
440
441 /* To shrink on this node, there must be a surplus page */
442 if (delta < 0 && !surplus_huge_pages_node[nid])
443 continue;
444 /* Surplus cannot exceed the total number of pages */
445 if (delta > 0 && surplus_huge_pages_node[nid] >=
446 nr_huge_pages_node[nid])
447 continue;
448
449 surplus_huge_pages += delta;
450 surplus_huge_pages_node[nid] += delta;
451 ret = 1;
452 break;
453 } while (nid != prev_nid);
454
455 prev_nid = nid;
456 return ret;
457}
458
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700459static struct page *alloc_fresh_huge_page_node(int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct page *page;
Joe Jinf96efd52007-07-15 23:38:12 -0700462
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700463 page = alloc_pages_node(nid,
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700464 htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
465 __GFP_REPEAT|__GFP_NOWARN,
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700466 HUGETLB_PAGE_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (page) {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700468 if (arch_prepare_hugepage(page)) {
469 __free_pages(page, HUGETLB_PAGE_ORDER);
Harvey Harrison7b8ee842008-04-28 14:13:19 -0700470 return NULL;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700471 }
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800472 set_compound_page_dtor(page, free_huge_page);
Eric Paris0bd0f9f2005-11-21 21:32:28 -0800473 spin_lock(&hugetlb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 nr_huge_pages++;
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700475 nr_huge_pages_node[nid]++;
Eric Paris0bd0f9f2005-11-21 21:32:28 -0800476 spin_unlock(&hugetlb_lock);
Nick Piggina4822892006-03-22 00:08:08 -0800477 put_page(page); /* free it into the hugepage allocator */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700479
480 return page;
481}
482
483static int alloc_fresh_huge_page(void)
484{
485 struct page *page;
486 int start_nid;
487 int next_nid;
488 int ret = 0;
489
490 start_nid = hugetlb_next_nid;
491
492 do {
493 page = alloc_fresh_huge_page_node(hugetlb_next_nid);
494 if (page)
495 ret = 1;
496 /*
497 * Use a helper variable to find the next node and then
498 * copy it back to hugetlb_next_nid afterwards:
499 * otherwise there's a window in which a racer might
500 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
501 * But we don't need to use a spin_lock here: it really
502 * doesn't matter if occasionally a racer chooses the
503 * same nid as we do. Move nid forward in the mask even
504 * if we just successfully allocated a hugepage so that
505 * the next caller gets hugepages on the next node.
506 */
507 next_nid = next_node(hugetlb_next_nid, node_online_map);
508 if (next_nid == MAX_NUMNODES)
509 next_nid = first_node(node_online_map);
510 hugetlb_next_nid = next_nid;
511 } while (!page && hugetlb_next_nid != start_nid);
512
Adam Litke3b116302008-04-28 02:13:06 -0700513 if (ret)
514 count_vm_event(HTLB_BUDDY_PGALLOC);
515 else
516 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
517
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700518 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Adam Litke7893d1d2007-10-16 01:26:18 -0700521static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
522 unsigned long address)
523{
524 struct page *page;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800525 unsigned int nid;
Adam Litke7893d1d2007-10-16 01:26:18 -0700526
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800527 /*
528 * Assume we will successfully allocate the surplus page to
529 * prevent racing processes from causing the surplus to exceed
530 * overcommit
531 *
532 * This however introduces a different race, where a process B
533 * tries to grow the static hugepage pool while alloc_pages() is
534 * called by process A. B will only examine the per-node
535 * counters in determining if surplus huge pages can be
536 * converted to normal huge pages in adjust_pool_surplus(). A
537 * won't be able to increment the per-node counter, until the
538 * lock is dropped by B, but B doesn't drop hugetlb_lock until
539 * no more huge pages can be converted from surplus to normal
540 * state (and doesn't try to convert again). Thus, we have a
541 * case where a surplus huge page exists, the pool is grown, and
542 * the surplus huge page still exists after, even though it
543 * should just have been converted to a normal huge page. This
544 * does not leak memory, though, as the hugepage will be freed
545 * once it is out of use. It also does not allow the counters to
546 * go out of whack in adjust_pool_surplus() as we don't modify
547 * the node values until we've gotten the hugepage and only the
548 * per-node value is checked there.
549 */
550 spin_lock(&hugetlb_lock);
551 if (surplus_huge_pages >= nr_overcommit_huge_pages) {
552 spin_unlock(&hugetlb_lock);
553 return NULL;
554 } else {
555 nr_huge_pages++;
556 surplus_huge_pages++;
557 }
558 spin_unlock(&hugetlb_lock);
559
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700560 page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
561 __GFP_REPEAT|__GFP_NOWARN,
Adam Litke7893d1d2007-10-16 01:26:18 -0700562 HUGETLB_PAGE_ORDER);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800563
564 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700565 if (page) {
Adam Litke2668db92008-03-10 11:43:50 -0700566 /*
567 * This page is now managed by the hugetlb allocator and has
568 * no users -- drop the buddy allocator's reference.
569 */
570 put_page_testzero(page);
571 VM_BUG_ON(page_count(page));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800572 nid = page_to_nid(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700573 set_compound_page_dtor(page, free_huge_page);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800574 /*
575 * We incremented the global counters already
576 */
577 nr_huge_pages_node[nid]++;
578 surplus_huge_pages_node[nid]++;
Adam Litke3b116302008-04-28 02:13:06 -0700579 __count_vm_event(HTLB_BUDDY_PGALLOC);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800580 } else {
581 nr_huge_pages--;
582 surplus_huge_pages--;
Adam Litke3b116302008-04-28 02:13:06 -0700583 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Adam Litke7893d1d2007-10-16 01:26:18 -0700584 }
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800585 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700586
587 return page;
588}
589
Adam Litkee4e574b2007-10-16 01:26:19 -0700590/*
591 * Increase the hugetlb pool such that it can accomodate a reservation
592 * of size 'delta'.
593 */
594static int gather_surplus_pages(int delta)
595{
596 struct list_head surplus_list;
597 struct page *page, *tmp;
598 int ret, i;
599 int needed, allocated;
600
601 needed = (resv_huge_pages + delta) - free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800602 if (needed <= 0) {
603 resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700604 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800605 }
Adam Litkee4e574b2007-10-16 01:26:19 -0700606
607 allocated = 0;
608 INIT_LIST_HEAD(&surplus_list);
609
610 ret = -ENOMEM;
611retry:
612 spin_unlock(&hugetlb_lock);
613 for (i = 0; i < needed; i++) {
614 page = alloc_buddy_huge_page(NULL, 0);
615 if (!page) {
616 /*
617 * We were not able to allocate enough pages to
618 * satisfy the entire reservation so we free what
619 * we've allocated so far.
620 */
621 spin_lock(&hugetlb_lock);
622 needed = 0;
623 goto free;
624 }
625
626 list_add(&page->lru, &surplus_list);
627 }
628 allocated += needed;
629
630 /*
631 * After retaking hugetlb_lock, we need to recalculate 'needed'
632 * because either resv_huge_pages or free_huge_pages may have changed.
633 */
634 spin_lock(&hugetlb_lock);
635 needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
636 if (needed > 0)
637 goto retry;
638
639 /*
640 * The surplus_list now contains _at_least_ the number of extra pages
641 * needed to accomodate the reservation. Add the appropriate number
642 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -0800643 * allocator. Commit the entire reservation here to prevent another
644 * process from stealing the pages as they are added to the pool but
645 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -0700646 */
647 needed += allocated;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800648 resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700649 ret = 0;
650free:
Adam Litke19fc3f02008-04-28 02:12:20 -0700651 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -0700652 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -0700653 if ((--needed) < 0)
654 break;
Adam Litkee4e574b2007-10-16 01:26:19 -0700655 list_del(&page->lru);
Adam Litke19fc3f02008-04-28 02:12:20 -0700656 enqueue_huge_page(page);
657 }
658
659 /* Free unnecessary surplus pages to the buddy allocator */
660 if (!list_empty(&surplus_list)) {
661 spin_unlock(&hugetlb_lock);
662 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
663 list_del(&page->lru);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700664 /*
Adam Litke2668db92008-03-10 11:43:50 -0700665 * The page has a reference count of zero already, so
666 * call free_huge_page directly instead of using
667 * put_page. This must be done with hugetlb_lock
Adam Litkeaf767cb2007-10-16 01:26:25 -0700668 * unlocked which is safe because free_huge_page takes
669 * hugetlb_lock before deciding how to free the page.
670 */
Adam Litke2668db92008-03-10 11:43:50 -0700671 free_huge_page(page);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700672 }
Adam Litke19fc3f02008-04-28 02:12:20 -0700673 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -0700674 }
675
676 return ret;
677}
678
679/*
680 * When releasing a hugetlb pool reservation, any surplus pages that were
681 * allocated to satisfy the reservation must be explicitly freed if they were
682 * never used.
683 */
Adrian Bunk8cde0452007-11-14 16:59:43 -0800684static void return_unused_surplus_pages(unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -0700685{
686 static int nid = -1;
687 struct page *page;
688 unsigned long nr_pages;
689
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700690 /*
691 * We want to release as many surplus pages as possible, spread
692 * evenly across all nodes. Iterate across all nodes until we
693 * can no longer free unreserved surplus pages. This occurs when
694 * the nodes with surplus pages have no free pages.
695 */
696 unsigned long remaining_iterations = num_online_nodes();
697
Adam Litkeac09b3a2008-03-04 14:29:38 -0800698 /* Uncommit the reservation */
699 resv_huge_pages -= unused_resv_pages;
700
Adam Litkee4e574b2007-10-16 01:26:19 -0700701 nr_pages = min(unused_resv_pages, surplus_huge_pages);
702
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700703 while (remaining_iterations-- && nr_pages) {
Adam Litkee4e574b2007-10-16 01:26:19 -0700704 nid = next_node(nid, node_online_map);
705 if (nid == MAX_NUMNODES)
706 nid = first_node(node_online_map);
707
708 if (!surplus_huge_pages_node[nid])
709 continue;
710
711 if (!list_empty(&hugepage_freelists[nid])) {
712 page = list_entry(hugepage_freelists[nid].next,
713 struct page, lru);
714 list_del(&page->lru);
715 update_and_free_page(page);
716 free_huge_pages--;
717 free_huge_pages_node[nid]--;
718 surplus_huge_pages--;
719 surplus_huge_pages_node[nid]--;
720 nr_pages--;
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700721 remaining_iterations = num_online_nodes();
Adam Litkee4e574b2007-10-16 01:26:19 -0700722 }
723 }
724}
725
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700726/*
727 * Determine if the huge page at addr within the vma has an associated
728 * reservation. Where it does not we will need to logically increase
729 * reservation and actually increase quota before an allocation can occur.
730 * Where any new reservation would be required the reservation change is
731 * prepared, but not committed. Once the page has been quota'd allocated
732 * an instantiated the change should be committed via vma_commit_reservation.
733 * No action is required on failure.
734 */
735static int vma_needs_reservation(struct vm_area_struct *vma, unsigned long addr)
736{
737 struct address_space *mapping = vma->vm_file->f_mapping;
738 struct inode *inode = mapping->host;
739
740 if (vma->vm_flags & VM_SHARED) {
741 pgoff_t idx = vma_pagecache_offset(vma, addr);
742 return region_chg(&inode->i_mapping->private_list,
743 idx, idx + 1);
744
745 } else {
746 if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER))
747 return 1;
748 }
749
750 return 0;
751}
752static void vma_commit_reservation(struct vm_area_struct *vma,
753 unsigned long addr)
754{
755 struct address_space *mapping = vma->vm_file->f_mapping;
756 struct inode *inode = mapping->host;
757
758 if (vma->vm_flags & VM_SHARED) {
759 pgoff_t idx = vma_pagecache_offset(vma, addr);
760 region_add(&inode->i_mapping->private_list, idx, idx + 1);
761 }
762}
763
David Gibson27a85ef2006-03-22 00:08:56 -0800764static struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700765 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Adam Litke348ea202007-11-14 16:59:37 -0800767 struct page *page;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800768 struct address_space *mapping = vma->vm_file->f_mapping;
Mel Gormana1e78772008-07-23 21:27:23 -0700769 struct inode *inode = mapping->host;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700770 unsigned int chg;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800771
Mel Gormana1e78772008-07-23 21:27:23 -0700772 /*
773 * Processes that did not create the mapping will have no reserves and
774 * will not have accounted against quota. Check that the quota can be
775 * made before satisfying the allocation
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700776 * MAP_NORESERVE mappings may also need pages and quota allocated
777 * if no reserve mapping overlaps.
Mel Gormana1e78772008-07-23 21:27:23 -0700778 */
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700779 chg = vma_needs_reservation(vma, addr);
780 if (chg < 0)
781 return ERR_PTR(chg);
782 if (chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700783 if (hugetlb_get_quota(inode->i_mapping, chg))
784 return ERR_PTR(-ENOSPC);
Mel Gormana1e78772008-07-23 21:27:23 -0700785
786 spin_lock(&hugetlb_lock);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700787 page = dequeue_huge_page_vma(vma, addr, avoid_reserve);
Mel Gormana1e78772008-07-23 21:27:23 -0700788 spin_unlock(&hugetlb_lock);
789
790 if (!page) {
791 page = alloc_buddy_huge_page(vma, addr);
792 if (!page) {
793 hugetlb_put_quota(inode->i_mapping, chg);
794 return ERR_PTR(-VM_FAULT_OOM);
795 }
796 }
797
798 set_page_refcounted(page);
799 set_page_private(page, (unsigned long) mapping);
800
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700801 vma_commit_reservation(vma, addr);
802
Adam Litke90d8b7e2007-11-14 16:59:42 -0800803 return page;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806static int __init hugetlb_init(void)
807{
808 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100810 if (HPAGE_SHIFT == 0)
811 return 0;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 for (i = 0; i < MAX_NUMNODES; ++i)
814 INIT_LIST_HEAD(&hugepage_freelists[i]);
815
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700816 hugetlb_next_nid = first_node(node_online_map);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 for (i = 0; i < max_huge_pages; ++i) {
Nick Piggina4822892006-03-22 00:08:08 -0800819 if (!alloc_fresh_huge_page())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822 max_huge_pages = free_huge_pages = nr_huge_pages = i;
823 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
824 return 0;
825}
826module_init(hugetlb_init);
827
828static int __init hugetlb_setup(char *s)
829{
830 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
831 max_huge_pages = 0;
832 return 1;
833}
834__setup("hugepages=", hugetlb_setup);
835
Ken Chen8a630112007-05-09 02:33:34 -0700836static unsigned int cpuset_mems_nr(unsigned int *array)
837{
838 int node;
839 unsigned int nr = 0;
840
841 for_each_node_mask(node, cpuset_current_mems_allowed)
842 nr += array[node];
843
844 return nr;
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848#ifdef CONFIG_HIGHMEM
849static void try_to_free_low(unsigned long count)
850{
Christoph Lameter4415cc82006-09-25 23:31:55 -0700851 int i;
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 for (i = 0; i < MAX_NUMNODES; ++i) {
854 struct page *page, *next;
855 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
Adam Litke6b0c8802007-10-16 01:26:23 -0700856 if (count >= nr_huge_pages)
857 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (PageHighMem(page))
859 continue;
860 list_del(&page->lru);
861 update_and_free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 free_huge_pages--;
Christoph Lameter4415cc82006-09-25 23:31:55 -0700863 free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865 }
866}
867#else
868static inline void try_to_free_low(unsigned long count)
869{
870}
871#endif
872
Adam Litke7893d1d2007-10-16 01:26:18 -0700873#define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static unsigned long set_max_huge_pages(unsigned long count)
875{
Adam Litke7893d1d2007-10-16 01:26:18 -0700876 unsigned long min_count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Adam Litke7893d1d2007-10-16 01:26:18 -0700878 /*
879 * Increase the pool size
880 * First take pages out of surplus state. Then make up the
881 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800882 *
883 * We might race with alloc_buddy_huge_page() here and be unable
884 * to convert a surplus huge page to a normal huge page. That is
885 * not critical, though, it just means the overall size of the
886 * pool might be one hugepage larger than it needs to be, but
887 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -0700888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700890 while (surplus_huge_pages && count > persistent_huge_pages) {
891 if (!adjust_pool_surplus(-1))
892 break;
893 }
894
895 while (count > persistent_huge_pages) {
Adam Litke7893d1d2007-10-16 01:26:18 -0700896 /*
897 * If this allocation races such that we no longer need the
898 * page, free_huge_page will handle it by freeing the page
899 * and reducing the surplus.
900 */
901 spin_unlock(&hugetlb_lock);
902 ret = alloc_fresh_huge_page();
903 spin_lock(&hugetlb_lock);
904 if (!ret)
905 goto out;
906
907 }
Adam Litke7893d1d2007-10-16 01:26:18 -0700908
909 /*
910 * Decrease the pool size
911 * First return free pages to the buddy allocator (being careful
912 * to keep enough around to satisfy reservations). Then place
913 * pages into surplus state as needed so the pool will shrink
914 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800915 *
916 * By placing pages into the surplus state independent of the
917 * overcommit value, we are allowing the surplus pool size to
918 * exceed overcommit. There are few sane options here. Since
919 * alloc_buddy_huge_page() is checking the global counter,
920 * though, we'll note that we're not allowed to exceed surplus
921 * and won't grow the pool anywhere else. Not until one of the
922 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -0700923 */
Adam Litke6b0c8802007-10-16 01:26:23 -0700924 min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
925 min_count = max(count, min_count);
Adam Litke7893d1d2007-10-16 01:26:18 -0700926 try_to_free_low(min_count);
927 while (min_count < persistent_huge_pages) {
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800928 struct page *page = dequeue_huge_page();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!page)
930 break;
931 update_and_free_page(page);
932 }
Adam Litke7893d1d2007-10-16 01:26:18 -0700933 while (count < persistent_huge_pages) {
934 if (!adjust_pool_surplus(1))
935 break;
936 }
937out:
938 ret = persistent_huge_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700940 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943int hugetlb_sysctl_handler(struct ctl_table *table, int write,
944 struct file *file, void __user *buffer,
945 size_t *length, loff_t *ppos)
946{
947 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
948 max_huge_pages = set_max_huge_pages(max_huge_pages);
949 return 0;
950}
Mel Gorman396faf02007-07-17 04:03:13 -0700951
952int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
953 struct file *file, void __user *buffer,
954 size_t *length, loff_t *ppos)
955{
956 proc_dointvec(table, write, file, buffer, length, ppos);
957 if (hugepages_treat_as_movable)
958 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
959 else
960 htlb_alloc_mask = GFP_HIGHUSER;
961 return 0;
962}
963
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -0800964int hugetlb_overcommit_handler(struct ctl_table *table, int write,
965 struct file *file, void __user *buffer,
966 size_t *length, loff_t *ppos)
967{
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -0800968 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Nishanth Aravamudan064d9ef2008-02-13 15:03:19 -0800969 spin_lock(&hugetlb_lock);
970 nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -0800971 spin_unlock(&hugetlb_lock);
972 return 0;
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975#endif /* CONFIG_SYSCTL */
976
977int hugetlb_report_meminfo(char *buf)
978{
979 return sprintf(buf,
980 "HugePages_Total: %5lu\n"
981 "HugePages_Free: %5lu\n"
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700982 "HugePages_Rsvd: %5lu\n"
Adam Litke7893d1d2007-10-16 01:26:18 -0700983 "HugePages_Surp: %5lu\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 "Hugepagesize: %5lu kB\n",
985 nr_huge_pages,
986 free_huge_pages,
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700987 resv_huge_pages,
Adam Litke7893d1d2007-10-16 01:26:18 -0700988 surplus_huge_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 HPAGE_SIZE/1024);
990}
991
992int hugetlb_report_node_meminfo(int nid, char *buf)
993{
994 return sprintf(buf,
995 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -0700996 "Node %d HugePages_Free: %5u\n"
997 "Node %d HugePages_Surp: %5u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 nid, nr_huge_pages_node[nid],
Nishanth Aravamudana1de0912008-03-26 14:37:53 -0700999 nid, free_huge_pages_node[nid],
1000 nid, surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
1004unsigned long hugetlb_total_pages(void)
1005{
1006 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
1007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001009static int hugetlb_acct_memory(long delta)
1010{
1011 int ret = -ENOMEM;
1012
1013 spin_lock(&hugetlb_lock);
1014 /*
1015 * When cpuset is configured, it breaks the strict hugetlb page
1016 * reservation as the accounting is done on a global variable. Such
1017 * reservation is completely rubbish in the presence of cpuset because
1018 * the reservation is not checked against page availability for the
1019 * current cpuset. Application can still potentially OOM'ed by kernel
1020 * with lack of free htlb page in cpuset that the task is in.
1021 * Attempt to enforce strict accounting with cpuset is almost
1022 * impossible (or too ugly) because cpuset is too fluid that
1023 * task or memory node can be dynamically moved between cpusets.
1024 *
1025 * The change of semantics for shared hugetlb mapping with cpuset is
1026 * undesirable. However, in order to preserve some of the semantics,
1027 * we fall back to check against current free page availability as
1028 * a best attempt and hopefully to minimize the impact of changing
1029 * semantics that cpuset has.
1030 */
1031 if (delta > 0) {
1032 if (gather_surplus_pages(delta) < 0)
1033 goto out;
1034
1035 if (delta > cpuset_mems_nr(free_huge_pages_node)) {
1036 return_unused_surplus_pages(delta);
1037 goto out;
1038 }
1039 }
1040
1041 ret = 0;
1042 if (delta < 0)
1043 return_unused_surplus_pages((unsigned long) -delta);
1044
1045out:
1046 spin_unlock(&hugetlb_lock);
1047 return ret;
1048}
1049
Mel Gormana1e78772008-07-23 21:27:23 -07001050static void hugetlb_vm_op_close(struct vm_area_struct *vma)
1051{
1052 unsigned long reserve = vma_resv_huge_pages(vma);
1053 if (reserve)
1054 hugetlb_acct_memory(-reserve);
1055}
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057/*
1058 * We cannot handle pagefaults against hugetlb pages at all. They cause
1059 * handle_mm_fault() to try to instantiate regular-sized pages in the
1060 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
1061 * this far.
1062 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001063static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07001070 .fault = hugetlb_vm_op_fault,
Mel Gormana1e78772008-07-23 21:27:23 -07001071 .close = hugetlb_vm_op_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072};
1073
David Gibson1e8f8892006-01-06 00:10:44 -08001074static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
1075 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07001076{
1077 pte_t entry;
1078
David Gibson1e8f8892006-01-06 00:10:44 -08001079 if (writable) {
David Gibson63551ae2005-06-21 17:14:44 -07001080 entry =
1081 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1082 } else {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001083 entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07001084 }
1085 entry = pte_mkyoung(entry);
1086 entry = pte_mkhuge(entry);
1087
1088 return entry;
1089}
1090
David Gibson1e8f8892006-01-06 00:10:44 -08001091static void set_huge_ptep_writable(struct vm_area_struct *vma,
1092 unsigned long address, pte_t *ptep)
1093{
1094 pte_t entry;
1095
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001096 entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
1097 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001098 update_mmu_cache(vma, address, entry);
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001099 }
David Gibson1e8f8892006-01-06 00:10:44 -08001100}
1101
1102
David Gibson63551ae2005-06-21 17:14:44 -07001103int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
1104 struct vm_area_struct *vma)
1105{
1106 pte_t *src_pte, *dst_pte, entry;
1107 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07001108 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08001109 int cow;
1110
1111 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07001112
Hugh Dickins1c598272005-10-19 21:23:43 -07001113 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
Hugh Dickinsc74df322005-10-29 18:16:23 -07001114 src_pte = huge_pte_offset(src, addr);
1115 if (!src_pte)
1116 continue;
David Gibson63551ae2005-06-21 17:14:44 -07001117 dst_pte = huge_pte_alloc(dst, addr);
1118 if (!dst_pte)
1119 goto nomem;
Larry Woodmanc5c99422008-01-24 05:49:25 -08001120
1121 /* If the pagetables are shared don't copy or take references */
1122 if (dst_pte == src_pte)
1123 continue;
1124
Hugh Dickinsc74df322005-10-29 18:16:23 -07001125 spin_lock(&dst->page_table_lock);
Nick Piggin46478752008-06-05 22:45:57 -07001126 spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001127 if (!huge_pte_none(huge_ptep_get(src_pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001128 if (cow)
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001129 huge_ptep_set_wrprotect(src, addr, src_pte);
1130 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07001131 ptepage = pte_page(entry);
1132 get_page(ptepage);
Hugh Dickins1c598272005-10-19 21:23:43 -07001133 set_huge_pte_at(dst, addr, dst_pte, entry);
1134 }
1135 spin_unlock(&src->page_table_lock);
Hugh Dickinsc74df322005-10-29 18:16:23 -07001136 spin_unlock(&dst->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001137 }
1138 return 0;
1139
1140nomem:
1141 return -ENOMEM;
1142}
1143
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001144void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001145 unsigned long end, struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07001146{
1147 struct mm_struct *mm = vma->vm_mm;
1148 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07001149 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07001150 pte_t pte;
1151 struct page *page;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001152 struct page *tmp;
Chen, Kenneth Wc0a499c2006-12-06 20:31:39 -08001153 /*
1154 * A page gathering list, protected by per file i_mmap_lock. The
1155 * lock is used to avoid list corruption from multiple unmapping
1156 * of the same page since we are using page->lru.
1157 */
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001158 LIST_HEAD(page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001159
1160 WARN_ON(!is_vm_hugetlb_page(vma));
1161 BUG_ON(start & ~HPAGE_MASK);
1162 BUG_ON(end & ~HPAGE_MASK);
1163
Hugh Dickins508034a2005-10-29 18:16:30 -07001164 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001165 for (address = start; address < end; address += HPAGE_SIZE) {
David Gibsonc7546f82005-08-05 11:59:35 -07001166 ptep = huge_pte_offset(mm, address);
Adam Litke4c887262005-10-29 18:16:46 -07001167 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07001168 continue;
1169
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001170 if (huge_pmd_unshare(mm, &address, ptep))
1171 continue;
1172
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001173 /*
1174 * If a reference page is supplied, it is because a specific
1175 * page is being unmapped, not a range. Ensure the page we
1176 * are about to unmap is the actual page of interest.
1177 */
1178 if (ref_page) {
1179 pte = huge_ptep_get(ptep);
1180 if (huge_pte_none(pte))
1181 continue;
1182 page = pte_page(pte);
1183 if (page != ref_page)
1184 continue;
1185
1186 /*
1187 * Mark the VMA as having unmapped its page so that
1188 * future faults in this VMA will fail rather than
1189 * looking like data was lost
1190 */
1191 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
1192 }
1193
David Gibsonc7546f82005-08-05 11:59:35 -07001194 pte = huge_ptep_get_and_clear(mm, address, ptep);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001195 if (huge_pte_none(pte))
David Gibson63551ae2005-06-21 17:14:44 -07001196 continue;
David Gibsonc7546f82005-08-05 11:59:35 -07001197
David Gibson63551ae2005-06-21 17:14:44 -07001198 page = pte_page(pte);
Ken Chen6649a382007-02-08 14:20:27 -08001199 if (pte_dirty(pte))
1200 set_page_dirty(page);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001201 list_add(&page->lru, &page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 spin_unlock(&mm->page_table_lock);
Hugh Dickins508034a2005-10-29 18:16:30 -07001204 flush_tlb_range(vma, start, end);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001205 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1206 list_del(&page->lru);
1207 put_page(page);
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
David Gibson63551ae2005-06-21 17:14:44 -07001210
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001211void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001212 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001213{
1214 /*
1215 * It is undesirable to test vma->vm_file as it should be non-null
1216 * for valid hugetlb area. However, vm_file will be NULL in the error
1217 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
1218 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
1219 * to clean up. Since no pte has actually been setup, it is safe to
1220 * do nothing in this case.
1221 */
1222 if (vma->vm_file) {
1223 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001224 __unmap_hugepage_range(vma, start, end, ref_page);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001225 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
1226 }
1227}
1228
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001229/*
1230 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1231 * mappping it owns the reserve page for. The intention is to unmap the page
1232 * from other VMAs and let the children be SIGKILLed if they are faulting the
1233 * same region.
1234 */
1235int unmap_ref_private(struct mm_struct *mm,
1236 struct vm_area_struct *vma,
1237 struct page *page,
1238 unsigned long address)
1239{
1240 struct vm_area_struct *iter_vma;
1241 struct address_space *mapping;
1242 struct prio_tree_iter iter;
1243 pgoff_t pgoff;
1244
1245 /*
1246 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1247 * from page cache lookup which is in HPAGE_SIZE units.
1248 */
1249 address = address & huge_page_mask(hstate_vma(vma));
1250 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
1251 + (vma->vm_pgoff >> PAGE_SHIFT);
1252 mapping = (struct address_space *)page_private(page);
1253
1254 vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1255 /* Do not unmap the current VMA */
1256 if (iter_vma == vma)
1257 continue;
1258
1259 /*
1260 * Unmap the page from other VMAs without their own reserves.
1261 * They get marked to be SIGKILLed if they fault in these
1262 * areas. This is because a future no-page fault on this VMA
1263 * could insert a zeroed page instead of the data existing
1264 * from the time of fork. This would look like data corruption
1265 */
1266 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
1267 unmap_hugepage_range(iter_vma,
1268 address, address + HPAGE_SIZE,
1269 page);
1270 }
1271
1272 return 1;
1273}
1274
David Gibson1e8f8892006-01-06 00:10:44 -08001275static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001276 unsigned long address, pte_t *ptep, pte_t pte,
1277 struct page *pagecache_page)
David Gibson1e8f8892006-01-06 00:10:44 -08001278{
1279 struct page *old_page, *new_page;
David Gibson79ac6ba2006-03-22 00:08:51 -08001280 int avoidcopy;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001281 int outside_reserve = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001282
1283 old_page = pte_page(pte);
1284
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001285retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08001286 /* If no-one else is actually using this page, avoid the copy
1287 * and just make the page writable */
1288 avoidcopy = (page_count(old_page) == 1);
1289 if (avoidcopy) {
1290 set_huge_ptep_writable(vma, address, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07001291 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001292 }
1293
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001294 /*
1295 * If the process that created a MAP_PRIVATE mapping is about to
1296 * perform a COW due to a shared page count, attempt to satisfy
1297 * the allocation without using the existing reserves. The pagecache
1298 * page is used to determine if the reserve at this address was
1299 * consumed or not. If reserves were used, a partial faulted mapping
1300 * at the time of fork() could consume its reserves on COW instead
1301 * of the full address range.
1302 */
1303 if (!(vma->vm_flags & VM_SHARED) &&
1304 is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
1305 old_page != pagecache_page)
1306 outside_reserve = 1;
1307
David Gibson1e8f8892006-01-06 00:10:44 -08001308 page_cache_get(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001309 new_page = alloc_huge_page(vma, address, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08001310
Adam Litke2fc39ce2007-11-14 16:59:39 -08001311 if (IS_ERR(new_page)) {
David Gibson1e8f8892006-01-06 00:10:44 -08001312 page_cache_release(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001313
1314 /*
1315 * If a process owning a MAP_PRIVATE mapping fails to COW,
1316 * it is due to references held by a child and an insufficient
1317 * huge page pool. To guarantee the original mappers
1318 * reliability, unmap the page from child processes. The child
1319 * may get SIGKILLed if it later faults.
1320 */
1321 if (outside_reserve) {
1322 BUG_ON(huge_pte_none(pte));
1323 if (unmap_ref_private(mm, vma, old_page, address)) {
1324 BUG_ON(page_count(old_page) != 1);
1325 BUG_ON(huge_pte_none(pte));
1326 goto retry_avoidcopy;
1327 }
1328 WARN_ON_ONCE(1);
1329 }
1330
Adam Litke2fc39ce2007-11-14 16:59:39 -08001331 return -PTR_ERR(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001332 }
1333
1334 spin_unlock(&mm->page_table_lock);
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00001335 copy_huge_page(new_page, old_page, address, vma);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001336 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001337 spin_lock(&mm->page_table_lock);
1338
1339 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001340 if (likely(pte_same(huge_ptep_get(ptep), pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001341 /* Break COW */
Gerald Schaefer8fe627e2008-04-28 02:13:28 -07001342 huge_ptep_clear_flush(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08001343 set_huge_pte_at(mm, address, ptep,
1344 make_huge_pte(vma, new_page, 1));
1345 /* Make the old page be freed below */
1346 new_page = old_page;
1347 }
1348 page_cache_release(new_page);
1349 page_cache_release(old_page);
Nick Piggin83c54072007-07-19 01:47:05 -07001350 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001351}
1352
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001353/* Return the pagecache page at a given address within a VMA */
1354static struct page *hugetlbfs_pagecache_page(struct vm_area_struct *vma,
1355 unsigned long address)
1356{
1357 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001358 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001359
1360 mapping = vma->vm_file->f_mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001361 idx = vma_pagecache_offset(vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001362
1363 return find_lock_page(mapping, idx);
1364}
1365
Robert P. J. Daya1ed3dd2007-07-17 04:03:33 -07001366static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
David Gibson1e8f8892006-01-06 00:10:44 -08001367 unsigned long address, pte_t *ptep, int write_access)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001368{
1369 int ret = VM_FAULT_SIGBUS;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001370 pgoff_t idx;
Adam Litke4c887262005-10-29 18:16:46 -07001371 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07001372 struct page *page;
1373 struct address_space *mapping;
David Gibson1e8f8892006-01-06 00:10:44 -08001374 pte_t new_pte;
Adam Litke4c887262005-10-29 18:16:46 -07001375
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001376 /*
1377 * Currently, we are forced to kill the process in the event the
1378 * original mapper has unmapped pages from the child due to a failed
1379 * COW. Warn that such a situation has occured as it may not be obvious
1380 */
1381 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
1382 printk(KERN_WARNING
1383 "PID %d killed due to inadequate hugepage pool\n",
1384 current->pid);
1385 return ret;
1386 }
1387
Adam Litke4c887262005-10-29 18:16:46 -07001388 mapping = vma->vm_file->f_mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001389 idx = vma_pagecache_offset(vma, address);
Adam Litke4c887262005-10-29 18:16:46 -07001390
1391 /*
1392 * Use page lock to guard against racing truncation
1393 * before we get page_table_lock.
1394 */
Christoph Lameter6bda6662006-01-06 00:10:49 -08001395retry:
1396 page = find_lock_page(mapping, idx);
1397 if (!page) {
Hugh Dickinsebed4bf2006-10-28 10:38:43 -07001398 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
1399 if (idx >= size)
1400 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001401 page = alloc_huge_page(vma, address, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08001402 if (IS_ERR(page)) {
1403 ret = -PTR_ERR(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001404 goto out;
1405 }
David Gibson79ac6ba2006-03-22 00:08:51 -08001406 clear_huge_page(page, address);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001407 __SetPageUptodate(page);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001408
Christoph Lameter6bda6662006-01-06 00:10:49 -08001409 if (vma->vm_flags & VM_SHARED) {
1410 int err;
Ken Chen45c682a2007-11-14 16:59:44 -08001411 struct inode *inode = mapping->host;
Christoph Lameter6bda6662006-01-06 00:10:49 -08001412
1413 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
1414 if (err) {
1415 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001416 if (err == -EEXIST)
1417 goto retry;
1418 goto out;
1419 }
Ken Chen45c682a2007-11-14 16:59:44 -08001420
1421 spin_lock(&inode->i_lock);
1422 inode->i_blocks += BLOCKS_PER_HUGEPAGE;
1423 spin_unlock(&inode->i_lock);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001424 } else
1425 lock_page(page);
1426 }
David Gibson1e8f8892006-01-06 00:10:44 -08001427
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001428 spin_lock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001429 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
1430 if (idx >= size)
1431 goto backout;
1432
Nick Piggin83c54072007-07-19 01:47:05 -07001433 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001434 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07001435 goto backout;
1436
David Gibson1e8f8892006-01-06 00:10:44 -08001437 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
1438 && (vma->vm_flags & VM_SHARED)));
1439 set_huge_pte_at(mm, address, ptep, new_pte);
1440
1441 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1442 /* Optimization, do the COW without a second fault */
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001443 ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
David Gibson1e8f8892006-01-06 00:10:44 -08001444 }
1445
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001446 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001447 unlock_page(page);
1448out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001449 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07001450
1451backout:
1452 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001453 unlock_page(page);
1454 put_page(page);
1455 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001456}
1457
Adam Litke86e52162006-01-06 00:10:43 -08001458int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1459 unsigned long address, int write_access)
1460{
1461 pte_t *ptep;
1462 pte_t entry;
David Gibson1e8f8892006-01-06 00:10:44 -08001463 int ret;
David Gibson3935baa2006-03-22 00:08:53 -08001464 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
Adam Litke86e52162006-01-06 00:10:43 -08001465
1466 ptep = huge_pte_alloc(mm, address);
1467 if (!ptep)
1468 return VM_FAULT_OOM;
1469
David Gibson3935baa2006-03-22 00:08:53 -08001470 /*
1471 * Serialize hugepage allocation and instantiation, so that we don't
1472 * get spurious allocation failures if two CPUs race to instantiate
1473 * the same page in the page cache.
1474 */
1475 mutex_lock(&hugetlb_instantiation_mutex);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001476 entry = huge_ptep_get(ptep);
1477 if (huge_pte_none(entry)) {
David Gibson3935baa2006-03-22 00:08:53 -08001478 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
1479 mutex_unlock(&hugetlb_instantiation_mutex);
1480 return ret;
1481 }
Adam Litke86e52162006-01-06 00:10:43 -08001482
Nick Piggin83c54072007-07-19 01:47:05 -07001483 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001484
1485 spin_lock(&mm->page_table_lock);
1486 /* Check for a racing update before calling hugetlb_cow */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001487 if (likely(pte_same(entry, huge_ptep_get(ptep))))
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001488 if (write_access && !pte_write(entry)) {
1489 struct page *page;
1490 page = hugetlbfs_pagecache_page(vma, address);
1491 ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
1492 if (page) {
1493 unlock_page(page);
1494 put_page(page);
1495 }
1496 }
David Gibson1e8f8892006-01-06 00:10:44 -08001497 spin_unlock(&mm->page_table_lock);
David Gibson3935baa2006-03-22 00:08:53 -08001498 mutex_unlock(&hugetlb_instantiation_mutex);
David Gibson1e8f8892006-01-06 00:10:44 -08001499
1500 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08001501}
1502
David Gibson63551ae2005-06-21 17:14:44 -07001503int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
1504 struct page **pages, struct vm_area_struct **vmas,
Adam Litke5b23dbe2007-11-14 16:59:33 -08001505 unsigned long *position, int *length, int i,
1506 int write)
David Gibson63551ae2005-06-21 17:14:44 -07001507{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001508 unsigned long pfn_offset;
1509 unsigned long vaddr = *position;
David Gibson63551ae2005-06-21 17:14:44 -07001510 int remainder = *length;
1511
Hugh Dickins1c598272005-10-19 21:23:43 -07001512 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001513 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07001514 pte_t *pte;
1515 struct page *page;
1516
1517 /*
1518 * Some archs (sparc64, sh*) have multiple pte_ts to
1519 * each hugepage. We have to make * sure we get the
1520 * first, for the page indexing below to work.
1521 */
1522 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
1523
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001524 if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
1525 (write && !pte_write(huge_ptep_get(pte)))) {
Adam Litke4c887262005-10-29 18:16:46 -07001526 int ret;
1527
1528 spin_unlock(&mm->page_table_lock);
Adam Litke5b23dbe2007-11-14 16:59:33 -08001529 ret = hugetlb_fault(mm, vma, vaddr, write);
Adam Litke4c887262005-10-29 18:16:46 -07001530 spin_lock(&mm->page_table_lock);
Adam Litkea89182c2007-08-22 14:01:51 -07001531 if (!(ret & VM_FAULT_ERROR))
Adam Litke4c887262005-10-29 18:16:46 -07001532 continue;
1533
1534 remainder = 0;
1535 if (!i)
1536 i = -EFAULT;
1537 break;
1538 }
David Gibson63551ae2005-06-21 17:14:44 -07001539
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001540 pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001541 page = pte_page(huge_ptep_get(pte));
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001542same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001543 if (pages) {
1544 get_page(page);
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001545 pages[i] = page + pfn_offset;
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001546 }
David Gibson63551ae2005-06-21 17:14:44 -07001547
1548 if (vmas)
1549 vmas[i] = vma;
1550
1551 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001552 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07001553 --remainder;
1554 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001555 if (vaddr < vma->vm_end && remainder &&
1556 pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
1557 /*
1558 * We use pfn_offset to avoid touching the pageframes
1559 * of this compound page.
1560 */
1561 goto same_page;
1562 }
David Gibson63551ae2005-06-21 17:14:44 -07001563 }
Hugh Dickins1c598272005-10-19 21:23:43 -07001564 spin_unlock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001565 *length = remainder;
1566 *position = vaddr;
1567
1568 return i;
1569}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001570
1571void hugetlb_change_protection(struct vm_area_struct *vma,
1572 unsigned long address, unsigned long end, pgprot_t newprot)
1573{
1574 struct mm_struct *mm = vma->vm_mm;
1575 unsigned long start = address;
1576 pte_t *ptep;
1577 pte_t pte;
1578
1579 BUG_ON(address >= end);
1580 flush_cache_range(vma, address, end);
1581
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001582 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001583 spin_lock(&mm->page_table_lock);
1584 for (; address < end; address += HPAGE_SIZE) {
1585 ptep = huge_pte_offset(mm, address);
1586 if (!ptep)
1587 continue;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001588 if (huge_pmd_unshare(mm, &address, ptep))
1589 continue;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001590 if (!huge_pte_none(huge_ptep_get(ptep))) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001591 pte = huge_ptep_get_and_clear(mm, address, ptep);
1592 pte = pte_mkhuge(pte_modify(pte, newprot));
1593 set_huge_pte_at(mm, address, ptep, pte);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001594 }
1595 }
1596 spin_unlock(&mm->page_table_lock);
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001597 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001598
1599 flush_tlb_range(vma, start, end);
1600}
1601
Mel Gormana1e78772008-07-23 21:27:23 -07001602int hugetlb_reserve_pages(struct inode *inode,
1603 long from, long to,
1604 struct vm_area_struct *vma)
Adam Litkee4e574b2007-10-16 01:26:19 -07001605{
1606 long ret, chg;
1607
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001608 if (vma && vma->vm_flags & VM_NORESERVE)
1609 return 0;
1610
Mel Gormana1e78772008-07-23 21:27:23 -07001611 /*
1612 * Shared mappings base their reservation on the number of pages that
1613 * are already allocated on behalf of the file. Private mappings need
1614 * to reserve the full area even if read-only as mprotect() may be
1615 * called to make the mapping read-write. Assume !vma is a shm mapping
1616 */
1617 if (!vma || vma->vm_flags & VM_SHARED)
1618 chg = region_chg(&inode->i_mapping->private_list, from, to);
1619 else {
1620 chg = to - from;
1621 set_vma_resv_huge_pages(vma, chg);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001622 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
Mel Gormana1e78772008-07-23 21:27:23 -07001623 }
1624
Adam Litkee4e574b2007-10-16 01:26:19 -07001625 if (chg < 0)
1626 return chg;
Ken Chen8a630112007-05-09 02:33:34 -07001627
Adam Litke90d8b7e2007-11-14 16:59:42 -08001628 if (hugetlb_get_quota(inode->i_mapping, chg))
1629 return -ENOSPC;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001630 ret = hugetlb_acct_memory(chg);
Ken Chen68842c92008-01-14 00:55:19 -08001631 if (ret < 0) {
1632 hugetlb_put_quota(inode->i_mapping, chg);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001633 return ret;
Ken Chen68842c92008-01-14 00:55:19 -08001634 }
Mel Gormana1e78772008-07-23 21:27:23 -07001635 if (!vma || vma->vm_flags & VM_SHARED)
1636 region_add(&inode->i_mapping->private_list, from, to);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001637 return 0;
1638}
1639
1640void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1641{
1642 long chg = region_truncate(&inode->i_mapping->private_list, offset);
Ken Chen45c682a2007-11-14 16:59:44 -08001643
1644 spin_lock(&inode->i_lock);
1645 inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
1646 spin_unlock(&inode->i_lock);
1647
Adam Litke90d8b7e2007-11-14 16:59:42 -08001648 hugetlb_put_quota(inode->i_mapping, (chg - freed));
1649 hugetlb_acct_memory(-(chg - freed));
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001650}