blob: ffc52d985751efdf6d3927ee2006dabd5a2143c9 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Generic hugetlb support.
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +01004 * (C) Nadia Yvette Chambers, April 2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/list.h>
7#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
Alexey Dobriyane1759c22008-10-15 23:50:22 +04009#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/sysctl.h>
11#include <linux/highmem.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070012#include <linux/mmu_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/nodemask.h>
David Gibson63551ae2005-06-21 17:14:44 -070014#include <linux/pagemap.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080015#include <linux/mempolicy.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070016#include <linux/compiler.h>
Christoph Lameteraea47ff2006-01-08 01:00:57 -080017#include <linux/cpuset.h>
David Gibson3935baa2006-03-22 00:08:53 -080018#include <linux/mutex.h>
Mike Rapoport97ad1082018-10-30 15:09:44 -070019#include <linux/memblock.h>
Nishanth Aravamudana3437872008-07-23 21:27:44 -070020#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Mike Kravetz63489f82018-03-22 16:17:13 -070022#include <linux/mmdebug.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +090024#include <linux/rmap.h>
Matthew Wilcoxc6247f72017-07-10 15:48:56 -070025#include <linux/string_helpers.h>
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +090026#include <linux/swap.h>
27#include <linux/swapops.h>
Davidlohr Bueso8382d912014-04-03 14:47:31 -070028#include <linux/jhash.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -080029#include <linux/numa.h>
Waiman Longc77c0a82020-01-04 13:00:15 -080030#include <linux/llist.h>
Linus Torvaldsd6606682008-08-06 12:04:54 -070031
David Gibson63551ae2005-06-21 17:14:44 -070032#include <asm/page.h>
33#include <asm/pgtable.h>
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070034#include <asm/tlb.h>
David Gibson63551ae2005-06-21 17:14:44 -070035
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070036#include <linux/io.h>
David Gibson63551ae2005-06-21 17:14:44 -070037#include <linux/hugetlb.h>
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -070038#include <linux/hugetlb_cgroup.h>
Lee Schermerhorn9a3052302009-12-14 17:58:25 -080039#include <linux/node.h>
Mike Kravetz1a1aad82017-02-22 15:43:01 -080040#include <linux/userfaultfd_k.h>
Michal Hockoab5ac902018-01-31 16:20:48 -080041#include <linux/page_owner.h>
Nick Piggin7835e982006-03-22 00:08:40 -080042#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070044int hugetlb_max_hstate __read_mostly;
Andi Kleene5ff2152008-07-23 21:27:42 -070045unsigned int default_hstate_idx;
46struct hstate hstates[HUGE_MAX_HSTATE];
Naoya Horiguchi641844f2015-06-24 16:56:59 -070047/*
48 * Minimum page order among possible hugepage sizes, set to a proper value
49 * at boot time.
50 */
51static unsigned int minimum_order __read_mostly = UINT_MAX;
Andi Kleene5ff2152008-07-23 21:27:42 -070052
Jon Tollefson53ba51d2008-07-23 21:27:52 -070053__initdata LIST_HEAD(huge_boot_pages);
54
Andi Kleene5ff2152008-07-23 21:27:42 -070055/* for command line parsing */
56static struct hstate * __initdata parsed_hstate;
57static unsigned long __initdata default_hstate_max_huge_pages;
Nick Piggine11bfbf2008-07-23 21:27:52 -070058static unsigned long __initdata default_hstate_size;
Vaishali Thakkar9fee0212016-05-19 17:11:04 -070059static bool __initdata parsed_valid_hugepagesz = true;
Andi Kleene5ff2152008-07-23 21:27:42 -070060
David Gibson3935baa2006-03-22 00:08:53 -080061/*
Naoya Horiguchi31caf662013-09-11 14:21:59 -070062 * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
63 * free_huge_pages, and surplus_huge_pages.
David Gibson3935baa2006-03-22 00:08:53 -080064 */
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070065DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080066
Davidlohr Bueso8382d912014-04-03 14:47:31 -070067/*
68 * Serializes faults on the same logical page. This is used to
69 * prevent spurious OOMs when the hugepage pool is fully utilized.
70 */
71static int num_fault_mutexes;
Mike Kravetzc672c7f2015-09-08 15:01:35 -070072struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp;
Davidlohr Bueso8382d912014-04-03 14:47:31 -070073
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070074/* Forward declaration */
75static int hugetlb_acct_memory(struct hstate *h, long delta);
76
David Gibson90481622012-03-21 16:34:12 -070077static 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
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070084 * 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);
David Gibson90481622012-03-21 16:34:12 -070090 kfree(spool);
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070091 }
David Gibson90481622012-03-21 16:34:12 -070092}
93
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070094struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
95 long min_hpages)
David Gibson90481622012-03-21 16:34:12 -070096{
97 struct hugepage_subpool *spool;
98
Mike Kravetzc6a91822015-04-15 16:13:36 -070099 spool = kzalloc(sizeof(*spool), GFP_KERNEL);
David Gibson90481622012-03-21 16:34:12 -0700100 if (!spool)
101 return NULL;
102
103 spin_lock_init(&spool->lock);
104 spool->count = 1;
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -0700105 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;
David Gibson90481622012-03-21 16:34:12 -0700114
115 return spool;
116}
117
118void 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
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700126/*
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 */
134static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700135 long delta)
136{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700137 long ret = delta;
David Gibson90481622012-03-21 16:34:12 -0700138
139 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700140 return ret;
David Gibson90481622012-03-21 16:34:12 -0700141
142 spin_lock(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700143
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700144 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 }
151 }
152
Mike Kravetz09a95e22016-05-19 17:11:01 -0700153 /* minimum size accounting */
154 if (spool->min_hpages != -1 && spool->rsv_hpages) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700155 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
168unlock_ret:
169 spin_unlock(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700170 return ret;
171}
172
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700173/*
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 */
179static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700180 long delta)
181{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700182 long ret = delta;
183
David Gibson90481622012-03-21 16:34:12 -0700184 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700185 return delta;
David Gibson90481622012-03-21 16:34:12 -0700186
187 spin_lock(&spool->lock);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700188
189 if (spool->max_hpages != -1) /* maximum size accounting */
190 spool->used_hpages -= delta;
191
Mike Kravetz09a95e22016-05-19 17:11:01 -0700192 /* minimum size accounting */
193 if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700194 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 */
David Gibson90481622012-03-21 16:34:12 -0700208 unlock_or_release_subpool(spool);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700209
210 return ret;
David Gibson90481622012-03-21 16:34:12 -0700211}
212
213static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
214{
215 return HUGETLBFS_SB(inode->i_sb)->spool;
216}
217
218static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
219{
Al Viro496ad9a2013-01-23 17:07:38 -0500220 return subpool_inode(file_inode(vma->vm_file));
David Gibson90481622012-03-21 16:34:12 -0700221}
222
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700223/*
Andy Whitcroft96822902008-07-23 21:27:29 -0700224 * Region tracking -- allows tracking of reservations and instantiated pages
225 * across the pages in a mapping.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700226 *
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700227 * 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.
Andy Whitcroft96822902008-07-23 21:27:29 -0700241 */
242struct file_region {
243 struct list_head link;
244 long from;
245 long to;
246};
247
Mina Almasryd75c6af2019-11-30 17:56:59 -0800248/* Must be called with resv->lock held. Calling this with count_only == true
249 * will count the number of pages to be added but will not modify the linked
250 * list.
251 */
252static long add_reservation_in_range(struct resv_map *resv, long f, long t,
253 bool count_only)
254{
255 long chg = 0;
256 struct list_head *head = &resv->regions;
257 struct file_region *rg = NULL, *trg = NULL, *nrg = NULL;
258
259 /* Locate the region we are before or in. */
260 list_for_each_entry(rg, head, link)
261 if (f <= rg->to)
262 break;
263
264 /* Round our left edge to the current segment if it encloses us. */
265 if (f > rg->from)
266 f = rg->from;
267
268 chg = t - f;
269
270 /* Check for and consume any regions we now overlap with. */
271 nrg = rg;
272 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
273 if (&rg->link == head)
274 break;
275 if (rg->from > t)
276 break;
277
278 /* We overlap with this area, if it extends further than
279 * us then we must extend ourselves. Account for its
280 * existing reservation.
281 */
282 if (rg->to > t) {
283 chg += rg->to - t;
284 t = rg->to;
285 }
286 chg -= rg->to - rg->from;
287
288 if (!count_only && rg != nrg) {
289 list_del(&rg->link);
290 kfree(rg);
291 }
292 }
293
294 if (!count_only) {
295 nrg->from = f;
296 nrg->to = t;
297 }
298
299 return chg;
300}
301
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700302/*
303 * Add the huge page range represented by [f, t) to the reserve
Mina Almasry5c911952019-11-30 17:56:54 -0800304 * map. Existing regions will be expanded to accommodate the specified
305 * range, or a region will be taken from the cache. Sufficient regions
306 * must exist in the cache due to the previous call to region_chg with
307 * the same range.
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700308 *
309 * Return the number of new huge pages added to the map. This
310 * number is greater than or equal to zero.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700311 */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700312static long region_add(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700313{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700314 struct list_head *head = &resv->regions;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800315 struct file_region *rg, *nrg;
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700316 long add = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700317
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700318 spin_lock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700319 /* Locate the region we are either in or before. */
320 list_for_each_entry(rg, head, link)
321 if (f <= rg->to)
322 break;
323
Mike Kravetz5e911372015-09-08 15:01:28 -0700324 /*
325 * If no region exists which can be expanded to include the
Mina Almasry5c911952019-11-30 17:56:54 -0800326 * specified range, pull a region descriptor from the cache
327 * and use it for this range.
Mike Kravetz5e911372015-09-08 15:01:28 -0700328 */
329 if (&rg->link == head || t < rg->from) {
330 VM_BUG_ON(resv->region_cache_count <= 0);
331
332 resv->region_cache_count--;
333 nrg = list_first_entry(&resv->region_cache, struct file_region,
334 link);
335 list_del(&nrg->link);
336
337 nrg->from = f;
338 nrg->to = t;
339 list_add(&nrg->link, rg->link.prev);
340
341 add += t - f;
342 goto out_locked;
343 }
344
Mina Almasryd75c6af2019-11-30 17:56:59 -0800345 add = add_reservation_in_range(resv, f, t, false);
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700346
Mike Kravetz5e911372015-09-08 15:01:28 -0700347out_locked:
348 resv->adds_in_progress--;
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700349 spin_unlock(&resv->lock);
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700350 VM_BUG_ON(add < 0);
351 return add;
Andy Whitcroft96822902008-07-23 21:27:29 -0700352}
353
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700354/*
355 * Examine the existing reserve map and determine how many
356 * huge pages in the specified range [f, t) are NOT currently
357 * represented. This routine is called before a subsequent
358 * call to region_add that will actually modify the reserve
359 * map to add the specified range [f, t). region_chg does
360 * not change the number of huge pages represented by the
Mina Almasry5c911952019-11-30 17:56:54 -0800361 * map. A new file_region structure is added to the cache
362 * as a placeholder, so that the subsequent region_add
363 * call will have all the regions it needs and will not fail.
Mike Kravetz5e911372015-09-08 15:01:28 -0700364 *
365 * Returns the number of huge pages that need to be added to the existing
366 * reservation map for the range [f, t). This number is greater or equal to
367 * zero. -ENOMEM is returned if a new file_region structure or cache entry
368 * is needed and can not be allocated.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700369 */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700370static long region_chg(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700371{
Andy Whitcroft96822902008-07-23 21:27:29 -0700372 long chg = 0;
373
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700374 spin_lock(&resv->lock);
Mike Kravetz5e911372015-09-08 15:01:28 -0700375retry_locked:
376 resv->adds_in_progress++;
377
378 /*
379 * Check for sufficient descriptors in the cache to accommodate
380 * the number of in progress add operations.
381 */
382 if (resv->adds_in_progress > resv->region_cache_count) {
383 struct file_region *trg;
384
385 VM_BUG_ON(resv->adds_in_progress - resv->region_cache_count > 1);
386 /* Must drop lock to allocate a new descriptor. */
387 resv->adds_in_progress--;
388 spin_unlock(&resv->lock);
389
390 trg = kmalloc(sizeof(*trg), GFP_KERNEL);
Mina Almasry5c911952019-11-30 17:56:54 -0800391 if (!trg)
Mike Kravetz5e911372015-09-08 15:01:28 -0700392 return -ENOMEM;
393
394 spin_lock(&resv->lock);
395 list_add(&trg->link, &resv->region_cache);
396 resv->region_cache_count++;
397 goto retry_locked;
398 }
399
Mina Almasryd75c6af2019-11-30 17:56:59 -0800400 chg = add_reservation_in_range(resv, f, t, true);
Andy Whitcroft96822902008-07-23 21:27:29 -0700401
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700402 spin_unlock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700403 return chg;
404}
405
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700406/*
Mike Kravetz5e911372015-09-08 15:01:28 -0700407 * Abort the in progress add operation. The adds_in_progress field
408 * of the resv_map keeps track of the operations in progress between
409 * calls to region_chg and region_add. Operations are sometimes
410 * aborted after the call to region_chg. In such cases, region_abort
411 * is called to decrement the adds_in_progress counter.
412 *
413 * NOTE: The range arguments [f, t) are not needed or used in this
414 * routine. They are kept to make reading the calling code easier as
415 * arguments will match the associated region_chg call.
416 */
417static void region_abort(struct resv_map *resv, long f, long t)
418{
419 spin_lock(&resv->lock);
420 VM_BUG_ON(!resv->region_cache_count);
421 resv->adds_in_progress--;
422 spin_unlock(&resv->lock);
423}
424
425/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700426 * Delete the specified range [f, t) from the reserve map. If the
427 * t parameter is LONG_MAX, this indicates that ALL regions after f
428 * should be deleted. Locate the regions which intersect [f, t)
429 * and either trim, delete or split the existing regions.
430 *
431 * Returns the number of huge pages deleted from the reserve map.
432 * In the normal case, the return value is zero or more. In the
433 * case where a region must be split, a new region descriptor must
434 * be allocated. If the allocation fails, -ENOMEM will be returned.
435 * NOTE: If the parameter t == LONG_MAX, then we will never split
436 * a region and possibly return -ENOMEM. Callers specifying
437 * t == LONG_MAX do not need to check for -ENOMEM error.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700438 */
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700439static long region_del(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700440{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700441 struct list_head *head = &resv->regions;
Andy Whitcroft96822902008-07-23 21:27:29 -0700442 struct file_region *rg, *trg;
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700443 struct file_region *nrg = NULL;
444 long del = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700445
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700446retry:
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700447 spin_lock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700448 list_for_each_entry_safe(rg, trg, head, link) {
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800449 /*
450 * Skip regions before the range to be deleted. file_region
451 * ranges are normally of the form [from, to). However, there
452 * may be a "placeholder" entry in the map which is of the form
453 * (from, to) with from == to. Check for placeholder entries
454 * at the beginning of the range to be deleted.
455 */
456 if (rg->to <= f && (rg->to != rg->from || rg->to != f))
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700457 continue;
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800458
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700459 if (rg->from >= t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700460 break;
Andy Whitcroft96822902008-07-23 21:27:29 -0700461
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700462 if (f > rg->from && t < rg->to) { /* Must split region */
463 /*
464 * Check for an entry in the cache before dropping
465 * lock and attempting allocation.
466 */
467 if (!nrg &&
468 resv->region_cache_count > resv->adds_in_progress) {
469 nrg = list_first_entry(&resv->region_cache,
470 struct file_region,
471 link);
472 list_del(&nrg->link);
473 resv->region_cache_count--;
474 }
475
476 if (!nrg) {
477 spin_unlock(&resv->lock);
478 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
479 if (!nrg)
480 return -ENOMEM;
481 goto retry;
482 }
483
484 del += t - f;
485
486 /* New entry for end of split region */
487 nrg->from = t;
488 nrg->to = rg->to;
489 INIT_LIST_HEAD(&nrg->link);
490
491 /* Original entry is trimmed */
492 rg->to = f;
493
494 list_add(&nrg->link, &rg->link);
495 nrg = NULL;
496 break;
497 }
498
499 if (f <= rg->from && t >= rg->to) { /* Remove entire region */
500 del += rg->to - rg->from;
501 list_del(&rg->link);
502 kfree(rg);
503 continue;
504 }
505
506 if (f <= rg->from) { /* Trim beginning of region */
507 del += t - rg->from;
508 rg->from = t;
509 } else { /* Trim end of region */
510 del += rg->to - f;
511 rg->to = f;
512 }
Andy Whitcroft96822902008-07-23 21:27:29 -0700513 }
514
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700515 spin_unlock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700516 kfree(nrg);
517 return del;
Andy Whitcroft96822902008-07-23 21:27:29 -0700518}
519
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700520/*
Mike Kravetzb5cec282015-09-08 15:01:41 -0700521 * A rare out of memory error was encountered which prevented removal of
522 * the reserve map region for a page. The huge page itself was free'ed
523 * and removed from the page cache. This routine will adjust the subpool
524 * usage count, and the global reserve count if needed. By incrementing
525 * these counts, the reserve map entry which could not be deleted will
526 * appear as a "reserved" entry instead of simply dangling with incorrect
527 * counts.
528 */
zhong jiang72e29362016-10-07 17:02:01 -0700529void hugetlb_fix_reserve_counts(struct inode *inode)
Mike Kravetzb5cec282015-09-08 15:01:41 -0700530{
531 struct hugepage_subpool *spool = subpool_inode(inode);
532 long rsv_adjust;
533
534 rsv_adjust = hugepage_subpool_get_pages(spool, 1);
zhong jiang72e29362016-10-07 17:02:01 -0700535 if (rsv_adjust) {
Mike Kravetzb5cec282015-09-08 15:01:41 -0700536 struct hstate *h = hstate_inode(inode);
537
538 hugetlb_acct_memory(h, 1);
539 }
540}
541
542/*
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700543 * Count and return the number of huge pages in the reserve map
544 * that intersect with the range [f, t).
545 */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700546static long region_count(struct resv_map *resv, long f, long t)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700547{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700548 struct list_head *head = &resv->regions;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700549 struct file_region *rg;
550 long chg = 0;
551
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700552 spin_lock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700553 /* Locate each segment we overlap with, and count that overlap. */
554 list_for_each_entry(rg, head, link) {
Wang Sheng-Huif2135a42012-05-29 15:06:17 -0700555 long seg_from;
556 long seg_to;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700557
558 if (rg->to <= f)
559 continue;
560 if (rg->from >= t)
561 break;
562
563 seg_from = max(rg->from, f);
564 seg_to = min(rg->to, t);
565
566 chg += seg_to - seg_from;
567 }
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700568 spin_unlock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700569
570 return chg;
571}
572
Andy Whitcroft96822902008-07-23 21:27:29 -0700573/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700574 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700575 * the mapping, in pagecache page units; huge pages here.
576 */
Andi Kleena5516432008-07-23 21:27:41 -0700577static pgoff_t vma_hugecache_offset(struct hstate *h,
578 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700579{
Andi Kleena5516432008-07-23 21:27:41 -0700580 return ((address - vma->vm_start) >> huge_page_shift(h)) +
581 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700582}
583
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900584pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
585 unsigned long address)
586{
587 return vma_hugecache_offset(hstate_vma(vma), vma, address);
588}
Dan Williamsdee41072016-05-14 12:20:44 -0700589EXPORT_SYMBOL_GPL(linear_hugepage_index);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900590
Andy Whitcroft84afd992008-07-23 21:27:32 -0700591/*
Mel Gorman08fba692009-01-06 14:38:53 -0800592 * Return the size of the pages allocated when backing a VMA. In the majority
593 * cases this will be same size as used by the page table entries.
594 */
595unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
596{
Dan Williams05ea8862018-04-05 16:24:25 -0700597 if (vma->vm_ops && vma->vm_ops->pagesize)
598 return vma->vm_ops->pagesize(vma);
599 return PAGE_SIZE;
Mel Gorman08fba692009-01-06 14:38:53 -0800600}
Joerg Roedelf340ca02009-06-19 15:16:22 +0200601EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
Mel Gorman08fba692009-01-06 14:38:53 -0800602
603/*
Mel Gorman33402892009-01-06 14:38:54 -0800604 * Return the page size being used by the MMU to back a VMA. In the majority
605 * of cases, the page size used by the kernel matches the MMU size. On
Dan Williams09135cc2018-04-05 16:24:21 -0700606 * architectures where it differs, an architecture-specific 'strong'
607 * version of this symbol is required.
Mel Gorman33402892009-01-06 14:38:54 -0800608 */
Dan Williams09135cc2018-04-05 16:24:21 -0700609__weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
Mel Gorman33402892009-01-06 14:38:54 -0800610{
611 return vma_kernel_pagesize(vma);
612}
Mel Gorman33402892009-01-06 14:38:54 -0800613
614/*
Andy Whitcroft84afd992008-07-23 21:27:32 -0700615 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
616 * bits of the reservation map pointer, which are always clear due to
617 * alignment.
618 */
619#define HPAGE_RESV_OWNER (1UL << 0)
620#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700621#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700622
Mel Gormana1e78772008-07-23 21:27:23 -0700623/*
624 * These helpers are used to track how many pages are reserved for
625 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
626 * is guaranteed to have their future faults succeed.
627 *
628 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
629 * the reserve counters are updated with the hugetlb_lock held. It is safe
630 * to reset the VMA at fork() time as it is not in use yet and there is no
631 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700632 *
633 * The private mapping reservation is represented in a subtly different
634 * manner to a shared mapping. A shared mapping has a region map associated
635 * with the underlying file, this region map represents the backing file
636 * pages which have ever had a reservation assigned which this persists even
637 * after the page is instantiated. A private mapping has a region map
638 * associated with the original mmap which is attached to all VMAs which
639 * reference it, this region map represents those offsets which have consumed
640 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -0700641 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700642static unsigned long get_vma_private_data(struct vm_area_struct *vma)
643{
644 return (unsigned long)vma->vm_private_data;
645}
646
647static void set_vma_private_data(struct vm_area_struct *vma,
648 unsigned long value)
649{
650 vma->vm_private_data = (void *)value;
651}
652
Joonsoo Kim9119a412014-04-03 14:47:25 -0700653struct resv_map *resv_map_alloc(void)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700654{
655 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
Mike Kravetz5e911372015-09-08 15:01:28 -0700656 struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL);
657
658 if (!resv_map || !rg) {
659 kfree(resv_map);
660 kfree(rg);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700661 return NULL;
Mike Kravetz5e911372015-09-08 15:01:28 -0700662 }
Andy Whitcroft84afd992008-07-23 21:27:32 -0700663
664 kref_init(&resv_map->refs);
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700665 spin_lock_init(&resv_map->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700666 INIT_LIST_HEAD(&resv_map->regions);
667
Mike Kravetz5e911372015-09-08 15:01:28 -0700668 resv_map->adds_in_progress = 0;
669
670 INIT_LIST_HEAD(&resv_map->region_cache);
671 list_add(&rg->link, &resv_map->region_cache);
672 resv_map->region_cache_count = 1;
673
Andy Whitcroft84afd992008-07-23 21:27:32 -0700674 return resv_map;
675}
676
Joonsoo Kim9119a412014-04-03 14:47:25 -0700677void resv_map_release(struct kref *ref)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700678{
679 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
Mike Kravetz5e911372015-09-08 15:01:28 -0700680 struct list_head *head = &resv_map->region_cache;
681 struct file_region *rg, *trg;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700682
683 /* Clear out any active regions before we release the map. */
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700684 region_del(resv_map, 0, LONG_MAX);
Mike Kravetz5e911372015-09-08 15:01:28 -0700685
686 /* ... and any entries left in the cache */
687 list_for_each_entry_safe(rg, trg, head, link) {
688 list_del(&rg->link);
689 kfree(rg);
690 }
691
692 VM_BUG_ON(resv_map->adds_in_progress);
693
Andy Whitcroft84afd992008-07-23 21:27:32 -0700694 kfree(resv_map);
695}
696
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700697static inline struct resv_map *inode_resv_map(struct inode *inode)
698{
Mike Kravetzf27a5132019-05-13 17:22:55 -0700699 /*
700 * At inode evict time, i_mapping may not point to the original
701 * address space within the inode. This original address space
702 * contains the pointer to the resv_map. So, always use the
703 * address space embedded within the inode.
704 * The VERY common case is inode->mapping == &inode->i_data but,
705 * this may not be true for device special inodes.
706 */
707 return (struct resv_map *)(&inode->i_data)->private_data;
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700708}
709
Andy Whitcroft84afd992008-07-23 21:27:32 -0700710static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700711{
Sasha Levin81d1b092014-10-09 15:28:10 -0700712 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700713 if (vma->vm_flags & VM_MAYSHARE) {
714 struct address_space *mapping = vma->vm_file->f_mapping;
715 struct inode *inode = mapping->host;
716
717 return inode_resv_map(inode);
718
719 } else {
Andy Whitcroft84afd992008-07-23 21:27:32 -0700720 return (struct resv_map *)(get_vma_private_data(vma) &
721 ~HPAGE_RESV_MASK);
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700722 }
Mel Gormana1e78772008-07-23 21:27:23 -0700723}
724
Andy Whitcroft84afd992008-07-23 21:27:32 -0700725static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -0700726{
Sasha Levin81d1b092014-10-09 15:28:10 -0700727 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
728 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700729
Andy Whitcroft84afd992008-07-23 21:27:32 -0700730 set_vma_private_data(vma, (get_vma_private_data(vma) &
731 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700732}
733
734static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
735{
Sasha Levin81d1b092014-10-09 15:28:10 -0700736 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
737 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700738
739 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700740}
741
742static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
743{
Sasha Levin81d1b092014-10-09 15:28:10 -0700744 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700745
746 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700747}
748
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700749/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700750void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
751{
Sasha Levin81d1b092014-10-09 15:28:10 -0700752 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Mel Gormanf83a2752009-05-28 14:34:40 -0700753 if (!(vma->vm_flags & VM_MAYSHARE))
Mel Gormana1e78772008-07-23 21:27:23 -0700754 vma->vm_private_data = (void *)0;
755}
756
757/* Returns true if the VMA has associated reserve pages */
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700758static bool vma_has_reserves(struct vm_area_struct *vma, long chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700759{
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700760 if (vma->vm_flags & VM_NORESERVE) {
761 /*
762 * This address is already reserved by other process(chg == 0),
763 * so, we should decrement reserved count. Without decrementing,
764 * reserve count remains after releasing inode, because this
765 * allocated page will go into page cache and is regarded as
766 * coming from reserved pool in releasing step. Currently, we
767 * don't have any other solution to deal with this situation
768 * properly, so add work-around here.
769 */
770 if (vma->vm_flags & VM_MAYSHARE && chg == 0)
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700771 return true;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700772 else
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700773 return false;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700774 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700775
776 /* Shared mappings always use reserves */
Mike Kravetz1fb1b0e2015-09-08 15:01:44 -0700777 if (vma->vm_flags & VM_MAYSHARE) {
778 /*
779 * We know VM_NORESERVE is not set. Therefore, there SHOULD
780 * be a region map for all pages. The only situation where
781 * there is no region map is if a hole was punched via
782 * fallocate. In this case, there really are no reverves to
783 * use. This situation is indicated if chg != 0.
784 */
785 if (chg)
786 return false;
787 else
788 return true;
789 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700790
791 /*
792 * Only the process that called mmap() has reserves for
793 * private mappings.
794 */
Mike Kravetz67961f92016-06-08 15:33:42 -0700795 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
796 /*
797 * Like the shared case above, a hole punch or truncate
798 * could have been performed on the private mapping.
799 * Examine the value of chg to determine if reserves
800 * actually exist or were previously consumed.
801 * Very Subtle - The value of chg comes from a previous
802 * call to vma_needs_reserves(). The reserve map for
803 * private mappings has different (opposite) semantics
804 * than that of shared mappings. vma_needs_reserves()
805 * has already taken this difference in semantics into
806 * account. Therefore, the meaning of chg is the same
807 * as in the shared case above. Code could easily be
808 * combined, but keeping it separate draws attention to
809 * subtle differences.
810 */
811 if (chg)
812 return false;
813 else
814 return true;
815 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700816
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700817 return false;
Mel Gormana1e78772008-07-23 21:27:23 -0700818}
819
Andi Kleena5516432008-07-23 21:27:41 -0700820static void enqueue_huge_page(struct hstate *h, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 int nid = page_to_nid(page);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -0700823 list_move(&page->lru, &h->hugepage_freelists[nid]);
Andi Kleena5516432008-07-23 21:27:41 -0700824 h->free_huge_pages++;
825 h->free_huge_pages_node[nid]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700828static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900829{
830 struct page *page;
831
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700832 list_for_each_entry(page, &h->hugepage_freelists[nid], lru)
Naoya Horiguchi243abd52017-07-10 15:47:32 -0700833 if (!PageHWPoison(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700834 break;
835 /*
836 * if 'non-isolated free hugepage' not found on the list,
837 * the allocation fails.
838 */
839 if (&h->hugepage_freelists[nid] == &page->lru)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900840 return NULL;
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -0700841 list_move(&page->lru, &h->hugepage_activelist);
Naoya Horiguchia9869b82010-09-08 10:19:37 +0900842 set_page_refcounted(page);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900843 h->free_huge_pages--;
844 h->free_huge_pages_node[nid]--;
845 return page;
846}
847
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700848static struct page *dequeue_huge_page_nodemask(struct hstate *h, gfp_t gfp_mask, int nid,
849 nodemask_t *nmask)
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700850{
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700851 unsigned int cpuset_mems_cookie;
852 struct zonelist *zonelist;
853 struct zone *zone;
854 struct zoneref *z;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800855 int node = NUMA_NO_NODE;
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700856
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700857 zonelist = node_zonelist(nid, gfp_mask);
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700858
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700859retry_cpuset:
860 cpuset_mems_cookie = read_mems_allowed_begin();
861 for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) {
862 struct page *page;
863
864 if (!cpuset_zone_allowed(zone, gfp_mask))
865 continue;
866 /*
867 * no need to ask again on the same node. Pool is node rather than
868 * zone aware
869 */
870 if (zone_to_nid(zone) == node)
871 continue;
872 node = zone_to_nid(zone);
873
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700874 page = dequeue_huge_page_node_exact(h, node);
875 if (page)
876 return page;
877 }
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700878 if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie)))
879 goto retry_cpuset;
880
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700881 return NULL;
882}
883
Naoya Horiguchi86cdb462013-09-11 14:22:13 -0700884/* Movability of hugepages depends on migration support. */
885static inline gfp_t htlb_alloc_mask(struct hstate *h)
886{
Anshuman Khandual7ed2c312019-03-05 15:43:44 -0800887 if (hugepage_movable_supported(h))
Naoya Horiguchi86cdb462013-09-11 14:22:13 -0700888 return GFP_HIGHUSER_MOVABLE;
889 else
890 return GFP_HIGHUSER;
891}
892
Andi Kleena5516432008-07-23 21:27:41 -0700893static struct page *dequeue_huge_page_vma(struct hstate *h,
894 struct vm_area_struct *vma,
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700895 unsigned long address, int avoid_reserve,
896 long chg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700898 struct page *page;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700899 struct mempolicy *mpol;
Vlastimil Babka04ec6262017-07-06 15:40:03 -0700900 gfp_t gfp_mask;
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700901 nodemask_t *nodemask;
Vlastimil Babka04ec6262017-07-06 15:40:03 -0700902 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Mel Gormana1e78772008-07-23 21:27:23 -0700904 /*
905 * A child process with MAP_PRIVATE mappings created by their parent
906 * have no page reserves. This check ensures that reservations are
907 * not "stolen". The child may still get SIGKILLed
908 */
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700909 if (!vma_has_reserves(vma, chg) &&
Andi Kleena5516432008-07-23 21:27:41 -0700910 h->free_huge_pages - h->resv_huge_pages == 0)
Miao Xiec0ff7452010-05-24 14:32:08 -0700911 goto err;
Mel Gormana1e78772008-07-23 21:27:23 -0700912
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700913 /* If reserves cannot be used, ensure enough pages are in the pool */
Andi Kleena5516432008-07-23 21:27:41 -0700914 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700915 goto err;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700916
Vlastimil Babka04ec6262017-07-06 15:40:03 -0700917 gfp_mask = htlb_alloc_mask(h);
918 nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700919 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask);
920 if (page && !avoid_reserve && vma_has_reserves(vma, chg)) {
921 SetPagePrivate(page);
922 h->resv_huge_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Mel Gormancc9a6c82012-03-21 16:34:11 -0700924
925 mpol_cond_put(mpol);
Mel Gormancc9a6c82012-03-21 16:34:11 -0700926 return page;
927
Miao Xiec0ff7452010-05-24 14:32:08 -0700928err:
Mel Gormancc9a6c82012-03-21 16:34:11 -0700929 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Luiz Capitulino1cac6f22014-06-04 16:07:11 -0700932/*
933 * common helper functions for hstate_next_node_to_{alloc|free}.
934 * We may have allocated or freed a huge page based on a different
935 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
936 * be outside of *nodes_allowed. Ensure that we use an allowed
937 * node for alloc or free.
938 */
939static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
940{
Andrew Morton0edaf862016-05-19 17:10:58 -0700941 nid = next_node_in(nid, *nodes_allowed);
Luiz Capitulino1cac6f22014-06-04 16:07:11 -0700942 VM_BUG_ON(nid >= MAX_NUMNODES);
943
944 return nid;
945}
946
947static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
948{
949 if (!node_isset(nid, *nodes_allowed))
950 nid = next_node_allowed(nid, nodes_allowed);
951 return nid;
952}
953
954/*
955 * returns the previously saved node ["this node"] from which to
956 * allocate a persistent huge page for the pool and advance the
957 * next node from which to allocate, handling wrap at end of node
958 * mask.
959 */
960static int hstate_next_node_to_alloc(struct hstate *h,
961 nodemask_t *nodes_allowed)
962{
963 int nid;
964
965 VM_BUG_ON(!nodes_allowed);
966
967 nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
968 h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
969
970 return nid;
971}
972
973/*
974 * helper for free_pool_huge_page() - return the previously saved
975 * node ["this node"] from which to free a huge page. Advance the
976 * next node id whether or not we find a free huge page to free so
977 * that the next attempt to free addresses the next node.
978 */
979static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
980{
981 int nid;
982
983 VM_BUG_ON(!nodes_allowed);
984
985 nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
986 h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
987
988 return nid;
989}
990
991#define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
992 for (nr_nodes = nodes_weight(*mask); \
993 nr_nodes > 0 && \
994 ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
995 nr_nodes--)
996
997#define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
998 for (nr_nodes = nodes_weight(*mask); \
999 nr_nodes > 0 && \
1000 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1001 nr_nodes--)
1002
Aneesh Kumar K.Ve1073d12017-07-06 15:39:17 -07001003#ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001004static void destroy_compound_gigantic_page(struct page *page,
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001005 unsigned int order)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001006{
1007 int i;
1008 int nr_pages = 1 << order;
1009 struct page *p = page + 1;
1010
Gerald Schaeferc8cc7082016-06-24 14:50:04 -07001011 atomic_set(compound_mapcount_ptr(page), 0);
John Hubbard47e29d32020-04-01 21:05:33 -07001012 if (hpage_pincount_available(page))
1013 atomic_set(compound_pincount_ptr(page), 0);
1014
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001015 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001016 clear_compound_head(p);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001017 set_page_refcounted(p);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001018 }
1019
1020 set_compound_order(page, 0);
1021 __ClearPageHead(page);
1022}
1023
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001024static void free_gigantic_page(struct page *page, unsigned int order)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001025{
1026 free_contig_range(page_to_pfn(page), 1 << order);
1027}
1028
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001029#ifdef CONFIG_CONTIG_ALLOC
Michal Hockod9cc948f2018-01-31 16:20:44 -08001030static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
1031 int nid, nodemask_t *nodemask)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001032{
Anshuman Khandual5e27a2d2019-11-30 17:55:06 -08001033 unsigned long nr_pages = 1UL << huge_page_order(h);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001034
Anshuman Khandual5e27a2d2019-11-30 17:55:06 -08001035 return alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001036}
1037
1038static void prep_new_huge_page(struct hstate *h, struct page *page, int nid);
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001039static void prep_compound_gigantic_page(struct page *page, unsigned int order);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001040#else /* !CONFIG_CONTIG_ALLOC */
1041static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
1042 int nid, nodemask_t *nodemask)
1043{
1044 return NULL;
1045}
1046#endif /* CONFIG_CONTIG_ALLOC */
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001047
Aneesh Kumar K.Ve1073d12017-07-06 15:39:17 -07001048#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
Michal Hockod9cc948f2018-01-31 16:20:44 -08001049static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001050 int nid, nodemask_t *nodemask)
1051{
1052 return NULL;
1053}
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001054static inline void free_gigantic_page(struct page *page, unsigned int order) { }
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001055static inline void destroy_compound_gigantic_page(struct page *page,
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001056 unsigned int order) { }
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001057#endif
1058
Andi Kleena5516432008-07-23 21:27:41 -07001059static void update_and_free_page(struct hstate *h, struct page *page)
Adam Litke6af2acb2007-10-16 01:26:16 -07001060{
1061 int i;
Andi Kleena5516432008-07-23 21:27:41 -07001062
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001063 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001064 return;
Andy Whitcroft18229df2008-11-06 12:53:27 -08001065
Andi Kleena5516432008-07-23 21:27:41 -07001066 h->nr_huge_pages--;
1067 h->nr_huge_pages_node[page_to_nid(page)]--;
1068 for (i = 0; i < pages_per_huge_page(h); i++) {
Chris Forbes32f84522011-07-25 17:12:14 -07001069 page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
1070 1 << PG_referenced | 1 << PG_dirty |
Luiz Capitulinoa7407a22014-06-04 16:07:09 -07001071 1 << PG_active | 1 << PG_private |
1072 1 << PG_writeback);
Adam Litke6af2acb2007-10-16 01:26:16 -07001073 }
Sasha Levin309381fea2014-01-23 15:52:54 -08001074 VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page);
Mina Almasry1adc4d42020-04-01 21:11:15 -07001075 VM_BUG_ON_PAGE(hugetlb_cgroup_from_page_rsvd(page), page);
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -08001076 set_compound_page_dtor(page, NULL_COMPOUND_DTOR);
Adam Litke6af2acb2007-10-16 01:26:16 -07001077 set_page_refcounted(page);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001078 if (hstate_is_gigantic(h)) {
1079 destroy_compound_gigantic_page(page, huge_page_order(h));
1080 free_gigantic_page(page, huge_page_order(h));
1081 } else {
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001082 __free_pages(page, huge_page_order(h));
1083 }
Adam Litke6af2acb2007-10-16 01:26:16 -07001084}
1085
Andi Kleene5ff2152008-07-23 21:27:42 -07001086struct hstate *size_to_hstate(unsigned long size)
1087{
1088 struct hstate *h;
1089
1090 for_each_hstate(h) {
1091 if (huge_page_size(h) == size)
1092 return h;
1093 }
1094 return NULL;
1095}
1096
Naoya Horiguchibcc54222015-04-15 16:14:38 -07001097/*
1098 * Test to determine whether the hugepage is "active/in-use" (i.e. being linked
1099 * to hstate->hugepage_activelist.)
1100 *
1101 * This function can be called for tail pages, but never returns true for them.
1102 */
1103bool page_huge_active(struct page *page)
1104{
1105 VM_BUG_ON_PAGE(!PageHuge(page), page);
1106 return PageHead(page) && PagePrivate(&page[1]);
1107}
1108
1109/* never called for tail page */
1110static void set_page_huge_active(struct page *page)
1111{
1112 VM_BUG_ON_PAGE(!PageHeadHuge(page), page);
1113 SetPagePrivate(&page[1]);
1114}
1115
1116static void clear_page_huge_active(struct page *page)
1117{
1118 VM_BUG_ON_PAGE(!PageHeadHuge(page), page);
1119 ClearPagePrivate(&page[1]);
1120}
1121
Michal Hockoab5ac902018-01-31 16:20:48 -08001122/*
1123 * Internal hugetlb specific page flag. Do not use outside of the hugetlb
1124 * code
1125 */
1126static inline bool PageHugeTemporary(struct page *page)
1127{
1128 if (!PageHuge(page))
1129 return false;
1130
1131 return (unsigned long)page[2].mapping == -1U;
1132}
1133
1134static inline void SetPageHugeTemporary(struct page *page)
1135{
1136 page[2].mapping = (void *)-1U;
1137}
1138
1139static inline void ClearPageHugeTemporary(struct page *page)
1140{
1141 page[2].mapping = NULL;
1142}
1143
Waiman Longc77c0a82020-01-04 13:00:15 -08001144static void __free_huge_page(struct page *page)
David Gibson27a85ef2006-03-22 00:08:56 -08001145{
Andi Kleena5516432008-07-23 21:27:41 -07001146 /*
1147 * Can't pass hstate in here because it is called from the
1148 * compound page destructor.
1149 */
Andi Kleene5ff2152008-07-23 21:27:42 -07001150 struct hstate *h = page_hstate(page);
Adam Litke7893d1d2007-10-16 01:26:18 -07001151 int nid = page_to_nid(page);
David Gibson90481622012-03-21 16:34:12 -07001152 struct hugepage_subpool *spool =
1153 (struct hugepage_subpool *)page_private(page);
Joonsoo Kim07443a82013-09-11 14:21:58 -07001154 bool restore_reserve;
David Gibson27a85ef2006-03-22 00:08:56 -08001155
Mike Kravetzb4330af2016-02-05 15:36:38 -08001156 VM_BUG_ON_PAGE(page_count(page), page);
1157 VM_BUG_ON_PAGE(page_mapcount(page), page);
Yongkai Wu8ace22b2018-12-14 14:17:10 -08001158
1159 set_page_private(page, 0);
1160 page->mapping = NULL;
Joonsoo Kim07443a82013-09-11 14:21:58 -07001161 restore_reserve = PagePrivate(page);
Joonsoo Kim16c794b2013-10-16 13:46:48 -07001162 ClearPagePrivate(page);
David Gibson27a85ef2006-03-22 00:08:56 -08001163
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001164 /*
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001165 * If PagePrivate() was set on page, page allocation consumed a
1166 * reservation. If the page was associated with a subpool, there
1167 * would have been a page reserved in the subpool before allocation
1168 * via hugepage_subpool_get_pages(). Since we are 'restoring' the
1169 * reservtion, do not call hugepage_subpool_put_pages() as this will
1170 * remove the reserved page from the subpool.
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001171 */
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001172 if (!restore_reserve) {
1173 /*
1174 * A return code of zero implies that the subpool will be
1175 * under its minimum size if the reservation is not restored
1176 * after page is free. Therefore, force restore_reserve
1177 * operation.
1178 */
1179 if (hugepage_subpool_put_pages(spool, 1) == 0)
1180 restore_reserve = true;
1181 }
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001182
David Gibson27a85ef2006-03-22 00:08:56 -08001183 spin_lock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07001184 clear_page_huge_active(page);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07001185 hugetlb_cgroup_uncharge_page(hstate_index(h),
1186 pages_per_huge_page(h), page);
Joonsoo Kim07443a82013-09-11 14:21:58 -07001187 if (restore_reserve)
1188 h->resv_huge_pages++;
1189
Michal Hockoab5ac902018-01-31 16:20:48 -08001190 if (PageHugeTemporary(page)) {
1191 list_del(&page->lru);
1192 ClearPageHugeTemporary(page);
1193 update_and_free_page(h, page);
1194 } else if (h->surplus_huge_pages_node[nid]) {
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07001195 /* remove the page from active list */
1196 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -07001197 update_and_free_page(h, page);
1198 h->surplus_huge_pages--;
1199 h->surplus_huge_pages_node[nid]--;
Adam Litke7893d1d2007-10-16 01:26:18 -07001200 } else {
Will Deacon5d3a5512012-10-08 16:29:32 -07001201 arch_clear_hugepage_flags(page);
Andi Kleena5516432008-07-23 21:27:41 -07001202 enqueue_huge_page(h, page);
Adam Litke7893d1d2007-10-16 01:26:18 -07001203 }
David Gibson27a85ef2006-03-22 00:08:56 -08001204 spin_unlock(&hugetlb_lock);
1205}
1206
Waiman Longc77c0a82020-01-04 13:00:15 -08001207/*
1208 * As free_huge_page() can be called from a non-task context, we have
1209 * to defer the actual freeing in a workqueue to prevent potential
1210 * hugetlb_lock deadlock.
1211 *
1212 * free_hpage_workfn() locklessly retrieves the linked list of pages to
1213 * be freed and frees them one-by-one. As the page->mapping pointer is
1214 * going to be cleared in __free_huge_page() anyway, it is reused as the
1215 * llist_node structure of a lockless linked list of huge pages to be freed.
1216 */
1217static LLIST_HEAD(hpage_freelist);
1218
1219static void free_hpage_workfn(struct work_struct *work)
1220{
1221 struct llist_node *node;
1222 struct page *page;
1223
1224 node = llist_del_all(&hpage_freelist);
1225
1226 while (node) {
1227 page = container_of((struct address_space **)node,
1228 struct page, mapping);
1229 node = node->next;
1230 __free_huge_page(page);
1231 }
1232}
1233static DECLARE_WORK(free_hpage_work, free_hpage_workfn);
1234
1235void free_huge_page(struct page *page)
1236{
1237 /*
1238 * Defer freeing if in non-task context to avoid hugetlb_lock deadlock.
1239 */
1240 if (!in_task()) {
1241 /*
1242 * Only call schedule_work() if hpage_freelist is previously
1243 * empty. Otherwise, schedule_work() had been called but the
1244 * workfn hasn't retrieved the list yet.
1245 */
1246 if (llist_add((struct llist_node *)&page->mapping,
1247 &hpage_freelist))
1248 schedule_work(&free_hpage_work);
1249 return;
1250 }
1251
1252 __free_huge_page(page);
1253}
1254
Andi Kleena5516432008-07-23 21:27:41 -07001255static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001256{
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07001257 INIT_LIST_HEAD(&page->lru);
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -08001258 set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001259 spin_lock(&hugetlb_lock);
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -07001260 set_hugetlb_cgroup(page, NULL);
Mina Almasry1adc4d42020-04-01 21:11:15 -07001261 set_hugetlb_cgroup_rsvd(page, NULL);
Andi Kleena5516432008-07-23 21:27:41 -07001262 h->nr_huge_pages++;
1263 h->nr_huge_pages_node[nid]++;
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001264 spin_unlock(&hugetlb_lock);
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001265}
1266
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001267static void prep_compound_gigantic_page(struct page *page, unsigned int order)
Wu Fengguang20a03072009-06-16 15:32:22 -07001268{
1269 int i;
1270 int nr_pages = 1 << order;
1271 struct page *p = page + 1;
1272
1273 /* we rely on prep_new_huge_page to set the destructor */
1274 set_compound_order(page, order);
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001275 __ClearPageReserved(page);
Kirill A. Shutemovde09d312016-01-15 16:51:42 -08001276 __SetPageHead(page);
Wu Fengguang20a03072009-06-16 15:32:22 -07001277 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001278 /*
1279 * For gigantic hugepages allocated through bootmem at
1280 * boot, it's safer to be consistent with the not-gigantic
1281 * hugepages and clear the PG_reserved bit from all tail pages
1282 * too. Otherwse drivers using get_user_pages() to access tail
1283 * pages may get the reference counting wrong if they see
1284 * PG_reserved set on a tail page (despite the head page not
1285 * having PG_reserved set). Enforcing this consistency between
1286 * head and tail pages allows drivers to optimize away a check
1287 * on the head page when they need know if put_page() is needed
1288 * after get_user_pages().
1289 */
1290 __ClearPageReserved(p);
Youquan Song58a84aa2011-12-08 14:34:18 -08001291 set_page_count(p, 0);
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001292 set_compound_head(p, page);
Wu Fengguang20a03072009-06-16 15:32:22 -07001293 }
Mike Kravetzb4330af2016-02-05 15:36:38 -08001294 atomic_set(compound_mapcount_ptr(page), -1);
John Hubbard47e29d32020-04-01 21:05:33 -07001295
1296 if (hpage_pincount_available(page))
1297 atomic_set(compound_pincount_ptr(page), 0);
Wu Fengguang20a03072009-06-16 15:32:22 -07001298}
1299
Andrew Morton77959122012-10-08 16:34:11 -07001300/*
1301 * PageHuge() only returns true for hugetlbfs pages, but not for normal or
1302 * transparent huge pages. See the PageTransHuge() documentation for more
1303 * details.
1304 */
Wu Fengguang20a03072009-06-16 15:32:22 -07001305int PageHuge(struct page *page)
1306{
Wu Fengguang20a03072009-06-16 15:32:22 -07001307 if (!PageCompound(page))
1308 return 0;
1309
1310 page = compound_head(page);
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -08001311 return page[1].compound_dtor == HUGETLB_PAGE_DTOR;
Wu Fengguang20a03072009-06-16 15:32:22 -07001312}
Naoya Horiguchi43131e12010-05-28 09:29:22 +09001313EXPORT_SYMBOL_GPL(PageHuge);
1314
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001315/*
1316 * PageHeadHuge() only returns true for hugetlbfs head page, but not for
1317 * normal or transparent huge pages.
1318 */
1319int PageHeadHuge(struct page *page_head)
1320{
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001321 if (!PageHead(page_head))
1322 return 0;
1323
Andrew Morton758f66a2014-01-21 15:48:57 -08001324 return get_compound_page_dtor(page_head) == free_huge_page;
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001325}
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001326
Mike Kravetzc0d03812020-04-01 21:11:05 -07001327/*
1328 * Find address_space associated with hugetlbfs page.
1329 * Upon entry page is locked and page 'was' mapped although mapped state
1330 * could change. If necessary, use anon_vma to find vma and associated
1331 * address space. The returned mapping may be stale, but it can not be
1332 * invalid as page lock (which is held) is required to destroy mapping.
1333 */
1334static struct address_space *_get_hugetlb_page_mapping(struct page *hpage)
1335{
1336 struct anon_vma *anon_vma;
1337 pgoff_t pgoff_start, pgoff_end;
1338 struct anon_vma_chain *avc;
1339 struct address_space *mapping = page_mapping(hpage);
1340
1341 /* Simple file based mapping */
1342 if (mapping)
1343 return mapping;
1344
1345 /*
1346 * Even anonymous hugetlbfs mappings are associated with an
1347 * underlying hugetlbfs file (see hugetlb_file_setup in mmap
1348 * code). Find a vma associated with the anonymous vma, and
1349 * use the file pointer to get address_space.
1350 */
1351 anon_vma = page_lock_anon_vma_read(hpage);
1352 if (!anon_vma)
1353 return mapping; /* NULL */
1354
1355 /* Use first found vma */
1356 pgoff_start = page_to_pgoff(hpage);
1357 pgoff_end = pgoff_start + hpage_nr_pages(hpage) - 1;
1358 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
1359 pgoff_start, pgoff_end) {
1360 struct vm_area_struct *vma = avc->vma;
1361
1362 mapping = vma->vm_file->f_mapping;
1363 break;
1364 }
1365
1366 anon_vma_unlock_read(anon_vma);
1367 return mapping;
1368}
1369
1370/*
1371 * Find and lock address space (mapping) in write mode.
1372 *
1373 * Upon entry, the page is locked which allows us to find the mapping
1374 * even in the case of an anon page. However, locking order dictates
1375 * the i_mmap_rwsem be acquired BEFORE the page lock. This is hugetlbfs
1376 * specific. So, we first try to lock the sema while still holding the
1377 * page lock. If this works, great! If not, then we need to drop the
1378 * page lock and then acquire i_mmap_rwsem and reacquire page lock. Of
1379 * course, need to revalidate state along the way.
1380 */
1381struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage)
1382{
1383 struct address_space *mapping, *mapping2;
1384
1385 mapping = _get_hugetlb_page_mapping(hpage);
1386retry:
1387 if (!mapping)
1388 return mapping;
1389
1390 /*
1391 * If no contention, take lock and return
1392 */
1393 if (i_mmap_trylock_write(mapping))
1394 return mapping;
1395
1396 /*
1397 * Must drop page lock and wait on mapping sema.
1398 * Note: Once page lock is dropped, mapping could become invalid.
1399 * As a hack, increase map count until we lock page again.
1400 */
1401 atomic_inc(&hpage->_mapcount);
1402 unlock_page(hpage);
1403 i_mmap_lock_write(mapping);
1404 lock_page(hpage);
1405 atomic_add_negative(-1, &hpage->_mapcount);
1406
1407 /* verify page is still mapped */
1408 if (!page_mapped(hpage)) {
1409 i_mmap_unlock_write(mapping);
1410 return NULL;
1411 }
1412
1413 /*
1414 * Get address space again and verify it is the same one
1415 * we locked. If not, drop lock and retry.
1416 */
1417 mapping2 = _get_hugetlb_page_mapping(hpage);
1418 if (mapping2 != mapping) {
1419 i_mmap_unlock_write(mapping);
1420 mapping = mapping2;
1421 goto retry;
1422 }
1423
1424 return mapping;
1425}
1426
Zhang Yi13d60f42013-06-25 21:19:31 +08001427pgoff_t __basepage_index(struct page *page)
1428{
1429 struct page *page_head = compound_head(page);
1430 pgoff_t index = page_index(page_head);
1431 unsigned long compound_idx;
1432
1433 if (!PageHuge(page_head))
1434 return page_index(page);
1435
1436 if (compound_order(page_head) >= MAX_ORDER)
1437 compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
1438 else
1439 compound_idx = page - page_head;
1440
1441 return (index << compound_order(page_head)) + compound_idx;
1442}
1443
Michal Hocko0c397da2018-01-31 16:20:56 -08001444static struct page *alloc_buddy_huge_page(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07001445 gfp_t gfp_mask, int nid, nodemask_t *nmask,
1446 nodemask_t *node_alloc_noretry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001448 int order = huge_page_order(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 struct page *page;
Mike Kravetzf60858f2019-09-23 15:37:35 -07001450 bool alloc_try_hard = true;
Joe Jinf96efd52007-07-15 23:38:12 -07001451
Mike Kravetzf60858f2019-09-23 15:37:35 -07001452 /*
1453 * By default we always try hard to allocate the page with
1454 * __GFP_RETRY_MAYFAIL flag. However, if we are allocating pages in
1455 * a loop (to adjust global huge page counts) and previous allocation
1456 * failed, do not continue to try hard on the same node. Use the
1457 * node_alloc_noretry bitmap to manage this state information.
1458 */
1459 if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry))
1460 alloc_try_hard = false;
1461 gfp_mask |= __GFP_COMP|__GFP_NOWARN;
1462 if (alloc_try_hard)
1463 gfp_mask |= __GFP_RETRY_MAYFAIL;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001464 if (nid == NUMA_NO_NODE)
1465 nid = numa_mem_id();
1466 page = __alloc_pages_nodemask(gfp_mask, order, nid, nmask);
1467 if (page)
1468 __count_vm_event(HTLB_BUDDY_PGALLOC);
1469 else
1470 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -07001471
Mike Kravetzf60858f2019-09-23 15:37:35 -07001472 /*
1473 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this
1474 * indicates an overall state change. Clear bit so that we resume
1475 * normal 'try hard' allocations.
1476 */
1477 if (node_alloc_noretry && page && !alloc_try_hard)
1478 node_clear(nid, *node_alloc_noretry);
1479
1480 /*
1481 * If we tried hard to get a page but failed, set bit so that
1482 * subsequent attempts will not try as hard until there is an
1483 * overall state change.
1484 */
1485 if (node_alloc_noretry && !page && alloc_try_hard)
1486 node_set(nid, *node_alloc_noretry);
1487
Nishanth Aravamudan63b46132007-10-16 01:26:24 -07001488 return page;
1489}
1490
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001491/*
Michal Hocko0c397da2018-01-31 16:20:56 -08001492 * Common helper to allocate a fresh hugetlb page. All specific allocators
1493 * should use this function to get new hugetlb pages
1494 */
1495static struct page *alloc_fresh_huge_page(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07001496 gfp_t gfp_mask, int nid, nodemask_t *nmask,
1497 nodemask_t *node_alloc_noretry)
Michal Hocko0c397da2018-01-31 16:20:56 -08001498{
1499 struct page *page;
1500
1501 if (hstate_is_gigantic(h))
1502 page = alloc_gigantic_page(h, gfp_mask, nid, nmask);
1503 else
1504 page = alloc_buddy_huge_page(h, gfp_mask,
Mike Kravetzf60858f2019-09-23 15:37:35 -07001505 nid, nmask, node_alloc_noretry);
Michal Hocko0c397da2018-01-31 16:20:56 -08001506 if (!page)
1507 return NULL;
1508
1509 if (hstate_is_gigantic(h))
1510 prep_compound_gigantic_page(page, huge_page_order(h));
1511 prep_new_huge_page(h, page, page_to_nid(page));
1512
1513 return page;
1514}
1515
1516/*
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001517 * Allocates a fresh page to the hugetlb allocator pool in the node interleaved
1518 * manner.
1519 */
Mike Kravetzf60858f2019-09-23 15:37:35 -07001520static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
1521 nodemask_t *node_alloc_noretry)
Joonsoo Kimb2261022013-09-11 14:21:00 -07001522{
1523 struct page *page;
1524 int nr_nodes, node;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001525 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001526
1527 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
Mike Kravetzf60858f2019-09-23 15:37:35 -07001528 page = alloc_fresh_huge_page(h, gfp_mask, node, nodes_allowed,
1529 node_alloc_noretry);
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001530 if (page)
Joonsoo Kimb2261022013-09-11 14:21:00 -07001531 break;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001532 }
1533
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001534 if (!page)
1535 return 0;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001536
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001537 put_page(page); /* free it into the hugepage allocator */
1538
1539 return 1;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001540}
1541
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001542/*
1543 * Free huge page from pool from next node to free.
1544 * Attempt to keep persistent huge pages more or less
1545 * balanced over allowed nodes.
1546 * Called with hugetlb_lock locked.
1547 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001548static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
1549 bool acct_surplus)
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001550{
Joonsoo Kimb2261022013-09-11 14:21:00 -07001551 int nr_nodes, node;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001552 int ret = 0;
1553
Joonsoo Kimb2261022013-09-11 14:21:00 -07001554 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001555 /*
1556 * If we're returning unused surplus pages, only examine
1557 * nodes with surplus pages.
1558 */
Joonsoo Kimb2261022013-09-11 14:21:00 -07001559 if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
1560 !list_empty(&h->hugepage_freelists[node])) {
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001561 struct page *page =
Joonsoo Kimb2261022013-09-11 14:21:00 -07001562 list_entry(h->hugepage_freelists[node].next,
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001563 struct page, lru);
1564 list_del(&page->lru);
1565 h->free_huge_pages--;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001566 h->free_huge_pages_node[node]--;
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001567 if (acct_surplus) {
1568 h->surplus_huge_pages--;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001569 h->surplus_huge_pages_node[node]--;
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001570 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001571 update_and_free_page(h, page);
1572 ret = 1;
Lee Schermerhorn9a76db02009-12-14 17:58:15 -08001573 break;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001574 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07001575 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001576
1577 return ret;
1578}
1579
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001580/*
1581 * Dissolve a given free hugepage into free buddy pages. This function does
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001582 * nothing for in-use hugepages and non-hugepages.
1583 * This function returns values like below:
1584 *
1585 * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
1586 * (allocated or reserved.)
1587 * 0: successfully dissolved free hugepages or the page is not a
1588 * hugepage (considered as already dissolved)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001589 */
Anshuman Khandualc3114a82017-07-10 15:47:41 -07001590int dissolve_free_huge_page(struct page *page)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001591{
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07001592 int rc = -EBUSY;
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001593
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001594 /* Not to disrupt normal path by vainly holding hugetlb_lock */
1595 if (!PageHuge(page))
1596 return 0;
1597
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001598 spin_lock(&hugetlb_lock);
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001599 if (!PageHuge(page)) {
1600 rc = 0;
1601 goto out;
1602 }
1603
1604 if (!page_count(page)) {
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001605 struct page *head = compound_head(page);
1606 struct hstate *h = page_hstate(head);
1607 int nid = page_to_nid(head);
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07001608 if (h->free_huge_pages - h->resv_huge_pages == 0)
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001609 goto out;
Anshuman Khandualc3114a82017-07-10 15:47:41 -07001610 /*
1611 * Move PageHWPoison flag from head page to the raw error page,
1612 * which makes any subpages rather than the error page reusable.
1613 */
1614 if (PageHWPoison(head) && page != head) {
1615 SetPageHWPoison(page);
1616 ClearPageHWPoison(head);
1617 }
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001618 list_del(&head->lru);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001619 h->free_huge_pages--;
1620 h->free_huge_pages_node[nid]--;
zhong jiangc1470b32016-08-11 15:32:55 -07001621 h->max_huge_pages--;
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001622 update_and_free_page(h, head);
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07001623 rc = 0;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001624 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001625out:
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001626 spin_unlock(&hugetlb_lock);
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001627 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001628}
1629
1630/*
1631 * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
1632 * make specified memory blocks removable from the system.
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001633 * Note that this will dissolve a free gigantic hugepage completely, if any
1634 * part of it lies within the given range.
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001635 * Also note that if dissolve_free_huge_page() returns with an error, all
1636 * free hugepages that were dissolved before that error are lost.
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001637 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001638int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001639{
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001640 unsigned long pfn;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07001641 struct page *page;
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001642 int rc = 0;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001643
Li Zhongd0177632014-08-06 16:07:56 -07001644 if (!hugepages_supported())
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001645 return rc;
Li Zhongd0177632014-08-06 16:07:56 -07001646
Gerald Schaefereb03aa02016-10-07 17:01:13 -07001647 for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
1648 page = pfn_to_page(pfn);
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001649 rc = dissolve_free_huge_page(page);
1650 if (rc)
1651 break;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07001652 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001653
1654 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001655}
1656
Michal Hockoab5ac902018-01-31 16:20:48 -08001657/*
1658 * Allocates a fresh surplus page from the page allocator.
1659 */
Michal Hocko0c397da2018-01-31 16:20:56 -08001660static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,
Michal Hockoaaf14e42017-07-10 15:49:08 -07001661 int nid, nodemask_t *nmask)
Adam Litke7893d1d2007-10-16 01:26:18 -07001662{
Michal Hocko9980d742018-01-31 16:20:52 -08001663 struct page *page = NULL;
Adam Litke7893d1d2007-10-16 01:26:18 -07001664
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001665 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07001666 return NULL;
1667
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001668 spin_lock(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08001669 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages)
1670 goto out_unlock;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001671 spin_unlock(&hugetlb_lock);
1672
Mike Kravetzf60858f2019-09-23 15:37:35 -07001673 page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL);
Michal Hocko9980d742018-01-31 16:20:52 -08001674 if (!page)
Michal Hocko0c397da2018-01-31 16:20:56 -08001675 return NULL;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001676
1677 spin_lock(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08001678 /*
1679 * We could have raced with the pool size change.
1680 * Double check that and simply deallocate the new page
1681 * if we would end up overcommiting the surpluses. Abuse
1682 * temporary page to workaround the nasty free_huge_page
1683 * codeflow
1684 */
1685 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
1686 SetPageHugeTemporary(page);
Kai Shen2bf753e2019-05-13 17:15:37 -07001687 spin_unlock(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08001688 put_page(page);
Kai Shen2bf753e2019-05-13 17:15:37 -07001689 return NULL;
Michal Hocko9980d742018-01-31 16:20:52 -08001690 } else {
Michal Hocko9980d742018-01-31 16:20:52 -08001691 h->surplus_huge_pages++;
Michal Hocko4704dea2018-03-09 15:50:55 -08001692 h->surplus_huge_pages_node[page_to_nid(page)]++;
Adam Litke7893d1d2007-10-16 01:26:18 -07001693 }
Michal Hocko9980d742018-01-31 16:20:52 -08001694
1695out_unlock:
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001696 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001697
1698 return page;
1699}
1700
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001701struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
1702 int nid, nodemask_t *nmask)
Michal Hockoab5ac902018-01-31 16:20:48 -08001703{
1704 struct page *page;
1705
1706 if (hstate_is_gigantic(h))
1707 return NULL;
1708
Mike Kravetzf60858f2019-09-23 15:37:35 -07001709 page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL);
Michal Hockoab5ac902018-01-31 16:20:48 -08001710 if (!page)
1711 return NULL;
1712
1713 /*
1714 * We do not account these pages as surplus because they are only
1715 * temporary and will be released properly on the last reference
1716 */
Michal Hockoab5ac902018-01-31 16:20:48 -08001717 SetPageHugeTemporary(page);
1718
1719 return page;
1720}
1721
Adam Litkee4e574b2007-10-16 01:26:19 -07001722/*
Dave Hansen099730d2015-11-05 18:50:17 -08001723 * Use the VMA's mpolicy to allocate a huge page from the buddy.
1724 */
Dave Hansene0ec90e2015-11-05 18:50:20 -08001725static
Michal Hocko0c397da2018-01-31 16:20:56 -08001726struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h,
Dave Hansen099730d2015-11-05 18:50:17 -08001727 struct vm_area_struct *vma, unsigned long addr)
1728{
Michal Hockoaaf14e42017-07-10 15:49:08 -07001729 struct page *page;
1730 struct mempolicy *mpol;
1731 gfp_t gfp_mask = htlb_alloc_mask(h);
1732 int nid;
1733 nodemask_t *nodemask;
1734
1735 nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
Michal Hocko0c397da2018-01-31 16:20:56 -08001736 page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask);
Michal Hockoaaf14e42017-07-10 15:49:08 -07001737 mpol_cond_put(mpol);
1738
1739 return page;
Dave Hansen099730d2015-11-05 18:50:17 -08001740}
1741
Michal Hockoab5ac902018-01-31 16:20:48 -08001742/* page migration callback function */
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001743struct page *alloc_huge_page_node(struct hstate *h, int nid)
1744{
Michal Hockoaaf14e42017-07-10 15:49:08 -07001745 gfp_t gfp_mask = htlb_alloc_mask(h);
Joonsoo Kim4ef91842013-09-11 14:21:51 -07001746 struct page *page = NULL;
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001747
Michal Hockoaaf14e42017-07-10 15:49:08 -07001748 if (nid != NUMA_NO_NODE)
1749 gfp_mask |= __GFP_THISNODE;
1750
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001751 spin_lock(&hugetlb_lock);
Joonsoo Kim4ef91842013-09-11 14:21:51 -07001752 if (h->free_huge_pages - h->resv_huge_pages > 0)
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001753 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, NULL);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001754 spin_unlock(&hugetlb_lock);
1755
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -07001756 if (!page)
Michal Hocko0c397da2018-01-31 16:20:56 -08001757 page = alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001758
1759 return page;
1760}
1761
Michal Hockoab5ac902018-01-31 16:20:48 -08001762/* page migration callback function */
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001763struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
1764 nodemask_t *nmask)
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001765{
Michal Hockoaaf14e42017-07-10 15:49:08 -07001766 gfp_t gfp_mask = htlb_alloc_mask(h);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001767
1768 spin_lock(&hugetlb_lock);
1769 if (h->free_huge_pages - h->resv_huge_pages > 0) {
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001770 struct page *page;
1771
1772 page = dequeue_huge_page_nodemask(h, gfp_mask, preferred_nid, nmask);
1773 if (page) {
1774 spin_unlock(&hugetlb_lock);
1775 return page;
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001776 }
1777 }
1778 spin_unlock(&hugetlb_lock);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001779
Michal Hocko0c397da2018-01-31 16:20:56 -08001780 return alloc_migrate_huge_page(h, gfp_mask, preferred_nid, nmask);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001781}
1782
Michal Hockoebd63722018-01-31 16:21:00 -08001783/* mempolicy aware migration callback */
Michal Hocko389c8172018-01-31 16:21:03 -08001784struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
1785 unsigned long address)
Michal Hockoebd63722018-01-31 16:21:00 -08001786{
1787 struct mempolicy *mpol;
1788 nodemask_t *nodemask;
1789 struct page *page;
Michal Hockoebd63722018-01-31 16:21:00 -08001790 gfp_t gfp_mask;
1791 int node;
1792
Michal Hockoebd63722018-01-31 16:21:00 -08001793 gfp_mask = htlb_alloc_mask(h);
1794 node = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
1795 page = alloc_huge_page_nodemask(h, node, nodemask);
1796 mpol_cond_put(mpol);
1797
1798 return page;
1799}
1800
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001801/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001802 * Increase the hugetlb pool such that it can accommodate a reservation
Adam Litkee4e574b2007-10-16 01:26:19 -07001803 * of size 'delta'.
1804 */
Andi Kleena5516432008-07-23 21:27:41 -07001805static int gather_surplus_pages(struct hstate *h, int delta)
Adam Litkee4e574b2007-10-16 01:26:19 -07001806{
1807 struct list_head surplus_list;
1808 struct page *page, *tmp;
1809 int ret, i;
1810 int needed, allocated;
Hillf Danton28073b02012-03-21 16:34:00 -07001811 bool alloc_ok = true;
Adam Litkee4e574b2007-10-16 01:26:19 -07001812
Andi Kleena5516432008-07-23 21:27:41 -07001813 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -08001814 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -07001815 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07001816 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -08001817 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001818
1819 allocated = 0;
1820 INIT_LIST_HEAD(&surplus_list);
1821
1822 ret = -ENOMEM;
1823retry:
1824 spin_unlock(&hugetlb_lock);
1825 for (i = 0; i < needed; i++) {
Michal Hocko0c397da2018-01-31 16:20:56 -08001826 page = alloc_surplus_huge_page(h, htlb_alloc_mask(h),
Michal Hockoaaf14e42017-07-10 15:49:08 -07001827 NUMA_NO_NODE, NULL);
Hillf Danton28073b02012-03-21 16:34:00 -07001828 if (!page) {
1829 alloc_ok = false;
1830 break;
1831 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001832 list_add(&page->lru, &surplus_list);
David Rientjes69ed7792017-07-10 15:48:50 -07001833 cond_resched();
Adam Litkee4e574b2007-10-16 01:26:19 -07001834 }
Hillf Danton28073b02012-03-21 16:34:00 -07001835 allocated += i;
Adam Litkee4e574b2007-10-16 01:26:19 -07001836
1837 /*
1838 * After retaking hugetlb_lock, we need to recalculate 'needed'
1839 * because either resv_huge_pages or free_huge_pages may have changed.
1840 */
1841 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001842 needed = (h->resv_huge_pages + delta) -
1843 (h->free_huge_pages + allocated);
Hillf Danton28073b02012-03-21 16:34:00 -07001844 if (needed > 0) {
1845 if (alloc_ok)
1846 goto retry;
1847 /*
1848 * We were not able to allocate enough pages to
1849 * satisfy the entire reservation so we free what
1850 * we've allocated so far.
1851 */
1852 goto free;
1853 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001854 /*
1855 * The surplus_list now contains _at_least_ the number of extra pages
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001856 * needed to accommodate the reservation. Add the appropriate number
Adam Litkee4e574b2007-10-16 01:26:19 -07001857 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -08001858 * allocator. Commit the entire reservation here to prevent another
1859 * process from stealing the pages as they are added to the pool but
1860 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -07001861 */
1862 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -07001863 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07001864 ret = 0;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001865
Adam Litke19fc3f02008-04-28 02:12:20 -07001866 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -07001867 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -07001868 if ((--needed) < 0)
1869 break;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001870 /*
1871 * This page is now managed by the hugetlb allocator and has
1872 * no users -- drop the buddy allocator's reference.
1873 */
1874 put_page_testzero(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001875 VM_BUG_ON_PAGE(page_count(page), page);
Andi Kleena5516432008-07-23 21:27:41 -07001876 enqueue_huge_page(h, page);
Adam Litke19fc3f02008-04-28 02:12:20 -07001877 }
Hillf Danton28073b02012-03-21 16:34:00 -07001878free:
Hillf Dantonb0365c82011-12-28 15:57:16 -08001879 spin_unlock(&hugetlb_lock);
Adam Litke19fc3f02008-04-28 02:12:20 -07001880
1881 /* Free unnecessary surplus pages to the buddy allocator */
Joonsoo Kimc0d934b2013-09-11 14:21:02 -07001882 list_for_each_entry_safe(page, tmp, &surplus_list, lru)
1883 put_page(page);
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001884 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07001885
1886 return ret;
1887}
1888
1889/*
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001890 * This routine has two main purposes:
1891 * 1) Decrement the reservation count (resv_huge_pages) by the value passed
1892 * in unused_resv_pages. This corresponds to the prior adjustments made
1893 * to the associated reservation map.
1894 * 2) Free any unused surplus pages that may have been allocated to satisfy
1895 * the reservation. As many as unused_resv_pages may be freed.
1896 *
1897 * Called with hugetlb_lock held. However, the lock could be dropped (and
1898 * reacquired) during calls to cond_resched_lock. Whenever dropping the lock,
1899 * we must make sure nobody else can claim pages we are in the process of
1900 * freeing. Do this by ensuring resv_huge_page always is greater than the
1901 * number of huge pages we plan to free when dropping the lock.
Adam Litkee4e574b2007-10-16 01:26:19 -07001902 */
Andi Kleena5516432008-07-23 21:27:41 -07001903static void return_unused_surplus_pages(struct hstate *h,
1904 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -07001905{
Adam Litkee4e574b2007-10-16 01:26:19 -07001906 unsigned long nr_pages;
1907
Andi Kleenaa888a72008-07-23 21:27:47 -07001908 /* Cannot return gigantic pages currently */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001909 if (hstate_is_gigantic(h))
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001910 goto out;
Andi Kleenaa888a72008-07-23 21:27:47 -07001911
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001912 /*
1913 * Part (or even all) of the reservation could have been backed
1914 * by pre-allocated pages. Only free surplus pages.
1915 */
Andi Kleena5516432008-07-23 21:27:41 -07001916 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -07001917
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001918 /*
1919 * We want to release as many surplus pages as possible, spread
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08001920 * evenly across all nodes with memory. Iterate across these nodes
1921 * until we can no longer free unreserved surplus pages. This occurs
1922 * when the nodes with surplus pages have no free pages.
1923 * free_pool_huge_page() will balance the the freed pages across the
1924 * on-line nodes with memory and will handle the hstate accounting.
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001925 *
1926 * Note that we decrement resv_huge_pages as we free the pages. If
1927 * we drop the lock, resv_huge_pages will still be sufficiently large
1928 * to cover subsequent pages we may free.
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001929 */
1930 while (nr_pages--) {
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001931 h->resv_huge_pages--;
1932 unused_resv_pages--;
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08001933 if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001934 goto out;
Mizuma, Masayoshi7848a4b2014-04-18 15:07:18 -07001935 cond_resched_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07001936 }
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001937
1938out:
1939 /* Fully uncommit the reservation */
1940 h->resv_huge_pages -= unused_resv_pages;
Adam Litkee4e574b2007-10-16 01:26:19 -07001941}
1942
Mike Kravetz5e911372015-09-08 15:01:28 -07001943
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001944/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001945 * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
Mike Kravetz5e911372015-09-08 15:01:28 -07001946 * are used by the huge page allocation routines to manage reservations.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001947 *
1948 * vma_needs_reservation is called to determine if the huge page at addr
1949 * within the vma has an associated reservation. If a reservation is
1950 * needed, the value 1 is returned. The caller is then responsible for
1951 * managing the global reservation and subpool usage counts. After
1952 * the huge page has been allocated, vma_commit_reservation is called
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001953 * to add the page to the reservation map. If the page allocation fails,
1954 * the reservation must be ended instead of committed. vma_end_reservation
1955 * is called in such cases.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001956 *
1957 * In the normal case, vma_commit_reservation returns the same value
1958 * as the preceding vma_needs_reservation call. The only time this
1959 * is not the case is if a reserve map was changed between calls. It
1960 * is the responsibility of the caller to notice the difference and
1961 * take appropriate action.
Mike Kravetz96b96a92016-11-10 10:46:32 -08001962 *
1963 * vma_add_reservation is used in error paths where a reservation must
1964 * be restored when a newly allocated huge page must be freed. It is
1965 * to be called after calling vma_needs_reservation to determine if a
1966 * reservation exists.
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001967 */
Mike Kravetz5e911372015-09-08 15:01:28 -07001968enum vma_resv_mode {
1969 VMA_NEEDS_RESV,
1970 VMA_COMMIT_RESV,
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001971 VMA_END_RESV,
Mike Kravetz96b96a92016-11-10 10:46:32 -08001972 VMA_ADD_RESV,
Mike Kravetz5e911372015-09-08 15:01:28 -07001973};
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001974static long __vma_reservation_common(struct hstate *h,
1975 struct vm_area_struct *vma, unsigned long addr,
Mike Kravetz5e911372015-09-08 15:01:28 -07001976 enum vma_resv_mode mode)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001977{
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001978 struct resv_map *resv;
1979 pgoff_t idx;
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001980 long ret;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001981
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001982 resv = vma_resv_map(vma);
1983 if (!resv)
Andy Whitcroft84afd992008-07-23 21:27:32 -07001984 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001985
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001986 idx = vma_hugecache_offset(h, vma, addr);
Mike Kravetz5e911372015-09-08 15:01:28 -07001987 switch (mode) {
1988 case VMA_NEEDS_RESV:
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001989 ret = region_chg(resv, idx, idx + 1);
Mike Kravetz5e911372015-09-08 15:01:28 -07001990 break;
1991 case VMA_COMMIT_RESV:
1992 ret = region_add(resv, idx, idx + 1);
1993 break;
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001994 case VMA_END_RESV:
Mike Kravetz5e911372015-09-08 15:01:28 -07001995 region_abort(resv, idx, idx + 1);
1996 ret = 0;
1997 break;
Mike Kravetz96b96a92016-11-10 10:46:32 -08001998 case VMA_ADD_RESV:
1999 if (vma->vm_flags & VM_MAYSHARE)
2000 ret = region_add(resv, idx, idx + 1);
2001 else {
2002 region_abort(resv, idx, idx + 1);
2003 ret = region_del(resv, idx, idx + 1);
2004 }
2005 break;
Mike Kravetz5e911372015-09-08 15:01:28 -07002006 default:
2007 BUG();
2008 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07002009
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002010 if (vma->vm_flags & VM_MAYSHARE)
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002011 return ret;
Mike Kravetz67961f92016-06-08 15:33:42 -07002012 else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && ret >= 0) {
2013 /*
2014 * In most cases, reserves always exist for private mappings.
2015 * However, a file associated with mapping could have been
2016 * hole punched or truncated after reserves were consumed.
2017 * As subsequent fault on such a range will not use reserves.
2018 * Subtle - The reserve map for private mappings has the
2019 * opposite meaning than that of shared mappings. If NO
2020 * entry is in the reserve map, it means a reservation exists.
2021 * If an entry exists in the reserve map, it means the
2022 * reservation has already been consumed. As a result, the
2023 * return value of this routine is the opposite of the
2024 * value returned from reserve map manipulation routines above.
2025 */
2026 if (ret)
2027 return 0;
2028 else
2029 return 1;
2030 }
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002031 else
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002032 return ret < 0 ? ret : 0;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002033}
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002034
2035static long vma_needs_reservation(struct hstate *h,
Andi Kleena5516432008-07-23 21:27:41 -07002036 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002037{
Mike Kravetz5e911372015-09-08 15:01:28 -07002038 return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002039}
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002040
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002041static long vma_commit_reservation(struct hstate *h,
2042 struct vm_area_struct *vma, unsigned long addr)
2043{
Mike Kravetz5e911372015-09-08 15:01:28 -07002044 return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
2045}
2046
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002047static void vma_end_reservation(struct hstate *h,
Mike Kravetz5e911372015-09-08 15:01:28 -07002048 struct vm_area_struct *vma, unsigned long addr)
2049{
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002050 (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002051}
2052
Mike Kravetz96b96a92016-11-10 10:46:32 -08002053static long vma_add_reservation(struct hstate *h,
2054 struct vm_area_struct *vma, unsigned long addr)
2055{
2056 return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
2057}
2058
2059/*
2060 * This routine is called to restore a reservation on error paths. In the
2061 * specific error paths, a huge page was allocated (via alloc_huge_page)
2062 * and is about to be freed. If a reservation for the page existed,
2063 * alloc_huge_page would have consumed the reservation and set PagePrivate
2064 * in the newly allocated page. When the page is freed via free_huge_page,
2065 * the global reservation count will be incremented if PagePrivate is set.
2066 * However, free_huge_page can not adjust the reserve map. Adjust the
2067 * reserve map here to be consistent with global reserve count adjustments
2068 * to be made by free_huge_page.
2069 */
2070static void restore_reserve_on_error(struct hstate *h,
2071 struct vm_area_struct *vma, unsigned long address,
2072 struct page *page)
2073{
2074 if (unlikely(PagePrivate(page))) {
2075 long rc = vma_needs_reservation(h, vma, address);
2076
2077 if (unlikely(rc < 0)) {
2078 /*
2079 * Rare out of memory condition in reserve map
2080 * manipulation. Clear PagePrivate so that
2081 * global reserve count will not be incremented
2082 * by free_huge_page. This will make it appear
2083 * as though the reservation for this page was
2084 * consumed. This may prevent the task from
2085 * faulting in the page at a later time. This
2086 * is better than inconsistent global huge page
2087 * accounting of reserve counts.
2088 */
2089 ClearPagePrivate(page);
2090 } else if (rc) {
2091 rc = vma_add_reservation(h, vma, address);
2092 if (unlikely(rc < 0))
2093 /*
2094 * See above comment about rare out of
2095 * memory condition.
2096 */
2097 ClearPagePrivate(page);
2098 } else
2099 vma_end_reservation(h, vma, address);
2100 }
2101}
2102
Mike Kravetz70c35472015-09-08 15:01:54 -07002103struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002104 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
David Gibson90481622012-03-21 16:34:12 -07002106 struct hugepage_subpool *spool = subpool_vma(vma);
Andi Kleena5516432008-07-23 21:27:41 -07002107 struct hstate *h = hstate_vma(vma);
Adam Litke348ea202007-11-14 16:59:37 -08002108 struct page *page;
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002109 long map_chg, map_commit;
2110 long gbl_chg;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07002111 int ret, idx;
2112 struct hugetlb_cgroup *h_cg;
Adam Litke2fc39ce2007-11-14 16:59:39 -08002113
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07002114 idx = hstate_index(h);
Mel Gormana1e78772008-07-23 21:27:23 -07002115 /*
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002116 * Examine the region/reserve map to determine if the process
2117 * has a reservation for the page to be allocated. A return
2118 * code of zero indicates a reservation exists (no change).
Mel Gormana1e78772008-07-23 21:27:23 -07002119 */
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002120 map_chg = gbl_chg = vma_needs_reservation(h, vma, addr);
2121 if (map_chg < 0)
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07002122 return ERR_PTR(-ENOMEM);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002123
2124 /*
2125 * Processes that did not create the mapping will have no
2126 * reserves as indicated by the region/reserve map. Check
2127 * that the allocation will not exceed the subpool limit.
2128 * Allocations for MAP_NORESERVE mappings also need to be
2129 * checked against any subpool limit.
2130 */
2131 if (map_chg || avoid_reserve) {
2132 gbl_chg = hugepage_subpool_get_pages(spool, 1);
2133 if (gbl_chg < 0) {
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002134 vma_end_reservation(h, vma, addr);
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07002135 return ERR_PTR(-ENOSPC);
Mike Kravetz5e911372015-09-08 15:01:28 -07002136 }
Mel Gormana1e78772008-07-23 21:27:23 -07002137
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002138 /*
2139 * Even though there was no reservation in the region/reserve
2140 * map, there could be reservations associated with the
2141 * subpool that can be used. This would be indicated if the
2142 * return value of hugepage_subpool_get_pages() is zero.
2143 * However, if avoid_reserve is specified we still avoid even
2144 * the subpool reservations.
2145 */
2146 if (avoid_reserve)
2147 gbl_chg = 1;
2148 }
2149
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07002150 ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002151 if (ret)
2152 goto out_subpool_put;
2153
Mel Gormana1e78772008-07-23 21:27:23 -07002154 spin_lock(&hugetlb_lock);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002155 /*
2156 * glb_chg is passed to indicate whether or not a page must be taken
2157 * from the global free pool (global change). gbl_chg == 0 indicates
2158 * a reservation exists for the allocation.
2159 */
2160 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07002161 if (!page) {
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -07002162 spin_unlock(&hugetlb_lock);
Michal Hocko0c397da2018-01-31 16:20:56 -08002163 page = alloc_buddy_huge_page_with_mpol(h, vma, addr);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002164 if (!page)
2165 goto out_uncharge_cgroup;
Naoya Horiguchia88c7692015-12-11 13:40:24 -08002166 if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
2167 SetPagePrivate(page);
2168 h->resv_huge_pages--;
2169 }
Aneesh Kumar K.V79dbb232012-07-31 16:42:32 -07002170 spin_lock(&hugetlb_lock);
2171 list_move(&page->lru, &h->hugepage_activelist);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07002172 /* Fall through */
Mel Gormana1e78772008-07-23 21:27:23 -07002173 }
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07002174 hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
2175 spin_unlock(&hugetlb_lock);
Mel Gormana1e78772008-07-23 21:27:23 -07002176
David Gibson90481622012-03-21 16:34:12 -07002177 set_page_private(page, (unsigned long)spool);
Mel Gormana1e78772008-07-23 21:27:23 -07002178
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002179 map_commit = vma_commit_reservation(h, vma, addr);
2180 if (unlikely(map_chg > map_commit)) {
Mike Kravetz33039672015-06-24 16:57:58 -07002181 /*
2182 * The page was added to the reservation map between
2183 * vma_needs_reservation and vma_commit_reservation.
2184 * This indicates a race with hugetlb_reserve_pages.
2185 * Adjust for the subpool count incremented above AND
2186 * in hugetlb_reserve_pages for the same page. Also,
2187 * the reservation count added in hugetlb_reserve_pages
2188 * no longer applies.
2189 */
2190 long rsv_adjust;
2191
2192 rsv_adjust = hugepage_subpool_put_pages(spool, 1);
2193 hugetlb_acct_memory(h, -rsv_adjust);
2194 }
Adam Litke90d8b7e2007-11-14 16:59:42 -08002195 return page;
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002196
2197out_uncharge_cgroup:
2198 hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
2199out_subpool_put:
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002200 if (map_chg || avoid_reserve)
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002201 hugepage_subpool_put_pages(spool, 1);
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002202 vma_end_reservation(h, vma, addr);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002203 return ERR_PTR(-ENOSPC);
David Gibsonb45b5bd2006-03-22 00:08:55 -08002204}
2205
Aneesh Kumar K.Ve24a1302017-07-28 10:31:25 +05302206int alloc_bootmem_huge_page(struct hstate *h)
2207 __attribute__ ((weak, alias("__alloc_bootmem_huge_page")));
2208int __alloc_bootmem_huge_page(struct hstate *h)
Andi Kleenaa888a72008-07-23 21:27:47 -07002209{
2210 struct huge_bootmem_page *m;
Joonsoo Kimb2261022013-09-11 14:21:00 -07002211 int nr_nodes, node;
Andi Kleenaa888a72008-07-23 21:27:47 -07002212
Joonsoo Kimb2261022013-09-11 14:21:00 -07002213 for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
Andi Kleenaa888a72008-07-23 21:27:47 -07002214 void *addr;
2215
Mike Rapoporteb31d552018-10-30 15:08:04 -07002216 addr = memblock_alloc_try_nid_raw(
Grygorii Strashko8b89a112014-01-21 15:50:36 -08002217 huge_page_size(h), huge_page_size(h),
Mike Rapoport97ad1082018-10-30 15:09:44 -07002218 0, MEMBLOCK_ALLOC_ACCESSIBLE, node);
Andi Kleenaa888a72008-07-23 21:27:47 -07002219 if (addr) {
2220 /*
2221 * Use the beginning of the huge page to store the
2222 * huge_bootmem_page struct (until gather_bootmem
2223 * puts them into the mem_map).
2224 */
2225 m = addr;
Cyrill Gorcunov91f47662009-01-06 14:40:33 -08002226 goto found;
Andi Kleenaa888a72008-07-23 21:27:47 -07002227 }
Andi Kleenaa888a72008-07-23 21:27:47 -07002228 }
2229 return 0;
2230
2231found:
Luiz Capitulinodf994ea2014-12-12 16:55:21 -08002232 BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h)));
Andi Kleenaa888a72008-07-23 21:27:47 -07002233 /* Put them into a private list first because mem_map is not up yet */
Cannon Matthews330d6e42018-08-17 15:49:17 -07002234 INIT_LIST_HEAD(&m->list);
Andi Kleenaa888a72008-07-23 21:27:47 -07002235 list_add(&m->list, &huge_boot_pages);
2236 m->hstate = h;
2237 return 1;
2238}
2239
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08002240static void __init prep_compound_huge_page(struct page *page,
2241 unsigned int order)
Andy Whitcroft18229df2008-11-06 12:53:27 -08002242{
2243 if (unlikely(order > (MAX_ORDER - 1)))
2244 prep_compound_gigantic_page(page, order);
2245 else
2246 prep_compound_page(page, order);
2247}
2248
Andi Kleenaa888a72008-07-23 21:27:47 -07002249/* Put bootmem huge pages into the standard lists after mem_map is up */
2250static void __init gather_bootmem_prealloc(void)
2251{
2252 struct huge_bootmem_page *m;
2253
2254 list_for_each_entry(m, &huge_boot_pages, list) {
Mike Kravetz40d18eb2018-08-17 15:49:07 -07002255 struct page *page = virt_to_page(m);
Andi Kleenaa888a72008-07-23 21:27:47 -07002256 struct hstate *h = m->hstate;
Becky Bruceee8f2482011-07-25 17:11:50 -07002257
Andi Kleenaa888a72008-07-23 21:27:47 -07002258 WARN_ON(page_count(page) != 1);
Andy Whitcroft18229df2008-11-06 12:53:27 -08002259 prep_compound_huge_page(page, h->order);
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07002260 WARN_ON(PageReserved(page));
Andi Kleenaa888a72008-07-23 21:27:47 -07002261 prep_new_huge_page(h, page, page_to_nid(page));
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002262 put_page(page); /* free it into the hugepage allocator */
2263
Rafael Aquinib0320c72011-06-15 15:08:39 -07002264 /*
2265 * If we had gigantic hugepages allocated at boot time, we need
2266 * to restore the 'stolen' pages to totalram_pages in order to
2267 * fix confusing memory reports from free(1) and another
2268 * side-effects, like CommitLimit going negative.
2269 */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002270 if (hstate_is_gigantic(h))
Jiang Liu3dcc0572013-07-03 15:03:21 -07002271 adjust_managed_page_count(page, 1 << h->order);
Cannon Matthews520495f2018-07-03 17:02:43 -07002272 cond_resched();
Andi Kleenaa888a72008-07-23 21:27:47 -07002273 }
2274}
2275
Andi Kleen8faa8b02008-07-23 21:27:48 -07002276static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
2278 unsigned long i;
Mike Kravetzf60858f2019-09-23 15:37:35 -07002279 nodemask_t *node_alloc_noretry;
2280
2281 if (!hstate_is_gigantic(h)) {
2282 /*
2283 * Bit mask controlling how hard we retry per-node allocations.
2284 * Ignore errors as lower level routines can deal with
2285 * node_alloc_noretry == NULL. If this kmalloc fails at boot
2286 * time, we are likely in bigger trouble.
2287 */
2288 node_alloc_noretry = kmalloc(sizeof(*node_alloc_noretry),
2289 GFP_KERNEL);
2290 } else {
2291 /* allocations done at boot time */
2292 node_alloc_noretry = NULL;
2293 }
2294
2295 /* bit mask controlling how hard we retry per-node allocations */
2296 if (node_alloc_noretry)
2297 nodes_clear(*node_alloc_noretry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Andi Kleene5ff2152008-07-23 21:27:42 -07002299 for (i = 0; i < h->max_huge_pages; ++i) {
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002300 if (hstate_is_gigantic(h)) {
Andi Kleenaa888a72008-07-23 21:27:47 -07002301 if (!alloc_bootmem_huge_page(h))
2302 break;
Michal Hocko0c397da2018-01-31 16:20:56 -08002303 } else if (!alloc_pool_huge_page(h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07002304 &node_states[N_MEMORY],
2305 node_alloc_noretry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 break;
David Rientjes69ed7792017-07-10 15:48:50 -07002307 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
Liam R. Howlettd715cf82017-07-10 15:48:15 -07002309 if (i < h->max_huge_pages) {
2310 char buf[32];
2311
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07002312 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Liam R. Howlettd715cf82017-07-10 15:48:15 -07002313 pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n",
2314 h->max_huge_pages, buf, i);
2315 h->max_huge_pages = i;
2316 }
Mike Kravetzf60858f2019-09-23 15:37:35 -07002317
2318 kfree(node_alloc_noretry);
Andi Kleene5ff2152008-07-23 21:27:42 -07002319}
2320
2321static void __init hugetlb_init_hstates(void)
2322{
2323 struct hstate *h;
2324
2325 for_each_hstate(h) {
Naoya Horiguchi641844f2015-06-24 16:56:59 -07002326 if (minimum_order > huge_page_order(h))
2327 minimum_order = huge_page_order(h);
2328
Andi Kleen8faa8b02008-07-23 21:27:48 -07002329 /* oversize hugepages were init'ed in early boot */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002330 if (!hstate_is_gigantic(h))
Andi Kleen8faa8b02008-07-23 21:27:48 -07002331 hugetlb_hstate_alloc_pages(h);
Andi Kleene5ff2152008-07-23 21:27:42 -07002332 }
Naoya Horiguchi641844f2015-06-24 16:56:59 -07002333 VM_BUG_ON(minimum_order == UINT_MAX);
Andi Kleene5ff2152008-07-23 21:27:42 -07002334}
2335
2336static void __init report_hugepages(void)
2337{
2338 struct hstate *h;
2339
2340 for_each_hstate(h) {
Andi Kleen4abd32d2008-07-23 21:27:49 -07002341 char buf[32];
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07002342
2343 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Andrew Mortonffb22af2013-02-22 16:32:08 -08002344 pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07002345 buf, h->free_huge_pages);
Andi Kleene5ff2152008-07-23 21:27:42 -07002346 }
2347}
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349#ifdef CONFIG_HIGHMEM
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002350static void try_to_free_low(struct hstate *h, unsigned long count,
2351 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
Christoph Lameter4415cc82006-09-25 23:31:55 -07002353 int i;
2354
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002355 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07002356 return;
2357
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002358 for_each_node_mask(i, *nodes_allowed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -07002360 struct list_head *freel = &h->hugepage_freelists[i];
2361 list_for_each_entry_safe(page, next, freel, lru) {
2362 if (count >= h->nr_huge_pages)
Adam Litke6b0c8802007-10-16 01:26:23 -07002363 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (PageHighMem(page))
2365 continue;
2366 list_del(&page->lru);
Andi Kleene5ff2152008-07-23 21:27:42 -07002367 update_and_free_page(h, page);
Andi Kleena5516432008-07-23 21:27:41 -07002368 h->free_huge_pages--;
2369 h->free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 }
2372}
2373#else
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002374static inline void try_to_free_low(struct hstate *h, unsigned long count,
2375 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377}
2378#endif
2379
Wu Fengguang20a03072009-06-16 15:32:22 -07002380/*
2381 * Increment or decrement surplus_huge_pages. Keep node-specific counters
2382 * balanced by operating on them in a round-robin fashion.
2383 * Returns 1 if an adjustment was made.
2384 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002385static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
2386 int delta)
Wu Fengguang20a03072009-06-16 15:32:22 -07002387{
Joonsoo Kimb2261022013-09-11 14:21:00 -07002388 int nr_nodes, node;
Wu Fengguang20a03072009-06-16 15:32:22 -07002389
2390 VM_BUG_ON(delta != -1 && delta != 1);
Wu Fengguang20a03072009-06-16 15:32:22 -07002391
Joonsoo Kimb2261022013-09-11 14:21:00 -07002392 if (delta < 0) {
2393 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
2394 if (h->surplus_huge_pages_node[node])
2395 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002396 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002397 } else {
2398 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
2399 if (h->surplus_huge_pages_node[node] <
2400 h->nr_huge_pages_node[node])
2401 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002402 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002403 }
2404 return 0;
Wu Fengguang20a03072009-06-16 15:32:22 -07002405
Joonsoo Kimb2261022013-09-11 14:21:00 -07002406found:
2407 h->surplus_huge_pages += delta;
2408 h->surplus_huge_pages_node[node] += delta;
2409 return 1;
Wu Fengguang20a03072009-06-16 15:32:22 -07002410}
2411
Andi Kleena5516432008-07-23 21:27:41 -07002412#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002413static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002414 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Adam Litke7893d1d2007-10-16 01:26:18 -07002416 unsigned long min_count, ret;
Mike Kravetzf60858f2019-09-23 15:37:35 -07002417 NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL);
2418
2419 /*
2420 * Bit mask controlling how hard we retry per-node allocations.
2421 * If we can not allocate the bit mask, do not attempt to allocate
2422 * the requested huge pages.
2423 */
2424 if (node_alloc_noretry)
2425 nodes_clear(*node_alloc_noretry);
2426 else
2427 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002429 spin_lock(&hugetlb_lock);
2430
2431 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002432 * Check for a node specific request.
2433 * Changing node specific huge page count may require a corresponding
2434 * change to the global count. In any case, the passed node mask
2435 * (nodes_allowed) will restrict alloc/free to the specified node.
2436 */
2437 if (nid != NUMA_NO_NODE) {
2438 unsigned long old_count = count;
2439
2440 count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
2441 /*
2442 * User may have specified a large count value which caused the
2443 * above calculation to overflow. In this case, they wanted
2444 * to allocate as many huge pages as possible. Set count to
2445 * largest possible value to align with their intention.
2446 */
2447 if (count < old_count)
2448 count = ULONG_MAX;
2449 }
2450
2451 /*
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002452 * Gigantic pages runtime allocation depend on the capability for large
2453 * page range allocation.
2454 * If the system does not provide this feature, return an error when
2455 * the user tries to allocate gigantic pages but let the user free the
2456 * boottime allocated gigantic pages.
2457 */
2458 if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) {
2459 if (count > persistent_huge_pages(h)) {
2460 spin_unlock(&hugetlb_lock);
Mike Kravetzf60858f2019-09-23 15:37:35 -07002461 NODEMASK_FREE(node_alloc_noretry);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002462 return -EINVAL;
2463 }
2464 /* Fall through to decrease pool */
2465 }
Andi Kleenaa888a72008-07-23 21:27:47 -07002466
Adam Litke7893d1d2007-10-16 01:26:18 -07002467 /*
2468 * Increase the pool size
2469 * First take pages out of surplus state. Then make up the
2470 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002471 *
Michal Hocko0c397da2018-01-31 16:20:56 -08002472 * We might race with alloc_surplus_huge_page() here and be unable
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002473 * to convert a surplus huge page to a normal huge page. That is
2474 * not critical, though, it just means the overall size of the
2475 * pool might be one hugepage larger than it needs to be, but
2476 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -07002477 */
Andi Kleena5516432008-07-23 21:27:41 -07002478 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002479 if (!adjust_pool_surplus(h, nodes_allowed, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -07002480 break;
2481 }
2482
Andi Kleena5516432008-07-23 21:27:41 -07002483 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -07002484 /*
2485 * If this allocation races such that we no longer need the
2486 * page, free_huge_page will handle it by freeing the page
2487 * and reducing the surplus.
2488 */
2489 spin_unlock(&hugetlb_lock);
Jia He649920c2016-08-02 14:02:31 -07002490
2491 /* yield cpu to avoid soft lockup */
2492 cond_resched();
2493
Mike Kravetzf60858f2019-09-23 15:37:35 -07002494 ret = alloc_pool_huge_page(h, nodes_allowed,
2495 node_alloc_noretry);
Adam Litke7893d1d2007-10-16 01:26:18 -07002496 spin_lock(&hugetlb_lock);
2497 if (!ret)
2498 goto out;
2499
Mel Gorman536240f22009-12-14 17:59:56 -08002500 /* Bail for signals. Probably ctrl-c from user */
2501 if (signal_pending(current))
2502 goto out;
Adam Litke7893d1d2007-10-16 01:26:18 -07002503 }
Adam Litke7893d1d2007-10-16 01:26:18 -07002504
2505 /*
2506 * Decrease the pool size
2507 * First return free pages to the buddy allocator (being careful
2508 * to keep enough around to satisfy reservations). Then place
2509 * pages into surplus state as needed so the pool will shrink
2510 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002511 *
2512 * By placing pages into the surplus state independent of the
2513 * overcommit value, we are allowing the surplus pool size to
2514 * exceed overcommit. There are few sane options here. Since
Michal Hocko0c397da2018-01-31 16:20:56 -08002515 * alloc_surplus_huge_page() is checking the global counter,
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002516 * though, we'll note that we're not allowed to exceed surplus
2517 * and won't grow the pool anywhere else. Not until one of the
2518 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07002519 */
Andi Kleena5516432008-07-23 21:27:41 -07002520 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07002521 min_count = max(count, min_count);
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002522 try_to_free_low(h, min_count, nodes_allowed);
Andi Kleena5516432008-07-23 21:27:41 -07002523 while (min_count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002524 if (!free_pool_huge_page(h, nodes_allowed, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 break;
Mizuma, Masayoshi55f67142014-04-07 15:37:54 -07002526 cond_resched_lock(&hugetlb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
Andi Kleena5516432008-07-23 21:27:41 -07002528 while (count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002529 if (!adjust_pool_surplus(h, nodes_allowed, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07002530 break;
2531 }
2532out:
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002533 h->max_huge_pages = persistent_huge_pages(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 spin_unlock(&hugetlb_lock);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002535
Mike Kravetzf60858f2019-09-23 15:37:35 -07002536 NODEMASK_FREE(node_alloc_noretry);
2537
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002541#define HSTATE_ATTR_RO(_name) \
2542 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
2543
2544#define HSTATE_ATTR(_name) \
2545 static struct kobj_attribute _name##_attr = \
2546 __ATTR(_name, 0644, _name##_show, _name##_store)
2547
2548static struct kobject *hugepages_kobj;
2549static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
2550
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002551static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
2552
2553static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002554{
2555 int i;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002556
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002557 for (i = 0; i < HUGE_MAX_HSTATE; i++)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002558 if (hstate_kobjs[i] == kobj) {
2559 if (nidp)
2560 *nidp = NUMA_NO_NODE;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002561 return &hstates[i];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002562 }
2563
2564 return kobj_to_node_hstate(kobj, nidp);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002565}
2566
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002567static ssize_t nr_hugepages_show_common(struct kobject *kobj,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002568 struct kobj_attribute *attr, char *buf)
2569{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002570 struct hstate *h;
2571 unsigned long nr_huge_pages;
2572 int nid;
2573
2574 h = kobj_to_hstate(kobj, &nid);
2575 if (nid == NUMA_NO_NODE)
2576 nr_huge_pages = h->nr_huge_pages;
2577 else
2578 nr_huge_pages = h->nr_huge_pages_node[nid];
2579
2580 return sprintf(buf, "%lu\n", nr_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002581}
Eric B Munsonadbe8722011-01-13 15:47:27 -08002582
David Rientjes238d3c12014-08-06 16:06:51 -07002583static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
2584 struct hstate *h, int nid,
2585 unsigned long count, size_t len)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002586{
2587 int err;
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002588 nodemask_t nodes_allowed, *n_mask;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002589
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002590 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
2591 return -EINVAL;
Eric B Munsonadbe8722011-01-13 15:47:27 -08002592
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002593 if (nid == NUMA_NO_NODE) {
2594 /*
2595 * global hstate attribute
2596 */
2597 if (!(obey_mempolicy &&
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002598 init_nodemask_of_mempolicy(&nodes_allowed)))
2599 n_mask = &node_states[N_MEMORY];
2600 else
2601 n_mask = &nodes_allowed;
2602 } else {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002603 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002604 * Node specific request. count adjustment happens in
2605 * set_max_huge_pages() after acquiring hugetlb_lock.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002606 */
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002607 init_nodemask_of_node(&nodes_allowed, nid);
2608 n_mask = &nodes_allowed;
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002609 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002610
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002611 err = set_max_huge_pages(h, count, nid, n_mask);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002612
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002613 return err ? err : len;
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002614}
2615
David Rientjes238d3c12014-08-06 16:06:51 -07002616static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
2617 struct kobject *kobj, const char *buf,
2618 size_t len)
2619{
2620 struct hstate *h;
2621 unsigned long count;
2622 int nid;
2623 int err;
2624
2625 err = kstrtoul(buf, 10, &count);
2626 if (err)
2627 return err;
2628
2629 h = kobj_to_hstate(kobj, &nid);
2630 return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
2631}
2632
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002633static ssize_t nr_hugepages_show(struct kobject *kobj,
2634 struct kobj_attribute *attr, char *buf)
2635{
2636 return nr_hugepages_show_common(kobj, attr, buf);
2637}
2638
2639static ssize_t nr_hugepages_store(struct kobject *kobj,
2640 struct kobj_attribute *attr, const char *buf, size_t len)
2641{
David Rientjes238d3c12014-08-06 16:06:51 -07002642 return nr_hugepages_store_common(false, kobj, buf, len);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002643}
2644HSTATE_ATTR(nr_hugepages);
2645
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002646#ifdef CONFIG_NUMA
2647
2648/*
2649 * hstate attribute for optionally mempolicy-based constraint on persistent
2650 * huge page alloc/free.
2651 */
2652static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
2653 struct kobj_attribute *attr, char *buf)
2654{
2655 return nr_hugepages_show_common(kobj, attr, buf);
2656}
2657
2658static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
2659 struct kobj_attribute *attr, const char *buf, size_t len)
2660{
David Rientjes238d3c12014-08-06 16:06:51 -07002661 return nr_hugepages_store_common(true, kobj, buf, len);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002662}
2663HSTATE_ATTR(nr_hugepages_mempolicy);
2664#endif
2665
2666
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002667static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
2668 struct kobj_attribute *attr, char *buf)
2669{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002670 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002671 return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
2672}
Eric B Munsonadbe8722011-01-13 15:47:27 -08002673
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002674static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
2675 struct kobj_attribute *attr, const char *buf, size_t count)
2676{
2677 int err;
2678 unsigned long input;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002679 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002680
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002681 if (hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08002682 return -EINVAL;
2683
Jingoo Han3dbb95f2013-09-11 14:20:25 -07002684 err = kstrtoul(buf, 10, &input);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002685 if (err)
Eric B Munson73ae31e2011-01-13 15:47:28 -08002686 return err;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002687
2688 spin_lock(&hugetlb_lock);
2689 h->nr_overcommit_huge_pages = input;
2690 spin_unlock(&hugetlb_lock);
2691
2692 return count;
2693}
2694HSTATE_ATTR(nr_overcommit_hugepages);
2695
2696static ssize_t free_hugepages_show(struct kobject *kobj,
2697 struct kobj_attribute *attr, char *buf)
2698{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002699 struct hstate *h;
2700 unsigned long free_huge_pages;
2701 int nid;
2702
2703 h = kobj_to_hstate(kobj, &nid);
2704 if (nid == NUMA_NO_NODE)
2705 free_huge_pages = h->free_huge_pages;
2706 else
2707 free_huge_pages = h->free_huge_pages_node[nid];
2708
2709 return sprintf(buf, "%lu\n", free_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002710}
2711HSTATE_ATTR_RO(free_hugepages);
2712
2713static ssize_t resv_hugepages_show(struct kobject *kobj,
2714 struct kobj_attribute *attr, char *buf)
2715{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002716 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002717 return sprintf(buf, "%lu\n", h->resv_huge_pages);
2718}
2719HSTATE_ATTR_RO(resv_hugepages);
2720
2721static ssize_t surplus_hugepages_show(struct kobject *kobj,
2722 struct kobj_attribute *attr, char *buf)
2723{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002724 struct hstate *h;
2725 unsigned long surplus_huge_pages;
2726 int nid;
2727
2728 h = kobj_to_hstate(kobj, &nid);
2729 if (nid == NUMA_NO_NODE)
2730 surplus_huge_pages = h->surplus_huge_pages;
2731 else
2732 surplus_huge_pages = h->surplus_huge_pages_node[nid];
2733
2734 return sprintf(buf, "%lu\n", surplus_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002735}
2736HSTATE_ATTR_RO(surplus_hugepages);
2737
2738static struct attribute *hstate_attrs[] = {
2739 &nr_hugepages_attr.attr,
2740 &nr_overcommit_hugepages_attr.attr,
2741 &free_hugepages_attr.attr,
2742 &resv_hugepages_attr.attr,
2743 &surplus_hugepages_attr.attr,
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002744#ifdef CONFIG_NUMA
2745 &nr_hugepages_mempolicy_attr.attr,
2746#endif
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002747 NULL,
2748};
2749
Arvind Yadav67e5ed92017-09-06 16:22:06 -07002750static const struct attribute_group hstate_attr_group = {
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002751 .attrs = hstate_attrs,
2752};
2753
Jeff Mahoney094e9532010-02-02 13:44:14 -08002754static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
2755 struct kobject **hstate_kobjs,
Arvind Yadav67e5ed92017-09-06 16:22:06 -07002756 const struct attribute_group *hstate_attr_group)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002757{
2758 int retval;
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002759 int hi = hstate_index(h);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002760
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002761 hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
2762 if (!hstate_kobjs[hi])
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002763 return -ENOMEM;
2764
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002765 retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002766 if (retval)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002767 kobject_put(hstate_kobjs[hi]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002768
2769 return retval;
2770}
2771
2772static void __init hugetlb_sysfs_init(void)
2773{
2774 struct hstate *h;
2775 int err;
2776
2777 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
2778 if (!hugepages_kobj)
2779 return;
2780
2781 for_each_hstate(h) {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002782 err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
2783 hstate_kobjs, &hstate_attr_group);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002784 if (err)
Andrew Mortonffb22af2013-02-22 16:32:08 -08002785 pr_err("Hugetlb: Unable to add hstate %s", h->name);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002786 }
2787}
2788
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002789#ifdef CONFIG_NUMA
2790
2791/*
2792 * node_hstate/s - associate per node hstate attributes, via their kobjects,
Kay Sievers10fbcf42011-12-21 14:48:43 -08002793 * with node devices in node_devices[] using a parallel array. The array
2794 * index of a node device or _hstate == node id.
2795 * This is here to avoid any static dependency of the node device driver, in
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002796 * the base kernel, on the hugetlb module.
2797 */
2798struct node_hstate {
2799 struct kobject *hugepages_kobj;
2800 struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
2801};
Alexander Kuleshovb4e289a2015-11-05 18:50:14 -08002802static struct node_hstate node_hstates[MAX_NUMNODES];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002803
2804/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002805 * A subset of global hstate attributes for node devices
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002806 */
2807static struct attribute *per_node_hstate_attrs[] = {
2808 &nr_hugepages_attr.attr,
2809 &free_hugepages_attr.attr,
2810 &surplus_hugepages_attr.attr,
2811 NULL,
2812};
2813
Arvind Yadav67e5ed92017-09-06 16:22:06 -07002814static const struct attribute_group per_node_hstate_attr_group = {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002815 .attrs = per_node_hstate_attrs,
2816};
2817
2818/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002819 * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002820 * Returns node id via non-NULL nidp.
2821 */
2822static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
2823{
2824 int nid;
2825
2826 for (nid = 0; nid < nr_node_ids; nid++) {
2827 struct node_hstate *nhs = &node_hstates[nid];
2828 int i;
2829 for (i = 0; i < HUGE_MAX_HSTATE; i++)
2830 if (nhs->hstate_kobjs[i] == kobj) {
2831 if (nidp)
2832 *nidp = nid;
2833 return &hstates[i];
2834 }
2835 }
2836
2837 BUG();
2838 return NULL;
2839}
2840
2841/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002842 * Unregister hstate attributes from a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002843 * No-op if no hstate attributes attached.
2844 */
Claudiu Ghioc3cd8b442013-03-04 12:46:15 +02002845static void hugetlb_unregister_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002846{
2847 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08002848 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002849
2850 if (!nhs->hugepages_kobj)
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002851 return; /* no hstate attributes */
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002852
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002853 for_each_hstate(h) {
2854 int idx = hstate_index(h);
2855 if (nhs->hstate_kobjs[idx]) {
2856 kobject_put(nhs->hstate_kobjs[idx]);
2857 nhs->hstate_kobjs[idx] = NULL;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002858 }
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002859 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002860
2861 kobject_put(nhs->hugepages_kobj);
2862 nhs->hugepages_kobj = NULL;
2863}
2864
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002865
2866/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002867 * Register hstate attributes for a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002868 * No-op if attributes already registered.
2869 */
Claudiu Ghioc3cd8b442013-03-04 12:46:15 +02002870static void hugetlb_register_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002871{
2872 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08002873 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002874 int err;
2875
2876 if (nhs->hugepages_kobj)
2877 return; /* already allocated */
2878
2879 nhs->hugepages_kobj = kobject_create_and_add("hugepages",
Kay Sievers10fbcf42011-12-21 14:48:43 -08002880 &node->dev.kobj);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002881 if (!nhs->hugepages_kobj)
2882 return;
2883
2884 for_each_hstate(h) {
2885 err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
2886 nhs->hstate_kobjs,
2887 &per_node_hstate_attr_group);
2888 if (err) {
Andrew Mortonffb22af2013-02-22 16:32:08 -08002889 pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
2890 h->name, node->dev.id);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002891 hugetlb_unregister_node(node);
2892 break;
2893 }
2894 }
2895}
2896
2897/*
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002898 * hugetlb init time: register hstate attributes for all registered node
Kay Sievers10fbcf42011-12-21 14:48:43 -08002899 * devices of nodes that have memory. All on-line nodes should have
2900 * registered their associated device by this time.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002901 */
Luiz Capitulino7d9ca002014-12-12 16:55:24 -08002902static void __init hugetlb_register_all_nodes(void)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002903{
2904 int nid;
2905
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08002906 for_each_node_state(nid, N_MEMORY) {
Wen Congyang87327942012-12-11 16:00:56 -08002907 struct node *node = node_devices[nid];
Kay Sievers10fbcf42011-12-21 14:48:43 -08002908 if (node->dev.id == nid)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002909 hugetlb_register_node(node);
2910 }
2911
2912 /*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002913 * Let the node device driver know we're here so it can
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002914 * [un]register hstate attributes on node hotplug.
2915 */
2916 register_hugetlbfs_with_node(hugetlb_register_node,
2917 hugetlb_unregister_node);
2918}
2919#else /* !CONFIG_NUMA */
2920
2921static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
2922{
2923 BUG();
2924 if (nidp)
2925 *nidp = -1;
2926 return NULL;
2927}
2928
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002929static void hugetlb_register_all_nodes(void) { }
2930
2931#endif
2932
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002933static int __init hugetlb_init(void)
2934{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07002935 int i;
2936
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002937 if (!hugepages_supported())
Benjamin Herrenschmidt0ef89d22008-07-31 00:07:30 -07002938 return 0;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002939
Nick Piggine11bfbf2008-07-23 21:27:52 -07002940 if (!size_to_hstate(default_hstate_size)) {
Liam R. Howlettd715cf82017-07-10 15:48:15 -07002941 if (default_hstate_size != 0) {
2942 pr_err("HugeTLB: unsupported default_hugepagesz %lu. Reverting to %lu\n",
2943 default_hstate_size, HPAGE_SIZE);
2944 }
2945
Nick Piggine11bfbf2008-07-23 21:27:52 -07002946 default_hstate_size = HPAGE_SIZE;
2947 if (!size_to_hstate(default_hstate_size))
2948 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002949 }
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002950 default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
Vaishali Thakkarf8b74812016-02-17 13:11:26 -08002951 if (default_hstate_max_huge_pages) {
2952 if (!default_hstate.max_huge_pages)
2953 default_hstate.max_huge_pages = default_hstate_max_huge_pages;
2954 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002955
2956 hugetlb_init_hstates();
Andi Kleenaa888a72008-07-23 21:27:47 -07002957 gather_bootmem_prealloc();
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002958 report_hugepages();
2959
2960 hugetlb_sysfs_init();
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002961 hugetlb_register_all_nodes();
Jianguo Wu7179e7b2012-12-18 14:23:19 -08002962 hugetlb_cgroup_file_init();
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002963
Davidlohr Bueso8382d912014-04-03 14:47:31 -07002964#ifdef CONFIG_SMP
2965 num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
2966#else
2967 num_fault_mutexes = 1;
2968#endif
Mike Kravetzc672c7f2015-09-08 15:01:35 -07002969 hugetlb_fault_mutex_table =
Kees Cook6da2ec52018-06-12 13:55:00 -07002970 kmalloc_array(num_fault_mutexes, sizeof(struct mutex),
2971 GFP_KERNEL);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07002972 BUG_ON(!hugetlb_fault_mutex_table);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07002973
2974 for (i = 0; i < num_fault_mutexes; i++)
Mike Kravetzc672c7f2015-09-08 15:01:35 -07002975 mutex_init(&hugetlb_fault_mutex_table[i]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002976 return 0;
2977}
Paul Gortmaker3e89e1c2016-01-14 15:21:52 -08002978subsys_initcall(hugetlb_init);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002979
2980/* Should be called on processing a hugepagesz=... option */
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07002981void __init hugetlb_bad_size(void)
2982{
2983 parsed_valid_hugepagesz = false;
2984}
2985
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08002986void __init hugetlb_add_hstate(unsigned int order)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002987{
2988 struct hstate *h;
Andi Kleen8faa8b02008-07-23 21:27:48 -07002989 unsigned long i;
2990
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002991 if (size_to_hstate(PAGE_SIZE << order)) {
Joe Perches598d8092016-03-17 14:19:44 -07002992 pr_warn("hugepagesz= specified twice, ignoring\n");
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002993 return;
2994 }
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002995 BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002996 BUG_ON(order == 0);
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002997 h = &hstates[hugetlb_max_hstate++];
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002998 h->order = order;
2999 h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
Andi Kleen8faa8b02008-07-23 21:27:48 -07003000 h->nr_huge_pages = 0;
3001 h->free_huge_pages = 0;
3002 for (i = 0; i < MAX_NUMNODES; ++i)
3003 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07003004 INIT_LIST_HEAD(&h->hugepage_activelist);
Andrew Morton54f18d32016-05-19 17:11:40 -07003005 h->next_nid_to_alloc = first_memory_node;
3006 h->next_nid_to_free = first_memory_node;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003007 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
3008 huge_page_size(h)/1024);
Andi Kleen8faa8b02008-07-23 21:27:48 -07003009
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003010 parsed_hstate = h;
3011}
3012
Nick Piggine11bfbf2008-07-23 21:27:52 -07003013static int __init hugetlb_nrpages_setup(char *s)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003014{
3015 unsigned long *mhp;
Andi Kleen8faa8b02008-07-23 21:27:48 -07003016 static unsigned long *last_mhp;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003017
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07003018 if (!parsed_valid_hugepagesz) {
3019 pr_warn("hugepages = %s preceded by "
3020 "an unsupported hugepagesz, ignoring\n", s);
3021 parsed_valid_hugepagesz = true;
3022 return 1;
3023 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003024 /*
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07003025 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003026 * so this hugepages= parameter goes to the "default hstate".
3027 */
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07003028 else if (!hugetlb_max_hstate)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003029 mhp = &default_hstate_max_huge_pages;
3030 else
3031 mhp = &parsed_hstate->max_huge_pages;
3032
Andi Kleen8faa8b02008-07-23 21:27:48 -07003033 if (mhp == last_mhp) {
Joe Perches598d8092016-03-17 14:19:44 -07003034 pr_warn("hugepages= specified twice without interleaving hugepagesz=, ignoring\n");
Andi Kleen8faa8b02008-07-23 21:27:48 -07003035 return 1;
3036 }
3037
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003038 if (sscanf(s, "%lu", mhp) <= 0)
3039 *mhp = 0;
3040
Andi Kleen8faa8b02008-07-23 21:27:48 -07003041 /*
3042 * Global state is always initialized later in hugetlb_init.
3043 * But we need to allocate >= MAX_ORDER hstates here early to still
3044 * use the bootmem allocator.
3045 */
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07003046 if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
Andi Kleen8faa8b02008-07-23 21:27:48 -07003047 hugetlb_hstate_alloc_pages(parsed_hstate);
3048
3049 last_mhp = mhp;
3050
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003051 return 1;
3052}
Nick Piggine11bfbf2008-07-23 21:27:52 -07003053__setup("hugepages=", hugetlb_nrpages_setup);
3054
3055static int __init hugetlb_default_setup(char *s)
3056{
3057 default_hstate_size = memparse(s, &s);
3058 return 1;
3059}
3060__setup("default_hugepagesz=", hugetlb_default_setup);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003061
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07003062static unsigned int cpuset_mems_nr(unsigned int *array)
3063{
3064 int node;
3065 unsigned int nr = 0;
3066
3067 for_each_node_mask(node, cpuset_current_mems_allowed)
3068 nr += array[node];
3069
3070 return nr;
3071}
3072
3073#ifdef CONFIG_SYSCTL
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003074static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
3075 struct ctl_table *table, int write,
3076 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Andi Kleene5ff2152008-07-23 21:27:42 -07003078 struct hstate *h = &default_hstate;
David Rientjes238d3c12014-08-06 16:06:51 -07003079 unsigned long tmp = h->max_huge_pages;
Michal Hocko08d4a242011-01-13 15:47:26 -08003080 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07003081
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003082 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08003083 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003084
Andi Kleene5ff2152008-07-23 21:27:42 -07003085 table->data = &tmp;
3086 table->maxlen = sizeof(unsigned long);
Michal Hocko08d4a242011-01-13 15:47:26 -08003087 ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
3088 if (ret)
3089 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07003090
David Rientjes238d3c12014-08-06 16:06:51 -07003091 if (write)
3092 ret = __nr_hugepages_store_common(obey_mempolicy, h,
3093 NUMA_NO_NODE, tmp, *length);
Michal Hocko08d4a242011-01-13 15:47:26 -08003094out:
3095 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
Mel Gorman396faf02007-07-17 04:03:13 -07003097
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003098int hugetlb_sysctl_handler(struct ctl_table *table, int write,
3099 void __user *buffer, size_t *length, loff_t *ppos)
3100{
3101
3102 return hugetlb_sysctl_handler_common(false, table, write,
3103 buffer, length, ppos);
3104}
3105
3106#ifdef CONFIG_NUMA
3107int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
3108 void __user *buffer, size_t *length, loff_t *ppos)
3109{
3110 return hugetlb_sysctl_handler_common(true, table, write,
3111 buffer, length, ppos);
3112}
3113#endif /* CONFIG_NUMA */
3114
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08003115int hugetlb_overcommit_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003116 void __user *buffer,
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08003117 size_t *length, loff_t *ppos)
3118{
Andi Kleena5516432008-07-23 21:27:41 -07003119 struct hstate *h = &default_hstate;
Andi Kleene5ff2152008-07-23 21:27:42 -07003120 unsigned long tmp;
Michal Hocko08d4a242011-01-13 15:47:26 -08003121 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07003122
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003123 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08003124 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003125
Petr Holasekc033a932011-03-22 16:33:05 -07003126 tmp = h->nr_overcommit_huge_pages;
Andi Kleene5ff2152008-07-23 21:27:42 -07003127
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003128 if (write && hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08003129 return -EINVAL;
3130
Andi Kleene5ff2152008-07-23 21:27:42 -07003131 table->data = &tmp;
3132 table->maxlen = sizeof(unsigned long);
Michal Hocko08d4a242011-01-13 15:47:26 -08003133 ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
3134 if (ret)
3135 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07003136
3137 if (write) {
3138 spin_lock(&hugetlb_lock);
3139 h->nr_overcommit_huge_pages = tmp;
3140 spin_unlock(&hugetlb_lock);
3141 }
Michal Hocko08d4a242011-01-13 15:47:26 -08003142out:
3143 return ret;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08003144}
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146#endif /* CONFIG_SYSCTL */
3147
Alexey Dobriyane1759c22008-10-15 23:50:22 +04003148void hugetlb_report_meminfo(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08003150 struct hstate *h;
3151 unsigned long total = 0;
3152
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003153 if (!hugepages_supported())
3154 return;
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08003155
3156 for_each_hstate(h) {
3157 unsigned long count = h->nr_huge_pages;
3158
3159 total += (PAGE_SIZE << huge_page_order(h)) * count;
3160
3161 if (h == &default_hstate)
3162 seq_printf(m,
3163 "HugePages_Total: %5lu\n"
3164 "HugePages_Free: %5lu\n"
3165 "HugePages_Rsvd: %5lu\n"
3166 "HugePages_Surp: %5lu\n"
3167 "Hugepagesize: %8lu kB\n",
3168 count,
3169 h->free_huge_pages,
3170 h->resv_huge_pages,
3171 h->surplus_huge_pages,
3172 (PAGE_SIZE << huge_page_order(h)) / 1024);
3173 }
3174
3175 seq_printf(m, "Hugetlb: %8lu kB\n", total / 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177
3178int hugetlb_report_node_meminfo(int nid, char *buf)
3179{
Andi Kleena5516432008-07-23 21:27:41 -07003180 struct hstate *h = &default_hstate;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003181 if (!hugepages_supported())
3182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 return sprintf(buf,
3184 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07003185 "Node %d HugePages_Free: %5u\n"
3186 "Node %d HugePages_Surp: %5u\n",
Andi Kleena5516432008-07-23 21:27:41 -07003187 nid, h->nr_huge_pages_node[nid],
3188 nid, h->free_huge_pages_node[nid],
3189 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
David Rientjes949f7ec2013-04-29 15:07:48 -07003192void hugetlb_show_meminfo(void)
3193{
3194 struct hstate *h;
3195 int nid;
3196
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003197 if (!hugepages_supported())
3198 return;
3199
David Rientjes949f7ec2013-04-29 15:07:48 -07003200 for_each_node_state(nid, N_MEMORY)
3201 for_each_hstate(h)
3202 pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
3203 nid,
3204 h->nr_huge_pages_node[nid],
3205 h->free_huge_pages_node[nid],
3206 h->surplus_huge_pages_node[nid],
3207 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
3208}
3209
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003210void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
3211{
3212 seq_printf(m, "HugetlbPages:\t%8lu kB\n",
3213 atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10));
3214}
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
3217unsigned long hugetlb_total_pages(void)
3218{
Wanpeng Lid0028582013-03-22 15:04:40 -07003219 struct hstate *h;
3220 unsigned long nr_total_pages = 0;
3221
3222 for_each_hstate(h)
3223 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
3224 return nr_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Andi Kleena5516432008-07-23 21:27:41 -07003227static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003228{
3229 int ret = -ENOMEM;
3230
3231 spin_lock(&hugetlb_lock);
3232 /*
3233 * When cpuset is configured, it breaks the strict hugetlb page
3234 * reservation as the accounting is done on a global variable. Such
3235 * reservation is completely rubbish in the presence of cpuset because
3236 * the reservation is not checked against page availability for the
3237 * current cpuset. Application can still potentially OOM'ed by kernel
3238 * with lack of free htlb page in cpuset that the task is in.
3239 * Attempt to enforce strict accounting with cpuset is almost
3240 * impossible (or too ugly) because cpuset is too fluid that
3241 * task or memory node can be dynamically moved between cpusets.
3242 *
3243 * The change of semantics for shared hugetlb mapping with cpuset is
3244 * undesirable. However, in order to preserve some of the semantics,
3245 * we fall back to check against current free page availability as
3246 * a best attempt and hopefully to minimize the impact of changing
3247 * semantics that cpuset has.
3248 */
3249 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07003250 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003251 goto out;
3252
Andi Kleena5516432008-07-23 21:27:41 -07003253 if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
3254 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003255 goto out;
3256 }
3257 }
3258
3259 ret = 0;
3260 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07003261 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003262
3263out:
3264 spin_unlock(&hugetlb_lock);
3265 return ret;
3266}
3267
Andy Whitcroft84afd992008-07-23 21:27:32 -07003268static void hugetlb_vm_op_open(struct vm_area_struct *vma)
3269{
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07003270 struct resv_map *resv = vma_resv_map(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003271
3272 /*
3273 * This new VMA should share its siblings reservation map if present.
3274 * The VMA will only ever have a valid reservation map pointer where
3275 * it is being copied for another still existing VMA. As that VMA
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003276 * has a reference to the reservation map it cannot disappear until
Andy Whitcroft84afd992008-07-23 21:27:32 -07003277 * after this open call completes. It is therefore safe to take a
3278 * new reference here without additional locking.
3279 */
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003280 if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07003281 kref_get(&resv->refs);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003282}
3283
Mel Gormana1e78772008-07-23 21:27:23 -07003284static void hugetlb_vm_op_close(struct vm_area_struct *vma)
3285{
Andi Kleena5516432008-07-23 21:27:41 -07003286 struct hstate *h = hstate_vma(vma);
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07003287 struct resv_map *resv = vma_resv_map(vma);
David Gibson90481622012-03-21 16:34:12 -07003288 struct hugepage_subpool *spool = subpool_vma(vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003289 unsigned long reserve, start, end;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003290 long gbl_reserve;
Andy Whitcroft84afd992008-07-23 21:27:32 -07003291
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003292 if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
3293 return;
Andy Whitcroft84afd992008-07-23 21:27:32 -07003294
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003295 start = vma_hugecache_offset(h, vma, vma->vm_start);
3296 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003297
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003298 reserve = (end - start) - region_count(resv, start, end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003299
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003300 kref_put(&resv->refs, resv_map_release);
3301
3302 if (reserve) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003303 /*
3304 * Decrement reserve counts. The global reserve count may be
3305 * adjusted if the subpool has a minimum size.
3306 */
3307 gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
3308 hugetlb_acct_memory(h, -gbl_reserve);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003309 }
Mel Gormana1e78772008-07-23 21:27:23 -07003310}
3311
Dan Williams31383c62017-11-29 16:10:28 -08003312static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
3313{
3314 if (addr & ~(huge_page_mask(hstate_vma(vma))))
3315 return -EINVAL;
3316 return 0;
3317}
3318
Dan Williams05ea8862018-04-05 16:24:25 -07003319static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
3320{
3321 struct hstate *hstate = hstate_vma(vma);
3322
3323 return 1UL << huge_page_shift(hstate);
3324}
3325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326/*
3327 * We cannot handle pagefaults against hugetlb pages at all. They cause
3328 * handle_mm_fault() to try to instantiate regular-sized pages in the
3329 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
3330 * this far.
3331 */
Souptick Joarderb3ec9f32018-06-07 17:08:04 -07003332static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07003335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336}
3337
Jane Chueec36362018-08-02 15:36:05 -07003338/*
3339 * When a new function is introduced to vm_operations_struct and added
3340 * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
3341 * This is because under System V memory model, mappings created via
3342 * shmget/shmat with "huge page" specified are backed by hugetlbfs files,
3343 * their original vm_ops are overwritten with shm_vm_ops.
3344 */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003345const struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07003346 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07003347 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07003348 .close = hugetlb_vm_op_close,
Dan Williams31383c62017-11-29 16:10:28 -08003349 .split = hugetlb_vm_op_split,
Dan Williams05ea8862018-04-05 16:24:25 -07003350 .pagesize = hugetlb_vm_op_pagesize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351};
3352
David Gibson1e8f8892006-01-06 00:10:44 -08003353static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
3354 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07003355{
3356 pte_t entry;
3357
David Gibson1e8f8892006-01-06 00:10:44 -08003358 if (writable) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07003359 entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
3360 vma->vm_page_prot)));
David Gibson63551ae2005-06-21 17:14:44 -07003361 } else {
Gerald Schaefer106c9922013-04-29 15:07:23 -07003362 entry = huge_pte_wrprotect(mk_huge_pte(page,
3363 vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07003364 }
3365 entry = pte_mkyoung(entry);
3366 entry = pte_mkhuge(entry);
Chris Metcalfd9ed9fa2012-04-01 14:01:34 -04003367 entry = arch_make_huge_pte(entry, vma, page, writable);
David Gibson63551ae2005-06-21 17:14:44 -07003368
3369 return entry;
3370}
3371
David Gibson1e8f8892006-01-06 00:10:44 -08003372static void set_huge_ptep_writable(struct vm_area_struct *vma,
3373 unsigned long address, pte_t *ptep)
3374{
3375 pte_t entry;
3376
Gerald Schaefer106c9922013-04-29 15:07:23 -07003377 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
Chris Forbes32f84522011-07-25 17:12:14 -07003378 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
Russell King4b3073e2009-12-18 16:40:18 +00003379 update_mmu_cache(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08003380}
3381
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003382bool is_hugetlb_entry_migration(pte_t pte)
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003383{
3384 swp_entry_t swp;
3385
3386 if (huge_pte_none(pte) || pte_present(pte))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003387 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003388 swp = pte_to_swp_entry(pte);
3389 if (non_swap_entry(swp) && is_migration_entry(swp))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003390 return true;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003391 else
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003392 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003393}
3394
3395static int is_hugetlb_entry_hwpoisoned(pte_t pte)
3396{
3397 swp_entry_t swp;
3398
3399 if (huge_pte_none(pte) || pte_present(pte))
3400 return 0;
3401 swp = pte_to_swp_entry(pte);
3402 if (non_swap_entry(swp) && is_hwpoison_entry(swp))
3403 return 1;
3404 else
3405 return 0;
3406}
David Gibson1e8f8892006-01-06 00:10:44 -08003407
David Gibson63551ae2005-06-21 17:14:44 -07003408int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
3409 struct vm_area_struct *vma)
3410{
Mike Kravetz5e415402018-11-16 15:08:04 -08003411 pte_t *src_pte, *dst_pte, entry, dst_entry;
David Gibson63551ae2005-06-21 17:14:44 -07003412 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07003413 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08003414 int cow;
Andi Kleena5516432008-07-23 21:27:41 -07003415 struct hstate *h = hstate_vma(vma);
3416 unsigned long sz = huge_page_size(h);
Mike Kravetzc0d03812020-04-01 21:11:05 -07003417 struct address_space *mapping = vma->vm_file->f_mapping;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003418 struct mmu_notifier_range range;
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003419 int ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08003420
3421 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07003422
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003423 if (cow) {
Jérôme Glisse7269f992019-05-13 17:20:53 -07003424 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, src,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07003425 vma->vm_start,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003426 vma->vm_end);
3427 mmu_notifier_invalidate_range_start(&range);
Mike Kravetzc0d03812020-04-01 21:11:05 -07003428 } else {
3429 /*
3430 * For shared mappings i_mmap_rwsem must be held to call
3431 * huge_pte_alloc, otherwise the returned ptep could go
3432 * away if part of a shared pmd and another thread calls
3433 * huge_pmd_unshare.
3434 */
3435 i_mmap_lock_read(mapping);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003436 }
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003437
Andi Kleena5516432008-07-23 21:27:41 -07003438 for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003439 spinlock_t *src_ptl, *dst_ptl;
Punit Agrawal7868a202017-07-06 15:39:42 -07003440 src_pte = huge_pte_offset(src, addr, sz);
Hugh Dickinsc74df322005-10-29 18:16:23 -07003441 if (!src_pte)
3442 continue;
Andi Kleena5516432008-07-23 21:27:41 -07003443 dst_pte = huge_pte_alloc(dst, addr, sz);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003444 if (!dst_pte) {
3445 ret = -ENOMEM;
3446 break;
3447 }
Larry Woodmanc5c99422008-01-24 05:49:25 -08003448
Mike Kravetz5e415402018-11-16 15:08:04 -08003449 /*
3450 * If the pagetables are shared don't copy or take references.
3451 * dst_pte == src_pte is the common case of src/dest sharing.
3452 *
3453 * However, src could have 'unshared' and dst shares with
3454 * another vma. If dst_pte !none, this implies sharing.
3455 * Check here before taking page table lock, and once again
3456 * after taking the lock below.
3457 */
3458 dst_entry = huge_ptep_get(dst_pte);
3459 if ((dst_pte == src_pte) || !huge_pte_none(dst_entry))
Larry Woodmanc5c99422008-01-24 05:49:25 -08003460 continue;
3461
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003462 dst_ptl = huge_pte_lock(h, dst, dst_pte);
3463 src_ptl = huge_pte_lockptr(h, src, src_pte);
3464 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003465 entry = huge_ptep_get(src_pte);
Mike Kravetz5e415402018-11-16 15:08:04 -08003466 dst_entry = huge_ptep_get(dst_pte);
3467 if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) {
3468 /*
3469 * Skip if src entry none. Also, skip in the
3470 * unlikely case dst entry !none as this implies
3471 * sharing with another vma.
3472 */
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003473 ;
3474 } else if (unlikely(is_hugetlb_entry_migration(entry) ||
3475 is_hugetlb_entry_hwpoisoned(entry))) {
3476 swp_entry_t swp_entry = pte_to_swp_entry(entry);
3477
3478 if (is_write_migration_entry(swp_entry) && cow) {
3479 /*
3480 * COW mappings require pages in both
3481 * parent and child to be set to read.
3482 */
3483 make_migration_entry_read(&swp_entry);
3484 entry = swp_entry_to_pte(swp_entry);
Punit Agrawale5251fd2017-07-06 15:39:50 -07003485 set_huge_swap_pte_at(src, addr, src_pte,
3486 entry, sz);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003487 }
Punit Agrawale5251fd2017-07-06 15:39:50 -07003488 set_huge_swap_pte_at(dst, addr, dst_pte, entry, sz);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003489 } else {
Joerg Roedel34ee6452014-11-13 13:46:09 +11003490 if (cow) {
Jérôme Glisse0f108512017-11-15 17:34:07 -08003491 /*
3492 * No need to notify as we are downgrading page
3493 * table protection not changing it to point
3494 * to a new page.
3495 *
Mike Rapoportad56b732018-03-21 21:22:47 +02003496 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08003497 */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07003498 huge_ptep_set_wrprotect(src, addr, src_pte);
Joerg Roedel34ee6452014-11-13 13:46:09 +11003499 }
Naoya Horiguchi0253d632014-07-23 14:00:19 -07003500 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07003501 ptepage = pte_page(entry);
3502 get_page(ptepage);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08003503 page_dup_rmap(ptepage, true);
Hugh Dickins1c598272005-10-19 21:23:43 -07003504 set_huge_pte_at(dst, addr, dst_pte, entry);
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003505 hugetlb_count_add(pages_per_huge_page(h), dst);
Hugh Dickins1c598272005-10-19 21:23:43 -07003506 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003507 spin_unlock(src_ptl);
3508 spin_unlock(dst_ptl);
David Gibson63551ae2005-06-21 17:14:44 -07003509 }
David Gibson63551ae2005-06-21 17:14:44 -07003510
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003511 if (cow)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003512 mmu_notifier_invalidate_range_end(&range);
Mike Kravetzc0d03812020-04-01 21:11:05 -07003513 else
3514 i_mmap_unlock_read(mapping);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003515
3516 return ret;
David Gibson63551ae2005-06-21 17:14:44 -07003517}
3518
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003519void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
3520 unsigned long start, unsigned long end,
3521 struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07003522{
3523 struct mm_struct *mm = vma->vm_mm;
3524 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07003525 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07003526 pte_t pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003527 spinlock_t *ptl;
David Gibson63551ae2005-06-21 17:14:44 -07003528 struct page *page;
Andi Kleena5516432008-07-23 21:27:41 -07003529 struct hstate *h = hstate_vma(vma);
3530 unsigned long sz = huge_page_size(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003531 struct mmu_notifier_range range;
Andi Kleena5516432008-07-23 21:27:41 -07003532
David Gibson63551ae2005-06-21 17:14:44 -07003533 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07003534 BUG_ON(start & ~huge_page_mask(h));
3535 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07003536
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08003537 /*
3538 * This is a hugetlb vma, all the pte entries should point
3539 * to huge page.
3540 */
Peter Zijlstraed6a7932018-08-31 14:46:08 +02003541 tlb_change_page_size(tlb, sz);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003542 tlb_start_vma(tlb, vma);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003543
3544 /*
3545 * If sharing possible, alert mmu notifiers of worst case.
3546 */
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07003547 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, mm, start,
3548 end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003549 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
3550 mmu_notifier_invalidate_range_start(&range);
Hillf Danton569f48b82014-12-10 15:44:41 -08003551 address = start;
Hillf Danton569f48b82014-12-10 15:44:41 -08003552 for (; address < end; address += sz) {
Punit Agrawal7868a202017-07-06 15:39:42 -07003553 ptep = huge_pte_offset(mm, address, sz);
Adam Litke4c887262005-10-29 18:16:46 -07003554 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07003555 continue;
3556
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003557 ptl = huge_pte_lock(h, mm, ptep);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003558 if (huge_pmd_unshare(mm, &address, ptep)) {
3559 spin_unlock(ptl);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003560 /*
3561 * We just unmapped a page of PMDs by clearing a PUD.
3562 * The caller's TLB flush range should cover this area.
3563 */
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003564 continue;
3565 }
Chen, Kenneth W39dde652006-12-06 20:32:03 -08003566
Hillf Danton66293262012-03-23 15:01:48 -07003567 pte = huge_ptep_get(ptep);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003568 if (huge_pte_none(pte)) {
3569 spin_unlock(ptl);
3570 continue;
3571 }
Hillf Danton66293262012-03-23 15:01:48 -07003572
3573 /*
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08003574 * Migrating hugepage or HWPoisoned hugepage is already
3575 * unmapped and its refcount is dropped, so just clear pte here.
Hillf Danton66293262012-03-23 15:01:48 -07003576 */
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08003577 if (unlikely(!pte_present(pte))) {
Punit Agrawal9386fac2017-07-06 15:39:46 -07003578 huge_pte_clear(mm, address, ptep, sz);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003579 spin_unlock(ptl);
3580 continue;
Naoya Horiguchi8c4894c2012-12-12 13:52:28 -08003581 }
Hillf Danton66293262012-03-23 15:01:48 -07003582
3583 page = pte_page(pte);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003584 /*
3585 * If a reference page is supplied, it is because a specific
3586 * page is being unmapped, not a range. Ensure the page we
3587 * are about to unmap is the actual page of interest.
3588 */
3589 if (ref_page) {
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003590 if (page != ref_page) {
3591 spin_unlock(ptl);
3592 continue;
3593 }
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003594 /*
3595 * Mark the VMA as having unmapped its page so that
3596 * future faults in this VMA will fail rather than
3597 * looking like data was lost
3598 */
3599 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
3600 }
3601
David Gibsonc7546f82005-08-05 11:59:35 -07003602 pte = huge_ptep_get_and_clear(mm, address, ptep);
Aneesh Kumar K.Vb528e4b2016-12-12 16:42:37 -08003603 tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
Gerald Schaefer106c9922013-04-29 15:07:23 -07003604 if (huge_pte_dirty(pte))
Ken Chen6649a382007-02-08 14:20:27 -08003605 set_page_dirty(page);
Hillf Danton9e811302012-03-21 16:34:03 -07003606
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003607 hugetlb_count_sub(pages_per_huge_page(h), mm);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08003608 page_remove_rmap(page, true);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003609
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003610 spin_unlock(ptl);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -07003611 tlb_remove_page_size(tlb, page, huge_page_size(h));
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003612 /*
3613 * Bail out after unmapping reference page if supplied
3614 */
3615 if (ref_page)
3616 break;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07003617 }
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003618 mmu_notifier_invalidate_range_end(&range);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003619 tlb_end_vma(tlb, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
David Gibson63551ae2005-06-21 17:14:44 -07003621
Mel Gormand8333522012-07-31 16:46:20 -07003622void __unmap_hugepage_range_final(struct mmu_gather *tlb,
3623 struct vm_area_struct *vma, unsigned long start,
3624 unsigned long end, struct page *ref_page)
3625{
3626 __unmap_hugepage_range(tlb, vma, start, end, ref_page);
3627
3628 /*
3629 * Clear this flag so that x86's huge_pmd_share page_table_shareable
3630 * test will fail on a vma being torn down, and not grab a page table
3631 * on its way out. We're lucky that the flag has such an appropriate
3632 * name, and can in fact be safely cleared here. We could clear it
3633 * before the __unmap_hugepage_range above, but all that's necessary
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003634 * is to clear it before releasing the i_mmap_rwsem. This works
Mel Gormand8333522012-07-31 16:46:20 -07003635 * because in the context this is called, the VMA is about to be
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003636 * destroyed and the i_mmap_rwsem is held.
Mel Gormand8333522012-07-31 16:46:20 -07003637 */
3638 vma->vm_flags &= ~VM_MAYSHARE;
3639}
3640
Chen, Kenneth W502717f2006-10-11 01:20:46 -07003641void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003642 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07003643{
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003644 struct mm_struct *mm;
3645 struct mmu_gather tlb;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003646 unsigned long tlb_start = start;
3647 unsigned long tlb_end = end;
3648
3649 /*
3650 * If shared PMDs were possibly used within this vma range, adjust
3651 * start/end for worst case tlb flushing.
3652 * Note that we can not be sure if PMDs are shared until we try to
3653 * unmap pages. However, we want to make sure TLB flushing covers
3654 * the largest possible range.
3655 */
3656 adjust_range_if_pmd_sharing_possible(vma, &tlb_start, &tlb_end);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003657
3658 mm = vma->vm_mm;
3659
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003660 tlb_gather_mmu(&tlb, mm, tlb_start, tlb_end);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003661 __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003662 tlb_finish_mmu(&tlb, tlb_start, tlb_end);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07003663}
3664
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003665/*
3666 * This is called when the original mapper is failing to COW a MAP_PRIVATE
3667 * mappping it owns the reserve page for. The intention is to unmap the page
3668 * from other VMAs and let the children be SIGKILLed if they are faulting the
3669 * same region.
3670 */
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07003671static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
3672 struct page *page, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003673{
Adam Litke75266742008-11-12 13:24:56 -08003674 struct hstate *h = hstate_vma(vma);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003675 struct vm_area_struct *iter_vma;
3676 struct address_space *mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003677 pgoff_t pgoff;
3678
3679 /*
3680 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
3681 * from page cache lookup which is in HPAGE_SIZE units.
3682 */
Adam Litke75266742008-11-12 13:24:56 -08003683 address = address & huge_page_mask(h);
Michal Hocko36e4f202012-10-08 16:33:31 -07003684 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
3685 vma->vm_pgoff;
Al Viro93c76a32015-12-04 23:45:44 -05003686 mapping = vma->vm_file->f_mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003687
Mel Gorman4eb2b1d2009-12-14 17:59:53 -08003688 /*
3689 * Take the mapping lock for the duration of the table walk. As
3690 * this mapping should be shared between all the VMAs,
3691 * __unmap_hugepage_range() is called as the lock is already held
3692 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003693 i_mmap_lock_write(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07003694 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003695 /* Do not unmap the current VMA */
3696 if (iter_vma == vma)
3697 continue;
3698
3699 /*
Mel Gorman2f84a892015-10-01 15:36:57 -07003700 * Shared VMAs have their own reserves and do not affect
3701 * MAP_PRIVATE accounting but it is possible that a shared
3702 * VMA is using the same page so check and skip such VMAs.
3703 */
3704 if (iter_vma->vm_flags & VM_MAYSHARE)
3705 continue;
3706
3707 /*
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003708 * Unmap the page from other VMAs without their own reserves.
3709 * They get marked to be SIGKILLed if they fault in these
3710 * areas. This is because a future no-page fault on this VMA
3711 * could insert a zeroed page instead of the data existing
3712 * from the time of fork. This would look like data corruption
3713 */
3714 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003715 unmap_hugepage_range(iter_vma, address,
3716 address + huge_page_size(h), page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003717 }
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003718 i_mmap_unlock_write(mapping);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003719}
3720
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003721/*
3722 * Hugetlb_cow() should be called with page lock of the original hugepage held.
Michal Hockoef009b22012-01-10 15:07:21 -08003723 * Called with hugetlb_instantiation_mutex held and pte_page locked so we
3724 * cannot race with other handlers or page migration.
3725 * Keep the pte_same checks anyway to make transition from the mutex easier.
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003726 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003727static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Huang Ying974e6d62018-08-17 15:45:57 -07003728 unsigned long address, pte_t *ptep,
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08003729 struct page *pagecache_page, spinlock_t *ptl)
David Gibson1e8f8892006-01-06 00:10:44 -08003730{
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08003731 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07003732 struct hstate *h = hstate_vma(vma);
David Gibson1e8f8892006-01-06 00:10:44 -08003733 struct page *old_page, *new_page;
Souptick Joarder2b740302018-08-23 17:01:36 -07003734 int outside_reserve = 0;
3735 vm_fault_t ret = 0;
Huang Ying974e6d62018-08-17 15:45:57 -07003736 unsigned long haddr = address & huge_page_mask(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003737 struct mmu_notifier_range range;
David Gibson1e8f8892006-01-06 00:10:44 -08003738
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08003739 pte = huge_ptep_get(ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08003740 old_page = pte_page(pte);
3741
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003742retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08003743 /* If no-one else is actually using this page, avoid the copy
3744 * and just make the page writable */
Joonsoo Kim37a21402013-09-11 14:21:04 -07003745 if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
Hugh Dickins5a499732016-07-14 12:07:38 -07003746 page_move_anon_rmap(old_page, vma);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003747 set_huge_ptep_writable(vma, haddr, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07003748 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08003749 }
3750
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003751 /*
3752 * If the process that created a MAP_PRIVATE mapping is about to
3753 * perform a COW due to a shared page count, attempt to satisfy
3754 * the allocation without using the existing reserves. The pagecache
3755 * page is used to determine if the reserve at this address was
3756 * consumed or not. If reserves were used, a partial faulted mapping
3757 * at the time of fork() could consume its reserves on COW instead
3758 * of the full address range.
3759 */
Joonsoo Kim5944d012013-09-11 14:21:55 -07003760 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003761 old_page != pagecache_page)
3762 outside_reserve = 1;
3763
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003764 get_page(old_page);
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08003765
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003766 /*
3767 * Drop page table lock as buddy allocator may be called. It will
3768 * be acquired again before returning to the caller, as expected.
3769 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003770 spin_unlock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003771 new_page = alloc_huge_page(vma, haddr, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08003772
Adam Litke2fc39ce2007-11-14 16:59:39 -08003773 if (IS_ERR(new_page)) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003774 /*
3775 * If a process owning a MAP_PRIVATE mapping fails to COW,
3776 * it is due to references held by a child and an insufficient
3777 * huge page pool. To guarantee the original mappers
3778 * reliability, unmap the page from child processes. The child
3779 * may get SIGKILLed if it later faults.
3780 */
3781 if (outside_reserve) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003782 put_page(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003783 BUG_ON(huge_pte_none(pte));
Huang Ying5b7a1d42018-08-17 15:45:53 -07003784 unmap_ref_private(mm, vma, old_page, haddr);
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07003785 BUG_ON(huge_pte_none(pte));
3786 spin_lock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003787 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07003788 if (likely(ptep &&
3789 pte_same(huge_ptep_get(ptep), pte)))
3790 goto retry_avoidcopy;
3791 /*
3792 * race occurs while re-acquiring page table
3793 * lock, and our job is done.
3794 */
3795 return 0;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003796 }
3797
Souptick Joarder2b740302018-08-23 17:01:36 -07003798 ret = vmf_error(PTR_ERR(new_page));
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003799 goto out_release_old;
David Gibson1e8f8892006-01-06 00:10:44 -08003800 }
3801
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003802 /*
3803 * When the original hugepage is shared one, it does not have
3804 * anon_vma prepared.
3805 */
Dean Nelson44e2aa92010-10-26 14:22:08 -07003806 if (unlikely(anon_vma_prepare(vma))) {
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003807 ret = VM_FAULT_OOM;
3808 goto out_release_all;
Dean Nelson44e2aa92010-10-26 14:22:08 -07003809 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003810
Huang Ying974e6d62018-08-17 15:45:57 -07003811 copy_user_huge_page(new_page, old_page, address, vma,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003812 pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08003813 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08003814
Jérôme Glisse7269f992019-05-13 17:20:53 -07003815 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, haddr,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07003816 haddr + huge_page_size(h));
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003817 mmu_notifier_invalidate_range_start(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003818
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08003819 /*
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003820 * Retake the page table lock to check for racing updates
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08003821 * before the page tables are altered
3822 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003823 spin_lock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003824 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Naoya Horiguchia9af0c52014-04-07 15:36:54 -07003825 if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
Joonsoo Kim07443a82013-09-11 14:21:58 -07003826 ClearPagePrivate(new_page);
3827
David Gibson1e8f8892006-01-06 00:10:44 -08003828 /* Break COW */
Huang Ying5b7a1d42018-08-17 15:45:53 -07003829 huge_ptep_clear_flush(vma, haddr, ptep);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003830 mmu_notifier_invalidate_range(mm, range.start, range.end);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003831 set_huge_pte_at(mm, haddr, ptep,
David Gibson1e8f8892006-01-06 00:10:44 -08003832 make_huge_pte(vma, new_page, 1));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08003833 page_remove_rmap(old_page, true);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003834 hugepage_add_new_anon_rmap(new_page, vma, haddr);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08003835 set_page_huge_active(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08003836 /* Make the old page be freed below */
3837 new_page = old_page;
3838 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003839 spin_unlock(ptl);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003840 mmu_notifier_invalidate_range_end(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003841out_release_all:
Huang Ying5b7a1d42018-08-17 15:45:53 -07003842 restore_reserve_on_error(h, vma, haddr, new_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003843 put_page(new_page);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003844out_release_old:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003845 put_page(old_page);
Joonsoo Kim83120342013-09-11 14:21:57 -07003846
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003847 spin_lock(ptl); /* Caller expects lock to be held */
3848 return ret;
David Gibson1e8f8892006-01-06 00:10:44 -08003849}
3850
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003851/* Return the pagecache page at a given address within a VMA */
Andi Kleena5516432008-07-23 21:27:41 -07003852static struct page *hugetlbfs_pagecache_page(struct hstate *h,
3853 struct vm_area_struct *vma, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003854{
3855 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07003856 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003857
3858 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07003859 idx = vma_hugecache_offset(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003860
3861 return find_lock_page(mapping, idx);
3862}
3863
Hugh Dickins3ae77f42009-09-21 17:03:33 -07003864/*
3865 * Return whether there is a pagecache page to back given address within VMA.
3866 * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
3867 */
3868static bool hugetlbfs_pagecache_present(struct hstate *h,
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003869 struct vm_area_struct *vma, unsigned long address)
3870{
3871 struct address_space *mapping;
3872 pgoff_t idx;
3873 struct page *page;
3874
3875 mapping = vma->vm_file->f_mapping;
3876 idx = vma_hugecache_offset(h, vma, address);
3877
3878 page = find_get_page(mapping, idx);
3879 if (page)
3880 put_page(page);
3881 return page != NULL;
3882}
3883
Mike Kravetzab76ad52015-09-08 15:01:50 -07003884int huge_add_to_page_cache(struct page *page, struct address_space *mapping,
3885 pgoff_t idx)
3886{
3887 struct inode *inode = mapping->host;
3888 struct hstate *h = hstate_inode(inode);
3889 int err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
3890
3891 if (err)
3892 return err;
3893 ClearPagePrivate(page);
3894
Mike Kravetz22146c32018-10-26 15:10:58 -07003895 /*
3896 * set page dirty so that it will not be removed from cache/file
3897 * by non-hugetlbfs specific code paths.
3898 */
3899 set_page_dirty(page);
3900
Mike Kravetzab76ad52015-09-08 15:01:50 -07003901 spin_lock(&inode->i_lock);
3902 inode->i_blocks += blocks_per_huge_page(h);
3903 spin_unlock(&inode->i_lock);
3904 return 0;
3905}
3906
Souptick Joarder2b740302018-08-23 17:01:36 -07003907static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
3908 struct vm_area_struct *vma,
3909 struct address_space *mapping, pgoff_t idx,
3910 unsigned long address, pte_t *ptep, unsigned int flags)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003911{
Andi Kleena5516432008-07-23 21:27:41 -07003912 struct hstate *h = hstate_vma(vma);
Souptick Joarder2b740302018-08-23 17:01:36 -07003913 vm_fault_t ret = VM_FAULT_SIGBUS;
Hillf Danton409eb8c2012-01-20 14:34:13 -08003914 int anon_rmap = 0;
Adam Litke4c887262005-10-29 18:16:46 -07003915 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07003916 struct page *page;
David Gibson1e8f8892006-01-06 00:10:44 -08003917 pte_t new_pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003918 spinlock_t *ptl;
Huang Ying285b8dc2018-06-07 17:08:08 -07003919 unsigned long haddr = address & huge_page_mask(h);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08003920 bool new_page = false;
Adam Litke4c887262005-10-29 18:16:46 -07003921
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003922 /*
3923 * Currently, we are forced to kill the process in the event the
3924 * original mapper has unmapped pages from the child due to a failed
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003925 * COW. Warn that such a situation has occurred as it may not be obvious
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003926 */
3927 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
Geoffrey Thomas910154d2016-03-09 14:08:04 -08003928 pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
Andrew Mortonffb22af2013-02-22 16:32:08 -08003929 current->pid);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003930 return ret;
3931 }
3932
Adam Litke4c887262005-10-29 18:16:46 -07003933 /*
Mike Kravetz87bf91d2020-04-01 21:11:08 -07003934 * We can not race with truncation due to holding i_mmap_rwsem.
3935 * i_size is modified when holding i_mmap_rwsem, so check here
3936 * once for faults beyond end of file.
Adam Litke4c887262005-10-29 18:16:46 -07003937 */
Mike Kravetz87bf91d2020-04-01 21:11:08 -07003938 size = i_size_read(mapping->host) >> huge_page_shift(h);
3939 if (idx >= size)
3940 goto out;
3941
Christoph Lameter6bda6662006-01-06 00:10:49 -08003942retry:
3943 page = find_lock_page(mapping, idx);
3944 if (!page) {
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003945 /*
3946 * Check for page in userfault range
3947 */
3948 if (userfaultfd_missing(vma)) {
3949 u32 hash;
3950 struct vm_fault vmf = {
3951 .vma = vma,
Huang Ying285b8dc2018-06-07 17:08:08 -07003952 .address = haddr,
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003953 .flags = flags,
3954 /*
3955 * Hard to debug if it ends up being
3956 * used by a callee that assumes
3957 * something about the other
3958 * uninitialized fields... same as in
3959 * memory.c
3960 */
3961 };
3962
3963 /*
Mike Kravetzc0d03812020-04-01 21:11:05 -07003964 * hugetlb_fault_mutex and i_mmap_rwsem must be
3965 * dropped before handling userfault. Reacquire
3966 * after handling fault to make calling code simpler.
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003967 */
Wei Yang188b04a2019-11-30 17:57:02 -08003968 hash = hugetlb_fault_mutex_hash(mapping, idx);
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003969 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Mike Kravetzc0d03812020-04-01 21:11:05 -07003970 i_mmap_unlock_read(mapping);
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003971 ret = handle_userfault(&vmf, VM_UFFD_MISSING);
Mike Kravetzc0d03812020-04-01 21:11:05 -07003972 i_mmap_lock_read(mapping);
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003973 mutex_lock(&hugetlb_fault_mutex_table[hash]);
3974 goto out;
3975 }
3976
Huang Ying285b8dc2018-06-07 17:08:08 -07003977 page = alloc_huge_page(vma, haddr, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08003978 if (IS_ERR(page)) {
Mike Kravetz4643d672019-08-13 15:38:00 -07003979 /*
3980 * Returning error will result in faulting task being
3981 * sent SIGBUS. The hugetlb fault mutex prevents two
3982 * tasks from racing to fault in the same page which
3983 * could result in false unable to allocate errors.
3984 * Page migration does not take the fault mutex, but
3985 * does a clear then write of pte's under page table
3986 * lock. Page fault code could race with migration,
3987 * notice the clear pte and try to allocate a page
3988 * here. Before returning error, get ptl and make
3989 * sure there really is no pte entry.
3990 */
3991 ptl = huge_pte_lock(h, mm, ptep);
3992 if (!huge_pte_none(huge_ptep_get(ptep))) {
3993 ret = 0;
3994 spin_unlock(ptl);
3995 goto out;
3996 }
3997 spin_unlock(ptl);
Souptick Joarder2b740302018-08-23 17:01:36 -07003998 ret = vmf_error(PTR_ERR(page));
Christoph Lameter6bda6662006-01-06 00:10:49 -08003999 goto out;
4000 }
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004001 clear_huge_page(page, address, pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08004002 __SetPageUptodate(page);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08004003 new_page = true;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01004004
Mel Gormanf83a2752009-05-28 14:34:40 -07004005 if (vma->vm_flags & VM_MAYSHARE) {
Mike Kravetzab76ad52015-09-08 15:01:50 -07004006 int err = huge_add_to_page_cache(page, mapping, idx);
Christoph Lameter6bda6662006-01-06 00:10:49 -08004007 if (err) {
4008 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08004009 if (err == -EEXIST)
4010 goto retry;
4011 goto out;
4012 }
Mel Gorman23be7462010-04-23 13:17:56 -04004013 } else {
Christoph Lameter6bda6662006-01-06 00:10:49 -08004014 lock_page(page);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09004015 if (unlikely(anon_vma_prepare(vma))) {
4016 ret = VM_FAULT_OOM;
4017 goto backout_unlocked;
4018 }
Hillf Danton409eb8c2012-01-20 14:34:13 -08004019 anon_rmap = 1;
Mel Gorman23be7462010-04-23 13:17:56 -04004020 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09004021 } else {
Naoya Horiguchi998b4382010-09-08 10:19:32 +09004022 /*
4023 * If memory error occurs between mmap() and fault, some process
4024 * don't have hwpoisoned swap entry for errored virtual address.
4025 * So we need to block hugepage fault by PG_hwpoison bit check.
4026 */
4027 if (unlikely(PageHWPoison(page))) {
Chris Forbes32f84522011-07-25 17:12:14 -07004028 ret = VM_FAULT_HWPOISON |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004029 VM_FAULT_SET_HINDEX(hstate_index(h));
Naoya Horiguchi998b4382010-09-08 10:19:32 +09004030 goto backout_unlocked;
4031 }
Christoph Lameter6bda6662006-01-06 00:10:49 -08004032 }
David Gibson1e8f8892006-01-06 00:10:44 -08004033
Andy Whitcroft57303d82008-08-12 15:08:47 -07004034 /*
4035 * If we are going to COW a private mapping later, we examine the
4036 * pending reservations for this page now. This will ensure that
4037 * any allocations necessary to record that reservation occur outside
4038 * the spinlock.
4039 */
Mike Kravetz5e911372015-09-08 15:01:28 -07004040 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07004041 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07004042 ret = VM_FAULT_OOM;
4043 goto backout_unlocked;
4044 }
Mike Kravetz5e911372015-09-08 15:01:28 -07004045 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07004046 vma_end_reservation(h, vma, haddr);
Mike Kravetz5e911372015-09-08 15:01:28 -07004047 }
Andy Whitcroft57303d82008-08-12 15:08:47 -07004048
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08004049 ptl = huge_pte_lock(h, mm, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07004050 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07004051 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07004052 goto backout;
4053
Joonsoo Kim07443a82013-09-11 14:21:58 -07004054 if (anon_rmap) {
4055 ClearPagePrivate(page);
Huang Ying285b8dc2018-06-07 17:08:08 -07004056 hugepage_add_new_anon_rmap(page, vma, haddr);
Choi Gi-yongac714902014-04-07 15:37:36 -07004057 } else
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08004058 page_dup_rmap(page, true);
David Gibson1e8f8892006-01-06 00:10:44 -08004059 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
4060 && (vma->vm_flags & VM_SHARED)));
Huang Ying285b8dc2018-06-07 17:08:08 -07004061 set_huge_pte_at(mm, haddr, ptep, new_pte);
David Gibson1e8f8892006-01-06 00:10:44 -08004062
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08004063 hugetlb_count_add(pages_per_huge_page(h), mm);
Hugh Dickins788c7df2009-06-23 13:49:05 +01004064 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
David Gibson1e8f8892006-01-06 00:10:44 -08004065 /* Optimization, do the COW without a second fault */
Huang Ying974e6d62018-08-17 15:45:57 -07004066 ret = hugetlb_cow(mm, vma, address, ptep, page, ptl);
David Gibson1e8f8892006-01-06 00:10:44 -08004067 }
4068
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004069 spin_unlock(ptl);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08004070
4071 /*
4072 * Only make newly allocated pages active. Existing pages found
4073 * in the pagecache could be !page_huge_active() if they have been
4074 * isolated for migration.
4075 */
4076 if (new_page)
4077 set_page_huge_active(page);
4078
Adam Litke4c887262005-10-29 18:16:46 -07004079 unlock_page(page);
4080out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01004081 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07004082
4083backout:
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004084 spin_unlock(ptl);
Andy Whitcroft2b267362008-08-12 15:08:49 -07004085backout_unlocked:
Adam Litke4c887262005-10-29 18:16:46 -07004086 unlock_page(page);
Huang Ying285b8dc2018-06-07 17:08:08 -07004087 restore_reserve_on_error(h, vma, haddr, page);
Adam Litke4c887262005-10-29 18:16:46 -07004088 put_page(page);
4089 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01004090}
4091
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004092#ifdef CONFIG_SMP
Wei Yang188b04a2019-11-30 17:57:02 -08004093u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004094{
4095 unsigned long key[2];
4096 u32 hash;
4097
Mike Kravetz1b426ba2019-05-13 17:19:41 -07004098 key[0] = (unsigned long) mapping;
4099 key[1] = idx;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004100
Mike Kravetz55254632019-11-30 17:56:30 -08004101 hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004102
4103 return hash & (num_fault_mutexes - 1);
4104}
4105#else
4106/*
4107 * For uniprocesor systems we always use a single mutex, so just
4108 * return 0 and avoid the hashing overhead.
4109 */
Wei Yang188b04a2019-11-30 17:57:02 -08004110u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004111{
4112 return 0;
4113}
4114#endif
4115
Souptick Joarder2b740302018-08-23 17:01:36 -07004116vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
Hugh Dickins788c7df2009-06-23 13:49:05 +01004117 unsigned long address, unsigned int flags)
Adam Litke86e52162006-01-06 00:10:43 -08004118{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004119 pte_t *ptep, entry;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004120 spinlock_t *ptl;
Souptick Joarder2b740302018-08-23 17:01:36 -07004121 vm_fault_t ret;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004122 u32 hash;
4123 pgoff_t idx;
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09004124 struct page *page = NULL;
Andy Whitcroft57303d82008-08-12 15:08:47 -07004125 struct page *pagecache_page = NULL;
Andi Kleena5516432008-07-23 21:27:41 -07004126 struct hstate *h = hstate_vma(vma);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004127 struct address_space *mapping;
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004128 int need_wait_lock = 0;
Huang Ying285b8dc2018-06-07 17:08:08 -07004129 unsigned long haddr = address & huge_page_mask(h);
Adam Litke86e52162006-01-06 00:10:43 -08004130
Huang Ying285b8dc2018-06-07 17:08:08 -07004131 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09004132 if (ptep) {
Mike Kravetzc0d03812020-04-01 21:11:05 -07004133 /*
4134 * Since we hold no locks, ptep could be stale. That is
4135 * OK as we are only making decisions based on content and
4136 * not actually modifying content here.
4137 */
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09004138 entry = huge_ptep_get(ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09004139 if (unlikely(is_hugetlb_entry_migration(entry))) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004140 migration_entry_wait_huge(vma, mm, ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09004141 return 0;
4142 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
Chris Forbes32f84522011-07-25 17:12:14 -07004143 return VM_FAULT_HWPOISON_LARGE |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004144 VM_FAULT_SET_HINDEX(hstate_index(h));
Mike Kravetzddeaab32019-01-08 15:23:36 -08004145 } else {
4146 ptep = huge_pte_alloc(mm, haddr, huge_page_size(h));
4147 if (!ptep)
4148 return VM_FAULT_OOM;
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09004149 }
4150
Mike Kravetzc0d03812020-04-01 21:11:05 -07004151 /*
4152 * Acquire i_mmap_rwsem before calling huge_pte_alloc and hold
Mike Kravetz87bf91d2020-04-01 21:11:08 -07004153 * until finished with ptep. This serves two purposes:
4154 * 1) It prevents huge_pmd_unshare from being called elsewhere
4155 * and making the ptep no longer valid.
4156 * 2) It synchronizes us with i_size modifications during truncation.
Mike Kravetzc0d03812020-04-01 21:11:05 -07004157 *
4158 * ptep could have already be assigned via huge_pte_offset. That
4159 * is OK, as huge_pte_alloc will return the same value unless
4160 * something has changed.
4161 */
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004162 mapping = vma->vm_file->f_mapping;
Mike Kravetzc0d03812020-04-01 21:11:05 -07004163 i_mmap_lock_read(mapping);
4164 ptep = huge_pte_alloc(mm, haddr, huge_page_size(h));
4165 if (!ptep) {
4166 i_mmap_unlock_read(mapping);
4167 return VM_FAULT_OOM;
4168 }
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004169
David Gibson3935baa2006-03-22 00:08:53 -08004170 /*
4171 * Serialize hugepage allocation and instantiation, so that we don't
4172 * get spurious allocation failures if two CPUs race to instantiate
4173 * the same page in the page cache.
4174 */
Mike Kravetzc0d03812020-04-01 21:11:05 -07004175 idx = vma_hugecache_offset(h, vma, haddr);
Wei Yang188b04a2019-11-30 17:57:02 -08004176 hash = hugetlb_fault_mutex_hash(mapping, idx);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004177 mutex_lock(&hugetlb_fault_mutex_table[hash]);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004178
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07004179 entry = huge_ptep_get(ptep);
4180 if (huge_pte_none(entry)) {
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004181 ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags);
David Gibsonb4d1d992008-10-15 22:01:11 -07004182 goto out_mutex;
David Gibson3935baa2006-03-22 00:08:53 -08004183 }
Adam Litke86e52162006-01-06 00:10:43 -08004184
Nick Piggin83c54072007-07-19 01:47:05 -07004185 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08004186
Andy Whitcroft57303d82008-08-12 15:08:47 -07004187 /*
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004188 * entry could be a migration/hwpoison entry at this point, so this
4189 * check prevents the kernel from going below assuming that we have
4190 * a active hugepage in pagecache. This goto expects the 2nd page fault,
4191 * and is_hugetlb_entry_(migration|hwpoisoned) check will properly
4192 * handle it.
4193 */
4194 if (!pte_present(entry))
4195 goto out_mutex;
4196
4197 /*
Andy Whitcroft57303d82008-08-12 15:08:47 -07004198 * If we are going to COW the mapping later, we examine the pending
4199 * reservations for this page now. This will ensure that any
4200 * allocations necessary to record that reservation occur outside the
4201 * spinlock. For private mappings, we also lookup the pagecache
4202 * page now as it is used to determine if a reservation has been
4203 * consumed.
4204 */
Gerald Schaefer106c9922013-04-29 15:07:23 -07004205 if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07004206 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07004207 ret = VM_FAULT_OOM;
David Gibsonb4d1d992008-10-15 22:01:11 -07004208 goto out_mutex;
Andy Whitcroft2b267362008-08-12 15:08:49 -07004209 }
Mike Kravetz5e911372015-09-08 15:01:28 -07004210 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07004211 vma_end_reservation(h, vma, haddr);
Andy Whitcroft57303d82008-08-12 15:08:47 -07004212
Mel Gormanf83a2752009-05-28 14:34:40 -07004213 if (!(vma->vm_flags & VM_MAYSHARE))
Andy Whitcroft57303d82008-08-12 15:08:47 -07004214 pagecache_page = hugetlbfs_pagecache_page(h,
Huang Ying285b8dc2018-06-07 17:08:08 -07004215 vma, haddr);
Andy Whitcroft57303d82008-08-12 15:08:47 -07004216 }
4217
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004218 ptl = huge_pte_lock(h, mm, ptep);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09004219
David Gibson1e8f8892006-01-06 00:10:44 -08004220 /* Check for a racing update before calling hugetlb_cow */
David Gibsonb4d1d992008-10-15 22:01:11 -07004221 if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004222 goto out_ptl;
David Gibsonb4d1d992008-10-15 22:01:11 -07004223
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004224 /*
4225 * hugetlb_cow() requires page locks of pte_page(entry) and
4226 * pagecache_page, so here we need take the former one
4227 * when page != pagecache_page or !pagecache_page.
4228 */
4229 page = pte_page(entry);
4230 if (page != pagecache_page)
4231 if (!trylock_page(page)) {
4232 need_wait_lock = 1;
4233 goto out_ptl;
4234 }
4235
4236 get_page(page);
David Gibsonb4d1d992008-10-15 22:01:11 -07004237
Hugh Dickins788c7df2009-06-23 13:49:05 +01004238 if (flags & FAULT_FLAG_WRITE) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07004239 if (!huge_pte_write(entry)) {
Huang Ying974e6d62018-08-17 15:45:57 -07004240 ret = hugetlb_cow(mm, vma, address, ptep,
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08004241 pagecache_page, ptl);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004242 goto out_put_page;
David Gibsonb4d1d992008-10-15 22:01:11 -07004243 }
Gerald Schaefer106c9922013-04-29 15:07:23 -07004244 entry = huge_pte_mkdirty(entry);
David Gibsonb4d1d992008-10-15 22:01:11 -07004245 }
4246 entry = pte_mkyoung(entry);
Huang Ying285b8dc2018-06-07 17:08:08 -07004247 if (huge_ptep_set_access_flags(vma, haddr, ptep, entry,
Hugh Dickins788c7df2009-06-23 13:49:05 +01004248 flags & FAULT_FLAG_WRITE))
Huang Ying285b8dc2018-06-07 17:08:08 -07004249 update_mmu_cache(vma, haddr, ptep);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004250out_put_page:
4251 if (page != pagecache_page)
4252 unlock_page(page);
4253 put_page(page);
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004254out_ptl:
4255 spin_unlock(ptl);
Andy Whitcroft57303d82008-08-12 15:08:47 -07004256
4257 if (pagecache_page) {
4258 unlock_page(pagecache_page);
4259 put_page(pagecache_page);
4260 }
David Gibsonb4d1d992008-10-15 22:01:11 -07004261out_mutex:
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004262 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Mike Kravetzc0d03812020-04-01 21:11:05 -07004263 i_mmap_unlock_read(mapping);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004264 /*
4265 * Generally it's safe to hold refcount during waiting page lock. But
4266 * here we just wait to defer the next page fault to avoid busy loop and
4267 * the page is not used after unlocked before returning from the current
4268 * page fault. So we are safe from accessing freed page, even if we wait
4269 * here without taking refcount.
4270 */
4271 if (need_wait_lock)
4272 wait_on_page_locked(page);
David Gibson1e8f8892006-01-06 00:10:44 -08004273 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08004274}
4275
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004276/*
4277 * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with
4278 * modifications for huge pages.
4279 */
4280int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
4281 pte_t *dst_pte,
4282 struct vm_area_struct *dst_vma,
4283 unsigned long dst_addr,
4284 unsigned long src_addr,
4285 struct page **pagep)
4286{
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004287 struct address_space *mapping;
4288 pgoff_t idx;
4289 unsigned long size;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004290 int vm_shared = dst_vma->vm_flags & VM_SHARED;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004291 struct hstate *h = hstate_vma(dst_vma);
4292 pte_t _dst_pte;
4293 spinlock_t *ptl;
4294 int ret;
4295 struct page *page;
4296
4297 if (!*pagep) {
4298 ret = -ENOMEM;
4299 page = alloc_huge_page(dst_vma, dst_addr, 0);
4300 if (IS_ERR(page))
4301 goto out;
4302
4303 ret = copy_huge_page_from_user(page,
4304 (const void __user *) src_addr,
Mike Kravetz810a56b2017-02-22 15:42:58 -08004305 pages_per_huge_page(h), false);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004306
4307 /* fallback to copy_from_user outside mmap_sem */
4308 if (unlikely(ret)) {
Andrea Arcangeli9e368252018-11-30 14:09:25 -08004309 ret = -ENOENT;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004310 *pagep = page;
4311 /* don't free the page */
4312 goto out;
4313 }
4314 } else {
4315 page = *pagep;
4316 *pagep = NULL;
4317 }
4318
4319 /*
4320 * The memory barrier inside __SetPageUptodate makes sure that
4321 * preceding stores to the page contents become visible before
4322 * the set_pte_at() write.
4323 */
4324 __SetPageUptodate(page);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004325
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004326 mapping = dst_vma->vm_file->f_mapping;
4327 idx = vma_hugecache_offset(h, dst_vma, dst_addr);
4328
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004329 /*
4330 * If shared, add to page cache
4331 */
4332 if (vm_shared) {
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004333 size = i_size_read(mapping->host) >> huge_page_shift(h);
4334 ret = -EFAULT;
4335 if (idx >= size)
4336 goto out_release_nounlock;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004337
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004338 /*
4339 * Serialization between remove_inode_hugepages() and
4340 * huge_add_to_page_cache() below happens through the
4341 * hugetlb_fault_mutex_table that here must be hold by
4342 * the caller.
4343 */
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004344 ret = huge_add_to_page_cache(page, mapping, idx);
4345 if (ret)
4346 goto out_release_nounlock;
4347 }
4348
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004349 ptl = huge_pte_lockptr(h, dst_mm, dst_pte);
4350 spin_lock(ptl);
4351
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004352 /*
4353 * Recheck the i_size after holding PT lock to make sure not
4354 * to leave any page mapped (as page_mapped()) beyond the end
4355 * of the i_size (remove_inode_hugepages() is strict about
4356 * enforcing that). If we bail out here, we'll also leave a
4357 * page in the radix tree in the vm_shared case beyond the end
4358 * of the i_size, but remove_inode_hugepages() will take care
4359 * of it as soon as we drop the hugetlb_fault_mutex_table.
4360 */
4361 size = i_size_read(mapping->host) >> huge_page_shift(h);
4362 ret = -EFAULT;
4363 if (idx >= size)
4364 goto out_release_unlock;
4365
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004366 ret = -EEXIST;
4367 if (!huge_pte_none(huge_ptep_get(dst_pte)))
4368 goto out_release_unlock;
4369
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004370 if (vm_shared) {
4371 page_dup_rmap(page, true);
4372 } else {
4373 ClearPagePrivate(page);
4374 hugepage_add_new_anon_rmap(page, dst_vma, dst_addr);
4375 }
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004376
4377 _dst_pte = make_huge_pte(dst_vma, page, dst_vma->vm_flags & VM_WRITE);
4378 if (dst_vma->vm_flags & VM_WRITE)
4379 _dst_pte = huge_pte_mkdirty(_dst_pte);
4380 _dst_pte = pte_mkyoung(_dst_pte);
4381
4382 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
4383
4384 (void)huge_ptep_set_access_flags(dst_vma, dst_addr, dst_pte, _dst_pte,
4385 dst_vma->vm_flags & VM_WRITE);
4386 hugetlb_count_add(pages_per_huge_page(h), dst_mm);
4387
4388 /* No need to invalidate - it was non-present before */
4389 update_mmu_cache(dst_vma, dst_addr, dst_pte);
4390
4391 spin_unlock(ptl);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08004392 set_page_huge_active(page);
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004393 if (vm_shared)
4394 unlock_page(page);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004395 ret = 0;
4396out:
4397 return ret;
4398out_release_unlock:
4399 spin_unlock(ptl);
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004400 if (vm_shared)
4401 unlock_page(page);
Andrea Arcangeli5af10df2017-08-10 15:23:38 -07004402out_release_nounlock:
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004403 put_page(page);
4404 goto out;
4405}
4406
Michel Lespinasse28a35712013-02-22 16:35:55 -08004407long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
4408 struct page **pages, struct vm_area_struct **vmas,
4409 unsigned long *position, unsigned long *nr_pages,
Peter Xu4f6da932020-04-01 21:07:58 -07004410 long i, unsigned int flags, int *locked)
David Gibson63551ae2005-06-21 17:14:44 -07004411{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004412 unsigned long pfn_offset;
4413 unsigned long vaddr = *position;
Michel Lespinasse28a35712013-02-22 16:35:55 -08004414 unsigned long remainder = *nr_pages;
Andi Kleena5516432008-07-23 21:27:41 -07004415 struct hstate *h = hstate_vma(vma);
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07004416 int err = -EFAULT;
David Gibson63551ae2005-06-21 17:14:44 -07004417
David Gibson63551ae2005-06-21 17:14:44 -07004418 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07004419 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004420 spinlock_t *ptl = NULL;
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004421 int absent;
Adam Litke4c887262005-10-29 18:16:46 -07004422 struct page *page;
4423
4424 /*
David Rientjes02057962015-04-14 15:48:24 -07004425 * If we have a pending SIGKILL, don't keep faulting pages and
4426 * potentially allocating memory.
4427 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -08004428 if (fatal_signal_pending(current)) {
David Rientjes02057962015-04-14 15:48:24 -07004429 remainder = 0;
4430 break;
4431 }
4432
4433 /*
Adam Litke4c887262005-10-29 18:16:46 -07004434 * Some archs (sparc64, sh*) have multiple pte_ts to
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004435 * each hugepage. We have to make sure we get the
Adam Litke4c887262005-10-29 18:16:46 -07004436 * first, for the page indexing below to work.
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004437 *
4438 * Note that page table lock is not held when pte is null.
Adam Litke4c887262005-10-29 18:16:46 -07004439 */
Punit Agrawal7868a202017-07-06 15:39:42 -07004440 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h),
4441 huge_page_size(h));
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004442 if (pte)
4443 ptl = huge_pte_lock(h, mm, pte);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004444 absent = !pte || huge_pte_none(huge_ptep_get(pte));
Adam Litke4c887262005-10-29 18:16:46 -07004445
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004446 /*
4447 * When coredumping, it suits get_dump_page if we just return
Hugh Dickins3ae77f42009-09-21 17:03:33 -07004448 * an error where there's an empty slot with no huge pagecache
4449 * to back it. This way, we avoid allocating a hugepage, and
4450 * the sparse dumpfile avoids allocating disk blocks, but its
4451 * huge holes still show up with zeroes where they need to be.
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004452 */
Hugh Dickins3ae77f42009-09-21 17:03:33 -07004453 if (absent && (flags & FOLL_DUMP) &&
4454 !hugetlbfs_pagecache_present(h, vma, vaddr)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004455 if (pte)
4456 spin_unlock(ptl);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004457 remainder = 0;
4458 break;
4459 }
4460
Naoya Horiguchi9cc3a5b2013-04-17 15:58:30 -07004461 /*
4462 * We need call hugetlb_fault for both hugepages under migration
4463 * (in which case hugetlb_fault waits for the migration,) and
4464 * hwpoisoned hugepages (in which case we need to prevent the
4465 * caller from accessing to them.) In order to do this, we use
4466 * here is_swap_pte instead of is_hugetlb_entry_migration and
4467 * is_hugetlb_entry_hwpoisoned. This is because it simply covers
4468 * both cases, and because we can't follow correct pages
4469 * directly from any kind of swap entries.
4470 */
4471 if (absent || is_swap_pte(huge_ptep_get(pte)) ||
Gerald Schaefer106c9922013-04-29 15:07:23 -07004472 ((flags & FOLL_WRITE) &&
4473 !huge_pte_write(huge_ptep_get(pte)))) {
Souptick Joarder2b740302018-08-23 17:01:36 -07004474 vm_fault_t ret;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004475 unsigned int fault_flags = 0;
Adam Litke4c887262005-10-29 18:16:46 -07004476
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004477 if (pte)
4478 spin_unlock(ptl);
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004479 if (flags & FOLL_WRITE)
4480 fault_flags |= FAULT_FLAG_WRITE;
Peter Xu4f6da932020-04-01 21:07:58 -07004481 if (locked)
Peter Xu71335f32020-04-01 21:08:53 -07004482 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
4483 FAULT_FLAG_KILLABLE;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004484 if (flags & FOLL_NOWAIT)
4485 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
4486 FAULT_FLAG_RETRY_NOWAIT;
4487 if (flags & FOLL_TRIED) {
Peter Xu4426e942020-04-01 21:08:49 -07004488 /*
4489 * Note: FAULT_FLAG_ALLOW_RETRY and
4490 * FAULT_FLAG_TRIED can co-exist
4491 */
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004492 fault_flags |= FAULT_FLAG_TRIED;
4493 }
4494 ret = hugetlb_fault(mm, vma, vaddr, fault_flags);
4495 if (ret & VM_FAULT_ERROR) {
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07004496 err = vm_fault_to_errno(ret, flags);
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004497 remainder = 0;
4498 break;
4499 }
4500 if (ret & VM_FAULT_RETRY) {
Peter Xu4f6da932020-04-01 21:07:58 -07004501 if (locked &&
Andrea Arcangeli1ac25012019-02-01 14:20:16 -08004502 !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Peter Xu4f6da932020-04-01 21:07:58 -07004503 *locked = 0;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004504 *nr_pages = 0;
4505 /*
4506 * VM_FAULT_RETRY must not return an
4507 * error, it will return zero
4508 * instead.
4509 *
4510 * No need to update "position" as the
4511 * caller will not check it after
4512 * *nr_pages is set to 0.
4513 */
4514 return i;
4515 }
4516 continue;
Adam Litke4c887262005-10-29 18:16:46 -07004517 }
David Gibson63551ae2005-06-21 17:14:44 -07004518
Andi Kleena5516432008-07-23 21:27:41 -07004519 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07004520 page = pte_page(huge_ptep_get(pte));
Linus Torvalds8fde12c2019-04-11 10:49:19 -07004521
4522 /*
Zhigang Luacbfb082019-11-30 17:57:06 -08004523 * If subpage information not requested, update counters
4524 * and skip the same_page loop below.
4525 */
4526 if (!pages && !vmas && !pfn_offset &&
4527 (vaddr + huge_page_size(h) < vma->vm_end) &&
4528 (remainder >= pages_per_huge_page(h))) {
4529 vaddr += huge_page_size(h);
4530 remainder -= pages_per_huge_page(h);
4531 i += pages_per_huge_page(h);
4532 spin_unlock(ptl);
4533 continue;
4534 }
4535
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004536same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08004537 if (pages) {
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004538 pages[i] = mem_map_offset(page, pfn_offset);
John Hubbard3faa52c2020-04-01 21:05:29 -07004539 /*
4540 * try_grab_page() should always succeed here, because:
4541 * a) we hold the ptl lock, and b) we've just checked
4542 * that the huge page is present in the page tables. If
4543 * the huge page is present, then the tail pages must
4544 * also be present. The ptl prevents the head page and
4545 * tail pages from being rearranged in any way. So this
4546 * page must be available at this point, unless the page
4547 * refcount overflowed:
4548 */
4549 if (WARN_ON_ONCE(!try_grab_page(pages[i], flags))) {
4550 spin_unlock(ptl);
4551 remainder = 0;
4552 err = -ENOMEM;
4553 break;
4554 }
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08004555 }
David Gibson63551ae2005-06-21 17:14:44 -07004556
4557 if (vmas)
4558 vmas[i] = vma;
4559
4560 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004561 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07004562 --remainder;
4563 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004564 if (vaddr < vma->vm_end && remainder &&
Andi Kleena5516432008-07-23 21:27:41 -07004565 pfn_offset < pages_per_huge_page(h)) {
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004566 /*
4567 * We use pfn_offset to avoid touching the pageframes
4568 * of this compound page.
4569 */
4570 goto same_page;
4571 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004572 spin_unlock(ptl);
David Gibson63551ae2005-06-21 17:14:44 -07004573 }
Michel Lespinasse28a35712013-02-22 16:35:55 -08004574 *nr_pages = remainder;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004575 /*
4576 * setting position is actually required only if remainder is
4577 * not zero but it's faster not to add a "if (remainder)"
4578 * branch.
4579 */
David Gibson63551ae2005-06-21 17:14:44 -07004580 *position = vaddr;
4581
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07004582 return i ? i : err;
David Gibson63551ae2005-06-21 17:14:44 -07004583}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004584
Aneesh Kumar K.V5491ae72016-07-13 15:06:43 +05304585#ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
4586/*
4587 * ARCHes with special requirements for evicting HUGETLB backing TLB entries can
4588 * implement this.
4589 */
4590#define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
4591#endif
4592
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004593unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004594 unsigned long address, unsigned long end, pgprot_t newprot)
4595{
4596 struct mm_struct *mm = vma->vm_mm;
4597 unsigned long start = address;
4598 pte_t *ptep;
4599 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07004600 struct hstate *h = hstate_vma(vma);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004601 unsigned long pages = 0;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004602 bool shared_pmd = false;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004603 struct mmu_notifier_range range;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004604
4605 /*
4606 * In the case of shared PMDs, the area to flush could be beyond
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004607 * start/end. Set range.start/range.end to cover the maximum possible
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004608 * range if PMD sharing is possible.
4609 */
Jérôme Glisse7269f992019-05-13 17:20:53 -07004610 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
4611 0, vma, mm, start, end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004612 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004613
4614 BUG_ON(address >= end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004615 flush_cache_range(vma, range.start, range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004616
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004617 mmu_notifier_invalidate_range_start(&range);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08004618 i_mmap_lock_write(vma->vm_file->f_mapping);
Andi Kleena5516432008-07-23 21:27:41 -07004619 for (; address < end; address += huge_page_size(h)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004620 spinlock_t *ptl;
Punit Agrawal7868a202017-07-06 15:39:42 -07004621 ptep = huge_pte_offset(mm, address, huge_page_size(h));
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004622 if (!ptep)
4623 continue;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004624 ptl = huge_pte_lock(h, mm, ptep);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004625 if (huge_pmd_unshare(mm, &address, ptep)) {
4626 pages++;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004627 spin_unlock(ptl);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004628 shared_pmd = true;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08004629 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004630 }
Naoya Horiguchia8bda282015-02-11 15:25:28 -08004631 pte = huge_ptep_get(ptep);
4632 if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
4633 spin_unlock(ptl);
4634 continue;
4635 }
4636 if (unlikely(is_hugetlb_entry_migration(pte))) {
4637 swp_entry_t entry = pte_to_swp_entry(pte);
4638
4639 if (is_write_migration_entry(entry)) {
4640 pte_t newpte;
4641
4642 make_migration_entry_read(&entry);
4643 newpte = swp_entry_to_pte(entry);
Punit Agrawale5251fd2017-07-06 15:39:50 -07004644 set_huge_swap_pte_at(mm, address, ptep,
4645 newpte, huge_page_size(h));
Naoya Horiguchia8bda282015-02-11 15:25:28 -08004646 pages++;
4647 }
4648 spin_unlock(ptl);
4649 continue;
4650 }
4651 if (!huge_pte_none(pte)) {
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08004652 pte_t old_pte;
4653
4654 old_pte = huge_ptep_modify_prot_start(vma, address, ptep);
4655 pte = pte_mkhuge(huge_pte_modify(old_pte, newprot));
Tony Lube7517d2013-02-04 14:28:46 -08004656 pte = arch_make_huge_pte(pte, vma, NULL, 0);
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08004657 huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004658 pages++;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004659 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004660 spin_unlock(ptl);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004661 }
Mel Gormand8333522012-07-31 16:46:20 -07004662 /*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08004663 * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
Mel Gormand8333522012-07-31 16:46:20 -07004664 * may have cleared our pud entry and done put_page on the page table:
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08004665 * once we release i_mmap_rwsem, another task can do the final put_page
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004666 * and that page table be reused and filled with junk. If we actually
4667 * did unshare a page of pmds, flush the range corresponding to the pud.
Mel Gormand8333522012-07-31 16:46:20 -07004668 */
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004669 if (shared_pmd)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004670 flush_hugetlb_tlb_range(vma, range.start, range.end);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004671 else
4672 flush_hugetlb_tlb_range(vma, start, end);
Jérôme Glisse0f108512017-11-15 17:34:07 -08004673 /*
4674 * No need to call mmu_notifier_invalidate_range() we are downgrading
4675 * page table protection not changing it to point to a new page.
4676 *
Mike Rapoportad56b732018-03-21 21:22:47 +02004677 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08004678 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08004679 i_mmap_unlock_write(vma->vm_file->f_mapping);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004680 mmu_notifier_invalidate_range_end(&range);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004681
4682 return pages << h->order;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004683}
4684
Mel Gormana1e78772008-07-23 21:27:23 -07004685int hugetlb_reserve_pages(struct inode *inode,
4686 long from, long to,
Mel Gorman5a6fe122009-02-10 14:02:27 +00004687 struct vm_area_struct *vma,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09004688 vm_flags_t vm_flags)
Adam Litkee4e574b2007-10-16 01:26:19 -07004689{
Mel Gorman17c9d122009-02-11 16:34:16 +00004690 long ret, chg;
Andi Kleena5516432008-07-23 21:27:41 -07004691 struct hstate *h = hstate_inode(inode);
David Gibson90481622012-03-21 16:34:12 -07004692 struct hugepage_subpool *spool = subpool_inode(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004693 struct resv_map *resv_map;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004694 long gbl_reserve;
Adam Litkee4e574b2007-10-16 01:26:19 -07004695
Mike Kravetz63489f82018-03-22 16:17:13 -07004696 /* This should never happen */
4697 if (from > to) {
4698 VM_WARN(1, "%s called with a negative range\n", __func__);
4699 return -EINVAL;
4700 }
4701
Mel Gormana1e78772008-07-23 21:27:23 -07004702 /*
Mel Gorman17c9d122009-02-11 16:34:16 +00004703 * Only apply hugepage reservation if asked. At fault time, an
4704 * attempt will be made for VM_NORESERVE to allocate a page
David Gibson90481622012-03-21 16:34:12 -07004705 * without using reserves
Mel Gorman17c9d122009-02-11 16:34:16 +00004706 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09004707 if (vm_flags & VM_NORESERVE)
Mel Gorman17c9d122009-02-11 16:34:16 +00004708 return 0;
4709
4710 /*
Mel Gormana1e78772008-07-23 21:27:23 -07004711 * Shared mappings base their reservation on the number of pages that
4712 * are already allocated on behalf of the file. Private mappings need
4713 * to reserve the full area even if read-only as mprotect() may be
4714 * called to make the mapping read-write. Assume !vma is a shm mapping
4715 */
Joonsoo Kim9119a412014-04-03 14:47:25 -07004716 if (!vma || vma->vm_flags & VM_MAYSHARE) {
Mike Kravetzf27a5132019-05-13 17:22:55 -07004717 /*
4718 * resv_map can not be NULL as hugetlb_reserve_pages is only
4719 * called for inodes for which resv_maps were created (see
4720 * hugetlbfs_get_inode).
4721 */
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004722 resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004723
Joonsoo Kim1406ec92014-04-03 14:47:26 -07004724 chg = region_chg(resv_map, from, to);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004725
4726 } else {
4727 resv_map = resv_map_alloc();
Mel Gorman5a6fe122009-02-10 14:02:27 +00004728 if (!resv_map)
4729 return -ENOMEM;
4730
Mel Gorman17c9d122009-02-11 16:34:16 +00004731 chg = to - from;
4732
Mel Gorman5a6fe122009-02-10 14:02:27 +00004733 set_vma_resv_map(vma, resv_map);
4734 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
4735 }
4736
Dave Hansenc50ac052012-05-29 15:06:46 -07004737 if (chg < 0) {
4738 ret = chg;
4739 goto out_err;
4740 }
Mel Gorman17c9d122009-02-11 16:34:16 +00004741
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004742 /*
4743 * There must be enough pages in the subpool for the mapping. If
4744 * the subpool has a minimum size, there may be some global
4745 * reservations already in place (gbl_reserve).
4746 */
4747 gbl_reserve = hugepage_subpool_get_pages(spool, chg);
4748 if (gbl_reserve < 0) {
Dave Hansenc50ac052012-05-29 15:06:46 -07004749 ret = -ENOSPC;
4750 goto out_err;
4751 }
Mel Gorman17c9d122009-02-11 16:34:16 +00004752
4753 /*
4754 * Check enough hugepages are available for the reservation.
David Gibson90481622012-03-21 16:34:12 -07004755 * Hand the pages back to the subpool if there are not
Mel Gorman17c9d122009-02-11 16:34:16 +00004756 */
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004757 ret = hugetlb_acct_memory(h, gbl_reserve);
Mel Gorman17c9d122009-02-11 16:34:16 +00004758 if (ret < 0) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004759 /* put back original number of pages, chg */
4760 (void)hugepage_subpool_put_pages(spool, chg);
Dave Hansenc50ac052012-05-29 15:06:46 -07004761 goto out_err;
Mel Gorman17c9d122009-02-11 16:34:16 +00004762 }
4763
4764 /*
4765 * Account for the reservations made. Shared mappings record regions
4766 * that have reservations as they are shared by multiple VMAs.
4767 * When the last VMA disappears, the region map says how much
4768 * the reservation was and the page cache tells how much of
4769 * the reservation was consumed. Private mappings are per-VMA and
4770 * only the consumed reservations are tracked. When the VMA
4771 * disappears, the original reservation is the VMA size and the
4772 * consumed reservations are stored in the map. Hence, nothing
4773 * else has to be done for private mappings here
4774 */
Mike Kravetz33039672015-06-24 16:57:58 -07004775 if (!vma || vma->vm_flags & VM_MAYSHARE) {
4776 long add = region_add(resv_map, from, to);
4777
4778 if (unlikely(chg > add)) {
4779 /*
4780 * pages in this range were added to the reserve
4781 * map between region_chg and region_add. This
4782 * indicates a race with alloc_huge_page. Adjust
4783 * the subpool and reserve counts modified above
4784 * based on the difference.
4785 */
4786 long rsv_adjust;
4787
4788 rsv_adjust = hugepage_subpool_put_pages(spool,
4789 chg - add);
4790 hugetlb_acct_memory(h, -rsv_adjust);
4791 }
4792 }
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07004793 return 0;
Dave Hansenc50ac052012-05-29 15:06:46 -07004794out_err:
Mike Kravetz5e911372015-09-08 15:01:28 -07004795 if (!vma || vma->vm_flags & VM_MAYSHARE)
Mike Kravetzff8c0c52017-03-31 15:12:07 -07004796 /* Don't call region_abort if region_chg failed */
4797 if (chg >= 0)
4798 region_abort(resv_map, from, to);
Joonsoo Kimf031dd22014-04-03 14:47:28 -07004799 if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
4800 kref_put(&resv_map->refs, resv_map_release);
Dave Hansenc50ac052012-05-29 15:06:46 -07004801 return ret;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07004802}
4803
Mike Kravetzb5cec282015-09-08 15:01:41 -07004804long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
4805 long freed)
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07004806{
Andi Kleena5516432008-07-23 21:27:41 -07004807 struct hstate *h = hstate_inode(inode);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004808 struct resv_map *resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004809 long chg = 0;
David Gibson90481622012-03-21 16:34:12 -07004810 struct hugepage_subpool *spool = subpool_inode(inode);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004811 long gbl_reserve;
Ken Chen45c682a2007-11-14 16:59:44 -08004812
Mike Kravetzf27a5132019-05-13 17:22:55 -07004813 /*
4814 * Since this routine can be called in the evict inode path for all
4815 * hugetlbfs inodes, resv_map could be NULL.
4816 */
Mike Kravetzb5cec282015-09-08 15:01:41 -07004817 if (resv_map) {
4818 chg = region_del(resv_map, start, end);
4819 /*
4820 * region_del() can fail in the rare case where a region
4821 * must be split and another region descriptor can not be
4822 * allocated. If end == LONG_MAX, it will not fail.
4823 */
4824 if (chg < 0)
4825 return chg;
4826 }
4827
Ken Chen45c682a2007-11-14 16:59:44 -08004828 spin_lock(&inode->i_lock);
Eric Sandeene4c6f8b2009-07-29 15:02:16 -07004829 inode->i_blocks -= (blocks_per_huge_page(h) * freed);
Ken Chen45c682a2007-11-14 16:59:44 -08004830 spin_unlock(&inode->i_lock);
4831
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004832 /*
4833 * If the subpool has a minimum size, the number of global
4834 * reservations to be released may be adjusted.
4835 */
4836 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
4837 hugetlb_acct_memory(h, -gbl_reserve);
Mike Kravetzb5cec282015-09-08 15:01:41 -07004838
4839 return 0;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07004840}
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09004841
Steve Capper3212b532013-04-23 12:35:02 +01004842#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
4843static unsigned long page_table_shareable(struct vm_area_struct *svma,
4844 struct vm_area_struct *vma,
4845 unsigned long addr, pgoff_t idx)
4846{
4847 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
4848 svma->vm_start;
4849 unsigned long sbase = saddr & PUD_MASK;
4850 unsigned long s_end = sbase + PUD_SIZE;
4851
4852 /* Allow segments to share if only one is marked locked */
Eric B Munsonde60f5f2015-11-05 18:51:36 -08004853 unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
4854 unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK;
Steve Capper3212b532013-04-23 12:35:02 +01004855
4856 /*
4857 * match the virtual addresses, permission and the alignment of the
4858 * page table page.
4859 */
4860 if (pmd_index(addr) != pmd_index(saddr) ||
4861 vm_flags != svm_flags ||
4862 sbase < svma->vm_start || svma->vm_end < s_end)
4863 return 0;
4864
4865 return saddr;
4866}
4867
Nicholas Krause31aafb42015-09-04 15:47:58 -07004868static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr)
Steve Capper3212b532013-04-23 12:35:02 +01004869{
4870 unsigned long base = addr & PUD_MASK;
4871 unsigned long end = base + PUD_SIZE;
4872
4873 /*
4874 * check on proper vm_flags and page table alignment
4875 */
Mike Kravetz017b1662018-10-05 15:51:29 -07004876 if (vma->vm_flags & VM_MAYSHARE && range_in_vma(vma, base, end))
Nicholas Krause31aafb42015-09-04 15:47:58 -07004877 return true;
4878 return false;
Steve Capper3212b532013-04-23 12:35:02 +01004879}
4880
4881/*
Mike Kravetz017b1662018-10-05 15:51:29 -07004882 * Determine if start,end range within vma could be mapped by shared pmd.
4883 * If yes, adjust start and end to cover range associated with possible
4884 * shared pmd mappings.
4885 */
4886void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
4887 unsigned long *start, unsigned long *end)
4888{
4889 unsigned long check_addr = *start;
4890
4891 if (!(vma->vm_flags & VM_MAYSHARE))
4892 return;
4893
4894 for (check_addr = *start; check_addr < *end; check_addr += PUD_SIZE) {
4895 unsigned long a_start = check_addr & PUD_MASK;
4896 unsigned long a_end = a_start + PUD_SIZE;
4897
4898 /*
4899 * If sharing is possible, adjust start/end if necessary.
4900 */
4901 if (range_in_vma(vma, a_start, a_end)) {
4902 if (a_start < *start)
4903 *start = a_start;
4904 if (a_end > *end)
4905 *end = a_end;
4906 }
4907 }
4908}
4909
4910/*
Steve Capper3212b532013-04-23 12:35:02 +01004911 * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
4912 * and returns the corresponding pte. While this is not necessary for the
4913 * !shared pmd case because we can allocate the pmd later as well, it makes the
Mike Kravetzc0d03812020-04-01 21:11:05 -07004914 * code much cleaner.
4915 *
4916 * This routine must be called with i_mmap_rwsem held in at least read mode.
4917 * For hugetlbfs, this prevents removal of any page table entries associated
4918 * with the address space. This is important as we are setting up sharing
4919 * based on existing page table entries (mappings).
Steve Capper3212b532013-04-23 12:35:02 +01004920 */
4921pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
4922{
4923 struct vm_area_struct *vma = find_vma(mm, addr);
4924 struct address_space *mapping = vma->vm_file->f_mapping;
4925 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
4926 vma->vm_pgoff;
4927 struct vm_area_struct *svma;
4928 unsigned long saddr;
4929 pte_t *spte = NULL;
4930 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004931 spinlock_t *ptl;
Steve Capper3212b532013-04-23 12:35:02 +01004932
4933 if (!vma_shareable(vma, addr))
4934 return (pte_t *)pmd_alloc(mm, pud, addr);
4935
Steve Capper3212b532013-04-23 12:35:02 +01004936 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
4937 if (svma == vma)
4938 continue;
4939
4940 saddr = page_table_shareable(svma, vma, addr, idx);
4941 if (saddr) {
Punit Agrawal7868a202017-07-06 15:39:42 -07004942 spte = huge_pte_offset(svma->vm_mm, saddr,
4943 vma_mmu_pagesize(svma));
Steve Capper3212b532013-04-23 12:35:02 +01004944 if (spte) {
4945 get_page(virt_to_page(spte));
4946 break;
4947 }
4948 }
4949 }
4950
4951 if (!spte)
4952 goto out;
4953
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08004954 ptl = huge_pte_lock(hstate_vma(vma), mm, spte);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004955 if (pud_none(*pud)) {
Steve Capper3212b532013-04-23 12:35:02 +01004956 pud_populate(mm, pud,
4957 (pmd_t *)((unsigned long)spte & PAGE_MASK));
Kirill A. Shutemovc17b1f42016-06-24 14:49:51 -07004958 mm_inc_nr_pmds(mm);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004959 } else {
Steve Capper3212b532013-04-23 12:35:02 +01004960 put_page(virt_to_page(spte));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004961 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004962 spin_unlock(ptl);
Steve Capper3212b532013-04-23 12:35:02 +01004963out:
4964 pte = (pte_t *)pmd_alloc(mm, pud, addr);
Steve Capper3212b532013-04-23 12:35:02 +01004965 return pte;
4966}
4967
4968/*
4969 * unmap huge page backed by shared pte.
4970 *
4971 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
4972 * indicated by page_count > 1, unmap is achieved by clearing pud and
4973 * decrementing the ref count. If count == 1, the pte page is not shared.
4974 *
Mike Kravetzc0d03812020-04-01 21:11:05 -07004975 * Called with page table lock held and i_mmap_rwsem held in write mode.
Steve Capper3212b532013-04-23 12:35:02 +01004976 *
4977 * returns: 1 successfully unmapped a shared pte page
4978 * 0 the underlying pte page is not shared, or it is the last user
4979 */
4980int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
4981{
4982 pgd_t *pgd = pgd_offset(mm, *addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004983 p4d_t *p4d = p4d_offset(pgd, *addr);
4984 pud_t *pud = pud_offset(p4d, *addr);
Steve Capper3212b532013-04-23 12:35:02 +01004985
4986 BUG_ON(page_count(virt_to_page(ptep)) == 0);
4987 if (page_count(virt_to_page(ptep)) == 1)
4988 return 0;
4989
4990 pud_clear(pud);
4991 put_page(virt_to_page(ptep));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004992 mm_dec_nr_pmds(mm);
Steve Capper3212b532013-04-23 12:35:02 +01004993 *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
4994 return 1;
4995}
Steve Capper9e5fc742013-04-30 08:02:03 +01004996#define want_pmd_share() (1)
4997#else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
4998pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
4999{
5000 return NULL;
5001}
Zhang Zhene81f2d22015-06-24 16:56:13 -07005002
5003int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
5004{
5005 return 0;
5006}
Mike Kravetz017b1662018-10-05 15:51:29 -07005007
5008void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
5009 unsigned long *start, unsigned long *end)
5010{
5011}
Steve Capper9e5fc742013-04-30 08:02:03 +01005012#define want_pmd_share() (0)
Steve Capper3212b532013-04-23 12:35:02 +01005013#endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
5014
Steve Capper9e5fc742013-04-30 08:02:03 +01005015#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
5016pte_t *huge_pte_alloc(struct mm_struct *mm,
5017 unsigned long addr, unsigned long sz)
5018{
5019 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005020 p4d_t *p4d;
Steve Capper9e5fc742013-04-30 08:02:03 +01005021 pud_t *pud;
5022 pte_t *pte = NULL;
5023
5024 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovf4f0a3d2017-11-29 16:11:30 -08005025 p4d = p4d_alloc(mm, pgd, addr);
5026 if (!p4d)
5027 return NULL;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005028 pud = pud_alloc(mm, p4d, addr);
Steve Capper9e5fc742013-04-30 08:02:03 +01005029 if (pud) {
5030 if (sz == PUD_SIZE) {
5031 pte = (pte_t *)pud;
5032 } else {
5033 BUG_ON(sz != PMD_SIZE);
5034 if (want_pmd_share() && pud_none(*pud))
5035 pte = huge_pmd_share(mm, addr, pud);
5036 else
5037 pte = (pte_t *)pmd_alloc(mm, pud, addr);
5038 }
5039 }
Michal Hocko4e666312016-08-02 14:02:34 -07005040 BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte));
Steve Capper9e5fc742013-04-30 08:02:03 +01005041
5042 return pte;
5043}
5044
Punit Agrawal9b19df22017-09-06 16:21:01 -07005045/*
5046 * huge_pte_offset() - Walk the page table to resolve the hugepage
5047 * entry at address @addr
5048 *
5049 * Return: Pointer to page table or swap entry (PUD or PMD) for
5050 * address @addr, or NULL if a p*d_none() entry is encountered and the
5051 * size @sz doesn't match the hugepage size at this level of the page
5052 * table.
5053 */
Punit Agrawal7868a202017-07-06 15:39:42 -07005054pte_t *huge_pte_offset(struct mm_struct *mm,
5055 unsigned long addr, unsigned long sz)
Steve Capper9e5fc742013-04-30 08:02:03 +01005056{
5057 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005058 p4d_t *p4d;
Steve Capper9e5fc742013-04-30 08:02:03 +01005059 pud_t *pud;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005060 pmd_t *pmd;
Steve Capper9e5fc742013-04-30 08:02:03 +01005061
5062 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005063 if (!pgd_present(*pgd))
5064 return NULL;
5065 p4d = p4d_offset(pgd, addr);
5066 if (!p4d_present(*p4d))
5067 return NULL;
Punit Agrawal9b19df22017-09-06 16:21:01 -07005068
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005069 pud = pud_offset(p4d, addr);
Punit Agrawal9b19df22017-09-06 16:21:01 -07005070 if (sz != PUD_SIZE && pud_none(*pud))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005071 return NULL;
Punit Agrawal9b19df22017-09-06 16:21:01 -07005072 /* hugepage or swap? */
5073 if (pud_huge(*pud) || !pud_present(*pud))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005074 return (pte_t *)pud;
Punit Agrawal9b19df22017-09-06 16:21:01 -07005075
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005076 pmd = pmd_offset(pud, addr);
Punit Agrawal9b19df22017-09-06 16:21:01 -07005077 if (sz != PMD_SIZE && pmd_none(*pmd))
5078 return NULL;
5079 /* hugepage or swap? */
5080 if (pmd_huge(*pmd) || !pmd_present(*pmd))
5081 return (pte_t *)pmd;
5082
5083 return NULL;
Steve Capper9e5fc742013-04-30 08:02:03 +01005084}
5085
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08005086#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
5087
5088/*
5089 * These functions are overwritable if your architecture needs its own
5090 * behavior.
5091 */
5092struct page * __weak
5093follow_huge_addr(struct mm_struct *mm, unsigned long address,
5094 int write)
5095{
5096 return ERR_PTR(-EINVAL);
5097}
5098
5099struct page * __weak
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -07005100follow_huge_pd(struct vm_area_struct *vma,
5101 unsigned long address, hugepd_t hpd, int flags, int pdshift)
5102{
5103 WARN(1, "hugepd follow called with no support for hugepage directory format\n");
5104 return NULL;
5105}
5106
5107struct page * __weak
Steve Capper9e5fc742013-04-30 08:02:03 +01005108follow_huge_pmd(struct mm_struct *mm, unsigned long address,
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005109 pmd_t *pmd, int flags)
Steve Capper9e5fc742013-04-30 08:02:03 +01005110{
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005111 struct page *page = NULL;
5112 spinlock_t *ptl;
Naoya Horiguchic9d398f2017-03-31 15:11:55 -07005113 pte_t pte;
John Hubbard3faa52c2020-04-01 21:05:29 -07005114
5115 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
5116 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
5117 (FOLL_PIN | FOLL_GET)))
5118 return NULL;
5119
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005120retry:
5121 ptl = pmd_lockptr(mm, pmd);
5122 spin_lock(ptl);
5123 /*
5124 * make sure that the address range covered by this pmd is not
5125 * unmapped from other threads.
5126 */
5127 if (!pmd_huge(*pmd))
5128 goto out;
Naoya Horiguchic9d398f2017-03-31 15:11:55 -07005129 pte = huge_ptep_get((pte_t *)pmd);
5130 if (pte_present(pte)) {
Gerald Schaefer97534122015-04-14 15:42:30 -07005131 page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbard3faa52c2020-04-01 21:05:29 -07005132 /*
5133 * try_grab_page() should always succeed here, because: a) we
5134 * hold the pmd (ptl) lock, and b) we've just checked that the
5135 * huge pmd (head) page is present in the page tables. The ptl
5136 * prevents the head page and tail pages from being rearranged
5137 * in any way. So this page must be available at this point,
5138 * unless the page refcount overflowed:
5139 */
5140 if (WARN_ON_ONCE(!try_grab_page(page, flags))) {
5141 page = NULL;
5142 goto out;
5143 }
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005144 } else {
Naoya Horiguchic9d398f2017-03-31 15:11:55 -07005145 if (is_hugetlb_entry_migration(pte)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005146 spin_unlock(ptl);
5147 __migration_entry_wait(mm, (pte_t *)pmd, ptl);
5148 goto retry;
5149 }
5150 /*
5151 * hwpoisoned entry is treated as no_page_table in
5152 * follow_page_mask().
5153 */
5154 }
5155out:
5156 spin_unlock(ptl);
Steve Capper9e5fc742013-04-30 08:02:03 +01005157 return page;
5158}
5159
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08005160struct page * __weak
Steve Capper9e5fc742013-04-30 08:02:03 +01005161follow_huge_pud(struct mm_struct *mm, unsigned long address,
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005162 pud_t *pud, int flags)
Steve Capper9e5fc742013-04-30 08:02:03 +01005163{
John Hubbard3faa52c2020-04-01 21:05:29 -07005164 if (flags & (FOLL_GET | FOLL_PIN))
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005165 return NULL;
Steve Capper9e5fc742013-04-30 08:02:03 +01005166
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005167 return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
Steve Capper9e5fc742013-04-30 08:02:03 +01005168}
5169
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -07005170struct page * __weak
5171follow_huge_pgd(struct mm_struct *mm, unsigned long address, pgd_t *pgd, int flags)
5172{
John Hubbard3faa52c2020-04-01 21:05:29 -07005173 if (flags & (FOLL_GET | FOLL_PIN))
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -07005174 return NULL;
5175
5176 return pte_page(*(pte_t *)pgd) + ((address & ~PGDIR_MASK) >> PAGE_SHIFT);
5177}
5178
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005179bool isolate_huge_page(struct page *page, struct list_head *list)
5180{
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005181 bool ret = true;
5182
Sasha Levin309381fea2014-01-23 15:52:54 -08005183 VM_BUG_ON_PAGE(!PageHead(page), page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005184 spin_lock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005185 if (!page_huge_active(page) || !get_page_unless_zero(page)) {
5186 ret = false;
5187 goto unlock;
5188 }
5189 clear_page_huge_active(page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005190 list_move_tail(&page->lru, list);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005191unlock:
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005192 spin_unlock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005193 return ret;
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005194}
5195
5196void putback_active_hugepage(struct page *page)
5197{
Sasha Levin309381fea2014-01-23 15:52:54 -08005198 VM_BUG_ON_PAGE(!PageHead(page), page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005199 spin_lock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005200 set_page_huge_active(page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005201 list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
5202 spin_unlock(&hugetlb_lock);
5203 put_page(page);
5204}
Michal Hockoab5ac902018-01-31 16:20:48 -08005205
5206void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
5207{
5208 struct hstate *h = page_hstate(oldpage);
5209
5210 hugetlb_cgroup_migrate(oldpage, newpage);
5211 set_page_owner_migrate_reason(newpage, reason);
5212
5213 /*
5214 * transfer temporary state of the new huge page. This is
5215 * reverse to other transitions because the newpage is going to
5216 * be final while the old one will be freed so it takes over
5217 * the temporary status.
5218 *
5219 * Also note that we have to transfer the per-node surplus state
5220 * here as well otherwise the global surplus count will not match
5221 * the per-node's.
5222 */
5223 if (PageHugeTemporary(newpage)) {
5224 int old_nid = page_to_nid(oldpage);
5225 int new_nid = page_to_nid(newpage);
5226
5227 SetPageHugeTemporary(oldpage);
5228 ClearPageHugeTemporary(newpage);
5229
5230 spin_lock(&hugetlb_lock);
5231 if (h->surplus_huge_pages_node[old_nid]) {
5232 h->surplus_huge_pages_node[old_nid]--;
5233 h->surplus_huge_pages_node[new_nid]++;
5234 }
5235 spin_unlock(&hugetlb_lock);
5236 }
5237}