Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
e1759c21 | 10 | #include <linux/seq_file.h> |
1da177e4 LT |
11 | #include <linux/sysctl.h> |
12 | #include <linux/highmem.h> | |
cddb8a5c | 13 | #include <linux/mmu_notifier.h> |
1da177e4 | 14 | #include <linux/nodemask.h> |
63551ae0 | 15 | #include <linux/pagemap.h> |
5da7ca86 | 16 | #include <linux/mempolicy.h> |
aea47ff3 | 17 | #include <linux/cpuset.h> |
3935baa9 | 18 | #include <linux/mutex.h> |
aa888a74 | 19 | #include <linux/bootmem.h> |
a3437870 | 20 | #include <linux/sysfs.h> |
d6606683 | 21 | |
63551ae0 DG |
22 | #include <asm/page.h> |
23 | #include <asm/pgtable.h> | |
78a34ae2 | 24 | #include <asm/io.h> |
63551ae0 DG |
25 | |
26 | #include <linux/hugetlb.h> | |
7835e98b | 27 | #include "internal.h" |
1da177e4 LT |
28 | |
29 | const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL; | |
396faf03 MG |
30 | static gfp_t htlb_alloc_mask = GFP_HIGHUSER; |
31 | unsigned long hugepages_treat_as_movable; | |
a5516438 | 32 | |
e5ff2159 AK |
33 | static int max_hstate; |
34 | unsigned int default_hstate_idx; | |
35 | struct hstate hstates[HUGE_MAX_HSTATE]; | |
36 | ||
53ba51d2 JT |
37 | __initdata LIST_HEAD(huge_boot_pages); |
38 | ||
e5ff2159 AK |
39 | /* for command line parsing */ |
40 | static struct hstate * __initdata parsed_hstate; | |
41 | static unsigned long __initdata default_hstate_max_huge_pages; | |
e11bfbfc | 42 | static unsigned long __initdata default_hstate_size; |
e5ff2159 AK |
43 | |
44 | #define for_each_hstate(h) \ | |
45 | for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++) | |
396faf03 | 46 | |
3935baa9 DG |
47 | /* |
48 | * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages | |
49 | */ | |
50 | static DEFINE_SPINLOCK(hugetlb_lock); | |
0bd0f9fb | 51 | |
96822904 AW |
52 | /* |
53 | * Region tracking -- allows tracking of reservations and instantiated pages | |
54 | * across the pages in a mapping. | |
84afd99b AW |
55 | * |
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); | |
96822904 AW |
65 | */ |
66 | struct file_region { | |
67 | struct list_head link; | |
68 | long from; | |
69 | long to; | |
70 | }; | |
71 | ||
72 | static 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 | ||
108 | static 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 | ||
157 | static 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 | ||
84afd99b AW |
187 | static 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 | ||
e7c4b0bf AW |
211 | /* |
212 | * Convert the address within this vma to the page offset within | |
213 | * the mapping, in pagecache page units; huge pages here. | |
214 | */ | |
a5516438 AK |
215 | static pgoff_t vma_hugecache_offset(struct hstate *h, |
216 | struct vm_area_struct *vma, unsigned long address) | |
e7c4b0bf | 217 | { |
a5516438 AK |
218 | return ((address - vma->vm_start) >> huge_page_shift(h)) + |
219 | (vma->vm_pgoff >> huge_page_order(h)); | |
e7c4b0bf AW |
220 | } |
221 | ||
08fba699 MG |
222 | /* |
223 | * 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 | */ | |
226 | unsigned 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 | ||
84afd99b AW |
238 | /* |
239 | * 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) | |
04f2cbe3 | 245 | #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) |
84afd99b | 246 | |
a1e78772 MG |
247 | /* |
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. | |
84afd99b AW |
256 | * |
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. | |
a1e78772 | 265 | */ |
e7c4b0bf AW |
266 | static unsigned long get_vma_private_data(struct vm_area_struct *vma) |
267 | { | |
268 | return (unsigned long)vma->vm_private_data; | |
269 | } | |
270 | ||
271 | static void set_vma_private_data(struct vm_area_struct *vma, | |
272 | unsigned long value) | |
273 | { | |
274 | vma->vm_private_data = (void *)value; | |
275 | } | |
276 | ||
84afd99b AW |
277 | struct resv_map { |
278 | struct kref refs; | |
279 | struct list_head regions; | |
280 | }; | |
281 | ||
2a4b3ded | 282 | static struct resv_map *resv_map_alloc(void) |
84afd99b AW |
283 | { |
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 | ||
2a4b3ded | 294 | static void resv_map_release(struct kref *ref) |
84afd99b AW |
295 | { |
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 | ||
303 | static struct resv_map *vma_resv_map(struct vm_area_struct *vma) | |
a1e78772 MG |
304 | { |
305 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); | |
306 | if (!(vma->vm_flags & VM_SHARED)) | |
84afd99b AW |
307 | return (struct resv_map *)(get_vma_private_data(vma) & |
308 | ~HPAGE_RESV_MASK); | |
2a4b3ded | 309 | return NULL; |
a1e78772 MG |
310 | } |
311 | ||
84afd99b | 312 | static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) |
a1e78772 MG |
313 | { |
314 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); | |
315 | VM_BUG_ON(vma->vm_flags & VM_SHARED); | |
316 | ||
84afd99b AW |
317 | set_vma_private_data(vma, (get_vma_private_data(vma) & |
318 | HPAGE_RESV_MASK) | (unsigned long)map); | |
04f2cbe3 MG |
319 | } |
320 | ||
321 | static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) | |
322 | { | |
04f2cbe3 | 323 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
e7c4b0bf AW |
324 | VM_BUG_ON(vma->vm_flags & VM_SHARED); |
325 | ||
326 | set_vma_private_data(vma, get_vma_private_data(vma) | flags); | |
04f2cbe3 MG |
327 | } |
328 | ||
329 | static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) | |
330 | { | |
331 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); | |
e7c4b0bf AW |
332 | |
333 | return (get_vma_private_data(vma) & flag) != 0; | |
a1e78772 MG |
334 | } |
335 | ||
336 | /* Decrement the reserved pages in the hugepage pool by one */ | |
a5516438 AK |
337 | static void decrement_hugepage_resv_vma(struct hstate *h, |
338 | struct vm_area_struct *vma) | |
a1e78772 | 339 | { |
c37f9fb1 AW |
340 | if (vma->vm_flags & VM_NORESERVE) |
341 | return; | |
342 | ||
a1e78772 MG |
343 | if (vma->vm_flags & VM_SHARED) { |
344 | /* Shared mappings always use reserves */ | |
a5516438 | 345 | h->resv_huge_pages--; |
84afd99b | 346 | } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { |
a1e78772 MG |
347 | /* |
348 | * Only the process that called mmap() has reserves for | |
349 | * private mappings. | |
350 | */ | |
a5516438 | 351 | h->resv_huge_pages--; |
a1e78772 MG |
352 | } |
353 | } | |
354 | ||
04f2cbe3 | 355 | /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ |
a1e78772 MG |
356 | void 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 */ | |
7f09ca51 | 364 | static int vma_has_reserves(struct vm_area_struct *vma) |
a1e78772 MG |
365 | { |
366 | if (vma->vm_flags & VM_SHARED) | |
7f09ca51 MG |
367 | return 1; |
368 | if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) | |
369 | return 1; | |
370 | return 0; | |
a1e78772 MG |
371 | } |
372 | ||
69d177c2 AW |
373 | static 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 | } | |
a5516438 AK |
385 | static void clear_huge_page(struct page *page, |
386 | unsigned long addr, unsigned long sz) | |
79ac6ba4 DG |
387 | { |
388 | int i; | |
389 | ||
69d177c2 AW |
390 | if (unlikely(sz > MAX_ORDER_NR_PAGES)) |
391 | return clear_gigantic_page(page, addr, sz); | |
392 | ||
79ac6ba4 | 393 | might_sleep(); |
a5516438 | 394 | for (i = 0; i < sz/PAGE_SIZE; i++) { |
79ac6ba4 | 395 | cond_resched(); |
281e0e3b | 396 | clear_user_highpage(page + i, addr + i * PAGE_SIZE); |
79ac6ba4 DG |
397 | } |
398 | } | |
399 | ||
69d177c2 AW |
400 | static 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 | } | |
79ac6ba4 | 417 | static void copy_huge_page(struct page *dst, struct page *src, |
9de455b2 | 418 | unsigned long addr, struct vm_area_struct *vma) |
79ac6ba4 DG |
419 | { |
420 | int i; | |
a5516438 | 421 | struct hstate *h = hstate_vma(vma); |
79ac6ba4 | 422 | |
69d177c2 AW |
423 | if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) |
424 | return copy_gigantic_page(dst, src, addr, vma); | |
425 | ||
79ac6ba4 | 426 | might_sleep(); |
a5516438 | 427 | for (i = 0; i < pages_per_huge_page(h); i++) { |
79ac6ba4 | 428 | cond_resched(); |
9de455b2 | 429 | copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); |
79ac6ba4 DG |
430 | } |
431 | } | |
432 | ||
a5516438 | 433 | static void enqueue_huge_page(struct hstate *h, struct page *page) |
1da177e4 LT |
434 | { |
435 | int nid = page_to_nid(page); | |
a5516438 AK |
436 | list_add(&page->lru, &h->hugepage_freelists[nid]); |
437 | h->free_huge_pages++; | |
438 | h->free_huge_pages_node[nid]++; | |
1da177e4 LT |
439 | } |
440 | ||
a5516438 | 441 | static struct page *dequeue_huge_page(struct hstate *h) |
348e1e04 NA |
442 | { |
443 | int nid; | |
444 | struct page *page = NULL; | |
445 | ||
446 | for (nid = 0; nid < MAX_NUMNODES; ++nid) { | |
a5516438 AK |
447 | if (!list_empty(&h->hugepage_freelists[nid])) { |
448 | page = list_entry(h->hugepage_freelists[nid].next, | |
348e1e04 NA |
449 | struct page, lru); |
450 | list_del(&page->lru); | |
a5516438 AK |
451 | h->free_huge_pages--; |
452 | h->free_huge_pages_node[nid]--; | |
348e1e04 NA |
453 | break; |
454 | } | |
455 | } | |
456 | return page; | |
457 | } | |
458 | ||
a5516438 AK |
459 | static struct page *dequeue_huge_page_vma(struct hstate *h, |
460 | struct vm_area_struct *vma, | |
04f2cbe3 | 461 | unsigned long address, int avoid_reserve) |
1da177e4 | 462 | { |
31a5c6e4 | 463 | int nid; |
1da177e4 | 464 | struct page *page = NULL; |
480eccf9 | 465 | struct mempolicy *mpol; |
19770b32 | 466 | nodemask_t *nodemask; |
396faf03 | 467 | struct zonelist *zonelist = huge_zonelist(vma, address, |
19770b32 | 468 | htlb_alloc_mask, &mpol, &nodemask); |
dd1a239f MG |
469 | struct zone *zone; |
470 | struct zoneref *z; | |
1da177e4 | 471 | |
a1e78772 MG |
472 | /* |
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 | */ | |
7f09ca51 | 477 | if (!vma_has_reserves(vma) && |
a5516438 | 478 | h->free_huge_pages - h->resv_huge_pages == 0) |
a1e78772 MG |
479 | return NULL; |
480 | ||
04f2cbe3 | 481 | /* If reserves cannot be used, ensure enough pages are in the pool */ |
a5516438 | 482 | if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0) |
04f2cbe3 MG |
483 | return NULL; |
484 | ||
19770b32 MG |
485 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
486 | MAX_NR_ZONES - 1, nodemask) { | |
54a6eb5c MG |
487 | nid = zone_to_nid(zone); |
488 | if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) && | |
a5516438 AK |
489 | !list_empty(&h->hugepage_freelists[nid])) { |
490 | page = list_entry(h->hugepage_freelists[nid].next, | |
3abf7afd AM |
491 | struct page, lru); |
492 | list_del(&page->lru); | |
a5516438 AK |
493 | h->free_huge_pages--; |
494 | h->free_huge_pages_node[nid]--; | |
04f2cbe3 MG |
495 | |
496 | if (!avoid_reserve) | |
a5516438 | 497 | decrement_hugepage_resv_vma(h, vma); |
a1e78772 | 498 | |
5ab3ee7b | 499 | break; |
3abf7afd | 500 | } |
1da177e4 | 501 | } |
52cd3b07 | 502 | mpol_cond_put(mpol); |
1da177e4 LT |
503 | return page; |
504 | } | |
505 | ||
a5516438 | 506 | static void update_and_free_page(struct hstate *h, struct page *page) |
6af2acb6 AL |
507 | { |
508 | int i; | |
a5516438 | 509 | |
18229df5 AW |
510 | VM_BUG_ON(h->order >= MAX_ORDER); |
511 | ||
a5516438 AK |
512 | 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++) { | |
6af2acb6 AL |
515 | 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); | |
7f2e9525 | 521 | arch_release_hugepage(page); |
a5516438 | 522 | __free_pages(page, huge_page_order(h)); |
6af2acb6 AL |
523 | } |
524 | ||
e5ff2159 AK |
525 | struct 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 | ||
27a85ef1 DG |
536 | static void free_huge_page(struct page *page) |
537 | { | |
a5516438 AK |
538 | /* |
539 | * Can't pass hstate in here because it is called from the | |
540 | * compound page destructor. | |
541 | */ | |
e5ff2159 | 542 | struct hstate *h = page_hstate(page); |
7893d1d5 | 543 | int nid = page_to_nid(page); |
c79fb75e | 544 | struct address_space *mapping; |
27a85ef1 | 545 | |
c79fb75e | 546 | mapping = (struct address_space *) page_private(page); |
e5df70ab | 547 | set_page_private(page, 0); |
7893d1d5 | 548 | BUG_ON(page_count(page)); |
27a85ef1 DG |
549 | INIT_LIST_HEAD(&page->lru); |
550 | ||
551 | spin_lock(&hugetlb_lock); | |
aa888a74 | 552 | if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) { |
a5516438 AK |
553 | update_and_free_page(h, page); |
554 | h->surplus_huge_pages--; | |
555 | h->surplus_huge_pages_node[nid]--; | |
7893d1d5 | 556 | } else { |
a5516438 | 557 | enqueue_huge_page(h, page); |
7893d1d5 | 558 | } |
27a85ef1 | 559 | spin_unlock(&hugetlb_lock); |
c79fb75e | 560 | if (mapping) |
9a119c05 | 561 | hugetlb_put_quota(mapping, 1); |
27a85ef1 DG |
562 | } |
563 | ||
7893d1d5 AL |
564 | /* |
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 | */ | |
a5516438 | 569 | static int adjust_pool_surplus(struct hstate *h, int delta) |
7893d1d5 AL |
570 | { |
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 */ | |
a5516438 | 582 | if (delta < 0 && !h->surplus_huge_pages_node[nid]) |
7893d1d5 AL |
583 | continue; |
584 | /* Surplus cannot exceed the total number of pages */ | |
a5516438 AK |
585 | if (delta > 0 && h->surplus_huge_pages_node[nid] >= |
586 | h->nr_huge_pages_node[nid]) | |
7893d1d5 AL |
587 | continue; |
588 | ||
a5516438 AK |
589 | h->surplus_huge_pages += delta; |
590 | h->surplus_huge_pages_node[nid] += delta; | |
7893d1d5 AL |
591 | ret = 1; |
592 | break; | |
593 | } while (nid != prev_nid); | |
594 | ||
595 | prev_nid = nid; | |
596 | return ret; | |
597 | } | |
598 | ||
a5516438 | 599 | static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) |
b7ba30c6 AK |
600 | { |
601 | set_compound_page_dtor(page, free_huge_page); | |
602 | spin_lock(&hugetlb_lock); | |
a5516438 AK |
603 | h->nr_huge_pages++; |
604 | h->nr_huge_pages_node[nid]++; | |
b7ba30c6 AK |
605 | spin_unlock(&hugetlb_lock); |
606 | put_page(page); /* free it into the hugepage allocator */ | |
607 | } | |
608 | ||
a5516438 | 609 | static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid) |
1da177e4 | 610 | { |
1da177e4 | 611 | struct page *page; |
f96efd58 | 612 | |
aa888a74 AK |
613 | if (h->order >= MAX_ORDER) |
614 | return NULL; | |
615 | ||
63b4613c | 616 | page = alloc_pages_node(nid, |
551883ae NA |
617 | htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE| |
618 | __GFP_REPEAT|__GFP_NOWARN, | |
a5516438 | 619 | huge_page_order(h)); |
1da177e4 | 620 | if (page) { |
7f2e9525 | 621 | if (arch_prepare_hugepage(page)) { |
caff3a2c | 622 | __free_pages(page, huge_page_order(h)); |
7b8ee84d | 623 | return NULL; |
7f2e9525 | 624 | } |
a5516438 | 625 | prep_new_huge_page(h, page, nid); |
1da177e4 | 626 | } |
63b4613c NA |
627 | |
628 | return page; | |
629 | } | |
630 | ||
5ced66c9 AK |
631 | /* |
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 | */ | |
642 | static 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 | ||
a5516438 | 652 | static int alloc_fresh_huge_page(struct hstate *h) |
63b4613c NA |
653 | { |
654 | struct page *page; | |
655 | int start_nid; | |
656 | int next_nid; | |
657 | int ret = 0; | |
658 | ||
a5516438 | 659 | start_nid = h->hugetlb_next_nid; |
63b4613c NA |
660 | |
661 | do { | |
a5516438 | 662 | page = alloc_fresh_huge_page_node(h, h->hugetlb_next_nid); |
63b4613c NA |
663 | if (page) |
664 | ret = 1; | |
5ced66c9 | 665 | next_nid = hstate_next_node(h); |
a5516438 | 666 | } while (!page && h->hugetlb_next_nid != start_nid); |
63b4613c | 667 | |
3b116300 AL |
668 | if (ret) |
669 | count_vm_event(HTLB_BUDDY_PGALLOC); | |
670 | else | |
671 | count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); | |
672 | ||
63b4613c | 673 | return ret; |
1da177e4 LT |
674 | } |
675 | ||
a5516438 AK |
676 | static struct page *alloc_buddy_huge_page(struct hstate *h, |
677 | struct vm_area_struct *vma, unsigned long address) | |
7893d1d5 AL |
678 | { |
679 | struct page *page; | |
d1c3fb1f | 680 | unsigned int nid; |
7893d1d5 | 681 | |
aa888a74 AK |
682 | if (h->order >= MAX_ORDER) |
683 | return NULL; | |
684 | ||
d1c3fb1f NA |
685 | /* |
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); | |
a5516438 | 709 | if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { |
d1c3fb1f NA |
710 | spin_unlock(&hugetlb_lock); |
711 | return NULL; | |
712 | } else { | |
a5516438 AK |
713 | h->nr_huge_pages++; |
714 | h->surplus_huge_pages++; | |
d1c3fb1f NA |
715 | } |
716 | spin_unlock(&hugetlb_lock); | |
717 | ||
551883ae NA |
718 | page = alloc_pages(htlb_alloc_mask|__GFP_COMP| |
719 | __GFP_REPEAT|__GFP_NOWARN, | |
a5516438 | 720 | huge_page_order(h)); |
d1c3fb1f | 721 | |
caff3a2c GS |
722 | if (page && arch_prepare_hugepage(page)) { |
723 | __free_pages(page, huge_page_order(h)); | |
724 | return NULL; | |
725 | } | |
726 | ||
d1c3fb1f | 727 | spin_lock(&hugetlb_lock); |
7893d1d5 | 728 | if (page) { |
2668db91 AL |
729 | /* |
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)); | |
d1c3fb1f | 735 | nid = page_to_nid(page); |
7893d1d5 | 736 | set_compound_page_dtor(page, free_huge_page); |
d1c3fb1f NA |
737 | /* |
738 | * We incremented the global counters already | |
739 | */ | |
a5516438 AK |
740 | h->nr_huge_pages_node[nid]++; |
741 | h->surplus_huge_pages_node[nid]++; | |
3b116300 | 742 | __count_vm_event(HTLB_BUDDY_PGALLOC); |
d1c3fb1f | 743 | } else { |
a5516438 AK |
744 | h->nr_huge_pages--; |
745 | h->surplus_huge_pages--; | |
3b116300 | 746 | __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); |
7893d1d5 | 747 | } |
d1c3fb1f | 748 | spin_unlock(&hugetlb_lock); |
7893d1d5 AL |
749 | |
750 | return page; | |
751 | } | |
752 | ||
e4e574b7 AL |
753 | /* |
754 | * Increase the hugetlb pool such that it can accomodate a reservation | |
755 | * of size 'delta'. | |
756 | */ | |
a5516438 | 757 | static int gather_surplus_pages(struct hstate *h, int delta) |
e4e574b7 AL |
758 | { |
759 | struct list_head surplus_list; | |
760 | struct page *page, *tmp; | |
761 | int ret, i; | |
762 | int needed, allocated; | |
763 | ||
a5516438 | 764 | needed = (h->resv_huge_pages + delta) - h->free_huge_pages; |
ac09b3a1 | 765 | if (needed <= 0) { |
a5516438 | 766 | h->resv_huge_pages += delta; |
e4e574b7 | 767 | return 0; |
ac09b3a1 | 768 | } |
e4e574b7 AL |
769 | |
770 | allocated = 0; | |
771 | INIT_LIST_HEAD(&surplus_list); | |
772 | ||
773 | ret = -ENOMEM; | |
774 | retry: | |
775 | spin_unlock(&hugetlb_lock); | |
776 | for (i = 0; i < needed; i++) { | |
a5516438 | 777 | page = alloc_buddy_huge_page(h, NULL, 0); |
e4e574b7 AL |
778 | 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); | |
a5516438 AK |
798 | needed = (h->resv_huge_pages + delta) - |
799 | (h->free_huge_pages + allocated); | |
e4e574b7 AL |
800 | 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 | |
ac09b3a1 AL |
807 | * 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. | |
e4e574b7 AL |
810 | */ |
811 | needed += allocated; | |
a5516438 | 812 | h->resv_huge_pages += delta; |
e4e574b7 AL |
813 | ret = 0; |
814 | free: | |
19fc3f0a | 815 | /* Free the needed pages to the hugetlb pool */ |
e4e574b7 | 816 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) { |
19fc3f0a AL |
817 | if ((--needed) < 0) |
818 | break; | |
e4e574b7 | 819 | list_del(&page->lru); |
a5516438 | 820 | enqueue_huge_page(h, page); |
19fc3f0a AL |
821 | } |
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); | |
af767cbd | 828 | /* |
2668db91 AL |
829 | * 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 | |
af767cbd AL |
832 | * unlocked which is safe because free_huge_page takes |
833 | * hugetlb_lock before deciding how to free the page. | |
834 | */ | |
2668db91 | 835 | free_huge_page(page); |
af767cbd | 836 | } |
19fc3f0a | 837 | spin_lock(&hugetlb_lock); |
e4e574b7 AL |
838 | } |
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 | */ | |
a5516438 AK |
848 | static void return_unused_surplus_pages(struct hstate *h, |
849 | unsigned long unused_resv_pages) | |
e4e574b7 AL |
850 | { |
851 | static int nid = -1; | |
852 | struct page *page; | |
853 | unsigned long nr_pages; | |
854 | ||
11320d17 NA |
855 | /* |
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 | ||
ac09b3a1 | 863 | /* Uncommit the reservation */ |
a5516438 | 864 | h->resv_huge_pages -= unused_resv_pages; |
ac09b3a1 | 865 | |
aa888a74 AK |
866 | /* Cannot return gigantic pages currently */ |
867 | if (h->order >= MAX_ORDER) | |
868 | return; | |
869 | ||
a5516438 | 870 | nr_pages = min(unused_resv_pages, h->surplus_huge_pages); |
e4e574b7 | 871 | |
11320d17 | 872 | while (remaining_iterations-- && nr_pages) { |
e4e574b7 AL |
873 | nid = next_node(nid, node_online_map); |
874 | if (nid == MAX_NUMNODES) | |
875 | nid = first_node(node_online_map); | |
876 | ||
a5516438 | 877 | if (!h->surplus_huge_pages_node[nid]) |
e4e574b7 AL |
878 | continue; |
879 | ||
a5516438 AK |
880 | if (!list_empty(&h->hugepage_freelists[nid])) { |
881 | page = list_entry(h->hugepage_freelists[nid].next, | |
e4e574b7 AL |
882 | struct page, lru); |
883 | list_del(&page->lru); | |
a5516438 AK |
884 | 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]--; | |
e4e574b7 | 889 | nr_pages--; |
11320d17 | 890 | remaining_iterations = num_online_nodes(); |
e4e574b7 AL |
891 | } |
892 | } | |
893 | } | |
894 | ||
c37f9fb1 AW |
895 | /* |
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 | */ | |
a5516438 AK |
904 | static int vma_needs_reservation(struct hstate *h, |
905 | struct vm_area_struct *vma, unsigned long addr) | |
c37f9fb1 AW |
906 | { |
907 | struct address_space *mapping = vma->vm_file->f_mapping; | |
908 | struct inode *inode = mapping->host; | |
909 | ||
910 | if (vma->vm_flags & VM_SHARED) { | |
a5516438 | 911 | pgoff_t idx = vma_hugecache_offset(h, vma, addr); |
c37f9fb1 AW |
912 | return region_chg(&inode->i_mapping->private_list, |
913 | idx, idx + 1); | |
914 | ||
84afd99b AW |
915 | } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { |
916 | return 1; | |
c37f9fb1 | 917 | |
84afd99b AW |
918 | } else { |
919 | int err; | |
a5516438 | 920 | pgoff_t idx = vma_hugecache_offset(h, vma, addr); |
84afd99b AW |
921 | 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 | } | |
c37f9fb1 | 928 | } |
a5516438 AK |
929 | static void vma_commit_reservation(struct hstate *h, |
930 | struct vm_area_struct *vma, unsigned long addr) | |
c37f9fb1 AW |
931 | { |
932 | struct address_space *mapping = vma->vm_file->f_mapping; | |
933 | struct inode *inode = mapping->host; | |
934 | ||
935 | if (vma->vm_flags & VM_SHARED) { | |
a5516438 | 936 | pgoff_t idx = vma_hugecache_offset(h, vma, addr); |
c37f9fb1 | 937 | region_add(&inode->i_mapping->private_list, idx, idx + 1); |
84afd99b AW |
938 | |
939 | } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { | |
a5516438 | 940 | pgoff_t idx = vma_hugecache_offset(h, vma, addr); |
84afd99b AW |
941 | 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); | |
c37f9fb1 AW |
945 | } |
946 | } | |
947 | ||
a1e78772 | 948 | static struct page *alloc_huge_page(struct vm_area_struct *vma, |
04f2cbe3 | 949 | unsigned long addr, int avoid_reserve) |
1da177e4 | 950 | { |
a5516438 | 951 | struct hstate *h = hstate_vma(vma); |
348ea204 | 952 | struct page *page; |
a1e78772 MG |
953 | struct address_space *mapping = vma->vm_file->f_mapping; |
954 | struct inode *inode = mapping->host; | |
c37f9fb1 | 955 | unsigned int chg; |
a1e78772 MG |
956 | |
957 | /* | |
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 | |
c37f9fb1 AW |
961 | * MAP_NORESERVE mappings may also need pages and quota allocated |
962 | * if no reserve mapping overlaps. | |
a1e78772 | 963 | */ |
a5516438 | 964 | chg = vma_needs_reservation(h, vma, addr); |
c37f9fb1 AW |
965 | if (chg < 0) |
966 | return ERR_PTR(chg); | |
967 | if (chg) | |
a1e78772 MG |
968 | if (hugetlb_get_quota(inode->i_mapping, chg)) |
969 | return ERR_PTR(-ENOSPC); | |
1da177e4 LT |
970 | |
971 | spin_lock(&hugetlb_lock); | |
a5516438 | 972 | page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve); |
1da177e4 | 973 | spin_unlock(&hugetlb_lock); |
b45b5bd6 | 974 | |
68842c9b | 975 | if (!page) { |
a5516438 | 976 | page = alloc_buddy_huge_page(h, vma, addr); |
68842c9b | 977 | if (!page) { |
a1e78772 | 978 | hugetlb_put_quota(inode->i_mapping, chg); |
68842c9b KC |
979 | return ERR_PTR(-VM_FAULT_OOM); |
980 | } | |
981 | } | |
348ea204 | 982 | |
a1e78772 MG |
983 | set_page_refcounted(page); |
984 | set_page_private(page, (unsigned long) mapping); | |
90d8b7e6 | 985 | |
a5516438 | 986 | vma_commit_reservation(h, vma, addr); |
c37f9fb1 | 987 | |
90d8b7e6 | 988 | return page; |
b45b5bd6 DG |
989 | } |
990 | ||
53ba51d2 | 991 | __attribute__((weak)) int alloc_bootmem_huge_page(struct hstate *h) |
aa888a74 AK |
992 | { |
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 | ||
1018 | found: | |
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 | ||
18229df5 AW |
1026 | static 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 | ||
aa888a74 AK |
1034 | /* Put bootmem huge pages into the standard lists after mem_map is up */ |
1035 | static 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); | |
18229df5 | 1044 | prep_compound_huge_page(page, h->order); |
aa888a74 AK |
1045 | prep_new_huge_page(h, page, page_to_nid(page)); |
1046 | } | |
1047 | } | |
1048 | ||
8faa8b07 | 1049 | static void __init hugetlb_hstate_alloc_pages(struct hstate *h) |
1da177e4 LT |
1050 | { |
1051 | unsigned long i; | |
a5516438 | 1052 | |
e5ff2159 | 1053 | for (i = 0; i < h->max_huge_pages; ++i) { |
aa888a74 AK |
1054 | if (h->order >= MAX_ORDER) { |
1055 | if (!alloc_bootmem_huge_page(h)) | |
1056 | break; | |
1057 | } else if (!alloc_fresh_huge_page(h)) | |
1da177e4 | 1058 | break; |
1da177e4 | 1059 | } |
8faa8b07 | 1060 | h->max_huge_pages = i; |
e5ff2159 AK |
1061 | } |
1062 | ||
1063 | static void __init hugetlb_init_hstates(void) | |
1064 | { | |
1065 | struct hstate *h; | |
1066 | ||
1067 | for_each_hstate(h) { | |
8faa8b07 AK |
1068 | /* oversize hugepages were init'ed in early boot */ |
1069 | if (h->order < MAX_ORDER) | |
1070 | hugetlb_hstate_alloc_pages(h); | |
e5ff2159 AK |
1071 | } |
1072 | } | |
1073 | ||
4abd32db AK |
1074 | static 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 | ||
e5ff2159 AK |
1085 | static void __init report_hugepages(void) |
1086 | { | |
1087 | struct hstate *h; | |
1088 | ||
1089 | for_each_hstate(h) { | |
4abd32db AK |
1090 | 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); | |
e5ff2159 AK |
1095 | } |
1096 | } | |
1097 | ||
1da177e4 | 1098 | #ifdef CONFIG_HIGHMEM |
a5516438 | 1099 | static void try_to_free_low(struct hstate *h, unsigned long count) |
1da177e4 | 1100 | { |
4415cc8d CL |
1101 | int i; |
1102 | ||
aa888a74 AK |
1103 | if (h->order >= MAX_ORDER) |
1104 | return; | |
1105 | ||
1da177e4 LT |
1106 | for (i = 0; i < MAX_NUMNODES; ++i) { |
1107 | struct page *page, *next; | |
a5516438 AK |
1108 | 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) | |
6b0c880d | 1111 | return; |
1da177e4 LT |
1112 | if (PageHighMem(page)) |
1113 | continue; | |
1114 | list_del(&page->lru); | |
e5ff2159 | 1115 | update_and_free_page(h, page); |
a5516438 AK |
1116 | h->free_huge_pages--; |
1117 | h->free_huge_pages_node[page_to_nid(page)]--; | |
1da177e4 LT |
1118 | } |
1119 | } | |
1120 | } | |
1121 | #else | |
a5516438 | 1122 | static inline void try_to_free_low(struct hstate *h, unsigned long count) |
1da177e4 LT |
1123 | { |
1124 | } | |
1125 | #endif | |
1126 | ||
a5516438 | 1127 | #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) |
e5ff2159 | 1128 | static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count) |
1da177e4 | 1129 | { |
7893d1d5 | 1130 | unsigned long min_count, ret; |
1da177e4 | 1131 | |
aa888a74 AK |
1132 | if (h->order >= MAX_ORDER) |
1133 | return h->max_huge_pages; | |
1134 | ||
7893d1d5 AL |
1135 | /* |
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. | |
d1c3fb1f NA |
1139 | * |
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. | |
7893d1d5 | 1145 | */ |
1da177e4 | 1146 | spin_lock(&hugetlb_lock); |
a5516438 AK |
1147 | while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { |
1148 | if (!adjust_pool_surplus(h, -1)) | |
7893d1d5 AL |
1149 | break; |
1150 | } | |
1151 | ||
a5516438 | 1152 | while (count > persistent_huge_pages(h)) { |
7893d1d5 AL |
1153 | /* |
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); | |
a5516438 | 1159 | ret = alloc_fresh_huge_page(h); |
7893d1d5 AL |
1160 | spin_lock(&hugetlb_lock); |
1161 | if (!ret) | |
1162 | goto out; | |
1163 | ||
1164 | } | |
7893d1d5 AL |
1165 | |
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. | |
d1c3fb1f NA |
1172 | * |
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. | |
7893d1d5 | 1180 | */ |
a5516438 | 1181 | min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; |
6b0c880d | 1182 | min_count = max(count, min_count); |
a5516438 AK |
1183 | try_to_free_low(h, min_count); |
1184 | while (min_count < persistent_huge_pages(h)) { | |
1185 | struct page *page = dequeue_huge_page(h); | |
1da177e4 LT |
1186 | if (!page) |
1187 | break; | |
a5516438 | 1188 | update_and_free_page(h, page); |
1da177e4 | 1189 | } |
a5516438 AK |
1190 | while (count < persistent_huge_pages(h)) { |
1191 | if (!adjust_pool_surplus(h, 1)) | |
7893d1d5 AL |
1192 | break; |
1193 | } | |
1194 | out: | |
a5516438 | 1195 | ret = persistent_huge_pages(h); |
1da177e4 | 1196 | spin_unlock(&hugetlb_lock); |
7893d1d5 | 1197 | return ret; |
1da177e4 LT |
1198 | } |
1199 | ||
a3437870 NA |
1200 | #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 | ||
1207 | static struct kobject *hugepages_kobj; | |
1208 | static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; | |
1209 | ||
1210 | static 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 | ||
1220 | static 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 | } | |
1226 | static 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 | } | |
1241 | HSTATE_ATTR(nr_hugepages); | |
1242 | ||
1243 | static 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 | } | |
1249 | static 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 | } | |
1266 | HSTATE_ATTR(nr_overcommit_hugepages); | |
1267 | ||
1268 | static 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 | } | |
1274 | HSTATE_ATTR_RO(free_hugepages); | |
1275 | ||
1276 | static 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 | } | |
1282 | HSTATE_ATTR_RO(resv_hugepages); | |
1283 | ||
1284 | static 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 | } | |
1290 | HSTATE_ATTR_RO(surplus_hugepages); | |
1291 | ||
1292 | static 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 | ||
1301 | static struct attribute_group hstate_attr_group = { | |
1302 | .attrs = hstate_attrs, | |
1303 | }; | |
1304 | ||
1305 | static 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 | ||
1322 | static 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 | ||
1339 | static 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 | } | |
1349 | module_exit(hugetlb_exit); | |
1350 | ||
1351 | static int __init hugetlb_init(void) | |
1352 | { | |
0ef89d25 BH |
1353 | /* 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; | |
a3437870 | 1359 | |
e11bfbfc NP |
1360 | 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); | |
a3437870 | 1364 | } |
e11bfbfc NP |
1365 | 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; | |
a3437870 NA |
1368 | |
1369 | hugetlb_init_hstates(); | |
1370 | ||
aa888a74 AK |
1371 | gather_bootmem_prealloc(); |
1372 | ||
a3437870 NA |
1373 | report_hugepages(); |
1374 | ||
1375 | hugetlb_sysfs_init(); | |
1376 | ||
1377 | return 0; | |
1378 | } | |
1379 | module_init(hugetlb_init); | |
1380 | ||
1381 | /* Should be called on processing a hugepagesz=... option */ | |
1382 | void __init hugetlb_add_hstate(unsigned order) | |
1383 | { | |
1384 | struct hstate *h; | |
8faa8b07 AK |
1385 | unsigned long i; |
1386 | ||
a3437870 NA |
1387 | 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); | |
8faa8b07 AK |
1396 | 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); | |
a3437870 NA |
1401 | snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", |
1402 | huge_page_size(h)/1024); | |
8faa8b07 | 1403 | |
a3437870 NA |
1404 | parsed_hstate = h; |
1405 | } | |
1406 | ||
e11bfbfc | 1407 | static int __init hugetlb_nrpages_setup(char *s) |
a3437870 NA |
1408 | { |
1409 | unsigned long *mhp; | |
8faa8b07 | 1410 | static unsigned long *last_mhp; |
a3437870 NA |
1411 | |
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 | ||
8faa8b07 AK |
1421 | if (mhp == last_mhp) { |
1422 | printk(KERN_WARNING "hugepages= specified twice without " | |
1423 | "interleaving hugepagesz=, ignoring\n"); | |
1424 | return 1; | |
1425 | } | |
1426 | ||
a3437870 NA |
1427 | if (sscanf(s, "%lu", mhp) <= 0) |
1428 | *mhp = 0; | |
1429 | ||
8faa8b07 AK |
1430 | /* |
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 | ||
a3437870 NA |
1440 | return 1; |
1441 | } | |
e11bfbfc NP |
1442 | __setup("hugepages=", hugetlb_nrpages_setup); |
1443 | ||
1444 | static 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); | |
a3437870 | 1450 | |
8a213460 NA |
1451 | static 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 | |
1da177e4 LT |
1463 | int hugetlb_sysctl_handler(struct ctl_table *table, int write, |
1464 | struct file *file, void __user *buffer, | |
1465 | size_t *length, loff_t *ppos) | |
1466 | { | |
e5ff2159 AK |
1467 | 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); | |
1da177e4 | 1475 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); |
e5ff2159 AK |
1476 | |
1477 | if (write) | |
1478 | h->max_huge_pages = set_max_huge_pages(h, tmp); | |
1479 | ||
1da177e4 LT |
1480 | return 0; |
1481 | } | |
396faf03 MG |
1482 | |
1483 | int 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 | ||
a3d0c6aa NA |
1495 | int hugetlb_overcommit_handler(struct ctl_table *table, int write, |
1496 | struct file *file, void __user *buffer, | |
1497 | size_t *length, loff_t *ppos) | |
1498 | { | |
a5516438 | 1499 | struct hstate *h = &default_hstate; |
e5ff2159 AK |
1500 | 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); | |
a3d0c6aa | 1507 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); |
e5ff2159 AK |
1508 | |
1509 | if (write) { | |
1510 | spin_lock(&hugetlb_lock); | |
1511 | h->nr_overcommit_huge_pages = tmp; | |
1512 | spin_unlock(&hugetlb_lock); | |
1513 | } | |
1514 | ||
a3d0c6aa NA |
1515 | return 0; |
1516 | } | |
1517 | ||
1da177e4 LT |
1518 | #endif /* CONFIG_SYSCTL */ |
1519 | ||
e1759c21 | 1520 | void hugetlb_report_meminfo(struct seq_file *m) |
1da177e4 | 1521 | { |
a5516438 | 1522 | struct hstate *h = &default_hstate; |
e1759c21 | 1523 | seq_printf(m, |
4f98a2fe RR |
1524 | "HugePages_Total: %5lu\n" |
1525 | "HugePages_Free: %5lu\n" | |
1526 | "HugePages_Rsvd: %5lu\n" | |
1527 | "HugePages_Surp: %5lu\n" | |
1528 | "Hugepagesize: %8lu kB\n", | |
a5516438 AK |
1529 | 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)); | |
1da177e4 LT |
1534 | } |
1535 | ||
1536 | int hugetlb_report_node_meminfo(int nid, char *buf) | |
1537 | { | |
a5516438 | 1538 | struct hstate *h = &default_hstate; |
1da177e4 LT |
1539 | return sprintf(buf, |
1540 | "Node %d HugePages_Total: %5u\n" | |
a1de0919 NA |
1541 | "Node %d HugePages_Free: %5u\n" |
1542 | "Node %d HugePages_Surp: %5u\n", | |
a5516438 AK |
1543 | nid, h->nr_huge_pages_node[nid], |
1544 | nid, h->free_huge_pages_node[nid], | |
1545 | nid, h->surplus_huge_pages_node[nid]); | |
1da177e4 LT |
1546 | } |
1547 | ||
1da177e4 LT |
1548 | /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ |
1549 | unsigned long hugetlb_total_pages(void) | |
1550 | { | |
a5516438 AK |
1551 | struct hstate *h = &default_hstate; |
1552 | return h->nr_huge_pages * pages_per_huge_page(h); | |
1da177e4 | 1553 | } |
1da177e4 | 1554 | |
a5516438 | 1555 | static int hugetlb_acct_memory(struct hstate *h, long delta) |
fc1b8a73 MG |
1556 | { |
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) { | |
a5516438 | 1578 | if (gather_surplus_pages(h, delta) < 0) |
fc1b8a73 MG |
1579 | goto out; |
1580 | ||
a5516438 AK |
1581 | if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { |
1582 | return_unused_surplus_pages(h, delta); | |
fc1b8a73 MG |
1583 | goto out; |
1584 | } | |
1585 | } | |
1586 | ||
1587 | ret = 0; | |
1588 | if (delta < 0) | |
a5516438 | 1589 | return_unused_surplus_pages(h, (unsigned long) -delta); |
fc1b8a73 MG |
1590 | |
1591 | out: | |
1592 | spin_unlock(&hugetlb_lock); | |
1593 | return ret; | |
1594 | } | |
1595 | ||
84afd99b AW |
1596 | static 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 | ||
a1e78772 MG |
1612 | static void hugetlb_vm_op_close(struct vm_area_struct *vma) |
1613 | { | |
a5516438 | 1614 | struct hstate *h = hstate_vma(vma); |
84afd99b AW |
1615 | struct resv_map *reservations = vma_resv_map(vma); |
1616 | unsigned long reserve; | |
1617 | unsigned long start; | |
1618 | unsigned long end; | |
1619 | ||
1620 | if (reservations) { | |
a5516438 AK |
1621 | start = vma_hugecache_offset(h, vma, vma->vm_start); |
1622 | end = vma_hugecache_offset(h, vma, vma->vm_end); | |
84afd99b AW |
1623 | |
1624 | reserve = (end - start) - | |
1625 | region_count(&reservations->regions, start, end); | |
1626 | ||
1627 | kref_put(&reservations->refs, resv_map_release); | |
1628 | ||
7251ff78 | 1629 | if (reserve) { |
a5516438 | 1630 | hugetlb_acct_memory(h, -reserve); |
7251ff78 AL |
1631 | hugetlb_put_quota(vma->vm_file->f_mapping, reserve); |
1632 | } | |
84afd99b | 1633 | } |
a1e78772 MG |
1634 | } |
1635 | ||
1da177e4 LT |
1636 | /* |
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 | */ | |
d0217ac0 | 1642 | static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 LT |
1643 | { |
1644 | BUG(); | |
d0217ac0 | 1645 | return 0; |
1da177e4 LT |
1646 | } |
1647 | ||
1648 | struct vm_operations_struct hugetlb_vm_ops = { | |
d0217ac0 | 1649 | .fault = hugetlb_vm_op_fault, |
84afd99b | 1650 | .open = hugetlb_vm_op_open, |
a1e78772 | 1651 | .close = hugetlb_vm_op_close, |
1da177e4 LT |
1652 | }; |
1653 | ||
1e8f889b DG |
1654 | static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, |
1655 | int writable) | |
63551ae0 DG |
1656 | { |
1657 | pte_t entry; | |
1658 | ||
1e8f889b | 1659 | if (writable) { |
63551ae0 DG |
1660 | entry = |
1661 | pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); | |
1662 | } else { | |
7f2e9525 | 1663 | entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot)); |
63551ae0 DG |
1664 | } |
1665 | entry = pte_mkyoung(entry); | |
1666 | entry = pte_mkhuge(entry); | |
1667 | ||
1668 | return entry; | |
1669 | } | |
1670 | ||
1e8f889b DG |
1671 | static void set_huge_ptep_writable(struct vm_area_struct *vma, |
1672 | unsigned long address, pte_t *ptep) | |
1673 | { | |
1674 | pte_t entry; | |
1675 | ||
7f2e9525 GS |
1676 | entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep))); |
1677 | if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) { | |
8dab5241 | 1678 | update_mmu_cache(vma, address, entry); |
8dab5241 | 1679 | } |
1e8f889b DG |
1680 | } |
1681 | ||
1682 | ||
63551ae0 DG |
1683 | int 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; | |
1c59827d | 1688 | unsigned long addr; |
1e8f889b | 1689 | int cow; |
a5516438 AK |
1690 | struct hstate *h = hstate_vma(vma); |
1691 | unsigned long sz = huge_page_size(h); | |
1e8f889b DG |
1692 | |
1693 | cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; | |
63551ae0 | 1694 | |
a5516438 | 1695 | for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { |
c74df32c HD |
1696 | src_pte = huge_pte_offset(src, addr); |
1697 | if (!src_pte) | |
1698 | continue; | |
a5516438 | 1699 | dst_pte = huge_pte_alloc(dst, addr, sz); |
63551ae0 DG |
1700 | if (!dst_pte) |
1701 | goto nomem; | |
c5c99429 LW |
1702 | |
1703 | /* If the pagetables are shared don't copy or take references */ | |
1704 | if (dst_pte == src_pte) | |
1705 | continue; | |
1706 | ||
c74df32c | 1707 | spin_lock(&dst->page_table_lock); |
46478758 | 1708 | spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING); |
7f2e9525 | 1709 | if (!huge_pte_none(huge_ptep_get(src_pte))) { |
1e8f889b | 1710 | if (cow) |
7f2e9525 GS |
1711 | huge_ptep_set_wrprotect(src, addr, src_pte); |
1712 | entry = huge_ptep_get(src_pte); | |
1c59827d HD |
1713 | ptepage = pte_page(entry); |
1714 | get_page(ptepage); | |
1c59827d HD |
1715 | set_huge_pte_at(dst, addr, dst_pte, entry); |
1716 | } | |
1717 | spin_unlock(&src->page_table_lock); | |
c74df32c | 1718 | spin_unlock(&dst->page_table_lock); |
63551ae0 DG |
1719 | } |
1720 | return 0; | |
1721 | ||
1722 | nomem: | |
1723 | return -ENOMEM; | |
1724 | } | |
1725 | ||
502717f4 | 1726 | void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
04f2cbe3 | 1727 | unsigned long end, struct page *ref_page) |
63551ae0 DG |
1728 | { |
1729 | struct mm_struct *mm = vma->vm_mm; | |
1730 | unsigned long address; | |
c7546f8f | 1731 | pte_t *ptep; |
63551ae0 DG |
1732 | pte_t pte; |
1733 | struct page *page; | |
fe1668ae | 1734 | struct page *tmp; |
a5516438 AK |
1735 | struct hstate *h = hstate_vma(vma); |
1736 | unsigned long sz = huge_page_size(h); | |
1737 | ||
c0a499c2 CK |
1738 | /* |
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 | */ | |
fe1668ae | 1743 | LIST_HEAD(page_list); |
63551ae0 DG |
1744 | |
1745 | WARN_ON(!is_vm_hugetlb_page(vma)); | |
a5516438 AK |
1746 | BUG_ON(start & ~huge_page_mask(h)); |
1747 | BUG_ON(end & ~huge_page_mask(h)); | |
63551ae0 | 1748 | |
cddb8a5c | 1749 | mmu_notifier_invalidate_range_start(mm, start, end); |
508034a3 | 1750 | spin_lock(&mm->page_table_lock); |
a5516438 | 1751 | for (address = start; address < end; address += sz) { |
c7546f8f | 1752 | ptep = huge_pte_offset(mm, address); |
4c887265 | 1753 | if (!ptep) |
c7546f8f DG |
1754 | continue; |
1755 | ||
39dde65c CK |
1756 | if (huge_pmd_unshare(mm, &address, ptep)) |
1757 | continue; | |
1758 | ||
04f2cbe3 MG |
1759 | /* |
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 | ||
c7546f8f | 1780 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
7f2e9525 | 1781 | if (huge_pte_none(pte)) |
63551ae0 | 1782 | continue; |
c7546f8f | 1783 | |
63551ae0 | 1784 | page = pte_page(pte); |
6649a386 KC |
1785 | if (pte_dirty(pte)) |
1786 | set_page_dirty(page); | |
fe1668ae | 1787 | list_add(&page->lru, &page_list); |
63551ae0 | 1788 | } |
1da177e4 | 1789 | spin_unlock(&mm->page_table_lock); |
508034a3 | 1790 | flush_tlb_range(vma, start, end); |
cddb8a5c | 1791 | mmu_notifier_invalidate_range_end(mm, start, end); |
fe1668ae CK |
1792 | list_for_each_entry_safe(page, tmp, &page_list, lru) { |
1793 | list_del(&page->lru); | |
1794 | put_page(page); | |
1795 | } | |
1da177e4 | 1796 | } |
63551ae0 | 1797 | |
502717f4 | 1798 | void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
04f2cbe3 | 1799 | unsigned long end, struct page *ref_page) |
502717f4 | 1800 | { |
a137e1cc AK |
1801 | 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); | |
502717f4 CK |
1804 | } |
1805 | ||
04f2cbe3 MG |
1806 | /* |
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 | */ | |
2a4b3ded HH |
1812 | static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, |
1813 | struct page *page, unsigned long address) | |
04f2cbe3 | 1814 | { |
7526674d | 1815 | struct hstate *h = hstate_vma(vma); |
04f2cbe3 MG |
1816 | 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 | */ | |
7526674d | 1825 | address = address & huge_page_mask(h); |
04f2cbe3 MG |
1826 | 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, | |
7526674d | 1844 | address, address + huge_page_size(h), |
04f2cbe3 MG |
1845 | page); |
1846 | } | |
1847 | ||
1848 | return 1; | |
1849 | } | |
1850 | ||
1e8f889b | 1851 | static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, |
04f2cbe3 MG |
1852 | unsigned long address, pte_t *ptep, pte_t pte, |
1853 | struct page *pagecache_page) | |
1e8f889b | 1854 | { |
a5516438 | 1855 | struct hstate *h = hstate_vma(vma); |
1e8f889b | 1856 | struct page *old_page, *new_page; |
79ac6ba4 | 1857 | int avoidcopy; |
04f2cbe3 | 1858 | int outside_reserve = 0; |
1e8f889b DG |
1859 | |
1860 | old_page = pte_page(pte); | |
1861 | ||
04f2cbe3 | 1862 | retry_avoidcopy: |
1e8f889b DG |
1863 | /* 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); | |
83c54070 | 1868 | return 0; |
1e8f889b DG |
1869 | } |
1870 | ||
04f2cbe3 MG |
1871 | /* |
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 | ||
1e8f889b | 1885 | page_cache_get(old_page); |
04f2cbe3 | 1886 | new_page = alloc_huge_page(vma, address, outside_reserve); |
1e8f889b | 1887 | |
2fc39cec | 1888 | if (IS_ERR(new_page)) { |
1e8f889b | 1889 | page_cache_release(old_page); |
04f2cbe3 MG |
1890 | |
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 | ||
2fc39cec | 1908 | return -PTR_ERR(new_page); |
1e8f889b DG |
1909 | } |
1910 | ||
1911 | spin_unlock(&mm->page_table_lock); | |
9de455b2 | 1912 | copy_huge_page(new_page, old_page, address, vma); |
0ed361de | 1913 | __SetPageUptodate(new_page); |
1e8f889b DG |
1914 | spin_lock(&mm->page_table_lock); |
1915 | ||
a5516438 | 1916 | ptep = huge_pte_offset(mm, address & huge_page_mask(h)); |
7f2e9525 | 1917 | if (likely(pte_same(huge_ptep_get(ptep), pte))) { |
1e8f889b | 1918 | /* Break COW */ |
8fe627ec | 1919 | huge_ptep_clear_flush(vma, address, ptep); |
1e8f889b DG |
1920 | 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); | |
83c54070 | 1927 | return 0; |
1e8f889b DG |
1928 | } |
1929 | ||
04f2cbe3 | 1930 | /* Return the pagecache page at a given address within a VMA */ |
a5516438 AK |
1931 | static struct page *hugetlbfs_pagecache_page(struct hstate *h, |
1932 | struct vm_area_struct *vma, unsigned long address) | |
04f2cbe3 MG |
1933 | { |
1934 | struct address_space *mapping; | |
e7c4b0bf | 1935 | pgoff_t idx; |
04f2cbe3 MG |
1936 | |
1937 | mapping = vma->vm_file->f_mapping; | |
a5516438 | 1938 | idx = vma_hugecache_offset(h, vma, address); |
04f2cbe3 MG |
1939 | |
1940 | return find_lock_page(mapping, idx); | |
1941 | } | |
1942 | ||
a1ed3dda | 1943 | static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, |
1e8f889b | 1944 | unsigned long address, pte_t *ptep, int write_access) |
ac9b9c66 | 1945 | { |
a5516438 | 1946 | struct hstate *h = hstate_vma(vma); |
ac9b9c66 | 1947 | int ret = VM_FAULT_SIGBUS; |
e7c4b0bf | 1948 | pgoff_t idx; |
4c887265 | 1949 | unsigned long size; |
4c887265 AL |
1950 | struct page *page; |
1951 | struct address_space *mapping; | |
1e8f889b | 1952 | pte_t new_pte; |
4c887265 | 1953 | |
04f2cbe3 MG |
1954 | /* |
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 | ||
4c887265 | 1966 | mapping = vma->vm_file->f_mapping; |
a5516438 | 1967 | idx = vma_hugecache_offset(h, vma, address); |
4c887265 AL |
1968 | |
1969 | /* | |
1970 | * Use page lock to guard against racing truncation | |
1971 | * before we get page_table_lock. | |
1972 | */ | |
6bda666a CL |
1973 | retry: |
1974 | page = find_lock_page(mapping, idx); | |
1975 | if (!page) { | |
a5516438 | 1976 | size = i_size_read(mapping->host) >> huge_page_shift(h); |
ebed4bfc HD |
1977 | if (idx >= size) |
1978 | goto out; | |
04f2cbe3 | 1979 | page = alloc_huge_page(vma, address, 0); |
2fc39cec AL |
1980 | if (IS_ERR(page)) { |
1981 | ret = -PTR_ERR(page); | |
6bda666a CL |
1982 | goto out; |
1983 | } | |
a5516438 | 1984 | clear_huge_page(page, address, huge_page_size(h)); |
0ed361de | 1985 | __SetPageUptodate(page); |
ac9b9c66 | 1986 | |
6bda666a CL |
1987 | if (vma->vm_flags & VM_SHARED) { |
1988 | int err; | |
45c682a6 | 1989 | struct inode *inode = mapping->host; |
6bda666a CL |
1990 | |
1991 | err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); | |
1992 | if (err) { | |
1993 | put_page(page); | |
6bda666a CL |
1994 | if (err == -EEXIST) |
1995 | goto retry; | |
1996 | goto out; | |
1997 | } | |
45c682a6 KC |
1998 | |
1999 | spin_lock(&inode->i_lock); | |
a5516438 | 2000 | inode->i_blocks += blocks_per_huge_page(h); |
45c682a6 | 2001 | spin_unlock(&inode->i_lock); |
6bda666a CL |
2002 | } else |
2003 | lock_page(page); | |
2004 | } | |
1e8f889b | 2005 | |
57303d80 AW |
2006 | /* |
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)) | |
2b26736c AW |
2013 | if (vma_needs_reservation(h, vma, address) < 0) { |
2014 | ret = VM_FAULT_OOM; | |
2015 | goto backout_unlocked; | |
2016 | } | |
57303d80 | 2017 | |
ac9b9c66 | 2018 | spin_lock(&mm->page_table_lock); |
a5516438 | 2019 | size = i_size_read(mapping->host) >> huge_page_shift(h); |
4c887265 AL |
2020 | if (idx >= size) |
2021 | goto backout; | |
2022 | ||
83c54070 | 2023 | ret = 0; |
7f2e9525 | 2024 | if (!huge_pte_none(huge_ptep_get(ptep))) |
4c887265 AL |
2025 | goto backout; |
2026 | ||
1e8f889b DG |
2027 | 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 */ | |
04f2cbe3 | 2033 | ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page); |
1e8f889b DG |
2034 | } |
2035 | ||
ac9b9c66 | 2036 | spin_unlock(&mm->page_table_lock); |
4c887265 AL |
2037 | unlock_page(page); |
2038 | out: | |
ac9b9c66 | 2039 | return ret; |
4c887265 AL |
2040 | |
2041 | backout: | |
2042 | spin_unlock(&mm->page_table_lock); | |
2b26736c | 2043 | backout_unlocked: |
4c887265 AL |
2044 | unlock_page(page); |
2045 | put_page(page); | |
2046 | goto out; | |
ac9b9c66 HD |
2047 | } |
2048 | ||
86e5216f AL |
2049 | int 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; | |
1e8f889b | 2054 | int ret; |
57303d80 | 2055 | struct page *pagecache_page = NULL; |
3935baa9 | 2056 | static DEFINE_MUTEX(hugetlb_instantiation_mutex); |
a5516438 | 2057 | struct hstate *h = hstate_vma(vma); |
86e5216f | 2058 | |
a5516438 | 2059 | ptep = huge_pte_alloc(mm, address, huge_page_size(h)); |
86e5216f AL |
2060 | if (!ptep) |
2061 | return VM_FAULT_OOM; | |
2062 | ||
3935baa9 DG |
2063 | /* |
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); | |
7f2e9525 GS |
2069 | entry = huge_ptep_get(ptep); |
2070 | if (huge_pte_none(entry)) { | |
3935baa9 | 2071 | ret = hugetlb_no_page(mm, vma, address, ptep, write_access); |
b4d1d99f | 2072 | goto out_mutex; |
3935baa9 | 2073 | } |
86e5216f | 2074 | |
83c54070 | 2075 | ret = 0; |
1e8f889b | 2076 | |
57303d80 AW |
2077 | /* |
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)) { | |
2b26736c AW |
2086 | if (vma_needs_reservation(h, vma, address) < 0) { |
2087 | ret = VM_FAULT_OOM; | |
b4d1d99f | 2088 | goto out_mutex; |
2b26736c | 2089 | } |
57303d80 AW |
2090 | |
2091 | if (!(vma->vm_flags & VM_SHARED)) | |
2092 | pagecache_page = hugetlbfs_pagecache_page(h, | |
2093 | vma, address); | |
2094 | } | |
2095 | ||
1e8f889b DG |
2096 | spin_lock(&mm->page_table_lock); |
2097 | /* Check for a racing update before calling hugetlb_cow */ | |
b4d1d99f DG |
2098 | 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)) { | |
57303d80 AW |
2104 | ret = hugetlb_cow(mm, vma, address, ptep, entry, |
2105 | pagecache_page); | |
b4d1d99f DG |
2106 | 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 | ||
2114 | out_page_table_lock: | |
1e8f889b | 2115 | spin_unlock(&mm->page_table_lock); |
57303d80 AW |
2116 | |
2117 | if (pagecache_page) { | |
2118 | unlock_page(pagecache_page); | |
2119 | put_page(pagecache_page); | |
2120 | } | |
2121 | ||
b4d1d99f | 2122 | out_mutex: |
3935baa9 | 2123 | mutex_unlock(&hugetlb_instantiation_mutex); |
1e8f889b DG |
2124 | |
2125 | return ret; | |
86e5216f AL |
2126 | } |
2127 | ||
ceb86879 AK |
2128 | /* Can be overriden by architectures */ |
2129 | __attribute__((weak)) struct page * | |
2130 | follow_huge_pud(struct mm_struct *mm, unsigned long address, | |
2131 | pud_t *pud, int write) | |
2132 | { | |
2133 | BUG(); | |
2134 | return NULL; | |
2135 | } | |
2136 | ||
4b2e38ad KM |
2137 | static 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 | ||
63551ae0 DG |
2145 | int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, |
2146 | struct page **pages, struct vm_area_struct **vmas, | |
5b23dbe8 AL |
2147 | unsigned long *position, int *length, int i, |
2148 | int write) | |
63551ae0 | 2149 | { |
d5d4b0aa CK |
2150 | unsigned long pfn_offset; |
2151 | unsigned long vaddr = *position; | |
63551ae0 | 2152 | int remainder = *length; |
a5516438 | 2153 | struct hstate *h = hstate_vma(vma); |
4b2e38ad KM |
2154 | int zeropage_ok = 0; |
2155 | int shared = vma->vm_flags & VM_SHARED; | |
63551ae0 | 2156 | |
1c59827d | 2157 | spin_lock(&mm->page_table_lock); |
63551ae0 | 2158 | while (vaddr < vma->vm_end && remainder) { |
4c887265 AL |
2159 | pte_t *pte; |
2160 | struct page *page; | |
63551ae0 | 2161 | |
4c887265 AL |
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 | */ | |
a5516438 | 2167 | pte = huge_pte_offset(mm, vaddr & huge_page_mask(h)); |
4b2e38ad KM |
2168 | if (huge_zeropage_ok(pte, write, shared)) |
2169 | zeropage_ok = 1; | |
63551ae0 | 2170 | |
4b2e38ad KM |
2171 | if (!pte || |
2172 | (huge_pte_none(huge_ptep_get(pte)) && !zeropage_ok) || | |
7f2e9525 | 2173 | (write && !pte_write(huge_ptep_get(pte)))) { |
4c887265 | 2174 | int ret; |
63551ae0 | 2175 | |
4c887265 | 2176 | spin_unlock(&mm->page_table_lock); |
5b23dbe8 | 2177 | ret = hugetlb_fault(mm, vma, vaddr, write); |
4c887265 | 2178 | spin_lock(&mm->page_table_lock); |
a89182c7 | 2179 | if (!(ret & VM_FAULT_ERROR)) |
4c887265 | 2180 | continue; |
63551ae0 | 2181 | |
4c887265 AL |
2182 | remainder = 0; |
2183 | if (!i) | |
2184 | i = -EFAULT; | |
2185 | break; | |
2186 | } | |
2187 | ||
a5516438 | 2188 | pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; |
7f2e9525 | 2189 | page = pte_page(huge_ptep_get(pte)); |
d5d4b0aa | 2190 | same_page: |
d6692183 | 2191 | if (pages) { |
4b2e38ad KM |
2192 | if (zeropage_ok) |
2193 | pages[i] = ZERO_PAGE(0); | |
2194 | else | |
69d177c2 | 2195 | pages[i] = mem_map_offset(page, pfn_offset); |
4b2e38ad | 2196 | get_page(pages[i]); |
d6692183 | 2197 | } |
63551ae0 DG |
2198 | |
2199 | if (vmas) | |
2200 | vmas[i] = vma; | |
2201 | ||
2202 | vaddr += PAGE_SIZE; | |
d5d4b0aa | 2203 | ++pfn_offset; |
63551ae0 DG |
2204 | --remainder; |
2205 | ++i; | |
d5d4b0aa | 2206 | if (vaddr < vma->vm_end && remainder && |
a5516438 | 2207 | pfn_offset < pages_per_huge_page(h)) { |
d5d4b0aa CK |
2208 | /* |
2209 | * We use pfn_offset to avoid touching the pageframes | |
2210 | * of this compound page. | |
2211 | */ | |
2212 | goto same_page; | |
2213 | } | |
63551ae0 | 2214 | } |
1c59827d | 2215 | spin_unlock(&mm->page_table_lock); |
63551ae0 DG |
2216 | *length = remainder; |
2217 | *position = vaddr; | |
2218 | ||
2219 | return i; | |
2220 | } | |
8f860591 ZY |
2221 | |
2222 | void 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; | |
a5516438 | 2229 | struct hstate *h = hstate_vma(vma); |
8f860591 ZY |
2230 | |
2231 | BUG_ON(address >= end); | |
2232 | flush_cache_range(vma, address, end); | |
2233 | ||
39dde65c | 2234 | spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); |
8f860591 | 2235 | spin_lock(&mm->page_table_lock); |
a5516438 | 2236 | for (; address < end; address += huge_page_size(h)) { |
8f860591 ZY |
2237 | ptep = huge_pte_offset(mm, address); |
2238 | if (!ptep) | |
2239 | continue; | |
39dde65c CK |
2240 | if (huge_pmd_unshare(mm, &address, ptep)) |
2241 | continue; | |
7f2e9525 | 2242 | if (!huge_pte_none(huge_ptep_get(ptep))) { |
8f860591 ZY |
2243 | 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); | |
8f860591 ZY |
2246 | } |
2247 | } | |
2248 | spin_unlock(&mm->page_table_lock); | |
39dde65c | 2249 | spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); |
8f860591 ZY |
2250 | |
2251 | flush_tlb_range(vma, start, end); | |
2252 | } | |
2253 | ||
a1e78772 MG |
2254 | int hugetlb_reserve_pages(struct inode *inode, |
2255 | long from, long to, | |
2256 | struct vm_area_struct *vma) | |
e4e574b7 AL |
2257 | { |
2258 | long ret, chg; | |
a5516438 | 2259 | struct hstate *h = hstate_inode(inode); |
e4e574b7 | 2260 | |
c37f9fb1 AW |
2261 | if (vma && vma->vm_flags & VM_NORESERVE) |
2262 | return 0; | |
2263 | ||
a1e78772 MG |
2264 | /* |
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 { | |
84afd99b AW |
2273 | struct resv_map *resv_map = resv_map_alloc(); |
2274 | if (!resv_map) | |
2275 | return -ENOMEM; | |
2276 | ||
a1e78772 | 2277 | chg = to - from; |
84afd99b AW |
2278 | |
2279 | set_vma_resv_map(vma, resv_map); | |
04f2cbe3 | 2280 | set_vma_resv_flags(vma, HPAGE_RESV_OWNER); |
a1e78772 MG |
2281 | } |
2282 | ||
e4e574b7 AL |
2283 | if (chg < 0) |
2284 | return chg; | |
8a630112 | 2285 | |
90d8b7e6 AL |
2286 | if (hugetlb_get_quota(inode->i_mapping, chg)) |
2287 | return -ENOSPC; | |
a5516438 | 2288 | ret = hugetlb_acct_memory(h, chg); |
68842c9b KC |
2289 | if (ret < 0) { |
2290 | hugetlb_put_quota(inode->i_mapping, chg); | |
a43a8c39 | 2291 | return ret; |
68842c9b | 2292 | } |
a1e78772 MG |
2293 | if (!vma || vma->vm_flags & VM_SHARED) |
2294 | region_add(&inode->i_mapping->private_list, from, to); | |
a43a8c39 CK |
2295 | return 0; |
2296 | } | |
2297 | ||
2298 | void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) | |
2299 | { | |
a5516438 | 2300 | struct hstate *h = hstate_inode(inode); |
a43a8c39 | 2301 | long chg = region_truncate(&inode->i_mapping->private_list, offset); |
45c682a6 KC |
2302 | |
2303 | spin_lock(&inode->i_lock); | |
a5516438 | 2304 | inode->i_blocks -= blocks_per_huge_page(h); |
45c682a6 KC |
2305 | spin_unlock(&inode->i_lock); |
2306 | ||
90d8b7e6 | 2307 | hugetlb_put_quota(inode->i_mapping, (chg - freed)); |
a5516438 | 2308 | hugetlb_acct_memory(h, -(chg - freed)); |
a43a8c39 | 2309 | } |