blob: 82378d44a0c5aded21a84686ac56052d588cea37 [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;
Mel Gorman396faf02007-07-17 04:03:13 -070025static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
26unsigned long hugepages_treat_as_movable;
Andi Kleena5516432008-07-23 21:27:41 -070027
Andi Kleene5ff2152008-07-23 21:27:42 -070028static int max_hstate;
29unsigned int default_hstate_idx;
30struct hstate hstates[HUGE_MAX_HSTATE];
31
32/* for command line parsing */
33static struct hstate * __initdata parsed_hstate;
34static unsigned long __initdata default_hstate_max_huge_pages;
35
36#define for_each_hstate(h) \
37 for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
Mel Gorman396faf02007-07-17 04:03:13 -070038
David Gibson3935baa2006-03-22 00:08:53 -080039/*
40 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
41 */
42static DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080043
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -070044/*
Andy Whitcroft96822902008-07-23 21:27:29 -070045 * Region tracking -- allows tracking of reservations and instantiated pages
46 * across the pages in a mapping.
Andy Whitcroft84afd992008-07-23 21:27:32 -070047 *
48 * The region data structures are protected by a combination of the mmap_sem
49 * and the hugetlb_instantion_mutex. To access or modify a region the caller
50 * must either hold the mmap_sem for write, or the mmap_sem for read and
51 * the hugetlb_instantiation mutex:
52 *
53 * down_write(&mm->mmap_sem);
54 * or
55 * down_read(&mm->mmap_sem);
56 * mutex_lock(&hugetlb_instantiation_mutex);
Andy Whitcroft96822902008-07-23 21:27:29 -070057 */
58struct file_region {
59 struct list_head link;
60 long from;
61 long to;
62};
63
64static long region_add(struct list_head *head, long f, long t)
65{
66 struct file_region *rg, *nrg, *trg;
67
68 /* Locate the region we are either in or before. */
69 list_for_each_entry(rg, head, link)
70 if (f <= rg->to)
71 break;
72
73 /* Round our left edge to the current segment if it encloses us. */
74 if (f > rg->from)
75 f = rg->from;
76
77 /* Check for and consume any regions we now overlap with. */
78 nrg = rg;
79 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
80 if (&rg->link == head)
81 break;
82 if (rg->from > t)
83 break;
84
85 /* If this area reaches higher then extend our area to
86 * include it completely. If this is not the first area
87 * which we intend to reuse, free it. */
88 if (rg->to > t)
89 t = rg->to;
90 if (rg != nrg) {
91 list_del(&rg->link);
92 kfree(rg);
93 }
94 }
95 nrg->from = f;
96 nrg->to = t;
97 return 0;
98}
99
100static long region_chg(struct list_head *head, long f, long t)
101{
102 struct file_region *rg, *nrg;
103 long chg = 0;
104
105 /* Locate the region we are before or in. */
106 list_for_each_entry(rg, head, link)
107 if (f <= rg->to)
108 break;
109
110 /* If we are below the current region then a new region is required.
111 * Subtle, allocate a new region at the position but make it zero
112 * size such that we can guarantee to record the reservation. */
113 if (&rg->link == head || t < rg->from) {
114 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
115 if (!nrg)
116 return -ENOMEM;
117 nrg->from = f;
118 nrg->to = f;
119 INIT_LIST_HEAD(&nrg->link);
120 list_add(&nrg->link, rg->link.prev);
121
122 return t - f;
123 }
124
125 /* Round our left edge to the current segment if it encloses us. */
126 if (f > rg->from)
127 f = rg->from;
128 chg = t - f;
129
130 /* Check for and consume any regions we now overlap with. */
131 list_for_each_entry(rg, rg->link.prev, link) {
132 if (&rg->link == head)
133 break;
134 if (rg->from > t)
135 return chg;
136
137 /* We overlap with this area, if it extends futher than
138 * us then we must extend ourselves. Account for its
139 * existing reservation. */
140 if (rg->to > t) {
141 chg += rg->to - t;
142 t = rg->to;
143 }
144 chg -= rg->to - rg->from;
145 }
146 return chg;
147}
148
149static long region_truncate(struct list_head *head, long end)
150{
151 struct file_region *rg, *trg;
152 long chg = 0;
153
154 /* Locate the region we are either in or before. */
155 list_for_each_entry(rg, head, link)
156 if (end <= rg->to)
157 break;
158 if (&rg->link == head)
159 return 0;
160
161 /* If we are in the middle of a region then adjust it. */
162 if (end > rg->from) {
163 chg = rg->to - end;
164 rg->to = end;
165 rg = list_entry(rg->link.next, typeof(*rg), link);
166 }
167
168 /* Drop any remaining regions. */
169 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
170 if (&rg->link == head)
171 break;
172 chg += rg->to - rg->from;
173 list_del(&rg->link);
174 kfree(rg);
175 }
176 return chg;
177}
178
Andy Whitcroft84afd992008-07-23 21:27:32 -0700179static long region_count(struct list_head *head, long f, long t)
180{
181 struct file_region *rg;
182 long chg = 0;
183
184 /* Locate each segment we overlap with, and count that overlap. */
185 list_for_each_entry(rg, head, link) {
186 int seg_from;
187 int seg_to;
188
189 if (rg->to <= f)
190 continue;
191 if (rg->from >= t)
192 break;
193
194 seg_from = max(rg->from, f);
195 seg_to = min(rg->to, t);
196
197 chg += seg_to - seg_from;
198 }
199
200 return chg;
201}
202
Andy Whitcroft96822902008-07-23 21:27:29 -0700203/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700204 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700205 * the mapping, in pagecache page units; huge pages here.
206 */
Andi Kleena5516432008-07-23 21:27:41 -0700207static pgoff_t vma_hugecache_offset(struct hstate *h,
208 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700209{
Andi Kleena5516432008-07-23 21:27:41 -0700210 return ((address - vma->vm_start) >> huge_page_shift(h)) +
211 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700212}
213
Andy Whitcroft84afd992008-07-23 21:27:32 -0700214/*
215 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
216 * bits of the reservation map pointer, which are always clear due to
217 * alignment.
218 */
219#define HPAGE_RESV_OWNER (1UL << 0)
220#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700221#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700222
Mel Gormana1e78772008-07-23 21:27:23 -0700223/*
224 * These helpers are used to track how many pages are reserved for
225 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
226 * is guaranteed to have their future faults succeed.
227 *
228 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
229 * the reserve counters are updated with the hugetlb_lock held. It is safe
230 * to reset the VMA at fork() time as it is not in use yet and there is no
231 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700232 *
233 * The private mapping reservation is represented in a subtly different
234 * manner to a shared mapping. A shared mapping has a region map associated
235 * with the underlying file, this region map represents the backing file
236 * pages which have ever had a reservation assigned which this persists even
237 * after the page is instantiated. A private mapping has a region map
238 * associated with the original mmap which is attached to all VMAs which
239 * reference it, this region map represents those offsets which have consumed
240 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -0700241 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700242static unsigned long get_vma_private_data(struct vm_area_struct *vma)
243{
244 return (unsigned long)vma->vm_private_data;
245}
246
247static void set_vma_private_data(struct vm_area_struct *vma,
248 unsigned long value)
249{
250 vma->vm_private_data = (void *)value;
251}
252
Andy Whitcroft84afd992008-07-23 21:27:32 -0700253struct resv_map {
254 struct kref refs;
255 struct list_head regions;
256};
257
258struct resv_map *resv_map_alloc(void)
259{
260 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
261 if (!resv_map)
262 return NULL;
263
264 kref_init(&resv_map->refs);
265 INIT_LIST_HEAD(&resv_map->regions);
266
267 return resv_map;
268}
269
270void resv_map_release(struct kref *ref)
271{
272 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
273
274 /* Clear out any active regions before we release the map. */
275 region_truncate(&resv_map->regions, 0);
276 kfree(resv_map);
277}
278
279static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700280{
281 VM_BUG_ON(!is_vm_hugetlb_page(vma));
282 if (!(vma->vm_flags & VM_SHARED))
Andy Whitcroft84afd992008-07-23 21:27:32 -0700283 return (struct resv_map *)(get_vma_private_data(vma) &
284 ~HPAGE_RESV_MASK);
Mel Gormana1e78772008-07-23 21:27:23 -0700285 return 0;
286}
287
Andy Whitcroft84afd992008-07-23 21:27:32 -0700288static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -0700289{
290 VM_BUG_ON(!is_vm_hugetlb_page(vma));
291 VM_BUG_ON(vma->vm_flags & VM_SHARED);
292
Andy Whitcroft84afd992008-07-23 21:27:32 -0700293 set_vma_private_data(vma, (get_vma_private_data(vma) &
294 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700295}
296
297static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
298{
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700299 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700300 VM_BUG_ON(vma->vm_flags & VM_SHARED);
301
302 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700303}
304
305static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
306{
307 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700308
309 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700310}
311
312/* Decrement the reserved pages in the hugepage pool by one */
Andi Kleena5516432008-07-23 21:27:41 -0700313static void decrement_hugepage_resv_vma(struct hstate *h,
314 struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700315{
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700316 if (vma->vm_flags & VM_NORESERVE)
317 return;
318
Mel Gormana1e78772008-07-23 21:27:23 -0700319 if (vma->vm_flags & VM_SHARED) {
320 /* Shared mappings always use reserves */
Andi Kleena5516432008-07-23 21:27:41 -0700321 h->resv_huge_pages--;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700322 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Mel Gormana1e78772008-07-23 21:27:23 -0700323 /*
324 * Only the process that called mmap() has reserves for
325 * private mappings.
326 */
Andi Kleena5516432008-07-23 21:27:41 -0700327 h->resv_huge_pages--;
Mel Gormana1e78772008-07-23 21:27:23 -0700328 }
329}
330
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700331/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700332void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
333{
334 VM_BUG_ON(!is_vm_hugetlb_page(vma));
335 if (!(vma->vm_flags & VM_SHARED))
336 vma->vm_private_data = (void *)0;
337}
338
339/* Returns true if the VMA has associated reserve pages */
340static int vma_has_private_reserves(struct vm_area_struct *vma)
341{
342 if (vma->vm_flags & VM_SHARED)
343 return 0;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700344 if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER))
Mel Gormana1e78772008-07-23 21:27:23 -0700345 return 0;
346 return 1;
347}
348
Andi Kleena5516432008-07-23 21:27:41 -0700349static void clear_huge_page(struct page *page,
350 unsigned long addr, unsigned long sz)
David Gibson79ac6ba2006-03-22 00:08:51 -0800351{
352 int i;
353
354 might_sleep();
Andi Kleena5516432008-07-23 21:27:41 -0700355 for (i = 0; i < sz/PAGE_SIZE; i++) {
David Gibson79ac6ba2006-03-22 00:08:51 -0800356 cond_resched();
Ralf Baechle281e0e32007-10-01 01:20:10 -0700357 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
David Gibson79ac6ba2006-03-22 00:08:51 -0800358 }
359}
360
361static void copy_huge_page(struct page *dst, struct page *src,
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000362 unsigned long addr, struct vm_area_struct *vma)
David Gibson79ac6ba2006-03-22 00:08:51 -0800363{
364 int i;
Andi Kleena5516432008-07-23 21:27:41 -0700365 struct hstate *h = hstate_vma(vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800366
367 might_sleep();
Andi Kleena5516432008-07-23 21:27:41 -0700368 for (i = 0; i < pages_per_huge_page(h); i++) {
David Gibson79ac6ba2006-03-22 00:08:51 -0800369 cond_resched();
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000370 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800371 }
372}
373
Andi Kleena5516432008-07-23 21:27:41 -0700374static void enqueue_huge_page(struct hstate *h, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 int nid = page_to_nid(page);
Andi Kleena5516432008-07-23 21:27:41 -0700377 list_add(&page->lru, &h->hugepage_freelists[nid]);
378 h->free_huge_pages++;
379 h->free_huge_pages_node[nid]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Andi Kleena5516432008-07-23 21:27:41 -0700382static struct page *dequeue_huge_page(struct hstate *h)
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800383{
384 int nid;
385 struct page *page = NULL;
386
387 for (nid = 0; nid < MAX_NUMNODES; ++nid) {
Andi Kleena5516432008-07-23 21:27:41 -0700388 if (!list_empty(&h->hugepage_freelists[nid])) {
389 page = list_entry(h->hugepage_freelists[nid].next,
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800390 struct page, lru);
391 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700392 h->free_huge_pages--;
393 h->free_huge_pages_node[nid]--;
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800394 break;
395 }
396 }
397 return page;
398}
399
Andi Kleena5516432008-07-23 21:27:41 -0700400static struct page *dequeue_huge_page_vma(struct hstate *h,
401 struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700402 unsigned long address, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Nishanth Aravamudan31a5c6e2007-07-15 23:38:02 -0700404 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct page *page = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700406 struct mempolicy *mpol;
Mel Gorman19770b32008-04-28 02:12:18 -0700407 nodemask_t *nodemask;
Mel Gorman396faf02007-07-17 04:03:13 -0700408 struct zonelist *zonelist = huge_zonelist(vma, address,
Mel Gorman19770b32008-04-28 02:12:18 -0700409 htlb_alloc_mask, &mpol, &nodemask);
Mel Gormandd1a2392008-04-28 02:12:17 -0700410 struct zone *zone;
411 struct zoneref *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Mel Gormana1e78772008-07-23 21:27:23 -0700413 /*
414 * A child process with MAP_PRIVATE mappings created by their parent
415 * have no page reserves. This check ensures that reservations are
416 * not "stolen". The child may still get SIGKILLed
417 */
418 if (!vma_has_private_reserves(vma) &&
Andi Kleena5516432008-07-23 21:27:41 -0700419 h->free_huge_pages - h->resv_huge_pages == 0)
Mel Gormana1e78772008-07-23 21:27:23 -0700420 return NULL;
421
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700422 /* If reserves cannot be used, ensure enough pages are in the pool */
Andi Kleena5516432008-07-23 21:27:41 -0700423 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700424 return NULL;
425
Mel Gorman19770b32008-04-28 02:12:18 -0700426 for_each_zone_zonelist_nodemask(zone, z, zonelist,
427 MAX_NR_ZONES - 1, nodemask) {
Mel Gorman54a6eb52008-04-28 02:12:16 -0700428 nid = zone_to_nid(zone);
429 if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
Andi Kleena5516432008-07-23 21:27:41 -0700430 !list_empty(&h->hugepage_freelists[nid])) {
431 page = list_entry(h->hugepage_freelists[nid].next,
Andrew Morton3abf7af2007-07-19 01:49:08 -0700432 struct page, lru);
433 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700434 h->free_huge_pages--;
435 h->free_huge_pages_node[nid]--;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700436
437 if (!avoid_reserve)
Andi Kleena5516432008-07-23 21:27:41 -0700438 decrement_hugepage_resv_vma(h, vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700439
Ken Chen5ab3ee72007-07-23 18:44:00 -0700440 break;
Andrew Morton3abf7af2007-07-19 01:49:08 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700443 mpol_cond_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return page;
445}
446
Andi Kleena5516432008-07-23 21:27:41 -0700447static void update_and_free_page(struct hstate *h, struct page *page)
Adam Litke6af2acb2007-10-16 01:26:16 -0700448{
449 int i;
Andi Kleena5516432008-07-23 21:27:41 -0700450
451 h->nr_huge_pages--;
452 h->nr_huge_pages_node[page_to_nid(page)]--;
453 for (i = 0; i < pages_per_huge_page(h); i++) {
Adam Litke6af2acb2007-10-16 01:26:16 -0700454 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
455 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
456 1 << PG_private | 1<< PG_writeback);
457 }
458 set_compound_page_dtor(page, NULL);
459 set_page_refcounted(page);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700460 arch_release_hugepage(page);
Andi Kleena5516432008-07-23 21:27:41 -0700461 __free_pages(page, huge_page_order(h));
Adam Litke6af2acb2007-10-16 01:26:16 -0700462}
463
Andi Kleene5ff2152008-07-23 21:27:42 -0700464struct hstate *size_to_hstate(unsigned long size)
465{
466 struct hstate *h;
467
468 for_each_hstate(h) {
469 if (huge_page_size(h) == size)
470 return h;
471 }
472 return NULL;
473}
474
David Gibson27a85ef2006-03-22 00:08:56 -0800475static void free_huge_page(struct page *page)
476{
Andi Kleena5516432008-07-23 21:27:41 -0700477 /*
478 * Can't pass hstate in here because it is called from the
479 * compound page destructor.
480 */
Andi Kleene5ff2152008-07-23 21:27:42 -0700481 struct hstate *h = page_hstate(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700482 int nid = page_to_nid(page);
Adam Litkec79fb752007-11-14 16:59:38 -0800483 struct address_space *mapping;
David Gibson27a85ef2006-03-22 00:08:56 -0800484
Adam Litkec79fb752007-11-14 16:59:38 -0800485 mapping = (struct address_space *) page_private(page);
Andy Whitcrofte5df70a2008-02-23 15:23:32 -0800486 set_page_private(page, 0);
Adam Litke7893d1d2007-10-16 01:26:18 -0700487 BUG_ON(page_count(page));
David Gibson27a85ef2006-03-22 00:08:56 -0800488 INIT_LIST_HEAD(&page->lru);
489
490 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700491 if (h->surplus_huge_pages_node[nid]) {
492 update_and_free_page(h, page);
493 h->surplus_huge_pages--;
494 h->surplus_huge_pages_node[nid]--;
Adam Litke7893d1d2007-10-16 01:26:18 -0700495 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700496 enqueue_huge_page(h, page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700497 }
David Gibson27a85ef2006-03-22 00:08:56 -0800498 spin_unlock(&hugetlb_lock);
Adam Litkec79fb752007-11-14 16:59:38 -0800499 if (mapping)
Adam Litke9a119c02007-11-14 16:59:41 -0800500 hugetlb_put_quota(mapping, 1);
David Gibson27a85ef2006-03-22 00:08:56 -0800501}
502
Adam Litke7893d1d2007-10-16 01:26:18 -0700503/*
504 * Increment or decrement surplus_huge_pages. Keep node-specific counters
505 * balanced by operating on them in a round-robin fashion.
506 * Returns 1 if an adjustment was made.
507 */
Andi Kleena5516432008-07-23 21:27:41 -0700508static int adjust_pool_surplus(struct hstate *h, int delta)
Adam Litke7893d1d2007-10-16 01:26:18 -0700509{
510 static int prev_nid;
511 int nid = prev_nid;
512 int ret = 0;
513
514 VM_BUG_ON(delta != -1 && delta != 1);
515 do {
516 nid = next_node(nid, node_online_map);
517 if (nid == MAX_NUMNODES)
518 nid = first_node(node_online_map);
519
520 /* To shrink on this node, there must be a surplus page */
Andi Kleena5516432008-07-23 21:27:41 -0700521 if (delta < 0 && !h->surplus_huge_pages_node[nid])
Adam Litke7893d1d2007-10-16 01:26:18 -0700522 continue;
523 /* Surplus cannot exceed the total number of pages */
Andi Kleena5516432008-07-23 21:27:41 -0700524 if (delta > 0 && h->surplus_huge_pages_node[nid] >=
525 h->nr_huge_pages_node[nid])
Adam Litke7893d1d2007-10-16 01:26:18 -0700526 continue;
527
Andi Kleena5516432008-07-23 21:27:41 -0700528 h->surplus_huge_pages += delta;
529 h->surplus_huge_pages_node[nid] += delta;
Adam Litke7893d1d2007-10-16 01:26:18 -0700530 ret = 1;
531 break;
532 } while (nid != prev_nid);
533
534 prev_nid = nid;
535 return ret;
536}
537
Andi Kleena5516432008-07-23 21:27:41 -0700538static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700539{
540 set_compound_page_dtor(page, free_huge_page);
541 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700542 h->nr_huge_pages++;
543 h->nr_huge_pages_node[nid]++;
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700544 spin_unlock(&hugetlb_lock);
545 put_page(page); /* free it into the hugepage allocator */
546}
547
Andi Kleena5516432008-07-23 21:27:41 -0700548static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct page *page;
Joe Jinf96efd52007-07-15 23:38:12 -0700551
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700552 page = alloc_pages_node(nid,
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700553 htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
554 __GFP_REPEAT|__GFP_NOWARN,
Andi Kleena5516432008-07-23 21:27:41 -0700555 huge_page_order(h));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (page) {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700557 if (arch_prepare_hugepage(page)) {
558 __free_pages(page, HUGETLB_PAGE_ORDER);
Harvey Harrison7b8ee842008-04-28 14:13:19 -0700559 return NULL;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700560 }
Andi Kleena5516432008-07-23 21:27:41 -0700561 prep_new_huge_page(h, page, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700563
564 return page;
565}
566
Andi Kleena5516432008-07-23 21:27:41 -0700567static int alloc_fresh_huge_page(struct hstate *h)
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700568{
569 struct page *page;
570 int start_nid;
571 int next_nid;
572 int ret = 0;
573
Andi Kleena5516432008-07-23 21:27:41 -0700574 start_nid = h->hugetlb_next_nid;
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700575
576 do {
Andi Kleena5516432008-07-23 21:27:41 -0700577 page = alloc_fresh_huge_page_node(h, h->hugetlb_next_nid);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700578 if (page)
579 ret = 1;
580 /*
581 * Use a helper variable to find the next node and then
582 * copy it back to hugetlb_next_nid afterwards:
583 * otherwise there's a window in which a racer might
584 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
585 * But we don't need to use a spin_lock here: it really
586 * doesn't matter if occasionally a racer chooses the
587 * same nid as we do. Move nid forward in the mask even
588 * if we just successfully allocated a hugepage so that
589 * the next caller gets hugepages on the next node.
590 */
Andi Kleena5516432008-07-23 21:27:41 -0700591 next_nid = next_node(h->hugetlb_next_nid, node_online_map);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700592 if (next_nid == MAX_NUMNODES)
593 next_nid = first_node(node_online_map);
Andi Kleena5516432008-07-23 21:27:41 -0700594 h->hugetlb_next_nid = next_nid;
595 } while (!page && h->hugetlb_next_nid != start_nid);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700596
Adam Litke3b116302008-04-28 02:13:06 -0700597 if (ret)
598 count_vm_event(HTLB_BUDDY_PGALLOC);
599 else
600 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
601
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700602 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Andi Kleena5516432008-07-23 21:27:41 -0700605static struct page *alloc_buddy_huge_page(struct hstate *h,
606 struct vm_area_struct *vma, unsigned long address)
Adam Litke7893d1d2007-10-16 01:26:18 -0700607{
608 struct page *page;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800609 unsigned int nid;
Adam Litke7893d1d2007-10-16 01:26:18 -0700610
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800611 /*
612 * Assume we will successfully allocate the surplus page to
613 * prevent racing processes from causing the surplus to exceed
614 * overcommit
615 *
616 * This however introduces a different race, where a process B
617 * tries to grow the static hugepage pool while alloc_pages() is
618 * called by process A. B will only examine the per-node
619 * counters in determining if surplus huge pages can be
620 * converted to normal huge pages in adjust_pool_surplus(). A
621 * won't be able to increment the per-node counter, until the
622 * lock is dropped by B, but B doesn't drop hugetlb_lock until
623 * no more huge pages can be converted from surplus to normal
624 * state (and doesn't try to convert again). Thus, we have a
625 * case where a surplus huge page exists, the pool is grown, and
626 * the surplus huge page still exists after, even though it
627 * should just have been converted to a normal huge page. This
628 * does not leak memory, though, as the hugepage will be freed
629 * once it is out of use. It also does not allow the counters to
630 * go out of whack in adjust_pool_surplus() as we don't modify
631 * the node values until we've gotten the hugepage and only the
632 * per-node value is checked there.
633 */
634 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700635 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800636 spin_unlock(&hugetlb_lock);
637 return NULL;
638 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700639 h->nr_huge_pages++;
640 h->surplus_huge_pages++;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800641 }
642 spin_unlock(&hugetlb_lock);
643
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700644 page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
645 __GFP_REPEAT|__GFP_NOWARN,
Andi Kleena5516432008-07-23 21:27:41 -0700646 huge_page_order(h));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800647
648 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700649 if (page) {
Adam Litke2668db92008-03-10 11:43:50 -0700650 /*
651 * This page is now managed by the hugetlb allocator and has
652 * no users -- drop the buddy allocator's reference.
653 */
654 put_page_testzero(page);
655 VM_BUG_ON(page_count(page));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800656 nid = page_to_nid(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700657 set_compound_page_dtor(page, free_huge_page);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800658 /*
659 * We incremented the global counters already
660 */
Andi Kleena5516432008-07-23 21:27:41 -0700661 h->nr_huge_pages_node[nid]++;
662 h->surplus_huge_pages_node[nid]++;
Adam Litke3b116302008-04-28 02:13:06 -0700663 __count_vm_event(HTLB_BUDDY_PGALLOC);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800664 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700665 h->nr_huge_pages--;
666 h->surplus_huge_pages--;
Adam Litke3b116302008-04-28 02:13:06 -0700667 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Adam Litke7893d1d2007-10-16 01:26:18 -0700668 }
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800669 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700670
671 return page;
672}
673
Adam Litkee4e574b2007-10-16 01:26:19 -0700674/*
675 * Increase the hugetlb pool such that it can accomodate a reservation
676 * of size 'delta'.
677 */
Andi Kleena5516432008-07-23 21:27:41 -0700678static int gather_surplus_pages(struct hstate *h, int delta)
Adam Litkee4e574b2007-10-16 01:26:19 -0700679{
680 struct list_head surplus_list;
681 struct page *page, *tmp;
682 int ret, i;
683 int needed, allocated;
684
Andi Kleena5516432008-07-23 21:27:41 -0700685 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800686 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -0700687 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700688 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800689 }
Adam Litkee4e574b2007-10-16 01:26:19 -0700690
691 allocated = 0;
692 INIT_LIST_HEAD(&surplus_list);
693
694 ret = -ENOMEM;
695retry:
696 spin_unlock(&hugetlb_lock);
697 for (i = 0; i < needed; i++) {
Andi Kleena5516432008-07-23 21:27:41 -0700698 page = alloc_buddy_huge_page(h, NULL, 0);
Adam Litkee4e574b2007-10-16 01:26:19 -0700699 if (!page) {
700 /*
701 * We were not able to allocate enough pages to
702 * satisfy the entire reservation so we free what
703 * we've allocated so far.
704 */
705 spin_lock(&hugetlb_lock);
706 needed = 0;
707 goto free;
708 }
709
710 list_add(&page->lru, &surplus_list);
711 }
712 allocated += needed;
713
714 /*
715 * After retaking hugetlb_lock, we need to recalculate 'needed'
716 * because either resv_huge_pages or free_huge_pages may have changed.
717 */
718 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700719 needed = (h->resv_huge_pages + delta) -
720 (h->free_huge_pages + allocated);
Adam Litkee4e574b2007-10-16 01:26:19 -0700721 if (needed > 0)
722 goto retry;
723
724 /*
725 * The surplus_list now contains _at_least_ the number of extra pages
726 * needed to accomodate the reservation. Add the appropriate number
727 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -0800728 * allocator. Commit the entire reservation here to prevent another
729 * process from stealing the pages as they are added to the pool but
730 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -0700731 */
732 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -0700733 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700734 ret = 0;
735free:
Adam Litke19fc3f02008-04-28 02:12:20 -0700736 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -0700737 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -0700738 if ((--needed) < 0)
739 break;
Adam Litkee4e574b2007-10-16 01:26:19 -0700740 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700741 enqueue_huge_page(h, page);
Adam Litke19fc3f02008-04-28 02:12:20 -0700742 }
743
744 /* Free unnecessary surplus pages to the buddy allocator */
745 if (!list_empty(&surplus_list)) {
746 spin_unlock(&hugetlb_lock);
747 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
748 list_del(&page->lru);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700749 /*
Adam Litke2668db92008-03-10 11:43:50 -0700750 * The page has a reference count of zero already, so
751 * call free_huge_page directly instead of using
752 * put_page. This must be done with hugetlb_lock
Adam Litkeaf767cb2007-10-16 01:26:25 -0700753 * unlocked which is safe because free_huge_page takes
754 * hugetlb_lock before deciding how to free the page.
755 */
Adam Litke2668db92008-03-10 11:43:50 -0700756 free_huge_page(page);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700757 }
Adam Litke19fc3f02008-04-28 02:12:20 -0700758 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -0700759 }
760
761 return ret;
762}
763
764/*
765 * When releasing a hugetlb pool reservation, any surplus pages that were
766 * allocated to satisfy the reservation must be explicitly freed if they were
767 * never used.
768 */
Andi Kleena5516432008-07-23 21:27:41 -0700769static void return_unused_surplus_pages(struct hstate *h,
770 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -0700771{
772 static int nid = -1;
773 struct page *page;
774 unsigned long nr_pages;
775
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700776 /*
777 * We want to release as many surplus pages as possible, spread
778 * evenly across all nodes. Iterate across all nodes until we
779 * can no longer free unreserved surplus pages. This occurs when
780 * the nodes with surplus pages have no free pages.
781 */
782 unsigned long remaining_iterations = num_online_nodes();
783
Adam Litkeac09b3a2008-03-04 14:29:38 -0800784 /* Uncommit the reservation */
Andi Kleena5516432008-07-23 21:27:41 -0700785 h->resv_huge_pages -= unused_resv_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800786
Andi Kleena5516432008-07-23 21:27:41 -0700787 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -0700788
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700789 while (remaining_iterations-- && nr_pages) {
Adam Litkee4e574b2007-10-16 01:26:19 -0700790 nid = next_node(nid, node_online_map);
791 if (nid == MAX_NUMNODES)
792 nid = first_node(node_online_map);
793
Andi Kleena5516432008-07-23 21:27:41 -0700794 if (!h->surplus_huge_pages_node[nid])
Adam Litkee4e574b2007-10-16 01:26:19 -0700795 continue;
796
Andi Kleena5516432008-07-23 21:27:41 -0700797 if (!list_empty(&h->hugepage_freelists[nid])) {
798 page = list_entry(h->hugepage_freelists[nid].next,
Adam Litkee4e574b2007-10-16 01:26:19 -0700799 struct page, lru);
800 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700801 update_and_free_page(h, page);
802 h->free_huge_pages--;
803 h->free_huge_pages_node[nid]--;
804 h->surplus_huge_pages--;
805 h->surplus_huge_pages_node[nid]--;
Adam Litkee4e574b2007-10-16 01:26:19 -0700806 nr_pages--;
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700807 remaining_iterations = num_online_nodes();
Adam Litkee4e574b2007-10-16 01:26:19 -0700808 }
809 }
810}
811
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700812/*
813 * Determine if the huge page at addr within the vma has an associated
814 * reservation. Where it does not we will need to logically increase
815 * reservation and actually increase quota before an allocation can occur.
816 * Where any new reservation would be required the reservation change is
817 * prepared, but not committed. Once the page has been quota'd allocated
818 * an instantiated the change should be committed via vma_commit_reservation.
819 * No action is required on failure.
820 */
Andi Kleena5516432008-07-23 21:27:41 -0700821static int vma_needs_reservation(struct hstate *h,
822 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700823{
824 struct address_space *mapping = vma->vm_file->f_mapping;
825 struct inode *inode = mapping->host;
826
827 if (vma->vm_flags & VM_SHARED) {
Andi Kleena5516432008-07-23 21:27:41 -0700828 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700829 return region_chg(&inode->i_mapping->private_list,
830 idx, idx + 1);
831
Andy Whitcroft84afd992008-07-23 21:27:32 -0700832 } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
833 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700834
Andy Whitcroft84afd992008-07-23 21:27:32 -0700835 } else {
836 int err;
Andi Kleena5516432008-07-23 21:27:41 -0700837 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700838 struct resv_map *reservations = vma_resv_map(vma);
839
840 err = region_chg(&reservations->regions, idx, idx + 1);
841 if (err < 0)
842 return err;
843 return 0;
844 }
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700845}
Andi Kleena5516432008-07-23 21:27:41 -0700846static void vma_commit_reservation(struct hstate *h,
847 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700848{
849 struct address_space *mapping = vma->vm_file->f_mapping;
850 struct inode *inode = mapping->host;
851
852 if (vma->vm_flags & VM_SHARED) {
Andi Kleena5516432008-07-23 21:27:41 -0700853 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700854 region_add(&inode->i_mapping->private_list, idx, idx + 1);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700855
856 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Andi Kleena5516432008-07-23 21:27:41 -0700857 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700858 struct resv_map *reservations = vma_resv_map(vma);
859
860 /* Mark this page used in the map. */
861 region_add(&reservations->regions, idx, idx + 1);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700862 }
863}
864
David Gibson27a85ef2006-03-22 00:08:56 -0800865static struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700866 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Andi Kleena5516432008-07-23 21:27:41 -0700868 struct hstate *h = hstate_vma(vma);
Adam Litke348ea202007-11-14 16:59:37 -0800869 struct page *page;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800870 struct address_space *mapping = vma->vm_file->f_mapping;
Mel Gormana1e78772008-07-23 21:27:23 -0700871 struct inode *inode = mapping->host;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700872 unsigned int chg;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800873
Mel Gormana1e78772008-07-23 21:27:23 -0700874 /*
875 * Processes that did not create the mapping will have no reserves and
876 * will not have accounted against quota. Check that the quota can be
877 * made before satisfying the allocation
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700878 * MAP_NORESERVE mappings may also need pages and quota allocated
879 * if no reserve mapping overlaps.
Mel Gormana1e78772008-07-23 21:27:23 -0700880 */
Andi Kleena5516432008-07-23 21:27:41 -0700881 chg = vma_needs_reservation(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700882 if (chg < 0)
883 return ERR_PTR(chg);
884 if (chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700885 if (hugetlb_get_quota(inode->i_mapping, chg))
886 return ERR_PTR(-ENOSPC);
Mel Gormana1e78772008-07-23 21:27:23 -0700887
888 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700889 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
Mel Gormana1e78772008-07-23 21:27:23 -0700890 spin_unlock(&hugetlb_lock);
891
892 if (!page) {
Andi Kleena5516432008-07-23 21:27:41 -0700893 page = alloc_buddy_huge_page(h, vma, addr);
Mel Gormana1e78772008-07-23 21:27:23 -0700894 if (!page) {
895 hugetlb_put_quota(inode->i_mapping, chg);
896 return ERR_PTR(-VM_FAULT_OOM);
897 }
898 }
899
900 set_page_refcounted(page);
901 set_page_private(page, (unsigned long) mapping);
902
Andi Kleena5516432008-07-23 21:27:41 -0700903 vma_commit_reservation(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700904
Adam Litke90d8b7e2007-11-14 16:59:42 -0800905 return page;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800906}
907
Andi Kleene5ff2152008-07-23 21:27:42 -0700908static void __init hugetlb_init_one_hstate(struct hstate *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Andi Kleena5516432008-07-23 21:27:41 -0700912 for (i = 0; i < MAX_NUMNODES; ++i)
913 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
914
915 h->hugetlb_next_nid = first_node(node_online_map);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700916
Andi Kleene5ff2152008-07-23 21:27:42 -0700917 for (i = 0; i < h->max_huge_pages; ++i) {
Andi Kleena5516432008-07-23 21:27:41 -0700918 if (!alloc_fresh_huge_page(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Andi Kleene5ff2152008-07-23 21:27:42 -0700921 h->max_huge_pages = h->free_huge_pages = h->nr_huge_pages = i;
922}
923
924static void __init hugetlb_init_hstates(void)
925{
926 struct hstate *h;
927
928 for_each_hstate(h) {
929 hugetlb_init_one_hstate(h);
930 }
931}
932
933static void __init report_hugepages(void)
934{
935 struct hstate *h;
936
937 for_each_hstate(h) {
938 printk(KERN_INFO "Total HugeTLB memory allocated, "
939 "%ld %dMB pages\n",
940 h->free_huge_pages,
941 1 << (h->order + PAGE_SHIFT - 20));
942 }
943}
944
945static int __init hugetlb_init(void)
946{
947 BUILD_BUG_ON(HPAGE_SHIFT == 0);
948
949 if (!size_to_hstate(HPAGE_SIZE)) {
950 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
951 parsed_hstate->max_huge_pages = default_hstate_max_huge_pages;
952 }
953 default_hstate_idx = size_to_hstate(HPAGE_SIZE) - hstates;
954
955 hugetlb_init_hstates();
956
957 report_hugepages();
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
960}
961module_init(hugetlb_init);
962
Andi Kleene5ff2152008-07-23 21:27:42 -0700963/* Should be called on processing a hugepagesz=... option */
964void __init hugetlb_add_hstate(unsigned order)
965{
966 struct hstate *h;
967 if (size_to_hstate(PAGE_SIZE << order)) {
968 printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
969 return;
970 }
971 BUG_ON(max_hstate >= HUGE_MAX_HSTATE);
972 BUG_ON(order == 0);
973 h = &hstates[max_hstate++];
974 h->order = order;
975 h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
976 hugetlb_init_one_hstate(h);
977 parsed_hstate = h;
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static int __init hugetlb_setup(char *s)
981{
Andi Kleene5ff2152008-07-23 21:27:42 -0700982 unsigned long *mhp;
983
984 /*
985 * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
986 * so this hugepages= parameter goes to the "default hstate".
987 */
988 if (!max_hstate)
989 mhp = &default_hstate_max_huge_pages;
990 else
991 mhp = &parsed_hstate->max_huge_pages;
992
993 if (sscanf(s, "%lu", mhp) <= 0)
994 *mhp = 0;
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return 1;
997}
998__setup("hugepages=", hugetlb_setup);
999
Ken Chen8a630112007-05-09 02:33:34 -07001000static unsigned int cpuset_mems_nr(unsigned int *array)
1001{
1002 int node;
1003 unsigned int nr = 0;
1004
1005 for_each_node_mask(node, cpuset_current_mems_allowed)
1006 nr += array[node];
1007
1008 return nr;
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012#ifdef CONFIG_HIGHMEM
Andi Kleena5516432008-07-23 21:27:41 -07001013static void try_to_free_low(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Christoph Lameter4415cc82006-09-25 23:31:55 -07001015 int i;
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 for (i = 0; i < MAX_NUMNODES; ++i) {
1018 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -07001019 struct list_head *freel = &h->hugepage_freelists[i];
1020 list_for_each_entry_safe(page, next, freel, lru) {
1021 if (count >= h->nr_huge_pages)
Adam Litke6b0c8802007-10-16 01:26:23 -07001022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (PageHighMem(page))
1024 continue;
1025 list_del(&page->lru);
Andi Kleene5ff2152008-07-23 21:27:42 -07001026 update_and_free_page(h, page);
Andi Kleena5516432008-07-23 21:27:41 -07001027 h->free_huge_pages--;
1028 h->free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 }
1031}
1032#else
Andi Kleena5516432008-07-23 21:27:41 -07001033static inline void try_to_free_low(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035}
1036#endif
1037
Andi Kleena5516432008-07-23 21:27:41 -07001038#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Andi Kleene5ff2152008-07-23 21:27:42 -07001039static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Adam Litke7893d1d2007-10-16 01:26:18 -07001041 unsigned long min_count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Adam Litke7893d1d2007-10-16 01:26:18 -07001043 /*
1044 * Increase the pool size
1045 * First take pages out of surplus state. Then make up the
1046 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001047 *
1048 * We might race with alloc_buddy_huge_page() here and be unable
1049 * to convert a surplus huge page to a normal huge page. That is
1050 * not critical, though, it just means the overall size of the
1051 * pool might be one hugepage larger than it needs to be, but
1052 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -07001053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001055 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
1056 if (!adjust_pool_surplus(h, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -07001057 break;
1058 }
1059
Andi Kleena5516432008-07-23 21:27:41 -07001060 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -07001061 /*
1062 * If this allocation races such that we no longer need the
1063 * page, free_huge_page will handle it by freeing the page
1064 * and reducing the surplus.
1065 */
1066 spin_unlock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001067 ret = alloc_fresh_huge_page(h);
Adam Litke7893d1d2007-10-16 01:26:18 -07001068 spin_lock(&hugetlb_lock);
1069 if (!ret)
1070 goto out;
1071
1072 }
Adam Litke7893d1d2007-10-16 01:26:18 -07001073
1074 /*
1075 * Decrease the pool size
1076 * First return free pages to the buddy allocator (being careful
1077 * to keep enough around to satisfy reservations). Then place
1078 * pages into surplus state as needed so the pool will shrink
1079 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001080 *
1081 * By placing pages into the surplus state independent of the
1082 * overcommit value, we are allowing the surplus pool size to
1083 * exceed overcommit. There are few sane options here. Since
1084 * alloc_buddy_huge_page() is checking the global counter,
1085 * though, we'll note that we're not allowed to exceed surplus
1086 * and won't grow the pool anywhere else. Not until one of the
1087 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07001088 */
Andi Kleena5516432008-07-23 21:27:41 -07001089 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07001090 min_count = max(count, min_count);
Andi Kleena5516432008-07-23 21:27:41 -07001091 try_to_free_low(h, min_count);
1092 while (min_count < persistent_huge_pages(h)) {
1093 struct page *page = dequeue_huge_page(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!page)
1095 break;
Andi Kleena5516432008-07-23 21:27:41 -07001096 update_and_free_page(h, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Andi Kleena5516432008-07-23 21:27:41 -07001098 while (count < persistent_huge_pages(h)) {
1099 if (!adjust_pool_surplus(h, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07001100 break;
1101 }
1102out:
Andi Kleena5516432008-07-23 21:27:41 -07001103 ret = persistent_huge_pages(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001105 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108int hugetlb_sysctl_handler(struct ctl_table *table, int write,
1109 struct file *file, void __user *buffer,
1110 size_t *length, loff_t *ppos)
1111{
Andi Kleene5ff2152008-07-23 21:27:42 -07001112 struct hstate *h = &default_hstate;
1113 unsigned long tmp;
1114
1115 if (!write)
1116 tmp = h->max_huge_pages;
1117
1118 table->data = &tmp;
1119 table->maxlen = sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Andi Kleene5ff2152008-07-23 21:27:42 -07001121
1122 if (write)
1123 h->max_huge_pages = set_max_huge_pages(h, tmp);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
1126}
Mel Gorman396faf02007-07-17 04:03:13 -07001127
1128int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
1129 struct file *file, void __user *buffer,
1130 size_t *length, loff_t *ppos)
1131{
1132 proc_dointvec(table, write, file, buffer, length, ppos);
1133 if (hugepages_treat_as_movable)
1134 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
1135 else
1136 htlb_alloc_mask = GFP_HIGHUSER;
1137 return 0;
1138}
1139
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001140int hugetlb_overcommit_handler(struct ctl_table *table, int write,
1141 struct file *file, void __user *buffer,
1142 size_t *length, loff_t *ppos)
1143{
Andi Kleena5516432008-07-23 21:27:41 -07001144 struct hstate *h = &default_hstate;
Andi Kleene5ff2152008-07-23 21:27:42 -07001145 unsigned long tmp;
1146
1147 if (!write)
1148 tmp = h->nr_overcommit_huge_pages;
1149
1150 table->data = &tmp;
1151 table->maxlen = sizeof(unsigned long);
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001152 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Andi Kleene5ff2152008-07-23 21:27:42 -07001153
1154 if (write) {
1155 spin_lock(&hugetlb_lock);
1156 h->nr_overcommit_huge_pages = tmp;
1157 spin_unlock(&hugetlb_lock);
1158 }
1159
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001160 return 0;
1161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163#endif /* CONFIG_SYSCTL */
1164
1165int hugetlb_report_meminfo(char *buf)
1166{
Andi Kleena5516432008-07-23 21:27:41 -07001167 struct hstate *h = &default_hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return sprintf(buf,
1169 "HugePages_Total: %5lu\n"
1170 "HugePages_Free: %5lu\n"
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001171 "HugePages_Rsvd: %5lu\n"
Adam Litke7893d1d2007-10-16 01:26:18 -07001172 "HugePages_Surp: %5lu\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 "Hugepagesize: %5lu kB\n",
Andi Kleena5516432008-07-23 21:27:41 -07001174 h->nr_huge_pages,
1175 h->free_huge_pages,
1176 h->resv_huge_pages,
1177 h->surplus_huge_pages,
1178 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181int hugetlb_report_node_meminfo(int nid, char *buf)
1182{
Andi Kleena5516432008-07-23 21:27:41 -07001183 struct hstate *h = &default_hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return sprintf(buf,
1185 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07001186 "Node %d HugePages_Free: %5u\n"
1187 "Node %d HugePages_Surp: %5u\n",
Andi Kleena5516432008-07-23 21:27:41 -07001188 nid, h->nr_huge_pages_node[nid],
1189 nid, h->free_huge_pages_node[nid],
1190 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
1194unsigned long hugetlb_total_pages(void)
1195{
Andi Kleena5516432008-07-23 21:27:41 -07001196 struct hstate *h = &default_hstate;
1197 return h->nr_huge_pages * pages_per_huge_page(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Andi Kleena5516432008-07-23 21:27:41 -07001200static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001201{
1202 int ret = -ENOMEM;
1203
1204 spin_lock(&hugetlb_lock);
1205 /*
1206 * When cpuset is configured, it breaks the strict hugetlb page
1207 * reservation as the accounting is done on a global variable. Such
1208 * reservation is completely rubbish in the presence of cpuset because
1209 * the reservation is not checked against page availability for the
1210 * current cpuset. Application can still potentially OOM'ed by kernel
1211 * with lack of free htlb page in cpuset that the task is in.
1212 * Attempt to enforce strict accounting with cpuset is almost
1213 * impossible (or too ugly) because cpuset is too fluid that
1214 * task or memory node can be dynamically moved between cpusets.
1215 *
1216 * The change of semantics for shared hugetlb mapping with cpuset is
1217 * undesirable. However, in order to preserve some of the semantics,
1218 * we fall back to check against current free page availability as
1219 * a best attempt and hopefully to minimize the impact of changing
1220 * semantics that cpuset has.
1221 */
1222 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07001223 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001224 goto out;
1225
Andi Kleena5516432008-07-23 21:27:41 -07001226 if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
1227 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001228 goto out;
1229 }
1230 }
1231
1232 ret = 0;
1233 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07001234 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001235
1236out:
1237 spin_unlock(&hugetlb_lock);
1238 return ret;
1239}
1240
Andy Whitcroft84afd992008-07-23 21:27:32 -07001241static void hugetlb_vm_op_open(struct vm_area_struct *vma)
1242{
1243 struct resv_map *reservations = vma_resv_map(vma);
1244
1245 /*
1246 * This new VMA should share its siblings reservation map if present.
1247 * The VMA will only ever have a valid reservation map pointer where
1248 * it is being copied for another still existing VMA. As that VMA
1249 * has a reference to the reservation map it cannot dissappear until
1250 * after this open call completes. It is therefore safe to take a
1251 * new reference here without additional locking.
1252 */
1253 if (reservations)
1254 kref_get(&reservations->refs);
1255}
1256
Mel Gormana1e78772008-07-23 21:27:23 -07001257static void hugetlb_vm_op_close(struct vm_area_struct *vma)
1258{
Andi Kleena5516432008-07-23 21:27:41 -07001259 struct hstate *h = hstate_vma(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001260 struct resv_map *reservations = vma_resv_map(vma);
1261 unsigned long reserve;
1262 unsigned long start;
1263 unsigned long end;
1264
1265 if (reservations) {
Andi Kleena5516432008-07-23 21:27:41 -07001266 start = vma_hugecache_offset(h, vma, vma->vm_start);
1267 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001268
1269 reserve = (end - start) -
1270 region_count(&reservations->regions, start, end);
1271
1272 kref_put(&reservations->refs, resv_map_release);
1273
1274 if (reserve)
Andi Kleena5516432008-07-23 21:27:41 -07001275 hugetlb_acct_memory(h, -reserve);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001276 }
Mel Gormana1e78772008-07-23 21:27:23 -07001277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279/*
1280 * We cannot handle pagefaults against hugetlb pages at all. They cause
1281 * handle_mm_fault() to try to instantiate regular-sized pages in the
1282 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
1283 * this far.
1284 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001285static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07001292 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07001293 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07001294 .close = hugetlb_vm_op_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295};
1296
David Gibson1e8f8892006-01-06 00:10:44 -08001297static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
1298 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07001299{
1300 pte_t entry;
1301
David Gibson1e8f8892006-01-06 00:10:44 -08001302 if (writable) {
David Gibson63551ae2005-06-21 17:14:44 -07001303 entry =
1304 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1305 } else {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001306 entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07001307 }
1308 entry = pte_mkyoung(entry);
1309 entry = pte_mkhuge(entry);
1310
1311 return entry;
1312}
1313
David Gibson1e8f8892006-01-06 00:10:44 -08001314static void set_huge_ptep_writable(struct vm_area_struct *vma,
1315 unsigned long address, pte_t *ptep)
1316{
1317 pte_t entry;
1318
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001319 entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
1320 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001321 update_mmu_cache(vma, address, entry);
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001322 }
David Gibson1e8f8892006-01-06 00:10:44 -08001323}
1324
1325
David Gibson63551ae2005-06-21 17:14:44 -07001326int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
1327 struct vm_area_struct *vma)
1328{
1329 pte_t *src_pte, *dst_pte, entry;
1330 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07001331 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08001332 int cow;
Andi Kleena5516432008-07-23 21:27:41 -07001333 struct hstate *h = hstate_vma(vma);
1334 unsigned long sz = huge_page_size(h);
David Gibson1e8f8892006-01-06 00:10:44 -08001335
1336 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07001337
Andi Kleena5516432008-07-23 21:27:41 -07001338 for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
Hugh Dickinsc74df322005-10-29 18:16:23 -07001339 src_pte = huge_pte_offset(src, addr);
1340 if (!src_pte)
1341 continue;
Andi Kleena5516432008-07-23 21:27:41 -07001342 dst_pte = huge_pte_alloc(dst, addr, sz);
David Gibson63551ae2005-06-21 17:14:44 -07001343 if (!dst_pte)
1344 goto nomem;
Larry Woodmanc5c99422008-01-24 05:49:25 -08001345
1346 /* If the pagetables are shared don't copy or take references */
1347 if (dst_pte == src_pte)
1348 continue;
1349
Hugh Dickinsc74df322005-10-29 18:16:23 -07001350 spin_lock(&dst->page_table_lock);
Nick Piggin46478752008-06-05 22:45:57 -07001351 spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001352 if (!huge_pte_none(huge_ptep_get(src_pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001353 if (cow)
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001354 huge_ptep_set_wrprotect(src, addr, src_pte);
1355 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07001356 ptepage = pte_page(entry);
1357 get_page(ptepage);
Hugh Dickins1c598272005-10-19 21:23:43 -07001358 set_huge_pte_at(dst, addr, dst_pte, entry);
1359 }
1360 spin_unlock(&src->page_table_lock);
Hugh Dickinsc74df322005-10-29 18:16:23 -07001361 spin_unlock(&dst->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001362 }
1363 return 0;
1364
1365nomem:
1366 return -ENOMEM;
1367}
1368
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001369void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001370 unsigned long end, struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07001371{
1372 struct mm_struct *mm = vma->vm_mm;
1373 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07001374 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07001375 pte_t pte;
1376 struct page *page;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001377 struct page *tmp;
Andi Kleena5516432008-07-23 21:27:41 -07001378 struct hstate *h = hstate_vma(vma);
1379 unsigned long sz = huge_page_size(h);
1380
Chen, Kenneth Wc0a499c2006-12-06 20:31:39 -08001381 /*
1382 * A page gathering list, protected by per file i_mmap_lock. The
1383 * lock is used to avoid list corruption from multiple unmapping
1384 * of the same page since we are using page->lru.
1385 */
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001386 LIST_HEAD(page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001387
1388 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07001389 BUG_ON(start & ~huge_page_mask(h));
1390 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07001391
Hugh Dickins508034a2005-10-29 18:16:30 -07001392 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001393 for (address = start; address < end; address += sz) {
David Gibsonc7546f82005-08-05 11:59:35 -07001394 ptep = huge_pte_offset(mm, address);
Adam Litke4c887262005-10-29 18:16:46 -07001395 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07001396 continue;
1397
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001398 if (huge_pmd_unshare(mm, &address, ptep))
1399 continue;
1400
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001401 /*
1402 * If a reference page is supplied, it is because a specific
1403 * page is being unmapped, not a range. Ensure the page we
1404 * are about to unmap is the actual page of interest.
1405 */
1406 if (ref_page) {
1407 pte = huge_ptep_get(ptep);
1408 if (huge_pte_none(pte))
1409 continue;
1410 page = pte_page(pte);
1411 if (page != ref_page)
1412 continue;
1413
1414 /*
1415 * Mark the VMA as having unmapped its page so that
1416 * future faults in this VMA will fail rather than
1417 * looking like data was lost
1418 */
1419 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
1420 }
1421
David Gibsonc7546f82005-08-05 11:59:35 -07001422 pte = huge_ptep_get_and_clear(mm, address, ptep);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001423 if (huge_pte_none(pte))
David Gibson63551ae2005-06-21 17:14:44 -07001424 continue;
David Gibsonc7546f82005-08-05 11:59:35 -07001425
David Gibson63551ae2005-06-21 17:14:44 -07001426 page = pte_page(pte);
Ken Chen6649a382007-02-08 14:20:27 -08001427 if (pte_dirty(pte))
1428 set_page_dirty(page);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001429 list_add(&page->lru, &page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 spin_unlock(&mm->page_table_lock);
Hugh Dickins508034a2005-10-29 18:16:30 -07001432 flush_tlb_range(vma, start, end);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001433 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1434 list_del(&page->lru);
1435 put_page(page);
1436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
David Gibson63551ae2005-06-21 17:14:44 -07001438
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001439void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001440 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001441{
1442 /*
1443 * It is undesirable to test vma->vm_file as it should be non-null
1444 * for valid hugetlb area. However, vm_file will be NULL in the error
1445 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
1446 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
1447 * to clean up. Since no pte has actually been setup, it is safe to
1448 * do nothing in this case.
1449 */
1450 if (vma->vm_file) {
1451 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001452 __unmap_hugepage_range(vma, start, end, ref_page);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001453 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
1454 }
1455}
1456
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001457/*
1458 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1459 * mappping it owns the reserve page for. The intention is to unmap the page
1460 * from other VMAs and let the children be SIGKILLed if they are faulting the
1461 * same region.
1462 */
1463int unmap_ref_private(struct mm_struct *mm,
1464 struct vm_area_struct *vma,
1465 struct page *page,
1466 unsigned long address)
1467{
1468 struct vm_area_struct *iter_vma;
1469 struct address_space *mapping;
1470 struct prio_tree_iter iter;
1471 pgoff_t pgoff;
1472
1473 /*
1474 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1475 * from page cache lookup which is in HPAGE_SIZE units.
1476 */
1477 address = address & huge_page_mask(hstate_vma(vma));
1478 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
1479 + (vma->vm_pgoff >> PAGE_SHIFT);
1480 mapping = (struct address_space *)page_private(page);
1481
1482 vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1483 /* Do not unmap the current VMA */
1484 if (iter_vma == vma)
1485 continue;
1486
1487 /*
1488 * Unmap the page from other VMAs without their own reserves.
1489 * They get marked to be SIGKILLed if they fault in these
1490 * areas. This is because a future no-page fault on this VMA
1491 * could insert a zeroed page instead of the data existing
1492 * from the time of fork. This would look like data corruption
1493 */
1494 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
1495 unmap_hugepage_range(iter_vma,
1496 address, address + HPAGE_SIZE,
1497 page);
1498 }
1499
1500 return 1;
1501}
1502
David Gibson1e8f8892006-01-06 00:10:44 -08001503static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001504 unsigned long address, pte_t *ptep, pte_t pte,
1505 struct page *pagecache_page)
David Gibson1e8f8892006-01-06 00:10:44 -08001506{
Andi Kleena5516432008-07-23 21:27:41 -07001507 struct hstate *h = hstate_vma(vma);
David Gibson1e8f8892006-01-06 00:10:44 -08001508 struct page *old_page, *new_page;
David Gibson79ac6ba2006-03-22 00:08:51 -08001509 int avoidcopy;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001510 int outside_reserve = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001511
1512 old_page = pte_page(pte);
1513
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001514retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08001515 /* If no-one else is actually using this page, avoid the copy
1516 * and just make the page writable */
1517 avoidcopy = (page_count(old_page) == 1);
1518 if (avoidcopy) {
1519 set_huge_ptep_writable(vma, address, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07001520 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001521 }
1522
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001523 /*
1524 * If the process that created a MAP_PRIVATE mapping is about to
1525 * perform a COW due to a shared page count, attempt to satisfy
1526 * the allocation without using the existing reserves. The pagecache
1527 * page is used to determine if the reserve at this address was
1528 * consumed or not. If reserves were used, a partial faulted mapping
1529 * at the time of fork() could consume its reserves on COW instead
1530 * of the full address range.
1531 */
1532 if (!(vma->vm_flags & VM_SHARED) &&
1533 is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
1534 old_page != pagecache_page)
1535 outside_reserve = 1;
1536
David Gibson1e8f8892006-01-06 00:10:44 -08001537 page_cache_get(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001538 new_page = alloc_huge_page(vma, address, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08001539
Adam Litke2fc39ce2007-11-14 16:59:39 -08001540 if (IS_ERR(new_page)) {
David Gibson1e8f8892006-01-06 00:10:44 -08001541 page_cache_release(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001542
1543 /*
1544 * If a process owning a MAP_PRIVATE mapping fails to COW,
1545 * it is due to references held by a child and an insufficient
1546 * huge page pool. To guarantee the original mappers
1547 * reliability, unmap the page from child processes. The child
1548 * may get SIGKILLed if it later faults.
1549 */
1550 if (outside_reserve) {
1551 BUG_ON(huge_pte_none(pte));
1552 if (unmap_ref_private(mm, vma, old_page, address)) {
1553 BUG_ON(page_count(old_page) != 1);
1554 BUG_ON(huge_pte_none(pte));
1555 goto retry_avoidcopy;
1556 }
1557 WARN_ON_ONCE(1);
1558 }
1559
Adam Litke2fc39ce2007-11-14 16:59:39 -08001560 return -PTR_ERR(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001561 }
1562
1563 spin_unlock(&mm->page_table_lock);
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00001564 copy_huge_page(new_page, old_page, address, vma);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001565 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001566 spin_lock(&mm->page_table_lock);
1567
Andi Kleena5516432008-07-23 21:27:41 -07001568 ptep = huge_pte_offset(mm, address & huge_page_mask(h));
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001569 if (likely(pte_same(huge_ptep_get(ptep), pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001570 /* Break COW */
Gerald Schaefer8fe627e2008-04-28 02:13:28 -07001571 huge_ptep_clear_flush(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08001572 set_huge_pte_at(mm, address, ptep,
1573 make_huge_pte(vma, new_page, 1));
1574 /* Make the old page be freed below */
1575 new_page = old_page;
1576 }
1577 page_cache_release(new_page);
1578 page_cache_release(old_page);
Nick Piggin83c54072007-07-19 01:47:05 -07001579 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001580}
1581
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001582/* Return the pagecache page at a given address within a VMA */
Andi Kleena5516432008-07-23 21:27:41 -07001583static struct page *hugetlbfs_pagecache_page(struct hstate *h,
1584 struct vm_area_struct *vma, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001585{
1586 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001587 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001588
1589 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07001590 idx = vma_hugecache_offset(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001591
1592 return find_lock_page(mapping, idx);
1593}
1594
Robert P. J. Daya1ed3dd2007-07-17 04:03:33 -07001595static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
David Gibson1e8f8892006-01-06 00:10:44 -08001596 unsigned long address, pte_t *ptep, int write_access)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001597{
Andi Kleena5516432008-07-23 21:27:41 -07001598 struct hstate *h = hstate_vma(vma);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001599 int ret = VM_FAULT_SIGBUS;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001600 pgoff_t idx;
Adam Litke4c887262005-10-29 18:16:46 -07001601 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07001602 struct page *page;
1603 struct address_space *mapping;
David Gibson1e8f8892006-01-06 00:10:44 -08001604 pte_t new_pte;
Adam Litke4c887262005-10-29 18:16:46 -07001605
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001606 /*
1607 * Currently, we are forced to kill the process in the event the
1608 * original mapper has unmapped pages from the child due to a failed
1609 * COW. Warn that such a situation has occured as it may not be obvious
1610 */
1611 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
1612 printk(KERN_WARNING
1613 "PID %d killed due to inadequate hugepage pool\n",
1614 current->pid);
1615 return ret;
1616 }
1617
Adam Litke4c887262005-10-29 18:16:46 -07001618 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07001619 idx = vma_hugecache_offset(h, vma, address);
Adam Litke4c887262005-10-29 18:16:46 -07001620
1621 /*
1622 * Use page lock to guard against racing truncation
1623 * before we get page_table_lock.
1624 */
Christoph Lameter6bda6662006-01-06 00:10:49 -08001625retry:
1626 page = find_lock_page(mapping, idx);
1627 if (!page) {
Andi Kleena5516432008-07-23 21:27:41 -07001628 size = i_size_read(mapping->host) >> huge_page_shift(h);
Hugh Dickinsebed4bf2006-10-28 10:38:43 -07001629 if (idx >= size)
1630 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001631 page = alloc_huge_page(vma, address, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08001632 if (IS_ERR(page)) {
1633 ret = -PTR_ERR(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001634 goto out;
1635 }
Andi Kleena5516432008-07-23 21:27:41 -07001636 clear_huge_page(page, address, huge_page_size(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08001637 __SetPageUptodate(page);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001638
Christoph Lameter6bda6662006-01-06 00:10:49 -08001639 if (vma->vm_flags & VM_SHARED) {
1640 int err;
Ken Chen45c682a2007-11-14 16:59:44 -08001641 struct inode *inode = mapping->host;
Christoph Lameter6bda6662006-01-06 00:10:49 -08001642
1643 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
1644 if (err) {
1645 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001646 if (err == -EEXIST)
1647 goto retry;
1648 goto out;
1649 }
Ken Chen45c682a2007-11-14 16:59:44 -08001650
1651 spin_lock(&inode->i_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001652 inode->i_blocks += blocks_per_huge_page(h);
Ken Chen45c682a2007-11-14 16:59:44 -08001653 spin_unlock(&inode->i_lock);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001654 } else
1655 lock_page(page);
1656 }
David Gibson1e8f8892006-01-06 00:10:44 -08001657
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001658 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001659 size = i_size_read(mapping->host) >> huge_page_shift(h);
Adam Litke4c887262005-10-29 18:16:46 -07001660 if (idx >= size)
1661 goto backout;
1662
Nick Piggin83c54072007-07-19 01:47:05 -07001663 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001664 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07001665 goto backout;
1666
David Gibson1e8f8892006-01-06 00:10:44 -08001667 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
1668 && (vma->vm_flags & VM_SHARED)));
1669 set_huge_pte_at(mm, address, ptep, new_pte);
1670
1671 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1672 /* Optimization, do the COW without a second fault */
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001673 ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
David Gibson1e8f8892006-01-06 00:10:44 -08001674 }
1675
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001676 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001677 unlock_page(page);
1678out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001679 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07001680
1681backout:
1682 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001683 unlock_page(page);
1684 put_page(page);
1685 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001686}
1687
Adam Litke86e52162006-01-06 00:10:43 -08001688int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1689 unsigned long address, int write_access)
1690{
1691 pte_t *ptep;
1692 pte_t entry;
David Gibson1e8f8892006-01-06 00:10:44 -08001693 int ret;
David Gibson3935baa2006-03-22 00:08:53 -08001694 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
Andi Kleena5516432008-07-23 21:27:41 -07001695 struct hstate *h = hstate_vma(vma);
Adam Litke86e52162006-01-06 00:10:43 -08001696
Andi Kleena5516432008-07-23 21:27:41 -07001697 ptep = huge_pte_alloc(mm, address, huge_page_size(h));
Adam Litke86e52162006-01-06 00:10:43 -08001698 if (!ptep)
1699 return VM_FAULT_OOM;
1700
David Gibson3935baa2006-03-22 00:08:53 -08001701 /*
1702 * Serialize hugepage allocation and instantiation, so that we don't
1703 * get spurious allocation failures if two CPUs race to instantiate
1704 * the same page in the page cache.
1705 */
1706 mutex_lock(&hugetlb_instantiation_mutex);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001707 entry = huge_ptep_get(ptep);
1708 if (huge_pte_none(entry)) {
David Gibson3935baa2006-03-22 00:08:53 -08001709 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
1710 mutex_unlock(&hugetlb_instantiation_mutex);
1711 return ret;
1712 }
Adam Litke86e52162006-01-06 00:10:43 -08001713
Nick Piggin83c54072007-07-19 01:47:05 -07001714 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001715
1716 spin_lock(&mm->page_table_lock);
1717 /* Check for a racing update before calling hugetlb_cow */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001718 if (likely(pte_same(entry, huge_ptep_get(ptep))))
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001719 if (write_access && !pte_write(entry)) {
1720 struct page *page;
Andi Kleena5516432008-07-23 21:27:41 -07001721 page = hugetlbfs_pagecache_page(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001722 ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
1723 if (page) {
1724 unlock_page(page);
1725 put_page(page);
1726 }
1727 }
David Gibson1e8f8892006-01-06 00:10:44 -08001728 spin_unlock(&mm->page_table_lock);
David Gibson3935baa2006-03-22 00:08:53 -08001729 mutex_unlock(&hugetlb_instantiation_mutex);
David Gibson1e8f8892006-01-06 00:10:44 -08001730
1731 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08001732}
1733
David Gibson63551ae2005-06-21 17:14:44 -07001734int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
1735 struct page **pages, struct vm_area_struct **vmas,
Adam Litke5b23dbe2007-11-14 16:59:33 -08001736 unsigned long *position, int *length, int i,
1737 int write)
David Gibson63551ae2005-06-21 17:14:44 -07001738{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001739 unsigned long pfn_offset;
1740 unsigned long vaddr = *position;
David Gibson63551ae2005-06-21 17:14:44 -07001741 int remainder = *length;
Andi Kleena5516432008-07-23 21:27:41 -07001742 struct hstate *h = hstate_vma(vma);
David Gibson63551ae2005-06-21 17:14:44 -07001743
Hugh Dickins1c598272005-10-19 21:23:43 -07001744 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001745 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07001746 pte_t *pte;
1747 struct page *page;
1748
1749 /*
1750 * Some archs (sparc64, sh*) have multiple pte_ts to
1751 * each hugepage. We have to make * sure we get the
1752 * first, for the page indexing below to work.
1753 */
Andi Kleena5516432008-07-23 21:27:41 -07001754 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
Adam Litke4c887262005-10-29 18:16:46 -07001755
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001756 if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
1757 (write && !pte_write(huge_ptep_get(pte)))) {
Adam Litke4c887262005-10-29 18:16:46 -07001758 int ret;
1759
1760 spin_unlock(&mm->page_table_lock);
Adam Litke5b23dbe2007-11-14 16:59:33 -08001761 ret = hugetlb_fault(mm, vma, vaddr, write);
Adam Litke4c887262005-10-29 18:16:46 -07001762 spin_lock(&mm->page_table_lock);
Adam Litkea89182c2007-08-22 14:01:51 -07001763 if (!(ret & VM_FAULT_ERROR))
Adam Litke4c887262005-10-29 18:16:46 -07001764 continue;
1765
1766 remainder = 0;
1767 if (!i)
1768 i = -EFAULT;
1769 break;
1770 }
David Gibson63551ae2005-06-21 17:14:44 -07001771
Andi Kleena5516432008-07-23 21:27:41 -07001772 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001773 page = pte_page(huge_ptep_get(pte));
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001774same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001775 if (pages) {
1776 get_page(page);
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001777 pages[i] = page + pfn_offset;
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001778 }
David Gibson63551ae2005-06-21 17:14:44 -07001779
1780 if (vmas)
1781 vmas[i] = vma;
1782
1783 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001784 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07001785 --remainder;
1786 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001787 if (vaddr < vma->vm_end && remainder &&
Andi Kleena5516432008-07-23 21:27:41 -07001788 pfn_offset < pages_per_huge_page(h)) {
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001789 /*
1790 * We use pfn_offset to avoid touching the pageframes
1791 * of this compound page.
1792 */
1793 goto same_page;
1794 }
David Gibson63551ae2005-06-21 17:14:44 -07001795 }
Hugh Dickins1c598272005-10-19 21:23:43 -07001796 spin_unlock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001797 *length = remainder;
1798 *position = vaddr;
1799
1800 return i;
1801}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001802
1803void hugetlb_change_protection(struct vm_area_struct *vma,
1804 unsigned long address, unsigned long end, pgprot_t newprot)
1805{
1806 struct mm_struct *mm = vma->vm_mm;
1807 unsigned long start = address;
1808 pte_t *ptep;
1809 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07001810 struct hstate *h = hstate_vma(vma);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001811
1812 BUG_ON(address >= end);
1813 flush_cache_range(vma, address, end);
1814
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001815 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001816 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001817 for (; address < end; address += huge_page_size(h)) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001818 ptep = huge_pte_offset(mm, address);
1819 if (!ptep)
1820 continue;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001821 if (huge_pmd_unshare(mm, &address, ptep))
1822 continue;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001823 if (!huge_pte_none(huge_ptep_get(ptep))) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001824 pte = huge_ptep_get_and_clear(mm, address, ptep);
1825 pte = pte_mkhuge(pte_modify(pte, newprot));
1826 set_huge_pte_at(mm, address, ptep, pte);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001827 }
1828 }
1829 spin_unlock(&mm->page_table_lock);
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001830 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001831
1832 flush_tlb_range(vma, start, end);
1833}
1834
Mel Gormana1e78772008-07-23 21:27:23 -07001835int hugetlb_reserve_pages(struct inode *inode,
1836 long from, long to,
1837 struct vm_area_struct *vma)
Adam Litkee4e574b2007-10-16 01:26:19 -07001838{
1839 long ret, chg;
Andi Kleena5516432008-07-23 21:27:41 -07001840 struct hstate *h = hstate_inode(inode);
Adam Litkee4e574b2007-10-16 01:26:19 -07001841
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001842 if (vma && vma->vm_flags & VM_NORESERVE)
1843 return 0;
1844
Mel Gormana1e78772008-07-23 21:27:23 -07001845 /*
1846 * Shared mappings base their reservation on the number of pages that
1847 * are already allocated on behalf of the file. Private mappings need
1848 * to reserve the full area even if read-only as mprotect() may be
1849 * called to make the mapping read-write. Assume !vma is a shm mapping
1850 */
1851 if (!vma || vma->vm_flags & VM_SHARED)
1852 chg = region_chg(&inode->i_mapping->private_list, from, to);
1853 else {
Andy Whitcroft84afd992008-07-23 21:27:32 -07001854 struct resv_map *resv_map = resv_map_alloc();
1855 if (!resv_map)
1856 return -ENOMEM;
1857
Mel Gormana1e78772008-07-23 21:27:23 -07001858 chg = to - from;
Andy Whitcroft84afd992008-07-23 21:27:32 -07001859
1860 set_vma_resv_map(vma, resv_map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001861 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
Mel Gormana1e78772008-07-23 21:27:23 -07001862 }
1863
Adam Litkee4e574b2007-10-16 01:26:19 -07001864 if (chg < 0)
1865 return chg;
Ken Chen8a630112007-05-09 02:33:34 -07001866
Adam Litke90d8b7e2007-11-14 16:59:42 -08001867 if (hugetlb_get_quota(inode->i_mapping, chg))
1868 return -ENOSPC;
Andi Kleena5516432008-07-23 21:27:41 -07001869 ret = hugetlb_acct_memory(h, chg);
Ken Chen68842c92008-01-14 00:55:19 -08001870 if (ret < 0) {
1871 hugetlb_put_quota(inode->i_mapping, chg);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001872 return ret;
Ken Chen68842c92008-01-14 00:55:19 -08001873 }
Mel Gormana1e78772008-07-23 21:27:23 -07001874 if (!vma || vma->vm_flags & VM_SHARED)
1875 region_add(&inode->i_mapping->private_list, from, to);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001876 return 0;
1877}
1878
1879void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1880{
Andi Kleena5516432008-07-23 21:27:41 -07001881 struct hstate *h = hstate_inode(inode);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001882 long chg = region_truncate(&inode->i_mapping->private_list, offset);
Ken Chen45c682a2007-11-14 16:59:44 -08001883
1884 spin_lock(&inode->i_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001885 inode->i_blocks -= blocks_per_huge_page(h);
Ken Chen45c682a2007-11-14 16:59:44 -08001886 spin_unlock(&inode->i_lock);
1887
Adam Litke90d8b7e2007-11-14 16:59:42 -08001888 hugetlb_put_quota(inode->i_mapping, (chg - freed));
Andi Kleena5516432008-07-23 21:27:41 -07001889 hugetlb_acct_memory(h, -(chg - freed));
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001890}