blob: bb49ce5d0067fba7d820bee3ecbc7c8ecafb2f52 [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>
Nishanth Aravamudana3437872008-07-23 21:27:44 -070017#include <linux/sysfs.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080018
David Gibson63551ae2005-06-21 17:14:44 -070019#include <asm/page.h>
20#include <asm/pgtable.h>
21
22#include <linux/hugetlb.h>
Nick Piggin7835e982006-03-22 00:08:40 -080023#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
Mel Gorman396faf02007-07-17 04:03:13 -070026static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
27unsigned long hugepages_treat_as_movable;
Andi Kleena5516432008-07-23 21:27:41 -070028
Andi Kleene5ff2152008-07-23 21:27:42 -070029static int max_hstate;
30unsigned int default_hstate_idx;
31struct hstate hstates[HUGE_MAX_HSTATE];
32
33/* for command line parsing */
34static struct hstate * __initdata parsed_hstate;
35static unsigned long __initdata default_hstate_max_huge_pages;
36
37#define for_each_hstate(h) \
38 for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
Mel Gorman396faf02007-07-17 04:03:13 -070039
David Gibson3935baa2006-03-22 00:08:53 -080040/*
41 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
42 */
43static DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080044
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -070045/*
Andy Whitcroft96822902008-07-23 21:27:29 -070046 * Region tracking -- allows tracking of reservations and instantiated pages
47 * across the pages in a mapping.
Andy Whitcroft84afd992008-07-23 21:27:32 -070048 *
49 * The region data structures are protected by a combination of the mmap_sem
50 * and the hugetlb_instantion_mutex. To access or modify a region the caller
51 * must either hold the mmap_sem for write, or the mmap_sem for read and
52 * the hugetlb_instantiation mutex:
53 *
54 * down_write(&mm->mmap_sem);
55 * or
56 * down_read(&mm->mmap_sem);
57 * mutex_lock(&hugetlb_instantiation_mutex);
Andy Whitcroft96822902008-07-23 21:27:29 -070058 */
59struct file_region {
60 struct list_head link;
61 long from;
62 long to;
63};
64
65static long region_add(struct list_head *head, long f, long t)
66{
67 struct file_region *rg, *nrg, *trg;
68
69 /* Locate the region we are either in or before. */
70 list_for_each_entry(rg, head, link)
71 if (f <= rg->to)
72 break;
73
74 /* Round our left edge to the current segment if it encloses us. */
75 if (f > rg->from)
76 f = rg->from;
77
78 /* Check for and consume any regions we now overlap with. */
79 nrg = rg;
80 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
81 if (&rg->link == head)
82 break;
83 if (rg->from > t)
84 break;
85
86 /* If this area reaches higher then extend our area to
87 * include it completely. If this is not the first area
88 * which we intend to reuse, free it. */
89 if (rg->to > t)
90 t = rg->to;
91 if (rg != nrg) {
92 list_del(&rg->link);
93 kfree(rg);
94 }
95 }
96 nrg->from = f;
97 nrg->to = t;
98 return 0;
99}
100
101static long region_chg(struct list_head *head, long f, long t)
102{
103 struct file_region *rg, *nrg;
104 long chg = 0;
105
106 /* Locate the region we are before or in. */
107 list_for_each_entry(rg, head, link)
108 if (f <= rg->to)
109 break;
110
111 /* If we are below the current region then a new region is required.
112 * Subtle, allocate a new region at the position but make it zero
113 * size such that we can guarantee to record the reservation. */
114 if (&rg->link == head || t < rg->from) {
115 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
116 if (!nrg)
117 return -ENOMEM;
118 nrg->from = f;
119 nrg->to = f;
120 INIT_LIST_HEAD(&nrg->link);
121 list_add(&nrg->link, rg->link.prev);
122
123 return t - f;
124 }
125
126 /* Round our left edge to the current segment if it encloses us. */
127 if (f > rg->from)
128 f = rg->from;
129 chg = t - f;
130
131 /* Check for and consume any regions we now overlap with. */
132 list_for_each_entry(rg, rg->link.prev, link) {
133 if (&rg->link == head)
134 break;
135 if (rg->from > t)
136 return chg;
137
138 /* We overlap with this area, if it extends futher than
139 * us then we must extend ourselves. Account for its
140 * existing reservation. */
141 if (rg->to > t) {
142 chg += rg->to - t;
143 t = rg->to;
144 }
145 chg -= rg->to - rg->from;
146 }
147 return chg;
148}
149
150static long region_truncate(struct list_head *head, long end)
151{
152 struct file_region *rg, *trg;
153 long chg = 0;
154
155 /* Locate the region we are either in or before. */
156 list_for_each_entry(rg, head, link)
157 if (end <= rg->to)
158 break;
159 if (&rg->link == head)
160 return 0;
161
162 /* If we are in the middle of a region then adjust it. */
163 if (end > rg->from) {
164 chg = rg->to - end;
165 rg->to = end;
166 rg = list_entry(rg->link.next, typeof(*rg), link);
167 }
168
169 /* Drop any remaining regions. */
170 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
171 if (&rg->link == head)
172 break;
173 chg += rg->to - rg->from;
174 list_del(&rg->link);
175 kfree(rg);
176 }
177 return chg;
178}
179
Andy Whitcroft84afd992008-07-23 21:27:32 -0700180static long region_count(struct list_head *head, long f, long t)
181{
182 struct file_region *rg;
183 long chg = 0;
184
185 /* Locate each segment we overlap with, and count that overlap. */
186 list_for_each_entry(rg, head, link) {
187 int seg_from;
188 int seg_to;
189
190 if (rg->to <= f)
191 continue;
192 if (rg->from >= t)
193 break;
194
195 seg_from = max(rg->from, f);
196 seg_to = min(rg->to, t);
197
198 chg += seg_to - seg_from;
199 }
200
201 return chg;
202}
203
Andy Whitcroft96822902008-07-23 21:27:29 -0700204/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700205 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700206 * the mapping, in pagecache page units; huge pages here.
207 */
Andi Kleena5516432008-07-23 21:27:41 -0700208static pgoff_t vma_hugecache_offset(struct hstate *h,
209 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700210{
Andi Kleena5516432008-07-23 21:27:41 -0700211 return ((address - vma->vm_start) >> huge_page_shift(h)) +
212 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700213}
214
Andy Whitcroft84afd992008-07-23 21:27:32 -0700215/*
216 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
217 * bits of the reservation map pointer, which are always clear due to
218 * alignment.
219 */
220#define HPAGE_RESV_OWNER (1UL << 0)
221#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700222#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700223
Mel Gormana1e78772008-07-23 21:27:23 -0700224/*
225 * These helpers are used to track how many pages are reserved for
226 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
227 * is guaranteed to have their future faults succeed.
228 *
229 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
230 * the reserve counters are updated with the hugetlb_lock held. It is safe
231 * to reset the VMA at fork() time as it is not in use yet and there is no
232 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700233 *
234 * The private mapping reservation is represented in a subtly different
235 * manner to a shared mapping. A shared mapping has a region map associated
236 * with the underlying file, this region map represents the backing file
237 * pages which have ever had a reservation assigned which this persists even
238 * after the page is instantiated. A private mapping has a region map
239 * associated with the original mmap which is attached to all VMAs which
240 * reference it, this region map represents those offsets which have consumed
241 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -0700242 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700243static unsigned long get_vma_private_data(struct vm_area_struct *vma)
244{
245 return (unsigned long)vma->vm_private_data;
246}
247
248static void set_vma_private_data(struct vm_area_struct *vma,
249 unsigned long value)
250{
251 vma->vm_private_data = (void *)value;
252}
253
Andy Whitcroft84afd992008-07-23 21:27:32 -0700254struct resv_map {
255 struct kref refs;
256 struct list_head regions;
257};
258
259struct resv_map *resv_map_alloc(void)
260{
261 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
262 if (!resv_map)
263 return NULL;
264
265 kref_init(&resv_map->refs);
266 INIT_LIST_HEAD(&resv_map->regions);
267
268 return resv_map;
269}
270
271void resv_map_release(struct kref *ref)
272{
273 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
274
275 /* Clear out any active regions before we release the map. */
276 region_truncate(&resv_map->regions, 0);
277 kfree(resv_map);
278}
279
280static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700281{
282 VM_BUG_ON(!is_vm_hugetlb_page(vma));
283 if (!(vma->vm_flags & VM_SHARED))
Andy Whitcroft84afd992008-07-23 21:27:32 -0700284 return (struct resv_map *)(get_vma_private_data(vma) &
285 ~HPAGE_RESV_MASK);
Mel Gormana1e78772008-07-23 21:27:23 -0700286 return 0;
287}
288
Andy Whitcroft84afd992008-07-23 21:27:32 -0700289static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -0700290{
291 VM_BUG_ON(!is_vm_hugetlb_page(vma));
292 VM_BUG_ON(vma->vm_flags & VM_SHARED);
293
Andy Whitcroft84afd992008-07-23 21:27:32 -0700294 set_vma_private_data(vma, (get_vma_private_data(vma) &
295 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700296}
297
298static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
299{
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700300 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700301 VM_BUG_ON(vma->vm_flags & VM_SHARED);
302
303 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700304}
305
306static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
307{
308 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700309
310 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700311}
312
313/* Decrement the reserved pages in the hugepage pool by one */
Andi Kleena5516432008-07-23 21:27:41 -0700314static void decrement_hugepage_resv_vma(struct hstate *h,
315 struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700316{
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700317 if (vma->vm_flags & VM_NORESERVE)
318 return;
319
Mel Gormana1e78772008-07-23 21:27:23 -0700320 if (vma->vm_flags & VM_SHARED) {
321 /* Shared mappings always use reserves */
Andi Kleena5516432008-07-23 21:27:41 -0700322 h->resv_huge_pages--;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700323 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Mel Gormana1e78772008-07-23 21:27:23 -0700324 /*
325 * Only the process that called mmap() has reserves for
326 * private mappings.
327 */
Andi Kleena5516432008-07-23 21:27:41 -0700328 h->resv_huge_pages--;
Mel Gormana1e78772008-07-23 21:27:23 -0700329 }
330}
331
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700332/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700333void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
334{
335 VM_BUG_ON(!is_vm_hugetlb_page(vma));
336 if (!(vma->vm_flags & VM_SHARED))
337 vma->vm_private_data = (void *)0;
338}
339
340/* Returns true if the VMA has associated reserve pages */
341static int vma_has_private_reserves(struct vm_area_struct *vma)
342{
343 if (vma->vm_flags & VM_SHARED)
344 return 0;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700345 if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER))
Mel Gormana1e78772008-07-23 21:27:23 -0700346 return 0;
347 return 1;
348}
349
Andi Kleena5516432008-07-23 21:27:41 -0700350static void clear_huge_page(struct page *page,
351 unsigned long addr, unsigned long sz)
David Gibson79ac6ba2006-03-22 00:08:51 -0800352{
353 int i;
354
355 might_sleep();
Andi Kleena5516432008-07-23 21:27:41 -0700356 for (i = 0; i < sz/PAGE_SIZE; i++) {
David Gibson79ac6ba2006-03-22 00:08:51 -0800357 cond_resched();
Ralf Baechle281e0e32007-10-01 01:20:10 -0700358 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
David Gibson79ac6ba2006-03-22 00:08:51 -0800359 }
360}
361
362static void copy_huge_page(struct page *dst, struct page *src,
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000363 unsigned long addr, struct vm_area_struct *vma)
David Gibson79ac6ba2006-03-22 00:08:51 -0800364{
365 int i;
Andi Kleena5516432008-07-23 21:27:41 -0700366 struct hstate *h = hstate_vma(vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800367
368 might_sleep();
Andi Kleena5516432008-07-23 21:27:41 -0700369 for (i = 0; i < pages_per_huge_page(h); i++) {
David Gibson79ac6ba2006-03-22 00:08:51 -0800370 cond_resched();
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000371 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800372 }
373}
374
Andi Kleena5516432008-07-23 21:27:41 -0700375static void enqueue_huge_page(struct hstate *h, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 int nid = page_to_nid(page);
Andi Kleena5516432008-07-23 21:27:41 -0700378 list_add(&page->lru, &h->hugepage_freelists[nid]);
379 h->free_huge_pages++;
380 h->free_huge_pages_node[nid]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Andi Kleena5516432008-07-23 21:27:41 -0700383static struct page *dequeue_huge_page(struct hstate *h)
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800384{
385 int nid;
386 struct page *page = NULL;
387
388 for (nid = 0; nid < MAX_NUMNODES; ++nid) {
Andi Kleena5516432008-07-23 21:27:41 -0700389 if (!list_empty(&h->hugepage_freelists[nid])) {
390 page = list_entry(h->hugepage_freelists[nid].next,
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800391 struct page, lru);
392 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700393 h->free_huge_pages--;
394 h->free_huge_pages_node[nid]--;
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800395 break;
396 }
397 }
398 return page;
399}
400
Andi Kleena5516432008-07-23 21:27:41 -0700401static struct page *dequeue_huge_page_vma(struct hstate *h,
402 struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700403 unsigned long address, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Nishanth Aravamudan31a5c6e2007-07-15 23:38:02 -0700405 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct page *page = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700407 struct mempolicy *mpol;
Mel Gorman19770b32008-04-28 02:12:18 -0700408 nodemask_t *nodemask;
Mel Gorman396faf02007-07-17 04:03:13 -0700409 struct zonelist *zonelist = huge_zonelist(vma, address,
Mel Gorman19770b32008-04-28 02:12:18 -0700410 htlb_alloc_mask, &mpol, &nodemask);
Mel Gormandd1a2392008-04-28 02:12:17 -0700411 struct zone *zone;
412 struct zoneref *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Mel Gormana1e78772008-07-23 21:27:23 -0700414 /*
415 * A child process with MAP_PRIVATE mappings created by their parent
416 * have no page reserves. This check ensures that reservations are
417 * not "stolen". The child may still get SIGKILLed
418 */
419 if (!vma_has_private_reserves(vma) &&
Andi Kleena5516432008-07-23 21:27:41 -0700420 h->free_huge_pages - h->resv_huge_pages == 0)
Mel Gormana1e78772008-07-23 21:27:23 -0700421 return NULL;
422
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700423 /* If reserves cannot be used, ensure enough pages are in the pool */
Andi Kleena5516432008-07-23 21:27:41 -0700424 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700425 return NULL;
426
Mel Gorman19770b32008-04-28 02:12:18 -0700427 for_each_zone_zonelist_nodemask(zone, z, zonelist,
428 MAX_NR_ZONES - 1, nodemask) {
Mel Gorman54a6eb52008-04-28 02:12:16 -0700429 nid = zone_to_nid(zone);
430 if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
Andi Kleena5516432008-07-23 21:27:41 -0700431 !list_empty(&h->hugepage_freelists[nid])) {
432 page = list_entry(h->hugepage_freelists[nid].next,
Andrew Morton3abf7af2007-07-19 01:49:08 -0700433 struct page, lru);
434 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700435 h->free_huge_pages--;
436 h->free_huge_pages_node[nid]--;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700437
438 if (!avoid_reserve)
Andi Kleena5516432008-07-23 21:27:41 -0700439 decrement_hugepage_resv_vma(h, vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700440
Ken Chen5ab3ee72007-07-23 18:44:00 -0700441 break;
Andrew Morton3abf7af2007-07-19 01:49:08 -0700442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700444 mpol_cond_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return page;
446}
447
Andi Kleena5516432008-07-23 21:27:41 -0700448static void update_and_free_page(struct hstate *h, struct page *page)
Adam Litke6af2acb2007-10-16 01:26:16 -0700449{
450 int i;
Andi Kleena5516432008-07-23 21:27:41 -0700451
452 h->nr_huge_pages--;
453 h->nr_huge_pages_node[page_to_nid(page)]--;
454 for (i = 0; i < pages_per_huge_page(h); i++) {
Adam Litke6af2acb2007-10-16 01:26:16 -0700455 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
456 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
457 1 << PG_private | 1<< PG_writeback);
458 }
459 set_compound_page_dtor(page, NULL);
460 set_page_refcounted(page);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700461 arch_release_hugepage(page);
Andi Kleena5516432008-07-23 21:27:41 -0700462 __free_pages(page, huge_page_order(h));
Adam Litke6af2acb2007-10-16 01:26:16 -0700463}
464
Andi Kleene5ff2152008-07-23 21:27:42 -0700465struct hstate *size_to_hstate(unsigned long size)
466{
467 struct hstate *h;
468
469 for_each_hstate(h) {
470 if (huge_page_size(h) == size)
471 return h;
472 }
473 return NULL;
474}
475
David Gibson27a85ef2006-03-22 00:08:56 -0800476static void free_huge_page(struct page *page)
477{
Andi Kleena5516432008-07-23 21:27:41 -0700478 /*
479 * Can't pass hstate in here because it is called from the
480 * compound page destructor.
481 */
Andi Kleene5ff2152008-07-23 21:27:42 -0700482 struct hstate *h = page_hstate(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700483 int nid = page_to_nid(page);
Adam Litkec79fb752007-11-14 16:59:38 -0800484 struct address_space *mapping;
David Gibson27a85ef2006-03-22 00:08:56 -0800485
Adam Litkec79fb752007-11-14 16:59:38 -0800486 mapping = (struct address_space *) page_private(page);
Andy Whitcrofte5df70a2008-02-23 15:23:32 -0800487 set_page_private(page, 0);
Adam Litke7893d1d2007-10-16 01:26:18 -0700488 BUG_ON(page_count(page));
David Gibson27a85ef2006-03-22 00:08:56 -0800489 INIT_LIST_HEAD(&page->lru);
490
491 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700492 if (h->surplus_huge_pages_node[nid]) {
493 update_and_free_page(h, page);
494 h->surplus_huge_pages--;
495 h->surplus_huge_pages_node[nid]--;
Adam Litke7893d1d2007-10-16 01:26:18 -0700496 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700497 enqueue_huge_page(h, page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700498 }
David Gibson27a85ef2006-03-22 00:08:56 -0800499 spin_unlock(&hugetlb_lock);
Adam Litkec79fb752007-11-14 16:59:38 -0800500 if (mapping)
Adam Litke9a119c02007-11-14 16:59:41 -0800501 hugetlb_put_quota(mapping, 1);
David Gibson27a85ef2006-03-22 00:08:56 -0800502}
503
Adam Litke7893d1d2007-10-16 01:26:18 -0700504/*
505 * Increment or decrement surplus_huge_pages. Keep node-specific counters
506 * balanced by operating on them in a round-robin fashion.
507 * Returns 1 if an adjustment was made.
508 */
Andi Kleena5516432008-07-23 21:27:41 -0700509static int adjust_pool_surplus(struct hstate *h, int delta)
Adam Litke7893d1d2007-10-16 01:26:18 -0700510{
511 static int prev_nid;
512 int nid = prev_nid;
513 int ret = 0;
514
515 VM_BUG_ON(delta != -1 && delta != 1);
516 do {
517 nid = next_node(nid, node_online_map);
518 if (nid == MAX_NUMNODES)
519 nid = first_node(node_online_map);
520
521 /* To shrink on this node, there must be a surplus page */
Andi Kleena5516432008-07-23 21:27:41 -0700522 if (delta < 0 && !h->surplus_huge_pages_node[nid])
Adam Litke7893d1d2007-10-16 01:26:18 -0700523 continue;
524 /* Surplus cannot exceed the total number of pages */
Andi Kleena5516432008-07-23 21:27:41 -0700525 if (delta > 0 && h->surplus_huge_pages_node[nid] >=
526 h->nr_huge_pages_node[nid])
Adam Litke7893d1d2007-10-16 01:26:18 -0700527 continue;
528
Andi Kleena5516432008-07-23 21:27:41 -0700529 h->surplus_huge_pages += delta;
530 h->surplus_huge_pages_node[nid] += delta;
Adam Litke7893d1d2007-10-16 01:26:18 -0700531 ret = 1;
532 break;
533 } while (nid != prev_nid);
534
535 prev_nid = nid;
536 return ret;
537}
538
Andi Kleena5516432008-07-23 21:27:41 -0700539static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700540{
541 set_compound_page_dtor(page, free_huge_page);
542 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700543 h->nr_huge_pages++;
544 h->nr_huge_pages_node[nid]++;
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700545 spin_unlock(&hugetlb_lock);
546 put_page(page); /* free it into the hugepage allocator */
547}
548
Andi Kleena5516432008-07-23 21:27:41 -0700549static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct page *page;
Joe Jinf96efd52007-07-15 23:38:12 -0700552
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700553 page = alloc_pages_node(nid,
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700554 htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
555 __GFP_REPEAT|__GFP_NOWARN,
Andi Kleena5516432008-07-23 21:27:41 -0700556 huge_page_order(h));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (page) {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700558 if (arch_prepare_hugepage(page)) {
559 __free_pages(page, HUGETLB_PAGE_ORDER);
Harvey Harrison7b8ee842008-04-28 14:13:19 -0700560 return NULL;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700561 }
Andi Kleena5516432008-07-23 21:27:41 -0700562 prep_new_huge_page(h, page, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700564
565 return page;
566}
567
Andi Kleena5516432008-07-23 21:27:41 -0700568static int alloc_fresh_huge_page(struct hstate *h)
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700569{
570 struct page *page;
571 int start_nid;
572 int next_nid;
573 int ret = 0;
574
Andi Kleena5516432008-07-23 21:27:41 -0700575 start_nid = h->hugetlb_next_nid;
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700576
577 do {
Andi Kleena5516432008-07-23 21:27:41 -0700578 page = alloc_fresh_huge_page_node(h, h->hugetlb_next_nid);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700579 if (page)
580 ret = 1;
581 /*
582 * Use a helper variable to find the next node and then
583 * copy it back to hugetlb_next_nid afterwards:
584 * otherwise there's a window in which a racer might
585 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
586 * But we don't need to use a spin_lock here: it really
587 * doesn't matter if occasionally a racer chooses the
588 * same nid as we do. Move nid forward in the mask even
589 * if we just successfully allocated a hugepage so that
590 * the next caller gets hugepages on the next node.
591 */
Andi Kleena5516432008-07-23 21:27:41 -0700592 next_nid = next_node(h->hugetlb_next_nid, node_online_map);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700593 if (next_nid == MAX_NUMNODES)
594 next_nid = first_node(node_online_map);
Andi Kleena5516432008-07-23 21:27:41 -0700595 h->hugetlb_next_nid = next_nid;
596 } while (!page && h->hugetlb_next_nid != start_nid);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700597
Adam Litke3b116302008-04-28 02:13:06 -0700598 if (ret)
599 count_vm_event(HTLB_BUDDY_PGALLOC);
600 else
601 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
602
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700603 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Andi Kleena5516432008-07-23 21:27:41 -0700606static struct page *alloc_buddy_huge_page(struct hstate *h,
607 struct vm_area_struct *vma, unsigned long address)
Adam Litke7893d1d2007-10-16 01:26:18 -0700608{
609 struct page *page;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800610 unsigned int nid;
Adam Litke7893d1d2007-10-16 01:26:18 -0700611
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800612 /*
613 * Assume we will successfully allocate the surplus page to
614 * prevent racing processes from causing the surplus to exceed
615 * overcommit
616 *
617 * This however introduces a different race, where a process B
618 * tries to grow the static hugepage pool while alloc_pages() is
619 * called by process A. B will only examine the per-node
620 * counters in determining if surplus huge pages can be
621 * converted to normal huge pages in adjust_pool_surplus(). A
622 * won't be able to increment the per-node counter, until the
623 * lock is dropped by B, but B doesn't drop hugetlb_lock until
624 * no more huge pages can be converted from surplus to normal
625 * state (and doesn't try to convert again). Thus, we have a
626 * case where a surplus huge page exists, the pool is grown, and
627 * the surplus huge page still exists after, even though it
628 * should just have been converted to a normal huge page. This
629 * does not leak memory, though, as the hugepage will be freed
630 * once it is out of use. It also does not allow the counters to
631 * go out of whack in adjust_pool_surplus() as we don't modify
632 * the node values until we've gotten the hugepage and only the
633 * per-node value is checked there.
634 */
635 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700636 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800637 spin_unlock(&hugetlb_lock);
638 return NULL;
639 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700640 h->nr_huge_pages++;
641 h->surplus_huge_pages++;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800642 }
643 spin_unlock(&hugetlb_lock);
644
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700645 page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
646 __GFP_REPEAT|__GFP_NOWARN,
Andi Kleena5516432008-07-23 21:27:41 -0700647 huge_page_order(h));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800648
649 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700650 if (page) {
Adam Litke2668db92008-03-10 11:43:50 -0700651 /*
652 * This page is now managed by the hugetlb allocator and has
653 * no users -- drop the buddy allocator's reference.
654 */
655 put_page_testzero(page);
656 VM_BUG_ON(page_count(page));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800657 nid = page_to_nid(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700658 set_compound_page_dtor(page, free_huge_page);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800659 /*
660 * We incremented the global counters already
661 */
Andi Kleena5516432008-07-23 21:27:41 -0700662 h->nr_huge_pages_node[nid]++;
663 h->surplus_huge_pages_node[nid]++;
Adam Litke3b116302008-04-28 02:13:06 -0700664 __count_vm_event(HTLB_BUDDY_PGALLOC);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800665 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700666 h->nr_huge_pages--;
667 h->surplus_huge_pages--;
Adam Litke3b116302008-04-28 02:13:06 -0700668 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Adam Litke7893d1d2007-10-16 01:26:18 -0700669 }
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800670 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700671
672 return page;
673}
674
Adam Litkee4e574b2007-10-16 01:26:19 -0700675/*
676 * Increase the hugetlb pool such that it can accomodate a reservation
677 * of size 'delta'.
678 */
Andi Kleena5516432008-07-23 21:27:41 -0700679static int gather_surplus_pages(struct hstate *h, int delta)
Adam Litkee4e574b2007-10-16 01:26:19 -0700680{
681 struct list_head surplus_list;
682 struct page *page, *tmp;
683 int ret, i;
684 int needed, allocated;
685
Andi Kleena5516432008-07-23 21:27:41 -0700686 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800687 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -0700688 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700689 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800690 }
Adam Litkee4e574b2007-10-16 01:26:19 -0700691
692 allocated = 0;
693 INIT_LIST_HEAD(&surplus_list);
694
695 ret = -ENOMEM;
696retry:
697 spin_unlock(&hugetlb_lock);
698 for (i = 0; i < needed; i++) {
Andi Kleena5516432008-07-23 21:27:41 -0700699 page = alloc_buddy_huge_page(h, NULL, 0);
Adam Litkee4e574b2007-10-16 01:26:19 -0700700 if (!page) {
701 /*
702 * We were not able to allocate enough pages to
703 * satisfy the entire reservation so we free what
704 * we've allocated so far.
705 */
706 spin_lock(&hugetlb_lock);
707 needed = 0;
708 goto free;
709 }
710
711 list_add(&page->lru, &surplus_list);
712 }
713 allocated += needed;
714
715 /*
716 * After retaking hugetlb_lock, we need to recalculate 'needed'
717 * because either resv_huge_pages or free_huge_pages may have changed.
718 */
719 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700720 needed = (h->resv_huge_pages + delta) -
721 (h->free_huge_pages + allocated);
Adam Litkee4e574b2007-10-16 01:26:19 -0700722 if (needed > 0)
723 goto retry;
724
725 /*
726 * The surplus_list now contains _at_least_ the number of extra pages
727 * needed to accomodate the reservation. Add the appropriate number
728 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -0800729 * allocator. Commit the entire reservation here to prevent another
730 * process from stealing the pages as they are added to the pool but
731 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -0700732 */
733 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -0700734 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700735 ret = 0;
736free:
Adam Litke19fc3f02008-04-28 02:12:20 -0700737 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -0700738 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -0700739 if ((--needed) < 0)
740 break;
Adam Litkee4e574b2007-10-16 01:26:19 -0700741 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700742 enqueue_huge_page(h, page);
Adam Litke19fc3f02008-04-28 02:12:20 -0700743 }
744
745 /* Free unnecessary surplus pages to the buddy allocator */
746 if (!list_empty(&surplus_list)) {
747 spin_unlock(&hugetlb_lock);
748 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
749 list_del(&page->lru);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700750 /*
Adam Litke2668db92008-03-10 11:43:50 -0700751 * The page has a reference count of zero already, so
752 * call free_huge_page directly instead of using
753 * put_page. This must be done with hugetlb_lock
Adam Litkeaf767cb2007-10-16 01:26:25 -0700754 * unlocked which is safe because free_huge_page takes
755 * hugetlb_lock before deciding how to free the page.
756 */
Adam Litke2668db92008-03-10 11:43:50 -0700757 free_huge_page(page);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700758 }
Adam Litke19fc3f02008-04-28 02:12:20 -0700759 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -0700760 }
761
762 return ret;
763}
764
765/*
766 * When releasing a hugetlb pool reservation, any surplus pages that were
767 * allocated to satisfy the reservation must be explicitly freed if they were
768 * never used.
769 */
Andi Kleena5516432008-07-23 21:27:41 -0700770static void return_unused_surplus_pages(struct hstate *h,
771 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -0700772{
773 static int nid = -1;
774 struct page *page;
775 unsigned long nr_pages;
776
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700777 /*
778 * We want to release as many surplus pages as possible, spread
779 * evenly across all nodes. Iterate across all nodes until we
780 * can no longer free unreserved surplus pages. This occurs when
781 * the nodes with surplus pages have no free pages.
782 */
783 unsigned long remaining_iterations = num_online_nodes();
784
Adam Litkeac09b3a2008-03-04 14:29:38 -0800785 /* Uncommit the reservation */
Andi Kleena5516432008-07-23 21:27:41 -0700786 h->resv_huge_pages -= unused_resv_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800787
Andi Kleena5516432008-07-23 21:27:41 -0700788 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -0700789
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700790 while (remaining_iterations-- && nr_pages) {
Adam Litkee4e574b2007-10-16 01:26:19 -0700791 nid = next_node(nid, node_online_map);
792 if (nid == MAX_NUMNODES)
793 nid = first_node(node_online_map);
794
Andi Kleena5516432008-07-23 21:27:41 -0700795 if (!h->surplus_huge_pages_node[nid])
Adam Litkee4e574b2007-10-16 01:26:19 -0700796 continue;
797
Andi Kleena5516432008-07-23 21:27:41 -0700798 if (!list_empty(&h->hugepage_freelists[nid])) {
799 page = list_entry(h->hugepage_freelists[nid].next,
Adam Litkee4e574b2007-10-16 01:26:19 -0700800 struct page, lru);
801 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700802 update_and_free_page(h, page);
803 h->free_huge_pages--;
804 h->free_huge_pages_node[nid]--;
805 h->surplus_huge_pages--;
806 h->surplus_huge_pages_node[nid]--;
Adam Litkee4e574b2007-10-16 01:26:19 -0700807 nr_pages--;
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700808 remaining_iterations = num_online_nodes();
Adam Litkee4e574b2007-10-16 01:26:19 -0700809 }
810 }
811}
812
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700813/*
814 * Determine if the huge page at addr within the vma has an associated
815 * reservation. Where it does not we will need to logically increase
816 * reservation and actually increase quota before an allocation can occur.
817 * Where any new reservation would be required the reservation change is
818 * prepared, but not committed. Once the page has been quota'd allocated
819 * an instantiated the change should be committed via vma_commit_reservation.
820 * No action is required on failure.
821 */
Andi Kleena5516432008-07-23 21:27:41 -0700822static int vma_needs_reservation(struct hstate *h,
823 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700824{
825 struct address_space *mapping = vma->vm_file->f_mapping;
826 struct inode *inode = mapping->host;
827
828 if (vma->vm_flags & VM_SHARED) {
Andi Kleena5516432008-07-23 21:27:41 -0700829 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700830 return region_chg(&inode->i_mapping->private_list,
831 idx, idx + 1);
832
Andy Whitcroft84afd992008-07-23 21:27:32 -0700833 } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
834 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700835
Andy Whitcroft84afd992008-07-23 21:27:32 -0700836 } else {
837 int err;
Andi Kleena5516432008-07-23 21:27:41 -0700838 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700839 struct resv_map *reservations = vma_resv_map(vma);
840
841 err = region_chg(&reservations->regions, idx, idx + 1);
842 if (err < 0)
843 return err;
844 return 0;
845 }
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700846}
Andi Kleena5516432008-07-23 21:27:41 -0700847static void vma_commit_reservation(struct hstate *h,
848 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700849{
850 struct address_space *mapping = vma->vm_file->f_mapping;
851 struct inode *inode = mapping->host;
852
853 if (vma->vm_flags & VM_SHARED) {
Andi Kleena5516432008-07-23 21:27:41 -0700854 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700855 region_add(&inode->i_mapping->private_list, idx, idx + 1);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700856
857 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Andi Kleena5516432008-07-23 21:27:41 -0700858 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700859 struct resv_map *reservations = vma_resv_map(vma);
860
861 /* Mark this page used in the map. */
862 region_add(&reservations->regions, idx, idx + 1);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700863 }
864}
865
David Gibson27a85ef2006-03-22 00:08:56 -0800866static struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700867 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Andi Kleena5516432008-07-23 21:27:41 -0700869 struct hstate *h = hstate_vma(vma);
Adam Litke348ea202007-11-14 16:59:37 -0800870 struct page *page;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800871 struct address_space *mapping = vma->vm_file->f_mapping;
Mel Gormana1e78772008-07-23 21:27:23 -0700872 struct inode *inode = mapping->host;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700873 unsigned int chg;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800874
Mel Gormana1e78772008-07-23 21:27:23 -0700875 /*
876 * Processes that did not create the mapping will have no reserves and
877 * will not have accounted against quota. Check that the quota can be
878 * made before satisfying the allocation
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700879 * MAP_NORESERVE mappings may also need pages and quota allocated
880 * if no reserve mapping overlaps.
Mel Gormana1e78772008-07-23 21:27:23 -0700881 */
Andi Kleena5516432008-07-23 21:27:41 -0700882 chg = vma_needs_reservation(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700883 if (chg < 0)
884 return ERR_PTR(chg);
885 if (chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700886 if (hugetlb_get_quota(inode->i_mapping, chg))
887 return ERR_PTR(-ENOSPC);
Mel Gormana1e78772008-07-23 21:27:23 -0700888
889 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700890 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
Mel Gormana1e78772008-07-23 21:27:23 -0700891 spin_unlock(&hugetlb_lock);
892
893 if (!page) {
Andi Kleena5516432008-07-23 21:27:41 -0700894 page = alloc_buddy_huge_page(h, vma, addr);
Mel Gormana1e78772008-07-23 21:27:23 -0700895 if (!page) {
896 hugetlb_put_quota(inode->i_mapping, chg);
897 return ERR_PTR(-VM_FAULT_OOM);
898 }
899 }
900
901 set_page_refcounted(page);
902 set_page_private(page, (unsigned long) mapping);
903
Andi Kleena5516432008-07-23 21:27:41 -0700904 vma_commit_reservation(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700905
Adam Litke90d8b7e2007-11-14 16:59:42 -0800906 return page;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800907}
908
Andi Kleene5ff2152008-07-23 21:27:42 -0700909static void __init hugetlb_init_one_hstate(struct hstate *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Andi Kleena5516432008-07-23 21:27:41 -0700913 for (i = 0; i < MAX_NUMNODES; ++i)
914 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
915
916 h->hugetlb_next_nid = first_node(node_online_map);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700917
Andi Kleene5ff2152008-07-23 21:27:42 -0700918 for (i = 0; i < h->max_huge_pages; ++i) {
Andi Kleena5516432008-07-23 21:27:41 -0700919 if (!alloc_fresh_huge_page(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Andi Kleene5ff2152008-07-23 21:27:42 -0700922 h->max_huge_pages = h->free_huge_pages = h->nr_huge_pages = i;
923}
924
925static void __init hugetlb_init_hstates(void)
926{
927 struct hstate *h;
928
929 for_each_hstate(h) {
930 hugetlb_init_one_hstate(h);
931 }
932}
933
934static void __init report_hugepages(void)
935{
936 struct hstate *h;
937
938 for_each_hstate(h) {
939 printk(KERN_INFO "Total HugeTLB memory allocated, "
940 "%ld %dMB pages\n",
941 h->free_huge_pages,
942 1 << (h->order + PAGE_SHIFT - 20));
943 }
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947#ifdef CONFIG_HIGHMEM
Andi Kleena5516432008-07-23 21:27:41 -0700948static void try_to_free_low(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Christoph Lameter4415cc82006-09-25 23:31:55 -0700950 int i;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 for (i = 0; i < MAX_NUMNODES; ++i) {
953 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -0700954 struct list_head *freel = &h->hugepage_freelists[i];
955 list_for_each_entry_safe(page, next, freel, lru) {
956 if (count >= h->nr_huge_pages)
Adam Litke6b0c8802007-10-16 01:26:23 -0700957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (PageHighMem(page))
959 continue;
960 list_del(&page->lru);
Andi Kleene5ff2152008-07-23 21:27:42 -0700961 update_and_free_page(h, page);
Andi Kleena5516432008-07-23 21:27:41 -0700962 h->free_huge_pages--;
963 h->free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 }
966}
967#else
Andi Kleena5516432008-07-23 21:27:41 -0700968static inline void try_to_free_low(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970}
971#endif
972
Andi Kleena5516432008-07-23 21:27:41 -0700973#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Andi Kleene5ff2152008-07-23 21:27:42 -0700974static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Adam Litke7893d1d2007-10-16 01:26:18 -0700976 unsigned long min_count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Adam Litke7893d1d2007-10-16 01:26:18 -0700978 /*
979 * Increase the pool size
980 * First take pages out of surplus state. Then make up the
981 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800982 *
983 * We might race with alloc_buddy_huge_page() here and be unable
984 * to convert a surplus huge page to a normal huge page. That is
985 * not critical, though, it just means the overall size of the
986 * pool might be one hugepage larger than it needs to be, but
987 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -0700988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700990 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
991 if (!adjust_pool_surplus(h, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -0700992 break;
993 }
994
Andi Kleena5516432008-07-23 21:27:41 -0700995 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -0700996 /*
997 * If this allocation races such that we no longer need the
998 * page, free_huge_page will handle it by freeing the page
999 * and reducing the surplus.
1000 */
1001 spin_unlock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001002 ret = alloc_fresh_huge_page(h);
Adam Litke7893d1d2007-10-16 01:26:18 -07001003 spin_lock(&hugetlb_lock);
1004 if (!ret)
1005 goto out;
1006
1007 }
Adam Litke7893d1d2007-10-16 01:26:18 -07001008
1009 /*
1010 * Decrease the pool size
1011 * First return free pages to the buddy allocator (being careful
1012 * to keep enough around to satisfy reservations). Then place
1013 * pages into surplus state as needed so the pool will shrink
1014 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001015 *
1016 * By placing pages into the surplus state independent of the
1017 * overcommit value, we are allowing the surplus pool size to
1018 * exceed overcommit. There are few sane options here. Since
1019 * alloc_buddy_huge_page() is checking the global counter,
1020 * though, we'll note that we're not allowed to exceed surplus
1021 * and won't grow the pool anywhere else. Not until one of the
1022 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07001023 */
Andi Kleena5516432008-07-23 21:27:41 -07001024 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07001025 min_count = max(count, min_count);
Andi Kleena5516432008-07-23 21:27:41 -07001026 try_to_free_low(h, min_count);
1027 while (min_count < persistent_huge_pages(h)) {
1028 struct page *page = dequeue_huge_page(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (!page)
1030 break;
Andi Kleena5516432008-07-23 21:27:41 -07001031 update_and_free_page(h, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
Andi Kleena5516432008-07-23 21:27:41 -07001033 while (count < persistent_huge_pages(h)) {
1034 if (!adjust_pool_surplus(h, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07001035 break;
1036 }
1037out:
Andi Kleena5516432008-07-23 21:27:41 -07001038 ret = persistent_huge_pages(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001043#define HSTATE_ATTR_RO(_name) \
1044 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1045
1046#define HSTATE_ATTR(_name) \
1047 static struct kobj_attribute _name##_attr = \
1048 __ATTR(_name, 0644, _name##_show, _name##_store)
1049
1050static struct kobject *hugepages_kobj;
1051static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
1052
1053static struct hstate *kobj_to_hstate(struct kobject *kobj)
1054{
1055 int i;
1056 for (i = 0; i < HUGE_MAX_HSTATE; i++)
1057 if (hstate_kobjs[i] == kobj)
1058 return &hstates[i];
1059 BUG();
1060 return NULL;
1061}
1062
1063static ssize_t nr_hugepages_show(struct kobject *kobj,
1064 struct kobj_attribute *attr, char *buf)
1065{
1066 struct hstate *h = kobj_to_hstate(kobj);
1067 return sprintf(buf, "%lu\n", h->nr_huge_pages);
1068}
1069static ssize_t nr_hugepages_store(struct kobject *kobj,
1070 struct kobj_attribute *attr, const char *buf, size_t count)
1071{
1072 int err;
1073 unsigned long input;
1074 struct hstate *h = kobj_to_hstate(kobj);
1075
1076 err = strict_strtoul(buf, 10, &input);
1077 if (err)
1078 return 0;
1079
1080 h->max_huge_pages = set_max_huge_pages(h, input);
1081
1082 return count;
1083}
1084HSTATE_ATTR(nr_hugepages);
1085
1086static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
1087 struct kobj_attribute *attr, char *buf)
1088{
1089 struct hstate *h = kobj_to_hstate(kobj);
1090 return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
1091}
1092static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
1093 struct kobj_attribute *attr, const char *buf, size_t count)
1094{
1095 int err;
1096 unsigned long input;
1097 struct hstate *h = kobj_to_hstate(kobj);
1098
1099 err = strict_strtoul(buf, 10, &input);
1100 if (err)
1101 return 0;
1102
1103 spin_lock(&hugetlb_lock);
1104 h->nr_overcommit_huge_pages = input;
1105 spin_unlock(&hugetlb_lock);
1106
1107 return count;
1108}
1109HSTATE_ATTR(nr_overcommit_hugepages);
1110
1111static ssize_t free_hugepages_show(struct kobject *kobj,
1112 struct kobj_attribute *attr, char *buf)
1113{
1114 struct hstate *h = kobj_to_hstate(kobj);
1115 return sprintf(buf, "%lu\n", h->free_huge_pages);
1116}
1117HSTATE_ATTR_RO(free_hugepages);
1118
1119static ssize_t resv_hugepages_show(struct kobject *kobj,
1120 struct kobj_attribute *attr, char *buf)
1121{
1122 struct hstate *h = kobj_to_hstate(kobj);
1123 return sprintf(buf, "%lu\n", h->resv_huge_pages);
1124}
1125HSTATE_ATTR_RO(resv_hugepages);
1126
1127static ssize_t surplus_hugepages_show(struct kobject *kobj,
1128 struct kobj_attribute *attr, char *buf)
1129{
1130 struct hstate *h = kobj_to_hstate(kobj);
1131 return sprintf(buf, "%lu\n", h->surplus_huge_pages);
1132}
1133HSTATE_ATTR_RO(surplus_hugepages);
1134
1135static struct attribute *hstate_attrs[] = {
1136 &nr_hugepages_attr.attr,
1137 &nr_overcommit_hugepages_attr.attr,
1138 &free_hugepages_attr.attr,
1139 &resv_hugepages_attr.attr,
1140 &surplus_hugepages_attr.attr,
1141 NULL,
1142};
1143
1144static struct attribute_group hstate_attr_group = {
1145 .attrs = hstate_attrs,
1146};
1147
1148static int __init hugetlb_sysfs_add_hstate(struct hstate *h)
1149{
1150 int retval;
1151
1152 hstate_kobjs[h - hstates] = kobject_create_and_add(h->name,
1153 hugepages_kobj);
1154 if (!hstate_kobjs[h - hstates])
1155 return -ENOMEM;
1156
1157 retval = sysfs_create_group(hstate_kobjs[h - hstates],
1158 &hstate_attr_group);
1159 if (retval)
1160 kobject_put(hstate_kobjs[h - hstates]);
1161
1162 return retval;
1163}
1164
1165static void __init hugetlb_sysfs_init(void)
1166{
1167 struct hstate *h;
1168 int err;
1169
1170 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
1171 if (!hugepages_kobj)
1172 return;
1173
1174 for_each_hstate(h) {
1175 err = hugetlb_sysfs_add_hstate(h);
1176 if (err)
1177 printk(KERN_ERR "Hugetlb: Unable to add hstate %s",
1178 h->name);
1179 }
1180}
1181
1182static void __exit hugetlb_exit(void)
1183{
1184 struct hstate *h;
1185
1186 for_each_hstate(h) {
1187 kobject_put(hstate_kobjs[h - hstates]);
1188 }
1189
1190 kobject_put(hugepages_kobj);
1191}
1192module_exit(hugetlb_exit);
1193
1194static int __init hugetlb_init(void)
1195{
1196 BUILD_BUG_ON(HPAGE_SHIFT == 0);
1197
1198 if (!size_to_hstate(HPAGE_SIZE)) {
1199 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
1200 parsed_hstate->max_huge_pages = default_hstate_max_huge_pages;
1201 }
1202 default_hstate_idx = size_to_hstate(HPAGE_SIZE) - hstates;
1203
1204 hugetlb_init_hstates();
1205
1206 report_hugepages();
1207
1208 hugetlb_sysfs_init();
1209
1210 return 0;
1211}
1212module_init(hugetlb_init);
1213
1214/* Should be called on processing a hugepagesz=... option */
1215void __init hugetlb_add_hstate(unsigned order)
1216{
1217 struct hstate *h;
1218 if (size_to_hstate(PAGE_SIZE << order)) {
1219 printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
1220 return;
1221 }
1222 BUG_ON(max_hstate >= HUGE_MAX_HSTATE);
1223 BUG_ON(order == 0);
1224 h = &hstates[max_hstate++];
1225 h->order = order;
1226 h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
1227 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
1228 huge_page_size(h)/1024);
1229 hugetlb_init_one_hstate(h);
1230 parsed_hstate = h;
1231}
1232
1233static int __init hugetlb_setup(char *s)
1234{
1235 unsigned long *mhp;
1236
1237 /*
1238 * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
1239 * so this hugepages= parameter goes to the "default hstate".
1240 */
1241 if (!max_hstate)
1242 mhp = &default_hstate_max_huge_pages;
1243 else
1244 mhp = &parsed_hstate->max_huge_pages;
1245
1246 if (sscanf(s, "%lu", mhp) <= 0)
1247 *mhp = 0;
1248
1249 return 1;
1250}
1251__setup("hugepages=", hugetlb_setup);
1252
1253static unsigned int cpuset_mems_nr(unsigned int *array)
1254{
1255 int node;
1256 unsigned int nr = 0;
1257
1258 for_each_node_mask(node, cpuset_current_mems_allowed)
1259 nr += array[node];
1260
1261 return nr;
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264int hugetlb_sysctl_handler(struct ctl_table *table, int write,
1265 struct file *file, void __user *buffer,
1266 size_t *length, loff_t *ppos)
1267{
Andi Kleene5ff2152008-07-23 21:27:42 -07001268 struct hstate *h = &default_hstate;
1269 unsigned long tmp;
1270
1271 if (!write)
1272 tmp = h->max_huge_pages;
1273
1274 table->data = &tmp;
1275 table->maxlen = sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Andi Kleene5ff2152008-07-23 21:27:42 -07001277
1278 if (write)
1279 h->max_huge_pages = set_max_huge_pages(h, tmp);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 0;
1282}
Mel Gorman396faf02007-07-17 04:03:13 -07001283
1284int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
1285 struct file *file, void __user *buffer,
1286 size_t *length, loff_t *ppos)
1287{
1288 proc_dointvec(table, write, file, buffer, length, ppos);
1289 if (hugepages_treat_as_movable)
1290 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
1291 else
1292 htlb_alloc_mask = GFP_HIGHUSER;
1293 return 0;
1294}
1295
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001296int hugetlb_overcommit_handler(struct ctl_table *table, int write,
1297 struct file *file, void __user *buffer,
1298 size_t *length, loff_t *ppos)
1299{
Andi Kleena5516432008-07-23 21:27:41 -07001300 struct hstate *h = &default_hstate;
Andi Kleene5ff2152008-07-23 21:27:42 -07001301 unsigned long tmp;
1302
1303 if (!write)
1304 tmp = h->nr_overcommit_huge_pages;
1305
1306 table->data = &tmp;
1307 table->maxlen = sizeof(unsigned long);
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001308 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Andi Kleene5ff2152008-07-23 21:27:42 -07001309
1310 if (write) {
1311 spin_lock(&hugetlb_lock);
1312 h->nr_overcommit_huge_pages = tmp;
1313 spin_unlock(&hugetlb_lock);
1314 }
1315
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001316 return 0;
1317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319#endif /* CONFIG_SYSCTL */
1320
1321int hugetlb_report_meminfo(char *buf)
1322{
Andi Kleena5516432008-07-23 21:27:41 -07001323 struct hstate *h = &default_hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return sprintf(buf,
1325 "HugePages_Total: %5lu\n"
1326 "HugePages_Free: %5lu\n"
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001327 "HugePages_Rsvd: %5lu\n"
Adam Litke7893d1d2007-10-16 01:26:18 -07001328 "HugePages_Surp: %5lu\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 "Hugepagesize: %5lu kB\n",
Andi Kleena5516432008-07-23 21:27:41 -07001330 h->nr_huge_pages,
1331 h->free_huge_pages,
1332 h->resv_huge_pages,
1333 h->surplus_huge_pages,
1334 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337int hugetlb_report_node_meminfo(int nid, char *buf)
1338{
Andi Kleena5516432008-07-23 21:27:41 -07001339 struct hstate *h = &default_hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return sprintf(buf,
1341 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07001342 "Node %d HugePages_Free: %5u\n"
1343 "Node %d HugePages_Surp: %5u\n",
Andi Kleena5516432008-07-23 21:27:41 -07001344 nid, h->nr_huge_pages_node[nid],
1345 nid, h->free_huge_pages_node[nid],
1346 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
1350unsigned long hugetlb_total_pages(void)
1351{
Andi Kleena5516432008-07-23 21:27:41 -07001352 struct hstate *h = &default_hstate;
1353 return h->nr_huge_pages * pages_per_huge_page(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Andi Kleena5516432008-07-23 21:27:41 -07001356static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001357{
1358 int ret = -ENOMEM;
1359
1360 spin_lock(&hugetlb_lock);
1361 /*
1362 * When cpuset is configured, it breaks the strict hugetlb page
1363 * reservation as the accounting is done on a global variable. Such
1364 * reservation is completely rubbish in the presence of cpuset because
1365 * the reservation is not checked against page availability for the
1366 * current cpuset. Application can still potentially OOM'ed by kernel
1367 * with lack of free htlb page in cpuset that the task is in.
1368 * Attempt to enforce strict accounting with cpuset is almost
1369 * impossible (or too ugly) because cpuset is too fluid that
1370 * task or memory node can be dynamically moved between cpusets.
1371 *
1372 * The change of semantics for shared hugetlb mapping with cpuset is
1373 * undesirable. However, in order to preserve some of the semantics,
1374 * we fall back to check against current free page availability as
1375 * a best attempt and hopefully to minimize the impact of changing
1376 * semantics that cpuset has.
1377 */
1378 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07001379 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001380 goto out;
1381
Andi Kleena5516432008-07-23 21:27:41 -07001382 if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
1383 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001384 goto out;
1385 }
1386 }
1387
1388 ret = 0;
1389 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07001390 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001391
1392out:
1393 spin_unlock(&hugetlb_lock);
1394 return ret;
1395}
1396
Andy Whitcroft84afd992008-07-23 21:27:32 -07001397static void hugetlb_vm_op_open(struct vm_area_struct *vma)
1398{
1399 struct resv_map *reservations = vma_resv_map(vma);
1400
1401 /*
1402 * This new VMA should share its siblings reservation map if present.
1403 * The VMA will only ever have a valid reservation map pointer where
1404 * it is being copied for another still existing VMA. As that VMA
1405 * has a reference to the reservation map it cannot dissappear until
1406 * after this open call completes. It is therefore safe to take a
1407 * new reference here without additional locking.
1408 */
1409 if (reservations)
1410 kref_get(&reservations->refs);
1411}
1412
Mel Gormana1e78772008-07-23 21:27:23 -07001413static void hugetlb_vm_op_close(struct vm_area_struct *vma)
1414{
Andi Kleena5516432008-07-23 21:27:41 -07001415 struct hstate *h = hstate_vma(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001416 struct resv_map *reservations = vma_resv_map(vma);
1417 unsigned long reserve;
1418 unsigned long start;
1419 unsigned long end;
1420
1421 if (reservations) {
Andi Kleena5516432008-07-23 21:27:41 -07001422 start = vma_hugecache_offset(h, vma, vma->vm_start);
1423 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001424
1425 reserve = (end - start) -
1426 region_count(&reservations->regions, start, end);
1427
1428 kref_put(&reservations->refs, resv_map_release);
1429
1430 if (reserve)
Andi Kleena5516432008-07-23 21:27:41 -07001431 hugetlb_acct_memory(h, -reserve);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001432 }
Mel Gormana1e78772008-07-23 21:27:23 -07001433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435/*
1436 * We cannot handle pagefaults against hugetlb pages at all. They cause
1437 * handle_mm_fault() to try to instantiate regular-sized pages in the
1438 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
1439 * this far.
1440 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001441static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
1447struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07001448 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07001449 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07001450 .close = hugetlb_vm_op_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451};
1452
David Gibson1e8f8892006-01-06 00:10:44 -08001453static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
1454 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07001455{
1456 pte_t entry;
1457
David Gibson1e8f8892006-01-06 00:10:44 -08001458 if (writable) {
David Gibson63551ae2005-06-21 17:14:44 -07001459 entry =
1460 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1461 } else {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001462 entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07001463 }
1464 entry = pte_mkyoung(entry);
1465 entry = pte_mkhuge(entry);
1466
1467 return entry;
1468}
1469
David Gibson1e8f8892006-01-06 00:10:44 -08001470static void set_huge_ptep_writable(struct vm_area_struct *vma,
1471 unsigned long address, pte_t *ptep)
1472{
1473 pte_t entry;
1474
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001475 entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
1476 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001477 update_mmu_cache(vma, address, entry);
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001478 }
David Gibson1e8f8892006-01-06 00:10:44 -08001479}
1480
1481
David Gibson63551ae2005-06-21 17:14:44 -07001482int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
1483 struct vm_area_struct *vma)
1484{
1485 pte_t *src_pte, *dst_pte, entry;
1486 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07001487 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08001488 int cow;
Andi Kleena5516432008-07-23 21:27:41 -07001489 struct hstate *h = hstate_vma(vma);
1490 unsigned long sz = huge_page_size(h);
David Gibson1e8f8892006-01-06 00:10:44 -08001491
1492 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07001493
Andi Kleena5516432008-07-23 21:27:41 -07001494 for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
Hugh Dickinsc74df322005-10-29 18:16:23 -07001495 src_pte = huge_pte_offset(src, addr);
1496 if (!src_pte)
1497 continue;
Andi Kleena5516432008-07-23 21:27:41 -07001498 dst_pte = huge_pte_alloc(dst, addr, sz);
David Gibson63551ae2005-06-21 17:14:44 -07001499 if (!dst_pte)
1500 goto nomem;
Larry Woodmanc5c99422008-01-24 05:49:25 -08001501
1502 /* If the pagetables are shared don't copy or take references */
1503 if (dst_pte == src_pte)
1504 continue;
1505
Hugh Dickinsc74df322005-10-29 18:16:23 -07001506 spin_lock(&dst->page_table_lock);
Nick Piggin46478752008-06-05 22:45:57 -07001507 spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001508 if (!huge_pte_none(huge_ptep_get(src_pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001509 if (cow)
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001510 huge_ptep_set_wrprotect(src, addr, src_pte);
1511 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07001512 ptepage = pte_page(entry);
1513 get_page(ptepage);
Hugh Dickins1c598272005-10-19 21:23:43 -07001514 set_huge_pte_at(dst, addr, dst_pte, entry);
1515 }
1516 spin_unlock(&src->page_table_lock);
Hugh Dickinsc74df322005-10-29 18:16:23 -07001517 spin_unlock(&dst->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001518 }
1519 return 0;
1520
1521nomem:
1522 return -ENOMEM;
1523}
1524
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001525void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001526 unsigned long end, struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07001527{
1528 struct mm_struct *mm = vma->vm_mm;
1529 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07001530 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07001531 pte_t pte;
1532 struct page *page;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001533 struct page *tmp;
Andi Kleena5516432008-07-23 21:27:41 -07001534 struct hstate *h = hstate_vma(vma);
1535 unsigned long sz = huge_page_size(h);
1536
Chen, Kenneth Wc0a499c2006-12-06 20:31:39 -08001537 /*
1538 * A page gathering list, protected by per file i_mmap_lock. The
1539 * lock is used to avoid list corruption from multiple unmapping
1540 * of the same page since we are using page->lru.
1541 */
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001542 LIST_HEAD(page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001543
1544 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07001545 BUG_ON(start & ~huge_page_mask(h));
1546 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07001547
Hugh Dickins508034a2005-10-29 18:16:30 -07001548 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001549 for (address = start; address < end; address += sz) {
David Gibsonc7546f82005-08-05 11:59:35 -07001550 ptep = huge_pte_offset(mm, address);
Adam Litke4c887262005-10-29 18:16:46 -07001551 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07001552 continue;
1553
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001554 if (huge_pmd_unshare(mm, &address, ptep))
1555 continue;
1556
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001557 /*
1558 * If a reference page is supplied, it is because a specific
1559 * page is being unmapped, not a range. Ensure the page we
1560 * are about to unmap is the actual page of interest.
1561 */
1562 if (ref_page) {
1563 pte = huge_ptep_get(ptep);
1564 if (huge_pte_none(pte))
1565 continue;
1566 page = pte_page(pte);
1567 if (page != ref_page)
1568 continue;
1569
1570 /*
1571 * Mark the VMA as having unmapped its page so that
1572 * future faults in this VMA will fail rather than
1573 * looking like data was lost
1574 */
1575 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
1576 }
1577
David Gibsonc7546f82005-08-05 11:59:35 -07001578 pte = huge_ptep_get_and_clear(mm, address, ptep);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001579 if (huge_pte_none(pte))
David Gibson63551ae2005-06-21 17:14:44 -07001580 continue;
David Gibsonc7546f82005-08-05 11:59:35 -07001581
David Gibson63551ae2005-06-21 17:14:44 -07001582 page = pte_page(pte);
Ken Chen6649a382007-02-08 14:20:27 -08001583 if (pte_dirty(pte))
1584 set_page_dirty(page);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001585 list_add(&page->lru, &page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 spin_unlock(&mm->page_table_lock);
Hugh Dickins508034a2005-10-29 18:16:30 -07001588 flush_tlb_range(vma, start, end);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001589 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1590 list_del(&page->lru);
1591 put_page(page);
1592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
David Gibson63551ae2005-06-21 17:14:44 -07001594
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001595void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001596 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001597{
Andi Kleena137e1c2008-07-23 21:27:43 -07001598 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
1599 __unmap_hugepage_range(vma, start, end, ref_page);
1600 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001601}
1602
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001603/*
1604 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1605 * mappping it owns the reserve page for. The intention is to unmap the page
1606 * from other VMAs and let the children be SIGKILLed if they are faulting the
1607 * same region.
1608 */
1609int unmap_ref_private(struct mm_struct *mm,
1610 struct vm_area_struct *vma,
1611 struct page *page,
1612 unsigned long address)
1613{
1614 struct vm_area_struct *iter_vma;
1615 struct address_space *mapping;
1616 struct prio_tree_iter iter;
1617 pgoff_t pgoff;
1618
1619 /*
1620 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1621 * from page cache lookup which is in HPAGE_SIZE units.
1622 */
1623 address = address & huge_page_mask(hstate_vma(vma));
1624 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
1625 + (vma->vm_pgoff >> PAGE_SHIFT);
1626 mapping = (struct address_space *)page_private(page);
1627
1628 vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1629 /* Do not unmap the current VMA */
1630 if (iter_vma == vma)
1631 continue;
1632
1633 /*
1634 * Unmap the page from other VMAs without their own reserves.
1635 * They get marked to be SIGKILLed if they fault in these
1636 * areas. This is because a future no-page fault on this VMA
1637 * could insert a zeroed page instead of the data existing
1638 * from the time of fork. This would look like data corruption
1639 */
1640 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
1641 unmap_hugepage_range(iter_vma,
1642 address, address + HPAGE_SIZE,
1643 page);
1644 }
1645
1646 return 1;
1647}
1648
David Gibson1e8f8892006-01-06 00:10:44 -08001649static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001650 unsigned long address, pte_t *ptep, pte_t pte,
1651 struct page *pagecache_page)
David Gibson1e8f8892006-01-06 00:10:44 -08001652{
Andi Kleena5516432008-07-23 21:27:41 -07001653 struct hstate *h = hstate_vma(vma);
David Gibson1e8f8892006-01-06 00:10:44 -08001654 struct page *old_page, *new_page;
David Gibson79ac6ba2006-03-22 00:08:51 -08001655 int avoidcopy;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001656 int outside_reserve = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001657
1658 old_page = pte_page(pte);
1659
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001660retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08001661 /* If no-one else is actually using this page, avoid the copy
1662 * and just make the page writable */
1663 avoidcopy = (page_count(old_page) == 1);
1664 if (avoidcopy) {
1665 set_huge_ptep_writable(vma, address, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07001666 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001667 }
1668
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001669 /*
1670 * If the process that created a MAP_PRIVATE mapping is about to
1671 * perform a COW due to a shared page count, attempt to satisfy
1672 * the allocation without using the existing reserves. The pagecache
1673 * page is used to determine if the reserve at this address was
1674 * consumed or not. If reserves were used, a partial faulted mapping
1675 * at the time of fork() could consume its reserves on COW instead
1676 * of the full address range.
1677 */
1678 if (!(vma->vm_flags & VM_SHARED) &&
1679 is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
1680 old_page != pagecache_page)
1681 outside_reserve = 1;
1682
David Gibson1e8f8892006-01-06 00:10:44 -08001683 page_cache_get(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001684 new_page = alloc_huge_page(vma, address, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08001685
Adam Litke2fc39ce2007-11-14 16:59:39 -08001686 if (IS_ERR(new_page)) {
David Gibson1e8f8892006-01-06 00:10:44 -08001687 page_cache_release(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001688
1689 /*
1690 * If a process owning a MAP_PRIVATE mapping fails to COW,
1691 * it is due to references held by a child and an insufficient
1692 * huge page pool. To guarantee the original mappers
1693 * reliability, unmap the page from child processes. The child
1694 * may get SIGKILLed if it later faults.
1695 */
1696 if (outside_reserve) {
1697 BUG_ON(huge_pte_none(pte));
1698 if (unmap_ref_private(mm, vma, old_page, address)) {
1699 BUG_ON(page_count(old_page) != 1);
1700 BUG_ON(huge_pte_none(pte));
1701 goto retry_avoidcopy;
1702 }
1703 WARN_ON_ONCE(1);
1704 }
1705
Adam Litke2fc39ce2007-11-14 16:59:39 -08001706 return -PTR_ERR(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001707 }
1708
1709 spin_unlock(&mm->page_table_lock);
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00001710 copy_huge_page(new_page, old_page, address, vma);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001711 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001712 spin_lock(&mm->page_table_lock);
1713
Andi Kleena5516432008-07-23 21:27:41 -07001714 ptep = huge_pte_offset(mm, address & huge_page_mask(h));
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001715 if (likely(pte_same(huge_ptep_get(ptep), pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001716 /* Break COW */
Gerald Schaefer8fe627e2008-04-28 02:13:28 -07001717 huge_ptep_clear_flush(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08001718 set_huge_pte_at(mm, address, ptep,
1719 make_huge_pte(vma, new_page, 1));
1720 /* Make the old page be freed below */
1721 new_page = old_page;
1722 }
1723 page_cache_release(new_page);
1724 page_cache_release(old_page);
Nick Piggin83c54072007-07-19 01:47:05 -07001725 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001726}
1727
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001728/* Return the pagecache page at a given address within a VMA */
Andi Kleena5516432008-07-23 21:27:41 -07001729static struct page *hugetlbfs_pagecache_page(struct hstate *h,
1730 struct vm_area_struct *vma, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001731{
1732 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001733 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001734
1735 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07001736 idx = vma_hugecache_offset(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001737
1738 return find_lock_page(mapping, idx);
1739}
1740
Robert P. J. Daya1ed3dd2007-07-17 04:03:33 -07001741static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
David Gibson1e8f8892006-01-06 00:10:44 -08001742 unsigned long address, pte_t *ptep, int write_access)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001743{
Andi Kleena5516432008-07-23 21:27:41 -07001744 struct hstate *h = hstate_vma(vma);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001745 int ret = VM_FAULT_SIGBUS;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001746 pgoff_t idx;
Adam Litke4c887262005-10-29 18:16:46 -07001747 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07001748 struct page *page;
1749 struct address_space *mapping;
David Gibson1e8f8892006-01-06 00:10:44 -08001750 pte_t new_pte;
Adam Litke4c887262005-10-29 18:16:46 -07001751
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001752 /*
1753 * Currently, we are forced to kill the process in the event the
1754 * original mapper has unmapped pages from the child due to a failed
1755 * COW. Warn that such a situation has occured as it may not be obvious
1756 */
1757 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
1758 printk(KERN_WARNING
1759 "PID %d killed due to inadequate hugepage pool\n",
1760 current->pid);
1761 return ret;
1762 }
1763
Adam Litke4c887262005-10-29 18:16:46 -07001764 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07001765 idx = vma_hugecache_offset(h, vma, address);
Adam Litke4c887262005-10-29 18:16:46 -07001766
1767 /*
1768 * Use page lock to guard against racing truncation
1769 * before we get page_table_lock.
1770 */
Christoph Lameter6bda6662006-01-06 00:10:49 -08001771retry:
1772 page = find_lock_page(mapping, idx);
1773 if (!page) {
Andi Kleena5516432008-07-23 21:27:41 -07001774 size = i_size_read(mapping->host) >> huge_page_shift(h);
Hugh Dickinsebed4bf2006-10-28 10:38:43 -07001775 if (idx >= size)
1776 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001777 page = alloc_huge_page(vma, address, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08001778 if (IS_ERR(page)) {
1779 ret = -PTR_ERR(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001780 goto out;
1781 }
Andi Kleena5516432008-07-23 21:27:41 -07001782 clear_huge_page(page, address, huge_page_size(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08001783 __SetPageUptodate(page);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001784
Christoph Lameter6bda6662006-01-06 00:10:49 -08001785 if (vma->vm_flags & VM_SHARED) {
1786 int err;
Ken Chen45c682a2007-11-14 16:59:44 -08001787 struct inode *inode = mapping->host;
Christoph Lameter6bda6662006-01-06 00:10:49 -08001788
1789 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
1790 if (err) {
1791 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001792 if (err == -EEXIST)
1793 goto retry;
1794 goto out;
1795 }
Ken Chen45c682a2007-11-14 16:59:44 -08001796
1797 spin_lock(&inode->i_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001798 inode->i_blocks += blocks_per_huge_page(h);
Ken Chen45c682a2007-11-14 16:59:44 -08001799 spin_unlock(&inode->i_lock);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001800 } else
1801 lock_page(page);
1802 }
David Gibson1e8f8892006-01-06 00:10:44 -08001803
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001804 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001805 size = i_size_read(mapping->host) >> huge_page_shift(h);
Adam Litke4c887262005-10-29 18:16:46 -07001806 if (idx >= size)
1807 goto backout;
1808
Nick Piggin83c54072007-07-19 01:47:05 -07001809 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001810 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07001811 goto backout;
1812
David Gibson1e8f8892006-01-06 00:10:44 -08001813 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
1814 && (vma->vm_flags & VM_SHARED)));
1815 set_huge_pte_at(mm, address, ptep, new_pte);
1816
1817 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1818 /* Optimization, do the COW without a second fault */
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001819 ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
David Gibson1e8f8892006-01-06 00:10:44 -08001820 }
1821
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001822 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001823 unlock_page(page);
1824out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001825 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07001826
1827backout:
1828 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001829 unlock_page(page);
1830 put_page(page);
1831 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001832}
1833
Adam Litke86e52162006-01-06 00:10:43 -08001834int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1835 unsigned long address, int write_access)
1836{
1837 pte_t *ptep;
1838 pte_t entry;
David Gibson1e8f8892006-01-06 00:10:44 -08001839 int ret;
David Gibson3935baa2006-03-22 00:08:53 -08001840 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
Andi Kleena5516432008-07-23 21:27:41 -07001841 struct hstate *h = hstate_vma(vma);
Adam Litke86e52162006-01-06 00:10:43 -08001842
Andi Kleena5516432008-07-23 21:27:41 -07001843 ptep = huge_pte_alloc(mm, address, huge_page_size(h));
Adam Litke86e52162006-01-06 00:10:43 -08001844 if (!ptep)
1845 return VM_FAULT_OOM;
1846
David Gibson3935baa2006-03-22 00:08:53 -08001847 /*
1848 * Serialize hugepage allocation and instantiation, so that we don't
1849 * get spurious allocation failures if two CPUs race to instantiate
1850 * the same page in the page cache.
1851 */
1852 mutex_lock(&hugetlb_instantiation_mutex);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001853 entry = huge_ptep_get(ptep);
1854 if (huge_pte_none(entry)) {
David Gibson3935baa2006-03-22 00:08:53 -08001855 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
1856 mutex_unlock(&hugetlb_instantiation_mutex);
1857 return ret;
1858 }
Adam Litke86e52162006-01-06 00:10:43 -08001859
Nick Piggin83c54072007-07-19 01:47:05 -07001860 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001861
1862 spin_lock(&mm->page_table_lock);
1863 /* Check for a racing update before calling hugetlb_cow */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001864 if (likely(pte_same(entry, huge_ptep_get(ptep))))
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001865 if (write_access && !pte_write(entry)) {
1866 struct page *page;
Andi Kleena5516432008-07-23 21:27:41 -07001867 page = hugetlbfs_pagecache_page(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001868 ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
1869 if (page) {
1870 unlock_page(page);
1871 put_page(page);
1872 }
1873 }
David Gibson1e8f8892006-01-06 00:10:44 -08001874 spin_unlock(&mm->page_table_lock);
David Gibson3935baa2006-03-22 00:08:53 -08001875 mutex_unlock(&hugetlb_instantiation_mutex);
David Gibson1e8f8892006-01-06 00:10:44 -08001876
1877 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08001878}
1879
David Gibson63551ae2005-06-21 17:14:44 -07001880int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
1881 struct page **pages, struct vm_area_struct **vmas,
Adam Litke5b23dbe2007-11-14 16:59:33 -08001882 unsigned long *position, int *length, int i,
1883 int write)
David Gibson63551ae2005-06-21 17:14:44 -07001884{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001885 unsigned long pfn_offset;
1886 unsigned long vaddr = *position;
David Gibson63551ae2005-06-21 17:14:44 -07001887 int remainder = *length;
Andi Kleena5516432008-07-23 21:27:41 -07001888 struct hstate *h = hstate_vma(vma);
David Gibson63551ae2005-06-21 17:14:44 -07001889
Hugh Dickins1c598272005-10-19 21:23:43 -07001890 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001891 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07001892 pte_t *pte;
1893 struct page *page;
1894
1895 /*
1896 * Some archs (sparc64, sh*) have multiple pte_ts to
1897 * each hugepage. We have to make * sure we get the
1898 * first, for the page indexing below to work.
1899 */
Andi Kleena5516432008-07-23 21:27:41 -07001900 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
Adam Litke4c887262005-10-29 18:16:46 -07001901
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001902 if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
1903 (write && !pte_write(huge_ptep_get(pte)))) {
Adam Litke4c887262005-10-29 18:16:46 -07001904 int ret;
1905
1906 spin_unlock(&mm->page_table_lock);
Adam Litke5b23dbe2007-11-14 16:59:33 -08001907 ret = hugetlb_fault(mm, vma, vaddr, write);
Adam Litke4c887262005-10-29 18:16:46 -07001908 spin_lock(&mm->page_table_lock);
Adam Litkea89182c2007-08-22 14:01:51 -07001909 if (!(ret & VM_FAULT_ERROR))
Adam Litke4c887262005-10-29 18:16:46 -07001910 continue;
1911
1912 remainder = 0;
1913 if (!i)
1914 i = -EFAULT;
1915 break;
1916 }
David Gibson63551ae2005-06-21 17:14:44 -07001917
Andi Kleena5516432008-07-23 21:27:41 -07001918 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001919 page = pte_page(huge_ptep_get(pte));
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001920same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001921 if (pages) {
1922 get_page(page);
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001923 pages[i] = page + pfn_offset;
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001924 }
David Gibson63551ae2005-06-21 17:14:44 -07001925
1926 if (vmas)
1927 vmas[i] = vma;
1928
1929 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001930 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07001931 --remainder;
1932 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001933 if (vaddr < vma->vm_end && remainder &&
Andi Kleena5516432008-07-23 21:27:41 -07001934 pfn_offset < pages_per_huge_page(h)) {
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001935 /*
1936 * We use pfn_offset to avoid touching the pageframes
1937 * of this compound page.
1938 */
1939 goto same_page;
1940 }
David Gibson63551ae2005-06-21 17:14:44 -07001941 }
Hugh Dickins1c598272005-10-19 21:23:43 -07001942 spin_unlock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001943 *length = remainder;
1944 *position = vaddr;
1945
1946 return i;
1947}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001948
1949void hugetlb_change_protection(struct vm_area_struct *vma,
1950 unsigned long address, unsigned long end, pgprot_t newprot)
1951{
1952 struct mm_struct *mm = vma->vm_mm;
1953 unsigned long start = address;
1954 pte_t *ptep;
1955 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07001956 struct hstate *h = hstate_vma(vma);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001957
1958 BUG_ON(address >= end);
1959 flush_cache_range(vma, address, end);
1960
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001961 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001962 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001963 for (; address < end; address += huge_page_size(h)) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001964 ptep = huge_pte_offset(mm, address);
1965 if (!ptep)
1966 continue;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001967 if (huge_pmd_unshare(mm, &address, ptep))
1968 continue;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001969 if (!huge_pte_none(huge_ptep_get(ptep))) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001970 pte = huge_ptep_get_and_clear(mm, address, ptep);
1971 pte = pte_mkhuge(pte_modify(pte, newprot));
1972 set_huge_pte_at(mm, address, ptep, pte);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001973 }
1974 }
1975 spin_unlock(&mm->page_table_lock);
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001976 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001977
1978 flush_tlb_range(vma, start, end);
1979}
1980
Mel Gormana1e78772008-07-23 21:27:23 -07001981int hugetlb_reserve_pages(struct inode *inode,
1982 long from, long to,
1983 struct vm_area_struct *vma)
Adam Litkee4e574b2007-10-16 01:26:19 -07001984{
1985 long ret, chg;
Andi Kleena5516432008-07-23 21:27:41 -07001986 struct hstate *h = hstate_inode(inode);
Adam Litkee4e574b2007-10-16 01:26:19 -07001987
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001988 if (vma && vma->vm_flags & VM_NORESERVE)
1989 return 0;
1990
Mel Gormana1e78772008-07-23 21:27:23 -07001991 /*
1992 * Shared mappings base their reservation on the number of pages that
1993 * are already allocated on behalf of the file. Private mappings need
1994 * to reserve the full area even if read-only as mprotect() may be
1995 * called to make the mapping read-write. Assume !vma is a shm mapping
1996 */
1997 if (!vma || vma->vm_flags & VM_SHARED)
1998 chg = region_chg(&inode->i_mapping->private_list, from, to);
1999 else {
Andy Whitcroft84afd992008-07-23 21:27:32 -07002000 struct resv_map *resv_map = resv_map_alloc();
2001 if (!resv_map)
2002 return -ENOMEM;
2003
Mel Gormana1e78772008-07-23 21:27:23 -07002004 chg = to - from;
Andy Whitcroft84afd992008-07-23 21:27:32 -07002005
2006 set_vma_resv_map(vma, resv_map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002007 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
Mel Gormana1e78772008-07-23 21:27:23 -07002008 }
2009
Adam Litkee4e574b2007-10-16 01:26:19 -07002010 if (chg < 0)
2011 return chg;
Ken Chen8a630112007-05-09 02:33:34 -07002012
Adam Litke90d8b7e2007-11-14 16:59:42 -08002013 if (hugetlb_get_quota(inode->i_mapping, chg))
2014 return -ENOSPC;
Andi Kleena5516432008-07-23 21:27:41 -07002015 ret = hugetlb_acct_memory(h, chg);
Ken Chen68842c92008-01-14 00:55:19 -08002016 if (ret < 0) {
2017 hugetlb_put_quota(inode->i_mapping, chg);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07002018 return ret;
Ken Chen68842c92008-01-14 00:55:19 -08002019 }
Mel Gormana1e78772008-07-23 21:27:23 -07002020 if (!vma || vma->vm_flags & VM_SHARED)
2021 region_add(&inode->i_mapping->private_list, from, to);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07002022 return 0;
2023}
2024
2025void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
2026{
Andi Kleena5516432008-07-23 21:27:41 -07002027 struct hstate *h = hstate_inode(inode);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07002028 long chg = region_truncate(&inode->i_mapping->private_list, offset);
Ken Chen45c682a2007-11-14 16:59:44 -08002029
2030 spin_lock(&inode->i_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002031 inode->i_blocks -= blocks_per_huge_page(h);
Ken Chen45c682a2007-11-14 16:59:44 -08002032 spin_unlock(&inode->i_lock);
2033
Adam Litke90d8b7e2007-11-14 16:59:42 -08002034 hugetlb_put_quota(inode->i_mapping, (chg - freed));
Andi Kleena5516432008-07-23 21:27:41 -07002035 hugetlb_acct_memory(h, -(chg - freed));
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07002036}