blob: 32dff4290c66ede5e8fea935b7242e78b7d11928 [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.
Andy Whitcroft84afd992008-07-23 21:27:32 -070046 *
47 * The region data structures are protected by a combination of the mmap_sem
48 * and the hugetlb_instantion_mutex. To access or modify a region the caller
49 * must either hold the mmap_sem for write, or the mmap_sem for read and
50 * the hugetlb_instantiation mutex:
51 *
52 * down_write(&mm->mmap_sem);
53 * or
54 * down_read(&mm->mmap_sem);
55 * mutex_lock(&hugetlb_instantiation_mutex);
Andy Whitcroft96822902008-07-23 21:27:29 -070056 */
57struct file_region {
58 struct list_head link;
59 long from;
60 long to;
61};
62
63static long region_add(struct list_head *head, long f, long t)
64{
65 struct file_region *rg, *nrg, *trg;
66
67 /* Locate the region we are either in or before. */
68 list_for_each_entry(rg, head, link)
69 if (f <= rg->to)
70 break;
71
72 /* Round our left edge to the current segment if it encloses us. */
73 if (f > rg->from)
74 f = rg->from;
75
76 /* Check for and consume any regions we now overlap with. */
77 nrg = rg;
78 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
79 if (&rg->link == head)
80 break;
81 if (rg->from > t)
82 break;
83
84 /* If this area reaches higher then extend our area to
85 * include it completely. If this is not the first area
86 * which we intend to reuse, free it. */
87 if (rg->to > t)
88 t = rg->to;
89 if (rg != nrg) {
90 list_del(&rg->link);
91 kfree(rg);
92 }
93 }
94 nrg->from = f;
95 nrg->to = t;
96 return 0;
97}
98
99static long region_chg(struct list_head *head, long f, long t)
100{
101 struct file_region *rg, *nrg;
102 long chg = 0;
103
104 /* Locate the region we are before or in. */
105 list_for_each_entry(rg, head, link)
106 if (f <= rg->to)
107 break;
108
109 /* If we are below the current region then a new region is required.
110 * Subtle, allocate a new region at the position but make it zero
111 * size such that we can guarantee to record the reservation. */
112 if (&rg->link == head || t < rg->from) {
113 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
114 if (!nrg)
115 return -ENOMEM;
116 nrg->from = f;
117 nrg->to = f;
118 INIT_LIST_HEAD(&nrg->link);
119 list_add(&nrg->link, rg->link.prev);
120
121 return t - f;
122 }
123
124 /* Round our left edge to the current segment if it encloses us. */
125 if (f > rg->from)
126 f = rg->from;
127 chg = t - f;
128
129 /* Check for and consume any regions we now overlap with. */
130 list_for_each_entry(rg, rg->link.prev, link) {
131 if (&rg->link == head)
132 break;
133 if (rg->from > t)
134 return chg;
135
136 /* We overlap with this area, if it extends futher than
137 * us then we must extend ourselves. Account for its
138 * existing reservation. */
139 if (rg->to > t) {
140 chg += rg->to - t;
141 t = rg->to;
142 }
143 chg -= rg->to - rg->from;
144 }
145 return chg;
146}
147
148static long region_truncate(struct list_head *head, long end)
149{
150 struct file_region *rg, *trg;
151 long chg = 0;
152
153 /* Locate the region we are either in or before. */
154 list_for_each_entry(rg, head, link)
155 if (end <= rg->to)
156 break;
157 if (&rg->link == head)
158 return 0;
159
160 /* If we are in the middle of a region then adjust it. */
161 if (end > rg->from) {
162 chg = rg->to - end;
163 rg->to = end;
164 rg = list_entry(rg->link.next, typeof(*rg), link);
165 }
166
167 /* Drop any remaining regions. */
168 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
169 if (&rg->link == head)
170 break;
171 chg += rg->to - rg->from;
172 list_del(&rg->link);
173 kfree(rg);
174 }
175 return chg;
176}
177
Andy Whitcroft84afd992008-07-23 21:27:32 -0700178static long region_count(struct list_head *head, long f, long t)
179{
180 struct file_region *rg;
181 long chg = 0;
182
183 /* Locate each segment we overlap with, and count that overlap. */
184 list_for_each_entry(rg, head, link) {
185 int seg_from;
186 int seg_to;
187
188 if (rg->to <= f)
189 continue;
190 if (rg->from >= t)
191 break;
192
193 seg_from = max(rg->from, f);
194 seg_to = min(rg->to, t);
195
196 chg += seg_to - seg_from;
197 }
198
199 return chg;
200}
201
Andy Whitcroft96822902008-07-23 21:27:29 -0700202/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700203 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700204 * the mapping, in pagecache page units; huge pages here.
205 */
Johannes Weinera858f7b2008-07-23 21:27:33 -0700206static pgoff_t vma_hugecache_offset(struct vm_area_struct *vma,
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700207 unsigned long address)
208{
209 return ((address - vma->vm_start) >> HPAGE_SHIFT) +
210 (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
211}
212
Andy Whitcroft84afd992008-07-23 21:27:32 -0700213/*
214 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
215 * bits of the reservation map pointer, which are always clear due to
216 * alignment.
217 */
218#define HPAGE_RESV_OWNER (1UL << 0)
219#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700220#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700221
Mel Gormana1e78772008-07-23 21:27:23 -0700222/*
223 * These helpers are used to track how many pages are reserved for
224 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
225 * is guaranteed to have their future faults succeed.
226 *
227 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
228 * the reserve counters are updated with the hugetlb_lock held. It is safe
229 * to reset the VMA at fork() time as it is not in use yet and there is no
230 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700231 *
232 * The private mapping reservation is represented in a subtly different
233 * manner to a shared mapping. A shared mapping has a region map associated
234 * with the underlying file, this region map represents the backing file
235 * pages which have ever had a reservation assigned which this persists even
236 * after the page is instantiated. A private mapping has a region map
237 * associated with the original mmap which is attached to all VMAs which
238 * reference it, this region map represents those offsets which have consumed
239 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -0700240 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700241static unsigned long get_vma_private_data(struct vm_area_struct *vma)
242{
243 return (unsigned long)vma->vm_private_data;
244}
245
246static void set_vma_private_data(struct vm_area_struct *vma,
247 unsigned long value)
248{
249 vma->vm_private_data = (void *)value;
250}
251
Andy Whitcroft84afd992008-07-23 21:27:32 -0700252struct resv_map {
253 struct kref refs;
254 struct list_head regions;
255};
256
257struct resv_map *resv_map_alloc(void)
258{
259 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
260 if (!resv_map)
261 return NULL;
262
263 kref_init(&resv_map->refs);
264 INIT_LIST_HEAD(&resv_map->regions);
265
266 return resv_map;
267}
268
269void resv_map_release(struct kref *ref)
270{
271 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
272
273 /* Clear out any active regions before we release the map. */
274 region_truncate(&resv_map->regions, 0);
275 kfree(resv_map);
276}
277
278static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700279{
280 VM_BUG_ON(!is_vm_hugetlb_page(vma));
281 if (!(vma->vm_flags & VM_SHARED))
Andy Whitcroft84afd992008-07-23 21:27:32 -0700282 return (struct resv_map *)(get_vma_private_data(vma) &
283 ~HPAGE_RESV_MASK);
Mel Gormana1e78772008-07-23 21:27:23 -0700284 return 0;
285}
286
Andy Whitcroft84afd992008-07-23 21:27:32 -0700287static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -0700288{
289 VM_BUG_ON(!is_vm_hugetlb_page(vma));
290 VM_BUG_ON(vma->vm_flags & VM_SHARED);
291
Andy Whitcroft84afd992008-07-23 21:27:32 -0700292 set_vma_private_data(vma, (get_vma_private_data(vma) &
293 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700294}
295
296static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
297{
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700298 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700299 VM_BUG_ON(vma->vm_flags & VM_SHARED);
300
301 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700302}
303
304static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
305{
306 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700307
308 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700309}
310
311/* Decrement the reserved pages in the hugepage pool by one */
312static void decrement_hugepage_resv_vma(struct vm_area_struct *vma)
313{
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700314 if (vma->vm_flags & VM_NORESERVE)
315 return;
316
Mel Gormana1e78772008-07-23 21:27:23 -0700317 if (vma->vm_flags & VM_SHARED) {
318 /* Shared mappings always use reserves */
319 resv_huge_pages--;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700320 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Mel Gormana1e78772008-07-23 21:27:23 -0700321 /*
322 * Only the process that called mmap() has reserves for
323 * private mappings.
324 */
Andy Whitcroft84afd992008-07-23 21:27:32 -0700325 resv_huge_pages--;
Mel Gormana1e78772008-07-23 21:27:23 -0700326 }
327}
328
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700329/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700330void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
331{
332 VM_BUG_ON(!is_vm_hugetlb_page(vma));
333 if (!(vma->vm_flags & VM_SHARED))
334 vma->vm_private_data = (void *)0;
335}
336
337/* Returns true if the VMA has associated reserve pages */
338static int vma_has_private_reserves(struct vm_area_struct *vma)
339{
340 if (vma->vm_flags & VM_SHARED)
341 return 0;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700342 if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER))
Mel Gormana1e78772008-07-23 21:27:23 -0700343 return 0;
344 return 1;
345}
346
David Gibson79ac6ba2006-03-22 00:08:51 -0800347static void clear_huge_page(struct page *page, unsigned long addr)
348{
349 int i;
350
351 might_sleep();
352 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
353 cond_resched();
Ralf Baechle281e0e32007-10-01 01:20:10 -0700354 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
David Gibson79ac6ba2006-03-22 00:08:51 -0800355 }
356}
357
358static void copy_huge_page(struct page *dst, struct page *src,
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000359 unsigned long addr, struct vm_area_struct *vma)
David Gibson79ac6ba2006-03-22 00:08:51 -0800360{
361 int i;
362
363 might_sleep();
364 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
365 cond_resched();
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000366 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800367 }
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static void enqueue_huge_page(struct page *page)
371{
372 int nid = page_to_nid(page);
373 list_add(&page->lru, &hugepage_freelists[nid]);
374 free_huge_pages++;
375 free_huge_pages_node[nid]++;
376}
377
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800378static struct page *dequeue_huge_page(void)
379{
380 int nid;
381 struct page *page = NULL;
382
383 for (nid = 0; nid < MAX_NUMNODES; ++nid) {
384 if (!list_empty(&hugepage_freelists[nid])) {
385 page = list_entry(hugepage_freelists[nid].next,
386 struct page, lru);
387 list_del(&page->lru);
388 free_huge_pages--;
389 free_huge_pages_node[nid]--;
390 break;
391 }
392 }
393 return page;
394}
395
396static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700397 unsigned long address, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Nishanth Aravamudan31a5c6e2007-07-15 23:38:02 -0700399 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct page *page = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700401 struct mempolicy *mpol;
Mel Gorman19770b32008-04-28 02:12:18 -0700402 nodemask_t *nodemask;
Mel Gorman396faf02007-07-17 04:03:13 -0700403 struct zonelist *zonelist = huge_zonelist(vma, address,
Mel Gorman19770b32008-04-28 02:12:18 -0700404 htlb_alloc_mask, &mpol, &nodemask);
Mel Gormandd1a2392008-04-28 02:12:17 -0700405 struct zone *zone;
406 struct zoneref *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Mel Gormana1e78772008-07-23 21:27:23 -0700408 /*
409 * A child process with MAP_PRIVATE mappings created by their parent
410 * have no page reserves. This check ensures that reservations are
411 * not "stolen". The child may still get SIGKILLed
412 */
413 if (!vma_has_private_reserves(vma) &&
414 free_huge_pages - resv_huge_pages == 0)
415 return NULL;
416
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700417 /* If reserves cannot be used, ensure enough pages are in the pool */
418 if (avoid_reserve && free_huge_pages - resv_huge_pages == 0)
419 return NULL;
420
Mel Gorman19770b32008-04-28 02:12:18 -0700421 for_each_zone_zonelist_nodemask(zone, z, zonelist,
422 MAX_NR_ZONES - 1, nodemask) {
Mel Gorman54a6eb52008-04-28 02:12:16 -0700423 nid = zone_to_nid(zone);
424 if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
Andrew Morton3abf7af2007-07-19 01:49:08 -0700425 !list_empty(&hugepage_freelists[nid])) {
426 page = list_entry(hugepage_freelists[nid].next,
427 struct page, lru);
428 list_del(&page->lru);
429 free_huge_pages--;
430 free_huge_pages_node[nid]--;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700431
432 if (!avoid_reserve)
433 decrement_hugepage_resv_vma(vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700434
Ken Chen5ab3ee72007-07-23 18:44:00 -0700435 break;
Andrew Morton3abf7af2007-07-19 01:49:08 -0700436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700438 mpol_cond_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return page;
440}
441
Adam Litke6af2acb2007-10-16 01:26:16 -0700442static void update_and_free_page(struct page *page)
443{
444 int i;
445 nr_huge_pages--;
446 nr_huge_pages_node[page_to_nid(page)]--;
447 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
448 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
449 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
450 1 << PG_private | 1<< PG_writeback);
451 }
452 set_compound_page_dtor(page, NULL);
453 set_page_refcounted(page);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700454 arch_release_hugepage(page);
Adam Litke6af2acb2007-10-16 01:26:16 -0700455 __free_pages(page, HUGETLB_PAGE_ORDER);
456}
457
David Gibson27a85ef2006-03-22 00:08:56 -0800458static void free_huge_page(struct page *page)
459{
Adam Litke7893d1d2007-10-16 01:26:18 -0700460 int nid = page_to_nid(page);
Adam Litkec79fb752007-11-14 16:59:38 -0800461 struct address_space *mapping;
David Gibson27a85ef2006-03-22 00:08:56 -0800462
Adam Litkec79fb752007-11-14 16:59:38 -0800463 mapping = (struct address_space *) page_private(page);
Andy Whitcrofte5df70a2008-02-23 15:23:32 -0800464 set_page_private(page, 0);
Adam Litke7893d1d2007-10-16 01:26:18 -0700465 BUG_ON(page_count(page));
David Gibson27a85ef2006-03-22 00:08:56 -0800466 INIT_LIST_HEAD(&page->lru);
467
468 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700469 if (surplus_huge_pages_node[nid]) {
470 update_and_free_page(page);
471 surplus_huge_pages--;
472 surplus_huge_pages_node[nid]--;
473 } else {
474 enqueue_huge_page(page);
475 }
David Gibson27a85ef2006-03-22 00:08:56 -0800476 spin_unlock(&hugetlb_lock);
Adam Litkec79fb752007-11-14 16:59:38 -0800477 if (mapping)
Adam Litke9a119c02007-11-14 16:59:41 -0800478 hugetlb_put_quota(mapping, 1);
David Gibson27a85ef2006-03-22 00:08:56 -0800479}
480
Adam Litke7893d1d2007-10-16 01:26:18 -0700481/*
482 * Increment or decrement surplus_huge_pages. Keep node-specific counters
483 * balanced by operating on them in a round-robin fashion.
484 * Returns 1 if an adjustment was made.
485 */
486static int adjust_pool_surplus(int delta)
487{
488 static int prev_nid;
489 int nid = prev_nid;
490 int ret = 0;
491
492 VM_BUG_ON(delta != -1 && delta != 1);
493 do {
494 nid = next_node(nid, node_online_map);
495 if (nid == MAX_NUMNODES)
496 nid = first_node(node_online_map);
497
498 /* To shrink on this node, there must be a surplus page */
499 if (delta < 0 && !surplus_huge_pages_node[nid])
500 continue;
501 /* Surplus cannot exceed the total number of pages */
502 if (delta > 0 && surplus_huge_pages_node[nid] >=
503 nr_huge_pages_node[nid])
504 continue;
505
506 surplus_huge_pages += delta;
507 surplus_huge_pages_node[nid] += delta;
508 ret = 1;
509 break;
510 } while (nid != prev_nid);
511
512 prev_nid = nid;
513 return ret;
514}
515
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700516static void prep_new_huge_page(struct page *page, int nid)
517{
518 set_compound_page_dtor(page, free_huge_page);
519 spin_lock(&hugetlb_lock);
520 nr_huge_pages++;
521 nr_huge_pages_node[nid]++;
522 spin_unlock(&hugetlb_lock);
523 put_page(page); /* free it into the hugepage allocator */
524}
525
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700526static struct page *alloc_fresh_huge_page_node(int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct page *page;
Joe Jinf96efd52007-07-15 23:38:12 -0700529
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700530 page = alloc_pages_node(nid,
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700531 htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
532 __GFP_REPEAT|__GFP_NOWARN,
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700533 HUGETLB_PAGE_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (page) {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700535 if (arch_prepare_hugepage(page)) {
536 __free_pages(page, HUGETLB_PAGE_ORDER);
Harvey Harrison7b8ee842008-04-28 14:13:19 -0700537 return NULL;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700538 }
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700539 prep_new_huge_page(page, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700541
542 return page;
543}
544
545static int alloc_fresh_huge_page(void)
546{
547 struct page *page;
548 int start_nid;
549 int next_nid;
550 int ret = 0;
551
552 start_nid = hugetlb_next_nid;
553
554 do {
555 page = alloc_fresh_huge_page_node(hugetlb_next_nid);
556 if (page)
557 ret = 1;
558 /*
559 * Use a helper variable to find the next node and then
560 * copy it back to hugetlb_next_nid afterwards:
561 * otherwise there's a window in which a racer might
562 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
563 * But we don't need to use a spin_lock here: it really
564 * doesn't matter if occasionally a racer chooses the
565 * same nid as we do. Move nid forward in the mask even
566 * if we just successfully allocated a hugepage so that
567 * the next caller gets hugepages on the next node.
568 */
569 next_nid = next_node(hugetlb_next_nid, node_online_map);
570 if (next_nid == MAX_NUMNODES)
571 next_nid = first_node(node_online_map);
572 hugetlb_next_nid = next_nid;
573 } while (!page && hugetlb_next_nid != start_nid);
574
Adam Litke3b116302008-04-28 02:13:06 -0700575 if (ret)
576 count_vm_event(HTLB_BUDDY_PGALLOC);
577 else
578 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
579
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Adam Litke7893d1d2007-10-16 01:26:18 -0700583static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
584 unsigned long address)
585{
586 struct page *page;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800587 unsigned int nid;
Adam Litke7893d1d2007-10-16 01:26:18 -0700588
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800589 /*
590 * Assume we will successfully allocate the surplus page to
591 * prevent racing processes from causing the surplus to exceed
592 * overcommit
593 *
594 * This however introduces a different race, where a process B
595 * tries to grow the static hugepage pool while alloc_pages() is
596 * called by process A. B will only examine the per-node
597 * counters in determining if surplus huge pages can be
598 * converted to normal huge pages in adjust_pool_surplus(). A
599 * won't be able to increment the per-node counter, until the
600 * lock is dropped by B, but B doesn't drop hugetlb_lock until
601 * no more huge pages can be converted from surplus to normal
602 * state (and doesn't try to convert again). Thus, we have a
603 * case where a surplus huge page exists, the pool is grown, and
604 * the surplus huge page still exists after, even though it
605 * should just have been converted to a normal huge page. This
606 * does not leak memory, though, as the hugepage will be freed
607 * once it is out of use. It also does not allow the counters to
608 * go out of whack in adjust_pool_surplus() as we don't modify
609 * the node values until we've gotten the hugepage and only the
610 * per-node value is checked there.
611 */
612 spin_lock(&hugetlb_lock);
613 if (surplus_huge_pages >= nr_overcommit_huge_pages) {
614 spin_unlock(&hugetlb_lock);
615 return NULL;
616 } else {
617 nr_huge_pages++;
618 surplus_huge_pages++;
619 }
620 spin_unlock(&hugetlb_lock);
621
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700622 page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
623 __GFP_REPEAT|__GFP_NOWARN,
Adam Litke7893d1d2007-10-16 01:26:18 -0700624 HUGETLB_PAGE_ORDER);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800625
626 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700627 if (page) {
Adam Litke2668db92008-03-10 11:43:50 -0700628 /*
629 * This page is now managed by the hugetlb allocator and has
630 * no users -- drop the buddy allocator's reference.
631 */
632 put_page_testzero(page);
633 VM_BUG_ON(page_count(page));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800634 nid = page_to_nid(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700635 set_compound_page_dtor(page, free_huge_page);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800636 /*
637 * We incremented the global counters already
638 */
639 nr_huge_pages_node[nid]++;
640 surplus_huge_pages_node[nid]++;
Adam Litke3b116302008-04-28 02:13:06 -0700641 __count_vm_event(HTLB_BUDDY_PGALLOC);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800642 } else {
643 nr_huge_pages--;
644 surplus_huge_pages--;
Adam Litke3b116302008-04-28 02:13:06 -0700645 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Adam Litke7893d1d2007-10-16 01:26:18 -0700646 }
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800647 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700648
649 return page;
650}
651
Adam Litkee4e574b2007-10-16 01:26:19 -0700652/*
653 * Increase the hugetlb pool such that it can accomodate a reservation
654 * of size 'delta'.
655 */
656static int gather_surplus_pages(int delta)
657{
658 struct list_head surplus_list;
659 struct page *page, *tmp;
660 int ret, i;
661 int needed, allocated;
662
663 needed = (resv_huge_pages + delta) - free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800664 if (needed <= 0) {
665 resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700666 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800667 }
Adam Litkee4e574b2007-10-16 01:26:19 -0700668
669 allocated = 0;
670 INIT_LIST_HEAD(&surplus_list);
671
672 ret = -ENOMEM;
673retry:
674 spin_unlock(&hugetlb_lock);
675 for (i = 0; i < needed; i++) {
676 page = alloc_buddy_huge_page(NULL, 0);
677 if (!page) {
678 /*
679 * We were not able to allocate enough pages to
680 * satisfy the entire reservation so we free what
681 * we've allocated so far.
682 */
683 spin_lock(&hugetlb_lock);
684 needed = 0;
685 goto free;
686 }
687
688 list_add(&page->lru, &surplus_list);
689 }
690 allocated += needed;
691
692 /*
693 * After retaking hugetlb_lock, we need to recalculate 'needed'
694 * because either resv_huge_pages or free_huge_pages may have changed.
695 */
696 spin_lock(&hugetlb_lock);
697 needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
698 if (needed > 0)
699 goto retry;
700
701 /*
702 * The surplus_list now contains _at_least_ the number of extra pages
703 * needed to accomodate the reservation. Add the appropriate number
704 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -0800705 * allocator. Commit the entire reservation here to prevent another
706 * process from stealing the pages as they are added to the pool but
707 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -0700708 */
709 needed += allocated;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800710 resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700711 ret = 0;
712free:
Adam Litke19fc3f02008-04-28 02:12:20 -0700713 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -0700714 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -0700715 if ((--needed) < 0)
716 break;
Adam Litkee4e574b2007-10-16 01:26:19 -0700717 list_del(&page->lru);
Adam Litke19fc3f02008-04-28 02:12:20 -0700718 enqueue_huge_page(page);
719 }
720
721 /* Free unnecessary surplus pages to the buddy allocator */
722 if (!list_empty(&surplus_list)) {
723 spin_unlock(&hugetlb_lock);
724 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
725 list_del(&page->lru);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700726 /*
Adam Litke2668db92008-03-10 11:43:50 -0700727 * The page has a reference count of zero already, so
728 * call free_huge_page directly instead of using
729 * put_page. This must be done with hugetlb_lock
Adam Litkeaf767cb2007-10-16 01:26:25 -0700730 * unlocked which is safe because free_huge_page takes
731 * hugetlb_lock before deciding how to free the page.
732 */
Adam Litke2668db92008-03-10 11:43:50 -0700733 free_huge_page(page);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700734 }
Adam Litke19fc3f02008-04-28 02:12:20 -0700735 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -0700736 }
737
738 return ret;
739}
740
741/*
742 * When releasing a hugetlb pool reservation, any surplus pages that were
743 * allocated to satisfy the reservation must be explicitly freed if they were
744 * never used.
745 */
Adrian Bunk8cde0452007-11-14 16:59:43 -0800746static void return_unused_surplus_pages(unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -0700747{
748 static int nid = -1;
749 struct page *page;
750 unsigned long nr_pages;
751
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700752 /*
753 * We want to release as many surplus pages as possible, spread
754 * evenly across all nodes. Iterate across all nodes until we
755 * can no longer free unreserved surplus pages. This occurs when
756 * the nodes with surplus pages have no free pages.
757 */
758 unsigned long remaining_iterations = num_online_nodes();
759
Adam Litkeac09b3a2008-03-04 14:29:38 -0800760 /* Uncommit the reservation */
761 resv_huge_pages -= unused_resv_pages;
762
Adam Litkee4e574b2007-10-16 01:26:19 -0700763 nr_pages = min(unused_resv_pages, surplus_huge_pages);
764
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700765 while (remaining_iterations-- && nr_pages) {
Adam Litkee4e574b2007-10-16 01:26:19 -0700766 nid = next_node(nid, node_online_map);
767 if (nid == MAX_NUMNODES)
768 nid = first_node(node_online_map);
769
770 if (!surplus_huge_pages_node[nid])
771 continue;
772
773 if (!list_empty(&hugepage_freelists[nid])) {
774 page = list_entry(hugepage_freelists[nid].next,
775 struct page, lru);
776 list_del(&page->lru);
777 update_and_free_page(page);
778 free_huge_pages--;
779 free_huge_pages_node[nid]--;
780 surplus_huge_pages--;
781 surplus_huge_pages_node[nid]--;
782 nr_pages--;
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700783 remaining_iterations = num_online_nodes();
Adam Litkee4e574b2007-10-16 01:26:19 -0700784 }
785 }
786}
787
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700788/*
789 * Determine if the huge page at addr within the vma has an associated
790 * reservation. Where it does not we will need to logically increase
791 * reservation and actually increase quota before an allocation can occur.
792 * Where any new reservation would be required the reservation change is
793 * prepared, but not committed. Once the page has been quota'd allocated
794 * an instantiated the change should be committed via vma_commit_reservation.
795 * No action is required on failure.
796 */
797static int vma_needs_reservation(struct vm_area_struct *vma, unsigned long addr)
798{
799 struct address_space *mapping = vma->vm_file->f_mapping;
800 struct inode *inode = mapping->host;
801
802 if (vma->vm_flags & VM_SHARED) {
Johannes Weinera858f7b2008-07-23 21:27:33 -0700803 pgoff_t idx = vma_hugecache_offset(vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700804 return region_chg(&inode->i_mapping->private_list,
805 idx, idx + 1);
806
Andy Whitcroft84afd992008-07-23 21:27:32 -0700807 } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
808 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700809
Andy Whitcroft84afd992008-07-23 21:27:32 -0700810 } else {
811 int err;
Johannes Weinera858f7b2008-07-23 21:27:33 -0700812 pgoff_t idx = vma_hugecache_offset(vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700813 struct resv_map *reservations = vma_resv_map(vma);
814
815 err = region_chg(&reservations->regions, idx, idx + 1);
816 if (err < 0)
817 return err;
818 return 0;
819 }
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700820}
821static void vma_commit_reservation(struct vm_area_struct *vma,
822 unsigned long addr)
823{
824 struct address_space *mapping = vma->vm_file->f_mapping;
825 struct inode *inode = mapping->host;
826
827 if (vma->vm_flags & VM_SHARED) {
Johannes Weinera858f7b2008-07-23 21:27:33 -0700828 pgoff_t idx = vma_hugecache_offset(vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700829 region_add(&inode->i_mapping->private_list, idx, idx + 1);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700830
831 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Johannes Weinera858f7b2008-07-23 21:27:33 -0700832 pgoff_t idx = vma_hugecache_offset(vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700833 struct resv_map *reservations = vma_resv_map(vma);
834
835 /* Mark this page used in the map. */
836 region_add(&reservations->regions, idx, idx + 1);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700837 }
838}
839
David Gibson27a85ef2006-03-22 00:08:56 -0800840static struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700841 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Adam Litke348ea202007-11-14 16:59:37 -0800843 struct page *page;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800844 struct address_space *mapping = vma->vm_file->f_mapping;
Mel Gormana1e78772008-07-23 21:27:23 -0700845 struct inode *inode = mapping->host;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700846 unsigned int chg;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800847
Mel Gormana1e78772008-07-23 21:27:23 -0700848 /*
849 * Processes that did not create the mapping will have no reserves and
850 * will not have accounted against quota. Check that the quota can be
851 * made before satisfying the allocation
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700852 * MAP_NORESERVE mappings may also need pages and quota allocated
853 * if no reserve mapping overlaps.
Mel Gormana1e78772008-07-23 21:27:23 -0700854 */
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700855 chg = vma_needs_reservation(vma, addr);
856 if (chg < 0)
857 return ERR_PTR(chg);
858 if (chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700859 if (hugetlb_get_quota(inode->i_mapping, chg))
860 return ERR_PTR(-ENOSPC);
Mel Gormana1e78772008-07-23 21:27:23 -0700861
862 spin_lock(&hugetlb_lock);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700863 page = dequeue_huge_page_vma(vma, addr, avoid_reserve);
Mel Gormana1e78772008-07-23 21:27:23 -0700864 spin_unlock(&hugetlb_lock);
865
866 if (!page) {
867 page = alloc_buddy_huge_page(vma, addr);
868 if (!page) {
869 hugetlb_put_quota(inode->i_mapping, chg);
870 return ERR_PTR(-VM_FAULT_OOM);
871 }
872 }
873
874 set_page_refcounted(page);
875 set_page_private(page, (unsigned long) mapping);
876
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700877 vma_commit_reservation(vma, addr);
878
Adam Litke90d8b7e2007-11-14 16:59:42 -0800879 return page;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882static int __init hugetlb_init(void)
883{
884 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100886 if (HPAGE_SHIFT == 0)
887 return 0;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 for (i = 0; i < MAX_NUMNODES; ++i)
890 INIT_LIST_HEAD(&hugepage_freelists[i]);
891
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700892 hugetlb_next_nid = first_node(node_online_map);
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 for (i = 0; i < max_huge_pages; ++i) {
Nick Piggina4822892006-03-22 00:08:08 -0800895 if (!alloc_fresh_huge_page())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 max_huge_pages = free_huge_pages = nr_huge_pages = i;
899 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
900 return 0;
901}
902module_init(hugetlb_init);
903
904static int __init hugetlb_setup(char *s)
905{
906 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
907 max_huge_pages = 0;
908 return 1;
909}
910__setup("hugepages=", hugetlb_setup);
911
Ken Chen8a630112007-05-09 02:33:34 -0700912static unsigned int cpuset_mems_nr(unsigned int *array)
913{
914 int node;
915 unsigned int nr = 0;
916
917 for_each_node_mask(node, cpuset_current_mems_allowed)
918 nr += array[node];
919
920 return nr;
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924#ifdef CONFIG_HIGHMEM
925static void try_to_free_low(unsigned long count)
926{
Christoph Lameter4415cc82006-09-25 23:31:55 -0700927 int i;
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 for (i = 0; i < MAX_NUMNODES; ++i) {
930 struct page *page, *next;
931 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
Adam Litke6b0c8802007-10-16 01:26:23 -0700932 if (count >= nr_huge_pages)
933 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (PageHighMem(page))
935 continue;
936 list_del(&page->lru);
937 update_and_free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 free_huge_pages--;
Christoph Lameter4415cc82006-09-25 23:31:55 -0700939 free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941 }
942}
943#else
944static inline void try_to_free_low(unsigned long count)
945{
946}
947#endif
948
Adam Litke7893d1d2007-10-16 01:26:18 -0700949#define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static unsigned long set_max_huge_pages(unsigned long count)
951{
Adam Litke7893d1d2007-10-16 01:26:18 -0700952 unsigned long min_count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Adam Litke7893d1d2007-10-16 01:26:18 -0700954 /*
955 * Increase the pool size
956 * First take pages out of surplus state. Then make up the
957 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800958 *
959 * We might race with alloc_buddy_huge_page() here and be unable
960 * to convert a surplus huge page to a normal huge page. That is
961 * not critical, though, it just means the overall size of the
962 * pool might be one hugepage larger than it needs to be, but
963 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -0700964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700966 while (surplus_huge_pages && count > persistent_huge_pages) {
967 if (!adjust_pool_surplus(-1))
968 break;
969 }
970
971 while (count > persistent_huge_pages) {
Adam Litke7893d1d2007-10-16 01:26:18 -0700972 /*
973 * If this allocation races such that we no longer need the
974 * page, free_huge_page will handle it by freeing the page
975 * and reducing the surplus.
976 */
977 spin_unlock(&hugetlb_lock);
978 ret = alloc_fresh_huge_page();
979 spin_lock(&hugetlb_lock);
980 if (!ret)
981 goto out;
982
983 }
Adam Litke7893d1d2007-10-16 01:26:18 -0700984
985 /*
986 * Decrease the pool size
987 * First return free pages to the buddy allocator (being careful
988 * to keep enough around to satisfy reservations). Then place
989 * pages into surplus state as needed so the pool will shrink
990 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800991 *
992 * By placing pages into the surplus state independent of the
993 * overcommit value, we are allowing the surplus pool size to
994 * exceed overcommit. There are few sane options here. Since
995 * alloc_buddy_huge_page() is checking the global counter,
996 * though, we'll note that we're not allowed to exceed surplus
997 * and won't grow the pool anywhere else. Not until one of the
998 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -0700999 */
Adam Litke6b0c8802007-10-16 01:26:23 -07001000 min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
1001 min_count = max(count, min_count);
Adam Litke7893d1d2007-10-16 01:26:18 -07001002 try_to_free_low(min_count);
1003 while (min_count < persistent_huge_pages) {
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -08001004 struct page *page = dequeue_huge_page();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!page)
1006 break;
1007 update_and_free_page(page);
1008 }
Adam Litke7893d1d2007-10-16 01:26:18 -07001009 while (count < persistent_huge_pages) {
1010 if (!adjust_pool_surplus(1))
1011 break;
1012 }
1013out:
1014 ret = persistent_huge_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001016 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019int hugetlb_sysctl_handler(struct ctl_table *table, int write,
1020 struct file *file, void __user *buffer,
1021 size_t *length, loff_t *ppos)
1022{
1023 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
1024 max_huge_pages = set_max_huge_pages(max_huge_pages);
1025 return 0;
1026}
Mel Gorman396faf02007-07-17 04:03:13 -07001027
1028int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
1029 struct file *file, void __user *buffer,
1030 size_t *length, loff_t *ppos)
1031{
1032 proc_dointvec(table, write, file, buffer, length, ppos);
1033 if (hugepages_treat_as_movable)
1034 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
1035 else
1036 htlb_alloc_mask = GFP_HIGHUSER;
1037 return 0;
1038}
1039
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001040int hugetlb_overcommit_handler(struct ctl_table *table, int write,
1041 struct file *file, void __user *buffer,
1042 size_t *length, loff_t *ppos)
1043{
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001044 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Nishanth Aravamudan064d9ef2008-02-13 15:03:19 -08001045 spin_lock(&hugetlb_lock);
1046 nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001047 spin_unlock(&hugetlb_lock);
1048 return 0;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051#endif /* CONFIG_SYSCTL */
1052
1053int hugetlb_report_meminfo(char *buf)
1054{
1055 return sprintf(buf,
1056 "HugePages_Total: %5lu\n"
1057 "HugePages_Free: %5lu\n"
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001058 "HugePages_Rsvd: %5lu\n"
Adam Litke7893d1d2007-10-16 01:26:18 -07001059 "HugePages_Surp: %5lu\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 "Hugepagesize: %5lu kB\n",
1061 nr_huge_pages,
1062 free_huge_pages,
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001063 resv_huge_pages,
Adam Litke7893d1d2007-10-16 01:26:18 -07001064 surplus_huge_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 HPAGE_SIZE/1024);
1066}
1067
1068int hugetlb_report_node_meminfo(int nid, char *buf)
1069{
1070 return sprintf(buf,
1071 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07001072 "Node %d HugePages_Free: %5u\n"
1073 "Node %d HugePages_Surp: %5u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 nid, nr_huge_pages_node[nid],
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07001075 nid, free_huge_pages_node[nid],
1076 nid, surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
1080unsigned long hugetlb_total_pages(void)
1081{
1082 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
1083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001085static int hugetlb_acct_memory(long delta)
1086{
1087 int ret = -ENOMEM;
1088
1089 spin_lock(&hugetlb_lock);
1090 /*
1091 * When cpuset is configured, it breaks the strict hugetlb page
1092 * reservation as the accounting is done on a global variable. Such
1093 * reservation is completely rubbish in the presence of cpuset because
1094 * the reservation is not checked against page availability for the
1095 * current cpuset. Application can still potentially OOM'ed by kernel
1096 * with lack of free htlb page in cpuset that the task is in.
1097 * Attempt to enforce strict accounting with cpuset is almost
1098 * impossible (or too ugly) because cpuset is too fluid that
1099 * task or memory node can be dynamically moved between cpusets.
1100 *
1101 * The change of semantics for shared hugetlb mapping with cpuset is
1102 * undesirable. However, in order to preserve some of the semantics,
1103 * we fall back to check against current free page availability as
1104 * a best attempt and hopefully to minimize the impact of changing
1105 * semantics that cpuset has.
1106 */
1107 if (delta > 0) {
1108 if (gather_surplus_pages(delta) < 0)
1109 goto out;
1110
1111 if (delta > cpuset_mems_nr(free_huge_pages_node)) {
1112 return_unused_surplus_pages(delta);
1113 goto out;
1114 }
1115 }
1116
1117 ret = 0;
1118 if (delta < 0)
1119 return_unused_surplus_pages((unsigned long) -delta);
1120
1121out:
1122 spin_unlock(&hugetlb_lock);
1123 return ret;
1124}
1125
Andy Whitcroft84afd992008-07-23 21:27:32 -07001126static void hugetlb_vm_op_open(struct vm_area_struct *vma)
1127{
1128 struct resv_map *reservations = vma_resv_map(vma);
1129
1130 /*
1131 * This new VMA should share its siblings reservation map if present.
1132 * The VMA will only ever have a valid reservation map pointer where
1133 * it is being copied for another still existing VMA. As that VMA
1134 * has a reference to the reservation map it cannot dissappear until
1135 * after this open call completes. It is therefore safe to take a
1136 * new reference here without additional locking.
1137 */
1138 if (reservations)
1139 kref_get(&reservations->refs);
1140}
1141
Mel Gormana1e78772008-07-23 21:27:23 -07001142static void hugetlb_vm_op_close(struct vm_area_struct *vma)
1143{
Andy Whitcroft84afd992008-07-23 21:27:32 -07001144 struct resv_map *reservations = vma_resv_map(vma);
1145 unsigned long reserve;
1146 unsigned long start;
1147 unsigned long end;
1148
1149 if (reservations) {
Johannes Weinera858f7b2008-07-23 21:27:33 -07001150 start = vma_hugecache_offset(vma, vma->vm_start);
1151 end = vma_hugecache_offset(vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001152
1153 reserve = (end - start) -
1154 region_count(&reservations->regions, start, end);
1155
1156 kref_put(&reservations->refs, resv_map_release);
1157
1158 if (reserve)
1159 hugetlb_acct_memory(-reserve);
1160 }
Mel Gormana1e78772008-07-23 21:27:23 -07001161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
1164 * We cannot handle pagefaults against hugetlb pages at all. They cause
1165 * handle_mm_fault() to try to instantiate regular-sized pages in the
1166 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
1167 * this far.
1168 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001169static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07001176 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07001177 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07001178 .close = hugetlb_vm_op_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179};
1180
David Gibson1e8f8892006-01-06 00:10:44 -08001181static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
1182 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07001183{
1184 pte_t entry;
1185
David Gibson1e8f8892006-01-06 00:10:44 -08001186 if (writable) {
David Gibson63551ae2005-06-21 17:14:44 -07001187 entry =
1188 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1189 } else {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001190 entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07001191 }
1192 entry = pte_mkyoung(entry);
1193 entry = pte_mkhuge(entry);
1194
1195 return entry;
1196}
1197
David Gibson1e8f8892006-01-06 00:10:44 -08001198static void set_huge_ptep_writable(struct vm_area_struct *vma,
1199 unsigned long address, pte_t *ptep)
1200{
1201 pte_t entry;
1202
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001203 entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
1204 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001205 update_mmu_cache(vma, address, entry);
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001206 }
David Gibson1e8f8892006-01-06 00:10:44 -08001207}
1208
1209
David Gibson63551ae2005-06-21 17:14:44 -07001210int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
1211 struct vm_area_struct *vma)
1212{
1213 pte_t *src_pte, *dst_pte, entry;
1214 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07001215 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08001216 int cow;
1217
1218 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07001219
Hugh Dickins1c598272005-10-19 21:23:43 -07001220 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
Hugh Dickinsc74df322005-10-29 18:16:23 -07001221 src_pte = huge_pte_offset(src, addr);
1222 if (!src_pte)
1223 continue;
David Gibson63551ae2005-06-21 17:14:44 -07001224 dst_pte = huge_pte_alloc(dst, addr);
1225 if (!dst_pte)
1226 goto nomem;
Larry Woodmanc5c99422008-01-24 05:49:25 -08001227
1228 /* If the pagetables are shared don't copy or take references */
1229 if (dst_pte == src_pte)
1230 continue;
1231
Hugh Dickinsc74df322005-10-29 18:16:23 -07001232 spin_lock(&dst->page_table_lock);
Nick Piggin46478752008-06-05 22:45:57 -07001233 spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001234 if (!huge_pte_none(huge_ptep_get(src_pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001235 if (cow)
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001236 huge_ptep_set_wrprotect(src, addr, src_pte);
1237 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07001238 ptepage = pte_page(entry);
1239 get_page(ptepage);
Hugh Dickins1c598272005-10-19 21:23:43 -07001240 set_huge_pte_at(dst, addr, dst_pte, entry);
1241 }
1242 spin_unlock(&src->page_table_lock);
Hugh Dickinsc74df322005-10-29 18:16:23 -07001243 spin_unlock(&dst->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001244 }
1245 return 0;
1246
1247nomem:
1248 return -ENOMEM;
1249}
1250
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001251void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001252 unsigned long end, struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07001253{
1254 struct mm_struct *mm = vma->vm_mm;
1255 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07001256 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07001257 pte_t pte;
1258 struct page *page;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001259 struct page *tmp;
Chen, Kenneth Wc0a499c2006-12-06 20:31:39 -08001260 /*
1261 * A page gathering list, protected by per file i_mmap_lock. The
1262 * lock is used to avoid list corruption from multiple unmapping
1263 * of the same page since we are using page->lru.
1264 */
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001265 LIST_HEAD(page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001266
1267 WARN_ON(!is_vm_hugetlb_page(vma));
1268 BUG_ON(start & ~HPAGE_MASK);
1269 BUG_ON(end & ~HPAGE_MASK);
1270
Hugh Dickins508034a2005-10-29 18:16:30 -07001271 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001272 for (address = start; address < end; address += HPAGE_SIZE) {
David Gibsonc7546f82005-08-05 11:59:35 -07001273 ptep = huge_pte_offset(mm, address);
Adam Litke4c887262005-10-29 18:16:46 -07001274 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07001275 continue;
1276
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001277 if (huge_pmd_unshare(mm, &address, ptep))
1278 continue;
1279
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001280 /*
1281 * If a reference page is supplied, it is because a specific
1282 * page is being unmapped, not a range. Ensure the page we
1283 * are about to unmap is the actual page of interest.
1284 */
1285 if (ref_page) {
1286 pte = huge_ptep_get(ptep);
1287 if (huge_pte_none(pte))
1288 continue;
1289 page = pte_page(pte);
1290 if (page != ref_page)
1291 continue;
1292
1293 /*
1294 * Mark the VMA as having unmapped its page so that
1295 * future faults in this VMA will fail rather than
1296 * looking like data was lost
1297 */
1298 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
1299 }
1300
David Gibsonc7546f82005-08-05 11:59:35 -07001301 pte = huge_ptep_get_and_clear(mm, address, ptep);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001302 if (huge_pte_none(pte))
David Gibson63551ae2005-06-21 17:14:44 -07001303 continue;
David Gibsonc7546f82005-08-05 11:59:35 -07001304
David Gibson63551ae2005-06-21 17:14:44 -07001305 page = pte_page(pte);
Ken Chen6649a382007-02-08 14:20:27 -08001306 if (pte_dirty(pte))
1307 set_page_dirty(page);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001308 list_add(&page->lru, &page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 spin_unlock(&mm->page_table_lock);
Hugh Dickins508034a2005-10-29 18:16:30 -07001311 flush_tlb_range(vma, start, end);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001312 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1313 list_del(&page->lru);
1314 put_page(page);
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
David Gibson63551ae2005-06-21 17:14:44 -07001317
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001318void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001319 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001320{
1321 /*
1322 * It is undesirable to test vma->vm_file as it should be non-null
1323 * for valid hugetlb area. However, vm_file will be NULL in the error
1324 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
1325 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
1326 * to clean up. Since no pte has actually been setup, it is safe to
1327 * do nothing in this case.
1328 */
1329 if (vma->vm_file) {
1330 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001331 __unmap_hugepage_range(vma, start, end, ref_page);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001332 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
1333 }
1334}
1335
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001336/*
1337 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1338 * mappping it owns the reserve page for. The intention is to unmap the page
1339 * from other VMAs and let the children be SIGKILLed if they are faulting the
1340 * same region.
1341 */
1342int unmap_ref_private(struct mm_struct *mm,
1343 struct vm_area_struct *vma,
1344 struct page *page,
1345 unsigned long address)
1346{
1347 struct vm_area_struct *iter_vma;
1348 struct address_space *mapping;
1349 struct prio_tree_iter iter;
1350 pgoff_t pgoff;
1351
1352 /*
1353 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1354 * from page cache lookup which is in HPAGE_SIZE units.
1355 */
1356 address = address & huge_page_mask(hstate_vma(vma));
1357 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
1358 + (vma->vm_pgoff >> PAGE_SHIFT);
1359 mapping = (struct address_space *)page_private(page);
1360
1361 vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1362 /* Do not unmap the current VMA */
1363 if (iter_vma == vma)
1364 continue;
1365
1366 /*
1367 * Unmap the page from other VMAs without their own reserves.
1368 * They get marked to be SIGKILLed if they fault in these
1369 * areas. This is because a future no-page fault on this VMA
1370 * could insert a zeroed page instead of the data existing
1371 * from the time of fork. This would look like data corruption
1372 */
1373 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
1374 unmap_hugepage_range(iter_vma,
1375 address, address + HPAGE_SIZE,
1376 page);
1377 }
1378
1379 return 1;
1380}
1381
David Gibson1e8f8892006-01-06 00:10:44 -08001382static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001383 unsigned long address, pte_t *ptep, pte_t pte,
1384 struct page *pagecache_page)
David Gibson1e8f8892006-01-06 00:10:44 -08001385{
1386 struct page *old_page, *new_page;
David Gibson79ac6ba2006-03-22 00:08:51 -08001387 int avoidcopy;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001388 int outside_reserve = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001389
1390 old_page = pte_page(pte);
1391
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001392retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08001393 /* If no-one else is actually using this page, avoid the copy
1394 * and just make the page writable */
1395 avoidcopy = (page_count(old_page) == 1);
1396 if (avoidcopy) {
1397 set_huge_ptep_writable(vma, address, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07001398 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001399 }
1400
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001401 /*
1402 * If the process that created a MAP_PRIVATE mapping is about to
1403 * perform a COW due to a shared page count, attempt to satisfy
1404 * the allocation without using the existing reserves. The pagecache
1405 * page is used to determine if the reserve at this address was
1406 * consumed or not. If reserves were used, a partial faulted mapping
1407 * at the time of fork() could consume its reserves on COW instead
1408 * of the full address range.
1409 */
1410 if (!(vma->vm_flags & VM_SHARED) &&
1411 is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
1412 old_page != pagecache_page)
1413 outside_reserve = 1;
1414
David Gibson1e8f8892006-01-06 00:10:44 -08001415 page_cache_get(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001416 new_page = alloc_huge_page(vma, address, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08001417
Adam Litke2fc39ce2007-11-14 16:59:39 -08001418 if (IS_ERR(new_page)) {
David Gibson1e8f8892006-01-06 00:10:44 -08001419 page_cache_release(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001420
1421 /*
1422 * If a process owning a MAP_PRIVATE mapping fails to COW,
1423 * it is due to references held by a child and an insufficient
1424 * huge page pool. To guarantee the original mappers
1425 * reliability, unmap the page from child processes. The child
1426 * may get SIGKILLed if it later faults.
1427 */
1428 if (outside_reserve) {
1429 BUG_ON(huge_pte_none(pte));
1430 if (unmap_ref_private(mm, vma, old_page, address)) {
1431 BUG_ON(page_count(old_page) != 1);
1432 BUG_ON(huge_pte_none(pte));
1433 goto retry_avoidcopy;
1434 }
1435 WARN_ON_ONCE(1);
1436 }
1437
Adam Litke2fc39ce2007-11-14 16:59:39 -08001438 return -PTR_ERR(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001439 }
1440
1441 spin_unlock(&mm->page_table_lock);
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00001442 copy_huge_page(new_page, old_page, address, vma);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001443 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001444 spin_lock(&mm->page_table_lock);
1445
1446 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001447 if (likely(pte_same(huge_ptep_get(ptep), pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001448 /* Break COW */
Gerald Schaefer8fe627e2008-04-28 02:13:28 -07001449 huge_ptep_clear_flush(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08001450 set_huge_pte_at(mm, address, ptep,
1451 make_huge_pte(vma, new_page, 1));
1452 /* Make the old page be freed below */
1453 new_page = old_page;
1454 }
1455 page_cache_release(new_page);
1456 page_cache_release(old_page);
Nick Piggin83c54072007-07-19 01:47:05 -07001457 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001458}
1459
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001460/* Return the pagecache page at a given address within a VMA */
1461static struct page *hugetlbfs_pagecache_page(struct vm_area_struct *vma,
1462 unsigned long address)
1463{
1464 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001465 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001466
1467 mapping = vma->vm_file->f_mapping;
Johannes Weinera858f7b2008-07-23 21:27:33 -07001468 idx = vma_hugecache_offset(vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001469
1470 return find_lock_page(mapping, idx);
1471}
1472
Robert P. J. Daya1ed3dd2007-07-17 04:03:33 -07001473static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
David Gibson1e8f8892006-01-06 00:10:44 -08001474 unsigned long address, pte_t *ptep, int write_access)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001475{
1476 int ret = VM_FAULT_SIGBUS;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001477 pgoff_t idx;
Adam Litke4c887262005-10-29 18:16:46 -07001478 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07001479 struct page *page;
1480 struct address_space *mapping;
David Gibson1e8f8892006-01-06 00:10:44 -08001481 pte_t new_pte;
Adam Litke4c887262005-10-29 18:16:46 -07001482
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001483 /*
1484 * Currently, we are forced to kill the process in the event the
1485 * original mapper has unmapped pages from the child due to a failed
1486 * COW. Warn that such a situation has occured as it may not be obvious
1487 */
1488 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
1489 printk(KERN_WARNING
1490 "PID %d killed due to inadequate hugepage pool\n",
1491 current->pid);
1492 return ret;
1493 }
1494
Adam Litke4c887262005-10-29 18:16:46 -07001495 mapping = vma->vm_file->f_mapping;
Johannes Weinera858f7b2008-07-23 21:27:33 -07001496 idx = vma_hugecache_offset(vma, address);
Adam Litke4c887262005-10-29 18:16:46 -07001497
1498 /*
1499 * Use page lock to guard against racing truncation
1500 * before we get page_table_lock.
1501 */
Christoph Lameter6bda6662006-01-06 00:10:49 -08001502retry:
1503 page = find_lock_page(mapping, idx);
1504 if (!page) {
Hugh Dickinsebed4bf2006-10-28 10:38:43 -07001505 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
1506 if (idx >= size)
1507 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001508 page = alloc_huge_page(vma, address, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08001509 if (IS_ERR(page)) {
1510 ret = -PTR_ERR(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001511 goto out;
1512 }
David Gibson79ac6ba2006-03-22 00:08:51 -08001513 clear_huge_page(page, address);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001514 __SetPageUptodate(page);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001515
Christoph Lameter6bda6662006-01-06 00:10:49 -08001516 if (vma->vm_flags & VM_SHARED) {
1517 int err;
Ken Chen45c682a2007-11-14 16:59:44 -08001518 struct inode *inode = mapping->host;
Christoph Lameter6bda6662006-01-06 00:10:49 -08001519
1520 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
1521 if (err) {
1522 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001523 if (err == -EEXIST)
1524 goto retry;
1525 goto out;
1526 }
Ken Chen45c682a2007-11-14 16:59:44 -08001527
1528 spin_lock(&inode->i_lock);
1529 inode->i_blocks += BLOCKS_PER_HUGEPAGE;
1530 spin_unlock(&inode->i_lock);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001531 } else
1532 lock_page(page);
1533 }
David Gibson1e8f8892006-01-06 00:10:44 -08001534
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001535 spin_lock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001536 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
1537 if (idx >= size)
1538 goto backout;
1539
Nick Piggin83c54072007-07-19 01:47:05 -07001540 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001541 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07001542 goto backout;
1543
David Gibson1e8f8892006-01-06 00:10:44 -08001544 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
1545 && (vma->vm_flags & VM_SHARED)));
1546 set_huge_pte_at(mm, address, ptep, new_pte);
1547
1548 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1549 /* Optimization, do the COW without a second fault */
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001550 ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
David Gibson1e8f8892006-01-06 00:10:44 -08001551 }
1552
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001553 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001554 unlock_page(page);
1555out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001556 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07001557
1558backout:
1559 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001560 unlock_page(page);
1561 put_page(page);
1562 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001563}
1564
Adam Litke86e52162006-01-06 00:10:43 -08001565int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1566 unsigned long address, int write_access)
1567{
1568 pte_t *ptep;
1569 pte_t entry;
David Gibson1e8f8892006-01-06 00:10:44 -08001570 int ret;
David Gibson3935baa2006-03-22 00:08:53 -08001571 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
Adam Litke86e52162006-01-06 00:10:43 -08001572
1573 ptep = huge_pte_alloc(mm, address);
1574 if (!ptep)
1575 return VM_FAULT_OOM;
1576
David Gibson3935baa2006-03-22 00:08:53 -08001577 /*
1578 * Serialize hugepage allocation and instantiation, so that we don't
1579 * get spurious allocation failures if two CPUs race to instantiate
1580 * the same page in the page cache.
1581 */
1582 mutex_lock(&hugetlb_instantiation_mutex);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001583 entry = huge_ptep_get(ptep);
1584 if (huge_pte_none(entry)) {
David Gibson3935baa2006-03-22 00:08:53 -08001585 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
1586 mutex_unlock(&hugetlb_instantiation_mutex);
1587 return ret;
1588 }
Adam Litke86e52162006-01-06 00:10:43 -08001589
Nick Piggin83c54072007-07-19 01:47:05 -07001590 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001591
1592 spin_lock(&mm->page_table_lock);
1593 /* Check for a racing update before calling hugetlb_cow */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001594 if (likely(pte_same(entry, huge_ptep_get(ptep))))
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001595 if (write_access && !pte_write(entry)) {
1596 struct page *page;
1597 page = hugetlbfs_pagecache_page(vma, address);
1598 ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
1599 if (page) {
1600 unlock_page(page);
1601 put_page(page);
1602 }
1603 }
David Gibson1e8f8892006-01-06 00:10:44 -08001604 spin_unlock(&mm->page_table_lock);
David Gibson3935baa2006-03-22 00:08:53 -08001605 mutex_unlock(&hugetlb_instantiation_mutex);
David Gibson1e8f8892006-01-06 00:10:44 -08001606
1607 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08001608}
1609
David Gibson63551ae2005-06-21 17:14:44 -07001610int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
1611 struct page **pages, struct vm_area_struct **vmas,
Adam Litke5b23dbe2007-11-14 16:59:33 -08001612 unsigned long *position, int *length, int i,
1613 int write)
David Gibson63551ae2005-06-21 17:14:44 -07001614{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001615 unsigned long pfn_offset;
1616 unsigned long vaddr = *position;
David Gibson63551ae2005-06-21 17:14:44 -07001617 int remainder = *length;
1618
Hugh Dickins1c598272005-10-19 21:23:43 -07001619 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001620 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07001621 pte_t *pte;
1622 struct page *page;
1623
1624 /*
1625 * Some archs (sparc64, sh*) have multiple pte_ts to
1626 * each hugepage. We have to make * sure we get the
1627 * first, for the page indexing below to work.
1628 */
1629 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
1630
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001631 if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
1632 (write && !pte_write(huge_ptep_get(pte)))) {
Adam Litke4c887262005-10-29 18:16:46 -07001633 int ret;
1634
1635 spin_unlock(&mm->page_table_lock);
Adam Litke5b23dbe2007-11-14 16:59:33 -08001636 ret = hugetlb_fault(mm, vma, vaddr, write);
Adam Litke4c887262005-10-29 18:16:46 -07001637 spin_lock(&mm->page_table_lock);
Adam Litkea89182c2007-08-22 14:01:51 -07001638 if (!(ret & VM_FAULT_ERROR))
Adam Litke4c887262005-10-29 18:16:46 -07001639 continue;
1640
1641 remainder = 0;
1642 if (!i)
1643 i = -EFAULT;
1644 break;
1645 }
David Gibson63551ae2005-06-21 17:14:44 -07001646
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001647 pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001648 page = pte_page(huge_ptep_get(pte));
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001649same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001650 if (pages) {
1651 get_page(page);
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001652 pages[i] = page + pfn_offset;
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001653 }
David Gibson63551ae2005-06-21 17:14:44 -07001654
1655 if (vmas)
1656 vmas[i] = vma;
1657
1658 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001659 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07001660 --remainder;
1661 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001662 if (vaddr < vma->vm_end && remainder &&
1663 pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
1664 /*
1665 * We use pfn_offset to avoid touching the pageframes
1666 * of this compound page.
1667 */
1668 goto same_page;
1669 }
David Gibson63551ae2005-06-21 17:14:44 -07001670 }
Hugh Dickins1c598272005-10-19 21:23:43 -07001671 spin_unlock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001672 *length = remainder;
1673 *position = vaddr;
1674
1675 return i;
1676}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001677
1678void hugetlb_change_protection(struct vm_area_struct *vma,
1679 unsigned long address, unsigned long end, pgprot_t newprot)
1680{
1681 struct mm_struct *mm = vma->vm_mm;
1682 unsigned long start = address;
1683 pte_t *ptep;
1684 pte_t pte;
1685
1686 BUG_ON(address >= end);
1687 flush_cache_range(vma, address, end);
1688
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001689 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001690 spin_lock(&mm->page_table_lock);
1691 for (; address < end; address += HPAGE_SIZE) {
1692 ptep = huge_pte_offset(mm, address);
1693 if (!ptep)
1694 continue;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001695 if (huge_pmd_unshare(mm, &address, ptep))
1696 continue;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001697 if (!huge_pte_none(huge_ptep_get(ptep))) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001698 pte = huge_ptep_get_and_clear(mm, address, ptep);
1699 pte = pte_mkhuge(pte_modify(pte, newprot));
1700 set_huge_pte_at(mm, address, ptep, pte);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001701 }
1702 }
1703 spin_unlock(&mm->page_table_lock);
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001704 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001705
1706 flush_tlb_range(vma, start, end);
1707}
1708
Mel Gormana1e78772008-07-23 21:27:23 -07001709int hugetlb_reserve_pages(struct inode *inode,
1710 long from, long to,
1711 struct vm_area_struct *vma)
Adam Litkee4e574b2007-10-16 01:26:19 -07001712{
1713 long ret, chg;
1714
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001715 if (vma && vma->vm_flags & VM_NORESERVE)
1716 return 0;
1717
Mel Gormana1e78772008-07-23 21:27:23 -07001718 /*
1719 * Shared mappings base their reservation on the number of pages that
1720 * are already allocated on behalf of the file. Private mappings need
1721 * to reserve the full area even if read-only as mprotect() may be
1722 * called to make the mapping read-write. Assume !vma is a shm mapping
1723 */
1724 if (!vma || vma->vm_flags & VM_SHARED)
1725 chg = region_chg(&inode->i_mapping->private_list, from, to);
1726 else {
Andy Whitcroft84afd992008-07-23 21:27:32 -07001727 struct resv_map *resv_map = resv_map_alloc();
1728 if (!resv_map)
1729 return -ENOMEM;
1730
Mel Gormana1e78772008-07-23 21:27:23 -07001731 chg = to - from;
Andy Whitcroft84afd992008-07-23 21:27:32 -07001732
1733 set_vma_resv_map(vma, resv_map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001734 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
Mel Gormana1e78772008-07-23 21:27:23 -07001735 }
1736
Adam Litkee4e574b2007-10-16 01:26:19 -07001737 if (chg < 0)
1738 return chg;
Ken Chen8a630112007-05-09 02:33:34 -07001739
Adam Litke90d8b7e2007-11-14 16:59:42 -08001740 if (hugetlb_get_quota(inode->i_mapping, chg))
1741 return -ENOSPC;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001742 ret = hugetlb_acct_memory(chg);
Ken Chen68842c92008-01-14 00:55:19 -08001743 if (ret < 0) {
1744 hugetlb_put_quota(inode->i_mapping, chg);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001745 return ret;
Ken Chen68842c92008-01-14 00:55:19 -08001746 }
Mel Gormana1e78772008-07-23 21:27:23 -07001747 if (!vma || vma->vm_flags & VM_SHARED)
1748 region_add(&inode->i_mapping->private_list, from, to);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001749 return 0;
1750}
1751
1752void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1753{
1754 long chg = region_truncate(&inode->i_mapping->private_list, offset);
Ken Chen45c682a2007-11-14 16:59:44 -08001755
1756 spin_lock(&inode->i_lock);
1757 inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
1758 spin_unlock(&inode->i_lock);
1759
Adam Litke90d8b7e2007-11-14 16:59:42 -08001760 hugetlb_put_quota(inode->i_mapping, (chg - freed));
1761 hugetlb_acct_memory(-(chg - freed));
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001762}