blob: 17178dbd1167047d31d3330210297f9e302c47e3 [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>
Linus Torvaldsd6606682008-08-06 12:04:54 -070030
David Gibson63551ae2005-06-21 17:14:44 -070031#include <asm/page.h>
32#include <asm/pgtable.h>
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070033#include <asm/tlb.h>
David Gibson63551ae2005-06-21 17:14:44 -070034
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070035#include <linux/io.h>
David Gibson63551ae2005-06-21 17:14:44 -070036#include <linux/hugetlb.h>
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -070037#include <linux/hugetlb_cgroup.h>
Lee Schermerhorn9a3052302009-12-14 17:58:25 -080038#include <linux/node.h>
Mike Kravetz1a1aad82017-02-22 15:43:01 -080039#include <linux/userfaultfd_k.h>
Michal Hockoab5ac902018-01-31 16:20:48 -080040#include <linux/page_owner.h>
Nick Piggin7835e982006-03-22 00:08:40 -080041#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070043int hugetlb_max_hstate __read_mostly;
Andi Kleene5ff2152008-07-23 21:27:42 -070044unsigned int default_hstate_idx;
45struct hstate hstates[HUGE_MAX_HSTATE];
Naoya Horiguchi641844f2015-06-24 16:56:59 -070046/*
47 * Minimum page order among possible hugepage sizes, set to a proper value
48 * at boot time.
49 */
50static unsigned int minimum_order __read_mostly = UINT_MAX;
Andi Kleene5ff2152008-07-23 21:27:42 -070051
Jon Tollefson53ba51d2008-07-23 21:27:52 -070052__initdata LIST_HEAD(huge_boot_pages);
53
Andi Kleene5ff2152008-07-23 21:27:42 -070054/* for command line parsing */
55static struct hstate * __initdata parsed_hstate;
56static unsigned long __initdata default_hstate_max_huge_pages;
Nick Piggine11bfbf2008-07-23 21:27:52 -070057static unsigned long __initdata default_hstate_size;
Vaishali Thakkar9fee0212016-05-19 17:11:04 -070058static bool __initdata parsed_valid_hugepagesz = true;
Andi Kleene5ff2152008-07-23 21:27:42 -070059
David Gibson3935baa2006-03-22 00:08:53 -080060/*
Naoya Horiguchi31caf662013-09-11 14:21:59 -070061 * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
62 * free_huge_pages, and surplus_huge_pages.
David Gibson3935baa2006-03-22 00:08:53 -080063 */
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070064DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080065
Davidlohr Bueso8382d912014-04-03 14:47:31 -070066/*
67 * Serializes faults on the same logical page. This is used to
68 * prevent spurious OOMs when the hugepage pool is fully utilized.
69 */
70static int num_fault_mutexes;
Mike Kravetzc672c7f2015-09-08 15:01:35 -070071struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp;
Davidlohr Bueso8382d912014-04-03 14:47:31 -070072
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070073/* Forward declaration */
74static int hugetlb_acct_memory(struct hstate *h, long delta);
75
David Gibson90481622012-03-21 16:34:12 -070076static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
77{
78 bool free = (spool->count == 0) && (spool->used_hpages == 0);
79
80 spin_unlock(&spool->lock);
81
82 /* If no pages are used, and no other handles to the subpool
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070083 * remain, give up any reservations mased on minimum size and
84 * free the subpool */
85 if (free) {
86 if (spool->min_hpages != -1)
87 hugetlb_acct_memory(spool->hstate,
88 -spool->min_hpages);
David Gibson90481622012-03-21 16:34:12 -070089 kfree(spool);
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070090 }
David Gibson90481622012-03-21 16:34:12 -070091}
92
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070093struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
94 long min_hpages)
David Gibson90481622012-03-21 16:34:12 -070095{
96 struct hugepage_subpool *spool;
97
Mike Kravetzc6a91822015-04-15 16:13:36 -070098 spool = kzalloc(sizeof(*spool), GFP_KERNEL);
David Gibson90481622012-03-21 16:34:12 -070099 if (!spool)
100 return NULL;
101
102 spin_lock_init(&spool->lock);
103 spool->count = 1;
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -0700104 spool->max_hpages = max_hpages;
105 spool->hstate = h;
106 spool->min_hpages = min_hpages;
107
108 if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) {
109 kfree(spool);
110 return NULL;
111 }
112 spool->rsv_hpages = min_hpages;
David Gibson90481622012-03-21 16:34:12 -0700113
114 return spool;
115}
116
117void hugepage_put_subpool(struct hugepage_subpool *spool)
118{
119 spin_lock(&spool->lock);
120 BUG_ON(!spool->count);
121 spool->count--;
122 unlock_or_release_subpool(spool);
123}
124
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700125/*
126 * Subpool accounting for allocating and reserving pages.
127 * Return -ENOMEM if there are not enough resources to satisfy the
128 * the request. Otherwise, return the number of pages by which the
129 * global pools must be adjusted (upward). The returned value may
130 * only be different than the passed value (delta) in the case where
131 * a subpool minimum size must be manitained.
132 */
133static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700134 long delta)
135{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700136 long ret = delta;
David Gibson90481622012-03-21 16:34:12 -0700137
138 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700139 return ret;
David Gibson90481622012-03-21 16:34:12 -0700140
141 spin_lock(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700142
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700143 if (spool->max_hpages != -1) { /* maximum size accounting */
144 if ((spool->used_hpages + delta) <= spool->max_hpages)
145 spool->used_hpages += delta;
146 else {
147 ret = -ENOMEM;
148 goto unlock_ret;
149 }
150 }
151
Mike Kravetz09a95e22016-05-19 17:11:01 -0700152 /* minimum size accounting */
153 if (spool->min_hpages != -1 && spool->rsv_hpages) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700154 if (delta > spool->rsv_hpages) {
155 /*
156 * Asking for more reserves than those already taken on
157 * behalf of subpool. Return difference.
158 */
159 ret = delta - spool->rsv_hpages;
160 spool->rsv_hpages = 0;
161 } else {
162 ret = 0; /* reserves already accounted for */
163 spool->rsv_hpages -= delta;
164 }
165 }
166
167unlock_ret:
168 spin_unlock(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700169 return ret;
170}
171
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700172/*
173 * Subpool accounting for freeing and unreserving pages.
174 * Return the number of global page reservations that must be dropped.
175 * The return value may only be different than the passed value (delta)
176 * in the case where a subpool minimum size must be maintained.
177 */
178static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700179 long delta)
180{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700181 long ret = delta;
182
David Gibson90481622012-03-21 16:34:12 -0700183 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700184 return delta;
David Gibson90481622012-03-21 16:34:12 -0700185
186 spin_lock(&spool->lock);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700187
188 if (spool->max_hpages != -1) /* maximum size accounting */
189 spool->used_hpages -= delta;
190
Mike Kravetz09a95e22016-05-19 17:11:01 -0700191 /* minimum size accounting */
192 if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700193 if (spool->rsv_hpages + delta <= spool->min_hpages)
194 ret = 0;
195 else
196 ret = spool->rsv_hpages + delta - spool->min_hpages;
197
198 spool->rsv_hpages += delta;
199 if (spool->rsv_hpages > spool->min_hpages)
200 spool->rsv_hpages = spool->min_hpages;
201 }
202
203 /*
204 * If hugetlbfs_put_super couldn't free spool due to an outstanding
205 * quota reference, free it now.
206 */
David Gibson90481622012-03-21 16:34:12 -0700207 unlock_or_release_subpool(spool);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700208
209 return ret;
David Gibson90481622012-03-21 16:34:12 -0700210}
211
212static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
213{
214 return HUGETLBFS_SB(inode->i_sb)->spool;
215}
216
217static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
218{
Al Viro496ad9a2013-01-23 17:07:38 -0500219 return subpool_inode(file_inode(vma->vm_file));
David Gibson90481622012-03-21 16:34:12 -0700220}
221
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700222/*
Andy Whitcroft96822902008-07-23 21:27:29 -0700223 * Region tracking -- allows tracking of reservations and instantiated pages
224 * across the pages in a mapping.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700225 *
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700226 * The region data structures are embedded into a resv_map and protected
227 * by a resv_map's lock. The set of regions within the resv_map represent
228 * reservations for huge pages, or huge pages that have already been
229 * instantiated within the map. The from and to elements are huge page
230 * indicies into the associated mapping. from indicates the starting index
231 * of the region. to represents the first index past the end of the region.
232 *
233 * For example, a file region structure with from == 0 and to == 4 represents
234 * four huge pages in a mapping. It is important to note that the to element
235 * represents the first element past the end of the region. This is used in
236 * arithmetic as 4(to) - 0(from) = 4 huge pages in the region.
237 *
238 * Interval notation of the form [from, to) will be used to indicate that
239 * the endpoint from is inclusive and to is exclusive.
Andy Whitcroft96822902008-07-23 21:27:29 -0700240 */
241struct file_region {
242 struct list_head link;
243 long from;
244 long to;
245};
246
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700247/*
248 * Add the huge page range represented by [f, t) to the reserve
Mina Almasry5c911952019-11-30 17:56:54 -0800249 * map. Existing regions will be expanded to accommodate the specified
250 * range, or a region will be taken from the cache. Sufficient regions
251 * must exist in the cache due to the previous call to region_chg with
252 * the same range.
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700253 *
254 * Return the number of new huge pages added to the map. This
255 * number is greater than or equal to zero.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700256 */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700257static long region_add(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700258{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700259 struct list_head *head = &resv->regions;
Andy Whitcroft96822902008-07-23 21:27:29 -0700260 struct file_region *rg, *nrg, *trg;
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700261 long add = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700262
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700263 spin_lock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700264 /* Locate the region we are either in or before. */
265 list_for_each_entry(rg, head, link)
266 if (f <= rg->to)
267 break;
268
Mike Kravetz5e911372015-09-08 15:01:28 -0700269 /*
270 * If no region exists which can be expanded to include the
Mina Almasry5c911952019-11-30 17:56:54 -0800271 * specified range, pull a region descriptor from the cache
272 * and use it for this range.
Mike Kravetz5e911372015-09-08 15:01:28 -0700273 */
274 if (&rg->link == head || t < rg->from) {
275 VM_BUG_ON(resv->region_cache_count <= 0);
276
277 resv->region_cache_count--;
278 nrg = list_first_entry(&resv->region_cache, struct file_region,
279 link);
280 list_del(&nrg->link);
281
282 nrg->from = f;
283 nrg->to = t;
284 list_add(&nrg->link, rg->link.prev);
285
286 add += t - f;
287 goto out_locked;
288 }
289
Andy Whitcroft96822902008-07-23 21:27:29 -0700290 /* Round our left edge to the current segment if it encloses us. */
291 if (f > rg->from)
292 f = rg->from;
293
294 /* Check for and consume any regions we now overlap with. */
295 nrg = rg;
296 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
297 if (&rg->link == head)
298 break;
299 if (rg->from > t)
300 break;
301
302 /* If this area reaches higher then extend our area to
303 * include it completely. If this is not the first area
304 * which we intend to reuse, free it. */
305 if (rg->to > t)
306 t = rg->to;
307 if (rg != nrg) {
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700308 /* Decrement return value by the deleted range.
309 * Another range will span this area so that by
310 * end of routine add will be >= zero
311 */
312 add -= (rg->to - rg->from);
Andy Whitcroft96822902008-07-23 21:27:29 -0700313 list_del(&rg->link);
314 kfree(rg);
315 }
316 }
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700317
318 add += (nrg->from - f); /* Added to beginning of region */
Andy Whitcroft96822902008-07-23 21:27:29 -0700319 nrg->from = f;
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700320 add += t - nrg->to; /* Added to end of region */
Andy Whitcroft96822902008-07-23 21:27:29 -0700321 nrg->to = t;
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700322
Mike Kravetz5e911372015-09-08 15:01:28 -0700323out_locked:
324 resv->adds_in_progress--;
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700325 spin_unlock(&resv->lock);
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700326 VM_BUG_ON(add < 0);
327 return add;
Andy Whitcroft96822902008-07-23 21:27:29 -0700328}
329
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700330/*
331 * Examine the existing reserve map and determine how many
332 * huge pages in the specified range [f, t) are NOT currently
333 * represented. This routine is called before a subsequent
334 * call to region_add that will actually modify the reserve
335 * map to add the specified range [f, t). region_chg does
336 * not change the number of huge pages represented by the
Mina Almasry5c911952019-11-30 17:56:54 -0800337 * map. A new file_region structure is added to the cache
338 * as a placeholder, so that the subsequent region_add
339 * call will have all the regions it needs and will not fail.
Mike Kravetz5e911372015-09-08 15:01:28 -0700340 *
341 * Returns the number of huge pages that need to be added to the existing
342 * reservation map for the range [f, t). This number is greater or equal to
343 * zero. -ENOMEM is returned if a new file_region structure or cache entry
344 * is needed and can not be allocated.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700345 */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700346static long region_chg(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700347{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700348 struct list_head *head = &resv->regions;
Mina Almasry5c911952019-11-30 17:56:54 -0800349 struct file_region *rg;
Andy Whitcroft96822902008-07-23 21:27:29 -0700350 long chg = 0;
351
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700352 spin_lock(&resv->lock);
Mike Kravetz5e911372015-09-08 15:01:28 -0700353retry_locked:
354 resv->adds_in_progress++;
355
356 /*
357 * Check for sufficient descriptors in the cache to accommodate
358 * the number of in progress add operations.
359 */
360 if (resv->adds_in_progress > resv->region_cache_count) {
361 struct file_region *trg;
362
363 VM_BUG_ON(resv->adds_in_progress - resv->region_cache_count > 1);
364 /* Must drop lock to allocate a new descriptor. */
365 resv->adds_in_progress--;
366 spin_unlock(&resv->lock);
367
368 trg = kmalloc(sizeof(*trg), GFP_KERNEL);
Mina Almasry5c911952019-11-30 17:56:54 -0800369 if (!trg)
Mike Kravetz5e911372015-09-08 15:01:28 -0700370 return -ENOMEM;
371
372 spin_lock(&resv->lock);
373 list_add(&trg->link, &resv->region_cache);
374 resv->region_cache_count++;
375 goto retry_locked;
376 }
377
Andy Whitcroft96822902008-07-23 21:27:29 -0700378 /* Locate the region we are before or in. */
379 list_for_each_entry(rg, head, link)
380 if (f <= rg->to)
381 break;
382
Andy Whitcroft96822902008-07-23 21:27:29 -0700383 /* Round our left edge to the current segment if it encloses us. */
384 if (f > rg->from)
385 f = rg->from;
386 chg = t - f;
387
388 /* Check for and consume any regions we now overlap with. */
389 list_for_each_entry(rg, rg->link.prev, link) {
390 if (&rg->link == head)
391 break;
392 if (rg->from > t)
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700393 goto out;
Andy Whitcroft96822902008-07-23 21:27:29 -0700394
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300395 /* We overlap with this area, if it extends further than
Andy Whitcroft96822902008-07-23 21:27:29 -0700396 * us then we must extend ourselves. Account for its
397 * existing reservation. */
398 if (rg->to > t) {
399 chg += rg->to - t;
400 t = rg->to;
401 }
402 chg -= rg->to - rg->from;
403 }
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700404
405out:
406 spin_unlock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700407 return chg;
408}
409
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700410/*
Mike Kravetz5e911372015-09-08 15:01:28 -0700411 * Abort the in progress add operation. The adds_in_progress field
412 * of the resv_map keeps track of the operations in progress between
413 * calls to region_chg and region_add. Operations are sometimes
414 * aborted after the call to region_chg. In such cases, region_abort
415 * is called to decrement the adds_in_progress counter.
416 *
417 * NOTE: The range arguments [f, t) are not needed or used in this
418 * routine. They are kept to make reading the calling code easier as
419 * arguments will match the associated region_chg call.
420 */
421static void region_abort(struct resv_map *resv, long f, long t)
422{
423 spin_lock(&resv->lock);
424 VM_BUG_ON(!resv->region_cache_count);
425 resv->adds_in_progress--;
426 spin_unlock(&resv->lock);
427}
428
429/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700430 * Delete the specified range [f, t) from the reserve map. If the
431 * t parameter is LONG_MAX, this indicates that ALL regions after f
432 * should be deleted. Locate the regions which intersect [f, t)
433 * and either trim, delete or split the existing regions.
434 *
435 * Returns the number of huge pages deleted from the reserve map.
436 * In the normal case, the return value is zero or more. In the
437 * case where a region must be split, a new region descriptor must
438 * be allocated. If the allocation fails, -ENOMEM will be returned.
439 * NOTE: If the parameter t == LONG_MAX, then we will never split
440 * a region and possibly return -ENOMEM. Callers specifying
441 * t == LONG_MAX do not need to check for -ENOMEM error.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700442 */
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700443static long region_del(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700444{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700445 struct list_head *head = &resv->regions;
Andy Whitcroft96822902008-07-23 21:27:29 -0700446 struct file_region *rg, *trg;
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700447 struct file_region *nrg = NULL;
448 long del = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700449
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700450retry:
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700451 spin_lock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700452 list_for_each_entry_safe(rg, trg, head, link) {
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800453 /*
454 * Skip regions before the range to be deleted. file_region
455 * ranges are normally of the form [from, to). However, there
456 * may be a "placeholder" entry in the map which is of the form
457 * (from, to) with from == to. Check for placeholder entries
458 * at the beginning of the range to be deleted.
459 */
460 if (rg->to <= f && (rg->to != rg->from || rg->to != f))
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700461 continue;
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800462
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700463 if (rg->from >= t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700464 break;
Andy Whitcroft96822902008-07-23 21:27:29 -0700465
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700466 if (f > rg->from && t < rg->to) { /* Must split region */
467 /*
468 * Check for an entry in the cache before dropping
469 * lock and attempting allocation.
470 */
471 if (!nrg &&
472 resv->region_cache_count > resv->adds_in_progress) {
473 nrg = list_first_entry(&resv->region_cache,
474 struct file_region,
475 link);
476 list_del(&nrg->link);
477 resv->region_cache_count--;
478 }
479
480 if (!nrg) {
481 spin_unlock(&resv->lock);
482 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
483 if (!nrg)
484 return -ENOMEM;
485 goto retry;
486 }
487
488 del += t - f;
489
490 /* New entry for end of split region */
491 nrg->from = t;
492 nrg->to = rg->to;
493 INIT_LIST_HEAD(&nrg->link);
494
495 /* Original entry is trimmed */
496 rg->to = f;
497
498 list_add(&nrg->link, &rg->link);
499 nrg = NULL;
500 break;
501 }
502
503 if (f <= rg->from && t >= rg->to) { /* Remove entire region */
504 del += rg->to - rg->from;
505 list_del(&rg->link);
506 kfree(rg);
507 continue;
508 }
509
510 if (f <= rg->from) { /* Trim beginning of region */
511 del += t - rg->from;
512 rg->from = t;
513 } else { /* Trim end of region */
514 del += rg->to - f;
515 rg->to = f;
516 }
Andy Whitcroft96822902008-07-23 21:27:29 -0700517 }
518
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700519 spin_unlock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700520 kfree(nrg);
521 return del;
Andy Whitcroft96822902008-07-23 21:27:29 -0700522}
523
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700524/*
Mike Kravetzb5cec282015-09-08 15:01:41 -0700525 * A rare out of memory error was encountered which prevented removal of
526 * the reserve map region for a page. The huge page itself was free'ed
527 * and removed from the page cache. This routine will adjust the subpool
528 * usage count, and the global reserve count if needed. By incrementing
529 * these counts, the reserve map entry which could not be deleted will
530 * appear as a "reserved" entry instead of simply dangling with incorrect
531 * counts.
532 */
zhong jiang72e29362016-10-07 17:02:01 -0700533void hugetlb_fix_reserve_counts(struct inode *inode)
Mike Kravetzb5cec282015-09-08 15:01:41 -0700534{
535 struct hugepage_subpool *spool = subpool_inode(inode);
536 long rsv_adjust;
537
538 rsv_adjust = hugepage_subpool_get_pages(spool, 1);
zhong jiang72e29362016-10-07 17:02:01 -0700539 if (rsv_adjust) {
Mike Kravetzb5cec282015-09-08 15:01:41 -0700540 struct hstate *h = hstate_inode(inode);
541
542 hugetlb_acct_memory(h, 1);
543 }
544}
545
546/*
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700547 * Count and return the number of huge pages in the reserve map
548 * that intersect with the range [f, t).
549 */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700550static long region_count(struct resv_map *resv, long f, long t)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700551{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700552 struct list_head *head = &resv->regions;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700553 struct file_region *rg;
554 long chg = 0;
555
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700556 spin_lock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700557 /* Locate each segment we overlap with, and count that overlap. */
558 list_for_each_entry(rg, head, link) {
Wang Sheng-Huif2135a42012-05-29 15:06:17 -0700559 long seg_from;
560 long seg_to;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700561
562 if (rg->to <= f)
563 continue;
564 if (rg->from >= t)
565 break;
566
567 seg_from = max(rg->from, f);
568 seg_to = min(rg->to, t);
569
570 chg += seg_to - seg_from;
571 }
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700572 spin_unlock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700573
574 return chg;
575}
576
Andy Whitcroft96822902008-07-23 21:27:29 -0700577/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700578 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700579 * the mapping, in pagecache page units; huge pages here.
580 */
Andi Kleena5516432008-07-23 21:27:41 -0700581static pgoff_t vma_hugecache_offset(struct hstate *h,
582 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700583{
Andi Kleena5516432008-07-23 21:27:41 -0700584 return ((address - vma->vm_start) >> huge_page_shift(h)) +
585 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700586}
587
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900588pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
589 unsigned long address)
590{
591 return vma_hugecache_offset(hstate_vma(vma), vma, address);
592}
Dan Williamsdee41072016-05-14 12:20:44 -0700593EXPORT_SYMBOL_GPL(linear_hugepage_index);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900594
Andy Whitcroft84afd992008-07-23 21:27:32 -0700595/*
Mel Gorman08fba692009-01-06 14:38:53 -0800596 * Return the size of the pages allocated when backing a VMA. In the majority
597 * cases this will be same size as used by the page table entries.
598 */
599unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
600{
Dan Williams05ea8862018-04-05 16:24:25 -0700601 if (vma->vm_ops && vma->vm_ops->pagesize)
602 return vma->vm_ops->pagesize(vma);
603 return PAGE_SIZE;
Mel Gorman08fba692009-01-06 14:38:53 -0800604}
Joerg Roedelf340ca02009-06-19 15:16:22 +0200605EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
Mel Gorman08fba692009-01-06 14:38:53 -0800606
607/*
Mel Gorman33402892009-01-06 14:38:54 -0800608 * Return the page size being used by the MMU to back a VMA. In the majority
609 * of cases, the page size used by the kernel matches the MMU size. On
Dan Williams09135cc2018-04-05 16:24:21 -0700610 * architectures where it differs, an architecture-specific 'strong'
611 * version of this symbol is required.
Mel Gorman33402892009-01-06 14:38:54 -0800612 */
Dan Williams09135cc2018-04-05 16:24:21 -0700613__weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
Mel Gorman33402892009-01-06 14:38:54 -0800614{
615 return vma_kernel_pagesize(vma);
616}
Mel Gorman33402892009-01-06 14:38:54 -0800617
618/*
Andy Whitcroft84afd992008-07-23 21:27:32 -0700619 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
620 * bits of the reservation map pointer, which are always clear due to
621 * alignment.
622 */
623#define HPAGE_RESV_OWNER (1UL << 0)
624#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700625#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700626
Mel Gormana1e78772008-07-23 21:27:23 -0700627/*
628 * These helpers are used to track how many pages are reserved for
629 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
630 * is guaranteed to have their future faults succeed.
631 *
632 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
633 * the reserve counters are updated with the hugetlb_lock held. It is safe
634 * to reset the VMA at fork() time as it is not in use yet and there is no
635 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700636 *
637 * The private mapping reservation is represented in a subtly different
638 * manner to a shared mapping. A shared mapping has a region map associated
639 * with the underlying file, this region map represents the backing file
640 * pages which have ever had a reservation assigned which this persists even
641 * after the page is instantiated. A private mapping has a region map
642 * associated with the original mmap which is attached to all VMAs which
643 * reference it, this region map represents those offsets which have consumed
644 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -0700645 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700646static unsigned long get_vma_private_data(struct vm_area_struct *vma)
647{
648 return (unsigned long)vma->vm_private_data;
649}
650
651static void set_vma_private_data(struct vm_area_struct *vma,
652 unsigned long value)
653{
654 vma->vm_private_data = (void *)value;
655}
656
Joonsoo Kim9119a412014-04-03 14:47:25 -0700657struct resv_map *resv_map_alloc(void)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700658{
659 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
Mike Kravetz5e911372015-09-08 15:01:28 -0700660 struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL);
661
662 if (!resv_map || !rg) {
663 kfree(resv_map);
664 kfree(rg);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700665 return NULL;
Mike Kravetz5e911372015-09-08 15:01:28 -0700666 }
Andy Whitcroft84afd992008-07-23 21:27:32 -0700667
668 kref_init(&resv_map->refs);
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700669 spin_lock_init(&resv_map->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700670 INIT_LIST_HEAD(&resv_map->regions);
671
Mike Kravetz5e911372015-09-08 15:01:28 -0700672 resv_map->adds_in_progress = 0;
673
674 INIT_LIST_HEAD(&resv_map->region_cache);
675 list_add(&rg->link, &resv_map->region_cache);
676 resv_map->region_cache_count = 1;
677
Andy Whitcroft84afd992008-07-23 21:27:32 -0700678 return resv_map;
679}
680
Joonsoo Kim9119a412014-04-03 14:47:25 -0700681void resv_map_release(struct kref *ref)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700682{
683 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
Mike Kravetz5e911372015-09-08 15:01:28 -0700684 struct list_head *head = &resv_map->region_cache;
685 struct file_region *rg, *trg;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700686
687 /* Clear out any active regions before we release the map. */
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700688 region_del(resv_map, 0, LONG_MAX);
Mike Kravetz5e911372015-09-08 15:01:28 -0700689
690 /* ... and any entries left in the cache */
691 list_for_each_entry_safe(rg, trg, head, link) {
692 list_del(&rg->link);
693 kfree(rg);
694 }
695
696 VM_BUG_ON(resv_map->adds_in_progress);
697
Andy Whitcroft84afd992008-07-23 21:27:32 -0700698 kfree(resv_map);
699}
700
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700701static inline struct resv_map *inode_resv_map(struct inode *inode)
702{
Mike Kravetzf27a5132019-05-13 17:22:55 -0700703 /*
704 * At inode evict time, i_mapping may not point to the original
705 * address space within the inode. This original address space
706 * contains the pointer to the resv_map. So, always use the
707 * address space embedded within the inode.
708 * The VERY common case is inode->mapping == &inode->i_data but,
709 * this may not be true for device special inodes.
710 */
711 return (struct resv_map *)(&inode->i_data)->private_data;
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700712}
713
Andy Whitcroft84afd992008-07-23 21:27:32 -0700714static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700715{
Sasha Levin81d1b092014-10-09 15:28:10 -0700716 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700717 if (vma->vm_flags & VM_MAYSHARE) {
718 struct address_space *mapping = vma->vm_file->f_mapping;
719 struct inode *inode = mapping->host;
720
721 return inode_resv_map(inode);
722
723 } else {
Andy Whitcroft84afd992008-07-23 21:27:32 -0700724 return (struct resv_map *)(get_vma_private_data(vma) &
725 ~HPAGE_RESV_MASK);
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700726 }
Mel Gormana1e78772008-07-23 21:27:23 -0700727}
728
Andy Whitcroft84afd992008-07-23 21:27:32 -0700729static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -0700730{
Sasha Levin81d1b092014-10-09 15:28:10 -0700731 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
732 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700733
Andy Whitcroft84afd992008-07-23 21:27:32 -0700734 set_vma_private_data(vma, (get_vma_private_data(vma) &
735 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700736}
737
738static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
739{
Sasha Levin81d1b092014-10-09 15:28:10 -0700740 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
741 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700742
743 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700744}
745
746static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
747{
Sasha Levin81d1b092014-10-09 15:28:10 -0700748 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700749
750 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700751}
752
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700753/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700754void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
755{
Sasha Levin81d1b092014-10-09 15:28:10 -0700756 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Mel Gormanf83a2752009-05-28 14:34:40 -0700757 if (!(vma->vm_flags & VM_MAYSHARE))
Mel Gormana1e78772008-07-23 21:27:23 -0700758 vma->vm_private_data = (void *)0;
759}
760
761/* Returns true if the VMA has associated reserve pages */
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700762static bool vma_has_reserves(struct vm_area_struct *vma, long chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700763{
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700764 if (vma->vm_flags & VM_NORESERVE) {
765 /*
766 * This address is already reserved by other process(chg == 0),
767 * so, we should decrement reserved count. Without decrementing,
768 * reserve count remains after releasing inode, because this
769 * allocated page will go into page cache and is regarded as
770 * coming from reserved pool in releasing step. Currently, we
771 * don't have any other solution to deal with this situation
772 * properly, so add work-around here.
773 */
774 if (vma->vm_flags & VM_MAYSHARE && chg == 0)
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700775 return true;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700776 else
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700777 return false;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700778 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700779
780 /* Shared mappings always use reserves */
Mike Kravetz1fb1b0e2015-09-08 15:01:44 -0700781 if (vma->vm_flags & VM_MAYSHARE) {
782 /*
783 * We know VM_NORESERVE is not set. Therefore, there SHOULD
784 * be a region map for all pages. The only situation where
785 * there is no region map is if a hole was punched via
786 * fallocate. In this case, there really are no reverves to
787 * use. This situation is indicated if chg != 0.
788 */
789 if (chg)
790 return false;
791 else
792 return true;
793 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700794
795 /*
796 * Only the process that called mmap() has reserves for
797 * private mappings.
798 */
Mike Kravetz67961f92016-06-08 15:33:42 -0700799 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
800 /*
801 * Like the shared case above, a hole punch or truncate
802 * could have been performed on the private mapping.
803 * Examine the value of chg to determine if reserves
804 * actually exist or were previously consumed.
805 * Very Subtle - The value of chg comes from a previous
806 * call to vma_needs_reserves(). The reserve map for
807 * private mappings has different (opposite) semantics
808 * than that of shared mappings. vma_needs_reserves()
809 * has already taken this difference in semantics into
810 * account. Therefore, the meaning of chg is the same
811 * as in the shared case above. Code could easily be
812 * combined, but keeping it separate draws attention to
813 * subtle differences.
814 */
815 if (chg)
816 return false;
817 else
818 return true;
819 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700820
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700821 return false;
Mel Gormana1e78772008-07-23 21:27:23 -0700822}
823
Andi Kleena5516432008-07-23 21:27:41 -0700824static void enqueue_huge_page(struct hstate *h, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 int nid = page_to_nid(page);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -0700827 list_move(&page->lru, &h->hugepage_freelists[nid]);
Andi Kleena5516432008-07-23 21:27:41 -0700828 h->free_huge_pages++;
829 h->free_huge_pages_node[nid]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700832static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900833{
834 struct page *page;
835
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700836 list_for_each_entry(page, &h->hugepage_freelists[nid], lru)
Naoya Horiguchi243abd52017-07-10 15:47:32 -0700837 if (!PageHWPoison(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700838 break;
839 /*
840 * if 'non-isolated free hugepage' not found on the list,
841 * the allocation fails.
842 */
843 if (&h->hugepage_freelists[nid] == &page->lru)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900844 return NULL;
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -0700845 list_move(&page->lru, &h->hugepage_activelist);
Naoya Horiguchia9869b82010-09-08 10:19:37 +0900846 set_page_refcounted(page);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900847 h->free_huge_pages--;
848 h->free_huge_pages_node[nid]--;
849 return page;
850}
851
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700852static struct page *dequeue_huge_page_nodemask(struct hstate *h, gfp_t gfp_mask, int nid,
853 nodemask_t *nmask)
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700854{
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700855 unsigned int cpuset_mems_cookie;
856 struct zonelist *zonelist;
857 struct zone *zone;
858 struct zoneref *z;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800859 int node = NUMA_NO_NODE;
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700860
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700861 zonelist = node_zonelist(nid, gfp_mask);
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700862
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700863retry_cpuset:
864 cpuset_mems_cookie = read_mems_allowed_begin();
865 for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) {
866 struct page *page;
867
868 if (!cpuset_zone_allowed(zone, gfp_mask))
869 continue;
870 /*
871 * no need to ask again on the same node. Pool is node rather than
872 * zone aware
873 */
874 if (zone_to_nid(zone) == node)
875 continue;
876 node = zone_to_nid(zone);
877
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700878 page = dequeue_huge_page_node_exact(h, node);
879 if (page)
880 return page;
881 }
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700882 if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie)))
883 goto retry_cpuset;
884
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700885 return NULL;
886}
887
Naoya Horiguchi86cdb462013-09-11 14:22:13 -0700888/* Movability of hugepages depends on migration support. */
889static inline gfp_t htlb_alloc_mask(struct hstate *h)
890{
Anshuman Khandual7ed2c312019-03-05 15:43:44 -0800891 if (hugepage_movable_supported(h))
Naoya Horiguchi86cdb462013-09-11 14:22:13 -0700892 return GFP_HIGHUSER_MOVABLE;
893 else
894 return GFP_HIGHUSER;
895}
896
Andi Kleena5516432008-07-23 21:27:41 -0700897static struct page *dequeue_huge_page_vma(struct hstate *h,
898 struct vm_area_struct *vma,
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700899 unsigned long address, int avoid_reserve,
900 long chg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700902 struct page *page;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700903 struct mempolicy *mpol;
Vlastimil Babka04ec6262017-07-06 15:40:03 -0700904 gfp_t gfp_mask;
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700905 nodemask_t *nodemask;
Vlastimil Babka04ec6262017-07-06 15:40:03 -0700906 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Mel Gormana1e78772008-07-23 21:27:23 -0700908 /*
909 * A child process with MAP_PRIVATE mappings created by their parent
910 * have no page reserves. This check ensures that reservations are
911 * not "stolen". The child may still get SIGKILLed
912 */
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700913 if (!vma_has_reserves(vma, chg) &&
Andi Kleena5516432008-07-23 21:27:41 -0700914 h->free_huge_pages - h->resv_huge_pages == 0)
Miao Xiec0ff7452010-05-24 14:32:08 -0700915 goto err;
Mel Gormana1e78772008-07-23 21:27:23 -0700916
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700917 /* If reserves cannot be used, ensure enough pages are in the pool */
Andi Kleena5516432008-07-23 21:27:41 -0700918 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700919 goto err;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700920
Vlastimil Babka04ec6262017-07-06 15:40:03 -0700921 gfp_mask = htlb_alloc_mask(h);
922 nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
Michal Hocko3e59fcb2017-07-10 15:49:11 -0700923 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask);
924 if (page && !avoid_reserve && vma_has_reserves(vma, chg)) {
925 SetPagePrivate(page);
926 h->resv_huge_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Mel Gormancc9a6c82012-03-21 16:34:11 -0700928
929 mpol_cond_put(mpol);
Mel Gormancc9a6c82012-03-21 16:34:11 -0700930 return page;
931
Miao Xiec0ff7452010-05-24 14:32:08 -0700932err:
Mel Gormancc9a6c82012-03-21 16:34:11 -0700933 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Luiz Capitulino1cac6f22014-06-04 16:07:11 -0700936/*
937 * common helper functions for hstate_next_node_to_{alloc|free}.
938 * We may have allocated or freed a huge page based on a different
939 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
940 * be outside of *nodes_allowed. Ensure that we use an allowed
941 * node for alloc or free.
942 */
943static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
944{
Andrew Morton0edaf862016-05-19 17:10:58 -0700945 nid = next_node_in(nid, *nodes_allowed);
Luiz Capitulino1cac6f22014-06-04 16:07:11 -0700946 VM_BUG_ON(nid >= MAX_NUMNODES);
947
948 return nid;
949}
950
951static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
952{
953 if (!node_isset(nid, *nodes_allowed))
954 nid = next_node_allowed(nid, nodes_allowed);
955 return nid;
956}
957
958/*
959 * returns the previously saved node ["this node"] from which to
960 * allocate a persistent huge page for the pool and advance the
961 * next node from which to allocate, handling wrap at end of node
962 * mask.
963 */
964static int hstate_next_node_to_alloc(struct hstate *h,
965 nodemask_t *nodes_allowed)
966{
967 int nid;
968
969 VM_BUG_ON(!nodes_allowed);
970
971 nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
972 h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
973
974 return nid;
975}
976
977/*
978 * helper for free_pool_huge_page() - return the previously saved
979 * node ["this node"] from which to free a huge page. Advance the
980 * next node id whether or not we find a free huge page to free so
981 * that the next attempt to free addresses the next node.
982 */
983static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
984{
985 int nid;
986
987 VM_BUG_ON(!nodes_allowed);
988
989 nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
990 h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
991
992 return nid;
993}
994
995#define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
996 for (nr_nodes = nodes_weight(*mask); \
997 nr_nodes > 0 && \
998 ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
999 nr_nodes--)
1000
1001#define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
1002 for (nr_nodes = nodes_weight(*mask); \
1003 nr_nodes > 0 && \
1004 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1005 nr_nodes--)
1006
Aneesh Kumar K.Ve1073d12017-07-06 15:39:17 -07001007#ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001008static void destroy_compound_gigantic_page(struct page *page,
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001009 unsigned int order)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001010{
1011 int i;
1012 int nr_pages = 1 << order;
1013 struct page *p = page + 1;
1014
Gerald Schaeferc8cc7082016-06-24 14:50:04 -07001015 atomic_set(compound_mapcount_ptr(page), 0);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001016 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001017 clear_compound_head(p);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001018 set_page_refcounted(p);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001019 }
1020
1021 set_compound_order(page, 0);
1022 __ClearPageHead(page);
1023}
1024
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001025static void free_gigantic_page(struct page *page, unsigned int order)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001026{
1027 free_contig_range(page_to_pfn(page), 1 << order);
1028}
1029
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001030#ifdef CONFIG_CONTIG_ALLOC
Michal Hockod9cc948f2018-01-31 16:20:44 -08001031static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
1032 int nid, nodemask_t *nodemask)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001033{
Anshuman Khandual5e27a2d2019-11-30 17:55:06 -08001034 unsigned long nr_pages = 1UL << huge_page_order(h);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001035
Anshuman Khandual5e27a2d2019-11-30 17:55:06 -08001036 return alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001037}
1038
1039static void prep_new_huge_page(struct hstate *h, struct page *page, int nid);
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001040static void prep_compound_gigantic_page(struct page *page, unsigned int order);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001041#else /* !CONFIG_CONTIG_ALLOC */
1042static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
1043 int nid, nodemask_t *nodemask)
1044{
1045 return NULL;
1046}
1047#endif /* CONFIG_CONTIG_ALLOC */
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001048
Aneesh Kumar K.Ve1073d12017-07-06 15:39:17 -07001049#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
Michal Hockod9cc948f2018-01-31 16:20:44 -08001050static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001051 int nid, nodemask_t *nodemask)
1052{
1053 return NULL;
1054}
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001055static inline void free_gigantic_page(struct page *page, unsigned int order) { }
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001056static inline void destroy_compound_gigantic_page(struct page *page,
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001057 unsigned int order) { }
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001058#endif
1059
Andi Kleena5516432008-07-23 21:27:41 -07001060static void update_and_free_page(struct hstate *h, struct page *page)
Adam Litke6af2acb2007-10-16 01:26:16 -07001061{
1062 int i;
Andi Kleena5516432008-07-23 21:27:41 -07001063
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001064 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001065 return;
Andy Whitcroft18229df2008-11-06 12:53:27 -08001066
Andi Kleena5516432008-07-23 21:27:41 -07001067 h->nr_huge_pages--;
1068 h->nr_huge_pages_node[page_to_nid(page)]--;
1069 for (i = 0; i < pages_per_huge_page(h); i++) {
Chris Forbes32f84522011-07-25 17:12:14 -07001070 page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
1071 1 << PG_referenced | 1 << PG_dirty |
Luiz Capitulinoa7407a22014-06-04 16:07:09 -07001072 1 << PG_active | 1 << PG_private |
1073 1 << PG_writeback);
Adam Litke6af2acb2007-10-16 01:26:16 -07001074 }
Sasha Levin309381fea2014-01-23 15:52:54 -08001075 VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(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
Atsushi Kumagai8f1d26d2014-07-30 16:08:39 -07001144void 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
Andi Kleena5516432008-07-23 21:27:41 -07001207static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001208{
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07001209 INIT_LIST_HEAD(&page->lru);
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -08001210 set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001211 spin_lock(&hugetlb_lock);
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -07001212 set_hugetlb_cgroup(page, NULL);
Andi Kleena5516432008-07-23 21:27:41 -07001213 h->nr_huge_pages++;
1214 h->nr_huge_pages_node[nid]++;
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001215 spin_unlock(&hugetlb_lock);
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001216}
1217
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001218static void prep_compound_gigantic_page(struct page *page, unsigned int order)
Wu Fengguang20a03072009-06-16 15:32:22 -07001219{
1220 int i;
1221 int nr_pages = 1 << order;
1222 struct page *p = page + 1;
1223
1224 /* we rely on prep_new_huge_page to set the destructor */
1225 set_compound_order(page, order);
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001226 __ClearPageReserved(page);
Kirill A. Shutemovde09d312016-01-15 16:51:42 -08001227 __SetPageHead(page);
Wu Fengguang20a03072009-06-16 15:32:22 -07001228 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001229 /*
1230 * For gigantic hugepages allocated through bootmem at
1231 * boot, it's safer to be consistent with the not-gigantic
1232 * hugepages and clear the PG_reserved bit from all tail pages
1233 * too. Otherwse drivers using get_user_pages() to access tail
1234 * pages may get the reference counting wrong if they see
1235 * PG_reserved set on a tail page (despite the head page not
1236 * having PG_reserved set). Enforcing this consistency between
1237 * head and tail pages allows drivers to optimize away a check
1238 * on the head page when they need know if put_page() is needed
1239 * after get_user_pages().
1240 */
1241 __ClearPageReserved(p);
Youquan Song58a84aa2011-12-08 14:34:18 -08001242 set_page_count(p, 0);
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001243 set_compound_head(p, page);
Wu Fengguang20a03072009-06-16 15:32:22 -07001244 }
Mike Kravetzb4330af2016-02-05 15:36:38 -08001245 atomic_set(compound_mapcount_ptr(page), -1);
Wu Fengguang20a03072009-06-16 15:32:22 -07001246}
1247
Andrew Morton77959122012-10-08 16:34:11 -07001248/*
1249 * PageHuge() only returns true for hugetlbfs pages, but not for normal or
1250 * transparent huge pages. See the PageTransHuge() documentation for more
1251 * details.
1252 */
Wu Fengguang20a03072009-06-16 15:32:22 -07001253int PageHuge(struct page *page)
1254{
Wu Fengguang20a03072009-06-16 15:32:22 -07001255 if (!PageCompound(page))
1256 return 0;
1257
1258 page = compound_head(page);
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -08001259 return page[1].compound_dtor == HUGETLB_PAGE_DTOR;
Wu Fengguang20a03072009-06-16 15:32:22 -07001260}
Naoya Horiguchi43131e12010-05-28 09:29:22 +09001261EXPORT_SYMBOL_GPL(PageHuge);
1262
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001263/*
1264 * PageHeadHuge() only returns true for hugetlbfs head page, but not for
1265 * normal or transparent huge pages.
1266 */
1267int PageHeadHuge(struct page *page_head)
1268{
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001269 if (!PageHead(page_head))
1270 return 0;
1271
Andrew Morton758f66a2014-01-21 15:48:57 -08001272 return get_compound_page_dtor(page_head) == free_huge_page;
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001273}
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001274
Zhang Yi13d60f42013-06-25 21:19:31 +08001275pgoff_t __basepage_index(struct page *page)
1276{
1277 struct page *page_head = compound_head(page);
1278 pgoff_t index = page_index(page_head);
1279 unsigned long compound_idx;
1280
1281 if (!PageHuge(page_head))
1282 return page_index(page);
1283
1284 if (compound_order(page_head) >= MAX_ORDER)
1285 compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
1286 else
1287 compound_idx = page - page_head;
1288
1289 return (index << compound_order(page_head)) + compound_idx;
1290}
1291
Michal Hocko0c397da2018-01-31 16:20:56 -08001292static struct page *alloc_buddy_huge_page(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07001293 gfp_t gfp_mask, int nid, nodemask_t *nmask,
1294 nodemask_t *node_alloc_noretry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001296 int order = huge_page_order(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct page *page;
Mike Kravetzf60858f2019-09-23 15:37:35 -07001298 bool alloc_try_hard = true;
Joe Jinf96efd52007-07-15 23:38:12 -07001299
Mike Kravetzf60858f2019-09-23 15:37:35 -07001300 /*
1301 * By default we always try hard to allocate the page with
1302 * __GFP_RETRY_MAYFAIL flag. However, if we are allocating pages in
1303 * a loop (to adjust global huge page counts) and previous allocation
1304 * failed, do not continue to try hard on the same node. Use the
1305 * node_alloc_noretry bitmap to manage this state information.
1306 */
1307 if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry))
1308 alloc_try_hard = false;
1309 gfp_mask |= __GFP_COMP|__GFP_NOWARN;
1310 if (alloc_try_hard)
1311 gfp_mask |= __GFP_RETRY_MAYFAIL;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001312 if (nid == NUMA_NO_NODE)
1313 nid = numa_mem_id();
1314 page = __alloc_pages_nodemask(gfp_mask, order, nid, nmask);
1315 if (page)
1316 __count_vm_event(HTLB_BUDDY_PGALLOC);
1317 else
1318 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -07001319
Mike Kravetzf60858f2019-09-23 15:37:35 -07001320 /*
1321 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this
1322 * indicates an overall state change. Clear bit so that we resume
1323 * normal 'try hard' allocations.
1324 */
1325 if (node_alloc_noretry && page && !alloc_try_hard)
1326 node_clear(nid, *node_alloc_noretry);
1327
1328 /*
1329 * If we tried hard to get a page but failed, set bit so that
1330 * subsequent attempts will not try as hard until there is an
1331 * overall state change.
1332 */
1333 if (node_alloc_noretry && !page && alloc_try_hard)
1334 node_set(nid, *node_alloc_noretry);
1335
Nishanth Aravamudan63b46132007-10-16 01:26:24 -07001336 return page;
1337}
1338
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001339/*
Michal Hocko0c397da2018-01-31 16:20:56 -08001340 * Common helper to allocate a fresh hugetlb page. All specific allocators
1341 * should use this function to get new hugetlb pages
1342 */
1343static struct page *alloc_fresh_huge_page(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07001344 gfp_t gfp_mask, int nid, nodemask_t *nmask,
1345 nodemask_t *node_alloc_noretry)
Michal Hocko0c397da2018-01-31 16:20:56 -08001346{
1347 struct page *page;
1348
1349 if (hstate_is_gigantic(h))
1350 page = alloc_gigantic_page(h, gfp_mask, nid, nmask);
1351 else
1352 page = alloc_buddy_huge_page(h, gfp_mask,
Mike Kravetzf60858f2019-09-23 15:37:35 -07001353 nid, nmask, node_alloc_noretry);
Michal Hocko0c397da2018-01-31 16:20:56 -08001354 if (!page)
1355 return NULL;
1356
1357 if (hstate_is_gigantic(h))
1358 prep_compound_gigantic_page(page, huge_page_order(h));
1359 prep_new_huge_page(h, page, page_to_nid(page));
1360
1361 return page;
1362}
1363
1364/*
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001365 * Allocates a fresh page to the hugetlb allocator pool in the node interleaved
1366 * manner.
1367 */
Mike Kravetzf60858f2019-09-23 15:37:35 -07001368static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
1369 nodemask_t *node_alloc_noretry)
Joonsoo Kimb2261022013-09-11 14:21:00 -07001370{
1371 struct page *page;
1372 int nr_nodes, node;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001373 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001374
1375 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
Mike Kravetzf60858f2019-09-23 15:37:35 -07001376 page = alloc_fresh_huge_page(h, gfp_mask, node, nodes_allowed,
1377 node_alloc_noretry);
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001378 if (page)
Joonsoo Kimb2261022013-09-11 14:21:00 -07001379 break;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001380 }
1381
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001382 if (!page)
1383 return 0;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001384
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001385 put_page(page); /* free it into the hugepage allocator */
1386
1387 return 1;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001388}
1389
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001390/*
1391 * Free huge page from pool from next node to free.
1392 * Attempt to keep persistent huge pages more or less
1393 * balanced over allowed nodes.
1394 * Called with hugetlb_lock locked.
1395 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001396static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
1397 bool acct_surplus)
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001398{
Joonsoo Kimb2261022013-09-11 14:21:00 -07001399 int nr_nodes, node;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001400 int ret = 0;
1401
Joonsoo Kimb2261022013-09-11 14:21:00 -07001402 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001403 /*
1404 * If we're returning unused surplus pages, only examine
1405 * nodes with surplus pages.
1406 */
Joonsoo Kimb2261022013-09-11 14:21:00 -07001407 if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
1408 !list_empty(&h->hugepage_freelists[node])) {
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001409 struct page *page =
Joonsoo Kimb2261022013-09-11 14:21:00 -07001410 list_entry(h->hugepage_freelists[node].next,
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001411 struct page, lru);
1412 list_del(&page->lru);
1413 h->free_huge_pages--;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001414 h->free_huge_pages_node[node]--;
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001415 if (acct_surplus) {
1416 h->surplus_huge_pages--;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001417 h->surplus_huge_pages_node[node]--;
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001418 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001419 update_and_free_page(h, page);
1420 ret = 1;
Lee Schermerhorn9a76db02009-12-14 17:58:15 -08001421 break;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001422 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07001423 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001424
1425 return ret;
1426}
1427
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001428/*
1429 * Dissolve a given free hugepage into free buddy pages. This function does
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001430 * nothing for in-use hugepages and non-hugepages.
1431 * This function returns values like below:
1432 *
1433 * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
1434 * (allocated or reserved.)
1435 * 0: successfully dissolved free hugepages or the page is not a
1436 * hugepage (considered as already dissolved)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001437 */
Anshuman Khandualc3114a82017-07-10 15:47:41 -07001438int dissolve_free_huge_page(struct page *page)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001439{
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07001440 int rc = -EBUSY;
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001441
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001442 /* Not to disrupt normal path by vainly holding hugetlb_lock */
1443 if (!PageHuge(page))
1444 return 0;
1445
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001446 spin_lock(&hugetlb_lock);
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001447 if (!PageHuge(page)) {
1448 rc = 0;
1449 goto out;
1450 }
1451
1452 if (!page_count(page)) {
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001453 struct page *head = compound_head(page);
1454 struct hstate *h = page_hstate(head);
1455 int nid = page_to_nid(head);
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07001456 if (h->free_huge_pages - h->resv_huge_pages == 0)
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001457 goto out;
Anshuman Khandualc3114a82017-07-10 15:47:41 -07001458 /*
1459 * Move PageHWPoison flag from head page to the raw error page,
1460 * which makes any subpages rather than the error page reusable.
1461 */
1462 if (PageHWPoison(head) && page != head) {
1463 SetPageHWPoison(page);
1464 ClearPageHWPoison(head);
1465 }
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001466 list_del(&head->lru);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001467 h->free_huge_pages--;
1468 h->free_huge_pages_node[nid]--;
zhong jiangc1470b32016-08-11 15:32:55 -07001469 h->max_huge_pages--;
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001470 update_and_free_page(h, head);
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07001471 rc = 0;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001472 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001473out:
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001474 spin_unlock(&hugetlb_lock);
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001475 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001476}
1477
1478/*
1479 * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
1480 * make specified memory blocks removable from the system.
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001481 * Note that this will dissolve a free gigantic hugepage completely, if any
1482 * part of it lies within the given range.
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001483 * Also note that if dissolve_free_huge_page() returns with an error, all
1484 * free hugepages that were dissolved before that error are lost.
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001485 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001486int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001487{
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001488 unsigned long pfn;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07001489 struct page *page;
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001490 int rc = 0;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001491
Li Zhongd0177632014-08-06 16:07:56 -07001492 if (!hugepages_supported())
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001493 return rc;
Li Zhongd0177632014-08-06 16:07:56 -07001494
Gerald Schaefereb03aa02016-10-07 17:01:13 -07001495 for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
1496 page = pfn_to_page(pfn);
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001497 rc = dissolve_free_huge_page(page);
1498 if (rc)
1499 break;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07001500 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001501
1502 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001503}
1504
Michal Hockoab5ac902018-01-31 16:20:48 -08001505/*
1506 * Allocates a fresh surplus page from the page allocator.
1507 */
Michal Hocko0c397da2018-01-31 16:20:56 -08001508static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,
Michal Hockoaaf14e42017-07-10 15:49:08 -07001509 int nid, nodemask_t *nmask)
Adam Litke7893d1d2007-10-16 01:26:18 -07001510{
Michal Hocko9980d742018-01-31 16:20:52 -08001511 struct page *page = NULL;
Adam Litke7893d1d2007-10-16 01:26:18 -07001512
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001513 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07001514 return NULL;
1515
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001516 spin_lock(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08001517 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages)
1518 goto out_unlock;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001519 spin_unlock(&hugetlb_lock);
1520
Mike Kravetzf60858f2019-09-23 15:37:35 -07001521 page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL);
Michal Hocko9980d742018-01-31 16:20:52 -08001522 if (!page)
Michal Hocko0c397da2018-01-31 16:20:56 -08001523 return NULL;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001524
1525 spin_lock(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08001526 /*
1527 * We could have raced with the pool size change.
1528 * Double check that and simply deallocate the new page
1529 * if we would end up overcommiting the surpluses. Abuse
1530 * temporary page to workaround the nasty free_huge_page
1531 * codeflow
1532 */
1533 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
1534 SetPageHugeTemporary(page);
Kai Shen2bf753e2019-05-13 17:15:37 -07001535 spin_unlock(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08001536 put_page(page);
Kai Shen2bf753e2019-05-13 17:15:37 -07001537 return NULL;
Michal Hocko9980d742018-01-31 16:20:52 -08001538 } else {
Michal Hocko9980d742018-01-31 16:20:52 -08001539 h->surplus_huge_pages++;
Michal Hocko4704dea2018-03-09 15:50:55 -08001540 h->surplus_huge_pages_node[page_to_nid(page)]++;
Adam Litke7893d1d2007-10-16 01:26:18 -07001541 }
Michal Hocko9980d742018-01-31 16:20:52 -08001542
1543out_unlock:
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001544 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001545
1546 return page;
1547}
1548
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001549struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
1550 int nid, nodemask_t *nmask)
Michal Hockoab5ac902018-01-31 16:20:48 -08001551{
1552 struct page *page;
1553
1554 if (hstate_is_gigantic(h))
1555 return NULL;
1556
Mike Kravetzf60858f2019-09-23 15:37:35 -07001557 page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL);
Michal Hockoab5ac902018-01-31 16:20:48 -08001558 if (!page)
1559 return NULL;
1560
1561 /*
1562 * We do not account these pages as surplus because they are only
1563 * temporary and will be released properly on the last reference
1564 */
Michal Hockoab5ac902018-01-31 16:20:48 -08001565 SetPageHugeTemporary(page);
1566
1567 return page;
1568}
1569
Adam Litkee4e574b2007-10-16 01:26:19 -07001570/*
Dave Hansen099730d2015-11-05 18:50:17 -08001571 * Use the VMA's mpolicy to allocate a huge page from the buddy.
1572 */
Dave Hansene0ec90e2015-11-05 18:50:20 -08001573static
Michal Hocko0c397da2018-01-31 16:20:56 -08001574struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h,
Dave Hansen099730d2015-11-05 18:50:17 -08001575 struct vm_area_struct *vma, unsigned long addr)
1576{
Michal Hockoaaf14e42017-07-10 15:49:08 -07001577 struct page *page;
1578 struct mempolicy *mpol;
1579 gfp_t gfp_mask = htlb_alloc_mask(h);
1580 int nid;
1581 nodemask_t *nodemask;
1582
1583 nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
Michal Hocko0c397da2018-01-31 16:20:56 -08001584 page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask);
Michal Hockoaaf14e42017-07-10 15:49:08 -07001585 mpol_cond_put(mpol);
1586
1587 return page;
Dave Hansen099730d2015-11-05 18:50:17 -08001588}
1589
Michal Hockoab5ac902018-01-31 16:20:48 -08001590/* page migration callback function */
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001591struct page *alloc_huge_page_node(struct hstate *h, int nid)
1592{
Michal Hockoaaf14e42017-07-10 15:49:08 -07001593 gfp_t gfp_mask = htlb_alloc_mask(h);
Joonsoo Kim4ef91842013-09-11 14:21:51 -07001594 struct page *page = NULL;
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001595
Michal Hockoaaf14e42017-07-10 15:49:08 -07001596 if (nid != NUMA_NO_NODE)
1597 gfp_mask |= __GFP_THISNODE;
1598
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001599 spin_lock(&hugetlb_lock);
Joonsoo Kim4ef91842013-09-11 14:21:51 -07001600 if (h->free_huge_pages - h->resv_huge_pages > 0)
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001601 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, NULL);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001602 spin_unlock(&hugetlb_lock);
1603
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -07001604 if (!page)
Michal Hocko0c397da2018-01-31 16:20:56 -08001605 page = alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001606
1607 return page;
1608}
1609
Michal Hockoab5ac902018-01-31 16:20:48 -08001610/* page migration callback function */
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001611struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
1612 nodemask_t *nmask)
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001613{
Michal Hockoaaf14e42017-07-10 15:49:08 -07001614 gfp_t gfp_mask = htlb_alloc_mask(h);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001615
1616 spin_lock(&hugetlb_lock);
1617 if (h->free_huge_pages - h->resv_huge_pages > 0) {
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001618 struct page *page;
1619
1620 page = dequeue_huge_page_nodemask(h, gfp_mask, preferred_nid, nmask);
1621 if (page) {
1622 spin_unlock(&hugetlb_lock);
1623 return page;
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001624 }
1625 }
1626 spin_unlock(&hugetlb_lock);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001627
Michal Hocko0c397da2018-01-31 16:20:56 -08001628 return alloc_migrate_huge_page(h, gfp_mask, preferred_nid, nmask);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001629}
1630
Michal Hockoebd63722018-01-31 16:21:00 -08001631/* mempolicy aware migration callback */
Michal Hocko389c8172018-01-31 16:21:03 -08001632struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
1633 unsigned long address)
Michal Hockoebd63722018-01-31 16:21:00 -08001634{
1635 struct mempolicy *mpol;
1636 nodemask_t *nodemask;
1637 struct page *page;
Michal Hockoebd63722018-01-31 16:21:00 -08001638 gfp_t gfp_mask;
1639 int node;
1640
Michal Hockoebd63722018-01-31 16:21:00 -08001641 gfp_mask = htlb_alloc_mask(h);
1642 node = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
1643 page = alloc_huge_page_nodemask(h, node, nodemask);
1644 mpol_cond_put(mpol);
1645
1646 return page;
1647}
1648
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001649/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001650 * Increase the hugetlb pool such that it can accommodate a reservation
Adam Litkee4e574b2007-10-16 01:26:19 -07001651 * of size 'delta'.
1652 */
Andi Kleena5516432008-07-23 21:27:41 -07001653static int gather_surplus_pages(struct hstate *h, int delta)
Adam Litkee4e574b2007-10-16 01:26:19 -07001654{
1655 struct list_head surplus_list;
1656 struct page *page, *tmp;
1657 int ret, i;
1658 int needed, allocated;
Hillf Danton28073b02012-03-21 16:34:00 -07001659 bool alloc_ok = true;
Adam Litkee4e574b2007-10-16 01:26:19 -07001660
Andi Kleena5516432008-07-23 21:27:41 -07001661 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -08001662 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -07001663 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07001664 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -08001665 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001666
1667 allocated = 0;
1668 INIT_LIST_HEAD(&surplus_list);
1669
1670 ret = -ENOMEM;
1671retry:
1672 spin_unlock(&hugetlb_lock);
1673 for (i = 0; i < needed; i++) {
Michal Hocko0c397da2018-01-31 16:20:56 -08001674 page = alloc_surplus_huge_page(h, htlb_alloc_mask(h),
Michal Hockoaaf14e42017-07-10 15:49:08 -07001675 NUMA_NO_NODE, NULL);
Hillf Danton28073b02012-03-21 16:34:00 -07001676 if (!page) {
1677 alloc_ok = false;
1678 break;
1679 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001680 list_add(&page->lru, &surplus_list);
David Rientjes69ed7792017-07-10 15:48:50 -07001681 cond_resched();
Adam Litkee4e574b2007-10-16 01:26:19 -07001682 }
Hillf Danton28073b02012-03-21 16:34:00 -07001683 allocated += i;
Adam Litkee4e574b2007-10-16 01:26:19 -07001684
1685 /*
1686 * After retaking hugetlb_lock, we need to recalculate 'needed'
1687 * because either resv_huge_pages or free_huge_pages may have changed.
1688 */
1689 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001690 needed = (h->resv_huge_pages + delta) -
1691 (h->free_huge_pages + allocated);
Hillf Danton28073b02012-03-21 16:34:00 -07001692 if (needed > 0) {
1693 if (alloc_ok)
1694 goto retry;
1695 /*
1696 * We were not able to allocate enough pages to
1697 * satisfy the entire reservation so we free what
1698 * we've allocated so far.
1699 */
1700 goto free;
1701 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001702 /*
1703 * The surplus_list now contains _at_least_ the number of extra pages
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001704 * needed to accommodate the reservation. Add the appropriate number
Adam Litkee4e574b2007-10-16 01:26:19 -07001705 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -08001706 * allocator. Commit the entire reservation here to prevent another
1707 * process from stealing the pages as they are added to the pool but
1708 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -07001709 */
1710 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -07001711 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07001712 ret = 0;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001713
Adam Litke19fc3f02008-04-28 02:12:20 -07001714 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -07001715 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -07001716 if ((--needed) < 0)
1717 break;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001718 /*
1719 * This page is now managed by the hugetlb allocator and has
1720 * no users -- drop the buddy allocator's reference.
1721 */
1722 put_page_testzero(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001723 VM_BUG_ON_PAGE(page_count(page), page);
Andi Kleena5516432008-07-23 21:27:41 -07001724 enqueue_huge_page(h, page);
Adam Litke19fc3f02008-04-28 02:12:20 -07001725 }
Hillf Danton28073b02012-03-21 16:34:00 -07001726free:
Hillf Dantonb0365c82011-12-28 15:57:16 -08001727 spin_unlock(&hugetlb_lock);
Adam Litke19fc3f02008-04-28 02:12:20 -07001728
1729 /* Free unnecessary surplus pages to the buddy allocator */
Joonsoo Kimc0d934b2013-09-11 14:21:02 -07001730 list_for_each_entry_safe(page, tmp, &surplus_list, lru)
1731 put_page(page);
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001732 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07001733
1734 return ret;
1735}
1736
1737/*
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001738 * This routine has two main purposes:
1739 * 1) Decrement the reservation count (resv_huge_pages) by the value passed
1740 * in unused_resv_pages. This corresponds to the prior adjustments made
1741 * to the associated reservation map.
1742 * 2) Free any unused surplus pages that may have been allocated to satisfy
1743 * the reservation. As many as unused_resv_pages may be freed.
1744 *
1745 * Called with hugetlb_lock held. However, the lock could be dropped (and
1746 * reacquired) during calls to cond_resched_lock. Whenever dropping the lock,
1747 * we must make sure nobody else can claim pages we are in the process of
1748 * freeing. Do this by ensuring resv_huge_page always is greater than the
1749 * number of huge pages we plan to free when dropping the lock.
Adam Litkee4e574b2007-10-16 01:26:19 -07001750 */
Andi Kleena5516432008-07-23 21:27:41 -07001751static void return_unused_surplus_pages(struct hstate *h,
1752 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -07001753{
Adam Litkee4e574b2007-10-16 01:26:19 -07001754 unsigned long nr_pages;
1755
Andi Kleenaa888a72008-07-23 21:27:47 -07001756 /* Cannot return gigantic pages currently */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001757 if (hstate_is_gigantic(h))
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001758 goto out;
Andi Kleenaa888a72008-07-23 21:27:47 -07001759
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001760 /*
1761 * Part (or even all) of the reservation could have been backed
1762 * by pre-allocated pages. Only free surplus pages.
1763 */
Andi Kleena5516432008-07-23 21:27:41 -07001764 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -07001765
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001766 /*
1767 * We want to release as many surplus pages as possible, spread
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08001768 * evenly across all nodes with memory. Iterate across these nodes
1769 * until we can no longer free unreserved surplus pages. This occurs
1770 * when the nodes with surplus pages have no free pages.
1771 * free_pool_huge_page() will balance the the freed pages across the
1772 * on-line nodes with memory and will handle the hstate accounting.
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001773 *
1774 * Note that we decrement resv_huge_pages as we free the pages. If
1775 * we drop the lock, resv_huge_pages will still be sufficiently large
1776 * to cover subsequent pages we may free.
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001777 */
1778 while (nr_pages--) {
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001779 h->resv_huge_pages--;
1780 unused_resv_pages--;
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08001781 if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001782 goto out;
Mizuma, Masayoshi7848a4b2014-04-18 15:07:18 -07001783 cond_resched_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07001784 }
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08001785
1786out:
1787 /* Fully uncommit the reservation */
1788 h->resv_huge_pages -= unused_resv_pages;
Adam Litkee4e574b2007-10-16 01:26:19 -07001789}
1790
Mike Kravetz5e911372015-09-08 15:01:28 -07001791
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001792/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001793 * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
Mike Kravetz5e911372015-09-08 15:01:28 -07001794 * are used by the huge page allocation routines to manage reservations.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001795 *
1796 * vma_needs_reservation is called to determine if the huge page at addr
1797 * within the vma has an associated reservation. If a reservation is
1798 * needed, the value 1 is returned. The caller is then responsible for
1799 * managing the global reservation and subpool usage counts. After
1800 * the huge page has been allocated, vma_commit_reservation is called
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001801 * to add the page to the reservation map. If the page allocation fails,
1802 * the reservation must be ended instead of committed. vma_end_reservation
1803 * is called in such cases.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001804 *
1805 * In the normal case, vma_commit_reservation returns the same value
1806 * as the preceding vma_needs_reservation call. The only time this
1807 * is not the case is if a reserve map was changed between calls. It
1808 * is the responsibility of the caller to notice the difference and
1809 * take appropriate action.
Mike Kravetz96b96a92016-11-10 10:46:32 -08001810 *
1811 * vma_add_reservation is used in error paths where a reservation must
1812 * be restored when a newly allocated huge page must be freed. It is
1813 * to be called after calling vma_needs_reservation to determine if a
1814 * reservation exists.
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001815 */
Mike Kravetz5e911372015-09-08 15:01:28 -07001816enum vma_resv_mode {
1817 VMA_NEEDS_RESV,
1818 VMA_COMMIT_RESV,
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001819 VMA_END_RESV,
Mike Kravetz96b96a92016-11-10 10:46:32 -08001820 VMA_ADD_RESV,
Mike Kravetz5e911372015-09-08 15:01:28 -07001821};
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001822static long __vma_reservation_common(struct hstate *h,
1823 struct vm_area_struct *vma, unsigned long addr,
Mike Kravetz5e911372015-09-08 15:01:28 -07001824 enum vma_resv_mode mode)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001825{
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001826 struct resv_map *resv;
1827 pgoff_t idx;
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001828 long ret;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001829
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001830 resv = vma_resv_map(vma);
1831 if (!resv)
Andy Whitcroft84afd992008-07-23 21:27:32 -07001832 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001833
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001834 idx = vma_hugecache_offset(h, vma, addr);
Mike Kravetz5e911372015-09-08 15:01:28 -07001835 switch (mode) {
1836 case VMA_NEEDS_RESV:
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001837 ret = region_chg(resv, idx, idx + 1);
Mike Kravetz5e911372015-09-08 15:01:28 -07001838 break;
1839 case VMA_COMMIT_RESV:
1840 ret = region_add(resv, idx, idx + 1);
1841 break;
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001842 case VMA_END_RESV:
Mike Kravetz5e911372015-09-08 15:01:28 -07001843 region_abort(resv, idx, idx + 1);
1844 ret = 0;
1845 break;
Mike Kravetz96b96a92016-11-10 10:46:32 -08001846 case VMA_ADD_RESV:
1847 if (vma->vm_flags & VM_MAYSHARE)
1848 ret = region_add(resv, idx, idx + 1);
1849 else {
1850 region_abort(resv, idx, idx + 1);
1851 ret = region_del(resv, idx, idx + 1);
1852 }
1853 break;
Mike Kravetz5e911372015-09-08 15:01:28 -07001854 default:
1855 BUG();
1856 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07001857
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001858 if (vma->vm_flags & VM_MAYSHARE)
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001859 return ret;
Mike Kravetz67961f92016-06-08 15:33:42 -07001860 else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && ret >= 0) {
1861 /*
1862 * In most cases, reserves always exist for private mappings.
1863 * However, a file associated with mapping could have been
1864 * hole punched or truncated after reserves were consumed.
1865 * As subsequent fault on such a range will not use reserves.
1866 * Subtle - The reserve map for private mappings has the
1867 * opposite meaning than that of shared mappings. If NO
1868 * entry is in the reserve map, it means a reservation exists.
1869 * If an entry exists in the reserve map, it means the
1870 * reservation has already been consumed. As a result, the
1871 * return value of this routine is the opposite of the
1872 * value returned from reserve map manipulation routines above.
1873 */
1874 if (ret)
1875 return 0;
1876 else
1877 return 1;
1878 }
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001879 else
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001880 return ret < 0 ? ret : 0;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001881}
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001882
1883static long vma_needs_reservation(struct hstate *h,
Andi Kleena5516432008-07-23 21:27:41 -07001884 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001885{
Mike Kravetz5e911372015-09-08 15:01:28 -07001886 return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001887}
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001888
Mike Kravetzcf3ad202015-06-24 16:57:55 -07001889static long vma_commit_reservation(struct hstate *h,
1890 struct vm_area_struct *vma, unsigned long addr)
1891{
Mike Kravetz5e911372015-09-08 15:01:28 -07001892 return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
1893}
1894
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001895static void vma_end_reservation(struct hstate *h,
Mike Kravetz5e911372015-09-08 15:01:28 -07001896 struct vm_area_struct *vma, unsigned long addr)
1897{
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001898 (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001899}
1900
Mike Kravetz96b96a92016-11-10 10:46:32 -08001901static long vma_add_reservation(struct hstate *h,
1902 struct vm_area_struct *vma, unsigned long addr)
1903{
1904 return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
1905}
1906
1907/*
1908 * This routine is called to restore a reservation on error paths. In the
1909 * specific error paths, a huge page was allocated (via alloc_huge_page)
1910 * and is about to be freed. If a reservation for the page existed,
1911 * alloc_huge_page would have consumed the reservation and set PagePrivate
1912 * in the newly allocated page. When the page is freed via free_huge_page,
1913 * the global reservation count will be incremented if PagePrivate is set.
1914 * However, free_huge_page can not adjust the reserve map. Adjust the
1915 * reserve map here to be consistent with global reserve count adjustments
1916 * to be made by free_huge_page.
1917 */
1918static void restore_reserve_on_error(struct hstate *h,
1919 struct vm_area_struct *vma, unsigned long address,
1920 struct page *page)
1921{
1922 if (unlikely(PagePrivate(page))) {
1923 long rc = vma_needs_reservation(h, vma, address);
1924
1925 if (unlikely(rc < 0)) {
1926 /*
1927 * Rare out of memory condition in reserve map
1928 * manipulation. Clear PagePrivate so that
1929 * global reserve count will not be incremented
1930 * by free_huge_page. This will make it appear
1931 * as though the reservation for this page was
1932 * consumed. This may prevent the task from
1933 * faulting in the page at a later time. This
1934 * is better than inconsistent global huge page
1935 * accounting of reserve counts.
1936 */
1937 ClearPagePrivate(page);
1938 } else if (rc) {
1939 rc = vma_add_reservation(h, vma, address);
1940 if (unlikely(rc < 0))
1941 /*
1942 * See above comment about rare out of
1943 * memory condition.
1944 */
1945 ClearPagePrivate(page);
1946 } else
1947 vma_end_reservation(h, vma, address);
1948 }
1949}
1950
Mike Kravetz70c35472015-09-08 15:01:54 -07001951struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001952 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
David Gibson90481622012-03-21 16:34:12 -07001954 struct hugepage_subpool *spool = subpool_vma(vma);
Andi Kleena5516432008-07-23 21:27:41 -07001955 struct hstate *h = hstate_vma(vma);
Adam Litke348ea202007-11-14 16:59:37 -08001956 struct page *page;
Mike Kravetzd85f69b2015-09-08 15:01:47 -07001957 long map_chg, map_commit;
1958 long gbl_chg;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07001959 int ret, idx;
1960 struct hugetlb_cgroup *h_cg;
Adam Litke2fc39ce2007-11-14 16:59:39 -08001961
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07001962 idx = hstate_index(h);
Mel Gormana1e78772008-07-23 21:27:23 -07001963 /*
Mike Kravetzd85f69b2015-09-08 15:01:47 -07001964 * Examine the region/reserve map to determine if the process
1965 * has a reservation for the page to be allocated. A return
1966 * code of zero indicates a reservation exists (no change).
Mel Gormana1e78772008-07-23 21:27:23 -07001967 */
Mike Kravetzd85f69b2015-09-08 15:01:47 -07001968 map_chg = gbl_chg = vma_needs_reservation(h, vma, addr);
1969 if (map_chg < 0)
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07001970 return ERR_PTR(-ENOMEM);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07001971
1972 /*
1973 * Processes that did not create the mapping will have no
1974 * reserves as indicated by the region/reserve map. Check
1975 * that the allocation will not exceed the subpool limit.
1976 * Allocations for MAP_NORESERVE mappings also need to be
1977 * checked against any subpool limit.
1978 */
1979 if (map_chg || avoid_reserve) {
1980 gbl_chg = hugepage_subpool_get_pages(spool, 1);
1981 if (gbl_chg < 0) {
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001982 vma_end_reservation(h, vma, addr);
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07001983 return ERR_PTR(-ENOSPC);
Mike Kravetz5e911372015-09-08 15:01:28 -07001984 }
Mel Gormana1e78772008-07-23 21:27:23 -07001985
Mike Kravetzd85f69b2015-09-08 15:01:47 -07001986 /*
1987 * Even though there was no reservation in the region/reserve
1988 * map, there could be reservations associated with the
1989 * subpool that can be used. This would be indicated if the
1990 * return value of hugepage_subpool_get_pages() is zero.
1991 * However, if avoid_reserve is specified we still avoid even
1992 * the subpool reservations.
1993 */
1994 if (avoid_reserve)
1995 gbl_chg = 1;
1996 }
1997
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07001998 ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07001999 if (ret)
2000 goto out_subpool_put;
2001
Mel Gormana1e78772008-07-23 21:27:23 -07002002 spin_lock(&hugetlb_lock);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002003 /*
2004 * glb_chg is passed to indicate whether or not a page must be taken
2005 * from the global free pool (global change). gbl_chg == 0 indicates
2006 * a reservation exists for the allocation.
2007 */
2008 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07002009 if (!page) {
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -07002010 spin_unlock(&hugetlb_lock);
Michal Hocko0c397da2018-01-31 16:20:56 -08002011 page = alloc_buddy_huge_page_with_mpol(h, vma, addr);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002012 if (!page)
2013 goto out_uncharge_cgroup;
Naoya Horiguchia88c7692015-12-11 13:40:24 -08002014 if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
2015 SetPagePrivate(page);
2016 h->resv_huge_pages--;
2017 }
Aneesh Kumar K.V79dbb232012-07-31 16:42:32 -07002018 spin_lock(&hugetlb_lock);
2019 list_move(&page->lru, &h->hugepage_activelist);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07002020 /* Fall through */
Mel Gormana1e78772008-07-23 21:27:23 -07002021 }
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07002022 hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
2023 spin_unlock(&hugetlb_lock);
Mel Gormana1e78772008-07-23 21:27:23 -07002024
David Gibson90481622012-03-21 16:34:12 -07002025 set_page_private(page, (unsigned long)spool);
Mel Gormana1e78772008-07-23 21:27:23 -07002026
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002027 map_commit = vma_commit_reservation(h, vma, addr);
2028 if (unlikely(map_chg > map_commit)) {
Mike Kravetz33039672015-06-24 16:57:58 -07002029 /*
2030 * The page was added to the reservation map between
2031 * vma_needs_reservation and vma_commit_reservation.
2032 * This indicates a race with hugetlb_reserve_pages.
2033 * Adjust for the subpool count incremented above AND
2034 * in hugetlb_reserve_pages for the same page. Also,
2035 * the reservation count added in hugetlb_reserve_pages
2036 * no longer applies.
2037 */
2038 long rsv_adjust;
2039
2040 rsv_adjust = hugepage_subpool_put_pages(spool, 1);
2041 hugetlb_acct_memory(h, -rsv_adjust);
2042 }
Adam Litke90d8b7e2007-11-14 16:59:42 -08002043 return page;
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002044
2045out_uncharge_cgroup:
2046 hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
2047out_subpool_put:
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002048 if (map_chg || avoid_reserve)
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002049 hugepage_subpool_put_pages(spool, 1);
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002050 vma_end_reservation(h, vma, addr);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002051 return ERR_PTR(-ENOSPC);
David Gibsonb45b5bd2006-03-22 00:08:55 -08002052}
2053
Aneesh Kumar K.Ve24a1302017-07-28 10:31:25 +05302054int alloc_bootmem_huge_page(struct hstate *h)
2055 __attribute__ ((weak, alias("__alloc_bootmem_huge_page")));
2056int __alloc_bootmem_huge_page(struct hstate *h)
Andi Kleenaa888a72008-07-23 21:27:47 -07002057{
2058 struct huge_bootmem_page *m;
Joonsoo Kimb2261022013-09-11 14:21:00 -07002059 int nr_nodes, node;
Andi Kleenaa888a72008-07-23 21:27:47 -07002060
Joonsoo Kimb2261022013-09-11 14:21:00 -07002061 for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
Andi Kleenaa888a72008-07-23 21:27:47 -07002062 void *addr;
2063
Mike Rapoporteb31d552018-10-30 15:08:04 -07002064 addr = memblock_alloc_try_nid_raw(
Grygorii Strashko8b89a112014-01-21 15:50:36 -08002065 huge_page_size(h), huge_page_size(h),
Mike Rapoport97ad1082018-10-30 15:09:44 -07002066 0, MEMBLOCK_ALLOC_ACCESSIBLE, node);
Andi Kleenaa888a72008-07-23 21:27:47 -07002067 if (addr) {
2068 /*
2069 * Use the beginning of the huge page to store the
2070 * huge_bootmem_page struct (until gather_bootmem
2071 * puts them into the mem_map).
2072 */
2073 m = addr;
Cyrill Gorcunov91f47662009-01-06 14:40:33 -08002074 goto found;
Andi Kleenaa888a72008-07-23 21:27:47 -07002075 }
Andi Kleenaa888a72008-07-23 21:27:47 -07002076 }
2077 return 0;
2078
2079found:
Luiz Capitulinodf994ea2014-12-12 16:55:21 -08002080 BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h)));
Andi Kleenaa888a72008-07-23 21:27:47 -07002081 /* Put them into a private list first because mem_map is not up yet */
Cannon Matthews330d6e42018-08-17 15:49:17 -07002082 INIT_LIST_HEAD(&m->list);
Andi Kleenaa888a72008-07-23 21:27:47 -07002083 list_add(&m->list, &huge_boot_pages);
2084 m->hstate = h;
2085 return 1;
2086}
2087
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08002088static void __init prep_compound_huge_page(struct page *page,
2089 unsigned int order)
Andy Whitcroft18229df2008-11-06 12:53:27 -08002090{
2091 if (unlikely(order > (MAX_ORDER - 1)))
2092 prep_compound_gigantic_page(page, order);
2093 else
2094 prep_compound_page(page, order);
2095}
2096
Andi Kleenaa888a72008-07-23 21:27:47 -07002097/* Put bootmem huge pages into the standard lists after mem_map is up */
2098static void __init gather_bootmem_prealloc(void)
2099{
2100 struct huge_bootmem_page *m;
2101
2102 list_for_each_entry(m, &huge_boot_pages, list) {
Mike Kravetz40d18eb2018-08-17 15:49:07 -07002103 struct page *page = virt_to_page(m);
Andi Kleenaa888a72008-07-23 21:27:47 -07002104 struct hstate *h = m->hstate;
Becky Bruceee8f2482011-07-25 17:11:50 -07002105
Andi Kleenaa888a72008-07-23 21:27:47 -07002106 WARN_ON(page_count(page) != 1);
Andy Whitcroft18229df2008-11-06 12:53:27 -08002107 prep_compound_huge_page(page, h->order);
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07002108 WARN_ON(PageReserved(page));
Andi Kleenaa888a72008-07-23 21:27:47 -07002109 prep_new_huge_page(h, page, page_to_nid(page));
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002110 put_page(page); /* free it into the hugepage allocator */
2111
Rafael Aquinib0320c72011-06-15 15:08:39 -07002112 /*
2113 * If we had gigantic hugepages allocated at boot time, we need
2114 * to restore the 'stolen' pages to totalram_pages in order to
2115 * fix confusing memory reports from free(1) and another
2116 * side-effects, like CommitLimit going negative.
2117 */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002118 if (hstate_is_gigantic(h))
Jiang Liu3dcc0572013-07-03 15:03:21 -07002119 adjust_managed_page_count(page, 1 << h->order);
Cannon Matthews520495f2018-07-03 17:02:43 -07002120 cond_resched();
Andi Kleenaa888a72008-07-23 21:27:47 -07002121 }
2122}
2123
Andi Kleen8faa8b02008-07-23 21:27:48 -07002124static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 unsigned long i;
Mike Kravetzf60858f2019-09-23 15:37:35 -07002127 nodemask_t *node_alloc_noretry;
2128
2129 if (!hstate_is_gigantic(h)) {
2130 /*
2131 * Bit mask controlling how hard we retry per-node allocations.
2132 * Ignore errors as lower level routines can deal with
2133 * node_alloc_noretry == NULL. If this kmalloc fails at boot
2134 * time, we are likely in bigger trouble.
2135 */
2136 node_alloc_noretry = kmalloc(sizeof(*node_alloc_noretry),
2137 GFP_KERNEL);
2138 } else {
2139 /* allocations done at boot time */
2140 node_alloc_noretry = NULL;
2141 }
2142
2143 /* bit mask controlling how hard we retry per-node allocations */
2144 if (node_alloc_noretry)
2145 nodes_clear(*node_alloc_noretry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Andi Kleene5ff2152008-07-23 21:27:42 -07002147 for (i = 0; i < h->max_huge_pages; ++i) {
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002148 if (hstate_is_gigantic(h)) {
Andi Kleenaa888a72008-07-23 21:27:47 -07002149 if (!alloc_bootmem_huge_page(h))
2150 break;
Michal Hocko0c397da2018-01-31 16:20:56 -08002151 } else if (!alloc_pool_huge_page(h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07002152 &node_states[N_MEMORY],
2153 node_alloc_noretry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 break;
David Rientjes69ed7792017-07-10 15:48:50 -07002155 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
Liam R. Howlettd715cf82017-07-10 15:48:15 -07002157 if (i < h->max_huge_pages) {
2158 char buf[32];
2159
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07002160 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Liam R. Howlettd715cf82017-07-10 15:48:15 -07002161 pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n",
2162 h->max_huge_pages, buf, i);
2163 h->max_huge_pages = i;
2164 }
Mike Kravetzf60858f2019-09-23 15:37:35 -07002165
2166 kfree(node_alloc_noretry);
Andi Kleene5ff2152008-07-23 21:27:42 -07002167}
2168
2169static void __init hugetlb_init_hstates(void)
2170{
2171 struct hstate *h;
2172
2173 for_each_hstate(h) {
Naoya Horiguchi641844f2015-06-24 16:56:59 -07002174 if (minimum_order > huge_page_order(h))
2175 minimum_order = huge_page_order(h);
2176
Andi Kleen8faa8b02008-07-23 21:27:48 -07002177 /* oversize hugepages were init'ed in early boot */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002178 if (!hstate_is_gigantic(h))
Andi Kleen8faa8b02008-07-23 21:27:48 -07002179 hugetlb_hstate_alloc_pages(h);
Andi Kleene5ff2152008-07-23 21:27:42 -07002180 }
Naoya Horiguchi641844f2015-06-24 16:56:59 -07002181 VM_BUG_ON(minimum_order == UINT_MAX);
Andi Kleene5ff2152008-07-23 21:27:42 -07002182}
2183
2184static void __init report_hugepages(void)
2185{
2186 struct hstate *h;
2187
2188 for_each_hstate(h) {
Andi Kleen4abd32d2008-07-23 21:27:49 -07002189 char buf[32];
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07002190
2191 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Andrew Mortonffb22af2013-02-22 16:32:08 -08002192 pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07002193 buf, h->free_huge_pages);
Andi Kleene5ff2152008-07-23 21:27:42 -07002194 }
2195}
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197#ifdef CONFIG_HIGHMEM
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002198static void try_to_free_low(struct hstate *h, unsigned long count,
2199 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Christoph Lameter4415cc82006-09-25 23:31:55 -07002201 int i;
2202
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002203 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07002204 return;
2205
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002206 for_each_node_mask(i, *nodes_allowed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -07002208 struct list_head *freel = &h->hugepage_freelists[i];
2209 list_for_each_entry_safe(page, next, freel, lru) {
2210 if (count >= h->nr_huge_pages)
Adam Litke6b0c8802007-10-16 01:26:23 -07002211 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (PageHighMem(page))
2213 continue;
2214 list_del(&page->lru);
Andi Kleene5ff2152008-07-23 21:27:42 -07002215 update_and_free_page(h, page);
Andi Kleena5516432008-07-23 21:27:41 -07002216 h->free_huge_pages--;
2217 h->free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219 }
2220}
2221#else
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002222static inline void try_to_free_low(struct hstate *h, unsigned long count,
2223 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225}
2226#endif
2227
Wu Fengguang20a03072009-06-16 15:32:22 -07002228/*
2229 * Increment or decrement surplus_huge_pages. Keep node-specific counters
2230 * balanced by operating on them in a round-robin fashion.
2231 * Returns 1 if an adjustment was made.
2232 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002233static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
2234 int delta)
Wu Fengguang20a03072009-06-16 15:32:22 -07002235{
Joonsoo Kimb2261022013-09-11 14:21:00 -07002236 int nr_nodes, node;
Wu Fengguang20a03072009-06-16 15:32:22 -07002237
2238 VM_BUG_ON(delta != -1 && delta != 1);
Wu Fengguang20a03072009-06-16 15:32:22 -07002239
Joonsoo Kimb2261022013-09-11 14:21:00 -07002240 if (delta < 0) {
2241 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
2242 if (h->surplus_huge_pages_node[node])
2243 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002244 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002245 } else {
2246 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
2247 if (h->surplus_huge_pages_node[node] <
2248 h->nr_huge_pages_node[node])
2249 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002250 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002251 }
2252 return 0;
Wu Fengguang20a03072009-06-16 15:32:22 -07002253
Joonsoo Kimb2261022013-09-11 14:21:00 -07002254found:
2255 h->surplus_huge_pages += delta;
2256 h->surplus_huge_pages_node[node] += delta;
2257 return 1;
Wu Fengguang20a03072009-06-16 15:32:22 -07002258}
2259
Andi Kleena5516432008-07-23 21:27:41 -07002260#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002261static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002262 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Adam Litke7893d1d2007-10-16 01:26:18 -07002264 unsigned long min_count, ret;
Mike Kravetzf60858f2019-09-23 15:37:35 -07002265 NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL);
2266
2267 /*
2268 * Bit mask controlling how hard we retry per-node allocations.
2269 * If we can not allocate the bit mask, do not attempt to allocate
2270 * the requested huge pages.
2271 */
2272 if (node_alloc_noretry)
2273 nodes_clear(*node_alloc_noretry);
2274 else
2275 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002277 spin_lock(&hugetlb_lock);
2278
2279 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002280 * Check for a node specific request.
2281 * Changing node specific huge page count may require a corresponding
2282 * change to the global count. In any case, the passed node mask
2283 * (nodes_allowed) will restrict alloc/free to the specified node.
2284 */
2285 if (nid != NUMA_NO_NODE) {
2286 unsigned long old_count = count;
2287
2288 count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
2289 /*
2290 * User may have specified a large count value which caused the
2291 * above calculation to overflow. In this case, they wanted
2292 * to allocate as many huge pages as possible. Set count to
2293 * largest possible value to align with their intention.
2294 */
2295 if (count < old_count)
2296 count = ULONG_MAX;
2297 }
2298
2299 /*
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002300 * Gigantic pages runtime allocation depend on the capability for large
2301 * page range allocation.
2302 * If the system does not provide this feature, return an error when
2303 * the user tries to allocate gigantic pages but let the user free the
2304 * boottime allocated gigantic pages.
2305 */
2306 if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) {
2307 if (count > persistent_huge_pages(h)) {
2308 spin_unlock(&hugetlb_lock);
Mike Kravetzf60858f2019-09-23 15:37:35 -07002309 NODEMASK_FREE(node_alloc_noretry);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002310 return -EINVAL;
2311 }
2312 /* Fall through to decrease pool */
2313 }
Andi Kleenaa888a72008-07-23 21:27:47 -07002314
Adam Litke7893d1d2007-10-16 01:26:18 -07002315 /*
2316 * Increase the pool size
2317 * First take pages out of surplus state. Then make up the
2318 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002319 *
Michal Hocko0c397da2018-01-31 16:20:56 -08002320 * We might race with alloc_surplus_huge_page() here and be unable
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002321 * to convert a surplus huge page to a normal huge page. That is
2322 * not critical, though, it just means the overall size of the
2323 * pool might be one hugepage larger than it needs to be, but
2324 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -07002325 */
Andi Kleena5516432008-07-23 21:27:41 -07002326 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002327 if (!adjust_pool_surplus(h, nodes_allowed, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -07002328 break;
2329 }
2330
Andi Kleena5516432008-07-23 21:27:41 -07002331 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -07002332 /*
2333 * If this allocation races such that we no longer need the
2334 * page, free_huge_page will handle it by freeing the page
2335 * and reducing the surplus.
2336 */
2337 spin_unlock(&hugetlb_lock);
Jia He649920c2016-08-02 14:02:31 -07002338
2339 /* yield cpu to avoid soft lockup */
2340 cond_resched();
2341
Mike Kravetzf60858f2019-09-23 15:37:35 -07002342 ret = alloc_pool_huge_page(h, nodes_allowed,
2343 node_alloc_noretry);
Adam Litke7893d1d2007-10-16 01:26:18 -07002344 spin_lock(&hugetlb_lock);
2345 if (!ret)
2346 goto out;
2347
Mel Gorman536240f22009-12-14 17:59:56 -08002348 /* Bail for signals. Probably ctrl-c from user */
2349 if (signal_pending(current))
2350 goto out;
Adam Litke7893d1d2007-10-16 01:26:18 -07002351 }
Adam Litke7893d1d2007-10-16 01:26:18 -07002352
2353 /*
2354 * Decrease the pool size
2355 * First return free pages to the buddy allocator (being careful
2356 * to keep enough around to satisfy reservations). Then place
2357 * pages into surplus state as needed so the pool will shrink
2358 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002359 *
2360 * By placing pages into the surplus state independent of the
2361 * overcommit value, we are allowing the surplus pool size to
2362 * exceed overcommit. There are few sane options here. Since
Michal Hocko0c397da2018-01-31 16:20:56 -08002363 * alloc_surplus_huge_page() is checking the global counter,
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002364 * though, we'll note that we're not allowed to exceed surplus
2365 * and won't grow the pool anywhere else. Not until one of the
2366 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07002367 */
Andi Kleena5516432008-07-23 21:27:41 -07002368 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07002369 min_count = max(count, min_count);
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002370 try_to_free_low(h, min_count, nodes_allowed);
Andi Kleena5516432008-07-23 21:27:41 -07002371 while (min_count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002372 if (!free_pool_huge_page(h, nodes_allowed, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 break;
Mizuma, Masayoshi55f67142014-04-07 15:37:54 -07002374 cond_resched_lock(&hugetlb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
Andi Kleena5516432008-07-23 21:27:41 -07002376 while (count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002377 if (!adjust_pool_surplus(h, nodes_allowed, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07002378 break;
2379 }
2380out:
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002381 h->max_huge_pages = persistent_huge_pages(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 spin_unlock(&hugetlb_lock);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002383
Mike Kravetzf60858f2019-09-23 15:37:35 -07002384 NODEMASK_FREE(node_alloc_noretry);
2385
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002389#define HSTATE_ATTR_RO(_name) \
2390 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
2391
2392#define HSTATE_ATTR(_name) \
2393 static struct kobj_attribute _name##_attr = \
2394 __ATTR(_name, 0644, _name##_show, _name##_store)
2395
2396static struct kobject *hugepages_kobj;
2397static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
2398
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002399static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
2400
2401static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002402{
2403 int i;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002404
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002405 for (i = 0; i < HUGE_MAX_HSTATE; i++)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002406 if (hstate_kobjs[i] == kobj) {
2407 if (nidp)
2408 *nidp = NUMA_NO_NODE;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002409 return &hstates[i];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002410 }
2411
2412 return kobj_to_node_hstate(kobj, nidp);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002413}
2414
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002415static ssize_t nr_hugepages_show_common(struct kobject *kobj,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002416 struct kobj_attribute *attr, char *buf)
2417{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002418 struct hstate *h;
2419 unsigned long nr_huge_pages;
2420 int nid;
2421
2422 h = kobj_to_hstate(kobj, &nid);
2423 if (nid == NUMA_NO_NODE)
2424 nr_huge_pages = h->nr_huge_pages;
2425 else
2426 nr_huge_pages = h->nr_huge_pages_node[nid];
2427
2428 return sprintf(buf, "%lu\n", nr_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002429}
Eric B Munsonadbe8722011-01-13 15:47:27 -08002430
David Rientjes238d3c12014-08-06 16:06:51 -07002431static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
2432 struct hstate *h, int nid,
2433 unsigned long count, size_t len)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002434{
2435 int err;
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002436 nodemask_t nodes_allowed, *n_mask;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002437
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002438 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
2439 return -EINVAL;
Eric B Munsonadbe8722011-01-13 15:47:27 -08002440
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002441 if (nid == NUMA_NO_NODE) {
2442 /*
2443 * global hstate attribute
2444 */
2445 if (!(obey_mempolicy &&
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002446 init_nodemask_of_mempolicy(&nodes_allowed)))
2447 n_mask = &node_states[N_MEMORY];
2448 else
2449 n_mask = &nodes_allowed;
2450 } else {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002451 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002452 * Node specific request. count adjustment happens in
2453 * set_max_huge_pages() after acquiring hugetlb_lock.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002454 */
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002455 init_nodemask_of_node(&nodes_allowed, nid);
2456 n_mask = &nodes_allowed;
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002457 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002458
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002459 err = set_max_huge_pages(h, count, nid, n_mask);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002460
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002461 return err ? err : len;
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002462}
2463
David Rientjes238d3c12014-08-06 16:06:51 -07002464static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
2465 struct kobject *kobj, const char *buf,
2466 size_t len)
2467{
2468 struct hstate *h;
2469 unsigned long count;
2470 int nid;
2471 int err;
2472
2473 err = kstrtoul(buf, 10, &count);
2474 if (err)
2475 return err;
2476
2477 h = kobj_to_hstate(kobj, &nid);
2478 return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
2479}
2480
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002481static ssize_t nr_hugepages_show(struct kobject *kobj,
2482 struct kobj_attribute *attr, char *buf)
2483{
2484 return nr_hugepages_show_common(kobj, attr, buf);
2485}
2486
2487static ssize_t nr_hugepages_store(struct kobject *kobj,
2488 struct kobj_attribute *attr, const char *buf, size_t len)
2489{
David Rientjes238d3c12014-08-06 16:06:51 -07002490 return nr_hugepages_store_common(false, kobj, buf, len);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002491}
2492HSTATE_ATTR(nr_hugepages);
2493
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002494#ifdef CONFIG_NUMA
2495
2496/*
2497 * hstate attribute for optionally mempolicy-based constraint on persistent
2498 * huge page alloc/free.
2499 */
2500static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
2501 struct kobj_attribute *attr, char *buf)
2502{
2503 return nr_hugepages_show_common(kobj, attr, buf);
2504}
2505
2506static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
2507 struct kobj_attribute *attr, const char *buf, size_t len)
2508{
David Rientjes238d3c12014-08-06 16:06:51 -07002509 return nr_hugepages_store_common(true, kobj, buf, len);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002510}
2511HSTATE_ATTR(nr_hugepages_mempolicy);
2512#endif
2513
2514
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002515static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
2516 struct kobj_attribute *attr, char *buf)
2517{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002518 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002519 return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
2520}
Eric B Munsonadbe8722011-01-13 15:47:27 -08002521
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002522static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
2523 struct kobj_attribute *attr, const char *buf, size_t count)
2524{
2525 int err;
2526 unsigned long input;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002527 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002528
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002529 if (hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08002530 return -EINVAL;
2531
Jingoo Han3dbb95f2013-09-11 14:20:25 -07002532 err = kstrtoul(buf, 10, &input);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002533 if (err)
Eric B Munson73ae31e2011-01-13 15:47:28 -08002534 return err;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002535
2536 spin_lock(&hugetlb_lock);
2537 h->nr_overcommit_huge_pages = input;
2538 spin_unlock(&hugetlb_lock);
2539
2540 return count;
2541}
2542HSTATE_ATTR(nr_overcommit_hugepages);
2543
2544static ssize_t free_hugepages_show(struct kobject *kobj,
2545 struct kobj_attribute *attr, char *buf)
2546{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002547 struct hstate *h;
2548 unsigned long free_huge_pages;
2549 int nid;
2550
2551 h = kobj_to_hstate(kobj, &nid);
2552 if (nid == NUMA_NO_NODE)
2553 free_huge_pages = h->free_huge_pages;
2554 else
2555 free_huge_pages = h->free_huge_pages_node[nid];
2556
2557 return sprintf(buf, "%lu\n", free_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002558}
2559HSTATE_ATTR_RO(free_hugepages);
2560
2561static ssize_t resv_hugepages_show(struct kobject *kobj,
2562 struct kobj_attribute *attr, char *buf)
2563{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002564 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002565 return sprintf(buf, "%lu\n", h->resv_huge_pages);
2566}
2567HSTATE_ATTR_RO(resv_hugepages);
2568
2569static ssize_t surplus_hugepages_show(struct kobject *kobj,
2570 struct kobj_attribute *attr, char *buf)
2571{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002572 struct hstate *h;
2573 unsigned long surplus_huge_pages;
2574 int nid;
2575
2576 h = kobj_to_hstate(kobj, &nid);
2577 if (nid == NUMA_NO_NODE)
2578 surplus_huge_pages = h->surplus_huge_pages;
2579 else
2580 surplus_huge_pages = h->surplus_huge_pages_node[nid];
2581
2582 return sprintf(buf, "%lu\n", surplus_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002583}
2584HSTATE_ATTR_RO(surplus_hugepages);
2585
2586static struct attribute *hstate_attrs[] = {
2587 &nr_hugepages_attr.attr,
2588 &nr_overcommit_hugepages_attr.attr,
2589 &free_hugepages_attr.attr,
2590 &resv_hugepages_attr.attr,
2591 &surplus_hugepages_attr.attr,
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002592#ifdef CONFIG_NUMA
2593 &nr_hugepages_mempolicy_attr.attr,
2594#endif
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002595 NULL,
2596};
2597
Arvind Yadav67e5ed92017-09-06 16:22:06 -07002598static const struct attribute_group hstate_attr_group = {
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002599 .attrs = hstate_attrs,
2600};
2601
Jeff Mahoney094e9532010-02-02 13:44:14 -08002602static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
2603 struct kobject **hstate_kobjs,
Arvind Yadav67e5ed92017-09-06 16:22:06 -07002604 const struct attribute_group *hstate_attr_group)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002605{
2606 int retval;
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002607 int hi = hstate_index(h);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002608
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002609 hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
2610 if (!hstate_kobjs[hi])
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002611 return -ENOMEM;
2612
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002613 retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002614 if (retval)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002615 kobject_put(hstate_kobjs[hi]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002616
2617 return retval;
2618}
2619
2620static void __init hugetlb_sysfs_init(void)
2621{
2622 struct hstate *h;
2623 int err;
2624
2625 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
2626 if (!hugepages_kobj)
2627 return;
2628
2629 for_each_hstate(h) {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002630 err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
2631 hstate_kobjs, &hstate_attr_group);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002632 if (err)
Andrew Mortonffb22af2013-02-22 16:32:08 -08002633 pr_err("Hugetlb: Unable to add hstate %s", h->name);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002634 }
2635}
2636
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002637#ifdef CONFIG_NUMA
2638
2639/*
2640 * node_hstate/s - associate per node hstate attributes, via their kobjects,
Kay Sievers10fbcf42011-12-21 14:48:43 -08002641 * with node devices in node_devices[] using a parallel array. The array
2642 * index of a node device or _hstate == node id.
2643 * This is here to avoid any static dependency of the node device driver, in
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002644 * the base kernel, on the hugetlb module.
2645 */
2646struct node_hstate {
2647 struct kobject *hugepages_kobj;
2648 struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
2649};
Alexander Kuleshovb4e289a2015-11-05 18:50:14 -08002650static struct node_hstate node_hstates[MAX_NUMNODES];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002651
2652/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002653 * A subset of global hstate attributes for node devices
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002654 */
2655static struct attribute *per_node_hstate_attrs[] = {
2656 &nr_hugepages_attr.attr,
2657 &free_hugepages_attr.attr,
2658 &surplus_hugepages_attr.attr,
2659 NULL,
2660};
2661
Arvind Yadav67e5ed92017-09-06 16:22:06 -07002662static const struct attribute_group per_node_hstate_attr_group = {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002663 .attrs = per_node_hstate_attrs,
2664};
2665
2666/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002667 * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002668 * Returns node id via non-NULL nidp.
2669 */
2670static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
2671{
2672 int nid;
2673
2674 for (nid = 0; nid < nr_node_ids; nid++) {
2675 struct node_hstate *nhs = &node_hstates[nid];
2676 int i;
2677 for (i = 0; i < HUGE_MAX_HSTATE; i++)
2678 if (nhs->hstate_kobjs[i] == kobj) {
2679 if (nidp)
2680 *nidp = nid;
2681 return &hstates[i];
2682 }
2683 }
2684
2685 BUG();
2686 return NULL;
2687}
2688
2689/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002690 * Unregister hstate attributes from a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002691 * No-op if no hstate attributes attached.
2692 */
Claudiu Ghioc3cd8b442013-03-04 12:46:15 +02002693static void hugetlb_unregister_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002694{
2695 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08002696 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002697
2698 if (!nhs->hugepages_kobj)
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002699 return; /* no hstate attributes */
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002700
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002701 for_each_hstate(h) {
2702 int idx = hstate_index(h);
2703 if (nhs->hstate_kobjs[idx]) {
2704 kobject_put(nhs->hstate_kobjs[idx]);
2705 nhs->hstate_kobjs[idx] = NULL;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002706 }
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002707 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002708
2709 kobject_put(nhs->hugepages_kobj);
2710 nhs->hugepages_kobj = NULL;
2711}
2712
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002713
2714/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002715 * Register hstate attributes for a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002716 * No-op if attributes already registered.
2717 */
Claudiu Ghioc3cd8b442013-03-04 12:46:15 +02002718static void hugetlb_register_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002719{
2720 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08002721 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002722 int err;
2723
2724 if (nhs->hugepages_kobj)
2725 return; /* already allocated */
2726
2727 nhs->hugepages_kobj = kobject_create_and_add("hugepages",
Kay Sievers10fbcf42011-12-21 14:48:43 -08002728 &node->dev.kobj);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002729 if (!nhs->hugepages_kobj)
2730 return;
2731
2732 for_each_hstate(h) {
2733 err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
2734 nhs->hstate_kobjs,
2735 &per_node_hstate_attr_group);
2736 if (err) {
Andrew Mortonffb22af2013-02-22 16:32:08 -08002737 pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
2738 h->name, node->dev.id);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002739 hugetlb_unregister_node(node);
2740 break;
2741 }
2742 }
2743}
2744
2745/*
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002746 * hugetlb init time: register hstate attributes for all registered node
Kay Sievers10fbcf42011-12-21 14:48:43 -08002747 * devices of nodes that have memory. All on-line nodes should have
2748 * registered their associated device by this time.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002749 */
Luiz Capitulino7d9ca002014-12-12 16:55:24 -08002750static void __init hugetlb_register_all_nodes(void)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002751{
2752 int nid;
2753
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08002754 for_each_node_state(nid, N_MEMORY) {
Wen Congyang87327942012-12-11 16:00:56 -08002755 struct node *node = node_devices[nid];
Kay Sievers10fbcf42011-12-21 14:48:43 -08002756 if (node->dev.id == nid)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002757 hugetlb_register_node(node);
2758 }
2759
2760 /*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002761 * Let the node device driver know we're here so it can
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002762 * [un]register hstate attributes on node hotplug.
2763 */
2764 register_hugetlbfs_with_node(hugetlb_register_node,
2765 hugetlb_unregister_node);
2766}
2767#else /* !CONFIG_NUMA */
2768
2769static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
2770{
2771 BUG();
2772 if (nidp)
2773 *nidp = -1;
2774 return NULL;
2775}
2776
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002777static void hugetlb_register_all_nodes(void) { }
2778
2779#endif
2780
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002781static int __init hugetlb_init(void)
2782{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07002783 int i;
2784
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002785 if (!hugepages_supported())
Benjamin Herrenschmidt0ef89d22008-07-31 00:07:30 -07002786 return 0;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002787
Nick Piggine11bfbf2008-07-23 21:27:52 -07002788 if (!size_to_hstate(default_hstate_size)) {
Liam R. Howlettd715cf82017-07-10 15:48:15 -07002789 if (default_hstate_size != 0) {
2790 pr_err("HugeTLB: unsupported default_hugepagesz %lu. Reverting to %lu\n",
2791 default_hstate_size, HPAGE_SIZE);
2792 }
2793
Nick Piggine11bfbf2008-07-23 21:27:52 -07002794 default_hstate_size = HPAGE_SIZE;
2795 if (!size_to_hstate(default_hstate_size))
2796 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002797 }
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002798 default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
Vaishali Thakkarf8b74812016-02-17 13:11:26 -08002799 if (default_hstate_max_huge_pages) {
2800 if (!default_hstate.max_huge_pages)
2801 default_hstate.max_huge_pages = default_hstate_max_huge_pages;
2802 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002803
2804 hugetlb_init_hstates();
Andi Kleenaa888a72008-07-23 21:27:47 -07002805 gather_bootmem_prealloc();
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002806 report_hugepages();
2807
2808 hugetlb_sysfs_init();
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002809 hugetlb_register_all_nodes();
Jianguo Wu7179e7b2012-12-18 14:23:19 -08002810 hugetlb_cgroup_file_init();
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002811
Davidlohr Bueso8382d912014-04-03 14:47:31 -07002812#ifdef CONFIG_SMP
2813 num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
2814#else
2815 num_fault_mutexes = 1;
2816#endif
Mike Kravetzc672c7f2015-09-08 15:01:35 -07002817 hugetlb_fault_mutex_table =
Kees Cook6da2ec52018-06-12 13:55:00 -07002818 kmalloc_array(num_fault_mutexes, sizeof(struct mutex),
2819 GFP_KERNEL);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07002820 BUG_ON(!hugetlb_fault_mutex_table);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07002821
2822 for (i = 0; i < num_fault_mutexes; i++)
Mike Kravetzc672c7f2015-09-08 15:01:35 -07002823 mutex_init(&hugetlb_fault_mutex_table[i]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002824 return 0;
2825}
Paul Gortmaker3e89e1c2016-01-14 15:21:52 -08002826subsys_initcall(hugetlb_init);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002827
2828/* Should be called on processing a hugepagesz=... option */
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07002829void __init hugetlb_bad_size(void)
2830{
2831 parsed_valid_hugepagesz = false;
2832}
2833
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08002834void __init hugetlb_add_hstate(unsigned int order)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002835{
2836 struct hstate *h;
Andi Kleen8faa8b02008-07-23 21:27:48 -07002837 unsigned long i;
2838
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002839 if (size_to_hstate(PAGE_SIZE << order)) {
Joe Perches598d8092016-03-17 14:19:44 -07002840 pr_warn("hugepagesz= specified twice, ignoring\n");
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002841 return;
2842 }
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002843 BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002844 BUG_ON(order == 0);
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002845 h = &hstates[hugetlb_max_hstate++];
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002846 h->order = order;
2847 h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
Andi Kleen8faa8b02008-07-23 21:27:48 -07002848 h->nr_huge_pages = 0;
2849 h->free_huge_pages = 0;
2850 for (i = 0; i < MAX_NUMNODES; ++i)
2851 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07002852 INIT_LIST_HEAD(&h->hugepage_activelist);
Andrew Morton54f18d32016-05-19 17:11:40 -07002853 h->next_nid_to_alloc = first_memory_node;
2854 h->next_nid_to_free = first_memory_node;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002855 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
2856 huge_page_size(h)/1024);
Andi Kleen8faa8b02008-07-23 21:27:48 -07002857
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002858 parsed_hstate = h;
2859}
2860
Nick Piggine11bfbf2008-07-23 21:27:52 -07002861static int __init hugetlb_nrpages_setup(char *s)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002862{
2863 unsigned long *mhp;
Andi Kleen8faa8b02008-07-23 21:27:48 -07002864 static unsigned long *last_mhp;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002865
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07002866 if (!parsed_valid_hugepagesz) {
2867 pr_warn("hugepages = %s preceded by "
2868 "an unsupported hugepagesz, ignoring\n", s);
2869 parsed_valid_hugepagesz = true;
2870 return 1;
2871 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002872 /*
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002873 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002874 * so this hugepages= parameter goes to the "default hstate".
2875 */
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07002876 else if (!hugetlb_max_hstate)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002877 mhp = &default_hstate_max_huge_pages;
2878 else
2879 mhp = &parsed_hstate->max_huge_pages;
2880
Andi Kleen8faa8b02008-07-23 21:27:48 -07002881 if (mhp == last_mhp) {
Joe Perches598d8092016-03-17 14:19:44 -07002882 pr_warn("hugepages= specified twice without interleaving hugepagesz=, ignoring\n");
Andi Kleen8faa8b02008-07-23 21:27:48 -07002883 return 1;
2884 }
2885
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002886 if (sscanf(s, "%lu", mhp) <= 0)
2887 *mhp = 0;
2888
Andi Kleen8faa8b02008-07-23 21:27:48 -07002889 /*
2890 * Global state is always initialized later in hugetlb_init.
2891 * But we need to allocate >= MAX_ORDER hstates here early to still
2892 * use the bootmem allocator.
2893 */
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002894 if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
Andi Kleen8faa8b02008-07-23 21:27:48 -07002895 hugetlb_hstate_alloc_pages(parsed_hstate);
2896
2897 last_mhp = mhp;
2898
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002899 return 1;
2900}
Nick Piggine11bfbf2008-07-23 21:27:52 -07002901__setup("hugepages=", hugetlb_nrpages_setup);
2902
2903static int __init hugetlb_default_setup(char *s)
2904{
2905 default_hstate_size = memparse(s, &s);
2906 return 1;
2907}
2908__setup("default_hugepagesz=", hugetlb_default_setup);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002909
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07002910static unsigned int cpuset_mems_nr(unsigned int *array)
2911{
2912 int node;
2913 unsigned int nr = 0;
2914
2915 for_each_node_mask(node, cpuset_current_mems_allowed)
2916 nr += array[node];
2917
2918 return nr;
2919}
2920
2921#ifdef CONFIG_SYSCTL
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002922static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
2923 struct ctl_table *table, int write,
2924 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
Andi Kleene5ff2152008-07-23 21:27:42 -07002926 struct hstate *h = &default_hstate;
David Rientjes238d3c12014-08-06 16:06:51 -07002927 unsigned long tmp = h->max_huge_pages;
Michal Hocko08d4a242011-01-13 15:47:26 -08002928 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07002929
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002930 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08002931 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002932
Andi Kleene5ff2152008-07-23 21:27:42 -07002933 table->data = &tmp;
2934 table->maxlen = sizeof(unsigned long);
Michal Hocko08d4a242011-01-13 15:47:26 -08002935 ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
2936 if (ret)
2937 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07002938
David Rientjes238d3c12014-08-06 16:06:51 -07002939 if (write)
2940 ret = __nr_hugepages_store_common(obey_mempolicy, h,
2941 NUMA_NO_NODE, tmp, *length);
Michal Hocko08d4a242011-01-13 15:47:26 -08002942out:
2943 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
Mel Gorman396faf02007-07-17 04:03:13 -07002945
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002946int hugetlb_sysctl_handler(struct ctl_table *table, int write,
2947 void __user *buffer, size_t *length, loff_t *ppos)
2948{
2949
2950 return hugetlb_sysctl_handler_common(false, table, write,
2951 buffer, length, ppos);
2952}
2953
2954#ifdef CONFIG_NUMA
2955int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
2956 void __user *buffer, size_t *length, loff_t *ppos)
2957{
2958 return hugetlb_sysctl_handler_common(true, table, write,
2959 buffer, length, ppos);
2960}
2961#endif /* CONFIG_NUMA */
2962
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08002963int hugetlb_overcommit_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002964 void __user *buffer,
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08002965 size_t *length, loff_t *ppos)
2966{
Andi Kleena5516432008-07-23 21:27:41 -07002967 struct hstate *h = &default_hstate;
Andi Kleene5ff2152008-07-23 21:27:42 -07002968 unsigned long tmp;
Michal Hocko08d4a242011-01-13 15:47:26 -08002969 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07002970
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002971 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08002972 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002973
Petr Holasekc033a932011-03-22 16:33:05 -07002974 tmp = h->nr_overcommit_huge_pages;
Andi Kleene5ff2152008-07-23 21:27:42 -07002975
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002976 if (write && hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08002977 return -EINVAL;
2978
Andi Kleene5ff2152008-07-23 21:27:42 -07002979 table->data = &tmp;
2980 table->maxlen = sizeof(unsigned long);
Michal Hocko08d4a242011-01-13 15:47:26 -08002981 ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
2982 if (ret)
2983 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07002984
2985 if (write) {
2986 spin_lock(&hugetlb_lock);
2987 h->nr_overcommit_huge_pages = tmp;
2988 spin_unlock(&hugetlb_lock);
2989 }
Michal Hocko08d4a242011-01-13 15:47:26 -08002990out:
2991 return ret;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08002992}
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994#endif /* CONFIG_SYSCTL */
2995
Alexey Dobriyane1759c22008-10-15 23:50:22 +04002996void hugetlb_report_meminfo(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08002998 struct hstate *h;
2999 unsigned long total = 0;
3000
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003001 if (!hugepages_supported())
3002 return;
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08003003
3004 for_each_hstate(h) {
3005 unsigned long count = h->nr_huge_pages;
3006
3007 total += (PAGE_SIZE << huge_page_order(h)) * count;
3008
3009 if (h == &default_hstate)
3010 seq_printf(m,
3011 "HugePages_Total: %5lu\n"
3012 "HugePages_Free: %5lu\n"
3013 "HugePages_Rsvd: %5lu\n"
3014 "HugePages_Surp: %5lu\n"
3015 "Hugepagesize: %8lu kB\n",
3016 count,
3017 h->free_huge_pages,
3018 h->resv_huge_pages,
3019 h->surplus_huge_pages,
3020 (PAGE_SIZE << huge_page_order(h)) / 1024);
3021 }
3022
3023 seq_printf(m, "Hugetlb: %8lu kB\n", total / 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024}
3025
3026int hugetlb_report_node_meminfo(int nid, char *buf)
3027{
Andi Kleena5516432008-07-23 21:27:41 -07003028 struct hstate *h = &default_hstate;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003029 if (!hugepages_supported())
3030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return sprintf(buf,
3032 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07003033 "Node %d HugePages_Free: %5u\n"
3034 "Node %d HugePages_Surp: %5u\n",
Andi Kleena5516432008-07-23 21:27:41 -07003035 nid, h->nr_huge_pages_node[nid],
3036 nid, h->free_huge_pages_node[nid],
3037 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
David Rientjes949f7ec2013-04-29 15:07:48 -07003040void hugetlb_show_meminfo(void)
3041{
3042 struct hstate *h;
3043 int nid;
3044
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003045 if (!hugepages_supported())
3046 return;
3047
David Rientjes949f7ec2013-04-29 15:07:48 -07003048 for_each_node_state(nid, N_MEMORY)
3049 for_each_hstate(h)
3050 pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
3051 nid,
3052 h->nr_huge_pages_node[nid],
3053 h->free_huge_pages_node[nid],
3054 h->surplus_huge_pages_node[nid],
3055 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
3056}
3057
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003058void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
3059{
3060 seq_printf(m, "HugetlbPages:\t%8lu kB\n",
3061 atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10));
3062}
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
3065unsigned long hugetlb_total_pages(void)
3066{
Wanpeng Lid0028582013-03-22 15:04:40 -07003067 struct hstate *h;
3068 unsigned long nr_total_pages = 0;
3069
3070 for_each_hstate(h)
3071 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
3072 return nr_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Andi Kleena5516432008-07-23 21:27:41 -07003075static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003076{
3077 int ret = -ENOMEM;
3078
3079 spin_lock(&hugetlb_lock);
3080 /*
3081 * When cpuset is configured, it breaks the strict hugetlb page
3082 * reservation as the accounting is done on a global variable. Such
3083 * reservation is completely rubbish in the presence of cpuset because
3084 * the reservation is not checked against page availability for the
3085 * current cpuset. Application can still potentially OOM'ed by kernel
3086 * with lack of free htlb page in cpuset that the task is in.
3087 * Attempt to enforce strict accounting with cpuset is almost
3088 * impossible (or too ugly) because cpuset is too fluid that
3089 * task or memory node can be dynamically moved between cpusets.
3090 *
3091 * The change of semantics for shared hugetlb mapping with cpuset is
3092 * undesirable. However, in order to preserve some of the semantics,
3093 * we fall back to check against current free page availability as
3094 * a best attempt and hopefully to minimize the impact of changing
3095 * semantics that cpuset has.
3096 */
3097 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07003098 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003099 goto out;
3100
Andi Kleena5516432008-07-23 21:27:41 -07003101 if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
3102 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003103 goto out;
3104 }
3105 }
3106
3107 ret = 0;
3108 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07003109 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003110
3111out:
3112 spin_unlock(&hugetlb_lock);
3113 return ret;
3114}
3115
Andy Whitcroft84afd992008-07-23 21:27:32 -07003116static void hugetlb_vm_op_open(struct vm_area_struct *vma)
3117{
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07003118 struct resv_map *resv = vma_resv_map(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003119
3120 /*
3121 * This new VMA should share its siblings reservation map if present.
3122 * The VMA will only ever have a valid reservation map pointer where
3123 * it is being copied for another still existing VMA. As that VMA
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003124 * has a reference to the reservation map it cannot disappear until
Andy Whitcroft84afd992008-07-23 21:27:32 -07003125 * after this open call completes. It is therefore safe to take a
3126 * new reference here without additional locking.
3127 */
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003128 if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07003129 kref_get(&resv->refs);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003130}
3131
Mel Gormana1e78772008-07-23 21:27:23 -07003132static void hugetlb_vm_op_close(struct vm_area_struct *vma)
3133{
Andi Kleena5516432008-07-23 21:27:41 -07003134 struct hstate *h = hstate_vma(vma);
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07003135 struct resv_map *resv = vma_resv_map(vma);
David Gibson90481622012-03-21 16:34:12 -07003136 struct hugepage_subpool *spool = subpool_vma(vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003137 unsigned long reserve, start, end;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003138 long gbl_reserve;
Andy Whitcroft84afd992008-07-23 21:27:32 -07003139
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003140 if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
3141 return;
Andy Whitcroft84afd992008-07-23 21:27:32 -07003142
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003143 start = vma_hugecache_offset(h, vma, vma->vm_start);
3144 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003145
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003146 reserve = (end - start) - region_count(resv, start, end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003147
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003148 kref_put(&resv->refs, resv_map_release);
3149
3150 if (reserve) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003151 /*
3152 * Decrement reserve counts. The global reserve count may be
3153 * adjusted if the subpool has a minimum size.
3154 */
3155 gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
3156 hugetlb_acct_memory(h, -gbl_reserve);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003157 }
Mel Gormana1e78772008-07-23 21:27:23 -07003158}
3159
Dan Williams31383c62017-11-29 16:10:28 -08003160static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
3161{
3162 if (addr & ~(huge_page_mask(hstate_vma(vma))))
3163 return -EINVAL;
3164 return 0;
3165}
3166
Dan Williams05ea8862018-04-05 16:24:25 -07003167static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
3168{
3169 struct hstate *hstate = hstate_vma(vma);
3170
3171 return 1UL << huge_page_shift(hstate);
3172}
3173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174/*
3175 * We cannot handle pagefaults against hugetlb pages at all. They cause
3176 * handle_mm_fault() to try to instantiate regular-sized pages in the
3177 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
3178 * this far.
3179 */
Souptick Joarderb3ec9f32018-06-07 17:08:04 -07003180static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
3182 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07003183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Jane Chueec36362018-08-02 15:36:05 -07003186/*
3187 * When a new function is introduced to vm_operations_struct and added
3188 * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
3189 * This is because under System V memory model, mappings created via
3190 * shmget/shmat with "huge page" specified are backed by hugetlbfs files,
3191 * their original vm_ops are overwritten with shm_vm_ops.
3192 */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003193const struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07003194 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07003195 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07003196 .close = hugetlb_vm_op_close,
Dan Williams31383c62017-11-29 16:10:28 -08003197 .split = hugetlb_vm_op_split,
Dan Williams05ea8862018-04-05 16:24:25 -07003198 .pagesize = hugetlb_vm_op_pagesize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199};
3200
David Gibson1e8f8892006-01-06 00:10:44 -08003201static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
3202 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07003203{
3204 pte_t entry;
3205
David Gibson1e8f8892006-01-06 00:10:44 -08003206 if (writable) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07003207 entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
3208 vma->vm_page_prot)));
David Gibson63551ae2005-06-21 17:14:44 -07003209 } else {
Gerald Schaefer106c9922013-04-29 15:07:23 -07003210 entry = huge_pte_wrprotect(mk_huge_pte(page,
3211 vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07003212 }
3213 entry = pte_mkyoung(entry);
3214 entry = pte_mkhuge(entry);
Chris Metcalfd9ed9fa2012-04-01 14:01:34 -04003215 entry = arch_make_huge_pte(entry, vma, page, writable);
David Gibson63551ae2005-06-21 17:14:44 -07003216
3217 return entry;
3218}
3219
David Gibson1e8f8892006-01-06 00:10:44 -08003220static void set_huge_ptep_writable(struct vm_area_struct *vma,
3221 unsigned long address, pte_t *ptep)
3222{
3223 pte_t entry;
3224
Gerald Schaefer106c9922013-04-29 15:07:23 -07003225 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
Chris Forbes32f84522011-07-25 17:12:14 -07003226 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
Russell King4b3073e2009-12-18 16:40:18 +00003227 update_mmu_cache(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08003228}
3229
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003230bool is_hugetlb_entry_migration(pte_t pte)
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003231{
3232 swp_entry_t swp;
3233
3234 if (huge_pte_none(pte) || pte_present(pte))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003235 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003236 swp = pte_to_swp_entry(pte);
3237 if (non_swap_entry(swp) && is_migration_entry(swp))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003238 return true;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003239 else
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003240 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003241}
3242
3243static int is_hugetlb_entry_hwpoisoned(pte_t pte)
3244{
3245 swp_entry_t swp;
3246
3247 if (huge_pte_none(pte) || pte_present(pte))
3248 return 0;
3249 swp = pte_to_swp_entry(pte);
3250 if (non_swap_entry(swp) && is_hwpoison_entry(swp))
3251 return 1;
3252 else
3253 return 0;
3254}
David Gibson1e8f8892006-01-06 00:10:44 -08003255
David Gibson63551ae2005-06-21 17:14:44 -07003256int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
3257 struct vm_area_struct *vma)
3258{
Mike Kravetz5e415402018-11-16 15:08:04 -08003259 pte_t *src_pte, *dst_pte, entry, dst_entry;
David Gibson63551ae2005-06-21 17:14:44 -07003260 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07003261 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08003262 int cow;
Andi Kleena5516432008-07-23 21:27:41 -07003263 struct hstate *h = hstate_vma(vma);
3264 unsigned long sz = huge_page_size(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003265 struct mmu_notifier_range range;
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003266 int ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08003267
3268 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07003269
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003270 if (cow) {
Jérôme Glisse7269f992019-05-13 17:20:53 -07003271 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, src,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07003272 vma->vm_start,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003273 vma->vm_end);
3274 mmu_notifier_invalidate_range_start(&range);
3275 }
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003276
Andi Kleena5516432008-07-23 21:27:41 -07003277 for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003278 spinlock_t *src_ptl, *dst_ptl;
Punit Agrawal7868a202017-07-06 15:39:42 -07003279 src_pte = huge_pte_offset(src, addr, sz);
Hugh Dickinsc74df322005-10-29 18:16:23 -07003280 if (!src_pte)
3281 continue;
Andi Kleena5516432008-07-23 21:27:41 -07003282 dst_pte = huge_pte_alloc(dst, addr, sz);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003283 if (!dst_pte) {
3284 ret = -ENOMEM;
3285 break;
3286 }
Larry Woodmanc5c99422008-01-24 05:49:25 -08003287
Mike Kravetz5e415402018-11-16 15:08:04 -08003288 /*
3289 * If the pagetables are shared don't copy or take references.
3290 * dst_pte == src_pte is the common case of src/dest sharing.
3291 *
3292 * However, src could have 'unshared' and dst shares with
3293 * another vma. If dst_pte !none, this implies sharing.
3294 * Check here before taking page table lock, and once again
3295 * after taking the lock below.
3296 */
3297 dst_entry = huge_ptep_get(dst_pte);
3298 if ((dst_pte == src_pte) || !huge_pte_none(dst_entry))
Larry Woodmanc5c99422008-01-24 05:49:25 -08003299 continue;
3300
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003301 dst_ptl = huge_pte_lock(h, dst, dst_pte);
3302 src_ptl = huge_pte_lockptr(h, src, src_pte);
3303 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003304 entry = huge_ptep_get(src_pte);
Mike Kravetz5e415402018-11-16 15:08:04 -08003305 dst_entry = huge_ptep_get(dst_pte);
3306 if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) {
3307 /*
3308 * Skip if src entry none. Also, skip in the
3309 * unlikely case dst entry !none as this implies
3310 * sharing with another vma.
3311 */
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003312 ;
3313 } else if (unlikely(is_hugetlb_entry_migration(entry) ||
3314 is_hugetlb_entry_hwpoisoned(entry))) {
3315 swp_entry_t swp_entry = pte_to_swp_entry(entry);
3316
3317 if (is_write_migration_entry(swp_entry) && cow) {
3318 /*
3319 * COW mappings require pages in both
3320 * parent and child to be set to read.
3321 */
3322 make_migration_entry_read(&swp_entry);
3323 entry = swp_entry_to_pte(swp_entry);
Punit Agrawale5251fd2017-07-06 15:39:50 -07003324 set_huge_swap_pte_at(src, addr, src_pte,
3325 entry, sz);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003326 }
Punit Agrawale5251fd2017-07-06 15:39:50 -07003327 set_huge_swap_pte_at(dst, addr, dst_pte, entry, sz);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003328 } else {
Joerg Roedel34ee6452014-11-13 13:46:09 +11003329 if (cow) {
Jérôme Glisse0f108512017-11-15 17:34:07 -08003330 /*
3331 * No need to notify as we are downgrading page
3332 * table protection not changing it to point
3333 * to a new page.
3334 *
Mike Rapoportad56b732018-03-21 21:22:47 +02003335 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08003336 */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07003337 huge_ptep_set_wrprotect(src, addr, src_pte);
Joerg Roedel34ee6452014-11-13 13:46:09 +11003338 }
Naoya Horiguchi0253d632014-07-23 14:00:19 -07003339 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07003340 ptepage = pte_page(entry);
3341 get_page(ptepage);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08003342 page_dup_rmap(ptepage, true);
Hugh Dickins1c598272005-10-19 21:23:43 -07003343 set_huge_pte_at(dst, addr, dst_pte, entry);
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003344 hugetlb_count_add(pages_per_huge_page(h), dst);
Hugh Dickins1c598272005-10-19 21:23:43 -07003345 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003346 spin_unlock(src_ptl);
3347 spin_unlock(dst_ptl);
David Gibson63551ae2005-06-21 17:14:44 -07003348 }
David Gibson63551ae2005-06-21 17:14:44 -07003349
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003350 if (cow)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003351 mmu_notifier_invalidate_range_end(&range);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003352
3353 return ret;
David Gibson63551ae2005-06-21 17:14:44 -07003354}
3355
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003356void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
3357 unsigned long start, unsigned long end,
3358 struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07003359{
3360 struct mm_struct *mm = vma->vm_mm;
3361 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07003362 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07003363 pte_t pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003364 spinlock_t *ptl;
David Gibson63551ae2005-06-21 17:14:44 -07003365 struct page *page;
Andi Kleena5516432008-07-23 21:27:41 -07003366 struct hstate *h = hstate_vma(vma);
3367 unsigned long sz = huge_page_size(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003368 struct mmu_notifier_range range;
Andi Kleena5516432008-07-23 21:27:41 -07003369
David Gibson63551ae2005-06-21 17:14:44 -07003370 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07003371 BUG_ON(start & ~huge_page_mask(h));
3372 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07003373
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08003374 /*
3375 * This is a hugetlb vma, all the pte entries should point
3376 * to huge page.
3377 */
Peter Zijlstraed6a7932018-08-31 14:46:08 +02003378 tlb_change_page_size(tlb, sz);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003379 tlb_start_vma(tlb, vma);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003380
3381 /*
3382 * If sharing possible, alert mmu notifiers of worst case.
3383 */
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07003384 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, mm, start,
3385 end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003386 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
3387 mmu_notifier_invalidate_range_start(&range);
Hillf Danton569f48b82014-12-10 15:44:41 -08003388 address = start;
Hillf Danton569f48b82014-12-10 15:44:41 -08003389 for (; address < end; address += sz) {
Punit Agrawal7868a202017-07-06 15:39:42 -07003390 ptep = huge_pte_offset(mm, address, sz);
Adam Litke4c887262005-10-29 18:16:46 -07003391 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07003392 continue;
3393
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003394 ptl = huge_pte_lock(h, mm, ptep);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003395 if (huge_pmd_unshare(mm, &address, ptep)) {
3396 spin_unlock(ptl);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003397 /*
3398 * We just unmapped a page of PMDs by clearing a PUD.
3399 * The caller's TLB flush range should cover this area.
3400 */
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003401 continue;
3402 }
Chen, Kenneth W39dde652006-12-06 20:32:03 -08003403
Hillf Danton66293262012-03-23 15:01:48 -07003404 pte = huge_ptep_get(ptep);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003405 if (huge_pte_none(pte)) {
3406 spin_unlock(ptl);
3407 continue;
3408 }
Hillf Danton66293262012-03-23 15:01:48 -07003409
3410 /*
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08003411 * Migrating hugepage or HWPoisoned hugepage is already
3412 * unmapped and its refcount is dropped, so just clear pte here.
Hillf Danton66293262012-03-23 15:01:48 -07003413 */
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08003414 if (unlikely(!pte_present(pte))) {
Punit Agrawal9386fac2017-07-06 15:39:46 -07003415 huge_pte_clear(mm, address, ptep, sz);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003416 spin_unlock(ptl);
3417 continue;
Naoya Horiguchi8c4894c2012-12-12 13:52:28 -08003418 }
Hillf Danton66293262012-03-23 15:01:48 -07003419
3420 page = pte_page(pte);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003421 /*
3422 * If a reference page is supplied, it is because a specific
3423 * page is being unmapped, not a range. Ensure the page we
3424 * are about to unmap is the actual page of interest.
3425 */
3426 if (ref_page) {
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003427 if (page != ref_page) {
3428 spin_unlock(ptl);
3429 continue;
3430 }
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003431 /*
3432 * Mark the VMA as having unmapped its page so that
3433 * future faults in this VMA will fail rather than
3434 * looking like data was lost
3435 */
3436 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
3437 }
3438
David Gibsonc7546f82005-08-05 11:59:35 -07003439 pte = huge_ptep_get_and_clear(mm, address, ptep);
Aneesh Kumar K.Vb528e4b2016-12-12 16:42:37 -08003440 tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
Gerald Schaefer106c9922013-04-29 15:07:23 -07003441 if (huge_pte_dirty(pte))
Ken Chen6649a382007-02-08 14:20:27 -08003442 set_page_dirty(page);
Hillf Danton9e811302012-03-21 16:34:03 -07003443
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003444 hugetlb_count_sub(pages_per_huge_page(h), mm);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08003445 page_remove_rmap(page, true);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003446
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003447 spin_unlock(ptl);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -07003448 tlb_remove_page_size(tlb, page, huge_page_size(h));
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003449 /*
3450 * Bail out after unmapping reference page if supplied
3451 */
3452 if (ref_page)
3453 break;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07003454 }
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003455 mmu_notifier_invalidate_range_end(&range);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003456 tlb_end_vma(tlb, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
David Gibson63551ae2005-06-21 17:14:44 -07003458
Mel Gormand8333522012-07-31 16:46:20 -07003459void __unmap_hugepage_range_final(struct mmu_gather *tlb,
3460 struct vm_area_struct *vma, unsigned long start,
3461 unsigned long end, struct page *ref_page)
3462{
3463 __unmap_hugepage_range(tlb, vma, start, end, ref_page);
3464
3465 /*
3466 * Clear this flag so that x86's huge_pmd_share page_table_shareable
3467 * test will fail on a vma being torn down, and not grab a page table
3468 * on its way out. We're lucky that the flag has such an appropriate
3469 * name, and can in fact be safely cleared here. We could clear it
3470 * before the __unmap_hugepage_range above, but all that's necessary
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003471 * is to clear it before releasing the i_mmap_rwsem. This works
Mel Gormand8333522012-07-31 16:46:20 -07003472 * because in the context this is called, the VMA is about to be
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003473 * destroyed and the i_mmap_rwsem is held.
Mel Gormand8333522012-07-31 16:46:20 -07003474 */
3475 vma->vm_flags &= ~VM_MAYSHARE;
3476}
3477
Chen, Kenneth W502717f2006-10-11 01:20:46 -07003478void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003479 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07003480{
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003481 struct mm_struct *mm;
3482 struct mmu_gather tlb;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003483 unsigned long tlb_start = start;
3484 unsigned long tlb_end = end;
3485
3486 /*
3487 * If shared PMDs were possibly used within this vma range, adjust
3488 * start/end for worst case tlb flushing.
3489 * Note that we can not be sure if PMDs are shared until we try to
3490 * unmap pages. However, we want to make sure TLB flushing covers
3491 * the largest possible range.
3492 */
3493 adjust_range_if_pmd_sharing_possible(vma, &tlb_start, &tlb_end);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003494
3495 mm = vma->vm_mm;
3496
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003497 tlb_gather_mmu(&tlb, mm, tlb_start, tlb_end);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003498 __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003499 tlb_finish_mmu(&tlb, tlb_start, tlb_end);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07003500}
3501
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003502/*
3503 * This is called when the original mapper is failing to COW a MAP_PRIVATE
3504 * mappping it owns the reserve page for. The intention is to unmap the page
3505 * from other VMAs and let the children be SIGKILLed if they are faulting the
3506 * same region.
3507 */
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07003508static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
3509 struct page *page, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003510{
Adam Litke75266742008-11-12 13:24:56 -08003511 struct hstate *h = hstate_vma(vma);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003512 struct vm_area_struct *iter_vma;
3513 struct address_space *mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003514 pgoff_t pgoff;
3515
3516 /*
3517 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
3518 * from page cache lookup which is in HPAGE_SIZE units.
3519 */
Adam Litke75266742008-11-12 13:24:56 -08003520 address = address & huge_page_mask(h);
Michal Hocko36e4f202012-10-08 16:33:31 -07003521 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
3522 vma->vm_pgoff;
Al Viro93c76a32015-12-04 23:45:44 -05003523 mapping = vma->vm_file->f_mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003524
Mel Gorman4eb2b1d2009-12-14 17:59:53 -08003525 /*
3526 * Take the mapping lock for the duration of the table walk. As
3527 * this mapping should be shared between all the VMAs,
3528 * __unmap_hugepage_range() is called as the lock is already held
3529 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003530 i_mmap_lock_write(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07003531 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003532 /* Do not unmap the current VMA */
3533 if (iter_vma == vma)
3534 continue;
3535
3536 /*
Mel Gorman2f84a892015-10-01 15:36:57 -07003537 * Shared VMAs have their own reserves and do not affect
3538 * MAP_PRIVATE accounting but it is possible that a shared
3539 * VMA is using the same page so check and skip such VMAs.
3540 */
3541 if (iter_vma->vm_flags & VM_MAYSHARE)
3542 continue;
3543
3544 /*
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003545 * Unmap the page from other VMAs without their own reserves.
3546 * They get marked to be SIGKILLed if they fault in these
3547 * areas. This is because a future no-page fault on this VMA
3548 * could insert a zeroed page instead of the data existing
3549 * from the time of fork. This would look like data corruption
3550 */
3551 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003552 unmap_hugepage_range(iter_vma, address,
3553 address + huge_page_size(h), page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003554 }
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003555 i_mmap_unlock_write(mapping);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003556}
3557
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003558/*
3559 * Hugetlb_cow() should be called with page lock of the original hugepage held.
Michal Hockoef009b22012-01-10 15:07:21 -08003560 * Called with hugetlb_instantiation_mutex held and pte_page locked so we
3561 * cannot race with other handlers or page migration.
3562 * Keep the pte_same checks anyway to make transition from the mutex easier.
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003563 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003564static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Huang Ying974e6d62018-08-17 15:45:57 -07003565 unsigned long address, pte_t *ptep,
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08003566 struct page *pagecache_page, spinlock_t *ptl)
David Gibson1e8f8892006-01-06 00:10:44 -08003567{
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08003568 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07003569 struct hstate *h = hstate_vma(vma);
David Gibson1e8f8892006-01-06 00:10:44 -08003570 struct page *old_page, *new_page;
Souptick Joarder2b740302018-08-23 17:01:36 -07003571 int outside_reserve = 0;
3572 vm_fault_t ret = 0;
Huang Ying974e6d62018-08-17 15:45:57 -07003573 unsigned long haddr = address & huge_page_mask(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003574 struct mmu_notifier_range range;
David Gibson1e8f8892006-01-06 00:10:44 -08003575
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08003576 pte = huge_ptep_get(ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08003577 old_page = pte_page(pte);
3578
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003579retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08003580 /* If no-one else is actually using this page, avoid the copy
3581 * and just make the page writable */
Joonsoo Kim37a21402013-09-11 14:21:04 -07003582 if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
Hugh Dickins5a499732016-07-14 12:07:38 -07003583 page_move_anon_rmap(old_page, vma);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003584 set_huge_ptep_writable(vma, haddr, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07003585 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08003586 }
3587
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003588 /*
3589 * If the process that created a MAP_PRIVATE mapping is about to
3590 * perform a COW due to a shared page count, attempt to satisfy
3591 * the allocation without using the existing reserves. The pagecache
3592 * page is used to determine if the reserve at this address was
3593 * consumed or not. If reserves were used, a partial faulted mapping
3594 * at the time of fork() could consume its reserves on COW instead
3595 * of the full address range.
3596 */
Joonsoo Kim5944d012013-09-11 14:21:55 -07003597 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003598 old_page != pagecache_page)
3599 outside_reserve = 1;
3600
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003601 get_page(old_page);
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08003602
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003603 /*
3604 * Drop page table lock as buddy allocator may be called. It will
3605 * be acquired again before returning to the caller, as expected.
3606 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003607 spin_unlock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003608 new_page = alloc_huge_page(vma, haddr, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08003609
Adam Litke2fc39ce2007-11-14 16:59:39 -08003610 if (IS_ERR(new_page)) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003611 /*
3612 * If a process owning a MAP_PRIVATE mapping fails to COW,
3613 * it is due to references held by a child and an insufficient
3614 * huge page pool. To guarantee the original mappers
3615 * reliability, unmap the page from child processes. The child
3616 * may get SIGKILLed if it later faults.
3617 */
3618 if (outside_reserve) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003619 put_page(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003620 BUG_ON(huge_pte_none(pte));
Huang Ying5b7a1d42018-08-17 15:45:53 -07003621 unmap_ref_private(mm, vma, old_page, haddr);
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07003622 BUG_ON(huge_pte_none(pte));
3623 spin_lock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003624 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07003625 if (likely(ptep &&
3626 pte_same(huge_ptep_get(ptep), pte)))
3627 goto retry_avoidcopy;
3628 /*
3629 * race occurs while re-acquiring page table
3630 * lock, and our job is done.
3631 */
3632 return 0;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003633 }
3634
Souptick Joarder2b740302018-08-23 17:01:36 -07003635 ret = vmf_error(PTR_ERR(new_page));
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003636 goto out_release_old;
David Gibson1e8f8892006-01-06 00:10:44 -08003637 }
3638
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003639 /*
3640 * When the original hugepage is shared one, it does not have
3641 * anon_vma prepared.
3642 */
Dean Nelson44e2aa92010-10-26 14:22:08 -07003643 if (unlikely(anon_vma_prepare(vma))) {
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003644 ret = VM_FAULT_OOM;
3645 goto out_release_all;
Dean Nelson44e2aa92010-10-26 14:22:08 -07003646 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003647
Huang Ying974e6d62018-08-17 15:45:57 -07003648 copy_user_huge_page(new_page, old_page, address, vma,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003649 pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08003650 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08003651
Jérôme Glisse7269f992019-05-13 17:20:53 -07003652 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, haddr,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07003653 haddr + huge_page_size(h));
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003654 mmu_notifier_invalidate_range_start(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003655
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08003656 /*
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003657 * Retake the page table lock to check for racing updates
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08003658 * before the page tables are altered
3659 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003660 spin_lock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003661 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Naoya Horiguchia9af0c52014-04-07 15:36:54 -07003662 if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
Joonsoo Kim07443a82013-09-11 14:21:58 -07003663 ClearPagePrivate(new_page);
3664
David Gibson1e8f8892006-01-06 00:10:44 -08003665 /* Break COW */
Huang Ying5b7a1d42018-08-17 15:45:53 -07003666 huge_ptep_clear_flush(vma, haddr, ptep);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003667 mmu_notifier_invalidate_range(mm, range.start, range.end);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003668 set_huge_pte_at(mm, haddr, ptep,
David Gibson1e8f8892006-01-06 00:10:44 -08003669 make_huge_pte(vma, new_page, 1));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08003670 page_remove_rmap(old_page, true);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003671 hugepage_add_new_anon_rmap(new_page, vma, haddr);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08003672 set_page_huge_active(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08003673 /* Make the old page be freed below */
3674 new_page = old_page;
3675 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003676 spin_unlock(ptl);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003677 mmu_notifier_invalidate_range_end(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003678out_release_all:
Huang Ying5b7a1d42018-08-17 15:45:53 -07003679 restore_reserve_on_error(h, vma, haddr, new_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003680 put_page(new_page);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003681out_release_old:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003682 put_page(old_page);
Joonsoo Kim83120342013-09-11 14:21:57 -07003683
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003684 spin_lock(ptl); /* Caller expects lock to be held */
3685 return ret;
David Gibson1e8f8892006-01-06 00:10:44 -08003686}
3687
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003688/* Return the pagecache page at a given address within a VMA */
Andi Kleena5516432008-07-23 21:27:41 -07003689static struct page *hugetlbfs_pagecache_page(struct hstate *h,
3690 struct vm_area_struct *vma, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003691{
3692 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07003693 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003694
3695 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07003696 idx = vma_hugecache_offset(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003697
3698 return find_lock_page(mapping, idx);
3699}
3700
Hugh Dickins3ae77f42009-09-21 17:03:33 -07003701/*
3702 * Return whether there is a pagecache page to back given address within VMA.
3703 * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
3704 */
3705static bool hugetlbfs_pagecache_present(struct hstate *h,
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003706 struct vm_area_struct *vma, unsigned long address)
3707{
3708 struct address_space *mapping;
3709 pgoff_t idx;
3710 struct page *page;
3711
3712 mapping = vma->vm_file->f_mapping;
3713 idx = vma_hugecache_offset(h, vma, address);
3714
3715 page = find_get_page(mapping, idx);
3716 if (page)
3717 put_page(page);
3718 return page != NULL;
3719}
3720
Mike Kravetzab76ad52015-09-08 15:01:50 -07003721int huge_add_to_page_cache(struct page *page, struct address_space *mapping,
3722 pgoff_t idx)
3723{
3724 struct inode *inode = mapping->host;
3725 struct hstate *h = hstate_inode(inode);
3726 int err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
3727
3728 if (err)
3729 return err;
3730 ClearPagePrivate(page);
3731
Mike Kravetz22146c32018-10-26 15:10:58 -07003732 /*
3733 * set page dirty so that it will not be removed from cache/file
3734 * by non-hugetlbfs specific code paths.
3735 */
3736 set_page_dirty(page);
3737
Mike Kravetzab76ad52015-09-08 15:01:50 -07003738 spin_lock(&inode->i_lock);
3739 inode->i_blocks += blocks_per_huge_page(h);
3740 spin_unlock(&inode->i_lock);
3741 return 0;
3742}
3743
Souptick Joarder2b740302018-08-23 17:01:36 -07003744static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
3745 struct vm_area_struct *vma,
3746 struct address_space *mapping, pgoff_t idx,
3747 unsigned long address, pte_t *ptep, unsigned int flags)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003748{
Andi Kleena5516432008-07-23 21:27:41 -07003749 struct hstate *h = hstate_vma(vma);
Souptick Joarder2b740302018-08-23 17:01:36 -07003750 vm_fault_t ret = VM_FAULT_SIGBUS;
Hillf Danton409eb8c2012-01-20 14:34:13 -08003751 int anon_rmap = 0;
Adam Litke4c887262005-10-29 18:16:46 -07003752 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07003753 struct page *page;
David Gibson1e8f8892006-01-06 00:10:44 -08003754 pte_t new_pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003755 spinlock_t *ptl;
Huang Ying285b8dc2018-06-07 17:08:08 -07003756 unsigned long haddr = address & huge_page_mask(h);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08003757 bool new_page = false;
Adam Litke4c887262005-10-29 18:16:46 -07003758
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003759 /*
3760 * Currently, we are forced to kill the process in the event the
3761 * original mapper has unmapped pages from the child due to a failed
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003762 * COW. Warn that such a situation has occurred as it may not be obvious
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003763 */
3764 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
Geoffrey Thomas910154d2016-03-09 14:08:04 -08003765 pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
Andrew Mortonffb22af2013-02-22 16:32:08 -08003766 current->pid);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003767 return ret;
3768 }
3769
Adam Litke4c887262005-10-29 18:16:46 -07003770 /*
Mike Kravetze7c58092019-01-08 15:23:32 -08003771 * Use page lock to guard against racing truncation
3772 * before we get page_table_lock.
Adam Litke4c887262005-10-29 18:16:46 -07003773 */
Christoph Lameter6bda6662006-01-06 00:10:49 -08003774retry:
3775 page = find_lock_page(mapping, idx);
3776 if (!page) {
Mike Kravetze7c58092019-01-08 15:23:32 -08003777 size = i_size_read(mapping->host) >> huge_page_shift(h);
3778 if (idx >= size)
3779 goto out;
3780
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003781 /*
3782 * Check for page in userfault range
3783 */
3784 if (userfaultfd_missing(vma)) {
3785 u32 hash;
3786 struct vm_fault vmf = {
3787 .vma = vma,
Huang Ying285b8dc2018-06-07 17:08:08 -07003788 .address = haddr,
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003789 .flags = flags,
3790 /*
3791 * Hard to debug if it ends up being
3792 * used by a callee that assumes
3793 * something about the other
3794 * uninitialized fields... same as in
3795 * memory.c
3796 */
3797 };
3798
3799 /*
Mike Kravetzddeaab32019-01-08 15:23:36 -08003800 * hugetlb_fault_mutex must be dropped before
3801 * handling userfault. Reacquire after handling
3802 * fault to make calling code simpler.
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003803 */
Mike Kravetz55254632019-11-30 17:56:30 -08003804 hash = hugetlb_fault_mutex_hash(h, mapping, idx);
Mike Kravetz1a1aad82017-02-22 15:43:01 -08003805 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
3806 ret = handle_userfault(&vmf, VM_UFFD_MISSING);
3807 mutex_lock(&hugetlb_fault_mutex_table[hash]);
3808 goto out;
3809 }
3810
Huang Ying285b8dc2018-06-07 17:08:08 -07003811 page = alloc_huge_page(vma, haddr, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08003812 if (IS_ERR(page)) {
Mike Kravetz4643d672019-08-13 15:38:00 -07003813 /*
3814 * Returning error will result in faulting task being
3815 * sent SIGBUS. The hugetlb fault mutex prevents two
3816 * tasks from racing to fault in the same page which
3817 * could result in false unable to allocate errors.
3818 * Page migration does not take the fault mutex, but
3819 * does a clear then write of pte's under page table
3820 * lock. Page fault code could race with migration,
3821 * notice the clear pte and try to allocate a page
3822 * here. Before returning error, get ptl and make
3823 * sure there really is no pte entry.
3824 */
3825 ptl = huge_pte_lock(h, mm, ptep);
3826 if (!huge_pte_none(huge_ptep_get(ptep))) {
3827 ret = 0;
3828 spin_unlock(ptl);
3829 goto out;
3830 }
3831 spin_unlock(ptl);
Souptick Joarder2b740302018-08-23 17:01:36 -07003832 ret = vmf_error(PTR_ERR(page));
Christoph Lameter6bda6662006-01-06 00:10:49 -08003833 goto out;
3834 }
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003835 clear_huge_page(page, address, pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08003836 __SetPageUptodate(page);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08003837 new_page = true;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003838
Mel Gormanf83a2752009-05-28 14:34:40 -07003839 if (vma->vm_flags & VM_MAYSHARE) {
Mike Kravetzab76ad52015-09-08 15:01:50 -07003840 int err = huge_add_to_page_cache(page, mapping, idx);
Christoph Lameter6bda6662006-01-06 00:10:49 -08003841 if (err) {
3842 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08003843 if (err == -EEXIST)
3844 goto retry;
3845 goto out;
3846 }
Mel Gorman23be7462010-04-23 13:17:56 -04003847 } else {
Christoph Lameter6bda6662006-01-06 00:10:49 -08003848 lock_page(page);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003849 if (unlikely(anon_vma_prepare(vma))) {
3850 ret = VM_FAULT_OOM;
3851 goto backout_unlocked;
3852 }
Hillf Danton409eb8c2012-01-20 14:34:13 -08003853 anon_rmap = 1;
Mel Gorman23be7462010-04-23 13:17:56 -04003854 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003855 } else {
Naoya Horiguchi998b4382010-09-08 10:19:32 +09003856 /*
3857 * If memory error occurs between mmap() and fault, some process
3858 * don't have hwpoisoned swap entry for errored virtual address.
3859 * So we need to block hugepage fault by PG_hwpoison bit check.
3860 */
3861 if (unlikely(PageHWPoison(page))) {
Chris Forbes32f84522011-07-25 17:12:14 -07003862 ret = VM_FAULT_HWPOISON |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07003863 VM_FAULT_SET_HINDEX(hstate_index(h));
Naoya Horiguchi998b4382010-09-08 10:19:32 +09003864 goto backout_unlocked;
3865 }
Christoph Lameter6bda6662006-01-06 00:10:49 -08003866 }
David Gibson1e8f8892006-01-06 00:10:44 -08003867
Andy Whitcroft57303d82008-08-12 15:08:47 -07003868 /*
3869 * If we are going to COW a private mapping later, we examine the
3870 * pending reservations for this page now. This will ensure that
3871 * any allocations necessary to record that reservation occur outside
3872 * the spinlock.
3873 */
Mike Kravetz5e911372015-09-08 15:01:28 -07003874 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07003875 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07003876 ret = VM_FAULT_OOM;
3877 goto backout_unlocked;
3878 }
Mike Kravetz5e911372015-09-08 15:01:28 -07003879 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07003880 vma_end_reservation(h, vma, haddr);
Mike Kravetz5e911372015-09-08 15:01:28 -07003881 }
Andy Whitcroft57303d82008-08-12 15:08:47 -07003882
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08003883 ptl = huge_pte_lock(h, mm, ptep);
Mike Kravetze7c58092019-01-08 15:23:32 -08003884 size = i_size_read(mapping->host) >> huge_page_shift(h);
3885 if (idx >= size)
3886 goto backout;
Adam Litke4c887262005-10-29 18:16:46 -07003887
Nick Piggin83c54072007-07-19 01:47:05 -07003888 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07003889 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07003890 goto backout;
3891
Joonsoo Kim07443a82013-09-11 14:21:58 -07003892 if (anon_rmap) {
3893 ClearPagePrivate(page);
Huang Ying285b8dc2018-06-07 17:08:08 -07003894 hugepage_add_new_anon_rmap(page, vma, haddr);
Choi Gi-yongac714902014-04-07 15:37:36 -07003895 } else
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08003896 page_dup_rmap(page, true);
David Gibson1e8f8892006-01-06 00:10:44 -08003897 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
3898 && (vma->vm_flags & VM_SHARED)));
Huang Ying285b8dc2018-06-07 17:08:08 -07003899 set_huge_pte_at(mm, haddr, ptep, new_pte);
David Gibson1e8f8892006-01-06 00:10:44 -08003900
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003901 hugetlb_count_add(pages_per_huge_page(h), mm);
Hugh Dickins788c7df2009-06-23 13:49:05 +01003902 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
David Gibson1e8f8892006-01-06 00:10:44 -08003903 /* Optimization, do the COW without a second fault */
Huang Ying974e6d62018-08-17 15:45:57 -07003904 ret = hugetlb_cow(mm, vma, address, ptep, page, ptl);
David Gibson1e8f8892006-01-06 00:10:44 -08003905 }
3906
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003907 spin_unlock(ptl);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08003908
3909 /*
3910 * Only make newly allocated pages active. Existing pages found
3911 * in the pagecache could be !page_huge_active() if they have been
3912 * isolated for migration.
3913 */
3914 if (new_page)
3915 set_page_huge_active(page);
3916
Adam Litke4c887262005-10-29 18:16:46 -07003917 unlock_page(page);
3918out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003919 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07003920
3921backout:
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003922 spin_unlock(ptl);
Andy Whitcroft2b267362008-08-12 15:08:49 -07003923backout_unlocked:
Adam Litke4c887262005-10-29 18:16:46 -07003924 unlock_page(page);
Huang Ying285b8dc2018-06-07 17:08:08 -07003925 restore_reserve_on_error(h, vma, haddr, page);
Adam Litke4c887262005-10-29 18:16:46 -07003926 put_page(page);
3927 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003928}
3929
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003930#ifdef CONFIG_SMP
Mike Kravetz1b426ba2019-05-13 17:19:41 -07003931u32 hugetlb_fault_mutex_hash(struct hstate *h, struct address_space *mapping,
Mike Kravetz55254632019-11-30 17:56:30 -08003932 pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003933{
3934 unsigned long key[2];
3935 u32 hash;
3936
Mike Kravetz1b426ba2019-05-13 17:19:41 -07003937 key[0] = (unsigned long) mapping;
3938 key[1] = idx;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003939
Mike Kravetz55254632019-11-30 17:56:30 -08003940 hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003941
3942 return hash & (num_fault_mutexes - 1);
3943}
3944#else
3945/*
3946 * For uniprocesor systems we always use a single mutex, so just
3947 * return 0 and avoid the hashing overhead.
3948 */
Mike Kravetz1b426ba2019-05-13 17:19:41 -07003949u32 hugetlb_fault_mutex_hash(struct hstate *h, struct address_space *mapping,
Mike Kravetz55254632019-11-30 17:56:30 -08003950 pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003951{
3952 return 0;
3953}
3954#endif
3955
Souptick Joarder2b740302018-08-23 17:01:36 -07003956vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
Hugh Dickins788c7df2009-06-23 13:49:05 +01003957 unsigned long address, unsigned int flags)
Adam Litke86e52162006-01-06 00:10:43 -08003958{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003959 pte_t *ptep, entry;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003960 spinlock_t *ptl;
Souptick Joarder2b740302018-08-23 17:01:36 -07003961 vm_fault_t ret;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003962 u32 hash;
3963 pgoff_t idx;
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003964 struct page *page = NULL;
Andy Whitcroft57303d82008-08-12 15:08:47 -07003965 struct page *pagecache_page = NULL;
Andi Kleena5516432008-07-23 21:27:41 -07003966 struct hstate *h = hstate_vma(vma);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003967 struct address_space *mapping;
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08003968 int need_wait_lock = 0;
Huang Ying285b8dc2018-06-07 17:08:08 -07003969 unsigned long haddr = address & huge_page_mask(h);
Adam Litke86e52162006-01-06 00:10:43 -08003970
Huang Ying285b8dc2018-06-07 17:08:08 -07003971 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09003972 if (ptep) {
3973 entry = huge_ptep_get(ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09003974 if (unlikely(is_hugetlb_entry_migration(entry))) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003975 migration_entry_wait_huge(vma, mm, ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09003976 return 0;
3977 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
Chris Forbes32f84522011-07-25 17:12:14 -07003978 return VM_FAULT_HWPOISON_LARGE |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07003979 VM_FAULT_SET_HINDEX(hstate_index(h));
Mike Kravetzddeaab32019-01-08 15:23:36 -08003980 } else {
3981 ptep = huge_pte_alloc(mm, haddr, huge_page_size(h));
3982 if (!ptep)
3983 return VM_FAULT_OOM;
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09003984 }
3985
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003986 mapping = vma->vm_file->f_mapping;
Mike Kravetzddeaab32019-01-08 15:23:36 -08003987 idx = vma_hugecache_offset(h, vma, haddr);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003988
David Gibson3935baa2006-03-22 00:08:53 -08003989 /*
3990 * Serialize hugepage allocation and instantiation, so that we don't
3991 * get spurious allocation failures if two CPUs race to instantiate
3992 * the same page in the page cache.
3993 */
Mike Kravetz55254632019-11-30 17:56:30 -08003994 hash = hugetlb_fault_mutex_hash(h, mapping, idx);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07003995 mutex_lock(&hugetlb_fault_mutex_table[hash]);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003996
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07003997 entry = huge_ptep_get(ptep);
3998 if (huge_pte_none(entry)) {
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003999 ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags);
David Gibsonb4d1d992008-10-15 22:01:11 -07004000 goto out_mutex;
David Gibson3935baa2006-03-22 00:08:53 -08004001 }
Adam Litke86e52162006-01-06 00:10:43 -08004002
Nick Piggin83c54072007-07-19 01:47:05 -07004003 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08004004
Andy Whitcroft57303d82008-08-12 15:08:47 -07004005 /*
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004006 * entry could be a migration/hwpoison entry at this point, so this
4007 * check prevents the kernel from going below assuming that we have
4008 * a active hugepage in pagecache. This goto expects the 2nd page fault,
4009 * and is_hugetlb_entry_(migration|hwpoisoned) check will properly
4010 * handle it.
4011 */
4012 if (!pte_present(entry))
4013 goto out_mutex;
4014
4015 /*
Andy Whitcroft57303d82008-08-12 15:08:47 -07004016 * If we are going to COW the mapping later, we examine the pending
4017 * reservations for this page now. This will ensure that any
4018 * allocations necessary to record that reservation occur outside the
4019 * spinlock. For private mappings, we also lookup the pagecache
4020 * page now as it is used to determine if a reservation has been
4021 * consumed.
4022 */
Gerald Schaefer106c9922013-04-29 15:07:23 -07004023 if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07004024 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07004025 ret = VM_FAULT_OOM;
David Gibsonb4d1d992008-10-15 22:01:11 -07004026 goto out_mutex;
Andy Whitcroft2b267362008-08-12 15:08:49 -07004027 }
Mike Kravetz5e911372015-09-08 15:01:28 -07004028 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07004029 vma_end_reservation(h, vma, haddr);
Andy Whitcroft57303d82008-08-12 15:08:47 -07004030
Mel Gormanf83a2752009-05-28 14:34:40 -07004031 if (!(vma->vm_flags & VM_MAYSHARE))
Andy Whitcroft57303d82008-08-12 15:08:47 -07004032 pagecache_page = hugetlbfs_pagecache_page(h,
Huang Ying285b8dc2018-06-07 17:08:08 -07004033 vma, haddr);
Andy Whitcroft57303d82008-08-12 15:08:47 -07004034 }
4035
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004036 ptl = huge_pte_lock(h, mm, ptep);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09004037
David Gibson1e8f8892006-01-06 00:10:44 -08004038 /* Check for a racing update before calling hugetlb_cow */
David Gibsonb4d1d992008-10-15 22:01:11 -07004039 if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004040 goto out_ptl;
David Gibsonb4d1d992008-10-15 22:01:11 -07004041
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004042 /*
4043 * hugetlb_cow() requires page locks of pte_page(entry) and
4044 * pagecache_page, so here we need take the former one
4045 * when page != pagecache_page or !pagecache_page.
4046 */
4047 page = pte_page(entry);
4048 if (page != pagecache_page)
4049 if (!trylock_page(page)) {
4050 need_wait_lock = 1;
4051 goto out_ptl;
4052 }
4053
4054 get_page(page);
David Gibsonb4d1d992008-10-15 22:01:11 -07004055
Hugh Dickins788c7df2009-06-23 13:49:05 +01004056 if (flags & FAULT_FLAG_WRITE) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07004057 if (!huge_pte_write(entry)) {
Huang Ying974e6d62018-08-17 15:45:57 -07004058 ret = hugetlb_cow(mm, vma, address, ptep,
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08004059 pagecache_page, ptl);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004060 goto out_put_page;
David Gibsonb4d1d992008-10-15 22:01:11 -07004061 }
Gerald Schaefer106c9922013-04-29 15:07:23 -07004062 entry = huge_pte_mkdirty(entry);
David Gibsonb4d1d992008-10-15 22:01:11 -07004063 }
4064 entry = pte_mkyoung(entry);
Huang Ying285b8dc2018-06-07 17:08:08 -07004065 if (huge_ptep_set_access_flags(vma, haddr, ptep, entry,
Hugh Dickins788c7df2009-06-23 13:49:05 +01004066 flags & FAULT_FLAG_WRITE))
Huang Ying285b8dc2018-06-07 17:08:08 -07004067 update_mmu_cache(vma, haddr, ptep);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004068out_put_page:
4069 if (page != pagecache_page)
4070 unlock_page(page);
4071 put_page(page);
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004072out_ptl:
4073 spin_unlock(ptl);
Andy Whitcroft57303d82008-08-12 15:08:47 -07004074
4075 if (pagecache_page) {
4076 unlock_page(pagecache_page);
4077 put_page(pagecache_page);
4078 }
David Gibsonb4d1d992008-10-15 22:01:11 -07004079out_mutex:
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004080 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004081 /*
4082 * Generally it's safe to hold refcount during waiting page lock. But
4083 * here we just wait to defer the next page fault to avoid busy loop and
4084 * the page is not used after unlocked before returning from the current
4085 * page fault. So we are safe from accessing freed page, even if we wait
4086 * here without taking refcount.
4087 */
4088 if (need_wait_lock)
4089 wait_on_page_locked(page);
David Gibson1e8f8892006-01-06 00:10:44 -08004090 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08004091}
4092
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004093/*
4094 * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with
4095 * modifications for huge pages.
4096 */
4097int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
4098 pte_t *dst_pte,
4099 struct vm_area_struct *dst_vma,
4100 unsigned long dst_addr,
4101 unsigned long src_addr,
4102 struct page **pagep)
4103{
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004104 struct address_space *mapping;
4105 pgoff_t idx;
4106 unsigned long size;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004107 int vm_shared = dst_vma->vm_flags & VM_SHARED;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004108 struct hstate *h = hstate_vma(dst_vma);
4109 pte_t _dst_pte;
4110 spinlock_t *ptl;
4111 int ret;
4112 struct page *page;
4113
4114 if (!*pagep) {
4115 ret = -ENOMEM;
4116 page = alloc_huge_page(dst_vma, dst_addr, 0);
4117 if (IS_ERR(page))
4118 goto out;
4119
4120 ret = copy_huge_page_from_user(page,
4121 (const void __user *) src_addr,
Mike Kravetz810a56b2017-02-22 15:42:58 -08004122 pages_per_huge_page(h), false);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004123
4124 /* fallback to copy_from_user outside mmap_sem */
4125 if (unlikely(ret)) {
Andrea Arcangeli9e368252018-11-30 14:09:25 -08004126 ret = -ENOENT;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004127 *pagep = page;
4128 /* don't free the page */
4129 goto out;
4130 }
4131 } else {
4132 page = *pagep;
4133 *pagep = NULL;
4134 }
4135
4136 /*
4137 * The memory barrier inside __SetPageUptodate makes sure that
4138 * preceding stores to the page contents become visible before
4139 * the set_pte_at() write.
4140 */
4141 __SetPageUptodate(page);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004142
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004143 mapping = dst_vma->vm_file->f_mapping;
4144 idx = vma_hugecache_offset(h, dst_vma, dst_addr);
4145
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004146 /*
4147 * If shared, add to page cache
4148 */
4149 if (vm_shared) {
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004150 size = i_size_read(mapping->host) >> huge_page_shift(h);
4151 ret = -EFAULT;
4152 if (idx >= size)
4153 goto out_release_nounlock;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004154
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004155 /*
4156 * Serialization between remove_inode_hugepages() and
4157 * huge_add_to_page_cache() below happens through the
4158 * hugetlb_fault_mutex_table that here must be hold by
4159 * the caller.
4160 */
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004161 ret = huge_add_to_page_cache(page, mapping, idx);
4162 if (ret)
4163 goto out_release_nounlock;
4164 }
4165
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004166 ptl = huge_pte_lockptr(h, dst_mm, dst_pte);
4167 spin_lock(ptl);
4168
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004169 /*
4170 * Recheck the i_size after holding PT lock to make sure not
4171 * to leave any page mapped (as page_mapped()) beyond the end
4172 * of the i_size (remove_inode_hugepages() is strict about
4173 * enforcing that). If we bail out here, we'll also leave a
4174 * page in the radix tree in the vm_shared case beyond the end
4175 * of the i_size, but remove_inode_hugepages() will take care
4176 * of it as soon as we drop the hugetlb_fault_mutex_table.
4177 */
4178 size = i_size_read(mapping->host) >> huge_page_shift(h);
4179 ret = -EFAULT;
4180 if (idx >= size)
4181 goto out_release_unlock;
4182
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004183 ret = -EEXIST;
4184 if (!huge_pte_none(huge_ptep_get(dst_pte)))
4185 goto out_release_unlock;
4186
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004187 if (vm_shared) {
4188 page_dup_rmap(page, true);
4189 } else {
4190 ClearPagePrivate(page);
4191 hugepage_add_new_anon_rmap(page, dst_vma, dst_addr);
4192 }
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004193
4194 _dst_pte = make_huge_pte(dst_vma, page, dst_vma->vm_flags & VM_WRITE);
4195 if (dst_vma->vm_flags & VM_WRITE)
4196 _dst_pte = huge_pte_mkdirty(_dst_pte);
4197 _dst_pte = pte_mkyoung(_dst_pte);
4198
4199 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
4200
4201 (void)huge_ptep_set_access_flags(dst_vma, dst_addr, dst_pte, _dst_pte,
4202 dst_vma->vm_flags & VM_WRITE);
4203 hugetlb_count_add(pages_per_huge_page(h), dst_mm);
4204
4205 /* No need to invalidate - it was non-present before */
4206 update_mmu_cache(dst_vma, dst_addr, dst_pte);
4207
4208 spin_unlock(ptl);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08004209 set_page_huge_active(page);
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004210 if (vm_shared)
4211 unlock_page(page);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004212 ret = 0;
4213out:
4214 return ret;
4215out_release_unlock:
4216 spin_unlock(ptl);
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004217 if (vm_shared)
4218 unlock_page(page);
Andrea Arcangeli5af10df2017-08-10 15:23:38 -07004219out_release_nounlock:
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004220 put_page(page);
4221 goto out;
4222}
4223
Michel Lespinasse28a35712013-02-22 16:35:55 -08004224long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
4225 struct page **pages, struct vm_area_struct **vmas,
4226 unsigned long *position, unsigned long *nr_pages,
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004227 long i, unsigned int flags, int *nonblocking)
David Gibson63551ae2005-06-21 17:14:44 -07004228{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004229 unsigned long pfn_offset;
4230 unsigned long vaddr = *position;
Michel Lespinasse28a35712013-02-22 16:35:55 -08004231 unsigned long remainder = *nr_pages;
Andi Kleena5516432008-07-23 21:27:41 -07004232 struct hstate *h = hstate_vma(vma);
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07004233 int err = -EFAULT;
David Gibson63551ae2005-06-21 17:14:44 -07004234
David Gibson63551ae2005-06-21 17:14:44 -07004235 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07004236 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004237 spinlock_t *ptl = NULL;
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004238 int absent;
Adam Litke4c887262005-10-29 18:16:46 -07004239 struct page *page;
4240
4241 /*
David Rientjes02057962015-04-14 15:48:24 -07004242 * If we have a pending SIGKILL, don't keep faulting pages and
4243 * potentially allocating memory.
4244 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -08004245 if (fatal_signal_pending(current)) {
David Rientjes02057962015-04-14 15:48:24 -07004246 remainder = 0;
4247 break;
4248 }
4249
4250 /*
Adam Litke4c887262005-10-29 18:16:46 -07004251 * Some archs (sparc64, sh*) have multiple pte_ts to
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004252 * each hugepage. We have to make sure we get the
Adam Litke4c887262005-10-29 18:16:46 -07004253 * first, for the page indexing below to work.
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004254 *
4255 * Note that page table lock is not held when pte is null.
Adam Litke4c887262005-10-29 18:16:46 -07004256 */
Punit Agrawal7868a202017-07-06 15:39:42 -07004257 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h),
4258 huge_page_size(h));
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004259 if (pte)
4260 ptl = huge_pte_lock(h, mm, pte);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004261 absent = !pte || huge_pte_none(huge_ptep_get(pte));
Adam Litke4c887262005-10-29 18:16:46 -07004262
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004263 /*
4264 * When coredumping, it suits get_dump_page if we just return
Hugh Dickins3ae77f42009-09-21 17:03:33 -07004265 * an error where there's an empty slot with no huge pagecache
4266 * to back it. This way, we avoid allocating a hugepage, and
4267 * the sparse dumpfile avoids allocating disk blocks, but its
4268 * huge holes still show up with zeroes where they need to be.
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004269 */
Hugh Dickins3ae77f42009-09-21 17:03:33 -07004270 if (absent && (flags & FOLL_DUMP) &&
4271 !hugetlbfs_pagecache_present(h, vma, vaddr)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004272 if (pte)
4273 spin_unlock(ptl);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004274 remainder = 0;
4275 break;
4276 }
4277
Naoya Horiguchi9cc3a5b2013-04-17 15:58:30 -07004278 /*
4279 * We need call hugetlb_fault for both hugepages under migration
4280 * (in which case hugetlb_fault waits for the migration,) and
4281 * hwpoisoned hugepages (in which case we need to prevent the
4282 * caller from accessing to them.) In order to do this, we use
4283 * here is_swap_pte instead of is_hugetlb_entry_migration and
4284 * is_hugetlb_entry_hwpoisoned. This is because it simply covers
4285 * both cases, and because we can't follow correct pages
4286 * directly from any kind of swap entries.
4287 */
4288 if (absent || is_swap_pte(huge_ptep_get(pte)) ||
Gerald Schaefer106c9922013-04-29 15:07:23 -07004289 ((flags & FOLL_WRITE) &&
4290 !huge_pte_write(huge_ptep_get(pte)))) {
Souptick Joarder2b740302018-08-23 17:01:36 -07004291 vm_fault_t ret;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004292 unsigned int fault_flags = 0;
Adam Litke4c887262005-10-29 18:16:46 -07004293
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004294 if (pte)
4295 spin_unlock(ptl);
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004296 if (flags & FOLL_WRITE)
4297 fault_flags |= FAULT_FLAG_WRITE;
4298 if (nonblocking)
4299 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
4300 if (flags & FOLL_NOWAIT)
4301 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
4302 FAULT_FLAG_RETRY_NOWAIT;
4303 if (flags & FOLL_TRIED) {
4304 VM_WARN_ON_ONCE(fault_flags &
4305 FAULT_FLAG_ALLOW_RETRY);
4306 fault_flags |= FAULT_FLAG_TRIED;
4307 }
4308 ret = hugetlb_fault(mm, vma, vaddr, fault_flags);
4309 if (ret & VM_FAULT_ERROR) {
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07004310 err = vm_fault_to_errno(ret, flags);
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004311 remainder = 0;
4312 break;
4313 }
4314 if (ret & VM_FAULT_RETRY) {
Andrea Arcangeli1ac25012019-02-01 14:20:16 -08004315 if (nonblocking &&
4316 !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004317 *nonblocking = 0;
4318 *nr_pages = 0;
4319 /*
4320 * VM_FAULT_RETRY must not return an
4321 * error, it will return zero
4322 * instead.
4323 *
4324 * No need to update "position" as the
4325 * caller will not check it after
4326 * *nr_pages is set to 0.
4327 */
4328 return i;
4329 }
4330 continue;
Adam Litke4c887262005-10-29 18:16:46 -07004331 }
David Gibson63551ae2005-06-21 17:14:44 -07004332
Andi Kleena5516432008-07-23 21:27:41 -07004333 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07004334 page = pte_page(huge_ptep_get(pte));
Linus Torvalds8fde12c2019-04-11 10:49:19 -07004335
4336 /*
4337 * Instead of doing 'try_get_page()' below in the same_page
4338 * loop, just check the count once here.
4339 */
4340 if (unlikely(page_count(page) <= 0)) {
4341 if (pages) {
4342 spin_unlock(ptl);
4343 remainder = 0;
4344 err = -ENOMEM;
4345 break;
4346 }
4347 }
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004348same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08004349 if (pages) {
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004350 pages[i] = mem_map_offset(page, pfn_offset);
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08004351 get_page(pages[i]);
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08004352 }
David Gibson63551ae2005-06-21 17:14:44 -07004353
4354 if (vmas)
4355 vmas[i] = vma;
4356
4357 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004358 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07004359 --remainder;
4360 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004361 if (vaddr < vma->vm_end && remainder &&
Andi Kleena5516432008-07-23 21:27:41 -07004362 pfn_offset < pages_per_huge_page(h)) {
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004363 /*
4364 * We use pfn_offset to avoid touching the pageframes
4365 * of this compound page.
4366 */
4367 goto same_page;
4368 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004369 spin_unlock(ptl);
David Gibson63551ae2005-06-21 17:14:44 -07004370 }
Michel Lespinasse28a35712013-02-22 16:35:55 -08004371 *nr_pages = remainder;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004372 /*
4373 * setting position is actually required only if remainder is
4374 * not zero but it's faster not to add a "if (remainder)"
4375 * branch.
4376 */
David Gibson63551ae2005-06-21 17:14:44 -07004377 *position = vaddr;
4378
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07004379 return i ? i : err;
David Gibson63551ae2005-06-21 17:14:44 -07004380}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004381
Aneesh Kumar K.V5491ae72016-07-13 15:06:43 +05304382#ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
4383/*
4384 * ARCHes with special requirements for evicting HUGETLB backing TLB entries can
4385 * implement this.
4386 */
4387#define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
4388#endif
4389
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004390unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004391 unsigned long address, unsigned long end, pgprot_t newprot)
4392{
4393 struct mm_struct *mm = vma->vm_mm;
4394 unsigned long start = address;
4395 pte_t *ptep;
4396 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07004397 struct hstate *h = hstate_vma(vma);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004398 unsigned long pages = 0;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004399 bool shared_pmd = false;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004400 struct mmu_notifier_range range;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004401
4402 /*
4403 * In the case of shared PMDs, the area to flush could be beyond
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004404 * start/end. Set range.start/range.end to cover the maximum possible
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004405 * range if PMD sharing is possible.
4406 */
Jérôme Glisse7269f992019-05-13 17:20:53 -07004407 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
4408 0, vma, mm, start, end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004409 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004410
4411 BUG_ON(address >= end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004412 flush_cache_range(vma, range.start, range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004413
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004414 mmu_notifier_invalidate_range_start(&range);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08004415 i_mmap_lock_write(vma->vm_file->f_mapping);
Andi Kleena5516432008-07-23 21:27:41 -07004416 for (; address < end; address += huge_page_size(h)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004417 spinlock_t *ptl;
Punit Agrawal7868a202017-07-06 15:39:42 -07004418 ptep = huge_pte_offset(mm, address, huge_page_size(h));
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004419 if (!ptep)
4420 continue;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004421 ptl = huge_pte_lock(h, mm, ptep);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004422 if (huge_pmd_unshare(mm, &address, ptep)) {
4423 pages++;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004424 spin_unlock(ptl);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004425 shared_pmd = true;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08004426 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004427 }
Naoya Horiguchia8bda282015-02-11 15:25:28 -08004428 pte = huge_ptep_get(ptep);
4429 if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
4430 spin_unlock(ptl);
4431 continue;
4432 }
4433 if (unlikely(is_hugetlb_entry_migration(pte))) {
4434 swp_entry_t entry = pte_to_swp_entry(pte);
4435
4436 if (is_write_migration_entry(entry)) {
4437 pte_t newpte;
4438
4439 make_migration_entry_read(&entry);
4440 newpte = swp_entry_to_pte(entry);
Punit Agrawale5251fd2017-07-06 15:39:50 -07004441 set_huge_swap_pte_at(mm, address, ptep,
4442 newpte, huge_page_size(h));
Naoya Horiguchia8bda282015-02-11 15:25:28 -08004443 pages++;
4444 }
4445 spin_unlock(ptl);
4446 continue;
4447 }
4448 if (!huge_pte_none(pte)) {
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08004449 pte_t old_pte;
4450
4451 old_pte = huge_ptep_modify_prot_start(vma, address, ptep);
4452 pte = pte_mkhuge(huge_pte_modify(old_pte, newprot));
Tony Lube7517d2013-02-04 14:28:46 -08004453 pte = arch_make_huge_pte(pte, vma, NULL, 0);
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08004454 huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004455 pages++;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004456 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004457 spin_unlock(ptl);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004458 }
Mel Gormand8333522012-07-31 16:46:20 -07004459 /*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08004460 * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
Mel Gormand8333522012-07-31 16:46:20 -07004461 * may have cleared our pud entry and done put_page on the page table:
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08004462 * once we release i_mmap_rwsem, another task can do the final put_page
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004463 * and that page table be reused and filled with junk. If we actually
4464 * did unshare a page of pmds, flush the range corresponding to the pud.
Mel Gormand8333522012-07-31 16:46:20 -07004465 */
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004466 if (shared_pmd)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004467 flush_hugetlb_tlb_range(vma, range.start, range.end);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004468 else
4469 flush_hugetlb_tlb_range(vma, start, end);
Jérôme Glisse0f108512017-11-15 17:34:07 -08004470 /*
4471 * No need to call mmu_notifier_invalidate_range() we are downgrading
4472 * page table protection not changing it to point to a new page.
4473 *
Mike Rapoportad56b732018-03-21 21:22:47 +02004474 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08004475 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08004476 i_mmap_unlock_write(vma->vm_file->f_mapping);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004477 mmu_notifier_invalidate_range_end(&range);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004478
4479 return pages << h->order;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004480}
4481
Mel Gormana1e78772008-07-23 21:27:23 -07004482int hugetlb_reserve_pages(struct inode *inode,
4483 long from, long to,
Mel Gorman5a6fe122009-02-10 14:02:27 +00004484 struct vm_area_struct *vma,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09004485 vm_flags_t vm_flags)
Adam Litkee4e574b2007-10-16 01:26:19 -07004486{
Mel Gorman17c9d122009-02-11 16:34:16 +00004487 long ret, chg;
Andi Kleena5516432008-07-23 21:27:41 -07004488 struct hstate *h = hstate_inode(inode);
David Gibson90481622012-03-21 16:34:12 -07004489 struct hugepage_subpool *spool = subpool_inode(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004490 struct resv_map *resv_map;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004491 long gbl_reserve;
Adam Litkee4e574b2007-10-16 01:26:19 -07004492
Mike Kravetz63489f82018-03-22 16:17:13 -07004493 /* This should never happen */
4494 if (from > to) {
4495 VM_WARN(1, "%s called with a negative range\n", __func__);
4496 return -EINVAL;
4497 }
4498
Mel Gormana1e78772008-07-23 21:27:23 -07004499 /*
Mel Gorman17c9d122009-02-11 16:34:16 +00004500 * Only apply hugepage reservation if asked. At fault time, an
4501 * attempt will be made for VM_NORESERVE to allocate a page
David Gibson90481622012-03-21 16:34:12 -07004502 * without using reserves
Mel Gorman17c9d122009-02-11 16:34:16 +00004503 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09004504 if (vm_flags & VM_NORESERVE)
Mel Gorman17c9d122009-02-11 16:34:16 +00004505 return 0;
4506
4507 /*
Mel Gormana1e78772008-07-23 21:27:23 -07004508 * Shared mappings base their reservation on the number of pages that
4509 * are already allocated on behalf of the file. Private mappings need
4510 * to reserve the full area even if read-only as mprotect() may be
4511 * called to make the mapping read-write. Assume !vma is a shm mapping
4512 */
Joonsoo Kim9119a412014-04-03 14:47:25 -07004513 if (!vma || vma->vm_flags & VM_MAYSHARE) {
Mike Kravetzf27a5132019-05-13 17:22:55 -07004514 /*
4515 * resv_map can not be NULL as hugetlb_reserve_pages is only
4516 * called for inodes for which resv_maps were created (see
4517 * hugetlbfs_get_inode).
4518 */
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004519 resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004520
Joonsoo Kim1406ec92014-04-03 14:47:26 -07004521 chg = region_chg(resv_map, from, to);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004522
4523 } else {
4524 resv_map = resv_map_alloc();
Mel Gorman5a6fe122009-02-10 14:02:27 +00004525 if (!resv_map)
4526 return -ENOMEM;
4527
Mel Gorman17c9d122009-02-11 16:34:16 +00004528 chg = to - from;
4529
Mel Gorman5a6fe122009-02-10 14:02:27 +00004530 set_vma_resv_map(vma, resv_map);
4531 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
4532 }
4533
Dave Hansenc50ac052012-05-29 15:06:46 -07004534 if (chg < 0) {
4535 ret = chg;
4536 goto out_err;
4537 }
Mel Gorman17c9d122009-02-11 16:34:16 +00004538
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004539 /*
4540 * There must be enough pages in the subpool for the mapping. If
4541 * the subpool has a minimum size, there may be some global
4542 * reservations already in place (gbl_reserve).
4543 */
4544 gbl_reserve = hugepage_subpool_get_pages(spool, chg);
4545 if (gbl_reserve < 0) {
Dave Hansenc50ac052012-05-29 15:06:46 -07004546 ret = -ENOSPC;
4547 goto out_err;
4548 }
Mel Gorman17c9d122009-02-11 16:34:16 +00004549
4550 /*
4551 * Check enough hugepages are available for the reservation.
David Gibson90481622012-03-21 16:34:12 -07004552 * Hand the pages back to the subpool if there are not
Mel Gorman17c9d122009-02-11 16:34:16 +00004553 */
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004554 ret = hugetlb_acct_memory(h, gbl_reserve);
Mel Gorman17c9d122009-02-11 16:34:16 +00004555 if (ret < 0) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004556 /* put back original number of pages, chg */
4557 (void)hugepage_subpool_put_pages(spool, chg);
Dave Hansenc50ac052012-05-29 15:06:46 -07004558 goto out_err;
Mel Gorman17c9d122009-02-11 16:34:16 +00004559 }
4560
4561 /*
4562 * Account for the reservations made. Shared mappings record regions
4563 * that have reservations as they are shared by multiple VMAs.
4564 * When the last VMA disappears, the region map says how much
4565 * the reservation was and the page cache tells how much of
4566 * the reservation was consumed. Private mappings are per-VMA and
4567 * only the consumed reservations are tracked. When the VMA
4568 * disappears, the original reservation is the VMA size and the
4569 * consumed reservations are stored in the map. Hence, nothing
4570 * else has to be done for private mappings here
4571 */
Mike Kravetz33039672015-06-24 16:57:58 -07004572 if (!vma || vma->vm_flags & VM_MAYSHARE) {
4573 long add = region_add(resv_map, from, to);
4574
4575 if (unlikely(chg > add)) {
4576 /*
4577 * pages in this range were added to the reserve
4578 * map between region_chg and region_add. This
4579 * indicates a race with alloc_huge_page. Adjust
4580 * the subpool and reserve counts modified above
4581 * based on the difference.
4582 */
4583 long rsv_adjust;
4584
4585 rsv_adjust = hugepage_subpool_put_pages(spool,
4586 chg - add);
4587 hugetlb_acct_memory(h, -rsv_adjust);
4588 }
4589 }
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07004590 return 0;
Dave Hansenc50ac052012-05-29 15:06:46 -07004591out_err:
Mike Kravetz5e911372015-09-08 15:01:28 -07004592 if (!vma || vma->vm_flags & VM_MAYSHARE)
Mike Kravetzff8c0c52017-03-31 15:12:07 -07004593 /* Don't call region_abort if region_chg failed */
4594 if (chg >= 0)
4595 region_abort(resv_map, from, to);
Joonsoo Kimf031dd22014-04-03 14:47:28 -07004596 if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
4597 kref_put(&resv_map->refs, resv_map_release);
Dave Hansenc50ac052012-05-29 15:06:46 -07004598 return ret;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07004599}
4600
Mike Kravetzb5cec282015-09-08 15:01:41 -07004601long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
4602 long freed)
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07004603{
Andi Kleena5516432008-07-23 21:27:41 -07004604 struct hstate *h = hstate_inode(inode);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004605 struct resv_map *resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004606 long chg = 0;
David Gibson90481622012-03-21 16:34:12 -07004607 struct hugepage_subpool *spool = subpool_inode(inode);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004608 long gbl_reserve;
Ken Chen45c682a2007-11-14 16:59:44 -08004609
Mike Kravetzf27a5132019-05-13 17:22:55 -07004610 /*
4611 * Since this routine can be called in the evict inode path for all
4612 * hugetlbfs inodes, resv_map could be NULL.
4613 */
Mike Kravetzb5cec282015-09-08 15:01:41 -07004614 if (resv_map) {
4615 chg = region_del(resv_map, start, end);
4616 /*
4617 * region_del() can fail in the rare case where a region
4618 * must be split and another region descriptor can not be
4619 * allocated. If end == LONG_MAX, it will not fail.
4620 */
4621 if (chg < 0)
4622 return chg;
4623 }
4624
Ken Chen45c682a2007-11-14 16:59:44 -08004625 spin_lock(&inode->i_lock);
Eric Sandeene4c6f8b2009-07-29 15:02:16 -07004626 inode->i_blocks -= (blocks_per_huge_page(h) * freed);
Ken Chen45c682a2007-11-14 16:59:44 -08004627 spin_unlock(&inode->i_lock);
4628
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004629 /*
4630 * If the subpool has a minimum size, the number of global
4631 * reservations to be released may be adjusted.
4632 */
4633 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
4634 hugetlb_acct_memory(h, -gbl_reserve);
Mike Kravetzb5cec282015-09-08 15:01:41 -07004635
4636 return 0;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07004637}
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09004638
Steve Capper3212b532013-04-23 12:35:02 +01004639#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
4640static unsigned long page_table_shareable(struct vm_area_struct *svma,
4641 struct vm_area_struct *vma,
4642 unsigned long addr, pgoff_t idx)
4643{
4644 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
4645 svma->vm_start;
4646 unsigned long sbase = saddr & PUD_MASK;
4647 unsigned long s_end = sbase + PUD_SIZE;
4648
4649 /* Allow segments to share if only one is marked locked */
Eric B Munsonde60f5f2015-11-05 18:51:36 -08004650 unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
4651 unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK;
Steve Capper3212b532013-04-23 12:35:02 +01004652
4653 /*
4654 * match the virtual addresses, permission and the alignment of the
4655 * page table page.
4656 */
4657 if (pmd_index(addr) != pmd_index(saddr) ||
4658 vm_flags != svm_flags ||
4659 sbase < svma->vm_start || svma->vm_end < s_end)
4660 return 0;
4661
4662 return saddr;
4663}
4664
Nicholas Krause31aafb42015-09-04 15:47:58 -07004665static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr)
Steve Capper3212b532013-04-23 12:35:02 +01004666{
4667 unsigned long base = addr & PUD_MASK;
4668 unsigned long end = base + PUD_SIZE;
4669
4670 /*
4671 * check on proper vm_flags and page table alignment
4672 */
Mike Kravetz017b1662018-10-05 15:51:29 -07004673 if (vma->vm_flags & VM_MAYSHARE && range_in_vma(vma, base, end))
Nicholas Krause31aafb42015-09-04 15:47:58 -07004674 return true;
4675 return false;
Steve Capper3212b532013-04-23 12:35:02 +01004676}
4677
4678/*
Mike Kravetz017b1662018-10-05 15:51:29 -07004679 * Determine if start,end range within vma could be mapped by shared pmd.
4680 * If yes, adjust start and end to cover range associated with possible
4681 * shared pmd mappings.
4682 */
4683void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
4684 unsigned long *start, unsigned long *end)
4685{
4686 unsigned long check_addr = *start;
4687
4688 if (!(vma->vm_flags & VM_MAYSHARE))
4689 return;
4690
4691 for (check_addr = *start; check_addr < *end; check_addr += PUD_SIZE) {
4692 unsigned long a_start = check_addr & PUD_MASK;
4693 unsigned long a_end = a_start + PUD_SIZE;
4694
4695 /*
4696 * If sharing is possible, adjust start/end if necessary.
4697 */
4698 if (range_in_vma(vma, a_start, a_end)) {
4699 if (a_start < *start)
4700 *start = a_start;
4701 if (a_end > *end)
4702 *end = a_end;
4703 }
4704 }
4705}
4706
4707/*
Steve Capper3212b532013-04-23 12:35:02 +01004708 * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
4709 * and returns the corresponding pte. While this is not necessary for the
4710 * !shared pmd case because we can allocate the pmd later as well, it makes the
Mike Kravetzddeaab32019-01-08 15:23:36 -08004711 * code much cleaner. pmd allocation is essential for the shared case because
4712 * pud has to be populated inside the same i_mmap_rwsem section - otherwise
4713 * racing tasks could either miss the sharing (see huge_pte_offset) or select a
4714 * bad pmd for sharing.
Steve Capper3212b532013-04-23 12:35:02 +01004715 */
4716pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
4717{
4718 struct vm_area_struct *vma = find_vma(mm, addr);
4719 struct address_space *mapping = vma->vm_file->f_mapping;
4720 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
4721 vma->vm_pgoff;
4722 struct vm_area_struct *svma;
4723 unsigned long saddr;
4724 pte_t *spte = NULL;
4725 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004726 spinlock_t *ptl;
Steve Capper3212b532013-04-23 12:35:02 +01004727
4728 if (!vma_shareable(vma, addr))
4729 return (pte_t *)pmd_alloc(mm, pud, addr);
4730
Waiman Long930668c2019-11-30 17:56:49 -08004731 i_mmap_lock_read(mapping);
Steve Capper3212b532013-04-23 12:35:02 +01004732 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
4733 if (svma == vma)
4734 continue;
4735
4736 saddr = page_table_shareable(svma, vma, addr, idx);
4737 if (saddr) {
Punit Agrawal7868a202017-07-06 15:39:42 -07004738 spte = huge_pte_offset(svma->vm_mm, saddr,
4739 vma_mmu_pagesize(svma));
Steve Capper3212b532013-04-23 12:35:02 +01004740 if (spte) {
4741 get_page(virt_to_page(spte));
4742 break;
4743 }
4744 }
4745 }
4746
4747 if (!spte)
4748 goto out;
4749
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08004750 ptl = huge_pte_lock(hstate_vma(vma), mm, spte);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004751 if (pud_none(*pud)) {
Steve Capper3212b532013-04-23 12:35:02 +01004752 pud_populate(mm, pud,
4753 (pmd_t *)((unsigned long)spte & PAGE_MASK));
Kirill A. Shutemovc17b1f42016-06-24 14:49:51 -07004754 mm_inc_nr_pmds(mm);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004755 } else {
Steve Capper3212b532013-04-23 12:35:02 +01004756 put_page(virt_to_page(spte));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004757 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004758 spin_unlock(ptl);
Steve Capper3212b532013-04-23 12:35:02 +01004759out:
4760 pte = (pte_t *)pmd_alloc(mm, pud, addr);
Waiman Long930668c2019-11-30 17:56:49 -08004761 i_mmap_unlock_read(mapping);
Steve Capper3212b532013-04-23 12:35:02 +01004762 return pte;
4763}
4764
4765/*
4766 * unmap huge page backed by shared pte.
4767 *
4768 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
4769 * indicated by page_count > 1, unmap is achieved by clearing pud and
4770 * decrementing the ref count. If count == 1, the pte page is not shared.
4771 *
Mike Kravetzddeaab32019-01-08 15:23:36 -08004772 * called with page table lock held.
Steve Capper3212b532013-04-23 12:35:02 +01004773 *
4774 * returns: 1 successfully unmapped a shared pte page
4775 * 0 the underlying pte page is not shared, or it is the last user
4776 */
4777int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
4778{
4779 pgd_t *pgd = pgd_offset(mm, *addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004780 p4d_t *p4d = p4d_offset(pgd, *addr);
4781 pud_t *pud = pud_offset(p4d, *addr);
Steve Capper3212b532013-04-23 12:35:02 +01004782
4783 BUG_ON(page_count(virt_to_page(ptep)) == 0);
4784 if (page_count(virt_to_page(ptep)) == 1)
4785 return 0;
4786
4787 pud_clear(pud);
4788 put_page(virt_to_page(ptep));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004789 mm_dec_nr_pmds(mm);
Steve Capper3212b532013-04-23 12:35:02 +01004790 *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
4791 return 1;
4792}
Steve Capper9e5fc742013-04-30 08:02:03 +01004793#define want_pmd_share() (1)
4794#else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
4795pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
4796{
4797 return NULL;
4798}
Zhang Zhene81f2d22015-06-24 16:56:13 -07004799
4800int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
4801{
4802 return 0;
4803}
Mike Kravetz017b1662018-10-05 15:51:29 -07004804
4805void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
4806 unsigned long *start, unsigned long *end)
4807{
4808}
Steve Capper9e5fc742013-04-30 08:02:03 +01004809#define want_pmd_share() (0)
Steve Capper3212b532013-04-23 12:35:02 +01004810#endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
4811
Steve Capper9e5fc742013-04-30 08:02:03 +01004812#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
4813pte_t *huge_pte_alloc(struct mm_struct *mm,
4814 unsigned long addr, unsigned long sz)
4815{
4816 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004817 p4d_t *p4d;
Steve Capper9e5fc742013-04-30 08:02:03 +01004818 pud_t *pud;
4819 pte_t *pte = NULL;
4820
4821 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovf4f0a3d2017-11-29 16:11:30 -08004822 p4d = p4d_alloc(mm, pgd, addr);
4823 if (!p4d)
4824 return NULL;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004825 pud = pud_alloc(mm, p4d, addr);
Steve Capper9e5fc742013-04-30 08:02:03 +01004826 if (pud) {
4827 if (sz == PUD_SIZE) {
4828 pte = (pte_t *)pud;
4829 } else {
4830 BUG_ON(sz != PMD_SIZE);
4831 if (want_pmd_share() && pud_none(*pud))
4832 pte = huge_pmd_share(mm, addr, pud);
4833 else
4834 pte = (pte_t *)pmd_alloc(mm, pud, addr);
4835 }
4836 }
Michal Hocko4e666312016-08-02 14:02:34 -07004837 BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte));
Steve Capper9e5fc742013-04-30 08:02:03 +01004838
4839 return pte;
4840}
4841
Punit Agrawal9b19df22017-09-06 16:21:01 -07004842/*
4843 * huge_pte_offset() - Walk the page table to resolve the hugepage
4844 * entry at address @addr
4845 *
4846 * Return: Pointer to page table or swap entry (PUD or PMD) for
4847 * address @addr, or NULL if a p*d_none() entry is encountered and the
4848 * size @sz doesn't match the hugepage size at this level of the page
4849 * table.
4850 */
Punit Agrawal7868a202017-07-06 15:39:42 -07004851pte_t *huge_pte_offset(struct mm_struct *mm,
4852 unsigned long addr, unsigned long sz)
Steve Capper9e5fc742013-04-30 08:02:03 +01004853{
4854 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004855 p4d_t *p4d;
Steve Capper9e5fc742013-04-30 08:02:03 +01004856 pud_t *pud;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004857 pmd_t *pmd;
Steve Capper9e5fc742013-04-30 08:02:03 +01004858
4859 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004860 if (!pgd_present(*pgd))
4861 return NULL;
4862 p4d = p4d_offset(pgd, addr);
4863 if (!p4d_present(*p4d))
4864 return NULL;
Punit Agrawal9b19df22017-09-06 16:21:01 -07004865
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004866 pud = pud_offset(p4d, addr);
Punit Agrawal9b19df22017-09-06 16:21:01 -07004867 if (sz != PUD_SIZE && pud_none(*pud))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004868 return NULL;
Punit Agrawal9b19df22017-09-06 16:21:01 -07004869 /* hugepage or swap? */
4870 if (pud_huge(*pud) || !pud_present(*pud))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004871 return (pte_t *)pud;
Punit Agrawal9b19df22017-09-06 16:21:01 -07004872
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004873 pmd = pmd_offset(pud, addr);
Punit Agrawal9b19df22017-09-06 16:21:01 -07004874 if (sz != PMD_SIZE && pmd_none(*pmd))
4875 return NULL;
4876 /* hugepage or swap? */
4877 if (pmd_huge(*pmd) || !pmd_present(*pmd))
4878 return (pte_t *)pmd;
4879
4880 return NULL;
Steve Capper9e5fc742013-04-30 08:02:03 +01004881}
4882
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08004883#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
4884
4885/*
4886 * These functions are overwritable if your architecture needs its own
4887 * behavior.
4888 */
4889struct page * __weak
4890follow_huge_addr(struct mm_struct *mm, unsigned long address,
4891 int write)
4892{
4893 return ERR_PTR(-EINVAL);
4894}
4895
4896struct page * __weak
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -07004897follow_huge_pd(struct vm_area_struct *vma,
4898 unsigned long address, hugepd_t hpd, int flags, int pdshift)
4899{
4900 WARN(1, "hugepd follow called with no support for hugepage directory format\n");
4901 return NULL;
4902}
4903
4904struct page * __weak
Steve Capper9e5fc742013-04-30 08:02:03 +01004905follow_huge_pmd(struct mm_struct *mm, unsigned long address,
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08004906 pmd_t *pmd, int flags)
Steve Capper9e5fc742013-04-30 08:02:03 +01004907{
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08004908 struct page *page = NULL;
4909 spinlock_t *ptl;
Naoya Horiguchic9d398f2017-03-31 15:11:55 -07004910 pte_t pte;
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08004911retry:
4912 ptl = pmd_lockptr(mm, pmd);
4913 spin_lock(ptl);
4914 /*
4915 * make sure that the address range covered by this pmd is not
4916 * unmapped from other threads.
4917 */
4918 if (!pmd_huge(*pmd))
4919 goto out;
Naoya Horiguchic9d398f2017-03-31 15:11:55 -07004920 pte = huge_ptep_get((pte_t *)pmd);
4921 if (pte_present(pte)) {
Gerald Schaefer97534122015-04-14 15:42:30 -07004922 page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT);
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08004923 if (flags & FOLL_GET)
4924 get_page(page);
4925 } else {
Naoya Horiguchic9d398f2017-03-31 15:11:55 -07004926 if (is_hugetlb_entry_migration(pte)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08004927 spin_unlock(ptl);
4928 __migration_entry_wait(mm, (pte_t *)pmd, ptl);
4929 goto retry;
4930 }
4931 /*
4932 * hwpoisoned entry is treated as no_page_table in
4933 * follow_page_mask().
4934 */
4935 }
4936out:
4937 spin_unlock(ptl);
Steve Capper9e5fc742013-04-30 08:02:03 +01004938 return page;
4939}
4940
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08004941struct page * __weak
Steve Capper9e5fc742013-04-30 08:02:03 +01004942follow_huge_pud(struct mm_struct *mm, unsigned long address,
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08004943 pud_t *pud, int flags)
Steve Capper9e5fc742013-04-30 08:02:03 +01004944{
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08004945 if (flags & FOLL_GET)
4946 return NULL;
Steve Capper9e5fc742013-04-30 08:02:03 +01004947
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08004948 return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
Steve Capper9e5fc742013-04-30 08:02:03 +01004949}
4950
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -07004951struct page * __weak
4952follow_huge_pgd(struct mm_struct *mm, unsigned long address, pgd_t *pgd, int flags)
4953{
4954 if (flags & FOLL_GET)
4955 return NULL;
4956
4957 return pte_page(*(pte_t *)pgd) + ((address & ~PGDIR_MASK) >> PAGE_SHIFT);
4958}
4959
Naoya Horiguchi31caf662013-09-11 14:21:59 -07004960bool isolate_huge_page(struct page *page, struct list_head *list)
4961{
Naoya Horiguchibcc54222015-04-15 16:14:38 -07004962 bool ret = true;
4963
Sasha Levin309381fea2014-01-23 15:52:54 -08004964 VM_BUG_ON_PAGE(!PageHead(page), page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07004965 spin_lock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07004966 if (!page_huge_active(page) || !get_page_unless_zero(page)) {
4967 ret = false;
4968 goto unlock;
4969 }
4970 clear_page_huge_active(page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07004971 list_move_tail(&page->lru, list);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07004972unlock:
Naoya Horiguchi31caf662013-09-11 14:21:59 -07004973 spin_unlock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07004974 return ret;
Naoya Horiguchi31caf662013-09-11 14:21:59 -07004975}
4976
4977void putback_active_hugepage(struct page *page)
4978{
Sasha Levin309381fea2014-01-23 15:52:54 -08004979 VM_BUG_ON_PAGE(!PageHead(page), page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07004980 spin_lock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07004981 set_page_huge_active(page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07004982 list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
4983 spin_unlock(&hugetlb_lock);
4984 put_page(page);
4985}
Michal Hockoab5ac902018-01-31 16:20:48 -08004986
4987void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
4988{
4989 struct hstate *h = page_hstate(oldpage);
4990
4991 hugetlb_cgroup_migrate(oldpage, newpage);
4992 set_page_owner_migrate_reason(newpage, reason);
4993
4994 /*
4995 * transfer temporary state of the new huge page. This is
4996 * reverse to other transitions because the newpage is going to
4997 * be final while the old one will be freed so it takes over
4998 * the temporary status.
4999 *
5000 * Also note that we have to transfer the per-node surplus state
5001 * here as well otherwise the global surplus count will not match
5002 * the per-node's.
5003 */
5004 if (PageHugeTemporary(newpage)) {
5005 int old_nid = page_to_nid(oldpage);
5006 int new_nid = page_to_nid(newpage);
5007
5008 SetPageHugeTemporary(oldpage);
5009 ClearPageHugeTemporary(newpage);
5010
5011 spin_lock(&hugetlb_lock);
5012 if (h->surplus_huge_pages_node[old_nid]) {
5013 h->surplus_huge_pages_node[old_nid]--;
5014 h->surplus_huge_pages_node[new_nid]++;
5015 }
5016 spin_unlock(&hugetlb_lock);
5017 }
5018}