blob: 3e873f0101fbaac7234ef67dfa56a3f42000939d [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/*
44 * Convert the address within this vma to the page offset within
45 * the mapping, in base page units.
46 */
47static pgoff_t vma_page_offset(struct vm_area_struct *vma,
48 unsigned long address)
49{
50 return ((address - vma->vm_start) >> PAGE_SHIFT) +
51 (vma->vm_pgoff >> PAGE_SHIFT);
52}
53
54/*
55 * Convert the address within this vma to the page offset within
56 * the mapping, in pagecache page units; huge pages here.
57 */
58static pgoff_t vma_pagecache_offset(struct vm_area_struct *vma,
59 unsigned long address)
60{
61 return ((address - vma->vm_start) >> HPAGE_SHIFT) +
62 (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
63}
64
Mel Gorman04f2cbe2008-07-23 21:27:25 -070065#define HPAGE_RESV_OWNER (1UL << (BITS_PER_LONG - 1))
66#define HPAGE_RESV_UNMAPPED (1UL << (BITS_PER_LONG - 2))
67#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Mel Gormana1e78772008-07-23 21:27:23 -070068/*
69 * These helpers are used to track how many pages are reserved for
70 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
71 * is guaranteed to have their future faults succeed.
72 *
73 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
74 * the reserve counters are updated with the hugetlb_lock held. It is safe
75 * to reset the VMA at fork() time as it is not in use yet and there is no
76 * chance of the global counters getting corrupted as a result of the values.
77 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -070078static unsigned long get_vma_private_data(struct vm_area_struct *vma)
79{
80 return (unsigned long)vma->vm_private_data;
81}
82
83static void set_vma_private_data(struct vm_area_struct *vma,
84 unsigned long value)
85{
86 vma->vm_private_data = (void *)value;
87}
88
Mel Gormana1e78772008-07-23 21:27:23 -070089static unsigned long vma_resv_huge_pages(struct vm_area_struct *vma)
90{
91 VM_BUG_ON(!is_vm_hugetlb_page(vma));
92 if (!(vma->vm_flags & VM_SHARED))
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -070093 return get_vma_private_data(vma) & ~HPAGE_RESV_MASK;
Mel Gormana1e78772008-07-23 21:27:23 -070094 return 0;
95}
96
97static void set_vma_resv_huge_pages(struct vm_area_struct *vma,
98 unsigned long reserve)
99{
100 VM_BUG_ON(!is_vm_hugetlb_page(vma));
101 VM_BUG_ON(vma->vm_flags & VM_SHARED);
102
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700103 set_vma_private_data(vma,
104 (get_vma_private_data(vma) & HPAGE_RESV_MASK) | reserve);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700105}
106
107static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
108{
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700109 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700110 VM_BUG_ON(vma->vm_flags & VM_SHARED);
111
112 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700113}
114
115static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
116{
117 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700118
119 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700120}
121
122/* Decrement the reserved pages in the hugepage pool by one */
123static void decrement_hugepage_resv_vma(struct vm_area_struct *vma)
124{
125 if (vma->vm_flags & VM_SHARED) {
126 /* Shared mappings always use reserves */
127 resv_huge_pages--;
128 } else {
129 /*
130 * Only the process that called mmap() has reserves for
131 * private mappings.
132 */
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700133 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
134 unsigned long flags, reserve;
Mel Gormana1e78772008-07-23 21:27:23 -0700135 resv_huge_pages--;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700136 flags = (unsigned long)vma->vm_private_data &
137 HPAGE_RESV_MASK;
Mel Gormana1e78772008-07-23 21:27:23 -0700138 reserve = (unsigned long)vma->vm_private_data - 1;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700139 vma->vm_private_data = (void *)(reserve | flags);
Mel Gormana1e78772008-07-23 21:27:23 -0700140 }
141 }
142}
143
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700144/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700145void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
146{
147 VM_BUG_ON(!is_vm_hugetlb_page(vma));
148 if (!(vma->vm_flags & VM_SHARED))
149 vma->vm_private_data = (void *)0;
150}
151
152/* Returns true if the VMA has associated reserve pages */
153static int vma_has_private_reserves(struct vm_area_struct *vma)
154{
155 if (vma->vm_flags & VM_SHARED)
156 return 0;
157 if (!vma_resv_huge_pages(vma))
158 return 0;
159 return 1;
160}
161
David Gibson79ac6ba2006-03-22 00:08:51 -0800162static void clear_huge_page(struct page *page, unsigned long addr)
163{
164 int i;
165
166 might_sleep();
167 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
168 cond_resched();
Ralf Baechle281e0e32007-10-01 01:20:10 -0700169 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
David Gibson79ac6ba2006-03-22 00:08:51 -0800170 }
171}
172
173static void copy_huge_page(struct page *dst, struct page *src,
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000174 unsigned long addr, struct vm_area_struct *vma)
David Gibson79ac6ba2006-03-22 00:08:51 -0800175{
176 int i;
177
178 might_sleep();
179 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
180 cond_resched();
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000181 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800182 }
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static void enqueue_huge_page(struct page *page)
186{
187 int nid = page_to_nid(page);
188 list_add(&page->lru, &hugepage_freelists[nid]);
189 free_huge_pages++;
190 free_huge_pages_node[nid]++;
191}
192
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800193static struct page *dequeue_huge_page(void)
194{
195 int nid;
196 struct page *page = NULL;
197
198 for (nid = 0; nid < MAX_NUMNODES; ++nid) {
199 if (!list_empty(&hugepage_freelists[nid])) {
200 page = list_entry(hugepage_freelists[nid].next,
201 struct page, lru);
202 list_del(&page->lru);
203 free_huge_pages--;
204 free_huge_pages_node[nid]--;
205 break;
206 }
207 }
208 return page;
209}
210
211static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700212 unsigned long address, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Nishanth Aravamudan31a5c6e2007-07-15 23:38:02 -0700214 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct page *page = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700216 struct mempolicy *mpol;
Mel Gorman19770b32008-04-28 02:12:18 -0700217 nodemask_t *nodemask;
Mel Gorman396faf02007-07-17 04:03:13 -0700218 struct zonelist *zonelist = huge_zonelist(vma, address,
Mel Gorman19770b32008-04-28 02:12:18 -0700219 htlb_alloc_mask, &mpol, &nodemask);
Mel Gormandd1a2392008-04-28 02:12:17 -0700220 struct zone *zone;
221 struct zoneref *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Mel Gormana1e78772008-07-23 21:27:23 -0700223 /*
224 * A child process with MAP_PRIVATE mappings created by their parent
225 * have no page reserves. This check ensures that reservations are
226 * not "stolen". The child may still get SIGKILLed
227 */
228 if (!vma_has_private_reserves(vma) &&
229 free_huge_pages - resv_huge_pages == 0)
230 return NULL;
231
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700232 /* If reserves cannot be used, ensure enough pages are in the pool */
233 if (avoid_reserve && free_huge_pages - resv_huge_pages == 0)
234 return NULL;
235
Mel Gorman19770b32008-04-28 02:12:18 -0700236 for_each_zone_zonelist_nodemask(zone, z, zonelist,
237 MAX_NR_ZONES - 1, nodemask) {
Mel Gorman54a6eb52008-04-28 02:12:16 -0700238 nid = zone_to_nid(zone);
239 if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
Andrew Morton3abf7af2007-07-19 01:49:08 -0700240 !list_empty(&hugepage_freelists[nid])) {
241 page = list_entry(hugepage_freelists[nid].next,
242 struct page, lru);
243 list_del(&page->lru);
244 free_huge_pages--;
245 free_huge_pages_node[nid]--;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700246
247 if (!avoid_reserve)
248 decrement_hugepage_resv_vma(vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700249
Ken Chen5ab3ee72007-07-23 18:44:00 -0700250 break;
Andrew Morton3abf7af2007-07-19 01:49:08 -0700251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700253 mpol_cond_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return page;
255}
256
Adam Litke6af2acb2007-10-16 01:26:16 -0700257static void update_and_free_page(struct page *page)
258{
259 int i;
260 nr_huge_pages--;
261 nr_huge_pages_node[page_to_nid(page)]--;
262 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
263 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
264 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
265 1 << PG_private | 1<< PG_writeback);
266 }
267 set_compound_page_dtor(page, NULL);
268 set_page_refcounted(page);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700269 arch_release_hugepage(page);
Adam Litke6af2acb2007-10-16 01:26:16 -0700270 __free_pages(page, HUGETLB_PAGE_ORDER);
271}
272
David Gibson27a85ef2006-03-22 00:08:56 -0800273static void free_huge_page(struct page *page)
274{
Adam Litke7893d1d2007-10-16 01:26:18 -0700275 int nid = page_to_nid(page);
Adam Litkec79fb752007-11-14 16:59:38 -0800276 struct address_space *mapping;
David Gibson27a85ef2006-03-22 00:08:56 -0800277
Adam Litkec79fb752007-11-14 16:59:38 -0800278 mapping = (struct address_space *) page_private(page);
Andy Whitcrofte5df70a2008-02-23 15:23:32 -0800279 set_page_private(page, 0);
Adam Litke7893d1d2007-10-16 01:26:18 -0700280 BUG_ON(page_count(page));
David Gibson27a85ef2006-03-22 00:08:56 -0800281 INIT_LIST_HEAD(&page->lru);
282
283 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700284 if (surplus_huge_pages_node[nid]) {
285 update_and_free_page(page);
286 surplus_huge_pages--;
287 surplus_huge_pages_node[nid]--;
288 } else {
289 enqueue_huge_page(page);
290 }
David Gibson27a85ef2006-03-22 00:08:56 -0800291 spin_unlock(&hugetlb_lock);
Adam Litkec79fb752007-11-14 16:59:38 -0800292 if (mapping)
Adam Litke9a119c02007-11-14 16:59:41 -0800293 hugetlb_put_quota(mapping, 1);
David Gibson27a85ef2006-03-22 00:08:56 -0800294}
295
Adam Litke7893d1d2007-10-16 01:26:18 -0700296/*
297 * Increment or decrement surplus_huge_pages. Keep node-specific counters
298 * balanced by operating on them in a round-robin fashion.
299 * Returns 1 if an adjustment was made.
300 */
301static int adjust_pool_surplus(int delta)
302{
303 static int prev_nid;
304 int nid = prev_nid;
305 int ret = 0;
306
307 VM_BUG_ON(delta != -1 && delta != 1);
308 do {
309 nid = next_node(nid, node_online_map);
310 if (nid == MAX_NUMNODES)
311 nid = first_node(node_online_map);
312
313 /* To shrink on this node, there must be a surplus page */
314 if (delta < 0 && !surplus_huge_pages_node[nid])
315 continue;
316 /* Surplus cannot exceed the total number of pages */
317 if (delta > 0 && surplus_huge_pages_node[nid] >=
318 nr_huge_pages_node[nid])
319 continue;
320
321 surplus_huge_pages += delta;
322 surplus_huge_pages_node[nid] += delta;
323 ret = 1;
324 break;
325 } while (nid != prev_nid);
326
327 prev_nid = nid;
328 return ret;
329}
330
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700331static struct page *alloc_fresh_huge_page_node(int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct page *page;
Joe Jinf96efd52007-07-15 23:38:12 -0700334
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700335 page = alloc_pages_node(nid,
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700336 htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
337 __GFP_REPEAT|__GFP_NOWARN,
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700338 HUGETLB_PAGE_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (page) {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700340 if (arch_prepare_hugepage(page)) {
341 __free_pages(page, HUGETLB_PAGE_ORDER);
Harvey Harrison7b8ee842008-04-28 14:13:19 -0700342 return NULL;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700343 }
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800344 set_compound_page_dtor(page, free_huge_page);
Eric Paris0bd0f9f2005-11-21 21:32:28 -0800345 spin_lock(&hugetlb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 nr_huge_pages++;
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700347 nr_huge_pages_node[nid]++;
Eric Paris0bd0f9f2005-11-21 21:32:28 -0800348 spin_unlock(&hugetlb_lock);
Nick Piggina4822892006-03-22 00:08:08 -0800349 put_page(page); /* free it into the hugepage allocator */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700351
352 return page;
353}
354
355static int alloc_fresh_huge_page(void)
356{
357 struct page *page;
358 int start_nid;
359 int next_nid;
360 int ret = 0;
361
362 start_nid = hugetlb_next_nid;
363
364 do {
365 page = alloc_fresh_huge_page_node(hugetlb_next_nid);
366 if (page)
367 ret = 1;
368 /*
369 * Use a helper variable to find the next node and then
370 * copy it back to hugetlb_next_nid afterwards:
371 * otherwise there's a window in which a racer might
372 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
373 * But we don't need to use a spin_lock here: it really
374 * doesn't matter if occasionally a racer chooses the
375 * same nid as we do. Move nid forward in the mask even
376 * if we just successfully allocated a hugepage so that
377 * the next caller gets hugepages on the next node.
378 */
379 next_nid = next_node(hugetlb_next_nid, node_online_map);
380 if (next_nid == MAX_NUMNODES)
381 next_nid = first_node(node_online_map);
382 hugetlb_next_nid = next_nid;
383 } while (!page && hugetlb_next_nid != start_nid);
384
Adam Litke3b116302008-04-28 02:13:06 -0700385 if (ret)
386 count_vm_event(HTLB_BUDDY_PGALLOC);
387 else
388 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
389
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700390 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Adam Litke7893d1d2007-10-16 01:26:18 -0700393static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
394 unsigned long address)
395{
396 struct page *page;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800397 unsigned int nid;
Adam Litke7893d1d2007-10-16 01:26:18 -0700398
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800399 /*
400 * Assume we will successfully allocate the surplus page to
401 * prevent racing processes from causing the surplus to exceed
402 * overcommit
403 *
404 * This however introduces a different race, where a process B
405 * tries to grow the static hugepage pool while alloc_pages() is
406 * called by process A. B will only examine the per-node
407 * counters in determining if surplus huge pages can be
408 * converted to normal huge pages in adjust_pool_surplus(). A
409 * won't be able to increment the per-node counter, until the
410 * lock is dropped by B, but B doesn't drop hugetlb_lock until
411 * no more huge pages can be converted from surplus to normal
412 * state (and doesn't try to convert again). Thus, we have a
413 * case where a surplus huge page exists, the pool is grown, and
414 * the surplus huge page still exists after, even though it
415 * should just have been converted to a normal huge page. This
416 * does not leak memory, though, as the hugepage will be freed
417 * once it is out of use. It also does not allow the counters to
418 * go out of whack in adjust_pool_surplus() as we don't modify
419 * the node values until we've gotten the hugepage and only the
420 * per-node value is checked there.
421 */
422 spin_lock(&hugetlb_lock);
423 if (surplus_huge_pages >= nr_overcommit_huge_pages) {
424 spin_unlock(&hugetlb_lock);
425 return NULL;
426 } else {
427 nr_huge_pages++;
428 surplus_huge_pages++;
429 }
430 spin_unlock(&hugetlb_lock);
431
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700432 page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
433 __GFP_REPEAT|__GFP_NOWARN,
Adam Litke7893d1d2007-10-16 01:26:18 -0700434 HUGETLB_PAGE_ORDER);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800435
436 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700437 if (page) {
Adam Litke2668db92008-03-10 11:43:50 -0700438 /*
439 * This page is now managed by the hugetlb allocator and has
440 * no users -- drop the buddy allocator's reference.
441 */
442 put_page_testzero(page);
443 VM_BUG_ON(page_count(page));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800444 nid = page_to_nid(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700445 set_compound_page_dtor(page, free_huge_page);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800446 /*
447 * We incremented the global counters already
448 */
449 nr_huge_pages_node[nid]++;
450 surplus_huge_pages_node[nid]++;
Adam Litke3b116302008-04-28 02:13:06 -0700451 __count_vm_event(HTLB_BUDDY_PGALLOC);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800452 } else {
453 nr_huge_pages--;
454 surplus_huge_pages--;
Adam Litke3b116302008-04-28 02:13:06 -0700455 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Adam Litke7893d1d2007-10-16 01:26:18 -0700456 }
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800457 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700458
459 return page;
460}
461
Adam Litkee4e574b2007-10-16 01:26:19 -0700462/*
463 * Increase the hugetlb pool such that it can accomodate a reservation
464 * of size 'delta'.
465 */
466static int gather_surplus_pages(int delta)
467{
468 struct list_head surplus_list;
469 struct page *page, *tmp;
470 int ret, i;
471 int needed, allocated;
472
473 needed = (resv_huge_pages + delta) - free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800474 if (needed <= 0) {
475 resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700476 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800477 }
Adam Litkee4e574b2007-10-16 01:26:19 -0700478
479 allocated = 0;
480 INIT_LIST_HEAD(&surplus_list);
481
482 ret = -ENOMEM;
483retry:
484 spin_unlock(&hugetlb_lock);
485 for (i = 0; i < needed; i++) {
486 page = alloc_buddy_huge_page(NULL, 0);
487 if (!page) {
488 /*
489 * We were not able to allocate enough pages to
490 * satisfy the entire reservation so we free what
491 * we've allocated so far.
492 */
493 spin_lock(&hugetlb_lock);
494 needed = 0;
495 goto free;
496 }
497
498 list_add(&page->lru, &surplus_list);
499 }
500 allocated += needed;
501
502 /*
503 * After retaking hugetlb_lock, we need to recalculate 'needed'
504 * because either resv_huge_pages or free_huge_pages may have changed.
505 */
506 spin_lock(&hugetlb_lock);
507 needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
508 if (needed > 0)
509 goto retry;
510
511 /*
512 * The surplus_list now contains _at_least_ the number of extra pages
513 * needed to accomodate the reservation. Add the appropriate number
514 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -0800515 * allocator. Commit the entire reservation here to prevent another
516 * process from stealing the pages as they are added to the pool but
517 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -0700518 */
519 needed += allocated;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800520 resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700521 ret = 0;
522free:
Adam Litke19fc3f02008-04-28 02:12:20 -0700523 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -0700524 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -0700525 if ((--needed) < 0)
526 break;
Adam Litkee4e574b2007-10-16 01:26:19 -0700527 list_del(&page->lru);
Adam Litke19fc3f02008-04-28 02:12:20 -0700528 enqueue_huge_page(page);
529 }
530
531 /* Free unnecessary surplus pages to the buddy allocator */
532 if (!list_empty(&surplus_list)) {
533 spin_unlock(&hugetlb_lock);
534 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
535 list_del(&page->lru);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700536 /*
Adam Litke2668db92008-03-10 11:43:50 -0700537 * The page has a reference count of zero already, so
538 * call free_huge_page directly instead of using
539 * put_page. This must be done with hugetlb_lock
Adam Litkeaf767cb2007-10-16 01:26:25 -0700540 * unlocked which is safe because free_huge_page takes
541 * hugetlb_lock before deciding how to free the page.
542 */
Adam Litke2668db92008-03-10 11:43:50 -0700543 free_huge_page(page);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700544 }
Adam Litke19fc3f02008-04-28 02:12:20 -0700545 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -0700546 }
547
548 return ret;
549}
550
551/*
552 * When releasing a hugetlb pool reservation, any surplus pages that were
553 * allocated to satisfy the reservation must be explicitly freed if they were
554 * never used.
555 */
Adrian Bunk8cde0452007-11-14 16:59:43 -0800556static void return_unused_surplus_pages(unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -0700557{
558 static int nid = -1;
559 struct page *page;
560 unsigned long nr_pages;
561
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700562 /*
563 * We want to release as many surplus pages as possible, spread
564 * evenly across all nodes. Iterate across all nodes until we
565 * can no longer free unreserved surplus pages. This occurs when
566 * the nodes with surplus pages have no free pages.
567 */
568 unsigned long remaining_iterations = num_online_nodes();
569
Adam Litkeac09b3a2008-03-04 14:29:38 -0800570 /* Uncommit the reservation */
571 resv_huge_pages -= unused_resv_pages;
572
Adam Litkee4e574b2007-10-16 01:26:19 -0700573 nr_pages = min(unused_resv_pages, surplus_huge_pages);
574
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700575 while (remaining_iterations-- && nr_pages) {
Adam Litkee4e574b2007-10-16 01:26:19 -0700576 nid = next_node(nid, node_online_map);
577 if (nid == MAX_NUMNODES)
578 nid = first_node(node_online_map);
579
580 if (!surplus_huge_pages_node[nid])
581 continue;
582
583 if (!list_empty(&hugepage_freelists[nid])) {
584 page = list_entry(hugepage_freelists[nid].next,
585 struct page, lru);
586 list_del(&page->lru);
587 update_and_free_page(page);
588 free_huge_pages--;
589 free_huge_pages_node[nid]--;
590 surplus_huge_pages--;
591 surplus_huge_pages_node[nid]--;
592 nr_pages--;
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700593 remaining_iterations = num_online_nodes();
Adam Litkee4e574b2007-10-16 01:26:19 -0700594 }
595 }
596}
597
David Gibson27a85ef2006-03-22 00:08:56 -0800598static struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700599 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Adam Litke348ea202007-11-14 16:59:37 -0800601 struct page *page;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800602 struct address_space *mapping = vma->vm_file->f_mapping;
Mel Gormana1e78772008-07-23 21:27:23 -0700603 struct inode *inode = mapping->host;
604 unsigned int chg = 0;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800605
Mel Gormana1e78772008-07-23 21:27:23 -0700606 /*
607 * Processes that did not create the mapping will have no reserves and
608 * will not have accounted against quota. Check that the quota can be
609 * made before satisfying the allocation
610 */
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700611 if (!(vma->vm_flags & VM_SHARED) &&
612 !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Mel Gormana1e78772008-07-23 21:27:23 -0700613 chg = 1;
614 if (hugetlb_get_quota(inode->i_mapping, chg))
615 return ERR_PTR(-ENOSPC);
Adam Litke90d8b7e2007-11-14 16:59:42 -0800616 }
Mel Gormana1e78772008-07-23 21:27:23 -0700617
618 spin_lock(&hugetlb_lock);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700619 page = dequeue_huge_page_vma(vma, addr, avoid_reserve);
Mel Gormana1e78772008-07-23 21:27:23 -0700620 spin_unlock(&hugetlb_lock);
621
622 if (!page) {
623 page = alloc_buddy_huge_page(vma, addr);
624 if (!page) {
625 hugetlb_put_quota(inode->i_mapping, chg);
626 return ERR_PTR(-VM_FAULT_OOM);
627 }
628 }
629
630 set_page_refcounted(page);
631 set_page_private(page, (unsigned long) mapping);
632
Adam Litke90d8b7e2007-11-14 16:59:42 -0800633 return page;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static int __init hugetlb_init(void)
637{
638 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100640 if (HPAGE_SHIFT == 0)
641 return 0;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 for (i = 0; i < MAX_NUMNODES; ++i)
644 INIT_LIST_HEAD(&hugepage_freelists[i]);
645
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700646 hugetlb_next_nid = first_node(node_online_map);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 for (i = 0; i < max_huge_pages; ++i) {
Nick Piggina4822892006-03-22 00:08:08 -0800649 if (!alloc_fresh_huge_page())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 max_huge_pages = free_huge_pages = nr_huge_pages = i;
653 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
654 return 0;
655}
656module_init(hugetlb_init);
657
658static int __init hugetlb_setup(char *s)
659{
660 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
661 max_huge_pages = 0;
662 return 1;
663}
664__setup("hugepages=", hugetlb_setup);
665
Ken Chen8a630112007-05-09 02:33:34 -0700666static unsigned int cpuset_mems_nr(unsigned int *array)
667{
668 int node;
669 unsigned int nr = 0;
670
671 for_each_node_mask(node, cpuset_current_mems_allowed)
672 nr += array[node];
673
674 return nr;
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678#ifdef CONFIG_HIGHMEM
679static void try_to_free_low(unsigned long count)
680{
Christoph Lameter4415cc82006-09-25 23:31:55 -0700681 int i;
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 for (i = 0; i < MAX_NUMNODES; ++i) {
684 struct page *page, *next;
685 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
Adam Litke6b0c8802007-10-16 01:26:23 -0700686 if (count >= nr_huge_pages)
687 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (PageHighMem(page))
689 continue;
690 list_del(&page->lru);
691 update_and_free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 free_huge_pages--;
Christoph Lameter4415cc82006-09-25 23:31:55 -0700693 free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 }
696}
697#else
698static inline void try_to_free_low(unsigned long count)
699{
700}
701#endif
702
Adam Litke7893d1d2007-10-16 01:26:18 -0700703#define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static unsigned long set_max_huge_pages(unsigned long count)
705{
Adam Litke7893d1d2007-10-16 01:26:18 -0700706 unsigned long min_count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Adam Litke7893d1d2007-10-16 01:26:18 -0700708 /*
709 * Increase the pool size
710 * First take pages out of surplus state. Then make up the
711 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800712 *
713 * We might race with alloc_buddy_huge_page() here and be unable
714 * to convert a surplus huge page to a normal huge page. That is
715 * not critical, though, it just means the overall size of the
716 * pool might be one hugepage larger than it needs to be, but
717 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -0700718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700720 while (surplus_huge_pages && count > persistent_huge_pages) {
721 if (!adjust_pool_surplus(-1))
722 break;
723 }
724
725 while (count > persistent_huge_pages) {
Adam Litke7893d1d2007-10-16 01:26:18 -0700726 /*
727 * If this allocation races such that we no longer need the
728 * page, free_huge_page will handle it by freeing the page
729 * and reducing the surplus.
730 */
731 spin_unlock(&hugetlb_lock);
732 ret = alloc_fresh_huge_page();
733 spin_lock(&hugetlb_lock);
734 if (!ret)
735 goto out;
736
737 }
Adam Litke7893d1d2007-10-16 01:26:18 -0700738
739 /*
740 * Decrease the pool size
741 * First return free pages to the buddy allocator (being careful
742 * to keep enough around to satisfy reservations). Then place
743 * pages into surplus state as needed so the pool will shrink
744 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800745 *
746 * By placing pages into the surplus state independent of the
747 * overcommit value, we are allowing the surplus pool size to
748 * exceed overcommit. There are few sane options here. Since
749 * alloc_buddy_huge_page() is checking the global counter,
750 * though, we'll note that we're not allowed to exceed surplus
751 * and won't grow the pool anywhere else. Not until one of the
752 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -0700753 */
Adam Litke6b0c8802007-10-16 01:26:23 -0700754 min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
755 min_count = max(count, min_count);
Adam Litke7893d1d2007-10-16 01:26:18 -0700756 try_to_free_low(min_count);
757 while (min_count < persistent_huge_pages) {
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800758 struct page *page = dequeue_huge_page();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!page)
760 break;
761 update_and_free_page(page);
762 }
Adam Litke7893d1d2007-10-16 01:26:18 -0700763 while (count < persistent_huge_pages) {
764 if (!adjust_pool_surplus(1))
765 break;
766 }
767out:
768 ret = persistent_huge_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700770 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773int hugetlb_sysctl_handler(struct ctl_table *table, int write,
774 struct file *file, void __user *buffer,
775 size_t *length, loff_t *ppos)
776{
777 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
778 max_huge_pages = set_max_huge_pages(max_huge_pages);
779 return 0;
780}
Mel Gorman396faf02007-07-17 04:03:13 -0700781
782int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
783 struct file *file, void __user *buffer,
784 size_t *length, loff_t *ppos)
785{
786 proc_dointvec(table, write, file, buffer, length, ppos);
787 if (hugepages_treat_as_movable)
788 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
789 else
790 htlb_alloc_mask = GFP_HIGHUSER;
791 return 0;
792}
793
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -0800794int hugetlb_overcommit_handler(struct ctl_table *table, int write,
795 struct file *file, void __user *buffer,
796 size_t *length, loff_t *ppos)
797{
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -0800798 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Nishanth Aravamudan064d9ef2008-02-13 15:03:19 -0800799 spin_lock(&hugetlb_lock);
800 nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -0800801 spin_unlock(&hugetlb_lock);
802 return 0;
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805#endif /* CONFIG_SYSCTL */
806
807int hugetlb_report_meminfo(char *buf)
808{
809 return sprintf(buf,
810 "HugePages_Total: %5lu\n"
811 "HugePages_Free: %5lu\n"
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700812 "HugePages_Rsvd: %5lu\n"
Adam Litke7893d1d2007-10-16 01:26:18 -0700813 "HugePages_Surp: %5lu\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 "Hugepagesize: %5lu kB\n",
815 nr_huge_pages,
816 free_huge_pages,
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700817 resv_huge_pages,
Adam Litke7893d1d2007-10-16 01:26:18 -0700818 surplus_huge_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 HPAGE_SIZE/1024);
820}
821
822int hugetlb_report_node_meminfo(int nid, char *buf)
823{
824 return sprintf(buf,
825 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -0700826 "Node %d HugePages_Free: %5u\n"
827 "Node %d HugePages_Surp: %5u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 nid, nr_huge_pages_node[nid],
Nishanth Aravamudana1de0912008-03-26 14:37:53 -0700829 nid, free_huge_pages_node[nid],
830 nid, surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
834unsigned long hugetlb_total_pages(void)
835{
836 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
837}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Mel Gormanfc1b8a72008-07-23 21:27:22 -0700839static int hugetlb_acct_memory(long delta)
840{
841 int ret = -ENOMEM;
842
843 spin_lock(&hugetlb_lock);
844 /*
845 * When cpuset is configured, it breaks the strict hugetlb page
846 * reservation as the accounting is done on a global variable. Such
847 * reservation is completely rubbish in the presence of cpuset because
848 * the reservation is not checked against page availability for the
849 * current cpuset. Application can still potentially OOM'ed by kernel
850 * with lack of free htlb page in cpuset that the task is in.
851 * Attempt to enforce strict accounting with cpuset is almost
852 * impossible (or too ugly) because cpuset is too fluid that
853 * task or memory node can be dynamically moved between cpusets.
854 *
855 * The change of semantics for shared hugetlb mapping with cpuset is
856 * undesirable. However, in order to preserve some of the semantics,
857 * we fall back to check against current free page availability as
858 * a best attempt and hopefully to minimize the impact of changing
859 * semantics that cpuset has.
860 */
861 if (delta > 0) {
862 if (gather_surplus_pages(delta) < 0)
863 goto out;
864
865 if (delta > cpuset_mems_nr(free_huge_pages_node)) {
866 return_unused_surplus_pages(delta);
867 goto out;
868 }
869 }
870
871 ret = 0;
872 if (delta < 0)
873 return_unused_surplus_pages((unsigned long) -delta);
874
875out:
876 spin_unlock(&hugetlb_lock);
877 return ret;
878}
879
Mel Gormana1e78772008-07-23 21:27:23 -0700880static void hugetlb_vm_op_close(struct vm_area_struct *vma)
881{
882 unsigned long reserve = vma_resv_huge_pages(vma);
883 if (reserve)
884 hugetlb_acct_memory(-reserve);
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/*
888 * We cannot handle pagefaults against hugetlb pages at all. They cause
889 * handle_mm_fault() to try to instantiate regular-sized pages in the
890 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
891 * this far.
892 */
Nick Piggind0217ac2007-07-19 01:47:03 -0700893static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -0700896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -0700900 .fault = hugetlb_vm_op_fault,
Mel Gormana1e78772008-07-23 21:27:23 -0700901 .close = hugetlb_vm_op_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902};
903
David Gibson1e8f8892006-01-06 00:10:44 -0800904static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
905 int writable)
David Gibson63551ae2005-06-21 17:14:44 -0700906{
907 pte_t entry;
908
David Gibson1e8f8892006-01-06 00:10:44 -0800909 if (writable) {
David Gibson63551ae2005-06-21 17:14:44 -0700910 entry =
911 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
912 } else {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700913 entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -0700914 }
915 entry = pte_mkyoung(entry);
916 entry = pte_mkhuge(entry);
917
918 return entry;
919}
920
David Gibson1e8f8892006-01-06 00:10:44 -0800921static void set_huge_ptep_writable(struct vm_area_struct *vma,
922 unsigned long address, pte_t *ptep)
923{
924 pte_t entry;
925
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700926 entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
927 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -0700928 update_mmu_cache(vma, address, entry);
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -0700929 }
David Gibson1e8f8892006-01-06 00:10:44 -0800930}
931
932
David Gibson63551ae2005-06-21 17:14:44 -0700933int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
934 struct vm_area_struct *vma)
935{
936 pte_t *src_pte, *dst_pte, entry;
937 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -0700938 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -0800939 int cow;
940
941 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -0700942
Hugh Dickins1c598272005-10-19 21:23:43 -0700943 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
Hugh Dickinsc74df322005-10-29 18:16:23 -0700944 src_pte = huge_pte_offset(src, addr);
945 if (!src_pte)
946 continue;
David Gibson63551ae2005-06-21 17:14:44 -0700947 dst_pte = huge_pte_alloc(dst, addr);
948 if (!dst_pte)
949 goto nomem;
Larry Woodmanc5c99422008-01-24 05:49:25 -0800950
951 /* If the pagetables are shared don't copy or take references */
952 if (dst_pte == src_pte)
953 continue;
954
Hugh Dickinsc74df322005-10-29 18:16:23 -0700955 spin_lock(&dst->page_table_lock);
Nick Piggin46478752008-06-05 22:45:57 -0700956 spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700957 if (!huge_pte_none(huge_ptep_get(src_pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -0800958 if (cow)
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700959 huge_ptep_set_wrprotect(src, addr, src_pte);
960 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -0700961 ptepage = pte_page(entry);
962 get_page(ptepage);
Hugh Dickins1c598272005-10-19 21:23:43 -0700963 set_huge_pte_at(dst, addr, dst_pte, entry);
964 }
965 spin_unlock(&src->page_table_lock);
Hugh Dickinsc74df322005-10-29 18:16:23 -0700966 spin_unlock(&dst->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -0700967 }
968 return 0;
969
970nomem:
971 return -ENOMEM;
972}
973
Chen, Kenneth W502717f2006-10-11 01:20:46 -0700974void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700975 unsigned long end, struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -0700976{
977 struct mm_struct *mm = vma->vm_mm;
978 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -0700979 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -0700980 pte_t pte;
981 struct page *page;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -0700982 struct page *tmp;
Chen, Kenneth Wc0a499c2006-12-06 20:31:39 -0800983 /*
984 * A page gathering list, protected by per file i_mmap_lock. The
985 * lock is used to avoid list corruption from multiple unmapping
986 * of the same page since we are using page->lru.
987 */
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -0700988 LIST_HEAD(page_list);
David Gibson63551ae2005-06-21 17:14:44 -0700989
990 WARN_ON(!is_vm_hugetlb_page(vma));
991 BUG_ON(start & ~HPAGE_MASK);
992 BUG_ON(end & ~HPAGE_MASK);
993
Hugh Dickins508034a2005-10-29 18:16:30 -0700994 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -0700995 for (address = start; address < end; address += HPAGE_SIZE) {
David Gibsonc7546f82005-08-05 11:59:35 -0700996 ptep = huge_pte_offset(mm, address);
Adam Litke4c887262005-10-29 18:16:46 -0700997 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -0700998 continue;
999
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001000 if (huge_pmd_unshare(mm, &address, ptep))
1001 continue;
1002
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001003 /*
1004 * If a reference page is supplied, it is because a specific
1005 * page is being unmapped, not a range. Ensure the page we
1006 * are about to unmap is the actual page of interest.
1007 */
1008 if (ref_page) {
1009 pte = huge_ptep_get(ptep);
1010 if (huge_pte_none(pte))
1011 continue;
1012 page = pte_page(pte);
1013 if (page != ref_page)
1014 continue;
1015
1016 /*
1017 * Mark the VMA as having unmapped its page so that
1018 * future faults in this VMA will fail rather than
1019 * looking like data was lost
1020 */
1021 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
1022 }
1023
David Gibsonc7546f82005-08-05 11:59:35 -07001024 pte = huge_ptep_get_and_clear(mm, address, ptep);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001025 if (huge_pte_none(pte))
David Gibson63551ae2005-06-21 17:14:44 -07001026 continue;
David Gibsonc7546f82005-08-05 11:59:35 -07001027
David Gibson63551ae2005-06-21 17:14:44 -07001028 page = pte_page(pte);
Ken Chen6649a382007-02-08 14:20:27 -08001029 if (pte_dirty(pte))
1030 set_page_dirty(page);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001031 list_add(&page->lru, &page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 spin_unlock(&mm->page_table_lock);
Hugh Dickins508034a2005-10-29 18:16:30 -07001034 flush_tlb_range(vma, start, end);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001035 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1036 list_del(&page->lru);
1037 put_page(page);
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
David Gibson63551ae2005-06-21 17:14:44 -07001040
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001041void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001042 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001043{
1044 /*
1045 * It is undesirable to test vma->vm_file as it should be non-null
1046 * for valid hugetlb area. However, vm_file will be NULL in the error
1047 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
1048 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
1049 * to clean up. Since no pte has actually been setup, it is safe to
1050 * do nothing in this case.
1051 */
1052 if (vma->vm_file) {
1053 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001054 __unmap_hugepage_range(vma, start, end, ref_page);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001055 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
1056 }
1057}
1058
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001059/*
1060 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1061 * mappping it owns the reserve page for. The intention is to unmap the page
1062 * from other VMAs and let the children be SIGKILLed if they are faulting the
1063 * same region.
1064 */
1065int unmap_ref_private(struct mm_struct *mm,
1066 struct vm_area_struct *vma,
1067 struct page *page,
1068 unsigned long address)
1069{
1070 struct vm_area_struct *iter_vma;
1071 struct address_space *mapping;
1072 struct prio_tree_iter iter;
1073 pgoff_t pgoff;
1074
1075 /*
1076 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1077 * from page cache lookup which is in HPAGE_SIZE units.
1078 */
1079 address = address & huge_page_mask(hstate_vma(vma));
1080 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
1081 + (vma->vm_pgoff >> PAGE_SHIFT);
1082 mapping = (struct address_space *)page_private(page);
1083
1084 vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1085 /* Do not unmap the current VMA */
1086 if (iter_vma == vma)
1087 continue;
1088
1089 /*
1090 * Unmap the page from other VMAs without their own reserves.
1091 * They get marked to be SIGKILLed if they fault in these
1092 * areas. This is because a future no-page fault on this VMA
1093 * could insert a zeroed page instead of the data existing
1094 * from the time of fork. This would look like data corruption
1095 */
1096 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
1097 unmap_hugepage_range(iter_vma,
1098 address, address + HPAGE_SIZE,
1099 page);
1100 }
1101
1102 return 1;
1103}
1104
David Gibson1e8f8892006-01-06 00:10:44 -08001105static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001106 unsigned long address, pte_t *ptep, pte_t pte,
1107 struct page *pagecache_page)
David Gibson1e8f8892006-01-06 00:10:44 -08001108{
1109 struct page *old_page, *new_page;
David Gibson79ac6ba2006-03-22 00:08:51 -08001110 int avoidcopy;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001111 int outside_reserve = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001112
1113 old_page = pte_page(pte);
1114
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001115retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08001116 /* If no-one else is actually using this page, avoid the copy
1117 * and just make the page writable */
1118 avoidcopy = (page_count(old_page) == 1);
1119 if (avoidcopy) {
1120 set_huge_ptep_writable(vma, address, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07001121 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001122 }
1123
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001124 /*
1125 * If the process that created a MAP_PRIVATE mapping is about to
1126 * perform a COW due to a shared page count, attempt to satisfy
1127 * the allocation without using the existing reserves. The pagecache
1128 * page is used to determine if the reserve at this address was
1129 * consumed or not. If reserves were used, a partial faulted mapping
1130 * at the time of fork() could consume its reserves on COW instead
1131 * of the full address range.
1132 */
1133 if (!(vma->vm_flags & VM_SHARED) &&
1134 is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
1135 old_page != pagecache_page)
1136 outside_reserve = 1;
1137
David Gibson1e8f8892006-01-06 00:10:44 -08001138 page_cache_get(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001139 new_page = alloc_huge_page(vma, address, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08001140
Adam Litke2fc39ce2007-11-14 16:59:39 -08001141 if (IS_ERR(new_page)) {
David Gibson1e8f8892006-01-06 00:10:44 -08001142 page_cache_release(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001143
1144 /*
1145 * If a process owning a MAP_PRIVATE mapping fails to COW,
1146 * it is due to references held by a child and an insufficient
1147 * huge page pool. To guarantee the original mappers
1148 * reliability, unmap the page from child processes. The child
1149 * may get SIGKILLed if it later faults.
1150 */
1151 if (outside_reserve) {
1152 BUG_ON(huge_pte_none(pte));
1153 if (unmap_ref_private(mm, vma, old_page, address)) {
1154 BUG_ON(page_count(old_page) != 1);
1155 BUG_ON(huge_pte_none(pte));
1156 goto retry_avoidcopy;
1157 }
1158 WARN_ON_ONCE(1);
1159 }
1160
Adam Litke2fc39ce2007-11-14 16:59:39 -08001161 return -PTR_ERR(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001162 }
1163
1164 spin_unlock(&mm->page_table_lock);
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00001165 copy_huge_page(new_page, old_page, address, vma);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001166 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001167 spin_lock(&mm->page_table_lock);
1168
1169 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001170 if (likely(pte_same(huge_ptep_get(ptep), pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001171 /* Break COW */
Gerald Schaefer8fe627e2008-04-28 02:13:28 -07001172 huge_ptep_clear_flush(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08001173 set_huge_pte_at(mm, address, ptep,
1174 make_huge_pte(vma, new_page, 1));
1175 /* Make the old page be freed below */
1176 new_page = old_page;
1177 }
1178 page_cache_release(new_page);
1179 page_cache_release(old_page);
Nick Piggin83c54072007-07-19 01:47:05 -07001180 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001181}
1182
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001183/* Return the pagecache page at a given address within a VMA */
1184static struct page *hugetlbfs_pagecache_page(struct vm_area_struct *vma,
1185 unsigned long address)
1186{
1187 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001188 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001189
1190 mapping = vma->vm_file->f_mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001191 idx = vma_pagecache_offset(vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001192
1193 return find_lock_page(mapping, idx);
1194}
1195
Robert P. J. Daya1ed3dd2007-07-17 04:03:33 -07001196static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
David Gibson1e8f8892006-01-06 00:10:44 -08001197 unsigned long address, pte_t *ptep, int write_access)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001198{
1199 int ret = VM_FAULT_SIGBUS;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001200 pgoff_t idx;
Adam Litke4c887262005-10-29 18:16:46 -07001201 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07001202 struct page *page;
1203 struct address_space *mapping;
David Gibson1e8f8892006-01-06 00:10:44 -08001204 pte_t new_pte;
Adam Litke4c887262005-10-29 18:16:46 -07001205
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001206 /*
1207 * Currently, we are forced to kill the process in the event the
1208 * original mapper has unmapped pages from the child due to a failed
1209 * COW. Warn that such a situation has occured as it may not be obvious
1210 */
1211 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
1212 printk(KERN_WARNING
1213 "PID %d killed due to inadequate hugepage pool\n",
1214 current->pid);
1215 return ret;
1216 }
1217
Adam Litke4c887262005-10-29 18:16:46 -07001218 mapping = vma->vm_file->f_mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001219 idx = vma_pagecache_offset(vma, address);
Adam Litke4c887262005-10-29 18:16:46 -07001220
1221 /*
1222 * Use page lock to guard against racing truncation
1223 * before we get page_table_lock.
1224 */
Christoph Lameter6bda6662006-01-06 00:10:49 -08001225retry:
1226 page = find_lock_page(mapping, idx);
1227 if (!page) {
Hugh Dickinsebed4bf2006-10-28 10:38:43 -07001228 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
1229 if (idx >= size)
1230 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001231 page = alloc_huge_page(vma, address, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08001232 if (IS_ERR(page)) {
1233 ret = -PTR_ERR(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001234 goto out;
1235 }
David Gibson79ac6ba2006-03-22 00:08:51 -08001236 clear_huge_page(page, address);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001237 __SetPageUptodate(page);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001238
Christoph Lameter6bda6662006-01-06 00:10:49 -08001239 if (vma->vm_flags & VM_SHARED) {
1240 int err;
Ken Chen45c682a2007-11-14 16:59:44 -08001241 struct inode *inode = mapping->host;
Christoph Lameter6bda6662006-01-06 00:10:49 -08001242
1243 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
1244 if (err) {
1245 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001246 if (err == -EEXIST)
1247 goto retry;
1248 goto out;
1249 }
Ken Chen45c682a2007-11-14 16:59:44 -08001250
1251 spin_lock(&inode->i_lock);
1252 inode->i_blocks += BLOCKS_PER_HUGEPAGE;
1253 spin_unlock(&inode->i_lock);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001254 } else
1255 lock_page(page);
1256 }
David Gibson1e8f8892006-01-06 00:10:44 -08001257
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001258 spin_lock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001259 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
1260 if (idx >= size)
1261 goto backout;
1262
Nick Piggin83c54072007-07-19 01:47:05 -07001263 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001264 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07001265 goto backout;
1266
David Gibson1e8f8892006-01-06 00:10:44 -08001267 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
1268 && (vma->vm_flags & VM_SHARED)));
1269 set_huge_pte_at(mm, address, ptep, new_pte);
1270
1271 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1272 /* Optimization, do the COW without a second fault */
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001273 ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
David Gibson1e8f8892006-01-06 00:10:44 -08001274 }
1275
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001276 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001277 unlock_page(page);
1278out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001279 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07001280
1281backout:
1282 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001283 unlock_page(page);
1284 put_page(page);
1285 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001286}
1287
Adam Litke86e52162006-01-06 00:10:43 -08001288int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1289 unsigned long address, int write_access)
1290{
1291 pte_t *ptep;
1292 pte_t entry;
David Gibson1e8f8892006-01-06 00:10:44 -08001293 int ret;
David Gibson3935baa2006-03-22 00:08:53 -08001294 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
Adam Litke86e52162006-01-06 00:10:43 -08001295
1296 ptep = huge_pte_alloc(mm, address);
1297 if (!ptep)
1298 return VM_FAULT_OOM;
1299
David Gibson3935baa2006-03-22 00:08:53 -08001300 /*
1301 * Serialize hugepage allocation and instantiation, so that we don't
1302 * get spurious allocation failures if two CPUs race to instantiate
1303 * the same page in the page cache.
1304 */
1305 mutex_lock(&hugetlb_instantiation_mutex);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001306 entry = huge_ptep_get(ptep);
1307 if (huge_pte_none(entry)) {
David Gibson3935baa2006-03-22 00:08:53 -08001308 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
1309 mutex_unlock(&hugetlb_instantiation_mutex);
1310 return ret;
1311 }
Adam Litke86e52162006-01-06 00:10:43 -08001312
Nick Piggin83c54072007-07-19 01:47:05 -07001313 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001314
1315 spin_lock(&mm->page_table_lock);
1316 /* Check for a racing update before calling hugetlb_cow */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001317 if (likely(pte_same(entry, huge_ptep_get(ptep))))
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001318 if (write_access && !pte_write(entry)) {
1319 struct page *page;
1320 page = hugetlbfs_pagecache_page(vma, address);
1321 ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
1322 if (page) {
1323 unlock_page(page);
1324 put_page(page);
1325 }
1326 }
David Gibson1e8f8892006-01-06 00:10:44 -08001327 spin_unlock(&mm->page_table_lock);
David Gibson3935baa2006-03-22 00:08:53 -08001328 mutex_unlock(&hugetlb_instantiation_mutex);
David Gibson1e8f8892006-01-06 00:10:44 -08001329
1330 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08001331}
1332
David Gibson63551ae2005-06-21 17:14:44 -07001333int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
1334 struct page **pages, struct vm_area_struct **vmas,
Adam Litke5b23dbe2007-11-14 16:59:33 -08001335 unsigned long *position, int *length, int i,
1336 int write)
David Gibson63551ae2005-06-21 17:14:44 -07001337{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001338 unsigned long pfn_offset;
1339 unsigned long vaddr = *position;
David Gibson63551ae2005-06-21 17:14:44 -07001340 int remainder = *length;
1341
Hugh Dickins1c598272005-10-19 21:23:43 -07001342 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001343 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07001344 pte_t *pte;
1345 struct page *page;
1346
1347 /*
1348 * Some archs (sparc64, sh*) have multiple pte_ts to
1349 * each hugepage. We have to make * sure we get the
1350 * first, for the page indexing below to work.
1351 */
1352 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
1353
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001354 if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
1355 (write && !pte_write(huge_ptep_get(pte)))) {
Adam Litke4c887262005-10-29 18:16:46 -07001356 int ret;
1357
1358 spin_unlock(&mm->page_table_lock);
Adam Litke5b23dbe2007-11-14 16:59:33 -08001359 ret = hugetlb_fault(mm, vma, vaddr, write);
Adam Litke4c887262005-10-29 18:16:46 -07001360 spin_lock(&mm->page_table_lock);
Adam Litkea89182c2007-08-22 14:01:51 -07001361 if (!(ret & VM_FAULT_ERROR))
Adam Litke4c887262005-10-29 18:16:46 -07001362 continue;
1363
1364 remainder = 0;
1365 if (!i)
1366 i = -EFAULT;
1367 break;
1368 }
David Gibson63551ae2005-06-21 17:14:44 -07001369
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001370 pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001371 page = pte_page(huge_ptep_get(pte));
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001372same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001373 if (pages) {
1374 get_page(page);
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001375 pages[i] = page + pfn_offset;
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001376 }
David Gibson63551ae2005-06-21 17:14:44 -07001377
1378 if (vmas)
1379 vmas[i] = vma;
1380
1381 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001382 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07001383 --remainder;
1384 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001385 if (vaddr < vma->vm_end && remainder &&
1386 pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
1387 /*
1388 * We use pfn_offset to avoid touching the pageframes
1389 * of this compound page.
1390 */
1391 goto same_page;
1392 }
David Gibson63551ae2005-06-21 17:14:44 -07001393 }
Hugh Dickins1c598272005-10-19 21:23:43 -07001394 spin_unlock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001395 *length = remainder;
1396 *position = vaddr;
1397
1398 return i;
1399}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001400
1401void hugetlb_change_protection(struct vm_area_struct *vma,
1402 unsigned long address, unsigned long end, pgprot_t newprot)
1403{
1404 struct mm_struct *mm = vma->vm_mm;
1405 unsigned long start = address;
1406 pte_t *ptep;
1407 pte_t pte;
1408
1409 BUG_ON(address >= end);
1410 flush_cache_range(vma, address, end);
1411
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001412 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001413 spin_lock(&mm->page_table_lock);
1414 for (; address < end; address += HPAGE_SIZE) {
1415 ptep = huge_pte_offset(mm, address);
1416 if (!ptep)
1417 continue;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001418 if (huge_pmd_unshare(mm, &address, ptep))
1419 continue;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001420 if (!huge_pte_none(huge_ptep_get(ptep))) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001421 pte = huge_ptep_get_and_clear(mm, address, ptep);
1422 pte = pte_mkhuge(pte_modify(pte, newprot));
1423 set_huge_pte_at(mm, address, ptep, pte);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001424 }
1425 }
1426 spin_unlock(&mm->page_table_lock);
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001427 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001428
1429 flush_tlb_range(vma, start, end);
1430}
1431
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001432struct file_region {
1433 struct list_head link;
1434 long from;
1435 long to;
1436};
1437
1438static long region_add(struct list_head *head, long f, long t)
1439{
1440 struct file_region *rg, *nrg, *trg;
1441
1442 /* Locate the region we are either in or before. */
1443 list_for_each_entry(rg, head, link)
1444 if (f <= rg->to)
1445 break;
1446
1447 /* Round our left edge to the current segment if it encloses us. */
1448 if (f > rg->from)
1449 f = rg->from;
1450
1451 /* Check for and consume any regions we now overlap with. */
1452 nrg = rg;
1453 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
1454 if (&rg->link == head)
1455 break;
1456 if (rg->from > t)
1457 break;
1458
1459 /* If this area reaches higher then extend our area to
1460 * include it completely. If this is not the first area
1461 * which we intend to reuse, free it. */
1462 if (rg->to > t)
1463 t = rg->to;
1464 if (rg != nrg) {
1465 list_del(&rg->link);
1466 kfree(rg);
1467 }
1468 }
1469 nrg->from = f;
1470 nrg->to = t;
1471 return 0;
1472}
1473
1474static long region_chg(struct list_head *head, long f, long t)
1475{
1476 struct file_region *rg, *nrg;
1477 long chg = 0;
1478
1479 /* Locate the region we are before or in. */
1480 list_for_each_entry(rg, head, link)
1481 if (f <= rg->to)
1482 break;
1483
1484 /* If we are below the current region then a new region is required.
1485 * Subtle, allocate a new region at the position but make it zero
Simon Arlott183ff222007-10-20 01:27:18 +02001486 * size such that we can guarantee to record the reservation. */
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001487 if (&rg->link == head || t < rg->from) {
1488 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001489 if (!nrg)
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001490 return -ENOMEM;
1491 nrg->from = f;
1492 nrg->to = f;
1493 INIT_LIST_HEAD(&nrg->link);
1494 list_add(&nrg->link, rg->link.prev);
1495
1496 return t - f;
1497 }
1498
1499 /* Round our left edge to the current segment if it encloses us. */
1500 if (f > rg->from)
1501 f = rg->from;
1502 chg = t - f;
1503
1504 /* Check for and consume any regions we now overlap with. */
1505 list_for_each_entry(rg, rg->link.prev, link) {
1506 if (&rg->link == head)
1507 break;
1508 if (rg->from > t)
1509 return chg;
1510
1511 /* We overlap with this area, if it extends futher than
1512 * us then we must extend ourselves. Account for its
1513 * existing reservation. */
1514 if (rg->to > t) {
1515 chg += rg->to - t;
1516 t = rg->to;
1517 }
1518 chg -= rg->to - rg->from;
1519 }
1520 return chg;
1521}
1522
1523static long region_truncate(struct list_head *head, long end)
1524{
1525 struct file_region *rg, *trg;
1526 long chg = 0;
1527
1528 /* Locate the region we are either in or before. */
1529 list_for_each_entry(rg, head, link)
1530 if (end <= rg->to)
1531 break;
1532 if (&rg->link == head)
1533 return 0;
1534
1535 /* If we are in the middle of a region then adjust it. */
1536 if (end > rg->from) {
1537 chg = rg->to - end;
1538 rg->to = end;
1539 rg = list_entry(rg->link.next, typeof(*rg), link);
1540 }
1541
1542 /* Drop any remaining regions. */
1543 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
1544 if (&rg->link == head)
1545 break;
1546 chg += rg->to - rg->from;
1547 list_del(&rg->link);
1548 kfree(rg);
1549 }
1550 return chg;
1551}
1552
Mel Gormana1e78772008-07-23 21:27:23 -07001553int hugetlb_reserve_pages(struct inode *inode,
1554 long from, long to,
1555 struct vm_area_struct *vma)
Adam Litkee4e574b2007-10-16 01:26:19 -07001556{
1557 long ret, chg;
1558
Mel Gormana1e78772008-07-23 21:27:23 -07001559 /*
1560 * Shared mappings base their reservation on the number of pages that
1561 * are already allocated on behalf of the file. Private mappings need
1562 * to reserve the full area even if read-only as mprotect() may be
1563 * called to make the mapping read-write. Assume !vma is a shm mapping
1564 */
1565 if (!vma || vma->vm_flags & VM_SHARED)
1566 chg = region_chg(&inode->i_mapping->private_list, from, to);
1567 else {
1568 chg = to - from;
1569 set_vma_resv_huge_pages(vma, chg);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001570 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
Mel Gormana1e78772008-07-23 21:27:23 -07001571 }
1572
Adam Litkee4e574b2007-10-16 01:26:19 -07001573 if (chg < 0)
1574 return chg;
Ken Chen8a630112007-05-09 02:33:34 -07001575
Adam Litke90d8b7e2007-11-14 16:59:42 -08001576 if (hugetlb_get_quota(inode->i_mapping, chg))
1577 return -ENOSPC;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001578 ret = hugetlb_acct_memory(chg);
Ken Chen68842c92008-01-14 00:55:19 -08001579 if (ret < 0) {
1580 hugetlb_put_quota(inode->i_mapping, chg);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001581 return ret;
Ken Chen68842c92008-01-14 00:55:19 -08001582 }
Mel Gormana1e78772008-07-23 21:27:23 -07001583 if (!vma || vma->vm_flags & VM_SHARED)
1584 region_add(&inode->i_mapping->private_list, from, to);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001585 return 0;
1586}
1587
1588void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1589{
1590 long chg = region_truncate(&inode->i_mapping->private_list, offset);
Ken Chen45c682a2007-11-14 16:59:44 -08001591
1592 spin_lock(&inode->i_lock);
1593 inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
1594 spin_unlock(&inode->i_lock);
1595
Adam Litke90d8b7e2007-11-14 16:59:42 -08001596 hugetlb_put_quota(inode->i_mapping, (chg - freed));
1597 hugetlb_acct_memory(-(chg - freed));
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001598}