blob: 5cb8bc7c80f709a8213309b9617c4a396854f057 [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>
Alexey Dobriyane1759c22008-10-15 23:50:22 +040010#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/sysctl.h>
12#include <linux/highmem.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070013#include <linux/mmu_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/nodemask.h>
David Gibson63551ae2005-06-21 17:14:44 -070015#include <linux/pagemap.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080016#include <linux/mempolicy.h>
Christoph Lameteraea47ff2006-01-08 01:00:57 -080017#include <linux/cpuset.h>
David Gibson3935baa2006-03-22 00:08:53 -080018#include <linux/mutex.h>
Andi Kleenaa888a72008-07-23 21:27:47 -070019#include <linux/bootmem.h>
Nishanth Aravamudana3437872008-07-23 21:27:44 -070020#include <linux/sysfs.h>
Linus Torvaldsd6606682008-08-06 12:04:54 -070021
David Gibson63551ae2005-06-21 17:14:44 -070022#include <asm/page.h>
23#include <asm/pgtable.h>
Adrian Bunk78a34ae2008-07-28 15:46:30 -070024#include <asm/io.h>
David Gibson63551ae2005-06-21 17:14:44 -070025
26#include <linux/hugetlb.h>
Nick Piggin7835e982006-03-22 00:08:40 -080027#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
Mel Gorman396faf02007-07-17 04:03:13 -070030static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
31unsigned long hugepages_treat_as_movable;
Andi Kleena5516432008-07-23 21:27:41 -070032
Andi Kleene5ff2152008-07-23 21:27:42 -070033static int max_hstate;
34unsigned int default_hstate_idx;
35struct hstate hstates[HUGE_MAX_HSTATE];
36
Jon Tollefson53ba51d2008-07-23 21:27:52 -070037__initdata LIST_HEAD(huge_boot_pages);
38
Andi Kleene5ff2152008-07-23 21:27:42 -070039/* for command line parsing */
40static struct hstate * __initdata parsed_hstate;
41static unsigned long __initdata default_hstate_max_huge_pages;
Nick Piggine11bfbf2008-07-23 21:27:52 -070042static unsigned long __initdata default_hstate_size;
Andi Kleene5ff2152008-07-23 21:27:42 -070043
44#define for_each_hstate(h) \
45 for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
Mel Gorman396faf02007-07-17 04:03:13 -070046
David Gibson3935baa2006-03-22 00:08:53 -080047/*
48 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
49 */
50static DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080051
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -070052/*
Andy Whitcroft96822902008-07-23 21:27:29 -070053 * Region tracking -- allows tracking of reservations and instantiated pages
54 * across the pages in a mapping.
Andy Whitcroft84afd992008-07-23 21:27:32 -070055 *
56 * The region data structures are protected by a combination of the mmap_sem
57 * and the hugetlb_instantion_mutex. To access or modify a region the caller
58 * must either hold the mmap_sem for write, or the mmap_sem for read and
59 * the hugetlb_instantiation mutex:
60 *
61 * down_write(&mm->mmap_sem);
62 * or
63 * down_read(&mm->mmap_sem);
64 * mutex_lock(&hugetlb_instantiation_mutex);
Andy Whitcroft96822902008-07-23 21:27:29 -070065 */
66struct file_region {
67 struct list_head link;
68 long from;
69 long to;
70};
71
72static long region_add(struct list_head *head, long f, long t)
73{
74 struct file_region *rg, *nrg, *trg;
75
76 /* Locate the region we are either in or before. */
77 list_for_each_entry(rg, head, link)
78 if (f <= rg->to)
79 break;
80
81 /* Round our left edge to the current segment if it encloses us. */
82 if (f > rg->from)
83 f = rg->from;
84
85 /* Check for and consume any regions we now overlap with. */
86 nrg = rg;
87 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
88 if (&rg->link == head)
89 break;
90 if (rg->from > t)
91 break;
92
93 /* If this area reaches higher then extend our area to
94 * include it completely. If this is not the first area
95 * which we intend to reuse, free it. */
96 if (rg->to > t)
97 t = rg->to;
98 if (rg != nrg) {
99 list_del(&rg->link);
100 kfree(rg);
101 }
102 }
103 nrg->from = f;
104 nrg->to = t;
105 return 0;
106}
107
108static long region_chg(struct list_head *head, long f, long t)
109{
110 struct file_region *rg, *nrg;
111 long chg = 0;
112
113 /* Locate the region we are before or in. */
114 list_for_each_entry(rg, head, link)
115 if (f <= rg->to)
116 break;
117
118 /* If we are below the current region then a new region is required.
119 * Subtle, allocate a new region at the position but make it zero
120 * size such that we can guarantee to record the reservation. */
121 if (&rg->link == head || t < rg->from) {
122 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
123 if (!nrg)
124 return -ENOMEM;
125 nrg->from = f;
126 nrg->to = f;
127 INIT_LIST_HEAD(&nrg->link);
128 list_add(&nrg->link, rg->link.prev);
129
130 return t - f;
131 }
132
133 /* Round our left edge to the current segment if it encloses us. */
134 if (f > rg->from)
135 f = rg->from;
136 chg = t - f;
137
138 /* Check for and consume any regions we now overlap with. */
139 list_for_each_entry(rg, rg->link.prev, link) {
140 if (&rg->link == head)
141 break;
142 if (rg->from > t)
143 return chg;
144
145 /* We overlap with this area, if it extends futher than
146 * us then we must extend ourselves. Account for its
147 * existing reservation. */
148 if (rg->to > t) {
149 chg += rg->to - t;
150 t = rg->to;
151 }
152 chg -= rg->to - rg->from;
153 }
154 return chg;
155}
156
157static long region_truncate(struct list_head *head, long end)
158{
159 struct file_region *rg, *trg;
160 long chg = 0;
161
162 /* Locate the region we are either in or before. */
163 list_for_each_entry(rg, head, link)
164 if (end <= rg->to)
165 break;
166 if (&rg->link == head)
167 return 0;
168
169 /* If we are in the middle of a region then adjust it. */
170 if (end > rg->from) {
171 chg = rg->to - end;
172 rg->to = end;
173 rg = list_entry(rg->link.next, typeof(*rg), link);
174 }
175
176 /* Drop any remaining regions. */
177 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
178 if (&rg->link == head)
179 break;
180 chg += rg->to - rg->from;
181 list_del(&rg->link);
182 kfree(rg);
183 }
184 return chg;
185}
186
Andy Whitcroft84afd992008-07-23 21:27:32 -0700187static long region_count(struct list_head *head, long f, long t)
188{
189 struct file_region *rg;
190 long chg = 0;
191
192 /* Locate each segment we overlap with, and count that overlap. */
193 list_for_each_entry(rg, head, link) {
194 int seg_from;
195 int seg_to;
196
197 if (rg->to <= f)
198 continue;
199 if (rg->from >= t)
200 break;
201
202 seg_from = max(rg->from, f);
203 seg_to = min(rg->to, t);
204
205 chg += seg_to - seg_from;
206 }
207
208 return chg;
209}
210
Andy Whitcroft96822902008-07-23 21:27:29 -0700211/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700212 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700213 * the mapping, in pagecache page units; huge pages here.
214 */
Andi Kleena5516432008-07-23 21:27:41 -0700215static pgoff_t vma_hugecache_offset(struct hstate *h,
216 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700217{
Andi Kleena5516432008-07-23 21:27:41 -0700218 return ((address - vma->vm_start) >> huge_page_shift(h)) +
219 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700220}
221
Andy Whitcroft84afd992008-07-23 21:27:32 -0700222/*
Mel Gorman08fba692009-01-06 14:38:53 -0800223 * Return the size of the pages allocated when backing a VMA. In the majority
224 * cases this will be same size as used by the page table entries.
225 */
226unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
227{
228 struct hstate *hstate;
229
230 if (!is_vm_hugetlb_page(vma))
231 return PAGE_SIZE;
232
233 hstate = hstate_vma(vma);
234
235 return 1UL << (hstate->order + PAGE_SHIFT);
236}
237
238/*
Andy Whitcroft84afd992008-07-23 21:27:32 -0700239 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
240 * bits of the reservation map pointer, which are always clear due to
241 * alignment.
242 */
243#define HPAGE_RESV_OWNER (1UL << 0)
244#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700245#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700246
Mel Gormana1e78772008-07-23 21:27:23 -0700247/*
248 * These helpers are used to track how many pages are reserved for
249 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
250 * is guaranteed to have their future faults succeed.
251 *
252 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
253 * the reserve counters are updated with the hugetlb_lock held. It is safe
254 * to reset the VMA at fork() time as it is not in use yet and there is no
255 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700256 *
257 * The private mapping reservation is represented in a subtly different
258 * manner to a shared mapping. A shared mapping has a region map associated
259 * with the underlying file, this region map represents the backing file
260 * pages which have ever had a reservation assigned which this persists even
261 * after the page is instantiated. A private mapping has a region map
262 * associated with the original mmap which is attached to all VMAs which
263 * reference it, this region map represents those offsets which have consumed
264 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -0700265 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700266static unsigned long get_vma_private_data(struct vm_area_struct *vma)
267{
268 return (unsigned long)vma->vm_private_data;
269}
270
271static void set_vma_private_data(struct vm_area_struct *vma,
272 unsigned long value)
273{
274 vma->vm_private_data = (void *)value;
275}
276
Andy Whitcroft84afd992008-07-23 21:27:32 -0700277struct resv_map {
278 struct kref refs;
279 struct list_head regions;
280};
281
Harvey Harrison2a4b3de2008-10-18 20:27:06 -0700282static struct resv_map *resv_map_alloc(void)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700283{
284 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
285 if (!resv_map)
286 return NULL;
287
288 kref_init(&resv_map->refs);
289 INIT_LIST_HEAD(&resv_map->regions);
290
291 return resv_map;
292}
293
Harvey Harrison2a4b3de2008-10-18 20:27:06 -0700294static void resv_map_release(struct kref *ref)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700295{
296 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
297
298 /* Clear out any active regions before we release the map. */
299 region_truncate(&resv_map->regions, 0);
300 kfree(resv_map);
301}
302
303static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700304{
305 VM_BUG_ON(!is_vm_hugetlb_page(vma));
306 if (!(vma->vm_flags & VM_SHARED))
Andy Whitcroft84afd992008-07-23 21:27:32 -0700307 return (struct resv_map *)(get_vma_private_data(vma) &
308 ~HPAGE_RESV_MASK);
Harvey Harrison2a4b3de2008-10-18 20:27:06 -0700309 return NULL;
Mel Gormana1e78772008-07-23 21:27:23 -0700310}
311
Andy Whitcroft84afd992008-07-23 21:27:32 -0700312static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -0700313{
314 VM_BUG_ON(!is_vm_hugetlb_page(vma));
315 VM_BUG_ON(vma->vm_flags & VM_SHARED);
316
Andy Whitcroft84afd992008-07-23 21:27:32 -0700317 set_vma_private_data(vma, (get_vma_private_data(vma) &
318 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700319}
320
321static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
322{
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700323 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700324 VM_BUG_ON(vma->vm_flags & VM_SHARED);
325
326 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700327}
328
329static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
330{
331 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700332
333 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700334}
335
336/* Decrement the reserved pages in the hugepage pool by one */
Andi Kleena5516432008-07-23 21:27:41 -0700337static void decrement_hugepage_resv_vma(struct hstate *h,
338 struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700339{
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700340 if (vma->vm_flags & VM_NORESERVE)
341 return;
342
Mel Gormana1e78772008-07-23 21:27:23 -0700343 if (vma->vm_flags & VM_SHARED) {
344 /* Shared mappings always use reserves */
Andi Kleena5516432008-07-23 21:27:41 -0700345 h->resv_huge_pages--;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700346 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Mel Gormana1e78772008-07-23 21:27:23 -0700347 /*
348 * Only the process that called mmap() has reserves for
349 * private mappings.
350 */
Andi Kleena5516432008-07-23 21:27:41 -0700351 h->resv_huge_pages--;
Mel Gormana1e78772008-07-23 21:27:23 -0700352 }
353}
354
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700355/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700356void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
357{
358 VM_BUG_ON(!is_vm_hugetlb_page(vma));
359 if (!(vma->vm_flags & VM_SHARED))
360 vma->vm_private_data = (void *)0;
361}
362
363/* Returns true if the VMA has associated reserve pages */
Mel Gorman7f09ca52008-07-23 21:27:58 -0700364static int vma_has_reserves(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700365{
366 if (vma->vm_flags & VM_SHARED)
Mel Gorman7f09ca52008-07-23 21:27:58 -0700367 return 1;
368 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
369 return 1;
370 return 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700371}
372
Andy Whitcroft69d177c2008-11-06 12:53:26 -0800373static void clear_gigantic_page(struct page *page,
374 unsigned long addr, unsigned long sz)
375{
376 int i;
377 struct page *p = page;
378
379 might_sleep();
380 for (i = 0; i < sz/PAGE_SIZE; i++, p = mem_map_next(p, page, i)) {
381 cond_resched();
382 clear_user_highpage(p, addr + i * PAGE_SIZE);
383 }
384}
Andi Kleena5516432008-07-23 21:27:41 -0700385static void clear_huge_page(struct page *page,
386 unsigned long addr, unsigned long sz)
David Gibson79ac6ba2006-03-22 00:08:51 -0800387{
388 int i;
389
Andy Whitcroft69d177c2008-11-06 12:53:26 -0800390 if (unlikely(sz > MAX_ORDER_NR_PAGES))
391 return clear_gigantic_page(page, addr, sz);
392
David Gibson79ac6ba2006-03-22 00:08:51 -0800393 might_sleep();
Andi Kleena5516432008-07-23 21:27:41 -0700394 for (i = 0; i < sz/PAGE_SIZE; i++) {
David Gibson79ac6ba2006-03-22 00:08:51 -0800395 cond_resched();
Ralf Baechle281e0e32007-10-01 01:20:10 -0700396 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
David Gibson79ac6ba2006-03-22 00:08:51 -0800397 }
398}
399
Andy Whitcroft69d177c2008-11-06 12:53:26 -0800400static void copy_gigantic_page(struct page *dst, struct page *src,
401 unsigned long addr, struct vm_area_struct *vma)
402{
403 int i;
404 struct hstate *h = hstate_vma(vma);
405 struct page *dst_base = dst;
406 struct page *src_base = src;
407 might_sleep();
408 for (i = 0; i < pages_per_huge_page(h); ) {
409 cond_resched();
410 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
411
412 i++;
413 dst = mem_map_next(dst, dst_base, i);
414 src = mem_map_next(src, src_base, i);
415 }
416}
David Gibson79ac6ba2006-03-22 00:08:51 -0800417static void copy_huge_page(struct page *dst, struct page *src,
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000418 unsigned long addr, struct vm_area_struct *vma)
David Gibson79ac6ba2006-03-22 00:08:51 -0800419{
420 int i;
Andi Kleena5516432008-07-23 21:27:41 -0700421 struct hstate *h = hstate_vma(vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800422
Andy Whitcroft69d177c2008-11-06 12:53:26 -0800423 if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES))
424 return copy_gigantic_page(dst, src, addr, vma);
425
David Gibson79ac6ba2006-03-22 00:08:51 -0800426 might_sleep();
Andi Kleena5516432008-07-23 21:27:41 -0700427 for (i = 0; i < pages_per_huge_page(h); i++) {
David Gibson79ac6ba2006-03-22 00:08:51 -0800428 cond_resched();
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000429 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800430 }
431}
432
Andi Kleena5516432008-07-23 21:27:41 -0700433static void enqueue_huge_page(struct hstate *h, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 int nid = page_to_nid(page);
Andi Kleena5516432008-07-23 21:27:41 -0700436 list_add(&page->lru, &h->hugepage_freelists[nid]);
437 h->free_huge_pages++;
438 h->free_huge_pages_node[nid]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Andi Kleena5516432008-07-23 21:27:41 -0700441static struct page *dequeue_huge_page(struct hstate *h)
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800442{
443 int nid;
444 struct page *page = NULL;
445
446 for (nid = 0; nid < MAX_NUMNODES; ++nid) {
Andi Kleena5516432008-07-23 21:27:41 -0700447 if (!list_empty(&h->hugepage_freelists[nid])) {
448 page = list_entry(h->hugepage_freelists[nid].next,
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800449 struct page, lru);
450 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700451 h->free_huge_pages--;
452 h->free_huge_pages_node[nid]--;
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800453 break;
454 }
455 }
456 return page;
457}
458
Andi Kleena5516432008-07-23 21:27:41 -0700459static struct page *dequeue_huge_page_vma(struct hstate *h,
460 struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700461 unsigned long address, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Nishanth Aravamudan31a5c6e2007-07-15 23:38:02 -0700463 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct page *page = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700465 struct mempolicy *mpol;
Mel Gorman19770b32008-04-28 02:12:18 -0700466 nodemask_t *nodemask;
Mel Gorman396faf02007-07-17 04:03:13 -0700467 struct zonelist *zonelist = huge_zonelist(vma, address,
Mel Gorman19770b32008-04-28 02:12:18 -0700468 htlb_alloc_mask, &mpol, &nodemask);
Mel Gormandd1a2392008-04-28 02:12:17 -0700469 struct zone *zone;
470 struct zoneref *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Mel Gormana1e78772008-07-23 21:27:23 -0700472 /*
473 * A child process with MAP_PRIVATE mappings created by their parent
474 * have no page reserves. This check ensures that reservations are
475 * not "stolen". The child may still get SIGKILLed
476 */
Mel Gorman7f09ca52008-07-23 21:27:58 -0700477 if (!vma_has_reserves(vma) &&
Andi Kleena5516432008-07-23 21:27:41 -0700478 h->free_huge_pages - h->resv_huge_pages == 0)
Mel Gormana1e78772008-07-23 21:27:23 -0700479 return NULL;
480
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700481 /* If reserves cannot be used, ensure enough pages are in the pool */
Andi Kleena5516432008-07-23 21:27:41 -0700482 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700483 return NULL;
484
Mel Gorman19770b32008-04-28 02:12:18 -0700485 for_each_zone_zonelist_nodemask(zone, z, zonelist,
486 MAX_NR_ZONES - 1, nodemask) {
Mel Gorman54a6eb52008-04-28 02:12:16 -0700487 nid = zone_to_nid(zone);
488 if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
Andi Kleena5516432008-07-23 21:27:41 -0700489 !list_empty(&h->hugepage_freelists[nid])) {
490 page = list_entry(h->hugepage_freelists[nid].next,
Andrew Morton3abf7af2007-07-19 01:49:08 -0700491 struct page, lru);
492 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700493 h->free_huge_pages--;
494 h->free_huge_pages_node[nid]--;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700495
496 if (!avoid_reserve)
Andi Kleena5516432008-07-23 21:27:41 -0700497 decrement_hugepage_resv_vma(h, vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700498
Ken Chen5ab3ee72007-07-23 18:44:00 -0700499 break;
Andrew Morton3abf7af2007-07-19 01:49:08 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700502 mpol_cond_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return page;
504}
505
Andi Kleena5516432008-07-23 21:27:41 -0700506static void update_and_free_page(struct hstate *h, struct page *page)
Adam Litke6af2acb2007-10-16 01:26:16 -0700507{
508 int i;
Andi Kleena5516432008-07-23 21:27:41 -0700509
Andy Whitcroft18229df2008-11-06 12:53:27 -0800510 VM_BUG_ON(h->order >= MAX_ORDER);
511
Andi Kleena5516432008-07-23 21:27:41 -0700512 h->nr_huge_pages--;
513 h->nr_huge_pages_node[page_to_nid(page)]--;
514 for (i = 0; i < pages_per_huge_page(h); i++) {
Adam Litke6af2acb2007-10-16 01:26:16 -0700515 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
516 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
517 1 << PG_private | 1<< PG_writeback);
518 }
519 set_compound_page_dtor(page, NULL);
520 set_page_refcounted(page);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700521 arch_release_hugepage(page);
Andi Kleena5516432008-07-23 21:27:41 -0700522 __free_pages(page, huge_page_order(h));
Adam Litke6af2acb2007-10-16 01:26:16 -0700523}
524
Andi Kleene5ff2152008-07-23 21:27:42 -0700525struct hstate *size_to_hstate(unsigned long size)
526{
527 struct hstate *h;
528
529 for_each_hstate(h) {
530 if (huge_page_size(h) == size)
531 return h;
532 }
533 return NULL;
534}
535
David Gibson27a85ef2006-03-22 00:08:56 -0800536static void free_huge_page(struct page *page)
537{
Andi Kleena5516432008-07-23 21:27:41 -0700538 /*
539 * Can't pass hstate in here because it is called from the
540 * compound page destructor.
541 */
Andi Kleene5ff2152008-07-23 21:27:42 -0700542 struct hstate *h = page_hstate(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700543 int nid = page_to_nid(page);
Adam Litkec79fb752007-11-14 16:59:38 -0800544 struct address_space *mapping;
David Gibson27a85ef2006-03-22 00:08:56 -0800545
Adam Litkec79fb752007-11-14 16:59:38 -0800546 mapping = (struct address_space *) page_private(page);
Andy Whitcrofte5df70a2008-02-23 15:23:32 -0800547 set_page_private(page, 0);
Adam Litke7893d1d2007-10-16 01:26:18 -0700548 BUG_ON(page_count(page));
David Gibson27a85ef2006-03-22 00:08:56 -0800549 INIT_LIST_HEAD(&page->lru);
550
551 spin_lock(&hugetlb_lock);
Andi Kleenaa888a72008-07-23 21:27:47 -0700552 if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
Andi Kleena5516432008-07-23 21:27:41 -0700553 update_and_free_page(h, page);
554 h->surplus_huge_pages--;
555 h->surplus_huge_pages_node[nid]--;
Adam Litke7893d1d2007-10-16 01:26:18 -0700556 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700557 enqueue_huge_page(h, page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700558 }
David Gibson27a85ef2006-03-22 00:08:56 -0800559 spin_unlock(&hugetlb_lock);
Adam Litkec79fb752007-11-14 16:59:38 -0800560 if (mapping)
Adam Litke9a119c02007-11-14 16:59:41 -0800561 hugetlb_put_quota(mapping, 1);
David Gibson27a85ef2006-03-22 00:08:56 -0800562}
563
Adam Litke7893d1d2007-10-16 01:26:18 -0700564/*
565 * Increment or decrement surplus_huge_pages. Keep node-specific counters
566 * balanced by operating on them in a round-robin fashion.
567 * Returns 1 if an adjustment was made.
568 */
Andi Kleena5516432008-07-23 21:27:41 -0700569static int adjust_pool_surplus(struct hstate *h, int delta)
Adam Litke7893d1d2007-10-16 01:26:18 -0700570{
571 static int prev_nid;
572 int nid = prev_nid;
573 int ret = 0;
574
575 VM_BUG_ON(delta != -1 && delta != 1);
576 do {
577 nid = next_node(nid, node_online_map);
578 if (nid == MAX_NUMNODES)
579 nid = first_node(node_online_map);
580
581 /* To shrink on this node, there must be a surplus page */
Andi Kleena5516432008-07-23 21:27:41 -0700582 if (delta < 0 && !h->surplus_huge_pages_node[nid])
Adam Litke7893d1d2007-10-16 01:26:18 -0700583 continue;
584 /* Surplus cannot exceed the total number of pages */
Andi Kleena5516432008-07-23 21:27:41 -0700585 if (delta > 0 && h->surplus_huge_pages_node[nid] >=
586 h->nr_huge_pages_node[nid])
Adam Litke7893d1d2007-10-16 01:26:18 -0700587 continue;
588
Andi Kleena5516432008-07-23 21:27:41 -0700589 h->surplus_huge_pages += delta;
590 h->surplus_huge_pages_node[nid] += delta;
Adam Litke7893d1d2007-10-16 01:26:18 -0700591 ret = 1;
592 break;
593 } while (nid != prev_nid);
594
595 prev_nid = nid;
596 return ret;
597}
598
Andi Kleena5516432008-07-23 21:27:41 -0700599static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700600{
601 set_compound_page_dtor(page, free_huge_page);
602 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700603 h->nr_huge_pages++;
604 h->nr_huge_pages_node[nid]++;
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700605 spin_unlock(&hugetlb_lock);
606 put_page(page); /* free it into the hugepage allocator */
607}
608
Andi Kleena5516432008-07-23 21:27:41 -0700609static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct page *page;
Joe Jinf96efd52007-07-15 23:38:12 -0700612
Andi Kleenaa888a72008-07-23 21:27:47 -0700613 if (h->order >= MAX_ORDER)
614 return NULL;
615
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700616 page = alloc_pages_node(nid,
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700617 htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
618 __GFP_REPEAT|__GFP_NOWARN,
Andi Kleena5516432008-07-23 21:27:41 -0700619 huge_page_order(h));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (page) {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700621 if (arch_prepare_hugepage(page)) {
Gerald Schaefercaff3a22008-08-12 15:08:38 -0700622 __free_pages(page, huge_page_order(h));
Harvey Harrison7b8ee842008-04-28 14:13:19 -0700623 return NULL;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700624 }
Andi Kleena5516432008-07-23 21:27:41 -0700625 prep_new_huge_page(h, page, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700627
628 return page;
629}
630
Andi Kleen5ced66c2008-07-23 21:27:45 -0700631/*
632 * Use a helper variable to find the next node and then
633 * copy it back to hugetlb_next_nid afterwards:
634 * otherwise there's a window in which a racer might
635 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
636 * But we don't need to use a spin_lock here: it really
637 * doesn't matter if occasionally a racer chooses the
638 * same nid as we do. Move nid forward in the mask even
639 * if we just successfully allocated a hugepage so that
640 * the next caller gets hugepages on the next node.
641 */
642static int hstate_next_node(struct hstate *h)
643{
644 int next_nid;
645 next_nid = next_node(h->hugetlb_next_nid, node_online_map);
646 if (next_nid == MAX_NUMNODES)
647 next_nid = first_node(node_online_map);
648 h->hugetlb_next_nid = next_nid;
649 return next_nid;
650}
651
Andi Kleena5516432008-07-23 21:27:41 -0700652static int alloc_fresh_huge_page(struct hstate *h)
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700653{
654 struct page *page;
655 int start_nid;
656 int next_nid;
657 int ret = 0;
658
Andi Kleena5516432008-07-23 21:27:41 -0700659 start_nid = h->hugetlb_next_nid;
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700660
661 do {
Andi Kleena5516432008-07-23 21:27:41 -0700662 page = alloc_fresh_huge_page_node(h, h->hugetlb_next_nid);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700663 if (page)
664 ret = 1;
Andi Kleen5ced66c2008-07-23 21:27:45 -0700665 next_nid = hstate_next_node(h);
Andi Kleena5516432008-07-23 21:27:41 -0700666 } while (!page && h->hugetlb_next_nid != start_nid);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700667
Adam Litke3b116302008-04-28 02:13:06 -0700668 if (ret)
669 count_vm_event(HTLB_BUDDY_PGALLOC);
670 else
671 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
672
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700673 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Andi Kleena5516432008-07-23 21:27:41 -0700676static struct page *alloc_buddy_huge_page(struct hstate *h,
677 struct vm_area_struct *vma, unsigned long address)
Adam Litke7893d1d2007-10-16 01:26:18 -0700678{
679 struct page *page;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800680 unsigned int nid;
Adam Litke7893d1d2007-10-16 01:26:18 -0700681
Andi Kleenaa888a72008-07-23 21:27:47 -0700682 if (h->order >= MAX_ORDER)
683 return NULL;
684
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800685 /*
686 * Assume we will successfully allocate the surplus page to
687 * prevent racing processes from causing the surplus to exceed
688 * overcommit
689 *
690 * This however introduces a different race, where a process B
691 * tries to grow the static hugepage pool while alloc_pages() is
692 * called by process A. B will only examine the per-node
693 * counters in determining if surplus huge pages can be
694 * converted to normal huge pages in adjust_pool_surplus(). A
695 * won't be able to increment the per-node counter, until the
696 * lock is dropped by B, but B doesn't drop hugetlb_lock until
697 * no more huge pages can be converted from surplus to normal
698 * state (and doesn't try to convert again). Thus, we have a
699 * case where a surplus huge page exists, the pool is grown, and
700 * the surplus huge page still exists after, even though it
701 * should just have been converted to a normal huge page. This
702 * does not leak memory, though, as the hugepage will be freed
703 * once it is out of use. It also does not allow the counters to
704 * go out of whack in adjust_pool_surplus() as we don't modify
705 * the node values until we've gotten the hugepage and only the
706 * per-node value is checked there.
707 */
708 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700709 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800710 spin_unlock(&hugetlb_lock);
711 return NULL;
712 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700713 h->nr_huge_pages++;
714 h->surplus_huge_pages++;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800715 }
716 spin_unlock(&hugetlb_lock);
717
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700718 page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
719 __GFP_REPEAT|__GFP_NOWARN,
Andi Kleena5516432008-07-23 21:27:41 -0700720 huge_page_order(h));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800721
Gerald Schaefercaff3a22008-08-12 15:08:38 -0700722 if (page && arch_prepare_hugepage(page)) {
723 __free_pages(page, huge_page_order(h));
724 return NULL;
725 }
726
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800727 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700728 if (page) {
Adam Litke2668db92008-03-10 11:43:50 -0700729 /*
730 * This page is now managed by the hugetlb allocator and has
731 * no users -- drop the buddy allocator's reference.
732 */
733 put_page_testzero(page);
734 VM_BUG_ON(page_count(page));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800735 nid = page_to_nid(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700736 set_compound_page_dtor(page, free_huge_page);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800737 /*
738 * We incremented the global counters already
739 */
Andi Kleena5516432008-07-23 21:27:41 -0700740 h->nr_huge_pages_node[nid]++;
741 h->surplus_huge_pages_node[nid]++;
Adam Litke3b116302008-04-28 02:13:06 -0700742 __count_vm_event(HTLB_BUDDY_PGALLOC);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800743 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700744 h->nr_huge_pages--;
745 h->surplus_huge_pages--;
Adam Litke3b116302008-04-28 02:13:06 -0700746 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Adam Litke7893d1d2007-10-16 01:26:18 -0700747 }
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800748 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700749
750 return page;
751}
752
Adam Litkee4e574b2007-10-16 01:26:19 -0700753/*
754 * Increase the hugetlb pool such that it can accomodate a reservation
755 * of size 'delta'.
756 */
Andi Kleena5516432008-07-23 21:27:41 -0700757static int gather_surplus_pages(struct hstate *h, int delta)
Adam Litkee4e574b2007-10-16 01:26:19 -0700758{
759 struct list_head surplus_list;
760 struct page *page, *tmp;
761 int ret, i;
762 int needed, allocated;
763
Andi Kleena5516432008-07-23 21:27:41 -0700764 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800765 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -0700766 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700767 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800768 }
Adam Litkee4e574b2007-10-16 01:26:19 -0700769
770 allocated = 0;
771 INIT_LIST_HEAD(&surplus_list);
772
773 ret = -ENOMEM;
774retry:
775 spin_unlock(&hugetlb_lock);
776 for (i = 0; i < needed; i++) {
Andi Kleena5516432008-07-23 21:27:41 -0700777 page = alloc_buddy_huge_page(h, NULL, 0);
Adam Litkee4e574b2007-10-16 01:26:19 -0700778 if (!page) {
779 /*
780 * We were not able to allocate enough pages to
781 * satisfy the entire reservation so we free what
782 * we've allocated so far.
783 */
784 spin_lock(&hugetlb_lock);
785 needed = 0;
786 goto free;
787 }
788
789 list_add(&page->lru, &surplus_list);
790 }
791 allocated += needed;
792
793 /*
794 * After retaking hugetlb_lock, we need to recalculate 'needed'
795 * because either resv_huge_pages or free_huge_pages may have changed.
796 */
797 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700798 needed = (h->resv_huge_pages + delta) -
799 (h->free_huge_pages + allocated);
Adam Litkee4e574b2007-10-16 01:26:19 -0700800 if (needed > 0)
801 goto retry;
802
803 /*
804 * The surplus_list now contains _at_least_ the number of extra pages
805 * needed to accomodate the reservation. Add the appropriate number
806 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -0800807 * allocator. Commit the entire reservation here to prevent another
808 * process from stealing the pages as they are added to the pool but
809 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -0700810 */
811 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -0700812 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700813 ret = 0;
814free:
Adam Litke19fc3f02008-04-28 02:12:20 -0700815 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -0700816 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -0700817 if ((--needed) < 0)
818 break;
Adam Litkee4e574b2007-10-16 01:26:19 -0700819 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700820 enqueue_huge_page(h, page);
Adam Litke19fc3f02008-04-28 02:12:20 -0700821 }
822
823 /* Free unnecessary surplus pages to the buddy allocator */
824 if (!list_empty(&surplus_list)) {
825 spin_unlock(&hugetlb_lock);
826 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
827 list_del(&page->lru);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700828 /*
Adam Litke2668db92008-03-10 11:43:50 -0700829 * The page has a reference count of zero already, so
830 * call free_huge_page directly instead of using
831 * put_page. This must be done with hugetlb_lock
Adam Litkeaf767cb2007-10-16 01:26:25 -0700832 * unlocked which is safe because free_huge_page takes
833 * hugetlb_lock before deciding how to free the page.
834 */
Adam Litke2668db92008-03-10 11:43:50 -0700835 free_huge_page(page);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700836 }
Adam Litke19fc3f02008-04-28 02:12:20 -0700837 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -0700838 }
839
840 return ret;
841}
842
843/*
844 * When releasing a hugetlb pool reservation, any surplus pages that were
845 * allocated to satisfy the reservation must be explicitly freed if they were
846 * never used.
847 */
Andi Kleena5516432008-07-23 21:27:41 -0700848static void return_unused_surplus_pages(struct hstate *h,
849 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -0700850{
851 static int nid = -1;
852 struct page *page;
853 unsigned long nr_pages;
854
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700855 /*
856 * We want to release as many surplus pages as possible, spread
857 * evenly across all nodes. Iterate across all nodes until we
858 * can no longer free unreserved surplus pages. This occurs when
859 * the nodes with surplus pages have no free pages.
860 */
861 unsigned long remaining_iterations = num_online_nodes();
862
Adam Litkeac09b3a2008-03-04 14:29:38 -0800863 /* Uncommit the reservation */
Andi Kleena5516432008-07-23 21:27:41 -0700864 h->resv_huge_pages -= unused_resv_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800865
Andi Kleenaa888a72008-07-23 21:27:47 -0700866 /* Cannot return gigantic pages currently */
867 if (h->order >= MAX_ORDER)
868 return;
869
Andi Kleena5516432008-07-23 21:27:41 -0700870 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -0700871
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700872 while (remaining_iterations-- && nr_pages) {
Adam Litkee4e574b2007-10-16 01:26:19 -0700873 nid = next_node(nid, node_online_map);
874 if (nid == MAX_NUMNODES)
875 nid = first_node(node_online_map);
876
Andi Kleena5516432008-07-23 21:27:41 -0700877 if (!h->surplus_huge_pages_node[nid])
Adam Litkee4e574b2007-10-16 01:26:19 -0700878 continue;
879
Andi Kleena5516432008-07-23 21:27:41 -0700880 if (!list_empty(&h->hugepage_freelists[nid])) {
881 page = list_entry(h->hugepage_freelists[nid].next,
Adam Litkee4e574b2007-10-16 01:26:19 -0700882 struct page, lru);
883 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700884 update_and_free_page(h, page);
885 h->free_huge_pages--;
886 h->free_huge_pages_node[nid]--;
887 h->surplus_huge_pages--;
888 h->surplus_huge_pages_node[nid]--;
Adam Litkee4e574b2007-10-16 01:26:19 -0700889 nr_pages--;
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700890 remaining_iterations = num_online_nodes();
Adam Litkee4e574b2007-10-16 01:26:19 -0700891 }
892 }
893}
894
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700895/*
896 * Determine if the huge page at addr within the vma has an associated
897 * reservation. Where it does not we will need to logically increase
898 * reservation and actually increase quota before an allocation can occur.
899 * Where any new reservation would be required the reservation change is
900 * prepared, but not committed. Once the page has been quota'd allocated
901 * an instantiated the change should be committed via vma_commit_reservation.
902 * No action is required on failure.
903 */
Andi Kleena5516432008-07-23 21:27:41 -0700904static int vma_needs_reservation(struct hstate *h,
905 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700906{
907 struct address_space *mapping = vma->vm_file->f_mapping;
908 struct inode *inode = mapping->host;
909
910 if (vma->vm_flags & VM_SHARED) {
Andi Kleena5516432008-07-23 21:27:41 -0700911 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700912 return region_chg(&inode->i_mapping->private_list,
913 idx, idx + 1);
914
Andy Whitcroft84afd992008-07-23 21:27:32 -0700915 } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
916 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700917
Andy Whitcroft84afd992008-07-23 21:27:32 -0700918 } else {
919 int err;
Andi Kleena5516432008-07-23 21:27:41 -0700920 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700921 struct resv_map *reservations = vma_resv_map(vma);
922
923 err = region_chg(&reservations->regions, idx, idx + 1);
924 if (err < 0)
925 return err;
926 return 0;
927 }
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700928}
Andi Kleena5516432008-07-23 21:27:41 -0700929static void vma_commit_reservation(struct hstate *h,
930 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700931{
932 struct address_space *mapping = vma->vm_file->f_mapping;
933 struct inode *inode = mapping->host;
934
935 if (vma->vm_flags & VM_SHARED) {
Andi Kleena5516432008-07-23 21:27:41 -0700936 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700937 region_add(&inode->i_mapping->private_list, idx, idx + 1);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700938
939 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Andi Kleena5516432008-07-23 21:27:41 -0700940 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700941 struct resv_map *reservations = vma_resv_map(vma);
942
943 /* Mark this page used in the map. */
944 region_add(&reservations->regions, idx, idx + 1);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700945 }
946}
947
David Gibson27a85ef2006-03-22 00:08:56 -0800948static struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700949 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Andi Kleena5516432008-07-23 21:27:41 -0700951 struct hstate *h = hstate_vma(vma);
Adam Litke348ea202007-11-14 16:59:37 -0800952 struct page *page;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800953 struct address_space *mapping = vma->vm_file->f_mapping;
Mel Gormana1e78772008-07-23 21:27:23 -0700954 struct inode *inode = mapping->host;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700955 unsigned int chg;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800956
Mel Gormana1e78772008-07-23 21:27:23 -0700957 /*
958 * Processes that did not create the mapping will have no reserves and
959 * will not have accounted against quota. Check that the quota can be
960 * made before satisfying the allocation
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700961 * MAP_NORESERVE mappings may also need pages and quota allocated
962 * if no reserve mapping overlaps.
Mel Gormana1e78772008-07-23 21:27:23 -0700963 */
Andi Kleena5516432008-07-23 21:27:41 -0700964 chg = vma_needs_reservation(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700965 if (chg < 0)
966 return ERR_PTR(chg);
967 if (chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700968 if (hugetlb_get_quota(inode->i_mapping, chg))
969 return ERR_PTR(-ENOSPC);
Mel Gormana1e78772008-07-23 21:27:23 -0700970
971 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700972 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
Mel Gormana1e78772008-07-23 21:27:23 -0700973 spin_unlock(&hugetlb_lock);
974
975 if (!page) {
Andi Kleena5516432008-07-23 21:27:41 -0700976 page = alloc_buddy_huge_page(h, vma, addr);
Mel Gormana1e78772008-07-23 21:27:23 -0700977 if (!page) {
978 hugetlb_put_quota(inode->i_mapping, chg);
979 return ERR_PTR(-VM_FAULT_OOM);
980 }
981 }
982
983 set_page_refcounted(page);
984 set_page_private(page, (unsigned long) mapping);
985
Andi Kleena5516432008-07-23 21:27:41 -0700986 vma_commit_reservation(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700987
Adam Litke90d8b7e2007-11-14 16:59:42 -0800988 return page;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800989}
990
Jon Tollefson53ba51d2008-07-23 21:27:52 -0700991__attribute__((weak)) int alloc_bootmem_huge_page(struct hstate *h)
Andi Kleenaa888a72008-07-23 21:27:47 -0700992{
993 struct huge_bootmem_page *m;
994 int nr_nodes = nodes_weight(node_online_map);
995
996 while (nr_nodes) {
997 void *addr;
998
999 addr = __alloc_bootmem_node_nopanic(
1000 NODE_DATA(h->hugetlb_next_nid),
1001 huge_page_size(h), huge_page_size(h), 0);
1002
1003 if (addr) {
1004 /*
1005 * Use the beginning of the huge page to store the
1006 * huge_bootmem_page struct (until gather_bootmem
1007 * puts them into the mem_map).
1008 */
1009 m = addr;
1010 if (m)
1011 goto found;
1012 }
1013 hstate_next_node(h);
1014 nr_nodes--;
1015 }
1016 return 0;
1017
1018found:
1019 BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
1020 /* Put them into a private list first because mem_map is not up yet */
1021 list_add(&m->list, &huge_boot_pages);
1022 m->hstate = h;
1023 return 1;
1024}
1025
Andy Whitcroft18229df2008-11-06 12:53:27 -08001026static void prep_compound_huge_page(struct page *page, int order)
1027{
1028 if (unlikely(order > (MAX_ORDER - 1)))
1029 prep_compound_gigantic_page(page, order);
1030 else
1031 prep_compound_page(page, order);
1032}
1033
Andi Kleenaa888a72008-07-23 21:27:47 -07001034/* Put bootmem huge pages into the standard lists after mem_map is up */
1035static void __init gather_bootmem_prealloc(void)
1036{
1037 struct huge_bootmem_page *m;
1038
1039 list_for_each_entry(m, &huge_boot_pages, list) {
1040 struct page *page = virt_to_page(m);
1041 struct hstate *h = m->hstate;
1042 __ClearPageReserved(page);
1043 WARN_ON(page_count(page) != 1);
Andy Whitcroft18229df2008-11-06 12:53:27 -08001044 prep_compound_huge_page(page, h->order);
Andi Kleenaa888a72008-07-23 21:27:47 -07001045 prep_new_huge_page(h, page, page_to_nid(page));
1046 }
1047}
1048
Andi Kleen8faa8b02008-07-23 21:27:48 -07001049static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Andi Kleene5ff2152008-07-23 21:27:42 -07001053 for (i = 0; i < h->max_huge_pages; ++i) {
Andi Kleenaa888a72008-07-23 21:27:47 -07001054 if (h->order >= MAX_ORDER) {
1055 if (!alloc_bootmem_huge_page(h))
1056 break;
1057 } else if (!alloc_fresh_huge_page(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Andi Kleen8faa8b02008-07-23 21:27:48 -07001060 h->max_huge_pages = i;
Andi Kleene5ff2152008-07-23 21:27:42 -07001061}
1062
1063static void __init hugetlb_init_hstates(void)
1064{
1065 struct hstate *h;
1066
1067 for_each_hstate(h) {
Andi Kleen8faa8b02008-07-23 21:27:48 -07001068 /* oversize hugepages were init'ed in early boot */
1069 if (h->order < MAX_ORDER)
1070 hugetlb_hstate_alloc_pages(h);
Andi Kleene5ff2152008-07-23 21:27:42 -07001071 }
1072}
1073
Andi Kleen4abd32d2008-07-23 21:27:49 -07001074static char * __init memfmt(char *buf, unsigned long n)
1075{
1076 if (n >= (1UL << 30))
1077 sprintf(buf, "%lu GB", n >> 30);
1078 else if (n >= (1UL << 20))
1079 sprintf(buf, "%lu MB", n >> 20);
1080 else
1081 sprintf(buf, "%lu KB", n >> 10);
1082 return buf;
1083}
1084
Andi Kleene5ff2152008-07-23 21:27:42 -07001085static void __init report_hugepages(void)
1086{
1087 struct hstate *h;
1088
1089 for_each_hstate(h) {
Andi Kleen4abd32d2008-07-23 21:27:49 -07001090 char buf[32];
1091 printk(KERN_INFO "HugeTLB registered %s page size, "
1092 "pre-allocated %ld pages\n",
1093 memfmt(buf, huge_page_size(h)),
1094 h->free_huge_pages);
Andi Kleene5ff2152008-07-23 21:27:42 -07001095 }
1096}
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098#ifdef CONFIG_HIGHMEM
Andi Kleena5516432008-07-23 21:27:41 -07001099static void try_to_free_low(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Christoph Lameter4415cc82006-09-25 23:31:55 -07001101 int i;
1102
Andi Kleenaa888a72008-07-23 21:27:47 -07001103 if (h->order >= MAX_ORDER)
1104 return;
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 for (i = 0; i < MAX_NUMNODES; ++i) {
1107 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -07001108 struct list_head *freel = &h->hugepage_freelists[i];
1109 list_for_each_entry_safe(page, next, freel, lru) {
1110 if (count >= h->nr_huge_pages)
Adam Litke6b0c8802007-10-16 01:26:23 -07001111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (PageHighMem(page))
1113 continue;
1114 list_del(&page->lru);
Andi Kleene5ff2152008-07-23 21:27:42 -07001115 update_and_free_page(h, page);
Andi Kleena5516432008-07-23 21:27:41 -07001116 h->free_huge_pages--;
1117 h->free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 }
1120}
1121#else
Andi Kleena5516432008-07-23 21:27:41 -07001122static inline void try_to_free_low(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124}
1125#endif
1126
Andi Kleena5516432008-07-23 21:27:41 -07001127#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Andi Kleene5ff2152008-07-23 21:27:42 -07001128static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Adam Litke7893d1d2007-10-16 01:26:18 -07001130 unsigned long min_count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Andi Kleenaa888a72008-07-23 21:27:47 -07001132 if (h->order >= MAX_ORDER)
1133 return h->max_huge_pages;
1134
Adam Litke7893d1d2007-10-16 01:26:18 -07001135 /*
1136 * Increase the pool size
1137 * First take pages out of surplus state. Then make up the
1138 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001139 *
1140 * We might race with alloc_buddy_huge_page() here and be unable
1141 * to convert a surplus huge page to a normal huge page. That is
1142 * not critical, though, it just means the overall size of the
1143 * pool might be one hugepage larger than it needs to be, but
1144 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -07001145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001147 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
1148 if (!adjust_pool_surplus(h, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -07001149 break;
1150 }
1151
Andi Kleena5516432008-07-23 21:27:41 -07001152 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -07001153 /*
1154 * If this allocation races such that we no longer need the
1155 * page, free_huge_page will handle it by freeing the page
1156 * and reducing the surplus.
1157 */
1158 spin_unlock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001159 ret = alloc_fresh_huge_page(h);
Adam Litke7893d1d2007-10-16 01:26:18 -07001160 spin_lock(&hugetlb_lock);
1161 if (!ret)
1162 goto out;
1163
1164 }
Adam Litke7893d1d2007-10-16 01:26:18 -07001165
1166 /*
1167 * Decrease the pool size
1168 * First return free pages to the buddy allocator (being careful
1169 * to keep enough around to satisfy reservations). Then place
1170 * pages into surplus state as needed so the pool will shrink
1171 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001172 *
1173 * By placing pages into the surplus state independent of the
1174 * overcommit value, we are allowing the surplus pool size to
1175 * exceed overcommit. There are few sane options here. Since
1176 * alloc_buddy_huge_page() is checking the global counter,
1177 * though, we'll note that we're not allowed to exceed surplus
1178 * and won't grow the pool anywhere else. Not until one of the
1179 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07001180 */
Andi Kleena5516432008-07-23 21:27:41 -07001181 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07001182 min_count = max(count, min_count);
Andi Kleena5516432008-07-23 21:27:41 -07001183 try_to_free_low(h, min_count);
1184 while (min_count < persistent_huge_pages(h)) {
1185 struct page *page = dequeue_huge_page(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (!page)
1187 break;
Andi Kleena5516432008-07-23 21:27:41 -07001188 update_and_free_page(h, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
Andi Kleena5516432008-07-23 21:27:41 -07001190 while (count < persistent_huge_pages(h)) {
1191 if (!adjust_pool_surplus(h, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07001192 break;
1193 }
1194out:
Andi Kleena5516432008-07-23 21:27:41 -07001195 ret = persistent_huge_pages(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001197 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001200#define HSTATE_ATTR_RO(_name) \
1201 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1202
1203#define HSTATE_ATTR(_name) \
1204 static struct kobj_attribute _name##_attr = \
1205 __ATTR(_name, 0644, _name##_show, _name##_store)
1206
1207static struct kobject *hugepages_kobj;
1208static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
1209
1210static struct hstate *kobj_to_hstate(struct kobject *kobj)
1211{
1212 int i;
1213 for (i = 0; i < HUGE_MAX_HSTATE; i++)
1214 if (hstate_kobjs[i] == kobj)
1215 return &hstates[i];
1216 BUG();
1217 return NULL;
1218}
1219
1220static ssize_t nr_hugepages_show(struct kobject *kobj,
1221 struct kobj_attribute *attr, char *buf)
1222{
1223 struct hstate *h = kobj_to_hstate(kobj);
1224 return sprintf(buf, "%lu\n", h->nr_huge_pages);
1225}
1226static ssize_t nr_hugepages_store(struct kobject *kobj,
1227 struct kobj_attribute *attr, const char *buf, size_t count)
1228{
1229 int err;
1230 unsigned long input;
1231 struct hstate *h = kobj_to_hstate(kobj);
1232
1233 err = strict_strtoul(buf, 10, &input);
1234 if (err)
1235 return 0;
1236
1237 h->max_huge_pages = set_max_huge_pages(h, input);
1238
1239 return count;
1240}
1241HSTATE_ATTR(nr_hugepages);
1242
1243static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
1244 struct kobj_attribute *attr, char *buf)
1245{
1246 struct hstate *h = kobj_to_hstate(kobj);
1247 return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
1248}
1249static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
1250 struct kobj_attribute *attr, const char *buf, size_t count)
1251{
1252 int err;
1253 unsigned long input;
1254 struct hstate *h = kobj_to_hstate(kobj);
1255
1256 err = strict_strtoul(buf, 10, &input);
1257 if (err)
1258 return 0;
1259
1260 spin_lock(&hugetlb_lock);
1261 h->nr_overcommit_huge_pages = input;
1262 spin_unlock(&hugetlb_lock);
1263
1264 return count;
1265}
1266HSTATE_ATTR(nr_overcommit_hugepages);
1267
1268static ssize_t free_hugepages_show(struct kobject *kobj,
1269 struct kobj_attribute *attr, char *buf)
1270{
1271 struct hstate *h = kobj_to_hstate(kobj);
1272 return sprintf(buf, "%lu\n", h->free_huge_pages);
1273}
1274HSTATE_ATTR_RO(free_hugepages);
1275
1276static ssize_t resv_hugepages_show(struct kobject *kobj,
1277 struct kobj_attribute *attr, char *buf)
1278{
1279 struct hstate *h = kobj_to_hstate(kobj);
1280 return sprintf(buf, "%lu\n", h->resv_huge_pages);
1281}
1282HSTATE_ATTR_RO(resv_hugepages);
1283
1284static ssize_t surplus_hugepages_show(struct kobject *kobj,
1285 struct kobj_attribute *attr, char *buf)
1286{
1287 struct hstate *h = kobj_to_hstate(kobj);
1288 return sprintf(buf, "%lu\n", h->surplus_huge_pages);
1289}
1290HSTATE_ATTR_RO(surplus_hugepages);
1291
1292static struct attribute *hstate_attrs[] = {
1293 &nr_hugepages_attr.attr,
1294 &nr_overcommit_hugepages_attr.attr,
1295 &free_hugepages_attr.attr,
1296 &resv_hugepages_attr.attr,
1297 &surplus_hugepages_attr.attr,
1298 NULL,
1299};
1300
1301static struct attribute_group hstate_attr_group = {
1302 .attrs = hstate_attrs,
1303};
1304
1305static int __init hugetlb_sysfs_add_hstate(struct hstate *h)
1306{
1307 int retval;
1308
1309 hstate_kobjs[h - hstates] = kobject_create_and_add(h->name,
1310 hugepages_kobj);
1311 if (!hstate_kobjs[h - hstates])
1312 return -ENOMEM;
1313
1314 retval = sysfs_create_group(hstate_kobjs[h - hstates],
1315 &hstate_attr_group);
1316 if (retval)
1317 kobject_put(hstate_kobjs[h - hstates]);
1318
1319 return retval;
1320}
1321
1322static void __init hugetlb_sysfs_init(void)
1323{
1324 struct hstate *h;
1325 int err;
1326
1327 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
1328 if (!hugepages_kobj)
1329 return;
1330
1331 for_each_hstate(h) {
1332 err = hugetlb_sysfs_add_hstate(h);
1333 if (err)
1334 printk(KERN_ERR "Hugetlb: Unable to add hstate %s",
1335 h->name);
1336 }
1337}
1338
1339static void __exit hugetlb_exit(void)
1340{
1341 struct hstate *h;
1342
1343 for_each_hstate(h) {
1344 kobject_put(hstate_kobjs[h - hstates]);
1345 }
1346
1347 kobject_put(hugepages_kobj);
1348}
1349module_exit(hugetlb_exit);
1350
1351static int __init hugetlb_init(void)
1352{
Benjamin Herrenschmidt0ef89d22008-07-31 00:07:30 -07001353 /* Some platform decide whether they support huge pages at boot
1354 * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
1355 * there is no such support
1356 */
1357 if (HPAGE_SHIFT == 0)
1358 return 0;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001359
Nick Piggine11bfbf2008-07-23 21:27:52 -07001360 if (!size_to_hstate(default_hstate_size)) {
1361 default_hstate_size = HPAGE_SIZE;
1362 if (!size_to_hstate(default_hstate_size))
1363 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001364 }
Nick Piggine11bfbf2008-07-23 21:27:52 -07001365 default_hstate_idx = size_to_hstate(default_hstate_size) - hstates;
1366 if (default_hstate_max_huge_pages)
1367 default_hstate.max_huge_pages = default_hstate_max_huge_pages;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001368
1369 hugetlb_init_hstates();
1370
Andi Kleenaa888a72008-07-23 21:27:47 -07001371 gather_bootmem_prealloc();
1372
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001373 report_hugepages();
1374
1375 hugetlb_sysfs_init();
1376
1377 return 0;
1378}
1379module_init(hugetlb_init);
1380
1381/* Should be called on processing a hugepagesz=... option */
1382void __init hugetlb_add_hstate(unsigned order)
1383{
1384 struct hstate *h;
Andi Kleen8faa8b02008-07-23 21:27:48 -07001385 unsigned long i;
1386
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001387 if (size_to_hstate(PAGE_SIZE << order)) {
1388 printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
1389 return;
1390 }
1391 BUG_ON(max_hstate >= HUGE_MAX_HSTATE);
1392 BUG_ON(order == 0);
1393 h = &hstates[max_hstate++];
1394 h->order = order;
1395 h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
Andi Kleen8faa8b02008-07-23 21:27:48 -07001396 h->nr_huge_pages = 0;
1397 h->free_huge_pages = 0;
1398 for (i = 0; i < MAX_NUMNODES; ++i)
1399 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
1400 h->hugetlb_next_nid = first_node(node_online_map);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001401 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
1402 huge_page_size(h)/1024);
Andi Kleen8faa8b02008-07-23 21:27:48 -07001403
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001404 parsed_hstate = h;
1405}
1406
Nick Piggine11bfbf2008-07-23 21:27:52 -07001407static int __init hugetlb_nrpages_setup(char *s)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001408{
1409 unsigned long *mhp;
Andi Kleen8faa8b02008-07-23 21:27:48 -07001410 static unsigned long *last_mhp;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001411
1412 /*
1413 * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
1414 * so this hugepages= parameter goes to the "default hstate".
1415 */
1416 if (!max_hstate)
1417 mhp = &default_hstate_max_huge_pages;
1418 else
1419 mhp = &parsed_hstate->max_huge_pages;
1420
Andi Kleen8faa8b02008-07-23 21:27:48 -07001421 if (mhp == last_mhp) {
1422 printk(KERN_WARNING "hugepages= specified twice without "
1423 "interleaving hugepagesz=, ignoring\n");
1424 return 1;
1425 }
1426
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001427 if (sscanf(s, "%lu", mhp) <= 0)
1428 *mhp = 0;
1429
Andi Kleen8faa8b02008-07-23 21:27:48 -07001430 /*
1431 * Global state is always initialized later in hugetlb_init.
1432 * But we need to allocate >= MAX_ORDER hstates here early to still
1433 * use the bootmem allocator.
1434 */
1435 if (max_hstate && parsed_hstate->order >= MAX_ORDER)
1436 hugetlb_hstate_alloc_pages(parsed_hstate);
1437
1438 last_mhp = mhp;
1439
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001440 return 1;
1441}
Nick Piggine11bfbf2008-07-23 21:27:52 -07001442__setup("hugepages=", hugetlb_nrpages_setup);
1443
1444static int __init hugetlb_default_setup(char *s)
1445{
1446 default_hstate_size = memparse(s, &s);
1447 return 1;
1448}
1449__setup("default_hugepagesz=", hugetlb_default_setup);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001450
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07001451static unsigned int cpuset_mems_nr(unsigned int *array)
1452{
1453 int node;
1454 unsigned int nr = 0;
1455
1456 for_each_node_mask(node, cpuset_current_mems_allowed)
1457 nr += array[node];
1458
1459 return nr;
1460}
1461
1462#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463int hugetlb_sysctl_handler(struct ctl_table *table, int write,
1464 struct file *file, void __user *buffer,
1465 size_t *length, loff_t *ppos)
1466{
Andi Kleene5ff2152008-07-23 21:27:42 -07001467 struct hstate *h = &default_hstate;
1468 unsigned long tmp;
1469
1470 if (!write)
1471 tmp = h->max_huge_pages;
1472
1473 table->data = &tmp;
1474 table->maxlen = sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Andi Kleene5ff2152008-07-23 21:27:42 -07001476
1477 if (write)
1478 h->max_huge_pages = set_max_huge_pages(h, tmp);
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481}
Mel Gorman396faf02007-07-17 04:03:13 -07001482
1483int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
1484 struct file *file, void __user *buffer,
1485 size_t *length, loff_t *ppos)
1486{
1487 proc_dointvec(table, write, file, buffer, length, ppos);
1488 if (hugepages_treat_as_movable)
1489 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
1490 else
1491 htlb_alloc_mask = GFP_HIGHUSER;
1492 return 0;
1493}
1494
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001495int hugetlb_overcommit_handler(struct ctl_table *table, int write,
1496 struct file *file, void __user *buffer,
1497 size_t *length, loff_t *ppos)
1498{
Andi Kleena5516432008-07-23 21:27:41 -07001499 struct hstate *h = &default_hstate;
Andi Kleene5ff2152008-07-23 21:27:42 -07001500 unsigned long tmp;
1501
1502 if (!write)
1503 tmp = h->nr_overcommit_huge_pages;
1504
1505 table->data = &tmp;
1506 table->maxlen = sizeof(unsigned long);
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001507 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Andi Kleene5ff2152008-07-23 21:27:42 -07001508
1509 if (write) {
1510 spin_lock(&hugetlb_lock);
1511 h->nr_overcommit_huge_pages = tmp;
1512 spin_unlock(&hugetlb_lock);
1513 }
1514
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001515 return 0;
1516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518#endif /* CONFIG_SYSCTL */
1519
Alexey Dobriyane1759c22008-10-15 23:50:22 +04001520void hugetlb_report_meminfo(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Andi Kleena5516432008-07-23 21:27:41 -07001522 struct hstate *h = &default_hstate;
Alexey Dobriyane1759c22008-10-15 23:50:22 +04001523 seq_printf(m,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001524 "HugePages_Total: %5lu\n"
1525 "HugePages_Free: %5lu\n"
1526 "HugePages_Rsvd: %5lu\n"
1527 "HugePages_Surp: %5lu\n"
1528 "Hugepagesize: %8lu kB\n",
Andi Kleena5516432008-07-23 21:27:41 -07001529 h->nr_huge_pages,
1530 h->free_huge_pages,
1531 h->resv_huge_pages,
1532 h->surplus_huge_pages,
1533 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536int hugetlb_report_node_meminfo(int nid, char *buf)
1537{
Andi Kleena5516432008-07-23 21:27:41 -07001538 struct hstate *h = &default_hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return sprintf(buf,
1540 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07001541 "Node %d HugePages_Free: %5u\n"
1542 "Node %d HugePages_Surp: %5u\n",
Andi Kleena5516432008-07-23 21:27:41 -07001543 nid, h->nr_huge_pages_node[nid],
1544 nid, h->free_huge_pages_node[nid],
1545 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
1549unsigned long hugetlb_total_pages(void)
1550{
Andi Kleena5516432008-07-23 21:27:41 -07001551 struct hstate *h = &default_hstate;
1552 return h->nr_huge_pages * pages_per_huge_page(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Andi Kleena5516432008-07-23 21:27:41 -07001555static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001556{
1557 int ret = -ENOMEM;
1558
1559 spin_lock(&hugetlb_lock);
1560 /*
1561 * When cpuset is configured, it breaks the strict hugetlb page
1562 * reservation as the accounting is done on a global variable. Such
1563 * reservation is completely rubbish in the presence of cpuset because
1564 * the reservation is not checked against page availability for the
1565 * current cpuset. Application can still potentially OOM'ed by kernel
1566 * with lack of free htlb page in cpuset that the task is in.
1567 * Attempt to enforce strict accounting with cpuset is almost
1568 * impossible (or too ugly) because cpuset is too fluid that
1569 * task or memory node can be dynamically moved between cpusets.
1570 *
1571 * The change of semantics for shared hugetlb mapping with cpuset is
1572 * undesirable. However, in order to preserve some of the semantics,
1573 * we fall back to check against current free page availability as
1574 * a best attempt and hopefully to minimize the impact of changing
1575 * semantics that cpuset has.
1576 */
1577 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07001578 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001579 goto out;
1580
Andi Kleena5516432008-07-23 21:27:41 -07001581 if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
1582 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001583 goto out;
1584 }
1585 }
1586
1587 ret = 0;
1588 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07001589 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001590
1591out:
1592 spin_unlock(&hugetlb_lock);
1593 return ret;
1594}
1595
Andy Whitcroft84afd992008-07-23 21:27:32 -07001596static void hugetlb_vm_op_open(struct vm_area_struct *vma)
1597{
1598 struct resv_map *reservations = vma_resv_map(vma);
1599
1600 /*
1601 * This new VMA should share its siblings reservation map if present.
1602 * The VMA will only ever have a valid reservation map pointer where
1603 * it is being copied for another still existing VMA. As that VMA
1604 * has a reference to the reservation map it cannot dissappear until
1605 * after this open call completes. It is therefore safe to take a
1606 * new reference here without additional locking.
1607 */
1608 if (reservations)
1609 kref_get(&reservations->refs);
1610}
1611
Mel Gormana1e78772008-07-23 21:27:23 -07001612static void hugetlb_vm_op_close(struct vm_area_struct *vma)
1613{
Andi Kleena5516432008-07-23 21:27:41 -07001614 struct hstate *h = hstate_vma(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001615 struct resv_map *reservations = vma_resv_map(vma);
1616 unsigned long reserve;
1617 unsigned long start;
1618 unsigned long end;
1619
1620 if (reservations) {
Andi Kleena5516432008-07-23 21:27:41 -07001621 start = vma_hugecache_offset(h, vma, vma->vm_start);
1622 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001623
1624 reserve = (end - start) -
1625 region_count(&reservations->regions, start, end);
1626
1627 kref_put(&reservations->refs, resv_map_release);
1628
Adam Litke7251ff72008-07-23 21:27:59 -07001629 if (reserve) {
Andi Kleena5516432008-07-23 21:27:41 -07001630 hugetlb_acct_memory(h, -reserve);
Adam Litke7251ff72008-07-23 21:27:59 -07001631 hugetlb_put_quota(vma->vm_file->f_mapping, reserve);
1632 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07001633 }
Mel Gormana1e78772008-07-23 21:27:23 -07001634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636/*
1637 * We cannot handle pagefaults against hugetlb pages at all. They cause
1638 * handle_mm_fault() to try to instantiate regular-sized pages in the
1639 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
1640 * this far.
1641 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001642static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
1648struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07001649 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07001650 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07001651 .close = hugetlb_vm_op_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652};
1653
David Gibson1e8f8892006-01-06 00:10:44 -08001654static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
1655 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07001656{
1657 pte_t entry;
1658
David Gibson1e8f8892006-01-06 00:10:44 -08001659 if (writable) {
David Gibson63551ae2005-06-21 17:14:44 -07001660 entry =
1661 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1662 } else {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001663 entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07001664 }
1665 entry = pte_mkyoung(entry);
1666 entry = pte_mkhuge(entry);
1667
1668 return entry;
1669}
1670
David Gibson1e8f8892006-01-06 00:10:44 -08001671static void set_huge_ptep_writable(struct vm_area_struct *vma,
1672 unsigned long address, pte_t *ptep)
1673{
1674 pte_t entry;
1675
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001676 entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
1677 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001678 update_mmu_cache(vma, address, entry);
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001679 }
David Gibson1e8f8892006-01-06 00:10:44 -08001680}
1681
1682
David Gibson63551ae2005-06-21 17:14:44 -07001683int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
1684 struct vm_area_struct *vma)
1685{
1686 pte_t *src_pte, *dst_pte, entry;
1687 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07001688 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08001689 int cow;
Andi Kleena5516432008-07-23 21:27:41 -07001690 struct hstate *h = hstate_vma(vma);
1691 unsigned long sz = huge_page_size(h);
David Gibson1e8f8892006-01-06 00:10:44 -08001692
1693 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07001694
Andi Kleena5516432008-07-23 21:27:41 -07001695 for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
Hugh Dickinsc74df322005-10-29 18:16:23 -07001696 src_pte = huge_pte_offset(src, addr);
1697 if (!src_pte)
1698 continue;
Andi Kleena5516432008-07-23 21:27:41 -07001699 dst_pte = huge_pte_alloc(dst, addr, sz);
David Gibson63551ae2005-06-21 17:14:44 -07001700 if (!dst_pte)
1701 goto nomem;
Larry Woodmanc5c99422008-01-24 05:49:25 -08001702
1703 /* If the pagetables are shared don't copy or take references */
1704 if (dst_pte == src_pte)
1705 continue;
1706
Hugh Dickinsc74df322005-10-29 18:16:23 -07001707 spin_lock(&dst->page_table_lock);
Nick Piggin46478752008-06-05 22:45:57 -07001708 spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001709 if (!huge_pte_none(huge_ptep_get(src_pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001710 if (cow)
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001711 huge_ptep_set_wrprotect(src, addr, src_pte);
1712 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07001713 ptepage = pte_page(entry);
1714 get_page(ptepage);
Hugh Dickins1c598272005-10-19 21:23:43 -07001715 set_huge_pte_at(dst, addr, dst_pte, entry);
1716 }
1717 spin_unlock(&src->page_table_lock);
Hugh Dickinsc74df322005-10-29 18:16:23 -07001718 spin_unlock(&dst->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001719 }
1720 return 0;
1721
1722nomem:
1723 return -ENOMEM;
1724}
1725
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001726void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001727 unsigned long end, struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07001728{
1729 struct mm_struct *mm = vma->vm_mm;
1730 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07001731 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07001732 pte_t pte;
1733 struct page *page;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001734 struct page *tmp;
Andi Kleena5516432008-07-23 21:27:41 -07001735 struct hstate *h = hstate_vma(vma);
1736 unsigned long sz = huge_page_size(h);
1737
Chen, Kenneth Wc0a499c2006-12-06 20:31:39 -08001738 /*
1739 * A page gathering list, protected by per file i_mmap_lock. The
1740 * lock is used to avoid list corruption from multiple unmapping
1741 * of the same page since we are using page->lru.
1742 */
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001743 LIST_HEAD(page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001744
1745 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07001746 BUG_ON(start & ~huge_page_mask(h));
1747 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07001748
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001749 mmu_notifier_invalidate_range_start(mm, start, end);
Hugh Dickins508034a2005-10-29 18:16:30 -07001750 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001751 for (address = start; address < end; address += sz) {
David Gibsonc7546f82005-08-05 11:59:35 -07001752 ptep = huge_pte_offset(mm, address);
Adam Litke4c887262005-10-29 18:16:46 -07001753 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07001754 continue;
1755
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001756 if (huge_pmd_unshare(mm, &address, ptep))
1757 continue;
1758
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001759 /*
1760 * If a reference page is supplied, it is because a specific
1761 * page is being unmapped, not a range. Ensure the page we
1762 * are about to unmap is the actual page of interest.
1763 */
1764 if (ref_page) {
1765 pte = huge_ptep_get(ptep);
1766 if (huge_pte_none(pte))
1767 continue;
1768 page = pte_page(pte);
1769 if (page != ref_page)
1770 continue;
1771
1772 /*
1773 * Mark the VMA as having unmapped its page so that
1774 * future faults in this VMA will fail rather than
1775 * looking like data was lost
1776 */
1777 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
1778 }
1779
David Gibsonc7546f82005-08-05 11:59:35 -07001780 pte = huge_ptep_get_and_clear(mm, address, ptep);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001781 if (huge_pte_none(pte))
David Gibson63551ae2005-06-21 17:14:44 -07001782 continue;
David Gibsonc7546f82005-08-05 11:59:35 -07001783
David Gibson63551ae2005-06-21 17:14:44 -07001784 page = pte_page(pte);
Ken Chen6649a382007-02-08 14:20:27 -08001785 if (pte_dirty(pte))
1786 set_page_dirty(page);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001787 list_add(&page->lru, &page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 spin_unlock(&mm->page_table_lock);
Hugh Dickins508034a2005-10-29 18:16:30 -07001790 flush_tlb_range(vma, start, end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001791 mmu_notifier_invalidate_range_end(mm, start, end);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001792 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1793 list_del(&page->lru);
1794 put_page(page);
1795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
David Gibson63551ae2005-06-21 17:14:44 -07001797
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001798void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001799 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001800{
Andi Kleena137e1c2008-07-23 21:27:43 -07001801 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
1802 __unmap_hugepage_range(vma, start, end, ref_page);
1803 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001804}
1805
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001806/*
1807 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1808 * mappping it owns the reserve page for. The intention is to unmap the page
1809 * from other VMAs and let the children be SIGKILLed if they are faulting the
1810 * same region.
1811 */
Harvey Harrison2a4b3de2008-10-18 20:27:06 -07001812static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
1813 struct page *page, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001814{
Adam Litke75266742008-11-12 13:24:56 -08001815 struct hstate *h = hstate_vma(vma);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001816 struct vm_area_struct *iter_vma;
1817 struct address_space *mapping;
1818 struct prio_tree_iter iter;
1819 pgoff_t pgoff;
1820
1821 /*
1822 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1823 * from page cache lookup which is in HPAGE_SIZE units.
1824 */
Adam Litke75266742008-11-12 13:24:56 -08001825 address = address & huge_page_mask(h);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001826 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
1827 + (vma->vm_pgoff >> PAGE_SHIFT);
1828 mapping = (struct address_space *)page_private(page);
1829
1830 vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1831 /* Do not unmap the current VMA */
1832 if (iter_vma == vma)
1833 continue;
1834
1835 /*
1836 * Unmap the page from other VMAs without their own reserves.
1837 * They get marked to be SIGKILLed if they fault in these
1838 * areas. This is because a future no-page fault on this VMA
1839 * could insert a zeroed page instead of the data existing
1840 * from the time of fork. This would look like data corruption
1841 */
1842 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
1843 unmap_hugepage_range(iter_vma,
Adam Litke75266742008-11-12 13:24:56 -08001844 address, address + huge_page_size(h),
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001845 page);
1846 }
1847
1848 return 1;
1849}
1850
David Gibson1e8f8892006-01-06 00:10:44 -08001851static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001852 unsigned long address, pte_t *ptep, pte_t pte,
1853 struct page *pagecache_page)
David Gibson1e8f8892006-01-06 00:10:44 -08001854{
Andi Kleena5516432008-07-23 21:27:41 -07001855 struct hstate *h = hstate_vma(vma);
David Gibson1e8f8892006-01-06 00:10:44 -08001856 struct page *old_page, *new_page;
David Gibson79ac6ba2006-03-22 00:08:51 -08001857 int avoidcopy;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001858 int outside_reserve = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001859
1860 old_page = pte_page(pte);
1861
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001862retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08001863 /* If no-one else is actually using this page, avoid the copy
1864 * and just make the page writable */
1865 avoidcopy = (page_count(old_page) == 1);
1866 if (avoidcopy) {
1867 set_huge_ptep_writable(vma, address, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07001868 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001869 }
1870
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001871 /*
1872 * If the process that created a MAP_PRIVATE mapping is about to
1873 * perform a COW due to a shared page count, attempt to satisfy
1874 * the allocation without using the existing reserves. The pagecache
1875 * page is used to determine if the reserve at this address was
1876 * consumed or not. If reserves were used, a partial faulted mapping
1877 * at the time of fork() could consume its reserves on COW instead
1878 * of the full address range.
1879 */
1880 if (!(vma->vm_flags & VM_SHARED) &&
1881 is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
1882 old_page != pagecache_page)
1883 outside_reserve = 1;
1884
David Gibson1e8f8892006-01-06 00:10:44 -08001885 page_cache_get(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001886 new_page = alloc_huge_page(vma, address, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08001887
Adam Litke2fc39ce2007-11-14 16:59:39 -08001888 if (IS_ERR(new_page)) {
David Gibson1e8f8892006-01-06 00:10:44 -08001889 page_cache_release(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001890
1891 /*
1892 * If a process owning a MAP_PRIVATE mapping fails to COW,
1893 * it is due to references held by a child and an insufficient
1894 * huge page pool. To guarantee the original mappers
1895 * reliability, unmap the page from child processes. The child
1896 * may get SIGKILLed if it later faults.
1897 */
1898 if (outside_reserve) {
1899 BUG_ON(huge_pte_none(pte));
1900 if (unmap_ref_private(mm, vma, old_page, address)) {
1901 BUG_ON(page_count(old_page) != 1);
1902 BUG_ON(huge_pte_none(pte));
1903 goto retry_avoidcopy;
1904 }
1905 WARN_ON_ONCE(1);
1906 }
1907
Adam Litke2fc39ce2007-11-14 16:59:39 -08001908 return -PTR_ERR(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001909 }
1910
1911 spin_unlock(&mm->page_table_lock);
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00001912 copy_huge_page(new_page, old_page, address, vma);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001913 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001914 spin_lock(&mm->page_table_lock);
1915
Andi Kleena5516432008-07-23 21:27:41 -07001916 ptep = huge_pte_offset(mm, address & huge_page_mask(h));
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001917 if (likely(pte_same(huge_ptep_get(ptep), pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001918 /* Break COW */
Gerald Schaefer8fe627e2008-04-28 02:13:28 -07001919 huge_ptep_clear_flush(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08001920 set_huge_pte_at(mm, address, ptep,
1921 make_huge_pte(vma, new_page, 1));
1922 /* Make the old page be freed below */
1923 new_page = old_page;
1924 }
1925 page_cache_release(new_page);
1926 page_cache_release(old_page);
Nick Piggin83c54072007-07-19 01:47:05 -07001927 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001928}
1929
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001930/* Return the pagecache page at a given address within a VMA */
Andi Kleena5516432008-07-23 21:27:41 -07001931static struct page *hugetlbfs_pagecache_page(struct hstate *h,
1932 struct vm_area_struct *vma, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001933{
1934 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001935 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001936
1937 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07001938 idx = vma_hugecache_offset(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001939
1940 return find_lock_page(mapping, idx);
1941}
1942
Robert P. J. Daya1ed3dd2007-07-17 04:03:33 -07001943static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
David Gibson1e8f8892006-01-06 00:10:44 -08001944 unsigned long address, pte_t *ptep, int write_access)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001945{
Andi Kleena5516432008-07-23 21:27:41 -07001946 struct hstate *h = hstate_vma(vma);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001947 int ret = VM_FAULT_SIGBUS;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001948 pgoff_t idx;
Adam Litke4c887262005-10-29 18:16:46 -07001949 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07001950 struct page *page;
1951 struct address_space *mapping;
David Gibson1e8f8892006-01-06 00:10:44 -08001952 pte_t new_pte;
Adam Litke4c887262005-10-29 18:16:46 -07001953
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001954 /*
1955 * Currently, we are forced to kill the process in the event the
1956 * original mapper has unmapped pages from the child due to a failed
1957 * COW. Warn that such a situation has occured as it may not be obvious
1958 */
1959 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
1960 printk(KERN_WARNING
1961 "PID %d killed due to inadequate hugepage pool\n",
1962 current->pid);
1963 return ret;
1964 }
1965
Adam Litke4c887262005-10-29 18:16:46 -07001966 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07001967 idx = vma_hugecache_offset(h, vma, address);
Adam Litke4c887262005-10-29 18:16:46 -07001968
1969 /*
1970 * Use page lock to guard against racing truncation
1971 * before we get page_table_lock.
1972 */
Christoph Lameter6bda6662006-01-06 00:10:49 -08001973retry:
1974 page = find_lock_page(mapping, idx);
1975 if (!page) {
Andi Kleena5516432008-07-23 21:27:41 -07001976 size = i_size_read(mapping->host) >> huge_page_shift(h);
Hugh Dickinsebed4bf2006-10-28 10:38:43 -07001977 if (idx >= size)
1978 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001979 page = alloc_huge_page(vma, address, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08001980 if (IS_ERR(page)) {
1981 ret = -PTR_ERR(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001982 goto out;
1983 }
Andi Kleena5516432008-07-23 21:27:41 -07001984 clear_huge_page(page, address, huge_page_size(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08001985 __SetPageUptodate(page);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001986
Christoph Lameter6bda6662006-01-06 00:10:49 -08001987 if (vma->vm_flags & VM_SHARED) {
1988 int err;
Ken Chen45c682a2007-11-14 16:59:44 -08001989 struct inode *inode = mapping->host;
Christoph Lameter6bda6662006-01-06 00:10:49 -08001990
1991 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
1992 if (err) {
1993 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001994 if (err == -EEXIST)
1995 goto retry;
1996 goto out;
1997 }
Ken Chen45c682a2007-11-14 16:59:44 -08001998
1999 spin_lock(&inode->i_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002000 inode->i_blocks += blocks_per_huge_page(h);
Ken Chen45c682a2007-11-14 16:59:44 -08002001 spin_unlock(&inode->i_lock);
Christoph Lameter6bda6662006-01-06 00:10:49 -08002002 } else
2003 lock_page(page);
2004 }
David Gibson1e8f8892006-01-06 00:10:44 -08002005
Andy Whitcroft57303d82008-08-12 15:08:47 -07002006 /*
2007 * If we are going to COW a private mapping later, we examine the
2008 * pending reservations for this page now. This will ensure that
2009 * any allocations necessary to record that reservation occur outside
2010 * the spinlock.
2011 */
2012 if (write_access && !(vma->vm_flags & VM_SHARED))
Andy Whitcroft2b267362008-08-12 15:08:49 -07002013 if (vma_needs_reservation(h, vma, address) < 0) {
2014 ret = VM_FAULT_OOM;
2015 goto backout_unlocked;
2016 }
Andy Whitcroft57303d82008-08-12 15:08:47 -07002017
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01002018 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002019 size = i_size_read(mapping->host) >> huge_page_shift(h);
Adam Litke4c887262005-10-29 18:16:46 -07002020 if (idx >= size)
2021 goto backout;
2022
Nick Piggin83c54072007-07-19 01:47:05 -07002023 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07002024 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07002025 goto backout;
2026
David Gibson1e8f8892006-01-06 00:10:44 -08002027 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
2028 && (vma->vm_flags & VM_SHARED)));
2029 set_huge_pte_at(mm, address, ptep, new_pte);
2030
2031 if (write_access && !(vma->vm_flags & VM_SHARED)) {
2032 /* Optimization, do the COW without a second fault */
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002033 ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
David Gibson1e8f8892006-01-06 00:10:44 -08002034 }
2035
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01002036 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07002037 unlock_page(page);
2038out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01002039 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07002040
2041backout:
2042 spin_unlock(&mm->page_table_lock);
Andy Whitcroft2b267362008-08-12 15:08:49 -07002043backout_unlocked:
Adam Litke4c887262005-10-29 18:16:46 -07002044 unlock_page(page);
2045 put_page(page);
2046 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01002047}
2048
Adam Litke86e52162006-01-06 00:10:43 -08002049int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2050 unsigned long address, int write_access)
2051{
2052 pte_t *ptep;
2053 pte_t entry;
David Gibson1e8f8892006-01-06 00:10:44 -08002054 int ret;
Andy Whitcroft57303d82008-08-12 15:08:47 -07002055 struct page *pagecache_page = NULL;
David Gibson3935baa2006-03-22 00:08:53 -08002056 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
Andi Kleena5516432008-07-23 21:27:41 -07002057 struct hstate *h = hstate_vma(vma);
Adam Litke86e52162006-01-06 00:10:43 -08002058
Andi Kleena5516432008-07-23 21:27:41 -07002059 ptep = huge_pte_alloc(mm, address, huge_page_size(h));
Adam Litke86e52162006-01-06 00:10:43 -08002060 if (!ptep)
2061 return VM_FAULT_OOM;
2062
David Gibson3935baa2006-03-22 00:08:53 -08002063 /*
2064 * Serialize hugepage allocation and instantiation, so that we don't
2065 * get spurious allocation failures if two CPUs race to instantiate
2066 * the same page in the page cache.
2067 */
2068 mutex_lock(&hugetlb_instantiation_mutex);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07002069 entry = huge_ptep_get(ptep);
2070 if (huge_pte_none(entry)) {
David Gibson3935baa2006-03-22 00:08:53 -08002071 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
David Gibsonb4d1d992008-10-15 22:01:11 -07002072 goto out_mutex;
David Gibson3935baa2006-03-22 00:08:53 -08002073 }
Adam Litke86e52162006-01-06 00:10:43 -08002074
Nick Piggin83c54072007-07-19 01:47:05 -07002075 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08002076
Andy Whitcroft57303d82008-08-12 15:08:47 -07002077 /*
2078 * If we are going to COW the mapping later, we examine the pending
2079 * reservations for this page now. This will ensure that any
2080 * allocations necessary to record that reservation occur outside the
2081 * spinlock. For private mappings, we also lookup the pagecache
2082 * page now as it is used to determine if a reservation has been
2083 * consumed.
2084 */
2085 if (write_access && !pte_write(entry)) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07002086 if (vma_needs_reservation(h, vma, address) < 0) {
2087 ret = VM_FAULT_OOM;
David Gibsonb4d1d992008-10-15 22:01:11 -07002088 goto out_mutex;
Andy Whitcroft2b267362008-08-12 15:08:49 -07002089 }
Andy Whitcroft57303d82008-08-12 15:08:47 -07002090
2091 if (!(vma->vm_flags & VM_SHARED))
2092 pagecache_page = hugetlbfs_pagecache_page(h,
2093 vma, address);
2094 }
2095
David Gibson1e8f8892006-01-06 00:10:44 -08002096 spin_lock(&mm->page_table_lock);
2097 /* Check for a racing update before calling hugetlb_cow */
David Gibsonb4d1d992008-10-15 22:01:11 -07002098 if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
2099 goto out_page_table_lock;
2100
2101
2102 if (write_access) {
2103 if (!pte_write(entry)) {
Andy Whitcroft57303d82008-08-12 15:08:47 -07002104 ret = hugetlb_cow(mm, vma, address, ptep, entry,
2105 pagecache_page);
David Gibsonb4d1d992008-10-15 22:01:11 -07002106 goto out_page_table_lock;
2107 }
2108 entry = pte_mkdirty(entry);
2109 }
2110 entry = pte_mkyoung(entry);
2111 if (huge_ptep_set_access_flags(vma, address, ptep, entry, write_access))
2112 update_mmu_cache(vma, address, entry);
2113
2114out_page_table_lock:
David Gibson1e8f8892006-01-06 00:10:44 -08002115 spin_unlock(&mm->page_table_lock);
Andy Whitcroft57303d82008-08-12 15:08:47 -07002116
2117 if (pagecache_page) {
2118 unlock_page(pagecache_page);
2119 put_page(pagecache_page);
2120 }
2121
David Gibsonb4d1d992008-10-15 22:01:11 -07002122out_mutex:
David Gibson3935baa2006-03-22 00:08:53 -08002123 mutex_unlock(&hugetlb_instantiation_mutex);
David Gibson1e8f8892006-01-06 00:10:44 -08002124
2125 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08002126}
2127
Andi Kleenceb86872008-07-23 21:27:50 -07002128/* Can be overriden by architectures */
2129__attribute__((weak)) struct page *
2130follow_huge_pud(struct mm_struct *mm, unsigned long address,
2131 pud_t *pud, int write)
2132{
2133 BUG();
2134 return NULL;
2135}
2136
KOSAKI Motohiro4b2e38a2008-10-18 20:27:10 -07002137static int huge_zeropage_ok(pte_t *ptep, int write, int shared)
2138{
2139 if (!ptep || write || shared)
2140 return 0;
2141 else
2142 return huge_pte_none(huge_ptep_get(ptep));
2143}
2144
David Gibson63551ae2005-06-21 17:14:44 -07002145int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
2146 struct page **pages, struct vm_area_struct **vmas,
Adam Litke5b23dbe2007-11-14 16:59:33 -08002147 unsigned long *position, int *length, int i,
2148 int write)
David Gibson63551ae2005-06-21 17:14:44 -07002149{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08002150 unsigned long pfn_offset;
2151 unsigned long vaddr = *position;
David Gibson63551ae2005-06-21 17:14:44 -07002152 int remainder = *length;
Andi Kleena5516432008-07-23 21:27:41 -07002153 struct hstate *h = hstate_vma(vma);
KOSAKI Motohiro4b2e38a2008-10-18 20:27:10 -07002154 int zeropage_ok = 0;
2155 int shared = vma->vm_flags & VM_SHARED;
David Gibson63551ae2005-06-21 17:14:44 -07002156
Hugh Dickins1c598272005-10-19 21:23:43 -07002157 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07002158 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07002159 pte_t *pte;
2160 struct page *page;
2161
2162 /*
2163 * Some archs (sparc64, sh*) have multiple pte_ts to
2164 * each hugepage. We have to make * sure we get the
2165 * first, for the page indexing below to work.
2166 */
Andi Kleena5516432008-07-23 21:27:41 -07002167 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
KOSAKI Motohiro4b2e38a2008-10-18 20:27:10 -07002168 if (huge_zeropage_ok(pte, write, shared))
2169 zeropage_ok = 1;
Adam Litke4c887262005-10-29 18:16:46 -07002170
KOSAKI Motohiro4b2e38a2008-10-18 20:27:10 -07002171 if (!pte ||
2172 (huge_pte_none(huge_ptep_get(pte)) && !zeropage_ok) ||
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07002173 (write && !pte_write(huge_ptep_get(pte)))) {
Adam Litke4c887262005-10-29 18:16:46 -07002174 int ret;
2175
2176 spin_unlock(&mm->page_table_lock);
Adam Litke5b23dbe2007-11-14 16:59:33 -08002177 ret = hugetlb_fault(mm, vma, vaddr, write);
Adam Litke4c887262005-10-29 18:16:46 -07002178 spin_lock(&mm->page_table_lock);
Adam Litkea89182c2007-08-22 14:01:51 -07002179 if (!(ret & VM_FAULT_ERROR))
Adam Litke4c887262005-10-29 18:16:46 -07002180 continue;
2181
2182 remainder = 0;
2183 if (!i)
2184 i = -EFAULT;
2185 break;
2186 }
David Gibson63551ae2005-06-21 17:14:44 -07002187
Andi Kleena5516432008-07-23 21:27:41 -07002188 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07002189 page = pte_page(huge_ptep_get(pte));
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08002190same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08002191 if (pages) {
KOSAKI Motohiro4b2e38a2008-10-18 20:27:10 -07002192 if (zeropage_ok)
2193 pages[i] = ZERO_PAGE(0);
2194 else
Andy Whitcroft69d177c2008-11-06 12:53:26 -08002195 pages[i] = mem_map_offset(page, pfn_offset);
KOSAKI Motohiro4b2e38a2008-10-18 20:27:10 -07002196 get_page(pages[i]);
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08002197 }
David Gibson63551ae2005-06-21 17:14:44 -07002198
2199 if (vmas)
2200 vmas[i] = vma;
2201
2202 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08002203 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07002204 --remainder;
2205 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08002206 if (vaddr < vma->vm_end && remainder &&
Andi Kleena5516432008-07-23 21:27:41 -07002207 pfn_offset < pages_per_huge_page(h)) {
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08002208 /*
2209 * We use pfn_offset to avoid touching the pageframes
2210 * of this compound page.
2211 */
2212 goto same_page;
2213 }
David Gibson63551ae2005-06-21 17:14:44 -07002214 }
Hugh Dickins1c598272005-10-19 21:23:43 -07002215 spin_unlock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07002216 *length = remainder;
2217 *position = vaddr;
2218
2219 return i;
2220}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08002221
2222void hugetlb_change_protection(struct vm_area_struct *vma,
2223 unsigned long address, unsigned long end, pgprot_t newprot)
2224{
2225 struct mm_struct *mm = vma->vm_mm;
2226 unsigned long start = address;
2227 pte_t *ptep;
2228 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07002229 struct hstate *h = hstate_vma(vma);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08002230
2231 BUG_ON(address >= end);
2232 flush_cache_range(vma, address, end);
2233
Chen, Kenneth W39dde652006-12-06 20:32:03 -08002234 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08002235 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002236 for (; address < end; address += huge_page_size(h)) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08002237 ptep = huge_pte_offset(mm, address);
2238 if (!ptep)
2239 continue;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08002240 if (huge_pmd_unshare(mm, &address, ptep))
2241 continue;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07002242 if (!huge_pte_none(huge_ptep_get(ptep))) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08002243 pte = huge_ptep_get_and_clear(mm, address, ptep);
2244 pte = pte_mkhuge(pte_modify(pte, newprot));
2245 set_huge_pte_at(mm, address, ptep, pte);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08002246 }
2247 }
2248 spin_unlock(&mm->page_table_lock);
Chen, Kenneth W39dde652006-12-06 20:32:03 -08002249 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08002250
2251 flush_tlb_range(vma, start, end);
2252}
2253
Mel Gormana1e78772008-07-23 21:27:23 -07002254int hugetlb_reserve_pages(struct inode *inode,
2255 long from, long to,
2256 struct vm_area_struct *vma)
Adam Litkee4e574b2007-10-16 01:26:19 -07002257{
2258 long ret, chg;
Andi Kleena5516432008-07-23 21:27:41 -07002259 struct hstate *h = hstate_inode(inode);
Adam Litkee4e574b2007-10-16 01:26:19 -07002260
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002261 if (vma && vma->vm_flags & VM_NORESERVE)
2262 return 0;
2263
Mel Gormana1e78772008-07-23 21:27:23 -07002264 /*
2265 * Shared mappings base their reservation on the number of pages that
2266 * are already allocated on behalf of the file. Private mappings need
2267 * to reserve the full area even if read-only as mprotect() may be
2268 * called to make the mapping read-write. Assume !vma is a shm mapping
2269 */
2270 if (!vma || vma->vm_flags & VM_SHARED)
2271 chg = region_chg(&inode->i_mapping->private_list, from, to);
2272 else {
Andy Whitcroft84afd992008-07-23 21:27:32 -07002273 struct resv_map *resv_map = resv_map_alloc();
2274 if (!resv_map)
2275 return -ENOMEM;
2276
Mel Gormana1e78772008-07-23 21:27:23 -07002277 chg = to - from;
Andy Whitcroft84afd992008-07-23 21:27:32 -07002278
2279 set_vma_resv_map(vma, resv_map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002280 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
Mel Gormana1e78772008-07-23 21:27:23 -07002281 }
2282
Adam Litkee4e574b2007-10-16 01:26:19 -07002283 if (chg < 0)
2284 return chg;
Ken Chen8a630112007-05-09 02:33:34 -07002285
Adam Litke90d8b7e2007-11-14 16:59:42 -08002286 if (hugetlb_get_quota(inode->i_mapping, chg))
2287 return -ENOSPC;
Andi Kleena5516432008-07-23 21:27:41 -07002288 ret = hugetlb_acct_memory(h, chg);
Ken Chen68842c92008-01-14 00:55:19 -08002289 if (ret < 0) {
2290 hugetlb_put_quota(inode->i_mapping, chg);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07002291 return ret;
Ken Chen68842c92008-01-14 00:55:19 -08002292 }
Mel Gormana1e78772008-07-23 21:27:23 -07002293 if (!vma || vma->vm_flags & VM_SHARED)
2294 region_add(&inode->i_mapping->private_list, from, to);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07002295 return 0;
2296}
2297
2298void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
2299{
Andi Kleena5516432008-07-23 21:27:41 -07002300 struct hstate *h = hstate_inode(inode);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07002301 long chg = region_truncate(&inode->i_mapping->private_list, offset);
Ken Chen45c682a2007-11-14 16:59:44 -08002302
2303 spin_lock(&inode->i_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002304 inode->i_blocks -= blocks_per_huge_page(h);
Ken Chen45c682a2007-11-14 16:59:44 -08002305 spin_unlock(&inode->i_lock);
2306
Adam Litke90d8b7e2007-11-14 16:59:42 -08002307 hugetlb_put_quota(inode->i_mapping, (chg - freed));
Andi Kleena5516432008-07-23 21:27:41 -07002308 hugetlb_acct_memory(h, -(chg - freed));
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07002309}