]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * Generic hugetlb support. | |
6d49e352 | 4 | * (C) Nadia Yvette Chambers, April 2004 |
1da177e4 | 5 | */ |
1da177e4 LT |
6 | #include <linux/list.h> |
7 | #include <linux/init.h> | |
1da177e4 | 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> |
97ad1087 | 19 | #include <linux/memblock.h> |
a3437870 | 20 | #include <linux/sysfs.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
63489f8e | 22 | #include <linux/mmdebug.h> |
174cd4b1 | 23 | #include <linux/sched/signal.h> |
0fe6e20b | 24 | #include <linux/rmap.h> |
c6247f72 | 25 | #include <linux/string_helpers.h> |
fd6a03ed NH |
26 | #include <linux/swap.h> |
27 | #include <linux/swapops.h> | |
8382d914 | 28 | #include <linux/jhash.h> |
98fa15f3 | 29 | #include <linux/numa.h> |
c77c0a8a | 30 | #include <linux/llist.h> |
d6606683 | 31 | |
63551ae0 DG |
32 | #include <asm/page.h> |
33 | #include <asm/pgtable.h> | |
24669e58 | 34 | #include <asm/tlb.h> |
63551ae0 | 35 | |
24669e58 | 36 | #include <linux/io.h> |
63551ae0 | 37 | #include <linux/hugetlb.h> |
9dd540e2 | 38 | #include <linux/hugetlb_cgroup.h> |
9a305230 | 39 | #include <linux/node.h> |
1a1aad8a | 40 | #include <linux/userfaultfd_k.h> |
ab5ac90a | 41 | #include <linux/page_owner.h> |
7835e98b | 42 | #include "internal.h" |
1da177e4 | 43 | |
c3f38a38 | 44 | int hugetlb_max_hstate __read_mostly; |
e5ff2159 AK |
45 | unsigned int default_hstate_idx; |
46 | struct hstate hstates[HUGE_MAX_HSTATE]; | |
641844f5 NH |
47 | /* |
48 | * Minimum page order among possible hugepage sizes, set to a proper value | |
49 | * at boot time. | |
50 | */ | |
51 | static unsigned int minimum_order __read_mostly = UINT_MAX; | |
e5ff2159 | 52 | |
53ba51d2 JT |
53 | __initdata LIST_HEAD(huge_boot_pages); |
54 | ||
e5ff2159 AK |
55 | /* for command line parsing */ |
56 | static struct hstate * __initdata parsed_hstate; | |
57 | static unsigned long __initdata default_hstate_max_huge_pages; | |
e11bfbfc | 58 | static unsigned long __initdata default_hstate_size; |
9fee021d | 59 | static bool __initdata parsed_valid_hugepagesz = true; |
e5ff2159 | 60 | |
3935baa9 | 61 | /* |
31caf665 NH |
62 | * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages, |
63 | * free_huge_pages, and surplus_huge_pages. | |
3935baa9 | 64 | */ |
c3f38a38 | 65 | DEFINE_SPINLOCK(hugetlb_lock); |
0bd0f9fb | 66 | |
8382d914 DB |
67 | /* |
68 | * Serializes faults on the same logical page. This is used to | |
69 | * prevent spurious OOMs when the hugepage pool is fully utilized. | |
70 | */ | |
71 | static int num_fault_mutexes; | |
c672c7f2 | 72 | struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp; |
8382d914 | 73 | |
7ca02d0a MK |
74 | /* Forward declaration */ |
75 | static int hugetlb_acct_memory(struct hstate *h, long delta); | |
76 | ||
90481622 DG |
77 | static inline void unlock_or_release_subpool(struct hugepage_subpool *spool) |
78 | { | |
79 | bool free = (spool->count == 0) && (spool->used_hpages == 0); | |
80 | ||
81 | spin_unlock(&spool->lock); | |
82 | ||
83 | /* If no pages are used, and no other handles to the subpool | |
7ca02d0a MK |
84 | * remain, give up any reservations mased on minimum size and |
85 | * free the subpool */ | |
86 | if (free) { | |
87 | if (spool->min_hpages != -1) | |
88 | hugetlb_acct_memory(spool->hstate, | |
89 | -spool->min_hpages); | |
90481622 | 90 | kfree(spool); |
7ca02d0a | 91 | } |
90481622 DG |
92 | } |
93 | ||
7ca02d0a MK |
94 | struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages, |
95 | long min_hpages) | |
90481622 DG |
96 | { |
97 | struct hugepage_subpool *spool; | |
98 | ||
c6a91820 | 99 | spool = kzalloc(sizeof(*spool), GFP_KERNEL); |
90481622 DG |
100 | if (!spool) |
101 | return NULL; | |
102 | ||
103 | spin_lock_init(&spool->lock); | |
104 | spool->count = 1; | |
7ca02d0a MK |
105 | spool->max_hpages = max_hpages; |
106 | spool->hstate = h; | |
107 | spool->min_hpages = min_hpages; | |
108 | ||
109 | if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) { | |
110 | kfree(spool); | |
111 | return NULL; | |
112 | } | |
113 | spool->rsv_hpages = min_hpages; | |
90481622 DG |
114 | |
115 | return spool; | |
116 | } | |
117 | ||
118 | void hugepage_put_subpool(struct hugepage_subpool *spool) | |
119 | { | |
120 | spin_lock(&spool->lock); | |
121 | BUG_ON(!spool->count); | |
122 | spool->count--; | |
123 | unlock_or_release_subpool(spool); | |
124 | } | |
125 | ||
1c5ecae3 MK |
126 | /* |
127 | * Subpool accounting for allocating and reserving pages. | |
128 | * Return -ENOMEM if there are not enough resources to satisfy the | |
129 | * the request. Otherwise, return the number of pages by which the | |
130 | * global pools must be adjusted (upward). The returned value may | |
131 | * only be different than the passed value (delta) in the case where | |
132 | * a subpool minimum size must be manitained. | |
133 | */ | |
134 | static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, | |
90481622 DG |
135 | long delta) |
136 | { | |
1c5ecae3 | 137 | long ret = delta; |
90481622 DG |
138 | |
139 | if (!spool) | |
1c5ecae3 | 140 | return ret; |
90481622 DG |
141 | |
142 | spin_lock(&spool->lock); | |
1c5ecae3 MK |
143 | |
144 | if (spool->max_hpages != -1) { /* maximum size accounting */ | |
145 | if ((spool->used_hpages + delta) <= spool->max_hpages) | |
146 | spool->used_hpages += delta; | |
147 | else { | |
148 | ret = -ENOMEM; | |
149 | goto unlock_ret; | |
150 | } | |
90481622 | 151 | } |
90481622 | 152 | |
09a95e29 MK |
153 | /* minimum size accounting */ |
154 | if (spool->min_hpages != -1 && spool->rsv_hpages) { | |
1c5ecae3 MK |
155 | if (delta > spool->rsv_hpages) { |
156 | /* | |
157 | * Asking for more reserves than those already taken on | |
158 | * behalf of subpool. Return difference. | |
159 | */ | |
160 | ret = delta - spool->rsv_hpages; | |
161 | spool->rsv_hpages = 0; | |
162 | } else { | |
163 | ret = 0; /* reserves already accounted for */ | |
164 | spool->rsv_hpages -= delta; | |
165 | } | |
166 | } | |
167 | ||
168 | unlock_ret: | |
169 | spin_unlock(&spool->lock); | |
90481622 DG |
170 | return ret; |
171 | } | |
172 | ||
1c5ecae3 MK |
173 | /* |
174 | * Subpool accounting for freeing and unreserving pages. | |
175 | * Return the number of global page reservations that must be dropped. | |
176 | * The return value may only be different than the passed value (delta) | |
177 | * in the case where a subpool minimum size must be maintained. | |
178 | */ | |
179 | static long hugepage_subpool_put_pages(struct hugepage_subpool *spool, | |
90481622 DG |
180 | long delta) |
181 | { | |
1c5ecae3 MK |
182 | long ret = delta; |
183 | ||
90481622 | 184 | if (!spool) |
1c5ecae3 | 185 | return delta; |
90481622 DG |
186 | |
187 | spin_lock(&spool->lock); | |
1c5ecae3 MK |
188 | |
189 | if (spool->max_hpages != -1) /* maximum size accounting */ | |
190 | spool->used_hpages -= delta; | |
191 | ||
09a95e29 MK |
192 | /* minimum size accounting */ |
193 | if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) { | |
1c5ecae3 MK |
194 | if (spool->rsv_hpages + delta <= spool->min_hpages) |
195 | ret = 0; | |
196 | else | |
197 | ret = spool->rsv_hpages + delta - spool->min_hpages; | |
198 | ||
199 | spool->rsv_hpages += delta; | |
200 | if (spool->rsv_hpages > spool->min_hpages) | |
201 | spool->rsv_hpages = spool->min_hpages; | |
202 | } | |
203 | ||
204 | /* | |
205 | * If hugetlbfs_put_super couldn't free spool due to an outstanding | |
206 | * quota reference, free it now. | |
207 | */ | |
90481622 | 208 | unlock_or_release_subpool(spool); |
1c5ecae3 MK |
209 | |
210 | return ret; | |
90481622 DG |
211 | } |
212 | ||
213 | static inline struct hugepage_subpool *subpool_inode(struct inode *inode) | |
214 | { | |
215 | return HUGETLBFS_SB(inode->i_sb)->spool; | |
216 | } | |
217 | ||
218 | static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma) | |
219 | { | |
496ad9aa | 220 | return subpool_inode(file_inode(vma->vm_file)); |
90481622 DG |
221 | } |
222 | ||
96822904 AW |
223 | /* |
224 | * Region tracking -- allows tracking of reservations and instantiated pages | |
225 | * across the pages in a mapping. | |
84afd99b | 226 | * |
1dd308a7 MK |
227 | * The region data structures are embedded into a resv_map and protected |
228 | * by a resv_map's lock. The set of regions within the resv_map represent | |
229 | * reservations for huge pages, or huge pages that have already been | |
230 | * instantiated within the map. The from and to elements are huge page | |
231 | * indicies into the associated mapping. from indicates the starting index | |
232 | * of the region. to represents the first index past the end of the region. | |
233 | * | |
234 | * For example, a file region structure with from == 0 and to == 4 represents | |
235 | * four huge pages in a mapping. It is important to note that the to element | |
236 | * represents the first element past the end of the region. This is used in | |
237 | * arithmetic as 4(to) - 0(from) = 4 huge pages in the region. | |
238 | * | |
239 | * Interval notation of the form [from, to) will be used to indicate that | |
240 | * the endpoint from is inclusive and to is exclusive. | |
96822904 AW |
241 | */ |
242 | struct file_region { | |
243 | struct list_head link; | |
244 | long from; | |
245 | long to; | |
246 | }; | |
247 | ||
0db9d74e MA |
248 | /* Helper that removes a struct file_region from the resv_map cache and returns |
249 | * it for use. | |
250 | */ | |
251 | static struct file_region * | |
252 | get_file_region_entry_from_cache(struct resv_map *resv, long from, long to) | |
253 | { | |
254 | struct file_region *nrg = NULL; | |
255 | ||
256 | VM_BUG_ON(resv->region_cache_count <= 0); | |
257 | ||
258 | resv->region_cache_count--; | |
259 | nrg = list_first_entry(&resv->region_cache, struct file_region, link); | |
260 | VM_BUG_ON(!nrg); | |
261 | list_del(&nrg->link); | |
262 | ||
263 | nrg->from = from; | |
264 | nrg->to = to; | |
265 | ||
266 | return nrg; | |
267 | } | |
268 | ||
d75c6af9 MA |
269 | /* Must be called with resv->lock held. Calling this with count_only == true |
270 | * will count the number of pages to be added but will not modify the linked | |
0db9d74e MA |
271 | * list. If regions_needed != NULL and count_only == true, then regions_needed |
272 | * will indicate the number of file_regions needed in the cache to carry out to | |
273 | * add the regions for this range. | |
d75c6af9 MA |
274 | */ |
275 | static long add_reservation_in_range(struct resv_map *resv, long f, long t, | |
0db9d74e | 276 | long *regions_needed, bool count_only) |
d75c6af9 | 277 | { |
0db9d74e | 278 | long add = 0; |
d75c6af9 | 279 | struct list_head *head = &resv->regions; |
0db9d74e | 280 | long last_accounted_offset = f; |
d75c6af9 MA |
281 | struct file_region *rg = NULL, *trg = NULL, *nrg = NULL; |
282 | ||
0db9d74e MA |
283 | if (regions_needed) |
284 | *regions_needed = 0; | |
d75c6af9 | 285 | |
0db9d74e MA |
286 | /* In this loop, we essentially handle an entry for the range |
287 | * [last_accounted_offset, rg->from), at every iteration, with some | |
288 | * bounds checking. | |
289 | */ | |
290 | list_for_each_entry_safe(rg, trg, head, link) { | |
291 | /* Skip irrelevant regions that start before our range. */ | |
292 | if (rg->from < f) { | |
293 | /* If this region ends after the last accounted offset, | |
294 | * then we need to update last_accounted_offset. | |
295 | */ | |
296 | if (rg->to > last_accounted_offset) | |
297 | last_accounted_offset = rg->to; | |
298 | continue; | |
299 | } | |
d75c6af9 | 300 | |
0db9d74e MA |
301 | /* When we find a region that starts beyond our range, we've |
302 | * finished. | |
303 | */ | |
d75c6af9 MA |
304 | if (rg->from > t) |
305 | break; | |
306 | ||
0db9d74e MA |
307 | /* Add an entry for last_accounted_offset -> rg->from, and |
308 | * update last_accounted_offset. | |
309 | */ | |
310 | if (rg->from > last_accounted_offset) { | |
311 | add += rg->from - last_accounted_offset; | |
312 | if (!count_only) { | |
313 | nrg = get_file_region_entry_from_cache( | |
314 | resv, last_accounted_offset, rg->from); | |
315 | list_add(&nrg->link, rg->link.prev); | |
316 | } else if (regions_needed) | |
317 | *regions_needed += 1; | |
318 | } | |
319 | ||
320 | last_accounted_offset = rg->to; | |
321 | } | |
322 | ||
323 | /* Handle the case where our range extends beyond | |
324 | * last_accounted_offset. | |
325 | */ | |
326 | if (last_accounted_offset < t) { | |
327 | add += t - last_accounted_offset; | |
328 | if (!count_only) { | |
329 | nrg = get_file_region_entry_from_cache( | |
330 | resv, last_accounted_offset, t); | |
331 | list_add(&nrg->link, rg->link.prev); | |
332 | } else if (regions_needed) | |
333 | *regions_needed += 1; | |
334 | } | |
335 | ||
336 | VM_BUG_ON(add < 0); | |
337 | return add; | |
338 | } | |
339 | ||
340 | /* Must be called with resv->lock acquired. Will drop lock to allocate entries. | |
341 | */ | |
342 | static int allocate_file_region_entries(struct resv_map *resv, | |
343 | int regions_needed) | |
344 | __must_hold(&resv->lock) | |
345 | { | |
346 | struct list_head allocated_regions; | |
347 | int to_allocate = 0, i = 0; | |
348 | struct file_region *trg = NULL, *rg = NULL; | |
349 | ||
350 | VM_BUG_ON(regions_needed < 0); | |
351 | ||
352 | INIT_LIST_HEAD(&allocated_regions); | |
353 | ||
354 | /* | |
355 | * Check for sufficient descriptors in the cache to accommodate | |
356 | * the number of in progress add operations plus regions_needed. | |
357 | * | |
358 | * This is a while loop because when we drop the lock, some other call | |
359 | * to region_add or region_del may have consumed some region_entries, | |
360 | * so we keep looping here until we finally have enough entries for | |
361 | * (adds_in_progress + regions_needed). | |
362 | */ | |
363 | while (resv->region_cache_count < | |
364 | (resv->adds_in_progress + regions_needed)) { | |
365 | to_allocate = resv->adds_in_progress + regions_needed - | |
366 | resv->region_cache_count; | |
367 | ||
368 | /* At this point, we should have enough entries in the cache | |
369 | * for all the existings adds_in_progress. We should only be | |
370 | * needing to allocate for regions_needed. | |
d75c6af9 | 371 | */ |
0db9d74e MA |
372 | VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress); |
373 | ||
374 | spin_unlock(&resv->lock); | |
375 | for (i = 0; i < to_allocate; i++) { | |
376 | trg = kmalloc(sizeof(*trg), GFP_KERNEL); | |
377 | if (!trg) | |
378 | goto out_of_memory; | |
379 | list_add(&trg->link, &allocated_regions); | |
d75c6af9 | 380 | } |
d75c6af9 | 381 | |
0db9d74e MA |
382 | spin_lock(&resv->lock); |
383 | ||
384 | list_for_each_entry_safe(rg, trg, &allocated_regions, link) { | |
d75c6af9 | 385 | list_del(&rg->link); |
0db9d74e MA |
386 | list_add(&rg->link, &resv->region_cache); |
387 | resv->region_cache_count++; | |
d75c6af9 MA |
388 | } |
389 | } | |
390 | ||
0db9d74e | 391 | return 0; |
d75c6af9 | 392 | |
0db9d74e MA |
393 | out_of_memory: |
394 | list_for_each_entry_safe(rg, trg, &allocated_regions, link) { | |
395 | list_del(&rg->link); | |
396 | kfree(rg); | |
397 | } | |
398 | return -ENOMEM; | |
d75c6af9 MA |
399 | } |
400 | ||
1dd308a7 MK |
401 | /* |
402 | * Add the huge page range represented by [f, t) to the reserve | |
0db9d74e MA |
403 | * map. Regions will be taken from the cache to fill in this range. |
404 | * Sufficient regions should exist in the cache due to the previous | |
405 | * call to region_chg with the same range, but in some cases the cache will not | |
406 | * have sufficient entries due to races with other code doing region_add or | |
407 | * region_del. The extra needed entries will be allocated. | |
cf3ad20b | 408 | * |
0db9d74e MA |
409 | * regions_needed is the out value provided by a previous call to region_chg. |
410 | * | |
411 | * Return the number of new huge pages added to the map. This number is greater | |
412 | * than or equal to zero. If file_region entries needed to be allocated for | |
413 | * this operation and we were not able to allocate, it ruturns -ENOMEM. | |
414 | * region_add of regions of length 1 never allocate file_regions and cannot | |
415 | * fail; region_chg will always allocate at least 1 entry and a region_add for | |
416 | * 1 page will only require at most 1 entry. | |
1dd308a7 | 417 | */ |
0db9d74e MA |
418 | static long region_add(struct resv_map *resv, long f, long t, |
419 | long in_regions_needed) | |
96822904 | 420 | { |
0db9d74e | 421 | long add = 0, actual_regions_needed = 0; |
96822904 | 422 | |
7b24d861 | 423 | spin_lock(&resv->lock); |
0db9d74e MA |
424 | retry: |
425 | ||
426 | /* Count how many regions are actually needed to execute this add. */ | |
427 | add_reservation_in_range(resv, f, t, &actual_regions_needed, true); | |
96822904 | 428 | |
5e911373 | 429 | /* |
0db9d74e MA |
430 | * Check for sufficient descriptors in the cache to accommodate |
431 | * this add operation. Note that actual_regions_needed may be greater | |
432 | * than in_regions_needed, as the resv_map may have been modified since | |
433 | * the region_chg call. In this case, we need to make sure that we | |
434 | * allocate extra entries, such that we have enough for all the | |
435 | * existing adds_in_progress, plus the excess needed for this | |
436 | * operation. | |
5e911373 | 437 | */ |
0db9d74e MA |
438 | if (actual_regions_needed > in_regions_needed && |
439 | resv->region_cache_count < | |
440 | resv->adds_in_progress + | |
441 | (actual_regions_needed - in_regions_needed)) { | |
442 | /* region_add operation of range 1 should never need to | |
443 | * allocate file_region entries. | |
444 | */ | |
445 | VM_BUG_ON(t - f <= 1); | |
5e911373 | 446 | |
0db9d74e MA |
447 | if (allocate_file_region_entries( |
448 | resv, actual_regions_needed - in_regions_needed)) { | |
449 | return -ENOMEM; | |
450 | } | |
5e911373 | 451 | |
0db9d74e | 452 | goto retry; |
5e911373 MK |
453 | } |
454 | ||
0db9d74e MA |
455 | add = add_reservation_in_range(resv, f, t, NULL, false); |
456 | ||
457 | resv->adds_in_progress -= in_regions_needed; | |
cf3ad20b | 458 | |
7b24d861 | 459 | spin_unlock(&resv->lock); |
cf3ad20b MK |
460 | VM_BUG_ON(add < 0); |
461 | return add; | |
96822904 AW |
462 | } |
463 | ||
1dd308a7 MK |
464 | /* |
465 | * Examine the existing reserve map and determine how many | |
466 | * huge pages in the specified range [f, t) are NOT currently | |
467 | * represented. This routine is called before a subsequent | |
468 | * call to region_add that will actually modify the reserve | |
469 | * map to add the specified range [f, t). region_chg does | |
470 | * not change the number of huge pages represented by the | |
0db9d74e MA |
471 | * map. A number of new file_region structures is added to the cache as a |
472 | * placeholder, for the subsequent region_add call to use. At least 1 | |
473 | * file_region structure is added. | |
474 | * | |
475 | * out_regions_needed is the number of regions added to the | |
476 | * resv->adds_in_progress. This value needs to be provided to a follow up call | |
477 | * to region_add or region_abort for proper accounting. | |
5e911373 MK |
478 | * |
479 | * Returns the number of huge pages that need to be added to the existing | |
480 | * reservation map for the range [f, t). This number is greater or equal to | |
481 | * zero. -ENOMEM is returned if a new file_region structure or cache entry | |
482 | * is needed and can not be allocated. | |
1dd308a7 | 483 | */ |
0db9d74e MA |
484 | static long region_chg(struct resv_map *resv, long f, long t, |
485 | long *out_regions_needed) | |
96822904 | 486 | { |
96822904 AW |
487 | long chg = 0; |
488 | ||
7b24d861 | 489 | spin_lock(&resv->lock); |
5e911373 | 490 | |
0db9d74e MA |
491 | /* Count how many hugepages in this range are NOT respresented. */ |
492 | chg = add_reservation_in_range(resv, f, t, out_regions_needed, true); | |
5e911373 | 493 | |
0db9d74e MA |
494 | if (*out_regions_needed == 0) |
495 | *out_regions_needed = 1; | |
5e911373 | 496 | |
0db9d74e MA |
497 | if (allocate_file_region_entries(resv, *out_regions_needed)) |
498 | return -ENOMEM; | |
5e911373 | 499 | |
0db9d74e | 500 | resv->adds_in_progress += *out_regions_needed; |
7b24d861 | 501 | |
7b24d861 | 502 | spin_unlock(&resv->lock); |
96822904 AW |
503 | return chg; |
504 | } | |
505 | ||
5e911373 MK |
506 | /* |
507 | * Abort the in progress add operation. The adds_in_progress field | |
508 | * of the resv_map keeps track of the operations in progress between | |
509 | * calls to region_chg and region_add. Operations are sometimes | |
510 | * aborted after the call to region_chg. In such cases, region_abort | |
0db9d74e MA |
511 | * is called to decrement the adds_in_progress counter. regions_needed |
512 | * is the value returned by the region_chg call, it is used to decrement | |
513 | * the adds_in_progress counter. | |
5e911373 MK |
514 | * |
515 | * NOTE: The range arguments [f, t) are not needed or used in this | |
516 | * routine. They are kept to make reading the calling code easier as | |
517 | * arguments will match the associated region_chg call. | |
518 | */ | |
0db9d74e MA |
519 | static void region_abort(struct resv_map *resv, long f, long t, |
520 | long regions_needed) | |
5e911373 MK |
521 | { |
522 | spin_lock(&resv->lock); | |
523 | VM_BUG_ON(!resv->region_cache_count); | |
0db9d74e | 524 | resv->adds_in_progress -= regions_needed; |
5e911373 MK |
525 | spin_unlock(&resv->lock); |
526 | } | |
527 | ||
1dd308a7 | 528 | /* |
feba16e2 MK |
529 | * Delete the specified range [f, t) from the reserve map. If the |
530 | * t parameter is LONG_MAX, this indicates that ALL regions after f | |
531 | * should be deleted. Locate the regions which intersect [f, t) | |
532 | * and either trim, delete or split the existing regions. | |
533 | * | |
534 | * Returns the number of huge pages deleted from the reserve map. | |
535 | * In the normal case, the return value is zero or more. In the | |
536 | * case where a region must be split, a new region descriptor must | |
537 | * be allocated. If the allocation fails, -ENOMEM will be returned. | |
538 | * NOTE: If the parameter t == LONG_MAX, then we will never split | |
539 | * a region and possibly return -ENOMEM. Callers specifying | |
540 | * t == LONG_MAX do not need to check for -ENOMEM error. | |
1dd308a7 | 541 | */ |
feba16e2 | 542 | static long region_del(struct resv_map *resv, long f, long t) |
96822904 | 543 | { |
1406ec9b | 544 | struct list_head *head = &resv->regions; |
96822904 | 545 | struct file_region *rg, *trg; |
feba16e2 MK |
546 | struct file_region *nrg = NULL; |
547 | long del = 0; | |
96822904 | 548 | |
feba16e2 | 549 | retry: |
7b24d861 | 550 | spin_lock(&resv->lock); |
feba16e2 | 551 | list_for_each_entry_safe(rg, trg, head, link) { |
dbe409e4 MK |
552 | /* |
553 | * Skip regions before the range to be deleted. file_region | |
554 | * ranges are normally of the form [from, to). However, there | |
555 | * may be a "placeholder" entry in the map which is of the form | |
556 | * (from, to) with from == to. Check for placeholder entries | |
557 | * at the beginning of the range to be deleted. | |
558 | */ | |
559 | if (rg->to <= f && (rg->to != rg->from || rg->to != f)) | |
feba16e2 | 560 | continue; |
dbe409e4 | 561 | |
feba16e2 | 562 | if (rg->from >= t) |
96822904 | 563 | break; |
96822904 | 564 | |
feba16e2 MK |
565 | if (f > rg->from && t < rg->to) { /* Must split region */ |
566 | /* | |
567 | * Check for an entry in the cache before dropping | |
568 | * lock and attempting allocation. | |
569 | */ | |
570 | if (!nrg && | |
571 | resv->region_cache_count > resv->adds_in_progress) { | |
572 | nrg = list_first_entry(&resv->region_cache, | |
573 | struct file_region, | |
574 | link); | |
575 | list_del(&nrg->link); | |
576 | resv->region_cache_count--; | |
577 | } | |
96822904 | 578 | |
feba16e2 MK |
579 | if (!nrg) { |
580 | spin_unlock(&resv->lock); | |
581 | nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); | |
582 | if (!nrg) | |
583 | return -ENOMEM; | |
584 | goto retry; | |
585 | } | |
586 | ||
587 | del += t - f; | |
588 | ||
589 | /* New entry for end of split region */ | |
590 | nrg->from = t; | |
591 | nrg->to = rg->to; | |
592 | INIT_LIST_HEAD(&nrg->link); | |
593 | ||
594 | /* Original entry is trimmed */ | |
595 | rg->to = f; | |
596 | ||
597 | list_add(&nrg->link, &rg->link); | |
598 | nrg = NULL; | |
96822904 | 599 | break; |
feba16e2 MK |
600 | } |
601 | ||
602 | if (f <= rg->from && t >= rg->to) { /* Remove entire region */ | |
603 | del += rg->to - rg->from; | |
604 | list_del(&rg->link); | |
605 | kfree(rg); | |
606 | continue; | |
607 | } | |
608 | ||
609 | if (f <= rg->from) { /* Trim beginning of region */ | |
610 | del += t - rg->from; | |
611 | rg->from = t; | |
612 | } else { /* Trim end of region */ | |
613 | del += rg->to - f; | |
614 | rg->to = f; | |
615 | } | |
96822904 | 616 | } |
7b24d861 | 617 | |
7b24d861 | 618 | spin_unlock(&resv->lock); |
feba16e2 MK |
619 | kfree(nrg); |
620 | return del; | |
96822904 AW |
621 | } |
622 | ||
b5cec28d MK |
623 | /* |
624 | * A rare out of memory error was encountered which prevented removal of | |
625 | * the reserve map region for a page. The huge page itself was free'ed | |
626 | * and removed from the page cache. This routine will adjust the subpool | |
627 | * usage count, and the global reserve count if needed. By incrementing | |
628 | * these counts, the reserve map entry which could not be deleted will | |
629 | * appear as a "reserved" entry instead of simply dangling with incorrect | |
630 | * counts. | |
631 | */ | |
72e2936c | 632 | void hugetlb_fix_reserve_counts(struct inode *inode) |
b5cec28d MK |
633 | { |
634 | struct hugepage_subpool *spool = subpool_inode(inode); | |
635 | long rsv_adjust; | |
636 | ||
637 | rsv_adjust = hugepage_subpool_get_pages(spool, 1); | |
72e2936c | 638 | if (rsv_adjust) { |
b5cec28d MK |
639 | struct hstate *h = hstate_inode(inode); |
640 | ||
641 | hugetlb_acct_memory(h, 1); | |
642 | } | |
643 | } | |
644 | ||
1dd308a7 MK |
645 | /* |
646 | * Count and return the number of huge pages in the reserve map | |
647 | * that intersect with the range [f, t). | |
648 | */ | |
1406ec9b | 649 | static long region_count(struct resv_map *resv, long f, long t) |
84afd99b | 650 | { |
1406ec9b | 651 | struct list_head *head = &resv->regions; |
84afd99b AW |
652 | struct file_region *rg; |
653 | long chg = 0; | |
654 | ||
7b24d861 | 655 | spin_lock(&resv->lock); |
84afd99b AW |
656 | /* Locate each segment we overlap with, and count that overlap. */ |
657 | list_for_each_entry(rg, head, link) { | |
f2135a4a WSH |
658 | long seg_from; |
659 | long seg_to; | |
84afd99b AW |
660 | |
661 | if (rg->to <= f) | |
662 | continue; | |
663 | if (rg->from >= t) | |
664 | break; | |
665 | ||
666 | seg_from = max(rg->from, f); | |
667 | seg_to = min(rg->to, t); | |
668 | ||
669 | chg += seg_to - seg_from; | |
670 | } | |
7b24d861 | 671 | spin_unlock(&resv->lock); |
84afd99b AW |
672 | |
673 | return chg; | |
674 | } | |
675 | ||
e7c4b0bf AW |
676 | /* |
677 | * Convert the address within this vma to the page offset within | |
678 | * the mapping, in pagecache page units; huge pages here. | |
679 | */ | |
a5516438 AK |
680 | static pgoff_t vma_hugecache_offset(struct hstate *h, |
681 | struct vm_area_struct *vma, unsigned long address) | |
e7c4b0bf | 682 | { |
a5516438 AK |
683 | return ((address - vma->vm_start) >> huge_page_shift(h)) + |
684 | (vma->vm_pgoff >> huge_page_order(h)); | |
e7c4b0bf AW |
685 | } |
686 | ||
0fe6e20b NH |
687 | pgoff_t linear_hugepage_index(struct vm_area_struct *vma, |
688 | unsigned long address) | |
689 | { | |
690 | return vma_hugecache_offset(hstate_vma(vma), vma, address); | |
691 | } | |
dee41079 | 692 | EXPORT_SYMBOL_GPL(linear_hugepage_index); |
0fe6e20b | 693 | |
08fba699 MG |
694 | /* |
695 | * Return the size of the pages allocated when backing a VMA. In the majority | |
696 | * cases this will be same size as used by the page table entries. | |
697 | */ | |
698 | unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) | |
699 | { | |
05ea8860 DW |
700 | if (vma->vm_ops && vma->vm_ops->pagesize) |
701 | return vma->vm_ops->pagesize(vma); | |
702 | return PAGE_SIZE; | |
08fba699 | 703 | } |
f340ca0f | 704 | EXPORT_SYMBOL_GPL(vma_kernel_pagesize); |
08fba699 | 705 | |
3340289d MG |
706 | /* |
707 | * Return the page size being used by the MMU to back a VMA. In the majority | |
708 | * of cases, the page size used by the kernel matches the MMU size. On | |
09135cc5 DW |
709 | * architectures where it differs, an architecture-specific 'strong' |
710 | * version of this symbol is required. | |
3340289d | 711 | */ |
09135cc5 | 712 | __weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) |
3340289d MG |
713 | { |
714 | return vma_kernel_pagesize(vma); | |
715 | } | |
3340289d | 716 | |
84afd99b AW |
717 | /* |
718 | * Flags for MAP_PRIVATE reservations. These are stored in the bottom | |
719 | * bits of the reservation map pointer, which are always clear due to | |
720 | * alignment. | |
721 | */ | |
722 | #define HPAGE_RESV_OWNER (1UL << 0) | |
723 | #define HPAGE_RESV_UNMAPPED (1UL << 1) | |
04f2cbe3 | 724 | #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) |
84afd99b | 725 | |
a1e78772 MG |
726 | /* |
727 | * These helpers are used to track how many pages are reserved for | |
728 | * faults in a MAP_PRIVATE mapping. Only the process that called mmap() | |
729 | * is guaranteed to have their future faults succeed. | |
730 | * | |
731 | * With the exception of reset_vma_resv_huge_pages() which is called at fork(), | |
732 | * the reserve counters are updated with the hugetlb_lock held. It is safe | |
733 | * to reset the VMA at fork() time as it is not in use yet and there is no | |
734 | * chance of the global counters getting corrupted as a result of the values. | |
84afd99b AW |
735 | * |
736 | * The private mapping reservation is represented in a subtly different | |
737 | * manner to a shared mapping. A shared mapping has a region map associated | |
738 | * with the underlying file, this region map represents the backing file | |
739 | * pages which have ever had a reservation assigned which this persists even | |
740 | * after the page is instantiated. A private mapping has a region map | |
741 | * associated with the original mmap which is attached to all VMAs which | |
742 | * reference it, this region map represents those offsets which have consumed | |
743 | * reservation ie. where pages have been instantiated. | |
a1e78772 | 744 | */ |
e7c4b0bf AW |
745 | static unsigned long get_vma_private_data(struct vm_area_struct *vma) |
746 | { | |
747 | return (unsigned long)vma->vm_private_data; | |
748 | } | |
749 | ||
750 | static void set_vma_private_data(struct vm_area_struct *vma, | |
751 | unsigned long value) | |
752 | { | |
753 | vma->vm_private_data = (void *)value; | |
754 | } | |
755 | ||
e9fe92ae MA |
756 | static void |
757 | resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map, | |
758 | struct hugetlb_cgroup *h_cg, | |
759 | struct hstate *h) | |
760 | { | |
761 | #ifdef CONFIG_CGROUP_HUGETLB | |
762 | if (!h_cg || !h) { | |
763 | resv_map->reservation_counter = NULL; | |
764 | resv_map->pages_per_hpage = 0; | |
765 | resv_map->css = NULL; | |
766 | } else { | |
767 | resv_map->reservation_counter = | |
768 | &h_cg->rsvd_hugepage[hstate_index(h)]; | |
769 | resv_map->pages_per_hpage = pages_per_huge_page(h); | |
770 | resv_map->css = &h_cg->css; | |
771 | } | |
772 | #endif | |
773 | } | |
774 | ||
9119a41e | 775 | struct resv_map *resv_map_alloc(void) |
84afd99b AW |
776 | { |
777 | struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); | |
5e911373 MK |
778 | struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL); |
779 | ||
780 | if (!resv_map || !rg) { | |
781 | kfree(resv_map); | |
782 | kfree(rg); | |
84afd99b | 783 | return NULL; |
5e911373 | 784 | } |
84afd99b AW |
785 | |
786 | kref_init(&resv_map->refs); | |
7b24d861 | 787 | spin_lock_init(&resv_map->lock); |
84afd99b AW |
788 | INIT_LIST_HEAD(&resv_map->regions); |
789 | ||
5e911373 | 790 | resv_map->adds_in_progress = 0; |
e9fe92ae MA |
791 | /* |
792 | * Initialize these to 0. On shared mappings, 0's here indicate these | |
793 | * fields don't do cgroup accounting. On private mappings, these will be | |
794 | * re-initialized to the proper values, to indicate that hugetlb cgroup | |
795 | * reservations are to be un-charged from here. | |
796 | */ | |
797 | resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL); | |
5e911373 MK |
798 | |
799 | INIT_LIST_HEAD(&resv_map->region_cache); | |
800 | list_add(&rg->link, &resv_map->region_cache); | |
801 | resv_map->region_cache_count = 1; | |
802 | ||
84afd99b AW |
803 | return resv_map; |
804 | } | |
805 | ||
9119a41e | 806 | void resv_map_release(struct kref *ref) |
84afd99b AW |
807 | { |
808 | struct resv_map *resv_map = container_of(ref, struct resv_map, refs); | |
5e911373 MK |
809 | struct list_head *head = &resv_map->region_cache; |
810 | struct file_region *rg, *trg; | |
84afd99b AW |
811 | |
812 | /* Clear out any active regions before we release the map. */ | |
feba16e2 | 813 | region_del(resv_map, 0, LONG_MAX); |
5e911373 MK |
814 | |
815 | /* ... and any entries left in the cache */ | |
816 | list_for_each_entry_safe(rg, trg, head, link) { | |
817 | list_del(&rg->link); | |
818 | kfree(rg); | |
819 | } | |
820 | ||
821 | VM_BUG_ON(resv_map->adds_in_progress); | |
822 | ||
84afd99b AW |
823 | kfree(resv_map); |
824 | } | |
825 | ||
4e35f483 JK |
826 | static inline struct resv_map *inode_resv_map(struct inode *inode) |
827 | { | |
f27a5136 MK |
828 | /* |
829 | * At inode evict time, i_mapping may not point to the original | |
830 | * address space within the inode. This original address space | |
831 | * contains the pointer to the resv_map. So, always use the | |
832 | * address space embedded within the inode. | |
833 | * The VERY common case is inode->mapping == &inode->i_data but, | |
834 | * this may not be true for device special inodes. | |
835 | */ | |
836 | return (struct resv_map *)(&inode->i_data)->private_data; | |
4e35f483 JK |
837 | } |
838 | ||
84afd99b | 839 | static struct resv_map *vma_resv_map(struct vm_area_struct *vma) |
a1e78772 | 840 | { |
81d1b09c | 841 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
4e35f483 JK |
842 | if (vma->vm_flags & VM_MAYSHARE) { |
843 | struct address_space *mapping = vma->vm_file->f_mapping; | |
844 | struct inode *inode = mapping->host; | |
845 | ||
846 | return inode_resv_map(inode); | |
847 | ||
848 | } else { | |
84afd99b AW |
849 | return (struct resv_map *)(get_vma_private_data(vma) & |
850 | ~HPAGE_RESV_MASK); | |
4e35f483 | 851 | } |
a1e78772 MG |
852 | } |
853 | ||
84afd99b | 854 | static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) |
a1e78772 | 855 | { |
81d1b09c SL |
856 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
857 | VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); | |
a1e78772 | 858 | |
84afd99b AW |
859 | set_vma_private_data(vma, (get_vma_private_data(vma) & |
860 | HPAGE_RESV_MASK) | (unsigned long)map); | |
04f2cbe3 MG |
861 | } |
862 | ||
863 | static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) | |
864 | { | |
81d1b09c SL |
865 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
866 | VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); | |
e7c4b0bf AW |
867 | |
868 | set_vma_private_data(vma, get_vma_private_data(vma) | flags); | |
04f2cbe3 MG |
869 | } |
870 | ||
871 | static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) | |
872 | { | |
81d1b09c | 873 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
e7c4b0bf AW |
874 | |
875 | return (get_vma_private_data(vma) & flag) != 0; | |
a1e78772 MG |
876 | } |
877 | ||
04f2cbe3 | 878 | /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ |
a1e78772 MG |
879 | void reset_vma_resv_huge_pages(struct vm_area_struct *vma) |
880 | { | |
81d1b09c | 881 | VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); |
f83a275d | 882 | if (!(vma->vm_flags & VM_MAYSHARE)) |
a1e78772 MG |
883 | vma->vm_private_data = (void *)0; |
884 | } | |
885 | ||
886 | /* Returns true if the VMA has associated reserve pages */ | |
559ec2f8 | 887 | static bool vma_has_reserves(struct vm_area_struct *vma, long chg) |
a1e78772 | 888 | { |
af0ed73e JK |
889 | if (vma->vm_flags & VM_NORESERVE) { |
890 | /* | |
891 | * This address is already reserved by other process(chg == 0), | |
892 | * so, we should decrement reserved count. Without decrementing, | |
893 | * reserve count remains after releasing inode, because this | |
894 | * allocated page will go into page cache and is regarded as | |
895 | * coming from reserved pool in releasing step. Currently, we | |
896 | * don't have any other solution to deal with this situation | |
897 | * properly, so add work-around here. | |
898 | */ | |
899 | if (vma->vm_flags & VM_MAYSHARE && chg == 0) | |
559ec2f8 | 900 | return true; |
af0ed73e | 901 | else |
559ec2f8 | 902 | return false; |
af0ed73e | 903 | } |
a63884e9 JK |
904 | |
905 | /* Shared mappings always use reserves */ | |
1fb1b0e9 MK |
906 | if (vma->vm_flags & VM_MAYSHARE) { |
907 | /* | |
908 | * We know VM_NORESERVE is not set. Therefore, there SHOULD | |
909 | * be a region map for all pages. The only situation where | |
910 | * there is no region map is if a hole was punched via | |
911 | * fallocate. In this case, there really are no reverves to | |
912 | * use. This situation is indicated if chg != 0. | |
913 | */ | |
914 | if (chg) | |
915 | return false; | |
916 | else | |
917 | return true; | |
918 | } | |
a63884e9 JK |
919 | |
920 | /* | |
921 | * Only the process that called mmap() has reserves for | |
922 | * private mappings. | |
923 | */ | |
67961f9d MK |
924 | if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { |
925 | /* | |
926 | * Like the shared case above, a hole punch or truncate | |
927 | * could have been performed on the private mapping. | |
928 | * Examine the value of chg to determine if reserves | |
929 | * actually exist or were previously consumed. | |
930 | * Very Subtle - The value of chg comes from a previous | |
931 | * call to vma_needs_reserves(). The reserve map for | |
932 | * private mappings has different (opposite) semantics | |
933 | * than that of shared mappings. vma_needs_reserves() | |
934 | * has already taken this difference in semantics into | |
935 | * account. Therefore, the meaning of chg is the same | |
936 | * as in the shared case above. Code could easily be | |
937 | * combined, but keeping it separate draws attention to | |
938 | * subtle differences. | |
939 | */ | |
940 | if (chg) | |
941 | return false; | |
942 | else | |
943 | return true; | |
944 | } | |
a63884e9 | 945 | |
559ec2f8 | 946 | return false; |
a1e78772 MG |
947 | } |
948 | ||
a5516438 | 949 | static void enqueue_huge_page(struct hstate *h, struct page *page) |
1da177e4 LT |
950 | { |
951 | int nid = page_to_nid(page); | |
0edaecfa | 952 | list_move(&page->lru, &h->hugepage_freelists[nid]); |
a5516438 AK |
953 | h->free_huge_pages++; |
954 | h->free_huge_pages_node[nid]++; | |
1da177e4 LT |
955 | } |
956 | ||
94310cbc | 957 | static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid) |
bf50bab2 NH |
958 | { |
959 | struct page *page; | |
960 | ||
c8721bbb | 961 | list_for_each_entry(page, &h->hugepage_freelists[nid], lru) |
243abd5b | 962 | if (!PageHWPoison(page)) |
c8721bbb NH |
963 | break; |
964 | /* | |
965 | * if 'non-isolated free hugepage' not found on the list, | |
966 | * the allocation fails. | |
967 | */ | |
968 | if (&h->hugepage_freelists[nid] == &page->lru) | |
bf50bab2 | 969 | return NULL; |
0edaecfa | 970 | list_move(&page->lru, &h->hugepage_activelist); |
a9869b83 | 971 | set_page_refcounted(page); |
bf50bab2 NH |
972 | h->free_huge_pages--; |
973 | h->free_huge_pages_node[nid]--; | |
974 | return page; | |
975 | } | |
976 | ||
3e59fcb0 MH |
977 | static struct page *dequeue_huge_page_nodemask(struct hstate *h, gfp_t gfp_mask, int nid, |
978 | nodemask_t *nmask) | |
94310cbc | 979 | { |
3e59fcb0 MH |
980 | unsigned int cpuset_mems_cookie; |
981 | struct zonelist *zonelist; | |
982 | struct zone *zone; | |
983 | struct zoneref *z; | |
98fa15f3 | 984 | int node = NUMA_NO_NODE; |
94310cbc | 985 | |
3e59fcb0 MH |
986 | zonelist = node_zonelist(nid, gfp_mask); |
987 | ||
988 | retry_cpuset: | |
989 | cpuset_mems_cookie = read_mems_allowed_begin(); | |
990 | for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) { | |
991 | struct page *page; | |
992 | ||
993 | if (!cpuset_zone_allowed(zone, gfp_mask)) | |
994 | continue; | |
995 | /* | |
996 | * no need to ask again on the same node. Pool is node rather than | |
997 | * zone aware | |
998 | */ | |
999 | if (zone_to_nid(zone) == node) | |
1000 | continue; | |
1001 | node = zone_to_nid(zone); | |
94310cbc | 1002 | |
94310cbc AK |
1003 | page = dequeue_huge_page_node_exact(h, node); |
1004 | if (page) | |
1005 | return page; | |
1006 | } | |
3e59fcb0 MH |
1007 | if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie))) |
1008 | goto retry_cpuset; | |
1009 | ||
94310cbc AK |
1010 | return NULL; |
1011 | } | |
1012 | ||
86cdb465 NH |
1013 | /* Movability of hugepages depends on migration support. */ |
1014 | static inline gfp_t htlb_alloc_mask(struct hstate *h) | |
1015 | { | |
7ed2c31d | 1016 | if (hugepage_movable_supported(h)) |
86cdb465 NH |
1017 | return GFP_HIGHUSER_MOVABLE; |
1018 | else | |
1019 | return GFP_HIGHUSER; | |
1020 | } | |
1021 | ||
a5516438 AK |
1022 | static struct page *dequeue_huge_page_vma(struct hstate *h, |
1023 | struct vm_area_struct *vma, | |
af0ed73e JK |
1024 | unsigned long address, int avoid_reserve, |
1025 | long chg) | |
1da177e4 | 1026 | { |
3e59fcb0 | 1027 | struct page *page; |
480eccf9 | 1028 | struct mempolicy *mpol; |
04ec6264 | 1029 | gfp_t gfp_mask; |
3e59fcb0 | 1030 | nodemask_t *nodemask; |
04ec6264 | 1031 | int nid; |
1da177e4 | 1032 | |
a1e78772 MG |
1033 | /* |
1034 | * A child process with MAP_PRIVATE mappings created by their parent | |
1035 | * have no page reserves. This check ensures that reservations are | |
1036 | * not "stolen". The child may still get SIGKILLed | |
1037 | */ | |
af0ed73e | 1038 | if (!vma_has_reserves(vma, chg) && |
a5516438 | 1039 | h->free_huge_pages - h->resv_huge_pages == 0) |
c0ff7453 | 1040 | goto err; |
a1e78772 | 1041 | |
04f2cbe3 | 1042 | /* If reserves cannot be used, ensure enough pages are in the pool */ |
a5516438 | 1043 | if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0) |
6eab04a8 | 1044 | goto err; |
04f2cbe3 | 1045 | |
04ec6264 VB |
1046 | gfp_mask = htlb_alloc_mask(h); |
1047 | nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask); | |
3e59fcb0 MH |
1048 | page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask); |
1049 | if (page && !avoid_reserve && vma_has_reserves(vma, chg)) { | |
1050 | SetPagePrivate(page); | |
1051 | h->resv_huge_pages--; | |
1da177e4 | 1052 | } |
cc9a6c87 | 1053 | |
52cd3b07 | 1054 | mpol_cond_put(mpol); |
1da177e4 | 1055 | return page; |
cc9a6c87 MG |
1056 | |
1057 | err: | |
cc9a6c87 | 1058 | return NULL; |
1da177e4 LT |
1059 | } |
1060 | ||
1cac6f2c LC |
1061 | /* |
1062 | * common helper functions for hstate_next_node_to_{alloc|free}. | |
1063 | * We may have allocated or freed a huge page based on a different | |
1064 | * nodes_allowed previously, so h->next_node_to_{alloc|free} might | |
1065 | * be outside of *nodes_allowed. Ensure that we use an allowed | |
1066 | * node for alloc or free. | |
1067 | */ | |
1068 | static int next_node_allowed(int nid, nodemask_t *nodes_allowed) | |
1069 | { | |
0edaf86c | 1070 | nid = next_node_in(nid, *nodes_allowed); |
1cac6f2c LC |
1071 | VM_BUG_ON(nid >= MAX_NUMNODES); |
1072 | ||
1073 | return nid; | |
1074 | } | |
1075 | ||
1076 | static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) | |
1077 | { | |
1078 | if (!node_isset(nid, *nodes_allowed)) | |
1079 | nid = next_node_allowed(nid, nodes_allowed); | |
1080 | return nid; | |
1081 | } | |
1082 | ||
1083 | /* | |
1084 | * returns the previously saved node ["this node"] from which to | |
1085 | * allocate a persistent huge page for the pool and advance the | |
1086 | * next node from which to allocate, handling wrap at end of node | |
1087 | * mask. | |
1088 | */ | |
1089 | static int hstate_next_node_to_alloc(struct hstate *h, | |
1090 | nodemask_t *nodes_allowed) | |
1091 | { | |
1092 | int nid; | |
1093 | ||
1094 | VM_BUG_ON(!nodes_allowed); | |
1095 | ||
1096 | nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); | |
1097 | h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); | |
1098 | ||
1099 | return nid; | |
1100 | } | |
1101 | ||
1102 | /* | |
1103 | * helper for free_pool_huge_page() - return the previously saved | |
1104 | * node ["this node"] from which to free a huge page. Advance the | |
1105 | * next node id whether or not we find a free huge page to free so | |
1106 | * that the next attempt to free addresses the next node. | |
1107 | */ | |
1108 | static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) | |
1109 | { | |
1110 | int nid; | |
1111 | ||
1112 | VM_BUG_ON(!nodes_allowed); | |
1113 | ||
1114 | nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); | |
1115 | h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); | |
1116 | ||
1117 | return nid; | |
1118 | } | |
1119 | ||
1120 | #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \ | |
1121 | for (nr_nodes = nodes_weight(*mask); \ | |
1122 | nr_nodes > 0 && \ | |
1123 | ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ | |
1124 | nr_nodes--) | |
1125 | ||
1126 | #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ | |
1127 | for (nr_nodes = nodes_weight(*mask); \ | |
1128 | nr_nodes > 0 && \ | |
1129 | ((node = hstate_next_node_to_free(hs, mask)) || 1); \ | |
1130 | nr_nodes--) | |
1131 | ||
e1073d1e | 1132 | #ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE |
944d9fec | 1133 | static void destroy_compound_gigantic_page(struct page *page, |
d00181b9 | 1134 | unsigned int order) |
944d9fec LC |
1135 | { |
1136 | int i; | |
1137 | int nr_pages = 1 << order; | |
1138 | struct page *p = page + 1; | |
1139 | ||
c8cc708a | 1140 | atomic_set(compound_mapcount_ptr(page), 0); |
47e29d32 JH |
1141 | if (hpage_pincount_available(page)) |
1142 | atomic_set(compound_pincount_ptr(page), 0); | |
1143 | ||
944d9fec | 1144 | for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { |
1d798ca3 | 1145 | clear_compound_head(p); |
944d9fec | 1146 | set_page_refcounted(p); |
944d9fec LC |
1147 | } |
1148 | ||
1149 | set_compound_order(page, 0); | |
1150 | __ClearPageHead(page); | |
1151 | } | |
1152 | ||
d00181b9 | 1153 | static void free_gigantic_page(struct page *page, unsigned int order) |
944d9fec LC |
1154 | { |
1155 | free_contig_range(page_to_pfn(page), 1 << order); | |
1156 | } | |
1157 | ||
4eb0716e | 1158 | #ifdef CONFIG_CONTIG_ALLOC |
d9cc948f MH |
1159 | static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask, |
1160 | int nid, nodemask_t *nodemask) | |
944d9fec | 1161 | { |
5e27a2df | 1162 | unsigned long nr_pages = 1UL << huge_page_order(h); |
944d9fec | 1163 | |
5e27a2df | 1164 | return alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask); |
944d9fec LC |
1165 | } |
1166 | ||
1167 | static void prep_new_huge_page(struct hstate *h, struct page *page, int nid); | |
d00181b9 | 1168 | static void prep_compound_gigantic_page(struct page *page, unsigned int order); |
4eb0716e AG |
1169 | #else /* !CONFIG_CONTIG_ALLOC */ |
1170 | static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask, | |
1171 | int nid, nodemask_t *nodemask) | |
1172 | { | |
1173 | return NULL; | |
1174 | } | |
1175 | #endif /* CONFIG_CONTIG_ALLOC */ | |
944d9fec | 1176 | |
e1073d1e | 1177 | #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */ |
d9cc948f | 1178 | static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask, |
4eb0716e AG |
1179 | int nid, nodemask_t *nodemask) |
1180 | { | |
1181 | return NULL; | |
1182 | } | |
d00181b9 | 1183 | static inline void free_gigantic_page(struct page *page, unsigned int order) { } |
944d9fec | 1184 | static inline void destroy_compound_gigantic_page(struct page *page, |
d00181b9 | 1185 | unsigned int order) { } |
944d9fec LC |
1186 | #endif |
1187 | ||
a5516438 | 1188 | static void update_and_free_page(struct hstate *h, struct page *page) |
6af2acb6 AL |
1189 | { |
1190 | int i; | |
a5516438 | 1191 | |
4eb0716e | 1192 | if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) |
944d9fec | 1193 | return; |
18229df5 | 1194 | |
a5516438 AK |
1195 | h->nr_huge_pages--; |
1196 | h->nr_huge_pages_node[page_to_nid(page)]--; | |
1197 | for (i = 0; i < pages_per_huge_page(h); i++) { | |
32f84528 CF |
1198 | page[i].flags &= ~(1 << PG_locked | 1 << PG_error | |
1199 | 1 << PG_referenced | 1 << PG_dirty | | |
a7407a27 LC |
1200 | 1 << PG_active | 1 << PG_private | |
1201 | 1 << PG_writeback); | |
6af2acb6 | 1202 | } |
309381fe | 1203 | VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page); |
1adc4d41 | 1204 | VM_BUG_ON_PAGE(hugetlb_cgroup_from_page_rsvd(page), page); |
f1e61557 | 1205 | set_compound_page_dtor(page, NULL_COMPOUND_DTOR); |
6af2acb6 | 1206 | set_page_refcounted(page); |
944d9fec LC |
1207 | if (hstate_is_gigantic(h)) { |
1208 | destroy_compound_gigantic_page(page, huge_page_order(h)); | |
1209 | free_gigantic_page(page, huge_page_order(h)); | |
1210 | } else { | |
944d9fec LC |
1211 | __free_pages(page, huge_page_order(h)); |
1212 | } | |
6af2acb6 AL |
1213 | } |
1214 | ||
e5ff2159 AK |
1215 | struct hstate *size_to_hstate(unsigned long size) |
1216 | { | |
1217 | struct hstate *h; | |
1218 | ||
1219 | for_each_hstate(h) { | |
1220 | if (huge_page_size(h) == size) | |
1221 | return h; | |
1222 | } | |
1223 | return NULL; | |
1224 | } | |
1225 | ||
bcc54222 NH |
1226 | /* |
1227 | * Test to determine whether the hugepage is "active/in-use" (i.e. being linked | |
1228 | * to hstate->hugepage_activelist.) | |
1229 | * | |
1230 | * This function can be called for tail pages, but never returns true for them. | |
1231 | */ | |
1232 | bool page_huge_active(struct page *page) | |
1233 | { | |
1234 | VM_BUG_ON_PAGE(!PageHuge(page), page); | |
1235 | return PageHead(page) && PagePrivate(&page[1]); | |
1236 | } | |
1237 | ||
1238 | /* never called for tail page */ | |
1239 | static void set_page_huge_active(struct page *page) | |
1240 | { | |
1241 | VM_BUG_ON_PAGE(!PageHeadHuge(page), page); | |
1242 | SetPagePrivate(&page[1]); | |
1243 | } | |
1244 | ||
1245 | static void clear_page_huge_active(struct page *page) | |
1246 | { | |
1247 | VM_BUG_ON_PAGE(!PageHeadHuge(page), page); | |
1248 | ClearPagePrivate(&page[1]); | |
1249 | } | |
1250 | ||
ab5ac90a MH |
1251 | /* |
1252 | * Internal hugetlb specific page flag. Do not use outside of the hugetlb | |
1253 | * code | |
1254 | */ | |
1255 | static inline bool PageHugeTemporary(struct page *page) | |
1256 | { | |
1257 | if (!PageHuge(page)) | |
1258 | return false; | |
1259 | ||
1260 | return (unsigned long)page[2].mapping == -1U; | |
1261 | } | |
1262 | ||
1263 | static inline void SetPageHugeTemporary(struct page *page) | |
1264 | { | |
1265 | page[2].mapping = (void *)-1U; | |
1266 | } | |
1267 | ||
1268 | static inline void ClearPageHugeTemporary(struct page *page) | |
1269 | { | |
1270 | page[2].mapping = NULL; | |
1271 | } | |
1272 | ||
c77c0a8a | 1273 | static void __free_huge_page(struct page *page) |
27a85ef1 | 1274 | { |
a5516438 AK |
1275 | /* |
1276 | * Can't pass hstate in here because it is called from the | |
1277 | * compound page destructor. | |
1278 | */ | |
e5ff2159 | 1279 | struct hstate *h = page_hstate(page); |
7893d1d5 | 1280 | int nid = page_to_nid(page); |
90481622 DG |
1281 | struct hugepage_subpool *spool = |
1282 | (struct hugepage_subpool *)page_private(page); | |
07443a85 | 1283 | bool restore_reserve; |
27a85ef1 | 1284 | |
b4330afb MK |
1285 | VM_BUG_ON_PAGE(page_count(page), page); |
1286 | VM_BUG_ON_PAGE(page_mapcount(page), page); | |
8ace22bc YW |
1287 | |
1288 | set_page_private(page, 0); | |
1289 | page->mapping = NULL; | |
07443a85 | 1290 | restore_reserve = PagePrivate(page); |
16c794b4 | 1291 | ClearPagePrivate(page); |
27a85ef1 | 1292 | |
1c5ecae3 | 1293 | /* |
0919e1b6 MK |
1294 | * If PagePrivate() was set on page, page allocation consumed a |
1295 | * reservation. If the page was associated with a subpool, there | |
1296 | * would have been a page reserved in the subpool before allocation | |
1297 | * via hugepage_subpool_get_pages(). Since we are 'restoring' the | |
1298 | * reservtion, do not call hugepage_subpool_put_pages() as this will | |
1299 | * remove the reserved page from the subpool. | |
1c5ecae3 | 1300 | */ |
0919e1b6 MK |
1301 | if (!restore_reserve) { |
1302 | /* | |
1303 | * A return code of zero implies that the subpool will be | |
1304 | * under its minimum size if the reservation is not restored | |
1305 | * after page is free. Therefore, force restore_reserve | |
1306 | * operation. | |
1307 | */ | |
1308 | if (hugepage_subpool_put_pages(spool, 1) == 0) | |
1309 | restore_reserve = true; | |
1310 | } | |
1c5ecae3 | 1311 | |
27a85ef1 | 1312 | spin_lock(&hugetlb_lock); |
bcc54222 | 1313 | clear_page_huge_active(page); |
6d76dcf4 AK |
1314 | hugetlb_cgroup_uncharge_page(hstate_index(h), |
1315 | pages_per_huge_page(h), page); | |
07443a85 JK |
1316 | if (restore_reserve) |
1317 | h->resv_huge_pages++; | |
1318 | ||
ab5ac90a MH |
1319 | if (PageHugeTemporary(page)) { |
1320 | list_del(&page->lru); | |
1321 | ClearPageHugeTemporary(page); | |
1322 | update_and_free_page(h, page); | |
1323 | } else if (h->surplus_huge_pages_node[nid]) { | |
0edaecfa AK |
1324 | /* remove the page from active list */ |
1325 | list_del(&page->lru); | |
a5516438 AK |
1326 | update_and_free_page(h, page); |
1327 | h->surplus_huge_pages--; | |
1328 | h->surplus_huge_pages_node[nid]--; | |
7893d1d5 | 1329 | } else { |
5d3a551c | 1330 | arch_clear_hugepage_flags(page); |
a5516438 | 1331 | enqueue_huge_page(h, page); |
7893d1d5 | 1332 | } |
27a85ef1 DG |
1333 | spin_unlock(&hugetlb_lock); |
1334 | } | |
1335 | ||
c77c0a8a WL |
1336 | /* |
1337 | * As free_huge_page() can be called from a non-task context, we have | |
1338 | * to defer the actual freeing in a workqueue to prevent potential | |
1339 | * hugetlb_lock deadlock. | |
1340 | * | |
1341 | * free_hpage_workfn() locklessly retrieves the linked list of pages to | |
1342 | * be freed and frees them one-by-one. As the page->mapping pointer is | |
1343 | * going to be cleared in __free_huge_page() anyway, it is reused as the | |
1344 | * llist_node structure of a lockless linked list of huge pages to be freed. | |
1345 | */ | |
1346 | static LLIST_HEAD(hpage_freelist); | |
1347 | ||
1348 | static void free_hpage_workfn(struct work_struct *work) | |
1349 | { | |
1350 | struct llist_node *node; | |
1351 | struct page *page; | |
1352 | ||
1353 | node = llist_del_all(&hpage_freelist); | |
1354 | ||
1355 | while (node) { | |
1356 | page = container_of((struct address_space **)node, | |
1357 | struct page, mapping); | |
1358 | node = node->next; | |
1359 | __free_huge_page(page); | |
1360 | } | |
1361 | } | |
1362 | static DECLARE_WORK(free_hpage_work, free_hpage_workfn); | |
1363 | ||
1364 | void free_huge_page(struct page *page) | |
1365 | { | |
1366 | /* | |
1367 | * Defer freeing if in non-task context to avoid hugetlb_lock deadlock. | |
1368 | */ | |
1369 | if (!in_task()) { | |
1370 | /* | |
1371 | * Only call schedule_work() if hpage_freelist is previously | |
1372 | * empty. Otherwise, schedule_work() had been called but the | |
1373 | * workfn hasn't retrieved the list yet. | |
1374 | */ | |
1375 | if (llist_add((struct llist_node *)&page->mapping, | |
1376 | &hpage_freelist)) | |
1377 | schedule_work(&free_hpage_work); | |
1378 | return; | |
1379 | } | |
1380 | ||
1381 | __free_huge_page(page); | |
1382 | } | |
1383 | ||
a5516438 | 1384 | static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) |
b7ba30c6 | 1385 | { |
0edaecfa | 1386 | INIT_LIST_HEAD(&page->lru); |
f1e61557 | 1387 | set_compound_page_dtor(page, HUGETLB_PAGE_DTOR); |
b7ba30c6 | 1388 | spin_lock(&hugetlb_lock); |
9dd540e2 | 1389 | set_hugetlb_cgroup(page, NULL); |
1adc4d41 | 1390 | set_hugetlb_cgroup_rsvd(page, NULL); |
a5516438 AK |
1391 | h->nr_huge_pages++; |
1392 | h->nr_huge_pages_node[nid]++; | |
b7ba30c6 | 1393 | spin_unlock(&hugetlb_lock); |
b7ba30c6 AK |
1394 | } |
1395 | ||
d00181b9 | 1396 | static void prep_compound_gigantic_page(struct page *page, unsigned int order) |
20a0307c WF |
1397 | { |
1398 | int i; | |
1399 | int nr_pages = 1 << order; | |
1400 | struct page *p = page + 1; | |
1401 | ||
1402 | /* we rely on prep_new_huge_page to set the destructor */ | |
1403 | set_compound_order(page, order); | |
ef5a22be | 1404 | __ClearPageReserved(page); |
de09d31d | 1405 | __SetPageHead(page); |
20a0307c | 1406 | for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { |
ef5a22be AA |
1407 | /* |
1408 | * For gigantic hugepages allocated through bootmem at | |
1409 | * boot, it's safer to be consistent with the not-gigantic | |
1410 | * hugepages and clear the PG_reserved bit from all tail pages | |
1411 | * too. Otherwse drivers using get_user_pages() to access tail | |
1412 | * pages may get the reference counting wrong if they see | |
1413 | * PG_reserved set on a tail page (despite the head page not | |
1414 | * having PG_reserved set). Enforcing this consistency between | |
1415 | * head and tail pages allows drivers to optimize away a check | |
1416 | * on the head page when they need know if put_page() is needed | |
1417 | * after get_user_pages(). | |
1418 | */ | |
1419 | __ClearPageReserved(p); | |
58a84aa9 | 1420 | set_page_count(p, 0); |
1d798ca3 | 1421 | set_compound_head(p, page); |
20a0307c | 1422 | } |
b4330afb | 1423 | atomic_set(compound_mapcount_ptr(page), -1); |
47e29d32 JH |
1424 | |
1425 | if (hpage_pincount_available(page)) | |
1426 | atomic_set(compound_pincount_ptr(page), 0); | |
20a0307c WF |
1427 | } |
1428 | ||
7795912c AM |
1429 | /* |
1430 | * PageHuge() only returns true for hugetlbfs pages, but not for normal or | |
1431 | * transparent huge pages. See the PageTransHuge() documentation for more | |
1432 | * details. | |
1433 | */ | |
20a0307c WF |
1434 | int PageHuge(struct page *page) |
1435 | { | |
20a0307c WF |
1436 | if (!PageCompound(page)) |
1437 | return 0; | |
1438 | ||
1439 | page = compound_head(page); | |
f1e61557 | 1440 | return page[1].compound_dtor == HUGETLB_PAGE_DTOR; |
20a0307c | 1441 | } |
43131e14 NH |
1442 | EXPORT_SYMBOL_GPL(PageHuge); |
1443 | ||
27c73ae7 AA |
1444 | /* |
1445 | * PageHeadHuge() only returns true for hugetlbfs head page, but not for | |
1446 | * normal or transparent huge pages. | |
1447 | */ | |
1448 | int PageHeadHuge(struct page *page_head) | |
1449 | { | |
27c73ae7 AA |
1450 | if (!PageHead(page_head)) |
1451 | return 0; | |
1452 | ||
758f66a2 | 1453 | return get_compound_page_dtor(page_head) == free_huge_page; |
27c73ae7 | 1454 | } |
27c73ae7 | 1455 | |
c0d0381a MK |
1456 | /* |
1457 | * Find address_space associated with hugetlbfs page. | |
1458 | * Upon entry page is locked and page 'was' mapped although mapped state | |
1459 | * could change. If necessary, use anon_vma to find vma and associated | |
1460 | * address space. The returned mapping may be stale, but it can not be | |
1461 | * invalid as page lock (which is held) is required to destroy mapping. | |
1462 | */ | |
1463 | static struct address_space *_get_hugetlb_page_mapping(struct page *hpage) | |
1464 | { | |
1465 | struct anon_vma *anon_vma; | |
1466 | pgoff_t pgoff_start, pgoff_end; | |
1467 | struct anon_vma_chain *avc; | |
1468 | struct address_space *mapping = page_mapping(hpage); | |
1469 | ||
1470 | /* Simple file based mapping */ | |
1471 | if (mapping) | |
1472 | return mapping; | |
1473 | ||
1474 | /* | |
1475 | * Even anonymous hugetlbfs mappings are associated with an | |
1476 | * underlying hugetlbfs file (see hugetlb_file_setup in mmap | |
1477 | * code). Find a vma associated with the anonymous vma, and | |
1478 | * use the file pointer to get address_space. | |
1479 | */ | |
1480 | anon_vma = page_lock_anon_vma_read(hpage); | |
1481 | if (!anon_vma) | |
1482 | return mapping; /* NULL */ | |
1483 | ||
1484 | /* Use first found vma */ | |
1485 | pgoff_start = page_to_pgoff(hpage); | |
1486 | pgoff_end = pgoff_start + hpage_nr_pages(hpage) - 1; | |
1487 | anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, | |
1488 | pgoff_start, pgoff_end) { | |
1489 | struct vm_area_struct *vma = avc->vma; | |
1490 | ||
1491 | mapping = vma->vm_file->f_mapping; | |
1492 | break; | |
1493 | } | |
1494 | ||
1495 | anon_vma_unlock_read(anon_vma); | |
1496 | return mapping; | |
1497 | } | |
1498 | ||
1499 | /* | |
1500 | * Find and lock address space (mapping) in write mode. | |
1501 | * | |
1502 | * Upon entry, the page is locked which allows us to find the mapping | |
1503 | * even in the case of an anon page. However, locking order dictates | |
1504 | * the i_mmap_rwsem be acquired BEFORE the page lock. This is hugetlbfs | |
1505 | * specific. So, we first try to lock the sema while still holding the | |
1506 | * page lock. If this works, great! If not, then we need to drop the | |
1507 | * page lock and then acquire i_mmap_rwsem and reacquire page lock. Of | |
1508 | * course, need to revalidate state along the way. | |
1509 | */ | |
1510 | struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage) | |
1511 | { | |
1512 | struct address_space *mapping, *mapping2; | |
1513 | ||
1514 | mapping = _get_hugetlb_page_mapping(hpage); | |
1515 | retry: | |
1516 | if (!mapping) | |
1517 | return mapping; | |
1518 | ||
1519 | /* | |
1520 | * If no contention, take lock and return | |
1521 | */ | |
1522 | if (i_mmap_trylock_write(mapping)) | |
1523 | return mapping; | |
1524 | ||
1525 | /* | |
1526 | * Must drop page lock and wait on mapping sema. | |
1527 | * Note: Once page lock is dropped, mapping could become invalid. | |
1528 | * As a hack, increase map count until we lock page again. | |
1529 | */ | |
1530 | atomic_inc(&hpage->_mapcount); | |
1531 | unlock_page(hpage); | |
1532 | i_mmap_lock_write(mapping); | |
1533 | lock_page(hpage); | |
1534 | atomic_add_negative(-1, &hpage->_mapcount); | |
1535 | ||
1536 | /* verify page is still mapped */ | |
1537 | if (!page_mapped(hpage)) { | |
1538 | i_mmap_unlock_write(mapping); | |
1539 | return NULL; | |
1540 | } | |
1541 | ||
1542 | /* | |
1543 | * Get address space again and verify it is the same one | |
1544 | * we locked. If not, drop lock and retry. | |
1545 | */ | |
1546 | mapping2 = _get_hugetlb_page_mapping(hpage); | |
1547 | if (mapping2 != mapping) { | |
1548 | i_mmap_unlock_write(mapping); | |
1549 | mapping = mapping2; | |
1550 | goto retry; | |
1551 | } | |
1552 | ||
1553 | return mapping; | |
1554 | } | |
1555 | ||
13d60f4b ZY |
1556 | pgoff_t __basepage_index(struct page *page) |
1557 | { | |
1558 | struct page *page_head = compound_head(page); | |
1559 | pgoff_t index = page_index(page_head); | |
1560 | unsigned long compound_idx; | |
1561 | ||
1562 | if (!PageHuge(page_head)) | |
1563 | return page_index(page); | |
1564 | ||
1565 | if (compound_order(page_head) >= MAX_ORDER) | |
1566 | compound_idx = page_to_pfn(page) - page_to_pfn(page_head); | |
1567 | else | |
1568 | compound_idx = page - page_head; | |
1569 | ||
1570 | return (index << compound_order(page_head)) + compound_idx; | |
1571 | } | |
1572 | ||
0c397dae | 1573 | static struct page *alloc_buddy_huge_page(struct hstate *h, |
f60858f9 MK |
1574 | gfp_t gfp_mask, int nid, nodemask_t *nmask, |
1575 | nodemask_t *node_alloc_noretry) | |
1da177e4 | 1576 | { |
af0fb9df | 1577 | int order = huge_page_order(h); |
1da177e4 | 1578 | struct page *page; |
f60858f9 | 1579 | bool alloc_try_hard = true; |
f96efd58 | 1580 | |
f60858f9 MK |
1581 | /* |
1582 | * By default we always try hard to allocate the page with | |
1583 | * __GFP_RETRY_MAYFAIL flag. However, if we are allocating pages in | |
1584 | * a loop (to adjust global huge page counts) and previous allocation | |
1585 | * failed, do not continue to try hard on the same node. Use the | |
1586 | * node_alloc_noretry bitmap to manage this state information. | |
1587 | */ | |
1588 | if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry)) | |
1589 | alloc_try_hard = false; | |
1590 | gfp_mask |= __GFP_COMP|__GFP_NOWARN; | |
1591 | if (alloc_try_hard) | |
1592 | gfp_mask |= __GFP_RETRY_MAYFAIL; | |
af0fb9df MH |
1593 | if (nid == NUMA_NO_NODE) |
1594 | nid = numa_mem_id(); | |
1595 | page = __alloc_pages_nodemask(gfp_mask, order, nid, nmask); | |
1596 | if (page) | |
1597 | __count_vm_event(HTLB_BUDDY_PGALLOC); | |
1598 | else | |
1599 | __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); | |
63b4613c | 1600 | |
f60858f9 MK |
1601 | /* |
1602 | * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this | |
1603 | * indicates an overall state change. Clear bit so that we resume | |
1604 | * normal 'try hard' allocations. | |
1605 | */ | |
1606 | if (node_alloc_noretry && page && !alloc_try_hard) | |
1607 | node_clear(nid, *node_alloc_noretry); | |
1608 | ||
1609 | /* | |
1610 | * If we tried hard to get a page but failed, set bit so that | |
1611 | * subsequent attempts will not try as hard until there is an | |
1612 | * overall state change. | |
1613 | */ | |
1614 | if (node_alloc_noretry && !page && alloc_try_hard) | |
1615 | node_set(nid, *node_alloc_noretry); | |
1616 | ||
63b4613c NA |
1617 | return page; |
1618 | } | |
1619 | ||
0c397dae MH |
1620 | /* |
1621 | * Common helper to allocate a fresh hugetlb page. All specific allocators | |
1622 | * should use this function to get new hugetlb pages | |
1623 | */ | |
1624 | static struct page *alloc_fresh_huge_page(struct hstate *h, | |
f60858f9 MK |
1625 | gfp_t gfp_mask, int nid, nodemask_t *nmask, |
1626 | nodemask_t *node_alloc_noretry) | |
0c397dae MH |
1627 | { |
1628 | struct page *page; | |
1629 | ||
1630 | if (hstate_is_gigantic(h)) | |
1631 | page = alloc_gigantic_page(h, gfp_mask, nid, nmask); | |
1632 | else | |
1633 | page = alloc_buddy_huge_page(h, gfp_mask, | |
f60858f9 | 1634 | nid, nmask, node_alloc_noretry); |
0c397dae MH |
1635 | if (!page) |
1636 | return NULL; | |
1637 | ||
1638 | if (hstate_is_gigantic(h)) | |
1639 | prep_compound_gigantic_page(page, huge_page_order(h)); | |
1640 | prep_new_huge_page(h, page, page_to_nid(page)); | |
1641 | ||
1642 | return page; | |
1643 | } | |
1644 | ||
af0fb9df MH |
1645 | /* |
1646 | * Allocates a fresh page to the hugetlb allocator pool in the node interleaved | |
1647 | * manner. | |
1648 | */ | |
f60858f9 MK |
1649 | static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, |
1650 | nodemask_t *node_alloc_noretry) | |
b2261026 JK |
1651 | { |
1652 | struct page *page; | |
1653 | int nr_nodes, node; | |
af0fb9df | 1654 | gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; |
b2261026 JK |
1655 | |
1656 | for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { | |
f60858f9 MK |
1657 | page = alloc_fresh_huge_page(h, gfp_mask, node, nodes_allowed, |
1658 | node_alloc_noretry); | |
af0fb9df | 1659 | if (page) |
b2261026 | 1660 | break; |
b2261026 JK |
1661 | } |
1662 | ||
af0fb9df MH |
1663 | if (!page) |
1664 | return 0; | |
b2261026 | 1665 | |
af0fb9df MH |
1666 | put_page(page); /* free it into the hugepage allocator */ |
1667 | ||
1668 | return 1; | |
b2261026 JK |
1669 | } |
1670 | ||
e8c5c824 LS |
1671 | /* |
1672 | * Free huge page from pool from next node to free. | |
1673 | * Attempt to keep persistent huge pages more or less | |
1674 | * balanced over allowed nodes. | |
1675 | * Called with hugetlb_lock locked. | |
1676 | */ | |
6ae11b27 LS |
1677 | static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, |
1678 | bool acct_surplus) | |
e8c5c824 | 1679 | { |
b2261026 | 1680 | int nr_nodes, node; |
e8c5c824 LS |
1681 | int ret = 0; |
1682 | ||
b2261026 | 1683 | for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { |
685f3457 LS |
1684 | /* |
1685 | * If we're returning unused surplus pages, only examine | |
1686 | * nodes with surplus pages. | |
1687 | */ | |
b2261026 JK |
1688 | if ((!acct_surplus || h->surplus_huge_pages_node[node]) && |
1689 | !list_empty(&h->hugepage_freelists[node])) { | |
e8c5c824 | 1690 | struct page *page = |
b2261026 | 1691 | list_entry(h->hugepage_freelists[node].next, |
e8c5c824 LS |
1692 | struct page, lru); |
1693 | list_del(&page->lru); | |
1694 | h->free_huge_pages--; | |
b2261026 | 1695 | h->free_huge_pages_node[node]--; |
685f3457 LS |
1696 | if (acct_surplus) { |
1697 | h->surplus_huge_pages--; | |
b2261026 | 1698 | h->surplus_huge_pages_node[node]--; |
685f3457 | 1699 | } |
e8c5c824 LS |
1700 | update_and_free_page(h, page); |
1701 | ret = 1; | |
9a76db09 | 1702 | break; |
e8c5c824 | 1703 | } |
b2261026 | 1704 | } |
e8c5c824 LS |
1705 | |
1706 | return ret; | |
1707 | } | |
1708 | ||
c8721bbb NH |
1709 | /* |
1710 | * Dissolve a given free hugepage into free buddy pages. This function does | |
faf53def NH |
1711 | * nothing for in-use hugepages and non-hugepages. |
1712 | * This function returns values like below: | |
1713 | * | |
1714 | * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use | |
1715 | * (allocated or reserved.) | |
1716 | * 0: successfully dissolved free hugepages or the page is not a | |
1717 | * hugepage (considered as already dissolved) | |
c8721bbb | 1718 | */ |
c3114a84 | 1719 | int dissolve_free_huge_page(struct page *page) |
c8721bbb | 1720 | { |
6bc9b564 | 1721 | int rc = -EBUSY; |
082d5b6b | 1722 | |
faf53def NH |
1723 | /* Not to disrupt normal path by vainly holding hugetlb_lock */ |
1724 | if (!PageHuge(page)) | |
1725 | return 0; | |
1726 | ||
c8721bbb | 1727 | spin_lock(&hugetlb_lock); |
faf53def NH |
1728 | if (!PageHuge(page)) { |
1729 | rc = 0; | |
1730 | goto out; | |
1731 | } | |
1732 | ||
1733 | if (!page_count(page)) { | |
2247bb33 GS |
1734 | struct page *head = compound_head(page); |
1735 | struct hstate *h = page_hstate(head); | |
1736 | int nid = page_to_nid(head); | |
6bc9b564 | 1737 | if (h->free_huge_pages - h->resv_huge_pages == 0) |
082d5b6b | 1738 | goto out; |
c3114a84 AK |
1739 | /* |
1740 | * Move PageHWPoison flag from head page to the raw error page, | |
1741 | * which makes any subpages rather than the error page reusable. | |
1742 | */ | |
1743 | if (PageHWPoison(head) && page != head) { | |
1744 | SetPageHWPoison(page); | |
1745 | ClearPageHWPoison(head); | |
1746 | } | |
2247bb33 | 1747 | list_del(&head->lru); |
c8721bbb NH |
1748 | h->free_huge_pages--; |
1749 | h->free_huge_pages_node[nid]--; | |
c1470b33 | 1750 | h->max_huge_pages--; |
2247bb33 | 1751 | update_and_free_page(h, head); |
6bc9b564 | 1752 | rc = 0; |
c8721bbb | 1753 | } |
082d5b6b | 1754 | out: |
c8721bbb | 1755 | spin_unlock(&hugetlb_lock); |
082d5b6b | 1756 | return rc; |
c8721bbb NH |
1757 | } |
1758 | ||
1759 | /* | |
1760 | * Dissolve free hugepages in a given pfn range. Used by memory hotplug to | |
1761 | * make specified memory blocks removable from the system. | |
2247bb33 GS |
1762 | * Note that this will dissolve a free gigantic hugepage completely, if any |
1763 | * part of it lies within the given range. | |
082d5b6b GS |
1764 | * Also note that if dissolve_free_huge_page() returns with an error, all |
1765 | * free hugepages that were dissolved before that error are lost. | |
c8721bbb | 1766 | */ |
082d5b6b | 1767 | int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) |
c8721bbb | 1768 | { |
c8721bbb | 1769 | unsigned long pfn; |
eb03aa00 | 1770 | struct page *page; |
082d5b6b | 1771 | int rc = 0; |
c8721bbb | 1772 | |
d0177639 | 1773 | if (!hugepages_supported()) |
082d5b6b | 1774 | return rc; |
d0177639 | 1775 | |
eb03aa00 GS |
1776 | for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) { |
1777 | page = pfn_to_page(pfn); | |
faf53def NH |
1778 | rc = dissolve_free_huge_page(page); |
1779 | if (rc) | |
1780 | break; | |
eb03aa00 | 1781 | } |
082d5b6b GS |
1782 | |
1783 | return rc; | |
c8721bbb NH |
1784 | } |
1785 | ||
ab5ac90a MH |
1786 | /* |
1787 | * Allocates a fresh surplus page from the page allocator. | |
1788 | */ | |
0c397dae | 1789 | static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask, |
aaf14e40 | 1790 | int nid, nodemask_t *nmask) |
7893d1d5 | 1791 | { |
9980d744 | 1792 | struct page *page = NULL; |
7893d1d5 | 1793 | |
bae7f4ae | 1794 | if (hstate_is_gigantic(h)) |
aa888a74 AK |
1795 | return NULL; |
1796 | ||
d1c3fb1f | 1797 | spin_lock(&hugetlb_lock); |
9980d744 MH |
1798 | if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) |
1799 | goto out_unlock; | |
d1c3fb1f NA |
1800 | spin_unlock(&hugetlb_lock); |
1801 | ||
f60858f9 | 1802 | page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL); |
9980d744 | 1803 | if (!page) |
0c397dae | 1804 | return NULL; |
d1c3fb1f NA |
1805 | |
1806 | spin_lock(&hugetlb_lock); | |
9980d744 MH |
1807 | /* |
1808 | * We could have raced with the pool size change. | |
1809 | * Double check that and simply deallocate the new page | |
1810 | * if we would end up overcommiting the surpluses. Abuse | |
1811 | * temporary page to workaround the nasty free_huge_page | |
1812 | * codeflow | |
1813 | */ | |
1814 | if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { | |
1815 | SetPageHugeTemporary(page); | |
2bf753e6 | 1816 | spin_unlock(&hugetlb_lock); |
9980d744 | 1817 | put_page(page); |
2bf753e6 | 1818 | return NULL; |
9980d744 | 1819 | } else { |
9980d744 | 1820 | h->surplus_huge_pages++; |
4704dea3 | 1821 | h->surplus_huge_pages_node[page_to_nid(page)]++; |
7893d1d5 | 1822 | } |
9980d744 MH |
1823 | |
1824 | out_unlock: | |
d1c3fb1f | 1825 | spin_unlock(&hugetlb_lock); |
7893d1d5 AL |
1826 | |
1827 | return page; | |
1828 | } | |
1829 | ||
9a4e9f3b AK |
1830 | struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask, |
1831 | int nid, nodemask_t *nmask) | |
ab5ac90a MH |
1832 | { |
1833 | struct page *page; | |
1834 | ||
1835 | if (hstate_is_gigantic(h)) | |
1836 | return NULL; | |
1837 | ||
f60858f9 | 1838 | page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL); |
ab5ac90a MH |
1839 | if (!page) |
1840 | return NULL; | |
1841 | ||
1842 | /* | |
1843 | * We do not account these pages as surplus because they are only | |
1844 | * temporary and will be released properly on the last reference | |
1845 | */ | |
ab5ac90a MH |
1846 | SetPageHugeTemporary(page); |
1847 | ||
1848 | return page; | |
1849 | } | |
1850 | ||
099730d6 DH |
1851 | /* |
1852 | * Use the VMA's mpolicy to allocate a huge page from the buddy. | |
1853 | */ | |
e0ec90ee | 1854 | static |
0c397dae | 1855 | struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h, |
099730d6 DH |
1856 | struct vm_area_struct *vma, unsigned long addr) |
1857 | { | |
aaf14e40 MH |
1858 | struct page *page; |
1859 | struct mempolicy *mpol; | |
1860 | gfp_t gfp_mask = htlb_alloc_mask(h); | |
1861 | int nid; | |
1862 | nodemask_t *nodemask; | |
1863 | ||
1864 | nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask); | |
0c397dae | 1865 | page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask); |
aaf14e40 MH |
1866 | mpol_cond_put(mpol); |
1867 | ||
1868 | return page; | |
099730d6 DH |
1869 | } |
1870 | ||
ab5ac90a | 1871 | /* page migration callback function */ |
bf50bab2 NH |
1872 | struct page *alloc_huge_page_node(struct hstate *h, int nid) |
1873 | { | |
aaf14e40 | 1874 | gfp_t gfp_mask = htlb_alloc_mask(h); |
4ef91848 | 1875 | struct page *page = NULL; |
bf50bab2 | 1876 | |
aaf14e40 MH |
1877 | if (nid != NUMA_NO_NODE) |
1878 | gfp_mask |= __GFP_THISNODE; | |
1879 | ||
bf50bab2 | 1880 | spin_lock(&hugetlb_lock); |
4ef91848 | 1881 | if (h->free_huge_pages - h->resv_huge_pages > 0) |
3e59fcb0 | 1882 | page = dequeue_huge_page_nodemask(h, gfp_mask, nid, NULL); |
bf50bab2 NH |
1883 | spin_unlock(&hugetlb_lock); |
1884 | ||
94ae8ba7 | 1885 | if (!page) |
0c397dae | 1886 | page = alloc_migrate_huge_page(h, gfp_mask, nid, NULL); |
bf50bab2 NH |
1887 | |
1888 | return page; | |
1889 | } | |
1890 | ||
ab5ac90a | 1891 | /* page migration callback function */ |
3e59fcb0 MH |
1892 | struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid, |
1893 | nodemask_t *nmask) | |
4db9b2ef | 1894 | { |
aaf14e40 | 1895 | gfp_t gfp_mask = htlb_alloc_mask(h); |
4db9b2ef MH |
1896 | |
1897 | spin_lock(&hugetlb_lock); | |
1898 | if (h->free_huge_pages - h->resv_huge_pages > 0) { | |
3e59fcb0 MH |
1899 | struct page *page; |
1900 | ||
1901 | page = dequeue_huge_page_nodemask(h, gfp_mask, preferred_nid, nmask); | |
1902 | if (page) { | |
1903 | spin_unlock(&hugetlb_lock); | |
1904 | return page; | |
4db9b2ef MH |
1905 | } |
1906 | } | |
1907 | spin_unlock(&hugetlb_lock); | |
4db9b2ef | 1908 | |
0c397dae | 1909 | return alloc_migrate_huge_page(h, gfp_mask, preferred_nid, nmask); |
4db9b2ef MH |
1910 | } |
1911 | ||
ebd63723 | 1912 | /* mempolicy aware migration callback */ |
389c8178 MH |
1913 | struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma, |
1914 | unsigned long address) | |
ebd63723 MH |
1915 | { |
1916 | struct mempolicy *mpol; | |
1917 | nodemask_t *nodemask; | |
1918 | struct page *page; | |
ebd63723 MH |
1919 | gfp_t gfp_mask; |
1920 | int node; | |
1921 | ||
ebd63723 MH |
1922 | gfp_mask = htlb_alloc_mask(h); |
1923 | node = huge_node(vma, address, gfp_mask, &mpol, &nodemask); | |
1924 | page = alloc_huge_page_nodemask(h, node, nodemask); | |
1925 | mpol_cond_put(mpol); | |
1926 | ||
1927 | return page; | |
1928 | } | |
1929 | ||
e4e574b7 | 1930 | /* |
25985edc | 1931 | * Increase the hugetlb pool such that it can accommodate a reservation |
e4e574b7 AL |
1932 | * of size 'delta'. |
1933 | */ | |
a5516438 | 1934 | static int gather_surplus_pages(struct hstate *h, int delta) |
e4e574b7 AL |
1935 | { |
1936 | struct list_head surplus_list; | |
1937 | struct page *page, *tmp; | |
1938 | int ret, i; | |
1939 | int needed, allocated; | |
28073b02 | 1940 | bool alloc_ok = true; |
e4e574b7 | 1941 | |
a5516438 | 1942 | needed = (h->resv_huge_pages + delta) - h->free_huge_pages; |
ac09b3a1 | 1943 | if (needed <= 0) { |
a5516438 | 1944 | h->resv_huge_pages += delta; |
e4e574b7 | 1945 | return 0; |
ac09b3a1 | 1946 | } |
e4e574b7 AL |
1947 | |
1948 | allocated = 0; | |
1949 | INIT_LIST_HEAD(&surplus_list); | |
1950 | ||
1951 | ret = -ENOMEM; | |
1952 | retry: | |
1953 | spin_unlock(&hugetlb_lock); | |
1954 | for (i = 0; i < needed; i++) { | |
0c397dae | 1955 | page = alloc_surplus_huge_page(h, htlb_alloc_mask(h), |
aaf14e40 | 1956 | NUMA_NO_NODE, NULL); |
28073b02 HD |
1957 | if (!page) { |
1958 | alloc_ok = false; | |
1959 | break; | |
1960 | } | |
e4e574b7 | 1961 | list_add(&page->lru, &surplus_list); |
69ed779a | 1962 | cond_resched(); |
e4e574b7 | 1963 | } |
28073b02 | 1964 | allocated += i; |
e4e574b7 AL |
1965 | |
1966 | /* | |
1967 | * After retaking hugetlb_lock, we need to recalculate 'needed' | |
1968 | * because either resv_huge_pages or free_huge_pages may have changed. | |
1969 | */ | |
1970 | spin_lock(&hugetlb_lock); | |
a5516438 AK |
1971 | needed = (h->resv_huge_pages + delta) - |
1972 | (h->free_huge_pages + allocated); | |
28073b02 HD |
1973 | if (needed > 0) { |
1974 | if (alloc_ok) | |
1975 | goto retry; | |
1976 | /* | |
1977 | * We were not able to allocate enough pages to | |
1978 | * satisfy the entire reservation so we free what | |
1979 | * we've allocated so far. | |
1980 | */ | |
1981 | goto free; | |
1982 | } | |
e4e574b7 AL |
1983 | /* |
1984 | * The surplus_list now contains _at_least_ the number of extra pages | |
25985edc | 1985 | * needed to accommodate the reservation. Add the appropriate number |
e4e574b7 | 1986 | * of pages to the hugetlb pool and free the extras back to the buddy |
ac09b3a1 AL |
1987 | * allocator. Commit the entire reservation here to prevent another |
1988 | * process from stealing the pages as they are added to the pool but | |
1989 | * before they are reserved. | |
e4e574b7 AL |
1990 | */ |
1991 | needed += allocated; | |
a5516438 | 1992 | h->resv_huge_pages += delta; |
e4e574b7 | 1993 | ret = 0; |
a9869b83 | 1994 | |
19fc3f0a | 1995 | /* Free the needed pages to the hugetlb pool */ |
e4e574b7 | 1996 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) { |
19fc3f0a AL |
1997 | if ((--needed) < 0) |
1998 | break; | |
a9869b83 NH |
1999 | /* |
2000 | * This page is now managed by the hugetlb allocator and has | |
2001 | * no users -- drop the buddy allocator's reference. | |
2002 | */ | |
2003 | put_page_testzero(page); | |
309381fe | 2004 | VM_BUG_ON_PAGE(page_count(page), page); |
a5516438 | 2005 | enqueue_huge_page(h, page); |
19fc3f0a | 2006 | } |
28073b02 | 2007 | free: |
b0365c8d | 2008 | spin_unlock(&hugetlb_lock); |
19fc3f0a AL |
2009 | |
2010 | /* Free unnecessary surplus pages to the buddy allocator */ | |
c0d934ba JK |
2011 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) |
2012 | put_page(page); | |
a9869b83 | 2013 | spin_lock(&hugetlb_lock); |
e4e574b7 AL |
2014 | |
2015 | return ret; | |
2016 | } | |
2017 | ||
2018 | /* | |
e5bbc8a6 MK |
2019 | * This routine has two main purposes: |
2020 | * 1) Decrement the reservation count (resv_huge_pages) by the value passed | |
2021 | * in unused_resv_pages. This corresponds to the prior adjustments made | |
2022 | * to the associated reservation map. | |
2023 | * 2) Free any unused surplus pages that may have been allocated to satisfy | |
2024 | * the reservation. As many as unused_resv_pages may be freed. | |
2025 | * | |
2026 | * Called with hugetlb_lock held. However, the lock could be dropped (and | |
2027 | * reacquired) during calls to cond_resched_lock. Whenever dropping the lock, | |
2028 | * we must make sure nobody else can claim pages we are in the process of | |
2029 | * freeing. Do this by ensuring resv_huge_page always is greater than the | |
2030 | * number of huge pages we plan to free when dropping the lock. | |
e4e574b7 | 2031 | */ |
a5516438 AK |
2032 | static void return_unused_surplus_pages(struct hstate *h, |
2033 | unsigned long unused_resv_pages) | |
e4e574b7 | 2034 | { |
e4e574b7 AL |
2035 | unsigned long nr_pages; |
2036 | ||
aa888a74 | 2037 | /* Cannot return gigantic pages currently */ |
bae7f4ae | 2038 | if (hstate_is_gigantic(h)) |
e5bbc8a6 | 2039 | goto out; |
aa888a74 | 2040 | |
e5bbc8a6 MK |
2041 | /* |
2042 | * Part (or even all) of the reservation could have been backed | |
2043 | * by pre-allocated pages. Only free surplus pages. | |
2044 | */ | |
a5516438 | 2045 | nr_pages = min(unused_resv_pages, h->surplus_huge_pages); |
e4e574b7 | 2046 | |
685f3457 LS |
2047 | /* |
2048 | * We want to release as many surplus pages as possible, spread | |
9b5e5d0f LS |
2049 | * evenly across all nodes with memory. Iterate across these nodes |
2050 | * until we can no longer free unreserved surplus pages. This occurs | |
2051 | * when the nodes with surplus pages have no free pages. | |
2052 | * free_pool_huge_page() will balance the the freed pages across the | |
2053 | * on-line nodes with memory and will handle the hstate accounting. | |
e5bbc8a6 MK |
2054 | * |
2055 | * Note that we decrement resv_huge_pages as we free the pages. If | |
2056 | * we drop the lock, resv_huge_pages will still be sufficiently large | |
2057 | * to cover subsequent pages we may free. | |
685f3457 LS |
2058 | */ |
2059 | while (nr_pages--) { | |
e5bbc8a6 MK |
2060 | h->resv_huge_pages--; |
2061 | unused_resv_pages--; | |
8cebfcd0 | 2062 | if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1)) |
e5bbc8a6 | 2063 | goto out; |
7848a4bf | 2064 | cond_resched_lock(&hugetlb_lock); |
e4e574b7 | 2065 | } |
e5bbc8a6 MK |
2066 | |
2067 | out: | |
2068 | /* Fully uncommit the reservation */ | |
2069 | h->resv_huge_pages -= unused_resv_pages; | |
e4e574b7 AL |
2070 | } |
2071 | ||
5e911373 | 2072 | |
c37f9fb1 | 2073 | /* |
feba16e2 | 2074 | * vma_needs_reservation, vma_commit_reservation and vma_end_reservation |
5e911373 | 2075 | * are used by the huge page allocation routines to manage reservations. |
cf3ad20b MK |
2076 | * |
2077 | * vma_needs_reservation is called to determine if the huge page at addr | |
2078 | * within the vma has an associated reservation. If a reservation is | |
2079 | * needed, the value 1 is returned. The caller is then responsible for | |
2080 | * managing the global reservation and subpool usage counts. After | |
2081 | * the huge page has been allocated, vma_commit_reservation is called | |
feba16e2 MK |
2082 | * to add the page to the reservation map. If the page allocation fails, |
2083 | * the reservation must be ended instead of committed. vma_end_reservation | |
2084 | * is called in such cases. | |
cf3ad20b MK |
2085 | * |
2086 | * In the normal case, vma_commit_reservation returns the same value | |
2087 | * as the preceding vma_needs_reservation call. The only time this | |
2088 | * is not the case is if a reserve map was changed between calls. It | |
2089 | * is the responsibility of the caller to notice the difference and | |
2090 | * take appropriate action. | |
96b96a96 MK |
2091 | * |
2092 | * vma_add_reservation is used in error paths where a reservation must | |
2093 | * be restored when a newly allocated huge page must be freed. It is | |
2094 | * to be called after calling vma_needs_reservation to determine if a | |
2095 | * reservation exists. | |
c37f9fb1 | 2096 | */ |
5e911373 MK |
2097 | enum vma_resv_mode { |
2098 | VMA_NEEDS_RESV, | |
2099 | VMA_COMMIT_RESV, | |
feba16e2 | 2100 | VMA_END_RESV, |
96b96a96 | 2101 | VMA_ADD_RESV, |
5e911373 | 2102 | }; |
cf3ad20b MK |
2103 | static long __vma_reservation_common(struct hstate *h, |
2104 | struct vm_area_struct *vma, unsigned long addr, | |
5e911373 | 2105 | enum vma_resv_mode mode) |
c37f9fb1 | 2106 | { |
4e35f483 JK |
2107 | struct resv_map *resv; |
2108 | pgoff_t idx; | |
cf3ad20b | 2109 | long ret; |
0db9d74e | 2110 | long dummy_out_regions_needed; |
c37f9fb1 | 2111 | |
4e35f483 JK |
2112 | resv = vma_resv_map(vma); |
2113 | if (!resv) | |
84afd99b | 2114 | return 1; |
c37f9fb1 | 2115 | |
4e35f483 | 2116 | idx = vma_hugecache_offset(h, vma, addr); |
5e911373 MK |
2117 | switch (mode) { |
2118 | case VMA_NEEDS_RESV: | |
0db9d74e MA |
2119 | ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed); |
2120 | /* We assume that vma_reservation_* routines always operate on | |
2121 | * 1 page, and that adding to resv map a 1 page entry can only | |
2122 | * ever require 1 region. | |
2123 | */ | |
2124 | VM_BUG_ON(dummy_out_regions_needed != 1); | |
5e911373 MK |
2125 | break; |
2126 | case VMA_COMMIT_RESV: | |
0db9d74e MA |
2127 | ret = region_add(resv, idx, idx + 1, 1); |
2128 | /* region_add calls of range 1 should never fail. */ | |
2129 | VM_BUG_ON(ret < 0); | |
5e911373 | 2130 | break; |
feba16e2 | 2131 | case VMA_END_RESV: |
0db9d74e | 2132 | region_abort(resv, idx, idx + 1, 1); |
5e911373 MK |
2133 | ret = 0; |
2134 | break; | |
96b96a96 | 2135 | case VMA_ADD_RESV: |
0db9d74e MA |
2136 | if (vma->vm_flags & VM_MAYSHARE) { |
2137 | ret = region_add(resv, idx, idx + 1, 1); | |
2138 | /* region_add calls of range 1 should never fail. */ | |
2139 | VM_BUG_ON(ret < 0); | |
2140 | } else { | |
2141 | region_abort(resv, idx, idx + 1, 1); | |
96b96a96 MK |
2142 | ret = region_del(resv, idx, idx + 1); |
2143 | } | |
2144 | break; | |
5e911373 MK |
2145 | default: |
2146 | BUG(); | |
2147 | } | |
84afd99b | 2148 | |
4e35f483 | 2149 | if (vma->vm_flags & VM_MAYSHARE) |
cf3ad20b | 2150 | return ret; |
67961f9d MK |
2151 | else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && ret >= 0) { |
2152 | /* | |
2153 | * In most cases, reserves always exist for private mappings. | |
2154 | * However, a file associated with mapping could have been | |
2155 | * hole punched or truncated after reserves were consumed. | |
2156 | * As subsequent fault on such a range will not use reserves. | |
2157 | * Subtle - The reserve map for private mappings has the | |
2158 | * opposite meaning than that of shared mappings. If NO | |
2159 | * entry is in the reserve map, it means a reservation exists. | |
2160 | * If an entry exists in the reserve map, it means the | |
2161 | * reservation has already been consumed. As a result, the | |
2162 | * return value of this routine is the opposite of the | |
2163 | * value returned from reserve map manipulation routines above. | |
2164 | */ | |
2165 | if (ret) | |
2166 | return 0; | |
2167 | else | |
2168 | return 1; | |
2169 | } | |
4e35f483 | 2170 | else |
cf3ad20b | 2171 | return ret < 0 ? ret : 0; |
c37f9fb1 | 2172 | } |
cf3ad20b MK |
2173 | |
2174 | static long vma_needs_reservation(struct hstate *h, | |
a5516438 | 2175 | struct vm_area_struct *vma, unsigned long addr) |
c37f9fb1 | 2176 | { |
5e911373 | 2177 | return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV); |
cf3ad20b | 2178 | } |
84afd99b | 2179 | |
cf3ad20b MK |
2180 | static long vma_commit_reservation(struct hstate *h, |
2181 | struct vm_area_struct *vma, unsigned long addr) | |
2182 | { | |
5e911373 MK |
2183 | return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV); |
2184 | } | |
2185 | ||
feba16e2 | 2186 | static void vma_end_reservation(struct hstate *h, |
5e911373 MK |
2187 | struct vm_area_struct *vma, unsigned long addr) |
2188 | { | |
feba16e2 | 2189 | (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV); |
c37f9fb1 AW |
2190 | } |
2191 | ||
96b96a96 MK |
2192 | static long vma_add_reservation(struct hstate *h, |
2193 | struct vm_area_struct *vma, unsigned long addr) | |
2194 | { | |
2195 | return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV); | |
2196 | } | |
2197 | ||
2198 | /* | |
2199 | * This routine is called to restore a reservation on error paths. In the | |
2200 | * specific error paths, a huge page was allocated (via alloc_huge_page) | |
2201 | * and is about to be freed. If a reservation for the page existed, | |
2202 | * alloc_huge_page would have consumed the reservation and set PagePrivate | |
2203 | * in the newly allocated page. When the page is freed via free_huge_page, | |
2204 | * the global reservation count will be incremented if PagePrivate is set. | |
2205 | * However, free_huge_page can not adjust the reserve map. Adjust the | |
2206 | * reserve map here to be consistent with global reserve count adjustments | |
2207 | * to be made by free_huge_page. | |
2208 | */ | |
2209 | static void restore_reserve_on_error(struct hstate *h, | |
2210 | struct vm_area_struct *vma, unsigned long address, | |
2211 | struct page *page) | |
2212 | { | |
2213 | if (unlikely(PagePrivate(page))) { | |
2214 | long rc = vma_needs_reservation(h, vma, address); | |
2215 | ||
2216 | if (unlikely(rc < 0)) { | |
2217 | /* | |
2218 | * Rare out of memory condition in reserve map | |
2219 | * manipulation. Clear PagePrivate so that | |
2220 | * global reserve count will not be incremented | |
2221 | * by free_huge_page. This will make it appear | |
2222 | * as though the reservation for this page was | |
2223 | * consumed. This may prevent the task from | |
2224 | * faulting in the page at a later time. This | |
2225 | * is better than inconsistent global huge page | |
2226 | * accounting of reserve counts. | |
2227 | */ | |
2228 | ClearPagePrivate(page); | |
2229 | } else if (rc) { | |
2230 | rc = vma_add_reservation(h, vma, address); | |
2231 | if (unlikely(rc < 0)) | |
2232 | /* | |
2233 | * See above comment about rare out of | |
2234 | * memory condition. | |
2235 | */ | |
2236 | ClearPagePrivate(page); | |
2237 | } else | |
2238 | vma_end_reservation(h, vma, address); | |
2239 | } | |
2240 | } | |
2241 | ||
70c3547e | 2242 | struct page *alloc_huge_page(struct vm_area_struct *vma, |
04f2cbe3 | 2243 | unsigned long addr, int avoid_reserve) |
1da177e4 | 2244 | { |
90481622 | 2245 | struct hugepage_subpool *spool = subpool_vma(vma); |
a5516438 | 2246 | struct hstate *h = hstate_vma(vma); |
348ea204 | 2247 | struct page *page; |
d85f69b0 MK |
2248 | long map_chg, map_commit; |
2249 | long gbl_chg; | |
6d76dcf4 AK |
2250 | int ret, idx; |
2251 | struct hugetlb_cgroup *h_cg; | |
a1e78772 | 2252 | |
6d76dcf4 | 2253 | idx = hstate_index(h); |
a1e78772 | 2254 | /* |
d85f69b0 MK |
2255 | * Examine the region/reserve map to determine if the process |
2256 | * has a reservation for the page to be allocated. A return | |
2257 | * code of zero indicates a reservation exists (no change). | |
a1e78772 | 2258 | */ |
d85f69b0 MK |
2259 | map_chg = gbl_chg = vma_needs_reservation(h, vma, addr); |
2260 | if (map_chg < 0) | |
76dcee75 | 2261 | return ERR_PTR(-ENOMEM); |
d85f69b0 MK |
2262 | |
2263 | /* | |
2264 | * Processes that did not create the mapping will have no | |
2265 | * reserves as indicated by the region/reserve map. Check | |
2266 | * that the allocation will not exceed the subpool limit. | |
2267 | * Allocations for MAP_NORESERVE mappings also need to be | |
2268 | * checked against any subpool limit. | |
2269 | */ | |
2270 | if (map_chg || avoid_reserve) { | |
2271 | gbl_chg = hugepage_subpool_get_pages(spool, 1); | |
2272 | if (gbl_chg < 0) { | |
feba16e2 | 2273 | vma_end_reservation(h, vma, addr); |
76dcee75 | 2274 | return ERR_PTR(-ENOSPC); |
5e911373 | 2275 | } |
1da177e4 | 2276 | |
d85f69b0 MK |
2277 | /* |
2278 | * Even though there was no reservation in the region/reserve | |
2279 | * map, there could be reservations associated with the | |
2280 | * subpool that can be used. This would be indicated if the | |
2281 | * return value of hugepage_subpool_get_pages() is zero. | |
2282 | * However, if avoid_reserve is specified we still avoid even | |
2283 | * the subpool reservations. | |
2284 | */ | |
2285 | if (avoid_reserve) | |
2286 | gbl_chg = 1; | |
2287 | } | |
2288 | ||
6d76dcf4 | 2289 | ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); |
8f34af6f JZ |
2290 | if (ret) |
2291 | goto out_subpool_put; | |
2292 | ||
1da177e4 | 2293 | spin_lock(&hugetlb_lock); |
d85f69b0 MK |
2294 | /* |
2295 | * glb_chg is passed to indicate whether or not a page must be taken | |
2296 | * from the global free pool (global change). gbl_chg == 0 indicates | |
2297 | * a reservation exists for the allocation. | |
2298 | */ | |
2299 | page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg); | |
81a6fcae | 2300 | if (!page) { |
94ae8ba7 | 2301 | spin_unlock(&hugetlb_lock); |
0c397dae | 2302 | page = alloc_buddy_huge_page_with_mpol(h, vma, addr); |
8f34af6f JZ |
2303 | if (!page) |
2304 | goto out_uncharge_cgroup; | |
a88c7695 NH |
2305 | if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) { |
2306 | SetPagePrivate(page); | |
2307 | h->resv_huge_pages--; | |
2308 | } | |
79dbb236 AK |
2309 | spin_lock(&hugetlb_lock); |
2310 | list_move(&page->lru, &h->hugepage_activelist); | |
81a6fcae | 2311 | /* Fall through */ |
68842c9b | 2312 | } |
81a6fcae JK |
2313 | hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page); |
2314 | spin_unlock(&hugetlb_lock); | |
348ea204 | 2315 | |
90481622 | 2316 | set_page_private(page, (unsigned long)spool); |
90d8b7e6 | 2317 | |
d85f69b0 MK |
2318 | map_commit = vma_commit_reservation(h, vma, addr); |
2319 | if (unlikely(map_chg > map_commit)) { | |
33039678 MK |
2320 | /* |
2321 | * The page was added to the reservation map between | |
2322 | * vma_needs_reservation and vma_commit_reservation. | |
2323 | * This indicates a race with hugetlb_reserve_pages. | |
2324 | * Adjust for the subpool count incremented above AND | |
2325 | * in hugetlb_reserve_pages for the same page. Also, | |
2326 | * the reservation count added in hugetlb_reserve_pages | |
2327 | * no longer applies. | |
2328 | */ | |
2329 | long rsv_adjust; | |
2330 | ||
2331 | rsv_adjust = hugepage_subpool_put_pages(spool, 1); | |
2332 | hugetlb_acct_memory(h, -rsv_adjust); | |
2333 | } | |
90d8b7e6 | 2334 | return page; |
8f34af6f JZ |
2335 | |
2336 | out_uncharge_cgroup: | |
2337 | hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); | |
2338 | out_subpool_put: | |
d85f69b0 | 2339 | if (map_chg || avoid_reserve) |
8f34af6f | 2340 | hugepage_subpool_put_pages(spool, 1); |
feba16e2 | 2341 | vma_end_reservation(h, vma, addr); |
8f34af6f | 2342 | return ERR_PTR(-ENOSPC); |
b45b5bd6 DG |
2343 | } |
2344 | ||
e24a1307 AK |
2345 | int alloc_bootmem_huge_page(struct hstate *h) |
2346 | __attribute__ ((weak, alias("__alloc_bootmem_huge_page"))); | |
2347 | int __alloc_bootmem_huge_page(struct hstate *h) | |
aa888a74 AK |
2348 | { |
2349 | struct huge_bootmem_page *m; | |
b2261026 | 2350 | int nr_nodes, node; |
aa888a74 | 2351 | |
b2261026 | 2352 | for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) { |
aa888a74 AK |
2353 | void *addr; |
2354 | ||
eb31d559 | 2355 | addr = memblock_alloc_try_nid_raw( |
8b89a116 | 2356 | huge_page_size(h), huge_page_size(h), |
97ad1087 | 2357 | 0, MEMBLOCK_ALLOC_ACCESSIBLE, node); |
aa888a74 AK |
2358 | if (addr) { |
2359 | /* | |
2360 | * Use the beginning of the huge page to store the | |
2361 | * huge_bootmem_page struct (until gather_bootmem | |
2362 | * puts them into the mem_map). | |
2363 | */ | |
2364 | m = addr; | |
91f47662 | 2365 | goto found; |
aa888a74 | 2366 | } |
aa888a74 AK |
2367 | } |
2368 | return 0; | |
2369 | ||
2370 | found: | |
df994ead | 2371 | BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h))); |
aa888a74 | 2372 | /* Put them into a private list first because mem_map is not up yet */ |
330d6e48 | 2373 | INIT_LIST_HEAD(&m->list); |
aa888a74 AK |
2374 | list_add(&m->list, &huge_boot_pages); |
2375 | m->hstate = h; | |
2376 | return 1; | |
2377 | } | |
2378 | ||
d00181b9 KS |
2379 | static void __init prep_compound_huge_page(struct page *page, |
2380 | unsigned int order) | |
18229df5 AW |
2381 | { |
2382 | if (unlikely(order > (MAX_ORDER - 1))) | |
2383 | prep_compound_gigantic_page(page, order); | |
2384 | else | |
2385 | prep_compound_page(page, order); | |
2386 | } | |
2387 | ||
aa888a74 AK |
2388 | /* Put bootmem huge pages into the standard lists after mem_map is up */ |
2389 | static void __init gather_bootmem_prealloc(void) | |
2390 | { | |
2391 | struct huge_bootmem_page *m; | |
2392 | ||
2393 | list_for_each_entry(m, &huge_boot_pages, list) { | |
40d18ebf | 2394 | struct page *page = virt_to_page(m); |
aa888a74 | 2395 | struct hstate *h = m->hstate; |
ee8f248d | 2396 | |
aa888a74 | 2397 | WARN_ON(page_count(page) != 1); |
18229df5 | 2398 | prep_compound_huge_page(page, h->order); |
ef5a22be | 2399 | WARN_ON(PageReserved(page)); |
aa888a74 | 2400 | prep_new_huge_page(h, page, page_to_nid(page)); |
af0fb9df MH |
2401 | put_page(page); /* free it into the hugepage allocator */ |
2402 | ||
b0320c7b RA |
2403 | /* |
2404 | * If we had gigantic hugepages allocated at boot time, we need | |
2405 | * to restore the 'stolen' pages to totalram_pages in order to | |
2406 | * fix confusing memory reports from free(1) and another | |
2407 | * side-effects, like CommitLimit going negative. | |
2408 | */ | |
bae7f4ae | 2409 | if (hstate_is_gigantic(h)) |
3dcc0571 | 2410 | adjust_managed_page_count(page, 1 << h->order); |
520495fe | 2411 | cond_resched(); |
aa888a74 AK |
2412 | } |
2413 | } | |
2414 | ||
8faa8b07 | 2415 | static void __init hugetlb_hstate_alloc_pages(struct hstate *h) |
1da177e4 LT |
2416 | { |
2417 | unsigned long i; | |
f60858f9 MK |
2418 | nodemask_t *node_alloc_noretry; |
2419 | ||
2420 | if (!hstate_is_gigantic(h)) { | |
2421 | /* | |
2422 | * Bit mask controlling how hard we retry per-node allocations. | |
2423 | * Ignore errors as lower level routines can deal with | |
2424 | * node_alloc_noretry == NULL. If this kmalloc fails at boot | |
2425 | * time, we are likely in bigger trouble. | |
2426 | */ | |
2427 | node_alloc_noretry = kmalloc(sizeof(*node_alloc_noretry), | |
2428 | GFP_KERNEL); | |
2429 | } else { | |
2430 | /* allocations done at boot time */ | |
2431 | node_alloc_noretry = NULL; | |
2432 | } | |
2433 | ||
2434 | /* bit mask controlling how hard we retry per-node allocations */ | |
2435 | if (node_alloc_noretry) | |
2436 | nodes_clear(*node_alloc_noretry); | |
a5516438 | 2437 | |
e5ff2159 | 2438 | for (i = 0; i < h->max_huge_pages; ++i) { |
bae7f4ae | 2439 | if (hstate_is_gigantic(h)) { |
aa888a74 AK |
2440 | if (!alloc_bootmem_huge_page(h)) |
2441 | break; | |
0c397dae | 2442 | } else if (!alloc_pool_huge_page(h, |
f60858f9 MK |
2443 | &node_states[N_MEMORY], |
2444 | node_alloc_noretry)) | |
1da177e4 | 2445 | break; |
69ed779a | 2446 | cond_resched(); |
1da177e4 | 2447 | } |
d715cf80 LH |
2448 | if (i < h->max_huge_pages) { |
2449 | char buf[32]; | |
2450 | ||
c6247f72 | 2451 | string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); |
d715cf80 LH |
2452 | pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n", |
2453 | h->max_huge_pages, buf, i); | |
2454 | h->max_huge_pages = i; | |
2455 | } | |
f60858f9 MK |
2456 | |
2457 | kfree(node_alloc_noretry); | |
e5ff2159 AK |
2458 | } |
2459 | ||
2460 | static void __init hugetlb_init_hstates(void) | |
2461 | { | |
2462 | struct hstate *h; | |
2463 | ||
2464 | for_each_hstate(h) { | |
641844f5 NH |
2465 | if (minimum_order > huge_page_order(h)) |
2466 | minimum_order = huge_page_order(h); | |
2467 | ||
8faa8b07 | 2468 | /* oversize hugepages were init'ed in early boot */ |
bae7f4ae | 2469 | if (!hstate_is_gigantic(h)) |
8faa8b07 | 2470 | hugetlb_hstate_alloc_pages(h); |
e5ff2159 | 2471 | } |
641844f5 | 2472 | VM_BUG_ON(minimum_order == UINT_MAX); |
e5ff2159 AK |
2473 | } |
2474 | ||
2475 | static void __init report_hugepages(void) | |
2476 | { | |
2477 | struct hstate *h; | |
2478 | ||
2479 | for_each_hstate(h) { | |
4abd32db | 2480 | char buf[32]; |
c6247f72 MW |
2481 | |
2482 | string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); | |
ffb22af5 | 2483 | pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n", |
c6247f72 | 2484 | buf, h->free_huge_pages); |
e5ff2159 AK |
2485 | } |
2486 | } | |
2487 | ||
1da177e4 | 2488 | #ifdef CONFIG_HIGHMEM |
6ae11b27 LS |
2489 | static void try_to_free_low(struct hstate *h, unsigned long count, |
2490 | nodemask_t *nodes_allowed) | |
1da177e4 | 2491 | { |
4415cc8d CL |
2492 | int i; |
2493 | ||
bae7f4ae | 2494 | if (hstate_is_gigantic(h)) |
aa888a74 AK |
2495 | return; |
2496 | ||
6ae11b27 | 2497 | for_each_node_mask(i, *nodes_allowed) { |
1da177e4 | 2498 | struct page *page, *next; |
a5516438 AK |
2499 | struct list_head *freel = &h->hugepage_freelists[i]; |
2500 | list_for_each_entry_safe(page, next, freel, lru) { | |
2501 | if (count >= h->nr_huge_pages) | |
6b0c880d | 2502 | return; |
1da177e4 LT |
2503 | if (PageHighMem(page)) |
2504 | continue; | |
2505 | list_del(&page->lru); | |
e5ff2159 | 2506 | update_and_free_page(h, page); |
a5516438 AK |
2507 | h->free_huge_pages--; |
2508 | h->free_huge_pages_node[page_to_nid(page)]--; | |
1da177e4 LT |
2509 | } |
2510 | } | |
2511 | } | |
2512 | #else | |
6ae11b27 LS |
2513 | static inline void try_to_free_low(struct hstate *h, unsigned long count, |
2514 | nodemask_t *nodes_allowed) | |
1da177e4 LT |
2515 | { |
2516 | } | |
2517 | #endif | |
2518 | ||
20a0307c WF |
2519 | /* |
2520 | * Increment or decrement surplus_huge_pages. Keep node-specific counters | |
2521 | * balanced by operating on them in a round-robin fashion. | |
2522 | * Returns 1 if an adjustment was made. | |
2523 | */ | |
6ae11b27 LS |
2524 | static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, |
2525 | int delta) | |
20a0307c | 2526 | { |
b2261026 | 2527 | int nr_nodes, node; |
20a0307c WF |
2528 | |
2529 | VM_BUG_ON(delta != -1 && delta != 1); | |
20a0307c | 2530 | |
b2261026 JK |
2531 | if (delta < 0) { |
2532 | for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { | |
2533 | if (h->surplus_huge_pages_node[node]) | |
2534 | goto found; | |
e8c5c824 | 2535 | } |
b2261026 JK |
2536 | } else { |
2537 | for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { | |
2538 | if (h->surplus_huge_pages_node[node] < | |
2539 | h->nr_huge_pages_node[node]) | |
2540 | goto found; | |
e8c5c824 | 2541 | } |
b2261026 JK |
2542 | } |
2543 | return 0; | |
20a0307c | 2544 | |
b2261026 JK |
2545 | found: |
2546 | h->surplus_huge_pages += delta; | |
2547 | h->surplus_huge_pages_node[node] += delta; | |
2548 | return 1; | |
20a0307c WF |
2549 | } |
2550 | ||
a5516438 | 2551 | #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) |
fd875dca | 2552 | static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid, |
4eb0716e | 2553 | nodemask_t *nodes_allowed) |
1da177e4 | 2554 | { |
7893d1d5 | 2555 | unsigned long min_count, ret; |
f60858f9 MK |
2556 | NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL); |
2557 | ||
2558 | /* | |
2559 | * Bit mask controlling how hard we retry per-node allocations. | |
2560 | * If we can not allocate the bit mask, do not attempt to allocate | |
2561 | * the requested huge pages. | |
2562 | */ | |
2563 | if (node_alloc_noretry) | |
2564 | nodes_clear(*node_alloc_noretry); | |
2565 | else | |
2566 | return -ENOMEM; | |
1da177e4 | 2567 | |
4eb0716e AG |
2568 | spin_lock(&hugetlb_lock); |
2569 | ||
fd875dca MK |
2570 | /* |
2571 | * Check for a node specific request. | |
2572 | * Changing node specific huge page count may require a corresponding | |
2573 | * change to the global count. In any case, the passed node mask | |
2574 | * (nodes_allowed) will restrict alloc/free to the specified node. | |
2575 | */ | |
2576 | if (nid != NUMA_NO_NODE) { | |
2577 | unsigned long old_count = count; | |
2578 | ||
2579 | count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; | |
2580 | /* | |
2581 | * User may have specified a large count value which caused the | |
2582 | * above calculation to overflow. In this case, they wanted | |
2583 | * to allocate as many huge pages as possible. Set count to | |
2584 | * largest possible value to align with their intention. | |
2585 | */ | |
2586 | if (count < old_count) | |
2587 | count = ULONG_MAX; | |
2588 | } | |
2589 | ||
4eb0716e AG |
2590 | /* |
2591 | * Gigantic pages runtime allocation depend on the capability for large | |
2592 | * page range allocation. | |
2593 | * If the system does not provide this feature, return an error when | |
2594 | * the user tries to allocate gigantic pages but let the user free the | |
2595 | * boottime allocated gigantic pages. | |
2596 | */ | |
2597 | if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) { | |
2598 | if (count > persistent_huge_pages(h)) { | |
2599 | spin_unlock(&hugetlb_lock); | |
f60858f9 | 2600 | NODEMASK_FREE(node_alloc_noretry); |
4eb0716e AG |
2601 | return -EINVAL; |
2602 | } | |
2603 | /* Fall through to decrease pool */ | |
2604 | } | |
aa888a74 | 2605 | |
7893d1d5 AL |
2606 | /* |
2607 | * Increase the pool size | |
2608 | * First take pages out of surplus state. Then make up the | |
2609 | * remaining difference by allocating fresh huge pages. | |
d1c3fb1f | 2610 | * |
0c397dae | 2611 | * We might race with alloc_surplus_huge_page() here and be unable |
d1c3fb1f NA |
2612 | * to convert a surplus huge page to a normal huge page. That is |
2613 | * not critical, though, it just means the overall size of the | |
2614 | * pool might be one hugepage larger than it needs to be, but | |
2615 | * within all the constraints specified by the sysctls. | |
7893d1d5 | 2616 | */ |
a5516438 | 2617 | while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { |
6ae11b27 | 2618 | if (!adjust_pool_surplus(h, nodes_allowed, -1)) |
7893d1d5 AL |
2619 | break; |
2620 | } | |
2621 | ||
a5516438 | 2622 | while (count > persistent_huge_pages(h)) { |
7893d1d5 AL |
2623 | /* |
2624 | * If this allocation races such that we no longer need the | |
2625 | * page, free_huge_page will handle it by freeing the page | |
2626 | * and reducing the surplus. | |
2627 | */ | |
2628 | spin_unlock(&hugetlb_lock); | |
649920c6 JH |
2629 | |
2630 | /* yield cpu to avoid soft lockup */ | |
2631 | cond_resched(); | |
2632 | ||
f60858f9 MK |
2633 | ret = alloc_pool_huge_page(h, nodes_allowed, |
2634 | node_alloc_noretry); | |
7893d1d5 AL |
2635 | spin_lock(&hugetlb_lock); |
2636 | if (!ret) | |
2637 | goto out; | |
2638 | ||
536240f2 MG |
2639 | /* Bail for signals. Probably ctrl-c from user */ |
2640 | if (signal_pending(current)) | |
2641 | goto out; | |
7893d1d5 | 2642 | } |
7893d1d5 AL |
2643 | |
2644 | /* | |
2645 | * Decrease the pool size | |
2646 | * First return free pages to the buddy allocator (being careful | |
2647 | * to keep enough around to satisfy reservations). Then place | |
2648 | * pages into surplus state as needed so the pool will shrink | |
2649 | * to the desired size as pages become free. | |
d1c3fb1f NA |
2650 | * |
2651 | * By placing pages into the surplus state independent of the | |
2652 | * overcommit value, we are allowing the surplus pool size to | |
2653 | * exceed overcommit. There are few sane options here. Since | |
0c397dae | 2654 | * alloc_surplus_huge_page() is checking the global counter, |
d1c3fb1f NA |
2655 | * though, we'll note that we're not allowed to exceed surplus |
2656 | * and won't grow the pool anywhere else. Not until one of the | |
2657 | * sysctls are changed, or the surplus pages go out of use. | |
7893d1d5 | 2658 | */ |
a5516438 | 2659 | min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; |
6b0c880d | 2660 | min_count = max(count, min_count); |
6ae11b27 | 2661 | try_to_free_low(h, min_count, nodes_allowed); |
a5516438 | 2662 | while (min_count < persistent_huge_pages(h)) { |
6ae11b27 | 2663 | if (!free_pool_huge_page(h, nodes_allowed, 0)) |
1da177e4 | 2664 | break; |
55f67141 | 2665 | cond_resched_lock(&hugetlb_lock); |
1da177e4 | 2666 | } |
a5516438 | 2667 | while (count < persistent_huge_pages(h)) { |
6ae11b27 | 2668 | if (!adjust_pool_surplus(h, nodes_allowed, 1)) |
7893d1d5 AL |
2669 | break; |
2670 | } | |
2671 | out: | |
4eb0716e | 2672 | h->max_huge_pages = persistent_huge_pages(h); |
1da177e4 | 2673 | spin_unlock(&hugetlb_lock); |
4eb0716e | 2674 | |
f60858f9 MK |
2675 | NODEMASK_FREE(node_alloc_noretry); |
2676 | ||
4eb0716e | 2677 | return 0; |
1da177e4 LT |
2678 | } |
2679 | ||
a3437870 NA |
2680 | #define HSTATE_ATTR_RO(_name) \ |
2681 | static struct kobj_attribute _name##_attr = __ATTR_RO(_name) | |
2682 | ||
2683 | #define HSTATE_ATTR(_name) \ | |
2684 | static struct kobj_attribute _name##_attr = \ | |
2685 | __ATTR(_name, 0644, _name##_show, _name##_store) | |
2686 | ||
2687 | static struct kobject *hugepages_kobj; | |
2688 | static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; | |
2689 | ||
9a305230 LS |
2690 | static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp); |
2691 | ||
2692 | static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp) | |
a3437870 NA |
2693 | { |
2694 | int i; | |
9a305230 | 2695 | |
a3437870 | 2696 | for (i = 0; i < HUGE_MAX_HSTATE; i++) |
9a305230 LS |
2697 | if (hstate_kobjs[i] == kobj) { |
2698 | if (nidp) | |
2699 | *nidp = NUMA_NO_NODE; | |
a3437870 | 2700 | return &hstates[i]; |
9a305230 LS |
2701 | } |
2702 | ||
2703 | return kobj_to_node_hstate(kobj, nidp); | |
a3437870 NA |
2704 | } |
2705 | ||
06808b08 | 2706 | static ssize_t nr_hugepages_show_common(struct kobject *kobj, |
a3437870 NA |
2707 | struct kobj_attribute *attr, char *buf) |
2708 | { | |
9a305230 LS |
2709 | struct hstate *h; |
2710 | unsigned long nr_huge_pages; | |
2711 | int nid; | |
2712 | ||
2713 | h = kobj_to_hstate(kobj, &nid); | |
2714 | if (nid == NUMA_NO_NODE) | |
2715 | nr_huge_pages = h->nr_huge_pages; | |
2716 | else | |
2717 | nr_huge_pages = h->nr_huge_pages_node[nid]; | |
2718 | ||
2719 | return sprintf(buf, "%lu\n", nr_huge_pages); | |
a3437870 | 2720 | } |
adbe8726 | 2721 | |
238d3c13 DR |
2722 | static ssize_t __nr_hugepages_store_common(bool obey_mempolicy, |
2723 | struct hstate *h, int nid, | |
2724 | unsigned long count, size_t len) | |
a3437870 NA |
2725 | { |
2726 | int err; | |
2d0adf7e | 2727 | nodemask_t nodes_allowed, *n_mask; |
a3437870 | 2728 | |
2d0adf7e OS |
2729 | if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) |
2730 | return -EINVAL; | |
adbe8726 | 2731 | |
9a305230 LS |
2732 | if (nid == NUMA_NO_NODE) { |
2733 | /* | |
2734 | * global hstate attribute | |
2735 | */ | |
2736 | if (!(obey_mempolicy && | |
2d0adf7e OS |
2737 | init_nodemask_of_mempolicy(&nodes_allowed))) |
2738 | n_mask = &node_states[N_MEMORY]; | |
2739 | else | |
2740 | n_mask = &nodes_allowed; | |
2741 | } else { | |
9a305230 | 2742 | /* |
fd875dca MK |
2743 | * Node specific request. count adjustment happens in |
2744 | * set_max_huge_pages() after acquiring hugetlb_lock. | |
9a305230 | 2745 | */ |
2d0adf7e OS |
2746 | init_nodemask_of_node(&nodes_allowed, nid); |
2747 | n_mask = &nodes_allowed; | |
fd875dca | 2748 | } |
9a305230 | 2749 | |
2d0adf7e | 2750 | err = set_max_huge_pages(h, count, nid, n_mask); |
06808b08 | 2751 | |
4eb0716e | 2752 | return err ? err : len; |
06808b08 LS |
2753 | } |
2754 | ||
238d3c13 DR |
2755 | static ssize_t nr_hugepages_store_common(bool obey_mempolicy, |
2756 | struct kobject *kobj, const char *buf, | |
2757 | size_t len) | |
2758 | { | |
2759 | struct hstate *h; | |
2760 | unsigned long count; | |
2761 | int nid; | |
2762 | int err; | |
2763 | ||
2764 | err = kstrtoul(buf, 10, &count); | |
2765 | if (err) | |
2766 | return err; | |
2767 | ||
2768 | h = kobj_to_hstate(kobj, &nid); | |
2769 | return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len); | |
2770 | } | |
2771 | ||
06808b08 LS |
2772 | static ssize_t nr_hugepages_show(struct kobject *kobj, |
2773 | struct kobj_attribute *attr, char *buf) | |
2774 | { | |
2775 | return nr_hugepages_show_common(kobj, attr, buf); | |
2776 | } | |
2777 | ||
2778 | static ssize_t nr_hugepages_store(struct kobject *kobj, | |
2779 | struct kobj_attribute *attr, const char *buf, size_t len) | |
2780 | { | |
238d3c13 | 2781 | return nr_hugepages_store_common(false, kobj, buf, len); |
a3437870 NA |
2782 | } |
2783 | HSTATE_ATTR(nr_hugepages); | |
2784 | ||
06808b08 LS |
2785 | #ifdef CONFIG_NUMA |
2786 | ||
2787 | /* | |
2788 | * hstate attribute for optionally mempolicy-based constraint on persistent | |
2789 | * huge page alloc/free. | |
2790 | */ | |
2791 | static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj, | |
2792 | struct kobj_attribute *attr, char *buf) | |
2793 | { | |
2794 | return nr_hugepages_show_common(kobj, attr, buf); | |
2795 | } | |
2796 | ||
2797 | static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj, | |
2798 | struct kobj_attribute *attr, const char *buf, size_t len) | |
2799 | { | |
238d3c13 | 2800 | return nr_hugepages_store_common(true, kobj, buf, len); |
06808b08 LS |
2801 | } |
2802 | HSTATE_ATTR(nr_hugepages_mempolicy); | |
2803 | #endif | |
2804 | ||
2805 | ||
a3437870 NA |
2806 | static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, |
2807 | struct kobj_attribute *attr, char *buf) | |
2808 | { | |
9a305230 | 2809 | struct hstate *h = kobj_to_hstate(kobj, NULL); |
a3437870 NA |
2810 | return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages); |
2811 | } | |
adbe8726 | 2812 | |
a3437870 NA |
2813 | static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, |
2814 | struct kobj_attribute *attr, const char *buf, size_t count) | |
2815 | { | |
2816 | int err; | |
2817 | unsigned long input; | |
9a305230 | 2818 | struct hstate *h = kobj_to_hstate(kobj, NULL); |
a3437870 | 2819 | |
bae7f4ae | 2820 | if (hstate_is_gigantic(h)) |
adbe8726 EM |
2821 | return -EINVAL; |
2822 | ||
3dbb95f7 | 2823 | err = kstrtoul(buf, 10, &input); |
a3437870 | 2824 | if (err) |
73ae31e5 | 2825 | return err; |
a3437870 NA |
2826 | |
2827 | spin_lock(&hugetlb_lock); | |
2828 | h->nr_overcommit_huge_pages = input; | |
2829 | spin_unlock(&hugetlb_lock); | |
2830 | ||
2831 | return count; | |
2832 | } | |
2833 | HSTATE_ATTR(nr_overcommit_hugepages); | |
2834 | ||
2835 | static ssize_t free_hugepages_show(struct kobject *kobj, | |
2836 | struct kobj_attribute *attr, char *buf) | |
2837 | { | |
9a305230 LS |
2838 | struct hstate *h; |
2839 | unsigned long free_huge_pages; | |
2840 | int nid; | |
2841 | ||
2842 | h = kobj_to_hstate(kobj, &nid); | |
2843 | if (nid == NUMA_NO_NODE) | |
2844 | free_huge_pages = h->free_huge_pages; | |
2845 | else | |
2846 | free_huge_pages = h->free_huge_pages_node[nid]; | |
2847 | ||
2848 | return sprintf(buf, "%lu\n", free_huge_pages); | |
a3437870 NA |
2849 | } |
2850 | HSTATE_ATTR_RO(free_hugepages); | |
2851 | ||
2852 | static ssize_t resv_hugepages_show(struct kobject *kobj, | |
2853 | struct kobj_attribute *attr, char *buf) | |
2854 | { | |
9a305230 | 2855 | struct hstate *h = kobj_to_hstate(kobj, NULL); |
a3437870 NA |
2856 | return sprintf(buf, "%lu\n", h->resv_huge_pages); |
2857 | } | |
2858 | HSTATE_ATTR_RO(resv_hugepages); | |
2859 | ||
2860 | static ssize_t surplus_hugepages_show(struct kobject *kobj, | |
2861 | struct kobj_attribute *attr, char *buf) | |
2862 | { | |
9a305230 LS |
2863 | struct hstate *h; |
2864 | unsigned long surplus_huge_pages; | |
2865 | int nid; | |
2866 | ||
2867 | h = kobj_to_hstate(kobj, &nid); | |
2868 | if (nid == NUMA_NO_NODE) | |
2869 | surplus_huge_pages = h->surplus_huge_pages; | |
2870 | else | |
2871 | surplus_huge_pages = h->surplus_huge_pages_node[nid]; | |
2872 | ||
2873 | return sprintf(buf, "%lu\n", surplus_huge_pages); | |
a3437870 NA |
2874 | } |
2875 | HSTATE_ATTR_RO(surplus_hugepages); | |
2876 | ||
2877 | static struct attribute *hstate_attrs[] = { | |
2878 | &nr_hugepages_attr.attr, | |
2879 | &nr_overcommit_hugepages_attr.attr, | |
2880 | &free_hugepages_attr.attr, | |
2881 | &resv_hugepages_attr.attr, | |
2882 | &surplus_hugepages_attr.attr, | |
06808b08 LS |
2883 | #ifdef CONFIG_NUMA |
2884 | &nr_hugepages_mempolicy_attr.attr, | |
2885 | #endif | |
a3437870 NA |
2886 | NULL, |
2887 | }; | |
2888 | ||
67e5ed96 | 2889 | static const struct attribute_group hstate_attr_group = { |
a3437870 NA |
2890 | .attrs = hstate_attrs, |
2891 | }; | |
2892 | ||
094e9539 JM |
2893 | static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, |
2894 | struct kobject **hstate_kobjs, | |
67e5ed96 | 2895 | const struct attribute_group *hstate_attr_group) |
a3437870 NA |
2896 | { |
2897 | int retval; | |
972dc4de | 2898 | int hi = hstate_index(h); |
a3437870 | 2899 | |
9a305230 LS |
2900 | hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); |
2901 | if (!hstate_kobjs[hi]) | |
a3437870 NA |
2902 | return -ENOMEM; |
2903 | ||
9a305230 | 2904 | retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group); |
a3437870 | 2905 | if (retval) |
9a305230 | 2906 | kobject_put(hstate_kobjs[hi]); |
a3437870 NA |
2907 | |
2908 | return retval; | |
2909 | } | |
2910 | ||
2911 | static void __init hugetlb_sysfs_init(void) | |
2912 | { | |
2913 | struct hstate *h; | |
2914 | int err; | |
2915 | ||
2916 | hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); | |
2917 | if (!hugepages_kobj) | |
2918 | return; | |
2919 | ||
2920 | for_each_hstate(h) { | |
9a305230 LS |
2921 | err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, |
2922 | hstate_kobjs, &hstate_attr_group); | |
a3437870 | 2923 | if (err) |
ffb22af5 | 2924 | pr_err("Hugetlb: Unable to add hstate %s", h->name); |
a3437870 NA |
2925 | } |
2926 | } | |
2927 | ||
9a305230 LS |
2928 | #ifdef CONFIG_NUMA |
2929 | ||
2930 | /* | |
2931 | * node_hstate/s - associate per node hstate attributes, via their kobjects, | |
10fbcf4c KS |
2932 | * with node devices in node_devices[] using a parallel array. The array |
2933 | * index of a node device or _hstate == node id. | |
2934 | * This is here to avoid any static dependency of the node device driver, in | |
9a305230 LS |
2935 | * the base kernel, on the hugetlb module. |
2936 | */ | |
2937 | struct node_hstate { | |
2938 | struct kobject *hugepages_kobj; | |
2939 | struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; | |
2940 | }; | |
b4e289a6 | 2941 | static struct node_hstate node_hstates[MAX_NUMNODES]; |
9a305230 LS |
2942 | |
2943 | /* | |
10fbcf4c | 2944 | * A subset of global hstate attributes for node devices |
9a305230 LS |
2945 | */ |
2946 | static struct attribute *per_node_hstate_attrs[] = { | |
2947 | &nr_hugepages_attr.attr, | |
2948 | &free_hugepages_attr.attr, | |
2949 | &surplus_hugepages_attr.attr, | |
2950 | NULL, | |
2951 | }; | |
2952 | ||
67e5ed96 | 2953 | static const struct attribute_group per_node_hstate_attr_group = { |
9a305230 LS |
2954 | .attrs = per_node_hstate_attrs, |
2955 | }; | |
2956 | ||
2957 | /* | |
10fbcf4c | 2958 | * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj. |
9a305230 LS |
2959 | * Returns node id via non-NULL nidp. |
2960 | */ | |
2961 | static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) | |
2962 | { | |
2963 | int nid; | |
2964 | ||
2965 | for (nid = 0; nid < nr_node_ids; nid++) { | |
2966 | struct node_hstate *nhs = &node_hstates[nid]; | |
2967 | int i; | |
2968 | for (i = 0; i < HUGE_MAX_HSTATE; i++) | |
2969 | if (nhs->hstate_kobjs[i] == kobj) { | |
2970 | if (nidp) | |
2971 | *nidp = nid; | |
2972 | return &hstates[i]; | |
2973 | } | |
2974 | } | |
2975 | ||
2976 | BUG(); | |
2977 | return NULL; | |
2978 | } | |
2979 | ||
2980 | /* | |
10fbcf4c | 2981 | * Unregister hstate attributes from a single node device. |
9a305230 LS |
2982 | * No-op if no hstate attributes attached. |
2983 | */ | |
3cd8b44f | 2984 | static void hugetlb_unregister_node(struct node *node) |
9a305230 LS |
2985 | { |
2986 | struct hstate *h; | |
10fbcf4c | 2987 | struct node_hstate *nhs = &node_hstates[node->dev.id]; |
9a305230 LS |
2988 | |
2989 | if (!nhs->hugepages_kobj) | |
9b5e5d0f | 2990 | return; /* no hstate attributes */ |
9a305230 | 2991 | |
972dc4de AK |
2992 | for_each_hstate(h) { |
2993 | int idx = hstate_index(h); | |
2994 | if (nhs->hstate_kobjs[idx]) { | |
2995 | kobject_put(nhs->hstate_kobjs[idx]); | |
2996 | nhs->hstate_kobjs[idx] = NULL; | |
9a305230 | 2997 | } |
972dc4de | 2998 | } |
9a305230 LS |
2999 | |
3000 | kobject_put(nhs->hugepages_kobj); | |
3001 | nhs->hugepages_kobj = NULL; | |
3002 | } | |
3003 | ||
9a305230 LS |
3004 | |
3005 | /* | |
10fbcf4c | 3006 | * Register hstate attributes for a single node device. |
9a305230 LS |
3007 | * No-op if attributes already registered. |
3008 | */ | |
3cd8b44f | 3009 | static void hugetlb_register_node(struct node *node) |
9a305230 LS |
3010 | { |
3011 | struct hstate *h; | |
10fbcf4c | 3012 | struct node_hstate *nhs = &node_hstates[node->dev.id]; |
9a305230 LS |
3013 | int err; |
3014 | ||
3015 | if (nhs->hugepages_kobj) | |
3016 | return; /* already allocated */ | |
3017 | ||
3018 | nhs->hugepages_kobj = kobject_create_and_add("hugepages", | |
10fbcf4c | 3019 | &node->dev.kobj); |
9a305230 LS |
3020 | if (!nhs->hugepages_kobj) |
3021 | return; | |
3022 | ||
3023 | for_each_hstate(h) { | |
3024 | err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, | |
3025 | nhs->hstate_kobjs, | |
3026 | &per_node_hstate_attr_group); | |
3027 | if (err) { | |
ffb22af5 AM |
3028 | pr_err("Hugetlb: Unable to add hstate %s for node %d\n", |
3029 | h->name, node->dev.id); | |
9a305230 LS |
3030 | hugetlb_unregister_node(node); |
3031 | break; | |
3032 | } | |
3033 | } | |
3034 | } | |
3035 | ||
3036 | /* | |
9b5e5d0f | 3037 | * hugetlb init time: register hstate attributes for all registered node |
10fbcf4c KS |
3038 | * devices of nodes that have memory. All on-line nodes should have |
3039 | * registered their associated device by this time. | |
9a305230 | 3040 | */ |
7d9ca000 | 3041 | static void __init hugetlb_register_all_nodes(void) |
9a305230 LS |
3042 | { |
3043 | int nid; | |
3044 | ||
8cebfcd0 | 3045 | for_each_node_state(nid, N_MEMORY) { |
8732794b | 3046 | struct node *node = node_devices[nid]; |
10fbcf4c | 3047 | if (node->dev.id == nid) |
9a305230 LS |
3048 | hugetlb_register_node(node); |
3049 | } | |
3050 | ||
3051 | /* | |
10fbcf4c | 3052 | * Let the node device driver know we're here so it can |
9a305230 LS |
3053 | * [un]register hstate attributes on node hotplug. |
3054 | */ | |
3055 | register_hugetlbfs_with_node(hugetlb_register_node, | |
3056 | hugetlb_unregister_node); | |
3057 | } | |
3058 | #else /* !CONFIG_NUMA */ | |
3059 | ||
3060 | static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) | |
3061 | { | |
3062 | BUG(); | |
3063 | if (nidp) | |
3064 | *nidp = -1; | |
3065 | return NULL; | |
3066 | } | |
3067 | ||
9a305230 LS |
3068 | static void hugetlb_register_all_nodes(void) { } |
3069 | ||
3070 | #endif | |
3071 | ||
a3437870 NA |
3072 | static int __init hugetlb_init(void) |
3073 | { | |
8382d914 DB |
3074 | int i; |
3075 | ||
457c1b27 | 3076 | if (!hugepages_supported()) |
0ef89d25 | 3077 | return 0; |
a3437870 | 3078 | |
e11bfbfc | 3079 | if (!size_to_hstate(default_hstate_size)) { |
d715cf80 LH |
3080 | if (default_hstate_size != 0) { |
3081 | pr_err("HugeTLB: unsupported default_hugepagesz %lu. Reverting to %lu\n", | |
3082 | default_hstate_size, HPAGE_SIZE); | |
3083 | } | |
3084 | ||
e11bfbfc NP |
3085 | default_hstate_size = HPAGE_SIZE; |
3086 | if (!size_to_hstate(default_hstate_size)) | |
3087 | hugetlb_add_hstate(HUGETLB_PAGE_ORDER); | |
a3437870 | 3088 | } |
972dc4de | 3089 | default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size)); |
f8b74815 VT |
3090 | if (default_hstate_max_huge_pages) { |
3091 | if (!default_hstate.max_huge_pages) | |
3092 | default_hstate.max_huge_pages = default_hstate_max_huge_pages; | |
3093 | } | |
a3437870 NA |
3094 | |
3095 | hugetlb_init_hstates(); | |
aa888a74 | 3096 | gather_bootmem_prealloc(); |
a3437870 NA |
3097 | report_hugepages(); |
3098 | ||
3099 | hugetlb_sysfs_init(); | |
9a305230 | 3100 | hugetlb_register_all_nodes(); |
7179e7bf | 3101 | hugetlb_cgroup_file_init(); |
9a305230 | 3102 | |
8382d914 DB |
3103 | #ifdef CONFIG_SMP |
3104 | num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); | |
3105 | #else | |
3106 | num_fault_mutexes = 1; | |
3107 | #endif | |
c672c7f2 | 3108 | hugetlb_fault_mutex_table = |
6da2ec56 KC |
3109 | kmalloc_array(num_fault_mutexes, sizeof(struct mutex), |
3110 | GFP_KERNEL); | |
c672c7f2 | 3111 | BUG_ON(!hugetlb_fault_mutex_table); |
8382d914 DB |
3112 | |
3113 | for (i = 0; i < num_fault_mutexes; i++) | |
c672c7f2 | 3114 | mutex_init(&hugetlb_fault_mutex_table[i]); |
a3437870 NA |
3115 | return 0; |
3116 | } | |
3e89e1c5 | 3117 | subsys_initcall(hugetlb_init); |
a3437870 NA |
3118 | |
3119 | /* Should be called on processing a hugepagesz=... option */ | |
9fee021d VT |
3120 | void __init hugetlb_bad_size(void) |
3121 | { | |
3122 | parsed_valid_hugepagesz = false; | |
3123 | } | |
3124 | ||
d00181b9 | 3125 | void __init hugetlb_add_hstate(unsigned int order) |
a3437870 NA |
3126 | { |
3127 | struct hstate *h; | |
8faa8b07 AK |
3128 | unsigned long i; |
3129 | ||
a3437870 | 3130 | if (size_to_hstate(PAGE_SIZE << order)) { |
598d8091 | 3131 | pr_warn("hugepagesz= specified twice, ignoring\n"); |
a3437870 NA |
3132 | return; |
3133 | } | |
47d38344 | 3134 | BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); |
a3437870 | 3135 | BUG_ON(order == 0); |
47d38344 | 3136 | h = &hstates[hugetlb_max_hstate++]; |
a3437870 NA |
3137 | h->order = order; |
3138 | h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); | |
8faa8b07 AK |
3139 | h->nr_huge_pages = 0; |
3140 | h->free_huge_pages = 0; | |
3141 | for (i = 0; i < MAX_NUMNODES; ++i) | |
3142 | INIT_LIST_HEAD(&h->hugepage_freelists[i]); | |
0edaecfa | 3143 | INIT_LIST_HEAD(&h->hugepage_activelist); |
54f18d35 AM |
3144 | h->next_nid_to_alloc = first_memory_node; |
3145 | h->next_nid_to_free = first_memory_node; | |
a3437870 NA |
3146 | snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", |
3147 | huge_page_size(h)/1024); | |
8faa8b07 | 3148 | |
a3437870 NA |
3149 | parsed_hstate = h; |
3150 | } | |
3151 | ||
e11bfbfc | 3152 | static int __init hugetlb_nrpages_setup(char *s) |
a3437870 NA |
3153 | { |
3154 | unsigned long *mhp; | |
8faa8b07 | 3155 | static unsigned long *last_mhp; |
a3437870 | 3156 | |
9fee021d VT |
3157 | if (!parsed_valid_hugepagesz) { |
3158 | pr_warn("hugepages = %s preceded by " | |
3159 | "an unsupported hugepagesz, ignoring\n", s); | |
3160 | parsed_valid_hugepagesz = true; | |
3161 | return 1; | |
3162 | } | |
a3437870 | 3163 | /* |
47d38344 | 3164 | * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet, |
a3437870 NA |
3165 | * so this hugepages= parameter goes to the "default hstate". |
3166 | */ | |
9fee021d | 3167 | else if (!hugetlb_max_hstate) |
a3437870 NA |
3168 | mhp = &default_hstate_max_huge_pages; |
3169 | else | |
3170 | mhp = &parsed_hstate->max_huge_pages; | |
3171 | ||
8faa8b07 | 3172 | if (mhp == last_mhp) { |
598d8091 | 3173 | pr_warn("hugepages= specified twice without interleaving hugepagesz=, ignoring\n"); |
8faa8b07 AK |
3174 | return 1; |
3175 | } | |
3176 | ||
a3437870 NA |
3177 | if (sscanf(s, "%lu", mhp) <= 0) |
3178 | *mhp = 0; | |
3179 | ||
8faa8b07 AK |
3180 | /* |
3181 | * Global state is always initialized later in hugetlb_init. | |
3182 | * But we need to allocate >= MAX_ORDER hstates here early to still | |
3183 | * use the bootmem allocator. | |
3184 | */ | |
47d38344 | 3185 | if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER) |
8faa8b07 AK |
3186 | hugetlb_hstate_alloc_pages(parsed_hstate); |
3187 | ||
3188 | last_mhp = mhp; | |
3189 | ||
a3437870 NA |
3190 | return 1; |
3191 | } | |
e11bfbfc NP |
3192 | __setup("hugepages=", hugetlb_nrpages_setup); |
3193 | ||
3194 | static int __init hugetlb_default_setup(char *s) | |
3195 | { | |
3196 | default_hstate_size = memparse(s, &s); | |
3197 | return 1; | |
3198 | } | |
3199 | __setup("default_hugepagesz=", hugetlb_default_setup); | |
a3437870 | 3200 | |
8a213460 NA |
3201 | static unsigned int cpuset_mems_nr(unsigned int *array) |
3202 | { | |
3203 | int node; | |
3204 | unsigned int nr = 0; | |
3205 | ||
3206 | for_each_node_mask(node, cpuset_current_mems_allowed) | |
3207 | nr += array[node]; | |
3208 | ||
3209 | return nr; | |
3210 | } | |
3211 | ||
3212 | #ifdef CONFIG_SYSCTL | |
06808b08 LS |
3213 | static int hugetlb_sysctl_handler_common(bool obey_mempolicy, |
3214 | struct ctl_table *table, int write, | |
3215 | void __user *buffer, size_t *length, loff_t *ppos) | |
1da177e4 | 3216 | { |
e5ff2159 | 3217 | struct hstate *h = &default_hstate; |
238d3c13 | 3218 | unsigned long tmp = h->max_huge_pages; |
08d4a246 | 3219 | int ret; |
e5ff2159 | 3220 | |
457c1b27 | 3221 | if (!hugepages_supported()) |
86613628 | 3222 | return -EOPNOTSUPP; |
457c1b27 | 3223 | |
e5ff2159 AK |
3224 | table->data = &tmp; |
3225 | table->maxlen = sizeof(unsigned long); | |
08d4a246 MH |
3226 | ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); |
3227 | if (ret) | |
3228 | goto out; | |
e5ff2159 | 3229 | |
238d3c13 DR |
3230 | if (write) |
3231 | ret = __nr_hugepages_store_common(obey_mempolicy, h, | |
3232 | NUMA_NO_NODE, tmp, *length); | |
08d4a246 MH |
3233 | out: |
3234 | return ret; | |
1da177e4 | 3235 | } |
396faf03 | 3236 | |
06808b08 LS |
3237 | int hugetlb_sysctl_handler(struct ctl_table *table, int write, |
3238 | void __user *buffer, size_t *length, loff_t *ppos) | |
3239 | { | |
3240 | ||
3241 | return hugetlb_sysctl_handler_common(false, table, write, | |
3242 | buffer, length, ppos); | |
3243 | } | |
3244 | ||
3245 | #ifdef CONFIG_NUMA | |
3246 | int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write, | |
3247 | void __user *buffer, size_t *length, loff_t *ppos) | |
3248 | { | |
3249 | return hugetlb_sysctl_handler_common(true, table, write, | |
3250 | buffer, length, ppos); | |
3251 | } | |
3252 | #endif /* CONFIG_NUMA */ | |
3253 | ||
a3d0c6aa | 3254 | int hugetlb_overcommit_handler(struct ctl_table *table, int write, |
8d65af78 | 3255 | void __user *buffer, |
a3d0c6aa NA |
3256 | size_t *length, loff_t *ppos) |
3257 | { | |
a5516438 | 3258 | struct hstate *h = &default_hstate; |
e5ff2159 | 3259 | unsigned long tmp; |
08d4a246 | 3260 | int ret; |
e5ff2159 | 3261 | |
457c1b27 | 3262 | if (!hugepages_supported()) |
86613628 | 3263 | return -EOPNOTSUPP; |
457c1b27 | 3264 | |
c033a93c | 3265 | tmp = h->nr_overcommit_huge_pages; |
e5ff2159 | 3266 | |
bae7f4ae | 3267 | if (write && hstate_is_gigantic(h)) |
adbe8726 EM |
3268 | return -EINVAL; |
3269 | ||
e5ff2159 AK |
3270 | table->data = &tmp; |
3271 | table->maxlen = sizeof(unsigned long); | |
08d4a246 MH |
3272 | ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); |
3273 | if (ret) | |
3274 | goto out; | |
e5ff2159 AK |
3275 | |
3276 | if (write) { | |
3277 | spin_lock(&hugetlb_lock); | |
3278 | h->nr_overcommit_huge_pages = tmp; | |
3279 | spin_unlock(&hugetlb_lock); | |
3280 | } | |
08d4a246 MH |
3281 | out: |
3282 | return ret; | |
a3d0c6aa NA |
3283 | } |
3284 | ||
1da177e4 LT |
3285 | #endif /* CONFIG_SYSCTL */ |
3286 | ||
e1759c21 | 3287 | void hugetlb_report_meminfo(struct seq_file *m) |
1da177e4 | 3288 | { |
fcb2b0c5 RG |
3289 | struct hstate *h; |
3290 | unsigned long total = 0; | |
3291 | ||
457c1b27 NA |
3292 | if (!hugepages_supported()) |
3293 | return; | |
fcb2b0c5 RG |
3294 | |
3295 | for_each_hstate(h) { | |
3296 | unsigned long count = h->nr_huge_pages; | |
3297 | ||
3298 | total += (PAGE_SIZE << huge_page_order(h)) * count; | |
3299 | ||
3300 | if (h == &default_hstate) | |
3301 | seq_printf(m, | |
3302 | "HugePages_Total: %5lu\n" | |
3303 | "HugePages_Free: %5lu\n" | |
3304 | "HugePages_Rsvd: %5lu\n" | |
3305 | "HugePages_Surp: %5lu\n" | |
3306 | "Hugepagesize: %8lu kB\n", | |
3307 | count, | |
3308 | h->free_huge_pages, | |
3309 | h->resv_huge_pages, | |
3310 | h->surplus_huge_pages, | |
3311 | (PAGE_SIZE << huge_page_order(h)) / 1024); | |
3312 | } | |
3313 | ||
3314 | seq_printf(m, "Hugetlb: %8lu kB\n", total / 1024); | |
1da177e4 LT |
3315 | } |
3316 | ||
3317 | int hugetlb_report_node_meminfo(int nid, char *buf) | |
3318 | { | |
a5516438 | 3319 | struct hstate *h = &default_hstate; |
457c1b27 NA |
3320 | if (!hugepages_supported()) |
3321 | return 0; | |
1da177e4 LT |
3322 | return sprintf(buf, |
3323 | "Node %d HugePages_Total: %5u\n" | |
a1de0919 NA |
3324 | "Node %d HugePages_Free: %5u\n" |
3325 | "Node %d HugePages_Surp: %5u\n", | |
a5516438 AK |
3326 | nid, h->nr_huge_pages_node[nid], |
3327 | nid, h->free_huge_pages_node[nid], | |
3328 | nid, h->surplus_huge_pages_node[nid]); | |
1da177e4 LT |
3329 | } |
3330 | ||
949f7ec5 DR |
3331 | void hugetlb_show_meminfo(void) |
3332 | { | |
3333 | struct hstate *h; | |
3334 | int nid; | |
3335 | ||
457c1b27 NA |
3336 | if (!hugepages_supported()) |
3337 | return; | |
3338 | ||
949f7ec5 DR |
3339 | for_each_node_state(nid, N_MEMORY) |
3340 | for_each_hstate(h) | |
3341 | pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", | |
3342 | nid, | |
3343 | h->nr_huge_pages_node[nid], | |
3344 | h->free_huge_pages_node[nid], | |
3345 | h->surplus_huge_pages_node[nid], | |
3346 | 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); | |
3347 | } | |
3348 | ||
5d317b2b NH |
3349 | void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm) |
3350 | { | |
3351 | seq_printf(m, "HugetlbPages:\t%8lu kB\n", | |
3352 | atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10)); | |
3353 | } | |
3354 | ||
1da177e4 LT |
3355 | /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ |
3356 | unsigned long hugetlb_total_pages(void) | |
3357 | { | |
d0028588 WL |
3358 | struct hstate *h; |
3359 | unsigned long nr_total_pages = 0; | |
3360 | ||
3361 | for_each_hstate(h) | |
3362 | nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); | |
3363 | return nr_total_pages; | |
1da177e4 | 3364 | } |
1da177e4 | 3365 | |
a5516438 | 3366 | static int hugetlb_acct_memory(struct hstate *h, long delta) |
fc1b8a73 MG |
3367 | { |
3368 | int ret = -ENOMEM; | |
3369 | ||
3370 | spin_lock(&hugetlb_lock); | |
3371 | /* | |
3372 | * When cpuset is configured, it breaks the strict hugetlb page | |
3373 | * reservation as the accounting is done on a global variable. Such | |
3374 | * reservation is completely rubbish in the presence of cpuset because | |
3375 | * the reservation is not checked against page availability for the | |
3376 | * current cpuset. Application can still potentially OOM'ed by kernel | |
3377 | * with lack of free htlb page in cpuset that the task is in. | |
3378 | * Attempt to enforce strict accounting with cpuset is almost | |
3379 | * impossible (or too ugly) because cpuset is too fluid that | |
3380 | * task or memory node can be dynamically moved between cpusets. | |
3381 | * | |
3382 | * The change of semantics for shared hugetlb mapping with cpuset is | |
3383 | * undesirable. However, in order to preserve some of the semantics, | |
3384 | * we fall back to check against current free page availability as | |
3385 | * a best attempt and hopefully to minimize the impact of changing | |
3386 | * semantics that cpuset has. | |
3387 | */ | |
3388 | if (delta > 0) { | |
a5516438 | 3389 | if (gather_surplus_pages(h, delta) < 0) |
fc1b8a73 MG |
3390 | goto out; |
3391 | ||
a5516438 AK |
3392 | if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { |
3393 | return_unused_surplus_pages(h, delta); | |
fc1b8a73 MG |
3394 | goto out; |
3395 | } | |
3396 | } | |
3397 | ||
3398 | ret = 0; | |
3399 | if (delta < 0) | |
a5516438 | 3400 | return_unused_surplus_pages(h, (unsigned long) -delta); |
fc1b8a73 MG |
3401 | |
3402 | out: | |
3403 | spin_unlock(&hugetlb_lock); | |
3404 | return ret; | |
3405 | } | |
3406 | ||
84afd99b AW |
3407 | static void hugetlb_vm_op_open(struct vm_area_struct *vma) |
3408 | { | |
f522c3ac | 3409 | struct resv_map *resv = vma_resv_map(vma); |
84afd99b AW |
3410 | |
3411 | /* | |
3412 | * This new VMA should share its siblings reservation map if present. | |
3413 | * The VMA will only ever have a valid reservation map pointer where | |
3414 | * it is being copied for another still existing VMA. As that VMA | |
25985edc | 3415 | * has a reference to the reservation map it cannot disappear until |
84afd99b AW |
3416 | * after this open call completes. It is therefore safe to take a |
3417 | * new reference here without additional locking. | |
3418 | */ | |
4e35f483 | 3419 | if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) |
f522c3ac | 3420 | kref_get(&resv->refs); |
84afd99b AW |
3421 | } |
3422 | ||
a1e78772 MG |
3423 | static void hugetlb_vm_op_close(struct vm_area_struct *vma) |
3424 | { | |
a5516438 | 3425 | struct hstate *h = hstate_vma(vma); |
f522c3ac | 3426 | struct resv_map *resv = vma_resv_map(vma); |
90481622 | 3427 | struct hugepage_subpool *spool = subpool_vma(vma); |
4e35f483 | 3428 | unsigned long reserve, start, end; |
1c5ecae3 | 3429 | long gbl_reserve; |
84afd99b | 3430 | |
4e35f483 JK |
3431 | if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) |
3432 | return; | |
84afd99b | 3433 | |
4e35f483 JK |
3434 | start = vma_hugecache_offset(h, vma, vma->vm_start); |
3435 | end = vma_hugecache_offset(h, vma, vma->vm_end); | |
84afd99b | 3436 | |
4e35f483 | 3437 | reserve = (end - start) - region_count(resv, start, end); |
e9fe92ae | 3438 | hugetlb_cgroup_uncharge_counter(resv, start, end); |
4e35f483 | 3439 | if (reserve) { |
1c5ecae3 MK |
3440 | /* |
3441 | * Decrement reserve counts. The global reserve count may be | |
3442 | * adjusted if the subpool has a minimum size. | |
3443 | */ | |
3444 | gbl_reserve = hugepage_subpool_put_pages(spool, reserve); | |
3445 | hugetlb_acct_memory(h, -gbl_reserve); | |
84afd99b | 3446 | } |
e9fe92ae MA |
3447 | |
3448 | kref_put(&resv->refs, resv_map_release); | |
a1e78772 MG |
3449 | } |
3450 | ||
31383c68 DW |
3451 | static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr) |
3452 | { | |
3453 | if (addr & ~(huge_page_mask(hstate_vma(vma)))) | |
3454 | return -EINVAL; | |
3455 | return 0; | |
3456 | } | |
3457 | ||
05ea8860 DW |
3458 | static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma) |
3459 | { | |
3460 | struct hstate *hstate = hstate_vma(vma); | |
3461 | ||
3462 | return 1UL << huge_page_shift(hstate); | |
3463 | } | |
3464 | ||
1da177e4 LT |
3465 | /* |
3466 | * We cannot handle pagefaults against hugetlb pages at all. They cause | |
3467 | * handle_mm_fault() to try to instantiate regular-sized pages in the | |
3468 | * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get | |
3469 | * this far. | |
3470 | */ | |
b3ec9f33 | 3471 | static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf) |
1da177e4 LT |
3472 | { |
3473 | BUG(); | |
d0217ac0 | 3474 | return 0; |
1da177e4 LT |
3475 | } |
3476 | ||
eec3636a JC |
3477 | /* |
3478 | * When a new function is introduced to vm_operations_struct and added | |
3479 | * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops. | |
3480 | * This is because under System V memory model, mappings created via | |
3481 | * shmget/shmat with "huge page" specified are backed by hugetlbfs files, | |
3482 | * their original vm_ops are overwritten with shm_vm_ops. | |
3483 | */ | |
f0f37e2f | 3484 | const struct vm_operations_struct hugetlb_vm_ops = { |
d0217ac0 | 3485 | .fault = hugetlb_vm_op_fault, |
84afd99b | 3486 | .open = hugetlb_vm_op_open, |
a1e78772 | 3487 | .close = hugetlb_vm_op_close, |
31383c68 | 3488 | .split = hugetlb_vm_op_split, |
05ea8860 | 3489 | .pagesize = hugetlb_vm_op_pagesize, |
1da177e4 LT |
3490 | }; |
3491 | ||
1e8f889b DG |
3492 | static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, |
3493 | int writable) | |
63551ae0 DG |
3494 | { |
3495 | pte_t entry; | |
3496 | ||
1e8f889b | 3497 | if (writable) { |
106c992a GS |
3498 | entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, |
3499 | vma->vm_page_prot))); | |
63551ae0 | 3500 | } else { |
106c992a GS |
3501 | entry = huge_pte_wrprotect(mk_huge_pte(page, |
3502 | vma->vm_page_prot)); | |
63551ae0 DG |
3503 | } |
3504 | entry = pte_mkyoung(entry); | |
3505 | entry = pte_mkhuge(entry); | |
d9ed9faa | 3506 | entry = arch_make_huge_pte(entry, vma, page, writable); |
63551ae0 DG |
3507 | |
3508 | return entry; | |
3509 | } | |
3510 | ||
1e8f889b DG |
3511 | static void set_huge_ptep_writable(struct vm_area_struct *vma, |
3512 | unsigned long address, pte_t *ptep) | |
3513 | { | |
3514 | pte_t entry; | |
3515 | ||
106c992a | 3516 | entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); |
32f84528 | 3517 | if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) |
4b3073e1 | 3518 | update_mmu_cache(vma, address, ptep); |
1e8f889b DG |
3519 | } |
3520 | ||
d5ed7444 | 3521 | bool is_hugetlb_entry_migration(pte_t pte) |
4a705fef NH |
3522 | { |
3523 | swp_entry_t swp; | |
3524 | ||
3525 | if (huge_pte_none(pte) || pte_present(pte)) | |
d5ed7444 | 3526 | return false; |
4a705fef NH |
3527 | swp = pte_to_swp_entry(pte); |
3528 | if (non_swap_entry(swp) && is_migration_entry(swp)) | |
d5ed7444 | 3529 | return true; |
4a705fef | 3530 | else |
d5ed7444 | 3531 | return false; |
4a705fef NH |
3532 | } |
3533 | ||
3534 | static int is_hugetlb_entry_hwpoisoned(pte_t pte) | |
3535 | { | |
3536 | swp_entry_t swp; | |
3537 | ||
3538 | if (huge_pte_none(pte) || pte_present(pte)) | |
3539 | return 0; | |
3540 | swp = pte_to_swp_entry(pte); | |
3541 | if (non_swap_entry(swp) && is_hwpoison_entry(swp)) | |
3542 | return 1; | |
3543 | else | |
3544 | return 0; | |
3545 | } | |
1e8f889b | 3546 | |
63551ae0 DG |
3547 | int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, |
3548 | struct vm_area_struct *vma) | |
3549 | { | |
5e41540c | 3550 | pte_t *src_pte, *dst_pte, entry, dst_entry; |
63551ae0 | 3551 | struct page *ptepage; |
1c59827d | 3552 | unsigned long addr; |
1e8f889b | 3553 | int cow; |
a5516438 AK |
3554 | struct hstate *h = hstate_vma(vma); |
3555 | unsigned long sz = huge_page_size(h); | |
c0d0381a | 3556 | struct address_space *mapping = vma->vm_file->f_mapping; |
ac46d4f3 | 3557 | struct mmu_notifier_range range; |
e8569dd2 | 3558 | int ret = 0; |
1e8f889b DG |
3559 | |
3560 | cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; | |
63551ae0 | 3561 | |
ac46d4f3 | 3562 | if (cow) { |
7269f999 | 3563 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, src, |
6f4f13e8 | 3564 | vma->vm_start, |
ac46d4f3 JG |
3565 | vma->vm_end); |
3566 | mmu_notifier_invalidate_range_start(&range); | |
c0d0381a MK |
3567 | } else { |
3568 | /* | |
3569 | * For shared mappings i_mmap_rwsem must be held to call | |
3570 | * huge_pte_alloc, otherwise the returned ptep could go | |
3571 | * away if part of a shared pmd and another thread calls | |
3572 | * huge_pmd_unshare. | |
3573 | */ | |
3574 | i_mmap_lock_read(mapping); | |
ac46d4f3 | 3575 | } |
e8569dd2 | 3576 | |
a5516438 | 3577 | for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { |
cb900f41 | 3578 | spinlock_t *src_ptl, *dst_ptl; |
7868a208 | 3579 | src_pte = huge_pte_offset(src, addr, sz); |
c74df32c HD |
3580 | if (!src_pte) |
3581 | continue; | |
a5516438 | 3582 | dst_pte = huge_pte_alloc(dst, addr, sz); |
e8569dd2 AS |
3583 | if (!dst_pte) { |
3584 | ret = -ENOMEM; | |
3585 | break; | |
3586 | } | |
c5c99429 | 3587 | |
5e41540c MK |
3588 | /* |
3589 | * If the pagetables are shared don't copy or take references. | |
3590 | * dst_pte == src_pte is the common case of src/dest sharing. | |
3591 | * | |
3592 | * However, src could have 'unshared' and dst shares with | |
3593 | * another vma. If dst_pte !none, this implies sharing. | |
3594 | * Check here before taking page table lock, and once again | |
3595 | * after taking the lock below. | |
3596 | */ | |
3597 | dst_entry = huge_ptep_get(dst_pte); | |
3598 | if ((dst_pte == src_pte) || !huge_pte_none(dst_entry)) | |
c5c99429 LW |
3599 | continue; |
3600 | ||
cb900f41 KS |
3601 | dst_ptl = huge_pte_lock(h, dst, dst_pte); |
3602 | src_ptl = huge_pte_lockptr(h, src, src_pte); | |
3603 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); | |
4a705fef | 3604 | entry = huge_ptep_get(src_pte); |
5e41540c MK |
3605 | dst_entry = huge_ptep_get(dst_pte); |
3606 | if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) { | |
3607 | /* | |
3608 | * Skip if src entry none. Also, skip in the | |
3609 | * unlikely case dst entry !none as this implies | |
3610 | * sharing with another vma. | |
3611 | */ | |
4a705fef NH |
3612 | ; |
3613 | } else if (unlikely(is_hugetlb_entry_migration(entry) || | |
3614 | is_hugetlb_entry_hwpoisoned(entry))) { | |
3615 | swp_entry_t swp_entry = pte_to_swp_entry(entry); | |
3616 | ||
3617 | if (is_write_migration_entry(swp_entry) && cow) { | |
3618 | /* | |
3619 | * COW mappings require pages in both | |
3620 | * parent and child to be set to read. | |
3621 | */ | |
3622 | make_migration_entry_read(&swp_entry); | |
3623 | entry = swp_entry_to_pte(swp_entry); | |
e5251fd4 PA |
3624 | set_huge_swap_pte_at(src, addr, src_pte, |
3625 | entry, sz); | |
4a705fef | 3626 | } |
e5251fd4 | 3627 | set_huge_swap_pte_at(dst, addr, dst_pte, entry, sz); |
4a705fef | 3628 | } else { |
34ee645e | 3629 | if (cow) { |
0f10851e JG |
3630 | /* |
3631 | * No need to notify as we are downgrading page | |
3632 | * table protection not changing it to point | |
3633 | * to a new page. | |
3634 | * | |
ad56b738 | 3635 | * See Documentation/vm/mmu_notifier.rst |
0f10851e | 3636 | */ |
7f2e9525 | 3637 | huge_ptep_set_wrprotect(src, addr, src_pte); |
34ee645e | 3638 | } |
0253d634 | 3639 | entry = huge_ptep_get(src_pte); |
1c59827d HD |
3640 | ptepage = pte_page(entry); |
3641 | get_page(ptepage); | |
53f9263b | 3642 | page_dup_rmap(ptepage, true); |
1c59827d | 3643 | set_huge_pte_at(dst, addr, dst_pte, entry); |
5d317b2b | 3644 | hugetlb_count_add(pages_per_huge_page(h), dst); |
1c59827d | 3645 | } |
cb900f41 KS |
3646 | spin_unlock(src_ptl); |
3647 | spin_unlock(dst_ptl); | |
63551ae0 | 3648 | } |
63551ae0 | 3649 | |
e8569dd2 | 3650 | if (cow) |
ac46d4f3 | 3651 | mmu_notifier_invalidate_range_end(&range); |
c0d0381a MK |
3652 | else |
3653 | i_mmap_unlock_read(mapping); | |
e8569dd2 AS |
3654 | |
3655 | return ret; | |
63551ae0 DG |
3656 | } |
3657 | ||
24669e58 AK |
3658 | void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, |
3659 | unsigned long start, unsigned long end, | |
3660 | struct page *ref_page) | |
63551ae0 DG |
3661 | { |
3662 | struct mm_struct *mm = vma->vm_mm; | |
3663 | unsigned long address; | |
c7546f8f | 3664 | pte_t *ptep; |
63551ae0 | 3665 | pte_t pte; |
cb900f41 | 3666 | spinlock_t *ptl; |
63551ae0 | 3667 | struct page *page; |
a5516438 AK |
3668 | struct hstate *h = hstate_vma(vma); |
3669 | unsigned long sz = huge_page_size(h); | |
ac46d4f3 | 3670 | struct mmu_notifier_range range; |
a5516438 | 3671 | |
63551ae0 | 3672 | WARN_ON(!is_vm_hugetlb_page(vma)); |
a5516438 AK |
3673 | BUG_ON(start & ~huge_page_mask(h)); |
3674 | BUG_ON(end & ~huge_page_mask(h)); | |
63551ae0 | 3675 | |
07e32661 AK |
3676 | /* |
3677 | * This is a hugetlb vma, all the pte entries should point | |
3678 | * to huge page. | |
3679 | */ | |
ed6a7935 | 3680 | tlb_change_page_size(tlb, sz); |
24669e58 | 3681 | tlb_start_vma(tlb, vma); |
dff11abe MK |
3682 | |
3683 | /* | |
3684 | * If sharing possible, alert mmu notifiers of worst case. | |
3685 | */ | |
6f4f13e8 JG |
3686 | mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, mm, start, |
3687 | end); | |
ac46d4f3 JG |
3688 | adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); |
3689 | mmu_notifier_invalidate_range_start(&range); | |
569f48b8 | 3690 | address = start; |
569f48b8 | 3691 | for (; address < end; address += sz) { |
7868a208 | 3692 | ptep = huge_pte_offset(mm, address, sz); |
4c887265 | 3693 | if (!ptep) |
c7546f8f DG |
3694 | continue; |
3695 | ||
cb900f41 | 3696 | ptl = huge_pte_lock(h, mm, ptep); |
31d49da5 AK |
3697 | if (huge_pmd_unshare(mm, &address, ptep)) { |
3698 | spin_unlock(ptl); | |
dff11abe MK |
3699 | /* |
3700 | * We just unmapped a page of PMDs by clearing a PUD. | |
3701 | * The caller's TLB flush range should cover this area. | |
3702 | */ | |
31d49da5 AK |
3703 | continue; |
3704 | } | |
39dde65c | 3705 | |
6629326b | 3706 | pte = huge_ptep_get(ptep); |
31d49da5 AK |
3707 | if (huge_pte_none(pte)) { |
3708 | spin_unlock(ptl); | |
3709 | continue; | |
3710 | } | |
6629326b HD |
3711 | |
3712 | /* | |
9fbc1f63 NH |
3713 | * Migrating hugepage or HWPoisoned hugepage is already |
3714 | * unmapped and its refcount is dropped, so just clear pte here. | |
6629326b | 3715 | */ |
9fbc1f63 | 3716 | if (unlikely(!pte_present(pte))) { |
9386fac3 | 3717 | huge_pte_clear(mm, address, ptep, sz); |
31d49da5 AK |
3718 | spin_unlock(ptl); |
3719 | continue; | |
8c4894c6 | 3720 | } |
6629326b HD |
3721 | |
3722 | page = pte_page(pte); | |
04f2cbe3 MG |
3723 | /* |
3724 | * If a reference page is supplied, it is because a specific | |
3725 | * page is being unmapped, not a range. Ensure the page we | |
3726 | * are about to unmap is the actual page of interest. | |
3727 | */ | |
3728 | if (ref_page) { | |
31d49da5 AK |
3729 | if (page != ref_page) { |
3730 | spin_unlock(ptl); | |
3731 | continue; | |
3732 | } | |
04f2cbe3 MG |
3733 | /* |
3734 | * Mark the VMA as having unmapped its page so that | |
3735 | * future faults in this VMA will fail rather than | |
3736 | * looking like data was lost | |
3737 | */ | |
3738 | set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); | |
3739 | } | |
3740 | ||
c7546f8f | 3741 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
b528e4b6 | 3742 | tlb_remove_huge_tlb_entry(h, tlb, ptep, address); |
106c992a | 3743 | if (huge_pte_dirty(pte)) |
6649a386 | 3744 | set_page_dirty(page); |
9e81130b | 3745 | |
5d317b2b | 3746 | hugetlb_count_sub(pages_per_huge_page(h), mm); |
d281ee61 | 3747 | page_remove_rmap(page, true); |
31d49da5 | 3748 | |
cb900f41 | 3749 | spin_unlock(ptl); |
e77b0852 | 3750 | tlb_remove_page_size(tlb, page, huge_page_size(h)); |
31d49da5 AK |
3751 | /* |
3752 | * Bail out after unmapping reference page if supplied | |
3753 | */ | |
3754 | if (ref_page) | |
3755 | break; | |
fe1668ae | 3756 | } |
ac46d4f3 | 3757 | mmu_notifier_invalidate_range_end(&range); |
24669e58 | 3758 | tlb_end_vma(tlb, vma); |
1da177e4 | 3759 | } |
63551ae0 | 3760 | |
d833352a MG |
3761 | void __unmap_hugepage_range_final(struct mmu_gather *tlb, |
3762 | struct vm_area_struct *vma, unsigned long start, | |
3763 | unsigned long end, struct page *ref_page) | |
3764 | { | |
3765 | __unmap_hugepage_range(tlb, vma, start, end, ref_page); | |
3766 | ||
3767 | /* | |
3768 | * Clear this flag so that x86's huge_pmd_share page_table_shareable | |
3769 | * test will fail on a vma being torn down, and not grab a page table | |
3770 | * on its way out. We're lucky that the flag has such an appropriate | |
3771 | * name, and can in fact be safely cleared here. We could clear it | |
3772 | * before the __unmap_hugepage_range above, but all that's necessary | |
c8c06efa | 3773 | * is to clear it before releasing the i_mmap_rwsem. This works |
d833352a | 3774 | * because in the context this is called, the VMA is about to be |
c8c06efa | 3775 | * destroyed and the i_mmap_rwsem is held. |
d833352a MG |
3776 | */ |
3777 | vma->vm_flags &= ~VM_MAYSHARE; | |
3778 | } | |
3779 | ||
502717f4 | 3780 | void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
04f2cbe3 | 3781 | unsigned long end, struct page *ref_page) |
502717f4 | 3782 | { |
24669e58 AK |
3783 | struct mm_struct *mm; |
3784 | struct mmu_gather tlb; | |
dff11abe MK |
3785 | unsigned long tlb_start = start; |
3786 | unsigned long tlb_end = end; | |
3787 | ||
3788 | /* | |
3789 | * If shared PMDs were possibly used within this vma range, adjust | |
3790 | * start/end for worst case tlb flushing. | |
3791 | * Note that we can not be sure if PMDs are shared until we try to | |
3792 | * unmap pages. However, we want to make sure TLB flushing covers | |
3793 | * the largest possible range. | |
3794 | */ | |
3795 | adjust_range_if_pmd_sharing_possible(vma, &tlb_start, &tlb_end); | |
24669e58 AK |
3796 | |
3797 | mm = vma->vm_mm; | |
3798 | ||
dff11abe | 3799 | tlb_gather_mmu(&tlb, mm, tlb_start, tlb_end); |
24669e58 | 3800 | __unmap_hugepage_range(&tlb, vma, start, end, ref_page); |
dff11abe | 3801 | tlb_finish_mmu(&tlb, tlb_start, tlb_end); |
502717f4 KC |
3802 | } |
3803 | ||
04f2cbe3 MG |
3804 | /* |
3805 | * This is called when the original mapper is failing to COW a MAP_PRIVATE | |
3806 | * mappping it owns the reserve page for. The intention is to unmap the page | |
3807 | * from other VMAs and let the children be SIGKILLed if they are faulting the | |
3808 | * same region. | |
3809 | */ | |
2f4612af DB |
3810 | static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, |
3811 | struct page *page, unsigned long address) | |
04f2cbe3 | 3812 | { |
7526674d | 3813 | struct hstate *h = hstate_vma(vma); |
04f2cbe3 MG |
3814 | struct vm_area_struct *iter_vma; |
3815 | struct address_space *mapping; | |
04f2cbe3 MG |
3816 | pgoff_t pgoff; |
3817 | ||
3818 | /* | |
3819 | * vm_pgoff is in PAGE_SIZE units, hence the different calculation | |
3820 | * from page cache lookup which is in HPAGE_SIZE units. | |
3821 | */ | |
7526674d | 3822 | address = address & huge_page_mask(h); |
36e4f20a MH |
3823 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + |
3824 | vma->vm_pgoff; | |
93c76a3d | 3825 | mapping = vma->vm_file->f_mapping; |
04f2cbe3 | 3826 | |
4eb2b1dc MG |
3827 | /* |
3828 | * Take the mapping lock for the duration of the table walk. As | |
3829 | * this mapping should be shared between all the VMAs, | |
3830 | * __unmap_hugepage_range() is called as the lock is already held | |
3831 | */ | |
83cde9e8 | 3832 | i_mmap_lock_write(mapping); |
6b2dbba8 | 3833 | vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { |
04f2cbe3 MG |
3834 | /* Do not unmap the current VMA */ |
3835 | if (iter_vma == vma) | |
3836 | continue; | |
3837 | ||
2f84a899 MG |
3838 | /* |
3839 | * Shared VMAs have their own reserves and do not affect | |
3840 | * MAP_PRIVATE accounting but it is possible that a shared | |
3841 | * VMA is using the same page so check and skip such VMAs. | |
3842 | */ | |
3843 | if (iter_vma->vm_flags & VM_MAYSHARE) | |
3844 | continue; | |
3845 | ||
04f2cbe3 MG |
3846 | /* |
3847 | * Unmap the page from other VMAs without their own reserves. | |
3848 | * They get marked to be SIGKILLed if they fault in these | |
3849 | * areas. This is because a future no-page fault on this VMA | |
3850 | * could insert a zeroed page instead of the data existing | |
3851 | * from the time of fork. This would look like data corruption | |
3852 | */ | |
3853 | if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) | |
24669e58 AK |
3854 | unmap_hugepage_range(iter_vma, address, |
3855 | address + huge_page_size(h), page); | |
04f2cbe3 | 3856 | } |
83cde9e8 | 3857 | i_mmap_unlock_write(mapping); |
04f2cbe3 MG |
3858 | } |
3859 | ||
0fe6e20b NH |
3860 | /* |
3861 | * Hugetlb_cow() should be called with page lock of the original hugepage held. | |
ef009b25 MH |
3862 | * Called with hugetlb_instantiation_mutex held and pte_page locked so we |
3863 | * cannot race with other handlers or page migration. | |
3864 | * Keep the pte_same checks anyway to make transition from the mutex easier. | |
0fe6e20b | 3865 | */ |
2b740303 | 3866 | static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, |
974e6d66 | 3867 | unsigned long address, pte_t *ptep, |
3999f52e | 3868 | struct page *pagecache_page, spinlock_t *ptl) |
1e8f889b | 3869 | { |
3999f52e | 3870 | pte_t pte; |
a5516438 | 3871 | struct hstate *h = hstate_vma(vma); |
1e8f889b | 3872 | struct page *old_page, *new_page; |
2b740303 SJ |
3873 | int outside_reserve = 0; |
3874 | vm_fault_t ret = 0; | |
974e6d66 | 3875 | unsigned long haddr = address & huge_page_mask(h); |
ac46d4f3 | 3876 | struct mmu_notifier_range range; |
1e8f889b | 3877 | |
3999f52e | 3878 | pte = huge_ptep_get(ptep); |
1e8f889b DG |
3879 | old_page = pte_page(pte); |
3880 | ||
04f2cbe3 | 3881 | retry_avoidcopy: |
1e8f889b DG |
3882 | /* If no-one else is actually using this page, avoid the copy |
3883 | * and just make the page writable */ | |
37a2140d | 3884 | if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { |
5a49973d | 3885 | page_move_anon_rmap(old_page, vma); |
5b7a1d40 | 3886 | set_huge_ptep_writable(vma, haddr, ptep); |
83c54070 | 3887 | return 0; |
1e8f889b DG |
3888 | } |
3889 | ||
04f2cbe3 MG |
3890 | /* |
3891 | * If the process that created a MAP_PRIVATE mapping is about to | |
3892 | * perform a COW due to a shared page count, attempt to satisfy | |
3893 | * the allocation without using the existing reserves. The pagecache | |
3894 | * page is used to determine if the reserve at this address was | |
3895 | * consumed or not. If reserves were used, a partial faulted mapping | |
3896 | * at the time of fork() could consume its reserves on COW instead | |
3897 | * of the full address range. | |
3898 | */ | |
5944d011 | 3899 | if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && |
04f2cbe3 MG |
3900 | old_page != pagecache_page) |
3901 | outside_reserve = 1; | |
3902 | ||
09cbfeaf | 3903 | get_page(old_page); |
b76c8cfb | 3904 | |
ad4404a2 DB |
3905 | /* |
3906 | * Drop page table lock as buddy allocator may be called. It will | |
3907 | * be acquired again before returning to the caller, as expected. | |
3908 | */ | |
cb900f41 | 3909 | spin_unlock(ptl); |
5b7a1d40 | 3910 | new_page = alloc_huge_page(vma, haddr, outside_reserve); |
1e8f889b | 3911 | |
2fc39cec | 3912 | if (IS_ERR(new_page)) { |
04f2cbe3 MG |
3913 | /* |
3914 | * If a process owning a MAP_PRIVATE mapping fails to COW, | |
3915 | * it is due to references held by a child and an insufficient | |
3916 | * huge page pool. To guarantee the original mappers | |
3917 | * reliability, unmap the page from child processes. The child | |
3918 | * may get SIGKILLed if it later faults. | |
3919 | */ | |
3920 | if (outside_reserve) { | |
09cbfeaf | 3921 | put_page(old_page); |
04f2cbe3 | 3922 | BUG_ON(huge_pte_none(pte)); |
5b7a1d40 | 3923 | unmap_ref_private(mm, vma, old_page, haddr); |
2f4612af DB |
3924 | BUG_ON(huge_pte_none(pte)); |
3925 | spin_lock(ptl); | |
5b7a1d40 | 3926 | ptep = huge_pte_offset(mm, haddr, huge_page_size(h)); |
2f4612af DB |
3927 | if (likely(ptep && |
3928 | pte_same(huge_ptep_get(ptep), pte))) | |
3929 | goto retry_avoidcopy; | |
3930 | /* | |
3931 | * race occurs while re-acquiring page table | |
3932 | * lock, and our job is done. | |
3933 | */ | |
3934 | return 0; | |
04f2cbe3 MG |
3935 | } |
3936 | ||
2b740303 | 3937 | ret = vmf_error(PTR_ERR(new_page)); |
ad4404a2 | 3938 | goto out_release_old; |
1e8f889b DG |
3939 | } |
3940 | ||
0fe6e20b NH |
3941 | /* |
3942 | * When the original hugepage is shared one, it does not have | |
3943 | * anon_vma prepared. | |
3944 | */ | |
44e2aa93 | 3945 | if (unlikely(anon_vma_prepare(vma))) { |
ad4404a2 DB |
3946 | ret = VM_FAULT_OOM; |
3947 | goto out_release_all; | |
44e2aa93 | 3948 | } |
0fe6e20b | 3949 | |
974e6d66 | 3950 | copy_user_huge_page(new_page, old_page, address, vma, |
47ad8475 | 3951 | pages_per_huge_page(h)); |
0ed361de | 3952 | __SetPageUptodate(new_page); |
1e8f889b | 3953 | |
7269f999 | 3954 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, haddr, |
6f4f13e8 | 3955 | haddr + huge_page_size(h)); |
ac46d4f3 | 3956 | mmu_notifier_invalidate_range_start(&range); |
ad4404a2 | 3957 | |
b76c8cfb | 3958 | /* |
cb900f41 | 3959 | * Retake the page table lock to check for racing updates |
b76c8cfb LW |
3960 | * before the page tables are altered |
3961 | */ | |
cb900f41 | 3962 | spin_lock(ptl); |
5b7a1d40 | 3963 | ptep = huge_pte_offset(mm, haddr, huge_page_size(h)); |
a9af0c5d | 3964 | if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { |
07443a85 JK |
3965 | ClearPagePrivate(new_page); |
3966 | ||
1e8f889b | 3967 | /* Break COW */ |
5b7a1d40 | 3968 | huge_ptep_clear_flush(vma, haddr, ptep); |
ac46d4f3 | 3969 | mmu_notifier_invalidate_range(mm, range.start, range.end); |
5b7a1d40 | 3970 | set_huge_pte_at(mm, haddr, ptep, |
1e8f889b | 3971 | make_huge_pte(vma, new_page, 1)); |
d281ee61 | 3972 | page_remove_rmap(old_page, true); |
5b7a1d40 | 3973 | hugepage_add_new_anon_rmap(new_page, vma, haddr); |
cb6acd01 | 3974 | set_page_huge_active(new_page); |
1e8f889b DG |
3975 | /* Make the old page be freed below */ |
3976 | new_page = old_page; | |
3977 | } | |
cb900f41 | 3978 | spin_unlock(ptl); |
ac46d4f3 | 3979 | mmu_notifier_invalidate_range_end(&range); |
ad4404a2 | 3980 | out_release_all: |
5b7a1d40 | 3981 | restore_reserve_on_error(h, vma, haddr, new_page); |
09cbfeaf | 3982 | put_page(new_page); |
ad4404a2 | 3983 | out_release_old: |
09cbfeaf | 3984 | put_page(old_page); |
8312034f | 3985 | |
ad4404a2 DB |
3986 | spin_lock(ptl); /* Caller expects lock to be held */ |
3987 | return ret; | |
1e8f889b DG |
3988 | } |
3989 | ||
04f2cbe3 | 3990 | /* Return the pagecache page at a given address within a VMA */ |
a5516438 AK |
3991 | static struct page *hugetlbfs_pagecache_page(struct hstate *h, |
3992 | struct vm_area_struct *vma, unsigned long address) | |
04f2cbe3 MG |
3993 | { |
3994 | struct address_space *mapping; | |
e7c4b0bf | 3995 | pgoff_t idx; |
04f2cbe3 MG |
3996 | |
3997 | mapping = vma->vm_file->f_mapping; | |
a5516438 | 3998 | idx = vma_hugecache_offset(h, vma, address); |
04f2cbe3 MG |
3999 | |
4000 | return find_lock_page(mapping, idx); | |
4001 | } | |
4002 | ||
3ae77f43 HD |
4003 | /* |
4004 | * Return whether there is a pagecache page to back given address within VMA. | |
4005 | * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page. | |
4006 | */ | |
4007 | static bool hugetlbfs_pagecache_present(struct hstate *h, | |
2a15efc9 HD |
4008 | struct vm_area_struct *vma, unsigned long address) |
4009 | { | |
4010 | struct address_space *mapping; | |
4011 | pgoff_t idx; | |
4012 | struct page *page; | |
4013 | ||
4014 | mapping = vma->vm_file->f_mapping; | |
4015 | idx = vma_hugecache_offset(h, vma, address); | |
4016 | ||
4017 | page = find_get_page(mapping, idx); | |
4018 | if (page) | |
4019 | put_page(page); | |
4020 | return page != NULL; | |
4021 | } | |
4022 | ||
ab76ad54 MK |
4023 | int huge_add_to_page_cache(struct page *page, struct address_space *mapping, |
4024 | pgoff_t idx) | |
4025 | { | |
4026 | struct inode *inode = mapping->host; | |
4027 | struct hstate *h = hstate_inode(inode); | |
4028 | int err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); | |
4029 | ||
4030 | if (err) | |
4031 | return err; | |
4032 | ClearPagePrivate(page); | |
4033 | ||
22146c3c MK |
4034 | /* |
4035 | * set page dirty so that it will not be removed from cache/file | |
4036 | * by non-hugetlbfs specific code paths. | |
4037 | */ | |
4038 | set_page_dirty(page); | |
4039 | ||
ab76ad54 MK |
4040 | spin_lock(&inode->i_lock); |
4041 | inode->i_blocks += blocks_per_huge_page(h); | |
4042 | spin_unlock(&inode->i_lock); | |
4043 | return 0; | |
4044 | } | |
4045 | ||
2b740303 SJ |
4046 | static vm_fault_t hugetlb_no_page(struct mm_struct *mm, |
4047 | struct vm_area_struct *vma, | |
4048 | struct address_space *mapping, pgoff_t idx, | |
4049 | unsigned long address, pte_t *ptep, unsigned int flags) | |
ac9b9c66 | 4050 | { |
a5516438 | 4051 | struct hstate *h = hstate_vma(vma); |
2b740303 | 4052 | vm_fault_t ret = VM_FAULT_SIGBUS; |
409eb8c2 | 4053 | int anon_rmap = 0; |
4c887265 | 4054 | unsigned long size; |
4c887265 | 4055 | struct page *page; |
1e8f889b | 4056 | pte_t new_pte; |
cb900f41 | 4057 | spinlock_t *ptl; |
285b8dca | 4058 | unsigned long haddr = address & huge_page_mask(h); |
cb6acd01 | 4059 | bool new_page = false; |
4c887265 | 4060 | |
04f2cbe3 MG |
4061 | /* |
4062 | * Currently, we are forced to kill the process in the event the | |
4063 | * original mapper has unmapped pages from the child due to a failed | |
25985edc | 4064 | * COW. Warn that such a situation has occurred as it may not be obvious |
04f2cbe3 MG |
4065 | */ |
4066 | if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { | |
910154d5 | 4067 | pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n", |
ffb22af5 | 4068 | current->pid); |
04f2cbe3 MG |
4069 | return ret; |
4070 | } | |
4071 | ||
4c887265 | 4072 | /* |
87bf91d3 MK |
4073 | * We can not race with truncation due to holding i_mmap_rwsem. |
4074 | * i_size is modified when holding i_mmap_rwsem, so check here | |
4075 | * once for faults beyond end of file. | |
4c887265 | 4076 | */ |
87bf91d3 MK |
4077 | size = i_size_read(mapping->host) >> huge_page_shift(h); |
4078 | if (idx >= size) | |
4079 | goto out; | |
4080 | ||
6bda666a CL |
4081 | retry: |
4082 | page = find_lock_page(mapping, idx); | |
4083 | if (!page) { | |
1a1aad8a MK |
4084 | /* |
4085 | * Check for page in userfault range | |
4086 | */ | |
4087 | if (userfaultfd_missing(vma)) { | |
4088 | u32 hash; | |
4089 | struct vm_fault vmf = { | |
4090 | .vma = vma, | |
285b8dca | 4091 | .address = haddr, |
1a1aad8a MK |
4092 | .flags = flags, |
4093 | /* | |
4094 | * Hard to debug if it ends up being | |
4095 | * used by a callee that assumes | |
4096 | * something about the other | |
4097 | * uninitialized fields... same as in | |
4098 | * memory.c | |
4099 | */ | |
4100 | }; | |
4101 | ||
4102 | /* | |
c0d0381a MK |
4103 | * hugetlb_fault_mutex and i_mmap_rwsem must be |
4104 | * dropped before handling userfault. Reacquire | |
4105 | * after handling fault to make calling code simpler. | |
1a1aad8a | 4106 | */ |
188b04a7 | 4107 | hash = hugetlb_fault_mutex_hash(mapping, idx); |
1a1aad8a | 4108 | mutex_unlock(&hugetlb_fault_mutex_table[hash]); |
c0d0381a | 4109 | i_mmap_unlock_read(mapping); |
1a1aad8a | 4110 | ret = handle_userfault(&vmf, VM_UFFD_MISSING); |
c0d0381a | 4111 | i_mmap_lock_read(mapping); |
1a1aad8a MK |
4112 | mutex_lock(&hugetlb_fault_mutex_table[hash]); |
4113 | goto out; | |
4114 | } | |
4115 | ||
285b8dca | 4116 | page = alloc_huge_page(vma, haddr, 0); |
2fc39cec | 4117 | if (IS_ERR(page)) { |
4643d67e MK |
4118 | /* |
4119 | * Returning error will result in faulting task being | |
4120 | * sent SIGBUS. The hugetlb fault mutex prevents two | |
4121 | * tasks from racing to fault in the same page which | |
4122 | * could result in false unable to allocate errors. | |
4123 | * Page migration does not take the fault mutex, but | |
4124 | * does a clear then write of pte's under page table | |
4125 | * lock. Page fault code could race with migration, | |
4126 | * notice the clear pte and try to allocate a page | |
4127 | * here. Before returning error, get ptl and make | |
4128 | * sure there really is no pte entry. | |
4129 | */ | |
4130 | ptl = huge_pte_lock(h, mm, ptep); | |
4131 | if (!huge_pte_none(huge_ptep_get(ptep))) { | |
4132 | ret = 0; | |
4133 | spin_unlock(ptl); | |
4134 | goto out; | |
4135 | } | |
4136 | spin_unlock(ptl); | |
2b740303 | 4137 | ret = vmf_error(PTR_ERR(page)); |
6bda666a CL |
4138 | goto out; |
4139 | } | |
47ad8475 | 4140 | clear_huge_page(page, address, pages_per_huge_page(h)); |
0ed361de | 4141 | __SetPageUptodate(page); |
cb6acd01 | 4142 | new_page = true; |
ac9b9c66 | 4143 | |
f83a275d | 4144 | if (vma->vm_flags & VM_MAYSHARE) { |
ab76ad54 | 4145 | int err = huge_add_to_page_cache(page, mapping, idx); |
6bda666a CL |
4146 | if (err) { |
4147 | put_page(page); | |
6bda666a CL |
4148 | if (err == -EEXIST) |
4149 | goto retry; | |
4150 | goto out; | |
4151 | } | |
23be7468 | 4152 | } else { |
6bda666a | 4153 | lock_page(page); |
0fe6e20b NH |
4154 | if (unlikely(anon_vma_prepare(vma))) { |
4155 | ret = VM_FAULT_OOM; | |
4156 | goto backout_unlocked; | |
4157 | } | |
409eb8c2 | 4158 | anon_rmap = 1; |
23be7468 | 4159 | } |
0fe6e20b | 4160 | } else { |
998b4382 NH |
4161 | /* |
4162 | * If memory error occurs between mmap() and fault, some process | |
4163 | * don't have hwpoisoned swap entry for errored virtual address. | |
4164 | * So we need to block hugepage fault by PG_hwpoison bit check. | |
4165 | */ | |
4166 | if (unlikely(PageHWPoison(page))) { | |
32f84528 | 4167 | ret = VM_FAULT_HWPOISON | |
972dc4de | 4168 | VM_FAULT_SET_HINDEX(hstate_index(h)); |
998b4382 NH |
4169 | goto backout_unlocked; |
4170 | } | |
6bda666a | 4171 | } |
1e8f889b | 4172 | |
57303d80 AW |
4173 | /* |
4174 | * If we are going to COW a private mapping later, we examine the | |
4175 | * pending reservations for this page now. This will ensure that | |
4176 | * any allocations necessary to record that reservation occur outside | |
4177 | * the spinlock. | |
4178 | */ | |
5e911373 | 4179 | if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { |
285b8dca | 4180 | if (vma_needs_reservation(h, vma, haddr) < 0) { |
2b26736c AW |
4181 | ret = VM_FAULT_OOM; |
4182 | goto backout_unlocked; | |
4183 | } | |
5e911373 | 4184 | /* Just decrements count, does not deallocate */ |
285b8dca | 4185 | vma_end_reservation(h, vma, haddr); |
5e911373 | 4186 | } |
57303d80 | 4187 | |
8bea8052 | 4188 | ptl = huge_pte_lock(h, mm, ptep); |
83c54070 | 4189 | ret = 0; |
7f2e9525 | 4190 | if (!huge_pte_none(huge_ptep_get(ptep))) |
4c887265 AL |
4191 | goto backout; |
4192 | ||
07443a85 JK |
4193 | if (anon_rmap) { |
4194 | ClearPagePrivate(page); | |
285b8dca | 4195 | hugepage_add_new_anon_rmap(page, vma, haddr); |
ac714904 | 4196 | } else |
53f9263b | 4197 | page_dup_rmap(page, true); |
1e8f889b DG |
4198 | new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) |
4199 | && (vma->vm_flags & VM_SHARED))); | |
285b8dca | 4200 | set_huge_pte_at(mm, haddr, ptep, new_pte); |
1e8f889b | 4201 | |
5d317b2b | 4202 | hugetlb_count_add(pages_per_huge_page(h), mm); |
788c7df4 | 4203 | if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { |
1e8f889b | 4204 | /* Optimization, do the COW without a second fault */ |
974e6d66 | 4205 | ret = hugetlb_cow(mm, vma, address, ptep, page, ptl); |
1e8f889b DG |
4206 | } |
4207 | ||
cb900f41 | 4208 | spin_unlock(ptl); |
cb6acd01 MK |
4209 | |
4210 | /* | |
4211 | * Only make newly allocated pages active. Existing pages found | |
4212 | * in the pagecache could be !page_huge_active() if they have been | |
4213 | * isolated for migration. | |
4214 | */ | |
4215 | if (new_page) | |
4216 | set_page_huge_active(page); | |
4217 | ||
4c887265 AL |
4218 | unlock_page(page); |
4219 | out: | |
ac9b9c66 | 4220 | return ret; |
4c887265 AL |
4221 | |
4222 | backout: | |
cb900f41 | 4223 | spin_unlock(ptl); |
2b26736c | 4224 | backout_unlocked: |
4c887265 | 4225 | unlock_page(page); |
285b8dca | 4226 | restore_reserve_on_error(h, vma, haddr, page); |
4c887265 AL |
4227 | put_page(page); |
4228 | goto out; | |
ac9b9c66 HD |
4229 | } |
4230 | ||
8382d914 | 4231 | #ifdef CONFIG_SMP |
188b04a7 | 4232 | u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx) |
8382d914 DB |
4233 | { |
4234 | unsigned long key[2]; | |
4235 | u32 hash; | |
4236 | ||
1b426bac MK |
4237 | key[0] = (unsigned long) mapping; |
4238 | key[1] = idx; | |
8382d914 | 4239 | |
55254636 | 4240 | hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0); |
8382d914 DB |
4241 | |
4242 | return hash & (num_fault_mutexes - 1); | |
4243 | } | |
4244 | #else | |
4245 | /* | |
4246 | * For uniprocesor systems we always use a single mutex, so just | |
4247 | * return 0 and avoid the hashing overhead. | |
4248 | */ | |
188b04a7 | 4249 | u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx) |
8382d914 DB |
4250 | { |
4251 | return 0; | |
4252 | } | |
4253 | #endif | |
4254 | ||
2b740303 | 4255 | vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, |
788c7df4 | 4256 | unsigned long address, unsigned int flags) |
86e5216f | 4257 | { |
8382d914 | 4258 | pte_t *ptep, entry; |
cb900f41 | 4259 | spinlock_t *ptl; |
2b740303 | 4260 | vm_fault_t ret; |
8382d914 DB |
4261 | u32 hash; |
4262 | pgoff_t idx; | |
0fe6e20b | 4263 | struct page *page = NULL; |
57303d80 | 4264 | struct page *pagecache_page = NULL; |
a5516438 | 4265 | struct hstate *h = hstate_vma(vma); |
8382d914 | 4266 | struct address_space *mapping; |
0f792cf9 | 4267 | int need_wait_lock = 0; |
285b8dca | 4268 | unsigned long haddr = address & huge_page_mask(h); |
86e5216f | 4269 | |
285b8dca | 4270 | ptep = huge_pte_offset(mm, haddr, huge_page_size(h)); |
fd6a03ed | 4271 | if (ptep) { |
c0d0381a MK |
4272 | /* |
4273 | * Since we hold no locks, ptep could be stale. That is | |
4274 | * OK as we are only making decisions based on content and | |
4275 | * not actually modifying content here. | |
4276 | */ | |
fd6a03ed | 4277 | entry = huge_ptep_get(ptep); |
290408d4 | 4278 | if (unlikely(is_hugetlb_entry_migration(entry))) { |
cb900f41 | 4279 | migration_entry_wait_huge(vma, mm, ptep); |
290408d4 NH |
4280 | return 0; |
4281 | } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) | |
32f84528 | 4282 | return VM_FAULT_HWPOISON_LARGE | |
972dc4de | 4283 | VM_FAULT_SET_HINDEX(hstate_index(h)); |
ddeaab32 MK |
4284 | } else { |
4285 | ptep = huge_pte_alloc(mm, haddr, huge_page_size(h)); | |
4286 | if (!ptep) | |
4287 | return VM_FAULT_OOM; | |
fd6a03ed NH |
4288 | } |
4289 | ||
c0d0381a MK |
4290 | /* |
4291 | * Acquire i_mmap_rwsem before calling huge_pte_alloc and hold | |
87bf91d3 MK |
4292 | * until finished with ptep. This serves two purposes: |
4293 | * 1) It prevents huge_pmd_unshare from being called elsewhere | |
4294 | * and making the ptep no longer valid. | |
4295 | * 2) It synchronizes us with i_size modifications during truncation. | |
c0d0381a MK |
4296 | * |
4297 | * ptep could have already be assigned via huge_pte_offset. That | |
4298 | * is OK, as huge_pte_alloc will return the same value unless | |
4299 | * something has changed. | |
4300 | */ | |
8382d914 | 4301 | mapping = vma->vm_file->f_mapping; |
c0d0381a MK |
4302 | i_mmap_lock_read(mapping); |
4303 | ptep = huge_pte_alloc(mm, haddr, huge_page_size(h)); | |
4304 | if (!ptep) { | |
4305 | i_mmap_unlock_read(mapping); | |
4306 | return VM_FAULT_OOM; | |
4307 | } | |
8382d914 | 4308 | |
3935baa9 DG |
4309 | /* |
4310 | * Serialize hugepage allocation and instantiation, so that we don't | |
4311 | * get spurious allocation failures if two CPUs race to instantiate | |
4312 | * the same page in the page cache. | |
4313 | */ | |
c0d0381a | 4314 | idx = vma_hugecache_offset(h, vma, haddr); |
188b04a7 | 4315 | hash = hugetlb_fault_mutex_hash(mapping, idx); |
c672c7f2 | 4316 | mutex_lock(&hugetlb_fault_mutex_table[hash]); |
8382d914 | 4317 | |
7f2e9525 GS |
4318 | entry = huge_ptep_get(ptep); |
4319 | if (huge_pte_none(entry)) { | |
8382d914 | 4320 | ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags); |
b4d1d99f | 4321 | goto out_mutex; |
3935baa9 | 4322 | } |
86e5216f | 4323 | |
83c54070 | 4324 | ret = 0; |
1e8f889b | 4325 | |
0f792cf9 NH |
4326 | /* |
4327 | * entry could be a migration/hwpoison entry at this point, so this | |
4328 | * check prevents the kernel from going below assuming that we have | |
4329 | * a active hugepage in pagecache. This goto expects the 2nd page fault, | |
4330 | * and is_hugetlb_entry_(migration|hwpoisoned) check will properly | |
4331 | * handle it. | |
4332 | */ | |
4333 | if (!pte_present(entry)) | |
4334 | goto out_mutex; | |
4335 | ||
57303d80 AW |
4336 | /* |
4337 | * If we are going to COW the mapping later, we examine the pending | |
4338 | * reservations for this page now. This will ensure that any | |
4339 | * allocations necessary to record that reservation occur outside the | |
4340 | * spinlock. For private mappings, we also lookup the pagecache | |
4341 | * page now as it is used to determine if a reservation has been | |
4342 | * consumed. | |
4343 | */ | |
106c992a | 4344 | if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { |
285b8dca | 4345 | if (vma_needs_reservation(h, vma, haddr) < 0) { |
2b26736c | 4346 | ret = VM_FAULT_OOM; |
b4d1d99f | 4347 | goto out_mutex; |
2b26736c | 4348 | } |
5e911373 | 4349 | /* Just decrements count, does not deallocate */ |
285b8dca | 4350 | vma_end_reservation(h, vma, haddr); |
57303d80 | 4351 | |
f83a275d | 4352 | if (!(vma->vm_flags & VM_MAYSHARE)) |
57303d80 | 4353 | pagecache_page = hugetlbfs_pagecache_page(h, |
285b8dca | 4354 | vma, haddr); |
57303d80 AW |
4355 | } |
4356 | ||
0f792cf9 NH |
4357 | ptl = huge_pte_lock(h, mm, ptep); |
4358 | ||
4359 | /* Check for a racing update before calling hugetlb_cow */ | |
4360 | if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) | |
4361 | goto out_ptl; | |
4362 | ||
56c9cfb1 NH |
4363 | /* |
4364 | * hugetlb_cow() requires page locks of pte_page(entry) and | |
4365 | * pagecache_page, so here we need take the former one | |
4366 | * when page != pagecache_page or !pagecache_page. | |
56c9cfb1 NH |
4367 | */ |
4368 | page = pte_page(entry); | |
4369 | if (page != pagecache_page) | |
0f792cf9 NH |
4370 | if (!trylock_page(page)) { |
4371 | need_wait_lock = 1; | |
4372 | goto out_ptl; | |
4373 | } | |
b4d1d99f | 4374 | |
0f792cf9 | 4375 | get_page(page); |
b4d1d99f | 4376 | |
788c7df4 | 4377 | if (flags & FAULT_FLAG_WRITE) { |
106c992a | 4378 | if (!huge_pte_write(entry)) { |
974e6d66 | 4379 | ret = hugetlb_cow(mm, vma, address, ptep, |
3999f52e | 4380 | pagecache_page, ptl); |
0f792cf9 | 4381 | goto out_put_page; |
b4d1d99f | 4382 | } |
106c992a | 4383 | entry = huge_pte_mkdirty(entry); |
b4d1d99f DG |
4384 | } |
4385 | entry = pte_mkyoung(entry); | |
285b8dca | 4386 | if (huge_ptep_set_access_flags(vma, haddr, ptep, entry, |
788c7df4 | 4387 | flags & FAULT_FLAG_WRITE)) |
285b8dca | 4388 | update_mmu_cache(vma, haddr, ptep); |
0f792cf9 NH |
4389 | out_put_page: |
4390 | if (page != pagecache_page) | |
4391 | unlock_page(page); | |
4392 | put_page(page); | |
cb900f41 KS |
4393 | out_ptl: |
4394 | spin_unlock(ptl); | |
57303d80 AW |
4395 | |
4396 | if (pagecache_page) { | |
4397 | unlock_page(pagecache_page); | |
4398 | put_page(pagecache_page); | |
4399 | } | |
b4d1d99f | 4400 | out_mutex: |
c672c7f2 | 4401 | mutex_unlock(&hugetlb_fault_mutex_table[hash]); |
c0d0381a | 4402 | i_mmap_unlock_read(mapping); |
0f792cf9 NH |
4403 | /* |
4404 | * Generally it's safe to hold refcount during waiting page lock. But | |
4405 | * here we just wait to defer the next page fault to avoid busy loop and | |
4406 | * the page is not used after unlocked before returning from the current | |
4407 | * page fault. So we are safe from accessing freed page, even if we wait | |
4408 | * here without taking refcount. | |
4409 | */ | |
4410 | if (need_wait_lock) | |
4411 | wait_on_page_locked(page); | |
1e8f889b | 4412 | return ret; |
86e5216f AL |
4413 | } |
4414 | ||
8fb5debc MK |
4415 | /* |
4416 | * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with | |
4417 | * modifications for huge pages. | |
4418 | */ | |
4419 | int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, | |
4420 | pte_t *dst_pte, | |
4421 | struct vm_area_struct *dst_vma, | |
4422 | unsigned long dst_addr, | |
4423 | unsigned long src_addr, | |
4424 | struct page **pagep) | |
4425 | { | |
1e392147 AA |
4426 | struct address_space *mapping; |
4427 | pgoff_t idx; | |
4428 | unsigned long size; | |
1c9e8def | 4429 | int vm_shared = dst_vma->vm_flags & VM_SHARED; |
8fb5debc MK |
4430 | struct hstate *h = hstate_vma(dst_vma); |
4431 | pte_t _dst_pte; | |
4432 | spinlock_t *ptl; | |
4433 | int ret; | |
4434 | struct page *page; | |
4435 | ||
4436 | if (!*pagep) { | |
4437 | ret = -ENOMEM; | |
4438 | page = alloc_huge_page(dst_vma, dst_addr, 0); | |
4439 | if (IS_ERR(page)) | |
4440 | goto out; | |
4441 | ||
4442 | ret = copy_huge_page_from_user(page, | |
4443 | (const void __user *) src_addr, | |
810a56b9 | 4444 | pages_per_huge_page(h), false); |
8fb5debc MK |
4445 | |
4446 | /* fallback to copy_from_user outside mmap_sem */ | |
4447 | if (unlikely(ret)) { | |
9e368259 | 4448 | ret = -ENOENT; |
8fb5debc MK |
4449 | *pagep = page; |
4450 | /* don't free the page */ | |
4451 | goto out; | |
4452 | } | |
4453 | } else { | |
4454 | page = *pagep; | |
4455 | *pagep = NULL; | |
4456 | } | |
4457 | ||
4458 | /* | |
4459 | * The memory barrier inside __SetPageUptodate makes sure that | |
4460 | * preceding stores to the page contents become visible before | |
4461 | * the set_pte_at() write. | |
4462 | */ | |
4463 | __SetPageUptodate(page); | |
8fb5debc | 4464 | |
1e392147 AA |
4465 | mapping = dst_vma->vm_file->f_mapping; |
4466 | idx = vma_hugecache_offset(h, dst_vma, dst_addr); | |
4467 | ||
1c9e8def MK |
4468 | /* |
4469 | * If shared, add to page cache | |
4470 | */ | |
4471 | if (vm_shared) { | |
1e392147 AA |
4472 | size = i_size_read(mapping->host) >> huge_page_shift(h); |
4473 | ret = -EFAULT; | |
4474 | if (idx >= size) | |
4475 | goto out_release_nounlock; | |
1c9e8def | 4476 | |
1e392147 AA |
4477 | /* |
4478 | * Serialization between remove_inode_hugepages() and | |
4479 | * huge_add_to_page_cache() below happens through the | |
4480 | * hugetlb_fault_mutex_table that here must be hold by | |
4481 | * the caller. | |
4482 | */ | |
1c9e8def MK |
4483 | ret = huge_add_to_page_cache(page, mapping, idx); |
4484 | if (ret) | |
4485 | goto out_release_nounlock; | |
4486 | } | |
4487 | ||
8fb5debc MK |
4488 | ptl = huge_pte_lockptr(h, dst_mm, dst_pte); |
4489 | spin_lock(ptl); | |
4490 | ||
1e392147 AA |
4491 | /* |
4492 | * Recheck the i_size after holding PT lock to make sure not | |
4493 | * to leave any page mapped (as page_mapped()) beyond the end | |
4494 | * of the i_size (remove_inode_hugepages() is strict about | |
4495 | * enforcing that). If we bail out here, we'll also leave a | |
4496 | * page in the radix tree in the vm_shared case beyond the end | |
4497 | * of the i_size, but remove_inode_hugepages() will take care | |
4498 | * of it as soon as we drop the hugetlb_fault_mutex_table. | |
4499 | */ | |
4500 | size = i_size_read(mapping->host) >> huge_page_shift(h); | |
4501 | ret = -EFAULT; | |
4502 | if (idx >= size) | |
4503 | goto out_release_unlock; | |
4504 | ||
8fb5debc MK |
4505 | ret = -EEXIST; |
4506 | if (!huge_pte_none(huge_ptep_get(dst_pte))) | |
4507 | goto out_release_unlock; | |
4508 | ||
1c9e8def MK |
4509 | if (vm_shared) { |
4510 | page_dup_rmap(page, true); | |
4511 | } else { | |
4512 | ClearPagePrivate(page); | |
4513 | hugepage_add_new_anon_rmap(page, dst_vma, dst_addr); | |
4514 | } | |
8fb5debc MK |
4515 | |
4516 | _dst_pte = make_huge_pte(dst_vma, page, dst_vma->vm_flags & VM_WRITE); | |
4517 | if (dst_vma->vm_flags & VM_WRITE) | |
4518 | _dst_pte = huge_pte_mkdirty(_dst_pte); | |
4519 | _dst_pte = pte_mkyoung(_dst_pte); | |
4520 | ||
4521 | set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); | |
4522 | ||
4523 | (void)huge_ptep_set_access_flags(dst_vma, dst_addr, dst_pte, _dst_pte, | |
4524 | dst_vma->vm_flags & VM_WRITE); | |
4525 | hugetlb_count_add(pages_per_huge_page(h), dst_mm); | |
4526 | ||
4527 | /* No need to invalidate - it was non-present before */ | |
4528 | update_mmu_cache(dst_vma, dst_addr, dst_pte); | |
4529 | ||
4530 | spin_unlock(ptl); | |
cb6acd01 | 4531 | set_page_huge_active(page); |
1c9e8def MK |
4532 | if (vm_shared) |
4533 | unlock_page(page); | |
8fb5debc MK |
4534 | ret = 0; |
4535 | out: | |
4536 | return ret; | |
4537 | out_release_unlock: | |
4538 | spin_unlock(ptl); | |
1c9e8def MK |
4539 | if (vm_shared) |
4540 | unlock_page(page); | |
5af10dfd | 4541 | out_release_nounlock: |
8fb5debc MK |
4542 | put_page(page); |
4543 | goto out; | |
4544 | } | |
4545 | ||
28a35716 ML |
4546 | long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, |
4547 | struct page **pages, struct vm_area_struct **vmas, | |
4548 | unsigned long *position, unsigned long *nr_pages, | |
4f6da934 | 4549 | long i, unsigned int flags, int *locked) |
63551ae0 | 4550 | { |
d5d4b0aa KC |
4551 | unsigned long pfn_offset; |
4552 | unsigned long vaddr = *position; | |
28a35716 | 4553 | unsigned long remainder = *nr_pages; |
a5516438 | 4554 | struct hstate *h = hstate_vma(vma); |
2be7cfed | 4555 | int err = -EFAULT; |
63551ae0 | 4556 | |
63551ae0 | 4557 | while (vaddr < vma->vm_end && remainder) { |
4c887265 | 4558 | pte_t *pte; |
cb900f41 | 4559 | spinlock_t *ptl = NULL; |
2a15efc9 | 4560 | int absent; |
4c887265 | 4561 | struct page *page; |
63551ae0 | 4562 | |
02057967 DR |
4563 | /* |
4564 | * If we have a pending SIGKILL, don't keep faulting pages and | |
4565 | * potentially allocating memory. | |
4566 | */ | |
fa45f116 | 4567 | if (fatal_signal_pending(current)) { |
02057967 DR |
4568 | remainder = 0; |
4569 | break; | |
4570 | } | |
4571 | ||
4c887265 AL |
4572 | /* |
4573 | * Some archs (sparc64, sh*) have multiple pte_ts to | |
2a15efc9 | 4574 | * each hugepage. We have to make sure we get the |
4c887265 | 4575 | * first, for the page indexing below to work. |
cb900f41 KS |
4576 | * |
4577 | * Note that page table lock is not held when pte is null. | |
4c887265 | 4578 | */ |
7868a208 PA |
4579 | pte = huge_pte_offset(mm, vaddr & huge_page_mask(h), |
4580 | huge_page_size(h)); | |
cb900f41 KS |
4581 | if (pte) |
4582 | ptl = huge_pte_lock(h, mm, pte); | |
2a15efc9 HD |
4583 | absent = !pte || huge_pte_none(huge_ptep_get(pte)); |
4584 | ||
4585 | /* | |
4586 | * When coredumping, it suits get_dump_page if we just return | |
3ae77f43 HD |
4587 | * an error where there's an empty slot with no huge pagecache |
4588 | * to back it. This way, we avoid allocating a hugepage, and | |
4589 | * the sparse dumpfile avoids allocating disk blocks, but its | |
4590 | * huge holes still show up with zeroes where they need to be. | |
2a15efc9 | 4591 | */ |
3ae77f43 HD |
4592 | if (absent && (flags & FOLL_DUMP) && |
4593 | !hugetlbfs_pagecache_present(h, vma, vaddr)) { | |
cb900f41 KS |
4594 | if (pte) |
4595 | spin_unlock(ptl); | |
2a15efc9 HD |
4596 | remainder = 0; |
4597 | break; | |
4598 | } | |
63551ae0 | 4599 | |
9cc3a5bd NH |
4600 | /* |
4601 | * We need call hugetlb_fault for both hugepages under migration | |
4602 | * (in which case hugetlb_fault waits for the migration,) and | |
4603 | * hwpoisoned hugepages (in which case we need to prevent the | |
4604 | * caller from accessing to them.) In order to do this, we use | |
4605 | * here is_swap_pte instead of is_hugetlb_entry_migration and | |
4606 | * is_hugetlb_entry_hwpoisoned. This is because it simply covers | |
4607 | * both cases, and because we can't follow correct pages | |
4608 | * directly from any kind of swap entries. | |
4609 | */ | |
4610 | if (absent || is_swap_pte(huge_ptep_get(pte)) || | |
106c992a GS |
4611 | ((flags & FOLL_WRITE) && |
4612 | !huge_pte_write(huge_ptep_get(pte)))) { | |
2b740303 | 4613 | vm_fault_t ret; |
87ffc118 | 4614 | unsigned int fault_flags = 0; |
63551ae0 | 4615 | |
cb900f41 KS |
4616 | if (pte) |
4617 | spin_unlock(ptl); | |
87ffc118 AA |
4618 | if (flags & FOLL_WRITE) |
4619 | fault_flags |= FAULT_FLAG_WRITE; | |
4f6da934 | 4620 | if (locked) |
71335f37 PX |
4621 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | |
4622 | FAULT_FLAG_KILLABLE; | |
87ffc118 AA |
4623 | if (flags & FOLL_NOWAIT) |
4624 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | | |
4625 | FAULT_FLAG_RETRY_NOWAIT; | |
4626 | if (flags & FOLL_TRIED) { | |
4426e945 PX |
4627 | /* |
4628 | * Note: FAULT_FLAG_ALLOW_RETRY and | |
4629 | * FAULT_FLAG_TRIED can co-exist | |
4630 | */ | |
87ffc118 AA |
4631 | fault_flags |= FAULT_FLAG_TRIED; |
4632 | } | |
4633 | ret = hugetlb_fault(mm, vma, vaddr, fault_flags); | |
4634 | if (ret & VM_FAULT_ERROR) { | |
2be7cfed | 4635 | err = vm_fault_to_errno(ret, flags); |
87ffc118 AA |
4636 | remainder = 0; |
4637 | break; | |
4638 | } | |
4639 | if (ret & VM_FAULT_RETRY) { | |
4f6da934 | 4640 | if (locked && |
1ac25013 | 4641 | !(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) |
4f6da934 | 4642 | *locked = 0; |
87ffc118 AA |
4643 | *nr_pages = 0; |
4644 | /* | |
4645 | * VM_FAULT_RETRY must not return an | |
4646 | * error, it will return zero | |
4647 | * instead. | |
4648 | * | |
4649 | * No need to update "position" as the | |
4650 | * caller will not check it after | |
4651 | * *nr_pages is set to 0. | |
4652 | */ | |
4653 | return i; | |
4654 | } | |
4655 | continue; | |
4c887265 AL |
4656 | } |
4657 | ||
a5516438 | 4658 | pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; |
7f2e9525 | 4659 | page = pte_page(huge_ptep_get(pte)); |
8fde12ca | 4660 | |
acbfb087 ZL |
4661 | /* |
4662 | * If subpage information not requested, update counters | |
4663 | * and skip the same_page loop below. | |
4664 | */ | |
4665 | if (!pages && !vmas && !pfn_offset && | |
4666 | (vaddr + huge_page_size(h) < vma->vm_end) && | |
4667 | (remainder >= pages_per_huge_page(h))) { | |
4668 | vaddr += huge_page_size(h); | |
4669 | remainder -= pages_per_huge_page(h); | |
4670 | i += pages_per_huge_page(h); | |
4671 | spin_unlock(ptl); | |
4672 | continue; | |
4673 | } | |
4674 | ||
d5d4b0aa | 4675 | same_page: |
d6692183 | 4676 | if (pages) { |
2a15efc9 | 4677 | pages[i] = mem_map_offset(page, pfn_offset); |
3faa52c0 JH |
4678 | /* |
4679 | * try_grab_page() should always succeed here, because: | |
4680 | * a) we hold the ptl lock, and b) we've just checked | |
4681 | * that the huge page is present in the page tables. If | |
4682 | * the huge page is present, then the tail pages must | |
4683 | * also be present. The ptl prevents the head page and | |
4684 | * tail pages from being rearranged in any way. So this | |
4685 | * page must be available at this point, unless the page | |
4686 | * refcount overflowed: | |
4687 | */ | |
4688 | if (WARN_ON_ONCE(!try_grab_page(pages[i], flags))) { | |
4689 | spin_unlock(ptl); | |
4690 | remainder = 0; | |
4691 | err = -ENOMEM; | |
4692 | break; | |
4693 | } | |
d6692183 | 4694 | } |
63551ae0 DG |
4695 | |
4696 | if (vmas) | |
4697 | vmas[i] = vma; | |
4698 | ||
4699 | vaddr += PAGE_SIZE; | |
d5d4b0aa | 4700 | ++pfn_offset; |
63551ae0 DG |
4701 | --remainder; |
4702 | ++i; | |
d5d4b0aa | 4703 | if (vaddr < vma->vm_end && remainder && |
a5516438 | 4704 | pfn_offset < pages_per_huge_page(h)) { |
d5d4b0aa KC |
4705 | /* |
4706 | * We use pfn_offset to avoid touching the pageframes | |
4707 | * of this compound page. | |
4708 | */ | |
4709 | goto same_page; | |
4710 | } | |
cb900f41 | 4711 | spin_unlock(ptl); |
63551ae0 | 4712 | } |
28a35716 | 4713 | *nr_pages = remainder; |
87ffc118 AA |
4714 | /* |
4715 | * setting position is actually required only if remainder is | |
4716 | * not zero but it's faster not to add a "if (remainder)" | |
4717 | * branch. | |
4718 | */ | |
63551ae0 DG |
4719 | *position = vaddr; |
4720 | ||
2be7cfed | 4721 | return i ? i : err; |
63551ae0 | 4722 | } |
8f860591 | 4723 | |
5491ae7b AK |
4724 | #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE |
4725 | /* | |
4726 | * ARCHes with special requirements for evicting HUGETLB backing TLB entries can | |
4727 | * implement this. | |
4728 | */ | |
4729 | #define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end) | |
4730 | #endif | |
4731 | ||
7da4d641 | 4732 | unsigned long hugetlb_change_protection(struct vm_area_struct *vma, |
8f860591 ZY |
4733 | unsigned long address, unsigned long end, pgprot_t newprot) |
4734 | { | |
4735 | struct mm_struct *mm = vma->vm_mm; | |
4736 | unsigned long start = address; | |
4737 | pte_t *ptep; | |
4738 | pte_t pte; | |
a5516438 | 4739 | struct hstate *h = hstate_vma(vma); |
7da4d641 | 4740 | unsigned long pages = 0; |
dff11abe | 4741 | bool shared_pmd = false; |
ac46d4f3 | 4742 | struct mmu_notifier_range range; |
dff11abe MK |
4743 | |
4744 | /* | |
4745 | * In the case of shared PMDs, the area to flush could be beyond | |
ac46d4f3 | 4746 | * start/end. Set range.start/range.end to cover the maximum possible |
dff11abe MK |
4747 | * range if PMD sharing is possible. |
4748 | */ | |
7269f999 JG |
4749 | mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, |
4750 | 0, vma, mm, start, end); | |
ac46d4f3 | 4751 | adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); |
8f860591 ZY |
4752 | |
4753 | BUG_ON(address >= end); | |
ac46d4f3 | 4754 | flush_cache_range(vma, range.start, range.end); |
8f860591 | 4755 | |
ac46d4f3 | 4756 | mmu_notifier_invalidate_range_start(&range); |
83cde9e8 | 4757 | i_mmap_lock_write(vma->vm_file->f_mapping); |
a5516438 | 4758 | for (; address < end; address += huge_page_size(h)) { |
cb900f41 | 4759 | spinlock_t *ptl; |
7868a208 | 4760 | ptep = huge_pte_offset(mm, address, huge_page_size(h)); |
8f860591 ZY |
4761 | if (!ptep) |
4762 | continue; | |
cb900f41 | 4763 | ptl = huge_pte_lock(h, mm, ptep); |
7da4d641 PZ |
4764 | if (huge_pmd_unshare(mm, &address, ptep)) { |
4765 | pages++; | |
cb900f41 | 4766 | spin_unlock(ptl); |
dff11abe | 4767 | shared_pmd = true; |
39dde65c | 4768 | continue; |
7da4d641 | 4769 | } |
a8bda28d NH |
4770 | pte = huge_ptep_get(ptep); |
4771 | if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { | |
4772 | spin_unlock(ptl); | |
4773 | continue; | |
4774 | } | |
4775 | if (unlikely(is_hugetlb_entry_migration(pte))) { | |
4776 | swp_entry_t entry = pte_to_swp_entry(pte); | |
4777 | ||
4778 | if (is_write_migration_entry(entry)) { | |
4779 | pte_t newpte; | |
4780 | ||
4781 | make_migration_entry_read(&entry); | |
4782 | newpte = swp_entry_to_pte(entry); | |
e5251fd4 PA |
4783 | set_huge_swap_pte_at(mm, address, ptep, |
4784 | newpte, huge_page_size(h)); | |
a8bda28d NH |
4785 | pages++; |
4786 | } | |
4787 | spin_unlock(ptl); | |
4788 | continue; | |
4789 | } | |
4790 | if (!huge_pte_none(pte)) { | |
023bdd00 AK |
4791 | pte_t old_pte; |
4792 | ||
4793 | old_pte = huge_ptep_modify_prot_start(vma, address, ptep); | |
4794 | pte = pte_mkhuge(huge_pte_modify(old_pte, newprot)); | |
be7517d6 | 4795 | pte = arch_make_huge_pte(pte, vma, NULL, 0); |
023bdd00 | 4796 | huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte); |
7da4d641 | 4797 | pages++; |
8f860591 | 4798 | } |
cb900f41 | 4799 | spin_unlock(ptl); |
8f860591 | 4800 | } |
d833352a | 4801 | /* |
c8c06efa | 4802 | * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare |
d833352a | 4803 | * may have cleared our pud entry and done put_page on the page table: |
c8c06efa | 4804 | * once we release i_mmap_rwsem, another task can do the final put_page |
dff11abe MK |
4805 | * and that page table be reused and filled with junk. If we actually |
4806 | * did unshare a page of pmds, flush the range corresponding to the pud. | |
d833352a | 4807 | */ |
dff11abe | 4808 | if (shared_pmd) |
ac46d4f3 | 4809 | flush_hugetlb_tlb_range(vma, range.start, range.end); |
dff11abe MK |
4810 | else |
4811 | flush_hugetlb_tlb_range(vma, start, end); | |
0f10851e JG |
4812 | /* |
4813 | * No need to call mmu_notifier_invalidate_range() we are downgrading | |
4814 | * page table protection not changing it to point to a new page. | |
4815 | * | |
ad56b738 | 4816 | * See Documentation/vm/mmu_notifier.rst |
0f10851e | 4817 | */ |
83cde9e8 | 4818 | i_mmap_unlock_write(vma->vm_file->f_mapping); |
ac46d4f3 | 4819 | mmu_notifier_invalidate_range_end(&range); |
7da4d641 PZ |
4820 | |
4821 | return pages << h->order; | |
8f860591 ZY |
4822 | } |
4823 | ||
a1e78772 MG |
4824 | int hugetlb_reserve_pages(struct inode *inode, |
4825 | long from, long to, | |
5a6fe125 | 4826 | struct vm_area_struct *vma, |
ca16d140 | 4827 | vm_flags_t vm_flags) |
e4e574b7 | 4828 | { |
0db9d74e | 4829 | long ret, chg, add = -1; |
a5516438 | 4830 | struct hstate *h = hstate_inode(inode); |
90481622 | 4831 | struct hugepage_subpool *spool = subpool_inode(inode); |
9119a41e | 4832 | struct resv_map *resv_map; |
e9fe92ae | 4833 | struct hugetlb_cgroup *h_cg; |
0db9d74e | 4834 | long gbl_reserve, regions_needed = 0; |
e4e574b7 | 4835 | |
63489f8e MK |
4836 | /* This should never happen */ |
4837 | if (from > to) { | |
4838 | VM_WARN(1, "%s called with a negative range\n", __func__); | |
4839 | return -EINVAL; | |
4840 | } | |
4841 | ||
17c9d12e MG |
4842 | /* |
4843 | * Only apply hugepage reservation if asked. At fault time, an | |
4844 | * attempt will be made for VM_NORESERVE to allocate a page | |
90481622 | 4845 | * without using reserves |
17c9d12e | 4846 | */ |
ca16d140 | 4847 | if (vm_flags & VM_NORESERVE) |
17c9d12e MG |
4848 | return 0; |
4849 | ||
a1e78772 MG |
4850 | /* |
4851 | * Shared mappings base their reservation on the number of pages that | |
4852 | * are already allocated on behalf of the file. Private mappings need | |
4853 | * to reserve the full area even if read-only as mprotect() may be | |
4854 | * called to make the mapping read-write. Assume !vma is a shm mapping | |
4855 | */ | |
9119a41e | 4856 | if (!vma || vma->vm_flags & VM_MAYSHARE) { |
f27a5136 MK |
4857 | /* |
4858 | * resv_map can not be NULL as hugetlb_reserve_pages is only | |
4859 | * called for inodes for which resv_maps were created (see | |
4860 | * hugetlbfs_get_inode). | |
4861 | */ | |
4e35f483 | 4862 | resv_map = inode_resv_map(inode); |
9119a41e | 4863 | |
0db9d74e | 4864 | chg = region_chg(resv_map, from, to, ®ions_needed); |
9119a41e JK |
4865 | |
4866 | } else { | |
e9fe92ae | 4867 | /* Private mapping. */ |
9119a41e | 4868 | resv_map = resv_map_alloc(); |
17c9d12e MG |
4869 | if (!resv_map) |
4870 | return -ENOMEM; | |
4871 | ||
a1e78772 | 4872 | chg = to - from; |
84afd99b | 4873 | |
e9fe92ae MA |
4874 | if (hugetlb_cgroup_charge_cgroup_rsvd( |
4875 | hstate_index(h), chg * pages_per_huge_page(h), | |
4876 | &h_cg)) { | |
4877 | kref_put(&resv_map->refs, resv_map_release); | |
4878 | return -ENOMEM; | |
4879 | } | |
4880 | ||
4881 | /* | |
4882 | * Since this branch handles private mappings, we attach the | |
4883 | * counter to uncharge for this reservation off resv_map. | |
4884 | */ | |
4885 | resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h); | |
4886 | ||
17c9d12e MG |
4887 | set_vma_resv_map(vma, resv_map); |
4888 | set_vma_resv_flags(vma, HPAGE_RESV_OWNER); | |
4889 | } | |
4890 | ||
c50ac050 DH |
4891 | if (chg < 0) { |
4892 | ret = chg; | |
4893 | goto out_err; | |
4894 | } | |
8a630112 | 4895 | |
1c5ecae3 MK |
4896 | /* |
4897 | * There must be enough pages in the subpool for the mapping. If | |
4898 | * the subpool has a minimum size, there may be some global | |
4899 | * reservations already in place (gbl_reserve). | |
4900 | */ | |
4901 | gbl_reserve = hugepage_subpool_get_pages(spool, chg); | |
4902 | if (gbl_reserve < 0) { | |
c50ac050 DH |
4903 | ret = -ENOSPC; |
4904 | goto out_err; | |
4905 | } | |
5a6fe125 MG |
4906 | |
4907 | /* | |
17c9d12e | 4908 | * Check enough hugepages are available for the reservation. |
90481622 | 4909 | * Hand the pages back to the subpool if there are not |
5a6fe125 | 4910 | */ |
1c5ecae3 | 4911 | ret = hugetlb_acct_memory(h, gbl_reserve); |
68842c9b | 4912 | if (ret < 0) { |
1c5ecae3 MK |
4913 | /* put back original number of pages, chg */ |
4914 | (void)hugepage_subpool_put_pages(spool, chg); | |
c50ac050 | 4915 | goto out_err; |
68842c9b | 4916 | } |
17c9d12e MG |
4917 | |
4918 | /* | |
4919 | * Account for the reservations made. Shared mappings record regions | |
4920 | * that have reservations as they are shared by multiple VMAs. | |
4921 | * When the last VMA disappears, the region map says how much | |
4922 | * the reservation was and the page cache tells how much of | |
4923 | * the reservation was consumed. Private mappings are per-VMA and | |
4924 | * only the consumed reservations are tracked. When the VMA | |
4925 | * disappears, the original reservation is the VMA size and the | |
4926 | * consumed reservations are stored in the map. Hence, nothing | |
4927 | * else has to be done for private mappings here | |
4928 | */ | |
33039678 | 4929 | if (!vma || vma->vm_flags & VM_MAYSHARE) { |
0db9d74e MA |
4930 | add = region_add(resv_map, from, to, regions_needed); |
4931 | ||
4932 | if (unlikely(add < 0)) { | |
4933 | hugetlb_acct_memory(h, -gbl_reserve); | |
4934 | /* put back original number of pages, chg */ | |
4935 | (void)hugepage_subpool_put_pages(spool, chg); | |
4936 | goto out_err; | |
4937 | } else if (unlikely(chg > add)) { | |
33039678 MK |
4938 | /* |
4939 | * pages in this range were added to the reserve | |
4940 | * map between region_chg and region_add. This | |
4941 | * indicates a race with alloc_huge_page. Adjust | |
4942 | * the subpool and reserve counts modified above | |
4943 | * based on the difference. | |
4944 | */ | |
4945 | long rsv_adjust; | |
4946 | ||
4947 | rsv_adjust = hugepage_subpool_put_pages(spool, | |
4948 | chg - add); | |
4949 | hugetlb_acct_memory(h, -rsv_adjust); | |
4950 | } | |
4951 | } | |
a43a8c39 | 4952 | return 0; |
c50ac050 | 4953 | out_err: |
5e911373 | 4954 | if (!vma || vma->vm_flags & VM_MAYSHARE) |
0db9d74e MA |
4955 | /* Only call region_abort if the region_chg succeeded but the |
4956 | * region_add failed or didn't run. | |
4957 | */ | |
4958 | if (chg >= 0 && add < 0) | |
4959 | region_abort(resv_map, from, to, regions_needed); | |
f031dd27 JK |
4960 | if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) |
4961 | kref_put(&resv_map->refs, resv_map_release); | |
c50ac050 | 4962 | return ret; |
a43a8c39 KC |
4963 | } |
4964 | ||
b5cec28d MK |
4965 | long hugetlb_unreserve_pages(struct inode *inode, long start, long end, |
4966 | long freed) | |
a43a8c39 | 4967 | { |
a5516438 | 4968 | struct hstate *h = hstate_inode(inode); |
4e35f483 | 4969 | struct resv_map *resv_map = inode_resv_map(inode); |
9119a41e | 4970 | long chg = 0; |
90481622 | 4971 | struct hugepage_subpool *spool = subpool_inode(inode); |
1c5ecae3 | 4972 | long gbl_reserve; |
45c682a6 | 4973 | |
f27a5136 MK |
4974 | /* |
4975 | * Since this routine can be called in the evict inode path for all | |
4976 | * hugetlbfs inodes, resv_map could be NULL. | |
4977 | */ | |
b5cec28d MK |
4978 | if (resv_map) { |
4979 | chg = region_del(resv_map, start, end); | |
4980 | /* | |
4981 | * region_del() can fail in the rare case where a region | |
4982 | * must be split and another region descriptor can not be | |
4983 | * allocated. If end == LONG_MAX, it will not fail. | |
4984 | */ | |
4985 | if (chg < 0) | |
4986 | return chg; | |
4987 | } | |
4988 | ||
45c682a6 | 4989 | spin_lock(&inode->i_lock); |
e4c6f8be | 4990 | inode->i_blocks -= (blocks_per_huge_page(h) * freed); |
45c682a6 KC |
4991 | spin_unlock(&inode->i_lock); |
4992 | ||
1c5ecae3 MK |
4993 | /* |
4994 | * If the subpool has a minimum size, the number of global | |
4995 | * reservations to be released may be adjusted. | |
4996 | */ | |
4997 | gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); | |
4998 | hugetlb_acct_memory(h, -gbl_reserve); | |
b5cec28d MK |
4999 | |
5000 | return 0; | |
a43a8c39 | 5001 | } |
93f70f90 | 5002 | |
3212b535 SC |
5003 | #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE |
5004 | static unsigned long page_table_shareable(struct vm_area_struct *svma, | |
5005 | struct vm_area_struct *vma, | |
5006 | unsigned long addr, pgoff_t idx) | |
5007 | { | |
5008 | unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + | |
5009 | svma->vm_start; | |
5010 | unsigned long sbase = saddr & PUD_MASK; | |
5011 | unsigned long s_end = sbase + PUD_SIZE; | |
5012 | ||
5013 | /* Allow segments to share if only one is marked locked */ | |
de60f5f1 EM |
5014 | unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK; |
5015 | unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK; | |
3212b535 SC |
5016 | |
5017 | /* | |
5018 | * match the virtual addresses, permission and the alignment of the | |
5019 | * page table page. | |
5020 | */ | |
5021 | if (pmd_index(addr) != pmd_index(saddr) || | |
5022 | vm_flags != svm_flags || | |
5023 | sbase < svma->vm_start || svma->vm_end < s_end) | |
5024 | return 0; | |
5025 | ||
5026 | return saddr; | |
5027 | } | |
5028 | ||
31aafb45 | 5029 | static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr) |
3212b535 SC |
5030 | { |
5031 | unsigned long base = addr & PUD_MASK; | |
5032 | unsigned long end = base + PUD_SIZE; | |
5033 | ||
5034 | /* | |
5035 | * check on proper vm_flags and page table alignment | |
5036 | */ | |
017b1660 | 5037 | if (vma->vm_flags & VM_MAYSHARE && range_in_vma(vma, base, end)) |
31aafb45 NK |
5038 | return true; |
5039 | return false; | |
3212b535 SC |
5040 | } |
5041 | ||
017b1660 MK |
5042 | /* |
5043 | * Determine if start,end range within vma could be mapped by shared pmd. | |
5044 | * If yes, adjust start and end to cover range associated with possible | |
5045 | * shared pmd mappings. | |
5046 | */ | |
5047 | void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, | |
5048 | unsigned long *start, unsigned long *end) | |
5049 | { | |
5050 | unsigned long check_addr = *start; | |
5051 | ||
5052 | if (!(vma->vm_flags & VM_MAYSHARE)) | |
5053 | return; | |
5054 | ||
5055 | for (check_addr = *start; check_addr < *end; check_addr += PUD_SIZE) { | |
5056 | unsigned long a_start = check_addr & PUD_MASK; | |
5057 | unsigned long a_end = a_start + PUD_SIZE; | |
5058 | ||
5059 | /* | |
5060 | * If sharing is possible, adjust start/end if necessary. | |
5061 | */ | |
5062 | if (range_in_vma(vma, a_start, a_end)) { | |
5063 | if (a_start < *start) | |
5064 | *start = a_start; | |
5065 | if (a_end > *end) | |
5066 | *end = a_end; | |
5067 | } | |
5068 | } | |
5069 | } | |
5070 | ||
3212b535 SC |
5071 | /* |
5072 | * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() | |
5073 | * and returns the corresponding pte. While this is not necessary for the | |
5074 | * !shared pmd case because we can allocate the pmd later as well, it makes the | |
c0d0381a MK |
5075 | * code much cleaner. |
5076 | * | |
5077 | * This routine must be called with i_mmap_rwsem held in at least read mode. | |
5078 | * For hugetlbfs, this prevents removal of any page table entries associated | |
5079 | * with the address space. This is important as we are setting up sharing | |
5080 | * based on existing page table entries (mappings). | |
3212b535 SC |
5081 | */ |
5082 | pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) | |
5083 | { | |
5084 | struct vm_area_struct *vma = find_vma(mm, addr); | |
5085 | struct address_space *mapping = vma->vm_file->f_mapping; | |
5086 | pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + | |
5087 | vma->vm_pgoff; | |
5088 | struct vm_area_struct *svma; | |
5089 | unsigned long saddr; | |
5090 | pte_t *spte = NULL; | |
5091 | pte_t *pte; | |
cb900f41 | 5092 | spinlock_t *ptl; |
3212b535 SC |
5093 | |
5094 | if (!vma_shareable(vma, addr)) | |
5095 | return (pte_t *)pmd_alloc(mm, pud, addr); | |
5096 | ||
3212b535 SC |
5097 | vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { |
5098 | if (svma == vma) | |
5099 | continue; | |
5100 | ||
5101 | saddr = page_table_shareable(svma, vma, addr, idx); | |
5102 | if (saddr) { | |
7868a208 PA |
5103 | spte = huge_pte_offset(svma->vm_mm, saddr, |
5104 | vma_mmu_pagesize(svma)); | |
3212b535 SC |
5105 | if (spte) { |
5106 | get_page(virt_to_page(spte)); | |
5107 | break; | |
5108 | } | |
5109 | } | |
5110 | } | |
5111 | ||
5112 | if (!spte) | |
5113 | goto out; | |
5114 | ||
8bea8052 | 5115 | ptl = huge_pte_lock(hstate_vma(vma), mm, spte); |
dc6c9a35 | 5116 | if (pud_none(*pud)) { |
3212b535 SC |
5117 | pud_populate(mm, pud, |
5118 | (pmd_t *)((unsigned long)spte & PAGE_MASK)); | |
c17b1f42 | 5119 | mm_inc_nr_pmds(mm); |
dc6c9a35 | 5120 | } else { |
3212b535 | 5121 | put_page(virt_to_page(spte)); |
dc6c9a35 | 5122 | } |
cb900f41 | 5123 | spin_unlock(ptl); |
3212b535 SC |
5124 | out: |
5125 | pte = (pte_t *)pmd_alloc(mm, pud, addr); | |
3212b535 SC |
5126 | return pte; |
5127 | } | |
5128 | ||
5129 | /* | |
5130 | * unmap huge page backed by shared pte. | |
5131 | * | |
5132 | * Hugetlb pte page is ref counted at the time of mapping. If pte is shared | |
5133 | * indicated by page_count > 1, unmap is achieved by clearing pud and | |
5134 | * decrementing the ref count. If count == 1, the pte page is not shared. | |
5135 | * | |
c0d0381a | 5136 | * Called with page table lock held and i_mmap_rwsem held in write mode. |
3212b535 SC |
5137 | * |
5138 | * returns: 1 successfully unmapped a shared pte page | |
5139 | * 0 the underlying pte page is not shared, or it is the last user | |
5140 | */ | |
5141 | int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) | |
5142 | { | |
5143 | pgd_t *pgd = pgd_offset(mm, *addr); | |
c2febafc KS |
5144 | p4d_t *p4d = p4d_offset(pgd, *addr); |
5145 | pud_t *pud = pud_offset(p4d, *addr); | |
3212b535 SC |
5146 | |
5147 | BUG_ON(page_count(virt_to_page(ptep)) == 0); | |
5148 | if (page_count(virt_to_page(ptep)) == 1) | |
5149 | return 0; | |
5150 | ||
5151 | pud_clear(pud); | |
5152 | put_page(virt_to_page(ptep)); | |
dc6c9a35 | 5153 | mm_dec_nr_pmds(mm); |
3212b535 SC |
5154 | *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE; |
5155 | return 1; | |
5156 | } | |
9e5fc74c SC |
5157 | #define want_pmd_share() (1) |
5158 | #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ | |
5159 | pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) | |
5160 | { | |
5161 | return NULL; | |
5162 | } | |
e81f2d22 ZZ |
5163 | |
5164 | int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) | |
5165 | { | |
5166 | return 0; | |
5167 | } | |
017b1660 MK |
5168 | |
5169 | void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, | |
5170 | unsigned long *start, unsigned long *end) | |
5171 | { | |
5172 | } | |
9e5fc74c | 5173 | #define want_pmd_share() (0) |
3212b535 SC |
5174 | #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ |
5175 | ||
9e5fc74c SC |
5176 | #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB |
5177 | pte_t *huge_pte_alloc(struct mm_struct *mm, | |
5178 | unsigned long addr, unsigned long sz) | |
5179 | { | |
5180 | pgd_t *pgd; | |
c2febafc | 5181 | p4d_t *p4d; |
9e5fc74c SC |
5182 | pud_t *pud; |
5183 | pte_t *pte = NULL; | |
5184 | ||
5185 | pgd = pgd_offset(mm, addr); | |
f4f0a3d8 KS |
5186 | p4d = p4d_alloc(mm, pgd, addr); |
5187 | if (!p4d) | |
5188 | return NULL; | |
c2febafc | 5189 | pud = pud_alloc(mm, p4d, addr); |
9e5fc74c SC |
5190 | if (pud) { |
5191 | if (sz == PUD_SIZE) { | |
5192 | pte = (pte_t *)pud; | |
5193 | } else { | |
5194 | BUG_ON(sz != PMD_SIZE); | |
5195 | if (want_pmd_share() && pud_none(*pud)) | |
5196 | pte = huge_pmd_share(mm, addr, pud); | |
5197 | else | |
5198 | pte = (pte_t *)pmd_alloc(mm, pud, addr); | |
5199 | } | |
5200 | } | |
4e666314 | 5201 | BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte)); |
9e5fc74c SC |
5202 | |
5203 | return pte; | |
5204 | } | |
5205 | ||
9b19df29 PA |
5206 | /* |
5207 | * huge_pte_offset() - Walk the page table to resolve the hugepage | |
5208 | * entry at address @addr | |
5209 | * | |
5210 | * Return: Pointer to page table or swap entry (PUD or PMD) for | |
5211 | * address @addr, or NULL if a p*d_none() entry is encountered and the | |
5212 | * size @sz doesn't match the hugepage size at this level of the page | |
5213 | * table. | |
5214 | */ | |
7868a208 PA |
5215 | pte_t *huge_pte_offset(struct mm_struct *mm, |
5216 | unsigned long addr, unsigned long sz) | |
9e5fc74c SC |
5217 | { |
5218 | pgd_t *pgd; | |
c2febafc | 5219 | p4d_t *p4d; |
9e5fc74c | 5220 | pud_t *pud; |
c2febafc | 5221 | pmd_t *pmd; |
9e5fc74c SC |
5222 | |
5223 | pgd = pgd_offset(mm, addr); | |
c2febafc KS |
5224 | if (!pgd_present(*pgd)) |
5225 | return NULL; | |
5226 | p4d = p4d_offset(pgd, addr); | |
5227 | if (!p4d_present(*p4d)) | |
5228 | return NULL; | |
9b19df29 | 5229 | |
c2febafc | 5230 | pud = pud_offset(p4d, addr); |
9b19df29 | 5231 | if (sz != PUD_SIZE && pud_none(*pud)) |
c2febafc | 5232 | return NULL; |
9b19df29 PA |
5233 | /* hugepage or swap? */ |
5234 | if (pud_huge(*pud) || !pud_present(*pud)) | |
c2febafc | 5235 | return (pte_t *)pud; |
9b19df29 | 5236 | |
c2febafc | 5237 | pmd = pmd_offset(pud, addr); |
9b19df29 PA |
5238 | if (sz != PMD_SIZE && pmd_none(*pmd)) |
5239 | return NULL; | |
5240 | /* hugepage or swap? */ | |
5241 | if (pmd_huge(*pmd) || !pmd_present(*pmd)) | |
5242 | return (pte_t *)pmd; | |
5243 | ||
5244 | return NULL; | |
9e5fc74c SC |
5245 | } |
5246 | ||
61f77eda NH |
5247 | #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ |
5248 | ||
5249 | /* | |
5250 | * These functions are overwritable if your architecture needs its own | |
5251 | * behavior. | |
5252 | */ | |
5253 | struct page * __weak | |
5254 | follow_huge_addr(struct mm_struct *mm, unsigned long address, | |
5255 | int write) | |
5256 | { | |
5257 | return ERR_PTR(-EINVAL); | |
5258 | } | |
5259 | ||
4dc71451 AK |
5260 | struct page * __weak |
5261 | follow_huge_pd(struct vm_area_struct *vma, | |
5262 | unsigned long address, hugepd_t hpd, int flags, int pdshift) | |
5263 | { | |
5264 | WARN(1, "hugepd follow called with no support for hugepage directory format\n"); | |
5265 | return NULL; | |
5266 | } | |
5267 | ||
61f77eda | 5268 | struct page * __weak |
9e5fc74c | 5269 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
e66f17ff | 5270 | pmd_t *pmd, int flags) |
9e5fc74c | 5271 | { |
e66f17ff NH |
5272 | struct page *page = NULL; |
5273 | spinlock_t *ptl; | |
c9d398fa | 5274 | pte_t pte; |
3faa52c0 JH |
5275 | |
5276 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ | |
5277 | if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) == | |
5278 | (FOLL_PIN | FOLL_GET))) | |
5279 | return NULL; | |
5280 | ||
e66f17ff NH |
5281 | retry: |
5282 | ptl = pmd_lockptr(mm, pmd); | |
5283 | spin_lock(ptl); | |
5284 | /* | |
5285 | * make sure that the address range covered by this pmd is not | |
5286 | * unmapped from other threads. | |
5287 | */ | |
5288 | if (!pmd_huge(*pmd)) | |
5289 | goto out; | |
c9d398fa NH |
5290 | pte = huge_ptep_get((pte_t *)pmd); |
5291 | if (pte_present(pte)) { | |
97534127 | 5292 | page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT); |
3faa52c0 JH |
5293 | /* |
5294 | * try_grab_page() should always succeed here, because: a) we | |
5295 | * hold the pmd (ptl) lock, and b) we've just checked that the | |
5296 | * huge pmd (head) page is present in the page tables. The ptl | |
5297 | * prevents the head page and tail pages from being rearranged | |
5298 | * in any way. So this page must be available at this point, | |
5299 | * unless the page refcount overflowed: | |
5300 | */ | |
5301 | if (WARN_ON_ONCE(!try_grab_page(page, flags))) { | |
5302 | page = NULL; | |
5303 | goto out; | |
5304 | } | |
e66f17ff | 5305 | } else { |
c9d398fa | 5306 | if (is_hugetlb_entry_migration(pte)) { |
e66f17ff NH |
5307 | spin_unlock(ptl); |
5308 | __migration_entry_wait(mm, (pte_t *)pmd, ptl); | |
5309 | goto retry; | |
5310 | } | |
5311 | /* | |
5312 | * hwpoisoned entry is treated as no_page_table in | |
5313 | * follow_page_mask(). | |
5314 | */ | |
5315 | } | |
5316 | out: | |
5317 | spin_unlock(ptl); | |
9e5fc74c SC |
5318 | return page; |
5319 | } | |
5320 | ||
61f77eda | 5321 | struct page * __weak |
9e5fc74c | 5322 | follow_huge_pud(struct mm_struct *mm, unsigned long address, |
e66f17ff | 5323 | pud_t *pud, int flags) |
9e5fc74c | 5324 | { |
3faa52c0 | 5325 | if (flags & (FOLL_GET | FOLL_PIN)) |
e66f17ff | 5326 | return NULL; |
9e5fc74c | 5327 | |
e66f17ff | 5328 | return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT); |
9e5fc74c SC |
5329 | } |
5330 | ||
faaa5b62 AK |
5331 | struct page * __weak |
5332 | follow_huge_pgd(struct mm_struct *mm, unsigned long address, pgd_t *pgd, int flags) | |
5333 | { | |
3faa52c0 | 5334 | if (flags & (FOLL_GET | FOLL_PIN)) |
faaa5b62 AK |
5335 | return NULL; |
5336 | ||
5337 | return pte_page(*(pte_t *)pgd) + ((address & ~PGDIR_MASK) >> PAGE_SHIFT); | |
5338 | } | |
5339 | ||
31caf665 NH |
5340 | bool isolate_huge_page(struct page *page, struct list_head *list) |
5341 | { | |
bcc54222 NH |
5342 | bool ret = true; |
5343 | ||
309381fe | 5344 | VM_BUG_ON_PAGE(!PageHead(page), page); |
31caf665 | 5345 | spin_lock(&hugetlb_lock); |
bcc54222 NH |
5346 | if (!page_huge_active(page) || !get_page_unless_zero(page)) { |
5347 | ret = false; | |
5348 | goto unlock; | |
5349 | } | |
5350 | clear_page_huge_active(page); | |
31caf665 | 5351 | list_move_tail(&page->lru, list); |
bcc54222 | 5352 | unlock: |
31caf665 | 5353 | spin_unlock(&hugetlb_lock); |
bcc54222 | 5354 | return ret; |
31caf665 NH |
5355 | } |
5356 | ||
5357 | void putback_active_hugepage(struct page *page) | |
5358 | { | |
309381fe | 5359 | VM_BUG_ON_PAGE(!PageHead(page), page); |
31caf665 | 5360 | spin_lock(&hugetlb_lock); |
bcc54222 | 5361 | set_page_huge_active(page); |
31caf665 NH |
5362 | list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist); |
5363 | spin_unlock(&hugetlb_lock); | |
5364 | put_page(page); | |
5365 | } | |
ab5ac90a MH |
5366 | |
5367 | void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason) | |
5368 | { | |
5369 | struct hstate *h = page_hstate(oldpage); | |
5370 | ||
5371 | hugetlb_cgroup_migrate(oldpage, newpage); | |
5372 | set_page_owner_migrate_reason(newpage, reason); | |
5373 | ||
5374 | /* | |
5375 | * transfer temporary state of the new huge page. This is | |
5376 | * reverse to other transitions because the newpage is going to | |
5377 | * be final while the old one will be freed so it takes over | |
5378 | * the temporary status. | |
5379 | * | |
5380 | * Also note that we have to transfer the per-node surplus state | |
5381 | * here as well otherwise the global surplus count will not match | |
5382 | * the per-node's. | |
5383 | */ | |
5384 | if (PageHugeTemporary(newpage)) { | |
5385 | int old_nid = page_to_nid(oldpage); | |
5386 | int new_nid = page_to_nid(newpage); | |
5387 | ||
5388 | SetPageHugeTemporary(oldpage); | |
5389 | ClearPageHugeTemporary(newpage); | |
5390 | ||
5391 | spin_lock(&hugetlb_lock); | |
5392 | if (h->surplus_huge_pages_node[old_nid]) { | |
5393 | h->surplus_huge_pages_node[old_nid]--; | |
5394 | h->surplus_huge_pages_node[new_nid]++; | |
5395 | } | |
5396 | spin_unlock(&hugetlb_lock); | |
5397 | } | |
5398 | } |