blob: 0accbff524773b6a262f3a12d1fe83add8de3769 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Generic hugetlb support.
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +01004 * (C) Nadia Yvette Chambers, April 2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/list.h>
7#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
Alexey Dobriyane1759c22008-10-15 23:50:22 +04009#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/sysctl.h>
11#include <linux/highmem.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070012#include <linux/mmu_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/nodemask.h>
David Gibson63551ae2005-06-21 17:14:44 -070014#include <linux/pagemap.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080015#include <linux/mempolicy.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070016#include <linux/compiler.h>
Christoph Lameteraea47ff2006-01-08 01:00:57 -080017#include <linux/cpuset.h>
David Gibson3935baa2006-03-22 00:08:53 -080018#include <linux/mutex.h>
Mike Rapoport97ad1082018-10-30 15:09:44 -070019#include <linux/memblock.h>
Nishanth Aravamudana3437872008-07-23 21:27:44 -070020#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Mike Kravetz63489f82018-03-22 16:17:13 -070022#include <linux/mmdebug.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +090024#include <linux/rmap.h>
Matthew Wilcoxc6247f72017-07-10 15:48:56 -070025#include <linux/string_helpers.h>
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +090026#include <linux/swap.h>
27#include <linux/swapops.h>
Davidlohr Bueso8382d912014-04-03 14:47:31 -070028#include <linux/jhash.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -080029#include <linux/numa.h>
Waiman Longc77c0a82020-01-04 13:00:15 -080030#include <linux/llist.h>
Linus Torvaldsd6606682008-08-06 12:04:54 -070031
David Gibson63551ae2005-06-21 17:14:44 -070032#include <asm/page.h>
33#include <asm/pgtable.h>
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070034#include <asm/tlb.h>
David Gibson63551ae2005-06-21 17:14:44 -070035
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070036#include <linux/io.h>
David Gibson63551ae2005-06-21 17:14:44 -070037#include <linux/hugetlb.h>
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -070038#include <linux/hugetlb_cgroup.h>
Lee Schermerhorn9a3052302009-12-14 17:58:25 -080039#include <linux/node.h>
Mike Kravetz1a1aad82017-02-22 15:43:01 -080040#include <linux/userfaultfd_k.h>
Michal Hockoab5ac902018-01-31 16:20:48 -080041#include <linux/page_owner.h>
Nick Piggin7835e982006-03-22 00:08:40 -080042#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070044int hugetlb_max_hstate __read_mostly;
Andi Kleene5ff2152008-07-23 21:27:42 -070045unsigned int default_hstate_idx;
46struct hstate hstates[HUGE_MAX_HSTATE];
Naoya Horiguchi641844f2015-06-24 16:56:59 -070047/*
48 * Minimum page order among possible hugepage sizes, set to a proper value
49 * at boot time.
50 */
51static unsigned int minimum_order __read_mostly = UINT_MAX;
Andi Kleene5ff2152008-07-23 21:27:42 -070052
Jon Tollefson53ba51d2008-07-23 21:27:52 -070053__initdata LIST_HEAD(huge_boot_pages);
54
Andi Kleene5ff2152008-07-23 21:27:42 -070055/* for command line parsing */
56static struct hstate * __initdata parsed_hstate;
57static unsigned long __initdata default_hstate_max_huge_pages;
Nick Piggine11bfbf2008-07-23 21:27:52 -070058static unsigned long __initdata default_hstate_size;
Vaishali Thakkar9fee0212016-05-19 17:11:04 -070059static bool __initdata parsed_valid_hugepagesz = true;
Andi Kleene5ff2152008-07-23 21:27:42 -070060
David Gibson3935baa2006-03-22 00:08:53 -080061/*
Naoya Horiguchi31caf662013-09-11 14:21:59 -070062 * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
63 * free_huge_pages, and surplus_huge_pages.
David Gibson3935baa2006-03-22 00:08:53 -080064 */
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070065DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080066
Davidlohr Bueso8382d912014-04-03 14:47:31 -070067/*
68 * Serializes faults on the same logical page. This is used to
69 * prevent spurious OOMs when the hugepage pool is fully utilized.
70 */
71static int num_fault_mutexes;
Mike Kravetzc672c7f2015-09-08 15:01:35 -070072struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp;
Davidlohr Bueso8382d912014-04-03 14:47:31 -070073
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070074/* Forward declaration */
75static int hugetlb_acct_memory(struct hstate *h, long delta);
76
David Gibson90481622012-03-21 16:34:12 -070077static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
78{
79 bool free = (spool->count == 0) && (spool->used_hpages == 0);
80
81 spin_unlock(&spool->lock);
82
83 /* If no pages are used, and no other handles to the subpool
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070084 * remain, give up any reservations mased on minimum size and
85 * free the subpool */
86 if (free) {
87 if (spool->min_hpages != -1)
88 hugetlb_acct_memory(spool->hstate,
89 -spool->min_hpages);
David Gibson90481622012-03-21 16:34:12 -070090 kfree(spool);
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070091 }
David Gibson90481622012-03-21 16:34:12 -070092}
93
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070094struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
95 long min_hpages)
David Gibson90481622012-03-21 16:34:12 -070096{
97 struct hugepage_subpool *spool;
98
Mike Kravetzc6a91822015-04-15 16:13:36 -070099 spool = kzalloc(sizeof(*spool), GFP_KERNEL);
David Gibson90481622012-03-21 16:34:12 -0700100 if (!spool)
101 return NULL;
102
103 spin_lock_init(&spool->lock);
104 spool->count = 1;
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -0700105 spool->max_hpages = max_hpages;
106 spool->hstate = h;
107 spool->min_hpages = min_hpages;
108
109 if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) {
110 kfree(spool);
111 return NULL;
112 }
113 spool->rsv_hpages = min_hpages;
David Gibson90481622012-03-21 16:34:12 -0700114
115 return spool;
116}
117
118void hugepage_put_subpool(struct hugepage_subpool *spool)
119{
120 spin_lock(&spool->lock);
121 BUG_ON(!spool->count);
122 spool->count--;
123 unlock_or_release_subpool(spool);
124}
125
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700126/*
127 * Subpool accounting for allocating and reserving pages.
128 * Return -ENOMEM if there are not enough resources to satisfy the
129 * the request. Otherwise, return the number of pages by which the
130 * global pools must be adjusted (upward). The returned value may
131 * only be different than the passed value (delta) in the case where
132 * a subpool minimum size must be manitained.
133 */
134static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700135 long delta)
136{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700137 long ret = delta;
David Gibson90481622012-03-21 16:34:12 -0700138
139 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700140 return ret;
David Gibson90481622012-03-21 16:34:12 -0700141
142 spin_lock(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700143
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700144 if (spool->max_hpages != -1) { /* maximum size accounting */
145 if ((spool->used_hpages + delta) <= spool->max_hpages)
146 spool->used_hpages += delta;
147 else {
148 ret = -ENOMEM;
149 goto unlock_ret;
150 }
151 }
152
Mike Kravetz09a95e22016-05-19 17:11:01 -0700153 /* minimum size accounting */
154 if (spool->min_hpages != -1 && spool->rsv_hpages) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700155 if (delta > spool->rsv_hpages) {
156 /*
157 * Asking for more reserves than those already taken on
158 * behalf of subpool. Return difference.
159 */
160 ret = delta - spool->rsv_hpages;
161 spool->rsv_hpages = 0;
162 } else {
163 ret = 0; /* reserves already accounted for */
164 spool->rsv_hpages -= delta;
165 }
166 }
167
168unlock_ret:
169 spin_unlock(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700170 return ret;
171}
172
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700173/*
174 * Subpool accounting for freeing and unreserving pages.
175 * Return the number of global page reservations that must be dropped.
176 * The return value may only be different than the passed value (delta)
177 * in the case where a subpool minimum size must be maintained.
178 */
179static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700180 long delta)
181{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700182 long ret = delta;
183
David Gibson90481622012-03-21 16:34:12 -0700184 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700185 return delta;
David Gibson90481622012-03-21 16:34:12 -0700186
187 spin_lock(&spool->lock);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700188
189 if (spool->max_hpages != -1) /* maximum size accounting */
190 spool->used_hpages -= delta;
191
Mike Kravetz09a95e22016-05-19 17:11:01 -0700192 /* minimum size accounting */
193 if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700194 if (spool->rsv_hpages + delta <= spool->min_hpages)
195 ret = 0;
196 else
197 ret = spool->rsv_hpages + delta - spool->min_hpages;
198
199 spool->rsv_hpages += delta;
200 if (spool->rsv_hpages > spool->min_hpages)
201 spool->rsv_hpages = spool->min_hpages;
202 }
203
204 /*
205 * If hugetlbfs_put_super couldn't free spool due to an outstanding
206 * quota reference, free it now.
207 */
David Gibson90481622012-03-21 16:34:12 -0700208 unlock_or_release_subpool(spool);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700209
210 return ret;
David Gibson90481622012-03-21 16:34:12 -0700211}
212
213static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
214{
215 return HUGETLBFS_SB(inode->i_sb)->spool;
216}
217
218static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
219{
Al Viro496ad9a2013-01-23 17:07:38 -0500220 return subpool_inode(file_inode(vma->vm_file));
David Gibson90481622012-03-21 16:34:12 -0700221}
222
Mina Almasry0db9d742020-04-01 21:11:25 -0700223/* Helper that removes a struct file_region from the resv_map cache and returns
224 * it for use.
225 */
226static struct file_region *
227get_file_region_entry_from_cache(struct resv_map *resv, long from, long to)
228{
229 struct file_region *nrg = NULL;
230
231 VM_BUG_ON(resv->region_cache_count <= 0);
232
233 resv->region_cache_count--;
234 nrg = list_first_entry(&resv->region_cache, struct file_region, link);
235 VM_BUG_ON(!nrg);
236 list_del(&nrg->link);
237
238 nrg->from = from;
239 nrg->to = to;
240
241 return nrg;
242}
243
Mina Almasry075a61d2020-04-01 21:11:28 -0700244static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg,
245 struct file_region *rg)
246{
247#ifdef CONFIG_CGROUP_HUGETLB
248 nrg->reservation_counter = rg->reservation_counter;
249 nrg->css = rg->css;
250 if (rg->css)
251 css_get(rg->css);
252#endif
253}
254
255/* Helper that records hugetlb_cgroup uncharge info. */
256static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,
257 struct hstate *h,
258 struct resv_map *resv,
259 struct file_region *nrg)
260{
261#ifdef CONFIG_CGROUP_HUGETLB
262 if (h_cg) {
263 nrg->reservation_counter =
264 &h_cg->rsvd_hugepage[hstate_index(h)];
265 nrg->css = &h_cg->css;
266 if (!resv->pages_per_hpage)
267 resv->pages_per_hpage = pages_per_huge_page(h);
268 /* pages_per_hpage should be the same for all entries in
269 * a resv_map.
270 */
271 VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h));
272 } else {
273 nrg->reservation_counter = NULL;
274 nrg->css = NULL;
275 }
276#endif
277}
278
Mina Almasryd75c6af2019-11-30 17:56:59 -0800279/* Must be called with resv->lock held. Calling this with count_only == true
280 * will count the number of pages to be added but will not modify the linked
Mina Almasry0db9d742020-04-01 21:11:25 -0700281 * list. If regions_needed != NULL and count_only == true, then regions_needed
282 * will indicate the number of file_regions needed in the cache to carry out to
283 * add the regions for this range.
Mina Almasryd75c6af2019-11-30 17:56:59 -0800284 */
285static long add_reservation_in_range(struct resv_map *resv, long f, long t,
Mina Almasry075a61d2020-04-01 21:11:28 -0700286 struct hugetlb_cgroup *h_cg,
287 struct hstate *h, long *regions_needed,
288 bool count_only)
Mina Almasryd75c6af2019-11-30 17:56:59 -0800289{
Mina Almasry0db9d742020-04-01 21:11:25 -0700290 long add = 0;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800291 struct list_head *head = &resv->regions;
Mina Almasry0db9d742020-04-01 21:11:25 -0700292 long last_accounted_offset = f;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800293 struct file_region *rg = NULL, *trg = NULL, *nrg = NULL;
294
Mina Almasry0db9d742020-04-01 21:11:25 -0700295 if (regions_needed)
296 *regions_needed = 0;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800297
Mina Almasry0db9d742020-04-01 21:11:25 -0700298 /* In this loop, we essentially handle an entry for the range
299 * [last_accounted_offset, rg->from), at every iteration, with some
300 * bounds checking.
301 */
302 list_for_each_entry_safe(rg, trg, head, link) {
303 /* Skip irrelevant regions that start before our range. */
304 if (rg->from < f) {
305 /* If this region ends after the last accounted offset,
306 * then we need to update last_accounted_offset.
307 */
308 if (rg->to > last_accounted_offset)
309 last_accounted_offset = rg->to;
310 continue;
311 }
Mina Almasryd75c6af2019-11-30 17:56:59 -0800312
Mina Almasry0db9d742020-04-01 21:11:25 -0700313 /* When we find a region that starts beyond our range, we've
314 * finished.
315 */
Mina Almasryd75c6af2019-11-30 17:56:59 -0800316 if (rg->from > t)
317 break;
318
Mina Almasry0db9d742020-04-01 21:11:25 -0700319 /* Add an entry for last_accounted_offset -> rg->from, and
320 * update last_accounted_offset.
Mina Almasryd75c6af2019-11-30 17:56:59 -0800321 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700322 if (rg->from > last_accounted_offset) {
323 add += rg->from - last_accounted_offset;
324 if (!count_only) {
325 nrg = get_file_region_entry_from_cache(
326 resv, last_accounted_offset, rg->from);
Mina Almasry075a61d2020-04-01 21:11:28 -0700327 record_hugetlb_cgroup_uncharge_info(h_cg, h,
328 resv, nrg);
Mina Almasry0db9d742020-04-01 21:11:25 -0700329 list_add(&nrg->link, rg->link.prev);
330 } else if (regions_needed)
331 *regions_needed += 1;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800332 }
Mina Almasryd75c6af2019-11-30 17:56:59 -0800333
Mina Almasry0db9d742020-04-01 21:11:25 -0700334 last_accounted_offset = rg->to;
335 }
336
337 /* Handle the case where our range extends beyond
338 * last_accounted_offset.
339 */
340 if (last_accounted_offset < t) {
341 add += t - last_accounted_offset;
342 if (!count_only) {
343 nrg = get_file_region_entry_from_cache(
344 resv, last_accounted_offset, t);
Mina Almasry075a61d2020-04-01 21:11:28 -0700345 record_hugetlb_cgroup_uncharge_info(h_cg, h, resv, nrg);
Mina Almasry0db9d742020-04-01 21:11:25 -0700346 list_add(&nrg->link, rg->link.prev);
347 } else if (regions_needed)
348 *regions_needed += 1;
349 }
350
351 VM_BUG_ON(add < 0);
352 return add;
353}
354
355/* Must be called with resv->lock acquired. Will drop lock to allocate entries.
356 */
357static int allocate_file_region_entries(struct resv_map *resv,
358 int regions_needed)
359 __must_hold(&resv->lock)
360{
361 struct list_head allocated_regions;
362 int to_allocate = 0, i = 0;
363 struct file_region *trg = NULL, *rg = NULL;
364
365 VM_BUG_ON(regions_needed < 0);
366
367 INIT_LIST_HEAD(&allocated_regions);
368
369 /*
370 * Check for sufficient descriptors in the cache to accommodate
371 * the number of in progress add operations plus regions_needed.
372 *
373 * This is a while loop because when we drop the lock, some other call
374 * to region_add or region_del may have consumed some region_entries,
375 * so we keep looping here until we finally have enough entries for
376 * (adds_in_progress + regions_needed).
377 */
378 while (resv->region_cache_count <
379 (resv->adds_in_progress + regions_needed)) {
380 to_allocate = resv->adds_in_progress + regions_needed -
381 resv->region_cache_count;
382
383 /* At this point, we should have enough entries in the cache
384 * for all the existings adds_in_progress. We should only be
385 * needing to allocate for regions_needed.
386 */
387 VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress);
388
389 spin_unlock(&resv->lock);
390 for (i = 0; i < to_allocate; i++) {
391 trg = kmalloc(sizeof(*trg), GFP_KERNEL);
392 if (!trg)
393 goto out_of_memory;
394 list_add(&trg->link, &allocated_regions);
395 }
396
397 spin_lock(&resv->lock);
398
399 list_for_each_entry_safe(rg, trg, &allocated_regions, link) {
Mina Almasryd75c6af2019-11-30 17:56:59 -0800400 list_del(&rg->link);
Mina Almasry0db9d742020-04-01 21:11:25 -0700401 list_add(&rg->link, &resv->region_cache);
402 resv->region_cache_count++;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800403 }
404 }
405
Mina Almasry0db9d742020-04-01 21:11:25 -0700406 return 0;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800407
Mina Almasry0db9d742020-04-01 21:11:25 -0700408out_of_memory:
409 list_for_each_entry_safe(rg, trg, &allocated_regions, link) {
410 list_del(&rg->link);
411 kfree(rg);
412 }
413 return -ENOMEM;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800414}
415
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700416/*
417 * Add the huge page range represented by [f, t) to the reserve
Mina Almasry0db9d742020-04-01 21:11:25 -0700418 * map. Regions will be taken from the cache to fill in this range.
419 * Sufficient regions should exist in the cache due to the previous
420 * call to region_chg with the same range, but in some cases the cache will not
421 * have sufficient entries due to races with other code doing region_add or
422 * region_del. The extra needed entries will be allocated.
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700423 *
Mina Almasry0db9d742020-04-01 21:11:25 -0700424 * regions_needed is the out value provided by a previous call to region_chg.
425 *
426 * Return the number of new huge pages added to the map. This number is greater
427 * than or equal to zero. If file_region entries needed to be allocated for
428 * this operation and we were not able to allocate, it ruturns -ENOMEM.
429 * region_add of regions of length 1 never allocate file_regions and cannot
430 * fail; region_chg will always allocate at least 1 entry and a region_add for
431 * 1 page will only require at most 1 entry.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700432 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700433static long region_add(struct resv_map *resv, long f, long t,
Mina Almasry075a61d2020-04-01 21:11:28 -0700434 long in_regions_needed, struct hstate *h,
435 struct hugetlb_cgroup *h_cg)
Andy Whitcroft96822902008-07-23 21:27:29 -0700436{
Mina Almasry0db9d742020-04-01 21:11:25 -0700437 long add = 0, actual_regions_needed = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700438
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700439 spin_lock(&resv->lock);
Mina Almasry0db9d742020-04-01 21:11:25 -0700440retry:
441
442 /* Count how many regions are actually needed to execute this add. */
Mina Almasry075a61d2020-04-01 21:11:28 -0700443 add_reservation_in_range(resv, f, t, NULL, NULL, &actual_regions_needed,
444 true);
Andy Whitcroft96822902008-07-23 21:27:29 -0700445
Mike Kravetz5e911372015-09-08 15:01:28 -0700446 /*
Mina Almasry0db9d742020-04-01 21:11:25 -0700447 * Check for sufficient descriptors in the cache to accommodate
448 * this add operation. Note that actual_regions_needed may be greater
449 * than in_regions_needed, as the resv_map may have been modified since
450 * the region_chg call. In this case, we need to make sure that we
451 * allocate extra entries, such that we have enough for all the
452 * existing adds_in_progress, plus the excess needed for this
453 * operation.
Mike Kravetz5e911372015-09-08 15:01:28 -0700454 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700455 if (actual_regions_needed > in_regions_needed &&
456 resv->region_cache_count <
457 resv->adds_in_progress +
458 (actual_regions_needed - in_regions_needed)) {
459 /* region_add operation of range 1 should never need to
460 * allocate file_region entries.
461 */
462 VM_BUG_ON(t - f <= 1);
Mike Kravetz5e911372015-09-08 15:01:28 -0700463
Mina Almasry0db9d742020-04-01 21:11:25 -0700464 if (allocate_file_region_entries(
465 resv, actual_regions_needed - in_regions_needed)) {
466 return -ENOMEM;
467 }
Mike Kravetz5e911372015-09-08 15:01:28 -0700468
Mina Almasry0db9d742020-04-01 21:11:25 -0700469 goto retry;
Mike Kravetz5e911372015-09-08 15:01:28 -0700470 }
471
Mina Almasry075a61d2020-04-01 21:11:28 -0700472 add = add_reservation_in_range(resv, f, t, h_cg, h, NULL, false);
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700473
Mina Almasry0db9d742020-04-01 21:11:25 -0700474 resv->adds_in_progress -= in_regions_needed;
475
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700476 spin_unlock(&resv->lock);
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700477 VM_BUG_ON(add < 0);
478 return add;
Andy Whitcroft96822902008-07-23 21:27:29 -0700479}
480
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700481/*
482 * Examine the existing reserve map and determine how many
483 * huge pages in the specified range [f, t) are NOT currently
484 * represented. This routine is called before a subsequent
485 * call to region_add that will actually modify the reserve
486 * map to add the specified range [f, t). region_chg does
487 * not change the number of huge pages represented by the
Mina Almasry0db9d742020-04-01 21:11:25 -0700488 * map. A number of new file_region structures is added to the cache as a
489 * placeholder, for the subsequent region_add call to use. At least 1
490 * file_region structure is added.
491 *
492 * out_regions_needed is the number of regions added to the
493 * resv->adds_in_progress. This value needs to be provided to a follow up call
494 * to region_add or region_abort for proper accounting.
Mike Kravetz5e911372015-09-08 15:01:28 -0700495 *
496 * Returns the number of huge pages that need to be added to the existing
497 * reservation map for the range [f, t). This number is greater or equal to
498 * zero. -ENOMEM is returned if a new file_region structure or cache entry
499 * is needed and can not be allocated.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700500 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700501static long region_chg(struct resv_map *resv, long f, long t,
502 long *out_regions_needed)
Andy Whitcroft96822902008-07-23 21:27:29 -0700503{
Andy Whitcroft96822902008-07-23 21:27:29 -0700504 long chg = 0;
505
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700506 spin_lock(&resv->lock);
Mike Kravetz5e911372015-09-08 15:01:28 -0700507
Mina Almasry0db9d742020-04-01 21:11:25 -0700508 /* Count how many hugepages in this range are NOT respresented. */
Mina Almasry075a61d2020-04-01 21:11:28 -0700509 chg = add_reservation_in_range(resv, f, t, NULL, NULL,
510 out_regions_needed, true);
Mike Kravetz5e911372015-09-08 15:01:28 -0700511
Mina Almasry0db9d742020-04-01 21:11:25 -0700512 if (*out_regions_needed == 0)
513 *out_regions_needed = 1;
Mike Kravetz5e911372015-09-08 15:01:28 -0700514
Mina Almasry0db9d742020-04-01 21:11:25 -0700515 if (allocate_file_region_entries(resv, *out_regions_needed))
516 return -ENOMEM;
Mike Kravetz5e911372015-09-08 15:01:28 -0700517
Mina Almasry0db9d742020-04-01 21:11:25 -0700518 resv->adds_in_progress += *out_regions_needed;
Andy Whitcroft96822902008-07-23 21:27:29 -0700519
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700520 spin_unlock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700521 return chg;
522}
523
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700524/*
Mike Kravetz5e911372015-09-08 15:01:28 -0700525 * Abort the in progress add operation. The adds_in_progress field
526 * of the resv_map keeps track of the operations in progress between
527 * calls to region_chg and region_add. Operations are sometimes
528 * aborted after the call to region_chg. In such cases, region_abort
Mina Almasry0db9d742020-04-01 21:11:25 -0700529 * is called to decrement the adds_in_progress counter. regions_needed
530 * is the value returned by the region_chg call, it is used to decrement
531 * the adds_in_progress counter.
Mike Kravetz5e911372015-09-08 15:01:28 -0700532 *
533 * NOTE: The range arguments [f, t) are not needed or used in this
534 * routine. They are kept to make reading the calling code easier as
535 * arguments will match the associated region_chg call.
536 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700537static void region_abort(struct resv_map *resv, long f, long t,
538 long regions_needed)
Mike Kravetz5e911372015-09-08 15:01:28 -0700539{
540 spin_lock(&resv->lock);
541 VM_BUG_ON(!resv->region_cache_count);
Mina Almasry0db9d742020-04-01 21:11:25 -0700542 resv->adds_in_progress -= regions_needed;
Mike Kravetz5e911372015-09-08 15:01:28 -0700543 spin_unlock(&resv->lock);
544}
545
546/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700547 * Delete the specified range [f, t) from the reserve map. If the
548 * t parameter is LONG_MAX, this indicates that ALL regions after f
549 * should be deleted. Locate the regions which intersect [f, t)
550 * and either trim, delete or split the existing regions.
551 *
552 * Returns the number of huge pages deleted from the reserve map.
553 * In the normal case, the return value is zero or more. In the
554 * case where a region must be split, a new region descriptor must
555 * be allocated. If the allocation fails, -ENOMEM will be returned.
556 * NOTE: If the parameter t == LONG_MAX, then we will never split
557 * a region and possibly return -ENOMEM. Callers specifying
558 * t == LONG_MAX do not need to check for -ENOMEM error.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700559 */
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700560static long region_del(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700561{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700562 struct list_head *head = &resv->regions;
Andy Whitcroft96822902008-07-23 21:27:29 -0700563 struct file_region *rg, *trg;
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700564 struct file_region *nrg = NULL;
565 long del = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700566
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700567retry:
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700568 spin_lock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700569 list_for_each_entry_safe(rg, trg, head, link) {
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800570 /*
571 * Skip regions before the range to be deleted. file_region
572 * ranges are normally of the form [from, to). However, there
573 * may be a "placeholder" entry in the map which is of the form
574 * (from, to) with from == to. Check for placeholder entries
575 * at the beginning of the range to be deleted.
576 */
577 if (rg->to <= f && (rg->to != rg->from || rg->to != f))
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700578 continue;
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800579
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700580 if (rg->from >= t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700581 break;
Andy Whitcroft96822902008-07-23 21:27:29 -0700582
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700583 if (f > rg->from && t < rg->to) { /* Must split region */
584 /*
585 * Check for an entry in the cache before dropping
586 * lock and attempting allocation.
587 */
588 if (!nrg &&
589 resv->region_cache_count > resv->adds_in_progress) {
590 nrg = list_first_entry(&resv->region_cache,
591 struct file_region,
592 link);
593 list_del(&nrg->link);
594 resv->region_cache_count--;
595 }
596
597 if (!nrg) {
598 spin_unlock(&resv->lock);
599 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
600 if (!nrg)
601 return -ENOMEM;
602 goto retry;
603 }
604
605 del += t - f;
606
607 /* New entry for end of split region */
608 nrg->from = t;
609 nrg->to = rg->to;
Mina Almasry075a61d2020-04-01 21:11:28 -0700610
611 copy_hugetlb_cgroup_uncharge_info(nrg, rg);
612
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700613 INIT_LIST_HEAD(&nrg->link);
614
615 /* Original entry is trimmed */
616 rg->to = f;
617
Mina Almasry075a61d2020-04-01 21:11:28 -0700618 hugetlb_cgroup_uncharge_file_region(
619 resv, rg, nrg->to - nrg->from);
620
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700621 list_add(&nrg->link, &rg->link);
622 nrg = NULL;
623 break;
624 }
625
626 if (f <= rg->from && t >= rg->to) { /* Remove entire region */
627 del += rg->to - rg->from;
Mina Almasry075a61d2020-04-01 21:11:28 -0700628 hugetlb_cgroup_uncharge_file_region(resv, rg,
629 rg->to - rg->from);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700630 list_del(&rg->link);
631 kfree(rg);
632 continue;
633 }
634
635 if (f <= rg->from) { /* Trim beginning of region */
636 del += t - rg->from;
637 rg->from = t;
Mina Almasry075a61d2020-04-01 21:11:28 -0700638
639 hugetlb_cgroup_uncharge_file_region(resv, rg,
640 t - rg->from);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700641 } else { /* Trim end of region */
642 del += rg->to - f;
643 rg->to = f;
Mina Almasry075a61d2020-04-01 21:11:28 -0700644
645 hugetlb_cgroup_uncharge_file_region(resv, rg,
646 rg->to - f);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700647 }
Andy Whitcroft96822902008-07-23 21:27:29 -0700648 }
649
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700650 spin_unlock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700651 kfree(nrg);
652 return del;
Andy Whitcroft96822902008-07-23 21:27:29 -0700653}
654
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700655/*
Mike Kravetzb5cec282015-09-08 15:01:41 -0700656 * A rare out of memory error was encountered which prevented removal of
657 * the reserve map region for a page. The huge page itself was free'ed
658 * and removed from the page cache. This routine will adjust the subpool
659 * usage count, and the global reserve count if needed. By incrementing
660 * these counts, the reserve map entry which could not be deleted will
661 * appear as a "reserved" entry instead of simply dangling with incorrect
662 * counts.
663 */
zhong jiang72e29362016-10-07 17:02:01 -0700664void hugetlb_fix_reserve_counts(struct inode *inode)
Mike Kravetzb5cec282015-09-08 15:01:41 -0700665{
666 struct hugepage_subpool *spool = subpool_inode(inode);
667 long rsv_adjust;
668
669 rsv_adjust = hugepage_subpool_get_pages(spool, 1);
zhong jiang72e29362016-10-07 17:02:01 -0700670 if (rsv_adjust) {
Mike Kravetzb5cec282015-09-08 15:01:41 -0700671 struct hstate *h = hstate_inode(inode);
672
673 hugetlb_acct_memory(h, 1);
674 }
675}
676
677/*
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700678 * Count and return the number of huge pages in the reserve map
679 * that intersect with the range [f, t).
680 */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700681static long region_count(struct resv_map *resv, long f, long t)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700682{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700683 struct list_head *head = &resv->regions;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700684 struct file_region *rg;
685 long chg = 0;
686
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700687 spin_lock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700688 /* Locate each segment we overlap with, and count that overlap. */
689 list_for_each_entry(rg, head, link) {
Wang Sheng-Huif2135a42012-05-29 15:06:17 -0700690 long seg_from;
691 long seg_to;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700692
693 if (rg->to <= f)
694 continue;
695 if (rg->from >= t)
696 break;
697
698 seg_from = max(rg->from, f);
699 seg_to = min(rg->to, t);
700
701 chg += seg_to - seg_from;
702 }
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700703 spin_unlock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700704
705 return chg;
706}
707
Andy Whitcroft96822902008-07-23 21:27:29 -0700708/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700709 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700710 * the mapping, in pagecache page units; huge pages here.
711 */
Andi Kleena5516432008-07-23 21:27:41 -0700712static pgoff_t vma_hugecache_offset(struct hstate *h,
713 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700714{
Andi Kleena5516432008-07-23 21:27:41 -0700715 return ((address - vma->vm_start) >> huge_page_shift(h)) +
716 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700717}
718
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900719pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
720 unsigned long address)
721{
722 return vma_hugecache_offset(hstate_vma(vma), vma, address);
723}
Dan Williamsdee41072016-05-14 12:20:44 -0700724EXPORT_SYMBOL_GPL(linear_hugepage_index);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900725
Andy Whitcroft84afd992008-07-23 21:27:32 -0700726/*
Mel Gorman08fba692009-01-06 14:38:53 -0800727 * Return the size of the pages allocated when backing a VMA. In the majority
728 * cases this will be same size as used by the page table entries.
729 */
730unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
731{
Dan Williams05ea8862018-04-05 16:24:25 -0700732 if (vma->vm_ops && vma->vm_ops->pagesize)
733 return vma->vm_ops->pagesize(vma);
734 return PAGE_SIZE;
Mel Gorman08fba692009-01-06 14:38:53 -0800735}
Joerg Roedelf340ca02009-06-19 15:16:22 +0200736EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
Mel Gorman08fba692009-01-06 14:38:53 -0800737
738/*
Mel Gorman33402892009-01-06 14:38:54 -0800739 * Return the page size being used by the MMU to back a VMA. In the majority
740 * of cases, the page size used by the kernel matches the MMU size. On
Dan Williams09135cc2018-04-05 16:24:21 -0700741 * architectures where it differs, an architecture-specific 'strong'
742 * version of this symbol is required.
Mel Gorman33402892009-01-06 14:38:54 -0800743 */
Dan Williams09135cc2018-04-05 16:24:21 -0700744__weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
Mel Gorman33402892009-01-06 14:38:54 -0800745{
746 return vma_kernel_pagesize(vma);
747}
Mel Gorman33402892009-01-06 14:38:54 -0800748
749/*
Andy Whitcroft84afd992008-07-23 21:27:32 -0700750 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
751 * bits of the reservation map pointer, which are always clear due to
752 * alignment.
753 */
754#define HPAGE_RESV_OWNER (1UL << 0)
755#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700756#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700757
Mel Gormana1e78772008-07-23 21:27:23 -0700758/*
759 * These helpers are used to track how many pages are reserved for
760 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
761 * is guaranteed to have their future faults succeed.
762 *
763 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
764 * the reserve counters are updated with the hugetlb_lock held. It is safe
765 * to reset the VMA at fork() time as it is not in use yet and there is no
766 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700767 *
768 * The private mapping reservation is represented in a subtly different
769 * manner to a shared mapping. A shared mapping has a region map associated
770 * with the underlying file, this region map represents the backing file
771 * pages which have ever had a reservation assigned which this persists even
772 * after the page is instantiated. A private mapping has a region map
773 * associated with the original mmap which is attached to all VMAs which
774 * reference it, this region map represents those offsets which have consumed
775 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -0700776 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700777static unsigned long get_vma_private_data(struct vm_area_struct *vma)
778{
779 return (unsigned long)vma->vm_private_data;
780}
781
782static void set_vma_private_data(struct vm_area_struct *vma,
783 unsigned long value)
784{
785 vma->vm_private_data = (void *)value;
786}
787
Mina Almasrye9fe92a2020-04-01 21:11:21 -0700788static void
789resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map,
790 struct hugetlb_cgroup *h_cg,
791 struct hstate *h)
792{
793#ifdef CONFIG_CGROUP_HUGETLB
794 if (!h_cg || !h) {
795 resv_map->reservation_counter = NULL;
796 resv_map->pages_per_hpage = 0;
797 resv_map->css = NULL;
798 } else {
799 resv_map->reservation_counter =
800 &h_cg->rsvd_hugepage[hstate_index(h)];
801 resv_map->pages_per_hpage = pages_per_huge_page(h);
802 resv_map->css = &h_cg->css;
803 }
804#endif
805}
806
Joonsoo Kim9119a412014-04-03 14:47:25 -0700807struct resv_map *resv_map_alloc(void)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700808{
809 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
Mike Kravetz5e911372015-09-08 15:01:28 -0700810 struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL);
811
812 if (!resv_map || !rg) {
813 kfree(resv_map);
814 kfree(rg);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700815 return NULL;
Mike Kravetz5e911372015-09-08 15:01:28 -0700816 }
Andy Whitcroft84afd992008-07-23 21:27:32 -0700817
818 kref_init(&resv_map->refs);
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700819 spin_lock_init(&resv_map->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700820 INIT_LIST_HEAD(&resv_map->regions);
821
Mike Kravetz5e911372015-09-08 15:01:28 -0700822 resv_map->adds_in_progress = 0;
Mina Almasrye9fe92a2020-04-01 21:11:21 -0700823 /*
824 * Initialize these to 0. On shared mappings, 0's here indicate these
825 * fields don't do cgroup accounting. On private mappings, these will be
826 * re-initialized to the proper values, to indicate that hugetlb cgroup
827 * reservations are to be un-charged from here.
828 */
829 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL);
Mike Kravetz5e911372015-09-08 15:01:28 -0700830
831 INIT_LIST_HEAD(&resv_map->region_cache);
832 list_add(&rg->link, &resv_map->region_cache);
833 resv_map->region_cache_count = 1;
834
Andy Whitcroft84afd992008-07-23 21:27:32 -0700835 return resv_map;
836}
837
Joonsoo Kim9119a412014-04-03 14:47:25 -0700838void resv_map_release(struct kref *ref)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700839{
840 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
Mike Kravetz5e911372015-09-08 15:01:28 -0700841 struct list_head *head = &resv_map->region_cache;
842 struct file_region *rg, *trg;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700843
844 /* Clear out any active regions before we release the map. */
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700845 region_del(resv_map, 0, LONG_MAX);
Mike Kravetz5e911372015-09-08 15:01:28 -0700846
847 /* ... and any entries left in the cache */
848 list_for_each_entry_safe(rg, trg, head, link) {
849 list_del(&rg->link);
850 kfree(rg);
851 }
852
853 VM_BUG_ON(resv_map->adds_in_progress);
854
Andy Whitcroft84afd992008-07-23 21:27:32 -0700855 kfree(resv_map);
856}
857
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700858static inline struct resv_map *inode_resv_map(struct inode *inode)
859{
Mike Kravetzf27a5132019-05-13 17:22:55 -0700860 /*
861 * At inode evict time, i_mapping may not point to the original
862 * address space within the inode. This original address space
863 * contains the pointer to the resv_map. So, always use the
864 * address space embedded within the inode.
865 * The VERY common case is inode->mapping == &inode->i_data but,
866 * this may not be true for device special inodes.
867 */
868 return (struct resv_map *)(&inode->i_data)->private_data;
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700869}
870
Andy Whitcroft84afd992008-07-23 21:27:32 -0700871static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700872{
Sasha Levin81d1b092014-10-09 15:28:10 -0700873 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700874 if (vma->vm_flags & VM_MAYSHARE) {
875 struct address_space *mapping = vma->vm_file->f_mapping;
876 struct inode *inode = mapping->host;
877
878 return inode_resv_map(inode);
879
880 } else {
Andy Whitcroft84afd992008-07-23 21:27:32 -0700881 return (struct resv_map *)(get_vma_private_data(vma) &
882 ~HPAGE_RESV_MASK);
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700883 }
Mel Gormana1e78772008-07-23 21:27:23 -0700884}
885
Andy Whitcroft84afd992008-07-23 21:27:32 -0700886static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -0700887{
Sasha Levin81d1b092014-10-09 15:28:10 -0700888 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
889 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700890
Andy Whitcroft84afd992008-07-23 21:27:32 -0700891 set_vma_private_data(vma, (get_vma_private_data(vma) &
892 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700893}
894
895static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
896{
Sasha Levin81d1b092014-10-09 15:28:10 -0700897 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
898 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700899
900 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700901}
902
903static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
904{
Sasha Levin81d1b092014-10-09 15:28:10 -0700905 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700906
907 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700908}
909
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700910/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700911void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
912{
Sasha Levin81d1b092014-10-09 15:28:10 -0700913 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Mel Gormanf83a2752009-05-28 14:34:40 -0700914 if (!(vma->vm_flags & VM_MAYSHARE))
Mel Gormana1e78772008-07-23 21:27:23 -0700915 vma->vm_private_data = (void *)0;
916}
917
918/* Returns true if the VMA has associated reserve pages */
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700919static bool vma_has_reserves(struct vm_area_struct *vma, long chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700920{
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700921 if (vma->vm_flags & VM_NORESERVE) {
922 /*
923 * This address is already reserved by other process(chg == 0),
924 * so, we should decrement reserved count. Without decrementing,
925 * reserve count remains after releasing inode, because this
926 * allocated page will go into page cache and is regarded as
927 * coming from reserved pool in releasing step. Currently, we
928 * don't have any other solution to deal with this situation
929 * properly, so add work-around here.
930 */
931 if (vma->vm_flags & VM_MAYSHARE && chg == 0)
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700932 return true;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700933 else
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700934 return false;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700935 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700936
937 /* Shared mappings always use reserves */
Mike Kravetz1fb1b0e2015-09-08 15:01:44 -0700938 if (vma->vm_flags & VM_MAYSHARE) {
939 /*
940 * We know VM_NORESERVE is not set. Therefore, there SHOULD
941 * be a region map for all pages. The only situation where
942 * there is no region map is if a hole was punched via
943 * fallocate. In this case, there really are no reverves to
944 * use. This situation is indicated if chg != 0.
945 */
946 if (chg)
947 return false;
948 else
949 return true;
950 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700951
952 /*
953 * Only the process that called mmap() has reserves for
954 * private mappings.
955 */
Mike Kravetz67961f92016-06-08 15:33:42 -0700956 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
957 /*
958 * Like the shared case above, a hole punch or truncate
959 * could have been performed on the private mapping.
960 * Examine the value of chg to determine if reserves
961 * actually exist or were previously consumed.
962 * Very Subtle - The value of chg comes from a previous
963 * call to vma_needs_reserves(). The reserve map for
964 * private mappings has different (opposite) semantics
965 * than that of shared mappings. vma_needs_reserves()
966 * has already taken this difference in semantics into
967 * account. Therefore, the meaning of chg is the same
968 * as in the shared case above. Code could easily be
969 * combined, but keeping it separate draws attention to
970 * subtle differences.
971 */
972 if (chg)
973 return false;
974 else
975 return true;
976 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700977
Nicholas Krause559ec2f2015-09-04 15:48:27 -0700978 return false;
Mel Gormana1e78772008-07-23 21:27:23 -0700979}
980
Andi Kleena5516432008-07-23 21:27:41 -0700981static void enqueue_huge_page(struct hstate *h, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 int nid = page_to_nid(page);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -0700984 list_move(&page->lru, &h->hugepage_freelists[nid]);
Andi Kleena5516432008-07-23 21:27:41 -0700985 h->free_huge_pages++;
986 h->free_huge_pages_node[nid]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Anshuman Khandual94310cb2017-07-06 15:38:38 -0700989static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900990{
991 struct page *page;
992
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700993 list_for_each_entry(page, &h->hugepage_freelists[nid], lru)
Naoya Horiguchi243abd52017-07-10 15:47:32 -0700994 if (!PageHWPoison(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700995 break;
996 /*
997 * if 'non-isolated free hugepage' not found on the list,
998 * the allocation fails.
999 */
1000 if (&h->hugepage_freelists[nid] == &page->lru)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001001 return NULL;
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07001002 list_move(&page->lru, &h->hugepage_activelist);
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001003 set_page_refcounted(page);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001004 h->free_huge_pages--;
1005 h->free_huge_pages_node[nid]--;
1006 return page;
1007}
1008
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001009static struct page *dequeue_huge_page_nodemask(struct hstate *h, gfp_t gfp_mask, int nid,
1010 nodemask_t *nmask)
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001011{
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001012 unsigned int cpuset_mems_cookie;
1013 struct zonelist *zonelist;
1014 struct zone *zone;
1015 struct zoneref *z;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08001016 int node = NUMA_NO_NODE;
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001017
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001018 zonelist = node_zonelist(nid, gfp_mask);
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001019
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001020retry_cpuset:
1021 cpuset_mems_cookie = read_mems_allowed_begin();
1022 for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) {
1023 struct page *page;
1024
1025 if (!cpuset_zone_allowed(zone, gfp_mask))
1026 continue;
1027 /*
1028 * no need to ask again on the same node. Pool is node rather than
1029 * zone aware
1030 */
1031 if (zone_to_nid(zone) == node)
1032 continue;
1033 node = zone_to_nid(zone);
1034
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001035 page = dequeue_huge_page_node_exact(h, node);
1036 if (page)
1037 return page;
1038 }
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001039 if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie)))
1040 goto retry_cpuset;
1041
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001042 return NULL;
1043}
1044
Naoya Horiguchi86cdb462013-09-11 14:22:13 -07001045/* Movability of hugepages depends on migration support. */
1046static inline gfp_t htlb_alloc_mask(struct hstate *h)
1047{
Anshuman Khandual7ed2c312019-03-05 15:43:44 -08001048 if (hugepage_movable_supported(h))
Naoya Horiguchi86cdb462013-09-11 14:22:13 -07001049 return GFP_HIGHUSER_MOVABLE;
1050 else
1051 return GFP_HIGHUSER;
1052}
1053
Andi Kleena5516432008-07-23 21:27:41 -07001054static struct page *dequeue_huge_page_vma(struct hstate *h,
1055 struct vm_area_struct *vma,
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001056 unsigned long address, int avoid_reserve,
1057 long chg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001059 struct page *page;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -07001060 struct mempolicy *mpol;
Vlastimil Babka04ec6262017-07-06 15:40:03 -07001061 gfp_t gfp_mask;
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001062 nodemask_t *nodemask;
Vlastimil Babka04ec6262017-07-06 15:40:03 -07001063 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Mel Gormana1e78772008-07-23 21:27:23 -07001065 /*
1066 * A child process with MAP_PRIVATE mappings created by their parent
1067 * have no page reserves. This check ensures that reservations are
1068 * not "stolen". The child may still get SIGKILLed
1069 */
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001070 if (!vma_has_reserves(vma, chg) &&
Andi Kleena5516432008-07-23 21:27:41 -07001071 h->free_huge_pages - h->resv_huge_pages == 0)
Miao Xiec0ff7452010-05-24 14:32:08 -07001072 goto err;
Mel Gormana1e78772008-07-23 21:27:23 -07001073
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001074 /* If reserves cannot be used, ensure enough pages are in the pool */
Andi Kleena5516432008-07-23 21:27:41 -07001075 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001076 goto err;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001077
Vlastimil Babka04ec6262017-07-06 15:40:03 -07001078 gfp_mask = htlb_alloc_mask(h);
1079 nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001080 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask);
1081 if (page && !avoid_reserve && vma_has_reserves(vma, chg)) {
1082 SetPagePrivate(page);
1083 h->resv_huge_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001085
1086 mpol_cond_put(mpol);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001087 return page;
1088
Miao Xiec0ff7452010-05-24 14:32:08 -07001089err:
Mel Gormancc9a6c82012-03-21 16:34:11 -07001090 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Luiz Capitulino1cac6f22014-06-04 16:07:11 -07001093/*
1094 * common helper functions for hstate_next_node_to_{alloc|free}.
1095 * We may have allocated or freed a huge page based on a different
1096 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
1097 * be outside of *nodes_allowed. Ensure that we use an allowed
1098 * node for alloc or free.
1099 */
1100static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
1101{
Andrew Morton0edaf862016-05-19 17:10:58 -07001102 nid = next_node_in(nid, *nodes_allowed);
Luiz Capitulino1cac6f22014-06-04 16:07:11 -07001103 VM_BUG_ON(nid >= MAX_NUMNODES);
1104
1105 return nid;
1106}
1107
1108static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
1109{
1110 if (!node_isset(nid, *nodes_allowed))
1111 nid = next_node_allowed(nid, nodes_allowed);
1112 return nid;
1113}
1114
1115/*
1116 * returns the previously saved node ["this node"] from which to
1117 * allocate a persistent huge page for the pool and advance the
1118 * next node from which to allocate, handling wrap at end of node
1119 * mask.
1120 */
1121static int hstate_next_node_to_alloc(struct hstate *h,
1122 nodemask_t *nodes_allowed)
1123{
1124 int nid;
1125
1126 VM_BUG_ON(!nodes_allowed);
1127
1128 nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
1129 h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
1130
1131 return nid;
1132}
1133
1134/*
1135 * helper for free_pool_huge_page() - return the previously saved
1136 * node ["this node"] from which to free a huge page. Advance the
1137 * next node id whether or not we find a free huge page to free so
1138 * that the next attempt to free addresses the next node.
1139 */
1140static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
1141{
1142 int nid;
1143
1144 VM_BUG_ON(!nodes_allowed);
1145
1146 nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
1147 h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
1148
1149 return nid;
1150}
1151
1152#define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
1153 for (nr_nodes = nodes_weight(*mask); \
1154 nr_nodes > 0 && \
1155 ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
1156 nr_nodes--)
1157
1158#define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
1159 for (nr_nodes = nodes_weight(*mask); \
1160 nr_nodes > 0 && \
1161 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1162 nr_nodes--)
1163
Aneesh Kumar K.Ve1073d12017-07-06 15:39:17 -07001164#ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001165static void destroy_compound_gigantic_page(struct page *page,
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001166 unsigned int order)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001167{
1168 int i;
1169 int nr_pages = 1 << order;
1170 struct page *p = page + 1;
1171
Gerald Schaeferc8cc7082016-06-24 14:50:04 -07001172 atomic_set(compound_mapcount_ptr(page), 0);
John Hubbard47e29d32020-04-01 21:05:33 -07001173 if (hpage_pincount_available(page))
1174 atomic_set(compound_pincount_ptr(page), 0);
1175
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001176 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001177 clear_compound_head(p);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001178 set_page_refcounted(p);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001179 }
1180
1181 set_compound_order(page, 0);
1182 __ClearPageHead(page);
1183}
1184
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001185static void free_gigantic_page(struct page *page, unsigned int order)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001186{
1187 free_contig_range(page_to_pfn(page), 1 << order);
1188}
1189
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001190#ifdef CONFIG_CONTIG_ALLOC
Michal Hockod9cc948f2018-01-31 16:20:44 -08001191static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
1192 int nid, nodemask_t *nodemask)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001193{
Anshuman Khandual5e27a2d2019-11-30 17:55:06 -08001194 unsigned long nr_pages = 1UL << huge_page_order(h);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001195
Anshuman Khandual5e27a2d2019-11-30 17:55:06 -08001196 return alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001197}
1198
1199static void prep_new_huge_page(struct hstate *h, struct page *page, int nid);
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001200static void prep_compound_gigantic_page(struct page *page, unsigned int order);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001201#else /* !CONFIG_CONTIG_ALLOC */
1202static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
1203 int nid, nodemask_t *nodemask)
1204{
1205 return NULL;
1206}
1207#endif /* CONFIG_CONTIG_ALLOC */
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001208
Aneesh Kumar K.Ve1073d12017-07-06 15:39:17 -07001209#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
Michal Hockod9cc948f2018-01-31 16:20:44 -08001210static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001211 int nid, nodemask_t *nodemask)
1212{
1213 return NULL;
1214}
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001215static inline void free_gigantic_page(struct page *page, unsigned int order) { }
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001216static inline void destroy_compound_gigantic_page(struct page *page,
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001217 unsigned int order) { }
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001218#endif
1219
Andi Kleena5516432008-07-23 21:27:41 -07001220static void update_and_free_page(struct hstate *h, struct page *page)
Adam Litke6af2acb2007-10-16 01:26:16 -07001221{
1222 int i;
Andi Kleena5516432008-07-23 21:27:41 -07001223
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001224 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001225 return;
Andy Whitcroft18229df2008-11-06 12:53:27 -08001226
Andi Kleena5516432008-07-23 21:27:41 -07001227 h->nr_huge_pages--;
1228 h->nr_huge_pages_node[page_to_nid(page)]--;
1229 for (i = 0; i < pages_per_huge_page(h); i++) {
Chris Forbes32f84522011-07-25 17:12:14 -07001230 page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
1231 1 << PG_referenced | 1 << PG_dirty |
Luiz Capitulinoa7407a22014-06-04 16:07:09 -07001232 1 << PG_active | 1 << PG_private |
1233 1 << PG_writeback);
Adam Litke6af2acb2007-10-16 01:26:16 -07001234 }
Sasha Levin309381fea2014-01-23 15:52:54 -08001235 VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page);
Mina Almasry1adc4d42020-04-01 21:11:15 -07001236 VM_BUG_ON_PAGE(hugetlb_cgroup_from_page_rsvd(page), page);
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -08001237 set_compound_page_dtor(page, NULL_COMPOUND_DTOR);
Adam Litke6af2acb2007-10-16 01:26:16 -07001238 set_page_refcounted(page);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001239 if (hstate_is_gigantic(h)) {
1240 destroy_compound_gigantic_page(page, huge_page_order(h));
1241 free_gigantic_page(page, huge_page_order(h));
1242 } else {
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001243 __free_pages(page, huge_page_order(h));
1244 }
Adam Litke6af2acb2007-10-16 01:26:16 -07001245}
1246
Andi Kleene5ff2152008-07-23 21:27:42 -07001247struct hstate *size_to_hstate(unsigned long size)
1248{
1249 struct hstate *h;
1250
1251 for_each_hstate(h) {
1252 if (huge_page_size(h) == size)
1253 return h;
1254 }
1255 return NULL;
1256}
1257
Naoya Horiguchibcc54222015-04-15 16:14:38 -07001258/*
1259 * Test to determine whether the hugepage is "active/in-use" (i.e. being linked
1260 * to hstate->hugepage_activelist.)
1261 *
1262 * This function can be called for tail pages, but never returns true for them.
1263 */
1264bool page_huge_active(struct page *page)
1265{
1266 VM_BUG_ON_PAGE(!PageHuge(page), page);
1267 return PageHead(page) && PagePrivate(&page[1]);
1268}
1269
1270/* never called for tail page */
1271static void set_page_huge_active(struct page *page)
1272{
1273 VM_BUG_ON_PAGE(!PageHeadHuge(page), page);
1274 SetPagePrivate(&page[1]);
1275}
1276
1277static void clear_page_huge_active(struct page *page)
1278{
1279 VM_BUG_ON_PAGE(!PageHeadHuge(page), page);
1280 ClearPagePrivate(&page[1]);
1281}
1282
Michal Hockoab5ac902018-01-31 16:20:48 -08001283/*
1284 * Internal hugetlb specific page flag. Do not use outside of the hugetlb
1285 * code
1286 */
1287static inline bool PageHugeTemporary(struct page *page)
1288{
1289 if (!PageHuge(page))
1290 return false;
1291
1292 return (unsigned long)page[2].mapping == -1U;
1293}
1294
1295static inline void SetPageHugeTemporary(struct page *page)
1296{
1297 page[2].mapping = (void *)-1U;
1298}
1299
1300static inline void ClearPageHugeTemporary(struct page *page)
1301{
1302 page[2].mapping = NULL;
1303}
1304
Waiman Longc77c0a82020-01-04 13:00:15 -08001305static void __free_huge_page(struct page *page)
David Gibson27a85ef2006-03-22 00:08:56 -08001306{
Andi Kleena5516432008-07-23 21:27:41 -07001307 /*
1308 * Can't pass hstate in here because it is called from the
1309 * compound page destructor.
1310 */
Andi Kleene5ff2152008-07-23 21:27:42 -07001311 struct hstate *h = page_hstate(page);
Adam Litke7893d1d2007-10-16 01:26:18 -07001312 int nid = page_to_nid(page);
David Gibson90481622012-03-21 16:34:12 -07001313 struct hugepage_subpool *spool =
1314 (struct hugepage_subpool *)page_private(page);
Joonsoo Kim07443a82013-09-11 14:21:58 -07001315 bool restore_reserve;
David Gibson27a85ef2006-03-22 00:08:56 -08001316
Mike Kravetzb4330af2016-02-05 15:36:38 -08001317 VM_BUG_ON_PAGE(page_count(page), page);
1318 VM_BUG_ON_PAGE(page_mapcount(page), page);
Yongkai Wu8ace22b2018-12-14 14:17:10 -08001319
1320 set_page_private(page, 0);
1321 page->mapping = NULL;
Joonsoo Kim07443a82013-09-11 14:21:58 -07001322 restore_reserve = PagePrivate(page);
Joonsoo Kim16c794b2013-10-16 13:46:48 -07001323 ClearPagePrivate(page);
David Gibson27a85ef2006-03-22 00:08:56 -08001324
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001325 /*
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001326 * If PagePrivate() was set on page, page allocation consumed a
1327 * reservation. If the page was associated with a subpool, there
1328 * would have been a page reserved in the subpool before allocation
1329 * via hugepage_subpool_get_pages(). Since we are 'restoring' the
1330 * reservtion, do not call hugepage_subpool_put_pages() as this will
1331 * remove the reserved page from the subpool.
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001332 */
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001333 if (!restore_reserve) {
1334 /*
1335 * A return code of zero implies that the subpool will be
1336 * under its minimum size if the reservation is not restored
1337 * after page is free. Therefore, force restore_reserve
1338 * operation.
1339 */
1340 if (hugepage_subpool_put_pages(spool, 1) == 0)
1341 restore_reserve = true;
1342 }
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001343
David Gibson27a85ef2006-03-22 00:08:56 -08001344 spin_lock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07001345 clear_page_huge_active(page);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07001346 hugetlb_cgroup_uncharge_page(hstate_index(h),
1347 pages_per_huge_page(h), page);
Joonsoo Kim07443a82013-09-11 14:21:58 -07001348 if (restore_reserve)
1349 h->resv_huge_pages++;
1350
Michal Hockoab5ac902018-01-31 16:20:48 -08001351 if (PageHugeTemporary(page)) {
1352 list_del(&page->lru);
1353 ClearPageHugeTemporary(page);
1354 update_and_free_page(h, page);
1355 } else if (h->surplus_huge_pages_node[nid]) {
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07001356 /* remove the page from active list */
1357 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -07001358 update_and_free_page(h, page);
1359 h->surplus_huge_pages--;
1360 h->surplus_huge_pages_node[nid]--;
Adam Litke7893d1d2007-10-16 01:26:18 -07001361 } else {
Will Deacon5d3a5512012-10-08 16:29:32 -07001362 arch_clear_hugepage_flags(page);
Andi Kleena5516432008-07-23 21:27:41 -07001363 enqueue_huge_page(h, page);
Adam Litke7893d1d2007-10-16 01:26:18 -07001364 }
David Gibson27a85ef2006-03-22 00:08:56 -08001365 spin_unlock(&hugetlb_lock);
1366}
1367
Waiman Longc77c0a82020-01-04 13:00:15 -08001368/*
1369 * As free_huge_page() can be called from a non-task context, we have
1370 * to defer the actual freeing in a workqueue to prevent potential
1371 * hugetlb_lock deadlock.
1372 *
1373 * free_hpage_workfn() locklessly retrieves the linked list of pages to
1374 * be freed and frees them one-by-one. As the page->mapping pointer is
1375 * going to be cleared in __free_huge_page() anyway, it is reused as the
1376 * llist_node structure of a lockless linked list of huge pages to be freed.
1377 */
1378static LLIST_HEAD(hpage_freelist);
1379
1380static void free_hpage_workfn(struct work_struct *work)
1381{
1382 struct llist_node *node;
1383 struct page *page;
1384
1385 node = llist_del_all(&hpage_freelist);
1386
1387 while (node) {
1388 page = container_of((struct address_space **)node,
1389 struct page, mapping);
1390 node = node->next;
1391 __free_huge_page(page);
1392 }
1393}
1394static DECLARE_WORK(free_hpage_work, free_hpage_workfn);
1395
1396void free_huge_page(struct page *page)
1397{
1398 /*
1399 * Defer freeing if in non-task context to avoid hugetlb_lock deadlock.
1400 */
1401 if (!in_task()) {
1402 /*
1403 * Only call schedule_work() if hpage_freelist is previously
1404 * empty. Otherwise, schedule_work() had been called but the
1405 * workfn hasn't retrieved the list yet.
1406 */
1407 if (llist_add((struct llist_node *)&page->mapping,
1408 &hpage_freelist))
1409 schedule_work(&free_hpage_work);
1410 return;
1411 }
1412
1413 __free_huge_page(page);
1414}
1415
Andi Kleena5516432008-07-23 21:27:41 -07001416static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001417{
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07001418 INIT_LIST_HEAD(&page->lru);
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -08001419 set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001420 spin_lock(&hugetlb_lock);
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -07001421 set_hugetlb_cgroup(page, NULL);
Mina Almasry1adc4d42020-04-01 21:11:15 -07001422 set_hugetlb_cgroup_rsvd(page, NULL);
Andi Kleena5516432008-07-23 21:27:41 -07001423 h->nr_huge_pages++;
1424 h->nr_huge_pages_node[nid]++;
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001425 spin_unlock(&hugetlb_lock);
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001426}
1427
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001428static void prep_compound_gigantic_page(struct page *page, unsigned int order)
Wu Fengguang20a03072009-06-16 15:32:22 -07001429{
1430 int i;
1431 int nr_pages = 1 << order;
1432 struct page *p = page + 1;
1433
1434 /* we rely on prep_new_huge_page to set the destructor */
1435 set_compound_order(page, order);
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001436 __ClearPageReserved(page);
Kirill A. Shutemovde09d312016-01-15 16:51:42 -08001437 __SetPageHead(page);
Wu Fengguang20a03072009-06-16 15:32:22 -07001438 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001439 /*
1440 * For gigantic hugepages allocated through bootmem at
1441 * boot, it's safer to be consistent with the not-gigantic
1442 * hugepages and clear the PG_reserved bit from all tail pages
1443 * too. Otherwse drivers using get_user_pages() to access tail
1444 * pages may get the reference counting wrong if they see
1445 * PG_reserved set on a tail page (despite the head page not
1446 * having PG_reserved set). Enforcing this consistency between
1447 * head and tail pages allows drivers to optimize away a check
1448 * on the head page when they need know if put_page() is needed
1449 * after get_user_pages().
1450 */
1451 __ClearPageReserved(p);
Youquan Song58a84aa2011-12-08 14:34:18 -08001452 set_page_count(p, 0);
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001453 set_compound_head(p, page);
Wu Fengguang20a03072009-06-16 15:32:22 -07001454 }
Mike Kravetzb4330af2016-02-05 15:36:38 -08001455 atomic_set(compound_mapcount_ptr(page), -1);
John Hubbard47e29d32020-04-01 21:05:33 -07001456
1457 if (hpage_pincount_available(page))
1458 atomic_set(compound_pincount_ptr(page), 0);
Wu Fengguang20a03072009-06-16 15:32:22 -07001459}
1460
Andrew Morton77959122012-10-08 16:34:11 -07001461/*
1462 * PageHuge() only returns true for hugetlbfs pages, but not for normal or
1463 * transparent huge pages. See the PageTransHuge() documentation for more
1464 * details.
1465 */
Wu Fengguang20a03072009-06-16 15:32:22 -07001466int PageHuge(struct page *page)
1467{
Wu Fengguang20a03072009-06-16 15:32:22 -07001468 if (!PageCompound(page))
1469 return 0;
1470
1471 page = compound_head(page);
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -08001472 return page[1].compound_dtor == HUGETLB_PAGE_DTOR;
Wu Fengguang20a03072009-06-16 15:32:22 -07001473}
Naoya Horiguchi43131e12010-05-28 09:29:22 +09001474EXPORT_SYMBOL_GPL(PageHuge);
1475
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001476/*
1477 * PageHeadHuge() only returns true for hugetlbfs head page, but not for
1478 * normal or transparent huge pages.
1479 */
1480int PageHeadHuge(struct page *page_head)
1481{
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001482 if (!PageHead(page_head))
1483 return 0;
1484
Andrew Morton758f66a2014-01-21 15:48:57 -08001485 return get_compound_page_dtor(page_head) == free_huge_page;
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001486}
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001487
Mike Kravetzc0d03812020-04-01 21:11:05 -07001488/*
1489 * Find address_space associated with hugetlbfs page.
1490 * Upon entry page is locked and page 'was' mapped although mapped state
1491 * could change. If necessary, use anon_vma to find vma and associated
1492 * address space. The returned mapping may be stale, but it can not be
1493 * invalid as page lock (which is held) is required to destroy mapping.
1494 */
1495static struct address_space *_get_hugetlb_page_mapping(struct page *hpage)
1496{
1497 struct anon_vma *anon_vma;
1498 pgoff_t pgoff_start, pgoff_end;
1499 struct anon_vma_chain *avc;
1500 struct address_space *mapping = page_mapping(hpage);
1501
1502 /* Simple file based mapping */
1503 if (mapping)
1504 return mapping;
1505
1506 /*
1507 * Even anonymous hugetlbfs mappings are associated with an
1508 * underlying hugetlbfs file (see hugetlb_file_setup in mmap
1509 * code). Find a vma associated with the anonymous vma, and
1510 * use the file pointer to get address_space.
1511 */
1512 anon_vma = page_lock_anon_vma_read(hpage);
1513 if (!anon_vma)
1514 return mapping; /* NULL */
1515
1516 /* Use first found vma */
1517 pgoff_start = page_to_pgoff(hpage);
1518 pgoff_end = pgoff_start + hpage_nr_pages(hpage) - 1;
1519 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
1520 pgoff_start, pgoff_end) {
1521 struct vm_area_struct *vma = avc->vma;
1522
1523 mapping = vma->vm_file->f_mapping;
1524 break;
1525 }
1526
1527 anon_vma_unlock_read(anon_vma);
1528 return mapping;
1529}
1530
1531/*
1532 * Find and lock address space (mapping) in write mode.
1533 *
1534 * Upon entry, the page is locked which allows us to find the mapping
1535 * even in the case of an anon page. However, locking order dictates
1536 * the i_mmap_rwsem be acquired BEFORE the page lock. This is hugetlbfs
1537 * specific. So, we first try to lock the sema while still holding the
1538 * page lock. If this works, great! If not, then we need to drop the
1539 * page lock and then acquire i_mmap_rwsem and reacquire page lock. Of
1540 * course, need to revalidate state along the way.
1541 */
1542struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage)
1543{
1544 struct address_space *mapping, *mapping2;
1545
1546 mapping = _get_hugetlb_page_mapping(hpage);
1547retry:
1548 if (!mapping)
1549 return mapping;
1550
1551 /*
1552 * If no contention, take lock and return
1553 */
1554 if (i_mmap_trylock_write(mapping))
1555 return mapping;
1556
1557 /*
1558 * Must drop page lock and wait on mapping sema.
1559 * Note: Once page lock is dropped, mapping could become invalid.
1560 * As a hack, increase map count until we lock page again.
1561 */
1562 atomic_inc(&hpage->_mapcount);
1563 unlock_page(hpage);
1564 i_mmap_lock_write(mapping);
1565 lock_page(hpage);
1566 atomic_add_negative(-1, &hpage->_mapcount);
1567
1568 /* verify page is still mapped */
1569 if (!page_mapped(hpage)) {
1570 i_mmap_unlock_write(mapping);
1571 return NULL;
1572 }
1573
1574 /*
1575 * Get address space again and verify it is the same one
1576 * we locked. If not, drop lock and retry.
1577 */
1578 mapping2 = _get_hugetlb_page_mapping(hpage);
1579 if (mapping2 != mapping) {
1580 i_mmap_unlock_write(mapping);
1581 mapping = mapping2;
1582 goto retry;
1583 }
1584
1585 return mapping;
1586}
1587
Zhang Yi13d60f42013-06-25 21:19:31 +08001588pgoff_t __basepage_index(struct page *page)
1589{
1590 struct page *page_head = compound_head(page);
1591 pgoff_t index = page_index(page_head);
1592 unsigned long compound_idx;
1593
1594 if (!PageHuge(page_head))
1595 return page_index(page);
1596
1597 if (compound_order(page_head) >= MAX_ORDER)
1598 compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
1599 else
1600 compound_idx = page - page_head;
1601
1602 return (index << compound_order(page_head)) + compound_idx;
1603}
1604
Michal Hocko0c397da2018-01-31 16:20:56 -08001605static struct page *alloc_buddy_huge_page(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07001606 gfp_t gfp_mask, int nid, nodemask_t *nmask,
1607 nodemask_t *node_alloc_noretry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001609 int order = huge_page_order(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct page *page;
Mike Kravetzf60858f2019-09-23 15:37:35 -07001611 bool alloc_try_hard = true;
Joe Jinf96efd52007-07-15 23:38:12 -07001612
Mike Kravetzf60858f2019-09-23 15:37:35 -07001613 /*
1614 * By default we always try hard to allocate the page with
1615 * __GFP_RETRY_MAYFAIL flag. However, if we are allocating pages in
1616 * a loop (to adjust global huge page counts) and previous allocation
1617 * failed, do not continue to try hard on the same node. Use the
1618 * node_alloc_noretry bitmap to manage this state information.
1619 */
1620 if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry))
1621 alloc_try_hard = false;
1622 gfp_mask |= __GFP_COMP|__GFP_NOWARN;
1623 if (alloc_try_hard)
1624 gfp_mask |= __GFP_RETRY_MAYFAIL;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001625 if (nid == NUMA_NO_NODE)
1626 nid = numa_mem_id();
1627 page = __alloc_pages_nodemask(gfp_mask, order, nid, nmask);
1628 if (page)
1629 __count_vm_event(HTLB_BUDDY_PGALLOC);
1630 else
1631 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -07001632
Mike Kravetzf60858f2019-09-23 15:37:35 -07001633 /*
1634 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this
1635 * indicates an overall state change. Clear bit so that we resume
1636 * normal 'try hard' allocations.
1637 */
1638 if (node_alloc_noretry && page && !alloc_try_hard)
1639 node_clear(nid, *node_alloc_noretry);
1640
1641 /*
1642 * If we tried hard to get a page but failed, set bit so that
1643 * subsequent attempts will not try as hard until there is an
1644 * overall state change.
1645 */
1646 if (node_alloc_noretry && !page && alloc_try_hard)
1647 node_set(nid, *node_alloc_noretry);
1648
Nishanth Aravamudan63b46132007-10-16 01:26:24 -07001649 return page;
1650}
1651
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001652/*
Michal Hocko0c397da2018-01-31 16:20:56 -08001653 * Common helper to allocate a fresh hugetlb page. All specific allocators
1654 * should use this function to get new hugetlb pages
1655 */
1656static struct page *alloc_fresh_huge_page(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07001657 gfp_t gfp_mask, int nid, nodemask_t *nmask,
1658 nodemask_t *node_alloc_noretry)
Michal Hocko0c397da2018-01-31 16:20:56 -08001659{
1660 struct page *page;
1661
1662 if (hstate_is_gigantic(h))
1663 page = alloc_gigantic_page(h, gfp_mask, nid, nmask);
1664 else
1665 page = alloc_buddy_huge_page(h, gfp_mask,
Mike Kravetzf60858f2019-09-23 15:37:35 -07001666 nid, nmask, node_alloc_noretry);
Michal Hocko0c397da2018-01-31 16:20:56 -08001667 if (!page)
1668 return NULL;
1669
1670 if (hstate_is_gigantic(h))
1671 prep_compound_gigantic_page(page, huge_page_order(h));
1672 prep_new_huge_page(h, page, page_to_nid(page));
1673
1674 return page;
1675}
1676
1677/*
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001678 * Allocates a fresh page to the hugetlb allocator pool in the node interleaved
1679 * manner.
1680 */
Mike Kravetzf60858f2019-09-23 15:37:35 -07001681static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
1682 nodemask_t *node_alloc_noretry)
Joonsoo Kimb2261022013-09-11 14:21:00 -07001683{
1684 struct page *page;
1685 int nr_nodes, node;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001686 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001687
1688 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
Mike Kravetzf60858f2019-09-23 15:37:35 -07001689 page = alloc_fresh_huge_page(h, gfp_mask, node, nodes_allowed,
1690 node_alloc_noretry);
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001691 if (page)
Joonsoo Kimb2261022013-09-11 14:21:00 -07001692 break;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001693 }
1694
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001695 if (!page)
1696 return 0;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001697
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08001698 put_page(page); /* free it into the hugepage allocator */
1699
1700 return 1;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001701}
1702
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001703/*
1704 * Free huge page from pool from next node to free.
1705 * Attempt to keep persistent huge pages more or less
1706 * balanced over allowed nodes.
1707 * Called with hugetlb_lock locked.
1708 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001709static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
1710 bool acct_surplus)
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001711{
Joonsoo Kimb2261022013-09-11 14:21:00 -07001712 int nr_nodes, node;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001713 int ret = 0;
1714
Joonsoo Kimb2261022013-09-11 14:21:00 -07001715 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001716 /*
1717 * If we're returning unused surplus pages, only examine
1718 * nodes with surplus pages.
1719 */
Joonsoo Kimb2261022013-09-11 14:21:00 -07001720 if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
1721 !list_empty(&h->hugepage_freelists[node])) {
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001722 struct page *page =
Joonsoo Kimb2261022013-09-11 14:21:00 -07001723 list_entry(h->hugepage_freelists[node].next,
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001724 struct page, lru);
1725 list_del(&page->lru);
1726 h->free_huge_pages--;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001727 h->free_huge_pages_node[node]--;
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001728 if (acct_surplus) {
1729 h->surplus_huge_pages--;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001730 h->surplus_huge_pages_node[node]--;
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001731 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001732 update_and_free_page(h, page);
1733 ret = 1;
Lee Schermerhorn9a76db02009-12-14 17:58:15 -08001734 break;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001735 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07001736 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001737
1738 return ret;
1739}
1740
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001741/*
1742 * Dissolve a given free hugepage into free buddy pages. This function does
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001743 * nothing for in-use hugepages and non-hugepages.
1744 * This function returns values like below:
1745 *
1746 * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
1747 * (allocated or reserved.)
1748 * 0: successfully dissolved free hugepages or the page is not a
1749 * hugepage (considered as already dissolved)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001750 */
Anshuman Khandualc3114a82017-07-10 15:47:41 -07001751int dissolve_free_huge_page(struct page *page)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001752{
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07001753 int rc = -EBUSY;
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001754
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001755 /* Not to disrupt normal path by vainly holding hugetlb_lock */
1756 if (!PageHuge(page))
1757 return 0;
1758
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001759 spin_lock(&hugetlb_lock);
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001760 if (!PageHuge(page)) {
1761 rc = 0;
1762 goto out;
1763 }
1764
1765 if (!page_count(page)) {
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001766 struct page *head = compound_head(page);
1767 struct hstate *h = page_hstate(head);
1768 int nid = page_to_nid(head);
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07001769 if (h->free_huge_pages - h->resv_huge_pages == 0)
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001770 goto out;
Anshuman Khandualc3114a82017-07-10 15:47:41 -07001771 /*
1772 * Move PageHWPoison flag from head page to the raw error page,
1773 * which makes any subpages rather than the error page reusable.
1774 */
1775 if (PageHWPoison(head) && page != head) {
1776 SetPageHWPoison(page);
1777 ClearPageHWPoison(head);
1778 }
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001779 list_del(&head->lru);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001780 h->free_huge_pages--;
1781 h->free_huge_pages_node[nid]--;
zhong jiangc1470b32016-08-11 15:32:55 -07001782 h->max_huge_pages--;
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001783 update_and_free_page(h, head);
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07001784 rc = 0;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001785 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001786out:
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001787 spin_unlock(&hugetlb_lock);
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001788 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001789}
1790
1791/*
1792 * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
1793 * make specified memory blocks removable from the system.
Gerald Schaefer2247bb32016-10-07 17:01:07 -07001794 * Note that this will dissolve a free gigantic hugepage completely, if any
1795 * part of it lies within the given range.
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001796 * Also note that if dissolve_free_huge_page() returns with an error, all
1797 * free hugepages that were dissolved before that error are lost.
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001798 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001799int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001800{
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001801 unsigned long pfn;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07001802 struct page *page;
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001803 int rc = 0;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001804
Li Zhongd0177632014-08-06 16:07:56 -07001805 if (!hugepages_supported())
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001806 return rc;
Li Zhongd0177632014-08-06 16:07:56 -07001807
Gerald Schaefereb03aa02016-10-07 17:01:13 -07001808 for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
1809 page = pfn_to_page(pfn);
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07001810 rc = dissolve_free_huge_page(page);
1811 if (rc)
1812 break;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07001813 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001814
1815 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001816}
1817
Michal Hockoab5ac902018-01-31 16:20:48 -08001818/*
1819 * Allocates a fresh surplus page from the page allocator.
1820 */
Michal Hocko0c397da2018-01-31 16:20:56 -08001821static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,
Michal Hockoaaf14e42017-07-10 15:49:08 -07001822 int nid, nodemask_t *nmask)
Adam Litke7893d1d2007-10-16 01:26:18 -07001823{
Michal Hocko9980d742018-01-31 16:20:52 -08001824 struct page *page = NULL;
Adam Litke7893d1d2007-10-16 01:26:18 -07001825
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001826 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07001827 return NULL;
1828
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001829 spin_lock(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08001830 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages)
1831 goto out_unlock;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001832 spin_unlock(&hugetlb_lock);
1833
Mike Kravetzf60858f2019-09-23 15:37:35 -07001834 page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL);
Michal Hocko9980d742018-01-31 16:20:52 -08001835 if (!page)
Michal Hocko0c397da2018-01-31 16:20:56 -08001836 return NULL;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001837
1838 spin_lock(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08001839 /*
1840 * We could have raced with the pool size change.
1841 * Double check that and simply deallocate the new page
1842 * if we would end up overcommiting the surpluses. Abuse
1843 * temporary page to workaround the nasty free_huge_page
1844 * codeflow
1845 */
1846 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
1847 SetPageHugeTemporary(page);
Kai Shen2bf753e2019-05-13 17:15:37 -07001848 spin_unlock(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08001849 put_page(page);
Kai Shen2bf753e2019-05-13 17:15:37 -07001850 return NULL;
Michal Hocko9980d742018-01-31 16:20:52 -08001851 } else {
Michal Hocko9980d742018-01-31 16:20:52 -08001852 h->surplus_huge_pages++;
Michal Hocko4704dea2018-03-09 15:50:55 -08001853 h->surplus_huge_pages_node[page_to_nid(page)]++;
Adam Litke7893d1d2007-10-16 01:26:18 -07001854 }
Michal Hocko9980d742018-01-31 16:20:52 -08001855
1856out_unlock:
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001857 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001858
1859 return page;
1860}
1861
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001862struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
1863 int nid, nodemask_t *nmask)
Michal Hockoab5ac902018-01-31 16:20:48 -08001864{
1865 struct page *page;
1866
1867 if (hstate_is_gigantic(h))
1868 return NULL;
1869
Mike Kravetzf60858f2019-09-23 15:37:35 -07001870 page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL);
Michal Hockoab5ac902018-01-31 16:20:48 -08001871 if (!page)
1872 return NULL;
1873
1874 /*
1875 * We do not account these pages as surplus because they are only
1876 * temporary and will be released properly on the last reference
1877 */
Michal Hockoab5ac902018-01-31 16:20:48 -08001878 SetPageHugeTemporary(page);
1879
1880 return page;
1881}
1882
Adam Litkee4e574b2007-10-16 01:26:19 -07001883/*
Dave Hansen099730d2015-11-05 18:50:17 -08001884 * Use the VMA's mpolicy to allocate a huge page from the buddy.
1885 */
Dave Hansene0ec90e2015-11-05 18:50:20 -08001886static
Michal Hocko0c397da2018-01-31 16:20:56 -08001887struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h,
Dave Hansen099730d2015-11-05 18:50:17 -08001888 struct vm_area_struct *vma, unsigned long addr)
1889{
Michal Hockoaaf14e42017-07-10 15:49:08 -07001890 struct page *page;
1891 struct mempolicy *mpol;
1892 gfp_t gfp_mask = htlb_alloc_mask(h);
1893 int nid;
1894 nodemask_t *nodemask;
1895
1896 nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
Michal Hocko0c397da2018-01-31 16:20:56 -08001897 page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask);
Michal Hockoaaf14e42017-07-10 15:49:08 -07001898 mpol_cond_put(mpol);
1899
1900 return page;
Dave Hansen099730d2015-11-05 18:50:17 -08001901}
1902
Michal Hockoab5ac902018-01-31 16:20:48 -08001903/* page migration callback function */
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001904struct page *alloc_huge_page_node(struct hstate *h, int nid)
1905{
Michal Hockoaaf14e42017-07-10 15:49:08 -07001906 gfp_t gfp_mask = htlb_alloc_mask(h);
Joonsoo Kim4ef91842013-09-11 14:21:51 -07001907 struct page *page = NULL;
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001908
Michal Hockoaaf14e42017-07-10 15:49:08 -07001909 if (nid != NUMA_NO_NODE)
1910 gfp_mask |= __GFP_THISNODE;
1911
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001912 spin_lock(&hugetlb_lock);
Joonsoo Kim4ef91842013-09-11 14:21:51 -07001913 if (h->free_huge_pages - h->resv_huge_pages > 0)
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001914 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, NULL);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001915 spin_unlock(&hugetlb_lock);
1916
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -07001917 if (!page)
Michal Hocko0c397da2018-01-31 16:20:56 -08001918 page = alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001919
1920 return page;
1921}
1922
Michal Hockoab5ac902018-01-31 16:20:48 -08001923/* page migration callback function */
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001924struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
1925 nodemask_t *nmask)
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001926{
Michal Hockoaaf14e42017-07-10 15:49:08 -07001927 gfp_t gfp_mask = htlb_alloc_mask(h);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001928
1929 spin_lock(&hugetlb_lock);
1930 if (h->free_huge_pages - h->resv_huge_pages > 0) {
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001931 struct page *page;
1932
1933 page = dequeue_huge_page_nodemask(h, gfp_mask, preferred_nid, nmask);
1934 if (page) {
1935 spin_unlock(&hugetlb_lock);
1936 return page;
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001937 }
1938 }
1939 spin_unlock(&hugetlb_lock);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001940
Michal Hocko0c397da2018-01-31 16:20:56 -08001941 return alloc_migrate_huge_page(h, gfp_mask, preferred_nid, nmask);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07001942}
1943
Michal Hockoebd63722018-01-31 16:21:00 -08001944/* mempolicy aware migration callback */
Michal Hocko389c8172018-01-31 16:21:03 -08001945struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
1946 unsigned long address)
Michal Hockoebd63722018-01-31 16:21:00 -08001947{
1948 struct mempolicy *mpol;
1949 nodemask_t *nodemask;
1950 struct page *page;
Michal Hockoebd63722018-01-31 16:21:00 -08001951 gfp_t gfp_mask;
1952 int node;
1953
Michal Hockoebd63722018-01-31 16:21:00 -08001954 gfp_mask = htlb_alloc_mask(h);
1955 node = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
1956 page = alloc_huge_page_nodemask(h, node, nodemask);
1957 mpol_cond_put(mpol);
1958
1959 return page;
1960}
1961
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001962/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001963 * Increase the hugetlb pool such that it can accommodate a reservation
Adam Litkee4e574b2007-10-16 01:26:19 -07001964 * of size 'delta'.
1965 */
Andi Kleena5516432008-07-23 21:27:41 -07001966static int gather_surplus_pages(struct hstate *h, int delta)
Adam Litkee4e574b2007-10-16 01:26:19 -07001967{
1968 struct list_head surplus_list;
1969 struct page *page, *tmp;
1970 int ret, i;
1971 int needed, allocated;
Hillf Danton28073b02012-03-21 16:34:00 -07001972 bool alloc_ok = true;
Adam Litkee4e574b2007-10-16 01:26:19 -07001973
Andi Kleena5516432008-07-23 21:27:41 -07001974 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -08001975 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -07001976 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07001977 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -08001978 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001979
1980 allocated = 0;
1981 INIT_LIST_HEAD(&surplus_list);
1982
1983 ret = -ENOMEM;
1984retry:
1985 spin_unlock(&hugetlb_lock);
1986 for (i = 0; i < needed; i++) {
Michal Hocko0c397da2018-01-31 16:20:56 -08001987 page = alloc_surplus_huge_page(h, htlb_alloc_mask(h),
Michal Hockoaaf14e42017-07-10 15:49:08 -07001988 NUMA_NO_NODE, NULL);
Hillf Danton28073b02012-03-21 16:34:00 -07001989 if (!page) {
1990 alloc_ok = false;
1991 break;
1992 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001993 list_add(&page->lru, &surplus_list);
David Rientjes69ed7792017-07-10 15:48:50 -07001994 cond_resched();
Adam Litkee4e574b2007-10-16 01:26:19 -07001995 }
Hillf Danton28073b02012-03-21 16:34:00 -07001996 allocated += i;
Adam Litkee4e574b2007-10-16 01:26:19 -07001997
1998 /*
1999 * After retaking hugetlb_lock, we need to recalculate 'needed'
2000 * because either resv_huge_pages or free_huge_pages may have changed.
2001 */
2002 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002003 needed = (h->resv_huge_pages + delta) -
2004 (h->free_huge_pages + allocated);
Hillf Danton28073b02012-03-21 16:34:00 -07002005 if (needed > 0) {
2006 if (alloc_ok)
2007 goto retry;
2008 /*
2009 * We were not able to allocate enough pages to
2010 * satisfy the entire reservation so we free what
2011 * we've allocated so far.
2012 */
2013 goto free;
2014 }
Adam Litkee4e574b2007-10-16 01:26:19 -07002015 /*
2016 * The surplus_list now contains _at_least_ the number of extra pages
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002017 * needed to accommodate the reservation. Add the appropriate number
Adam Litkee4e574b2007-10-16 01:26:19 -07002018 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -08002019 * allocator. Commit the entire reservation here to prevent another
2020 * process from stealing the pages as they are added to the pool but
2021 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -07002022 */
2023 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -07002024 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07002025 ret = 0;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09002026
Adam Litke19fc3f02008-04-28 02:12:20 -07002027 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -07002028 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -07002029 if ((--needed) < 0)
2030 break;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09002031 /*
2032 * This page is now managed by the hugetlb allocator and has
2033 * no users -- drop the buddy allocator's reference.
2034 */
2035 put_page_testzero(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08002036 VM_BUG_ON_PAGE(page_count(page), page);
Andi Kleena5516432008-07-23 21:27:41 -07002037 enqueue_huge_page(h, page);
Adam Litke19fc3f02008-04-28 02:12:20 -07002038 }
Hillf Danton28073b02012-03-21 16:34:00 -07002039free:
Hillf Dantonb0365c82011-12-28 15:57:16 -08002040 spin_unlock(&hugetlb_lock);
Adam Litke19fc3f02008-04-28 02:12:20 -07002041
2042 /* Free unnecessary surplus pages to the buddy allocator */
Joonsoo Kimc0d934b2013-09-11 14:21:02 -07002043 list_for_each_entry_safe(page, tmp, &surplus_list, lru)
2044 put_page(page);
Naoya Horiguchia9869b82010-09-08 10:19:37 +09002045 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07002046
2047 return ret;
2048}
2049
2050/*
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002051 * This routine has two main purposes:
2052 * 1) Decrement the reservation count (resv_huge_pages) by the value passed
2053 * in unused_resv_pages. This corresponds to the prior adjustments made
2054 * to the associated reservation map.
2055 * 2) Free any unused surplus pages that may have been allocated to satisfy
2056 * the reservation. As many as unused_resv_pages may be freed.
2057 *
2058 * Called with hugetlb_lock held. However, the lock could be dropped (and
2059 * reacquired) during calls to cond_resched_lock. Whenever dropping the lock,
2060 * we must make sure nobody else can claim pages we are in the process of
2061 * freeing. Do this by ensuring resv_huge_page always is greater than the
2062 * number of huge pages we plan to free when dropping the lock.
Adam Litkee4e574b2007-10-16 01:26:19 -07002063 */
Andi Kleena5516432008-07-23 21:27:41 -07002064static void return_unused_surplus_pages(struct hstate *h,
2065 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -07002066{
Adam Litkee4e574b2007-10-16 01:26:19 -07002067 unsigned long nr_pages;
2068
Andi Kleenaa888a72008-07-23 21:27:47 -07002069 /* Cannot return gigantic pages currently */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002070 if (hstate_is_gigantic(h))
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002071 goto out;
Andi Kleenaa888a72008-07-23 21:27:47 -07002072
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002073 /*
2074 * Part (or even all) of the reservation could have been backed
2075 * by pre-allocated pages. Only free surplus pages.
2076 */
Andi Kleena5516432008-07-23 21:27:41 -07002077 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -07002078
Lee Schermerhorn685f3452009-09-21 17:01:23 -07002079 /*
2080 * We want to release as many surplus pages as possible, spread
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002081 * evenly across all nodes with memory. Iterate across these nodes
2082 * until we can no longer free unreserved surplus pages. This occurs
2083 * when the nodes with surplus pages have no free pages.
2084 * free_pool_huge_page() will balance the the freed pages across the
2085 * on-line nodes with memory and will handle the hstate accounting.
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002086 *
2087 * Note that we decrement resv_huge_pages as we free the pages. If
2088 * we drop the lock, resv_huge_pages will still be sufficiently large
2089 * to cover subsequent pages we may free.
Lee Schermerhorn685f3452009-09-21 17:01:23 -07002090 */
2091 while (nr_pages--) {
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002092 h->resv_huge_pages--;
2093 unused_resv_pages--;
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08002094 if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002095 goto out;
Mizuma, Masayoshi7848a4b2014-04-18 15:07:18 -07002096 cond_resched_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07002097 }
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002098
2099out:
2100 /* Fully uncommit the reservation */
2101 h->resv_huge_pages -= unused_resv_pages;
Adam Litkee4e574b2007-10-16 01:26:19 -07002102}
2103
Mike Kravetz5e911372015-09-08 15:01:28 -07002104
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002105/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002106 * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
Mike Kravetz5e911372015-09-08 15:01:28 -07002107 * are used by the huge page allocation routines to manage reservations.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002108 *
2109 * vma_needs_reservation is called to determine if the huge page at addr
2110 * within the vma has an associated reservation. If a reservation is
2111 * needed, the value 1 is returned. The caller is then responsible for
2112 * managing the global reservation and subpool usage counts. After
2113 * the huge page has been allocated, vma_commit_reservation is called
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002114 * to add the page to the reservation map. If the page allocation fails,
2115 * the reservation must be ended instead of committed. vma_end_reservation
2116 * is called in such cases.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002117 *
2118 * In the normal case, vma_commit_reservation returns the same value
2119 * as the preceding vma_needs_reservation call. The only time this
2120 * is not the case is if a reserve map was changed between calls. It
2121 * is the responsibility of the caller to notice the difference and
2122 * take appropriate action.
Mike Kravetz96b96a92016-11-10 10:46:32 -08002123 *
2124 * vma_add_reservation is used in error paths where a reservation must
2125 * be restored when a newly allocated huge page must be freed. It is
2126 * to be called after calling vma_needs_reservation to determine if a
2127 * reservation exists.
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002128 */
Mike Kravetz5e911372015-09-08 15:01:28 -07002129enum vma_resv_mode {
2130 VMA_NEEDS_RESV,
2131 VMA_COMMIT_RESV,
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002132 VMA_END_RESV,
Mike Kravetz96b96a92016-11-10 10:46:32 -08002133 VMA_ADD_RESV,
Mike Kravetz5e911372015-09-08 15:01:28 -07002134};
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002135static long __vma_reservation_common(struct hstate *h,
2136 struct vm_area_struct *vma, unsigned long addr,
Mike Kravetz5e911372015-09-08 15:01:28 -07002137 enum vma_resv_mode mode)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002138{
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002139 struct resv_map *resv;
2140 pgoff_t idx;
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002141 long ret;
Mina Almasry0db9d742020-04-01 21:11:25 -07002142 long dummy_out_regions_needed;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002143
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002144 resv = vma_resv_map(vma);
2145 if (!resv)
Andy Whitcroft84afd992008-07-23 21:27:32 -07002146 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002147
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002148 idx = vma_hugecache_offset(h, vma, addr);
Mike Kravetz5e911372015-09-08 15:01:28 -07002149 switch (mode) {
2150 case VMA_NEEDS_RESV:
Mina Almasry0db9d742020-04-01 21:11:25 -07002151 ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed);
2152 /* We assume that vma_reservation_* routines always operate on
2153 * 1 page, and that adding to resv map a 1 page entry can only
2154 * ever require 1 region.
2155 */
2156 VM_BUG_ON(dummy_out_regions_needed != 1);
Mike Kravetz5e911372015-09-08 15:01:28 -07002157 break;
2158 case VMA_COMMIT_RESV:
Mina Almasry075a61d2020-04-01 21:11:28 -07002159 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
Mina Almasry0db9d742020-04-01 21:11:25 -07002160 /* region_add calls of range 1 should never fail. */
2161 VM_BUG_ON(ret < 0);
Mike Kravetz5e911372015-09-08 15:01:28 -07002162 break;
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002163 case VMA_END_RESV:
Mina Almasry0db9d742020-04-01 21:11:25 -07002164 region_abort(resv, idx, idx + 1, 1);
Mike Kravetz5e911372015-09-08 15:01:28 -07002165 ret = 0;
2166 break;
Mike Kravetz96b96a92016-11-10 10:46:32 -08002167 case VMA_ADD_RESV:
Mina Almasry0db9d742020-04-01 21:11:25 -07002168 if (vma->vm_flags & VM_MAYSHARE) {
Mina Almasry075a61d2020-04-01 21:11:28 -07002169 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
Mina Almasry0db9d742020-04-01 21:11:25 -07002170 /* region_add calls of range 1 should never fail. */
2171 VM_BUG_ON(ret < 0);
2172 } else {
2173 region_abort(resv, idx, idx + 1, 1);
Mike Kravetz96b96a92016-11-10 10:46:32 -08002174 ret = region_del(resv, idx, idx + 1);
2175 }
2176 break;
Mike Kravetz5e911372015-09-08 15:01:28 -07002177 default:
2178 BUG();
2179 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07002180
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002181 if (vma->vm_flags & VM_MAYSHARE)
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002182 return ret;
Mike Kravetz67961f92016-06-08 15:33:42 -07002183 else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && ret >= 0) {
2184 /*
2185 * In most cases, reserves always exist for private mappings.
2186 * However, a file associated with mapping could have been
2187 * hole punched or truncated after reserves were consumed.
2188 * As subsequent fault on such a range will not use reserves.
2189 * Subtle - The reserve map for private mappings has the
2190 * opposite meaning than that of shared mappings. If NO
2191 * entry is in the reserve map, it means a reservation exists.
2192 * If an entry exists in the reserve map, it means the
2193 * reservation has already been consumed. As a result, the
2194 * return value of this routine is the opposite of the
2195 * value returned from reserve map manipulation routines above.
2196 */
2197 if (ret)
2198 return 0;
2199 else
2200 return 1;
2201 }
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002202 else
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002203 return ret < 0 ? ret : 0;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002204}
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002205
2206static long vma_needs_reservation(struct hstate *h,
Andi Kleena5516432008-07-23 21:27:41 -07002207 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002208{
Mike Kravetz5e911372015-09-08 15:01:28 -07002209 return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002210}
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002211
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002212static long vma_commit_reservation(struct hstate *h,
2213 struct vm_area_struct *vma, unsigned long addr)
2214{
Mike Kravetz5e911372015-09-08 15:01:28 -07002215 return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
2216}
2217
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002218static void vma_end_reservation(struct hstate *h,
Mike Kravetz5e911372015-09-08 15:01:28 -07002219 struct vm_area_struct *vma, unsigned long addr)
2220{
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002221 (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002222}
2223
Mike Kravetz96b96a92016-11-10 10:46:32 -08002224static long vma_add_reservation(struct hstate *h,
2225 struct vm_area_struct *vma, unsigned long addr)
2226{
2227 return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
2228}
2229
2230/*
2231 * This routine is called to restore a reservation on error paths. In the
2232 * specific error paths, a huge page was allocated (via alloc_huge_page)
2233 * and is about to be freed. If a reservation for the page existed,
2234 * alloc_huge_page would have consumed the reservation and set PagePrivate
2235 * in the newly allocated page. When the page is freed via free_huge_page,
2236 * the global reservation count will be incremented if PagePrivate is set.
2237 * However, free_huge_page can not adjust the reserve map. Adjust the
2238 * reserve map here to be consistent with global reserve count adjustments
2239 * to be made by free_huge_page.
2240 */
2241static void restore_reserve_on_error(struct hstate *h,
2242 struct vm_area_struct *vma, unsigned long address,
2243 struct page *page)
2244{
2245 if (unlikely(PagePrivate(page))) {
2246 long rc = vma_needs_reservation(h, vma, address);
2247
2248 if (unlikely(rc < 0)) {
2249 /*
2250 * Rare out of memory condition in reserve map
2251 * manipulation. Clear PagePrivate so that
2252 * global reserve count will not be incremented
2253 * by free_huge_page. This will make it appear
2254 * as though the reservation for this page was
2255 * consumed. This may prevent the task from
2256 * faulting in the page at a later time. This
2257 * is better than inconsistent global huge page
2258 * accounting of reserve counts.
2259 */
2260 ClearPagePrivate(page);
2261 } else if (rc) {
2262 rc = vma_add_reservation(h, vma, address);
2263 if (unlikely(rc < 0))
2264 /*
2265 * See above comment about rare out of
2266 * memory condition.
2267 */
2268 ClearPagePrivate(page);
2269 } else
2270 vma_end_reservation(h, vma, address);
2271 }
2272}
2273
Mike Kravetz70c35472015-09-08 15:01:54 -07002274struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002275 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
David Gibson90481622012-03-21 16:34:12 -07002277 struct hugepage_subpool *spool = subpool_vma(vma);
Andi Kleena5516432008-07-23 21:27:41 -07002278 struct hstate *h = hstate_vma(vma);
Adam Litke348ea202007-11-14 16:59:37 -08002279 struct page *page;
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002280 long map_chg, map_commit;
2281 long gbl_chg;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07002282 int ret, idx;
2283 struct hugetlb_cgroup *h_cg;
Adam Litke2fc39ce2007-11-14 16:59:39 -08002284
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07002285 idx = hstate_index(h);
Mel Gormana1e78772008-07-23 21:27:23 -07002286 /*
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002287 * Examine the region/reserve map to determine if the process
2288 * has a reservation for the page to be allocated. A return
2289 * code of zero indicates a reservation exists (no change).
Mel Gormana1e78772008-07-23 21:27:23 -07002290 */
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002291 map_chg = gbl_chg = vma_needs_reservation(h, vma, addr);
2292 if (map_chg < 0)
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07002293 return ERR_PTR(-ENOMEM);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002294
2295 /*
2296 * Processes that did not create the mapping will have no
2297 * reserves as indicated by the region/reserve map. Check
2298 * that the allocation will not exceed the subpool limit.
2299 * Allocations for MAP_NORESERVE mappings also need to be
2300 * checked against any subpool limit.
2301 */
2302 if (map_chg || avoid_reserve) {
2303 gbl_chg = hugepage_subpool_get_pages(spool, 1);
2304 if (gbl_chg < 0) {
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002305 vma_end_reservation(h, vma, addr);
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07002306 return ERR_PTR(-ENOSPC);
Mike Kravetz5e911372015-09-08 15:01:28 -07002307 }
Mel Gormana1e78772008-07-23 21:27:23 -07002308
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002309 /*
2310 * Even though there was no reservation in the region/reserve
2311 * map, there could be reservations associated with the
2312 * subpool that can be used. This would be indicated if the
2313 * return value of hugepage_subpool_get_pages() is zero.
2314 * However, if avoid_reserve is specified we still avoid even
2315 * the subpool reservations.
2316 */
2317 if (avoid_reserve)
2318 gbl_chg = 1;
2319 }
2320
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07002321 ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002322 if (ret)
2323 goto out_subpool_put;
2324
Mel Gormana1e78772008-07-23 21:27:23 -07002325 spin_lock(&hugetlb_lock);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002326 /*
2327 * glb_chg is passed to indicate whether or not a page must be taken
2328 * from the global free pool (global change). gbl_chg == 0 indicates
2329 * a reservation exists for the allocation.
2330 */
2331 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07002332 if (!page) {
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -07002333 spin_unlock(&hugetlb_lock);
Michal Hocko0c397da2018-01-31 16:20:56 -08002334 page = alloc_buddy_huge_page_with_mpol(h, vma, addr);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002335 if (!page)
2336 goto out_uncharge_cgroup;
Naoya Horiguchia88c7692015-12-11 13:40:24 -08002337 if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
2338 SetPagePrivate(page);
2339 h->resv_huge_pages--;
2340 }
Aneesh Kumar K.V79dbb232012-07-31 16:42:32 -07002341 spin_lock(&hugetlb_lock);
2342 list_move(&page->lru, &h->hugepage_activelist);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07002343 /* Fall through */
Mel Gormana1e78772008-07-23 21:27:23 -07002344 }
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07002345 hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
2346 spin_unlock(&hugetlb_lock);
Mel Gormana1e78772008-07-23 21:27:23 -07002347
David Gibson90481622012-03-21 16:34:12 -07002348 set_page_private(page, (unsigned long)spool);
Mel Gormana1e78772008-07-23 21:27:23 -07002349
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002350 map_commit = vma_commit_reservation(h, vma, addr);
2351 if (unlikely(map_chg > map_commit)) {
Mike Kravetz33039672015-06-24 16:57:58 -07002352 /*
2353 * The page was added to the reservation map between
2354 * vma_needs_reservation and vma_commit_reservation.
2355 * This indicates a race with hugetlb_reserve_pages.
2356 * Adjust for the subpool count incremented above AND
2357 * in hugetlb_reserve_pages for the same page. Also,
2358 * the reservation count added in hugetlb_reserve_pages
2359 * no longer applies.
2360 */
2361 long rsv_adjust;
2362
2363 rsv_adjust = hugepage_subpool_put_pages(spool, 1);
2364 hugetlb_acct_memory(h, -rsv_adjust);
2365 }
Adam Litke90d8b7e2007-11-14 16:59:42 -08002366 return page;
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002367
2368out_uncharge_cgroup:
2369 hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
2370out_subpool_put:
Mike Kravetzd85f69b2015-09-08 15:01:47 -07002371 if (map_chg || avoid_reserve)
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002372 hugepage_subpool_put_pages(spool, 1);
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002373 vma_end_reservation(h, vma, addr);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07002374 return ERR_PTR(-ENOSPC);
David Gibsonb45b5bd2006-03-22 00:08:55 -08002375}
2376
Aneesh Kumar K.Ve24a1302017-07-28 10:31:25 +05302377int alloc_bootmem_huge_page(struct hstate *h)
2378 __attribute__ ((weak, alias("__alloc_bootmem_huge_page")));
2379int __alloc_bootmem_huge_page(struct hstate *h)
Andi Kleenaa888a72008-07-23 21:27:47 -07002380{
2381 struct huge_bootmem_page *m;
Joonsoo Kimb2261022013-09-11 14:21:00 -07002382 int nr_nodes, node;
Andi Kleenaa888a72008-07-23 21:27:47 -07002383
Joonsoo Kimb2261022013-09-11 14:21:00 -07002384 for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
Andi Kleenaa888a72008-07-23 21:27:47 -07002385 void *addr;
2386
Mike Rapoporteb31d552018-10-30 15:08:04 -07002387 addr = memblock_alloc_try_nid_raw(
Grygorii Strashko8b89a112014-01-21 15:50:36 -08002388 huge_page_size(h), huge_page_size(h),
Mike Rapoport97ad1082018-10-30 15:09:44 -07002389 0, MEMBLOCK_ALLOC_ACCESSIBLE, node);
Andi Kleenaa888a72008-07-23 21:27:47 -07002390 if (addr) {
2391 /*
2392 * Use the beginning of the huge page to store the
2393 * huge_bootmem_page struct (until gather_bootmem
2394 * puts them into the mem_map).
2395 */
2396 m = addr;
Cyrill Gorcunov91f47662009-01-06 14:40:33 -08002397 goto found;
Andi Kleenaa888a72008-07-23 21:27:47 -07002398 }
Andi Kleenaa888a72008-07-23 21:27:47 -07002399 }
2400 return 0;
2401
2402found:
Luiz Capitulinodf994ea2014-12-12 16:55:21 -08002403 BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h)));
Andi Kleenaa888a72008-07-23 21:27:47 -07002404 /* Put them into a private list first because mem_map is not up yet */
Cannon Matthews330d6e42018-08-17 15:49:17 -07002405 INIT_LIST_HEAD(&m->list);
Andi Kleenaa888a72008-07-23 21:27:47 -07002406 list_add(&m->list, &huge_boot_pages);
2407 m->hstate = h;
2408 return 1;
2409}
2410
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08002411static void __init prep_compound_huge_page(struct page *page,
2412 unsigned int order)
Andy Whitcroft18229df2008-11-06 12:53:27 -08002413{
2414 if (unlikely(order > (MAX_ORDER - 1)))
2415 prep_compound_gigantic_page(page, order);
2416 else
2417 prep_compound_page(page, order);
2418}
2419
Andi Kleenaa888a72008-07-23 21:27:47 -07002420/* Put bootmem huge pages into the standard lists after mem_map is up */
2421static void __init gather_bootmem_prealloc(void)
2422{
2423 struct huge_bootmem_page *m;
2424
2425 list_for_each_entry(m, &huge_boot_pages, list) {
Mike Kravetz40d18eb2018-08-17 15:49:07 -07002426 struct page *page = virt_to_page(m);
Andi Kleenaa888a72008-07-23 21:27:47 -07002427 struct hstate *h = m->hstate;
Becky Bruceee8f2482011-07-25 17:11:50 -07002428
Andi Kleenaa888a72008-07-23 21:27:47 -07002429 WARN_ON(page_count(page) != 1);
Andy Whitcroft18229df2008-11-06 12:53:27 -08002430 prep_compound_huge_page(page, h->order);
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07002431 WARN_ON(PageReserved(page));
Andi Kleenaa888a72008-07-23 21:27:47 -07002432 prep_new_huge_page(h, page, page_to_nid(page));
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002433 put_page(page); /* free it into the hugepage allocator */
2434
Rafael Aquinib0320c72011-06-15 15:08:39 -07002435 /*
2436 * If we had gigantic hugepages allocated at boot time, we need
2437 * to restore the 'stolen' pages to totalram_pages in order to
2438 * fix confusing memory reports from free(1) and another
2439 * side-effects, like CommitLimit going negative.
2440 */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002441 if (hstate_is_gigantic(h))
Jiang Liu3dcc0572013-07-03 15:03:21 -07002442 adjust_managed_page_count(page, 1 << h->order);
Cannon Matthews520495f2018-07-03 17:02:43 -07002443 cond_resched();
Andi Kleenaa888a72008-07-23 21:27:47 -07002444 }
2445}
2446
Andi Kleen8faa8b02008-07-23 21:27:48 -07002447static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 unsigned long i;
Mike Kravetzf60858f2019-09-23 15:37:35 -07002450 nodemask_t *node_alloc_noretry;
2451
2452 if (!hstate_is_gigantic(h)) {
2453 /*
2454 * Bit mask controlling how hard we retry per-node allocations.
2455 * Ignore errors as lower level routines can deal with
2456 * node_alloc_noretry == NULL. If this kmalloc fails at boot
2457 * time, we are likely in bigger trouble.
2458 */
2459 node_alloc_noretry = kmalloc(sizeof(*node_alloc_noretry),
2460 GFP_KERNEL);
2461 } else {
2462 /* allocations done at boot time */
2463 node_alloc_noretry = NULL;
2464 }
2465
2466 /* bit mask controlling how hard we retry per-node allocations */
2467 if (node_alloc_noretry)
2468 nodes_clear(*node_alloc_noretry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Andi Kleene5ff2152008-07-23 21:27:42 -07002470 for (i = 0; i < h->max_huge_pages; ++i) {
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002471 if (hstate_is_gigantic(h)) {
Andi Kleenaa888a72008-07-23 21:27:47 -07002472 if (!alloc_bootmem_huge_page(h))
2473 break;
Michal Hocko0c397da2018-01-31 16:20:56 -08002474 } else if (!alloc_pool_huge_page(h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07002475 &node_states[N_MEMORY],
2476 node_alloc_noretry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 break;
David Rientjes69ed7792017-07-10 15:48:50 -07002478 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Liam R. Howlettd715cf82017-07-10 15:48:15 -07002480 if (i < h->max_huge_pages) {
2481 char buf[32];
2482
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07002483 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Liam R. Howlettd715cf82017-07-10 15:48:15 -07002484 pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n",
2485 h->max_huge_pages, buf, i);
2486 h->max_huge_pages = i;
2487 }
Mike Kravetzf60858f2019-09-23 15:37:35 -07002488
2489 kfree(node_alloc_noretry);
Andi Kleene5ff2152008-07-23 21:27:42 -07002490}
2491
2492static void __init hugetlb_init_hstates(void)
2493{
2494 struct hstate *h;
2495
2496 for_each_hstate(h) {
Naoya Horiguchi641844f2015-06-24 16:56:59 -07002497 if (minimum_order > huge_page_order(h))
2498 minimum_order = huge_page_order(h);
2499
Andi Kleen8faa8b02008-07-23 21:27:48 -07002500 /* oversize hugepages were init'ed in early boot */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002501 if (!hstate_is_gigantic(h))
Andi Kleen8faa8b02008-07-23 21:27:48 -07002502 hugetlb_hstate_alloc_pages(h);
Andi Kleene5ff2152008-07-23 21:27:42 -07002503 }
Naoya Horiguchi641844f2015-06-24 16:56:59 -07002504 VM_BUG_ON(minimum_order == UINT_MAX);
Andi Kleene5ff2152008-07-23 21:27:42 -07002505}
2506
2507static void __init report_hugepages(void)
2508{
2509 struct hstate *h;
2510
2511 for_each_hstate(h) {
Andi Kleen4abd32d2008-07-23 21:27:49 -07002512 char buf[32];
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07002513
2514 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Andrew Mortonffb22af2013-02-22 16:32:08 -08002515 pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07002516 buf, h->free_huge_pages);
Andi Kleene5ff2152008-07-23 21:27:42 -07002517 }
2518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520#ifdef CONFIG_HIGHMEM
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002521static void try_to_free_low(struct hstate *h, unsigned long count,
2522 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Christoph Lameter4415cc82006-09-25 23:31:55 -07002524 int i;
2525
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002526 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07002527 return;
2528
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002529 for_each_node_mask(i, *nodes_allowed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -07002531 struct list_head *freel = &h->hugepage_freelists[i];
2532 list_for_each_entry_safe(page, next, freel, lru) {
2533 if (count >= h->nr_huge_pages)
Adam Litke6b0c8802007-10-16 01:26:23 -07002534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (PageHighMem(page))
2536 continue;
2537 list_del(&page->lru);
Andi Kleene5ff2152008-07-23 21:27:42 -07002538 update_and_free_page(h, page);
Andi Kleena5516432008-07-23 21:27:41 -07002539 h->free_huge_pages--;
2540 h->free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
2542 }
2543}
2544#else
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002545static inline void try_to_free_low(struct hstate *h, unsigned long count,
2546 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548}
2549#endif
2550
Wu Fengguang20a03072009-06-16 15:32:22 -07002551/*
2552 * Increment or decrement surplus_huge_pages. Keep node-specific counters
2553 * balanced by operating on them in a round-robin fashion.
2554 * Returns 1 if an adjustment was made.
2555 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002556static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
2557 int delta)
Wu Fengguang20a03072009-06-16 15:32:22 -07002558{
Joonsoo Kimb2261022013-09-11 14:21:00 -07002559 int nr_nodes, node;
Wu Fengguang20a03072009-06-16 15:32:22 -07002560
2561 VM_BUG_ON(delta != -1 && delta != 1);
Wu Fengguang20a03072009-06-16 15:32:22 -07002562
Joonsoo Kimb2261022013-09-11 14:21:00 -07002563 if (delta < 0) {
2564 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
2565 if (h->surplus_huge_pages_node[node])
2566 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002567 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002568 } else {
2569 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
2570 if (h->surplus_huge_pages_node[node] <
2571 h->nr_huge_pages_node[node])
2572 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002573 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002574 }
2575 return 0;
Wu Fengguang20a03072009-06-16 15:32:22 -07002576
Joonsoo Kimb2261022013-09-11 14:21:00 -07002577found:
2578 h->surplus_huge_pages += delta;
2579 h->surplus_huge_pages_node[node] += delta;
2580 return 1;
Wu Fengguang20a03072009-06-16 15:32:22 -07002581}
2582
Andi Kleena5516432008-07-23 21:27:41 -07002583#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002584static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002585 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Adam Litke7893d1d2007-10-16 01:26:18 -07002587 unsigned long min_count, ret;
Mike Kravetzf60858f2019-09-23 15:37:35 -07002588 NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL);
2589
2590 /*
2591 * Bit mask controlling how hard we retry per-node allocations.
2592 * If we can not allocate the bit mask, do not attempt to allocate
2593 * the requested huge pages.
2594 */
2595 if (node_alloc_noretry)
2596 nodes_clear(*node_alloc_noretry);
2597 else
2598 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002600 spin_lock(&hugetlb_lock);
2601
2602 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002603 * Check for a node specific request.
2604 * Changing node specific huge page count may require a corresponding
2605 * change to the global count. In any case, the passed node mask
2606 * (nodes_allowed) will restrict alloc/free to the specified node.
2607 */
2608 if (nid != NUMA_NO_NODE) {
2609 unsigned long old_count = count;
2610
2611 count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
2612 /*
2613 * User may have specified a large count value which caused the
2614 * above calculation to overflow. In this case, they wanted
2615 * to allocate as many huge pages as possible. Set count to
2616 * largest possible value to align with their intention.
2617 */
2618 if (count < old_count)
2619 count = ULONG_MAX;
2620 }
2621
2622 /*
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002623 * Gigantic pages runtime allocation depend on the capability for large
2624 * page range allocation.
2625 * If the system does not provide this feature, return an error when
2626 * the user tries to allocate gigantic pages but let the user free the
2627 * boottime allocated gigantic pages.
2628 */
2629 if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) {
2630 if (count > persistent_huge_pages(h)) {
2631 spin_unlock(&hugetlb_lock);
Mike Kravetzf60858f2019-09-23 15:37:35 -07002632 NODEMASK_FREE(node_alloc_noretry);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002633 return -EINVAL;
2634 }
2635 /* Fall through to decrease pool */
2636 }
Andi Kleenaa888a72008-07-23 21:27:47 -07002637
Adam Litke7893d1d2007-10-16 01:26:18 -07002638 /*
2639 * Increase the pool size
2640 * First take pages out of surplus state. Then make up the
2641 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002642 *
Michal Hocko0c397da2018-01-31 16:20:56 -08002643 * We might race with alloc_surplus_huge_page() here and be unable
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002644 * to convert a surplus huge page to a normal huge page. That is
2645 * not critical, though, it just means the overall size of the
2646 * pool might be one hugepage larger than it needs to be, but
2647 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -07002648 */
Andi Kleena5516432008-07-23 21:27:41 -07002649 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002650 if (!adjust_pool_surplus(h, nodes_allowed, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -07002651 break;
2652 }
2653
Andi Kleena5516432008-07-23 21:27:41 -07002654 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -07002655 /*
2656 * If this allocation races such that we no longer need the
2657 * page, free_huge_page will handle it by freeing the page
2658 * and reducing the surplus.
2659 */
2660 spin_unlock(&hugetlb_lock);
Jia He649920c2016-08-02 14:02:31 -07002661
2662 /* yield cpu to avoid soft lockup */
2663 cond_resched();
2664
Mike Kravetzf60858f2019-09-23 15:37:35 -07002665 ret = alloc_pool_huge_page(h, nodes_allowed,
2666 node_alloc_noretry);
Adam Litke7893d1d2007-10-16 01:26:18 -07002667 spin_lock(&hugetlb_lock);
2668 if (!ret)
2669 goto out;
2670
Mel Gorman536240f22009-12-14 17:59:56 -08002671 /* Bail for signals. Probably ctrl-c from user */
2672 if (signal_pending(current))
2673 goto out;
Adam Litke7893d1d2007-10-16 01:26:18 -07002674 }
Adam Litke7893d1d2007-10-16 01:26:18 -07002675
2676 /*
2677 * Decrease the pool size
2678 * First return free pages to the buddy allocator (being careful
2679 * to keep enough around to satisfy reservations). Then place
2680 * pages into surplus state as needed so the pool will shrink
2681 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002682 *
2683 * By placing pages into the surplus state independent of the
2684 * overcommit value, we are allowing the surplus pool size to
2685 * exceed overcommit. There are few sane options here. Since
Michal Hocko0c397da2018-01-31 16:20:56 -08002686 * alloc_surplus_huge_page() is checking the global counter,
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002687 * though, we'll note that we're not allowed to exceed surplus
2688 * and won't grow the pool anywhere else. Not until one of the
2689 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07002690 */
Andi Kleena5516432008-07-23 21:27:41 -07002691 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07002692 min_count = max(count, min_count);
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002693 try_to_free_low(h, min_count, nodes_allowed);
Andi Kleena5516432008-07-23 21:27:41 -07002694 while (min_count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002695 if (!free_pool_huge_page(h, nodes_allowed, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 break;
Mizuma, Masayoshi55f67142014-04-07 15:37:54 -07002697 cond_resched_lock(&hugetlb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Andi Kleena5516432008-07-23 21:27:41 -07002699 while (count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08002700 if (!adjust_pool_surplus(h, nodes_allowed, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07002701 break;
2702 }
2703out:
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002704 h->max_huge_pages = persistent_huge_pages(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 spin_unlock(&hugetlb_lock);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002706
Mike Kravetzf60858f2019-09-23 15:37:35 -07002707 NODEMASK_FREE(node_alloc_noretry);
2708
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002712#define HSTATE_ATTR_RO(_name) \
2713 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
2714
2715#define HSTATE_ATTR(_name) \
2716 static struct kobj_attribute _name##_attr = \
2717 __ATTR(_name, 0644, _name##_show, _name##_store)
2718
2719static struct kobject *hugepages_kobj;
2720static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
2721
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002722static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
2723
2724static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002725{
2726 int i;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002727
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002728 for (i = 0; i < HUGE_MAX_HSTATE; i++)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002729 if (hstate_kobjs[i] == kobj) {
2730 if (nidp)
2731 *nidp = NUMA_NO_NODE;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002732 return &hstates[i];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002733 }
2734
2735 return kobj_to_node_hstate(kobj, nidp);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002736}
2737
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002738static ssize_t nr_hugepages_show_common(struct kobject *kobj,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002739 struct kobj_attribute *attr, char *buf)
2740{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002741 struct hstate *h;
2742 unsigned long nr_huge_pages;
2743 int nid;
2744
2745 h = kobj_to_hstate(kobj, &nid);
2746 if (nid == NUMA_NO_NODE)
2747 nr_huge_pages = h->nr_huge_pages;
2748 else
2749 nr_huge_pages = h->nr_huge_pages_node[nid];
2750
2751 return sprintf(buf, "%lu\n", nr_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002752}
Eric B Munsonadbe8722011-01-13 15:47:27 -08002753
David Rientjes238d3c12014-08-06 16:06:51 -07002754static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
2755 struct hstate *h, int nid,
2756 unsigned long count, size_t len)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002757{
2758 int err;
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002759 nodemask_t nodes_allowed, *n_mask;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002760
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002761 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
2762 return -EINVAL;
Eric B Munsonadbe8722011-01-13 15:47:27 -08002763
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002764 if (nid == NUMA_NO_NODE) {
2765 /*
2766 * global hstate attribute
2767 */
2768 if (!(obey_mempolicy &&
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002769 init_nodemask_of_mempolicy(&nodes_allowed)))
2770 n_mask = &node_states[N_MEMORY];
2771 else
2772 n_mask = &nodes_allowed;
2773 } else {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002774 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002775 * Node specific request. count adjustment happens in
2776 * set_max_huge_pages() after acquiring hugetlb_lock.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002777 */
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002778 init_nodemask_of_node(&nodes_allowed, nid);
2779 n_mask = &nodes_allowed;
Mike Kravetzfd875dc2019-05-13 17:19:20 -07002780 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002781
Oscar Salvador2d0adf72019-05-13 17:19:23 -07002782 err = set_max_huge_pages(h, count, nid, n_mask);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002783
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07002784 return err ? err : len;
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002785}
2786
David Rientjes238d3c12014-08-06 16:06:51 -07002787static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
2788 struct kobject *kobj, const char *buf,
2789 size_t len)
2790{
2791 struct hstate *h;
2792 unsigned long count;
2793 int nid;
2794 int err;
2795
2796 err = kstrtoul(buf, 10, &count);
2797 if (err)
2798 return err;
2799
2800 h = kobj_to_hstate(kobj, &nid);
2801 return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
2802}
2803
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002804static ssize_t nr_hugepages_show(struct kobject *kobj,
2805 struct kobj_attribute *attr, char *buf)
2806{
2807 return nr_hugepages_show_common(kobj, attr, buf);
2808}
2809
2810static ssize_t nr_hugepages_store(struct kobject *kobj,
2811 struct kobj_attribute *attr, const char *buf, size_t len)
2812{
David Rientjes238d3c12014-08-06 16:06:51 -07002813 return nr_hugepages_store_common(false, kobj, buf, len);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002814}
2815HSTATE_ATTR(nr_hugepages);
2816
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002817#ifdef CONFIG_NUMA
2818
2819/*
2820 * hstate attribute for optionally mempolicy-based constraint on persistent
2821 * huge page alloc/free.
2822 */
2823static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
2824 struct kobj_attribute *attr, char *buf)
2825{
2826 return nr_hugepages_show_common(kobj, attr, buf);
2827}
2828
2829static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
2830 struct kobj_attribute *attr, const char *buf, size_t len)
2831{
David Rientjes238d3c12014-08-06 16:06:51 -07002832 return nr_hugepages_store_common(true, kobj, buf, len);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002833}
2834HSTATE_ATTR(nr_hugepages_mempolicy);
2835#endif
2836
2837
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002838static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
2839 struct kobj_attribute *attr, char *buf)
2840{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002841 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002842 return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
2843}
Eric B Munsonadbe8722011-01-13 15:47:27 -08002844
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002845static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
2846 struct kobj_attribute *attr, const char *buf, size_t count)
2847{
2848 int err;
2849 unsigned long input;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002850 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002851
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002852 if (hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08002853 return -EINVAL;
2854
Jingoo Han3dbb95f2013-09-11 14:20:25 -07002855 err = kstrtoul(buf, 10, &input);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002856 if (err)
Eric B Munson73ae31e2011-01-13 15:47:28 -08002857 return err;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002858
2859 spin_lock(&hugetlb_lock);
2860 h->nr_overcommit_huge_pages = input;
2861 spin_unlock(&hugetlb_lock);
2862
2863 return count;
2864}
2865HSTATE_ATTR(nr_overcommit_hugepages);
2866
2867static ssize_t free_hugepages_show(struct kobject *kobj,
2868 struct kobj_attribute *attr, char *buf)
2869{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002870 struct hstate *h;
2871 unsigned long free_huge_pages;
2872 int nid;
2873
2874 h = kobj_to_hstate(kobj, &nid);
2875 if (nid == NUMA_NO_NODE)
2876 free_huge_pages = h->free_huge_pages;
2877 else
2878 free_huge_pages = h->free_huge_pages_node[nid];
2879
2880 return sprintf(buf, "%lu\n", free_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002881}
2882HSTATE_ATTR_RO(free_hugepages);
2883
2884static ssize_t resv_hugepages_show(struct kobject *kobj,
2885 struct kobj_attribute *attr, char *buf)
2886{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002887 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002888 return sprintf(buf, "%lu\n", h->resv_huge_pages);
2889}
2890HSTATE_ATTR_RO(resv_hugepages);
2891
2892static ssize_t surplus_hugepages_show(struct kobject *kobj,
2893 struct kobj_attribute *attr, char *buf)
2894{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002895 struct hstate *h;
2896 unsigned long surplus_huge_pages;
2897 int nid;
2898
2899 h = kobj_to_hstate(kobj, &nid);
2900 if (nid == NUMA_NO_NODE)
2901 surplus_huge_pages = h->surplus_huge_pages;
2902 else
2903 surplus_huge_pages = h->surplus_huge_pages_node[nid];
2904
2905 return sprintf(buf, "%lu\n", surplus_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002906}
2907HSTATE_ATTR_RO(surplus_hugepages);
2908
2909static struct attribute *hstate_attrs[] = {
2910 &nr_hugepages_attr.attr,
2911 &nr_overcommit_hugepages_attr.attr,
2912 &free_hugepages_attr.attr,
2913 &resv_hugepages_attr.attr,
2914 &surplus_hugepages_attr.attr,
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002915#ifdef CONFIG_NUMA
2916 &nr_hugepages_mempolicy_attr.attr,
2917#endif
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002918 NULL,
2919};
2920
Arvind Yadav67e5ed92017-09-06 16:22:06 -07002921static const struct attribute_group hstate_attr_group = {
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002922 .attrs = hstate_attrs,
2923};
2924
Jeff Mahoney094e9532010-02-02 13:44:14 -08002925static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
2926 struct kobject **hstate_kobjs,
Arvind Yadav67e5ed92017-09-06 16:22:06 -07002927 const struct attribute_group *hstate_attr_group)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002928{
2929 int retval;
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002930 int hi = hstate_index(h);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002931
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002932 hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
2933 if (!hstate_kobjs[hi])
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002934 return -ENOMEM;
2935
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002936 retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002937 if (retval)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002938 kobject_put(hstate_kobjs[hi]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002939
2940 return retval;
2941}
2942
2943static void __init hugetlb_sysfs_init(void)
2944{
2945 struct hstate *h;
2946 int err;
2947
2948 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
2949 if (!hugepages_kobj)
2950 return;
2951
2952 for_each_hstate(h) {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002953 err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
2954 hstate_kobjs, &hstate_attr_group);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002955 if (err)
Andrew Mortonffb22af2013-02-22 16:32:08 -08002956 pr_err("Hugetlb: Unable to add hstate %s", h->name);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002957 }
2958}
2959
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002960#ifdef CONFIG_NUMA
2961
2962/*
2963 * node_hstate/s - associate per node hstate attributes, via their kobjects,
Kay Sievers10fbcf42011-12-21 14:48:43 -08002964 * with node devices in node_devices[] using a parallel array. The array
2965 * index of a node device or _hstate == node id.
2966 * This is here to avoid any static dependency of the node device driver, in
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002967 * the base kernel, on the hugetlb module.
2968 */
2969struct node_hstate {
2970 struct kobject *hugepages_kobj;
2971 struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
2972};
Alexander Kuleshovb4e289a2015-11-05 18:50:14 -08002973static struct node_hstate node_hstates[MAX_NUMNODES];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002974
2975/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002976 * A subset of global hstate attributes for node devices
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002977 */
2978static struct attribute *per_node_hstate_attrs[] = {
2979 &nr_hugepages_attr.attr,
2980 &free_hugepages_attr.attr,
2981 &surplus_hugepages_attr.attr,
2982 NULL,
2983};
2984
Arvind Yadav67e5ed92017-09-06 16:22:06 -07002985static const struct attribute_group per_node_hstate_attr_group = {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002986 .attrs = per_node_hstate_attrs,
2987};
2988
2989/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002990 * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08002991 * Returns node id via non-NULL nidp.
2992 */
2993static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
2994{
2995 int nid;
2996
2997 for (nid = 0; nid < nr_node_ids; nid++) {
2998 struct node_hstate *nhs = &node_hstates[nid];
2999 int i;
3000 for (i = 0; i < HUGE_MAX_HSTATE; i++)
3001 if (nhs->hstate_kobjs[i] == kobj) {
3002 if (nidp)
3003 *nidp = nid;
3004 return &hstates[i];
3005 }
3006 }
3007
3008 BUG();
3009 return NULL;
3010}
3011
3012/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08003013 * Unregister hstate attributes from a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003014 * No-op if no hstate attributes attached.
3015 */
Claudiu Ghioc3cd8b442013-03-04 12:46:15 +02003016static void hugetlb_unregister_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003017{
3018 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08003019 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003020
3021 if (!nhs->hugepages_kobj)
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08003022 return; /* no hstate attributes */
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003023
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07003024 for_each_hstate(h) {
3025 int idx = hstate_index(h);
3026 if (nhs->hstate_kobjs[idx]) {
3027 kobject_put(nhs->hstate_kobjs[idx]);
3028 nhs->hstate_kobjs[idx] = NULL;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003029 }
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07003030 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003031
3032 kobject_put(nhs->hugepages_kobj);
3033 nhs->hugepages_kobj = NULL;
3034}
3035
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003036
3037/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08003038 * Register hstate attributes for a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003039 * No-op if attributes already registered.
3040 */
Claudiu Ghioc3cd8b442013-03-04 12:46:15 +02003041static void hugetlb_register_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003042{
3043 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08003044 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003045 int err;
3046
3047 if (nhs->hugepages_kobj)
3048 return; /* already allocated */
3049
3050 nhs->hugepages_kobj = kobject_create_and_add("hugepages",
Kay Sievers10fbcf42011-12-21 14:48:43 -08003051 &node->dev.kobj);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003052 if (!nhs->hugepages_kobj)
3053 return;
3054
3055 for_each_hstate(h) {
3056 err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
3057 nhs->hstate_kobjs,
3058 &per_node_hstate_attr_group);
3059 if (err) {
Andrew Mortonffb22af2013-02-22 16:32:08 -08003060 pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
3061 h->name, node->dev.id);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003062 hugetlb_unregister_node(node);
3063 break;
3064 }
3065 }
3066}
3067
3068/*
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08003069 * hugetlb init time: register hstate attributes for all registered node
Kay Sievers10fbcf42011-12-21 14:48:43 -08003070 * devices of nodes that have memory. All on-line nodes should have
3071 * registered their associated device by this time.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003072 */
Luiz Capitulino7d9ca002014-12-12 16:55:24 -08003073static void __init hugetlb_register_all_nodes(void)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003074{
3075 int nid;
3076
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08003077 for_each_node_state(nid, N_MEMORY) {
Wen Congyang87327942012-12-11 16:00:56 -08003078 struct node *node = node_devices[nid];
Kay Sievers10fbcf42011-12-21 14:48:43 -08003079 if (node->dev.id == nid)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003080 hugetlb_register_node(node);
3081 }
3082
3083 /*
Kay Sievers10fbcf42011-12-21 14:48:43 -08003084 * Let the node device driver know we're here so it can
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003085 * [un]register hstate attributes on node hotplug.
3086 */
3087 register_hugetlbfs_with_node(hugetlb_register_node,
3088 hugetlb_unregister_node);
3089}
3090#else /* !CONFIG_NUMA */
3091
3092static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
3093{
3094 BUG();
3095 if (nidp)
3096 *nidp = -1;
3097 return NULL;
3098}
3099
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003100static void hugetlb_register_all_nodes(void) { }
3101
3102#endif
3103
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003104static int __init hugetlb_init(void)
3105{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003106 int i;
3107
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003108 if (!hugepages_supported())
Benjamin Herrenschmidt0ef89d22008-07-31 00:07:30 -07003109 return 0;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003110
Nick Piggine11bfbf2008-07-23 21:27:52 -07003111 if (!size_to_hstate(default_hstate_size)) {
Liam R. Howlettd715cf82017-07-10 15:48:15 -07003112 if (default_hstate_size != 0) {
3113 pr_err("HugeTLB: unsupported default_hugepagesz %lu. Reverting to %lu\n",
3114 default_hstate_size, HPAGE_SIZE);
3115 }
3116
Nick Piggine11bfbf2008-07-23 21:27:52 -07003117 default_hstate_size = HPAGE_SIZE;
3118 if (!size_to_hstate(default_hstate_size))
3119 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003120 }
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07003121 default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
Vaishali Thakkarf8b74812016-02-17 13:11:26 -08003122 if (default_hstate_max_huge_pages) {
3123 if (!default_hstate.max_huge_pages)
3124 default_hstate.max_huge_pages = default_hstate_max_huge_pages;
3125 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003126
3127 hugetlb_init_hstates();
Andi Kleenaa888a72008-07-23 21:27:47 -07003128 gather_bootmem_prealloc();
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003129 report_hugepages();
3130
3131 hugetlb_sysfs_init();
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003132 hugetlb_register_all_nodes();
Jianguo Wu7179e7b2012-12-18 14:23:19 -08003133 hugetlb_cgroup_file_init();
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003134
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003135#ifdef CONFIG_SMP
3136 num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
3137#else
3138 num_fault_mutexes = 1;
3139#endif
Mike Kravetzc672c7f2015-09-08 15:01:35 -07003140 hugetlb_fault_mutex_table =
Kees Cook6da2ec52018-06-12 13:55:00 -07003141 kmalloc_array(num_fault_mutexes, sizeof(struct mutex),
3142 GFP_KERNEL);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07003143 BUG_ON(!hugetlb_fault_mutex_table);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003144
3145 for (i = 0; i < num_fault_mutexes; i++)
Mike Kravetzc672c7f2015-09-08 15:01:35 -07003146 mutex_init(&hugetlb_fault_mutex_table[i]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003147 return 0;
3148}
Paul Gortmaker3e89e1c2016-01-14 15:21:52 -08003149subsys_initcall(hugetlb_init);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003150
3151/* Should be called on processing a hugepagesz=... option */
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07003152void __init hugetlb_bad_size(void)
3153{
3154 parsed_valid_hugepagesz = false;
3155}
3156
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08003157void __init hugetlb_add_hstate(unsigned int order)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003158{
3159 struct hstate *h;
Andi Kleen8faa8b02008-07-23 21:27:48 -07003160 unsigned long i;
3161
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003162 if (size_to_hstate(PAGE_SIZE << order)) {
Joe Perches598d8092016-03-17 14:19:44 -07003163 pr_warn("hugepagesz= specified twice, ignoring\n");
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003164 return;
3165 }
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07003166 BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003167 BUG_ON(order == 0);
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07003168 h = &hstates[hugetlb_max_hstate++];
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003169 h->order = order;
3170 h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
Andi Kleen8faa8b02008-07-23 21:27:48 -07003171 h->nr_huge_pages = 0;
3172 h->free_huge_pages = 0;
3173 for (i = 0; i < MAX_NUMNODES; ++i)
3174 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07003175 INIT_LIST_HEAD(&h->hugepage_activelist);
Andrew Morton54f18d32016-05-19 17:11:40 -07003176 h->next_nid_to_alloc = first_memory_node;
3177 h->next_nid_to_free = first_memory_node;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003178 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
3179 huge_page_size(h)/1024);
Andi Kleen8faa8b02008-07-23 21:27:48 -07003180
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003181 parsed_hstate = h;
3182}
3183
Nick Piggine11bfbf2008-07-23 21:27:52 -07003184static int __init hugetlb_nrpages_setup(char *s)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003185{
3186 unsigned long *mhp;
Andi Kleen8faa8b02008-07-23 21:27:48 -07003187 static unsigned long *last_mhp;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003188
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07003189 if (!parsed_valid_hugepagesz) {
3190 pr_warn("hugepages = %s preceded by "
3191 "an unsupported hugepagesz, ignoring\n", s);
3192 parsed_valid_hugepagesz = true;
3193 return 1;
3194 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003195 /*
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07003196 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003197 * so this hugepages= parameter goes to the "default hstate".
3198 */
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07003199 else if (!hugetlb_max_hstate)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003200 mhp = &default_hstate_max_huge_pages;
3201 else
3202 mhp = &parsed_hstate->max_huge_pages;
3203
Andi Kleen8faa8b02008-07-23 21:27:48 -07003204 if (mhp == last_mhp) {
Joe Perches598d8092016-03-17 14:19:44 -07003205 pr_warn("hugepages= specified twice without interleaving hugepagesz=, ignoring\n");
Andi Kleen8faa8b02008-07-23 21:27:48 -07003206 return 1;
3207 }
3208
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003209 if (sscanf(s, "%lu", mhp) <= 0)
3210 *mhp = 0;
3211
Andi Kleen8faa8b02008-07-23 21:27:48 -07003212 /*
3213 * Global state is always initialized later in hugetlb_init.
3214 * But we need to allocate >= MAX_ORDER hstates here early to still
3215 * use the bootmem allocator.
3216 */
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07003217 if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
Andi Kleen8faa8b02008-07-23 21:27:48 -07003218 hugetlb_hstate_alloc_pages(parsed_hstate);
3219
3220 last_mhp = mhp;
3221
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003222 return 1;
3223}
Nick Piggine11bfbf2008-07-23 21:27:52 -07003224__setup("hugepages=", hugetlb_nrpages_setup);
3225
3226static int __init hugetlb_default_setup(char *s)
3227{
3228 default_hstate_size = memparse(s, &s);
3229 return 1;
3230}
3231__setup("default_hugepagesz=", hugetlb_default_setup);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003232
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07003233static unsigned int cpuset_mems_nr(unsigned int *array)
3234{
3235 int node;
3236 unsigned int nr = 0;
3237
3238 for_each_node_mask(node, cpuset_current_mems_allowed)
3239 nr += array[node];
3240
3241 return nr;
3242}
3243
3244#ifdef CONFIG_SYSCTL
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003245static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
3246 struct ctl_table *table, int write,
3247 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Andi Kleene5ff2152008-07-23 21:27:42 -07003249 struct hstate *h = &default_hstate;
David Rientjes238d3c12014-08-06 16:06:51 -07003250 unsigned long tmp = h->max_huge_pages;
Michal Hocko08d4a242011-01-13 15:47:26 -08003251 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07003252
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003253 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08003254 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003255
Andi Kleene5ff2152008-07-23 21:27:42 -07003256 table->data = &tmp;
3257 table->maxlen = sizeof(unsigned long);
Michal Hocko08d4a242011-01-13 15:47:26 -08003258 ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
3259 if (ret)
3260 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07003261
David Rientjes238d3c12014-08-06 16:06:51 -07003262 if (write)
3263 ret = __nr_hugepages_store_common(obey_mempolicy, h,
3264 NUMA_NO_NODE, tmp, *length);
Michal Hocko08d4a242011-01-13 15:47:26 -08003265out:
3266 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
Mel Gorman396faf02007-07-17 04:03:13 -07003268
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003269int hugetlb_sysctl_handler(struct ctl_table *table, int write,
3270 void __user *buffer, size_t *length, loff_t *ppos)
3271{
3272
3273 return hugetlb_sysctl_handler_common(false, table, write,
3274 buffer, length, ppos);
3275}
3276
3277#ifdef CONFIG_NUMA
3278int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
3279 void __user *buffer, size_t *length, loff_t *ppos)
3280{
3281 return hugetlb_sysctl_handler_common(true, table, write,
3282 buffer, length, ppos);
3283}
3284#endif /* CONFIG_NUMA */
3285
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08003286int hugetlb_overcommit_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003287 void __user *buffer,
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08003288 size_t *length, loff_t *ppos)
3289{
Andi Kleena5516432008-07-23 21:27:41 -07003290 struct hstate *h = &default_hstate;
Andi Kleene5ff2152008-07-23 21:27:42 -07003291 unsigned long tmp;
Michal Hocko08d4a242011-01-13 15:47:26 -08003292 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07003293
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003294 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08003295 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003296
Petr Holasekc033a932011-03-22 16:33:05 -07003297 tmp = h->nr_overcommit_huge_pages;
Andi Kleene5ff2152008-07-23 21:27:42 -07003298
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003299 if (write && hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08003300 return -EINVAL;
3301
Andi Kleene5ff2152008-07-23 21:27:42 -07003302 table->data = &tmp;
3303 table->maxlen = sizeof(unsigned long);
Michal Hocko08d4a242011-01-13 15:47:26 -08003304 ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
3305 if (ret)
3306 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07003307
3308 if (write) {
3309 spin_lock(&hugetlb_lock);
3310 h->nr_overcommit_huge_pages = tmp;
3311 spin_unlock(&hugetlb_lock);
3312 }
Michal Hocko08d4a242011-01-13 15:47:26 -08003313out:
3314 return ret;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08003315}
3316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317#endif /* CONFIG_SYSCTL */
3318
Alexey Dobriyane1759c22008-10-15 23:50:22 +04003319void hugetlb_report_meminfo(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08003321 struct hstate *h;
3322 unsigned long total = 0;
3323
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003324 if (!hugepages_supported())
3325 return;
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08003326
3327 for_each_hstate(h) {
3328 unsigned long count = h->nr_huge_pages;
3329
3330 total += (PAGE_SIZE << huge_page_order(h)) * count;
3331
3332 if (h == &default_hstate)
3333 seq_printf(m,
3334 "HugePages_Total: %5lu\n"
3335 "HugePages_Free: %5lu\n"
3336 "HugePages_Rsvd: %5lu\n"
3337 "HugePages_Surp: %5lu\n"
3338 "Hugepagesize: %8lu kB\n",
3339 count,
3340 h->free_huge_pages,
3341 h->resv_huge_pages,
3342 h->surplus_huge_pages,
3343 (PAGE_SIZE << huge_page_order(h)) / 1024);
3344 }
3345
3346 seq_printf(m, "Hugetlb: %8lu kB\n", total / 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
3349int hugetlb_report_node_meminfo(int nid, char *buf)
3350{
Andi Kleena5516432008-07-23 21:27:41 -07003351 struct hstate *h = &default_hstate;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003352 if (!hugepages_supported())
3353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 return sprintf(buf,
3355 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07003356 "Node %d HugePages_Free: %5u\n"
3357 "Node %d HugePages_Surp: %5u\n",
Andi Kleena5516432008-07-23 21:27:41 -07003358 nid, h->nr_huge_pages_node[nid],
3359 nid, h->free_huge_pages_node[nid],
3360 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
David Rientjes949f7ec2013-04-29 15:07:48 -07003363void hugetlb_show_meminfo(void)
3364{
3365 struct hstate *h;
3366 int nid;
3367
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07003368 if (!hugepages_supported())
3369 return;
3370
David Rientjes949f7ec2013-04-29 15:07:48 -07003371 for_each_node_state(nid, N_MEMORY)
3372 for_each_hstate(h)
3373 pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
3374 nid,
3375 h->nr_huge_pages_node[nid],
3376 h->free_huge_pages_node[nid],
3377 h->surplus_huge_pages_node[nid],
3378 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
3379}
3380
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003381void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
3382{
3383 seq_printf(m, "HugetlbPages:\t%8lu kB\n",
3384 atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10));
3385}
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
3388unsigned long hugetlb_total_pages(void)
3389{
Wanpeng Lid0028582013-03-22 15:04:40 -07003390 struct hstate *h;
3391 unsigned long nr_total_pages = 0;
3392
3393 for_each_hstate(h)
3394 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
3395 return nr_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Andi Kleena5516432008-07-23 21:27:41 -07003398static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003399{
3400 int ret = -ENOMEM;
3401
3402 spin_lock(&hugetlb_lock);
3403 /*
3404 * When cpuset is configured, it breaks the strict hugetlb page
3405 * reservation as the accounting is done on a global variable. Such
3406 * reservation is completely rubbish in the presence of cpuset because
3407 * the reservation is not checked against page availability for the
3408 * current cpuset. Application can still potentially OOM'ed by kernel
3409 * with lack of free htlb page in cpuset that the task is in.
3410 * Attempt to enforce strict accounting with cpuset is almost
3411 * impossible (or too ugly) because cpuset is too fluid that
3412 * task or memory node can be dynamically moved between cpusets.
3413 *
3414 * The change of semantics for shared hugetlb mapping with cpuset is
3415 * undesirable. However, in order to preserve some of the semantics,
3416 * we fall back to check against current free page availability as
3417 * a best attempt and hopefully to minimize the impact of changing
3418 * semantics that cpuset has.
3419 */
3420 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07003421 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003422 goto out;
3423
Andi Kleena5516432008-07-23 21:27:41 -07003424 if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
3425 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003426 goto out;
3427 }
3428 }
3429
3430 ret = 0;
3431 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07003432 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07003433
3434out:
3435 spin_unlock(&hugetlb_lock);
3436 return ret;
3437}
3438
Andy Whitcroft84afd992008-07-23 21:27:32 -07003439static void hugetlb_vm_op_open(struct vm_area_struct *vma)
3440{
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07003441 struct resv_map *resv = vma_resv_map(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003442
3443 /*
3444 * This new VMA should share its siblings reservation map if present.
3445 * The VMA will only ever have a valid reservation map pointer where
3446 * it is being copied for another still existing VMA. As that VMA
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003447 * has a reference to the reservation map it cannot disappear until
Andy Whitcroft84afd992008-07-23 21:27:32 -07003448 * after this open call completes. It is therefore safe to take a
3449 * new reference here without additional locking.
3450 */
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003451 if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07003452 kref_get(&resv->refs);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003453}
3454
Mel Gormana1e78772008-07-23 21:27:23 -07003455static void hugetlb_vm_op_close(struct vm_area_struct *vma)
3456{
Andi Kleena5516432008-07-23 21:27:41 -07003457 struct hstate *h = hstate_vma(vma);
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07003458 struct resv_map *resv = vma_resv_map(vma);
David Gibson90481622012-03-21 16:34:12 -07003459 struct hugepage_subpool *spool = subpool_vma(vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003460 unsigned long reserve, start, end;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003461 long gbl_reserve;
Andy Whitcroft84afd992008-07-23 21:27:32 -07003462
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003463 if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
3464 return;
Andy Whitcroft84afd992008-07-23 21:27:32 -07003465
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003466 start = vma_hugecache_offset(h, vma, vma->vm_start);
3467 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003468
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003469 reserve = (end - start) - region_count(resv, start, end);
Mina Almasrye9fe92a2020-04-01 21:11:21 -07003470 hugetlb_cgroup_uncharge_counter(resv, start, end);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003471 if (reserve) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003472 /*
3473 * Decrement reserve counts. The global reserve count may be
3474 * adjusted if the subpool has a minimum size.
3475 */
3476 gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
3477 hugetlb_acct_memory(h, -gbl_reserve);
Andy Whitcroft84afd992008-07-23 21:27:32 -07003478 }
Mina Almasrye9fe92a2020-04-01 21:11:21 -07003479
3480 kref_put(&resv->refs, resv_map_release);
Mel Gormana1e78772008-07-23 21:27:23 -07003481}
3482
Dan Williams31383c62017-11-29 16:10:28 -08003483static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
3484{
3485 if (addr & ~(huge_page_mask(hstate_vma(vma))))
3486 return -EINVAL;
3487 return 0;
3488}
3489
Dan Williams05ea8862018-04-05 16:24:25 -07003490static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
3491{
3492 struct hstate *hstate = hstate_vma(vma);
3493
3494 return 1UL << huge_page_shift(hstate);
3495}
3496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497/*
3498 * We cannot handle pagefaults against hugetlb pages at all. They cause
3499 * handle_mm_fault() to try to instantiate regular-sized pages in the
3500 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
3501 * this far.
3502 */
Souptick Joarderb3ec9f32018-06-07 17:08:04 -07003503static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
3505 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07003506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
Jane Chueec36362018-08-02 15:36:05 -07003509/*
3510 * When a new function is introduced to vm_operations_struct and added
3511 * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
3512 * This is because under System V memory model, mappings created via
3513 * shmget/shmat with "huge page" specified are backed by hugetlbfs files,
3514 * their original vm_ops are overwritten with shm_vm_ops.
3515 */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003516const struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07003517 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07003518 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07003519 .close = hugetlb_vm_op_close,
Dan Williams31383c62017-11-29 16:10:28 -08003520 .split = hugetlb_vm_op_split,
Dan Williams05ea8862018-04-05 16:24:25 -07003521 .pagesize = hugetlb_vm_op_pagesize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522};
3523
David Gibson1e8f8892006-01-06 00:10:44 -08003524static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
3525 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07003526{
3527 pte_t entry;
3528
David Gibson1e8f8892006-01-06 00:10:44 -08003529 if (writable) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07003530 entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
3531 vma->vm_page_prot)));
David Gibson63551ae2005-06-21 17:14:44 -07003532 } else {
Gerald Schaefer106c9922013-04-29 15:07:23 -07003533 entry = huge_pte_wrprotect(mk_huge_pte(page,
3534 vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07003535 }
3536 entry = pte_mkyoung(entry);
3537 entry = pte_mkhuge(entry);
Chris Metcalfd9ed9fa2012-04-01 14:01:34 -04003538 entry = arch_make_huge_pte(entry, vma, page, writable);
David Gibson63551ae2005-06-21 17:14:44 -07003539
3540 return entry;
3541}
3542
David Gibson1e8f8892006-01-06 00:10:44 -08003543static void set_huge_ptep_writable(struct vm_area_struct *vma,
3544 unsigned long address, pte_t *ptep)
3545{
3546 pte_t entry;
3547
Gerald Schaefer106c9922013-04-29 15:07:23 -07003548 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
Chris Forbes32f84522011-07-25 17:12:14 -07003549 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
Russell King4b3073e2009-12-18 16:40:18 +00003550 update_mmu_cache(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08003551}
3552
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003553bool is_hugetlb_entry_migration(pte_t pte)
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003554{
3555 swp_entry_t swp;
3556
3557 if (huge_pte_none(pte) || pte_present(pte))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003558 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003559 swp = pte_to_swp_entry(pte);
3560 if (non_swap_entry(swp) && is_migration_entry(swp))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003561 return true;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003562 else
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07003563 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003564}
3565
3566static int is_hugetlb_entry_hwpoisoned(pte_t pte)
3567{
3568 swp_entry_t swp;
3569
3570 if (huge_pte_none(pte) || pte_present(pte))
3571 return 0;
3572 swp = pte_to_swp_entry(pte);
3573 if (non_swap_entry(swp) && is_hwpoison_entry(swp))
3574 return 1;
3575 else
3576 return 0;
3577}
David Gibson1e8f8892006-01-06 00:10:44 -08003578
David Gibson63551ae2005-06-21 17:14:44 -07003579int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
3580 struct vm_area_struct *vma)
3581{
Mike Kravetz5e415402018-11-16 15:08:04 -08003582 pte_t *src_pte, *dst_pte, entry, dst_entry;
David Gibson63551ae2005-06-21 17:14:44 -07003583 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07003584 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08003585 int cow;
Andi Kleena5516432008-07-23 21:27:41 -07003586 struct hstate *h = hstate_vma(vma);
3587 unsigned long sz = huge_page_size(h);
Mike Kravetzc0d03812020-04-01 21:11:05 -07003588 struct address_space *mapping = vma->vm_file->f_mapping;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003589 struct mmu_notifier_range range;
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003590 int ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08003591
3592 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07003593
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003594 if (cow) {
Jérôme Glisse7269f992019-05-13 17:20:53 -07003595 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, src,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07003596 vma->vm_start,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003597 vma->vm_end);
3598 mmu_notifier_invalidate_range_start(&range);
Mike Kravetzc0d03812020-04-01 21:11:05 -07003599 } else {
3600 /*
3601 * For shared mappings i_mmap_rwsem must be held to call
3602 * huge_pte_alloc, otherwise the returned ptep could go
3603 * away if part of a shared pmd and another thread calls
3604 * huge_pmd_unshare.
3605 */
3606 i_mmap_lock_read(mapping);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003607 }
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003608
Andi Kleena5516432008-07-23 21:27:41 -07003609 for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003610 spinlock_t *src_ptl, *dst_ptl;
Punit Agrawal7868a202017-07-06 15:39:42 -07003611 src_pte = huge_pte_offset(src, addr, sz);
Hugh Dickinsc74df322005-10-29 18:16:23 -07003612 if (!src_pte)
3613 continue;
Andi Kleena5516432008-07-23 21:27:41 -07003614 dst_pte = huge_pte_alloc(dst, addr, sz);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003615 if (!dst_pte) {
3616 ret = -ENOMEM;
3617 break;
3618 }
Larry Woodmanc5c99422008-01-24 05:49:25 -08003619
Mike Kravetz5e415402018-11-16 15:08:04 -08003620 /*
3621 * If the pagetables are shared don't copy or take references.
3622 * dst_pte == src_pte is the common case of src/dest sharing.
3623 *
3624 * However, src could have 'unshared' and dst shares with
3625 * another vma. If dst_pte !none, this implies sharing.
3626 * Check here before taking page table lock, and once again
3627 * after taking the lock below.
3628 */
3629 dst_entry = huge_ptep_get(dst_pte);
3630 if ((dst_pte == src_pte) || !huge_pte_none(dst_entry))
Larry Woodmanc5c99422008-01-24 05:49:25 -08003631 continue;
3632
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003633 dst_ptl = huge_pte_lock(h, dst, dst_pte);
3634 src_ptl = huge_pte_lockptr(h, src, src_pte);
3635 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003636 entry = huge_ptep_get(src_pte);
Mike Kravetz5e415402018-11-16 15:08:04 -08003637 dst_entry = huge_ptep_get(dst_pte);
3638 if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) {
3639 /*
3640 * Skip if src entry none. Also, skip in the
3641 * unlikely case dst entry !none as this implies
3642 * sharing with another vma.
3643 */
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003644 ;
3645 } else if (unlikely(is_hugetlb_entry_migration(entry) ||
3646 is_hugetlb_entry_hwpoisoned(entry))) {
3647 swp_entry_t swp_entry = pte_to_swp_entry(entry);
3648
3649 if (is_write_migration_entry(swp_entry) && cow) {
3650 /*
3651 * COW mappings require pages in both
3652 * parent and child to be set to read.
3653 */
3654 make_migration_entry_read(&swp_entry);
3655 entry = swp_entry_to_pte(swp_entry);
Punit Agrawale5251fd2017-07-06 15:39:50 -07003656 set_huge_swap_pte_at(src, addr, src_pte,
3657 entry, sz);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003658 }
Punit Agrawale5251fd2017-07-06 15:39:50 -07003659 set_huge_swap_pte_at(dst, addr, dst_pte, entry, sz);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07003660 } else {
Joerg Roedel34ee6452014-11-13 13:46:09 +11003661 if (cow) {
Jérôme Glisse0f108512017-11-15 17:34:07 -08003662 /*
3663 * No need to notify as we are downgrading page
3664 * table protection not changing it to point
3665 * to a new page.
3666 *
Mike Rapoportad56b732018-03-21 21:22:47 +02003667 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08003668 */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07003669 huge_ptep_set_wrprotect(src, addr, src_pte);
Joerg Roedel34ee6452014-11-13 13:46:09 +11003670 }
Naoya Horiguchi0253d632014-07-23 14:00:19 -07003671 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07003672 ptepage = pte_page(entry);
3673 get_page(ptepage);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08003674 page_dup_rmap(ptepage, true);
Hugh Dickins1c598272005-10-19 21:23:43 -07003675 set_huge_pte_at(dst, addr, dst_pte, entry);
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003676 hugetlb_count_add(pages_per_huge_page(h), dst);
Hugh Dickins1c598272005-10-19 21:23:43 -07003677 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003678 spin_unlock(src_ptl);
3679 spin_unlock(dst_ptl);
David Gibson63551ae2005-06-21 17:14:44 -07003680 }
David Gibson63551ae2005-06-21 17:14:44 -07003681
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003682 if (cow)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003683 mmu_notifier_invalidate_range_end(&range);
Mike Kravetzc0d03812020-04-01 21:11:05 -07003684 else
3685 i_mmap_unlock_read(mapping);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08003686
3687 return ret;
David Gibson63551ae2005-06-21 17:14:44 -07003688}
3689
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003690void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
3691 unsigned long start, unsigned long end,
3692 struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07003693{
3694 struct mm_struct *mm = vma->vm_mm;
3695 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07003696 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07003697 pte_t pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003698 spinlock_t *ptl;
David Gibson63551ae2005-06-21 17:14:44 -07003699 struct page *page;
Andi Kleena5516432008-07-23 21:27:41 -07003700 struct hstate *h = hstate_vma(vma);
3701 unsigned long sz = huge_page_size(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003702 struct mmu_notifier_range range;
Andi Kleena5516432008-07-23 21:27:41 -07003703
David Gibson63551ae2005-06-21 17:14:44 -07003704 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07003705 BUG_ON(start & ~huge_page_mask(h));
3706 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07003707
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08003708 /*
3709 * This is a hugetlb vma, all the pte entries should point
3710 * to huge page.
3711 */
Peter Zijlstraed6a7932018-08-31 14:46:08 +02003712 tlb_change_page_size(tlb, sz);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003713 tlb_start_vma(tlb, vma);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003714
3715 /*
3716 * If sharing possible, alert mmu notifiers of worst case.
3717 */
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07003718 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, mm, start,
3719 end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003720 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
3721 mmu_notifier_invalidate_range_start(&range);
Hillf Danton569f48b82014-12-10 15:44:41 -08003722 address = start;
Hillf Danton569f48b82014-12-10 15:44:41 -08003723 for (; address < end; address += sz) {
Punit Agrawal7868a202017-07-06 15:39:42 -07003724 ptep = huge_pte_offset(mm, address, sz);
Adam Litke4c887262005-10-29 18:16:46 -07003725 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07003726 continue;
3727
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003728 ptl = huge_pte_lock(h, mm, ptep);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003729 if (huge_pmd_unshare(mm, &address, ptep)) {
3730 spin_unlock(ptl);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003731 /*
3732 * We just unmapped a page of PMDs by clearing a PUD.
3733 * The caller's TLB flush range should cover this area.
3734 */
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003735 continue;
3736 }
Chen, Kenneth W39dde652006-12-06 20:32:03 -08003737
Hillf Danton66293262012-03-23 15:01:48 -07003738 pte = huge_ptep_get(ptep);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003739 if (huge_pte_none(pte)) {
3740 spin_unlock(ptl);
3741 continue;
3742 }
Hillf Danton66293262012-03-23 15:01:48 -07003743
3744 /*
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08003745 * Migrating hugepage or HWPoisoned hugepage is already
3746 * unmapped and its refcount is dropped, so just clear pte here.
Hillf Danton66293262012-03-23 15:01:48 -07003747 */
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08003748 if (unlikely(!pte_present(pte))) {
Punit Agrawal9386fac2017-07-06 15:39:46 -07003749 huge_pte_clear(mm, address, ptep, sz);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003750 spin_unlock(ptl);
3751 continue;
Naoya Horiguchi8c4894c2012-12-12 13:52:28 -08003752 }
Hillf Danton66293262012-03-23 15:01:48 -07003753
3754 page = pte_page(pte);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003755 /*
3756 * If a reference page is supplied, it is because a specific
3757 * page is being unmapped, not a range. Ensure the page we
3758 * are about to unmap is the actual page of interest.
3759 */
3760 if (ref_page) {
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003761 if (page != ref_page) {
3762 spin_unlock(ptl);
3763 continue;
3764 }
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003765 /*
3766 * Mark the VMA as having unmapped its page so that
3767 * future faults in this VMA will fail rather than
3768 * looking like data was lost
3769 */
3770 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
3771 }
3772
David Gibsonc7546f82005-08-05 11:59:35 -07003773 pte = huge_ptep_get_and_clear(mm, address, ptep);
Aneesh Kumar K.Vb528e4b2016-12-12 16:42:37 -08003774 tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
Gerald Schaefer106c9922013-04-29 15:07:23 -07003775 if (huge_pte_dirty(pte))
Ken Chen6649a382007-02-08 14:20:27 -08003776 set_page_dirty(page);
Hillf Danton9e811302012-03-21 16:34:03 -07003777
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08003778 hugetlb_count_sub(pages_per_huge_page(h), mm);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08003779 page_remove_rmap(page, true);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003780
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003781 spin_unlock(ptl);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -07003782 tlb_remove_page_size(tlb, page, huge_page_size(h));
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07003783 /*
3784 * Bail out after unmapping reference page if supplied
3785 */
3786 if (ref_page)
3787 break;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07003788 }
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003789 mmu_notifier_invalidate_range_end(&range);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003790 tlb_end_vma(tlb, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791}
David Gibson63551ae2005-06-21 17:14:44 -07003792
Mel Gormand8333522012-07-31 16:46:20 -07003793void __unmap_hugepage_range_final(struct mmu_gather *tlb,
3794 struct vm_area_struct *vma, unsigned long start,
3795 unsigned long end, struct page *ref_page)
3796{
3797 __unmap_hugepage_range(tlb, vma, start, end, ref_page);
3798
3799 /*
3800 * Clear this flag so that x86's huge_pmd_share page_table_shareable
3801 * test will fail on a vma being torn down, and not grab a page table
3802 * on its way out. We're lucky that the flag has such an appropriate
3803 * name, and can in fact be safely cleared here. We could clear it
3804 * before the __unmap_hugepage_range above, but all that's necessary
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003805 * is to clear it before releasing the i_mmap_rwsem. This works
Mel Gormand8333522012-07-31 16:46:20 -07003806 * because in the context this is called, the VMA is about to be
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003807 * destroyed and the i_mmap_rwsem is held.
Mel Gormand8333522012-07-31 16:46:20 -07003808 */
3809 vma->vm_flags &= ~VM_MAYSHARE;
3810}
3811
Chen, Kenneth W502717f2006-10-11 01:20:46 -07003812void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003813 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07003814{
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003815 struct mm_struct *mm;
3816 struct mmu_gather tlb;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003817 unsigned long tlb_start = start;
3818 unsigned long tlb_end = end;
3819
3820 /*
3821 * If shared PMDs were possibly used within this vma range, adjust
3822 * start/end for worst case tlb flushing.
3823 * Note that we can not be sure if PMDs are shared until we try to
3824 * unmap pages. However, we want to make sure TLB flushing covers
3825 * the largest possible range.
3826 */
3827 adjust_range_if_pmd_sharing_possible(vma, &tlb_start, &tlb_end);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003828
3829 mm = vma->vm_mm;
3830
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003831 tlb_gather_mmu(&tlb, mm, tlb_start, tlb_end);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003832 __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07003833 tlb_finish_mmu(&tlb, tlb_start, tlb_end);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07003834}
3835
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003836/*
3837 * This is called when the original mapper is failing to COW a MAP_PRIVATE
3838 * mappping it owns the reserve page for. The intention is to unmap the page
3839 * from other VMAs and let the children be SIGKILLed if they are faulting the
3840 * same region.
3841 */
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07003842static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
3843 struct page *page, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003844{
Adam Litke75266742008-11-12 13:24:56 -08003845 struct hstate *h = hstate_vma(vma);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003846 struct vm_area_struct *iter_vma;
3847 struct address_space *mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003848 pgoff_t pgoff;
3849
3850 /*
3851 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
3852 * from page cache lookup which is in HPAGE_SIZE units.
3853 */
Adam Litke75266742008-11-12 13:24:56 -08003854 address = address & huge_page_mask(h);
Michal Hocko36e4f202012-10-08 16:33:31 -07003855 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
3856 vma->vm_pgoff;
Al Viro93c76a32015-12-04 23:45:44 -05003857 mapping = vma->vm_file->f_mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003858
Mel Gorman4eb2b1d2009-12-14 17:59:53 -08003859 /*
3860 * Take the mapping lock for the duration of the table walk. As
3861 * this mapping should be shared between all the VMAs,
3862 * __unmap_hugepage_range() is called as the lock is already held
3863 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003864 i_mmap_lock_write(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07003865 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003866 /* Do not unmap the current VMA */
3867 if (iter_vma == vma)
3868 continue;
3869
3870 /*
Mel Gorman2f84a892015-10-01 15:36:57 -07003871 * Shared VMAs have their own reserves and do not affect
3872 * MAP_PRIVATE accounting but it is possible that a shared
3873 * VMA is using the same page so check and skip such VMAs.
3874 */
3875 if (iter_vma->vm_flags & VM_MAYSHARE)
3876 continue;
3877
3878 /*
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003879 * Unmap the page from other VMAs without their own reserves.
3880 * They get marked to be SIGKILLed if they fault in these
3881 * areas. This is because a future no-page fault on this VMA
3882 * could insert a zeroed page instead of the data existing
3883 * from the time of fork. This would look like data corruption
3884 */
3885 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07003886 unmap_hugepage_range(iter_vma, address,
3887 address + huge_page_size(h), page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003888 }
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003889 i_mmap_unlock_write(mapping);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003890}
3891
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003892/*
3893 * Hugetlb_cow() should be called with page lock of the original hugepage held.
Michal Hockoef009b22012-01-10 15:07:21 -08003894 * Called with hugetlb_instantiation_mutex held and pte_page locked so we
3895 * cannot race with other handlers or page migration.
3896 * Keep the pte_same checks anyway to make transition from the mutex easier.
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003897 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003898static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Huang Ying974e6d62018-08-17 15:45:57 -07003899 unsigned long address, pte_t *ptep,
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08003900 struct page *pagecache_page, spinlock_t *ptl)
David Gibson1e8f8892006-01-06 00:10:44 -08003901{
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08003902 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07003903 struct hstate *h = hstate_vma(vma);
David Gibson1e8f8892006-01-06 00:10:44 -08003904 struct page *old_page, *new_page;
Souptick Joarder2b740302018-08-23 17:01:36 -07003905 int outside_reserve = 0;
3906 vm_fault_t ret = 0;
Huang Ying974e6d62018-08-17 15:45:57 -07003907 unsigned long haddr = address & huge_page_mask(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003908 struct mmu_notifier_range range;
David Gibson1e8f8892006-01-06 00:10:44 -08003909
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08003910 pte = huge_ptep_get(ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08003911 old_page = pte_page(pte);
3912
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003913retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08003914 /* If no-one else is actually using this page, avoid the copy
3915 * and just make the page writable */
Joonsoo Kim37a21402013-09-11 14:21:04 -07003916 if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
Hugh Dickins5a499732016-07-14 12:07:38 -07003917 page_move_anon_rmap(old_page, vma);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003918 set_huge_ptep_writable(vma, haddr, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07003919 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08003920 }
3921
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003922 /*
3923 * If the process that created a MAP_PRIVATE mapping is about to
3924 * perform a COW due to a shared page count, attempt to satisfy
3925 * the allocation without using the existing reserves. The pagecache
3926 * page is used to determine if the reserve at this address was
3927 * consumed or not. If reserves were used, a partial faulted mapping
3928 * at the time of fork() could consume its reserves on COW instead
3929 * of the full address range.
3930 */
Joonsoo Kim5944d012013-09-11 14:21:55 -07003931 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003932 old_page != pagecache_page)
3933 outside_reserve = 1;
3934
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003935 get_page(old_page);
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08003936
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003937 /*
3938 * Drop page table lock as buddy allocator may be called. It will
3939 * be acquired again before returning to the caller, as expected.
3940 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003941 spin_unlock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003942 new_page = alloc_huge_page(vma, haddr, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08003943
Adam Litke2fc39ce2007-11-14 16:59:39 -08003944 if (IS_ERR(new_page)) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003945 /*
3946 * If a process owning a MAP_PRIVATE mapping fails to COW,
3947 * it is due to references held by a child and an insufficient
3948 * huge page pool. To guarantee the original mappers
3949 * reliability, unmap the page from child processes. The child
3950 * may get SIGKILLed if it later faults.
3951 */
3952 if (outside_reserve) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003953 put_page(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003954 BUG_ON(huge_pte_none(pte));
Huang Ying5b7a1d42018-08-17 15:45:53 -07003955 unmap_ref_private(mm, vma, old_page, haddr);
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07003956 BUG_ON(huge_pte_none(pte));
3957 spin_lock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003958 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07003959 if (likely(ptep &&
3960 pte_same(huge_ptep_get(ptep), pte)))
3961 goto retry_avoidcopy;
3962 /*
3963 * race occurs while re-acquiring page table
3964 * lock, and our job is done.
3965 */
3966 return 0;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003967 }
3968
Souptick Joarder2b740302018-08-23 17:01:36 -07003969 ret = vmf_error(PTR_ERR(new_page));
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003970 goto out_release_old;
David Gibson1e8f8892006-01-06 00:10:44 -08003971 }
3972
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003973 /*
3974 * When the original hugepage is shared one, it does not have
3975 * anon_vma prepared.
3976 */
Dean Nelson44e2aa92010-10-26 14:22:08 -07003977 if (unlikely(anon_vma_prepare(vma))) {
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003978 ret = VM_FAULT_OOM;
3979 goto out_release_all;
Dean Nelson44e2aa92010-10-26 14:22:08 -07003980 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003981
Huang Ying974e6d62018-08-17 15:45:57 -07003982 copy_user_huge_page(new_page, old_page, address, vma,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003983 pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08003984 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08003985
Jérôme Glisse7269f992019-05-13 17:20:53 -07003986 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, haddr,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07003987 haddr + huge_page_size(h));
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003988 mmu_notifier_invalidate_range_start(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07003989
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08003990 /*
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003991 * Retake the page table lock to check for racing updates
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08003992 * before the page tables are altered
3993 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003994 spin_lock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07003995 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Naoya Horiguchia9af0c52014-04-07 15:36:54 -07003996 if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
Joonsoo Kim07443a82013-09-11 14:21:58 -07003997 ClearPagePrivate(new_page);
3998
David Gibson1e8f8892006-01-06 00:10:44 -08003999 /* Break COW */
Huang Ying5b7a1d42018-08-17 15:45:53 -07004000 huge_ptep_clear_flush(vma, haddr, ptep);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004001 mmu_notifier_invalidate_range(mm, range.start, range.end);
Huang Ying5b7a1d42018-08-17 15:45:53 -07004002 set_huge_pte_at(mm, haddr, ptep,
David Gibson1e8f8892006-01-06 00:10:44 -08004003 make_huge_pte(vma, new_page, 1));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08004004 page_remove_rmap(old_page, true);
Huang Ying5b7a1d42018-08-17 15:45:53 -07004005 hugepage_add_new_anon_rmap(new_page, vma, haddr);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08004006 set_page_huge_active(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08004007 /* Make the old page be freed below */
4008 new_page = old_page;
4009 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004010 spin_unlock(ptl);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004011 mmu_notifier_invalidate_range_end(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07004012out_release_all:
Huang Ying5b7a1d42018-08-17 15:45:53 -07004013 restore_reserve_on_error(h, vma, haddr, new_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004014 put_page(new_page);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07004015out_release_old:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004016 put_page(old_page);
Joonsoo Kim83120342013-09-11 14:21:57 -07004017
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07004018 spin_lock(ptl); /* Caller expects lock to be held */
4019 return ret;
David Gibson1e8f8892006-01-06 00:10:44 -08004020}
4021
Mel Gorman04f2cbe2008-07-23 21:27:25 -07004022/* Return the pagecache page at a given address within a VMA */
Andi Kleena5516432008-07-23 21:27:41 -07004023static struct page *hugetlbfs_pagecache_page(struct hstate *h,
4024 struct vm_area_struct *vma, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07004025{
4026 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07004027 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07004028
4029 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07004030 idx = vma_hugecache_offset(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07004031
4032 return find_lock_page(mapping, idx);
4033}
4034
Hugh Dickins3ae77f42009-09-21 17:03:33 -07004035/*
4036 * Return whether there is a pagecache page to back given address within VMA.
4037 * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
4038 */
4039static bool hugetlbfs_pagecache_present(struct hstate *h,
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004040 struct vm_area_struct *vma, unsigned long address)
4041{
4042 struct address_space *mapping;
4043 pgoff_t idx;
4044 struct page *page;
4045
4046 mapping = vma->vm_file->f_mapping;
4047 idx = vma_hugecache_offset(h, vma, address);
4048
4049 page = find_get_page(mapping, idx);
4050 if (page)
4051 put_page(page);
4052 return page != NULL;
4053}
4054
Mike Kravetzab76ad52015-09-08 15:01:50 -07004055int huge_add_to_page_cache(struct page *page, struct address_space *mapping,
4056 pgoff_t idx)
4057{
4058 struct inode *inode = mapping->host;
4059 struct hstate *h = hstate_inode(inode);
4060 int err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
4061
4062 if (err)
4063 return err;
4064 ClearPagePrivate(page);
4065
Mike Kravetz22146c32018-10-26 15:10:58 -07004066 /*
4067 * set page dirty so that it will not be removed from cache/file
4068 * by non-hugetlbfs specific code paths.
4069 */
4070 set_page_dirty(page);
4071
Mike Kravetzab76ad52015-09-08 15:01:50 -07004072 spin_lock(&inode->i_lock);
4073 inode->i_blocks += blocks_per_huge_page(h);
4074 spin_unlock(&inode->i_lock);
4075 return 0;
4076}
4077
Souptick Joarder2b740302018-08-23 17:01:36 -07004078static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
4079 struct vm_area_struct *vma,
4080 struct address_space *mapping, pgoff_t idx,
4081 unsigned long address, pte_t *ptep, unsigned int flags)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01004082{
Andi Kleena5516432008-07-23 21:27:41 -07004083 struct hstate *h = hstate_vma(vma);
Souptick Joarder2b740302018-08-23 17:01:36 -07004084 vm_fault_t ret = VM_FAULT_SIGBUS;
Hillf Danton409eb8c2012-01-20 14:34:13 -08004085 int anon_rmap = 0;
Adam Litke4c887262005-10-29 18:16:46 -07004086 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07004087 struct page *page;
David Gibson1e8f8892006-01-06 00:10:44 -08004088 pte_t new_pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004089 spinlock_t *ptl;
Huang Ying285b8dc2018-06-07 17:08:08 -07004090 unsigned long haddr = address & huge_page_mask(h);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08004091 bool new_page = false;
Adam Litke4c887262005-10-29 18:16:46 -07004092
Mel Gorman04f2cbe2008-07-23 21:27:25 -07004093 /*
4094 * Currently, we are forced to kill the process in the event the
4095 * original mapper has unmapped pages from the child due to a failed
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004096 * COW. Warn that such a situation has occurred as it may not be obvious
Mel Gorman04f2cbe2008-07-23 21:27:25 -07004097 */
4098 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
Geoffrey Thomas910154d2016-03-09 14:08:04 -08004099 pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
Andrew Mortonffb22af2013-02-22 16:32:08 -08004100 current->pid);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07004101 return ret;
4102 }
4103
Adam Litke4c887262005-10-29 18:16:46 -07004104 /*
Mike Kravetz87bf91d2020-04-01 21:11:08 -07004105 * We can not race with truncation due to holding i_mmap_rwsem.
4106 * i_size is modified when holding i_mmap_rwsem, so check here
4107 * once for faults beyond end of file.
Adam Litke4c887262005-10-29 18:16:46 -07004108 */
Mike Kravetz87bf91d2020-04-01 21:11:08 -07004109 size = i_size_read(mapping->host) >> huge_page_shift(h);
4110 if (idx >= size)
4111 goto out;
4112
Christoph Lameter6bda6662006-01-06 00:10:49 -08004113retry:
4114 page = find_lock_page(mapping, idx);
4115 if (!page) {
Mike Kravetz1a1aad82017-02-22 15:43:01 -08004116 /*
4117 * Check for page in userfault range
4118 */
4119 if (userfaultfd_missing(vma)) {
4120 u32 hash;
4121 struct vm_fault vmf = {
4122 .vma = vma,
Huang Ying285b8dc2018-06-07 17:08:08 -07004123 .address = haddr,
Mike Kravetz1a1aad82017-02-22 15:43:01 -08004124 .flags = flags,
4125 /*
4126 * Hard to debug if it ends up being
4127 * used by a callee that assumes
4128 * something about the other
4129 * uninitialized fields... same as in
4130 * memory.c
4131 */
4132 };
4133
4134 /*
Mike Kravetzc0d03812020-04-01 21:11:05 -07004135 * hugetlb_fault_mutex and i_mmap_rwsem must be
4136 * dropped before handling userfault. Reacquire
4137 * after handling fault to make calling code simpler.
Mike Kravetz1a1aad82017-02-22 15:43:01 -08004138 */
Wei Yang188b04a2019-11-30 17:57:02 -08004139 hash = hugetlb_fault_mutex_hash(mapping, idx);
Mike Kravetz1a1aad82017-02-22 15:43:01 -08004140 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Mike Kravetzc0d03812020-04-01 21:11:05 -07004141 i_mmap_unlock_read(mapping);
Mike Kravetz1a1aad82017-02-22 15:43:01 -08004142 ret = handle_userfault(&vmf, VM_UFFD_MISSING);
Mike Kravetzc0d03812020-04-01 21:11:05 -07004143 i_mmap_lock_read(mapping);
Mike Kravetz1a1aad82017-02-22 15:43:01 -08004144 mutex_lock(&hugetlb_fault_mutex_table[hash]);
4145 goto out;
4146 }
4147
Huang Ying285b8dc2018-06-07 17:08:08 -07004148 page = alloc_huge_page(vma, haddr, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08004149 if (IS_ERR(page)) {
Mike Kravetz4643d672019-08-13 15:38:00 -07004150 /*
4151 * Returning error will result in faulting task being
4152 * sent SIGBUS. The hugetlb fault mutex prevents two
4153 * tasks from racing to fault in the same page which
4154 * could result in false unable to allocate errors.
4155 * Page migration does not take the fault mutex, but
4156 * does a clear then write of pte's under page table
4157 * lock. Page fault code could race with migration,
4158 * notice the clear pte and try to allocate a page
4159 * here. Before returning error, get ptl and make
4160 * sure there really is no pte entry.
4161 */
4162 ptl = huge_pte_lock(h, mm, ptep);
4163 if (!huge_pte_none(huge_ptep_get(ptep))) {
4164 ret = 0;
4165 spin_unlock(ptl);
4166 goto out;
4167 }
4168 spin_unlock(ptl);
Souptick Joarder2b740302018-08-23 17:01:36 -07004169 ret = vmf_error(PTR_ERR(page));
Christoph Lameter6bda6662006-01-06 00:10:49 -08004170 goto out;
4171 }
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004172 clear_huge_page(page, address, pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08004173 __SetPageUptodate(page);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08004174 new_page = true;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01004175
Mel Gormanf83a2752009-05-28 14:34:40 -07004176 if (vma->vm_flags & VM_MAYSHARE) {
Mike Kravetzab76ad52015-09-08 15:01:50 -07004177 int err = huge_add_to_page_cache(page, mapping, idx);
Christoph Lameter6bda6662006-01-06 00:10:49 -08004178 if (err) {
4179 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08004180 if (err == -EEXIST)
4181 goto retry;
4182 goto out;
4183 }
Mel Gorman23be7462010-04-23 13:17:56 -04004184 } else {
Christoph Lameter6bda6662006-01-06 00:10:49 -08004185 lock_page(page);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09004186 if (unlikely(anon_vma_prepare(vma))) {
4187 ret = VM_FAULT_OOM;
4188 goto backout_unlocked;
4189 }
Hillf Danton409eb8c2012-01-20 14:34:13 -08004190 anon_rmap = 1;
Mel Gorman23be7462010-04-23 13:17:56 -04004191 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09004192 } else {
Naoya Horiguchi998b4382010-09-08 10:19:32 +09004193 /*
4194 * If memory error occurs between mmap() and fault, some process
4195 * don't have hwpoisoned swap entry for errored virtual address.
4196 * So we need to block hugepage fault by PG_hwpoison bit check.
4197 */
4198 if (unlikely(PageHWPoison(page))) {
Chris Forbes32f84522011-07-25 17:12:14 -07004199 ret = VM_FAULT_HWPOISON |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004200 VM_FAULT_SET_HINDEX(hstate_index(h));
Naoya Horiguchi998b4382010-09-08 10:19:32 +09004201 goto backout_unlocked;
4202 }
Christoph Lameter6bda6662006-01-06 00:10:49 -08004203 }
David Gibson1e8f8892006-01-06 00:10:44 -08004204
Andy Whitcroft57303d82008-08-12 15:08:47 -07004205 /*
4206 * If we are going to COW a private mapping later, we examine the
4207 * pending reservations for this page now. This will ensure that
4208 * any allocations necessary to record that reservation occur outside
4209 * the spinlock.
4210 */
Mike Kravetz5e911372015-09-08 15:01:28 -07004211 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07004212 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07004213 ret = VM_FAULT_OOM;
4214 goto backout_unlocked;
4215 }
Mike Kravetz5e911372015-09-08 15:01:28 -07004216 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07004217 vma_end_reservation(h, vma, haddr);
Mike Kravetz5e911372015-09-08 15:01:28 -07004218 }
Andy Whitcroft57303d82008-08-12 15:08:47 -07004219
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08004220 ptl = huge_pte_lock(h, mm, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07004221 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07004222 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07004223 goto backout;
4224
Joonsoo Kim07443a82013-09-11 14:21:58 -07004225 if (anon_rmap) {
4226 ClearPagePrivate(page);
Huang Ying285b8dc2018-06-07 17:08:08 -07004227 hugepage_add_new_anon_rmap(page, vma, haddr);
Choi Gi-yongac714902014-04-07 15:37:36 -07004228 } else
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08004229 page_dup_rmap(page, true);
David Gibson1e8f8892006-01-06 00:10:44 -08004230 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
4231 && (vma->vm_flags & VM_SHARED)));
Huang Ying285b8dc2018-06-07 17:08:08 -07004232 set_huge_pte_at(mm, haddr, ptep, new_pte);
David Gibson1e8f8892006-01-06 00:10:44 -08004233
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08004234 hugetlb_count_add(pages_per_huge_page(h), mm);
Hugh Dickins788c7df2009-06-23 13:49:05 +01004235 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
David Gibson1e8f8892006-01-06 00:10:44 -08004236 /* Optimization, do the COW without a second fault */
Huang Ying974e6d62018-08-17 15:45:57 -07004237 ret = hugetlb_cow(mm, vma, address, ptep, page, ptl);
David Gibson1e8f8892006-01-06 00:10:44 -08004238 }
4239
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004240 spin_unlock(ptl);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08004241
4242 /*
4243 * Only make newly allocated pages active. Existing pages found
4244 * in the pagecache could be !page_huge_active() if they have been
4245 * isolated for migration.
4246 */
4247 if (new_page)
4248 set_page_huge_active(page);
4249
Adam Litke4c887262005-10-29 18:16:46 -07004250 unlock_page(page);
4251out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01004252 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07004253
4254backout:
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004255 spin_unlock(ptl);
Andy Whitcroft2b267362008-08-12 15:08:49 -07004256backout_unlocked:
Adam Litke4c887262005-10-29 18:16:46 -07004257 unlock_page(page);
Huang Ying285b8dc2018-06-07 17:08:08 -07004258 restore_reserve_on_error(h, vma, haddr, page);
Adam Litke4c887262005-10-29 18:16:46 -07004259 put_page(page);
4260 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01004261}
4262
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004263#ifdef CONFIG_SMP
Wei Yang188b04a2019-11-30 17:57:02 -08004264u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004265{
4266 unsigned long key[2];
4267 u32 hash;
4268
Mike Kravetz1b426ba2019-05-13 17:19:41 -07004269 key[0] = (unsigned long) mapping;
4270 key[1] = idx;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004271
Mike Kravetz55254632019-11-30 17:56:30 -08004272 hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004273
4274 return hash & (num_fault_mutexes - 1);
4275}
4276#else
4277/*
4278 * For uniprocesor systems we always use a single mutex, so just
4279 * return 0 and avoid the hashing overhead.
4280 */
Wei Yang188b04a2019-11-30 17:57:02 -08004281u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004282{
4283 return 0;
4284}
4285#endif
4286
Souptick Joarder2b740302018-08-23 17:01:36 -07004287vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
Hugh Dickins788c7df2009-06-23 13:49:05 +01004288 unsigned long address, unsigned int flags)
Adam Litke86e52162006-01-06 00:10:43 -08004289{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004290 pte_t *ptep, entry;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004291 spinlock_t *ptl;
Souptick Joarder2b740302018-08-23 17:01:36 -07004292 vm_fault_t ret;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004293 u32 hash;
4294 pgoff_t idx;
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09004295 struct page *page = NULL;
Andy Whitcroft57303d82008-08-12 15:08:47 -07004296 struct page *pagecache_page = NULL;
Andi Kleena5516432008-07-23 21:27:41 -07004297 struct hstate *h = hstate_vma(vma);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004298 struct address_space *mapping;
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004299 int need_wait_lock = 0;
Huang Ying285b8dc2018-06-07 17:08:08 -07004300 unsigned long haddr = address & huge_page_mask(h);
Adam Litke86e52162006-01-06 00:10:43 -08004301
Huang Ying285b8dc2018-06-07 17:08:08 -07004302 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09004303 if (ptep) {
Mike Kravetzc0d03812020-04-01 21:11:05 -07004304 /*
4305 * Since we hold no locks, ptep could be stale. That is
4306 * OK as we are only making decisions based on content and
4307 * not actually modifying content here.
4308 */
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09004309 entry = huge_ptep_get(ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09004310 if (unlikely(is_hugetlb_entry_migration(entry))) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004311 migration_entry_wait_huge(vma, mm, ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09004312 return 0;
4313 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
Chris Forbes32f84522011-07-25 17:12:14 -07004314 return VM_FAULT_HWPOISON_LARGE |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004315 VM_FAULT_SET_HINDEX(hstate_index(h));
Mike Kravetzddeaab32019-01-08 15:23:36 -08004316 } else {
4317 ptep = huge_pte_alloc(mm, haddr, huge_page_size(h));
4318 if (!ptep)
4319 return VM_FAULT_OOM;
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09004320 }
4321
Mike Kravetzc0d03812020-04-01 21:11:05 -07004322 /*
4323 * Acquire i_mmap_rwsem before calling huge_pte_alloc and hold
Mike Kravetz87bf91d2020-04-01 21:11:08 -07004324 * until finished with ptep. This serves two purposes:
4325 * 1) It prevents huge_pmd_unshare from being called elsewhere
4326 * and making the ptep no longer valid.
4327 * 2) It synchronizes us with i_size modifications during truncation.
Mike Kravetzc0d03812020-04-01 21:11:05 -07004328 *
4329 * ptep could have already be assigned via huge_pte_offset. That
4330 * is OK, as huge_pte_alloc will return the same value unless
4331 * something has changed.
4332 */
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004333 mapping = vma->vm_file->f_mapping;
Mike Kravetzc0d03812020-04-01 21:11:05 -07004334 i_mmap_lock_read(mapping);
4335 ptep = huge_pte_alloc(mm, haddr, huge_page_size(h));
4336 if (!ptep) {
4337 i_mmap_unlock_read(mapping);
4338 return VM_FAULT_OOM;
4339 }
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004340
David Gibson3935baa2006-03-22 00:08:53 -08004341 /*
4342 * Serialize hugepage allocation and instantiation, so that we don't
4343 * get spurious allocation failures if two CPUs race to instantiate
4344 * the same page in the page cache.
4345 */
Mike Kravetzc0d03812020-04-01 21:11:05 -07004346 idx = vma_hugecache_offset(h, vma, haddr);
Wei Yang188b04a2019-11-30 17:57:02 -08004347 hash = hugetlb_fault_mutex_hash(mapping, idx);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004348 mutex_lock(&hugetlb_fault_mutex_table[hash]);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004349
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07004350 entry = huge_ptep_get(ptep);
4351 if (huge_pte_none(entry)) {
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004352 ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags);
David Gibsonb4d1d992008-10-15 22:01:11 -07004353 goto out_mutex;
David Gibson3935baa2006-03-22 00:08:53 -08004354 }
Adam Litke86e52162006-01-06 00:10:43 -08004355
Nick Piggin83c54072007-07-19 01:47:05 -07004356 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08004357
Andy Whitcroft57303d82008-08-12 15:08:47 -07004358 /*
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004359 * entry could be a migration/hwpoison entry at this point, so this
4360 * check prevents the kernel from going below assuming that we have
4361 * a active hugepage in pagecache. This goto expects the 2nd page fault,
4362 * and is_hugetlb_entry_(migration|hwpoisoned) check will properly
4363 * handle it.
4364 */
4365 if (!pte_present(entry))
4366 goto out_mutex;
4367
4368 /*
Andy Whitcroft57303d82008-08-12 15:08:47 -07004369 * If we are going to COW the mapping later, we examine the pending
4370 * reservations for this page now. This will ensure that any
4371 * allocations necessary to record that reservation occur outside the
4372 * spinlock. For private mappings, we also lookup the pagecache
4373 * page now as it is used to determine if a reservation has been
4374 * consumed.
4375 */
Gerald Schaefer106c9922013-04-29 15:07:23 -07004376 if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07004377 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07004378 ret = VM_FAULT_OOM;
David Gibsonb4d1d992008-10-15 22:01:11 -07004379 goto out_mutex;
Andy Whitcroft2b267362008-08-12 15:08:49 -07004380 }
Mike Kravetz5e911372015-09-08 15:01:28 -07004381 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07004382 vma_end_reservation(h, vma, haddr);
Andy Whitcroft57303d82008-08-12 15:08:47 -07004383
Mel Gormanf83a2752009-05-28 14:34:40 -07004384 if (!(vma->vm_flags & VM_MAYSHARE))
Andy Whitcroft57303d82008-08-12 15:08:47 -07004385 pagecache_page = hugetlbfs_pagecache_page(h,
Huang Ying285b8dc2018-06-07 17:08:08 -07004386 vma, haddr);
Andy Whitcroft57303d82008-08-12 15:08:47 -07004387 }
4388
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004389 ptl = huge_pte_lock(h, mm, ptep);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09004390
David Gibson1e8f8892006-01-06 00:10:44 -08004391 /* Check for a racing update before calling hugetlb_cow */
David Gibsonb4d1d992008-10-15 22:01:11 -07004392 if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004393 goto out_ptl;
David Gibsonb4d1d992008-10-15 22:01:11 -07004394
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004395 /*
4396 * hugetlb_cow() requires page locks of pte_page(entry) and
4397 * pagecache_page, so here we need take the former one
4398 * when page != pagecache_page or !pagecache_page.
4399 */
4400 page = pte_page(entry);
4401 if (page != pagecache_page)
4402 if (!trylock_page(page)) {
4403 need_wait_lock = 1;
4404 goto out_ptl;
4405 }
4406
4407 get_page(page);
David Gibsonb4d1d992008-10-15 22:01:11 -07004408
Hugh Dickins788c7df2009-06-23 13:49:05 +01004409 if (flags & FAULT_FLAG_WRITE) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07004410 if (!huge_pte_write(entry)) {
Huang Ying974e6d62018-08-17 15:45:57 -07004411 ret = hugetlb_cow(mm, vma, address, ptep,
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08004412 pagecache_page, ptl);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004413 goto out_put_page;
David Gibsonb4d1d992008-10-15 22:01:11 -07004414 }
Gerald Schaefer106c9922013-04-29 15:07:23 -07004415 entry = huge_pte_mkdirty(entry);
David Gibsonb4d1d992008-10-15 22:01:11 -07004416 }
4417 entry = pte_mkyoung(entry);
Huang Ying285b8dc2018-06-07 17:08:08 -07004418 if (huge_ptep_set_access_flags(vma, haddr, ptep, entry,
Hugh Dickins788c7df2009-06-23 13:49:05 +01004419 flags & FAULT_FLAG_WRITE))
Huang Ying285b8dc2018-06-07 17:08:08 -07004420 update_mmu_cache(vma, haddr, ptep);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004421out_put_page:
4422 if (page != pagecache_page)
4423 unlock_page(page);
4424 put_page(page);
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004425out_ptl:
4426 spin_unlock(ptl);
Andy Whitcroft57303d82008-08-12 15:08:47 -07004427
4428 if (pagecache_page) {
4429 unlock_page(pagecache_page);
4430 put_page(pagecache_page);
4431 }
David Gibsonb4d1d992008-10-15 22:01:11 -07004432out_mutex:
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004433 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Mike Kravetzc0d03812020-04-01 21:11:05 -07004434 i_mmap_unlock_read(mapping);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08004435 /*
4436 * Generally it's safe to hold refcount during waiting page lock. But
4437 * here we just wait to defer the next page fault to avoid busy loop and
4438 * the page is not used after unlocked before returning from the current
4439 * page fault. So we are safe from accessing freed page, even if we wait
4440 * here without taking refcount.
4441 */
4442 if (need_wait_lock)
4443 wait_on_page_locked(page);
David Gibson1e8f8892006-01-06 00:10:44 -08004444 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08004445}
4446
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004447/*
4448 * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with
4449 * modifications for huge pages.
4450 */
4451int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
4452 pte_t *dst_pte,
4453 struct vm_area_struct *dst_vma,
4454 unsigned long dst_addr,
4455 unsigned long src_addr,
4456 struct page **pagep)
4457{
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004458 struct address_space *mapping;
4459 pgoff_t idx;
4460 unsigned long size;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004461 int vm_shared = dst_vma->vm_flags & VM_SHARED;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004462 struct hstate *h = hstate_vma(dst_vma);
4463 pte_t _dst_pte;
4464 spinlock_t *ptl;
4465 int ret;
4466 struct page *page;
4467
4468 if (!*pagep) {
4469 ret = -ENOMEM;
4470 page = alloc_huge_page(dst_vma, dst_addr, 0);
4471 if (IS_ERR(page))
4472 goto out;
4473
4474 ret = copy_huge_page_from_user(page,
4475 (const void __user *) src_addr,
Mike Kravetz810a56b2017-02-22 15:42:58 -08004476 pages_per_huge_page(h), false);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004477
4478 /* fallback to copy_from_user outside mmap_sem */
4479 if (unlikely(ret)) {
Andrea Arcangeli9e368252018-11-30 14:09:25 -08004480 ret = -ENOENT;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004481 *pagep = page;
4482 /* don't free the page */
4483 goto out;
4484 }
4485 } else {
4486 page = *pagep;
4487 *pagep = NULL;
4488 }
4489
4490 /*
4491 * The memory barrier inside __SetPageUptodate makes sure that
4492 * preceding stores to the page contents become visible before
4493 * the set_pte_at() write.
4494 */
4495 __SetPageUptodate(page);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004496
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004497 mapping = dst_vma->vm_file->f_mapping;
4498 idx = vma_hugecache_offset(h, dst_vma, dst_addr);
4499
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004500 /*
4501 * If shared, add to page cache
4502 */
4503 if (vm_shared) {
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004504 size = i_size_read(mapping->host) >> huge_page_shift(h);
4505 ret = -EFAULT;
4506 if (idx >= size)
4507 goto out_release_nounlock;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004508
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004509 /*
4510 * Serialization between remove_inode_hugepages() and
4511 * huge_add_to_page_cache() below happens through the
4512 * hugetlb_fault_mutex_table that here must be hold by
4513 * the caller.
4514 */
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004515 ret = huge_add_to_page_cache(page, mapping, idx);
4516 if (ret)
4517 goto out_release_nounlock;
4518 }
4519
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004520 ptl = huge_pte_lockptr(h, dst_mm, dst_pte);
4521 spin_lock(ptl);
4522
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07004523 /*
4524 * Recheck the i_size after holding PT lock to make sure not
4525 * to leave any page mapped (as page_mapped()) beyond the end
4526 * of the i_size (remove_inode_hugepages() is strict about
4527 * enforcing that). If we bail out here, we'll also leave a
4528 * page in the radix tree in the vm_shared case beyond the end
4529 * of the i_size, but remove_inode_hugepages() will take care
4530 * of it as soon as we drop the hugetlb_fault_mutex_table.
4531 */
4532 size = i_size_read(mapping->host) >> huge_page_shift(h);
4533 ret = -EFAULT;
4534 if (idx >= size)
4535 goto out_release_unlock;
4536
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004537 ret = -EEXIST;
4538 if (!huge_pte_none(huge_ptep_get(dst_pte)))
4539 goto out_release_unlock;
4540
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004541 if (vm_shared) {
4542 page_dup_rmap(page, true);
4543 } else {
4544 ClearPagePrivate(page);
4545 hugepage_add_new_anon_rmap(page, dst_vma, dst_addr);
4546 }
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004547
4548 _dst_pte = make_huge_pte(dst_vma, page, dst_vma->vm_flags & VM_WRITE);
4549 if (dst_vma->vm_flags & VM_WRITE)
4550 _dst_pte = huge_pte_mkdirty(_dst_pte);
4551 _dst_pte = pte_mkyoung(_dst_pte);
4552
4553 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
4554
4555 (void)huge_ptep_set_access_flags(dst_vma, dst_addr, dst_pte, _dst_pte,
4556 dst_vma->vm_flags & VM_WRITE);
4557 hugetlb_count_add(pages_per_huge_page(h), dst_mm);
4558
4559 /* No need to invalidate - it was non-present before */
4560 update_mmu_cache(dst_vma, dst_addr, dst_pte);
4561
4562 spin_unlock(ptl);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08004563 set_page_huge_active(page);
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004564 if (vm_shared)
4565 unlock_page(page);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004566 ret = 0;
4567out:
4568 return ret;
4569out_release_unlock:
4570 spin_unlock(ptl);
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08004571 if (vm_shared)
4572 unlock_page(page);
Andrea Arcangeli5af10df2017-08-10 15:23:38 -07004573out_release_nounlock:
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08004574 put_page(page);
4575 goto out;
4576}
4577
Michel Lespinasse28a35712013-02-22 16:35:55 -08004578long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
4579 struct page **pages, struct vm_area_struct **vmas,
4580 unsigned long *position, unsigned long *nr_pages,
Peter Xu4f6da932020-04-01 21:07:58 -07004581 long i, unsigned int flags, int *locked)
David Gibson63551ae2005-06-21 17:14:44 -07004582{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004583 unsigned long pfn_offset;
4584 unsigned long vaddr = *position;
Michel Lespinasse28a35712013-02-22 16:35:55 -08004585 unsigned long remainder = *nr_pages;
Andi Kleena5516432008-07-23 21:27:41 -07004586 struct hstate *h = hstate_vma(vma);
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07004587 int err = -EFAULT;
David Gibson63551ae2005-06-21 17:14:44 -07004588
David Gibson63551ae2005-06-21 17:14:44 -07004589 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07004590 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004591 spinlock_t *ptl = NULL;
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004592 int absent;
Adam Litke4c887262005-10-29 18:16:46 -07004593 struct page *page;
4594
4595 /*
David Rientjes02057962015-04-14 15:48:24 -07004596 * If we have a pending SIGKILL, don't keep faulting pages and
4597 * potentially allocating memory.
4598 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -08004599 if (fatal_signal_pending(current)) {
David Rientjes02057962015-04-14 15:48:24 -07004600 remainder = 0;
4601 break;
4602 }
4603
4604 /*
Adam Litke4c887262005-10-29 18:16:46 -07004605 * Some archs (sparc64, sh*) have multiple pte_ts to
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004606 * each hugepage. We have to make sure we get the
Adam Litke4c887262005-10-29 18:16:46 -07004607 * first, for the page indexing below to work.
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004608 *
4609 * Note that page table lock is not held when pte is null.
Adam Litke4c887262005-10-29 18:16:46 -07004610 */
Punit Agrawal7868a202017-07-06 15:39:42 -07004611 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h),
4612 huge_page_size(h));
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004613 if (pte)
4614 ptl = huge_pte_lock(h, mm, pte);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004615 absent = !pte || huge_pte_none(huge_ptep_get(pte));
Adam Litke4c887262005-10-29 18:16:46 -07004616
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004617 /*
4618 * When coredumping, it suits get_dump_page if we just return
Hugh Dickins3ae77f42009-09-21 17:03:33 -07004619 * an error where there's an empty slot with no huge pagecache
4620 * to back it. This way, we avoid allocating a hugepage, and
4621 * the sparse dumpfile avoids allocating disk blocks, but its
4622 * huge holes still show up with zeroes where they need to be.
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004623 */
Hugh Dickins3ae77f42009-09-21 17:03:33 -07004624 if (absent && (flags & FOLL_DUMP) &&
4625 !hugetlbfs_pagecache_present(h, vma, vaddr)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004626 if (pte)
4627 spin_unlock(ptl);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004628 remainder = 0;
4629 break;
4630 }
4631
Naoya Horiguchi9cc3a5b2013-04-17 15:58:30 -07004632 /*
4633 * We need call hugetlb_fault for both hugepages under migration
4634 * (in which case hugetlb_fault waits for the migration,) and
4635 * hwpoisoned hugepages (in which case we need to prevent the
4636 * caller from accessing to them.) In order to do this, we use
4637 * here is_swap_pte instead of is_hugetlb_entry_migration and
4638 * is_hugetlb_entry_hwpoisoned. This is because it simply covers
4639 * both cases, and because we can't follow correct pages
4640 * directly from any kind of swap entries.
4641 */
4642 if (absent || is_swap_pte(huge_ptep_get(pte)) ||
Gerald Schaefer106c9922013-04-29 15:07:23 -07004643 ((flags & FOLL_WRITE) &&
4644 !huge_pte_write(huge_ptep_get(pte)))) {
Souptick Joarder2b740302018-08-23 17:01:36 -07004645 vm_fault_t ret;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004646 unsigned int fault_flags = 0;
Adam Litke4c887262005-10-29 18:16:46 -07004647
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004648 if (pte)
4649 spin_unlock(ptl);
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004650 if (flags & FOLL_WRITE)
4651 fault_flags |= FAULT_FLAG_WRITE;
Peter Xu4f6da932020-04-01 21:07:58 -07004652 if (locked)
Peter Xu71335f32020-04-01 21:08:53 -07004653 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
4654 FAULT_FLAG_KILLABLE;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004655 if (flags & FOLL_NOWAIT)
4656 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
4657 FAULT_FLAG_RETRY_NOWAIT;
4658 if (flags & FOLL_TRIED) {
Peter Xu4426e942020-04-01 21:08:49 -07004659 /*
4660 * Note: FAULT_FLAG_ALLOW_RETRY and
4661 * FAULT_FLAG_TRIED can co-exist
4662 */
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004663 fault_flags |= FAULT_FLAG_TRIED;
4664 }
4665 ret = hugetlb_fault(mm, vma, vaddr, fault_flags);
4666 if (ret & VM_FAULT_ERROR) {
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07004667 err = vm_fault_to_errno(ret, flags);
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004668 remainder = 0;
4669 break;
4670 }
4671 if (ret & VM_FAULT_RETRY) {
Peter Xu4f6da932020-04-01 21:07:58 -07004672 if (locked &&
Andrea Arcangeli1ac25012019-02-01 14:20:16 -08004673 !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Peter Xu4f6da932020-04-01 21:07:58 -07004674 *locked = 0;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004675 *nr_pages = 0;
4676 /*
4677 * VM_FAULT_RETRY must not return an
4678 * error, it will return zero
4679 * instead.
4680 *
4681 * No need to update "position" as the
4682 * caller will not check it after
4683 * *nr_pages is set to 0.
4684 */
4685 return i;
4686 }
4687 continue;
Adam Litke4c887262005-10-29 18:16:46 -07004688 }
David Gibson63551ae2005-06-21 17:14:44 -07004689
Andi Kleena5516432008-07-23 21:27:41 -07004690 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07004691 page = pte_page(huge_ptep_get(pte));
Linus Torvalds8fde12c2019-04-11 10:49:19 -07004692
4693 /*
Zhigang Luacbfb082019-11-30 17:57:06 -08004694 * If subpage information not requested, update counters
4695 * and skip the same_page loop below.
4696 */
4697 if (!pages && !vmas && !pfn_offset &&
4698 (vaddr + huge_page_size(h) < vma->vm_end) &&
4699 (remainder >= pages_per_huge_page(h))) {
4700 vaddr += huge_page_size(h);
4701 remainder -= pages_per_huge_page(h);
4702 i += pages_per_huge_page(h);
4703 spin_unlock(ptl);
4704 continue;
4705 }
4706
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004707same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08004708 if (pages) {
Hugh Dickins2a15efc2009-09-21 17:03:27 -07004709 pages[i] = mem_map_offset(page, pfn_offset);
John Hubbard3faa52c2020-04-01 21:05:29 -07004710 /*
4711 * try_grab_page() should always succeed here, because:
4712 * a) we hold the ptl lock, and b) we've just checked
4713 * that the huge page is present in the page tables. If
4714 * the huge page is present, then the tail pages must
4715 * also be present. The ptl prevents the head page and
4716 * tail pages from being rearranged in any way. So this
4717 * page must be available at this point, unless the page
4718 * refcount overflowed:
4719 */
4720 if (WARN_ON_ONCE(!try_grab_page(pages[i], flags))) {
4721 spin_unlock(ptl);
4722 remainder = 0;
4723 err = -ENOMEM;
4724 break;
4725 }
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08004726 }
David Gibson63551ae2005-06-21 17:14:44 -07004727
4728 if (vmas)
4729 vmas[i] = vma;
4730
4731 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004732 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07004733 --remainder;
4734 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004735 if (vaddr < vma->vm_end && remainder &&
Andi Kleena5516432008-07-23 21:27:41 -07004736 pfn_offset < pages_per_huge_page(h)) {
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08004737 /*
4738 * We use pfn_offset to avoid touching the pageframes
4739 * of this compound page.
4740 */
4741 goto same_page;
4742 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004743 spin_unlock(ptl);
David Gibson63551ae2005-06-21 17:14:44 -07004744 }
Michel Lespinasse28a35712013-02-22 16:35:55 -08004745 *nr_pages = remainder;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08004746 /*
4747 * setting position is actually required only if remainder is
4748 * not zero but it's faster not to add a "if (remainder)"
4749 * branch.
4750 */
David Gibson63551ae2005-06-21 17:14:44 -07004751 *position = vaddr;
4752
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07004753 return i ? i : err;
David Gibson63551ae2005-06-21 17:14:44 -07004754}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004755
Aneesh Kumar K.V5491ae72016-07-13 15:06:43 +05304756#ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
4757/*
4758 * ARCHes with special requirements for evicting HUGETLB backing TLB entries can
4759 * implement this.
4760 */
4761#define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
4762#endif
4763
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004764unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004765 unsigned long address, unsigned long end, pgprot_t newprot)
4766{
4767 struct mm_struct *mm = vma->vm_mm;
4768 unsigned long start = address;
4769 pte_t *ptep;
4770 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07004771 struct hstate *h = hstate_vma(vma);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004772 unsigned long pages = 0;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004773 bool shared_pmd = false;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004774 struct mmu_notifier_range range;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004775
4776 /*
4777 * In the case of shared PMDs, the area to flush could be beyond
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004778 * start/end. Set range.start/range.end to cover the maximum possible
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004779 * range if PMD sharing is possible.
4780 */
Jérôme Glisse7269f992019-05-13 17:20:53 -07004781 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
4782 0, vma, mm, start, end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004783 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004784
4785 BUG_ON(address >= end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004786 flush_cache_range(vma, range.start, range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004787
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004788 mmu_notifier_invalidate_range_start(&range);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08004789 i_mmap_lock_write(vma->vm_file->f_mapping);
Andi Kleena5516432008-07-23 21:27:41 -07004790 for (; address < end; address += huge_page_size(h)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004791 spinlock_t *ptl;
Punit Agrawal7868a202017-07-06 15:39:42 -07004792 ptep = huge_pte_offset(mm, address, huge_page_size(h));
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004793 if (!ptep)
4794 continue;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004795 ptl = huge_pte_lock(h, mm, ptep);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004796 if (huge_pmd_unshare(mm, &address, ptep)) {
4797 pages++;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004798 spin_unlock(ptl);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004799 shared_pmd = true;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08004800 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004801 }
Naoya Horiguchia8bda282015-02-11 15:25:28 -08004802 pte = huge_ptep_get(ptep);
4803 if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
4804 spin_unlock(ptl);
4805 continue;
4806 }
4807 if (unlikely(is_hugetlb_entry_migration(pte))) {
4808 swp_entry_t entry = pte_to_swp_entry(pte);
4809
4810 if (is_write_migration_entry(entry)) {
4811 pte_t newpte;
4812
4813 make_migration_entry_read(&entry);
4814 newpte = swp_entry_to_pte(entry);
Punit Agrawale5251fd2017-07-06 15:39:50 -07004815 set_huge_swap_pte_at(mm, address, ptep,
4816 newpte, huge_page_size(h));
Naoya Horiguchia8bda282015-02-11 15:25:28 -08004817 pages++;
4818 }
4819 spin_unlock(ptl);
4820 continue;
4821 }
4822 if (!huge_pte_none(pte)) {
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08004823 pte_t old_pte;
4824
4825 old_pte = huge_ptep_modify_prot_start(vma, address, ptep);
4826 pte = pte_mkhuge(huge_pte_modify(old_pte, newprot));
Tony Lube7517d2013-02-04 14:28:46 -08004827 pte = arch_make_huge_pte(pte, vma, NULL, 0);
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08004828 huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004829 pages++;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004830 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004831 spin_unlock(ptl);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004832 }
Mel Gormand8333522012-07-31 16:46:20 -07004833 /*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08004834 * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
Mel Gormand8333522012-07-31 16:46:20 -07004835 * may have cleared our pud entry and done put_page on the page table:
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08004836 * once we release i_mmap_rwsem, another task can do the final put_page
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004837 * and that page table be reused and filled with junk. If we actually
4838 * did unshare a page of pmds, flush the range corresponding to the pud.
Mel Gormand8333522012-07-31 16:46:20 -07004839 */
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004840 if (shared_pmd)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004841 flush_hugetlb_tlb_range(vma, range.start, range.end);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07004842 else
4843 flush_hugetlb_tlb_range(vma, start, end);
Jérôme Glisse0f108512017-11-15 17:34:07 -08004844 /*
4845 * No need to call mmu_notifier_invalidate_range() we are downgrading
4846 * page table protection not changing it to point to a new page.
4847 *
Mike Rapoportad56b732018-03-21 21:22:47 +02004848 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08004849 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08004850 i_mmap_unlock_write(vma->vm_file->f_mapping);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004851 mmu_notifier_invalidate_range_end(&range);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01004852
4853 return pages << h->order;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08004854}
4855
Mel Gormana1e78772008-07-23 21:27:23 -07004856int hugetlb_reserve_pages(struct inode *inode,
4857 long from, long to,
Mel Gorman5a6fe122009-02-10 14:02:27 +00004858 struct vm_area_struct *vma,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09004859 vm_flags_t vm_flags)
Adam Litkee4e574b2007-10-16 01:26:19 -07004860{
Mina Almasry0db9d742020-04-01 21:11:25 -07004861 long ret, chg, add = -1;
Andi Kleena5516432008-07-23 21:27:41 -07004862 struct hstate *h = hstate_inode(inode);
David Gibson90481622012-03-21 16:34:12 -07004863 struct hugepage_subpool *spool = subpool_inode(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004864 struct resv_map *resv_map;
Mina Almasry075a61d2020-04-01 21:11:28 -07004865 struct hugetlb_cgroup *h_cg = NULL;
Mina Almasry0db9d742020-04-01 21:11:25 -07004866 long gbl_reserve, regions_needed = 0;
Adam Litkee4e574b2007-10-16 01:26:19 -07004867
Mike Kravetz63489f82018-03-22 16:17:13 -07004868 /* This should never happen */
4869 if (from > to) {
4870 VM_WARN(1, "%s called with a negative range\n", __func__);
4871 return -EINVAL;
4872 }
4873
Mel Gormana1e78772008-07-23 21:27:23 -07004874 /*
Mel Gorman17c9d122009-02-11 16:34:16 +00004875 * Only apply hugepage reservation if asked. At fault time, an
4876 * attempt will be made for VM_NORESERVE to allocate a page
David Gibson90481622012-03-21 16:34:12 -07004877 * without using reserves
Mel Gorman17c9d122009-02-11 16:34:16 +00004878 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09004879 if (vm_flags & VM_NORESERVE)
Mel Gorman17c9d122009-02-11 16:34:16 +00004880 return 0;
4881
4882 /*
Mel Gormana1e78772008-07-23 21:27:23 -07004883 * Shared mappings base their reservation on the number of pages that
4884 * are already allocated on behalf of the file. Private mappings need
4885 * to reserve the full area even if read-only as mprotect() may be
4886 * called to make the mapping read-write. Assume !vma is a shm mapping
4887 */
Joonsoo Kim9119a412014-04-03 14:47:25 -07004888 if (!vma || vma->vm_flags & VM_MAYSHARE) {
Mike Kravetzf27a5132019-05-13 17:22:55 -07004889 /*
4890 * resv_map can not be NULL as hugetlb_reserve_pages is only
4891 * called for inodes for which resv_maps were created (see
4892 * hugetlbfs_get_inode).
4893 */
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004894 resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004895
Mina Almasry0db9d742020-04-01 21:11:25 -07004896 chg = region_chg(resv_map, from, to, &regions_needed);
Joonsoo Kim9119a412014-04-03 14:47:25 -07004897
4898 } else {
Mina Almasrye9fe92a2020-04-01 21:11:21 -07004899 /* Private mapping. */
Joonsoo Kim9119a412014-04-03 14:47:25 -07004900 resv_map = resv_map_alloc();
Mel Gorman5a6fe122009-02-10 14:02:27 +00004901 if (!resv_map)
4902 return -ENOMEM;
4903
Mel Gorman17c9d122009-02-11 16:34:16 +00004904 chg = to - from;
4905
Mel Gorman5a6fe122009-02-10 14:02:27 +00004906 set_vma_resv_map(vma, resv_map);
4907 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
4908 }
4909
Dave Hansenc50ac052012-05-29 15:06:46 -07004910 if (chg < 0) {
4911 ret = chg;
4912 goto out_err;
4913 }
Mel Gorman17c9d122009-02-11 16:34:16 +00004914
Mina Almasry075a61d2020-04-01 21:11:28 -07004915 ret = hugetlb_cgroup_charge_cgroup_rsvd(
4916 hstate_index(h), chg * pages_per_huge_page(h), &h_cg);
4917
4918 if (ret < 0) {
4919 ret = -ENOMEM;
4920 goto out_err;
4921 }
4922
4923 if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) {
4924 /* For private mappings, the hugetlb_cgroup uncharge info hangs
4925 * of the resv_map.
4926 */
4927 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h);
4928 }
4929
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004930 /*
4931 * There must be enough pages in the subpool for the mapping. If
4932 * the subpool has a minimum size, there may be some global
4933 * reservations already in place (gbl_reserve).
4934 */
4935 gbl_reserve = hugepage_subpool_get_pages(spool, chg);
4936 if (gbl_reserve < 0) {
Dave Hansenc50ac052012-05-29 15:06:46 -07004937 ret = -ENOSPC;
Mina Almasry075a61d2020-04-01 21:11:28 -07004938 goto out_uncharge_cgroup;
Dave Hansenc50ac052012-05-29 15:06:46 -07004939 }
Mel Gorman17c9d122009-02-11 16:34:16 +00004940
4941 /*
4942 * Check enough hugepages are available for the reservation.
David Gibson90481622012-03-21 16:34:12 -07004943 * Hand the pages back to the subpool if there are not
Mel Gorman17c9d122009-02-11 16:34:16 +00004944 */
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004945 ret = hugetlb_acct_memory(h, gbl_reserve);
Mel Gorman17c9d122009-02-11 16:34:16 +00004946 if (ret < 0) {
Mina Almasry075a61d2020-04-01 21:11:28 -07004947 goto out_put_pages;
Mel Gorman17c9d122009-02-11 16:34:16 +00004948 }
4949
4950 /*
4951 * Account for the reservations made. Shared mappings record regions
4952 * that have reservations as they are shared by multiple VMAs.
4953 * When the last VMA disappears, the region map says how much
4954 * the reservation was and the page cache tells how much of
4955 * the reservation was consumed. Private mappings are per-VMA and
4956 * only the consumed reservations are tracked. When the VMA
4957 * disappears, the original reservation is the VMA size and the
4958 * consumed reservations are stored in the map. Hence, nothing
4959 * else has to be done for private mappings here
4960 */
Mike Kravetz33039672015-06-24 16:57:58 -07004961 if (!vma || vma->vm_flags & VM_MAYSHARE) {
Mina Almasry075a61d2020-04-01 21:11:28 -07004962 add = region_add(resv_map, from, to, regions_needed, h, h_cg);
Mike Kravetz33039672015-06-24 16:57:58 -07004963
Mina Almasry0db9d742020-04-01 21:11:25 -07004964 if (unlikely(add < 0)) {
4965 hugetlb_acct_memory(h, -gbl_reserve);
Mina Almasry075a61d2020-04-01 21:11:28 -07004966 goto out_put_pages;
Mina Almasry0db9d742020-04-01 21:11:25 -07004967 } else if (unlikely(chg > add)) {
Mike Kravetz33039672015-06-24 16:57:58 -07004968 /*
4969 * pages in this range were added to the reserve
4970 * map between region_chg and region_add. This
4971 * indicates a race with alloc_huge_page. Adjust
4972 * the subpool and reserve counts modified above
4973 * based on the difference.
4974 */
4975 long rsv_adjust;
4976
Mina Almasry075a61d2020-04-01 21:11:28 -07004977 hugetlb_cgroup_uncharge_cgroup_rsvd(
4978 hstate_index(h),
4979 (chg - add) * pages_per_huge_page(h), h_cg);
4980
Mike Kravetz33039672015-06-24 16:57:58 -07004981 rsv_adjust = hugepage_subpool_put_pages(spool,
4982 chg - add);
4983 hugetlb_acct_memory(h, -rsv_adjust);
4984 }
4985 }
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07004986 return 0;
Mina Almasry075a61d2020-04-01 21:11:28 -07004987out_put_pages:
4988 /* put back original number of pages, chg */
4989 (void)hugepage_subpool_put_pages(spool, chg);
4990out_uncharge_cgroup:
4991 hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h),
4992 chg * pages_per_huge_page(h), h_cg);
Dave Hansenc50ac052012-05-29 15:06:46 -07004993out_err:
Mike Kravetz5e911372015-09-08 15:01:28 -07004994 if (!vma || vma->vm_flags & VM_MAYSHARE)
Mina Almasry0db9d742020-04-01 21:11:25 -07004995 /* Only call region_abort if the region_chg succeeded but the
4996 * region_add failed or didn't run.
4997 */
4998 if (chg >= 0 && add < 0)
4999 region_abort(resv_map, from, to, regions_needed);
Joonsoo Kimf031dd22014-04-03 14:47:28 -07005000 if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
5001 kref_put(&resv_map->refs, resv_map_release);
Dave Hansenc50ac052012-05-29 15:06:46 -07005002 return ret;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07005003}
5004
Mike Kravetzb5cec282015-09-08 15:01:41 -07005005long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
5006 long freed)
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07005007{
Andi Kleena5516432008-07-23 21:27:41 -07005008 struct hstate *h = hstate_inode(inode);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07005009 struct resv_map *resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07005010 long chg = 0;
David Gibson90481622012-03-21 16:34:12 -07005011 struct hugepage_subpool *spool = subpool_inode(inode);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07005012 long gbl_reserve;
Ken Chen45c682a2007-11-14 16:59:44 -08005013
Mike Kravetzf27a5132019-05-13 17:22:55 -07005014 /*
5015 * Since this routine can be called in the evict inode path for all
5016 * hugetlbfs inodes, resv_map could be NULL.
5017 */
Mike Kravetzb5cec282015-09-08 15:01:41 -07005018 if (resv_map) {
5019 chg = region_del(resv_map, start, end);
5020 /*
5021 * region_del() can fail in the rare case where a region
5022 * must be split and another region descriptor can not be
5023 * allocated. If end == LONG_MAX, it will not fail.
5024 */
5025 if (chg < 0)
5026 return chg;
5027 }
5028
Ken Chen45c682a2007-11-14 16:59:44 -08005029 spin_lock(&inode->i_lock);
Eric Sandeene4c6f8b2009-07-29 15:02:16 -07005030 inode->i_blocks -= (blocks_per_huge_page(h) * freed);
Ken Chen45c682a2007-11-14 16:59:44 -08005031 spin_unlock(&inode->i_lock);
5032
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07005033 /*
5034 * If the subpool has a minimum size, the number of global
5035 * reservations to be released may be adjusted.
5036 */
5037 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
5038 hugetlb_acct_memory(h, -gbl_reserve);
Mike Kravetzb5cec282015-09-08 15:01:41 -07005039
5040 return 0;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07005041}
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09005042
Steve Capper3212b532013-04-23 12:35:02 +01005043#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
5044static unsigned long page_table_shareable(struct vm_area_struct *svma,
5045 struct vm_area_struct *vma,
5046 unsigned long addr, pgoff_t idx)
5047{
5048 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
5049 svma->vm_start;
5050 unsigned long sbase = saddr & PUD_MASK;
5051 unsigned long s_end = sbase + PUD_SIZE;
5052
5053 /* Allow segments to share if only one is marked locked */
Eric B Munsonde60f5f2015-11-05 18:51:36 -08005054 unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
5055 unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK;
Steve Capper3212b532013-04-23 12:35:02 +01005056
5057 /*
5058 * match the virtual addresses, permission and the alignment of the
5059 * page table page.
5060 */
5061 if (pmd_index(addr) != pmd_index(saddr) ||
5062 vm_flags != svm_flags ||
5063 sbase < svma->vm_start || svma->vm_end < s_end)
5064 return 0;
5065
5066 return saddr;
5067}
5068
Nicholas Krause31aafb42015-09-04 15:47:58 -07005069static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr)
Steve Capper3212b532013-04-23 12:35:02 +01005070{
5071 unsigned long base = addr & PUD_MASK;
5072 unsigned long end = base + PUD_SIZE;
5073
5074 /*
5075 * check on proper vm_flags and page table alignment
5076 */
Mike Kravetz017b1662018-10-05 15:51:29 -07005077 if (vma->vm_flags & VM_MAYSHARE && range_in_vma(vma, base, end))
Nicholas Krause31aafb42015-09-04 15:47:58 -07005078 return true;
5079 return false;
Steve Capper3212b532013-04-23 12:35:02 +01005080}
5081
5082/*
Mike Kravetz017b1662018-10-05 15:51:29 -07005083 * Determine if start,end range within vma could be mapped by shared pmd.
5084 * If yes, adjust start and end to cover range associated with possible
5085 * shared pmd mappings.
5086 */
5087void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
5088 unsigned long *start, unsigned long *end)
5089{
5090 unsigned long check_addr = *start;
5091
5092 if (!(vma->vm_flags & VM_MAYSHARE))
5093 return;
5094
5095 for (check_addr = *start; check_addr < *end; check_addr += PUD_SIZE) {
5096 unsigned long a_start = check_addr & PUD_MASK;
5097 unsigned long a_end = a_start + PUD_SIZE;
5098
5099 /*
5100 * If sharing is possible, adjust start/end if necessary.
5101 */
5102 if (range_in_vma(vma, a_start, a_end)) {
5103 if (a_start < *start)
5104 *start = a_start;
5105 if (a_end > *end)
5106 *end = a_end;
5107 }
5108 }
5109}
5110
5111/*
Steve Capper3212b532013-04-23 12:35:02 +01005112 * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
5113 * and returns the corresponding pte. While this is not necessary for the
5114 * !shared pmd case because we can allocate the pmd later as well, it makes the
Mike Kravetzc0d03812020-04-01 21:11:05 -07005115 * code much cleaner.
5116 *
5117 * This routine must be called with i_mmap_rwsem held in at least read mode.
5118 * For hugetlbfs, this prevents removal of any page table entries associated
5119 * with the address space. This is important as we are setting up sharing
5120 * based on existing page table entries (mappings).
Steve Capper3212b532013-04-23 12:35:02 +01005121 */
5122pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
5123{
5124 struct vm_area_struct *vma = find_vma(mm, addr);
5125 struct address_space *mapping = vma->vm_file->f_mapping;
5126 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
5127 vma->vm_pgoff;
5128 struct vm_area_struct *svma;
5129 unsigned long saddr;
5130 pte_t *spte = NULL;
5131 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005132 spinlock_t *ptl;
Steve Capper3212b532013-04-23 12:35:02 +01005133
5134 if (!vma_shareable(vma, addr))
5135 return (pte_t *)pmd_alloc(mm, pud, addr);
5136
Steve Capper3212b532013-04-23 12:35:02 +01005137 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
5138 if (svma == vma)
5139 continue;
5140
5141 saddr = page_table_shareable(svma, vma, addr, idx);
5142 if (saddr) {
Punit Agrawal7868a202017-07-06 15:39:42 -07005143 spte = huge_pte_offset(svma->vm_mm, saddr,
5144 vma_mmu_pagesize(svma));
Steve Capper3212b532013-04-23 12:35:02 +01005145 if (spte) {
5146 get_page(virt_to_page(spte));
5147 break;
5148 }
5149 }
5150 }
5151
5152 if (!spte)
5153 goto out;
5154
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08005155 ptl = huge_pte_lock(hstate_vma(vma), mm, spte);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08005156 if (pud_none(*pud)) {
Steve Capper3212b532013-04-23 12:35:02 +01005157 pud_populate(mm, pud,
5158 (pmd_t *)((unsigned long)spte & PAGE_MASK));
Kirill A. Shutemovc17b1f42016-06-24 14:49:51 -07005159 mm_inc_nr_pmds(mm);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08005160 } else {
Steve Capper3212b532013-04-23 12:35:02 +01005161 put_page(virt_to_page(spte));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08005162 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005163 spin_unlock(ptl);
Steve Capper3212b532013-04-23 12:35:02 +01005164out:
5165 pte = (pte_t *)pmd_alloc(mm, pud, addr);
Steve Capper3212b532013-04-23 12:35:02 +01005166 return pte;
5167}
5168
5169/*
5170 * unmap huge page backed by shared pte.
5171 *
5172 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
5173 * indicated by page_count > 1, unmap is achieved by clearing pud and
5174 * decrementing the ref count. If count == 1, the pte page is not shared.
5175 *
Mike Kravetzc0d03812020-04-01 21:11:05 -07005176 * Called with page table lock held and i_mmap_rwsem held in write mode.
Steve Capper3212b532013-04-23 12:35:02 +01005177 *
5178 * returns: 1 successfully unmapped a shared pte page
5179 * 0 the underlying pte page is not shared, or it is the last user
5180 */
5181int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
5182{
5183 pgd_t *pgd = pgd_offset(mm, *addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005184 p4d_t *p4d = p4d_offset(pgd, *addr);
5185 pud_t *pud = pud_offset(p4d, *addr);
Steve Capper3212b532013-04-23 12:35:02 +01005186
5187 BUG_ON(page_count(virt_to_page(ptep)) == 0);
5188 if (page_count(virt_to_page(ptep)) == 1)
5189 return 0;
5190
5191 pud_clear(pud);
5192 put_page(virt_to_page(ptep));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08005193 mm_dec_nr_pmds(mm);
Steve Capper3212b532013-04-23 12:35:02 +01005194 *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
5195 return 1;
5196}
Steve Capper9e5fc742013-04-30 08:02:03 +01005197#define want_pmd_share() (1)
5198#else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
5199pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
5200{
5201 return NULL;
5202}
Zhang Zhene81f2d22015-06-24 16:56:13 -07005203
5204int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
5205{
5206 return 0;
5207}
Mike Kravetz017b1662018-10-05 15:51:29 -07005208
5209void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
5210 unsigned long *start, unsigned long *end)
5211{
5212}
Steve Capper9e5fc742013-04-30 08:02:03 +01005213#define want_pmd_share() (0)
Steve Capper3212b532013-04-23 12:35:02 +01005214#endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
5215
Steve Capper9e5fc742013-04-30 08:02:03 +01005216#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
5217pte_t *huge_pte_alloc(struct mm_struct *mm,
5218 unsigned long addr, unsigned long sz)
5219{
5220 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005221 p4d_t *p4d;
Steve Capper9e5fc742013-04-30 08:02:03 +01005222 pud_t *pud;
5223 pte_t *pte = NULL;
5224
5225 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovf4f0a3d2017-11-29 16:11:30 -08005226 p4d = p4d_alloc(mm, pgd, addr);
5227 if (!p4d)
5228 return NULL;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005229 pud = pud_alloc(mm, p4d, addr);
Steve Capper9e5fc742013-04-30 08:02:03 +01005230 if (pud) {
5231 if (sz == PUD_SIZE) {
5232 pte = (pte_t *)pud;
5233 } else {
5234 BUG_ON(sz != PMD_SIZE);
5235 if (want_pmd_share() && pud_none(*pud))
5236 pte = huge_pmd_share(mm, addr, pud);
5237 else
5238 pte = (pte_t *)pmd_alloc(mm, pud, addr);
5239 }
5240 }
Michal Hocko4e666312016-08-02 14:02:34 -07005241 BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte));
Steve Capper9e5fc742013-04-30 08:02:03 +01005242
5243 return pte;
5244}
5245
Punit Agrawal9b19df22017-09-06 16:21:01 -07005246/*
5247 * huge_pte_offset() - Walk the page table to resolve the hugepage
5248 * entry at address @addr
5249 *
5250 * Return: Pointer to page table or swap entry (PUD or PMD) for
5251 * address @addr, or NULL if a p*d_none() entry is encountered and the
5252 * size @sz doesn't match the hugepage size at this level of the page
5253 * table.
5254 */
Punit Agrawal7868a202017-07-06 15:39:42 -07005255pte_t *huge_pte_offset(struct mm_struct *mm,
5256 unsigned long addr, unsigned long sz)
Steve Capper9e5fc742013-04-30 08:02:03 +01005257{
5258 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005259 p4d_t *p4d;
Steve Capper9e5fc742013-04-30 08:02:03 +01005260 pud_t *pud;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005261 pmd_t *pmd;
Steve Capper9e5fc742013-04-30 08:02:03 +01005262
5263 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005264 if (!pgd_present(*pgd))
5265 return NULL;
5266 p4d = p4d_offset(pgd, addr);
5267 if (!p4d_present(*p4d))
5268 return NULL;
Punit Agrawal9b19df22017-09-06 16:21:01 -07005269
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005270 pud = pud_offset(p4d, addr);
Punit Agrawal9b19df22017-09-06 16:21:01 -07005271 if (sz != PUD_SIZE && pud_none(*pud))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005272 return NULL;
Punit Agrawal9b19df22017-09-06 16:21:01 -07005273 /* hugepage or swap? */
5274 if (pud_huge(*pud) || !pud_present(*pud))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005275 return (pte_t *)pud;
Punit Agrawal9b19df22017-09-06 16:21:01 -07005276
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03005277 pmd = pmd_offset(pud, addr);
Punit Agrawal9b19df22017-09-06 16:21:01 -07005278 if (sz != PMD_SIZE && pmd_none(*pmd))
5279 return NULL;
5280 /* hugepage or swap? */
5281 if (pmd_huge(*pmd) || !pmd_present(*pmd))
5282 return (pte_t *)pmd;
5283
5284 return NULL;
Steve Capper9e5fc742013-04-30 08:02:03 +01005285}
5286
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08005287#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
5288
5289/*
5290 * These functions are overwritable if your architecture needs its own
5291 * behavior.
5292 */
5293struct page * __weak
5294follow_huge_addr(struct mm_struct *mm, unsigned long address,
5295 int write)
5296{
5297 return ERR_PTR(-EINVAL);
5298}
5299
5300struct page * __weak
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -07005301follow_huge_pd(struct vm_area_struct *vma,
5302 unsigned long address, hugepd_t hpd, int flags, int pdshift)
5303{
5304 WARN(1, "hugepd follow called with no support for hugepage directory format\n");
5305 return NULL;
5306}
5307
5308struct page * __weak
Steve Capper9e5fc742013-04-30 08:02:03 +01005309follow_huge_pmd(struct mm_struct *mm, unsigned long address,
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005310 pmd_t *pmd, int flags)
Steve Capper9e5fc742013-04-30 08:02:03 +01005311{
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005312 struct page *page = NULL;
5313 spinlock_t *ptl;
Naoya Horiguchic9d398f2017-03-31 15:11:55 -07005314 pte_t pte;
John Hubbard3faa52c2020-04-01 21:05:29 -07005315
5316 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
5317 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
5318 (FOLL_PIN | FOLL_GET)))
5319 return NULL;
5320
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005321retry:
5322 ptl = pmd_lockptr(mm, pmd);
5323 spin_lock(ptl);
5324 /*
5325 * make sure that the address range covered by this pmd is not
5326 * unmapped from other threads.
5327 */
5328 if (!pmd_huge(*pmd))
5329 goto out;
Naoya Horiguchic9d398f2017-03-31 15:11:55 -07005330 pte = huge_ptep_get((pte_t *)pmd);
5331 if (pte_present(pte)) {
Gerald Schaefer97534122015-04-14 15:42:30 -07005332 page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbard3faa52c2020-04-01 21:05:29 -07005333 /*
5334 * try_grab_page() should always succeed here, because: a) we
5335 * hold the pmd (ptl) lock, and b) we've just checked that the
5336 * huge pmd (head) page is present in the page tables. The ptl
5337 * prevents the head page and tail pages from being rearranged
5338 * in any way. So this page must be available at this point,
5339 * unless the page refcount overflowed:
5340 */
5341 if (WARN_ON_ONCE(!try_grab_page(page, flags))) {
5342 page = NULL;
5343 goto out;
5344 }
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005345 } else {
Naoya Horiguchic9d398f2017-03-31 15:11:55 -07005346 if (is_hugetlb_entry_migration(pte)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005347 spin_unlock(ptl);
5348 __migration_entry_wait(mm, (pte_t *)pmd, ptl);
5349 goto retry;
5350 }
5351 /*
5352 * hwpoisoned entry is treated as no_page_table in
5353 * follow_page_mask().
5354 */
5355 }
5356out:
5357 spin_unlock(ptl);
Steve Capper9e5fc742013-04-30 08:02:03 +01005358 return page;
5359}
5360
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08005361struct page * __weak
Steve Capper9e5fc742013-04-30 08:02:03 +01005362follow_huge_pud(struct mm_struct *mm, unsigned long address,
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005363 pud_t *pud, int flags)
Steve Capper9e5fc742013-04-30 08:02:03 +01005364{
John Hubbard3faa52c2020-04-01 21:05:29 -07005365 if (flags & (FOLL_GET | FOLL_PIN))
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005366 return NULL;
Steve Capper9e5fc742013-04-30 08:02:03 +01005367
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08005368 return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
Steve Capper9e5fc742013-04-30 08:02:03 +01005369}
5370
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -07005371struct page * __weak
5372follow_huge_pgd(struct mm_struct *mm, unsigned long address, pgd_t *pgd, int flags)
5373{
John Hubbard3faa52c2020-04-01 21:05:29 -07005374 if (flags & (FOLL_GET | FOLL_PIN))
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -07005375 return NULL;
5376
5377 return pte_page(*(pte_t *)pgd) + ((address & ~PGDIR_MASK) >> PAGE_SHIFT);
5378}
5379
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005380bool isolate_huge_page(struct page *page, struct list_head *list)
5381{
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005382 bool ret = true;
5383
Sasha Levin309381fea2014-01-23 15:52:54 -08005384 VM_BUG_ON_PAGE(!PageHead(page), page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005385 spin_lock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005386 if (!page_huge_active(page) || !get_page_unless_zero(page)) {
5387 ret = false;
5388 goto unlock;
5389 }
5390 clear_page_huge_active(page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005391 list_move_tail(&page->lru, list);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005392unlock:
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005393 spin_unlock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005394 return ret;
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005395}
5396
5397void putback_active_hugepage(struct page *page)
5398{
Sasha Levin309381fea2014-01-23 15:52:54 -08005399 VM_BUG_ON_PAGE(!PageHead(page), page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005400 spin_lock(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07005401 set_page_huge_active(page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07005402 list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
5403 spin_unlock(&hugetlb_lock);
5404 put_page(page);
5405}
Michal Hockoab5ac902018-01-31 16:20:48 -08005406
5407void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
5408{
5409 struct hstate *h = page_hstate(oldpage);
5410
5411 hugetlb_cgroup_migrate(oldpage, newpage);
5412 set_page_owner_migrate_reason(newpage, reason);
5413
5414 /*
5415 * transfer temporary state of the new huge page. This is
5416 * reverse to other transitions because the newpage is going to
5417 * be final while the old one will be freed so it takes over
5418 * the temporary status.
5419 *
5420 * Also note that we have to transfer the per-node surplus state
5421 * here as well otherwise the global surplus count will not match
5422 * the per-node's.
5423 */
5424 if (PageHugeTemporary(newpage)) {
5425 int old_nid = page_to_nid(oldpage);
5426 int new_nid = page_to_nid(newpage);
5427
5428 SetPageHugeTemporary(oldpage);
5429 ClearPageHugeTemporary(newpage);
5430
5431 spin_lock(&hugetlb_lock);
5432 if (h->surplus_huge_pages_node[old_nid]) {
5433 h->surplus_huge_pages_node[old_nid]--;
5434 h->surplus_huge_pages_node[new_nid]++;
5435 }
5436 spin_unlock(&hugetlb_lock);
5437 }
5438}