blob: 0d8153e25f091fc04fdf7c2e4009e6161d454284 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic hugetlb support.
3 * (C) William Irwin, April 2004
4 */
5#include <linux/gfp.h>
6#include <linux/list.h>
7#include <linux/init.h>
8#include <linux/module.h>
9#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/sysctl.h>
11#include <linux/highmem.h>
12#include <linux/nodemask.h>
David Gibson63551ae2005-06-21 17:14:44 -070013#include <linux/pagemap.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080014#include <linux/mempolicy.h>
Christoph Lameteraea47ff2006-01-08 01:00:57 -080015#include <linux/cpuset.h>
David Gibson3935baa2006-03-22 00:08:53 -080016#include <linux/mutex.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080017
David Gibson63551ae2005-06-21 17:14:44 -070018#include <asm/page.h>
19#include <asm/pgtable.h>
20
21#include <linux/hugetlb.h>
Nick Piggin7835e982006-03-22 00:08:40 -080022#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025unsigned long max_huge_pages;
Nishanth Aravamudan064d9ef2008-02-13 15:03:19 -080026unsigned long sysctl_overcommit_huge_pages;
Mel Gorman396faf02007-07-17 04:03:13 -070027static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
28unsigned long hugepages_treat_as_movable;
Andi Kleena5516432008-07-23 21:27:41 -070029
30struct hstate default_hstate;
Mel Gorman396faf02007-07-17 04:03:13 -070031
David Gibson3935baa2006-03-22 00:08:53 -080032/*
33 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
34 */
35static DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080036
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -070037/*
Andy Whitcroft96822902008-07-23 21:27:29 -070038 * Region tracking -- allows tracking of reservations and instantiated pages
39 * across the pages in a mapping.
Andy Whitcroft84afd992008-07-23 21:27:32 -070040 *
41 * The region data structures are protected by a combination of the mmap_sem
42 * and the hugetlb_instantion_mutex. To access or modify a region the caller
43 * must either hold the mmap_sem for write, or the mmap_sem for read and
44 * the hugetlb_instantiation mutex:
45 *
46 * down_write(&mm->mmap_sem);
47 * or
48 * down_read(&mm->mmap_sem);
49 * mutex_lock(&hugetlb_instantiation_mutex);
Andy Whitcroft96822902008-07-23 21:27:29 -070050 */
51struct file_region {
52 struct list_head link;
53 long from;
54 long to;
55};
56
57static long region_add(struct list_head *head, long f, long t)
58{
59 struct file_region *rg, *nrg, *trg;
60
61 /* Locate the region we are either in or before. */
62 list_for_each_entry(rg, head, link)
63 if (f <= rg->to)
64 break;
65
66 /* Round our left edge to the current segment if it encloses us. */
67 if (f > rg->from)
68 f = rg->from;
69
70 /* Check for and consume any regions we now overlap with. */
71 nrg = rg;
72 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
73 if (&rg->link == head)
74 break;
75 if (rg->from > t)
76 break;
77
78 /* If this area reaches higher then extend our area to
79 * include it completely. If this is not the first area
80 * which we intend to reuse, free it. */
81 if (rg->to > t)
82 t = rg->to;
83 if (rg != nrg) {
84 list_del(&rg->link);
85 kfree(rg);
86 }
87 }
88 nrg->from = f;
89 nrg->to = t;
90 return 0;
91}
92
93static long region_chg(struct list_head *head, long f, long t)
94{
95 struct file_region *rg, *nrg;
96 long chg = 0;
97
98 /* Locate the region we are before or in. */
99 list_for_each_entry(rg, head, link)
100 if (f <= rg->to)
101 break;
102
103 /* If we are below the current region then a new region is required.
104 * Subtle, allocate a new region at the position but make it zero
105 * size such that we can guarantee to record the reservation. */
106 if (&rg->link == head || t < rg->from) {
107 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
108 if (!nrg)
109 return -ENOMEM;
110 nrg->from = f;
111 nrg->to = f;
112 INIT_LIST_HEAD(&nrg->link);
113 list_add(&nrg->link, rg->link.prev);
114
115 return t - f;
116 }
117
118 /* Round our left edge to the current segment if it encloses us. */
119 if (f > rg->from)
120 f = rg->from;
121 chg = t - f;
122
123 /* Check for and consume any regions we now overlap with. */
124 list_for_each_entry(rg, rg->link.prev, link) {
125 if (&rg->link == head)
126 break;
127 if (rg->from > t)
128 return chg;
129
130 /* We overlap with this area, if it extends futher than
131 * us then we must extend ourselves. Account for its
132 * existing reservation. */
133 if (rg->to > t) {
134 chg += rg->to - t;
135 t = rg->to;
136 }
137 chg -= rg->to - rg->from;
138 }
139 return chg;
140}
141
142static long region_truncate(struct list_head *head, long end)
143{
144 struct file_region *rg, *trg;
145 long chg = 0;
146
147 /* Locate the region we are either in or before. */
148 list_for_each_entry(rg, head, link)
149 if (end <= rg->to)
150 break;
151 if (&rg->link == head)
152 return 0;
153
154 /* If we are in the middle of a region then adjust it. */
155 if (end > rg->from) {
156 chg = rg->to - end;
157 rg->to = end;
158 rg = list_entry(rg->link.next, typeof(*rg), link);
159 }
160
161 /* Drop any remaining regions. */
162 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
163 if (&rg->link == head)
164 break;
165 chg += rg->to - rg->from;
166 list_del(&rg->link);
167 kfree(rg);
168 }
169 return chg;
170}
171
Andy Whitcroft84afd992008-07-23 21:27:32 -0700172static long region_count(struct list_head *head, long f, long t)
173{
174 struct file_region *rg;
175 long chg = 0;
176
177 /* Locate each segment we overlap with, and count that overlap. */
178 list_for_each_entry(rg, head, link) {
179 int seg_from;
180 int seg_to;
181
182 if (rg->to <= f)
183 continue;
184 if (rg->from >= t)
185 break;
186
187 seg_from = max(rg->from, f);
188 seg_to = min(rg->to, t);
189
190 chg += seg_to - seg_from;
191 }
192
193 return chg;
194}
195
Andy Whitcroft96822902008-07-23 21:27:29 -0700196/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700197 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700198 * the mapping, in pagecache page units; huge pages here.
199 */
Andi Kleena5516432008-07-23 21:27:41 -0700200static pgoff_t vma_hugecache_offset(struct hstate *h,
201 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700202{
Andi Kleena5516432008-07-23 21:27:41 -0700203 return ((address - vma->vm_start) >> huge_page_shift(h)) +
204 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700205}
206
Andy Whitcroft84afd992008-07-23 21:27:32 -0700207/*
208 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
209 * bits of the reservation map pointer, which are always clear due to
210 * alignment.
211 */
212#define HPAGE_RESV_OWNER (1UL << 0)
213#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700214#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700215
Mel Gormana1e78772008-07-23 21:27:23 -0700216/*
217 * These helpers are used to track how many pages are reserved for
218 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
219 * is guaranteed to have their future faults succeed.
220 *
221 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
222 * the reserve counters are updated with the hugetlb_lock held. It is safe
223 * to reset the VMA at fork() time as it is not in use yet and there is no
224 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700225 *
226 * The private mapping reservation is represented in a subtly different
227 * manner to a shared mapping. A shared mapping has a region map associated
228 * with the underlying file, this region map represents the backing file
229 * pages which have ever had a reservation assigned which this persists even
230 * after the page is instantiated. A private mapping has a region map
231 * associated with the original mmap which is attached to all VMAs which
232 * reference it, this region map represents those offsets which have consumed
233 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -0700234 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700235static unsigned long get_vma_private_data(struct vm_area_struct *vma)
236{
237 return (unsigned long)vma->vm_private_data;
238}
239
240static void set_vma_private_data(struct vm_area_struct *vma,
241 unsigned long value)
242{
243 vma->vm_private_data = (void *)value;
244}
245
Andy Whitcroft84afd992008-07-23 21:27:32 -0700246struct resv_map {
247 struct kref refs;
248 struct list_head regions;
249};
250
251struct resv_map *resv_map_alloc(void)
252{
253 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
254 if (!resv_map)
255 return NULL;
256
257 kref_init(&resv_map->refs);
258 INIT_LIST_HEAD(&resv_map->regions);
259
260 return resv_map;
261}
262
263void resv_map_release(struct kref *ref)
264{
265 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
266
267 /* Clear out any active regions before we release the map. */
268 region_truncate(&resv_map->regions, 0);
269 kfree(resv_map);
270}
271
272static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700273{
274 VM_BUG_ON(!is_vm_hugetlb_page(vma));
275 if (!(vma->vm_flags & VM_SHARED))
Andy Whitcroft84afd992008-07-23 21:27:32 -0700276 return (struct resv_map *)(get_vma_private_data(vma) &
277 ~HPAGE_RESV_MASK);
Mel Gormana1e78772008-07-23 21:27:23 -0700278 return 0;
279}
280
Andy Whitcroft84afd992008-07-23 21:27:32 -0700281static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -0700282{
283 VM_BUG_ON(!is_vm_hugetlb_page(vma));
284 VM_BUG_ON(vma->vm_flags & VM_SHARED);
285
Andy Whitcroft84afd992008-07-23 21:27:32 -0700286 set_vma_private_data(vma, (get_vma_private_data(vma) &
287 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700288}
289
290static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
291{
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700292 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700293 VM_BUG_ON(vma->vm_flags & VM_SHARED);
294
295 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700296}
297
298static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
299{
300 VM_BUG_ON(!is_vm_hugetlb_page(vma));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700301
302 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700303}
304
305/* Decrement the reserved pages in the hugepage pool by one */
Andi Kleena5516432008-07-23 21:27:41 -0700306static void decrement_hugepage_resv_vma(struct hstate *h,
307 struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700308{
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700309 if (vma->vm_flags & VM_NORESERVE)
310 return;
311
Mel Gormana1e78772008-07-23 21:27:23 -0700312 if (vma->vm_flags & VM_SHARED) {
313 /* Shared mappings always use reserves */
Andi Kleena5516432008-07-23 21:27:41 -0700314 h->resv_huge_pages--;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700315 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Mel Gormana1e78772008-07-23 21:27:23 -0700316 /*
317 * Only the process that called mmap() has reserves for
318 * private mappings.
319 */
Andi Kleena5516432008-07-23 21:27:41 -0700320 h->resv_huge_pages--;
Mel Gormana1e78772008-07-23 21:27:23 -0700321 }
322}
323
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700324/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700325void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
326{
327 VM_BUG_ON(!is_vm_hugetlb_page(vma));
328 if (!(vma->vm_flags & VM_SHARED))
329 vma->vm_private_data = (void *)0;
330}
331
332/* Returns true if the VMA has associated reserve pages */
333static int vma_has_private_reserves(struct vm_area_struct *vma)
334{
335 if (vma->vm_flags & VM_SHARED)
336 return 0;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700337 if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER))
Mel Gormana1e78772008-07-23 21:27:23 -0700338 return 0;
339 return 1;
340}
341
Andi Kleena5516432008-07-23 21:27:41 -0700342static void clear_huge_page(struct page *page,
343 unsigned long addr, unsigned long sz)
David Gibson79ac6ba2006-03-22 00:08:51 -0800344{
345 int i;
346
347 might_sleep();
Andi Kleena5516432008-07-23 21:27:41 -0700348 for (i = 0; i < sz/PAGE_SIZE; i++) {
David Gibson79ac6ba2006-03-22 00:08:51 -0800349 cond_resched();
Ralf Baechle281e0e32007-10-01 01:20:10 -0700350 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
David Gibson79ac6ba2006-03-22 00:08:51 -0800351 }
352}
353
354static void copy_huge_page(struct page *dst, struct page *src,
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000355 unsigned long addr, struct vm_area_struct *vma)
David Gibson79ac6ba2006-03-22 00:08:51 -0800356{
357 int i;
Andi Kleena5516432008-07-23 21:27:41 -0700358 struct hstate *h = hstate_vma(vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800359
360 might_sleep();
Andi Kleena5516432008-07-23 21:27:41 -0700361 for (i = 0; i < pages_per_huge_page(h); i++) {
David Gibson79ac6ba2006-03-22 00:08:51 -0800362 cond_resched();
Atsushi Nemoto9de455b2006-12-12 17:14:55 +0000363 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
David Gibson79ac6ba2006-03-22 00:08:51 -0800364 }
365}
366
Andi Kleena5516432008-07-23 21:27:41 -0700367static void enqueue_huge_page(struct hstate *h, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 int nid = page_to_nid(page);
Andi Kleena5516432008-07-23 21:27:41 -0700370 list_add(&page->lru, &h->hugepage_freelists[nid]);
371 h->free_huge_pages++;
372 h->free_huge_pages_node[nid]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Andi Kleena5516432008-07-23 21:27:41 -0700375static struct page *dequeue_huge_page(struct hstate *h)
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800376{
377 int nid;
378 struct page *page = NULL;
379
380 for (nid = 0; nid < MAX_NUMNODES; ++nid) {
Andi Kleena5516432008-07-23 21:27:41 -0700381 if (!list_empty(&h->hugepage_freelists[nid])) {
382 page = list_entry(h->hugepage_freelists[nid].next,
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800383 struct page, lru);
384 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700385 h->free_huge_pages--;
386 h->free_huge_pages_node[nid]--;
Nishanth Aravamudan348e1e02008-03-04 14:29:42 -0800387 break;
388 }
389 }
390 return page;
391}
392
Andi Kleena5516432008-07-23 21:27:41 -0700393static struct page *dequeue_huge_page_vma(struct hstate *h,
394 struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700395 unsigned long address, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Nishanth Aravamudan31a5c6e2007-07-15 23:38:02 -0700397 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct page *page = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700399 struct mempolicy *mpol;
Mel Gorman19770b32008-04-28 02:12:18 -0700400 nodemask_t *nodemask;
Mel Gorman396faf02007-07-17 04:03:13 -0700401 struct zonelist *zonelist = huge_zonelist(vma, address,
Mel Gorman19770b32008-04-28 02:12:18 -0700402 htlb_alloc_mask, &mpol, &nodemask);
Mel Gormandd1a2392008-04-28 02:12:17 -0700403 struct zone *zone;
404 struct zoneref *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Mel Gormana1e78772008-07-23 21:27:23 -0700406 /*
407 * A child process with MAP_PRIVATE mappings created by their parent
408 * have no page reserves. This check ensures that reservations are
409 * not "stolen". The child may still get SIGKILLed
410 */
411 if (!vma_has_private_reserves(vma) &&
Andi Kleena5516432008-07-23 21:27:41 -0700412 h->free_huge_pages - h->resv_huge_pages == 0)
Mel Gormana1e78772008-07-23 21:27:23 -0700413 return NULL;
414
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700415 /* If reserves cannot be used, ensure enough pages are in the pool */
Andi Kleena5516432008-07-23 21:27:41 -0700416 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700417 return NULL;
418
Mel Gorman19770b32008-04-28 02:12:18 -0700419 for_each_zone_zonelist_nodemask(zone, z, zonelist,
420 MAX_NR_ZONES - 1, nodemask) {
Mel Gorman54a6eb52008-04-28 02:12:16 -0700421 nid = zone_to_nid(zone);
422 if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
Andi Kleena5516432008-07-23 21:27:41 -0700423 !list_empty(&h->hugepage_freelists[nid])) {
424 page = list_entry(h->hugepage_freelists[nid].next,
Andrew Morton3abf7af2007-07-19 01:49:08 -0700425 struct page, lru);
426 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700427 h->free_huge_pages--;
428 h->free_huge_pages_node[nid]--;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700429
430 if (!avoid_reserve)
Andi Kleena5516432008-07-23 21:27:41 -0700431 decrement_hugepage_resv_vma(h, vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700432
Ken Chen5ab3ee72007-07-23 18:44:00 -0700433 break;
Andrew Morton3abf7af2007-07-19 01:49:08 -0700434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700436 mpol_cond_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return page;
438}
439
Andi Kleena5516432008-07-23 21:27:41 -0700440static void update_and_free_page(struct hstate *h, struct page *page)
Adam Litke6af2acb2007-10-16 01:26:16 -0700441{
442 int i;
Andi Kleena5516432008-07-23 21:27:41 -0700443
444 h->nr_huge_pages--;
445 h->nr_huge_pages_node[page_to_nid(page)]--;
446 for (i = 0; i < pages_per_huge_page(h); i++) {
Adam Litke6af2acb2007-10-16 01:26:16 -0700447 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
448 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
449 1 << PG_private | 1<< PG_writeback);
450 }
451 set_compound_page_dtor(page, NULL);
452 set_page_refcounted(page);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700453 arch_release_hugepage(page);
Andi Kleena5516432008-07-23 21:27:41 -0700454 __free_pages(page, huge_page_order(h));
Adam Litke6af2acb2007-10-16 01:26:16 -0700455}
456
David Gibson27a85ef2006-03-22 00:08:56 -0800457static void free_huge_page(struct page *page)
458{
Andi Kleena5516432008-07-23 21:27:41 -0700459 /*
460 * Can't pass hstate in here because it is called from the
461 * compound page destructor.
462 */
463 struct hstate *h = &default_hstate;
Adam Litke7893d1d2007-10-16 01:26:18 -0700464 int nid = page_to_nid(page);
Adam Litkec79fb752007-11-14 16:59:38 -0800465 struct address_space *mapping;
David Gibson27a85ef2006-03-22 00:08:56 -0800466
Adam Litkec79fb752007-11-14 16:59:38 -0800467 mapping = (struct address_space *) page_private(page);
Andy Whitcrofte5df70a2008-02-23 15:23:32 -0800468 set_page_private(page, 0);
Adam Litke7893d1d2007-10-16 01:26:18 -0700469 BUG_ON(page_count(page));
David Gibson27a85ef2006-03-22 00:08:56 -0800470 INIT_LIST_HEAD(&page->lru);
471
472 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700473 if (h->surplus_huge_pages_node[nid]) {
474 update_and_free_page(h, page);
475 h->surplus_huge_pages--;
476 h->surplus_huge_pages_node[nid]--;
Adam Litke7893d1d2007-10-16 01:26:18 -0700477 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700478 enqueue_huge_page(h, page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700479 }
David Gibson27a85ef2006-03-22 00:08:56 -0800480 spin_unlock(&hugetlb_lock);
Adam Litkec79fb752007-11-14 16:59:38 -0800481 if (mapping)
Adam Litke9a119c02007-11-14 16:59:41 -0800482 hugetlb_put_quota(mapping, 1);
David Gibson27a85ef2006-03-22 00:08:56 -0800483}
484
Adam Litke7893d1d2007-10-16 01:26:18 -0700485/*
486 * Increment or decrement surplus_huge_pages. Keep node-specific counters
487 * balanced by operating on them in a round-robin fashion.
488 * Returns 1 if an adjustment was made.
489 */
Andi Kleena5516432008-07-23 21:27:41 -0700490static int adjust_pool_surplus(struct hstate *h, int delta)
Adam Litke7893d1d2007-10-16 01:26:18 -0700491{
492 static int prev_nid;
493 int nid = prev_nid;
494 int ret = 0;
495
496 VM_BUG_ON(delta != -1 && delta != 1);
497 do {
498 nid = next_node(nid, node_online_map);
499 if (nid == MAX_NUMNODES)
500 nid = first_node(node_online_map);
501
502 /* To shrink on this node, there must be a surplus page */
Andi Kleena5516432008-07-23 21:27:41 -0700503 if (delta < 0 && !h->surplus_huge_pages_node[nid])
Adam Litke7893d1d2007-10-16 01:26:18 -0700504 continue;
505 /* Surplus cannot exceed the total number of pages */
Andi Kleena5516432008-07-23 21:27:41 -0700506 if (delta > 0 && h->surplus_huge_pages_node[nid] >=
507 h->nr_huge_pages_node[nid])
Adam Litke7893d1d2007-10-16 01:26:18 -0700508 continue;
509
Andi Kleena5516432008-07-23 21:27:41 -0700510 h->surplus_huge_pages += delta;
511 h->surplus_huge_pages_node[nid] += delta;
Adam Litke7893d1d2007-10-16 01:26:18 -0700512 ret = 1;
513 break;
514 } while (nid != prev_nid);
515
516 prev_nid = nid;
517 return ret;
518}
519
Andi Kleena5516432008-07-23 21:27:41 -0700520static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700521{
522 set_compound_page_dtor(page, free_huge_page);
523 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700524 h->nr_huge_pages++;
525 h->nr_huge_pages_node[nid]++;
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700526 spin_unlock(&hugetlb_lock);
527 put_page(page); /* free it into the hugepage allocator */
528}
529
Andi Kleena5516432008-07-23 21:27:41 -0700530static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct page *page;
Joe Jinf96efd52007-07-15 23:38:12 -0700533
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700534 page = alloc_pages_node(nid,
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700535 htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
536 __GFP_REPEAT|__GFP_NOWARN,
Andi Kleena5516432008-07-23 21:27:41 -0700537 huge_page_order(h));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (page) {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700539 if (arch_prepare_hugepage(page)) {
540 __free_pages(page, HUGETLB_PAGE_ORDER);
Harvey Harrison7b8ee842008-04-28 14:13:19 -0700541 return NULL;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -0700542 }
Andi Kleena5516432008-07-23 21:27:41 -0700543 prep_new_huge_page(h, page, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700545
546 return page;
547}
548
Andi Kleena5516432008-07-23 21:27:41 -0700549static int alloc_fresh_huge_page(struct hstate *h)
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700550{
551 struct page *page;
552 int start_nid;
553 int next_nid;
554 int ret = 0;
555
Andi Kleena5516432008-07-23 21:27:41 -0700556 start_nid = h->hugetlb_next_nid;
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700557
558 do {
Andi Kleena5516432008-07-23 21:27:41 -0700559 page = alloc_fresh_huge_page_node(h, h->hugetlb_next_nid);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700560 if (page)
561 ret = 1;
562 /*
563 * Use a helper variable to find the next node and then
564 * copy it back to hugetlb_next_nid afterwards:
565 * otherwise there's a window in which a racer might
566 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
567 * But we don't need to use a spin_lock here: it really
568 * doesn't matter if occasionally a racer chooses the
569 * same nid as we do. Move nid forward in the mask even
570 * if we just successfully allocated a hugepage so that
571 * the next caller gets hugepages on the next node.
572 */
Andi Kleena5516432008-07-23 21:27:41 -0700573 next_nid = next_node(h->hugetlb_next_nid, node_online_map);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700574 if (next_nid == MAX_NUMNODES)
575 next_nid = first_node(node_online_map);
Andi Kleena5516432008-07-23 21:27:41 -0700576 h->hugetlb_next_nid = next_nid;
577 } while (!page && h->hugetlb_next_nid != start_nid);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700578
Adam Litke3b116302008-04-28 02:13:06 -0700579 if (ret)
580 count_vm_event(HTLB_BUDDY_PGALLOC);
581 else
582 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
583
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700584 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Andi Kleena5516432008-07-23 21:27:41 -0700587static struct page *alloc_buddy_huge_page(struct hstate *h,
588 struct vm_area_struct *vma, unsigned long address)
Adam Litke7893d1d2007-10-16 01:26:18 -0700589{
590 struct page *page;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800591 unsigned int nid;
Adam Litke7893d1d2007-10-16 01:26:18 -0700592
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800593 /*
594 * Assume we will successfully allocate the surplus page to
595 * prevent racing processes from causing the surplus to exceed
596 * overcommit
597 *
598 * This however introduces a different race, where a process B
599 * tries to grow the static hugepage pool while alloc_pages() is
600 * called by process A. B will only examine the per-node
601 * counters in determining if surplus huge pages can be
602 * converted to normal huge pages in adjust_pool_surplus(). A
603 * won't be able to increment the per-node counter, until the
604 * lock is dropped by B, but B doesn't drop hugetlb_lock until
605 * no more huge pages can be converted from surplus to normal
606 * state (and doesn't try to convert again). Thus, we have a
607 * case where a surplus huge page exists, the pool is grown, and
608 * the surplus huge page still exists after, even though it
609 * should just have been converted to a normal huge page. This
610 * does not leak memory, though, as the hugepage will be freed
611 * once it is out of use. It also does not allow the counters to
612 * go out of whack in adjust_pool_surplus() as we don't modify
613 * the node values until we've gotten the hugepage and only the
614 * per-node value is checked there.
615 */
616 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700617 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800618 spin_unlock(&hugetlb_lock);
619 return NULL;
620 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700621 h->nr_huge_pages++;
622 h->surplus_huge_pages++;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800623 }
624 spin_unlock(&hugetlb_lock);
625
Nishanth Aravamudan551883a2008-04-29 00:58:26 -0700626 page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
627 __GFP_REPEAT|__GFP_NOWARN,
Andi Kleena5516432008-07-23 21:27:41 -0700628 huge_page_order(h));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800629
630 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700631 if (page) {
Adam Litke2668db92008-03-10 11:43:50 -0700632 /*
633 * This page is now managed by the hugetlb allocator and has
634 * no users -- drop the buddy allocator's reference.
635 */
636 put_page_testzero(page);
637 VM_BUG_ON(page_count(page));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800638 nid = page_to_nid(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700639 set_compound_page_dtor(page, free_huge_page);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800640 /*
641 * We incremented the global counters already
642 */
Andi Kleena5516432008-07-23 21:27:41 -0700643 h->nr_huge_pages_node[nid]++;
644 h->surplus_huge_pages_node[nid]++;
Adam Litke3b116302008-04-28 02:13:06 -0700645 __count_vm_event(HTLB_BUDDY_PGALLOC);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800646 } else {
Andi Kleena5516432008-07-23 21:27:41 -0700647 h->nr_huge_pages--;
648 h->surplus_huge_pages--;
Adam Litke3b116302008-04-28 02:13:06 -0700649 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Adam Litke7893d1d2007-10-16 01:26:18 -0700650 }
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800651 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -0700652
653 return page;
654}
655
Adam Litkee4e574b2007-10-16 01:26:19 -0700656/*
657 * Increase the hugetlb pool such that it can accomodate a reservation
658 * of size 'delta'.
659 */
Andi Kleena5516432008-07-23 21:27:41 -0700660static int gather_surplus_pages(struct hstate *h, int delta)
Adam Litkee4e574b2007-10-16 01:26:19 -0700661{
662 struct list_head surplus_list;
663 struct page *page, *tmp;
664 int ret, i;
665 int needed, allocated;
666
Andi Kleena5516432008-07-23 21:27:41 -0700667 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800668 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -0700669 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700670 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800671 }
Adam Litkee4e574b2007-10-16 01:26:19 -0700672
673 allocated = 0;
674 INIT_LIST_HEAD(&surplus_list);
675
676 ret = -ENOMEM;
677retry:
678 spin_unlock(&hugetlb_lock);
679 for (i = 0; i < needed; i++) {
Andi Kleena5516432008-07-23 21:27:41 -0700680 page = alloc_buddy_huge_page(h, NULL, 0);
Adam Litkee4e574b2007-10-16 01:26:19 -0700681 if (!page) {
682 /*
683 * We were not able to allocate enough pages to
684 * satisfy the entire reservation so we free what
685 * we've allocated so far.
686 */
687 spin_lock(&hugetlb_lock);
688 needed = 0;
689 goto free;
690 }
691
692 list_add(&page->lru, &surplus_list);
693 }
694 allocated += needed;
695
696 /*
697 * After retaking hugetlb_lock, we need to recalculate 'needed'
698 * because either resv_huge_pages or free_huge_pages may have changed.
699 */
700 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700701 needed = (h->resv_huge_pages + delta) -
702 (h->free_huge_pages + allocated);
Adam Litkee4e574b2007-10-16 01:26:19 -0700703 if (needed > 0)
704 goto retry;
705
706 /*
707 * The surplus_list now contains _at_least_ the number of extra pages
708 * needed to accomodate the reservation. Add the appropriate number
709 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -0800710 * allocator. Commit the entire reservation here to prevent another
711 * process from stealing the pages as they are added to the pool but
712 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -0700713 */
714 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -0700715 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -0700716 ret = 0;
717free:
Adam Litke19fc3f02008-04-28 02:12:20 -0700718 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -0700719 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -0700720 if ((--needed) < 0)
721 break;
Adam Litkee4e574b2007-10-16 01:26:19 -0700722 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700723 enqueue_huge_page(h, page);
Adam Litke19fc3f02008-04-28 02:12:20 -0700724 }
725
726 /* Free unnecessary surplus pages to the buddy allocator */
727 if (!list_empty(&surplus_list)) {
728 spin_unlock(&hugetlb_lock);
729 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
730 list_del(&page->lru);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700731 /*
Adam Litke2668db92008-03-10 11:43:50 -0700732 * The page has a reference count of zero already, so
733 * call free_huge_page directly instead of using
734 * put_page. This must be done with hugetlb_lock
Adam Litkeaf767cb2007-10-16 01:26:25 -0700735 * unlocked which is safe because free_huge_page takes
736 * hugetlb_lock before deciding how to free the page.
737 */
Adam Litke2668db92008-03-10 11:43:50 -0700738 free_huge_page(page);
Adam Litkeaf767cb2007-10-16 01:26:25 -0700739 }
Adam Litke19fc3f02008-04-28 02:12:20 -0700740 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -0700741 }
742
743 return ret;
744}
745
746/*
747 * When releasing a hugetlb pool reservation, any surplus pages that were
748 * allocated to satisfy the reservation must be explicitly freed if they were
749 * never used.
750 */
Andi Kleena5516432008-07-23 21:27:41 -0700751static void return_unused_surplus_pages(struct hstate *h,
752 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -0700753{
754 static int nid = -1;
755 struct page *page;
756 unsigned long nr_pages;
757
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700758 /*
759 * We want to release as many surplus pages as possible, spread
760 * evenly across all nodes. Iterate across all nodes until we
761 * can no longer free unreserved surplus pages. This occurs when
762 * the nodes with surplus pages have no free pages.
763 */
764 unsigned long remaining_iterations = num_online_nodes();
765
Adam Litkeac09b3a2008-03-04 14:29:38 -0800766 /* Uncommit the reservation */
Andi Kleena5516432008-07-23 21:27:41 -0700767 h->resv_huge_pages -= unused_resv_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -0800768
Andi Kleena5516432008-07-23 21:27:41 -0700769 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -0700770
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700771 while (remaining_iterations-- && nr_pages) {
Adam Litkee4e574b2007-10-16 01:26:19 -0700772 nid = next_node(nid, node_online_map);
773 if (nid == MAX_NUMNODES)
774 nid = first_node(node_online_map);
775
Andi Kleena5516432008-07-23 21:27:41 -0700776 if (!h->surplus_huge_pages_node[nid])
Adam Litkee4e574b2007-10-16 01:26:19 -0700777 continue;
778
Andi Kleena5516432008-07-23 21:27:41 -0700779 if (!list_empty(&h->hugepage_freelists[nid])) {
780 page = list_entry(h->hugepage_freelists[nid].next,
Adam Litkee4e574b2007-10-16 01:26:19 -0700781 struct page, lru);
782 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700783 update_and_free_page(h, page);
784 h->free_huge_pages--;
785 h->free_huge_pages_node[nid]--;
786 h->surplus_huge_pages--;
787 h->surplus_huge_pages_node[nid]--;
Adam Litkee4e574b2007-10-16 01:26:19 -0700788 nr_pages--;
Nishanth Aravamudan11320d12008-03-26 14:40:20 -0700789 remaining_iterations = num_online_nodes();
Adam Litkee4e574b2007-10-16 01:26:19 -0700790 }
791 }
792}
793
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700794/*
795 * Determine if the huge page at addr within the vma has an associated
796 * reservation. Where it does not we will need to logically increase
797 * reservation and actually increase quota before an allocation can occur.
798 * Where any new reservation would be required the reservation change is
799 * prepared, but not committed. Once the page has been quota'd allocated
800 * an instantiated the change should be committed via vma_commit_reservation.
801 * No action is required on failure.
802 */
Andi Kleena5516432008-07-23 21:27:41 -0700803static int vma_needs_reservation(struct hstate *h,
804 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700805{
806 struct address_space *mapping = vma->vm_file->f_mapping;
807 struct inode *inode = mapping->host;
808
809 if (vma->vm_flags & VM_SHARED) {
Andi Kleena5516432008-07-23 21:27:41 -0700810 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700811 return region_chg(&inode->i_mapping->private_list,
812 idx, idx + 1);
813
Andy Whitcroft84afd992008-07-23 21:27:32 -0700814 } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
815 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700816
Andy Whitcroft84afd992008-07-23 21:27:32 -0700817 } else {
818 int err;
Andi Kleena5516432008-07-23 21:27:41 -0700819 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700820 struct resv_map *reservations = vma_resv_map(vma);
821
822 err = region_chg(&reservations->regions, idx, idx + 1);
823 if (err < 0)
824 return err;
825 return 0;
826 }
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700827}
Andi Kleena5516432008-07-23 21:27:41 -0700828static void vma_commit_reservation(struct hstate *h,
829 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700830{
831 struct address_space *mapping = vma->vm_file->f_mapping;
832 struct inode *inode = mapping->host;
833
834 if (vma->vm_flags & VM_SHARED) {
Andi Kleena5516432008-07-23 21:27:41 -0700835 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700836 region_add(&inode->i_mapping->private_list, idx, idx + 1);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700837
838 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
Andi Kleena5516432008-07-23 21:27:41 -0700839 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700840 struct resv_map *reservations = vma_resv_map(vma);
841
842 /* Mark this page used in the map. */
843 region_add(&reservations->regions, idx, idx + 1);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700844 }
845}
846
David Gibson27a85ef2006-03-22 00:08:56 -0800847static struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700848 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Andi Kleena5516432008-07-23 21:27:41 -0700850 struct hstate *h = hstate_vma(vma);
Adam Litke348ea202007-11-14 16:59:37 -0800851 struct page *page;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800852 struct address_space *mapping = vma->vm_file->f_mapping;
Mel Gormana1e78772008-07-23 21:27:23 -0700853 struct inode *inode = mapping->host;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700854 unsigned int chg;
Adam Litke2fc39ce2007-11-14 16:59:39 -0800855
Mel Gormana1e78772008-07-23 21:27:23 -0700856 /*
857 * Processes that did not create the mapping will have no reserves and
858 * will not have accounted against quota. Check that the quota can be
859 * made before satisfying the allocation
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700860 * MAP_NORESERVE mappings may also need pages and quota allocated
861 * if no reserve mapping overlaps.
Mel Gormana1e78772008-07-23 21:27:23 -0700862 */
Andi Kleena5516432008-07-23 21:27:41 -0700863 chg = vma_needs_reservation(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700864 if (chg < 0)
865 return ERR_PTR(chg);
866 if (chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700867 if (hugetlb_get_quota(inode->i_mapping, chg))
868 return ERR_PTR(-ENOSPC);
Mel Gormana1e78772008-07-23 21:27:23 -0700869
870 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700871 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
Mel Gormana1e78772008-07-23 21:27:23 -0700872 spin_unlock(&hugetlb_lock);
873
874 if (!page) {
Andi Kleena5516432008-07-23 21:27:41 -0700875 page = alloc_buddy_huge_page(h, vma, addr);
Mel Gormana1e78772008-07-23 21:27:23 -0700876 if (!page) {
877 hugetlb_put_quota(inode->i_mapping, chg);
878 return ERR_PTR(-VM_FAULT_OOM);
879 }
880 }
881
882 set_page_refcounted(page);
883 set_page_private(page, (unsigned long) mapping);
884
Andi Kleena5516432008-07-23 21:27:41 -0700885 vma_commit_reservation(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -0700886
Adam Litke90d8b7e2007-11-14 16:59:42 -0800887 return page;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890static int __init hugetlb_init(void)
891{
892 unsigned long i;
Andi Kleena5516432008-07-23 21:27:41 -0700893 struct hstate *h = &default_hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100895 if (HPAGE_SHIFT == 0)
896 return 0;
897
Andi Kleena5516432008-07-23 21:27:41 -0700898 if (!h->order) {
899 h->order = HPAGE_SHIFT - PAGE_SHIFT;
900 h->mask = HPAGE_MASK;
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Andi Kleena5516432008-07-23 21:27:41 -0700903 for (i = 0; i < MAX_NUMNODES; ++i)
904 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
905
906 h->hugetlb_next_nid = first_node(node_online_map);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -0700907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 for (i = 0; i < max_huge_pages; ++i) {
Andi Kleena5516432008-07-23 21:27:41 -0700909 if (!alloc_fresh_huge_page(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Andi Kleena5516432008-07-23 21:27:41 -0700912 max_huge_pages = h->free_huge_pages = h->nr_huge_pages = i;
913 printk(KERN_INFO "Total HugeTLB memory allocated, %ld\n",
914 h->free_huge_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
917module_init(hugetlb_init);
918
919static int __init hugetlb_setup(char *s)
920{
921 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
922 max_huge_pages = 0;
923 return 1;
924}
925__setup("hugepages=", hugetlb_setup);
926
Ken Chen8a630112007-05-09 02:33:34 -0700927static unsigned int cpuset_mems_nr(unsigned int *array)
928{
929 int node;
930 unsigned int nr = 0;
931
932 for_each_node_mask(node, cpuset_current_mems_allowed)
933 nr += array[node];
934
935 return nr;
936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939#ifdef CONFIG_HIGHMEM
Andi Kleena5516432008-07-23 21:27:41 -0700940static void try_to_free_low(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Christoph Lameter4415cc82006-09-25 23:31:55 -0700942 int i;
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 for (i = 0; i < MAX_NUMNODES; ++i) {
945 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -0700946 struct list_head *freel = &h->hugepage_freelists[i];
947 list_for_each_entry_safe(page, next, freel, lru) {
948 if (count >= h->nr_huge_pages)
Adam Litke6b0c8802007-10-16 01:26:23 -0700949 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (PageHighMem(page))
951 continue;
952 list_del(&page->lru);
953 update_and_free_page(page);
Andi Kleena5516432008-07-23 21:27:41 -0700954 h->free_huge_pages--;
955 h->free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 }
958}
959#else
Andi Kleena5516432008-07-23 21:27:41 -0700960static inline void try_to_free_low(struct hstate *h, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962}
963#endif
964
Andi Kleena5516432008-07-23 21:27:41 -0700965#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966static unsigned long set_max_huge_pages(unsigned long count)
967{
Adam Litke7893d1d2007-10-16 01:26:18 -0700968 unsigned long min_count, ret;
Andi Kleena5516432008-07-23 21:27:41 -0700969 struct hstate *h = &default_hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Adam Litke7893d1d2007-10-16 01:26:18 -0700971 /*
972 * Increase the pool size
973 * First take pages out of surplus state. Then make up the
974 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -0800975 *
976 * We might race with alloc_buddy_huge_page() here and be unable
977 * to convert a surplus huge page to a normal huge page. That is
978 * not critical, though, it just means the overall size of the
979 * pool might be one hugepage larger than it needs to be, but
980 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -0700981 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700983 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
984 if (!adjust_pool_surplus(h, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -0700985 break;
986 }
987
Andi Kleena5516432008-07-23 21:27:41 -0700988 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -0700989 /*
990 * If this allocation races such that we no longer need the
991 * page, free_huge_page will handle it by freeing the page
992 * and reducing the surplus.
993 */
994 spin_unlock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -0700995 ret = alloc_fresh_huge_page(h);
Adam Litke7893d1d2007-10-16 01:26:18 -0700996 spin_lock(&hugetlb_lock);
997 if (!ret)
998 goto out;
999
1000 }
Adam Litke7893d1d2007-10-16 01:26:18 -07001001
1002 /*
1003 * Decrease the pool size
1004 * First return free pages to the buddy allocator (being careful
1005 * to keep enough around to satisfy reservations). Then place
1006 * pages into surplus state as needed so the pool will shrink
1007 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001008 *
1009 * By placing pages into the surplus state independent of the
1010 * overcommit value, we are allowing the surplus pool size to
1011 * exceed overcommit. There are few sane options here. Since
1012 * alloc_buddy_huge_page() is checking the global counter,
1013 * though, we'll note that we're not allowed to exceed surplus
1014 * and won't grow the pool anywhere else. Not until one of the
1015 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07001016 */
Andi Kleena5516432008-07-23 21:27:41 -07001017 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07001018 min_count = max(count, min_count);
Andi Kleena5516432008-07-23 21:27:41 -07001019 try_to_free_low(h, min_count);
1020 while (min_count < persistent_huge_pages(h)) {
1021 struct page *page = dequeue_huge_page(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (!page)
1023 break;
Andi Kleena5516432008-07-23 21:27:41 -07001024 update_and_free_page(h, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Andi Kleena5516432008-07-23 21:27:41 -07001026 while (count < persistent_huge_pages(h)) {
1027 if (!adjust_pool_surplus(h, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07001028 break;
1029 }
1030out:
Andi Kleena5516432008-07-23 21:27:41 -07001031 ret = persistent_huge_pages(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001033 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036int hugetlb_sysctl_handler(struct ctl_table *table, int write,
1037 struct file *file, void __user *buffer,
1038 size_t *length, loff_t *ppos)
1039{
1040 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
1041 max_huge_pages = set_max_huge_pages(max_huge_pages);
1042 return 0;
1043}
Mel Gorman396faf02007-07-17 04:03:13 -07001044
1045int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
1046 struct file *file, void __user *buffer,
1047 size_t *length, loff_t *ppos)
1048{
1049 proc_dointvec(table, write, file, buffer, length, ppos);
1050 if (hugepages_treat_as_movable)
1051 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
1052 else
1053 htlb_alloc_mask = GFP_HIGHUSER;
1054 return 0;
1055}
1056
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001057int hugetlb_overcommit_handler(struct ctl_table *table, int write,
1058 struct file *file, void __user *buffer,
1059 size_t *length, loff_t *ppos)
1060{
Andi Kleena5516432008-07-23 21:27:41 -07001061 struct hstate *h = &default_hstate;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001062 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
Nishanth Aravamudan064d9ef2008-02-13 15:03:19 -08001063 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001064 h->nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08001065 spin_unlock(&hugetlb_lock);
1066 return 0;
1067}
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069#endif /* CONFIG_SYSCTL */
1070
1071int hugetlb_report_meminfo(char *buf)
1072{
Andi Kleena5516432008-07-23 21:27:41 -07001073 struct hstate *h = &default_hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return sprintf(buf,
1075 "HugePages_Total: %5lu\n"
1076 "HugePages_Free: %5lu\n"
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001077 "HugePages_Rsvd: %5lu\n"
Adam Litke7893d1d2007-10-16 01:26:18 -07001078 "HugePages_Surp: %5lu\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 "Hugepagesize: %5lu kB\n",
Andi Kleena5516432008-07-23 21:27:41 -07001080 h->nr_huge_pages,
1081 h->free_huge_pages,
1082 h->resv_huge_pages,
1083 h->surplus_huge_pages,
1084 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087int hugetlb_report_node_meminfo(int nid, char *buf)
1088{
Andi Kleena5516432008-07-23 21:27:41 -07001089 struct hstate *h = &default_hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return sprintf(buf,
1091 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07001092 "Node %d HugePages_Free: %5u\n"
1093 "Node %d HugePages_Surp: %5u\n",
Andi Kleena5516432008-07-23 21:27:41 -07001094 nid, h->nr_huge_pages_node[nid],
1095 nid, h->free_huge_pages_node[nid],
1096 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
1100unsigned long hugetlb_total_pages(void)
1101{
Andi Kleena5516432008-07-23 21:27:41 -07001102 struct hstate *h = &default_hstate;
1103 return h->nr_huge_pages * pages_per_huge_page(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Andi Kleena5516432008-07-23 21:27:41 -07001106static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001107{
1108 int ret = -ENOMEM;
1109
1110 spin_lock(&hugetlb_lock);
1111 /*
1112 * When cpuset is configured, it breaks the strict hugetlb page
1113 * reservation as the accounting is done on a global variable. Such
1114 * reservation is completely rubbish in the presence of cpuset because
1115 * the reservation is not checked against page availability for the
1116 * current cpuset. Application can still potentially OOM'ed by kernel
1117 * with lack of free htlb page in cpuset that the task is in.
1118 * Attempt to enforce strict accounting with cpuset is almost
1119 * impossible (or too ugly) because cpuset is too fluid that
1120 * task or memory node can be dynamically moved between cpusets.
1121 *
1122 * The change of semantics for shared hugetlb mapping with cpuset is
1123 * undesirable. However, in order to preserve some of the semantics,
1124 * we fall back to check against current free page availability as
1125 * a best attempt and hopefully to minimize the impact of changing
1126 * semantics that cpuset has.
1127 */
1128 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07001129 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001130 goto out;
1131
Andi Kleena5516432008-07-23 21:27:41 -07001132 if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
1133 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001134 goto out;
1135 }
1136 }
1137
1138 ret = 0;
1139 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07001140 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07001141
1142out:
1143 spin_unlock(&hugetlb_lock);
1144 return ret;
1145}
1146
Andy Whitcroft84afd992008-07-23 21:27:32 -07001147static void hugetlb_vm_op_open(struct vm_area_struct *vma)
1148{
1149 struct resv_map *reservations = vma_resv_map(vma);
1150
1151 /*
1152 * This new VMA should share its siblings reservation map if present.
1153 * The VMA will only ever have a valid reservation map pointer where
1154 * it is being copied for another still existing VMA. As that VMA
1155 * has a reference to the reservation map it cannot dissappear until
1156 * after this open call completes. It is therefore safe to take a
1157 * new reference here without additional locking.
1158 */
1159 if (reservations)
1160 kref_get(&reservations->refs);
1161}
1162
Mel Gormana1e78772008-07-23 21:27:23 -07001163static void hugetlb_vm_op_close(struct vm_area_struct *vma)
1164{
Andi Kleena5516432008-07-23 21:27:41 -07001165 struct hstate *h = hstate_vma(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001166 struct resv_map *reservations = vma_resv_map(vma);
1167 unsigned long reserve;
1168 unsigned long start;
1169 unsigned long end;
1170
1171 if (reservations) {
Andi Kleena5516432008-07-23 21:27:41 -07001172 start = vma_hugecache_offset(h, vma, vma->vm_start);
1173 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001174
1175 reserve = (end - start) -
1176 region_count(&reservations->regions, start, end);
1177
1178 kref_put(&reservations->refs, resv_map_release);
1179
1180 if (reserve)
Andi Kleena5516432008-07-23 21:27:41 -07001181 hugetlb_acct_memory(h, -reserve);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001182 }
Mel Gormana1e78772008-07-23 21:27:23 -07001183}
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185/*
1186 * We cannot handle pagefaults against hugetlb pages at all. They cause
1187 * handle_mm_fault() to try to instantiate regular-sized pages in the
1188 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
1189 * this far.
1190 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001191static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07001198 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07001199 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07001200 .close = hugetlb_vm_op_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201};
1202
David Gibson1e8f8892006-01-06 00:10:44 -08001203static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
1204 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07001205{
1206 pte_t entry;
1207
David Gibson1e8f8892006-01-06 00:10:44 -08001208 if (writable) {
David Gibson63551ae2005-06-21 17:14:44 -07001209 entry =
1210 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1211 } else {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001212 entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07001213 }
1214 entry = pte_mkyoung(entry);
1215 entry = pte_mkhuge(entry);
1216
1217 return entry;
1218}
1219
David Gibson1e8f8892006-01-06 00:10:44 -08001220static void set_huge_ptep_writable(struct vm_area_struct *vma,
1221 unsigned long address, pte_t *ptep)
1222{
1223 pte_t entry;
1224
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001225 entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
1226 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001227 update_mmu_cache(vma, address, entry);
Benjamin Herrenschmidt8dab5242007-06-16 10:16:12 -07001228 }
David Gibson1e8f8892006-01-06 00:10:44 -08001229}
1230
1231
David Gibson63551ae2005-06-21 17:14:44 -07001232int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
1233 struct vm_area_struct *vma)
1234{
1235 pte_t *src_pte, *dst_pte, entry;
1236 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07001237 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08001238 int cow;
Andi Kleena5516432008-07-23 21:27:41 -07001239 struct hstate *h = hstate_vma(vma);
1240 unsigned long sz = huge_page_size(h);
David Gibson1e8f8892006-01-06 00:10:44 -08001241
1242 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07001243
Andi Kleena5516432008-07-23 21:27:41 -07001244 for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
Hugh Dickinsc74df322005-10-29 18:16:23 -07001245 src_pte = huge_pte_offset(src, addr);
1246 if (!src_pte)
1247 continue;
Andi Kleena5516432008-07-23 21:27:41 -07001248 dst_pte = huge_pte_alloc(dst, addr, sz);
David Gibson63551ae2005-06-21 17:14:44 -07001249 if (!dst_pte)
1250 goto nomem;
Larry Woodmanc5c99422008-01-24 05:49:25 -08001251
1252 /* If the pagetables are shared don't copy or take references */
1253 if (dst_pte == src_pte)
1254 continue;
1255
Hugh Dickinsc74df322005-10-29 18:16:23 -07001256 spin_lock(&dst->page_table_lock);
Nick Piggin46478752008-06-05 22:45:57 -07001257 spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001258 if (!huge_pte_none(huge_ptep_get(src_pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001259 if (cow)
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001260 huge_ptep_set_wrprotect(src, addr, src_pte);
1261 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07001262 ptepage = pte_page(entry);
1263 get_page(ptepage);
Hugh Dickins1c598272005-10-19 21:23:43 -07001264 set_huge_pte_at(dst, addr, dst_pte, entry);
1265 }
1266 spin_unlock(&src->page_table_lock);
Hugh Dickinsc74df322005-10-29 18:16:23 -07001267 spin_unlock(&dst->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001268 }
1269 return 0;
1270
1271nomem:
1272 return -ENOMEM;
1273}
1274
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001275void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001276 unsigned long end, struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07001277{
1278 struct mm_struct *mm = vma->vm_mm;
1279 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07001280 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07001281 pte_t pte;
1282 struct page *page;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001283 struct page *tmp;
Andi Kleena5516432008-07-23 21:27:41 -07001284 struct hstate *h = hstate_vma(vma);
1285 unsigned long sz = huge_page_size(h);
1286
Chen, Kenneth Wc0a499c2006-12-06 20:31:39 -08001287 /*
1288 * A page gathering list, protected by per file i_mmap_lock. The
1289 * lock is used to avoid list corruption from multiple unmapping
1290 * of the same page since we are using page->lru.
1291 */
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001292 LIST_HEAD(page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001293
1294 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07001295 BUG_ON(start & ~huge_page_mask(h));
1296 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07001297
Hugh Dickins508034a2005-10-29 18:16:30 -07001298 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001299 for (address = start; address < end; address += sz) {
David Gibsonc7546f82005-08-05 11:59:35 -07001300 ptep = huge_pte_offset(mm, address);
Adam Litke4c887262005-10-29 18:16:46 -07001301 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07001302 continue;
1303
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001304 if (huge_pmd_unshare(mm, &address, ptep))
1305 continue;
1306
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001307 /*
1308 * If a reference page is supplied, it is because a specific
1309 * page is being unmapped, not a range. Ensure the page we
1310 * are about to unmap is the actual page of interest.
1311 */
1312 if (ref_page) {
1313 pte = huge_ptep_get(ptep);
1314 if (huge_pte_none(pte))
1315 continue;
1316 page = pte_page(pte);
1317 if (page != ref_page)
1318 continue;
1319
1320 /*
1321 * Mark the VMA as having unmapped its page so that
1322 * future faults in this VMA will fail rather than
1323 * looking like data was lost
1324 */
1325 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
1326 }
1327
David Gibsonc7546f82005-08-05 11:59:35 -07001328 pte = huge_ptep_get_and_clear(mm, address, ptep);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001329 if (huge_pte_none(pte))
David Gibson63551ae2005-06-21 17:14:44 -07001330 continue;
David Gibsonc7546f82005-08-05 11:59:35 -07001331
David Gibson63551ae2005-06-21 17:14:44 -07001332 page = pte_page(pte);
Ken Chen6649a382007-02-08 14:20:27 -08001333 if (pte_dirty(pte))
1334 set_page_dirty(page);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001335 list_add(&page->lru, &page_list);
David Gibson63551ae2005-06-21 17:14:44 -07001336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 spin_unlock(&mm->page_table_lock);
Hugh Dickins508034a2005-10-29 18:16:30 -07001338 flush_tlb_range(vma, start, end);
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07001339 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1340 list_del(&page->lru);
1341 put_page(page);
1342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
David Gibson63551ae2005-06-21 17:14:44 -07001344
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001345void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001346 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001347{
1348 /*
1349 * It is undesirable to test vma->vm_file as it should be non-null
1350 * for valid hugetlb area. However, vm_file will be NULL in the error
1351 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
1352 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
1353 * to clean up. Since no pte has actually been setup, it is safe to
1354 * do nothing in this case.
1355 */
1356 if (vma->vm_file) {
1357 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001358 __unmap_hugepage_range(vma, start, end, ref_page);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07001359 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
1360 }
1361}
1362
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001363/*
1364 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1365 * mappping it owns the reserve page for. The intention is to unmap the page
1366 * from other VMAs and let the children be SIGKILLed if they are faulting the
1367 * same region.
1368 */
1369int unmap_ref_private(struct mm_struct *mm,
1370 struct vm_area_struct *vma,
1371 struct page *page,
1372 unsigned long address)
1373{
1374 struct vm_area_struct *iter_vma;
1375 struct address_space *mapping;
1376 struct prio_tree_iter iter;
1377 pgoff_t pgoff;
1378
1379 /*
1380 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1381 * from page cache lookup which is in HPAGE_SIZE units.
1382 */
1383 address = address & huge_page_mask(hstate_vma(vma));
1384 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
1385 + (vma->vm_pgoff >> PAGE_SHIFT);
1386 mapping = (struct address_space *)page_private(page);
1387
1388 vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1389 /* Do not unmap the current VMA */
1390 if (iter_vma == vma)
1391 continue;
1392
1393 /*
1394 * Unmap the page from other VMAs without their own reserves.
1395 * They get marked to be SIGKILLed if they fault in these
1396 * areas. This is because a future no-page fault on this VMA
1397 * could insert a zeroed page instead of the data existing
1398 * from the time of fork. This would look like data corruption
1399 */
1400 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
1401 unmap_hugepage_range(iter_vma,
1402 address, address + HPAGE_SIZE,
1403 page);
1404 }
1405
1406 return 1;
1407}
1408
David Gibson1e8f8892006-01-06 00:10:44 -08001409static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001410 unsigned long address, pte_t *ptep, pte_t pte,
1411 struct page *pagecache_page)
David Gibson1e8f8892006-01-06 00:10:44 -08001412{
Andi Kleena5516432008-07-23 21:27:41 -07001413 struct hstate *h = hstate_vma(vma);
David Gibson1e8f8892006-01-06 00:10:44 -08001414 struct page *old_page, *new_page;
David Gibson79ac6ba2006-03-22 00:08:51 -08001415 int avoidcopy;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001416 int outside_reserve = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001417
1418 old_page = pte_page(pte);
1419
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001420retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08001421 /* If no-one else is actually using this page, avoid the copy
1422 * and just make the page writable */
1423 avoidcopy = (page_count(old_page) == 1);
1424 if (avoidcopy) {
1425 set_huge_ptep_writable(vma, address, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07001426 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001427 }
1428
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001429 /*
1430 * If the process that created a MAP_PRIVATE mapping is about to
1431 * perform a COW due to a shared page count, attempt to satisfy
1432 * the allocation without using the existing reserves. The pagecache
1433 * page is used to determine if the reserve at this address was
1434 * consumed or not. If reserves were used, a partial faulted mapping
1435 * at the time of fork() could consume its reserves on COW instead
1436 * of the full address range.
1437 */
1438 if (!(vma->vm_flags & VM_SHARED) &&
1439 is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
1440 old_page != pagecache_page)
1441 outside_reserve = 1;
1442
David Gibson1e8f8892006-01-06 00:10:44 -08001443 page_cache_get(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001444 new_page = alloc_huge_page(vma, address, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08001445
Adam Litke2fc39ce2007-11-14 16:59:39 -08001446 if (IS_ERR(new_page)) {
David Gibson1e8f8892006-01-06 00:10:44 -08001447 page_cache_release(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001448
1449 /*
1450 * If a process owning a MAP_PRIVATE mapping fails to COW,
1451 * it is due to references held by a child and an insufficient
1452 * huge page pool. To guarantee the original mappers
1453 * reliability, unmap the page from child processes. The child
1454 * may get SIGKILLed if it later faults.
1455 */
1456 if (outside_reserve) {
1457 BUG_ON(huge_pte_none(pte));
1458 if (unmap_ref_private(mm, vma, old_page, address)) {
1459 BUG_ON(page_count(old_page) != 1);
1460 BUG_ON(huge_pte_none(pte));
1461 goto retry_avoidcopy;
1462 }
1463 WARN_ON_ONCE(1);
1464 }
1465
Adam Litke2fc39ce2007-11-14 16:59:39 -08001466 return -PTR_ERR(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001467 }
1468
1469 spin_unlock(&mm->page_table_lock);
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00001470 copy_huge_page(new_page, old_page, address, vma);
Nick Piggin0ed361d2008-02-04 22:29:34 -08001471 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08001472 spin_lock(&mm->page_table_lock);
1473
Andi Kleena5516432008-07-23 21:27:41 -07001474 ptep = huge_pte_offset(mm, address & huge_page_mask(h));
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001475 if (likely(pte_same(huge_ptep_get(ptep), pte))) {
David Gibson1e8f8892006-01-06 00:10:44 -08001476 /* Break COW */
Gerald Schaefer8fe627e2008-04-28 02:13:28 -07001477 huge_ptep_clear_flush(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08001478 set_huge_pte_at(mm, address, ptep,
1479 make_huge_pte(vma, new_page, 1));
1480 /* Make the old page be freed below */
1481 new_page = old_page;
1482 }
1483 page_cache_release(new_page);
1484 page_cache_release(old_page);
Nick Piggin83c54072007-07-19 01:47:05 -07001485 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001486}
1487
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001488/* Return the pagecache page at a given address within a VMA */
Andi Kleena5516432008-07-23 21:27:41 -07001489static struct page *hugetlbfs_pagecache_page(struct hstate *h,
1490 struct vm_area_struct *vma, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001491{
1492 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001493 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001494
1495 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07001496 idx = vma_hugecache_offset(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001497
1498 return find_lock_page(mapping, idx);
1499}
1500
Robert P. J. Daya1ed3dd2007-07-17 04:03:33 -07001501static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
David Gibson1e8f8892006-01-06 00:10:44 -08001502 unsigned long address, pte_t *ptep, int write_access)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001503{
Andi Kleena5516432008-07-23 21:27:41 -07001504 struct hstate *h = hstate_vma(vma);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001505 int ret = VM_FAULT_SIGBUS;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001506 pgoff_t idx;
Adam Litke4c887262005-10-29 18:16:46 -07001507 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07001508 struct page *page;
1509 struct address_space *mapping;
David Gibson1e8f8892006-01-06 00:10:44 -08001510 pte_t new_pte;
Adam Litke4c887262005-10-29 18:16:46 -07001511
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001512 /*
1513 * Currently, we are forced to kill the process in the event the
1514 * original mapper has unmapped pages from the child due to a failed
1515 * COW. Warn that such a situation has occured as it may not be obvious
1516 */
1517 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
1518 printk(KERN_WARNING
1519 "PID %d killed due to inadequate hugepage pool\n",
1520 current->pid);
1521 return ret;
1522 }
1523
Adam Litke4c887262005-10-29 18:16:46 -07001524 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07001525 idx = vma_hugecache_offset(h, vma, address);
Adam Litke4c887262005-10-29 18:16:46 -07001526
1527 /*
1528 * Use page lock to guard against racing truncation
1529 * before we get page_table_lock.
1530 */
Christoph Lameter6bda6662006-01-06 00:10:49 -08001531retry:
1532 page = find_lock_page(mapping, idx);
1533 if (!page) {
Andi Kleena5516432008-07-23 21:27:41 -07001534 size = i_size_read(mapping->host) >> huge_page_shift(h);
Hugh Dickinsebed4bf2006-10-28 10:38:43 -07001535 if (idx >= size)
1536 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001537 page = alloc_huge_page(vma, address, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08001538 if (IS_ERR(page)) {
1539 ret = -PTR_ERR(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001540 goto out;
1541 }
Andi Kleena5516432008-07-23 21:27:41 -07001542 clear_huge_page(page, address, huge_page_size(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08001543 __SetPageUptodate(page);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001544
Christoph Lameter6bda6662006-01-06 00:10:49 -08001545 if (vma->vm_flags & VM_SHARED) {
1546 int err;
Ken Chen45c682a2007-11-14 16:59:44 -08001547 struct inode *inode = mapping->host;
Christoph Lameter6bda6662006-01-06 00:10:49 -08001548
1549 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
1550 if (err) {
1551 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001552 if (err == -EEXIST)
1553 goto retry;
1554 goto out;
1555 }
Ken Chen45c682a2007-11-14 16:59:44 -08001556
1557 spin_lock(&inode->i_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001558 inode->i_blocks += blocks_per_huge_page(h);
Ken Chen45c682a2007-11-14 16:59:44 -08001559 spin_unlock(&inode->i_lock);
Christoph Lameter6bda6662006-01-06 00:10:49 -08001560 } else
1561 lock_page(page);
1562 }
David Gibson1e8f8892006-01-06 00:10:44 -08001563
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001564 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001565 size = i_size_read(mapping->host) >> huge_page_shift(h);
Adam Litke4c887262005-10-29 18:16:46 -07001566 if (idx >= size)
1567 goto backout;
1568
Nick Piggin83c54072007-07-19 01:47:05 -07001569 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001570 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07001571 goto backout;
1572
David Gibson1e8f8892006-01-06 00:10:44 -08001573 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
1574 && (vma->vm_flags & VM_SHARED)));
1575 set_huge_pte_at(mm, address, ptep, new_pte);
1576
1577 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1578 /* Optimization, do the COW without a second fault */
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001579 ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
David Gibson1e8f8892006-01-06 00:10:44 -08001580 }
1581
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001582 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001583 unlock_page(page);
1584out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001585 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07001586
1587backout:
1588 spin_unlock(&mm->page_table_lock);
Adam Litke4c887262005-10-29 18:16:46 -07001589 unlock_page(page);
1590 put_page(page);
1591 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01001592}
1593
Adam Litke86e52162006-01-06 00:10:43 -08001594int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1595 unsigned long address, int write_access)
1596{
1597 pte_t *ptep;
1598 pte_t entry;
David Gibson1e8f8892006-01-06 00:10:44 -08001599 int ret;
David Gibson3935baa2006-03-22 00:08:53 -08001600 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
Andi Kleena5516432008-07-23 21:27:41 -07001601 struct hstate *h = hstate_vma(vma);
Adam Litke86e52162006-01-06 00:10:43 -08001602
Andi Kleena5516432008-07-23 21:27:41 -07001603 ptep = huge_pte_alloc(mm, address, huge_page_size(h));
Adam Litke86e52162006-01-06 00:10:43 -08001604 if (!ptep)
1605 return VM_FAULT_OOM;
1606
David Gibson3935baa2006-03-22 00:08:53 -08001607 /*
1608 * Serialize hugepage allocation and instantiation, so that we don't
1609 * get spurious allocation failures if two CPUs race to instantiate
1610 * the same page in the page cache.
1611 */
1612 mutex_lock(&hugetlb_instantiation_mutex);
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001613 entry = huge_ptep_get(ptep);
1614 if (huge_pte_none(entry)) {
David Gibson3935baa2006-03-22 00:08:53 -08001615 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
1616 mutex_unlock(&hugetlb_instantiation_mutex);
1617 return ret;
1618 }
Adam Litke86e52162006-01-06 00:10:43 -08001619
Nick Piggin83c54072007-07-19 01:47:05 -07001620 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08001621
1622 spin_lock(&mm->page_table_lock);
1623 /* Check for a racing update before calling hugetlb_cow */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001624 if (likely(pte_same(entry, huge_ptep_get(ptep))))
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001625 if (write_access && !pte_write(entry)) {
1626 struct page *page;
Andi Kleena5516432008-07-23 21:27:41 -07001627 page = hugetlbfs_pagecache_page(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001628 ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
1629 if (page) {
1630 unlock_page(page);
1631 put_page(page);
1632 }
1633 }
David Gibson1e8f8892006-01-06 00:10:44 -08001634 spin_unlock(&mm->page_table_lock);
David Gibson3935baa2006-03-22 00:08:53 -08001635 mutex_unlock(&hugetlb_instantiation_mutex);
David Gibson1e8f8892006-01-06 00:10:44 -08001636
1637 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08001638}
1639
David Gibson63551ae2005-06-21 17:14:44 -07001640int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
1641 struct page **pages, struct vm_area_struct **vmas,
Adam Litke5b23dbe2007-11-14 16:59:33 -08001642 unsigned long *position, int *length, int i,
1643 int write)
David Gibson63551ae2005-06-21 17:14:44 -07001644{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001645 unsigned long pfn_offset;
1646 unsigned long vaddr = *position;
David Gibson63551ae2005-06-21 17:14:44 -07001647 int remainder = *length;
Andi Kleena5516432008-07-23 21:27:41 -07001648 struct hstate *h = hstate_vma(vma);
David Gibson63551ae2005-06-21 17:14:44 -07001649
Hugh Dickins1c598272005-10-19 21:23:43 -07001650 spin_lock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001651 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07001652 pte_t *pte;
1653 struct page *page;
1654
1655 /*
1656 * Some archs (sparc64, sh*) have multiple pte_ts to
1657 * each hugepage. We have to make * sure we get the
1658 * first, for the page indexing below to work.
1659 */
Andi Kleena5516432008-07-23 21:27:41 -07001660 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
Adam Litke4c887262005-10-29 18:16:46 -07001661
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001662 if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
1663 (write && !pte_write(huge_ptep_get(pte)))) {
Adam Litke4c887262005-10-29 18:16:46 -07001664 int ret;
1665
1666 spin_unlock(&mm->page_table_lock);
Adam Litke5b23dbe2007-11-14 16:59:33 -08001667 ret = hugetlb_fault(mm, vma, vaddr, write);
Adam Litke4c887262005-10-29 18:16:46 -07001668 spin_lock(&mm->page_table_lock);
Adam Litkea89182c2007-08-22 14:01:51 -07001669 if (!(ret & VM_FAULT_ERROR))
Adam Litke4c887262005-10-29 18:16:46 -07001670 continue;
1671
1672 remainder = 0;
1673 if (!i)
1674 i = -EFAULT;
1675 break;
1676 }
David Gibson63551ae2005-06-21 17:14:44 -07001677
Andi Kleena5516432008-07-23 21:27:41 -07001678 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001679 page = pte_page(huge_ptep_get(pte));
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001680same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001681 if (pages) {
1682 get_page(page);
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001683 pages[i] = page + pfn_offset;
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08001684 }
David Gibson63551ae2005-06-21 17:14:44 -07001685
1686 if (vmas)
1687 vmas[i] = vma;
1688
1689 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001690 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07001691 --remainder;
1692 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001693 if (vaddr < vma->vm_end && remainder &&
Andi Kleena5516432008-07-23 21:27:41 -07001694 pfn_offset < pages_per_huge_page(h)) {
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08001695 /*
1696 * We use pfn_offset to avoid touching the pageframes
1697 * of this compound page.
1698 */
1699 goto same_page;
1700 }
David Gibson63551ae2005-06-21 17:14:44 -07001701 }
Hugh Dickins1c598272005-10-19 21:23:43 -07001702 spin_unlock(&mm->page_table_lock);
David Gibson63551ae2005-06-21 17:14:44 -07001703 *length = remainder;
1704 *position = vaddr;
1705
1706 return i;
1707}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001708
1709void hugetlb_change_protection(struct vm_area_struct *vma,
1710 unsigned long address, unsigned long end, pgprot_t newprot)
1711{
1712 struct mm_struct *mm = vma->vm_mm;
1713 unsigned long start = address;
1714 pte_t *ptep;
1715 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07001716 struct hstate *h = hstate_vma(vma);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001717
1718 BUG_ON(address >= end);
1719 flush_cache_range(vma, address, end);
1720
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001721 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001722 spin_lock(&mm->page_table_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001723 for (; address < end; address += huge_page_size(h)) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001724 ptep = huge_pte_offset(mm, address);
1725 if (!ptep)
1726 continue;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001727 if (huge_pmd_unshare(mm, &address, ptep))
1728 continue;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001729 if (!huge_pte_none(huge_ptep_get(ptep))) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001730 pte = huge_ptep_get_and_clear(mm, address, ptep);
1731 pte = pte_mkhuge(pte_modify(pte, newprot));
1732 set_huge_pte_at(mm, address, ptep, pte);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001733 }
1734 }
1735 spin_unlock(&mm->page_table_lock);
Chen, Kenneth W39dde652006-12-06 20:32:03 -08001736 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08001737
1738 flush_tlb_range(vma, start, end);
1739}
1740
Mel Gormana1e78772008-07-23 21:27:23 -07001741int hugetlb_reserve_pages(struct inode *inode,
1742 long from, long to,
1743 struct vm_area_struct *vma)
Adam Litkee4e574b2007-10-16 01:26:19 -07001744{
1745 long ret, chg;
Andi Kleena5516432008-07-23 21:27:41 -07001746 struct hstate *h = hstate_inode(inode);
Adam Litkee4e574b2007-10-16 01:26:19 -07001747
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001748 if (vma && vma->vm_flags & VM_NORESERVE)
1749 return 0;
1750
Mel Gormana1e78772008-07-23 21:27:23 -07001751 /*
1752 * Shared mappings base their reservation on the number of pages that
1753 * are already allocated on behalf of the file. Private mappings need
1754 * to reserve the full area even if read-only as mprotect() may be
1755 * called to make the mapping read-write. Assume !vma is a shm mapping
1756 */
1757 if (!vma || vma->vm_flags & VM_SHARED)
1758 chg = region_chg(&inode->i_mapping->private_list, from, to);
1759 else {
Andy Whitcroft84afd992008-07-23 21:27:32 -07001760 struct resv_map *resv_map = resv_map_alloc();
1761 if (!resv_map)
1762 return -ENOMEM;
1763
Mel Gormana1e78772008-07-23 21:27:23 -07001764 chg = to - from;
Andy Whitcroft84afd992008-07-23 21:27:32 -07001765
1766 set_vma_resv_map(vma, resv_map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001767 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
Mel Gormana1e78772008-07-23 21:27:23 -07001768 }
1769
Adam Litkee4e574b2007-10-16 01:26:19 -07001770 if (chg < 0)
1771 return chg;
Ken Chen8a630112007-05-09 02:33:34 -07001772
Adam Litke90d8b7e2007-11-14 16:59:42 -08001773 if (hugetlb_get_quota(inode->i_mapping, chg))
1774 return -ENOSPC;
Andi Kleena5516432008-07-23 21:27:41 -07001775 ret = hugetlb_acct_memory(h, chg);
Ken Chen68842c92008-01-14 00:55:19 -08001776 if (ret < 0) {
1777 hugetlb_put_quota(inode->i_mapping, chg);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001778 return ret;
Ken Chen68842c92008-01-14 00:55:19 -08001779 }
Mel Gormana1e78772008-07-23 21:27:23 -07001780 if (!vma || vma->vm_flags & VM_SHARED)
1781 region_add(&inode->i_mapping->private_list, from, to);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001782 return 0;
1783}
1784
1785void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1786{
Andi Kleena5516432008-07-23 21:27:41 -07001787 struct hstate *h = hstate_inode(inode);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001788 long chg = region_truncate(&inode->i_mapping->private_list, offset);
Ken Chen45c682a2007-11-14 16:59:44 -08001789
1790 spin_lock(&inode->i_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001791 inode->i_blocks -= blocks_per_huge_page(h);
Ken Chen45c682a2007-11-14 16:59:44 -08001792 spin_unlock(&inode->i_lock);
1793
Adam Litke90d8b7e2007-11-14 16:59:42 -08001794 hugetlb_put_quota(inode->i_mapping, (chg - freed));
Andi Kleena5516432008-07-23 21:27:41 -07001795 hugetlb_acct_memory(h, -(chg - freed));
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07001796}