]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Generic hugetlb support. | |
6d49e352 | 3 | * (C) Nadia Yvette Chambers, April 2004 |
1da177e4 | 4 | */ |
1da177e4 LT |
5 | #include <linux/list.h> |
6 | #include <linux/init.h> | |
7 | #include <linux/module.h> | |
8 | #include <linux/mm.h> | |
e1759c21 | 9 | #include <linux/seq_file.h> |
1da177e4 LT |
10 | #include <linux/sysctl.h> |
11 | #include <linux/highmem.h> | |
cddb8a5c | 12 | #include <linux/mmu_notifier.h> |
1da177e4 | 13 | #include <linux/nodemask.h> |
63551ae0 | 14 | #include <linux/pagemap.h> |
5da7ca86 | 15 | #include <linux/mempolicy.h> |
3b32123d | 16 | #include <linux/compiler.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> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
0fe6e20b | 22 | #include <linux/rmap.h> |
fd6a03ed NH |
23 | #include <linux/swap.h> |
24 | #include <linux/swapops.h> | |
c8721bbb | 25 | #include <linux/page-isolation.h> |
8382d914 | 26 | #include <linux/jhash.h> |
d6606683 | 27 | |
63551ae0 DG |
28 | #include <asm/page.h> |
29 | #include <asm/pgtable.h> | |
24669e58 | 30 | #include <asm/tlb.h> |
63551ae0 | 31 | |
24669e58 | 32 | #include <linux/io.h> |
63551ae0 | 33 | #include <linux/hugetlb.h> |
9dd540e2 | 34 | #include <linux/hugetlb_cgroup.h> |
9a305230 | 35 | #include <linux/node.h> |
7835e98b | 36 | #include "internal.h" |
1da177e4 | 37 | |
753162cd | 38 | int hugepages_treat_as_movable; |
a5516438 | 39 | |
c3f38a38 | 40 | int hugetlb_max_hstate __read_mostly; |
e5ff2159 AK |
41 | unsigned int default_hstate_idx; |
42 | struct hstate hstates[HUGE_MAX_HSTATE]; | |
43 | ||
53ba51d2 JT |
44 | __initdata LIST_HEAD(huge_boot_pages); |
45 | ||
e5ff2159 AK |
46 | /* for command line parsing */ |
47 | static struct hstate * __initdata parsed_hstate; | |
48 | static unsigned long __initdata default_hstate_max_huge_pages; | |
e11bfbfc | 49 | static unsigned long __initdata default_hstate_size; |
e5ff2159 | 50 | |
3935baa9 | 51 | /* |
31caf665 NH |
52 | * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages, |
53 | * free_huge_pages, and surplus_huge_pages. | |
3935baa9 | 54 | */ |
c3f38a38 | 55 | DEFINE_SPINLOCK(hugetlb_lock); |
0bd0f9fb | 56 | |
8382d914 DB |
57 | /* |
58 | * Serializes faults on the same logical page. This is used to | |
59 | * prevent spurious OOMs when the hugepage pool is fully utilized. | |
60 | */ | |
61 | static int num_fault_mutexes; | |
62 | static struct mutex *htlb_fault_mutex_table ____cacheline_aligned_in_smp; | |
63 | ||
90481622 DG |
64 | static inline void unlock_or_release_subpool(struct hugepage_subpool *spool) |
65 | { | |
66 | bool free = (spool->count == 0) && (spool->used_hpages == 0); | |
67 | ||
68 | spin_unlock(&spool->lock); | |
69 | ||
70 | /* If no pages are used, and no other handles to the subpool | |
71 | * remain, free the subpool the subpool remain */ | |
72 | if (free) | |
73 | kfree(spool); | |
74 | } | |
75 | ||
76 | struct hugepage_subpool *hugepage_new_subpool(long nr_blocks) | |
77 | { | |
78 | struct hugepage_subpool *spool; | |
79 | ||
c6a91820 | 80 | spool = kzalloc(sizeof(*spool), GFP_KERNEL); |
90481622 DG |
81 | if (!spool) |
82 | return NULL; | |
83 | ||
84 | spin_lock_init(&spool->lock); | |
85 | spool->count = 1; | |
86 | spool->max_hpages = nr_blocks; | |
90481622 DG |
87 | |
88 | return spool; | |
89 | } | |
90 | ||
91 | void hugepage_put_subpool(struct hugepage_subpool *spool) | |
92 | { | |
93 | spin_lock(&spool->lock); | |
94 | BUG_ON(!spool->count); | |
95 | spool->count--; | |
96 | unlock_or_release_subpool(spool); | |
97 | } | |
98 | ||
1c5ecae3 MK |
99 | /* |
100 | * Subpool accounting for allocating and reserving pages. | |
101 | * Return -ENOMEM if there are not enough resources to satisfy the | |
102 | * the request. Otherwise, return the number of pages by which the | |
103 | * global pools must be adjusted (upward). The returned value may | |
104 | * only be different than the passed value (delta) in the case where | |
105 | * a subpool minimum size must be manitained. | |
106 | */ | |
107 | static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, | |
90481622 DG |
108 | long delta) |
109 | { | |
1c5ecae3 | 110 | long ret = delta; |
90481622 DG |
111 | |
112 | if (!spool) | |
1c5ecae3 | 113 | return ret; |
90481622 DG |
114 | |
115 | spin_lock(&spool->lock); | |
1c5ecae3 MK |
116 | |
117 | if (spool->max_hpages != -1) { /* maximum size accounting */ | |
118 | if ((spool->used_hpages + delta) <= spool->max_hpages) | |
119 | spool->used_hpages += delta; | |
120 | else { | |
121 | ret = -ENOMEM; | |
122 | goto unlock_ret; | |
123 | } | |
90481622 | 124 | } |
90481622 | 125 | |
1c5ecae3 MK |
126 | if (spool->min_hpages != -1) { /* minimum size accounting */ |
127 | if (delta > spool->rsv_hpages) { | |
128 | /* | |
129 | * Asking for more reserves than those already taken on | |
130 | * behalf of subpool. Return difference. | |
131 | */ | |
132 | ret = delta - spool->rsv_hpages; | |
133 | spool->rsv_hpages = 0; | |
134 | } else { | |
135 | ret = 0; /* reserves already accounted for */ | |
136 | spool->rsv_hpages -= delta; | |
137 | } | |
138 | } | |
139 | ||
140 | unlock_ret: | |
141 | spin_unlock(&spool->lock); | |
90481622 DG |
142 | return ret; |
143 | } | |
144 | ||
1c5ecae3 MK |
145 | /* |
146 | * Subpool accounting for freeing and unreserving pages. | |
147 | * Return the number of global page reservations that must be dropped. | |
148 | * The return value may only be different than the passed value (delta) | |
149 | * in the case where a subpool minimum size must be maintained. | |
150 | */ | |
151 | static long hugepage_subpool_put_pages(struct hugepage_subpool *spool, | |
90481622 DG |
152 | long delta) |
153 | { | |
1c5ecae3 MK |
154 | long ret = delta; |
155 | ||
90481622 | 156 | if (!spool) |
1c5ecae3 | 157 | return delta; |
90481622 DG |
158 | |
159 | spin_lock(&spool->lock); | |
1c5ecae3 MK |
160 | |
161 | if (spool->max_hpages != -1) /* maximum size accounting */ | |
162 | spool->used_hpages -= delta; | |
163 | ||
164 | if (spool->min_hpages != -1) { /* minimum size accounting */ | |
165 | if (spool->rsv_hpages + delta <= spool->min_hpages) | |
166 | ret = 0; | |
167 | else | |
168 | ret = spool->rsv_hpages + delta - spool->min_hpages; | |
169 | ||
170 | spool->rsv_hpages += delta; | |
171 | if (spool->rsv_hpages > spool->min_hpages) | |
172 | spool->rsv_hpages = spool->min_hpages; | |
173 | } | |
174 | ||
175 | /* | |
176 | * If hugetlbfs_put_super couldn't free spool due to an outstanding | |
177 | * quota reference, free it now. | |
178 | */ | |
90481622 | 179 | unlock_or_release_subpool(spool); |
1c5ecae3 MK |
180 | |
181 | return ret; | |
90481622 DG |
182 | } |
183 | ||
184 | static inline struct hugepage_subpool *subpool_inode(struct inode *inode) | |
185 | { | |
186 | return HUGETLBFS_SB(inode->i_sb)->spool; | |
187 | } | |
188 | ||
189 | static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma) | |
190 | { | |
496ad9aa | 191 | return subpool_inode(file_inode(vma->vm_file)); |
90481622 DG |
192 | } |
193 | ||
96822904 AW |
194 | /* |
195 | * Region tracking -- allows tracking of reservations and instantiated pages | |
196 | * across the pages in a mapping. | |
84afd99b | 197 | * |
7b24d861 DB |
198 | * The region data structures are embedded into a resv_map and |
199 | * protected by a resv_map's lock | |
96822904 AW |
200 | */ |
201 | struct file_region { | |
202 | struct list_head link; | |
203 | long from; | |
204 | long to; | |
205 | }; | |
206 | ||
1406ec9b | 207 | static long region_add(struct resv_map *resv, long f, long t) |
96822904 | 208 | { |
1406ec9b | 209 | struct list_head *head = &resv->regions; |
96822904 AW |
210 | struct file_region *rg, *nrg, *trg; |
211 | ||
7b24d861 | 212 | spin_lock(&resv->lock); |
96822904 AW |
213 | /* Locate the region we are either in or before. */ |
214 | list_for_each_entry(rg, head, link) | |
215 | if (f <= rg->to) | |
216 | break; | |
217 | ||
218 | /* Round our left edge to the current segment if it encloses us. */ | |
219 | if (f > rg->from) | |
220 | f = rg->from; | |
221 | ||
222 | /* Check for and consume any regions we now overlap with. */ | |
223 | nrg = rg; | |
224 | list_for_each_entry_safe(rg, trg, rg->link.prev, link) { | |
225 | if (&rg->link == head) | |
226 | break; | |
227 | if (rg->from > t) | |
228 | break; | |
229 | ||
230 | /* If this area reaches higher then extend our area to | |
231 | * include it completely. If this is not the first area | |
232 | * which we intend to reuse, free it. */ | |
233 | if (rg->to > t) | |
234 | t = rg->to; | |
235 | if (rg != nrg) { | |
236 | list_del(&rg->link); | |
237 | kfree(rg); | |
238 | } | |
239 | } | |
240 | nrg->from = f; | |
241 | nrg->to = t; | |
7b24d861 | 242 | spin_unlock(&resv->lock); |
96822904 AW |
243 | return 0; |
244 | } | |
245 | ||
1406ec9b | 246 | static long region_chg(struct resv_map *resv, long f, long t) |
96822904 | 247 | { |
1406ec9b | 248 | struct list_head *head = &resv->regions; |
7b24d861 | 249 | struct file_region *rg, *nrg = NULL; |
96822904 AW |
250 | long chg = 0; |
251 | ||
7b24d861 DB |
252 | retry: |
253 | spin_lock(&resv->lock); | |
96822904 AW |
254 | /* Locate the region we are before or in. */ |
255 | list_for_each_entry(rg, head, link) | |
256 | if (f <= rg->to) | |
257 | break; | |
258 | ||
259 | /* If we are below the current region then a new region is required. | |
260 | * Subtle, allocate a new region at the position but make it zero | |
261 | * size such that we can guarantee to record the reservation. */ | |
262 | if (&rg->link == head || t < rg->from) { | |
7b24d861 DB |
263 | if (!nrg) { |
264 | spin_unlock(&resv->lock); | |
265 | nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); | |
266 | if (!nrg) | |
267 | return -ENOMEM; | |
268 | ||
269 | nrg->from = f; | |
270 | nrg->to = f; | |
271 | INIT_LIST_HEAD(&nrg->link); | |
272 | goto retry; | |
273 | } | |
96822904 | 274 | |
7b24d861 DB |
275 | list_add(&nrg->link, rg->link.prev); |
276 | chg = t - f; | |
277 | goto out_nrg; | |
96822904 AW |
278 | } |
279 | ||
280 | /* Round our left edge to the current segment if it encloses us. */ | |
281 | if (f > rg->from) | |
282 | f = rg->from; | |
283 | chg = t - f; | |
284 | ||
285 | /* Check for and consume any regions we now overlap with. */ | |
286 | list_for_each_entry(rg, rg->link.prev, link) { | |
287 | if (&rg->link == head) | |
288 | break; | |
289 | if (rg->from > t) | |
7b24d861 | 290 | goto out; |
96822904 | 291 | |
25985edc | 292 | /* We overlap with this area, if it extends further than |
96822904 AW |
293 | * us then we must extend ourselves. Account for its |
294 | * existing reservation. */ | |
295 | if (rg->to > t) { | |
296 | chg += rg->to - t; | |
297 | t = rg->to; | |
298 | } | |
299 | chg -= rg->to - rg->from; | |
300 | } | |
7b24d861 DB |
301 | |
302 | out: | |
303 | spin_unlock(&resv->lock); | |
304 | /* We already know we raced and no longer need the new region */ | |
305 | kfree(nrg); | |
306 | return chg; | |
307 | out_nrg: | |
308 | spin_unlock(&resv->lock); | |
96822904 AW |
309 | return chg; |
310 | } | |
311 | ||
1406ec9b | 312 | static long region_truncate(struct resv_map *resv, long end) |
96822904 | 313 | { |
1406ec9b | 314 | struct list_head *head = &resv->regions; |
96822904 AW |
315 | struct file_region *rg, *trg; |
316 | long chg = 0; | |
317 | ||
7b24d861 | 318 | spin_lock(&resv->lock); |
96822904 AW |
319 | /* Locate the region we are either in or before. */ |
320 | list_for_each_entry(rg, head, link) | |
321 | if (end <= rg->to) | |
322 | break; | |
323 | if (&rg->link == head) | |
7b24d861 | 324 | goto out; |
96822904 AW |
325 | |
326 | /* If we are in the middle of a region then adjust it. */ | |
327 | if (end > rg->from) { | |
328 | chg = rg->to - end; | |
329 | rg->to = end; | |
330 | rg = list_entry(rg->link.next, typeof(*rg), link); | |
331 | } | |
332 | ||
333 | /* Drop any remaining regions. */ | |
334 | list_for_each_entry_safe(rg, trg, rg->link.prev, link) { | |
335 | if (&rg->link == head) | |
336 | break; | |
337 | chg += rg->to - rg->from; | |
338 | list_del(&rg->link); | |
339 | kfree(rg); | |
340 | } | |
7b24d861 DB |
341 | |
342 | out: | |
343 | spin_unlock(&resv->lock); | |
96822904 AW |
344 | return chg; |
345 | } | |
346 | ||
1406ec9b | 347 | static long region_count(struct resv_map *resv, long f, long t) |
84afd99b | 348 | { |
1406ec9b | 349 | struct list_head *head = &resv->regions; |
84afd99b AW |
350 | struct file_region *rg; |
351 | long chg = 0; | |
352 | ||
7b24d861 | 353 | spin_lock(&resv->lock); |
84afd99b AW |
354 | /* Locate each segment we overlap with, and count that overlap. */ |
355 | list_for_each_entry(rg, head, link) { | |
f2135a4a WSH |
356 | long seg_from; |
357 | long seg_to; | |
84afd99b AW |
358 | |
359 | if (rg->to <= f) | |
360 | continue; | |
361 | if (rg->from >= t) | |
362 | break; | |
363 | ||
364 | seg_from = max(rg->from, f); | |
365 | seg_to = min(rg->to, t); | |
366 | ||
367 | chg += seg_to - seg_from; | |
368 | } | |
7b24d861 | 369 | spin_unlock(&resv->lock); |
84afd99b AW |
370 | |
371 | return chg; | |
372 | } | |
373 | ||
e7c4b0bf AW |
374 | /* |
375 | * Convert the address within this vma to the page offset within | |
376 | * the mapping, in pagecache page units; huge pages here. | |
377 | */ | |
a5516438 AK |
378 | static pgoff_t vma_hugecache_offset(struct hstate *h, |
379 | struct vm_area_struct *vma, unsigned long address) | |
e7c4b0bf | 380 | { |
a5516438 AK |
381 | return ((address - vma->vm_start) >> huge_page_shift(h)) + |
382 | (vma->vm_pgoff >> huge_page_order(h)); | |
e7c4b0bf AW |
383 | } |
384 | ||
0fe6e20b NH |
385 | pgoff_t linear_hugepage_index(struct vm_area_struct *vma, |
386 | unsigned long address) | |
387 | { | |
388 | return vma_hugecache_offset(hstate_vma(vma), vma, address); | |
389 | } | |
390 | ||
08fba699 MG |
391 | /* |
392 | * Return the size of the pages allocated when backing a VMA. In the majority | |
393 | * cases this will be same size as used by the page table entries. | |
394 | */ | |
395 | unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) | |
396 | { | |
397 | struct hstate *hstate; | |
398 | ||
399 | if (!is_vm_hugetlb_page(vma)) | |
400 | return PAGE_SIZE; | |
401 | ||
402 | hstate = hstate_vma(vma); | |
403 | ||
2415cf12 | 404 | return 1UL << huge_page_shift(hstate); |
08fba699 | 405 | } |
f340ca0f | 406 | EXPORT_SYMBOL_GPL(vma_kernel_pagesize); |
08fba699 | 407 | |
3340289d MG |
408 | /* |
409 | * Return the page size being used by the MMU to back a VMA. In the majority | |
410 | * of cases, the page size used by the kernel matches the MMU size. On | |
411 | * architectures where it differs, an architecture-specific version of this | |
412 | * function is required. | |
413 | */ | |
414 | #ifndef vma_mmu_pagesize | |
415 | unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) | |
416 | { | |
417 | return vma_kernel_pagesize(vma); | |
418 | } | |
419 | #endif | |
420 | ||
84afd99b AW |
421 | /* |
422 | * Flags for MAP_PRIVATE reservations. These are stored in the bottom | |
423 | * bits of the reservation map pointer, which are always clear due to | |
424 | * alignment. | |
425 | */ | |
426 | #define HPAGE_RESV_OWNER (1UL << 0) | |
427 | #define HPAGE_RESV_UNMAPPED (1UL << 1) | |
04f2cbe3 | 428 | #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) |
84afd99b | 429 | |
a1e78772 MG |
430 | /* |
431 | * These helpers are used to track how many pages are reserved for | |
432 | * faults in a MAP_PRIVATE mapping. Only the process that called mmap() | |
433 | * is guaranteed to have their future faults succeed. | |
434 | * | |
435 | * With the exception of reset_vma_resv_huge_pages() which is called at fork(), | |
436 | * the reserve counters are updated with the hugetlb_lock held. It is safe | |
437 | * to reset the VMA at fork() time as it is not in use yet and there is no | |
438 | * chance of the global counters getting corrupted as a result of the values. | |
84afd99b AW |
439 | * |
440 | * The private mapping reservation is represented in a subtly different | |
441 | * manner to a shared mapping. A shared mapping has a region map associated | |
442 | * with the underlying file, this region map represents the backing file | |
443 | * pages which have ever had a reservation assigned which this persists even | |
444 | * after the page is instantiated. A private mapping has a region map | |
445 | * associated with the original mmap which is attached to all VMAs which | |
446 | * reference it, this region map represents those offsets which have consumed | |
447 | * reservation ie. where pages have been instantiated. | |
a1e78772 | 448 | */ |
e7c4b0bf AW |
449 | static unsigned long get_vma_private_data(struct vm_area_struct *vma) |
450 | { | |
451 | return (unsigned long)vma->vm_private_data; | |
452 | } | |
453 | ||
454 | static void set_vma_private_data(struct vm_area_struct *vma, | |
455 | unsigned long value) | |
456 | { | |
457 | vma->vm_private_data = (void *)value; | |
458 | } | |
459 | ||
9119a41e | 460 | struct resv_map *resv_map_alloc(void) |
84afd99b AW |
461 | { |
462 | struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); | |
463 | if (!resv_map) | |
464 | return NULL; | |
465 | ||
466 | kref_init(&resv_map->refs); | |
7b24d861 | 467 | spin_lock_init(&resv_map->lock); |
84afd99b AW |
468 | INIT_LIST_HEAD(&resv_map->regions); |
469 | ||
470 | return resv_map; | |
471 | } | |
472 | ||
9119a41e | 473 | void resv_map_release(struct kref *ref) |
84afd99b AW |
474 | { |
475 | struct resv_map *resv_map = container_of(ref, struct resv_map, refs); | |
476 | ||
477 | /* Clear out any active regions before we release the map. */ | |
1406ec9b | 478 | region_truncate(resv_map, 0); |
84afd99b AW |
479 | kfree(resv_map); |
480 | } | |
481 | ||
4e35f483 JK |
482 | static inline struct resv_map *inode_resv_map(struct inode *inode) |
483 | { | |
484 | return inode->i_mapping->private_data; | |
485 | } | |
486 | ||
84afd99b | 487 | static struct resv_map *vma_resv_map(struct vm_area_struct *vma) |
a1e78772 | 488 | { |
81d1b09c | 489 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
4e35f483 JK |
490 | if (vma->vm_flags & VM_MAYSHARE) { |
491 | struct address_space *mapping = vma->vm_file->f_mapping; | |
492 | struct inode *inode = mapping->host; | |
493 | ||
494 | return inode_resv_map(inode); | |
495 | ||
496 | } else { | |
84afd99b AW |
497 | return (struct resv_map *)(get_vma_private_data(vma) & |
498 | ~HPAGE_RESV_MASK); | |
4e35f483 | 499 | } |
a1e78772 MG |
500 | } |
501 | ||
84afd99b | 502 | static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) |
a1e78772 | 503 | { |
81d1b09c SL |
504 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
505 | VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); | |
a1e78772 | 506 | |
84afd99b AW |
507 | set_vma_private_data(vma, (get_vma_private_data(vma) & |
508 | HPAGE_RESV_MASK) | (unsigned long)map); | |
04f2cbe3 MG |
509 | } |
510 | ||
511 | static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) | |
512 | { | |
81d1b09c SL |
513 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
514 | VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); | |
e7c4b0bf AW |
515 | |
516 | set_vma_private_data(vma, get_vma_private_data(vma) | flags); | |
04f2cbe3 MG |
517 | } |
518 | ||
519 | static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) | |
520 | { | |
81d1b09c | 521 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
e7c4b0bf AW |
522 | |
523 | return (get_vma_private_data(vma) & flag) != 0; | |
a1e78772 MG |
524 | } |
525 | ||
04f2cbe3 | 526 | /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ |
a1e78772 MG |
527 | void reset_vma_resv_huge_pages(struct vm_area_struct *vma) |
528 | { | |
81d1b09c | 529 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
f83a275d | 530 | if (!(vma->vm_flags & VM_MAYSHARE)) |
a1e78772 MG |
531 | vma->vm_private_data = (void *)0; |
532 | } | |
533 | ||
534 | /* Returns true if the VMA has associated reserve pages */ | |
af0ed73e | 535 | static int vma_has_reserves(struct vm_area_struct *vma, long chg) |
a1e78772 | 536 | { |
af0ed73e JK |
537 | if (vma->vm_flags & VM_NORESERVE) { |
538 | /* | |
539 | * This address is already reserved by other process(chg == 0), | |
540 | * so, we should decrement reserved count. Without decrementing, | |
541 | * reserve count remains after releasing inode, because this | |
542 | * allocated page will go into page cache and is regarded as | |
543 | * coming from reserved pool in releasing step. Currently, we | |
544 | * don't have any other solution to deal with this situation | |
545 | * properly, so add work-around here. | |
546 | */ | |
547 | if (vma->vm_flags & VM_MAYSHARE && chg == 0) | |
548 | return 1; | |
549 | else | |
550 | return 0; | |
551 | } | |
a63884e9 JK |
552 | |
553 | /* Shared mappings always use reserves */ | |
f83a275d | 554 | if (vma->vm_flags & VM_MAYSHARE) |
7f09ca51 | 555 | return 1; |
a63884e9 JK |
556 | |
557 | /* | |
558 | * Only the process that called mmap() has reserves for | |
559 | * private mappings. | |
560 | */ | |
7f09ca51 MG |
561 | if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) |
562 | return 1; | |
a63884e9 | 563 | |
7f09ca51 | 564 | return 0; |
a1e78772 MG |
565 | } |
566 | ||
a5516438 | 567 | static void enqueue_huge_page(struct hstate *h, struct page *page) |
1da177e4 LT |
568 | { |
569 | int nid = page_to_nid(page); | |
0edaecfa | 570 | list_move(&page->lru, &h->hugepage_freelists[nid]); |
a5516438 AK |
571 | h->free_huge_pages++; |
572 | h->free_huge_pages_node[nid]++; | |
1da177e4 LT |
573 | } |
574 | ||
bf50bab2 NH |
575 | static struct page *dequeue_huge_page_node(struct hstate *h, int nid) |
576 | { | |
577 | struct page *page; | |
578 | ||
c8721bbb NH |
579 | list_for_each_entry(page, &h->hugepage_freelists[nid], lru) |
580 | if (!is_migrate_isolate_page(page)) | |
581 | break; | |
582 | /* | |
583 | * if 'non-isolated free hugepage' not found on the list, | |
584 | * the allocation fails. | |
585 | */ | |
586 | if (&h->hugepage_freelists[nid] == &page->lru) | |
bf50bab2 | 587 | return NULL; |
0edaecfa | 588 | list_move(&page->lru, &h->hugepage_activelist); |
a9869b83 | 589 | set_page_refcounted(page); |
bf50bab2 NH |
590 | h->free_huge_pages--; |
591 | h->free_huge_pages_node[nid]--; | |
592 | return page; | |
593 | } | |
594 | ||
86cdb465 NH |
595 | /* Movability of hugepages depends on migration support. */ |
596 | static inline gfp_t htlb_alloc_mask(struct hstate *h) | |
597 | { | |
100873d7 | 598 | if (hugepages_treat_as_movable || hugepage_migration_supported(h)) |
86cdb465 NH |
599 | return GFP_HIGHUSER_MOVABLE; |
600 | else | |
601 | return GFP_HIGHUSER; | |
602 | } | |
603 | ||
a5516438 AK |
604 | static struct page *dequeue_huge_page_vma(struct hstate *h, |
605 | struct vm_area_struct *vma, | |
af0ed73e JK |
606 | unsigned long address, int avoid_reserve, |
607 | long chg) | |
1da177e4 | 608 | { |
b1c12cbc | 609 | struct page *page = NULL; |
480eccf9 | 610 | struct mempolicy *mpol; |
19770b32 | 611 | nodemask_t *nodemask; |
c0ff7453 | 612 | struct zonelist *zonelist; |
dd1a239f MG |
613 | struct zone *zone; |
614 | struct zoneref *z; | |
cc9a6c87 | 615 | unsigned int cpuset_mems_cookie; |
1da177e4 | 616 | |
a1e78772 MG |
617 | /* |
618 | * A child process with MAP_PRIVATE mappings created by their parent | |
619 | * have no page reserves. This check ensures that reservations are | |
620 | * not "stolen". The child may still get SIGKILLed | |
621 | */ | |
af0ed73e | 622 | if (!vma_has_reserves(vma, chg) && |
a5516438 | 623 | h->free_huge_pages - h->resv_huge_pages == 0) |
c0ff7453 | 624 | goto err; |
a1e78772 | 625 | |
04f2cbe3 | 626 | /* If reserves cannot be used, ensure enough pages are in the pool */ |
a5516438 | 627 | if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0) |
6eab04a8 | 628 | goto err; |
04f2cbe3 | 629 | |
9966c4bb | 630 | retry_cpuset: |
d26914d1 | 631 | cpuset_mems_cookie = read_mems_allowed_begin(); |
9966c4bb | 632 | zonelist = huge_zonelist(vma, address, |
86cdb465 | 633 | htlb_alloc_mask(h), &mpol, &nodemask); |
9966c4bb | 634 | |
19770b32 MG |
635 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
636 | MAX_NR_ZONES - 1, nodemask) { | |
344736f2 | 637 | if (cpuset_zone_allowed(zone, htlb_alloc_mask(h))) { |
bf50bab2 NH |
638 | page = dequeue_huge_page_node(h, zone_to_nid(zone)); |
639 | if (page) { | |
af0ed73e JK |
640 | if (avoid_reserve) |
641 | break; | |
642 | if (!vma_has_reserves(vma, chg)) | |
643 | break; | |
644 | ||
07443a85 | 645 | SetPagePrivate(page); |
af0ed73e | 646 | h->resv_huge_pages--; |
bf50bab2 NH |
647 | break; |
648 | } | |
3abf7afd | 649 | } |
1da177e4 | 650 | } |
cc9a6c87 | 651 | |
52cd3b07 | 652 | mpol_cond_put(mpol); |
d26914d1 | 653 | if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie))) |
cc9a6c87 | 654 | goto retry_cpuset; |
1da177e4 | 655 | return page; |
cc9a6c87 MG |
656 | |
657 | err: | |
cc9a6c87 | 658 | return NULL; |
1da177e4 LT |
659 | } |
660 | ||
1cac6f2c LC |
661 | /* |
662 | * common helper functions for hstate_next_node_to_{alloc|free}. | |
663 | * We may have allocated or freed a huge page based on a different | |
664 | * nodes_allowed previously, so h->next_node_to_{alloc|free} might | |
665 | * be outside of *nodes_allowed. Ensure that we use an allowed | |
666 | * node for alloc or free. | |
667 | */ | |
668 | static int next_node_allowed(int nid, nodemask_t *nodes_allowed) | |
669 | { | |
670 | nid = next_node(nid, *nodes_allowed); | |
671 | if (nid == MAX_NUMNODES) | |
672 | nid = first_node(*nodes_allowed); | |
673 | VM_BUG_ON(nid >= MAX_NUMNODES); | |
674 | ||
675 | return nid; | |
676 | } | |
677 | ||
678 | static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) | |
679 | { | |
680 | if (!node_isset(nid, *nodes_allowed)) | |
681 | nid = next_node_allowed(nid, nodes_allowed); | |
682 | return nid; | |
683 | } | |
684 | ||
685 | /* | |
686 | * returns the previously saved node ["this node"] from which to | |
687 | * allocate a persistent huge page for the pool and advance the | |
688 | * next node from which to allocate, handling wrap at end of node | |
689 | * mask. | |
690 | */ | |
691 | static int hstate_next_node_to_alloc(struct hstate *h, | |
692 | nodemask_t *nodes_allowed) | |
693 | { | |
694 | int nid; | |
695 | ||
696 | VM_BUG_ON(!nodes_allowed); | |
697 | ||
698 | nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); | |
699 | h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); | |
700 | ||
701 | return nid; | |
702 | } | |
703 | ||
704 | /* | |
705 | * helper for free_pool_huge_page() - return the previously saved | |
706 | * node ["this node"] from which to free a huge page. Advance the | |
707 | * next node id whether or not we find a free huge page to free so | |
708 | * that the next attempt to free addresses the next node. | |
709 | */ | |
710 | static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) | |
711 | { | |
712 | int nid; | |
713 | ||
714 | VM_BUG_ON(!nodes_allowed); | |
715 | ||
716 | nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); | |
717 | h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); | |
718 | ||
719 | return nid; | |
720 | } | |
721 | ||
722 | #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \ | |
723 | for (nr_nodes = nodes_weight(*mask); \ | |
724 | nr_nodes > 0 && \ | |
725 | ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ | |
726 | nr_nodes--) | |
727 | ||
728 | #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ | |
729 | for (nr_nodes = nodes_weight(*mask); \ | |
730 | nr_nodes > 0 && \ | |
731 | ((node = hstate_next_node_to_free(hs, mask)) || 1); \ | |
732 | nr_nodes--) | |
733 | ||
944d9fec LC |
734 | #if defined(CONFIG_CMA) && defined(CONFIG_X86_64) |
735 | static void destroy_compound_gigantic_page(struct page *page, | |
736 | unsigned long order) | |
737 | { | |
738 | int i; | |
739 | int nr_pages = 1 << order; | |
740 | struct page *p = page + 1; | |
741 | ||
742 | for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { | |
743 | __ClearPageTail(p); | |
744 | set_page_refcounted(p); | |
745 | p->first_page = NULL; | |
746 | } | |
747 | ||
748 | set_compound_order(page, 0); | |
749 | __ClearPageHead(page); | |
750 | } | |
751 | ||
752 | static void free_gigantic_page(struct page *page, unsigned order) | |
753 | { | |
754 | free_contig_range(page_to_pfn(page), 1 << order); | |
755 | } | |
756 | ||
757 | static int __alloc_gigantic_page(unsigned long start_pfn, | |
758 | unsigned long nr_pages) | |
759 | { | |
760 | unsigned long end_pfn = start_pfn + nr_pages; | |
761 | return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE); | |
762 | } | |
763 | ||
764 | static bool pfn_range_valid_gigantic(unsigned long start_pfn, | |
765 | unsigned long nr_pages) | |
766 | { | |
767 | unsigned long i, end_pfn = start_pfn + nr_pages; | |
768 | struct page *page; | |
769 | ||
770 | for (i = start_pfn; i < end_pfn; i++) { | |
771 | if (!pfn_valid(i)) | |
772 | return false; | |
773 | ||
774 | page = pfn_to_page(i); | |
775 | ||
776 | if (PageReserved(page)) | |
777 | return false; | |
778 | ||
779 | if (page_count(page) > 0) | |
780 | return false; | |
781 | ||
782 | if (PageHuge(page)) | |
783 | return false; | |
784 | } | |
785 | ||
786 | return true; | |
787 | } | |
788 | ||
789 | static bool zone_spans_last_pfn(const struct zone *zone, | |
790 | unsigned long start_pfn, unsigned long nr_pages) | |
791 | { | |
792 | unsigned long last_pfn = start_pfn + nr_pages - 1; | |
793 | return zone_spans_pfn(zone, last_pfn); | |
794 | } | |
795 | ||
796 | static struct page *alloc_gigantic_page(int nid, unsigned order) | |
797 | { | |
798 | unsigned long nr_pages = 1 << order; | |
799 | unsigned long ret, pfn, flags; | |
800 | struct zone *z; | |
801 | ||
802 | z = NODE_DATA(nid)->node_zones; | |
803 | for (; z - NODE_DATA(nid)->node_zones < MAX_NR_ZONES; z++) { | |
804 | spin_lock_irqsave(&z->lock, flags); | |
805 | ||
806 | pfn = ALIGN(z->zone_start_pfn, nr_pages); | |
807 | while (zone_spans_last_pfn(z, pfn, nr_pages)) { | |
808 | if (pfn_range_valid_gigantic(pfn, nr_pages)) { | |
809 | /* | |
810 | * We release the zone lock here because | |
811 | * alloc_contig_range() will also lock the zone | |
812 | * at some point. If there's an allocation | |
813 | * spinning on this lock, it may win the race | |
814 | * and cause alloc_contig_range() to fail... | |
815 | */ | |
816 | spin_unlock_irqrestore(&z->lock, flags); | |
817 | ret = __alloc_gigantic_page(pfn, nr_pages); | |
818 | if (!ret) | |
819 | return pfn_to_page(pfn); | |
820 | spin_lock_irqsave(&z->lock, flags); | |
821 | } | |
822 | pfn += nr_pages; | |
823 | } | |
824 | ||
825 | spin_unlock_irqrestore(&z->lock, flags); | |
826 | } | |
827 | ||
828 | return NULL; | |
829 | } | |
830 | ||
831 | static void prep_new_huge_page(struct hstate *h, struct page *page, int nid); | |
832 | static void prep_compound_gigantic_page(struct page *page, unsigned long order); | |
833 | ||
834 | static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid) | |
835 | { | |
836 | struct page *page; | |
837 | ||
838 | page = alloc_gigantic_page(nid, huge_page_order(h)); | |
839 | if (page) { | |
840 | prep_compound_gigantic_page(page, huge_page_order(h)); | |
841 | prep_new_huge_page(h, page, nid); | |
842 | } | |
843 | ||
844 | return page; | |
845 | } | |
846 | ||
847 | static int alloc_fresh_gigantic_page(struct hstate *h, | |
848 | nodemask_t *nodes_allowed) | |
849 | { | |
850 | struct page *page = NULL; | |
851 | int nr_nodes, node; | |
852 | ||
853 | for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { | |
854 | page = alloc_fresh_gigantic_page_node(h, node); | |
855 | if (page) | |
856 | return 1; | |
857 | } | |
858 | ||
859 | return 0; | |
860 | } | |
861 | ||
862 | static inline bool gigantic_page_supported(void) { return true; } | |
863 | #else | |
864 | static inline bool gigantic_page_supported(void) { return false; } | |
865 | static inline void free_gigantic_page(struct page *page, unsigned order) { } | |
866 | static inline void destroy_compound_gigantic_page(struct page *page, | |
867 | unsigned long order) { } | |
868 | static inline int alloc_fresh_gigantic_page(struct hstate *h, | |
869 | nodemask_t *nodes_allowed) { return 0; } | |
870 | #endif | |
871 | ||
a5516438 | 872 | static void update_and_free_page(struct hstate *h, struct page *page) |
6af2acb6 AL |
873 | { |
874 | int i; | |
a5516438 | 875 | |
944d9fec LC |
876 | if (hstate_is_gigantic(h) && !gigantic_page_supported()) |
877 | return; | |
18229df5 | 878 | |
a5516438 AK |
879 | h->nr_huge_pages--; |
880 | h->nr_huge_pages_node[page_to_nid(page)]--; | |
881 | for (i = 0; i < pages_per_huge_page(h); i++) { | |
32f84528 CF |
882 | page[i].flags &= ~(1 << PG_locked | 1 << PG_error | |
883 | 1 << PG_referenced | 1 << PG_dirty | | |
a7407a27 LC |
884 | 1 << PG_active | 1 << PG_private | |
885 | 1 << PG_writeback); | |
6af2acb6 | 886 | } |
309381fe | 887 | VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page); |
6af2acb6 AL |
888 | set_compound_page_dtor(page, NULL); |
889 | set_page_refcounted(page); | |
944d9fec LC |
890 | if (hstate_is_gigantic(h)) { |
891 | destroy_compound_gigantic_page(page, huge_page_order(h)); | |
892 | free_gigantic_page(page, huge_page_order(h)); | |
893 | } else { | |
894 | arch_release_hugepage(page); | |
895 | __free_pages(page, huge_page_order(h)); | |
896 | } | |
6af2acb6 AL |
897 | } |
898 | ||
e5ff2159 AK |
899 | struct hstate *size_to_hstate(unsigned long size) |
900 | { | |
901 | struct hstate *h; | |
902 | ||
903 | for_each_hstate(h) { | |
904 | if (huge_page_size(h) == size) | |
905 | return h; | |
906 | } | |
907 | return NULL; | |
908 | } | |
909 | ||
8f1d26d0 | 910 | void free_huge_page(struct page *page) |
27a85ef1 | 911 | { |
a5516438 AK |
912 | /* |
913 | * Can't pass hstate in here because it is called from the | |
914 | * compound page destructor. | |
915 | */ | |
e5ff2159 | 916 | struct hstate *h = page_hstate(page); |
7893d1d5 | 917 | int nid = page_to_nid(page); |
90481622 DG |
918 | struct hugepage_subpool *spool = |
919 | (struct hugepage_subpool *)page_private(page); | |
07443a85 | 920 | bool restore_reserve; |
27a85ef1 | 921 | |
e5df70ab | 922 | set_page_private(page, 0); |
23be7468 | 923 | page->mapping = NULL; |
7893d1d5 | 924 | BUG_ON(page_count(page)); |
0fe6e20b | 925 | BUG_ON(page_mapcount(page)); |
07443a85 | 926 | restore_reserve = PagePrivate(page); |
16c794b4 | 927 | ClearPagePrivate(page); |
27a85ef1 | 928 | |
1c5ecae3 MK |
929 | /* |
930 | * A return code of zero implies that the subpool will be under its | |
931 | * minimum size if the reservation is not restored after page is free. | |
932 | * Therefore, force restore_reserve operation. | |
933 | */ | |
934 | if (hugepage_subpool_put_pages(spool, 1) == 0) | |
935 | restore_reserve = true; | |
936 | ||
27a85ef1 | 937 | spin_lock(&hugetlb_lock); |
6d76dcf4 AK |
938 | hugetlb_cgroup_uncharge_page(hstate_index(h), |
939 | pages_per_huge_page(h), page); | |
07443a85 JK |
940 | if (restore_reserve) |
941 | h->resv_huge_pages++; | |
942 | ||
944d9fec | 943 | if (h->surplus_huge_pages_node[nid]) { |
0edaecfa AK |
944 | /* remove the page from active list */ |
945 | list_del(&page->lru); | |
a5516438 AK |
946 | update_and_free_page(h, page); |
947 | h->surplus_huge_pages--; | |
948 | h->surplus_huge_pages_node[nid]--; | |
7893d1d5 | 949 | } else { |
5d3a551c | 950 | arch_clear_hugepage_flags(page); |
a5516438 | 951 | enqueue_huge_page(h, page); |
7893d1d5 | 952 | } |
27a85ef1 DG |
953 | spin_unlock(&hugetlb_lock); |
954 | } | |
955 | ||
a5516438 | 956 | static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) |
b7ba30c6 | 957 | { |
0edaecfa | 958 | INIT_LIST_HEAD(&page->lru); |
b7ba30c6 AK |
959 | set_compound_page_dtor(page, free_huge_page); |
960 | spin_lock(&hugetlb_lock); | |
9dd540e2 | 961 | set_hugetlb_cgroup(page, NULL); |
a5516438 AK |
962 | h->nr_huge_pages++; |
963 | h->nr_huge_pages_node[nid]++; | |
b7ba30c6 AK |
964 | spin_unlock(&hugetlb_lock); |
965 | put_page(page); /* free it into the hugepage allocator */ | |
966 | } | |
967 | ||
2906dd52 | 968 | static void prep_compound_gigantic_page(struct page *page, unsigned long order) |
20a0307c WF |
969 | { |
970 | int i; | |
971 | int nr_pages = 1 << order; | |
972 | struct page *p = page + 1; | |
973 | ||
974 | /* we rely on prep_new_huge_page to set the destructor */ | |
975 | set_compound_order(page, order); | |
976 | __SetPageHead(page); | |
ef5a22be | 977 | __ClearPageReserved(page); |
20a0307c | 978 | for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { |
ef5a22be AA |
979 | /* |
980 | * For gigantic hugepages allocated through bootmem at | |
981 | * boot, it's safer to be consistent with the not-gigantic | |
982 | * hugepages and clear the PG_reserved bit from all tail pages | |
983 | * too. Otherwse drivers using get_user_pages() to access tail | |
984 | * pages may get the reference counting wrong if they see | |
985 | * PG_reserved set on a tail page (despite the head page not | |
986 | * having PG_reserved set). Enforcing this consistency between | |
987 | * head and tail pages allows drivers to optimize away a check | |
988 | * on the head page when they need know if put_page() is needed | |
989 | * after get_user_pages(). | |
990 | */ | |
991 | __ClearPageReserved(p); | |
58a84aa9 | 992 | set_page_count(p, 0); |
20a0307c | 993 | p->first_page = page; |
44fc8057 DR |
994 | /* Make sure p->first_page is always valid for PageTail() */ |
995 | smp_wmb(); | |
996 | __SetPageTail(p); | |
20a0307c WF |
997 | } |
998 | } | |
999 | ||
7795912c AM |
1000 | /* |
1001 | * PageHuge() only returns true for hugetlbfs pages, but not for normal or | |
1002 | * transparent huge pages. See the PageTransHuge() documentation for more | |
1003 | * details. | |
1004 | */ | |
20a0307c WF |
1005 | int PageHuge(struct page *page) |
1006 | { | |
20a0307c WF |
1007 | if (!PageCompound(page)) |
1008 | return 0; | |
1009 | ||
1010 | page = compound_head(page); | |
758f66a2 | 1011 | return get_compound_page_dtor(page) == free_huge_page; |
20a0307c | 1012 | } |
43131e14 NH |
1013 | EXPORT_SYMBOL_GPL(PageHuge); |
1014 | ||
27c73ae7 AA |
1015 | /* |
1016 | * PageHeadHuge() only returns true for hugetlbfs head page, but not for | |
1017 | * normal or transparent huge pages. | |
1018 | */ | |
1019 | int PageHeadHuge(struct page *page_head) | |
1020 | { | |
27c73ae7 AA |
1021 | if (!PageHead(page_head)) |
1022 | return 0; | |
1023 | ||
758f66a2 | 1024 | return get_compound_page_dtor(page_head) == free_huge_page; |
27c73ae7 | 1025 | } |
27c73ae7 | 1026 | |
13d60f4b ZY |
1027 | pgoff_t __basepage_index(struct page *page) |
1028 | { | |
1029 | struct page *page_head = compound_head(page); | |
1030 | pgoff_t index = page_index(page_head); | |
1031 | unsigned long compound_idx; | |
1032 | ||
1033 | if (!PageHuge(page_head)) | |
1034 | return page_index(page); | |
1035 | ||
1036 | if (compound_order(page_head) >= MAX_ORDER) | |
1037 | compound_idx = page_to_pfn(page) - page_to_pfn(page_head); | |
1038 | else | |
1039 | compound_idx = page - page_head; | |
1040 | ||
1041 | return (index << compound_order(page_head)) + compound_idx; | |
1042 | } | |
1043 | ||
a5516438 | 1044 | static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid) |
1da177e4 | 1045 | { |
1da177e4 | 1046 | struct page *page; |
f96efd58 | 1047 | |
6484eb3e | 1048 | page = alloc_pages_exact_node(nid, |
86cdb465 | 1049 | htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| |
551883ae | 1050 | __GFP_REPEAT|__GFP_NOWARN, |
a5516438 | 1051 | huge_page_order(h)); |
1da177e4 | 1052 | if (page) { |
7f2e9525 | 1053 | if (arch_prepare_hugepage(page)) { |
caff3a2c | 1054 | __free_pages(page, huge_page_order(h)); |
7b8ee84d | 1055 | return NULL; |
7f2e9525 | 1056 | } |
a5516438 | 1057 | prep_new_huge_page(h, page, nid); |
1da177e4 | 1058 | } |
63b4613c NA |
1059 | |
1060 | return page; | |
1061 | } | |
1062 | ||
b2261026 JK |
1063 | static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed) |
1064 | { | |
1065 | struct page *page; | |
1066 | int nr_nodes, node; | |
1067 | int ret = 0; | |
1068 | ||
1069 | for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { | |
1070 | page = alloc_fresh_huge_page_node(h, node); | |
1071 | if (page) { | |
1072 | ret = 1; | |
1073 | break; | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | if (ret) | |
1078 | count_vm_event(HTLB_BUDDY_PGALLOC); | |
1079 | else | |
1080 | count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); | |
1081 | ||
1082 | return ret; | |
1083 | } | |
1084 | ||
e8c5c824 LS |
1085 | /* |
1086 | * Free huge page from pool from next node to free. | |
1087 | * Attempt to keep persistent huge pages more or less | |
1088 | * balanced over allowed nodes. | |
1089 | * Called with hugetlb_lock locked. | |
1090 | */ | |
6ae11b27 LS |
1091 | static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, |
1092 | bool acct_surplus) | |
e8c5c824 | 1093 | { |
b2261026 | 1094 | int nr_nodes, node; |
e8c5c824 LS |
1095 | int ret = 0; |
1096 | ||
b2261026 | 1097 | for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { |
685f3457 LS |
1098 | /* |
1099 | * If we're returning unused surplus pages, only examine | |
1100 | * nodes with surplus pages. | |
1101 | */ | |
b2261026 JK |
1102 | if ((!acct_surplus || h->surplus_huge_pages_node[node]) && |
1103 | !list_empty(&h->hugepage_freelists[node])) { | |
e8c5c824 | 1104 | struct page *page = |
b2261026 | 1105 | list_entry(h->hugepage_freelists[node].next, |
e8c5c824 LS |
1106 | struct page, lru); |
1107 | list_del(&page->lru); | |
1108 | h->free_huge_pages--; | |
b2261026 | 1109 | h->free_huge_pages_node[node]--; |
685f3457 LS |
1110 | if (acct_surplus) { |
1111 | h->surplus_huge_pages--; | |
b2261026 | 1112 | h->surplus_huge_pages_node[node]--; |
685f3457 | 1113 | } |
e8c5c824 LS |
1114 | update_and_free_page(h, page); |
1115 | ret = 1; | |
9a76db09 | 1116 | break; |
e8c5c824 | 1117 | } |
b2261026 | 1118 | } |
e8c5c824 LS |
1119 | |
1120 | return ret; | |
1121 | } | |
1122 | ||
c8721bbb NH |
1123 | /* |
1124 | * Dissolve a given free hugepage into free buddy pages. This function does | |
1125 | * nothing for in-use (including surplus) hugepages. | |
1126 | */ | |
1127 | static void dissolve_free_huge_page(struct page *page) | |
1128 | { | |
1129 | spin_lock(&hugetlb_lock); | |
1130 | if (PageHuge(page) && !page_count(page)) { | |
1131 | struct hstate *h = page_hstate(page); | |
1132 | int nid = page_to_nid(page); | |
1133 | list_del(&page->lru); | |
1134 | h->free_huge_pages--; | |
1135 | h->free_huge_pages_node[nid]--; | |
1136 | update_and_free_page(h, page); | |
1137 | } | |
1138 | spin_unlock(&hugetlb_lock); | |
1139 | } | |
1140 | ||
1141 | /* | |
1142 | * Dissolve free hugepages in a given pfn range. Used by memory hotplug to | |
1143 | * make specified memory blocks removable from the system. | |
1144 | * Note that start_pfn should aligned with (minimum) hugepage size. | |
1145 | */ | |
1146 | void dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) | |
1147 | { | |
1148 | unsigned int order = 8 * sizeof(void *); | |
1149 | unsigned long pfn; | |
1150 | struct hstate *h; | |
1151 | ||
d0177639 LZ |
1152 | if (!hugepages_supported()) |
1153 | return; | |
1154 | ||
c8721bbb NH |
1155 | /* Set scan step to minimum hugepage size */ |
1156 | for_each_hstate(h) | |
1157 | if (order > huge_page_order(h)) | |
1158 | order = huge_page_order(h); | |
1159 | VM_BUG_ON(!IS_ALIGNED(start_pfn, 1 << order)); | |
1160 | for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) | |
1161 | dissolve_free_huge_page(pfn_to_page(pfn)); | |
1162 | } | |
1163 | ||
bf50bab2 | 1164 | static struct page *alloc_buddy_huge_page(struct hstate *h, int nid) |
7893d1d5 AL |
1165 | { |
1166 | struct page *page; | |
bf50bab2 | 1167 | unsigned int r_nid; |
7893d1d5 | 1168 | |
bae7f4ae | 1169 | if (hstate_is_gigantic(h)) |
aa888a74 AK |
1170 | return NULL; |
1171 | ||
d1c3fb1f NA |
1172 | /* |
1173 | * Assume we will successfully allocate the surplus page to | |
1174 | * prevent racing processes from causing the surplus to exceed | |
1175 | * overcommit | |
1176 | * | |
1177 | * This however introduces a different race, where a process B | |
1178 | * tries to grow the static hugepage pool while alloc_pages() is | |
1179 | * called by process A. B will only examine the per-node | |
1180 | * counters in determining if surplus huge pages can be | |
1181 | * converted to normal huge pages in adjust_pool_surplus(). A | |
1182 | * won't be able to increment the per-node counter, until the | |
1183 | * lock is dropped by B, but B doesn't drop hugetlb_lock until | |
1184 | * no more huge pages can be converted from surplus to normal | |
1185 | * state (and doesn't try to convert again). Thus, we have a | |
1186 | * case where a surplus huge page exists, the pool is grown, and | |
1187 | * the surplus huge page still exists after, even though it | |
1188 | * should just have been converted to a normal huge page. This | |
1189 | * does not leak memory, though, as the hugepage will be freed | |
1190 | * once it is out of use. It also does not allow the counters to | |
1191 | * go out of whack in adjust_pool_surplus() as we don't modify | |
1192 | * the node values until we've gotten the hugepage and only the | |
1193 | * per-node value is checked there. | |
1194 | */ | |
1195 | spin_lock(&hugetlb_lock); | |
a5516438 | 1196 | if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { |
d1c3fb1f NA |
1197 | spin_unlock(&hugetlb_lock); |
1198 | return NULL; | |
1199 | } else { | |
a5516438 AK |
1200 | h->nr_huge_pages++; |
1201 | h->surplus_huge_pages++; | |
d1c3fb1f NA |
1202 | } |
1203 | spin_unlock(&hugetlb_lock); | |
1204 | ||
bf50bab2 | 1205 | if (nid == NUMA_NO_NODE) |
86cdb465 | 1206 | page = alloc_pages(htlb_alloc_mask(h)|__GFP_COMP| |
bf50bab2 NH |
1207 | __GFP_REPEAT|__GFP_NOWARN, |
1208 | huge_page_order(h)); | |
1209 | else | |
1210 | page = alloc_pages_exact_node(nid, | |
86cdb465 | 1211 | htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| |
bf50bab2 | 1212 | __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h)); |
d1c3fb1f | 1213 | |
caff3a2c GS |
1214 | if (page && arch_prepare_hugepage(page)) { |
1215 | __free_pages(page, huge_page_order(h)); | |
ea5768c7 | 1216 | page = NULL; |
caff3a2c GS |
1217 | } |
1218 | ||
d1c3fb1f | 1219 | spin_lock(&hugetlb_lock); |
7893d1d5 | 1220 | if (page) { |
0edaecfa | 1221 | INIT_LIST_HEAD(&page->lru); |
bf50bab2 | 1222 | r_nid = page_to_nid(page); |
7893d1d5 | 1223 | set_compound_page_dtor(page, free_huge_page); |
9dd540e2 | 1224 | set_hugetlb_cgroup(page, NULL); |
d1c3fb1f NA |
1225 | /* |
1226 | * We incremented the global counters already | |
1227 | */ | |
bf50bab2 NH |
1228 | h->nr_huge_pages_node[r_nid]++; |
1229 | h->surplus_huge_pages_node[r_nid]++; | |
3b116300 | 1230 | __count_vm_event(HTLB_BUDDY_PGALLOC); |
d1c3fb1f | 1231 | } else { |
a5516438 AK |
1232 | h->nr_huge_pages--; |
1233 | h->surplus_huge_pages--; | |
3b116300 | 1234 | __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); |
7893d1d5 | 1235 | } |
d1c3fb1f | 1236 | spin_unlock(&hugetlb_lock); |
7893d1d5 AL |
1237 | |
1238 | return page; | |
1239 | } | |
1240 | ||
bf50bab2 NH |
1241 | /* |
1242 | * This allocation function is useful in the context where vma is irrelevant. | |
1243 | * E.g. soft-offlining uses this function because it only cares physical | |
1244 | * address of error page. | |
1245 | */ | |
1246 | struct page *alloc_huge_page_node(struct hstate *h, int nid) | |
1247 | { | |
4ef91848 | 1248 | struct page *page = NULL; |
bf50bab2 NH |
1249 | |
1250 | spin_lock(&hugetlb_lock); | |
4ef91848 JK |
1251 | if (h->free_huge_pages - h->resv_huge_pages > 0) |
1252 | page = dequeue_huge_page_node(h, nid); | |
bf50bab2 NH |
1253 | spin_unlock(&hugetlb_lock); |
1254 | ||
94ae8ba7 | 1255 | if (!page) |
bf50bab2 NH |
1256 | page = alloc_buddy_huge_page(h, nid); |
1257 | ||
1258 | return page; | |
1259 | } | |
1260 | ||
e4e574b7 | 1261 | /* |
25985edc | 1262 | * Increase the hugetlb pool such that it can accommodate a reservation |
e4e574b7 AL |
1263 | * of size 'delta'. |
1264 | */ | |
a5516438 | 1265 | static int gather_surplus_pages(struct hstate *h, int delta) |
e4e574b7 AL |
1266 | { |
1267 | struct list_head surplus_list; | |
1268 | struct page *page, *tmp; | |
1269 | int ret, i; | |
1270 | int needed, allocated; | |
28073b02 | 1271 | bool alloc_ok = true; |
e4e574b7 | 1272 | |
a5516438 | 1273 | needed = (h->resv_huge_pages + delta) - h->free_huge_pages; |
ac09b3a1 | 1274 | if (needed <= 0) { |
a5516438 | 1275 | h->resv_huge_pages += delta; |
e4e574b7 | 1276 | return 0; |
ac09b3a1 | 1277 | } |
e4e574b7 AL |
1278 | |
1279 | allocated = 0; | |
1280 | INIT_LIST_HEAD(&surplus_list); | |
1281 | ||
1282 | ret = -ENOMEM; | |
1283 | retry: | |
1284 | spin_unlock(&hugetlb_lock); | |
1285 | for (i = 0; i < needed; i++) { | |
bf50bab2 | 1286 | page = alloc_buddy_huge_page(h, NUMA_NO_NODE); |
28073b02 HD |
1287 | if (!page) { |
1288 | alloc_ok = false; | |
1289 | break; | |
1290 | } | |
e4e574b7 AL |
1291 | list_add(&page->lru, &surplus_list); |
1292 | } | |
28073b02 | 1293 | allocated += i; |
e4e574b7 AL |
1294 | |
1295 | /* | |
1296 | * After retaking hugetlb_lock, we need to recalculate 'needed' | |
1297 | * because either resv_huge_pages or free_huge_pages may have changed. | |
1298 | */ | |
1299 | spin_lock(&hugetlb_lock); | |
a5516438 AK |
1300 | needed = (h->resv_huge_pages + delta) - |
1301 | (h->free_huge_pages + allocated); | |
28073b02 HD |
1302 | if (needed > 0) { |
1303 | if (alloc_ok) | |
1304 | goto retry; | |
1305 | /* | |
1306 | * We were not able to allocate enough pages to | |
1307 | * satisfy the entire reservation so we free what | |
1308 | * we've allocated so far. | |
1309 | */ | |
1310 | goto free; | |
1311 | } | |
e4e574b7 AL |
1312 | /* |
1313 | * The surplus_list now contains _at_least_ the number of extra pages | |
25985edc | 1314 | * needed to accommodate the reservation. Add the appropriate number |
e4e574b7 | 1315 | * of pages to the hugetlb pool and free the extras back to the buddy |
ac09b3a1 AL |
1316 | * allocator. Commit the entire reservation here to prevent another |
1317 | * process from stealing the pages as they are added to the pool but | |
1318 | * before they are reserved. | |
e4e574b7 AL |
1319 | */ |
1320 | needed += allocated; | |
a5516438 | 1321 | h->resv_huge_pages += delta; |
e4e574b7 | 1322 | ret = 0; |
a9869b83 | 1323 | |
19fc3f0a | 1324 | /* Free the needed pages to the hugetlb pool */ |
e4e574b7 | 1325 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) { |
19fc3f0a AL |
1326 | if ((--needed) < 0) |
1327 | break; | |
a9869b83 NH |
1328 | /* |
1329 | * This page is now managed by the hugetlb allocator and has | |
1330 | * no users -- drop the buddy allocator's reference. | |
1331 | */ | |
1332 | put_page_testzero(page); | |
309381fe | 1333 | VM_BUG_ON_PAGE(page_count(page), page); |
a5516438 | 1334 | enqueue_huge_page(h, page); |
19fc3f0a | 1335 | } |
28073b02 | 1336 | free: |
b0365c8d | 1337 | spin_unlock(&hugetlb_lock); |
19fc3f0a AL |
1338 | |
1339 | /* Free unnecessary surplus pages to the buddy allocator */ | |
c0d934ba JK |
1340 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) |
1341 | put_page(page); | |
a9869b83 | 1342 | spin_lock(&hugetlb_lock); |
e4e574b7 AL |
1343 | |
1344 | return ret; | |
1345 | } | |
1346 | ||
1347 | /* | |
1348 | * When releasing a hugetlb pool reservation, any surplus pages that were | |
1349 | * allocated to satisfy the reservation must be explicitly freed if they were | |
1350 | * never used. | |
685f3457 | 1351 | * Called with hugetlb_lock held. |
e4e574b7 | 1352 | */ |
a5516438 AK |
1353 | static void return_unused_surplus_pages(struct hstate *h, |
1354 | unsigned long unused_resv_pages) | |
e4e574b7 | 1355 | { |
e4e574b7 AL |
1356 | unsigned long nr_pages; |
1357 | ||
ac09b3a1 | 1358 | /* Uncommit the reservation */ |
a5516438 | 1359 | h->resv_huge_pages -= unused_resv_pages; |
ac09b3a1 | 1360 | |
aa888a74 | 1361 | /* Cannot return gigantic pages currently */ |
bae7f4ae | 1362 | if (hstate_is_gigantic(h)) |
aa888a74 AK |
1363 | return; |
1364 | ||
a5516438 | 1365 | nr_pages = min(unused_resv_pages, h->surplus_huge_pages); |
e4e574b7 | 1366 | |
685f3457 LS |
1367 | /* |
1368 | * We want to release as many surplus pages as possible, spread | |
9b5e5d0f LS |
1369 | * evenly across all nodes with memory. Iterate across these nodes |
1370 | * until we can no longer free unreserved surplus pages. This occurs | |
1371 | * when the nodes with surplus pages have no free pages. | |
1372 | * free_pool_huge_page() will balance the the freed pages across the | |
1373 | * on-line nodes with memory and will handle the hstate accounting. | |
685f3457 LS |
1374 | */ |
1375 | while (nr_pages--) { | |
8cebfcd0 | 1376 | if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1)) |
685f3457 | 1377 | break; |
7848a4bf | 1378 | cond_resched_lock(&hugetlb_lock); |
e4e574b7 AL |
1379 | } |
1380 | } | |
1381 | ||
c37f9fb1 AW |
1382 | /* |
1383 | * Determine if the huge page at addr within the vma has an associated | |
1384 | * reservation. Where it does not we will need to logically increase | |
90481622 DG |
1385 | * reservation and actually increase subpool usage before an allocation |
1386 | * can occur. Where any new reservation would be required the | |
1387 | * reservation change is prepared, but not committed. Once the page | |
1388 | * has been allocated from the subpool and instantiated the change should | |
1389 | * be committed via vma_commit_reservation. No action is required on | |
1390 | * failure. | |
c37f9fb1 | 1391 | */ |
e2f17d94 | 1392 | static long vma_needs_reservation(struct hstate *h, |
a5516438 | 1393 | struct vm_area_struct *vma, unsigned long addr) |
c37f9fb1 | 1394 | { |
4e35f483 JK |
1395 | struct resv_map *resv; |
1396 | pgoff_t idx; | |
1397 | long chg; | |
c37f9fb1 | 1398 | |
4e35f483 JK |
1399 | resv = vma_resv_map(vma); |
1400 | if (!resv) | |
84afd99b | 1401 | return 1; |
c37f9fb1 | 1402 | |
4e35f483 JK |
1403 | idx = vma_hugecache_offset(h, vma, addr); |
1404 | chg = region_chg(resv, idx, idx + 1); | |
84afd99b | 1405 | |
4e35f483 JK |
1406 | if (vma->vm_flags & VM_MAYSHARE) |
1407 | return chg; | |
1408 | else | |
1409 | return chg < 0 ? chg : 0; | |
c37f9fb1 | 1410 | } |
a5516438 AK |
1411 | static void vma_commit_reservation(struct hstate *h, |
1412 | struct vm_area_struct *vma, unsigned long addr) | |
c37f9fb1 | 1413 | { |
4e35f483 JK |
1414 | struct resv_map *resv; |
1415 | pgoff_t idx; | |
84afd99b | 1416 | |
4e35f483 JK |
1417 | resv = vma_resv_map(vma); |
1418 | if (!resv) | |
1419 | return; | |
84afd99b | 1420 | |
4e35f483 JK |
1421 | idx = vma_hugecache_offset(h, vma, addr); |
1422 | region_add(resv, idx, idx + 1); | |
c37f9fb1 AW |
1423 | } |
1424 | ||
a1e78772 | 1425 | static struct page *alloc_huge_page(struct vm_area_struct *vma, |
04f2cbe3 | 1426 | unsigned long addr, int avoid_reserve) |
1da177e4 | 1427 | { |
90481622 | 1428 | struct hugepage_subpool *spool = subpool_vma(vma); |
a5516438 | 1429 | struct hstate *h = hstate_vma(vma); |
348ea204 | 1430 | struct page *page; |
e2f17d94 | 1431 | long chg; |
6d76dcf4 AK |
1432 | int ret, idx; |
1433 | struct hugetlb_cgroup *h_cg; | |
a1e78772 | 1434 | |
6d76dcf4 | 1435 | idx = hstate_index(h); |
a1e78772 | 1436 | /* |
90481622 DG |
1437 | * Processes that did not create the mapping will have no |
1438 | * reserves and will not have accounted against subpool | |
1439 | * limit. Check that the subpool limit can be made before | |
1440 | * satisfying the allocation MAP_NORESERVE mappings may also | |
1441 | * need pages and subpool limit allocated allocated if no reserve | |
1442 | * mapping overlaps. | |
a1e78772 | 1443 | */ |
a5516438 | 1444 | chg = vma_needs_reservation(h, vma, addr); |
c37f9fb1 | 1445 | if (chg < 0) |
76dcee75 | 1446 | return ERR_PTR(-ENOMEM); |
8bb3f12e | 1447 | if (chg || avoid_reserve) |
1c5ecae3 | 1448 | if (hugepage_subpool_get_pages(spool, 1) < 0) |
76dcee75 | 1449 | return ERR_PTR(-ENOSPC); |
1da177e4 | 1450 | |
6d76dcf4 | 1451 | ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); |
8f34af6f JZ |
1452 | if (ret) |
1453 | goto out_subpool_put; | |
1454 | ||
1da177e4 | 1455 | spin_lock(&hugetlb_lock); |
af0ed73e | 1456 | page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg); |
81a6fcae | 1457 | if (!page) { |
94ae8ba7 | 1458 | spin_unlock(&hugetlb_lock); |
bf50bab2 | 1459 | page = alloc_buddy_huge_page(h, NUMA_NO_NODE); |
8f34af6f JZ |
1460 | if (!page) |
1461 | goto out_uncharge_cgroup; | |
1462 | ||
79dbb236 AK |
1463 | spin_lock(&hugetlb_lock); |
1464 | list_move(&page->lru, &h->hugepage_activelist); | |
81a6fcae | 1465 | /* Fall through */ |
68842c9b | 1466 | } |
81a6fcae JK |
1467 | hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page); |
1468 | spin_unlock(&hugetlb_lock); | |
348ea204 | 1469 | |
90481622 | 1470 | set_page_private(page, (unsigned long)spool); |
90d8b7e6 | 1471 | |
a5516438 | 1472 | vma_commit_reservation(h, vma, addr); |
90d8b7e6 | 1473 | return page; |
8f34af6f JZ |
1474 | |
1475 | out_uncharge_cgroup: | |
1476 | hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); | |
1477 | out_subpool_put: | |
1478 | if (chg || avoid_reserve) | |
1479 | hugepage_subpool_put_pages(spool, 1); | |
1480 | return ERR_PTR(-ENOSPC); | |
b45b5bd6 DG |
1481 | } |
1482 | ||
74060e4d NH |
1483 | /* |
1484 | * alloc_huge_page()'s wrapper which simply returns the page if allocation | |
1485 | * succeeds, otherwise NULL. This function is called from new_vma_page(), | |
1486 | * where no ERR_VALUE is expected to be returned. | |
1487 | */ | |
1488 | struct page *alloc_huge_page_noerr(struct vm_area_struct *vma, | |
1489 | unsigned long addr, int avoid_reserve) | |
1490 | { | |
1491 | struct page *page = alloc_huge_page(vma, addr, avoid_reserve); | |
1492 | if (IS_ERR(page)) | |
1493 | page = NULL; | |
1494 | return page; | |
1495 | } | |
1496 | ||
91f47662 | 1497 | int __weak alloc_bootmem_huge_page(struct hstate *h) |
aa888a74 AK |
1498 | { |
1499 | struct huge_bootmem_page *m; | |
b2261026 | 1500 | int nr_nodes, node; |
aa888a74 | 1501 | |
b2261026 | 1502 | for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) { |
aa888a74 AK |
1503 | void *addr; |
1504 | ||
8b89a116 GS |
1505 | addr = memblock_virt_alloc_try_nid_nopanic( |
1506 | huge_page_size(h), huge_page_size(h), | |
1507 | 0, BOOTMEM_ALLOC_ACCESSIBLE, node); | |
aa888a74 AK |
1508 | if (addr) { |
1509 | /* | |
1510 | * Use the beginning of the huge page to store the | |
1511 | * huge_bootmem_page struct (until gather_bootmem | |
1512 | * puts them into the mem_map). | |
1513 | */ | |
1514 | m = addr; | |
91f47662 | 1515 | goto found; |
aa888a74 | 1516 | } |
aa888a74 AK |
1517 | } |
1518 | return 0; | |
1519 | ||
1520 | found: | |
df994ead | 1521 | BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h))); |
aa888a74 AK |
1522 | /* Put them into a private list first because mem_map is not up yet */ |
1523 | list_add(&m->list, &huge_boot_pages); | |
1524 | m->hstate = h; | |
1525 | return 1; | |
1526 | } | |
1527 | ||
f412c97a | 1528 | static void __init prep_compound_huge_page(struct page *page, int order) |
18229df5 AW |
1529 | { |
1530 | if (unlikely(order > (MAX_ORDER - 1))) | |
1531 | prep_compound_gigantic_page(page, order); | |
1532 | else | |
1533 | prep_compound_page(page, order); | |
1534 | } | |
1535 | ||
aa888a74 AK |
1536 | /* Put bootmem huge pages into the standard lists after mem_map is up */ |
1537 | static void __init gather_bootmem_prealloc(void) | |
1538 | { | |
1539 | struct huge_bootmem_page *m; | |
1540 | ||
1541 | list_for_each_entry(m, &huge_boot_pages, list) { | |
aa888a74 | 1542 | struct hstate *h = m->hstate; |
ee8f248d BB |
1543 | struct page *page; |
1544 | ||
1545 | #ifdef CONFIG_HIGHMEM | |
1546 | page = pfn_to_page(m->phys >> PAGE_SHIFT); | |
8b89a116 GS |
1547 | memblock_free_late(__pa(m), |
1548 | sizeof(struct huge_bootmem_page)); | |
ee8f248d BB |
1549 | #else |
1550 | page = virt_to_page(m); | |
1551 | #endif | |
aa888a74 | 1552 | WARN_ON(page_count(page) != 1); |
18229df5 | 1553 | prep_compound_huge_page(page, h->order); |
ef5a22be | 1554 | WARN_ON(PageReserved(page)); |
aa888a74 | 1555 | prep_new_huge_page(h, page, page_to_nid(page)); |
b0320c7b RA |
1556 | /* |
1557 | * If we had gigantic hugepages allocated at boot time, we need | |
1558 | * to restore the 'stolen' pages to totalram_pages in order to | |
1559 | * fix confusing memory reports from free(1) and another | |
1560 | * side-effects, like CommitLimit going negative. | |
1561 | */ | |
bae7f4ae | 1562 | if (hstate_is_gigantic(h)) |
3dcc0571 | 1563 | adjust_managed_page_count(page, 1 << h->order); |
aa888a74 AK |
1564 | } |
1565 | } | |
1566 | ||
8faa8b07 | 1567 | static void __init hugetlb_hstate_alloc_pages(struct hstate *h) |
1da177e4 LT |
1568 | { |
1569 | unsigned long i; | |
a5516438 | 1570 | |
e5ff2159 | 1571 | for (i = 0; i < h->max_huge_pages; ++i) { |
bae7f4ae | 1572 | if (hstate_is_gigantic(h)) { |
aa888a74 AK |
1573 | if (!alloc_bootmem_huge_page(h)) |
1574 | break; | |
9b5e5d0f | 1575 | } else if (!alloc_fresh_huge_page(h, |
8cebfcd0 | 1576 | &node_states[N_MEMORY])) |
1da177e4 | 1577 | break; |
1da177e4 | 1578 | } |
8faa8b07 | 1579 | h->max_huge_pages = i; |
e5ff2159 AK |
1580 | } |
1581 | ||
1582 | static void __init hugetlb_init_hstates(void) | |
1583 | { | |
1584 | struct hstate *h; | |
1585 | ||
1586 | for_each_hstate(h) { | |
8faa8b07 | 1587 | /* oversize hugepages were init'ed in early boot */ |
bae7f4ae | 1588 | if (!hstate_is_gigantic(h)) |
8faa8b07 | 1589 | hugetlb_hstate_alloc_pages(h); |
e5ff2159 AK |
1590 | } |
1591 | } | |
1592 | ||
4abd32db AK |
1593 | static char * __init memfmt(char *buf, unsigned long n) |
1594 | { | |
1595 | if (n >= (1UL << 30)) | |
1596 | sprintf(buf, "%lu GB", n >> 30); | |
1597 | else if (n >= (1UL << 20)) | |
1598 | sprintf(buf, "%lu MB", n >> 20); | |
1599 | else | |
1600 | sprintf(buf, "%lu KB", n >> 10); | |
1601 | return buf; | |
1602 | } | |
1603 | ||
e5ff2159 AK |
1604 | static void __init report_hugepages(void) |
1605 | { | |
1606 | struct hstate *h; | |
1607 | ||
1608 | for_each_hstate(h) { | |
4abd32db | 1609 | char buf[32]; |
ffb22af5 | 1610 | pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n", |
4abd32db AK |
1611 | memfmt(buf, huge_page_size(h)), |
1612 | h->free_huge_pages); | |
e5ff2159 AK |
1613 | } |
1614 | } | |
1615 | ||
1da177e4 | 1616 | #ifdef CONFIG_HIGHMEM |
6ae11b27 LS |
1617 | static void try_to_free_low(struct hstate *h, unsigned long count, |
1618 | nodemask_t *nodes_allowed) | |
1da177e4 | 1619 | { |
4415cc8d CL |
1620 | int i; |
1621 | ||
bae7f4ae | 1622 | if (hstate_is_gigantic(h)) |
aa888a74 AK |
1623 | return; |
1624 | ||
6ae11b27 | 1625 | for_each_node_mask(i, *nodes_allowed) { |
1da177e4 | 1626 | struct page *page, *next; |
a5516438 AK |
1627 | struct list_head *freel = &h->hugepage_freelists[i]; |
1628 | list_for_each_entry_safe(page, next, freel, lru) { | |
1629 | if (count >= h->nr_huge_pages) | |
6b0c880d | 1630 | return; |
1da177e4 LT |
1631 | if (PageHighMem(page)) |
1632 | continue; | |
1633 | list_del(&page->lru); | |
e5ff2159 | 1634 | update_and_free_page(h, page); |
a5516438 AK |
1635 | h->free_huge_pages--; |
1636 | h->free_huge_pages_node[page_to_nid(page)]--; | |
1da177e4 LT |
1637 | } |
1638 | } | |
1639 | } | |
1640 | #else | |
6ae11b27 LS |
1641 | static inline void try_to_free_low(struct hstate *h, unsigned long count, |
1642 | nodemask_t *nodes_allowed) | |
1da177e4 LT |
1643 | { |
1644 | } | |
1645 | #endif | |
1646 | ||
20a0307c WF |
1647 | /* |
1648 | * Increment or decrement surplus_huge_pages. Keep node-specific counters | |
1649 | * balanced by operating on them in a round-robin fashion. | |
1650 | * Returns 1 if an adjustment was made. | |
1651 | */ | |
6ae11b27 LS |
1652 | static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, |
1653 | int delta) | |
20a0307c | 1654 | { |
b2261026 | 1655 | int nr_nodes, node; |
20a0307c WF |
1656 | |
1657 | VM_BUG_ON(delta != -1 && delta != 1); | |
20a0307c | 1658 | |
b2261026 JK |
1659 | if (delta < 0) { |
1660 | for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { | |
1661 | if (h->surplus_huge_pages_node[node]) | |
1662 | goto found; | |
e8c5c824 | 1663 | } |
b2261026 JK |
1664 | } else { |
1665 | for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { | |
1666 | if (h->surplus_huge_pages_node[node] < | |
1667 | h->nr_huge_pages_node[node]) | |
1668 | goto found; | |
e8c5c824 | 1669 | } |
b2261026 JK |
1670 | } |
1671 | return 0; | |
20a0307c | 1672 | |
b2261026 JK |
1673 | found: |
1674 | h->surplus_huge_pages += delta; | |
1675 | h->surplus_huge_pages_node[node] += delta; | |
1676 | return 1; | |
20a0307c WF |
1677 | } |
1678 | ||
a5516438 | 1679 | #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) |
6ae11b27 LS |
1680 | static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count, |
1681 | nodemask_t *nodes_allowed) | |
1da177e4 | 1682 | { |
7893d1d5 | 1683 | unsigned long min_count, ret; |
1da177e4 | 1684 | |
944d9fec | 1685 | if (hstate_is_gigantic(h) && !gigantic_page_supported()) |
aa888a74 AK |
1686 | return h->max_huge_pages; |
1687 | ||
7893d1d5 AL |
1688 | /* |
1689 | * Increase the pool size | |
1690 | * First take pages out of surplus state. Then make up the | |
1691 | * remaining difference by allocating fresh huge pages. | |
d1c3fb1f NA |
1692 | * |
1693 | * We might race with alloc_buddy_huge_page() here and be unable | |
1694 | * to convert a surplus huge page to a normal huge page. That is | |
1695 | * not critical, though, it just means the overall size of the | |
1696 | * pool might be one hugepage larger than it needs to be, but | |
1697 | * within all the constraints specified by the sysctls. | |
7893d1d5 | 1698 | */ |
1da177e4 | 1699 | spin_lock(&hugetlb_lock); |
a5516438 | 1700 | while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { |
6ae11b27 | 1701 | if (!adjust_pool_surplus(h, nodes_allowed, -1)) |
7893d1d5 AL |
1702 | break; |
1703 | } | |
1704 | ||
a5516438 | 1705 | while (count > persistent_huge_pages(h)) { |
7893d1d5 AL |
1706 | /* |
1707 | * If this allocation races such that we no longer need the | |
1708 | * page, free_huge_page will handle it by freeing the page | |
1709 | * and reducing the surplus. | |
1710 | */ | |
1711 | spin_unlock(&hugetlb_lock); | |
944d9fec LC |
1712 | if (hstate_is_gigantic(h)) |
1713 | ret = alloc_fresh_gigantic_page(h, nodes_allowed); | |
1714 | else | |
1715 | ret = alloc_fresh_huge_page(h, nodes_allowed); | |
7893d1d5 AL |
1716 | spin_lock(&hugetlb_lock); |
1717 | if (!ret) | |
1718 | goto out; | |
1719 | ||
536240f2 MG |
1720 | /* Bail for signals. Probably ctrl-c from user */ |
1721 | if (signal_pending(current)) | |
1722 | goto out; | |
7893d1d5 | 1723 | } |
7893d1d5 AL |
1724 | |
1725 | /* | |
1726 | * Decrease the pool size | |
1727 | * First return free pages to the buddy allocator (being careful | |
1728 | * to keep enough around to satisfy reservations). Then place | |
1729 | * pages into surplus state as needed so the pool will shrink | |
1730 | * to the desired size as pages become free. | |
d1c3fb1f NA |
1731 | * |
1732 | * By placing pages into the surplus state independent of the | |
1733 | * overcommit value, we are allowing the surplus pool size to | |
1734 | * exceed overcommit. There are few sane options here. Since | |
1735 | * alloc_buddy_huge_page() is checking the global counter, | |
1736 | * though, we'll note that we're not allowed to exceed surplus | |
1737 | * and won't grow the pool anywhere else. Not until one of the | |
1738 | * sysctls are changed, or the surplus pages go out of use. | |
7893d1d5 | 1739 | */ |
a5516438 | 1740 | min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; |
6b0c880d | 1741 | min_count = max(count, min_count); |
6ae11b27 | 1742 | try_to_free_low(h, min_count, nodes_allowed); |
a5516438 | 1743 | while (min_count < persistent_huge_pages(h)) { |
6ae11b27 | 1744 | if (!free_pool_huge_page(h, nodes_allowed, 0)) |
1da177e4 | 1745 | break; |
55f67141 | 1746 | cond_resched_lock(&hugetlb_lock); |
1da177e4 | 1747 | } |
a5516438 | 1748 | while (count < persistent_huge_pages(h)) { |
6ae11b27 | 1749 | if (!adjust_pool_surplus(h, nodes_allowed, 1)) |
7893d1d5 AL |
1750 | break; |
1751 | } | |
1752 | out: | |
a5516438 | 1753 | ret = persistent_huge_pages(h); |
1da177e4 | 1754 | spin_unlock(&hugetlb_lock); |
7893d1d5 | 1755 | return ret; |
1da177e4 LT |
1756 | } |
1757 | ||
a3437870 NA |
1758 | #define HSTATE_ATTR_RO(_name) \ |
1759 | static struct kobj_attribute _name##_attr = __ATTR_RO(_name) | |
1760 | ||
1761 | #define HSTATE_ATTR(_name) \ | |
1762 | static struct kobj_attribute _name##_attr = \ | |
1763 | __ATTR(_name, 0644, _name##_show, _name##_store) | |
1764 | ||
1765 | static struct kobject *hugepages_kobj; | |
1766 | static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; | |
1767 | ||
9a305230 LS |
1768 | static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp); |
1769 | ||
1770 | static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp) | |
a3437870 NA |
1771 | { |
1772 | int i; | |
9a305230 | 1773 | |
a3437870 | 1774 | for (i = 0; i < HUGE_MAX_HSTATE; i++) |
9a305230 LS |
1775 | if (hstate_kobjs[i] == kobj) { |
1776 | if (nidp) | |
1777 | *nidp = NUMA_NO_NODE; | |
a3437870 | 1778 | return &hstates[i]; |
9a305230 LS |
1779 | } |
1780 | ||
1781 | return kobj_to_node_hstate(kobj, nidp); | |
a3437870 NA |
1782 | } |
1783 | ||
06808b08 | 1784 | static ssize_t nr_hugepages_show_common(struct kobject *kobj, |
a3437870 NA |
1785 | struct kobj_attribute *attr, char *buf) |
1786 | { | |
9a305230 LS |
1787 | struct hstate *h; |
1788 | unsigned long nr_huge_pages; | |
1789 | int nid; | |
1790 | ||
1791 | h = kobj_to_hstate(kobj, &nid); | |
1792 | if (nid == NUMA_NO_NODE) | |
1793 | nr_huge_pages = h->nr_huge_pages; | |
1794 | else | |
1795 | nr_huge_pages = h->nr_huge_pages_node[nid]; | |
1796 | ||
1797 | return sprintf(buf, "%lu\n", nr_huge_pages); | |
a3437870 | 1798 | } |
adbe8726 | 1799 | |
238d3c13 DR |
1800 | static ssize_t __nr_hugepages_store_common(bool obey_mempolicy, |
1801 | struct hstate *h, int nid, | |
1802 | unsigned long count, size_t len) | |
a3437870 NA |
1803 | { |
1804 | int err; | |
bad44b5b | 1805 | NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY); |
a3437870 | 1806 | |
944d9fec | 1807 | if (hstate_is_gigantic(h) && !gigantic_page_supported()) { |
adbe8726 EM |
1808 | err = -EINVAL; |
1809 | goto out; | |
1810 | } | |
1811 | ||
9a305230 LS |
1812 | if (nid == NUMA_NO_NODE) { |
1813 | /* | |
1814 | * global hstate attribute | |
1815 | */ | |
1816 | if (!(obey_mempolicy && | |
1817 | init_nodemask_of_mempolicy(nodes_allowed))) { | |
1818 | NODEMASK_FREE(nodes_allowed); | |
8cebfcd0 | 1819 | nodes_allowed = &node_states[N_MEMORY]; |
9a305230 LS |
1820 | } |
1821 | } else if (nodes_allowed) { | |
1822 | /* | |
1823 | * per node hstate attribute: adjust count to global, | |
1824 | * but restrict alloc/free to the specified node. | |
1825 | */ | |
1826 | count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; | |
1827 | init_nodemask_of_node(nodes_allowed, nid); | |
1828 | } else | |
8cebfcd0 | 1829 | nodes_allowed = &node_states[N_MEMORY]; |
9a305230 | 1830 | |
06808b08 | 1831 | h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed); |
a3437870 | 1832 | |
8cebfcd0 | 1833 | if (nodes_allowed != &node_states[N_MEMORY]) |
06808b08 LS |
1834 | NODEMASK_FREE(nodes_allowed); |
1835 | ||
1836 | return len; | |
adbe8726 EM |
1837 | out: |
1838 | NODEMASK_FREE(nodes_allowed); | |
1839 | return err; | |
06808b08 LS |
1840 | } |
1841 | ||
238d3c13 DR |
1842 | static ssize_t nr_hugepages_store_common(bool obey_mempolicy, |
1843 | struct kobject *kobj, const char *buf, | |
1844 | size_t len) | |
1845 | { | |
1846 | struct hstate *h; | |
1847 | unsigned long count; | |
1848 | int nid; | |
1849 | int err; | |
1850 | ||
1851 | err = kstrtoul(buf, 10, &count); | |
1852 | if (err) | |
1853 | return err; | |
1854 | ||
1855 | h = kobj_to_hstate(kobj, &nid); | |
1856 | return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len); | |
1857 | } | |
1858 | ||
06808b08 LS |
1859 | static ssize_t nr_hugepages_show(struct kobject *kobj, |
1860 | struct kobj_attribute *attr, char *buf) | |
1861 | { | |
1862 | return nr_hugepages_show_common(kobj, attr, buf); | |
1863 | } | |
1864 | ||
1865 | static ssize_t nr_hugepages_store(struct kobject *kobj, | |
1866 | struct kobj_attribute *attr, const char *buf, size_t len) | |
1867 | { | |
238d3c13 | 1868 | return nr_hugepages_store_common(false, kobj, buf, len); |
a3437870 NA |
1869 | } |
1870 | HSTATE_ATTR(nr_hugepages); | |
1871 | ||
06808b08 LS |
1872 | #ifdef CONFIG_NUMA |
1873 | ||
1874 | /* | |
1875 | * hstate attribute for optionally mempolicy-based constraint on persistent | |
1876 | * huge page alloc/free. | |
1877 | */ | |
1878 | static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj, | |
1879 | struct kobj_attribute *attr, char *buf) | |
1880 | { | |
1881 | return nr_hugepages_show_common(kobj, attr, buf); | |
1882 | } | |
1883 | ||
1884 | static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj, | |
1885 | struct kobj_attribute *attr, const char *buf, size_t len) | |
1886 | { | |
238d3c13 | 1887 | return nr_hugepages_store_common(true, kobj, buf, len); |
06808b08 LS |
1888 | } |
1889 | HSTATE_ATTR(nr_hugepages_mempolicy); | |
1890 | #endif | |
1891 | ||
1892 | ||
a3437870 NA |
1893 | static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, |
1894 | struct kobj_attribute *attr, char *buf) | |
1895 | { | |
9a305230 | 1896 | struct hstate *h = kobj_to_hstate(kobj, NULL); |
a3437870 NA |
1897 | return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages); |
1898 | } | |
adbe8726 | 1899 | |
a3437870 NA |
1900 | static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, |
1901 | struct kobj_attribute *attr, const char *buf, size_t count) | |
1902 | { | |
1903 | int err; | |
1904 | unsigned long input; | |
9a305230 | 1905 | struct hstate *h = kobj_to_hstate(kobj, NULL); |
a3437870 | 1906 | |
bae7f4ae | 1907 | if (hstate_is_gigantic(h)) |
adbe8726 EM |
1908 | return -EINVAL; |
1909 | ||
3dbb95f7 | 1910 | err = kstrtoul(buf, 10, &input); |
a3437870 | 1911 | if (err) |
73ae31e5 | 1912 | return err; |
a3437870 NA |
1913 | |
1914 | spin_lock(&hugetlb_lock); | |
1915 | h->nr_overcommit_huge_pages = input; | |
1916 | spin_unlock(&hugetlb_lock); | |
1917 | ||
1918 | return count; | |
1919 | } | |
1920 | HSTATE_ATTR(nr_overcommit_hugepages); | |
1921 | ||
1922 | static ssize_t free_hugepages_show(struct kobject *kobj, | |
1923 | struct kobj_attribute *attr, char *buf) | |
1924 | { | |
9a305230 LS |
1925 | struct hstate *h; |
1926 | unsigned long free_huge_pages; | |
1927 | int nid; | |
1928 | ||
1929 | h = kobj_to_hstate(kobj, &nid); | |
1930 | if (nid == NUMA_NO_NODE) | |
1931 | free_huge_pages = h->free_huge_pages; | |
1932 | else | |
1933 | free_huge_pages = h->free_huge_pages_node[nid]; | |
1934 | ||
1935 | return sprintf(buf, "%lu\n", free_huge_pages); | |
a3437870 NA |
1936 | } |
1937 | HSTATE_ATTR_RO(free_hugepages); | |
1938 | ||
1939 | static ssize_t resv_hugepages_show(struct kobject *kobj, | |
1940 | struct kobj_attribute *attr, char *buf) | |
1941 | { | |
9a305230 | 1942 | struct hstate *h = kobj_to_hstate(kobj, NULL); |
a3437870 NA |
1943 | return sprintf(buf, "%lu\n", h->resv_huge_pages); |
1944 | } | |
1945 | HSTATE_ATTR_RO(resv_hugepages); | |
1946 | ||
1947 | static ssize_t surplus_hugepages_show(struct kobject *kobj, | |
1948 | struct kobj_attribute *attr, char *buf) | |
1949 | { | |
9a305230 LS |
1950 | struct hstate *h; |
1951 | unsigned long surplus_huge_pages; | |
1952 | int nid; | |
1953 | ||
1954 | h = kobj_to_hstate(kobj, &nid); | |
1955 | if (nid == NUMA_NO_NODE) | |
1956 | surplus_huge_pages = h->surplus_huge_pages; | |
1957 | else | |
1958 | surplus_huge_pages = h->surplus_huge_pages_node[nid]; | |
1959 | ||
1960 | return sprintf(buf, "%lu\n", surplus_huge_pages); | |
a3437870 NA |
1961 | } |
1962 | HSTATE_ATTR_RO(surplus_hugepages); | |
1963 | ||
1964 | static struct attribute *hstate_attrs[] = { | |
1965 | &nr_hugepages_attr.attr, | |
1966 | &nr_overcommit_hugepages_attr.attr, | |
1967 | &free_hugepages_attr.attr, | |
1968 | &resv_hugepages_attr.attr, | |
1969 | &surplus_hugepages_attr.attr, | |
06808b08 LS |
1970 | #ifdef CONFIG_NUMA |
1971 | &nr_hugepages_mempolicy_attr.attr, | |
1972 | #endif | |
a3437870 NA |
1973 | NULL, |
1974 | }; | |
1975 | ||
1976 | static struct attribute_group hstate_attr_group = { | |
1977 | .attrs = hstate_attrs, | |
1978 | }; | |
1979 | ||
094e9539 JM |
1980 | static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, |
1981 | struct kobject **hstate_kobjs, | |
1982 | struct attribute_group *hstate_attr_group) | |
a3437870 NA |
1983 | { |
1984 | int retval; | |
972dc4de | 1985 | int hi = hstate_index(h); |
a3437870 | 1986 | |
9a305230 LS |
1987 | hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); |
1988 | if (!hstate_kobjs[hi]) | |
a3437870 NA |
1989 | return -ENOMEM; |
1990 | ||
9a305230 | 1991 | retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group); |
a3437870 | 1992 | if (retval) |
9a305230 | 1993 | kobject_put(hstate_kobjs[hi]); |
a3437870 NA |
1994 | |
1995 | return retval; | |
1996 | } | |
1997 | ||
1998 | static void __init hugetlb_sysfs_init(void) | |
1999 | { | |
2000 | struct hstate *h; | |
2001 | int err; | |
2002 | ||
2003 | hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); | |
2004 | if (!hugepages_kobj) | |
2005 | return; | |
2006 | ||
2007 | for_each_hstate(h) { | |
9a305230 LS |
2008 | err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, |
2009 | hstate_kobjs, &hstate_attr_group); | |
a3437870 | 2010 | if (err) |
ffb22af5 | 2011 | pr_err("Hugetlb: Unable to add hstate %s", h->name); |
a3437870 NA |
2012 | } |
2013 | } | |
2014 | ||
9a305230 LS |
2015 | #ifdef CONFIG_NUMA |
2016 | ||
2017 | /* | |
2018 | * node_hstate/s - associate per node hstate attributes, via their kobjects, | |
10fbcf4c KS |
2019 | * with node devices in node_devices[] using a parallel array. The array |
2020 | * index of a node device or _hstate == node id. | |
2021 | * This is here to avoid any static dependency of the node device driver, in | |
9a305230 LS |
2022 | * the base kernel, on the hugetlb module. |
2023 | */ | |
2024 | struct node_hstate { | |
2025 | struct kobject *hugepages_kobj; | |
2026 | struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; | |
2027 | }; | |
2028 | struct node_hstate node_hstates[MAX_NUMNODES]; | |
2029 | ||
2030 | /* | |
10fbcf4c | 2031 | * A subset of global hstate attributes for node devices |
9a305230 LS |
2032 | */ |
2033 | static struct attribute *per_node_hstate_attrs[] = { | |
2034 | &nr_hugepages_attr.attr, | |
2035 | &free_hugepages_attr.attr, | |
2036 | &surplus_hugepages_attr.attr, | |
2037 | NULL, | |
2038 | }; | |
2039 | ||
2040 | static struct attribute_group per_node_hstate_attr_group = { | |
2041 | .attrs = per_node_hstate_attrs, | |
2042 | }; | |
2043 | ||
2044 | /* | |
10fbcf4c | 2045 | * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj. |
9a305230 LS |
2046 | * Returns node id via non-NULL nidp. |
2047 | */ | |
2048 | static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) | |
2049 | { | |
2050 | int nid; | |
2051 | ||
2052 | for (nid = 0; nid < nr_node_ids; nid++) { | |
2053 | struct node_hstate *nhs = &node_hstates[nid]; | |
2054 | int i; | |
2055 | for (i = 0; i < HUGE_MAX_HSTATE; i++) | |
2056 | if (nhs->hstate_kobjs[i] == kobj) { | |
2057 | if (nidp) | |
2058 | *nidp = nid; | |
2059 | return &hstates[i]; | |
2060 | } | |
2061 | } | |
2062 | ||
2063 | BUG(); | |
2064 | return NULL; | |
2065 | } | |
2066 | ||
2067 | /* | |
10fbcf4c | 2068 | * Unregister hstate attributes from a single node device. |
9a305230 LS |
2069 | * No-op if no hstate attributes attached. |
2070 | */ | |
3cd8b44f | 2071 | static void hugetlb_unregister_node(struct node *node) |
9a305230 LS |
2072 | { |
2073 | struct hstate *h; | |
10fbcf4c | 2074 | struct node_hstate *nhs = &node_hstates[node->dev.id]; |
9a305230 LS |
2075 | |
2076 | if (!nhs->hugepages_kobj) | |
9b5e5d0f | 2077 | return; /* no hstate attributes */ |
9a305230 | 2078 | |
972dc4de AK |
2079 | for_each_hstate(h) { |
2080 | int idx = hstate_index(h); | |
2081 | if (nhs->hstate_kobjs[idx]) { | |
2082 | kobject_put(nhs->hstate_kobjs[idx]); | |
2083 | nhs->hstate_kobjs[idx] = NULL; | |
9a305230 | 2084 | } |
972dc4de | 2085 | } |
9a305230 LS |
2086 | |
2087 | kobject_put(nhs->hugepages_kobj); | |
2088 | nhs->hugepages_kobj = NULL; | |
2089 | } | |
2090 | ||
2091 | /* | |
10fbcf4c | 2092 | * hugetlb module exit: unregister hstate attributes from node devices |
9a305230 LS |
2093 | * that have them. |
2094 | */ | |
2095 | static void hugetlb_unregister_all_nodes(void) | |
2096 | { | |
2097 | int nid; | |
2098 | ||
2099 | /* | |
10fbcf4c | 2100 | * disable node device registrations. |
9a305230 LS |
2101 | */ |
2102 | register_hugetlbfs_with_node(NULL, NULL); | |
2103 | ||
2104 | /* | |
2105 | * remove hstate attributes from any nodes that have them. | |
2106 | */ | |
2107 | for (nid = 0; nid < nr_node_ids; nid++) | |
8732794b | 2108 | hugetlb_unregister_node(node_devices[nid]); |
9a305230 LS |
2109 | } |
2110 | ||
2111 | /* | |
10fbcf4c | 2112 | * Register hstate attributes for a single node device. |
9a305230 LS |
2113 | * No-op if attributes already registered. |
2114 | */ | |
3cd8b44f | 2115 | static void hugetlb_register_node(struct node *node) |
9a305230 LS |
2116 | { |
2117 | struct hstate *h; | |
10fbcf4c | 2118 | struct node_hstate *nhs = &node_hstates[node->dev.id]; |
9a305230 LS |
2119 | int err; |
2120 | ||
2121 | if (nhs->hugepages_kobj) | |
2122 | return; /* already allocated */ | |
2123 | ||
2124 | nhs->hugepages_kobj = kobject_create_and_add("hugepages", | |
10fbcf4c | 2125 | &node->dev.kobj); |
9a305230 LS |
2126 | if (!nhs->hugepages_kobj) |
2127 | return; | |
2128 | ||
2129 | for_each_hstate(h) { | |
2130 | err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, | |
2131 | nhs->hstate_kobjs, | |
2132 | &per_node_hstate_attr_group); | |
2133 | if (err) { | |
ffb22af5 AM |
2134 | pr_err("Hugetlb: Unable to add hstate %s for node %d\n", |
2135 | h->name, node->dev.id); | |
9a305230 LS |
2136 | hugetlb_unregister_node(node); |
2137 | break; | |
2138 | } | |
2139 | } | |
2140 | } | |
2141 | ||
2142 | /* | |
9b5e5d0f | 2143 | * hugetlb init time: register hstate attributes for all registered node |
10fbcf4c KS |
2144 | * devices of nodes that have memory. All on-line nodes should have |
2145 | * registered their associated device by this time. | |
9a305230 | 2146 | */ |
7d9ca000 | 2147 | static void __init hugetlb_register_all_nodes(void) |
9a305230 LS |
2148 | { |
2149 | int nid; | |
2150 | ||
8cebfcd0 | 2151 | for_each_node_state(nid, N_MEMORY) { |
8732794b | 2152 | struct node *node = node_devices[nid]; |
10fbcf4c | 2153 | if (node->dev.id == nid) |
9a305230 LS |
2154 | hugetlb_register_node(node); |
2155 | } | |
2156 | ||
2157 | /* | |
10fbcf4c | 2158 | * Let the node device driver know we're here so it can |
9a305230 LS |
2159 | * [un]register hstate attributes on node hotplug. |
2160 | */ | |
2161 | register_hugetlbfs_with_node(hugetlb_register_node, | |
2162 | hugetlb_unregister_node); | |
2163 | } | |
2164 | #else /* !CONFIG_NUMA */ | |
2165 | ||
2166 | static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) | |
2167 | { | |
2168 | BUG(); | |
2169 | if (nidp) | |
2170 | *nidp = -1; | |
2171 | return NULL; | |
2172 | } | |
2173 | ||
2174 | static void hugetlb_unregister_all_nodes(void) { } | |
2175 | ||
2176 | static void hugetlb_register_all_nodes(void) { } | |
2177 | ||
2178 | #endif | |
2179 | ||
a3437870 NA |
2180 | static void __exit hugetlb_exit(void) |
2181 | { | |
2182 | struct hstate *h; | |
2183 | ||
9a305230 LS |
2184 | hugetlb_unregister_all_nodes(); |
2185 | ||
a3437870 | 2186 | for_each_hstate(h) { |
972dc4de | 2187 | kobject_put(hstate_kobjs[hstate_index(h)]); |
a3437870 NA |
2188 | } |
2189 | ||
2190 | kobject_put(hugepages_kobj); | |
8382d914 | 2191 | kfree(htlb_fault_mutex_table); |
a3437870 NA |
2192 | } |
2193 | module_exit(hugetlb_exit); | |
2194 | ||
2195 | static int __init hugetlb_init(void) | |
2196 | { | |
8382d914 DB |
2197 | int i; |
2198 | ||
457c1b27 | 2199 | if (!hugepages_supported()) |
0ef89d25 | 2200 | return 0; |
a3437870 | 2201 | |
e11bfbfc NP |
2202 | if (!size_to_hstate(default_hstate_size)) { |
2203 | default_hstate_size = HPAGE_SIZE; | |
2204 | if (!size_to_hstate(default_hstate_size)) | |
2205 | hugetlb_add_hstate(HUGETLB_PAGE_ORDER); | |
a3437870 | 2206 | } |
972dc4de | 2207 | default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size)); |
e11bfbfc NP |
2208 | if (default_hstate_max_huge_pages) |
2209 | default_hstate.max_huge_pages = default_hstate_max_huge_pages; | |
a3437870 NA |
2210 | |
2211 | hugetlb_init_hstates(); | |
aa888a74 | 2212 | gather_bootmem_prealloc(); |
a3437870 NA |
2213 | report_hugepages(); |
2214 | ||
2215 | hugetlb_sysfs_init(); | |
9a305230 | 2216 | hugetlb_register_all_nodes(); |
7179e7bf | 2217 | hugetlb_cgroup_file_init(); |
9a305230 | 2218 | |
8382d914 DB |
2219 | #ifdef CONFIG_SMP |
2220 | num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); | |
2221 | #else | |
2222 | num_fault_mutexes = 1; | |
2223 | #endif | |
2224 | htlb_fault_mutex_table = | |
2225 | kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL); | |
2226 | BUG_ON(!htlb_fault_mutex_table); | |
2227 | ||
2228 | for (i = 0; i < num_fault_mutexes; i++) | |
2229 | mutex_init(&htlb_fault_mutex_table[i]); | |
a3437870 NA |
2230 | return 0; |
2231 | } | |
2232 | module_init(hugetlb_init); | |
2233 | ||
2234 | /* Should be called on processing a hugepagesz=... option */ | |
2235 | void __init hugetlb_add_hstate(unsigned order) | |
2236 | { | |
2237 | struct hstate *h; | |
8faa8b07 AK |
2238 | unsigned long i; |
2239 | ||
a3437870 | 2240 | if (size_to_hstate(PAGE_SIZE << order)) { |
ffb22af5 | 2241 | pr_warning("hugepagesz= specified twice, ignoring\n"); |
a3437870 NA |
2242 | return; |
2243 | } | |
47d38344 | 2244 | BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); |
a3437870 | 2245 | BUG_ON(order == 0); |
47d38344 | 2246 | h = &hstates[hugetlb_max_hstate++]; |
a3437870 NA |
2247 | h->order = order; |
2248 | h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); | |
8faa8b07 AK |
2249 | h->nr_huge_pages = 0; |
2250 | h->free_huge_pages = 0; | |
2251 | for (i = 0; i < MAX_NUMNODES; ++i) | |
2252 | INIT_LIST_HEAD(&h->hugepage_freelists[i]); | |
0edaecfa | 2253 | INIT_LIST_HEAD(&h->hugepage_activelist); |
8cebfcd0 LJ |
2254 | h->next_nid_to_alloc = first_node(node_states[N_MEMORY]); |
2255 | h->next_nid_to_free = first_node(node_states[N_MEMORY]); | |
a3437870 NA |
2256 | snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", |
2257 | huge_page_size(h)/1024); | |
8faa8b07 | 2258 | |
a3437870 NA |
2259 | parsed_hstate = h; |
2260 | } | |
2261 | ||
e11bfbfc | 2262 | static int __init hugetlb_nrpages_setup(char *s) |
a3437870 NA |
2263 | { |
2264 | unsigned long *mhp; | |
8faa8b07 | 2265 | static unsigned long *last_mhp; |
a3437870 NA |
2266 | |
2267 | /* | |
47d38344 | 2268 | * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet, |
a3437870 NA |
2269 | * so this hugepages= parameter goes to the "default hstate". |
2270 | */ | |
47d38344 | 2271 | if (!hugetlb_max_hstate) |
a3437870 NA |
2272 | mhp = &default_hstate_max_huge_pages; |
2273 | else | |
2274 | mhp = &parsed_hstate->max_huge_pages; | |
2275 | ||
8faa8b07 | 2276 | if (mhp == last_mhp) { |
ffb22af5 AM |
2277 | pr_warning("hugepages= specified twice without " |
2278 | "interleaving hugepagesz=, ignoring\n"); | |
8faa8b07 AK |
2279 | return 1; |
2280 | } | |
2281 | ||
a3437870 NA |
2282 | if (sscanf(s, "%lu", mhp) <= 0) |
2283 | *mhp = 0; | |
2284 | ||
8faa8b07 AK |
2285 | /* |
2286 | * Global state is always initialized later in hugetlb_init. | |
2287 | * But we need to allocate >= MAX_ORDER hstates here early to still | |
2288 | * use the bootmem allocator. | |
2289 | */ | |
47d38344 | 2290 | if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER) |
8faa8b07 AK |
2291 | hugetlb_hstate_alloc_pages(parsed_hstate); |
2292 | ||
2293 | last_mhp = mhp; | |
2294 | ||
a3437870 NA |
2295 | return 1; |
2296 | } | |
e11bfbfc NP |
2297 | __setup("hugepages=", hugetlb_nrpages_setup); |
2298 | ||
2299 | static int __init hugetlb_default_setup(char *s) | |
2300 | { | |
2301 | default_hstate_size = memparse(s, &s); | |
2302 | return 1; | |
2303 | } | |
2304 | __setup("default_hugepagesz=", hugetlb_default_setup); | |
a3437870 | 2305 | |
8a213460 NA |
2306 | static unsigned int cpuset_mems_nr(unsigned int *array) |
2307 | { | |
2308 | int node; | |
2309 | unsigned int nr = 0; | |
2310 | ||
2311 | for_each_node_mask(node, cpuset_current_mems_allowed) | |
2312 | nr += array[node]; | |
2313 | ||
2314 | return nr; | |
2315 | } | |
2316 | ||
2317 | #ifdef CONFIG_SYSCTL | |
06808b08 LS |
2318 | static int hugetlb_sysctl_handler_common(bool obey_mempolicy, |
2319 | struct ctl_table *table, int write, | |
2320 | void __user *buffer, size_t *length, loff_t *ppos) | |
1da177e4 | 2321 | { |
e5ff2159 | 2322 | struct hstate *h = &default_hstate; |
238d3c13 | 2323 | unsigned long tmp = h->max_huge_pages; |
08d4a246 | 2324 | int ret; |
e5ff2159 | 2325 | |
457c1b27 NA |
2326 | if (!hugepages_supported()) |
2327 | return -ENOTSUPP; | |
2328 | ||
e5ff2159 AK |
2329 | table->data = &tmp; |
2330 | table->maxlen = sizeof(unsigned long); | |
08d4a246 MH |
2331 | ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); |
2332 | if (ret) | |
2333 | goto out; | |
e5ff2159 | 2334 | |
238d3c13 DR |
2335 | if (write) |
2336 | ret = __nr_hugepages_store_common(obey_mempolicy, h, | |
2337 | NUMA_NO_NODE, tmp, *length); | |
08d4a246 MH |
2338 | out: |
2339 | return ret; | |
1da177e4 | 2340 | } |
396faf03 | 2341 | |
06808b08 LS |
2342 | int hugetlb_sysctl_handler(struct ctl_table *table, int write, |
2343 | void __user *buffer, size_t *length, loff_t *ppos) | |
2344 | { | |
2345 | ||
2346 | return hugetlb_sysctl_handler_common(false, table, write, | |
2347 | buffer, length, ppos); | |
2348 | } | |
2349 | ||
2350 | #ifdef CONFIG_NUMA | |
2351 | int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write, | |
2352 | void __user *buffer, size_t *length, loff_t *ppos) | |
2353 | { | |
2354 | return hugetlb_sysctl_handler_common(true, table, write, | |
2355 | buffer, length, ppos); | |
2356 | } | |
2357 | #endif /* CONFIG_NUMA */ | |
2358 | ||
a3d0c6aa | 2359 | int hugetlb_overcommit_handler(struct ctl_table *table, int write, |
8d65af78 | 2360 | void __user *buffer, |
a3d0c6aa NA |
2361 | size_t *length, loff_t *ppos) |
2362 | { | |
a5516438 | 2363 | struct hstate *h = &default_hstate; |
e5ff2159 | 2364 | unsigned long tmp; |
08d4a246 | 2365 | int ret; |
e5ff2159 | 2366 | |
457c1b27 NA |
2367 | if (!hugepages_supported()) |
2368 | return -ENOTSUPP; | |
2369 | ||
c033a93c | 2370 | tmp = h->nr_overcommit_huge_pages; |
e5ff2159 | 2371 | |
bae7f4ae | 2372 | if (write && hstate_is_gigantic(h)) |
adbe8726 EM |
2373 | return -EINVAL; |
2374 | ||
e5ff2159 AK |
2375 | table->data = &tmp; |
2376 | table->maxlen = sizeof(unsigned long); | |
08d4a246 MH |
2377 | ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); |
2378 | if (ret) | |
2379 | goto out; | |
e5ff2159 AK |
2380 | |
2381 | if (write) { | |
2382 | spin_lock(&hugetlb_lock); | |
2383 | h->nr_overcommit_huge_pages = tmp; | |
2384 | spin_unlock(&hugetlb_lock); | |
2385 | } | |
08d4a246 MH |
2386 | out: |
2387 | return ret; | |
a3d0c6aa NA |
2388 | } |
2389 | ||
1da177e4 LT |
2390 | #endif /* CONFIG_SYSCTL */ |
2391 | ||
e1759c21 | 2392 | void hugetlb_report_meminfo(struct seq_file *m) |
1da177e4 | 2393 | { |
a5516438 | 2394 | struct hstate *h = &default_hstate; |
457c1b27 NA |
2395 | if (!hugepages_supported()) |
2396 | return; | |
e1759c21 | 2397 | seq_printf(m, |
4f98a2fe RR |
2398 | "HugePages_Total: %5lu\n" |
2399 | "HugePages_Free: %5lu\n" | |
2400 | "HugePages_Rsvd: %5lu\n" | |
2401 | "HugePages_Surp: %5lu\n" | |
2402 | "Hugepagesize: %8lu kB\n", | |
a5516438 AK |
2403 | h->nr_huge_pages, |
2404 | h->free_huge_pages, | |
2405 | h->resv_huge_pages, | |
2406 | h->surplus_huge_pages, | |
2407 | 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); | |
1da177e4 LT |
2408 | } |
2409 | ||
2410 | int hugetlb_report_node_meminfo(int nid, char *buf) | |
2411 | { | |
a5516438 | 2412 | struct hstate *h = &default_hstate; |
457c1b27 NA |
2413 | if (!hugepages_supported()) |
2414 | return 0; | |
1da177e4 LT |
2415 | return sprintf(buf, |
2416 | "Node %d HugePages_Total: %5u\n" | |
a1de0919 NA |
2417 | "Node %d HugePages_Free: %5u\n" |
2418 | "Node %d HugePages_Surp: %5u\n", | |
a5516438 AK |
2419 | nid, h->nr_huge_pages_node[nid], |
2420 | nid, h->free_huge_pages_node[nid], | |
2421 | nid, h->surplus_huge_pages_node[nid]); | |
1da177e4 LT |
2422 | } |
2423 | ||
949f7ec5 DR |
2424 | void hugetlb_show_meminfo(void) |
2425 | { | |
2426 | struct hstate *h; | |
2427 | int nid; | |
2428 | ||
457c1b27 NA |
2429 | if (!hugepages_supported()) |
2430 | return; | |
2431 | ||
949f7ec5 DR |
2432 | for_each_node_state(nid, N_MEMORY) |
2433 | for_each_hstate(h) | |
2434 | pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", | |
2435 | nid, | |
2436 | h->nr_huge_pages_node[nid], | |
2437 | h->free_huge_pages_node[nid], | |
2438 | h->surplus_huge_pages_node[nid], | |
2439 | 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); | |
2440 | } | |
2441 | ||
1da177e4 LT |
2442 | /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ |
2443 | unsigned long hugetlb_total_pages(void) | |
2444 | { | |
d0028588 WL |
2445 | struct hstate *h; |
2446 | unsigned long nr_total_pages = 0; | |
2447 | ||
2448 | for_each_hstate(h) | |
2449 | nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); | |
2450 | return nr_total_pages; | |
1da177e4 | 2451 | } |
1da177e4 | 2452 | |
a5516438 | 2453 | static int hugetlb_acct_memory(struct hstate *h, long delta) |
fc1b8a73 MG |
2454 | { |
2455 | int ret = -ENOMEM; | |
2456 | ||
2457 | spin_lock(&hugetlb_lock); | |
2458 | /* | |
2459 | * When cpuset is configured, it breaks the strict hugetlb page | |
2460 | * reservation as the accounting is done on a global variable. Such | |
2461 | * reservation is completely rubbish in the presence of cpuset because | |
2462 | * the reservation is not checked against page availability for the | |
2463 | * current cpuset. Application can still potentially OOM'ed by kernel | |
2464 | * with lack of free htlb page in cpuset that the task is in. | |
2465 | * Attempt to enforce strict accounting with cpuset is almost | |
2466 | * impossible (or too ugly) because cpuset is too fluid that | |
2467 | * task or memory node can be dynamically moved between cpusets. | |
2468 | * | |
2469 | * The change of semantics for shared hugetlb mapping with cpuset is | |
2470 | * undesirable. However, in order to preserve some of the semantics, | |
2471 | * we fall back to check against current free page availability as | |
2472 | * a best attempt and hopefully to minimize the impact of changing | |
2473 | * semantics that cpuset has. | |
2474 | */ | |
2475 | if (delta > 0) { | |
a5516438 | 2476 | if (gather_surplus_pages(h, delta) < 0) |
fc1b8a73 MG |
2477 | goto out; |
2478 | ||
a5516438 AK |
2479 | if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { |
2480 | return_unused_surplus_pages(h, delta); | |
fc1b8a73 MG |
2481 | goto out; |
2482 | } | |
2483 | } | |
2484 | ||
2485 | ret = 0; | |
2486 | if (delta < 0) | |
a5516438 | 2487 | return_unused_surplus_pages(h, (unsigned long) -delta); |
fc1b8a73 MG |
2488 | |
2489 | out: | |
2490 | spin_unlock(&hugetlb_lock); | |
2491 | return ret; | |
2492 | } | |
2493 | ||
84afd99b AW |
2494 | static void hugetlb_vm_op_open(struct vm_area_struct *vma) |
2495 | { | |
f522c3ac | 2496 | struct resv_map *resv = vma_resv_map(vma); |
84afd99b AW |
2497 | |
2498 | /* | |
2499 | * This new VMA should share its siblings reservation map if present. | |
2500 | * The VMA will only ever have a valid reservation map pointer where | |
2501 | * it is being copied for another still existing VMA. As that VMA | |
25985edc | 2502 | * has a reference to the reservation map it cannot disappear until |
84afd99b AW |
2503 | * after this open call completes. It is therefore safe to take a |
2504 | * new reference here without additional locking. | |
2505 | */ | |
4e35f483 | 2506 | if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) |
f522c3ac | 2507 | kref_get(&resv->refs); |
84afd99b AW |
2508 | } |
2509 | ||
a1e78772 MG |
2510 | static void hugetlb_vm_op_close(struct vm_area_struct *vma) |
2511 | { | |
a5516438 | 2512 | struct hstate *h = hstate_vma(vma); |
f522c3ac | 2513 | struct resv_map *resv = vma_resv_map(vma); |
90481622 | 2514 | struct hugepage_subpool *spool = subpool_vma(vma); |
4e35f483 | 2515 | unsigned long reserve, start, end; |
1c5ecae3 | 2516 | long gbl_reserve; |
84afd99b | 2517 | |
4e35f483 JK |
2518 | if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) |
2519 | return; | |
84afd99b | 2520 | |
4e35f483 JK |
2521 | start = vma_hugecache_offset(h, vma, vma->vm_start); |
2522 | end = vma_hugecache_offset(h, vma, vma->vm_end); | |
84afd99b | 2523 | |
4e35f483 | 2524 | reserve = (end - start) - region_count(resv, start, end); |
84afd99b | 2525 | |
4e35f483 JK |
2526 | kref_put(&resv->refs, resv_map_release); |
2527 | ||
2528 | if (reserve) { | |
1c5ecae3 MK |
2529 | /* |
2530 | * Decrement reserve counts. The global reserve count may be | |
2531 | * adjusted if the subpool has a minimum size. | |
2532 | */ | |
2533 | gbl_reserve = hugepage_subpool_put_pages(spool, reserve); | |
2534 | hugetlb_acct_memory(h, -gbl_reserve); | |
84afd99b | 2535 | } |
a1e78772 MG |
2536 | } |
2537 | ||
1da177e4 LT |
2538 | /* |
2539 | * We cannot handle pagefaults against hugetlb pages at all. They cause | |
2540 | * handle_mm_fault() to try to instantiate regular-sized pages in the | |
2541 | * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get | |
2542 | * this far. | |
2543 | */ | |
d0217ac0 | 2544 | static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 LT |
2545 | { |
2546 | BUG(); | |
d0217ac0 | 2547 | return 0; |
1da177e4 LT |
2548 | } |
2549 | ||
f0f37e2f | 2550 | const struct vm_operations_struct hugetlb_vm_ops = { |
d0217ac0 | 2551 | .fault = hugetlb_vm_op_fault, |
84afd99b | 2552 | .open = hugetlb_vm_op_open, |
a1e78772 | 2553 | .close = hugetlb_vm_op_close, |
1da177e4 LT |
2554 | }; |
2555 | ||
1e8f889b DG |
2556 | static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, |
2557 | int writable) | |
63551ae0 DG |
2558 | { |
2559 | pte_t entry; | |
2560 | ||
1e8f889b | 2561 | if (writable) { |
106c992a GS |
2562 | entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, |
2563 | vma->vm_page_prot))); | |
63551ae0 | 2564 | } else { |
106c992a GS |
2565 | entry = huge_pte_wrprotect(mk_huge_pte(page, |
2566 | vma->vm_page_prot)); | |
63551ae0 DG |
2567 | } |
2568 | entry = pte_mkyoung(entry); | |
2569 | entry = pte_mkhuge(entry); | |
d9ed9faa | 2570 | entry = arch_make_huge_pte(entry, vma, page, writable); |
63551ae0 DG |
2571 | |
2572 | return entry; | |
2573 | } | |
2574 | ||
1e8f889b DG |
2575 | static void set_huge_ptep_writable(struct vm_area_struct *vma, |
2576 | unsigned long address, pte_t *ptep) | |
2577 | { | |
2578 | pte_t entry; | |
2579 | ||
106c992a | 2580 | entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); |
32f84528 | 2581 | if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) |
4b3073e1 | 2582 | update_mmu_cache(vma, address, ptep); |
1e8f889b DG |
2583 | } |
2584 | ||
4a705fef NH |
2585 | static int is_hugetlb_entry_migration(pte_t pte) |
2586 | { | |
2587 | swp_entry_t swp; | |
2588 | ||
2589 | if (huge_pte_none(pte) || pte_present(pte)) | |
2590 | return 0; | |
2591 | swp = pte_to_swp_entry(pte); | |
2592 | if (non_swap_entry(swp) && is_migration_entry(swp)) | |
2593 | return 1; | |
2594 | else | |
2595 | return 0; | |
2596 | } | |
2597 | ||
2598 | static int is_hugetlb_entry_hwpoisoned(pte_t pte) | |
2599 | { | |
2600 | swp_entry_t swp; | |
2601 | ||
2602 | if (huge_pte_none(pte) || pte_present(pte)) | |
2603 | return 0; | |
2604 | swp = pte_to_swp_entry(pte); | |
2605 | if (non_swap_entry(swp) && is_hwpoison_entry(swp)) | |
2606 | return 1; | |
2607 | else | |
2608 | return 0; | |
2609 | } | |
1e8f889b | 2610 | |
63551ae0 DG |
2611 | int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, |
2612 | struct vm_area_struct *vma) | |
2613 | { | |
2614 | pte_t *src_pte, *dst_pte, entry; | |
2615 | struct page *ptepage; | |
1c59827d | 2616 | unsigned long addr; |
1e8f889b | 2617 | int cow; |
a5516438 AK |
2618 | struct hstate *h = hstate_vma(vma); |
2619 | unsigned long sz = huge_page_size(h); | |
e8569dd2 AS |
2620 | unsigned long mmun_start; /* For mmu_notifiers */ |
2621 | unsigned long mmun_end; /* For mmu_notifiers */ | |
2622 | int ret = 0; | |
1e8f889b DG |
2623 | |
2624 | cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; | |
63551ae0 | 2625 | |
e8569dd2 AS |
2626 | mmun_start = vma->vm_start; |
2627 | mmun_end = vma->vm_end; | |
2628 | if (cow) | |
2629 | mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end); | |
2630 | ||
a5516438 | 2631 | for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { |
cb900f41 | 2632 | spinlock_t *src_ptl, *dst_ptl; |
c74df32c HD |
2633 | src_pte = huge_pte_offset(src, addr); |
2634 | if (!src_pte) | |
2635 | continue; | |
a5516438 | 2636 | dst_pte = huge_pte_alloc(dst, addr, sz); |
e8569dd2 AS |
2637 | if (!dst_pte) { |
2638 | ret = -ENOMEM; | |
2639 | break; | |
2640 | } | |
c5c99429 LW |
2641 | |
2642 | /* If the pagetables are shared don't copy or take references */ | |
2643 | if (dst_pte == src_pte) | |
2644 | continue; | |
2645 | ||
cb900f41 KS |
2646 | dst_ptl = huge_pte_lock(h, dst, dst_pte); |
2647 | src_ptl = huge_pte_lockptr(h, src, src_pte); | |
2648 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); | |
4a705fef NH |
2649 | entry = huge_ptep_get(src_pte); |
2650 | if (huge_pte_none(entry)) { /* skip none entry */ | |
2651 | ; | |
2652 | } else if (unlikely(is_hugetlb_entry_migration(entry) || | |
2653 | is_hugetlb_entry_hwpoisoned(entry))) { | |
2654 | swp_entry_t swp_entry = pte_to_swp_entry(entry); | |
2655 | ||
2656 | if (is_write_migration_entry(swp_entry) && cow) { | |
2657 | /* | |
2658 | * COW mappings require pages in both | |
2659 | * parent and child to be set to read. | |
2660 | */ | |
2661 | make_migration_entry_read(&swp_entry); | |
2662 | entry = swp_entry_to_pte(swp_entry); | |
2663 | set_huge_pte_at(src, addr, src_pte, entry); | |
2664 | } | |
2665 | set_huge_pte_at(dst, addr, dst_pte, entry); | |
2666 | } else { | |
34ee645e | 2667 | if (cow) { |
7f2e9525 | 2668 | huge_ptep_set_wrprotect(src, addr, src_pte); |
34ee645e JR |
2669 | mmu_notifier_invalidate_range(src, mmun_start, |
2670 | mmun_end); | |
2671 | } | |
0253d634 | 2672 | entry = huge_ptep_get(src_pte); |
1c59827d HD |
2673 | ptepage = pte_page(entry); |
2674 | get_page(ptepage); | |
0fe6e20b | 2675 | page_dup_rmap(ptepage); |
1c59827d HD |
2676 | set_huge_pte_at(dst, addr, dst_pte, entry); |
2677 | } | |
cb900f41 KS |
2678 | spin_unlock(src_ptl); |
2679 | spin_unlock(dst_ptl); | |
63551ae0 | 2680 | } |
63551ae0 | 2681 | |
e8569dd2 AS |
2682 | if (cow) |
2683 | mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end); | |
2684 | ||
2685 | return ret; | |
63551ae0 DG |
2686 | } |
2687 | ||
24669e58 AK |
2688 | void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, |
2689 | unsigned long start, unsigned long end, | |
2690 | struct page *ref_page) | |
63551ae0 | 2691 | { |
24669e58 | 2692 | int force_flush = 0; |
63551ae0 DG |
2693 | struct mm_struct *mm = vma->vm_mm; |
2694 | unsigned long address; | |
c7546f8f | 2695 | pte_t *ptep; |
63551ae0 | 2696 | pte_t pte; |
cb900f41 | 2697 | spinlock_t *ptl; |
63551ae0 | 2698 | struct page *page; |
a5516438 AK |
2699 | struct hstate *h = hstate_vma(vma); |
2700 | unsigned long sz = huge_page_size(h); | |
2ec74c3e SG |
2701 | const unsigned long mmun_start = start; /* For mmu_notifiers */ |
2702 | const unsigned long mmun_end = end; /* For mmu_notifiers */ | |
a5516438 | 2703 | |
63551ae0 | 2704 | WARN_ON(!is_vm_hugetlb_page(vma)); |
a5516438 AK |
2705 | BUG_ON(start & ~huge_page_mask(h)); |
2706 | BUG_ON(end & ~huge_page_mask(h)); | |
63551ae0 | 2707 | |
24669e58 | 2708 | tlb_start_vma(tlb, vma); |
2ec74c3e | 2709 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
569f48b8 | 2710 | address = start; |
24669e58 | 2711 | again: |
569f48b8 | 2712 | for (; address < end; address += sz) { |
c7546f8f | 2713 | ptep = huge_pte_offset(mm, address); |
4c887265 | 2714 | if (!ptep) |
c7546f8f DG |
2715 | continue; |
2716 | ||
cb900f41 | 2717 | ptl = huge_pte_lock(h, mm, ptep); |
39dde65c | 2718 | if (huge_pmd_unshare(mm, &address, ptep)) |
cb900f41 | 2719 | goto unlock; |
39dde65c | 2720 | |
6629326b HD |
2721 | pte = huge_ptep_get(ptep); |
2722 | if (huge_pte_none(pte)) | |
cb900f41 | 2723 | goto unlock; |
6629326b HD |
2724 | |
2725 | /* | |
9fbc1f63 NH |
2726 | * Migrating hugepage or HWPoisoned hugepage is already |
2727 | * unmapped and its refcount is dropped, so just clear pte here. | |
6629326b | 2728 | */ |
9fbc1f63 | 2729 | if (unlikely(!pte_present(pte))) { |
106c992a | 2730 | huge_pte_clear(mm, address, ptep); |
cb900f41 | 2731 | goto unlock; |
8c4894c6 | 2732 | } |
6629326b HD |
2733 | |
2734 | page = pte_page(pte); | |
04f2cbe3 MG |
2735 | /* |
2736 | * If a reference page is supplied, it is because a specific | |
2737 | * page is being unmapped, not a range. Ensure the page we | |
2738 | * are about to unmap is the actual page of interest. | |
2739 | */ | |
2740 | if (ref_page) { | |
04f2cbe3 | 2741 | if (page != ref_page) |
cb900f41 | 2742 | goto unlock; |
04f2cbe3 MG |
2743 | |
2744 | /* | |
2745 | * Mark the VMA as having unmapped its page so that | |
2746 | * future faults in this VMA will fail rather than | |
2747 | * looking like data was lost | |
2748 | */ | |
2749 | set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); | |
2750 | } | |
2751 | ||
c7546f8f | 2752 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
24669e58 | 2753 | tlb_remove_tlb_entry(tlb, ptep, address); |
106c992a | 2754 | if (huge_pte_dirty(pte)) |
6649a386 | 2755 | set_page_dirty(page); |
9e81130b | 2756 | |
24669e58 AK |
2757 | page_remove_rmap(page); |
2758 | force_flush = !__tlb_remove_page(tlb, page); | |
cb900f41 | 2759 | if (force_flush) { |
569f48b8 | 2760 | address += sz; |
cb900f41 | 2761 | spin_unlock(ptl); |
24669e58 | 2762 | break; |
cb900f41 | 2763 | } |
9e81130b | 2764 | /* Bail out after unmapping reference page if supplied */ |
cb900f41 KS |
2765 | if (ref_page) { |
2766 | spin_unlock(ptl); | |
9e81130b | 2767 | break; |
cb900f41 KS |
2768 | } |
2769 | unlock: | |
2770 | spin_unlock(ptl); | |
63551ae0 | 2771 | } |
24669e58 AK |
2772 | /* |
2773 | * mmu_gather ran out of room to batch pages, we break out of | |
2774 | * the PTE lock to avoid doing the potential expensive TLB invalidate | |
2775 | * and page-free while holding it. | |
2776 | */ | |
2777 | if (force_flush) { | |
2778 | force_flush = 0; | |
2779 | tlb_flush_mmu(tlb); | |
2780 | if (address < end && !ref_page) | |
2781 | goto again; | |
fe1668ae | 2782 | } |
2ec74c3e | 2783 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
24669e58 | 2784 | tlb_end_vma(tlb, vma); |
1da177e4 | 2785 | } |
63551ae0 | 2786 | |
d833352a MG |
2787 | void __unmap_hugepage_range_final(struct mmu_gather *tlb, |
2788 | struct vm_area_struct *vma, unsigned long start, | |
2789 | unsigned long end, struct page *ref_page) | |
2790 | { | |
2791 | __unmap_hugepage_range(tlb, vma, start, end, ref_page); | |
2792 | ||
2793 | /* | |
2794 | * Clear this flag so that x86's huge_pmd_share page_table_shareable | |
2795 | * test will fail on a vma being torn down, and not grab a page table | |
2796 | * on its way out. We're lucky that the flag has such an appropriate | |
2797 | * name, and can in fact be safely cleared here. We could clear it | |
2798 | * before the __unmap_hugepage_range above, but all that's necessary | |
c8c06efa | 2799 | * is to clear it before releasing the i_mmap_rwsem. This works |
d833352a | 2800 | * because in the context this is called, the VMA is about to be |
c8c06efa | 2801 | * destroyed and the i_mmap_rwsem is held. |
d833352a MG |
2802 | */ |
2803 | vma->vm_flags &= ~VM_MAYSHARE; | |
2804 | } | |
2805 | ||
502717f4 | 2806 | void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
04f2cbe3 | 2807 | unsigned long end, struct page *ref_page) |
502717f4 | 2808 | { |
24669e58 AK |
2809 | struct mm_struct *mm; |
2810 | struct mmu_gather tlb; | |
2811 | ||
2812 | mm = vma->vm_mm; | |
2813 | ||
2b047252 | 2814 | tlb_gather_mmu(&tlb, mm, start, end); |
24669e58 AK |
2815 | __unmap_hugepage_range(&tlb, vma, start, end, ref_page); |
2816 | tlb_finish_mmu(&tlb, start, end); | |
502717f4 CK |
2817 | } |
2818 | ||
04f2cbe3 MG |
2819 | /* |
2820 | * This is called when the original mapper is failing to COW a MAP_PRIVATE | |
2821 | * mappping it owns the reserve page for. The intention is to unmap the page | |
2822 | * from other VMAs and let the children be SIGKILLed if they are faulting the | |
2823 | * same region. | |
2824 | */ | |
2f4612af DB |
2825 | static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, |
2826 | struct page *page, unsigned long address) | |
04f2cbe3 | 2827 | { |
7526674d | 2828 | struct hstate *h = hstate_vma(vma); |
04f2cbe3 MG |
2829 | struct vm_area_struct *iter_vma; |
2830 | struct address_space *mapping; | |
04f2cbe3 MG |
2831 | pgoff_t pgoff; |
2832 | ||
2833 | /* | |
2834 | * vm_pgoff is in PAGE_SIZE units, hence the different calculation | |
2835 | * from page cache lookup which is in HPAGE_SIZE units. | |
2836 | */ | |
7526674d | 2837 | address = address & huge_page_mask(h); |
36e4f20a MH |
2838 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + |
2839 | vma->vm_pgoff; | |
496ad9aa | 2840 | mapping = file_inode(vma->vm_file)->i_mapping; |
04f2cbe3 | 2841 | |
4eb2b1dc MG |
2842 | /* |
2843 | * Take the mapping lock for the duration of the table walk. As | |
2844 | * this mapping should be shared between all the VMAs, | |
2845 | * __unmap_hugepage_range() is called as the lock is already held | |
2846 | */ | |
83cde9e8 | 2847 | i_mmap_lock_write(mapping); |
6b2dbba8 | 2848 | vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { |
04f2cbe3 MG |
2849 | /* Do not unmap the current VMA */ |
2850 | if (iter_vma == vma) | |
2851 | continue; | |
2852 | ||
2853 | /* | |
2854 | * Unmap the page from other VMAs without their own reserves. | |
2855 | * They get marked to be SIGKILLed if they fault in these | |
2856 | * areas. This is because a future no-page fault on this VMA | |
2857 | * could insert a zeroed page instead of the data existing | |
2858 | * from the time of fork. This would look like data corruption | |
2859 | */ | |
2860 | if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) | |
24669e58 AK |
2861 | unmap_hugepage_range(iter_vma, address, |
2862 | address + huge_page_size(h), page); | |
04f2cbe3 | 2863 | } |
83cde9e8 | 2864 | i_mmap_unlock_write(mapping); |
04f2cbe3 MG |
2865 | } |
2866 | ||
0fe6e20b NH |
2867 | /* |
2868 | * Hugetlb_cow() should be called with page lock of the original hugepage held. | |
ef009b25 MH |
2869 | * Called with hugetlb_instantiation_mutex held and pte_page locked so we |
2870 | * cannot race with other handlers or page migration. | |
2871 | * Keep the pte_same checks anyway to make transition from the mutex easier. | |
0fe6e20b | 2872 | */ |
1e8f889b | 2873 | static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, |
04f2cbe3 | 2874 | unsigned long address, pte_t *ptep, pte_t pte, |
cb900f41 | 2875 | struct page *pagecache_page, spinlock_t *ptl) |
1e8f889b | 2876 | { |
a5516438 | 2877 | struct hstate *h = hstate_vma(vma); |
1e8f889b | 2878 | struct page *old_page, *new_page; |
ad4404a2 | 2879 | int ret = 0, outside_reserve = 0; |
2ec74c3e SG |
2880 | unsigned long mmun_start; /* For mmu_notifiers */ |
2881 | unsigned long mmun_end; /* For mmu_notifiers */ | |
1e8f889b DG |
2882 | |
2883 | old_page = pte_page(pte); | |
2884 | ||
04f2cbe3 | 2885 | retry_avoidcopy: |
1e8f889b DG |
2886 | /* If no-one else is actually using this page, avoid the copy |
2887 | * and just make the page writable */ | |
37a2140d JK |
2888 | if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { |
2889 | page_move_anon_rmap(old_page, vma, address); | |
1e8f889b | 2890 | set_huge_ptep_writable(vma, address, ptep); |
83c54070 | 2891 | return 0; |
1e8f889b DG |
2892 | } |
2893 | ||
04f2cbe3 MG |
2894 | /* |
2895 | * If the process that created a MAP_PRIVATE mapping is about to | |
2896 | * perform a COW due to a shared page count, attempt to satisfy | |
2897 | * the allocation without using the existing reserves. The pagecache | |
2898 | * page is used to determine if the reserve at this address was | |
2899 | * consumed or not. If reserves were used, a partial faulted mapping | |
2900 | * at the time of fork() could consume its reserves on COW instead | |
2901 | * of the full address range. | |
2902 | */ | |
5944d011 | 2903 | if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && |
04f2cbe3 MG |
2904 | old_page != pagecache_page) |
2905 | outside_reserve = 1; | |
2906 | ||
1e8f889b | 2907 | page_cache_get(old_page); |
b76c8cfb | 2908 | |
ad4404a2 DB |
2909 | /* |
2910 | * Drop page table lock as buddy allocator may be called. It will | |
2911 | * be acquired again before returning to the caller, as expected. | |
2912 | */ | |
cb900f41 | 2913 | spin_unlock(ptl); |
04f2cbe3 | 2914 | new_page = alloc_huge_page(vma, address, outside_reserve); |
1e8f889b | 2915 | |
2fc39cec | 2916 | if (IS_ERR(new_page)) { |
04f2cbe3 MG |
2917 | /* |
2918 | * If a process owning a MAP_PRIVATE mapping fails to COW, | |
2919 | * it is due to references held by a child and an insufficient | |
2920 | * huge page pool. To guarantee the original mappers | |
2921 | * reliability, unmap the page from child processes. The child | |
2922 | * may get SIGKILLed if it later faults. | |
2923 | */ | |
2924 | if (outside_reserve) { | |
ad4404a2 | 2925 | page_cache_release(old_page); |
04f2cbe3 | 2926 | BUG_ON(huge_pte_none(pte)); |
2f4612af DB |
2927 | unmap_ref_private(mm, vma, old_page, address); |
2928 | BUG_ON(huge_pte_none(pte)); | |
2929 | spin_lock(ptl); | |
2930 | ptep = huge_pte_offset(mm, address & huge_page_mask(h)); | |
2931 | if (likely(ptep && | |
2932 | pte_same(huge_ptep_get(ptep), pte))) | |
2933 | goto retry_avoidcopy; | |
2934 | /* | |
2935 | * race occurs while re-acquiring page table | |
2936 | * lock, and our job is done. | |
2937 | */ | |
2938 | return 0; | |
04f2cbe3 MG |
2939 | } |
2940 | ||
ad4404a2 DB |
2941 | ret = (PTR_ERR(new_page) == -ENOMEM) ? |
2942 | VM_FAULT_OOM : VM_FAULT_SIGBUS; | |
2943 | goto out_release_old; | |
1e8f889b DG |
2944 | } |
2945 | ||
0fe6e20b NH |
2946 | /* |
2947 | * When the original hugepage is shared one, it does not have | |
2948 | * anon_vma prepared. | |
2949 | */ | |
44e2aa93 | 2950 | if (unlikely(anon_vma_prepare(vma))) { |
ad4404a2 DB |
2951 | ret = VM_FAULT_OOM; |
2952 | goto out_release_all; | |
44e2aa93 | 2953 | } |
0fe6e20b | 2954 | |
47ad8475 AA |
2955 | copy_user_huge_page(new_page, old_page, address, vma, |
2956 | pages_per_huge_page(h)); | |
0ed361de | 2957 | __SetPageUptodate(new_page); |
1e8f889b | 2958 | |
2ec74c3e SG |
2959 | mmun_start = address & huge_page_mask(h); |
2960 | mmun_end = mmun_start + huge_page_size(h); | |
2961 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); | |
ad4404a2 | 2962 | |
b76c8cfb | 2963 | /* |
cb900f41 | 2964 | * Retake the page table lock to check for racing updates |
b76c8cfb LW |
2965 | * before the page tables are altered |
2966 | */ | |
cb900f41 | 2967 | spin_lock(ptl); |
a5516438 | 2968 | ptep = huge_pte_offset(mm, address & huge_page_mask(h)); |
a9af0c5d | 2969 | if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { |
07443a85 JK |
2970 | ClearPagePrivate(new_page); |
2971 | ||
1e8f889b | 2972 | /* Break COW */ |
8fe627ec | 2973 | huge_ptep_clear_flush(vma, address, ptep); |
34ee645e | 2974 | mmu_notifier_invalidate_range(mm, mmun_start, mmun_end); |
1e8f889b DG |
2975 | set_huge_pte_at(mm, address, ptep, |
2976 | make_huge_pte(vma, new_page, 1)); | |
0fe6e20b | 2977 | page_remove_rmap(old_page); |
cd67f0d2 | 2978 | hugepage_add_new_anon_rmap(new_page, vma, address); |
1e8f889b DG |
2979 | /* Make the old page be freed below */ |
2980 | new_page = old_page; | |
2981 | } | |
cb900f41 | 2982 | spin_unlock(ptl); |
2ec74c3e | 2983 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
ad4404a2 | 2984 | out_release_all: |
1e8f889b | 2985 | page_cache_release(new_page); |
ad4404a2 | 2986 | out_release_old: |
1e8f889b | 2987 | page_cache_release(old_page); |
8312034f | 2988 | |
ad4404a2 DB |
2989 | spin_lock(ptl); /* Caller expects lock to be held */ |
2990 | return ret; | |
1e8f889b DG |
2991 | } |
2992 | ||
04f2cbe3 | 2993 | /* Return the pagecache page at a given address within a VMA */ |
a5516438 AK |
2994 | static struct page *hugetlbfs_pagecache_page(struct hstate *h, |
2995 | struct vm_area_struct *vma, unsigned long address) | |
04f2cbe3 MG |
2996 | { |
2997 | struct address_space *mapping; | |
e7c4b0bf | 2998 | pgoff_t idx; |
04f2cbe3 MG |
2999 | |
3000 | mapping = vma->vm_file->f_mapping; | |
a5516438 | 3001 | idx = vma_hugecache_offset(h, vma, address); |
04f2cbe3 MG |
3002 | |
3003 | return find_lock_page(mapping, idx); | |
3004 | } | |
3005 | ||
3ae77f43 HD |
3006 | /* |
3007 | * Return whether there is a pagecache page to back given address within VMA. | |
3008 | * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page. | |
3009 | */ | |
3010 | static bool hugetlbfs_pagecache_present(struct hstate *h, | |
2a15efc9 HD |
3011 | struct vm_area_struct *vma, unsigned long address) |
3012 | { | |
3013 | struct address_space *mapping; | |
3014 | pgoff_t idx; | |
3015 | struct page *page; | |
3016 | ||
3017 | mapping = vma->vm_file->f_mapping; | |
3018 | idx = vma_hugecache_offset(h, vma, address); | |
3019 | ||
3020 | page = find_get_page(mapping, idx); | |
3021 | if (page) | |
3022 | put_page(page); | |
3023 | return page != NULL; | |
3024 | } | |
3025 | ||
a1ed3dda | 3026 | static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, |
8382d914 DB |
3027 | struct address_space *mapping, pgoff_t idx, |
3028 | unsigned long address, pte_t *ptep, unsigned int flags) | |
ac9b9c66 | 3029 | { |
a5516438 | 3030 | struct hstate *h = hstate_vma(vma); |
ac9b9c66 | 3031 | int ret = VM_FAULT_SIGBUS; |
409eb8c2 | 3032 | int anon_rmap = 0; |
4c887265 | 3033 | unsigned long size; |
4c887265 | 3034 | struct page *page; |
1e8f889b | 3035 | pte_t new_pte; |
cb900f41 | 3036 | spinlock_t *ptl; |
4c887265 | 3037 | |
04f2cbe3 MG |
3038 | /* |
3039 | * Currently, we are forced to kill the process in the event the | |
3040 | * original mapper has unmapped pages from the child due to a failed | |
25985edc | 3041 | * COW. Warn that such a situation has occurred as it may not be obvious |
04f2cbe3 MG |
3042 | */ |
3043 | if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { | |
ffb22af5 AM |
3044 | pr_warning("PID %d killed due to inadequate hugepage pool\n", |
3045 | current->pid); | |
04f2cbe3 MG |
3046 | return ret; |
3047 | } | |
3048 | ||
4c887265 AL |
3049 | /* |
3050 | * Use page lock to guard against racing truncation | |
3051 | * before we get page_table_lock. | |
3052 | */ | |
6bda666a CL |
3053 | retry: |
3054 | page = find_lock_page(mapping, idx); | |
3055 | if (!page) { | |
a5516438 | 3056 | size = i_size_read(mapping->host) >> huge_page_shift(h); |
ebed4bfc HD |
3057 | if (idx >= size) |
3058 | goto out; | |
04f2cbe3 | 3059 | page = alloc_huge_page(vma, address, 0); |
2fc39cec | 3060 | if (IS_ERR(page)) { |
76dcee75 AK |
3061 | ret = PTR_ERR(page); |
3062 | if (ret == -ENOMEM) | |
3063 | ret = VM_FAULT_OOM; | |
3064 | else | |
3065 | ret = VM_FAULT_SIGBUS; | |
6bda666a CL |
3066 | goto out; |
3067 | } | |
47ad8475 | 3068 | clear_huge_page(page, address, pages_per_huge_page(h)); |
0ed361de | 3069 | __SetPageUptodate(page); |
ac9b9c66 | 3070 | |
f83a275d | 3071 | if (vma->vm_flags & VM_MAYSHARE) { |
6bda666a | 3072 | int err; |
45c682a6 | 3073 | struct inode *inode = mapping->host; |
6bda666a CL |
3074 | |
3075 | err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); | |
3076 | if (err) { | |
3077 | put_page(page); | |
6bda666a CL |
3078 | if (err == -EEXIST) |
3079 | goto retry; | |
3080 | goto out; | |
3081 | } | |
07443a85 | 3082 | ClearPagePrivate(page); |
45c682a6 KC |
3083 | |
3084 | spin_lock(&inode->i_lock); | |
a5516438 | 3085 | inode->i_blocks += blocks_per_huge_page(h); |
45c682a6 | 3086 | spin_unlock(&inode->i_lock); |
23be7468 | 3087 | } else { |
6bda666a | 3088 | lock_page(page); |
0fe6e20b NH |
3089 | if (unlikely(anon_vma_prepare(vma))) { |
3090 | ret = VM_FAULT_OOM; | |
3091 | goto backout_unlocked; | |
3092 | } | |
409eb8c2 | 3093 | anon_rmap = 1; |
23be7468 | 3094 | } |
0fe6e20b | 3095 | } else { |
998b4382 NH |
3096 | /* |
3097 | * If memory error occurs between mmap() and fault, some process | |
3098 | * don't have hwpoisoned swap entry for errored virtual address. | |
3099 | * So we need to block hugepage fault by PG_hwpoison bit check. | |
3100 | */ | |
3101 | if (unlikely(PageHWPoison(page))) { | |
32f84528 | 3102 | ret = VM_FAULT_HWPOISON | |
972dc4de | 3103 | VM_FAULT_SET_HINDEX(hstate_index(h)); |
998b4382 NH |
3104 | goto backout_unlocked; |
3105 | } | |
6bda666a | 3106 | } |
1e8f889b | 3107 | |
57303d80 AW |
3108 | /* |
3109 | * If we are going to COW a private mapping later, we examine the | |
3110 | * pending reservations for this page now. This will ensure that | |
3111 | * any allocations necessary to record that reservation occur outside | |
3112 | * the spinlock. | |
3113 | */ | |
788c7df4 | 3114 | if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) |
2b26736c AW |
3115 | if (vma_needs_reservation(h, vma, address) < 0) { |
3116 | ret = VM_FAULT_OOM; | |
3117 | goto backout_unlocked; | |
3118 | } | |
57303d80 | 3119 | |
cb900f41 KS |
3120 | ptl = huge_pte_lockptr(h, mm, ptep); |
3121 | spin_lock(ptl); | |
a5516438 | 3122 | size = i_size_read(mapping->host) >> huge_page_shift(h); |
4c887265 AL |
3123 | if (idx >= size) |
3124 | goto backout; | |
3125 | ||
83c54070 | 3126 | ret = 0; |
7f2e9525 | 3127 | if (!huge_pte_none(huge_ptep_get(ptep))) |
4c887265 AL |
3128 | goto backout; |
3129 | ||
07443a85 JK |
3130 | if (anon_rmap) { |
3131 | ClearPagePrivate(page); | |
409eb8c2 | 3132 | hugepage_add_new_anon_rmap(page, vma, address); |
ac714904 | 3133 | } else |
409eb8c2 | 3134 | page_dup_rmap(page); |
1e8f889b DG |
3135 | new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) |
3136 | && (vma->vm_flags & VM_SHARED))); | |
3137 | set_huge_pte_at(mm, address, ptep, new_pte); | |
3138 | ||
788c7df4 | 3139 | if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { |
1e8f889b | 3140 | /* Optimization, do the COW without a second fault */ |
cb900f41 | 3141 | ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page, ptl); |
1e8f889b DG |
3142 | } |
3143 | ||
cb900f41 | 3144 | spin_unlock(ptl); |
4c887265 AL |
3145 | unlock_page(page); |
3146 | out: | |
ac9b9c66 | 3147 | return ret; |
4c887265 AL |
3148 | |
3149 | backout: | |
cb900f41 | 3150 | spin_unlock(ptl); |
2b26736c | 3151 | backout_unlocked: |
4c887265 AL |
3152 | unlock_page(page); |
3153 | put_page(page); | |
3154 | goto out; | |
ac9b9c66 HD |
3155 | } |
3156 | ||
8382d914 DB |
3157 | #ifdef CONFIG_SMP |
3158 | static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm, | |
3159 | struct vm_area_struct *vma, | |
3160 | struct address_space *mapping, | |
3161 | pgoff_t idx, unsigned long address) | |
3162 | { | |
3163 | unsigned long key[2]; | |
3164 | u32 hash; | |
3165 | ||
3166 | if (vma->vm_flags & VM_SHARED) { | |
3167 | key[0] = (unsigned long) mapping; | |
3168 | key[1] = idx; | |
3169 | } else { | |
3170 | key[0] = (unsigned long) mm; | |
3171 | key[1] = address >> huge_page_shift(h); | |
3172 | } | |
3173 | ||
3174 | hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0); | |
3175 | ||
3176 | return hash & (num_fault_mutexes - 1); | |
3177 | } | |
3178 | #else | |
3179 | /* | |
3180 | * For uniprocesor systems we always use a single mutex, so just | |
3181 | * return 0 and avoid the hashing overhead. | |
3182 | */ | |
3183 | static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm, | |
3184 | struct vm_area_struct *vma, | |
3185 | struct address_space *mapping, | |
3186 | pgoff_t idx, unsigned long address) | |
3187 | { | |
3188 | return 0; | |
3189 | } | |
3190 | #endif | |
3191 | ||
86e5216f | 3192 | int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, |
788c7df4 | 3193 | unsigned long address, unsigned int flags) |
86e5216f | 3194 | { |
8382d914 | 3195 | pte_t *ptep, entry; |
cb900f41 | 3196 | spinlock_t *ptl; |
1e8f889b | 3197 | int ret; |
8382d914 DB |
3198 | u32 hash; |
3199 | pgoff_t idx; | |
0fe6e20b | 3200 | struct page *page = NULL; |
57303d80 | 3201 | struct page *pagecache_page = NULL; |
a5516438 | 3202 | struct hstate *h = hstate_vma(vma); |
8382d914 | 3203 | struct address_space *mapping; |
0f792cf9 | 3204 | int need_wait_lock = 0; |
86e5216f | 3205 | |
1e16a539 KH |
3206 | address &= huge_page_mask(h); |
3207 | ||
fd6a03ed NH |
3208 | ptep = huge_pte_offset(mm, address); |
3209 | if (ptep) { | |
3210 | entry = huge_ptep_get(ptep); | |
290408d4 | 3211 | if (unlikely(is_hugetlb_entry_migration(entry))) { |
cb900f41 | 3212 | migration_entry_wait_huge(vma, mm, ptep); |
290408d4 NH |
3213 | return 0; |
3214 | } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) | |
32f84528 | 3215 | return VM_FAULT_HWPOISON_LARGE | |
972dc4de | 3216 | VM_FAULT_SET_HINDEX(hstate_index(h)); |
fd6a03ed NH |
3217 | } |
3218 | ||
a5516438 | 3219 | ptep = huge_pte_alloc(mm, address, huge_page_size(h)); |
86e5216f AL |
3220 | if (!ptep) |
3221 | return VM_FAULT_OOM; | |
3222 | ||
8382d914 DB |
3223 | mapping = vma->vm_file->f_mapping; |
3224 | idx = vma_hugecache_offset(h, vma, address); | |
3225 | ||
3935baa9 DG |
3226 | /* |
3227 | * Serialize hugepage allocation and instantiation, so that we don't | |
3228 | * get spurious allocation failures if two CPUs race to instantiate | |
3229 | * the same page in the page cache. | |
3230 | */ | |
8382d914 DB |
3231 | hash = fault_mutex_hash(h, mm, vma, mapping, idx, address); |
3232 | mutex_lock(&htlb_fault_mutex_table[hash]); | |
3233 | ||
7f2e9525 GS |
3234 | entry = huge_ptep_get(ptep); |
3235 | if (huge_pte_none(entry)) { | |
8382d914 | 3236 | ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags); |
b4d1d99f | 3237 | goto out_mutex; |
3935baa9 | 3238 | } |
86e5216f | 3239 | |
83c54070 | 3240 | ret = 0; |
1e8f889b | 3241 | |
0f792cf9 NH |
3242 | /* |
3243 | * entry could be a migration/hwpoison entry at this point, so this | |
3244 | * check prevents the kernel from going below assuming that we have | |
3245 | * a active hugepage in pagecache. This goto expects the 2nd page fault, | |
3246 | * and is_hugetlb_entry_(migration|hwpoisoned) check will properly | |
3247 | * handle it. | |
3248 | */ | |
3249 | if (!pte_present(entry)) | |
3250 | goto out_mutex; | |
3251 | ||
57303d80 AW |
3252 | /* |
3253 | * If we are going to COW the mapping later, we examine the pending | |
3254 | * reservations for this page now. This will ensure that any | |
3255 | * allocations necessary to record that reservation occur outside the | |
3256 | * spinlock. For private mappings, we also lookup the pagecache | |
3257 | * page now as it is used to determine if a reservation has been | |
3258 | * consumed. | |
3259 | */ | |
106c992a | 3260 | if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { |
2b26736c AW |
3261 | if (vma_needs_reservation(h, vma, address) < 0) { |
3262 | ret = VM_FAULT_OOM; | |
b4d1d99f | 3263 | goto out_mutex; |
2b26736c | 3264 | } |
57303d80 | 3265 | |
f83a275d | 3266 | if (!(vma->vm_flags & VM_MAYSHARE)) |
57303d80 AW |
3267 | pagecache_page = hugetlbfs_pagecache_page(h, |
3268 | vma, address); | |
3269 | } | |
3270 | ||
0f792cf9 NH |
3271 | ptl = huge_pte_lock(h, mm, ptep); |
3272 | ||
3273 | /* Check for a racing update before calling hugetlb_cow */ | |
3274 | if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) | |
3275 | goto out_ptl; | |
3276 | ||
56c9cfb1 NH |
3277 | /* |
3278 | * hugetlb_cow() requires page locks of pte_page(entry) and | |
3279 | * pagecache_page, so here we need take the former one | |
3280 | * when page != pagecache_page or !pagecache_page. | |
56c9cfb1 NH |
3281 | */ |
3282 | page = pte_page(entry); | |
3283 | if (page != pagecache_page) | |
0f792cf9 NH |
3284 | if (!trylock_page(page)) { |
3285 | need_wait_lock = 1; | |
3286 | goto out_ptl; | |
3287 | } | |
b4d1d99f | 3288 | |
0f792cf9 | 3289 | get_page(page); |
b4d1d99f | 3290 | |
788c7df4 | 3291 | if (flags & FAULT_FLAG_WRITE) { |
106c992a | 3292 | if (!huge_pte_write(entry)) { |
57303d80 | 3293 | ret = hugetlb_cow(mm, vma, address, ptep, entry, |
cb900f41 | 3294 | pagecache_page, ptl); |
0f792cf9 | 3295 | goto out_put_page; |
b4d1d99f | 3296 | } |
106c992a | 3297 | entry = huge_pte_mkdirty(entry); |
b4d1d99f DG |
3298 | } |
3299 | entry = pte_mkyoung(entry); | |
788c7df4 HD |
3300 | if (huge_ptep_set_access_flags(vma, address, ptep, entry, |
3301 | flags & FAULT_FLAG_WRITE)) | |
4b3073e1 | 3302 | update_mmu_cache(vma, address, ptep); |
0f792cf9 NH |
3303 | out_put_page: |
3304 | if (page != pagecache_page) | |
3305 | unlock_page(page); | |
3306 | put_page(page); | |
cb900f41 KS |
3307 | out_ptl: |
3308 | spin_unlock(ptl); | |
57303d80 AW |
3309 | |
3310 | if (pagecache_page) { | |
3311 | unlock_page(pagecache_page); | |
3312 | put_page(pagecache_page); | |
3313 | } | |
b4d1d99f | 3314 | out_mutex: |
8382d914 | 3315 | mutex_unlock(&htlb_fault_mutex_table[hash]); |
0f792cf9 NH |
3316 | /* |
3317 | * Generally it's safe to hold refcount during waiting page lock. But | |
3318 | * here we just wait to defer the next page fault to avoid busy loop and | |
3319 | * the page is not used after unlocked before returning from the current | |
3320 | * page fault. So we are safe from accessing freed page, even if we wait | |
3321 | * here without taking refcount. | |
3322 | */ | |
3323 | if (need_wait_lock) | |
3324 | wait_on_page_locked(page); | |
1e8f889b | 3325 | return ret; |
86e5216f AL |
3326 | } |
3327 | ||
28a35716 ML |
3328 | long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, |
3329 | struct page **pages, struct vm_area_struct **vmas, | |
3330 | unsigned long *position, unsigned long *nr_pages, | |
3331 | long i, unsigned int flags) | |
63551ae0 | 3332 | { |
d5d4b0aa CK |
3333 | unsigned long pfn_offset; |
3334 | unsigned long vaddr = *position; | |
28a35716 | 3335 | unsigned long remainder = *nr_pages; |
a5516438 | 3336 | struct hstate *h = hstate_vma(vma); |
63551ae0 | 3337 | |
63551ae0 | 3338 | while (vaddr < vma->vm_end && remainder) { |
4c887265 | 3339 | pte_t *pte; |
cb900f41 | 3340 | spinlock_t *ptl = NULL; |
2a15efc9 | 3341 | int absent; |
4c887265 | 3342 | struct page *page; |
63551ae0 | 3343 | |
02057967 DR |
3344 | /* |
3345 | * If we have a pending SIGKILL, don't keep faulting pages and | |
3346 | * potentially allocating memory. | |
3347 | */ | |
3348 | if (unlikely(fatal_signal_pending(current))) { | |
3349 | remainder = 0; | |
3350 | break; | |
3351 | } | |
3352 | ||
4c887265 AL |
3353 | /* |
3354 | * Some archs (sparc64, sh*) have multiple pte_ts to | |
2a15efc9 | 3355 | * each hugepage. We have to make sure we get the |
4c887265 | 3356 | * first, for the page indexing below to work. |
cb900f41 KS |
3357 | * |
3358 | * Note that page table lock is not held when pte is null. | |
4c887265 | 3359 | */ |
a5516438 | 3360 | pte = huge_pte_offset(mm, vaddr & huge_page_mask(h)); |
cb900f41 KS |
3361 | if (pte) |
3362 | ptl = huge_pte_lock(h, mm, pte); | |
2a15efc9 HD |
3363 | absent = !pte || huge_pte_none(huge_ptep_get(pte)); |
3364 | ||
3365 | /* | |
3366 | * When coredumping, it suits get_dump_page if we just return | |
3ae77f43 HD |
3367 | * an error where there's an empty slot with no huge pagecache |
3368 | * to back it. This way, we avoid allocating a hugepage, and | |
3369 | * the sparse dumpfile avoids allocating disk blocks, but its | |
3370 | * huge holes still show up with zeroes where they need to be. | |
2a15efc9 | 3371 | */ |
3ae77f43 HD |
3372 | if (absent && (flags & FOLL_DUMP) && |
3373 | !hugetlbfs_pagecache_present(h, vma, vaddr)) { | |
cb900f41 KS |
3374 | if (pte) |
3375 | spin_unlock(ptl); | |
2a15efc9 HD |
3376 | remainder = 0; |
3377 | break; | |
3378 | } | |
63551ae0 | 3379 | |
9cc3a5bd NH |
3380 | /* |
3381 | * We need call hugetlb_fault for both hugepages under migration | |
3382 | * (in which case hugetlb_fault waits for the migration,) and | |
3383 | * hwpoisoned hugepages (in which case we need to prevent the | |
3384 | * caller from accessing to them.) In order to do this, we use | |
3385 | * here is_swap_pte instead of is_hugetlb_entry_migration and | |
3386 | * is_hugetlb_entry_hwpoisoned. This is because it simply covers | |
3387 | * both cases, and because we can't follow correct pages | |
3388 | * directly from any kind of swap entries. | |
3389 | */ | |
3390 | if (absent || is_swap_pte(huge_ptep_get(pte)) || | |
106c992a GS |
3391 | ((flags & FOLL_WRITE) && |
3392 | !huge_pte_write(huge_ptep_get(pte)))) { | |
4c887265 | 3393 | int ret; |
63551ae0 | 3394 | |
cb900f41 KS |
3395 | if (pte) |
3396 | spin_unlock(ptl); | |
2a15efc9 HD |
3397 | ret = hugetlb_fault(mm, vma, vaddr, |
3398 | (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0); | |
a89182c7 | 3399 | if (!(ret & VM_FAULT_ERROR)) |
4c887265 | 3400 | continue; |
63551ae0 | 3401 | |
4c887265 | 3402 | remainder = 0; |
4c887265 AL |
3403 | break; |
3404 | } | |
3405 | ||
a5516438 | 3406 | pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; |
7f2e9525 | 3407 | page = pte_page(huge_ptep_get(pte)); |
d5d4b0aa | 3408 | same_page: |
d6692183 | 3409 | if (pages) { |
2a15efc9 | 3410 | pages[i] = mem_map_offset(page, pfn_offset); |
a0368d4e | 3411 | get_page_foll(pages[i]); |
d6692183 | 3412 | } |
63551ae0 DG |
3413 | |
3414 | if (vmas) | |
3415 | vmas[i] = vma; | |
3416 | ||
3417 | vaddr += PAGE_SIZE; | |
d5d4b0aa | 3418 | ++pfn_offset; |
63551ae0 DG |
3419 | --remainder; |
3420 | ++i; | |
d5d4b0aa | 3421 | if (vaddr < vma->vm_end && remainder && |
a5516438 | 3422 | pfn_offset < pages_per_huge_page(h)) { |
d5d4b0aa CK |
3423 | /* |
3424 | * We use pfn_offset to avoid touching the pageframes | |
3425 | * of this compound page. | |
3426 | */ | |
3427 | goto same_page; | |
3428 | } | |
cb900f41 | 3429 | spin_unlock(ptl); |
63551ae0 | 3430 | } |
28a35716 | 3431 | *nr_pages = remainder; |
63551ae0 DG |
3432 | *position = vaddr; |
3433 | ||
2a15efc9 | 3434 | return i ? i : -EFAULT; |
63551ae0 | 3435 | } |
8f860591 | 3436 | |
7da4d641 | 3437 | unsigned long hugetlb_change_protection(struct vm_area_struct *vma, |
8f860591 ZY |
3438 | unsigned long address, unsigned long end, pgprot_t newprot) |
3439 | { | |
3440 | struct mm_struct *mm = vma->vm_mm; | |
3441 | unsigned long start = address; | |
3442 | pte_t *ptep; | |
3443 | pte_t pte; | |
a5516438 | 3444 | struct hstate *h = hstate_vma(vma); |
7da4d641 | 3445 | unsigned long pages = 0; |
8f860591 ZY |
3446 | |
3447 | BUG_ON(address >= end); | |
3448 | flush_cache_range(vma, address, end); | |
3449 | ||
a5338093 | 3450 | mmu_notifier_invalidate_range_start(mm, start, end); |
83cde9e8 | 3451 | i_mmap_lock_write(vma->vm_file->f_mapping); |
a5516438 | 3452 | for (; address < end; address += huge_page_size(h)) { |
cb900f41 | 3453 | spinlock_t *ptl; |
8f860591 ZY |
3454 | ptep = huge_pte_offset(mm, address); |
3455 | if (!ptep) | |
3456 | continue; | |
cb900f41 | 3457 | ptl = huge_pte_lock(h, mm, ptep); |
7da4d641 PZ |
3458 | if (huge_pmd_unshare(mm, &address, ptep)) { |
3459 | pages++; | |
cb900f41 | 3460 | spin_unlock(ptl); |
39dde65c | 3461 | continue; |
7da4d641 | 3462 | } |
a8bda28d NH |
3463 | pte = huge_ptep_get(ptep); |
3464 | if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { | |
3465 | spin_unlock(ptl); | |
3466 | continue; | |
3467 | } | |
3468 | if (unlikely(is_hugetlb_entry_migration(pte))) { | |
3469 | swp_entry_t entry = pte_to_swp_entry(pte); | |
3470 | ||
3471 | if (is_write_migration_entry(entry)) { | |
3472 | pte_t newpte; | |
3473 | ||
3474 | make_migration_entry_read(&entry); | |
3475 | newpte = swp_entry_to_pte(entry); | |
3476 | set_huge_pte_at(mm, address, ptep, newpte); | |
3477 | pages++; | |
3478 | } | |
3479 | spin_unlock(ptl); | |
3480 | continue; | |
3481 | } | |
3482 | if (!huge_pte_none(pte)) { | |
8f860591 | 3483 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
106c992a | 3484 | pte = pte_mkhuge(huge_pte_modify(pte, newprot)); |
be7517d6 | 3485 | pte = arch_make_huge_pte(pte, vma, NULL, 0); |
8f860591 | 3486 | set_huge_pte_at(mm, address, ptep, pte); |
7da4d641 | 3487 | pages++; |
8f860591 | 3488 | } |
cb900f41 | 3489 | spin_unlock(ptl); |
8f860591 | 3490 | } |
d833352a | 3491 | /* |
c8c06efa | 3492 | * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare |
d833352a | 3493 | * may have cleared our pud entry and done put_page on the page table: |
c8c06efa | 3494 | * once we release i_mmap_rwsem, another task can do the final put_page |
d833352a MG |
3495 | * and that page table be reused and filled with junk. |
3496 | */ | |
8f860591 | 3497 | flush_tlb_range(vma, start, end); |
34ee645e | 3498 | mmu_notifier_invalidate_range(mm, start, end); |
83cde9e8 | 3499 | i_mmap_unlock_write(vma->vm_file->f_mapping); |
a5338093 | 3500 | mmu_notifier_invalidate_range_end(mm, start, end); |
7da4d641 PZ |
3501 | |
3502 | return pages << h->order; | |
8f860591 ZY |
3503 | } |
3504 | ||
a1e78772 MG |
3505 | int hugetlb_reserve_pages(struct inode *inode, |
3506 | long from, long to, | |
5a6fe125 | 3507 | struct vm_area_struct *vma, |
ca16d140 | 3508 | vm_flags_t vm_flags) |
e4e574b7 | 3509 | { |
17c9d12e | 3510 | long ret, chg; |
a5516438 | 3511 | struct hstate *h = hstate_inode(inode); |
90481622 | 3512 | struct hugepage_subpool *spool = subpool_inode(inode); |
9119a41e | 3513 | struct resv_map *resv_map; |
1c5ecae3 | 3514 | long gbl_reserve; |
e4e574b7 | 3515 | |
17c9d12e MG |
3516 | /* |
3517 | * Only apply hugepage reservation if asked. At fault time, an | |
3518 | * attempt will be made for VM_NORESERVE to allocate a page | |
90481622 | 3519 | * without using reserves |
17c9d12e | 3520 | */ |
ca16d140 | 3521 | if (vm_flags & VM_NORESERVE) |
17c9d12e MG |
3522 | return 0; |
3523 | ||
a1e78772 MG |
3524 | /* |
3525 | * Shared mappings base their reservation on the number of pages that | |
3526 | * are already allocated on behalf of the file. Private mappings need | |
3527 | * to reserve the full area even if read-only as mprotect() may be | |
3528 | * called to make the mapping read-write. Assume !vma is a shm mapping | |
3529 | */ | |
9119a41e | 3530 | if (!vma || vma->vm_flags & VM_MAYSHARE) { |
4e35f483 | 3531 | resv_map = inode_resv_map(inode); |
9119a41e | 3532 | |
1406ec9b | 3533 | chg = region_chg(resv_map, from, to); |
9119a41e JK |
3534 | |
3535 | } else { | |
3536 | resv_map = resv_map_alloc(); | |
17c9d12e MG |
3537 | if (!resv_map) |
3538 | return -ENOMEM; | |
3539 | ||
a1e78772 | 3540 | chg = to - from; |
84afd99b | 3541 | |
17c9d12e MG |
3542 | set_vma_resv_map(vma, resv_map); |
3543 | set_vma_resv_flags(vma, HPAGE_RESV_OWNER); | |
3544 | } | |
3545 | ||
c50ac050 DH |
3546 | if (chg < 0) { |
3547 | ret = chg; | |
3548 | goto out_err; | |
3549 | } | |
8a630112 | 3550 | |
1c5ecae3 MK |
3551 | /* |
3552 | * There must be enough pages in the subpool for the mapping. If | |
3553 | * the subpool has a minimum size, there may be some global | |
3554 | * reservations already in place (gbl_reserve). | |
3555 | */ | |
3556 | gbl_reserve = hugepage_subpool_get_pages(spool, chg); | |
3557 | if (gbl_reserve < 0) { | |
c50ac050 DH |
3558 | ret = -ENOSPC; |
3559 | goto out_err; | |
3560 | } | |
5a6fe125 MG |
3561 | |
3562 | /* | |
17c9d12e | 3563 | * Check enough hugepages are available for the reservation. |
90481622 | 3564 | * Hand the pages back to the subpool if there are not |
5a6fe125 | 3565 | */ |
1c5ecae3 | 3566 | ret = hugetlb_acct_memory(h, gbl_reserve); |
68842c9b | 3567 | if (ret < 0) { |
1c5ecae3 MK |
3568 | /* put back original number of pages, chg */ |
3569 | (void)hugepage_subpool_put_pages(spool, chg); | |
c50ac050 | 3570 | goto out_err; |
68842c9b | 3571 | } |
17c9d12e MG |
3572 | |
3573 | /* | |
3574 | * Account for the reservations made. Shared mappings record regions | |
3575 | * that have reservations as they are shared by multiple VMAs. | |
3576 | * When the last VMA disappears, the region map says how much | |
3577 | * the reservation was and the page cache tells how much of | |
3578 | * the reservation was consumed. Private mappings are per-VMA and | |
3579 | * only the consumed reservations are tracked. When the VMA | |
3580 | * disappears, the original reservation is the VMA size and the | |
3581 | * consumed reservations are stored in the map. Hence, nothing | |
3582 | * else has to be done for private mappings here | |
3583 | */ | |
f83a275d | 3584 | if (!vma || vma->vm_flags & VM_MAYSHARE) |
1406ec9b | 3585 | region_add(resv_map, from, to); |
a43a8c39 | 3586 | return 0; |
c50ac050 | 3587 | out_err: |
f031dd27 JK |
3588 | if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) |
3589 | kref_put(&resv_map->refs, resv_map_release); | |
c50ac050 | 3590 | return ret; |
a43a8c39 CK |
3591 | } |
3592 | ||
3593 | void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) | |
3594 | { | |
a5516438 | 3595 | struct hstate *h = hstate_inode(inode); |
4e35f483 | 3596 | struct resv_map *resv_map = inode_resv_map(inode); |
9119a41e | 3597 | long chg = 0; |
90481622 | 3598 | struct hugepage_subpool *spool = subpool_inode(inode); |
1c5ecae3 | 3599 | long gbl_reserve; |
45c682a6 | 3600 | |
9119a41e | 3601 | if (resv_map) |
1406ec9b | 3602 | chg = region_truncate(resv_map, offset); |
45c682a6 | 3603 | spin_lock(&inode->i_lock); |
e4c6f8be | 3604 | inode->i_blocks -= (blocks_per_huge_page(h) * freed); |
45c682a6 KC |
3605 | spin_unlock(&inode->i_lock); |
3606 | ||
1c5ecae3 MK |
3607 | /* |
3608 | * If the subpool has a minimum size, the number of global | |
3609 | * reservations to be released may be adjusted. | |
3610 | */ | |
3611 | gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); | |
3612 | hugetlb_acct_memory(h, -gbl_reserve); | |
a43a8c39 | 3613 | } |
93f70f90 | 3614 | |
3212b535 SC |
3615 | #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE |
3616 | static unsigned long page_table_shareable(struct vm_area_struct *svma, | |
3617 | struct vm_area_struct *vma, | |
3618 | unsigned long addr, pgoff_t idx) | |
3619 | { | |
3620 | unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + | |
3621 | svma->vm_start; | |
3622 | unsigned long sbase = saddr & PUD_MASK; | |
3623 | unsigned long s_end = sbase + PUD_SIZE; | |
3624 | ||
3625 | /* Allow segments to share if only one is marked locked */ | |
3626 | unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED; | |
3627 | unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED; | |
3628 | ||
3629 | /* | |
3630 | * match the virtual addresses, permission and the alignment of the | |
3631 | * page table page. | |
3632 | */ | |
3633 | if (pmd_index(addr) != pmd_index(saddr) || | |
3634 | vm_flags != svm_flags || | |
3635 | sbase < svma->vm_start || svma->vm_end < s_end) | |
3636 | return 0; | |
3637 | ||
3638 | return saddr; | |
3639 | } | |
3640 | ||
3641 | static int vma_shareable(struct vm_area_struct *vma, unsigned long addr) | |
3642 | { | |
3643 | unsigned long base = addr & PUD_MASK; | |
3644 | unsigned long end = base + PUD_SIZE; | |
3645 | ||
3646 | /* | |
3647 | * check on proper vm_flags and page table alignment | |
3648 | */ | |
3649 | if (vma->vm_flags & VM_MAYSHARE && | |
3650 | vma->vm_start <= base && end <= vma->vm_end) | |
3651 | return 1; | |
3652 | return 0; | |
3653 | } | |
3654 | ||
3655 | /* | |
3656 | * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() | |
3657 | * and returns the corresponding pte. While this is not necessary for the | |
3658 | * !shared pmd case because we can allocate the pmd later as well, it makes the | |
3659 | * code much cleaner. pmd allocation is essential for the shared case because | |
c8c06efa | 3660 | * pud has to be populated inside the same i_mmap_rwsem section - otherwise |
3212b535 SC |
3661 | * racing tasks could either miss the sharing (see huge_pte_offset) or select a |
3662 | * bad pmd for sharing. | |
3663 | */ | |
3664 | pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) | |
3665 | { | |
3666 | struct vm_area_struct *vma = find_vma(mm, addr); | |
3667 | struct address_space *mapping = vma->vm_file->f_mapping; | |
3668 | pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + | |
3669 | vma->vm_pgoff; | |
3670 | struct vm_area_struct *svma; | |
3671 | unsigned long saddr; | |
3672 | pte_t *spte = NULL; | |
3673 | pte_t *pte; | |
cb900f41 | 3674 | spinlock_t *ptl; |
3212b535 SC |
3675 | |
3676 | if (!vma_shareable(vma, addr)) | |
3677 | return (pte_t *)pmd_alloc(mm, pud, addr); | |
3678 | ||
83cde9e8 | 3679 | i_mmap_lock_write(mapping); |
3212b535 SC |
3680 | vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { |
3681 | if (svma == vma) | |
3682 | continue; | |
3683 | ||
3684 | saddr = page_table_shareable(svma, vma, addr, idx); | |
3685 | if (saddr) { | |
3686 | spte = huge_pte_offset(svma->vm_mm, saddr); | |
3687 | if (spte) { | |
dc6c9a35 | 3688 | mm_inc_nr_pmds(mm); |
3212b535 SC |
3689 | get_page(virt_to_page(spte)); |
3690 | break; | |
3691 | } | |
3692 | } | |
3693 | } | |
3694 | ||
3695 | if (!spte) | |
3696 | goto out; | |
3697 | ||
cb900f41 KS |
3698 | ptl = huge_pte_lockptr(hstate_vma(vma), mm, spte); |
3699 | spin_lock(ptl); | |
dc6c9a35 | 3700 | if (pud_none(*pud)) { |
3212b535 SC |
3701 | pud_populate(mm, pud, |
3702 | (pmd_t *)((unsigned long)spte & PAGE_MASK)); | |
dc6c9a35 | 3703 | } else { |
3212b535 | 3704 | put_page(virt_to_page(spte)); |
dc6c9a35 KS |
3705 | mm_inc_nr_pmds(mm); |
3706 | } | |
cb900f41 | 3707 | spin_unlock(ptl); |
3212b535 SC |
3708 | out: |
3709 | pte = (pte_t *)pmd_alloc(mm, pud, addr); | |
83cde9e8 | 3710 | i_mmap_unlock_write(mapping); |
3212b535 SC |
3711 | return pte; |
3712 | } | |
3713 | ||
3714 | /* | |
3715 | * unmap huge page backed by shared pte. | |
3716 | * | |
3717 | * Hugetlb pte page is ref counted at the time of mapping. If pte is shared | |
3718 | * indicated by page_count > 1, unmap is achieved by clearing pud and | |
3719 | * decrementing the ref count. If count == 1, the pte page is not shared. | |
3720 | * | |
cb900f41 | 3721 | * called with page table lock held. |
3212b535 SC |
3722 | * |
3723 | * returns: 1 successfully unmapped a shared pte page | |
3724 | * 0 the underlying pte page is not shared, or it is the last user | |
3725 | */ | |
3726 | int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) | |
3727 | { | |
3728 | pgd_t *pgd = pgd_offset(mm, *addr); | |
3729 | pud_t *pud = pud_offset(pgd, *addr); | |
3730 | ||
3731 | BUG_ON(page_count(virt_to_page(ptep)) == 0); | |
3732 | if (page_count(virt_to_page(ptep)) == 1) | |
3733 | return 0; | |
3734 | ||
3735 | pud_clear(pud); | |
3736 | put_page(virt_to_page(ptep)); | |
dc6c9a35 | 3737 | mm_dec_nr_pmds(mm); |
3212b535 SC |
3738 | *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE; |
3739 | return 1; | |
3740 | } | |
9e5fc74c SC |
3741 | #define want_pmd_share() (1) |
3742 | #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ | |
3743 | pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) | |
3744 | { | |
3745 | return NULL; | |
3746 | } | |
3747 | #define want_pmd_share() (0) | |
3212b535 SC |
3748 | #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ |
3749 | ||
9e5fc74c SC |
3750 | #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB |
3751 | pte_t *huge_pte_alloc(struct mm_struct *mm, | |
3752 | unsigned long addr, unsigned long sz) | |
3753 | { | |
3754 | pgd_t *pgd; | |
3755 | pud_t *pud; | |
3756 | pte_t *pte = NULL; | |
3757 | ||
3758 | pgd = pgd_offset(mm, addr); | |
3759 | pud = pud_alloc(mm, pgd, addr); | |
3760 | if (pud) { | |
3761 | if (sz == PUD_SIZE) { | |
3762 | pte = (pte_t *)pud; | |
3763 | } else { | |
3764 | BUG_ON(sz != PMD_SIZE); | |
3765 | if (want_pmd_share() && pud_none(*pud)) | |
3766 | pte = huge_pmd_share(mm, addr, pud); | |
3767 | else | |
3768 | pte = (pte_t *)pmd_alloc(mm, pud, addr); | |
3769 | } | |
3770 | } | |
3771 | BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte)); | |
3772 | ||
3773 | return pte; | |
3774 | } | |
3775 | ||
3776 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) | |
3777 | { | |
3778 | pgd_t *pgd; | |
3779 | pud_t *pud; | |
3780 | pmd_t *pmd = NULL; | |
3781 | ||
3782 | pgd = pgd_offset(mm, addr); | |
3783 | if (pgd_present(*pgd)) { | |
3784 | pud = pud_offset(pgd, addr); | |
3785 | if (pud_present(*pud)) { | |
3786 | if (pud_huge(*pud)) | |
3787 | return (pte_t *)pud; | |
3788 | pmd = pmd_offset(pud, addr); | |
3789 | } | |
3790 | } | |
3791 | return (pte_t *) pmd; | |
3792 | } | |
3793 | ||
61f77eda NH |
3794 | #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ |
3795 | ||
3796 | /* | |
3797 | * These functions are overwritable if your architecture needs its own | |
3798 | * behavior. | |
3799 | */ | |
3800 | struct page * __weak | |
3801 | follow_huge_addr(struct mm_struct *mm, unsigned long address, | |
3802 | int write) | |
3803 | { | |
3804 | return ERR_PTR(-EINVAL); | |
3805 | } | |
3806 | ||
3807 | struct page * __weak | |
9e5fc74c | 3808 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
e66f17ff | 3809 | pmd_t *pmd, int flags) |
9e5fc74c | 3810 | { |
e66f17ff NH |
3811 | struct page *page = NULL; |
3812 | spinlock_t *ptl; | |
3813 | retry: | |
3814 | ptl = pmd_lockptr(mm, pmd); | |
3815 | spin_lock(ptl); | |
3816 | /* | |
3817 | * make sure that the address range covered by this pmd is not | |
3818 | * unmapped from other threads. | |
3819 | */ | |
3820 | if (!pmd_huge(*pmd)) | |
3821 | goto out; | |
3822 | if (pmd_present(*pmd)) { | |
97534127 | 3823 | page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT); |
e66f17ff NH |
3824 | if (flags & FOLL_GET) |
3825 | get_page(page); | |
3826 | } else { | |
3827 | if (is_hugetlb_entry_migration(huge_ptep_get((pte_t *)pmd))) { | |
3828 | spin_unlock(ptl); | |
3829 | __migration_entry_wait(mm, (pte_t *)pmd, ptl); | |
3830 | goto retry; | |
3831 | } | |
3832 | /* | |
3833 | * hwpoisoned entry is treated as no_page_table in | |
3834 | * follow_page_mask(). | |
3835 | */ | |
3836 | } | |
3837 | out: | |
3838 | spin_unlock(ptl); | |
9e5fc74c SC |
3839 | return page; |
3840 | } | |
3841 | ||
61f77eda | 3842 | struct page * __weak |
9e5fc74c | 3843 | follow_huge_pud(struct mm_struct *mm, unsigned long address, |
e66f17ff | 3844 | pud_t *pud, int flags) |
9e5fc74c | 3845 | { |
e66f17ff NH |
3846 | if (flags & FOLL_GET) |
3847 | return NULL; | |
9e5fc74c | 3848 | |
e66f17ff | 3849 | return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT); |
9e5fc74c SC |
3850 | } |
3851 | ||
d5bd9106 AK |
3852 | #ifdef CONFIG_MEMORY_FAILURE |
3853 | ||
6de2b1aa NH |
3854 | /* Should be called in hugetlb_lock */ |
3855 | static int is_hugepage_on_freelist(struct page *hpage) | |
3856 | { | |
3857 | struct page *page; | |
3858 | struct page *tmp; | |
3859 | struct hstate *h = page_hstate(hpage); | |
3860 | int nid = page_to_nid(hpage); | |
3861 | ||
3862 | list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru) | |
3863 | if (page == hpage) | |
3864 | return 1; | |
3865 | return 0; | |
3866 | } | |
3867 | ||
93f70f90 NH |
3868 | /* |
3869 | * This function is called from memory failure code. | |
3870 | * Assume the caller holds page lock of the head page. | |
3871 | */ | |
6de2b1aa | 3872 | int dequeue_hwpoisoned_huge_page(struct page *hpage) |
93f70f90 NH |
3873 | { |
3874 | struct hstate *h = page_hstate(hpage); | |
3875 | int nid = page_to_nid(hpage); | |
6de2b1aa | 3876 | int ret = -EBUSY; |
93f70f90 NH |
3877 | |
3878 | spin_lock(&hugetlb_lock); | |
6de2b1aa | 3879 | if (is_hugepage_on_freelist(hpage)) { |
56f2fb14 NH |
3880 | /* |
3881 | * Hwpoisoned hugepage isn't linked to activelist or freelist, | |
3882 | * but dangling hpage->lru can trigger list-debug warnings | |
3883 | * (this happens when we call unpoison_memory() on it), | |
3884 | * so let it point to itself with list_del_init(). | |
3885 | */ | |
3886 | list_del_init(&hpage->lru); | |
8c6c2ecb | 3887 | set_page_refcounted(hpage); |
6de2b1aa NH |
3888 | h->free_huge_pages--; |
3889 | h->free_huge_pages_node[nid]--; | |
3890 | ret = 0; | |
3891 | } | |
93f70f90 | 3892 | spin_unlock(&hugetlb_lock); |
6de2b1aa | 3893 | return ret; |
93f70f90 | 3894 | } |
6de2b1aa | 3895 | #endif |
31caf665 NH |
3896 | |
3897 | bool isolate_huge_page(struct page *page, struct list_head *list) | |
3898 | { | |
309381fe | 3899 | VM_BUG_ON_PAGE(!PageHead(page), page); |
31caf665 NH |
3900 | if (!get_page_unless_zero(page)) |
3901 | return false; | |
3902 | spin_lock(&hugetlb_lock); | |
3903 | list_move_tail(&page->lru, list); | |
3904 | spin_unlock(&hugetlb_lock); | |
3905 | return true; | |
3906 | } | |
3907 | ||
3908 | void putback_active_hugepage(struct page *page) | |
3909 | { | |
309381fe | 3910 | VM_BUG_ON_PAGE(!PageHead(page), page); |
31caf665 NH |
3911 | spin_lock(&hugetlb_lock); |
3912 | list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist); | |
3913 | spin_unlock(&hugetlb_lock); | |
3914 | put_page(page); | |
3915 | } | |
c8721bbb NH |
3916 | |
3917 | bool is_hugepage_active(struct page *page) | |
3918 | { | |
309381fe | 3919 | VM_BUG_ON_PAGE(!PageHuge(page), page); |
c8721bbb NH |
3920 | /* |
3921 | * This function can be called for a tail page because the caller, | |
3922 | * scan_movable_pages, scans through a given pfn-range which typically | |
3923 | * covers one memory block. In systems using gigantic hugepage (1GB | |
3924 | * for x86_64,) a hugepage is larger than a memory block, and we don't | |
3925 | * support migrating such large hugepages for now, so return false | |
3926 | * when called for tail pages. | |
3927 | */ | |
3928 | if (PageTail(page)) | |
3929 | return false; | |
3930 | /* | |
3931 | * Refcount of a hwpoisoned hugepages is 1, but they are not active, | |
3932 | * so we should return false for them. | |
3933 | */ | |
3934 | if (unlikely(PageHWPoison(page))) | |
3935 | return false; | |
3936 | return page_count(page) > 0; | |
3937 | } |