blob: 7fcdb98c9e68930260d063ab581490cacc01152d [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>
Joonsoo Kimbbe88752020-08-11 18:37:38 -070022#include <linux/sched/mm.h>
Mike Kravetz63489f82018-03-22 16:17:13 -070023#include <linux/mmdebug.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010024#include <linux/sched/signal.h>
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +090025#include <linux/rmap.h>
Matthew Wilcoxc6247f72017-07-10 15:48:56 -070026#include <linux/string_helpers.h>
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +090027#include <linux/swap.h>
28#include <linux/swapops.h>
Davidlohr Bueso8382d912014-04-03 14:47:31 -070029#include <linux/jhash.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -080030#include <linux/numa.h>
Waiman Longc77c0a82020-01-04 13:00:15 -080031#include <linux/llist.h>
Roman Gushchincf11e852020-04-10 14:32:45 -070032#include <linux/cma.h>
Mina Almasry8cc5fcb2021-06-30 18:48:19 -070033#include <linux/migrate.h>
Mike Kravetzf9317f72022-03-22 14:45:20 -070034#include <linux/nospec.h>
Yang Yang662ce1d2022-06-01 15:55:25 -070035#include <linux/delayacct.h>
Muchun Songb958d4d2022-09-14 15:26:02 +080036#include <linux/memory.h>
Linus Torvaldsd6606682008-08-06 12:04:54 -070037
David Gibson63551ae2005-06-21 17:14:44 -070038#include <asm/page.h>
Mike Rapoportca15ca42020-08-06 23:22:28 -070039#include <asm/pgalloc.h>
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070040#include <asm/tlb.h>
David Gibson63551ae2005-06-21 17:14:44 -070041
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070042#include <linux/io.h>
David Gibson63551ae2005-06-21 17:14:44 -070043#include <linux/hugetlb.h>
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -070044#include <linux/hugetlb_cgroup.h>
Lee Schermerhorn9a3052302009-12-14 17:58:25 -080045#include <linux/node.h>
Michal Hockoab5ac902018-01-31 16:20:48 -080046#include <linux/page_owner.h>
Nick Piggin7835e982006-03-22 00:08:40 -080047#include "internal.h"
Muchun Songf41f2ed2021-06-30 18:47:13 -070048#include "hugetlb_vmemmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070050int hugetlb_max_hstate __read_mostly;
Andi Kleene5ff2152008-07-23 21:27:42 -070051unsigned int default_hstate_idx;
52struct hstate hstates[HUGE_MAX_HSTATE];
Roman Gushchincf11e852020-04-10 14:32:45 -070053
Barry Songdbda8fe2020-07-23 21:15:30 -070054#ifdef CONFIG_CMA
Roman Gushchincf11e852020-04-10 14:32:45 -070055static struct cma *hugetlb_cma[MAX_NUMNODES];
Baolin Wang38e719a2021-11-05 13:41:46 -070056static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata;
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -080057static bool hugetlb_cma_folio(struct folio *folio, unsigned int order)
Mike Kravetza01f4392021-11-05 13:41:27 -070058{
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -080059 return cma_pages_valid(hugetlb_cma[folio_nid(folio)], &folio->page,
Mike Kravetza01f4392021-11-05 13:41:27 -070060 1 << order);
61}
62#else
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -080063static bool hugetlb_cma_folio(struct folio *folio, unsigned int order)
Mike Kravetza01f4392021-11-05 13:41:27 -070064{
65 return false;
66}
Barry Songdbda8fe2020-07-23 21:15:30 -070067#endif
68static unsigned long hugetlb_cma_size __initdata;
Roman Gushchincf11e852020-04-10 14:32:45 -070069
Jon Tollefson53ba51d2008-07-23 21:27:52 -070070__initdata LIST_HEAD(huge_boot_pages);
71
Andi Kleene5ff2152008-07-23 21:27:42 -070072/* for command line parsing */
73static struct hstate * __initdata parsed_hstate;
74static unsigned long __initdata default_hstate_max_huge_pages;
Vaishali Thakkar9fee0212016-05-19 17:11:04 -070075static bool __initdata parsed_valid_hugepagesz = true;
Mike Kravetz282f4212020-06-03 16:00:46 -070076static bool __initdata parsed_default_hugepagesz;
Zhenguo Yaob5389082021-11-05 13:43:28 -070077static unsigned int default_hugepages_in_node[MAX_NUMNODES] __initdata;
Andi Kleene5ff2152008-07-23 21:27:42 -070078
David Gibson3935baa2006-03-22 00:08:53 -080079/*
Naoya Horiguchi31caf662013-09-11 14:21:59 -070080 * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
81 * free_huge_pages, and surplus_huge_pages.
David Gibson3935baa2006-03-22 00:08:53 -080082 */
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070083DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080084
Davidlohr Bueso8382d912014-04-03 14:47:31 -070085/*
86 * Serializes faults on the same logical page. This is used to
87 * prevent spurious OOMs when the hugepage pool is fully utilized.
88 */
89static int num_fault_mutexes;
Mike Kravetzc672c7f2015-09-08 15:01:35 -070090struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp;
Davidlohr Bueso8382d912014-04-03 14:47:31 -070091
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070092/* Forward declaration */
93static int hugetlb_acct_memory(struct hstate *h, long delta);
Mike Kravetz8d9bfb22022-09-14 15:18:07 -070094static void hugetlb_vma_lock_free(struct vm_area_struct *vma);
95static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma);
Mike Kravetzecfbd732022-10-04 18:17:06 -070096static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma);
James Houghtonb30c14c2023-01-04 23:19:10 +000097static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
98 unsigned long start, unsigned long end);
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -070099
Miaohe Lin1d884332021-02-24 12:07:05 -0800100static inline bool subpool_is_free(struct hugepage_subpool *spool)
101{
102 if (spool->count)
103 return false;
104 if (spool->max_hpages != -1)
105 return spool->used_hpages == 0;
106 if (spool->min_hpages != -1)
107 return spool->rsv_hpages == spool->min_hpages;
108
109 return true;
110}
111
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700112static inline void unlock_or_release_subpool(struct hugepage_subpool *spool,
113 unsigned long irq_flags)
David Gibson90481622012-03-21 16:34:12 -0700114{
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700115 spin_unlock_irqrestore(&spool->lock, irq_flags);
David Gibson90481622012-03-21 16:34:12 -0700116
117 /* If no pages are used, and no other handles to the subpool
Ethon Paul7c8de352020-06-04 16:49:07 -0700118 * remain, give up any reservations based on minimum size and
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -0700119 * free the subpool */
Miaohe Lin1d884332021-02-24 12:07:05 -0800120 if (subpool_is_free(spool)) {
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -0700121 if (spool->min_hpages != -1)
122 hugetlb_acct_memory(spool->hstate,
123 -spool->min_hpages);
David Gibson90481622012-03-21 16:34:12 -0700124 kfree(spool);
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -0700125 }
David Gibson90481622012-03-21 16:34:12 -0700126}
127
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -0700128struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
129 long min_hpages)
David Gibson90481622012-03-21 16:34:12 -0700130{
131 struct hugepage_subpool *spool;
132
Mike Kravetzc6a91822015-04-15 16:13:36 -0700133 spool = kzalloc(sizeof(*spool), GFP_KERNEL);
David Gibson90481622012-03-21 16:34:12 -0700134 if (!spool)
135 return NULL;
136
137 spin_lock_init(&spool->lock);
138 spool->count = 1;
Mike Kravetz7ca02d0a2015-04-15 16:13:42 -0700139 spool->max_hpages = max_hpages;
140 spool->hstate = h;
141 spool->min_hpages = min_hpages;
142
143 if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) {
144 kfree(spool);
145 return NULL;
146 }
147 spool->rsv_hpages = min_hpages;
David Gibson90481622012-03-21 16:34:12 -0700148
149 return spool;
150}
151
152void hugepage_put_subpool(struct hugepage_subpool *spool)
153{
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700154 unsigned long flags;
155
156 spin_lock_irqsave(&spool->lock, flags);
David Gibson90481622012-03-21 16:34:12 -0700157 BUG_ON(!spool->count);
158 spool->count--;
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700159 unlock_or_release_subpool(spool, flags);
David Gibson90481622012-03-21 16:34:12 -0700160}
161
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700162/*
163 * Subpool accounting for allocating and reserving pages.
164 * Return -ENOMEM if there are not enough resources to satisfy the
Randy Dunlap9e7ee402020-08-11 18:32:59 -0700165 * request. Otherwise, return the number of pages by which the
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700166 * global pools must be adjusted (upward). The returned value may
167 * only be different than the passed value (delta) in the case where
Ethon Paul7c8de352020-06-04 16:49:07 -0700168 * a subpool minimum size must be maintained.
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700169 */
170static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700171 long delta)
172{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700173 long ret = delta;
David Gibson90481622012-03-21 16:34:12 -0700174
175 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700176 return ret;
David Gibson90481622012-03-21 16:34:12 -0700177
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700178 spin_lock_irq(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700179
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700180 if (spool->max_hpages != -1) { /* maximum size accounting */
181 if ((spool->used_hpages + delta) <= spool->max_hpages)
182 spool->used_hpages += delta;
183 else {
184 ret = -ENOMEM;
185 goto unlock_ret;
186 }
187 }
188
Mike Kravetz09a95e22016-05-19 17:11:01 -0700189 /* minimum size accounting */
190 if (spool->min_hpages != -1 && spool->rsv_hpages) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700191 if (delta > spool->rsv_hpages) {
192 /*
193 * Asking for more reserves than those already taken on
194 * behalf of subpool. Return difference.
195 */
196 ret = delta - spool->rsv_hpages;
197 spool->rsv_hpages = 0;
198 } else {
199 ret = 0; /* reserves already accounted for */
200 spool->rsv_hpages -= delta;
201 }
202 }
203
204unlock_ret:
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700205 spin_unlock_irq(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700206 return ret;
207}
208
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700209/*
210 * Subpool accounting for freeing and unreserving pages.
211 * Return the number of global page reservations that must be dropped.
212 * The return value may only be different than the passed value (delta)
213 * in the case where a subpool minimum size must be maintained.
214 */
215static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700216 long delta)
217{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700218 long ret = delta;
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700219 unsigned long flags;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700220
David Gibson90481622012-03-21 16:34:12 -0700221 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700222 return delta;
David Gibson90481622012-03-21 16:34:12 -0700223
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700224 spin_lock_irqsave(&spool->lock, flags);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700225
226 if (spool->max_hpages != -1) /* maximum size accounting */
227 spool->used_hpages -= delta;
228
Mike Kravetz09a95e22016-05-19 17:11:01 -0700229 /* minimum size accounting */
230 if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700231 if (spool->rsv_hpages + delta <= spool->min_hpages)
232 ret = 0;
233 else
234 ret = spool->rsv_hpages + delta - spool->min_hpages;
235
236 spool->rsv_hpages += delta;
237 if (spool->rsv_hpages > spool->min_hpages)
238 spool->rsv_hpages = spool->min_hpages;
239 }
240
241 /*
242 * If hugetlbfs_put_super couldn't free spool due to an outstanding
243 * quota reference, free it now.
244 */
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700245 unlock_or_release_subpool(spool, flags);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700246
247 return ret;
David Gibson90481622012-03-21 16:34:12 -0700248}
249
250static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
251{
252 return HUGETLBFS_SB(inode->i_sb)->spool;
253}
254
255static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
256{
Al Viro496ad9a2013-01-23 17:07:38 -0500257 return subpool_inode(file_inode(vma->vm_file));
David Gibson90481622012-03-21 16:34:12 -0700258}
259
Mike Kravetze7008982022-12-12 15:50:41 -0800260/*
261 * hugetlb vma_lock helper routines
262 */
263static bool __vma_shareable_lock(struct vm_area_struct *vma)
264{
265 return vma->vm_flags & (VM_MAYSHARE | VM_SHARED) &&
266 vma->vm_private_data;
267}
268
269void hugetlb_vma_lock_read(struct vm_area_struct *vma)
270{
271 if (__vma_shareable_lock(vma)) {
272 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
273
274 down_read(&vma_lock->rw_sema);
275 }
276}
277
278void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
279{
280 if (__vma_shareable_lock(vma)) {
281 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
282
283 up_read(&vma_lock->rw_sema);
284 }
285}
286
287void hugetlb_vma_lock_write(struct vm_area_struct *vma)
288{
289 if (__vma_shareable_lock(vma)) {
290 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
291
292 down_write(&vma_lock->rw_sema);
293 }
294}
295
296void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
297{
298 if (__vma_shareable_lock(vma)) {
299 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
300
301 up_write(&vma_lock->rw_sema);
302 }
303}
304
305int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
306{
307 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
308
309 if (!__vma_shareable_lock(vma))
310 return 1;
311
312 return down_write_trylock(&vma_lock->rw_sema);
313}
314
315void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
316{
317 if (__vma_shareable_lock(vma)) {
318 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
319
320 lockdep_assert_held(&vma_lock->rw_sema);
321 }
322}
323
324void hugetlb_vma_lock_release(struct kref *kref)
325{
326 struct hugetlb_vma_lock *vma_lock = container_of(kref,
327 struct hugetlb_vma_lock, refs);
328
329 kfree(vma_lock);
330}
331
332static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock)
333{
334 struct vm_area_struct *vma = vma_lock->vma;
335
336 /*
337 * vma_lock structure may or not be released as a result of put,
338 * it certainly will no longer be attached to vma so clear pointer.
339 * Semaphore synchronizes access to vma_lock->vma field.
340 */
341 vma_lock->vma = NULL;
342 vma->vm_private_data = NULL;
343 up_write(&vma_lock->rw_sema);
344 kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
345}
346
347static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)
348{
349 if (__vma_shareable_lock(vma)) {
350 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
351
352 __hugetlb_vma_unlock_write_put(vma_lock);
353 }
354}
355
356static void hugetlb_vma_lock_free(struct vm_area_struct *vma)
357{
358 /*
359 * Only present in sharable vmas.
360 */
361 if (!vma || !__vma_shareable_lock(vma))
362 return;
363
364 if (vma->vm_private_data) {
365 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
366
367 down_write(&vma_lock->rw_sema);
368 __hugetlb_vma_unlock_write_put(vma_lock);
369 }
370}
371
372static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma)
373{
374 struct hugetlb_vma_lock *vma_lock;
375
376 /* Only establish in (flags) sharable vmas */
377 if (!vma || !(vma->vm_flags & VM_MAYSHARE))
378 return;
379
380 /* Should never get here with non-NULL vm_private_data */
381 if (vma->vm_private_data)
382 return;
383
384 vma_lock = kmalloc(sizeof(*vma_lock), GFP_KERNEL);
385 if (!vma_lock) {
386 /*
387 * If we can not allocate structure, then vma can not
388 * participate in pmd sharing. This is only a possible
389 * performance enhancement and memory saving issue.
390 * However, the lock is also used to synchronize page
391 * faults with truncation. If the lock is not present,
392 * unlikely races could leave pages in a file past i_size
393 * until the file is removed. Warn in the unlikely case of
394 * allocation failure.
395 */
396 pr_warn_once("HugeTLB: unable to allocate vma specific lock\n");
397 return;
398 }
399
400 kref_init(&vma_lock->refs);
401 init_rwsem(&vma_lock->rw_sema);
402 vma_lock->vma = vma;
403 vma->vm_private_data = vma_lock;
404}
405
Mina Almasry0db9d742020-04-01 21:11:25 -0700406/* Helper that removes a struct file_region from the resv_map cache and returns
407 * it for use.
408 */
409static struct file_region *
410get_file_region_entry_from_cache(struct resv_map *resv, long from, long to)
411{
XU pengfei32599142022-09-14 09:21:14 +0800412 struct file_region *nrg;
Mina Almasry0db9d742020-04-01 21:11:25 -0700413
414 VM_BUG_ON(resv->region_cache_count <= 0);
415
416 resv->region_cache_count--;
417 nrg = list_first_entry(&resv->region_cache, struct file_region, link);
Mina Almasry0db9d742020-04-01 21:11:25 -0700418 list_del(&nrg->link);
419
420 nrg->from = from;
421 nrg->to = to;
422
423 return nrg;
424}
425
Mina Almasry075a61d2020-04-01 21:11:28 -0700426static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg,
427 struct file_region *rg)
428{
429#ifdef CONFIG_CGROUP_HUGETLB
430 nrg->reservation_counter = rg->reservation_counter;
431 nrg->css = rg->css;
432 if (rg->css)
433 css_get(rg->css);
434#endif
435}
436
437/* Helper that records hugetlb_cgroup uncharge info. */
438static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,
439 struct hstate *h,
440 struct resv_map *resv,
441 struct file_region *nrg)
442{
443#ifdef CONFIG_CGROUP_HUGETLB
444 if (h_cg) {
445 nrg->reservation_counter =
446 &h_cg->rsvd_hugepage[hstate_index(h)];
447 nrg->css = &h_cg->css;
Miaohe Lind85aecf2021-03-24 21:37:17 -0700448 /*
449 * The caller will hold exactly one h_cg->css reference for the
450 * whole contiguous reservation region. But this area might be
451 * scattered when there are already some file_regions reside in
452 * it. As a result, many file_regions may share only one css
453 * reference. In order to ensure that one file_region must hold
454 * exactly one h_cg->css reference, we should do css_get for
455 * each file_region and leave the reference held by caller
456 * untouched.
457 */
458 css_get(&h_cg->css);
Mina Almasry075a61d2020-04-01 21:11:28 -0700459 if (!resv->pages_per_hpage)
460 resv->pages_per_hpage = pages_per_huge_page(h);
461 /* pages_per_hpage should be the same for all entries in
462 * a resv_map.
463 */
464 VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h));
465 } else {
466 nrg->reservation_counter = NULL;
467 nrg->css = NULL;
468 }
469#endif
470}
471
Miaohe Lind85aecf2021-03-24 21:37:17 -0700472static void put_uncharge_info(struct file_region *rg)
473{
474#ifdef CONFIG_CGROUP_HUGETLB
475 if (rg->css)
476 css_put(rg->css);
477#endif
478}
479
Mina Almasrya9b3f862020-04-01 21:11:35 -0700480static bool has_same_uncharge_info(struct file_region *rg,
481 struct file_region *org)
482{
483#ifdef CONFIG_CGROUP_HUGETLB
Baolin Wang0739eb42021-11-05 13:41:58 -0700484 return rg->reservation_counter == org->reservation_counter &&
Mina Almasrya9b3f862020-04-01 21:11:35 -0700485 rg->css == org->css;
486
487#else
488 return true;
489#endif
490}
491
492static void coalesce_file_region(struct resv_map *resv, struct file_region *rg)
493{
XU pengfei32599142022-09-14 09:21:14 +0800494 struct file_region *nrg, *prg;
Mina Almasrya9b3f862020-04-01 21:11:35 -0700495
496 prg = list_prev_entry(rg, link);
497 if (&prg->link != &resv->regions && prg->to == rg->from &&
498 has_same_uncharge_info(prg, rg)) {
499 prg->to = rg->to;
500
501 list_del(&rg->link);
Miaohe Lind85aecf2021-03-24 21:37:17 -0700502 put_uncharge_info(rg);
Mina Almasrya9b3f862020-04-01 21:11:35 -0700503 kfree(rg);
504
Wei Yang7db5e7b2020-10-13 16:56:20 -0700505 rg = prg;
Mina Almasrya9b3f862020-04-01 21:11:35 -0700506 }
507
508 nrg = list_next_entry(rg, link);
509 if (&nrg->link != &resv->regions && nrg->from == rg->to &&
510 has_same_uncharge_info(nrg, rg)) {
511 nrg->from = rg->from;
512
513 list_del(&rg->link);
Miaohe Lind85aecf2021-03-24 21:37:17 -0700514 put_uncharge_info(rg);
Mina Almasrya9b3f862020-04-01 21:11:35 -0700515 kfree(rg);
Mina Almasrya9b3f862020-04-01 21:11:35 -0700516 }
517}
518
Peter Xu2103cf92021-03-12 21:07:18 -0800519static inline long
Jakob Koschel84448c82022-04-28 23:16:03 -0700520hugetlb_resv_map_add(struct resv_map *map, struct list_head *rg, long from,
Peter Xu2103cf92021-03-12 21:07:18 -0800521 long to, struct hstate *h, struct hugetlb_cgroup *cg,
522 long *regions_needed)
523{
524 struct file_region *nrg;
525
526 if (!regions_needed) {
527 nrg = get_file_region_entry_from_cache(map, from, to);
528 record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg);
Jakob Koschel84448c82022-04-28 23:16:03 -0700529 list_add(&nrg->link, rg);
Peter Xu2103cf92021-03-12 21:07:18 -0800530 coalesce_file_region(map, nrg);
531 } else
532 *regions_needed += 1;
533
534 return to - from;
535}
536
Wei Yang972a3da32020-10-13 16:56:30 -0700537/*
538 * Must be called with resv->lock held.
539 *
540 * Calling this with regions_needed != NULL will count the number of pages
541 * to be added but will not modify the linked list. And regions_needed will
542 * indicate the number of file_regions needed in the cache to carry out to add
543 * the regions for this range.
Mina Almasryd75c6af2019-11-30 17:56:59 -0800544 */
545static long add_reservation_in_range(struct resv_map *resv, long f, long t,
Mina Almasry075a61d2020-04-01 21:11:28 -0700546 struct hugetlb_cgroup *h_cg,
Wei Yang972a3da32020-10-13 16:56:30 -0700547 struct hstate *h, long *regions_needed)
Mina Almasryd75c6af2019-11-30 17:56:59 -0800548{
Mina Almasry0db9d742020-04-01 21:11:25 -0700549 long add = 0;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800550 struct list_head *head = &resv->regions;
Mina Almasry0db9d742020-04-01 21:11:25 -0700551 long last_accounted_offset = f;
Jakob Koschel84448c82022-04-28 23:16:03 -0700552 struct file_region *iter, *trg = NULL;
553 struct list_head *rg = NULL;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800554
Mina Almasry0db9d742020-04-01 21:11:25 -0700555 if (regions_needed)
556 *regions_needed = 0;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800557
Mina Almasry0db9d742020-04-01 21:11:25 -0700558 /* In this loop, we essentially handle an entry for the range
Jakob Koschel84448c82022-04-28 23:16:03 -0700559 * [last_accounted_offset, iter->from), at every iteration, with some
Mina Almasry0db9d742020-04-01 21:11:25 -0700560 * bounds checking.
561 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700562 list_for_each_entry_safe(iter, trg, head, link) {
Mina Almasry0db9d742020-04-01 21:11:25 -0700563 /* Skip irrelevant regions that start before our range. */
Jakob Koschel84448c82022-04-28 23:16:03 -0700564 if (iter->from < f) {
Mina Almasry0db9d742020-04-01 21:11:25 -0700565 /* If this region ends after the last accounted offset,
566 * then we need to update last_accounted_offset.
567 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700568 if (iter->to > last_accounted_offset)
569 last_accounted_offset = iter->to;
Mina Almasry0db9d742020-04-01 21:11:25 -0700570 continue;
571 }
Mina Almasryd75c6af2019-11-30 17:56:59 -0800572
Mina Almasry0db9d742020-04-01 21:11:25 -0700573 /* When we find a region that starts beyond our range, we've
574 * finished.
575 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700576 if (iter->from >= t) {
577 rg = iter->link.prev;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800578 break;
Jakob Koschel84448c82022-04-28 23:16:03 -0700579 }
Mina Almasryd75c6af2019-11-30 17:56:59 -0800580
Jakob Koschel84448c82022-04-28 23:16:03 -0700581 /* Add an entry for last_accounted_offset -> iter->from, and
Mina Almasry0db9d742020-04-01 21:11:25 -0700582 * update last_accounted_offset.
Mina Almasryd75c6af2019-11-30 17:56:59 -0800583 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700584 if (iter->from > last_accounted_offset)
585 add += hugetlb_resv_map_add(resv, iter->link.prev,
Peter Xu2103cf92021-03-12 21:07:18 -0800586 last_accounted_offset,
Jakob Koschel84448c82022-04-28 23:16:03 -0700587 iter->from, h, h_cg,
Peter Xu2103cf92021-03-12 21:07:18 -0800588 regions_needed);
Mina Almasryd75c6af2019-11-30 17:56:59 -0800589
Jakob Koschel84448c82022-04-28 23:16:03 -0700590 last_accounted_offset = iter->to;
Mina Almasry0db9d742020-04-01 21:11:25 -0700591 }
592
593 /* Handle the case where our range extends beyond
594 * last_accounted_offset.
595 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700596 if (!rg)
597 rg = head->prev;
Peter Xu2103cf92021-03-12 21:07:18 -0800598 if (last_accounted_offset < t)
599 add += hugetlb_resv_map_add(resv, rg, last_accounted_offset,
600 t, h, h_cg, regions_needed);
Mina Almasry0db9d742020-04-01 21:11:25 -0700601
Mina Almasry0db9d742020-04-01 21:11:25 -0700602 return add;
603}
604
605/* Must be called with resv->lock acquired. Will drop lock to allocate entries.
606 */
607static int allocate_file_region_entries(struct resv_map *resv,
608 int regions_needed)
609 __must_hold(&resv->lock)
610{
Miaohe Lin34665342022-09-01 20:00:23 +0800611 LIST_HEAD(allocated_regions);
Mina Almasry0db9d742020-04-01 21:11:25 -0700612 int to_allocate = 0, i = 0;
613 struct file_region *trg = NULL, *rg = NULL;
614
615 VM_BUG_ON(regions_needed < 0);
616
Mina Almasry0db9d742020-04-01 21:11:25 -0700617 /*
618 * Check for sufficient descriptors in the cache to accommodate
619 * the number of in progress add operations plus regions_needed.
620 *
621 * This is a while loop because when we drop the lock, some other call
622 * to region_add or region_del may have consumed some region_entries,
623 * so we keep looping here until we finally have enough entries for
624 * (adds_in_progress + regions_needed).
625 */
626 while (resv->region_cache_count <
627 (resv->adds_in_progress + regions_needed)) {
628 to_allocate = resv->adds_in_progress + regions_needed -
629 resv->region_cache_count;
630
631 /* At this point, we should have enough entries in the cache
Ingo Molnarf0953a12021-05-06 18:06:47 -0700632 * for all the existing adds_in_progress. We should only be
Mina Almasry0db9d742020-04-01 21:11:25 -0700633 * needing to allocate for regions_needed.
634 */
635 VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress);
636
637 spin_unlock(&resv->lock);
638 for (i = 0; i < to_allocate; i++) {
639 trg = kmalloc(sizeof(*trg), GFP_KERNEL);
640 if (!trg)
641 goto out_of_memory;
642 list_add(&trg->link, &allocated_regions);
643 }
644
645 spin_lock(&resv->lock);
646
Wei Yangd3ec7b62020-10-13 16:56:27 -0700647 list_splice(&allocated_regions, &resv->region_cache);
648 resv->region_cache_count += to_allocate;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800649 }
650
Mina Almasry0db9d742020-04-01 21:11:25 -0700651 return 0;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800652
Mina Almasry0db9d742020-04-01 21:11:25 -0700653out_of_memory:
654 list_for_each_entry_safe(rg, trg, &allocated_regions, link) {
655 list_del(&rg->link);
656 kfree(rg);
657 }
658 return -ENOMEM;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800659}
660
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700661/*
662 * Add the huge page range represented by [f, t) to the reserve
Mina Almasry0db9d742020-04-01 21:11:25 -0700663 * map. Regions will be taken from the cache to fill in this range.
664 * Sufficient regions should exist in the cache due to the previous
665 * call to region_chg with the same range, but in some cases the cache will not
666 * have sufficient entries due to races with other code doing region_add or
667 * region_del. The extra needed entries will be allocated.
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700668 *
Mina Almasry0db9d742020-04-01 21:11:25 -0700669 * regions_needed is the out value provided by a previous call to region_chg.
670 *
671 * Return the number of new huge pages added to the map. This number is greater
672 * than or equal to zero. If file_region entries needed to be allocated for
Ethon Paul7c8de352020-06-04 16:49:07 -0700673 * this operation and we were not able to allocate, it returns -ENOMEM.
Mina Almasry0db9d742020-04-01 21:11:25 -0700674 * region_add of regions of length 1 never allocate file_regions and cannot
675 * fail; region_chg will always allocate at least 1 entry and a region_add for
676 * 1 page will only require at most 1 entry.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700677 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700678static long region_add(struct resv_map *resv, long f, long t,
Mina Almasry075a61d2020-04-01 21:11:28 -0700679 long in_regions_needed, struct hstate *h,
680 struct hugetlb_cgroup *h_cg)
Andy Whitcroft96822902008-07-23 21:27:29 -0700681{
Mina Almasry0db9d742020-04-01 21:11:25 -0700682 long add = 0, actual_regions_needed = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700683
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700684 spin_lock(&resv->lock);
Mina Almasry0db9d742020-04-01 21:11:25 -0700685retry:
686
687 /* Count how many regions are actually needed to execute this add. */
Wei Yang972a3da32020-10-13 16:56:30 -0700688 add_reservation_in_range(resv, f, t, NULL, NULL,
689 &actual_regions_needed);
Andy Whitcroft96822902008-07-23 21:27:29 -0700690
Mike Kravetz5e911372015-09-08 15:01:28 -0700691 /*
Mina Almasry0db9d742020-04-01 21:11:25 -0700692 * Check for sufficient descriptors in the cache to accommodate
693 * this add operation. Note that actual_regions_needed may be greater
694 * than in_regions_needed, as the resv_map may have been modified since
695 * the region_chg call. In this case, we need to make sure that we
696 * allocate extra entries, such that we have enough for all the
697 * existing adds_in_progress, plus the excess needed for this
698 * operation.
Mike Kravetz5e911372015-09-08 15:01:28 -0700699 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700700 if (actual_regions_needed > in_regions_needed &&
701 resv->region_cache_count <
702 resv->adds_in_progress +
703 (actual_regions_needed - in_regions_needed)) {
704 /* region_add operation of range 1 should never need to
705 * allocate file_region entries.
706 */
707 VM_BUG_ON(t - f <= 1);
Mike Kravetz5e911372015-09-08 15:01:28 -0700708
Mina Almasry0db9d742020-04-01 21:11:25 -0700709 if (allocate_file_region_entries(
710 resv, actual_regions_needed - in_regions_needed)) {
711 return -ENOMEM;
712 }
Mike Kravetz5e911372015-09-08 15:01:28 -0700713
Mina Almasry0db9d742020-04-01 21:11:25 -0700714 goto retry;
Mike Kravetz5e911372015-09-08 15:01:28 -0700715 }
716
Wei Yang972a3da32020-10-13 16:56:30 -0700717 add = add_reservation_in_range(resv, f, t, h_cg, h, NULL);
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700718
Mina Almasry0db9d742020-04-01 21:11:25 -0700719 resv->adds_in_progress -= in_regions_needed;
720
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700721 spin_unlock(&resv->lock);
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700722 return add;
Andy Whitcroft96822902008-07-23 21:27:29 -0700723}
724
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700725/*
726 * Examine the existing reserve map and determine how many
727 * huge pages in the specified range [f, t) are NOT currently
728 * represented. This routine is called before a subsequent
729 * call to region_add that will actually modify the reserve
730 * map to add the specified range [f, t). region_chg does
731 * not change the number of huge pages represented by the
Mina Almasry0db9d742020-04-01 21:11:25 -0700732 * map. A number of new file_region structures is added to the cache as a
733 * placeholder, for the subsequent region_add call to use. At least 1
734 * file_region structure is added.
735 *
736 * out_regions_needed is the number of regions added to the
737 * resv->adds_in_progress. This value needs to be provided to a follow up call
738 * to region_add or region_abort for proper accounting.
Mike Kravetz5e911372015-09-08 15:01:28 -0700739 *
740 * Returns the number of huge pages that need to be added to the existing
741 * reservation map for the range [f, t). This number is greater or equal to
742 * zero. -ENOMEM is returned if a new file_region structure or cache entry
743 * is needed and can not be allocated.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700744 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700745static long region_chg(struct resv_map *resv, long f, long t,
746 long *out_regions_needed)
Andy Whitcroft96822902008-07-23 21:27:29 -0700747{
Andy Whitcroft96822902008-07-23 21:27:29 -0700748 long chg = 0;
749
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700750 spin_lock(&resv->lock);
Mike Kravetz5e911372015-09-08 15:01:28 -0700751
Wei Yang972a3da32020-10-13 16:56:30 -0700752 /* Count how many hugepages in this range are NOT represented. */
Mina Almasry075a61d2020-04-01 21:11:28 -0700753 chg = add_reservation_in_range(resv, f, t, NULL, NULL,
Wei Yang972a3da32020-10-13 16:56:30 -0700754 out_regions_needed);
Mike Kravetz5e911372015-09-08 15:01:28 -0700755
Mina Almasry0db9d742020-04-01 21:11:25 -0700756 if (*out_regions_needed == 0)
757 *out_regions_needed = 1;
Mike Kravetz5e911372015-09-08 15:01:28 -0700758
Mina Almasry0db9d742020-04-01 21:11:25 -0700759 if (allocate_file_region_entries(resv, *out_regions_needed))
760 return -ENOMEM;
Mike Kravetz5e911372015-09-08 15:01:28 -0700761
Mina Almasry0db9d742020-04-01 21:11:25 -0700762 resv->adds_in_progress += *out_regions_needed;
Andy Whitcroft96822902008-07-23 21:27:29 -0700763
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700764 spin_unlock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700765 return chg;
766}
767
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700768/*
Mike Kravetz5e911372015-09-08 15:01:28 -0700769 * Abort the in progress add operation. The adds_in_progress field
770 * of the resv_map keeps track of the operations in progress between
771 * calls to region_chg and region_add. Operations are sometimes
772 * aborted after the call to region_chg. In such cases, region_abort
Mina Almasry0db9d742020-04-01 21:11:25 -0700773 * is called to decrement the adds_in_progress counter. regions_needed
774 * is the value returned by the region_chg call, it is used to decrement
775 * the adds_in_progress counter.
Mike Kravetz5e911372015-09-08 15:01:28 -0700776 *
777 * NOTE: The range arguments [f, t) are not needed or used in this
778 * routine. They are kept to make reading the calling code easier as
779 * arguments will match the associated region_chg call.
780 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700781static void region_abort(struct resv_map *resv, long f, long t,
782 long regions_needed)
Mike Kravetz5e911372015-09-08 15:01:28 -0700783{
784 spin_lock(&resv->lock);
785 VM_BUG_ON(!resv->region_cache_count);
Mina Almasry0db9d742020-04-01 21:11:25 -0700786 resv->adds_in_progress -= regions_needed;
Mike Kravetz5e911372015-09-08 15:01:28 -0700787 spin_unlock(&resv->lock);
788}
789
790/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700791 * Delete the specified range [f, t) from the reserve map. If the
792 * t parameter is LONG_MAX, this indicates that ALL regions after f
793 * should be deleted. Locate the regions which intersect [f, t)
794 * and either trim, delete or split the existing regions.
795 *
796 * Returns the number of huge pages deleted from the reserve map.
797 * In the normal case, the return value is zero or more. In the
798 * case where a region must be split, a new region descriptor must
799 * be allocated. If the allocation fails, -ENOMEM will be returned.
800 * NOTE: If the parameter t == LONG_MAX, then we will never split
801 * a region and possibly return -ENOMEM. Callers specifying
802 * t == LONG_MAX do not need to check for -ENOMEM error.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700803 */
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700804static long region_del(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700805{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700806 struct list_head *head = &resv->regions;
Andy Whitcroft96822902008-07-23 21:27:29 -0700807 struct file_region *rg, *trg;
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700808 struct file_region *nrg = NULL;
809 long del = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700810
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700811retry:
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700812 spin_lock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700813 list_for_each_entry_safe(rg, trg, head, link) {
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800814 /*
815 * Skip regions before the range to be deleted. file_region
816 * ranges are normally of the form [from, to). However, there
817 * may be a "placeholder" entry in the map which is of the form
818 * (from, to) with from == to. Check for placeholder entries
819 * at the beginning of the range to be deleted.
820 */
821 if (rg->to <= f && (rg->to != rg->from || rg->to != f))
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700822 continue;
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800823
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700824 if (rg->from >= t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700825 break;
Andy Whitcroft96822902008-07-23 21:27:29 -0700826
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700827 if (f > rg->from && t < rg->to) { /* Must split region */
828 /*
829 * Check for an entry in the cache before dropping
830 * lock and attempting allocation.
831 */
832 if (!nrg &&
833 resv->region_cache_count > resv->adds_in_progress) {
834 nrg = list_first_entry(&resv->region_cache,
835 struct file_region,
836 link);
837 list_del(&nrg->link);
838 resv->region_cache_count--;
839 }
840
841 if (!nrg) {
842 spin_unlock(&resv->lock);
843 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
844 if (!nrg)
845 return -ENOMEM;
846 goto retry;
847 }
848
849 del += t - f;
Mike Kravetz79aa9252020-11-01 17:07:27 -0800850 hugetlb_cgroup_uncharge_file_region(
Miaohe Lind85aecf2021-03-24 21:37:17 -0700851 resv, rg, t - f, false);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700852
853 /* New entry for end of split region */
854 nrg->from = t;
855 nrg->to = rg->to;
Mina Almasry075a61d2020-04-01 21:11:28 -0700856
857 copy_hugetlb_cgroup_uncharge_info(nrg, rg);
858
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700859 INIT_LIST_HEAD(&nrg->link);
860
861 /* Original entry is trimmed */
862 rg->to = f;
863
864 list_add(&nrg->link, &rg->link);
865 nrg = NULL;
866 break;
867 }
868
869 if (f <= rg->from && t >= rg->to) { /* Remove entire region */
870 del += rg->to - rg->from;
Mina Almasry075a61d2020-04-01 21:11:28 -0700871 hugetlb_cgroup_uncharge_file_region(resv, rg,
Miaohe Lind85aecf2021-03-24 21:37:17 -0700872 rg->to - rg->from, true);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700873 list_del(&rg->link);
874 kfree(rg);
875 continue;
876 }
877
878 if (f <= rg->from) { /* Trim beginning of region */
Mina Almasry075a61d2020-04-01 21:11:28 -0700879 hugetlb_cgroup_uncharge_file_region(resv, rg,
Miaohe Lind85aecf2021-03-24 21:37:17 -0700880 t - rg->from, false);
Mina Almasry075a61d2020-04-01 21:11:28 -0700881
Mike Kravetz79aa9252020-11-01 17:07:27 -0800882 del += t - rg->from;
883 rg->from = t;
884 } else { /* Trim end of region */
Mina Almasry075a61d2020-04-01 21:11:28 -0700885 hugetlb_cgroup_uncharge_file_region(resv, rg,
Miaohe Lind85aecf2021-03-24 21:37:17 -0700886 rg->to - f, false);
Mike Kravetz79aa9252020-11-01 17:07:27 -0800887
888 del += rg->to - f;
889 rg->to = f;
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700890 }
Andy Whitcroft96822902008-07-23 21:27:29 -0700891 }
892
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700893 spin_unlock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700894 kfree(nrg);
895 return del;
Andy Whitcroft96822902008-07-23 21:27:29 -0700896}
897
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700898/*
Mike Kravetzb5cec282015-09-08 15:01:41 -0700899 * A rare out of memory error was encountered which prevented removal of
900 * the reserve map region for a page. The huge page itself was free'ed
901 * and removed from the page cache. This routine will adjust the subpool
902 * usage count, and the global reserve count if needed. By incrementing
903 * these counts, the reserve map entry which could not be deleted will
904 * appear as a "reserved" entry instead of simply dangling with incorrect
905 * counts.
906 */
zhong jiang72e29362016-10-07 17:02:01 -0700907void hugetlb_fix_reserve_counts(struct inode *inode)
Mike Kravetzb5cec282015-09-08 15:01:41 -0700908{
909 struct hugepage_subpool *spool = subpool_inode(inode);
910 long rsv_adjust;
Miaohe Linda563882021-05-04 18:34:38 -0700911 bool reserved = false;
Mike Kravetzb5cec282015-09-08 15:01:41 -0700912
913 rsv_adjust = hugepage_subpool_get_pages(spool, 1);
Miaohe Linda563882021-05-04 18:34:38 -0700914 if (rsv_adjust > 0) {
Mike Kravetzb5cec282015-09-08 15:01:41 -0700915 struct hstate *h = hstate_inode(inode);
916
Miaohe Linda563882021-05-04 18:34:38 -0700917 if (!hugetlb_acct_memory(h, 1))
918 reserved = true;
919 } else if (!rsv_adjust) {
920 reserved = true;
Mike Kravetzb5cec282015-09-08 15:01:41 -0700921 }
Miaohe Linda563882021-05-04 18:34:38 -0700922
923 if (!reserved)
924 pr_warn("hugetlb: Huge Page Reserved count may go negative.\n");
Mike Kravetzb5cec282015-09-08 15:01:41 -0700925}
926
927/*
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700928 * Count and return the number of huge pages in the reserve map
929 * that intersect with the range [f, t).
930 */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700931static long region_count(struct resv_map *resv, long f, long t)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700932{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700933 struct list_head *head = &resv->regions;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700934 struct file_region *rg;
935 long chg = 0;
936
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700937 spin_lock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700938 /* Locate each segment we overlap with, and count that overlap. */
939 list_for_each_entry(rg, head, link) {
Wang Sheng-Huif2135a42012-05-29 15:06:17 -0700940 long seg_from;
941 long seg_to;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700942
943 if (rg->to <= f)
944 continue;
945 if (rg->from >= t)
946 break;
947
948 seg_from = max(rg->from, f);
949 seg_to = min(rg->to, t);
950
951 chg += seg_to - seg_from;
952 }
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700953 spin_unlock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700954
955 return chg;
956}
957
Andy Whitcroft96822902008-07-23 21:27:29 -0700958/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700959 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700960 * the mapping, in pagecache page units; huge pages here.
961 */
Andi Kleena5516432008-07-23 21:27:41 -0700962static pgoff_t vma_hugecache_offset(struct hstate *h,
963 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700964{
Andi Kleena5516432008-07-23 21:27:41 -0700965 return ((address - vma->vm_start) >> huge_page_shift(h)) +
966 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700967}
968
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900969pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
970 unsigned long address)
971{
972 return vma_hugecache_offset(hstate_vma(vma), vma, address);
973}
Dan Williamsdee41072016-05-14 12:20:44 -0700974EXPORT_SYMBOL_GPL(linear_hugepage_index);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900975
Andy Whitcroft84afd992008-07-23 21:27:32 -0700976/*
Mel Gorman08fba692009-01-06 14:38:53 -0800977 * Return the size of the pages allocated when backing a VMA. In the majority
978 * cases this will be same size as used by the page table entries.
979 */
980unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
981{
Dan Williams05ea8862018-04-05 16:24:25 -0700982 if (vma->vm_ops && vma->vm_ops->pagesize)
983 return vma->vm_ops->pagesize(vma);
984 return PAGE_SIZE;
Mel Gorman08fba692009-01-06 14:38:53 -0800985}
Joerg Roedelf340ca02009-06-19 15:16:22 +0200986EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
Mel Gorman08fba692009-01-06 14:38:53 -0800987
988/*
Mel Gorman33402892009-01-06 14:38:54 -0800989 * Return the page size being used by the MMU to back a VMA. In the majority
990 * of cases, the page size used by the kernel matches the MMU size. On
Dan Williams09135cc2018-04-05 16:24:21 -0700991 * architectures where it differs, an architecture-specific 'strong'
992 * version of this symbol is required.
Mel Gorman33402892009-01-06 14:38:54 -0800993 */
Dan Williams09135cc2018-04-05 16:24:21 -0700994__weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
Mel Gorman33402892009-01-06 14:38:54 -0800995{
996 return vma_kernel_pagesize(vma);
997}
Mel Gorman33402892009-01-06 14:38:54 -0800998
999/*
Andy Whitcroft84afd992008-07-23 21:27:32 -07001000 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
1001 * bits of the reservation map pointer, which are always clear due to
1002 * alignment.
1003 */
1004#define HPAGE_RESV_OWNER (1UL << 0)
1005#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001006#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -07001007
Mel Gormana1e78772008-07-23 21:27:23 -07001008/*
1009 * These helpers are used to track how many pages are reserved for
1010 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
1011 * is guaranteed to have their future faults succeed.
1012 *
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001013 * With the exception of hugetlb_dup_vma_private() which is called at fork(),
Mel Gormana1e78772008-07-23 21:27:23 -07001014 * the reserve counters are updated with the hugetlb_lock held. It is safe
1015 * to reset the VMA at fork() time as it is not in use yet and there is no
1016 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -07001017 *
1018 * The private mapping reservation is represented in a subtly different
1019 * manner to a shared mapping. A shared mapping has a region map associated
1020 * with the underlying file, this region map represents the backing file
1021 * pages which have ever had a reservation assigned which this persists even
1022 * after the page is instantiated. A private mapping has a region map
1023 * associated with the original mmap which is attached to all VMAs which
1024 * reference it, this region map represents those offsets which have consumed
1025 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -07001026 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001027static unsigned long get_vma_private_data(struct vm_area_struct *vma)
1028{
1029 return (unsigned long)vma->vm_private_data;
1030}
1031
1032static void set_vma_private_data(struct vm_area_struct *vma,
1033 unsigned long value)
1034{
1035 vma->vm_private_data = (void *)value;
1036}
1037
Mina Almasrye9fe92a2020-04-01 21:11:21 -07001038static void
1039resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map,
1040 struct hugetlb_cgroup *h_cg,
1041 struct hstate *h)
1042{
1043#ifdef CONFIG_CGROUP_HUGETLB
1044 if (!h_cg || !h) {
1045 resv_map->reservation_counter = NULL;
1046 resv_map->pages_per_hpage = 0;
1047 resv_map->css = NULL;
1048 } else {
1049 resv_map->reservation_counter =
1050 &h_cg->rsvd_hugepage[hstate_index(h)];
1051 resv_map->pages_per_hpage = pages_per_huge_page(h);
1052 resv_map->css = &h_cg->css;
1053 }
1054#endif
1055}
1056
Joonsoo Kim9119a412014-04-03 14:47:25 -07001057struct resv_map *resv_map_alloc(void)
Andy Whitcroft84afd992008-07-23 21:27:32 -07001058{
1059 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
Mike Kravetz5e911372015-09-08 15:01:28 -07001060 struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL);
1061
1062 if (!resv_map || !rg) {
1063 kfree(resv_map);
1064 kfree(rg);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001065 return NULL;
Mike Kravetz5e911372015-09-08 15:01:28 -07001066 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07001067
1068 kref_init(&resv_map->refs);
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -07001069 spin_lock_init(&resv_map->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001070 INIT_LIST_HEAD(&resv_map->regions);
1071
Mike Kravetz5e911372015-09-08 15:01:28 -07001072 resv_map->adds_in_progress = 0;
Mina Almasrye9fe92a2020-04-01 21:11:21 -07001073 /*
1074 * Initialize these to 0. On shared mappings, 0's here indicate these
1075 * fields don't do cgroup accounting. On private mappings, these will be
1076 * re-initialized to the proper values, to indicate that hugetlb cgroup
1077 * reservations are to be un-charged from here.
1078 */
1079 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL);
Mike Kravetz5e911372015-09-08 15:01:28 -07001080
1081 INIT_LIST_HEAD(&resv_map->region_cache);
1082 list_add(&rg->link, &resv_map->region_cache);
1083 resv_map->region_cache_count = 1;
1084
Andy Whitcroft84afd992008-07-23 21:27:32 -07001085 return resv_map;
1086}
1087
Joonsoo Kim9119a412014-04-03 14:47:25 -07001088void resv_map_release(struct kref *ref)
Andy Whitcroft84afd992008-07-23 21:27:32 -07001089{
1090 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
Mike Kravetz5e911372015-09-08 15:01:28 -07001091 struct list_head *head = &resv_map->region_cache;
1092 struct file_region *rg, *trg;
Andy Whitcroft84afd992008-07-23 21:27:32 -07001093
1094 /* Clear out any active regions before we release the map. */
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001095 region_del(resv_map, 0, LONG_MAX);
Mike Kravetz5e911372015-09-08 15:01:28 -07001096
1097 /* ... and any entries left in the cache */
1098 list_for_each_entry_safe(rg, trg, head, link) {
1099 list_del(&rg->link);
1100 kfree(rg);
1101 }
1102
1103 VM_BUG_ON(resv_map->adds_in_progress);
1104
Andy Whitcroft84afd992008-07-23 21:27:32 -07001105 kfree(resv_map);
1106}
1107
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001108static inline struct resv_map *inode_resv_map(struct inode *inode)
1109{
Mike Kravetzf27a5132019-05-13 17:22:55 -07001110 /*
1111 * At inode evict time, i_mapping may not point to the original
1112 * address space within the inode. This original address space
1113 * contains the pointer to the resv_map. So, always use the
1114 * address space embedded within the inode.
1115 * The VERY common case is inode->mapping == &inode->i_data but,
1116 * this may not be true for device special inodes.
1117 */
1118 return (struct resv_map *)(&inode->i_data)->private_data;
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001119}
1120
Andy Whitcroft84afd992008-07-23 21:27:32 -07001121static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -07001122{
Sasha Levin81d1b092014-10-09 15:28:10 -07001123 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001124 if (vma->vm_flags & VM_MAYSHARE) {
1125 struct address_space *mapping = vma->vm_file->f_mapping;
1126 struct inode *inode = mapping->host;
1127
1128 return inode_resv_map(inode);
1129
1130 } else {
Andy Whitcroft84afd992008-07-23 21:27:32 -07001131 return (struct resv_map *)(get_vma_private_data(vma) &
1132 ~HPAGE_RESV_MASK);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001133 }
Mel Gormana1e78772008-07-23 21:27:23 -07001134}
1135
Andy Whitcroft84afd992008-07-23 21:27:32 -07001136static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -07001137{
Sasha Levin81d1b092014-10-09 15:28:10 -07001138 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1139 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Mel Gormana1e78772008-07-23 21:27:23 -07001140
Andy Whitcroft84afd992008-07-23 21:27:32 -07001141 set_vma_private_data(vma, (get_vma_private_data(vma) &
1142 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001143}
1144
1145static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
1146{
Sasha Levin81d1b092014-10-09 15:28:10 -07001147 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1148 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001149
1150 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001151}
1152
1153static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
1154{
Sasha Levin81d1b092014-10-09 15:28:10 -07001155 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001156
1157 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -07001158}
1159
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001160void hugetlb_dup_vma_private(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -07001161{
Sasha Levin81d1b092014-10-09 15:28:10 -07001162 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001163 /*
1164 * Clear vm_private_data
Mike Kravetz612b8a32022-10-19 13:19:57 -07001165 * - For shared mappings this is a per-vma semaphore that may be
1166 * allocated in a subsequent call to hugetlb_vm_op_open.
1167 * Before clearing, make sure pointer is not associated with vma
1168 * as this will leak the structure. This is the case when called
1169 * via clear_vma_resv_huge_pages() and hugetlb_vm_op_open has already
1170 * been called to allocate a new structure.
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001171 * - For MAP_PRIVATE mappings, this is the reserve map which does
1172 * not apply to children. Faults generated by the children are
1173 * not guaranteed to succeed, even if read-only.
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001174 */
Mike Kravetz612b8a32022-10-19 13:19:57 -07001175 if (vma->vm_flags & VM_MAYSHARE) {
1176 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
1177
1178 if (vma_lock && vma_lock->vma != vma)
1179 vma->vm_private_data = NULL;
1180 } else
1181 vma->vm_private_data = NULL;
Mel Gormana1e78772008-07-23 21:27:23 -07001182}
1183
Mina Almasry550a7d62021-11-05 13:41:40 -07001184/*
1185 * Reset and decrement one ref on hugepage private reservation.
Lorenzo Stoakes8651a132023-01-07 00:00:05 +00001186 * Called with mm->mmap_lock writer semaphore held.
Mina Almasry550a7d62021-11-05 13:41:40 -07001187 * This function should be only used by move_vma() and operate on
1188 * same sized vma. It should never come here with last ref on the
1189 * reservation.
1190 */
1191void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
1192{
1193 /*
1194 * Clear the old hugetlb private page reservation.
1195 * It has already been transferred to new_vma.
1196 *
1197 * During a mremap() operation of a hugetlb vma we call move_vma()
1198 * which copies vma into new_vma and unmaps vma. After the copy
1199 * operation both new_vma and vma share a reference to the resv_map
1200 * struct, and at that point vma is about to be unmapped. We don't
1201 * want to return the reservation to the pool at unmap of vma because
1202 * the reservation still lives on in new_vma, so simply decrement the
1203 * ref here and remove the resv_map reference from this vma.
1204 */
1205 struct resv_map *reservations = vma_resv_map(vma);
1206
Bui Quang Minhafe041c2021-11-19 16:43:40 -08001207 if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1208 resv_map_put_hugetlb_cgroup_uncharge_info(reservations);
Mina Almasry550a7d62021-11-05 13:41:40 -07001209 kref_put(&reservations->refs, resv_map_release);
Bui Quang Minhafe041c2021-11-19 16:43:40 -08001210 }
Mina Almasry550a7d62021-11-05 13:41:40 -07001211
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001212 hugetlb_dup_vma_private(vma);
Mina Almasry550a7d62021-11-05 13:41:40 -07001213}
1214
Mel Gormana1e78772008-07-23 21:27:23 -07001215/* Returns true if the VMA has associated reserve pages */
Nicholas Krause559ec2f2015-09-04 15:48:27 -07001216static bool vma_has_reserves(struct vm_area_struct *vma, long chg)
Mel Gormana1e78772008-07-23 21:27:23 -07001217{
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001218 if (vma->vm_flags & VM_NORESERVE) {
1219 /*
1220 * This address is already reserved by other process(chg == 0),
1221 * so, we should decrement reserved count. Without decrementing,
1222 * reserve count remains after releasing inode, because this
1223 * allocated page will go into page cache and is regarded as
1224 * coming from reserved pool in releasing step. Currently, we
1225 * don't have any other solution to deal with this situation
1226 * properly, so add work-around here.
1227 */
1228 if (vma->vm_flags & VM_MAYSHARE && chg == 0)
Nicholas Krause559ec2f2015-09-04 15:48:27 -07001229 return true;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001230 else
Nicholas Krause559ec2f2015-09-04 15:48:27 -07001231 return false;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001232 }
Joonsoo Kima63884e2013-09-11 14:21:07 -07001233
1234 /* Shared mappings always use reserves */
Mike Kravetz1fb1b0e2015-09-08 15:01:44 -07001235 if (vma->vm_flags & VM_MAYSHARE) {
1236 /*
1237 * We know VM_NORESERVE is not set. Therefore, there SHOULD
1238 * be a region map for all pages. The only situation where
1239 * there is no region map is if a hole was punched via
Ethon Paul7c8de352020-06-04 16:49:07 -07001240 * fallocate. In this case, there really are no reserves to
Mike Kravetz1fb1b0e2015-09-08 15:01:44 -07001241 * use. This situation is indicated if chg != 0.
1242 */
1243 if (chg)
1244 return false;
1245 else
1246 return true;
1247 }
Joonsoo Kima63884e2013-09-11 14:21:07 -07001248
1249 /*
1250 * Only the process that called mmap() has reserves for
1251 * private mappings.
1252 */
Mike Kravetz67961f92016-06-08 15:33:42 -07001253 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1254 /*
1255 * Like the shared case above, a hole punch or truncate
1256 * could have been performed on the private mapping.
1257 * Examine the value of chg to determine if reserves
1258 * actually exist or were previously consumed.
1259 * Very Subtle - The value of chg comes from a previous
1260 * call to vma_needs_reserves(). The reserve map for
1261 * private mappings has different (opposite) semantics
1262 * than that of shared mappings. vma_needs_reserves()
1263 * has already taken this difference in semantics into
1264 * account. Therefore, the meaning of chg is the same
1265 * as in the shared case above. Code could easily be
1266 * combined, but keeping it separate draws attention to
1267 * subtle differences.
1268 */
1269 if (chg)
1270 return false;
1271 else
1272 return true;
1273 }
Joonsoo Kima63884e2013-09-11 14:21:07 -07001274
Nicholas Krause559ec2f2015-09-04 15:48:27 -07001275 return false;
Mel Gormana1e78772008-07-23 21:27:23 -07001276}
1277
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001278static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001280 int nid = folio_nid(folio);
Mike Kravetz9487ca62021-05-04 18:35:10 -07001281
1282 lockdep_assert_held(&hugetlb_lock);
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001283 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07001284
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001285 list_move(&folio->lru, &h->hugepage_freelists[nid]);
Andi Kleena5516432008-07-23 21:27:41 -07001286 h->free_huge_pages++;
1287 h->free_huge_pages_node[nid]++;
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001288 folio_set_hugetlb_freed(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001291static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001292{
1293 struct page *page;
Pavel Tatashin1a08ae32021-05-04 18:38:53 -07001294 bool pin = !!(current->flags & PF_MEMALLOC_PIN);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001295
Mike Kravetz9487ca62021-05-04 18:35:10 -07001296 lockdep_assert_held(&hugetlb_lock);
Joonsoo Kimbbe88752020-08-11 18:37:38 -07001297 list_for_each_entry(page, &h->hugepage_freelists[nid], lru) {
Alex Sierra6077c942022-07-15 10:05:08 -05001298 if (pin && !is_longterm_pinnable_page(page))
Joonsoo Kimbbe88752020-08-11 18:37:38 -07001299 continue;
1300
Wei Yang6664bfc2020-10-13 16:56:39 -07001301 if (PageHWPoison(page))
1302 continue;
1303
1304 list_move(&page->lru, &h->hugepage_activelist);
1305 set_page_refcounted(page);
Mike Kravetz6c037142021-02-24 12:09:04 -08001306 ClearHPageFreed(page);
Wei Yang6664bfc2020-10-13 16:56:39 -07001307 h->free_huge_pages--;
1308 h->free_huge_pages_node[nid]--;
1309 return page;
Joonsoo Kimbbe88752020-08-11 18:37:38 -07001310 }
1311
Wei Yang6664bfc2020-10-13 16:56:39 -07001312 return NULL;
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001313}
1314
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001315static struct page *dequeue_huge_page_nodemask(struct hstate *h, gfp_t gfp_mask, int nid,
1316 nodemask_t *nmask)
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001317{
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001318 unsigned int cpuset_mems_cookie;
1319 struct zonelist *zonelist;
1320 struct zone *zone;
1321 struct zoneref *z;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08001322 int node = NUMA_NO_NODE;
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001323
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001324 zonelist = node_zonelist(nid, gfp_mask);
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001325
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001326retry_cpuset:
1327 cpuset_mems_cookie = read_mems_allowed_begin();
1328 for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) {
1329 struct page *page;
1330
1331 if (!cpuset_zone_allowed(zone, gfp_mask))
1332 continue;
1333 /*
1334 * no need to ask again on the same node. Pool is node rather than
1335 * zone aware
1336 */
1337 if (zone_to_nid(zone) == node)
1338 continue;
1339 node = zone_to_nid(zone);
1340
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001341 page = dequeue_huge_page_node_exact(h, node);
1342 if (page)
1343 return page;
1344 }
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001345 if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie)))
1346 goto retry_cpuset;
1347
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001348 return NULL;
1349}
1350
Xin Hao8346d692022-09-22 10:19:29 +08001351static unsigned long available_huge_pages(struct hstate *h)
1352{
1353 return h->free_huge_pages - h->resv_huge_pages;
1354}
1355
Andi Kleena5516432008-07-23 21:27:41 -07001356static struct page *dequeue_huge_page_vma(struct hstate *h,
1357 struct vm_area_struct *vma,
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001358 unsigned long address, int avoid_reserve,
1359 long chg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Ben Widawskycfcaa662021-09-02 15:00:13 -07001361 struct page *page = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -07001362 struct mempolicy *mpol;
Vlastimil Babka04ec6262017-07-06 15:40:03 -07001363 gfp_t gfp_mask;
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001364 nodemask_t *nodemask;
Vlastimil Babka04ec6262017-07-06 15:40:03 -07001365 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Mel Gormana1e78772008-07-23 21:27:23 -07001367 /*
1368 * A child process with MAP_PRIVATE mappings created by their parent
1369 * have no page reserves. This check ensures that reservations are
1370 * not "stolen". The child may still get SIGKILLed
1371 */
Xin Hao8346d692022-09-22 10:19:29 +08001372 if (!vma_has_reserves(vma, chg) && !available_huge_pages(h))
Miao Xiec0ff7452010-05-24 14:32:08 -07001373 goto err;
Mel Gormana1e78772008-07-23 21:27:23 -07001374
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001375 /* If reserves cannot be used, ensure enough pages are in the pool */
Xin Hao8346d692022-09-22 10:19:29 +08001376 if (avoid_reserve && !available_huge_pages(h))
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001377 goto err;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001378
Vlastimil Babka04ec6262017-07-06 15:40:03 -07001379 gfp_mask = htlb_alloc_mask(h);
1380 nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07001381
1382 if (mpol_is_preferred_many(mpol)) {
1383 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask);
1384
1385 /* Fallback to all nodes if page==NULL */
1386 nodemask = NULL;
1387 }
1388
1389 if (!page)
1390 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask);
1391
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001392 if (page && !avoid_reserve && vma_has_reserves(vma, chg)) {
Mike Kravetzd6995da2021-02-24 12:08:51 -08001393 SetHPageRestoreReserve(page);
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001394 h->resv_huge_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001396
1397 mpol_cond_put(mpol);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001398 return page;
1399
Miao Xiec0ff7452010-05-24 14:32:08 -07001400err:
Mel Gormancc9a6c82012-03-21 16:34:11 -07001401 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Luiz Capitulino1cac6f22014-06-04 16:07:11 -07001404/*
1405 * common helper functions for hstate_next_node_to_{alloc|free}.
1406 * We may have allocated or freed a huge page based on a different
1407 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
1408 * be outside of *nodes_allowed. Ensure that we use an allowed
1409 * node for alloc or free.
1410 */
1411static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
1412{
Andrew Morton0edaf862016-05-19 17:10:58 -07001413 nid = next_node_in(nid, *nodes_allowed);
Luiz Capitulino1cac6f22014-06-04 16:07:11 -07001414 VM_BUG_ON(nid >= MAX_NUMNODES);
1415
1416 return nid;
1417}
1418
1419static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
1420{
1421 if (!node_isset(nid, *nodes_allowed))
1422 nid = next_node_allowed(nid, nodes_allowed);
1423 return nid;
1424}
1425
1426/*
1427 * returns the previously saved node ["this node"] from which to
1428 * allocate a persistent huge page for the pool and advance the
1429 * next node from which to allocate, handling wrap at end of node
1430 * mask.
1431 */
1432static int hstate_next_node_to_alloc(struct hstate *h,
1433 nodemask_t *nodes_allowed)
1434{
1435 int nid;
1436
1437 VM_BUG_ON(!nodes_allowed);
1438
1439 nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
1440 h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
1441
1442 return nid;
1443}
1444
1445/*
Mike Kravetz10c6ec42021-05-04 18:35:03 -07001446 * helper for remove_pool_huge_page() - return the previously saved
Luiz Capitulino1cac6f22014-06-04 16:07:11 -07001447 * node ["this node"] from which to free a huge page. Advance the
1448 * next node id whether or not we find a free huge page to free so
1449 * that the next attempt to free addresses the next node.
1450 */
1451static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
1452{
1453 int nid;
1454
1455 VM_BUG_ON(!nodes_allowed);
1456
1457 nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
1458 h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
1459
1460 return nid;
1461}
1462
1463#define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
1464 for (nr_nodes = nodes_weight(*mask); \
1465 nr_nodes > 0 && \
1466 ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
1467 nr_nodes--)
1468
1469#define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
1470 for (nr_nodes = nodes_weight(*mask); \
1471 nr_nodes > 0 && \
1472 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1473 nr_nodes--)
1474
Mike Kravetz8531fc62021-11-05 13:41:33 -07001475/* used to demote non-gigantic_huge pages as well */
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001476static void __destroy_compound_gigantic_folio(struct folio *folio,
Mike Kravetz34d9e352021-11-05 13:41:30 -07001477 unsigned int order, bool demote)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001478{
1479 int i;
1480 int nr_pages = 1 << order;
Cheng Li14455ea2022-09-09 07:31:09 +00001481 struct page *p;
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001482
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001483 atomic_set(folio_mapcount_ptr(folio), 0);
1484 atomic_set(folio_subpages_mapcount_ptr(folio), 0);
1485 atomic_set(folio_pincount_ptr(folio), 0);
John Hubbard47e29d32020-04-01 21:05:33 -07001486
Cheng Li14455ea2022-09-09 07:31:09 +00001487 for (i = 1; i < nr_pages; i++) {
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001488 p = folio_page(folio, i);
Mike Kravetza01f4392021-11-05 13:41:27 -07001489 p->mapping = NULL;
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001490 clear_compound_head(p);
Mike Kravetz34d9e352021-11-05 13:41:30 -07001491 if (!demote)
1492 set_page_refcounted(p);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001493 }
1494
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001495 folio_set_compound_order(folio, 0);
1496 __folio_clear_head(folio);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001497}
1498
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001499static void destroy_compound_hugetlb_folio_for_demote(struct folio *folio,
Mike Kravetz8531fc62021-11-05 13:41:33 -07001500 unsigned int order)
1501{
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001502 __destroy_compound_gigantic_folio(folio, order, true);
Mike Kravetz8531fc62021-11-05 13:41:33 -07001503}
1504
1505#ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001506static void destroy_compound_gigantic_folio(struct folio *folio,
Mike Kravetz34d9e352021-11-05 13:41:30 -07001507 unsigned int order)
1508{
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001509 __destroy_compound_gigantic_folio(folio, order, false);
Mike Kravetz34d9e352021-11-05 13:41:30 -07001510}
1511
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001512static void free_gigantic_folio(struct folio *folio, unsigned int order)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001513{
Roman Gushchincf11e852020-04-10 14:32:45 -07001514 /*
1515 * If the page isn't allocated using the cma allocator,
1516 * cma_release() returns false.
1517 */
Barry Songdbda8fe2020-07-23 21:15:30 -07001518#ifdef CONFIG_CMA
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001519 int nid = folio_nid(folio);
1520
1521 if (cma_release(hugetlb_cma[nid], &folio->page, 1 << order))
Roman Gushchincf11e852020-04-10 14:32:45 -07001522 return;
Barry Songdbda8fe2020-07-23 21:15:30 -07001523#endif
Roman Gushchincf11e852020-04-10 14:32:45 -07001524
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001525 free_contig_range(folio_pfn(folio), 1 << order);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001526}
1527
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001528#ifdef CONFIG_CONTIG_ALLOC
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001529static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
Michal Hockod9cc948f2018-01-31 16:20:44 -08001530 int nid, nodemask_t *nodemask)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001531{
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001532 struct page *page;
Miaohe Lin04adbc32021-05-04 18:33:22 -07001533 unsigned long nr_pages = pages_per_huge_page(h);
Li Xinhai953f0642020-09-04 16:36:10 -07001534 if (nid == NUMA_NO_NODE)
1535 nid = numa_mem_id();
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001536
Barry Songdbda8fe2020-07-23 21:15:30 -07001537#ifdef CONFIG_CMA
1538 {
Roman Gushchincf11e852020-04-10 14:32:45 -07001539 int node;
1540
Li Xinhai953f0642020-09-04 16:36:10 -07001541 if (hugetlb_cma[nid]) {
1542 page = cma_alloc(hugetlb_cma[nid], nr_pages,
1543 huge_page_order(h), true);
Roman Gushchincf11e852020-04-10 14:32:45 -07001544 if (page)
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001545 return page_folio(page);
Roman Gushchincf11e852020-04-10 14:32:45 -07001546 }
Li Xinhai953f0642020-09-04 16:36:10 -07001547
1548 if (!(gfp_mask & __GFP_THISNODE)) {
1549 for_each_node_mask(node, *nodemask) {
1550 if (node == nid || !hugetlb_cma[node])
1551 continue;
1552
1553 page = cma_alloc(hugetlb_cma[node], nr_pages,
1554 huge_page_order(h), true);
1555 if (page)
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001556 return page_folio(page);
Li Xinhai953f0642020-09-04 16:36:10 -07001557 }
1558 }
Roman Gushchincf11e852020-04-10 14:32:45 -07001559 }
Barry Songdbda8fe2020-07-23 21:15:30 -07001560#endif
Roman Gushchincf11e852020-04-10 14:32:45 -07001561
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001562 page = alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask);
1563 return page ? page_folio(page) : NULL;
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001564}
1565
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001566#else /* !CONFIG_CONTIG_ALLOC */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001567static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001568 int nid, nodemask_t *nodemask)
1569{
1570 return NULL;
1571}
1572#endif /* CONFIG_CONTIG_ALLOC */
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001573
Aneesh Kumar K.Ve1073d12017-07-06 15:39:17 -07001574#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001575static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001576 int nid, nodemask_t *nodemask)
1577{
1578 return NULL;
1579}
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001580static inline void free_gigantic_folio(struct folio *folio,
1581 unsigned int order) { }
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001582static inline void destroy_compound_gigantic_folio(struct folio *folio,
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001583 unsigned int order) { }
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001584#endif
1585
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001586/*
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001587 * Remove hugetlb folio from lists, and update dtor so that the folio appears
Mike Kravetz34d9e352021-11-05 13:41:30 -07001588 * as just a compound page.
1589 *
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001590 * A reference is held on the folio, except in the case of demote.
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001591 *
1592 * Must be called with hugetlb lock held.
1593 */
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001594static void __remove_hugetlb_folio(struct hstate *h, struct folio *folio,
Mike Kravetz34d9e352021-11-05 13:41:30 -07001595 bool adjust_surplus,
1596 bool demote)
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001597{
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001598 int nid = folio_nid(folio);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001599
Sidhartha Kumarf0747322022-11-01 15:30:52 -07001600 VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio(folio), folio);
1601 VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio_rsvd(folio), folio);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001602
Mike Kravetz9487ca62021-05-04 18:35:10 -07001603 lockdep_assert_held(&hugetlb_lock);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001604 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
1605 return;
1606
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001607 list_del(&folio->lru);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001608
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001609 if (folio_test_hugetlb_freed(folio)) {
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001610 h->free_huge_pages--;
1611 h->free_huge_pages_node[nid]--;
1612 }
1613 if (adjust_surplus) {
1614 h->surplus_huge_pages--;
1615 h->surplus_huge_pages_node[nid]--;
1616 }
1617
Mike Kravetze32d20c2021-09-02 14:58:50 -07001618 /*
1619 * Very subtle
1620 *
1621 * For non-gigantic pages set the destructor to the normal compound
1622 * page dtor. This is needed in case someone takes an additional
1623 * temporary ref to the page, and freeing is delayed until they drop
1624 * their reference.
1625 *
1626 * For gigantic pages set the destructor to the null dtor. This
1627 * destructor will never be called. Before freeing the gigantic
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001628 * page destroy_compound_gigantic_folio will turn the folio into a
1629 * simple group of pages. After this the destructor does not
Mike Kravetze32d20c2021-09-02 14:58:50 -07001630 * apply.
1631 *
1632 * This handles the case where more than one ref is held when and
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001633 * after update_and_free_hugetlb_folio is called.
Mike Kravetz34d9e352021-11-05 13:41:30 -07001634 *
1635 * In the case of demote we do not ref count the page as it will soon
1636 * be turned into a page of smaller size.
Mike Kravetze32d20c2021-09-02 14:58:50 -07001637 */
Mike Kravetz34d9e352021-11-05 13:41:30 -07001638 if (!demote)
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001639 folio_ref_unfreeze(folio, 1);
Mike Kravetze32d20c2021-09-02 14:58:50 -07001640 if (hstate_is_gigantic(h))
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001641 folio_set_compound_dtor(folio, NULL_COMPOUND_DTOR);
Mike Kravetze32d20c2021-09-02 14:58:50 -07001642 else
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001643 folio_set_compound_dtor(folio, COMPOUND_PAGE_DTOR);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001644
1645 h->nr_huge_pages--;
1646 h->nr_huge_pages_node[nid]--;
1647}
1648
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001649static void remove_hugetlb_folio(struct hstate *h, struct folio *folio,
Mike Kravetz34d9e352021-11-05 13:41:30 -07001650 bool adjust_surplus)
1651{
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001652 __remove_hugetlb_folio(h, folio, adjust_surplus, false);
Mike Kravetz34d9e352021-11-05 13:41:30 -07001653}
1654
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001655static void remove_hugetlb_folio_for_demote(struct hstate *h, struct folio *folio,
Mike Kravetz8531fc62021-11-05 13:41:33 -07001656 bool adjust_surplus)
1657{
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001658 __remove_hugetlb_folio(h, folio, adjust_surplus, true);
Mike Kravetz8531fc62021-11-05 13:41:33 -07001659}
1660
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001661static void add_hugetlb_folio(struct hstate *h, struct folio *folio,
Muchun Songad2fa372021-06-30 18:47:21 -07001662 bool adjust_surplus)
1663{
1664 int zeroed;
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001665 int nid = folio_nid(folio);
Muchun Songad2fa372021-06-30 18:47:21 -07001666
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001667 VM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio);
Muchun Songad2fa372021-06-30 18:47:21 -07001668
1669 lockdep_assert_held(&hugetlb_lock);
1670
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001671 INIT_LIST_HEAD(&folio->lru);
Muchun Songad2fa372021-06-30 18:47:21 -07001672 h->nr_huge_pages++;
1673 h->nr_huge_pages_node[nid]++;
1674
1675 if (adjust_surplus) {
1676 h->surplus_huge_pages++;
1677 h->surplus_huge_pages_node[nid]++;
1678 }
1679
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001680 folio_set_compound_dtor(folio, HUGETLB_PAGE_DTOR);
1681 folio_change_private(folio, NULL);
Miaohe Lina9e1eab2022-09-01 20:00:28 +08001682 /*
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001683 * We have to set hugetlb_vmemmap_optimized again as above
1684 * folio_change_private(folio, NULL) cleared it.
Miaohe Lina9e1eab2022-09-01 20:00:28 +08001685 */
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001686 folio_set_hugetlb_vmemmap_optimized(folio);
Muchun Songad2fa372021-06-30 18:47:21 -07001687
1688 /*
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001689 * This folio is about to be managed by the hugetlb allocator and
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07001690 * should have no users. Drop our reference, and check for others
1691 * just in case.
Muchun Songad2fa372021-06-30 18:47:21 -07001692 */
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001693 zeroed = folio_put_testzero(folio);
1694 if (unlikely(!zeroed))
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07001695 /*
1696 * It is VERY unlikely soneone else has taken a ref on
1697 * the page. In this case, we simply return as the
1698 * hugetlb destructor (free_huge_page) will be called
1699 * when this other ref is dropped.
1700 */
1701 return;
1702
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001703 arch_clear_hugepage_flags(&folio->page);
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001704 enqueue_hugetlb_folio(h, folio);
Muchun Songad2fa372021-06-30 18:47:21 -07001705}
1706
Muchun Songb65d4ad2021-06-30 18:47:17 -07001707static void __update_and_free_page(struct hstate *h, struct page *page)
Adam Litke6af2acb2007-10-16 01:26:16 -07001708{
1709 int i;
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001710 struct folio *folio = page_folio(page);
Cheng Li14455ea2022-09-09 07:31:09 +00001711 struct page *subpage;
Andi Kleena5516432008-07-23 21:27:41 -07001712
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001713 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001714 return;
Andy Whitcroft18229df2008-11-06 12:53:27 -08001715
Naoya Horiguchi161df602022-07-14 13:24:15 +09001716 /*
1717 * If we don't know which subpages are hwpoisoned, we can't free
1718 * the hugepage, so it's leaked intentionally.
1719 */
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001720 if (folio_test_hugetlb_raw_hwp_unreliable(folio))
Naoya Horiguchi161df602022-07-14 13:24:15 +09001721 return;
1722
Muchun Song62138342022-06-28 17:22:33 +08001723 if (hugetlb_vmemmap_restore(h, page)) {
Muchun Songad2fa372021-06-30 18:47:21 -07001724 spin_lock_irq(&hugetlb_lock);
1725 /*
1726 * If we cannot allocate vmemmap pages, just refuse to free the
1727 * page and put the page back on the hugetlb free list and treat
1728 * as a surplus page.
1729 */
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001730 add_hugetlb_folio(h, folio, true);
Muchun Songad2fa372021-06-30 18:47:21 -07001731 spin_unlock_irq(&hugetlb_lock);
1732 return;
1733 }
1734
Naoya Horiguchi161df602022-07-14 13:24:15 +09001735 /*
1736 * Move PageHWPoison flag from head page to the raw error pages,
1737 * which makes any healthy subpages reusable.
1738 */
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001739 if (unlikely(folio_test_hwpoison(folio)))
1740 hugetlb_clear_page_hwpoison(&folio->page);
Naoya Horiguchi161df602022-07-14 13:24:15 +09001741
Cheng Li14455ea2022-09-09 07:31:09 +00001742 for (i = 0; i < pages_per_huge_page(h); i++) {
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001743 subpage = folio_page(folio, i);
Mike Kravetzdbfee5a2021-02-24 12:07:50 -08001744 subpage->flags &= ~(1 << PG_locked | 1 << PG_error |
Chris Forbes32f84522011-07-25 17:12:14 -07001745 1 << PG_referenced | 1 << PG_dirty |
Luiz Capitulinoa7407a22014-06-04 16:07:09 -07001746 1 << PG_active | 1 << PG_private |
1747 1 << PG_writeback);
Adam Litke6af2acb2007-10-16 01:26:16 -07001748 }
Mike Kravetza01f4392021-11-05 13:41:27 -07001749
1750 /*
1751 * Non-gigantic pages demoted from CMA allocated gigantic pages
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001752 * need to be given back to CMA in free_gigantic_folio.
Mike Kravetza01f4392021-11-05 13:41:27 -07001753 */
1754 if (hstate_is_gigantic(h) ||
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001755 hugetlb_cma_folio(folio, huge_page_order(h))) {
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001756 destroy_compound_gigantic_folio(folio, huge_page_order(h));
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001757 free_gigantic_folio(folio, huge_page_order(h));
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001758 } else {
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001759 __free_pages(page, huge_page_order(h));
1760 }
Adam Litke6af2acb2007-10-16 01:26:16 -07001761}
1762
Muchun Songb65d4ad2021-06-30 18:47:17 -07001763/*
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001764 * As update_and_free_hugetlb_folio() can be called under any context, so we cannot
Muchun Songb65d4ad2021-06-30 18:47:17 -07001765 * use GFP_KERNEL to allocate vmemmap pages. However, we can defer the
1766 * actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate
1767 * the vmemmap pages.
1768 *
1769 * free_hpage_workfn() locklessly retrieves the linked list of pages to be
1770 * freed and frees them one-by-one. As the page->mapping pointer is going
1771 * to be cleared in free_hpage_workfn() anyway, it is reused as the llist_node
1772 * structure of a lockless linked list of huge pages to be freed.
1773 */
1774static LLIST_HEAD(hpage_freelist);
1775
1776static void free_hpage_workfn(struct work_struct *work)
1777{
1778 struct llist_node *node;
1779
1780 node = llist_del_all(&hpage_freelist);
1781
1782 while (node) {
1783 struct page *page;
1784 struct hstate *h;
1785
1786 page = container_of((struct address_space **)node,
1787 struct page, mapping);
1788 node = node->next;
1789 page->mapping = NULL;
1790 /*
1791 * The VM_BUG_ON_PAGE(!PageHuge(page), page) in page_hstate()
1792 * is going to trigger because a previous call to
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001793 * remove_hugetlb_folio() will call folio_set_compound_dtor
1794 * (folio, NULL_COMPOUND_DTOR), so do not use page_hstate()
1795 * directly.
Muchun Songb65d4ad2021-06-30 18:47:17 -07001796 */
1797 h = size_to_hstate(page_size(page));
1798
1799 __update_and_free_page(h, page);
1800
1801 cond_resched();
1802 }
1803}
1804static DECLARE_WORK(free_hpage_work, free_hpage_workfn);
1805
1806static inline void flush_free_hpage_work(struct hstate *h)
1807{
Muchun Song62138342022-06-28 17:22:33 +08001808 if (hugetlb_vmemmap_optimizable(h))
Muchun Songb65d4ad2021-06-30 18:47:17 -07001809 flush_work(&free_hpage_work);
1810}
1811
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001812static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio,
Muchun Songb65d4ad2021-06-30 18:47:17 -07001813 bool atomic)
1814{
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001815 if (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) {
1816 __update_and_free_page(h, &folio->page);
Muchun Songb65d4ad2021-06-30 18:47:17 -07001817 return;
1818 }
1819
1820 /*
1821 * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap pages.
1822 *
1823 * Only call schedule_work() if hpage_freelist is previously
1824 * empty. Otherwise, schedule_work() had been called but the workfn
1825 * hasn't retrieved the list yet.
1826 */
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001827 if (llist_add((struct llist_node *)&folio->mapping, &hpage_freelist))
Muchun Songb65d4ad2021-06-30 18:47:17 -07001828 schedule_work(&free_hpage_work);
1829}
1830
Mike Kravetz10c6ec42021-05-04 18:35:03 -07001831static void update_and_free_pages_bulk(struct hstate *h, struct list_head *list)
1832{
1833 struct page *page, *t_page;
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001834 struct folio *folio;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07001835
1836 list_for_each_entry_safe(page, t_page, list, lru) {
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001837 folio = page_folio(page);
1838 update_and_free_hugetlb_folio(h, folio, false);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07001839 cond_resched();
1840 }
1841}
1842
Andi Kleene5ff2152008-07-23 21:27:42 -07001843struct hstate *size_to_hstate(unsigned long size)
1844{
1845 struct hstate *h;
1846
1847 for_each_hstate(h) {
1848 if (huge_page_size(h) == size)
1849 return h;
1850 }
1851 return NULL;
1852}
1853
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001854void free_huge_page(struct page *page)
David Gibson27a85ef2006-03-22 00:08:56 -08001855{
Andi Kleena5516432008-07-23 21:27:41 -07001856 /*
1857 * Can't pass hstate in here because it is called from the
1858 * compound page destructor.
1859 */
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001860 struct folio *folio = page_folio(page);
1861 struct hstate *h = folio_hstate(folio);
1862 int nid = folio_nid(folio);
1863 struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
Joonsoo Kim07443a82013-09-11 14:21:58 -07001864 bool restore_reserve;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001865 unsigned long flags;
David Gibson27a85ef2006-03-22 00:08:56 -08001866
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001867 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
1868 VM_BUG_ON_FOLIO(folio_mapcount(folio), folio);
Yongkai Wu8ace22b2018-12-14 14:17:10 -08001869
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001870 hugetlb_set_folio_subpool(folio, NULL);
1871 if (folio_test_anon(folio))
1872 __ClearPageAnonExclusive(&folio->page);
1873 folio->mapping = NULL;
1874 restore_reserve = folio_test_hugetlb_restore_reserve(folio);
1875 folio_clear_hugetlb_restore_reserve(folio);
David Gibson27a85ef2006-03-22 00:08:56 -08001876
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001877 /*
Mike Kravetzd6995da2021-02-24 12:08:51 -08001878 * If HPageRestoreReserve was set on page, page allocation consumed a
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001879 * reservation. If the page was associated with a subpool, there
1880 * would have been a page reserved in the subpool before allocation
1881 * via hugepage_subpool_get_pages(). Since we are 'restoring' the
Miaohe Lin6c26d312021-02-24 12:07:19 -08001882 * reservation, do not call hugepage_subpool_put_pages() as this will
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001883 * remove the reserved page from the subpool.
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001884 */
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001885 if (!restore_reserve) {
1886 /*
1887 * A return code of zero implies that the subpool will be
1888 * under its minimum size if the reservation is not restored
1889 * after page is free. Therefore, force restore_reserve
1890 * operation.
1891 */
1892 if (hugepage_subpool_put_pages(spool, 1) == 0)
1893 restore_reserve = true;
1894 }
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001895
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001896 spin_lock_irqsave(&hugetlb_lock, flags);
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001897 folio_clear_hugetlb_migratable(folio);
Sidhartha Kumard4ab0312022-11-01 15:30:57 -07001898 hugetlb_cgroup_uncharge_folio(hstate_index(h),
1899 pages_per_huge_page(h), folio);
1900 hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h),
1901 pages_per_huge_page(h), folio);
Joonsoo Kim07443a82013-09-11 14:21:58 -07001902 if (restore_reserve)
1903 h->resv_huge_pages++;
1904
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001905 if (folio_test_hugetlb_temporary(folio)) {
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001906 remove_hugetlb_folio(h, folio, false);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001907 spin_unlock_irqrestore(&hugetlb_lock, flags);
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001908 update_and_free_hugetlb_folio(h, folio, true);
Michal Hockoab5ac902018-01-31 16:20:48 -08001909 } else if (h->surplus_huge_pages_node[nid]) {
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07001910 /* remove the page from active list */
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001911 remove_hugetlb_folio(h, folio, true);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001912 spin_unlock_irqrestore(&hugetlb_lock, flags);
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001913 update_and_free_hugetlb_folio(h, folio, true);
Adam Litke7893d1d2007-10-16 01:26:18 -07001914 } else {
Will Deacon5d3a5512012-10-08 16:29:32 -07001915 arch_clear_hugepage_flags(page);
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001916 enqueue_hugetlb_folio(h, folio);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001917 spin_unlock_irqrestore(&hugetlb_lock, flags);
Adam Litke7893d1d2007-10-16 01:26:18 -07001918 }
David Gibson27a85ef2006-03-22 00:08:56 -08001919}
1920
Oscar Salvadord3d99fc2021-05-04 18:35:23 -07001921/*
1922 * Must be called with the hugetlb lock held
1923 */
1924static void __prep_account_new_huge_page(struct hstate *h, int nid)
1925{
1926 lockdep_assert_held(&hugetlb_lock);
1927 h->nr_huge_pages++;
1928 h->nr_huge_pages_node[nid]++;
1929}
1930
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07001931static void __prep_new_hugetlb_folio(struct hstate *h, struct folio *folio)
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001932{
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07001933 hugetlb_vmemmap_optimize(h, &folio->page);
1934 INIT_LIST_HEAD(&folio->lru);
Sidhartha Kumar9fd33052022-11-29 14:50:30 -08001935 folio_set_compound_dtor(folio, HUGETLB_PAGE_DTOR);
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07001936 hugetlb_set_folio_subpool(folio, NULL);
1937 set_hugetlb_cgroup(folio, NULL);
1938 set_hugetlb_cgroup_rsvd(folio, NULL);
Oscar Salvadord3d99fc2021-05-04 18:35:23 -07001939}
1940
Sidhartha Kumard1c60952022-11-29 14:50:38 -08001941static void prep_new_hugetlb_folio(struct hstate *h, struct folio *folio, int nid)
Oscar Salvadord3d99fc2021-05-04 18:35:23 -07001942{
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07001943 __prep_new_hugetlb_folio(h, folio);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001944 spin_lock_irq(&hugetlb_lock);
Oscar Salvadord3d99fc2021-05-04 18:35:23 -07001945 __prep_account_new_huge_page(h, nid);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001946 spin_unlock_irq(&hugetlb_lock);
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001947}
1948
Sidhartha Kumard1c60952022-11-29 14:50:38 -08001949static bool __prep_compound_gigantic_folio(struct folio *folio,
1950 unsigned int order, bool demote)
Wu Fengguang20a03072009-06-16 15:32:22 -07001951{
Mike Kravetz7118fc22021-06-30 18:48:34 -07001952 int i, j;
Wu Fengguang20a03072009-06-16 15:32:22 -07001953 int nr_pages = 1 << order;
Cheng Li14455ea2022-09-09 07:31:09 +00001954 struct page *p;
Wu Fengguang20a03072009-06-16 15:32:22 -07001955
Sidhartha Kumard1c60952022-11-29 14:50:38 -08001956 __folio_clear_reserved(folio);
1957 __folio_set_head(folio);
Sidhartha Kumarc45bc55a2022-12-12 14:55:29 -08001958 /* we rely on prep_new_hugetlb_folio to set the destructor */
1959 folio_set_compound_order(folio, order);
Mike Kravetz2b216242022-09-16 14:46:38 -07001960 for (i = 0; i < nr_pages; i++) {
Sidhartha Kumard1c60952022-11-29 14:50:38 -08001961 p = folio_page(folio, i);
Cheng Li14455ea2022-09-09 07:31:09 +00001962
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001963 /*
1964 * For gigantic hugepages allocated through bootmem at
1965 * boot, it's safer to be consistent with the not-gigantic
1966 * hugepages and clear the PG_reserved bit from all tail pages
Ethon Paul7c8de352020-06-04 16:49:07 -07001967 * too. Otherwise drivers using get_user_pages() to access tail
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001968 * pages may get the reference counting wrong if they see
1969 * PG_reserved set on a tail page (despite the head page not
1970 * having PG_reserved set). Enforcing this consistency between
1971 * head and tail pages allows drivers to optimize away a check
1972 * on the head page when they need know if put_page() is needed
1973 * after get_user_pages().
1974 */
Mike Kravetz7fb07282022-11-18 11:52:49 -08001975 if (i != 0) /* head page cleared above */
1976 __ClearPageReserved(p);
Mike Kravetz7118fc22021-06-30 18:48:34 -07001977 /*
1978 * Subtle and very unlikely
1979 *
1980 * Gigantic 'page allocators' such as memblock or cma will
1981 * return a set of pages with each page ref counted. We need
1982 * to turn this set of pages into a compound page with tail
1983 * page ref counts set to zero. Code such as speculative page
1984 * cache adding could take a ref on a 'to be' tail page.
1985 * We need to respect any increased ref count, and only set
1986 * the ref count to zero if count is currently 1. If count
Mike Kravetz416d85e2021-09-02 14:58:43 -07001987 * is not 1, we return an error. An error return indicates
1988 * the set of pages can not be converted to a gigantic page.
1989 * The caller who allocated the pages should then discard the
1990 * pages using the appropriate free interface.
Mike Kravetz34d9e352021-11-05 13:41:30 -07001991 *
1992 * In the case of demote, the ref count will be zero.
Mike Kravetz7118fc22021-06-30 18:48:34 -07001993 */
Mike Kravetz34d9e352021-11-05 13:41:30 -07001994 if (!demote) {
1995 if (!page_ref_freeze(p, 1)) {
1996 pr_warn("HugeTLB page can not be used due to unexpected inflated ref count\n");
1997 goto out_error;
1998 }
1999 } else {
2000 VM_BUG_ON_PAGE(page_count(p), p);
Mike Kravetz7118fc22021-06-30 18:48:34 -07002001 }
Mike Kravetz2b216242022-09-16 14:46:38 -07002002 if (i != 0)
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002003 set_compound_head(p, &folio->page);
Wu Fengguang20a03072009-06-16 15:32:22 -07002004 }
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002005 atomic_set(folio_mapcount_ptr(folio), -1);
2006 atomic_set(folio_subpages_mapcount_ptr(folio), 0);
2007 atomic_set(folio_pincount_ptr(folio), 0);
Mike Kravetz7118fc22021-06-30 18:48:34 -07002008 return true;
2009
2010out_error:
Mike Kravetz2b216242022-09-16 14:46:38 -07002011 /* undo page modifications made above */
2012 for (j = 0; j < i; j++) {
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002013 p = folio_page(folio, j);
Mike Kravetz2b216242022-09-16 14:46:38 -07002014 if (j != 0)
2015 clear_compound_head(p);
Mike Kravetz7118fc22021-06-30 18:48:34 -07002016 set_page_refcounted(p);
2017 }
2018 /* need to clear PG_reserved on remaining tail pages */
Cheng Li14455ea2022-09-09 07:31:09 +00002019 for (; j < nr_pages; j++) {
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002020 p = folio_page(folio, j);
Mike Kravetz7118fc22021-06-30 18:48:34 -07002021 __ClearPageReserved(p);
Cheng Li14455ea2022-09-09 07:31:09 +00002022 }
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002023 folio_set_compound_order(folio, 0);
2024 __folio_clear_head(folio);
Mike Kravetz7118fc22021-06-30 18:48:34 -07002025 return false;
Wu Fengguang20a03072009-06-16 15:32:22 -07002026}
2027
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002028static bool prep_compound_gigantic_folio(struct folio *folio,
Mike Kravetz8531fc62021-11-05 13:41:33 -07002029 unsigned int order)
2030{
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002031 return __prep_compound_gigantic_folio(folio, order, false);
2032}
2033
2034static bool prep_compound_gigantic_folio_for_demote(struct folio *folio,
2035 unsigned int order)
2036{
2037 return __prep_compound_gigantic_folio(folio, order, true);
Mike Kravetz8531fc62021-11-05 13:41:33 -07002038}
2039
Andrew Morton77959122012-10-08 16:34:11 -07002040/*
2041 * PageHuge() only returns true for hugetlbfs pages, but not for normal or
2042 * transparent huge pages. See the PageTransHuge() documentation for more
2043 * details.
2044 */
Wu Fengguang20a03072009-06-16 15:32:22 -07002045int PageHuge(struct page *page)
2046{
Wu Fengguang20a03072009-06-16 15:32:22 -07002047 if (!PageCompound(page))
2048 return 0;
2049
2050 page = compound_head(page);
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -08002051 return page[1].compound_dtor == HUGETLB_PAGE_DTOR;
Wu Fengguang20a03072009-06-16 15:32:22 -07002052}
Naoya Horiguchi43131e12010-05-28 09:29:22 +09002053EXPORT_SYMBOL_GPL(PageHuge);
2054
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08002055/*
2056 * PageHeadHuge() only returns true for hugetlbfs head page, but not for
2057 * normal or transparent huge pages.
2058 */
2059int PageHeadHuge(struct page *page_head)
2060{
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08002061 if (!PageHead(page_head))
2062 return 0;
2063
Vlastimil Babkad4af73e2020-04-01 21:11:48 -07002064 return page_head[1].compound_dtor == HUGETLB_PAGE_DTOR;
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08002065}
David Howells4e936ec2022-03-22 14:45:26 -07002066EXPORT_SYMBOL_GPL(PageHeadHuge);
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08002067
Mike Kravetzc0d03812020-04-01 21:11:05 -07002068/*
Mike Kravetzc0d03812020-04-01 21:11:05 -07002069 * Find and lock address space (mapping) in write mode.
2070 *
Mike Kravetz336bf302020-11-13 22:52:16 -08002071 * Upon entry, the page is locked which means that page_mapping() is
2072 * stable. Due to locking order, we can only trylock_write. If we can
2073 * not get the lock, simply return NULL to caller.
Mike Kravetzc0d03812020-04-01 21:11:05 -07002074 */
2075struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage)
2076{
Mike Kravetz336bf302020-11-13 22:52:16 -08002077 struct address_space *mapping = page_mapping(hpage);
Mike Kravetzc0d03812020-04-01 21:11:05 -07002078
Mike Kravetzc0d03812020-04-01 21:11:05 -07002079 if (!mapping)
2080 return mapping;
2081
Mike Kravetzc0d03812020-04-01 21:11:05 -07002082 if (i_mmap_trylock_write(mapping))
2083 return mapping;
2084
Mike Kravetz336bf302020-11-13 22:52:16 -08002085 return NULL;
Mike Kravetzc0d03812020-04-01 21:11:05 -07002086}
2087
Hugh Dickinsfe19bd32021-06-24 18:39:52 -07002088pgoff_t hugetlb_basepage_index(struct page *page)
Zhang Yi13d60f42013-06-25 21:19:31 +08002089{
2090 struct page *page_head = compound_head(page);
2091 pgoff_t index = page_index(page_head);
2092 unsigned long compound_idx;
2093
Zhang Yi13d60f42013-06-25 21:19:31 +08002094 if (compound_order(page_head) >= MAX_ORDER)
2095 compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
2096 else
2097 compound_idx = page - page_head;
2098
2099 return (index << compound_order(page_head)) + compound_idx;
2100}
2101
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002102static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07002103 gfp_t gfp_mask, int nid, nodemask_t *nmask,
2104 nodemask_t *node_alloc_noretry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002106 int order = huge_page_order(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 struct page *page;
Mike Kravetzf60858f2019-09-23 15:37:35 -07002108 bool alloc_try_hard = true;
Mike Kravetz2b216242022-09-16 14:46:38 -07002109 bool retry = true;
Joe Jinf96efd52007-07-15 23:38:12 -07002110
Mike Kravetzf60858f2019-09-23 15:37:35 -07002111 /*
2112 * By default we always try hard to allocate the page with
2113 * __GFP_RETRY_MAYFAIL flag. However, if we are allocating pages in
2114 * a loop (to adjust global huge page counts) and previous allocation
2115 * failed, do not continue to try hard on the same node. Use the
2116 * node_alloc_noretry bitmap to manage this state information.
2117 */
2118 if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry))
2119 alloc_try_hard = false;
2120 gfp_mask |= __GFP_COMP|__GFP_NOWARN;
2121 if (alloc_try_hard)
2122 gfp_mask |= __GFP_RETRY_MAYFAIL;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002123 if (nid == NUMA_NO_NODE)
2124 nid = numa_mem_id();
Mike Kravetz2b216242022-09-16 14:46:38 -07002125retry:
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07002126 page = __alloc_pages(gfp_mask, order, nid, nmask);
Mike Kravetz2b216242022-09-16 14:46:38 -07002127
2128 /* Freeze head page */
2129 if (page && !page_ref_freeze(page, 1)) {
2130 __free_pages(page, order);
2131 if (retry) { /* retry once */
2132 retry = false;
2133 goto retry;
2134 }
2135 /* WOW! twice in a row. */
2136 pr_warn("HugeTLB head page unexpected inflated ref count\n");
2137 page = NULL;
2138 }
2139
Mike Kravetzf60858f2019-09-23 15:37:35 -07002140 /*
2141 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this
2142 * indicates an overall state change. Clear bit so that we resume
2143 * normal 'try hard' allocations.
2144 */
2145 if (node_alloc_noretry && page && !alloc_try_hard)
2146 node_clear(nid, *node_alloc_noretry);
2147
2148 /*
2149 * If we tried hard to get a page but failed, set bit so that
2150 * subsequent attempts will not try as hard until there is an
2151 * overall state change.
2152 */
2153 if (node_alloc_noretry && !page && alloc_try_hard)
2154 node_set(nid, *node_alloc_noretry);
2155
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002156 if (!page) {
2157 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
2158 return NULL;
2159 }
2160
2161 __count_vm_event(HTLB_BUDDY_PGALLOC);
2162 return page_folio(page);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -07002163}
2164
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002165/*
Michal Hocko0c397da2018-01-31 16:20:56 -08002166 * Common helper to allocate a fresh hugetlb page. All specific allocators
2167 * should use this function to get new hugetlb pages
Mike Kravetz2b216242022-09-16 14:46:38 -07002168 *
2169 * Note that returned page is 'frozen': ref count of head page and all tail
2170 * pages is zero.
Michal Hocko0c397da2018-01-31 16:20:56 -08002171 */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002172static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07002173 gfp_t gfp_mask, int nid, nodemask_t *nmask,
2174 nodemask_t *node_alloc_noretry)
Michal Hocko0c397da2018-01-31 16:20:56 -08002175{
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08002176 struct folio *folio;
Mike Kravetz7118fc22021-06-30 18:48:34 -07002177 bool retry = false;
Michal Hocko0c397da2018-01-31 16:20:56 -08002178
Mike Kravetz7118fc22021-06-30 18:48:34 -07002179retry:
Michal Hocko0c397da2018-01-31 16:20:56 -08002180 if (hstate_is_gigantic(h))
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002181 folio = alloc_gigantic_folio(h, gfp_mask, nid, nmask);
Michal Hocko0c397da2018-01-31 16:20:56 -08002182 else
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002183 folio = alloc_buddy_hugetlb_folio(h, gfp_mask,
Mike Kravetzf60858f2019-09-23 15:37:35 -07002184 nid, nmask, node_alloc_noretry);
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002185 if (!folio)
Michal Hocko0c397da2018-01-31 16:20:56 -08002186 return NULL;
Mike Kravetz7118fc22021-06-30 18:48:34 -07002187 if (hstate_is_gigantic(h)) {
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002188 if (!prep_compound_gigantic_folio(folio, huge_page_order(h))) {
Mike Kravetz7118fc22021-06-30 18:48:34 -07002189 /*
2190 * Rare failure to convert pages to compound page.
2191 * Free pages and try again - ONCE!
2192 */
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08002193 free_gigantic_folio(folio, huge_page_order(h));
Mike Kravetz7118fc22021-06-30 18:48:34 -07002194 if (!retry) {
2195 retry = true;
2196 goto retry;
2197 }
Mike Kravetz7118fc22021-06-30 18:48:34 -07002198 return NULL;
2199 }
2200 }
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002201 prep_new_hugetlb_folio(h, folio, folio_nid(folio));
Michal Hocko0c397da2018-01-31 16:20:56 -08002202
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002203 return folio;
Michal Hocko0c397da2018-01-31 16:20:56 -08002204}
2205
2206/*
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002207 * Allocates a fresh page to the hugetlb allocator pool in the node interleaved
2208 * manner.
2209 */
Mike Kravetzf60858f2019-09-23 15:37:35 -07002210static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
2211 nodemask_t *node_alloc_noretry)
Joonsoo Kimb2261022013-09-11 14:21:00 -07002212{
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002213 struct folio *folio;
Joonsoo Kimb2261022013-09-11 14:21:00 -07002214 int nr_nodes, node;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002215 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
Joonsoo Kimb2261022013-09-11 14:21:00 -07002216
2217 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002218 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, node,
2219 nodes_allowed, node_alloc_noretry);
2220 if (folio) {
2221 free_huge_page(&folio->page); /* free it into the hugepage allocator */
2222 return 1;
2223 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002224 }
2225
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002226 return 0;
Joonsoo Kimb2261022013-09-11 14:21:00 -07002227}
2228
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002229/*
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002230 * Remove huge page from pool from next node to free. Attempt to keep
2231 * persistent huge pages more or less balanced over allowed nodes.
2232 * This routine only 'removes' the hugetlb page. The caller must make
2233 * an additional call to free the page to low level allocators.
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002234 * Called with hugetlb_lock locked.
2235 */
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002236static struct page *remove_pool_huge_page(struct hstate *h,
2237 nodemask_t *nodes_allowed,
2238 bool acct_surplus)
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002239{
Joonsoo Kimb2261022013-09-11 14:21:00 -07002240 int nr_nodes, node;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002241 struct page *page = NULL;
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08002242 struct folio *folio;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002243
Mike Kravetz9487ca62021-05-04 18:35:10 -07002244 lockdep_assert_held(&hugetlb_lock);
Joonsoo Kimb2261022013-09-11 14:21:00 -07002245 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
Lee Schermerhorn685f3452009-09-21 17:01:23 -07002246 /*
2247 * If we're returning unused surplus pages, only examine
2248 * nodes with surplus pages.
2249 */
Joonsoo Kimb2261022013-09-11 14:21:00 -07002250 if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
2251 !list_empty(&h->hugepage_freelists[node])) {
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002252 page = list_entry(h->hugepage_freelists[node].next,
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002253 struct page, lru);
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08002254 folio = page_folio(page);
2255 remove_hugetlb_folio(h, folio, acct_surplus);
Lee Schermerhorn9a76db02009-12-14 17:58:15 -08002256 break;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002257 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002258 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002259
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002260 return page;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002261}
2262
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002263/*
2264 * Dissolve a given free hugepage into free buddy pages. This function does
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002265 * nothing for in-use hugepages and non-hugepages.
2266 * This function returns values like below:
2267 *
Muchun Songad2fa372021-06-30 18:47:21 -07002268 * -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages
2269 * when the system is under memory pressure and the feature of
2270 * freeing unused vmemmap pages associated with each hugetlb page
2271 * is enabled.
2272 * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
2273 * (allocated or reserved.)
2274 * 0: successfully dissolved free hugepages or the page is not a
2275 * hugepage (considered as already dissolved)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002276 */
Anshuman Khandualc3114a82017-07-10 15:47:41 -07002277int dissolve_free_huge_page(struct page *page)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002278{
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07002279 int rc = -EBUSY;
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002280 struct folio *folio = page_folio(page);
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002281
Muchun Song7ffddd42021-02-04 18:32:06 -08002282retry:
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002283 /* Not to disrupt normal path by vainly holding hugetlb_lock */
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002284 if (!folio_test_hugetlb(folio))
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002285 return 0;
2286
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002287 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002288 if (!folio_test_hugetlb(folio)) {
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002289 rc = 0;
2290 goto out;
2291 }
2292
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002293 if (!folio_ref_count(folio)) {
2294 struct hstate *h = folio_hstate(folio);
Xin Hao8346d692022-09-22 10:19:29 +08002295 if (!available_huge_pages(h))
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002296 goto out;
Muchun Song7ffddd42021-02-04 18:32:06 -08002297
2298 /*
2299 * We should make sure that the page is already on the free list
2300 * when it is dissolved.
2301 */
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002302 if (unlikely(!folio_test_hugetlb_freed(folio))) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002303 spin_unlock_irq(&hugetlb_lock);
Muchun Song7ffddd42021-02-04 18:32:06 -08002304 cond_resched();
2305
2306 /*
2307 * Theoretically, we should return -EBUSY when we
2308 * encounter this race. In fact, we have a chance
2309 * to successfully dissolve the page if we do a
2310 * retry. Because the race window is quite small.
2311 * If we seize this opportunity, it is an optimization
2312 * for increasing the success rate of dissolving page.
2313 */
2314 goto retry;
2315 }
2316
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08002317 remove_hugetlb_folio(h, folio, false);
zhong jiangc1470b32016-08-11 15:32:55 -07002318 h->max_huge_pages--;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002319 spin_unlock_irq(&hugetlb_lock);
Muchun Songad2fa372021-06-30 18:47:21 -07002320
2321 /*
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08002322 * Normally update_and_free_hugtlb_folio will allocate required vmemmmap
2323 * before freeing the page. update_and_free_hugtlb_folio will fail to
Muchun Songad2fa372021-06-30 18:47:21 -07002324 * free the page if it can not allocate required vmemmap. We
2325 * need to adjust max_huge_pages if the page is not freed.
2326 * Attempt to allocate vmemmmap here so that we can take
2327 * appropriate action on failure.
2328 */
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002329 rc = hugetlb_vmemmap_restore(h, &folio->page);
Muchun Songad2fa372021-06-30 18:47:21 -07002330 if (!rc) {
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08002331 update_and_free_hugetlb_folio(h, folio, false);
Muchun Songad2fa372021-06-30 18:47:21 -07002332 } else {
2333 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08002334 add_hugetlb_folio(h, folio, false);
Muchun Songad2fa372021-06-30 18:47:21 -07002335 h->max_huge_pages++;
2336 spin_unlock_irq(&hugetlb_lock);
2337 }
2338
2339 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002340 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002341out:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002342 spin_unlock_irq(&hugetlb_lock);
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002343 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002344}
2345
2346/*
2347 * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
2348 * make specified memory blocks removable from the system.
Gerald Schaefer2247bb32016-10-07 17:01:07 -07002349 * Note that this will dissolve a free gigantic hugepage completely, if any
2350 * part of it lies within the given range.
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002351 * Also note that if dissolve_free_huge_page() returns with an error, all
2352 * free hugepages that were dissolved before that error are lost.
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002353 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002354int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002355{
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002356 unsigned long pfn;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07002357 struct page *page;
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002358 int rc = 0;
Muchun Songdc2628f2022-06-16 11:38:46 +08002359 unsigned int order;
2360 struct hstate *h;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002361
Li Zhongd0177632014-08-06 16:07:56 -07002362 if (!hugepages_supported())
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002363 return rc;
Li Zhongd0177632014-08-06 16:07:56 -07002364
Muchun Songdc2628f2022-06-16 11:38:46 +08002365 order = huge_page_order(&default_hstate);
2366 for_each_hstate(h)
2367 order = min(order, huge_page_order(h));
2368
2369 for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) {
Gerald Schaefereb03aa02016-10-07 17:01:13 -07002370 page = pfn_to_page(pfn);
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002371 rc = dissolve_free_huge_page(page);
2372 if (rc)
2373 break;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07002374 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002375
2376 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002377}
2378
Michal Hockoab5ac902018-01-31 16:20:48 -08002379/*
2380 * Allocates a fresh surplus page from the page allocator.
2381 */
Michal Hocko0c397da2018-01-31 16:20:56 -08002382static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,
Mike Kravetz2b216242022-09-16 14:46:38 -07002383 int nid, nodemask_t *nmask)
Adam Litke7893d1d2007-10-16 01:26:18 -07002384{
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002385 struct folio *folio = NULL;
Adam Litke7893d1d2007-10-16 01:26:18 -07002386
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002387 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07002388 return NULL;
2389
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002390 spin_lock_irq(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08002391 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages)
2392 goto out_unlock;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002393 spin_unlock_irq(&hugetlb_lock);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002394
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002395 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL);
2396 if (!folio)
Michal Hocko0c397da2018-01-31 16:20:56 -08002397 return NULL;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002398
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002399 spin_lock_irq(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08002400 /*
2401 * We could have raced with the pool size change.
2402 * Double check that and simply deallocate the new page
2403 * if we would end up overcommiting the surpluses. Abuse
2404 * temporary page to workaround the nasty free_huge_page
2405 * codeflow
2406 */
2407 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002408 folio_set_hugetlb_temporary(folio);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002409 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002410 free_huge_page(&folio->page);
Kai Shen2bf753e2019-05-13 17:15:37 -07002411 return NULL;
Adam Litke7893d1d2007-10-16 01:26:18 -07002412 }
Michal Hocko9980d742018-01-31 16:20:52 -08002413
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002414 h->surplus_huge_pages++;
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002415 h->surplus_huge_pages_node[folio_nid(folio)]++;
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002416
Michal Hocko9980d742018-01-31 16:20:52 -08002417out_unlock:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002418 spin_unlock_irq(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07002419
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002420 return &folio->page;
Adam Litke7893d1d2007-10-16 01:26:18 -07002421}
2422
Joonsoo Kimbbe88752020-08-11 18:37:38 -07002423static struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08002424 int nid, nodemask_t *nmask)
Michal Hockoab5ac902018-01-31 16:20:48 -08002425{
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002426 struct folio *folio;
Michal Hockoab5ac902018-01-31 16:20:48 -08002427
2428 if (hstate_is_gigantic(h))
2429 return NULL;
2430
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002431 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL);
2432 if (!folio)
Michal Hockoab5ac902018-01-31 16:20:48 -08002433 return NULL;
2434
Mike Kravetz2b216242022-09-16 14:46:38 -07002435 /* fresh huge pages are frozen */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002436 folio_ref_unfreeze(folio, 1);
Michal Hockoab5ac902018-01-31 16:20:48 -08002437 /*
2438 * We do not account these pages as surplus because they are only
2439 * temporary and will be released properly on the last reference
2440 */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002441 folio_set_hugetlb_temporary(folio);
Michal Hockoab5ac902018-01-31 16:20:48 -08002442
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002443 return &folio->page;
Michal Hockoab5ac902018-01-31 16:20:48 -08002444}
2445
Adam Litkee4e574b2007-10-16 01:26:19 -07002446/*
Dave Hansen099730d2015-11-05 18:50:17 -08002447 * Use the VMA's mpolicy to allocate a huge page from the buddy.
2448 */
Dave Hansene0ec90e2015-11-05 18:50:20 -08002449static
Michal Hocko0c397da2018-01-31 16:20:56 -08002450struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h,
Dave Hansen099730d2015-11-05 18:50:17 -08002451 struct vm_area_struct *vma, unsigned long addr)
2452{
Ben Widawskycfcaa662021-09-02 15:00:13 -07002453 struct page *page = NULL;
Michal Hockoaaf14e42017-07-10 15:49:08 -07002454 struct mempolicy *mpol;
2455 gfp_t gfp_mask = htlb_alloc_mask(h);
2456 int nid;
2457 nodemask_t *nodemask;
2458
2459 nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07002460 if (mpol_is_preferred_many(mpol)) {
2461 gfp_t gfp = gfp_mask | __GFP_NOWARN;
Michal Hockoaaf14e42017-07-10 15:49:08 -07002462
Ben Widawskycfcaa662021-09-02 15:00:13 -07002463 gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
Mike Kravetz2b216242022-09-16 14:46:38 -07002464 page = alloc_surplus_huge_page(h, gfp, nid, nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07002465
2466 /* Fallback to all nodes if page==NULL */
2467 nodemask = NULL;
2468 }
2469
2470 if (!page)
Mike Kravetz2b216242022-09-16 14:46:38 -07002471 page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07002472 mpol_cond_put(mpol);
Michal Hockoaaf14e42017-07-10 15:49:08 -07002473 return page;
Dave Hansen099730d2015-11-05 18:50:17 -08002474}
2475
Michal Hockoab5ac902018-01-31 16:20:48 -08002476/* page migration callback function */
Michal Hocko3e59fcb2017-07-10 15:49:11 -07002477struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07002478 nodemask_t *nmask, gfp_t gfp_mask)
Michal Hocko4db9b2e2017-07-10 15:48:44 -07002479{
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002480 spin_lock_irq(&hugetlb_lock);
Xin Hao8346d692022-09-22 10:19:29 +08002481 if (available_huge_pages(h)) {
Michal Hocko3e59fcb2017-07-10 15:49:11 -07002482 struct page *page;
2483
2484 page = dequeue_huge_page_nodemask(h, gfp_mask, preferred_nid, nmask);
2485 if (page) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002486 spin_unlock_irq(&hugetlb_lock);
Michal Hocko3e59fcb2017-07-10 15:49:11 -07002487 return page;
Michal Hocko4db9b2e2017-07-10 15:48:44 -07002488 }
2489 }
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002490 spin_unlock_irq(&hugetlb_lock);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07002491
Michal Hocko0c397da2018-01-31 16:20:56 -08002492 return alloc_migrate_huge_page(h, gfp_mask, preferred_nid, nmask);
Michal Hocko4db9b2e2017-07-10 15:48:44 -07002493}
2494
Michal Hockoebd63722018-01-31 16:21:00 -08002495/* mempolicy aware migration callback */
Michal Hocko389c8172018-01-31 16:21:03 -08002496struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
2497 unsigned long address)
Michal Hockoebd63722018-01-31 16:21:00 -08002498{
2499 struct mempolicy *mpol;
2500 nodemask_t *nodemask;
2501 struct page *page;
Michal Hockoebd63722018-01-31 16:21:00 -08002502 gfp_t gfp_mask;
2503 int node;
2504
Michal Hockoebd63722018-01-31 16:21:00 -08002505 gfp_mask = htlb_alloc_mask(h);
2506 node = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07002507 page = alloc_huge_page_nodemask(h, node, nodemask, gfp_mask);
Michal Hockoebd63722018-01-31 16:21:00 -08002508 mpol_cond_put(mpol);
2509
2510 return page;
2511}
2512
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09002513/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002514 * Increase the hugetlb pool such that it can accommodate a reservation
Adam Litkee4e574b2007-10-16 01:26:19 -07002515 * of size 'delta'.
2516 */
Liu Xiang0a4f3d12020-12-14 19:12:05 -08002517static int gather_surplus_pages(struct hstate *h, long delta)
Jules Irenge1b2a1e72020-04-06 20:08:09 -07002518 __must_hold(&hugetlb_lock)
Adam Litkee4e574b2007-10-16 01:26:19 -07002519{
Miaohe Lin34665342022-09-01 20:00:23 +08002520 LIST_HEAD(surplus_list);
Adam Litkee4e574b2007-10-16 01:26:19 -07002521 struct page *page, *tmp;
Liu Xiang0a4f3d12020-12-14 19:12:05 -08002522 int ret;
2523 long i;
2524 long needed, allocated;
Hillf Danton28073b02012-03-21 16:34:00 -07002525 bool alloc_ok = true;
Adam Litkee4e574b2007-10-16 01:26:19 -07002526
Mike Kravetz9487ca62021-05-04 18:35:10 -07002527 lockdep_assert_held(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002528 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -08002529 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -07002530 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07002531 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -08002532 }
Adam Litkee4e574b2007-10-16 01:26:19 -07002533
2534 allocated = 0;
Adam Litkee4e574b2007-10-16 01:26:19 -07002535
2536 ret = -ENOMEM;
2537retry:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002538 spin_unlock_irq(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07002539 for (i = 0; i < needed; i++) {
Michal Hocko0c397da2018-01-31 16:20:56 -08002540 page = alloc_surplus_huge_page(h, htlb_alloc_mask(h),
Mike Kravetz2b216242022-09-16 14:46:38 -07002541 NUMA_NO_NODE, NULL);
Hillf Danton28073b02012-03-21 16:34:00 -07002542 if (!page) {
2543 alloc_ok = false;
2544 break;
2545 }
Adam Litkee4e574b2007-10-16 01:26:19 -07002546 list_add(&page->lru, &surplus_list);
David Rientjes69ed7792017-07-10 15:48:50 -07002547 cond_resched();
Adam Litkee4e574b2007-10-16 01:26:19 -07002548 }
Hillf Danton28073b02012-03-21 16:34:00 -07002549 allocated += i;
Adam Litkee4e574b2007-10-16 01:26:19 -07002550
2551 /*
2552 * After retaking hugetlb_lock, we need to recalculate 'needed'
2553 * because either resv_huge_pages or free_huge_pages may have changed.
2554 */
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002555 spin_lock_irq(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002556 needed = (h->resv_huge_pages + delta) -
2557 (h->free_huge_pages + allocated);
Hillf Danton28073b02012-03-21 16:34:00 -07002558 if (needed > 0) {
2559 if (alloc_ok)
2560 goto retry;
2561 /*
2562 * We were not able to allocate enough pages to
2563 * satisfy the entire reservation so we free what
2564 * we've allocated so far.
2565 */
2566 goto free;
2567 }
Adam Litkee4e574b2007-10-16 01:26:19 -07002568 /*
2569 * The surplus_list now contains _at_least_ the number of extra pages
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002570 * needed to accommodate the reservation. Add the appropriate number
Adam Litkee4e574b2007-10-16 01:26:19 -07002571 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -08002572 * allocator. Commit the entire reservation here to prevent another
2573 * process from stealing the pages as they are added to the pool but
2574 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -07002575 */
2576 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -07002577 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07002578 ret = 0;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09002579
Adam Litke19fc3f02008-04-28 02:12:20 -07002580 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -07002581 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -07002582 if ((--needed) < 0)
2583 break;
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002584 /* Add the page to the hugetlb allocator */
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08002585 enqueue_hugetlb_folio(h, page_folio(page));
Adam Litke19fc3f02008-04-28 02:12:20 -07002586 }
Hillf Danton28073b02012-03-21 16:34:00 -07002587free:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002588 spin_unlock_irq(&hugetlb_lock);
Adam Litke19fc3f02008-04-28 02:12:20 -07002589
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002590 /*
2591 * Free unnecessary surplus pages to the buddy allocator.
2592 * Pages have no ref count, call free_huge_page directly.
2593 */
Joonsoo Kimc0d934b2013-09-11 14:21:02 -07002594 list_for_each_entry_safe(page, tmp, &surplus_list, lru)
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002595 free_huge_page(page);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002596 spin_lock_irq(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07002597
2598 return ret;
2599}
2600
2601/*
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002602 * This routine has two main purposes:
2603 * 1) Decrement the reservation count (resv_huge_pages) by the value passed
2604 * in unused_resv_pages. This corresponds to the prior adjustments made
2605 * to the associated reservation map.
2606 * 2) Free any unused surplus pages that may have been allocated to satisfy
2607 * the reservation. As many as unused_resv_pages may be freed.
Adam Litkee4e574b2007-10-16 01:26:19 -07002608 */
Andi Kleena5516432008-07-23 21:27:41 -07002609static void return_unused_surplus_pages(struct hstate *h,
2610 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -07002611{
Adam Litkee4e574b2007-10-16 01:26:19 -07002612 unsigned long nr_pages;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002613 struct page *page;
2614 LIST_HEAD(page_list);
2615
Mike Kravetz9487ca62021-05-04 18:35:10 -07002616 lockdep_assert_held(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002617 /* Uncommit the reservation */
2618 h->resv_huge_pages -= unused_resv_pages;
Adam Litkee4e574b2007-10-16 01:26:19 -07002619
Naoya Horiguchic0531712022-07-14 13:24:13 +09002620 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002621 goto out;
Andi Kleenaa888a72008-07-23 21:27:47 -07002622
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002623 /*
2624 * Part (or even all) of the reservation could have been backed
2625 * by pre-allocated pages. Only free surplus pages.
2626 */
Andi Kleena5516432008-07-23 21:27:41 -07002627 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -07002628
Lee Schermerhorn685f3452009-09-21 17:01:23 -07002629 /*
2630 * We want to release as many surplus pages as possible, spread
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002631 * evenly across all nodes with memory. Iterate across these nodes
2632 * until we can no longer free unreserved surplus pages. This occurs
2633 * when the nodes with surplus pages have no free pages.
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002634 * remove_pool_huge_page() will balance the freed pages across the
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002635 * on-line nodes with memory and will handle the hstate accounting.
Lee Schermerhorn685f3452009-09-21 17:01:23 -07002636 */
2637 while (nr_pages--) {
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002638 page = remove_pool_huge_page(h, &node_states[N_MEMORY], 1);
2639 if (!page)
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002640 goto out;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002641
2642 list_add(&page->lru, &page_list);
Adam Litkee4e574b2007-10-16 01:26:19 -07002643 }
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002644
2645out:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002646 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002647 update_and_free_pages_bulk(h, &page_list);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002648 spin_lock_irq(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07002649}
2650
Mike Kravetz5e911372015-09-08 15:01:28 -07002651
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002652/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002653 * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
Mike Kravetz5e911372015-09-08 15:01:28 -07002654 * are used by the huge page allocation routines to manage reservations.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002655 *
2656 * vma_needs_reservation is called to determine if the huge page at addr
2657 * within the vma has an associated reservation. If a reservation is
2658 * needed, the value 1 is returned. The caller is then responsible for
2659 * managing the global reservation and subpool usage counts. After
2660 * the huge page has been allocated, vma_commit_reservation is called
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002661 * to add the page to the reservation map. If the page allocation fails,
2662 * the reservation must be ended instead of committed. vma_end_reservation
2663 * is called in such cases.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002664 *
2665 * In the normal case, vma_commit_reservation returns the same value
2666 * as the preceding vma_needs_reservation call. The only time this
2667 * is not the case is if a reserve map was changed between calls. It
2668 * is the responsibility of the caller to notice the difference and
2669 * take appropriate action.
Mike Kravetz96b96a92016-11-10 10:46:32 -08002670 *
2671 * vma_add_reservation is used in error paths where a reservation must
2672 * be restored when a newly allocated huge page must be freed. It is
2673 * to be called after calling vma_needs_reservation to determine if a
2674 * reservation exists.
Mike Kravetz846be082021-06-15 18:23:29 -07002675 *
2676 * vma_del_reservation is used in error paths where an entry in the reserve
2677 * map was created during huge page allocation and must be removed. It is to
2678 * be called after calling vma_needs_reservation to determine if a reservation
2679 * exists.
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002680 */
Mike Kravetz5e911372015-09-08 15:01:28 -07002681enum vma_resv_mode {
2682 VMA_NEEDS_RESV,
2683 VMA_COMMIT_RESV,
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002684 VMA_END_RESV,
Mike Kravetz96b96a92016-11-10 10:46:32 -08002685 VMA_ADD_RESV,
Mike Kravetz846be082021-06-15 18:23:29 -07002686 VMA_DEL_RESV,
Mike Kravetz5e911372015-09-08 15:01:28 -07002687};
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002688static long __vma_reservation_common(struct hstate *h,
2689 struct vm_area_struct *vma, unsigned long addr,
Mike Kravetz5e911372015-09-08 15:01:28 -07002690 enum vma_resv_mode mode)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002691{
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002692 struct resv_map *resv;
2693 pgoff_t idx;
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002694 long ret;
Mina Almasry0db9d742020-04-01 21:11:25 -07002695 long dummy_out_regions_needed;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002696
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002697 resv = vma_resv_map(vma);
2698 if (!resv)
Andy Whitcroft84afd992008-07-23 21:27:32 -07002699 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002700
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002701 idx = vma_hugecache_offset(h, vma, addr);
Mike Kravetz5e911372015-09-08 15:01:28 -07002702 switch (mode) {
2703 case VMA_NEEDS_RESV:
Mina Almasry0db9d742020-04-01 21:11:25 -07002704 ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed);
2705 /* We assume that vma_reservation_* routines always operate on
2706 * 1 page, and that adding to resv map a 1 page entry can only
2707 * ever require 1 region.
2708 */
2709 VM_BUG_ON(dummy_out_regions_needed != 1);
Mike Kravetz5e911372015-09-08 15:01:28 -07002710 break;
2711 case VMA_COMMIT_RESV:
Mina Almasry075a61d2020-04-01 21:11:28 -07002712 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
Mina Almasry0db9d742020-04-01 21:11:25 -07002713 /* region_add calls of range 1 should never fail. */
2714 VM_BUG_ON(ret < 0);
Mike Kravetz5e911372015-09-08 15:01:28 -07002715 break;
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002716 case VMA_END_RESV:
Mina Almasry0db9d742020-04-01 21:11:25 -07002717 region_abort(resv, idx, idx + 1, 1);
Mike Kravetz5e911372015-09-08 15:01:28 -07002718 ret = 0;
2719 break;
Mike Kravetz96b96a92016-11-10 10:46:32 -08002720 case VMA_ADD_RESV:
Mina Almasry0db9d742020-04-01 21:11:25 -07002721 if (vma->vm_flags & VM_MAYSHARE) {
Mina Almasry075a61d2020-04-01 21:11:28 -07002722 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
Mina Almasry0db9d742020-04-01 21:11:25 -07002723 /* region_add calls of range 1 should never fail. */
2724 VM_BUG_ON(ret < 0);
2725 } else {
2726 region_abort(resv, idx, idx + 1, 1);
Mike Kravetz96b96a92016-11-10 10:46:32 -08002727 ret = region_del(resv, idx, idx + 1);
2728 }
2729 break;
Mike Kravetz846be082021-06-15 18:23:29 -07002730 case VMA_DEL_RESV:
2731 if (vma->vm_flags & VM_MAYSHARE) {
2732 region_abort(resv, idx, idx + 1, 1);
2733 ret = region_del(resv, idx, idx + 1);
2734 } else {
2735 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2736 /* region_add calls of range 1 should never fail. */
2737 VM_BUG_ON(ret < 0);
2738 }
2739 break;
Mike Kravetz5e911372015-09-08 15:01:28 -07002740 default:
2741 BUG();
2742 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07002743
Mike Kravetz846be082021-06-15 18:23:29 -07002744 if (vma->vm_flags & VM_MAYSHARE || mode == VMA_DEL_RESV)
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002745 return ret;
Miaohe Linbf3d12b2021-05-04 18:34:32 -07002746 /*
2747 * We know private mapping must have HPAGE_RESV_OWNER set.
2748 *
2749 * In most cases, reserves always exist for private mappings.
2750 * However, a file associated with mapping could have been
2751 * hole punched or truncated after reserves were consumed.
2752 * As subsequent fault on such a range will not use reserves.
2753 * Subtle - The reserve map for private mappings has the
2754 * opposite meaning than that of shared mappings. If NO
2755 * entry is in the reserve map, it means a reservation exists.
2756 * If an entry exists in the reserve map, it means the
2757 * reservation has already been consumed. As a result, the
2758 * return value of this routine is the opposite of the
2759 * value returned from reserve map manipulation routines above.
2760 */
2761 if (ret > 0)
2762 return 0;
2763 if (ret == 0)
2764 return 1;
2765 return ret;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002766}
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002767
2768static long vma_needs_reservation(struct hstate *h,
Andi Kleena5516432008-07-23 21:27:41 -07002769 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002770{
Mike Kravetz5e911372015-09-08 15:01:28 -07002771 return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002772}
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002773
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002774static long vma_commit_reservation(struct hstate *h,
2775 struct vm_area_struct *vma, unsigned long addr)
2776{
Mike Kravetz5e911372015-09-08 15:01:28 -07002777 return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
2778}
2779
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002780static void vma_end_reservation(struct hstate *h,
Mike Kravetz5e911372015-09-08 15:01:28 -07002781 struct vm_area_struct *vma, unsigned long addr)
2782{
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002783 (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002784}
2785
Mike Kravetz96b96a92016-11-10 10:46:32 -08002786static long vma_add_reservation(struct hstate *h,
2787 struct vm_area_struct *vma, unsigned long addr)
2788{
2789 return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
2790}
2791
Mike Kravetz846be082021-06-15 18:23:29 -07002792static long vma_del_reservation(struct hstate *h,
2793 struct vm_area_struct *vma, unsigned long addr)
Mike Kravetz96b96a92016-11-10 10:46:32 -08002794{
Mike Kravetz846be082021-06-15 18:23:29 -07002795 return __vma_reservation_common(h, vma, addr, VMA_DEL_RESV);
2796}
Mike Kravetz96b96a92016-11-10 10:46:32 -08002797
Mike Kravetz846be082021-06-15 18:23:29 -07002798/*
2799 * This routine is called to restore reservation information on error paths.
2800 * It should ONLY be called for pages allocated via alloc_huge_page(), and
2801 * the hugetlb mutex should remain held when calling this routine.
2802 *
2803 * It handles two specific cases:
2804 * 1) A reservation was in place and the page consumed the reservation.
2805 * HPageRestoreReserve is set in the page.
2806 * 2) No reservation was in place for the page, so HPageRestoreReserve is
2807 * not set. However, alloc_huge_page always updates the reserve map.
2808 *
2809 * In case 1, free_huge_page later in the error path will increment the
2810 * global reserve count. But, free_huge_page does not have enough context
2811 * to adjust the reservation map. This case deals primarily with private
2812 * mappings. Adjust the reserve map here to be consistent with global
2813 * reserve count adjustments to be made by free_huge_page. Make sure the
2814 * reserve map indicates there is a reservation present.
2815 *
2816 * In case 2, simply undo reserve map modifications done by alloc_huge_page.
2817 */
2818void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
2819 unsigned long address, struct page *page)
2820{
2821 long rc = vma_needs_reservation(h, vma, address);
2822
2823 if (HPageRestoreReserve(page)) {
2824 if (unlikely(rc < 0))
Mike Kravetz96b96a92016-11-10 10:46:32 -08002825 /*
2826 * Rare out of memory condition in reserve map
Mike Kravetzd6995da2021-02-24 12:08:51 -08002827 * manipulation. Clear HPageRestoreReserve so that
Mike Kravetz96b96a92016-11-10 10:46:32 -08002828 * global reserve count will not be incremented
2829 * by free_huge_page. This will make it appear
2830 * as though the reservation for this page was
2831 * consumed. This may prevent the task from
2832 * faulting in the page at a later time. This
2833 * is better than inconsistent global huge page
2834 * accounting of reserve counts.
2835 */
Mike Kravetzd6995da2021-02-24 12:08:51 -08002836 ClearHPageRestoreReserve(page);
Mike Kravetz846be082021-06-15 18:23:29 -07002837 else if (rc)
2838 (void)vma_add_reservation(h, vma, address);
2839 else
Mike Kravetz96b96a92016-11-10 10:46:32 -08002840 vma_end_reservation(h, vma, address);
Mike Kravetz846be082021-06-15 18:23:29 -07002841 } else {
2842 if (!rc) {
2843 /*
2844 * This indicates there is an entry in the reserve map
Mike Kravetzc7b18502021-08-19 19:04:33 -07002845 * not added by alloc_huge_page. We know it was added
Mike Kravetz846be082021-06-15 18:23:29 -07002846 * before the alloc_huge_page call, otherwise
2847 * HPageRestoreReserve would be set on the page.
2848 * Remove the entry so that a subsequent allocation
2849 * does not consume a reservation.
2850 */
2851 rc = vma_del_reservation(h, vma, address);
2852 if (rc < 0)
2853 /*
2854 * VERY rare out of memory condition. Since
2855 * we can not delete the entry, set
2856 * HPageRestoreReserve so that the reserve
2857 * count will be incremented when the page
2858 * is freed. This reserve will be consumed
2859 * on a subsequent allocation.
2860 */
2861 SetHPageRestoreReserve(page);
2862 } else if (rc < 0) {
2863 /*
2864 * Rare out of memory condition from
2865 * vma_needs_reservation call. Memory allocation is
2866 * only attempted if a new entry is needed. Therefore,
2867 * this implies there is not an entry in the
2868 * reserve map.
2869 *
2870 * For shared mappings, no entry in the map indicates
2871 * no reservation. We are done.
2872 */
2873 if (!(vma->vm_flags & VM_MAYSHARE))
2874 /*
2875 * For private mappings, no entry indicates
2876 * a reservation is present. Since we can
2877 * not add an entry, set SetHPageRestoreReserve
2878 * on the page so reserve count will be
2879 * incremented when freed. This reserve will
2880 * be consumed on a subsequent allocation.
2881 */
2882 SetHPageRestoreReserve(page);
2883 } else
2884 /*
2885 * No reservation present, do nothing
2886 */
2887 vma_end_reservation(h, vma, address);
Mike Kravetz96b96a92016-11-10 10:46:32 -08002888 }
2889}
2890
Oscar Salvador369fa222021-05-04 18:35:26 -07002891/*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002892 * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve
2893 * the old one
Oscar Salvador369fa222021-05-04 18:35:26 -07002894 * @h: struct hstate old page belongs to
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002895 * @old_folio: Old folio to dissolve
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002896 * @list: List to isolate the page in case we need to
Oscar Salvador369fa222021-05-04 18:35:26 -07002897 * Returns 0 on success, otherwise negated error.
2898 */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002899static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
2900 struct folio *old_folio, struct list_head *list)
Oscar Salvador369fa222021-05-04 18:35:26 -07002901{
2902 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002903 int nid = folio_nid(old_folio);
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002904 struct folio *new_folio;
Oscar Salvador369fa222021-05-04 18:35:26 -07002905 int ret = 0;
2906
2907 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002908 * Before dissolving the folio, we need to allocate a new one for the
2909 * pool to remain stable. Here, we allocate the folio and 'prep' it
Muchun Songf41f2ed2021-06-30 18:47:13 -07002910 * by doing everything but actually updating counters and adding to
2911 * the pool. This simplifies and let us do most of the processing
2912 * under the lock.
Oscar Salvador369fa222021-05-04 18:35:26 -07002913 */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002914 new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL);
2915 if (!new_folio)
Oscar Salvador369fa222021-05-04 18:35:26 -07002916 return -ENOMEM;
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002917 __prep_new_hugetlb_folio(h, new_folio);
Oscar Salvador369fa222021-05-04 18:35:26 -07002918
2919retry:
2920 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002921 if (!folio_test_hugetlb(old_folio)) {
Oscar Salvador369fa222021-05-04 18:35:26 -07002922 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002923 * Freed from under us. Drop new_folio too.
Oscar Salvador369fa222021-05-04 18:35:26 -07002924 */
2925 goto free_new;
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002926 } else if (folio_ref_count(old_folio)) {
Oscar Salvador369fa222021-05-04 18:35:26 -07002927 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002928 * Someone has grabbed the folio, try to isolate it here.
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002929 * Fail with -EBUSY if not possible.
Oscar Salvador369fa222021-05-04 18:35:26 -07002930 */
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002931 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002932 ret = isolate_hugetlb(&old_folio->page, list);
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002933 spin_lock_irq(&hugetlb_lock);
Oscar Salvador369fa222021-05-04 18:35:26 -07002934 goto free_new;
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002935 } else if (!folio_test_hugetlb_freed(old_folio)) {
Oscar Salvador369fa222021-05-04 18:35:26 -07002936 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002937 * Folio's refcount is 0 but it has not been enqueued in the
Oscar Salvador369fa222021-05-04 18:35:26 -07002938 * freelist yet. Race window is small, so we can succeed here if
2939 * we retry.
2940 */
2941 spin_unlock_irq(&hugetlb_lock);
2942 cond_resched();
2943 goto retry;
2944 } else {
2945 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002946 * Ok, old_folio is still a genuine free hugepage. Remove it from
Oscar Salvador369fa222021-05-04 18:35:26 -07002947 * the freelist and decrease the counters. These will be
2948 * incremented again when calling __prep_account_new_huge_page()
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08002949 * and enqueue_hugetlb_folio() for new_folio. The counters will
2950 * remain stable since this happens under the lock.
Oscar Salvador369fa222021-05-04 18:35:26 -07002951 */
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08002952 remove_hugetlb_folio(h, old_folio, false);
Oscar Salvador369fa222021-05-04 18:35:26 -07002953
2954 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002955 * Ref count on new_folio is already zero as it was dropped
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002956 * earlier. It can be directly added to the pool free list.
Oscar Salvador369fa222021-05-04 18:35:26 -07002957 */
Oscar Salvador369fa222021-05-04 18:35:26 -07002958 __prep_account_new_huge_page(h, nid);
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08002959 enqueue_hugetlb_folio(h, new_folio);
Oscar Salvador369fa222021-05-04 18:35:26 -07002960
2961 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002962 * Folio has been replaced, we can safely free the old one.
Oscar Salvador369fa222021-05-04 18:35:26 -07002963 */
2964 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08002965 update_and_free_hugetlb_folio(h, old_folio, false);
Oscar Salvador369fa222021-05-04 18:35:26 -07002966 }
2967
2968 return ret;
2969
2970free_new:
2971 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002972 /* Folio has a zero ref count, but needs a ref to be freed */
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002973 folio_ref_unfreeze(new_folio, 1);
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08002974 update_and_free_hugetlb_folio(h, new_folio, false);
Oscar Salvador369fa222021-05-04 18:35:26 -07002975
2976 return ret;
2977}
2978
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002979int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list)
Oscar Salvador369fa222021-05-04 18:35:26 -07002980{
2981 struct hstate *h;
Sidhartha Kumard5e33bd2022-11-01 15:30:55 -07002982 struct folio *folio = page_folio(page);
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002983 int ret = -EBUSY;
Oscar Salvador369fa222021-05-04 18:35:26 -07002984
2985 /*
2986 * The page might have been dissolved from under our feet, so make sure
2987 * to carefully check the state under the lock.
2988 * Return success when racing as if we dissolved the page ourselves.
2989 */
2990 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumard5e33bd2022-11-01 15:30:55 -07002991 if (folio_test_hugetlb(folio)) {
2992 h = folio_hstate(folio);
Oscar Salvador369fa222021-05-04 18:35:26 -07002993 } else {
2994 spin_unlock_irq(&hugetlb_lock);
2995 return 0;
2996 }
2997 spin_unlock_irq(&hugetlb_lock);
2998
2999 /*
3000 * Fence off gigantic pages as there is a cyclic dependency between
3001 * alloc_contig_range and them. Return -ENOMEM as this has the effect
3002 * of bailing out right away without further retrying.
3003 */
3004 if (hstate_is_gigantic(h))
3005 return -ENOMEM;
3006
Sidhartha Kumard5e33bd2022-11-01 15:30:55 -07003007 if (folio_ref_count(folio) && !isolate_hugetlb(&folio->page, list))
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07003008 ret = 0;
Sidhartha Kumard5e33bd2022-11-01 15:30:55 -07003009 else if (!folio_ref_count(folio))
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003010 ret = alloc_and_dissolve_hugetlb_folio(h, folio, list);
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07003011
3012 return ret;
Oscar Salvador369fa222021-05-04 18:35:26 -07003013}
3014
Mike Kravetz70c35472015-09-08 15:01:54 -07003015struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003016 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
David Gibson90481622012-03-21 16:34:12 -07003018 struct hugepage_subpool *spool = subpool_vma(vma);
Andi Kleena5516432008-07-23 21:27:41 -07003019 struct hstate *h = hstate_vma(vma);
Adam Litke348ea202007-11-14 16:59:37 -08003020 struct page *page;
Sidhartha Kumard4ab0312022-11-01 15:30:57 -07003021 struct folio *folio;
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003022 long map_chg, map_commit;
3023 long gbl_chg;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07003024 int ret, idx;
3025 struct hugetlb_cgroup *h_cg;
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003026 bool deferred_reserve;
Adam Litke2fc39ce2007-11-14 16:59:39 -08003027
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07003028 idx = hstate_index(h);
Mel Gormana1e78772008-07-23 21:27:23 -07003029 /*
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003030 * Examine the region/reserve map to determine if the process
3031 * has a reservation for the page to be allocated. A return
3032 * code of zero indicates a reservation exists (no change).
Mel Gormana1e78772008-07-23 21:27:23 -07003033 */
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003034 map_chg = gbl_chg = vma_needs_reservation(h, vma, addr);
3035 if (map_chg < 0)
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07003036 return ERR_PTR(-ENOMEM);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003037
3038 /*
3039 * Processes that did not create the mapping will have no
3040 * reserves as indicated by the region/reserve map. Check
3041 * that the allocation will not exceed the subpool limit.
3042 * Allocations for MAP_NORESERVE mappings also need to be
3043 * checked against any subpool limit.
3044 */
3045 if (map_chg || avoid_reserve) {
3046 gbl_chg = hugepage_subpool_get_pages(spool, 1);
3047 if (gbl_chg < 0) {
Mike Kravetzfeba16e2015-09-08 15:01:31 -07003048 vma_end_reservation(h, vma, addr);
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07003049 return ERR_PTR(-ENOSPC);
Mike Kravetz5e911372015-09-08 15:01:28 -07003050 }
Mel Gormana1e78772008-07-23 21:27:23 -07003051
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003052 /*
3053 * Even though there was no reservation in the region/reserve
3054 * map, there could be reservations associated with the
3055 * subpool that can be used. This would be indicated if the
3056 * return value of hugepage_subpool_get_pages() is zero.
3057 * However, if avoid_reserve is specified we still avoid even
3058 * the subpool reservations.
3059 */
3060 if (avoid_reserve)
3061 gbl_chg = 1;
3062 }
3063
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003064 /* If this allocation is not consuming a reservation, charge it now.
3065 */
Miaohe Lin6501fe52021-05-04 18:33:16 -07003066 deferred_reserve = map_chg || avoid_reserve;
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003067 if (deferred_reserve) {
3068 ret = hugetlb_cgroup_charge_cgroup_rsvd(
3069 idx, pages_per_huge_page(h), &h_cg);
3070 if (ret)
3071 goto out_subpool_put;
3072 }
3073
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07003074 ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003075 if (ret)
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003076 goto out_uncharge_cgroup_reservation;
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003077
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003078 spin_lock_irq(&hugetlb_lock);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003079 /*
3080 * glb_chg is passed to indicate whether or not a page must be taken
3081 * from the global free pool (global change). gbl_chg == 0 indicates
3082 * a reservation exists for the allocation.
3083 */
3084 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07003085 if (!page) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003086 spin_unlock_irq(&hugetlb_lock);
Michal Hocko0c397da2018-01-31 16:20:56 -08003087 page = alloc_buddy_huge_page_with_mpol(h, vma, addr);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003088 if (!page)
3089 goto out_uncharge_cgroup;
Rik van Riel12df1402022-10-17 20:25:05 -04003090 spin_lock_irq(&hugetlb_lock);
Naoya Horiguchia88c7692015-12-11 13:40:24 -08003091 if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
Mike Kravetzd6995da2021-02-24 12:08:51 -08003092 SetHPageRestoreReserve(page);
Naoya Horiguchia88c7692015-12-11 13:40:24 -08003093 h->resv_huge_pages--;
3094 }
Wei Yang15a8d682020-10-13 16:56:33 -07003095 list_add(&page->lru, &h->hugepage_activelist);
Mike Kravetz2b216242022-09-16 14:46:38 -07003096 set_page_refcounted(page);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07003097 /* Fall through */
Mel Gormana1e78772008-07-23 21:27:23 -07003098 }
Sidhartha Kumard4ab0312022-11-01 15:30:57 -07003099 folio = page_folio(page);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07003100 hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003101 /* If allocation is not consuming a reservation, also store the
3102 * hugetlb_cgroup pointer on the page.
3103 */
3104 if (deferred_reserve) {
3105 hugetlb_cgroup_commit_charge_rsvd(idx, pages_per_huge_page(h),
3106 h_cg, page);
3107 }
3108
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003109 spin_unlock_irq(&hugetlb_lock);
Mel Gormana1e78772008-07-23 21:27:23 -07003110
Mike Kravetzd6995da2021-02-24 12:08:51 -08003111 hugetlb_set_page_subpool(page, spool);
Mel Gormana1e78772008-07-23 21:27:23 -07003112
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003113 map_commit = vma_commit_reservation(h, vma, addr);
3114 if (unlikely(map_chg > map_commit)) {
Mike Kravetz33039672015-06-24 16:57:58 -07003115 /*
3116 * The page was added to the reservation map between
3117 * vma_needs_reservation and vma_commit_reservation.
3118 * This indicates a race with hugetlb_reserve_pages.
3119 * Adjust for the subpool count incremented above AND
3120 * in hugetlb_reserve_pages for the same page. Also,
3121 * the reservation count added in hugetlb_reserve_pages
3122 * no longer applies.
3123 */
3124 long rsv_adjust;
3125
3126 rsv_adjust = hugepage_subpool_put_pages(spool, 1);
3127 hugetlb_acct_memory(h, -rsv_adjust);
Mike Kravetz79aa9252020-11-01 17:07:27 -08003128 if (deferred_reserve)
Sidhartha Kumard4ab0312022-11-01 15:30:57 -07003129 hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h),
3130 pages_per_huge_page(h), folio);
Mike Kravetz33039672015-06-24 16:57:58 -07003131 }
Adam Litke90d8b7e2007-11-14 16:59:42 -08003132 return page;
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003133
3134out_uncharge_cgroup:
3135 hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003136out_uncharge_cgroup_reservation:
3137 if (deferred_reserve)
3138 hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h),
3139 h_cg);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003140out_subpool_put:
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003141 if (map_chg || avoid_reserve)
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003142 hugepage_subpool_put_pages(spool, 1);
Mike Kravetzfeba16e2015-09-08 15:01:31 -07003143 vma_end_reservation(h, vma, addr);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003144 return ERR_PTR(-ENOSPC);
David Gibsonb45b5bd2006-03-22 00:08:55 -08003145}
3146
Zhenguo Yaob5389082021-11-05 13:43:28 -07003147int alloc_bootmem_huge_page(struct hstate *h, int nid)
Aneesh Kumar K.Ve24a1302017-07-28 10:31:25 +05303148 __attribute__ ((weak, alias("__alloc_bootmem_huge_page")));
Zhenguo Yaob5389082021-11-05 13:43:28 -07003149int __alloc_bootmem_huge_page(struct hstate *h, int nid)
Andi Kleenaa888a72008-07-23 21:27:47 -07003150{
Zhenguo Yaob5389082021-11-05 13:43:28 -07003151 struct huge_bootmem_page *m = NULL; /* initialize for clang */
Joonsoo Kimb2261022013-09-11 14:21:00 -07003152 int nr_nodes, node;
Andi Kleenaa888a72008-07-23 21:27:47 -07003153
Zhenguo Yaob5389082021-11-05 13:43:28 -07003154 /* do node specific alloc */
3155 if (nid != NUMA_NO_NODE) {
3156 m = memblock_alloc_try_nid_raw(huge_page_size(h), huge_page_size(h),
3157 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
3158 if (!m)
3159 return 0;
3160 goto found;
3161 }
3162 /* allocate from next node when distributing huge pages */
Joonsoo Kimb2261022013-09-11 14:21:00 -07003163 for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
Zhenguo Yaob5389082021-11-05 13:43:28 -07003164 m = memblock_alloc_try_nid_raw(
Grygorii Strashko8b89a112014-01-21 15:50:36 -08003165 huge_page_size(h), huge_page_size(h),
Mike Rapoport97ad1082018-10-30 15:09:44 -07003166 0, MEMBLOCK_ALLOC_ACCESSIBLE, node);
Zhenguo Yaob5389082021-11-05 13:43:28 -07003167 /*
3168 * Use the beginning of the huge page to store the
3169 * huge_bootmem_page struct (until gather_bootmem
3170 * puts them into the mem_map).
3171 */
3172 if (!m)
3173 return 0;
3174 goto found;
Andi Kleenaa888a72008-07-23 21:27:47 -07003175 }
Andi Kleenaa888a72008-07-23 21:27:47 -07003176
3177found:
Andi Kleenaa888a72008-07-23 21:27:47 -07003178 /* Put them into a private list first because mem_map is not up yet */
Cannon Matthews330d6e42018-08-17 15:49:17 -07003179 INIT_LIST_HEAD(&m->list);
Andi Kleenaa888a72008-07-23 21:27:47 -07003180 list_add(&m->list, &huge_boot_pages);
3181 m->hstate = h;
3182 return 1;
3183}
3184
Mike Kravetz48b8d742021-06-30 18:48:31 -07003185/*
3186 * Put bootmem huge pages into the standard lists after mem_map is up.
3187 * Note: This only applies to gigantic (order > MAX_ORDER) pages.
3188 */
Andi Kleenaa888a72008-07-23 21:27:47 -07003189static void __init gather_bootmem_prealloc(void)
3190{
3191 struct huge_bootmem_page *m;
3192
3193 list_for_each_entry(m, &huge_boot_pages, list) {
Mike Kravetz40d18eb2018-08-17 15:49:07 -07003194 struct page *page = virt_to_page(m);
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08003195 struct folio *folio = page_folio(page);
Andi Kleenaa888a72008-07-23 21:27:47 -07003196 struct hstate *h = m->hstate;
Becky Bruceee8f2482011-07-25 17:11:50 -07003197
Mike Kravetz48b8d742021-06-30 18:48:31 -07003198 VM_BUG_ON(!hstate_is_gigantic(h));
Sidhartha Kumard1c60952022-11-29 14:50:38 -08003199 WARN_ON(folio_ref_count(folio) != 1);
3200 if (prep_compound_gigantic_folio(folio, huge_page_order(h))) {
3201 WARN_ON(folio_test_reserved(folio));
3202 prep_new_hugetlb_folio(h, folio, folio_nid(folio));
Mike Kravetz2b216242022-09-16 14:46:38 -07003203 free_huge_page(page); /* add to the hugepage allocator */
Mike Kravetz7118fc22021-06-30 18:48:34 -07003204 } else {
Mike Kravetz416d85e2021-09-02 14:58:43 -07003205 /* VERY unlikely inflated ref count on a tail page */
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08003206 free_gigantic_folio(folio, huge_page_order(h));
Mike Kravetz7118fc22021-06-30 18:48:34 -07003207 }
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08003208
Rafael Aquinib0320c72011-06-15 15:08:39 -07003209 /*
Mike Kravetz48b8d742021-06-30 18:48:31 -07003210 * We need to restore the 'stolen' pages to totalram_pages
3211 * in order to fix confusing memory reports from free(1) and
3212 * other side-effects, like CommitLimit going negative.
Rafael Aquinib0320c72011-06-15 15:08:39 -07003213 */
Mike Kravetz48b8d742021-06-30 18:48:31 -07003214 adjust_managed_page_count(page, pages_per_huge_page(h));
Cannon Matthews520495f2018-07-03 17:02:43 -07003215 cond_resched();
Andi Kleenaa888a72008-07-23 21:27:47 -07003216 }
3217}
Zhenguo Yaob5389082021-11-05 13:43:28 -07003218static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)
3219{
3220 unsigned long i;
3221 char buf[32];
3222
3223 for (i = 0; i < h->max_huge_pages_node[nid]; ++i) {
3224 if (hstate_is_gigantic(h)) {
3225 if (!alloc_bootmem_huge_page(h, nid))
3226 break;
3227 } else {
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003228 struct folio *folio;
Zhenguo Yaob5389082021-11-05 13:43:28 -07003229 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
3230
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003231 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid,
Zhenguo Yaob5389082021-11-05 13:43:28 -07003232 &node_states[N_MEMORY], NULL);
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003233 if (!folio)
Zhenguo Yaob5389082021-11-05 13:43:28 -07003234 break;
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003235 free_huge_page(&folio->page); /* free it into the hugepage allocator */
Zhenguo Yaob5389082021-11-05 13:43:28 -07003236 }
3237 cond_resched();
3238 }
3239 if (i == h->max_huge_pages_node[nid])
3240 return;
3241
3242 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3243 pr_warn("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n",
3244 h->max_huge_pages_node[nid], buf, nid, i);
3245 h->max_huge_pages -= (h->max_huge_pages_node[nid] - i);
3246 h->max_huge_pages_node[nid] = i;
3247}
Andi Kleenaa888a72008-07-23 21:27:47 -07003248
Andi Kleen8faa8b02008-07-23 21:27:48 -07003249static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 unsigned long i;
Mike Kravetzf60858f2019-09-23 15:37:35 -07003252 nodemask_t *node_alloc_noretry;
Zhenguo Yaob5389082021-11-05 13:43:28 -07003253 bool node_specific_alloc = false;
Mike Kravetzf60858f2019-09-23 15:37:35 -07003254
Zhenguo Yaob5389082021-11-05 13:43:28 -07003255 /* skip gigantic hugepages allocation if hugetlb_cma enabled */
3256 if (hstate_is_gigantic(h) && hugetlb_cma_size) {
3257 pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n");
3258 return;
3259 }
3260
3261 /* do node specific alloc */
Peng Liu0a7a0f62022-04-29 14:36:57 -07003262 for_each_online_node(i) {
Zhenguo Yaob5389082021-11-05 13:43:28 -07003263 if (h->max_huge_pages_node[i] > 0) {
3264 hugetlb_hstate_alloc_pages_onenode(h, i);
3265 node_specific_alloc = true;
3266 }
3267 }
3268
3269 if (node_specific_alloc)
3270 return;
3271
3272 /* below will do all node balanced alloc */
Mike Kravetzf60858f2019-09-23 15:37:35 -07003273 if (!hstate_is_gigantic(h)) {
3274 /*
3275 * Bit mask controlling how hard we retry per-node allocations.
3276 * Ignore errors as lower level routines can deal with
3277 * node_alloc_noretry == NULL. If this kmalloc fails at boot
3278 * time, we are likely in bigger trouble.
3279 */
3280 node_alloc_noretry = kmalloc(sizeof(*node_alloc_noretry),
3281 GFP_KERNEL);
3282 } else {
3283 /* allocations done at boot time */
3284 node_alloc_noretry = NULL;
3285 }
3286
3287 /* bit mask controlling how hard we retry per-node allocations */
3288 if (node_alloc_noretry)
3289 nodes_clear(*node_alloc_noretry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Andi Kleene5ff2152008-07-23 21:27:42 -07003291 for (i = 0; i < h->max_huge_pages; ++i) {
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003292 if (hstate_is_gigantic(h)) {
Zhenguo Yaob5389082021-11-05 13:43:28 -07003293 if (!alloc_bootmem_huge_page(h, NUMA_NO_NODE))
Andi Kleenaa888a72008-07-23 21:27:47 -07003294 break;
Michal Hocko0c397da2018-01-31 16:20:56 -08003295 } else if (!alloc_pool_huge_page(h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07003296 &node_states[N_MEMORY],
3297 node_alloc_noretry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 break;
David Rientjes69ed7792017-07-10 15:48:50 -07003299 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 }
Liam R. Howlettd715cf82017-07-10 15:48:15 -07003301 if (i < h->max_huge_pages) {
3302 char buf[32];
3303
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07003304 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Liam R. Howlettd715cf82017-07-10 15:48:15 -07003305 pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n",
3306 h->max_huge_pages, buf, i);
3307 h->max_huge_pages = i;
3308 }
Mike Kravetzf60858f2019-09-23 15:37:35 -07003309 kfree(node_alloc_noretry);
Andi Kleene5ff2152008-07-23 21:27:42 -07003310}
3311
3312static void __init hugetlb_init_hstates(void)
3313{
Mike Kravetz79dfc692021-11-05 13:41:20 -07003314 struct hstate *h, *h2;
Andi Kleene5ff2152008-07-23 21:27:42 -07003315
3316 for_each_hstate(h) {
Andi Kleen8faa8b02008-07-23 21:27:48 -07003317 /* oversize hugepages were init'ed in early boot */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003318 if (!hstate_is_gigantic(h))
Andi Kleen8faa8b02008-07-23 21:27:48 -07003319 hugetlb_hstate_alloc_pages(h);
Mike Kravetz79dfc692021-11-05 13:41:20 -07003320
3321 /*
3322 * Set demote order for each hstate. Note that
3323 * h->demote_order is initially 0.
3324 * - We can not demote gigantic pages if runtime freeing
3325 * is not supported, so skip this.
Mike Kravetza01f4392021-11-05 13:41:27 -07003326 * - If CMA allocation is possible, we can not demote
3327 * HUGETLB_PAGE_ORDER or smaller size pages.
Mike Kravetz79dfc692021-11-05 13:41:20 -07003328 */
3329 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
3330 continue;
Mike Kravetza01f4392021-11-05 13:41:27 -07003331 if (hugetlb_cma_size && h->order <= HUGETLB_PAGE_ORDER)
3332 continue;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003333 for_each_hstate(h2) {
3334 if (h2 == h)
3335 continue;
3336 if (h2->order < h->order &&
3337 h2->order > h->demote_order)
3338 h->demote_order = h2->order;
3339 }
Andi Kleene5ff2152008-07-23 21:27:42 -07003340 }
3341}
3342
3343static void __init report_hugepages(void)
3344{
3345 struct hstate *h;
3346
3347 for_each_hstate(h) {
Andi Kleen4abd32d2008-07-23 21:27:49 -07003348 char buf[32];
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07003349
3350 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Muchun Song62138342022-06-28 17:22:33 +08003351 pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n",
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07003352 buf, h->free_huge_pages);
Muchun Song62138342022-06-28 17:22:33 +08003353 pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n",
3354 hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf);
Andi Kleene5ff2152008-07-23 21:27:42 -07003355 }
3356}
3357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358#ifdef CONFIG_HIGHMEM
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003359static void try_to_free_low(struct hstate *h, unsigned long count,
3360 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
Christoph Lameter4415cc82006-09-25 23:31:55 -07003362 int i;
Mike Kravetz11218282021-05-04 18:34:59 -07003363 LIST_HEAD(page_list);
Christoph Lameter4415cc82006-09-25 23:31:55 -07003364
Mike Kravetz9487ca62021-05-04 18:35:10 -07003365 lockdep_assert_held(&hugetlb_lock);
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003366 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07003367 return;
3368
Mike Kravetz11218282021-05-04 18:34:59 -07003369 /*
3370 * Collect pages to be freed on a list, and free after dropping lock
3371 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003372 for_each_node_mask(i, *nodes_allowed) {
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003373 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -07003374 struct list_head *freel = &h->hugepage_freelists[i];
3375 list_for_each_entry_safe(page, next, freel, lru) {
3376 if (count >= h->nr_huge_pages)
Mike Kravetz11218282021-05-04 18:34:59 -07003377 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 if (PageHighMem(page))
3379 continue;
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08003380 remove_hugetlb_folio(h, page_folio(page), false);
Mike Kravetz11218282021-05-04 18:34:59 -07003381 list_add(&page->lru, &page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 }
3383 }
Mike Kravetz11218282021-05-04 18:34:59 -07003384
3385out:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003386 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003387 update_and_free_pages_bulk(h, &page_list);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003388 spin_lock_irq(&hugetlb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390#else
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003391static inline void try_to_free_low(struct hstate *h, unsigned long count,
3392 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393{
3394}
3395#endif
3396
Wu Fengguang20a03072009-06-16 15:32:22 -07003397/*
3398 * Increment or decrement surplus_huge_pages. Keep node-specific counters
3399 * balanced by operating on them in a round-robin fashion.
3400 * Returns 1 if an adjustment was made.
3401 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003402static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
3403 int delta)
Wu Fengguang20a03072009-06-16 15:32:22 -07003404{
Joonsoo Kimb2261022013-09-11 14:21:00 -07003405 int nr_nodes, node;
Wu Fengguang20a03072009-06-16 15:32:22 -07003406
Mike Kravetz9487ca62021-05-04 18:35:10 -07003407 lockdep_assert_held(&hugetlb_lock);
Wu Fengguang20a03072009-06-16 15:32:22 -07003408 VM_BUG_ON(delta != -1 && delta != 1);
Wu Fengguang20a03072009-06-16 15:32:22 -07003409
Joonsoo Kimb2261022013-09-11 14:21:00 -07003410 if (delta < 0) {
3411 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
3412 if (h->surplus_huge_pages_node[node])
3413 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07003414 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07003415 } else {
3416 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
3417 if (h->surplus_huge_pages_node[node] <
3418 h->nr_huge_pages_node[node])
3419 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07003420 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07003421 }
3422 return 0;
Wu Fengguang20a03072009-06-16 15:32:22 -07003423
Joonsoo Kimb2261022013-09-11 14:21:00 -07003424found:
3425 h->surplus_huge_pages += delta;
3426 h->surplus_huge_pages_node[node] += delta;
3427 return 1;
Wu Fengguang20a03072009-06-16 15:32:22 -07003428}
3429
Andi Kleena5516432008-07-23 21:27:41 -07003430#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Mike Kravetzfd875dc2019-05-13 17:19:20 -07003431static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003432 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Adam Litke7893d1d2007-10-16 01:26:18 -07003434 unsigned long min_count, ret;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003435 struct page *page;
3436 LIST_HEAD(page_list);
Mike Kravetzf60858f2019-09-23 15:37:35 -07003437 NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL);
3438
3439 /*
3440 * Bit mask controlling how hard we retry per-node allocations.
3441 * If we can not allocate the bit mask, do not attempt to allocate
3442 * the requested huge pages.
3443 */
3444 if (node_alloc_noretry)
3445 nodes_clear(*node_alloc_noretry);
3446 else
3447 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Mike Kravetz29383962021-05-04 18:34:52 -07003449 /*
3450 * resize_lock mutex prevents concurrent adjustments to number of
3451 * pages in hstate via the proc/sysfs interfaces.
3452 */
3453 mutex_lock(&h->resize_lock);
Muchun Songb65d4ad2021-06-30 18:47:17 -07003454 flush_free_hpage_work(h);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003455 spin_lock_irq(&hugetlb_lock);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003456
3457 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07003458 * Check for a node specific request.
3459 * Changing node specific huge page count may require a corresponding
3460 * change to the global count. In any case, the passed node mask
3461 * (nodes_allowed) will restrict alloc/free to the specified node.
3462 */
3463 if (nid != NUMA_NO_NODE) {
3464 unsigned long old_count = count;
3465
3466 count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
3467 /*
3468 * User may have specified a large count value which caused the
3469 * above calculation to overflow. In this case, they wanted
3470 * to allocate as many huge pages as possible. Set count to
3471 * largest possible value to align with their intention.
3472 */
3473 if (count < old_count)
3474 count = ULONG_MAX;
3475 }
3476
3477 /*
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003478 * Gigantic pages runtime allocation depend on the capability for large
3479 * page range allocation.
3480 * If the system does not provide this feature, return an error when
3481 * the user tries to allocate gigantic pages but let the user free the
3482 * boottime allocated gigantic pages.
3483 */
3484 if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) {
3485 if (count > persistent_huge_pages(h)) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003486 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz29383962021-05-04 18:34:52 -07003487 mutex_unlock(&h->resize_lock);
Mike Kravetzf60858f2019-09-23 15:37:35 -07003488 NODEMASK_FREE(node_alloc_noretry);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003489 return -EINVAL;
3490 }
3491 /* Fall through to decrease pool */
3492 }
Andi Kleenaa888a72008-07-23 21:27:47 -07003493
Adam Litke7893d1d2007-10-16 01:26:18 -07003494 /*
3495 * Increase the pool size
3496 * First take pages out of surplus state. Then make up the
3497 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08003498 *
Michal Hocko0c397da2018-01-31 16:20:56 -08003499 * We might race with alloc_surplus_huge_page() here and be unable
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08003500 * to convert a surplus huge page to a normal huge page. That is
3501 * not critical, though, it just means the overall size of the
3502 * pool might be one hugepage larger than it needs to be, but
3503 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -07003504 */
Andi Kleena5516432008-07-23 21:27:41 -07003505 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003506 if (!adjust_pool_surplus(h, nodes_allowed, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -07003507 break;
3508 }
3509
Andi Kleena5516432008-07-23 21:27:41 -07003510 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -07003511 /*
3512 * If this allocation races such that we no longer need the
3513 * page, free_huge_page will handle it by freeing the page
3514 * and reducing the surplus.
3515 */
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003516 spin_unlock_irq(&hugetlb_lock);
Jia He649920c2016-08-02 14:02:31 -07003517
3518 /* yield cpu to avoid soft lockup */
3519 cond_resched();
3520
Mike Kravetzf60858f2019-09-23 15:37:35 -07003521 ret = alloc_pool_huge_page(h, nodes_allowed,
3522 node_alloc_noretry);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003523 spin_lock_irq(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07003524 if (!ret)
3525 goto out;
3526
Mel Gorman536240f22009-12-14 17:59:56 -08003527 /* Bail for signals. Probably ctrl-c from user */
3528 if (signal_pending(current))
3529 goto out;
Adam Litke7893d1d2007-10-16 01:26:18 -07003530 }
Adam Litke7893d1d2007-10-16 01:26:18 -07003531
3532 /*
3533 * Decrease the pool size
3534 * First return free pages to the buddy allocator (being careful
3535 * to keep enough around to satisfy reservations). Then place
3536 * pages into surplus state as needed so the pool will shrink
3537 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08003538 *
3539 * By placing pages into the surplus state independent of the
3540 * overcommit value, we are allowing the surplus pool size to
3541 * exceed overcommit. There are few sane options here. Since
Michal Hocko0c397da2018-01-31 16:20:56 -08003542 * alloc_surplus_huge_page() is checking the global counter,
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08003543 * though, we'll note that we're not allowed to exceed surplus
3544 * and won't grow the pool anywhere else. Not until one of the
3545 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07003546 */
Andi Kleena5516432008-07-23 21:27:41 -07003547 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07003548 min_count = max(count, min_count);
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003549 try_to_free_low(h, min_count, nodes_allowed);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003550
3551 /*
3552 * Collect pages to be removed on list without dropping lock
3553 */
Andi Kleena5516432008-07-23 21:27:41 -07003554 while (min_count < persistent_huge_pages(h)) {
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003555 page = remove_pool_huge_page(h, nodes_allowed, 0);
3556 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 break;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003558
3559 list_add(&page->lru, &page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 }
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003561 /* free the pages after dropping lock */
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003562 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003563 update_and_free_pages_bulk(h, &page_list);
Muchun Songb65d4ad2021-06-30 18:47:17 -07003564 flush_free_hpage_work(h);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003565 spin_lock_irq(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003566
Andi Kleena5516432008-07-23 21:27:41 -07003567 while (count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003568 if (!adjust_pool_surplus(h, nodes_allowed, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07003569 break;
3570 }
3571out:
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003572 h->max_huge_pages = persistent_huge_pages(h);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003573 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz29383962021-05-04 18:34:52 -07003574 mutex_unlock(&h->resize_lock);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003575
Mike Kravetzf60858f2019-09-23 15:37:35 -07003576 NODEMASK_FREE(node_alloc_noretry);
3577
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
Mike Kravetz8531fc62021-11-05 13:41:33 -07003581static int demote_free_huge_page(struct hstate *h, struct page *page)
3582{
3583 int i, nid = page_to_nid(page);
3584 struct hstate *target_hstate;
Sidhartha Kumar911565b2022-11-29 14:50:31 -08003585 struct folio *folio = page_folio(page);
Doug Berger31731452022-09-14 12:09:17 -07003586 struct page *subpage;
Mike Kravetz8531fc62021-11-05 13:41:33 -07003587 int rc = 0;
3588
3589 target_hstate = size_to_hstate(PAGE_SIZE << h->demote_order);
3590
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08003591 remove_hugetlb_folio_for_demote(h, folio, false);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003592 spin_unlock_irq(&hugetlb_lock);
3593
Muchun Song62138342022-06-28 17:22:33 +08003594 rc = hugetlb_vmemmap_restore(h, page);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003595 if (rc) {
3596 /* Allocation of vmemmmap failed, we can not demote page */
3597 spin_lock_irq(&hugetlb_lock);
3598 set_page_refcounted(page);
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08003599 add_hugetlb_folio(h, page_folio(page), false);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003600 return rc;
3601 }
3602
3603 /*
Sidhartha Kumar911565b2022-11-29 14:50:31 -08003604 * Use destroy_compound_hugetlb_folio_for_demote for all huge page
Mike Kravetz8531fc62021-11-05 13:41:33 -07003605 * sizes as it will not ref count pages.
3606 */
Sidhartha Kumar911565b2022-11-29 14:50:31 -08003607 destroy_compound_hugetlb_folio_for_demote(folio, huge_page_order(h));
Mike Kravetz8531fc62021-11-05 13:41:33 -07003608
3609 /*
3610 * Taking target hstate mutex synchronizes with set_max_huge_pages.
3611 * Without the mutex, pages added to target hstate could be marked
3612 * as surplus.
3613 *
3614 * Note that we already hold h->resize_lock. To prevent deadlock,
3615 * use the convention of always taking larger size hstate mutex first.
3616 */
3617 mutex_lock(&target_hstate->resize_lock);
3618 for (i = 0; i < pages_per_huge_page(h);
3619 i += pages_per_huge_page(target_hstate)) {
Doug Berger31731452022-09-14 12:09:17 -07003620 subpage = nth_page(page, i);
Sidhartha Kumard1c60952022-11-29 14:50:38 -08003621 folio = page_folio(subpage);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003622 if (hstate_is_gigantic(target_hstate))
Sidhartha Kumard1c60952022-11-29 14:50:38 -08003623 prep_compound_gigantic_folio_for_demote(folio,
Mike Kravetz8531fc62021-11-05 13:41:33 -07003624 target_hstate->order);
3625 else
Doug Berger31731452022-09-14 12:09:17 -07003626 prep_compound_page(subpage, target_hstate->order);
3627 set_page_private(subpage, 0);
Sidhartha Kumard1c60952022-11-29 14:50:38 -08003628 prep_new_hugetlb_folio(target_hstate, folio, nid);
Mike Kravetz2b216242022-09-16 14:46:38 -07003629 free_huge_page(subpage);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003630 }
3631 mutex_unlock(&target_hstate->resize_lock);
3632
3633 spin_lock_irq(&hugetlb_lock);
3634
3635 /*
3636 * Not absolutely necessary, but for consistency update max_huge_pages
3637 * based on pool changes for the demoted page.
3638 */
3639 h->max_huge_pages--;
Miaohe Lina43a83c2022-08-16 21:05:48 +08003640 target_hstate->max_huge_pages +=
3641 pages_per_huge_page(h) / pages_per_huge_page(target_hstate);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003642
3643 return rc;
3644}
3645
Mike Kravetz79dfc692021-11-05 13:41:20 -07003646static int demote_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
3647 __must_hold(&hugetlb_lock)
3648{
Mike Kravetz8531fc62021-11-05 13:41:33 -07003649 int nr_nodes, node;
3650 struct page *page;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003651
3652 lockdep_assert_held(&hugetlb_lock);
3653
3654 /* We should never get here if no demote order */
3655 if (!h->demote_order) {
3656 pr_warn("HugeTLB: NULL demote order passed to demote_pool_huge_page.\n");
3657 return -EINVAL; /* internal error */
3658 }
3659
Mike Kravetz8531fc62021-11-05 13:41:33 -07003660 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
Mike Kravetz5a317412022-04-14 19:13:52 -07003661 list_for_each_entry(page, &h->hugepage_freelists[node], lru) {
3662 if (PageHWPoison(page))
3663 continue;
3664
3665 return demote_free_huge_page(h, page);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003666 }
3667 }
3668
Mike Kravetz5a317412022-04-14 19:13:52 -07003669 /*
3670 * Only way to get here is if all pages on free lists are poisoned.
3671 * Return -EBUSY so that caller will not retry.
3672 */
3673 return -EBUSY;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003674}
3675
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003676#define HSTATE_ATTR_RO(_name) \
3677 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
3678
Mike Kravetz79dfc692021-11-05 13:41:20 -07003679#define HSTATE_ATTR_WO(_name) \
3680 static struct kobj_attribute _name##_attr = __ATTR_WO(_name)
3681
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003682#define HSTATE_ATTR(_name) \
Miaohe Lin98bc26a2022-03-22 14:45:23 -07003683 static struct kobj_attribute _name##_attr = __ATTR_RW(_name)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003684
3685static struct kobject *hugepages_kobj;
3686static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
3687
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003688static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
3689
3690static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003691{
3692 int i;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003693
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003694 for (i = 0; i < HUGE_MAX_HSTATE; i++)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003695 if (hstate_kobjs[i] == kobj) {
3696 if (nidp)
3697 *nidp = NUMA_NO_NODE;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003698 return &hstates[i];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003699 }
3700
3701 return kobj_to_node_hstate(kobj, nidp);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003702}
3703
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003704static ssize_t nr_hugepages_show_common(struct kobject *kobj,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003705 struct kobj_attribute *attr, char *buf)
3706{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003707 struct hstate *h;
3708 unsigned long nr_huge_pages;
3709 int nid;
3710
3711 h = kobj_to_hstate(kobj, &nid);
3712 if (nid == NUMA_NO_NODE)
3713 nr_huge_pages = h->nr_huge_pages;
3714 else
3715 nr_huge_pages = h->nr_huge_pages_node[nid];
3716
Joe Perchesae7a9272020-12-14 19:14:42 -08003717 return sysfs_emit(buf, "%lu\n", nr_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003718}
Eric B Munsonadbe8722011-01-13 15:47:27 -08003719
David Rientjes238d3c12014-08-06 16:06:51 -07003720static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
3721 struct hstate *h, int nid,
3722 unsigned long count, size_t len)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003723{
3724 int err;
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003725 nodemask_t nodes_allowed, *n_mask;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003726
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003727 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
3728 return -EINVAL;
Eric B Munsonadbe8722011-01-13 15:47:27 -08003729
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003730 if (nid == NUMA_NO_NODE) {
3731 /*
3732 * global hstate attribute
3733 */
3734 if (!(obey_mempolicy &&
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003735 init_nodemask_of_mempolicy(&nodes_allowed)))
3736 n_mask = &node_states[N_MEMORY];
3737 else
3738 n_mask = &nodes_allowed;
3739 } else {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003740 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07003741 * Node specific request. count adjustment happens in
3742 * set_max_huge_pages() after acquiring hugetlb_lock.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003743 */
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003744 init_nodemask_of_node(&nodes_allowed, nid);
3745 n_mask = &nodes_allowed;
Mike Kravetzfd875dc2019-05-13 17:19:20 -07003746 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003747
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003748 err = set_max_huge_pages(h, count, nid, n_mask);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003749
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003750 return err ? err : len;
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003751}
3752
David Rientjes238d3c12014-08-06 16:06:51 -07003753static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
3754 struct kobject *kobj, const char *buf,
3755 size_t len)
3756{
3757 struct hstate *h;
3758 unsigned long count;
3759 int nid;
3760 int err;
3761
3762 err = kstrtoul(buf, 10, &count);
3763 if (err)
3764 return err;
3765
3766 h = kobj_to_hstate(kobj, &nid);
3767 return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
3768}
3769
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003770static ssize_t nr_hugepages_show(struct kobject *kobj,
3771 struct kobj_attribute *attr, char *buf)
3772{
3773 return nr_hugepages_show_common(kobj, attr, buf);
3774}
3775
3776static ssize_t nr_hugepages_store(struct kobject *kobj,
3777 struct kobj_attribute *attr, const char *buf, size_t len)
3778{
David Rientjes238d3c12014-08-06 16:06:51 -07003779 return nr_hugepages_store_common(false, kobj, buf, len);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003780}
3781HSTATE_ATTR(nr_hugepages);
3782
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003783#ifdef CONFIG_NUMA
3784
3785/*
3786 * hstate attribute for optionally mempolicy-based constraint on persistent
3787 * huge page alloc/free.
3788 */
3789static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
Joe Perchesae7a9272020-12-14 19:14:42 -08003790 struct kobj_attribute *attr,
3791 char *buf)
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003792{
3793 return nr_hugepages_show_common(kobj, attr, buf);
3794}
3795
3796static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
3797 struct kobj_attribute *attr, const char *buf, size_t len)
3798{
David Rientjes238d3c12014-08-06 16:06:51 -07003799 return nr_hugepages_store_common(true, kobj, buf, len);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003800}
3801HSTATE_ATTR(nr_hugepages_mempolicy);
3802#endif
3803
3804
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003805static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
3806 struct kobj_attribute *attr, char *buf)
3807{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003808 struct hstate *h = kobj_to_hstate(kobj, NULL);
Joe Perchesae7a9272020-12-14 19:14:42 -08003809 return sysfs_emit(buf, "%lu\n", h->nr_overcommit_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003810}
Eric B Munsonadbe8722011-01-13 15:47:27 -08003811
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003812static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
3813 struct kobj_attribute *attr, const char *buf, size_t count)
3814{
3815 int err;
3816 unsigned long input;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003817 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003818
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003819 if (hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08003820 return -EINVAL;
3821
Jingoo Han3dbb95f2013-09-11 14:20:25 -07003822 err = kstrtoul(buf, 10, &input);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003823 if (err)
Eric B Munson73ae31e2011-01-13 15:47:28 -08003824 return err;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003825
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003826 spin_lock_irq(&hugetlb_lock);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003827 h->nr_overcommit_huge_pages = input;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003828 spin_unlock_irq(&hugetlb_lock);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003829
3830 return count;
3831}
3832HSTATE_ATTR(nr_overcommit_hugepages);
3833
3834static ssize_t free_hugepages_show(struct kobject *kobj,
3835 struct kobj_attribute *attr, char *buf)
3836{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003837 struct hstate *h;
3838 unsigned long free_huge_pages;
3839 int nid;
3840
3841 h = kobj_to_hstate(kobj, &nid);
3842 if (nid == NUMA_NO_NODE)
3843 free_huge_pages = h->free_huge_pages;
3844 else
3845 free_huge_pages = h->free_huge_pages_node[nid];
3846
Joe Perchesae7a9272020-12-14 19:14:42 -08003847 return sysfs_emit(buf, "%lu\n", free_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003848}
3849HSTATE_ATTR_RO(free_hugepages);
3850
3851static ssize_t resv_hugepages_show(struct kobject *kobj,
3852 struct kobj_attribute *attr, char *buf)
3853{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003854 struct hstate *h = kobj_to_hstate(kobj, NULL);
Joe Perchesae7a9272020-12-14 19:14:42 -08003855 return sysfs_emit(buf, "%lu\n", h->resv_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003856}
3857HSTATE_ATTR_RO(resv_hugepages);
3858
3859static ssize_t surplus_hugepages_show(struct kobject *kobj,
3860 struct kobj_attribute *attr, char *buf)
3861{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003862 struct hstate *h;
3863 unsigned long surplus_huge_pages;
3864 int nid;
3865
3866 h = kobj_to_hstate(kobj, &nid);
3867 if (nid == NUMA_NO_NODE)
3868 surplus_huge_pages = h->surplus_huge_pages;
3869 else
3870 surplus_huge_pages = h->surplus_huge_pages_node[nid];
3871
Joe Perchesae7a9272020-12-14 19:14:42 -08003872 return sysfs_emit(buf, "%lu\n", surplus_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003873}
3874HSTATE_ATTR_RO(surplus_hugepages);
3875
Mike Kravetz79dfc692021-11-05 13:41:20 -07003876static ssize_t demote_store(struct kobject *kobj,
3877 struct kobj_attribute *attr, const char *buf, size_t len)
3878{
3879 unsigned long nr_demote;
3880 unsigned long nr_available;
3881 nodemask_t nodes_allowed, *n_mask;
3882 struct hstate *h;
Li zeming8eeda552022-09-05 10:09:18 +08003883 int err;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003884 int nid;
3885
3886 err = kstrtoul(buf, 10, &nr_demote);
3887 if (err)
3888 return err;
3889 h = kobj_to_hstate(kobj, &nid);
3890
3891 if (nid != NUMA_NO_NODE) {
3892 init_nodemask_of_node(&nodes_allowed, nid);
3893 n_mask = &nodes_allowed;
3894 } else {
3895 n_mask = &node_states[N_MEMORY];
3896 }
3897
3898 /* Synchronize with other sysfs operations modifying huge pages */
3899 mutex_lock(&h->resize_lock);
3900 spin_lock_irq(&hugetlb_lock);
3901
3902 while (nr_demote) {
3903 /*
3904 * Check for available pages to demote each time thorough the
3905 * loop as demote_pool_huge_page will drop hugetlb_lock.
Mike Kravetz79dfc692021-11-05 13:41:20 -07003906 */
3907 if (nid != NUMA_NO_NODE)
3908 nr_available = h->free_huge_pages_node[nid];
3909 else
3910 nr_available = h->free_huge_pages;
3911 nr_available -= h->resv_huge_pages;
3912 if (!nr_available)
3913 break;
3914
3915 err = demote_pool_huge_page(h, n_mask);
3916 if (err)
3917 break;
3918
3919 nr_demote--;
3920 }
3921
3922 spin_unlock_irq(&hugetlb_lock);
3923 mutex_unlock(&h->resize_lock);
3924
3925 if (err)
3926 return err;
3927 return len;
3928}
3929HSTATE_ATTR_WO(demote);
3930
3931static ssize_t demote_size_show(struct kobject *kobj,
3932 struct kobj_attribute *attr, char *buf)
3933{
Miaohe Lin12658ab2022-09-01 20:00:26 +08003934 struct hstate *h = kobj_to_hstate(kobj, NULL);
Mike Kravetz79dfc692021-11-05 13:41:20 -07003935 unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K;
3936
3937 return sysfs_emit(buf, "%lukB\n", demote_size);
3938}
3939
3940static ssize_t demote_size_store(struct kobject *kobj,
3941 struct kobj_attribute *attr,
3942 const char *buf, size_t count)
3943{
3944 struct hstate *h, *demote_hstate;
3945 unsigned long demote_size;
3946 unsigned int demote_order;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003947
3948 demote_size = (unsigned long)memparse(buf, NULL);
3949
3950 demote_hstate = size_to_hstate(demote_size);
3951 if (!demote_hstate)
3952 return -EINVAL;
3953 demote_order = demote_hstate->order;
Mike Kravetza01f4392021-11-05 13:41:27 -07003954 if (demote_order < HUGETLB_PAGE_ORDER)
3955 return -EINVAL;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003956
3957 /* demote order must be smaller than hstate order */
Miaohe Lin12658ab2022-09-01 20:00:26 +08003958 h = kobj_to_hstate(kobj, NULL);
Mike Kravetz79dfc692021-11-05 13:41:20 -07003959 if (demote_order >= h->order)
3960 return -EINVAL;
3961
3962 /* resize_lock synchronizes access to demote size and writes */
3963 mutex_lock(&h->resize_lock);
3964 h->demote_order = demote_order;
3965 mutex_unlock(&h->resize_lock);
3966
3967 return count;
3968}
3969HSTATE_ATTR(demote_size);
3970
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003971static struct attribute *hstate_attrs[] = {
3972 &nr_hugepages_attr.attr,
3973 &nr_overcommit_hugepages_attr.attr,
3974 &free_hugepages_attr.attr,
3975 &resv_hugepages_attr.attr,
3976 &surplus_hugepages_attr.attr,
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003977#ifdef CONFIG_NUMA
3978 &nr_hugepages_mempolicy_attr.attr,
3979#endif
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003980 NULL,
3981};
3982
Arvind Yadav67e5ed92017-09-06 16:22:06 -07003983static const struct attribute_group hstate_attr_group = {
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003984 .attrs = hstate_attrs,
3985};
3986
Mike Kravetz79dfc692021-11-05 13:41:20 -07003987static struct attribute *hstate_demote_attrs[] = {
3988 &demote_size_attr.attr,
3989 &demote_attr.attr,
3990 NULL,
3991};
3992
3993static const struct attribute_group hstate_demote_attr_group = {
3994 .attrs = hstate_demote_attrs,
3995};
3996
Jeff Mahoney094e9532010-02-02 13:44:14 -08003997static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
3998 struct kobject **hstate_kobjs,
Arvind Yadav67e5ed92017-09-06 16:22:06 -07003999 const struct attribute_group *hstate_attr_group)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004000{
4001 int retval;
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004002 int hi = hstate_index(h);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004003
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004004 hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
4005 if (!hstate_kobjs[hi])
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004006 return -ENOMEM;
4007
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004008 retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
Miaohe Lincc2205a2021-02-24 12:06:50 -08004009 if (retval) {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004010 kobject_put(hstate_kobjs[hi]);
Miaohe Lincc2205a2021-02-24 12:06:50 -08004011 hstate_kobjs[hi] = NULL;
Miaohe Lin3a6bdda2022-08-16 21:05:49 +08004012 return retval;
Miaohe Lincc2205a2021-02-24 12:06:50 -08004013 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004014
Mike Kravetz79dfc692021-11-05 13:41:20 -07004015 if (h->demote_order) {
Miaohe Lin01088a62022-08-16 21:05:52 +08004016 retval = sysfs_create_group(hstate_kobjs[hi],
4017 &hstate_demote_attr_group);
4018 if (retval) {
Mike Kravetz79dfc692021-11-05 13:41:20 -07004019 pr_warn("HugeTLB unable to create demote interfaces for %s\n", h->name);
Miaohe Lin01088a62022-08-16 21:05:52 +08004020 sysfs_remove_group(hstate_kobjs[hi], hstate_attr_group);
4021 kobject_put(hstate_kobjs[hi]);
4022 hstate_kobjs[hi] = NULL;
4023 return retval;
4024 }
Mike Kravetz79dfc692021-11-05 13:41:20 -07004025 }
4026
Miaohe Lin01088a62022-08-16 21:05:52 +08004027 return 0;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004028}
4029
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004030#ifdef CONFIG_NUMA
Muchun Songa4a00b42022-09-14 15:26:03 +08004031static bool hugetlb_sysfs_initialized __ro_after_init;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004032
4033/*
4034 * node_hstate/s - associate per node hstate attributes, via their kobjects,
Kay Sievers10fbcf42011-12-21 14:48:43 -08004035 * with node devices in node_devices[] using a parallel array. The array
4036 * index of a node device or _hstate == node id.
4037 * This is here to avoid any static dependency of the node device driver, in
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004038 * the base kernel, on the hugetlb module.
4039 */
4040struct node_hstate {
4041 struct kobject *hugepages_kobj;
4042 struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
4043};
Alexander Kuleshovb4e289a2015-11-05 18:50:14 -08004044static struct node_hstate node_hstates[MAX_NUMNODES];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004045
4046/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08004047 * A subset of global hstate attributes for node devices
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004048 */
4049static struct attribute *per_node_hstate_attrs[] = {
4050 &nr_hugepages_attr.attr,
4051 &free_hugepages_attr.attr,
4052 &surplus_hugepages_attr.attr,
4053 NULL,
4054};
4055
Arvind Yadav67e5ed92017-09-06 16:22:06 -07004056static const struct attribute_group per_node_hstate_attr_group = {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004057 .attrs = per_node_hstate_attrs,
4058};
4059
4060/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08004061 * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004062 * Returns node id via non-NULL nidp.
4063 */
4064static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
4065{
4066 int nid;
4067
4068 for (nid = 0; nid < nr_node_ids; nid++) {
4069 struct node_hstate *nhs = &node_hstates[nid];
4070 int i;
4071 for (i = 0; i < HUGE_MAX_HSTATE; i++)
4072 if (nhs->hstate_kobjs[i] == kobj) {
4073 if (nidp)
4074 *nidp = nid;
4075 return &hstates[i];
4076 }
4077 }
4078
4079 BUG();
4080 return NULL;
4081}
4082
4083/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08004084 * Unregister hstate attributes from a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004085 * No-op if no hstate attributes attached.
4086 */
Muchun Songa4a00b42022-09-14 15:26:03 +08004087void hugetlb_unregister_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004088{
4089 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08004090 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004091
4092 if (!nhs->hugepages_kobj)
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08004093 return; /* no hstate attributes */
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004094
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004095 for_each_hstate(h) {
4096 int idx = hstate_index(h);
Miaohe Lin01088a62022-08-16 21:05:52 +08004097 struct kobject *hstate_kobj = nhs->hstate_kobjs[idx];
4098
4099 if (!hstate_kobj)
4100 continue;
4101 if (h->demote_order)
4102 sysfs_remove_group(hstate_kobj, &hstate_demote_attr_group);
4103 sysfs_remove_group(hstate_kobj, &per_node_hstate_attr_group);
4104 kobject_put(hstate_kobj);
4105 nhs->hstate_kobjs[idx] = NULL;
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004106 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004107
4108 kobject_put(nhs->hugepages_kobj);
4109 nhs->hugepages_kobj = NULL;
4110}
4111
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004112
4113/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08004114 * Register hstate attributes for a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004115 * No-op if attributes already registered.
4116 */
Muchun Songa4a00b42022-09-14 15:26:03 +08004117void hugetlb_register_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004118{
4119 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08004120 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004121 int err;
4122
Muchun Songa4a00b42022-09-14 15:26:03 +08004123 if (!hugetlb_sysfs_initialized)
4124 return;
4125
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004126 if (nhs->hugepages_kobj)
4127 return; /* already allocated */
4128
4129 nhs->hugepages_kobj = kobject_create_and_add("hugepages",
Kay Sievers10fbcf42011-12-21 14:48:43 -08004130 &node->dev.kobj);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004131 if (!nhs->hugepages_kobj)
4132 return;
4133
4134 for_each_hstate(h) {
4135 err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
4136 nhs->hstate_kobjs,
4137 &per_node_hstate_attr_group);
4138 if (err) {
Mike Kravetz282f4212020-06-03 16:00:46 -07004139 pr_err("HugeTLB: Unable to add hstate %s for node %d\n",
Andrew Mortonffb22af2013-02-22 16:32:08 -08004140 h->name, node->dev.id);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004141 hugetlb_unregister_node(node);
4142 break;
4143 }
4144 }
4145}
4146
4147/*
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08004148 * hugetlb init time: register hstate attributes for all registered node
Kay Sievers10fbcf42011-12-21 14:48:43 -08004149 * devices of nodes that have memory. All on-line nodes should have
4150 * registered their associated device by this time.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004151 */
Luiz Capitulino7d9ca002014-12-12 16:55:24 -08004152static void __init hugetlb_register_all_nodes(void)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004153{
4154 int nid;
4155
Muchun Songa4a00b42022-09-14 15:26:03 +08004156 for_each_online_node(nid)
Muchun Songb958d4d2022-09-14 15:26:02 +08004157 hugetlb_register_node(node_devices[nid]);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004158}
4159#else /* !CONFIG_NUMA */
4160
4161static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
4162{
4163 BUG();
4164 if (nidp)
4165 *nidp = -1;
4166 return NULL;
4167}
4168
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004169static void hugetlb_register_all_nodes(void) { }
4170
4171#endif
4172
Miaohe Lin263b8992022-09-01 20:00:21 +08004173#ifdef CONFIG_CMA
4174static void __init hugetlb_cma_check(void);
4175#else
4176static inline __init void hugetlb_cma_check(void)
4177{
4178}
4179#endif
4180
Muchun Songa4a00b42022-09-14 15:26:03 +08004181static void __init hugetlb_sysfs_init(void)
4182{
4183 struct hstate *h;
4184 int err;
4185
4186 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
4187 if (!hugepages_kobj)
4188 return;
4189
4190 for_each_hstate(h) {
4191 err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
4192 hstate_kobjs, &hstate_attr_group);
4193 if (err)
4194 pr_err("HugeTLB: Unable to add hstate %s", h->name);
4195 }
4196
4197#ifdef CONFIG_NUMA
4198 hugetlb_sysfs_initialized = true;
4199#endif
4200 hugetlb_register_all_nodes();
4201}
4202
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004203static int __init hugetlb_init(void)
4204{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004205 int i;
4206
Mike Kravetzd6995da2021-02-24 12:08:51 -08004207 BUILD_BUG_ON(sizeof_field(struct page, private) * BITS_PER_BYTE <
4208 __NR_HPAGEFLAGS);
4209
Mike Kravetzc2833a52020-06-03 16:00:50 -07004210 if (!hugepages_supported()) {
4211 if (hugetlb_max_hstate || default_hstate_max_huge_pages)
4212 pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n");
Benjamin Herrenschmidt0ef89d22008-07-31 00:07:30 -07004213 return 0;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004214 }
Vaishali Thakkarf8b74812016-02-17 13:11:26 -08004215
Mike Kravetz282f4212020-06-03 16:00:46 -07004216 /*
4217 * Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists. Some
4218 * architectures depend on setup being done here.
4219 */
4220 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
4221 if (!parsed_default_hugepagesz) {
4222 /*
4223 * If we did not parse a default huge page size, set
4224 * default_hstate_idx to HPAGE_SIZE hstate. And, if the
4225 * number of huge pages for this default size was implicitly
4226 * specified, set that here as well.
4227 * Note that the implicit setting will overwrite an explicit
4228 * setting. A warning will be printed in this case.
4229 */
4230 default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE));
4231 if (default_hstate_max_huge_pages) {
4232 if (default_hstate.max_huge_pages) {
4233 char buf[32];
Andi Kleenaa888a72008-07-23 21:27:47 -07004234
Mike Kravetz282f4212020-06-03 16:00:46 -07004235 string_get_size(huge_page_size(&default_hstate),
4236 1, STRING_UNITS_2, buf, 32);
4237 pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n",
4238 default_hstate.max_huge_pages, buf);
4239 pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n",
4240 default_hstate_max_huge_pages);
4241 }
4242 default_hstate.max_huge_pages =
4243 default_hstate_max_huge_pages;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004244
Peng Liu0a7a0f62022-04-29 14:36:57 -07004245 for_each_online_node(i)
Zhenguo Yaob5389082021-11-05 13:43:28 -07004246 default_hstate.max_huge_pages_node[i] =
4247 default_hugepages_in_node[i];
Mike Kravetz282f4212020-06-03 16:00:46 -07004248 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004249 }
Andi Kleenaa888a72008-07-23 21:27:47 -07004250
Roman Gushchincf11e852020-04-10 14:32:45 -07004251 hugetlb_cma_check();
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004252 hugetlb_init_hstates();
Andi Kleenaa888a72008-07-23 21:27:47 -07004253 gather_bootmem_prealloc();
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004254 report_hugepages();
4255
4256 hugetlb_sysfs_init();
Jianguo Wu7179e7b2012-12-18 14:23:19 -08004257 hugetlb_cgroup_file_init();
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004258
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004259#ifdef CONFIG_SMP
4260 num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
4261#else
4262 num_fault_mutexes = 1;
4263#endif
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004264 hugetlb_fault_mutex_table =
Kees Cook6da2ec52018-06-12 13:55:00 -07004265 kmalloc_array(num_fault_mutexes, sizeof(struct mutex),
4266 GFP_KERNEL);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004267 BUG_ON(!hugetlb_fault_mutex_table);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004268
4269 for (i = 0; i < num_fault_mutexes; i++)
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004270 mutex_init(&hugetlb_fault_mutex_table[i]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004271 return 0;
4272}
Paul Gortmaker3e89e1c2016-01-14 15:21:52 -08004273subsys_initcall(hugetlb_init);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004274
Mike Kravetzae94da82020-06-03 16:00:34 -07004275/* Overwritten by architectures with more huge page sizes */
4276bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size)
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004277{
Mike Kravetzae94da82020-06-03 16:00:34 -07004278 return size == HPAGE_SIZE;
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004279}
4280
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08004281void __init hugetlb_add_hstate(unsigned int order)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004282{
4283 struct hstate *h;
Andi Kleen8faa8b02008-07-23 21:27:48 -07004284 unsigned long i;
4285
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004286 if (size_to_hstate(PAGE_SIZE << order)) {
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004287 return;
4288 }
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07004289 BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004290 BUG_ON(order == 0);
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07004291 h = &hstates[hugetlb_max_hstate++];
Mike Kravetz29383962021-05-04 18:34:52 -07004292 mutex_init(&h->resize_lock);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004293 h->order = order;
Miaohe Linaca78302021-02-24 12:07:46 -08004294 h->mask = ~(huge_page_size(h) - 1);
Andi Kleen8faa8b02008-07-23 21:27:48 -07004295 for (i = 0; i < MAX_NUMNODES; ++i)
4296 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07004297 INIT_LIST_HEAD(&h->hugepage_activelist);
Andrew Morton54f18d32016-05-19 17:11:40 -07004298 h->next_nid_to_alloc = first_memory_node;
4299 h->next_nid_to_free = first_memory_node;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004300 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
Miaohe Linc2c3a602022-09-01 20:00:22 +08004301 huge_page_size(h)/SZ_1K);
Andi Kleen8faa8b02008-07-23 21:27:48 -07004302
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004303 parsed_hstate = h;
4304}
4305
Zhenguo Yaob5389082021-11-05 13:43:28 -07004306bool __init __weak hugetlb_node_alloc_supported(void)
4307{
4308 return true;
4309}
Peng Liuf87442f2022-04-29 14:36:57 -07004310
4311static void __init hugepages_clear_pages_in_node(void)
4312{
4313 if (!hugetlb_max_hstate) {
4314 default_hstate_max_huge_pages = 0;
4315 memset(default_hugepages_in_node, 0,
Miaohe Lin10395682022-09-01 20:00:24 +08004316 sizeof(default_hugepages_in_node));
Peng Liuf87442f2022-04-29 14:36:57 -07004317 } else {
4318 parsed_hstate->max_huge_pages = 0;
4319 memset(parsed_hstate->max_huge_pages_node, 0,
Miaohe Lin10395682022-09-01 20:00:24 +08004320 sizeof(parsed_hstate->max_huge_pages_node));
Peng Liuf87442f2022-04-29 14:36:57 -07004321 }
4322}
4323
Mike Kravetz282f4212020-06-03 16:00:46 -07004324/*
4325 * hugepages command line processing
4326 * hugepages normally follows a valid hugepagsz or default_hugepagsz
4327 * specification. If not, ignore the hugepages value. hugepages can also
4328 * be the first huge page command line option in which case it implicitly
4329 * specifies the number of huge pages for the default size.
4330 */
4331static int __init hugepages_setup(char *s)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004332{
4333 unsigned long *mhp;
Andi Kleen8faa8b02008-07-23 21:27:48 -07004334 static unsigned long *last_mhp;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004335 int node = NUMA_NO_NODE;
4336 int count;
4337 unsigned long tmp;
4338 char *p = s;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004339
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004340 if (!parsed_valid_hugepagesz) {
Mike Kravetz282f4212020-06-03 16:00:46 -07004341 pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s);
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004342 parsed_valid_hugepagesz = true;
Peng Liuf81f6e42022-04-29 14:36:57 -07004343 return 1;
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004344 }
Mike Kravetz282f4212020-06-03 16:00:46 -07004345
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004346 /*
Mike Kravetz282f4212020-06-03 16:00:46 -07004347 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
4348 * yet, so this hugepages= parameter goes to the "default hstate".
4349 * Otherwise, it goes with the previously parsed hugepagesz or
4350 * default_hugepagesz.
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004351 */
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004352 else if (!hugetlb_max_hstate)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004353 mhp = &default_hstate_max_huge_pages;
4354 else
4355 mhp = &parsed_hstate->max_huge_pages;
4356
Andi Kleen8faa8b02008-07-23 21:27:48 -07004357 if (mhp == last_mhp) {
Mike Kravetz282f4212020-06-03 16:00:46 -07004358 pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004359 return 1;
Andi Kleen8faa8b02008-07-23 21:27:48 -07004360 }
4361
Zhenguo Yaob5389082021-11-05 13:43:28 -07004362 while (*p) {
4363 count = 0;
4364 if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4365 goto invalid;
4366 /* Parameter is node format */
4367 if (p[count] == ':') {
4368 if (!hugetlb_node_alloc_supported()) {
4369 pr_warn("HugeTLB: architecture can't support node specific alloc, ignoring!\n");
Peng Liuf81f6e42022-04-29 14:36:57 -07004370 return 1;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004371 }
Peng Liu0a7a0f62022-04-29 14:36:57 -07004372 if (tmp >= MAX_NUMNODES || !node_online(tmp))
Liu Yuntaoe79ce982022-02-25 19:11:02 -08004373 goto invalid;
Peng Liu0a7a0f62022-04-29 14:36:57 -07004374 node = array_index_nospec(tmp, MAX_NUMNODES);
Zhenguo Yaob5389082021-11-05 13:43:28 -07004375 p += count + 1;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004376 /* Parse hugepages */
4377 if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4378 goto invalid;
4379 if (!hugetlb_max_hstate)
4380 default_hugepages_in_node[node] = tmp;
4381 else
4382 parsed_hstate->max_huge_pages_node[node] = tmp;
4383 *mhp += tmp;
4384 /* Go to parse next node*/
4385 if (p[count] == ',')
4386 p += count + 1;
4387 else
4388 break;
4389 } else {
4390 if (p != s)
4391 goto invalid;
4392 *mhp = tmp;
4393 break;
4394 }
4395 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004396
Andi Kleen8faa8b02008-07-23 21:27:48 -07004397 /*
4398 * Global state is always initialized later in hugetlb_init.
Miaohe Lin04adbc32021-05-04 18:33:22 -07004399 * But we need to allocate gigantic hstates here early to still
Andi Kleen8faa8b02008-07-23 21:27:48 -07004400 * use the bootmem allocator.
4401 */
Miaohe Lin04adbc32021-05-04 18:33:22 -07004402 if (hugetlb_max_hstate && hstate_is_gigantic(parsed_hstate))
Andi Kleen8faa8b02008-07-23 21:27:48 -07004403 hugetlb_hstate_alloc_pages(parsed_hstate);
4404
4405 last_mhp = mhp;
4406
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004407 return 1;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004408
4409invalid:
4410 pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p);
Peng Liuf87442f2022-04-29 14:36:57 -07004411 hugepages_clear_pages_in_node();
Peng Liuf81f6e42022-04-29 14:36:57 -07004412 return 1;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004413}
Mike Kravetz282f4212020-06-03 16:00:46 -07004414__setup("hugepages=", hugepages_setup);
Nick Piggine11bfbf2008-07-23 21:27:52 -07004415
Mike Kravetz282f4212020-06-03 16:00:46 -07004416/*
4417 * hugepagesz command line processing
4418 * A specific huge page size can only be specified once with hugepagesz.
4419 * hugepagesz is followed by hugepages on the command line. The global
4420 * variable 'parsed_valid_hugepagesz' is used to determine if prior
4421 * hugepagesz argument was valid.
4422 */
Mike Kravetz359f2542020-06-03 16:00:38 -07004423static int __init hugepagesz_setup(char *s)
Nick Piggine11bfbf2008-07-23 21:27:52 -07004424{
Mike Kravetz359f2542020-06-03 16:00:38 -07004425 unsigned long size;
Mike Kravetz282f4212020-06-03 16:00:46 -07004426 struct hstate *h;
4427
4428 parsed_valid_hugepagesz = false;
Mike Kravetz359f2542020-06-03 16:00:38 -07004429 size = (unsigned long)memparse(s, NULL);
4430
4431 if (!arch_hugetlb_valid_size(size)) {
Mike Kravetz282f4212020-06-03 16:00:46 -07004432 pr_err("HugeTLB: unsupported hugepagesz=%s\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004433 return 1;
Mike Kravetz359f2542020-06-03 16:00:38 -07004434 }
4435
Mike Kravetz282f4212020-06-03 16:00:46 -07004436 h = size_to_hstate(size);
4437 if (h) {
4438 /*
4439 * hstate for this size already exists. This is normally
4440 * an error, but is allowed if the existing hstate is the
4441 * default hstate. More specifically, it is only allowed if
4442 * the number of huge pages for the default hstate was not
4443 * previously specified.
4444 */
4445 if (!parsed_default_hugepagesz || h != &default_hstate ||
4446 default_hstate.max_huge_pages) {
4447 pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004448 return 1;
Mike Kravetz282f4212020-06-03 16:00:46 -07004449 }
4450
4451 /*
4452 * No need to call hugetlb_add_hstate() as hstate already
4453 * exists. But, do set parsed_hstate so that a following
4454 * hugepages= parameter will be applied to this hstate.
4455 */
4456 parsed_hstate = h;
4457 parsed_valid_hugepagesz = true;
4458 return 1;
Mike Kravetz38237832020-06-03 16:00:42 -07004459 }
4460
Mike Kravetz359f2542020-06-03 16:00:38 -07004461 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
Mike Kravetz282f4212020-06-03 16:00:46 -07004462 parsed_valid_hugepagesz = true;
Nick Piggine11bfbf2008-07-23 21:27:52 -07004463 return 1;
4464}
Mike Kravetz359f2542020-06-03 16:00:38 -07004465__setup("hugepagesz=", hugepagesz_setup);
4466
Mike Kravetz282f4212020-06-03 16:00:46 -07004467/*
4468 * default_hugepagesz command line input
4469 * Only one instance of default_hugepagesz allowed on command line.
4470 */
Mike Kravetzae94da82020-06-03 16:00:34 -07004471static int __init default_hugepagesz_setup(char *s)
Nick Piggine11bfbf2008-07-23 21:27:52 -07004472{
Mike Kravetzae94da82020-06-03 16:00:34 -07004473 unsigned long size;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004474 int i;
Mike Kravetzae94da82020-06-03 16:00:34 -07004475
Mike Kravetz282f4212020-06-03 16:00:46 -07004476 parsed_valid_hugepagesz = false;
Mike Kravetz282f4212020-06-03 16:00:46 -07004477 if (parsed_default_hugepagesz) {
4478 pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004479 return 1;
Mike Kravetz282f4212020-06-03 16:00:46 -07004480 }
4481
4482 size = (unsigned long)memparse(s, NULL);
4483
4484 if (!arch_hugetlb_valid_size(size)) {
4485 pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004486 return 1;
Mike Kravetz282f4212020-06-03 16:00:46 -07004487 }
4488
4489 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
4490 parsed_valid_hugepagesz = true;
4491 parsed_default_hugepagesz = true;
4492 default_hstate_idx = hstate_index(size_to_hstate(size));
4493
4494 /*
4495 * The number of default huge pages (for this size) could have been
4496 * specified as the first hugetlb parameter: hugepages=X. If so,
4497 * then default_hstate_max_huge_pages is set. If the default huge
4498 * page size is gigantic (>= MAX_ORDER), then the pages must be
4499 * allocated here from bootmem allocator.
4500 */
4501 if (default_hstate_max_huge_pages) {
4502 default_hstate.max_huge_pages = default_hstate_max_huge_pages;
Peng Liu0a7a0f62022-04-29 14:36:57 -07004503 for_each_online_node(i)
Zhenguo Yaob5389082021-11-05 13:43:28 -07004504 default_hstate.max_huge_pages_node[i] =
4505 default_hugepages_in_node[i];
Mike Kravetz282f4212020-06-03 16:00:46 -07004506 if (hstate_is_gigantic(&default_hstate))
4507 hugetlb_hstate_alloc_pages(&default_hstate);
4508 default_hstate_max_huge_pages = 0;
4509 }
4510
Nick Piggine11bfbf2008-07-23 21:27:52 -07004511 return 1;
4512}
Mike Kravetzae94da82020-06-03 16:00:34 -07004513__setup("default_hugepagesz=", default_hugepagesz_setup);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004514
Feng Tangd2226eb2022-08-05 08:59:03 +08004515static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
4516{
4517#ifdef CONFIG_NUMA
4518 struct mempolicy *mpol = get_task_policy(current);
4519
4520 /*
4521 * Only enforce MPOL_BIND policy which overlaps with cpuset policy
4522 * (from policy_nodemask) specifically for hugetlb case
4523 */
4524 if (mpol->mode == MPOL_BIND &&
4525 (apply_policy_zone(mpol, gfp_zone(gfp)) &&
4526 cpuset_nodemask_valid_mems_allowed(&mpol->nodes)))
4527 return &mpol->nodes;
4528#endif
4529 return NULL;
4530}
4531
Muchun Song8ca39e62020-08-11 18:30:32 -07004532static unsigned int allowed_mems_nr(struct hstate *h)
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07004533{
4534 int node;
4535 unsigned int nr = 0;
Feng Tangd2226eb2022-08-05 08:59:03 +08004536 nodemask_t *mbind_nodemask;
Muchun Song8ca39e62020-08-11 18:30:32 -07004537 unsigned int *array = h->free_huge_pages_node;
4538 gfp_t gfp_mask = htlb_alloc_mask(h);
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07004539
Feng Tangd2226eb2022-08-05 08:59:03 +08004540 mbind_nodemask = policy_mbind_nodemask(gfp_mask);
Muchun Song8ca39e62020-08-11 18:30:32 -07004541 for_each_node_mask(node, cpuset_current_mems_allowed) {
Feng Tangd2226eb2022-08-05 08:59:03 +08004542 if (!mbind_nodemask || node_isset(node, *mbind_nodemask))
Muchun Song8ca39e62020-08-11 18:30:32 -07004543 nr += array[node];
4544 }
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07004545
4546 return nr;
4547}
4548
4549#ifdef CONFIG_SYSCTL
Muchun Song17743792020-09-04 16:36:13 -07004550static int proc_hugetlb_doulongvec_minmax(struct ctl_table *table, int write,
4551 void *buffer, size_t *length,
4552 loff_t *ppos, unsigned long *out)
4553{
4554 struct ctl_table dup_table;
4555
4556 /*
4557 * In order to avoid races with __do_proc_doulongvec_minmax(), we
4558 * can duplicate the @table and alter the duplicate of it.
4559 */
4560 dup_table = *table;
4561 dup_table.data = out;
4562
4563 return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos);
4564}
4565
Lee Schermerhorn06808b02009-12-14 17:58:21 -08004566static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
4567 struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02004568 void *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
Andi Kleene5ff2152008-07-23 21:27:42 -07004570 struct hstate *h = &default_hstate;
David Rientjes238d3c12014-08-06 16:06:51 -07004571 unsigned long tmp = h->max_huge_pages;
Michal Hocko08d4a242011-01-13 15:47:26 -08004572 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07004573
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004574 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08004575 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004576
Muchun Song17743792020-09-04 16:36:13 -07004577 ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
4578 &tmp);
Michal Hocko08d4a242011-01-13 15:47:26 -08004579 if (ret)
4580 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07004581
David Rientjes238d3c12014-08-06 16:06:51 -07004582 if (write)
4583 ret = __nr_hugepages_store_common(obey_mempolicy, h,
4584 NUMA_NO_NODE, tmp, *length);
Michal Hocko08d4a242011-01-13 15:47:26 -08004585out:
4586 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587}
Mel Gorman396faf02007-07-17 04:03:13 -07004588
Lee Schermerhorn06808b02009-12-14 17:58:21 -08004589int hugetlb_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02004590 void *buffer, size_t *length, loff_t *ppos)
Lee Schermerhorn06808b02009-12-14 17:58:21 -08004591{
4592
4593 return hugetlb_sysctl_handler_common(false, table, write,
4594 buffer, length, ppos);
4595}
4596
4597#ifdef CONFIG_NUMA
4598int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02004599 void *buffer, size_t *length, loff_t *ppos)
Lee Schermerhorn06808b02009-12-14 17:58:21 -08004600{
4601 return hugetlb_sysctl_handler_common(true, table, write,
4602 buffer, length, ppos);
4603}
4604#endif /* CONFIG_NUMA */
4605
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08004606int hugetlb_overcommit_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02004607 void *buffer, size_t *length, loff_t *ppos)
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08004608{
Andi Kleena5516432008-07-23 21:27:41 -07004609 struct hstate *h = &default_hstate;
Andi Kleene5ff2152008-07-23 21:27:42 -07004610 unsigned long tmp;
Michal Hocko08d4a242011-01-13 15:47:26 -08004611 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07004612
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004613 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08004614 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004615
Petr Holasekc033a932011-03-22 16:33:05 -07004616 tmp = h->nr_overcommit_huge_pages;
Andi Kleene5ff2152008-07-23 21:27:42 -07004617
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07004618 if (write && hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08004619 return -EINVAL;
4620
Muchun Song17743792020-09-04 16:36:13 -07004621 ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
4622 &tmp);
Michal Hocko08d4a242011-01-13 15:47:26 -08004623 if (ret)
4624 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07004625
4626 if (write) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07004627 spin_lock_irq(&hugetlb_lock);
Andi Kleene5ff2152008-07-23 21:27:42 -07004628 h->nr_overcommit_huge_pages = tmp;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07004629 spin_unlock_irq(&hugetlb_lock);
Andi Kleene5ff2152008-07-23 21:27:42 -07004630 }
Michal Hocko08d4a242011-01-13 15:47:26 -08004631out:
4632 return ret;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08004633}
4634
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635#endif /* CONFIG_SYSCTL */
4636
Alexey Dobriyane1759c22008-10-15 23:50:22 +04004637void hugetlb_report_meminfo(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08004639 struct hstate *h;
4640 unsigned long total = 0;
4641
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004642 if (!hugepages_supported())
4643 return;
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08004644
4645 for_each_hstate(h) {
4646 unsigned long count = h->nr_huge_pages;
4647
Miaohe Linaca78302021-02-24 12:07:46 -08004648 total += huge_page_size(h) * count;
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08004649
4650 if (h == &default_hstate)
4651 seq_printf(m,
4652 "HugePages_Total: %5lu\n"
4653 "HugePages_Free: %5lu\n"
4654 "HugePages_Rsvd: %5lu\n"
4655 "HugePages_Surp: %5lu\n"
4656 "Hugepagesize: %8lu kB\n",
4657 count,
4658 h->free_huge_pages,
4659 h->resv_huge_pages,
4660 h->surplus_huge_pages,
Miaohe Linaca78302021-02-24 12:07:46 -08004661 huge_page_size(h) / SZ_1K);
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08004662 }
4663
Miaohe Linaca78302021-02-24 12:07:46 -08004664 seq_printf(m, "Hugetlb: %8lu kB\n", total / SZ_1K);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
Joe Perches79815932020-09-16 13:40:43 -07004667int hugetlb_report_node_meminfo(char *buf, int len, int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668{
Andi Kleena5516432008-07-23 21:27:41 -07004669 struct hstate *h = &default_hstate;
Joe Perches79815932020-09-16 13:40:43 -07004670
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004671 if (!hugepages_supported())
4672 return 0;
Joe Perches79815932020-09-16 13:40:43 -07004673
4674 return sysfs_emit_at(buf, len,
4675 "Node %d HugePages_Total: %5u\n"
4676 "Node %d HugePages_Free: %5u\n"
4677 "Node %d HugePages_Surp: %5u\n",
4678 nid, h->nr_huge_pages_node[nid],
4679 nid, h->free_huge_pages_node[nid],
4680 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
Gang Lidcadcf12022-07-06 11:46:54 +08004683void hugetlb_show_meminfo_node(int nid)
David Rientjes949f7ec2013-04-29 15:07:48 -07004684{
4685 struct hstate *h;
David Rientjes949f7ec2013-04-29 15:07:48 -07004686
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004687 if (!hugepages_supported())
4688 return;
4689
Gang Lidcadcf12022-07-06 11:46:54 +08004690 for_each_hstate(h)
4691 printk("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
4692 nid,
4693 h->nr_huge_pages_node[nid],
4694 h->free_huge_pages_node[nid],
4695 h->surplus_huge_pages_node[nid],
4696 huge_page_size(h) / SZ_1K);
David Rientjes949f7ec2013-04-29 15:07:48 -07004697}
4698
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08004699void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
4700{
4701 seq_printf(m, "HugetlbPages:\t%8lu kB\n",
4702 atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10));
4703}
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
4706unsigned long hugetlb_total_pages(void)
4707{
Wanpeng Lid0028582013-03-22 15:04:40 -07004708 struct hstate *h;
4709 unsigned long nr_total_pages = 0;
4710
4711 for_each_hstate(h)
4712 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
4713 return nr_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Andi Kleena5516432008-07-23 21:27:41 -07004716static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004717{
4718 int ret = -ENOMEM;
4719
Miaohe Lin0aa7f352021-02-24 12:06:57 -08004720 if (!delta)
4721 return 0;
4722
Mike Kravetzdb71ef72021-05-04 18:35:07 -07004723 spin_lock_irq(&hugetlb_lock);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004724 /*
4725 * When cpuset is configured, it breaks the strict hugetlb page
4726 * reservation as the accounting is done on a global variable. Such
4727 * reservation is completely rubbish in the presence of cpuset because
4728 * the reservation is not checked against page availability for the
4729 * current cpuset. Application can still potentially OOM'ed by kernel
4730 * with lack of free htlb page in cpuset that the task is in.
4731 * Attempt to enforce strict accounting with cpuset is almost
4732 * impossible (or too ugly) because cpuset is too fluid that
4733 * task or memory node can be dynamically moved between cpusets.
4734 *
4735 * The change of semantics for shared hugetlb mapping with cpuset is
4736 * undesirable. However, in order to preserve some of the semantics,
4737 * we fall back to check against current free page availability as
4738 * a best attempt and hopefully to minimize the impact of changing
4739 * semantics that cpuset has.
Muchun Song8ca39e62020-08-11 18:30:32 -07004740 *
4741 * Apart from cpuset, we also have memory policy mechanism that
4742 * also determines from which node the kernel will allocate memory
4743 * in a NUMA system. So similar to cpuset, we also should consider
4744 * the memory policy of the current task. Similar to the description
4745 * above.
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004746 */
4747 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07004748 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004749 goto out;
4750
Muchun Song8ca39e62020-08-11 18:30:32 -07004751 if (delta > allowed_mems_nr(h)) {
Andi Kleena5516432008-07-23 21:27:41 -07004752 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004753 goto out;
4754 }
4755 }
4756
4757 ret = 0;
4758 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07004759 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004760
4761out:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07004762 spin_unlock_irq(&hugetlb_lock);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004763 return ret;
4764}
4765
Andy Whitcroft84afd992008-07-23 21:27:32 -07004766static void hugetlb_vm_op_open(struct vm_area_struct *vma)
4767{
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07004768 struct resv_map *resv = vma_resv_map(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07004769
4770 /*
Mike Kravetz612b8a32022-10-19 13:19:57 -07004771 * HPAGE_RESV_OWNER indicates a private mapping.
Andy Whitcroft84afd992008-07-23 21:27:32 -07004772 * This new VMA should share its siblings reservation map if present.
4773 * The VMA will only ever have a valid reservation map pointer where
4774 * it is being copied for another still existing VMA. As that VMA
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004775 * has a reference to the reservation map it cannot disappear until
Andy Whitcroft84afd992008-07-23 21:27:32 -07004776 * after this open call completes. It is therefore safe to take a
4777 * new reference here without additional locking.
4778 */
Mike Kravetz09a26e82021-09-02 14:58:53 -07004779 if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
4780 resv_map_dup_hugetlb_cgroup_uncharge_info(resv);
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07004781 kref_get(&resv->refs);
Mike Kravetz09a26e82021-09-02 14:58:53 -07004782 }
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07004783
Mike Kravetz131a79b2022-10-04 18:17:05 -07004784 /*
4785 * vma_lock structure for sharable mappings is vma specific.
Mike Kravetz612b8a32022-10-19 13:19:57 -07004786 * Clear old pointer (if copied via vm_area_dup) and allocate
4787 * new structure. Before clearing, make sure vma_lock is not
4788 * for this vma.
Mike Kravetz131a79b2022-10-04 18:17:05 -07004789 */
4790 if (vma->vm_flags & VM_MAYSHARE) {
Mike Kravetz612b8a32022-10-19 13:19:57 -07004791 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
4792
4793 if (vma_lock) {
4794 if (vma_lock->vma != vma) {
4795 vma->vm_private_data = NULL;
4796 hugetlb_vma_lock_alloc(vma);
4797 } else
4798 pr_warn("HugeTLB: vma_lock already exists in %s.\n", __func__);
4799 } else
4800 hugetlb_vma_lock_alloc(vma);
Mike Kravetz131a79b2022-10-04 18:17:05 -07004801 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07004802}
4803
Mel Gormana1e78772008-07-23 21:27:23 -07004804static void hugetlb_vm_op_close(struct vm_area_struct *vma)
4805{
Andi Kleena5516432008-07-23 21:27:41 -07004806 struct hstate *h = hstate_vma(vma);
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07004807 struct resv_map *resv;
David Gibson90481622012-03-21 16:34:12 -07004808 struct hugepage_subpool *spool = subpool_vma(vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004809 unsigned long reserve, start, end;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004810 long gbl_reserve;
Andy Whitcroft84afd992008-07-23 21:27:32 -07004811
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07004812 hugetlb_vma_lock_free(vma);
4813
4814 resv = vma_resv_map(vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004815 if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
4816 return;
Andy Whitcroft84afd992008-07-23 21:27:32 -07004817
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004818 start = vma_hugecache_offset(h, vma, vma->vm_start);
4819 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07004820
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004821 reserve = (end - start) - region_count(resv, start, end);
Mina Almasrye9fe92a2020-04-01 21:11:21 -07004822 hugetlb_cgroup_uncharge_counter(resv, start, end);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004823 if (reserve) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004824 /*
4825 * Decrement reserve counts. The global reserve count may be
4826 * adjusted if the subpool has a minimum size.
4827 */
4828 gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
4829 hugetlb_acct_memory(h, -gbl_reserve);
Andy Whitcroft84afd992008-07-23 21:27:32 -07004830 }
Mina Almasrye9fe92a2020-04-01 21:11:21 -07004831
4832 kref_put(&resv->refs, resv_map_release);
Mel Gormana1e78772008-07-23 21:27:23 -07004833}
4834
Dan Williams31383c62017-11-29 16:10:28 -08004835static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
4836{
4837 if (addr & ~(huge_page_mask(hstate_vma(vma))))
4838 return -EINVAL;
James Houghtonb30c14c2023-01-04 23:19:10 +00004839
4840 /*
4841 * PMD sharing is only possible for PUD_SIZE-aligned address ranges
4842 * in HugeTLB VMAs. If we will lose PUD_SIZE alignment due to this
4843 * split, unshare PMDs in the PUD_SIZE interval surrounding addr now.
4844 */
4845 if (addr & ~PUD_MASK) {
4846 /*
4847 * hugetlb_vm_op_split is called right before we attempt to
4848 * split the VMA. We will need to unshare PMDs in the old and
4849 * new VMAs, so let's unshare before we split.
4850 */
4851 unsigned long floor = addr & PUD_MASK;
4852 unsigned long ceil = floor + PUD_SIZE;
4853
4854 if (floor >= vma->vm_start && ceil <= vma->vm_end)
4855 hugetlb_unshare_pmds(vma, floor, ceil);
4856 }
4857
Dan Williams31383c62017-11-29 16:10:28 -08004858 return 0;
4859}
4860
Dan Williams05ea8862018-04-05 16:24:25 -07004861static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
4862{
Miaohe Linaca78302021-02-24 12:07:46 -08004863 return huge_page_size(hstate_vma(vma));
Dan Williams05ea8862018-04-05 16:24:25 -07004864}
4865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866/*
4867 * We cannot handle pagefaults against hugetlb pages at all. They cause
4868 * handle_mm_fault() to try to instantiate regular-sized pages in the
Miaohe Lin6c26d312021-02-24 12:07:19 -08004869 * hugepage VMA. do_page_fault() is supposed to trap this, so BUG is we get
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 * this far.
4871 */
Souptick Joarderb3ec9f32018-06-07 17:08:04 -07004872static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
4874 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07004875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
Jane Chueec36362018-08-02 15:36:05 -07004878/*
4879 * When a new function is introduced to vm_operations_struct and added
4880 * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
4881 * This is because under System V memory model, mappings created via
4882 * shmget/shmat with "huge page" specified are backed by hugetlbfs files,
4883 * their original vm_ops are overwritten with shm_vm_ops.
4884 */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004885const struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07004886 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07004887 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07004888 .close = hugetlb_vm_op_close,
Dmitry Safonovdd3b6142020-12-14 19:08:17 -08004889 .may_split = hugetlb_vm_op_split,
Dan Williams05ea8862018-04-05 16:24:25 -07004890 .pagesize = hugetlb_vm_op_pagesize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891};
4892
David Gibson1e8f8892006-01-06 00:10:44 -08004893static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
4894 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07004895{
4896 pte_t entry;
Christophe Leroy79c1c592021-06-30 18:48:00 -07004897 unsigned int shift = huge_page_shift(hstate_vma(vma));
David Gibson63551ae2005-06-21 17:14:44 -07004898
David Gibson1e8f8892006-01-06 00:10:44 -08004899 if (writable) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07004900 entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
4901 vma->vm_page_prot)));
David Gibson63551ae2005-06-21 17:14:44 -07004902 } else {
Gerald Schaefer106c9922013-04-29 15:07:23 -07004903 entry = huge_pte_wrprotect(mk_huge_pte(page,
4904 vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07004905 }
4906 entry = pte_mkyoung(entry);
Christophe Leroy79c1c592021-06-30 18:48:00 -07004907 entry = arch_make_huge_pte(entry, shift, vma->vm_flags);
David Gibson63551ae2005-06-21 17:14:44 -07004908
4909 return entry;
4910}
4911
David Gibson1e8f8892006-01-06 00:10:44 -08004912static void set_huge_ptep_writable(struct vm_area_struct *vma,
4913 unsigned long address, pte_t *ptep)
4914{
4915 pte_t entry;
4916
Gerald Schaefer106c9922013-04-29 15:07:23 -07004917 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
Chris Forbes32f84522011-07-25 17:12:14 -07004918 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
Russell King4b3073e2009-12-18 16:40:18 +00004919 update_mmu_cache(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08004920}
4921
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07004922bool is_hugetlb_entry_migration(pte_t pte)
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004923{
4924 swp_entry_t swp;
4925
4926 if (huge_pte_none(pte) || pte_present(pte))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07004927 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004928 swp = pte_to_swp_entry(pte);
Baoquan Hed79d1762020-10-13 16:56:14 -07004929 if (is_migration_entry(swp))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07004930 return true;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004931 else
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07004932 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004933}
4934
Baoquan He3e5c3602020-10-13 16:56:10 -07004935static bool is_hugetlb_entry_hwpoisoned(pte_t pte)
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004936{
4937 swp_entry_t swp;
4938
4939 if (huge_pte_none(pte) || pte_present(pte))
Baoquan He3e5c3602020-10-13 16:56:10 -07004940 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004941 swp = pte_to_swp_entry(pte);
Baoquan Hed79d1762020-10-13 16:56:14 -07004942 if (is_hwpoison_entry(swp))
Baoquan He3e5c3602020-10-13 16:56:10 -07004943 return true;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004944 else
Baoquan He3e5c3602020-10-13 16:56:10 -07004945 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004946}
David Gibson1e8f8892006-01-06 00:10:44 -08004947
Peter Xu4eae4ef2021-03-12 21:07:33 -08004948static void
4949hugetlb_install_page(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr,
4950 struct page *new_page)
4951{
4952 __SetPageUptodate(new_page);
Peter Xu4eae4ef2021-03-12 21:07:33 -08004953 hugepage_add_new_anon_rmap(new_page, vma, addr);
Pasha Tatashin1eba86c2022-01-14 14:06:29 -08004954 set_huge_pte_at(vma->vm_mm, addr, ptep, make_huge_pte(vma, new_page, 1));
Peter Xu4eae4ef2021-03-12 21:07:33 -08004955 hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm);
Peter Xu4eae4ef2021-03-12 21:07:33 -08004956 SetHPageMigratable(new_page);
4957}
4958
David Gibson63551ae2005-06-21 17:14:44 -07004959int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
Peter Xubc70fbf2022-05-12 20:22:55 -07004960 struct vm_area_struct *dst_vma,
4961 struct vm_area_struct *src_vma)
David Gibson63551ae2005-06-21 17:14:44 -07004962{
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08004963 pte_t *src_pte, *dst_pte, entry;
David Gibson63551ae2005-06-21 17:14:44 -07004964 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07004965 unsigned long addr;
Peter Xubc70fbf2022-05-12 20:22:55 -07004966 bool cow = is_cow_mapping(src_vma->vm_flags);
4967 struct hstate *h = hstate_vma(src_vma);
Andi Kleena5516432008-07-23 21:27:41 -07004968 unsigned long sz = huge_page_size(h);
Peter Xu4eae4ef2021-03-12 21:07:33 -08004969 unsigned long npages = pages_per_huge_page(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004970 struct mmu_notifier_range range;
Mike Kravetze95a9852022-06-21 16:56:17 -07004971 unsigned long last_addr_mask;
Andreas Sandberge8569dd2014-01-21 15:49:09 -08004972 int ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08004973
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004974 if (cow) {
Peter Xubc70fbf2022-05-12 20:22:55 -07004975 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, src_vma, src,
4976 src_vma->vm_start,
4977 src_vma->vm_end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004978 mmu_notifier_invalidate_range_start(&range);
David Hildenbrand623a1dd2022-05-09 18:20:42 -07004979 mmap_assert_write_locked(src);
4980 raw_write_seqcount_begin(&src->write_protect_seq);
Mike Kravetz40549ba2022-09-14 15:18:09 -07004981 } else {
4982 /*
4983 * For shared mappings the vma lock must be held before
4984 * calling huge_pte_offset in the src vma. Otherwise, the
4985 * returned ptep could go away if part of a shared pmd and
4986 * another thread calls huge_pmd_unshare.
4987 */
4988 hugetlb_vma_lock_read(src_vma);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004989 }
Andreas Sandberge8569dd2014-01-21 15:49:09 -08004990
Mike Kravetze95a9852022-06-21 16:56:17 -07004991 last_addr_mask = hugetlb_mask_last_page(h);
Peter Xubc70fbf2022-05-12 20:22:55 -07004992 for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004993 spinlock_t *src_ptl, *dst_ptl;
Punit Agrawal7868a202017-07-06 15:39:42 -07004994 src_pte = huge_pte_offset(src, addr, sz);
Mike Kravetze95a9852022-06-21 16:56:17 -07004995 if (!src_pte) {
4996 addr |= last_addr_mask;
Hugh Dickinsc74df322005-10-29 18:16:23 -07004997 continue;
Mike Kravetze95a9852022-06-21 16:56:17 -07004998 }
Peter Xubc70fbf2022-05-12 20:22:55 -07004999 dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08005000 if (!dst_pte) {
5001 ret = -ENOMEM;
5002 break;
5003 }
Larry Woodmanc5c99422008-01-24 05:49:25 -08005004
Mike Kravetz5e415402018-11-16 15:08:04 -08005005 /*
5006 * If the pagetables are shared don't copy or take references.
Mike Kravetz5e415402018-11-16 15:08:04 -08005007 *
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005008 * dst_pte == src_pte is the common case of src/dest sharing.
Mike Kravetz5e415402018-11-16 15:08:04 -08005009 * However, src could have 'unshared' and dst shares with
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005010 * another vma. So page_count of ptep page is checked instead
5011 * to reliably determine whether pte is shared.
Mike Kravetz5e415402018-11-16 15:08:04 -08005012 */
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005013 if (page_count(virt_to_page(dst_pte)) > 1) {
Mike Kravetze95a9852022-06-21 16:56:17 -07005014 addr |= last_addr_mask;
Larry Woodmanc5c99422008-01-24 05:49:25 -08005015 continue;
Mike Kravetze95a9852022-06-21 16:56:17 -07005016 }
Larry Woodmanc5c99422008-01-24 05:49:25 -08005017
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005018 dst_ptl = huge_pte_lock(h, dst, dst_pte);
5019 src_ptl = huge_pte_lockptr(h, src, src_pte);
5020 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005021 entry = huge_ptep_get(src_pte);
Peter Xu4eae4ef2021-03-12 21:07:33 -08005022again:
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005023 if (huge_pte_none(entry)) {
Mike Kravetz5e415402018-11-16 15:08:04 -08005024 /*
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005025 * Skip if src entry none.
Mike Kravetz5e415402018-11-16 15:08:04 -08005026 */
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005027 ;
Naoya Horiguchic2cb0dc2022-07-04 10:33:05 +09005028 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) {
5029 bool uffd_wp = huge_pte_uffd_wp(entry);
5030
5031 if (!userfaultfd_wp(dst_vma) && uffd_wp)
5032 entry = huge_pte_clear_uffd_wp(entry);
5033 set_huge_pte_at(dst, addr, dst_pte, entry);
5034 } else if (unlikely(is_hugetlb_entry_migration(entry))) {
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005035 swp_entry_t swp_entry = pte_to_swp_entry(entry);
Peter Xubc70fbf2022-05-12 20:22:55 -07005036 bool uffd_wp = huge_pte_uffd_wp(entry);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005037
David Hildenbrand6c287602022-05-09 18:20:44 -07005038 if (!is_readable_migration_entry(swp_entry) && cow) {
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005039 /*
5040 * COW mappings require pages in both
5041 * parent and child to be set to read.
5042 */
Alistair Popple4dd845b2021-06-30 18:54:09 -07005043 swp_entry = make_readable_migration_entry(
5044 swp_offset(swp_entry));
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005045 entry = swp_entry_to_pte(swp_entry);
Peter Xubc70fbf2022-05-12 20:22:55 -07005046 if (userfaultfd_wp(src_vma) && uffd_wp)
5047 entry = huge_pte_mkuffd_wp(entry);
Qi Zheng18f39622022-06-26 22:57:17 +08005048 set_huge_pte_at(src, addr, src_pte, entry);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005049 }
Peter Xubc70fbf2022-05-12 20:22:55 -07005050 if (!userfaultfd_wp(dst_vma) && uffd_wp)
5051 entry = huge_pte_clear_uffd_wp(entry);
Qi Zheng18f39622022-06-26 22:57:17 +08005052 set_huge_pte_at(dst, addr, dst_pte, entry);
Peter Xubc70fbf2022-05-12 20:22:55 -07005053 } else if (unlikely(is_pte_marker(entry))) {
5054 /*
5055 * We copy the pte marker only if the dst vma has
5056 * uffd-wp enabled.
5057 */
5058 if (userfaultfd_wp(dst_vma))
5059 set_huge_pte_at(dst, addr, dst_pte, entry);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005060 } else {
Peter Xu4eae4ef2021-03-12 21:07:33 -08005061 entry = huge_ptep_get(src_pte);
5062 ptepage = pte_page(entry);
5063 get_page(ptepage);
5064
5065 /*
David Hildenbrandfb3d8242022-05-09 18:20:43 -07005066 * Failing to duplicate the anon rmap is a rare case
5067 * where we see pinned hugetlb pages while they're
5068 * prone to COW. We need to do the COW earlier during
5069 * fork.
Peter Xu4eae4ef2021-03-12 21:07:33 -08005070 *
5071 * When pre-allocating the page or copying data, we
5072 * need to be without the pgtable locks since we could
5073 * sleep during the process.
5074 */
David Hildenbrandfb3d8242022-05-09 18:20:43 -07005075 if (!PageAnon(ptepage)) {
5076 page_dup_file_rmap(ptepage, true);
Peter Xubc70fbf2022-05-12 20:22:55 -07005077 } else if (page_try_dup_anon_rmap(ptepage, true,
5078 src_vma)) {
Peter Xu4eae4ef2021-03-12 21:07:33 -08005079 pte_t src_pte_old = entry;
5080 struct page *new;
5081
5082 spin_unlock(src_ptl);
5083 spin_unlock(dst_ptl);
5084 /* Do not use reserve as it's private owned */
Peter Xubc70fbf2022-05-12 20:22:55 -07005085 new = alloc_huge_page(dst_vma, addr, 1);
Peter Xu4eae4ef2021-03-12 21:07:33 -08005086 if (IS_ERR(new)) {
5087 put_page(ptepage);
5088 ret = PTR_ERR(new);
5089 break;
5090 }
Peter Xubc70fbf2022-05-12 20:22:55 -07005091 copy_user_huge_page(new, ptepage, addr, dst_vma,
Peter Xu4eae4ef2021-03-12 21:07:33 -08005092 npages);
5093 put_page(ptepage);
5094
5095 /* Install the new huge page if src pte stable */
5096 dst_ptl = huge_pte_lock(h, dst, dst_pte);
5097 src_ptl = huge_pte_lockptr(h, src, src_pte);
5098 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5099 entry = huge_ptep_get(src_pte);
5100 if (!pte_same(src_pte_old, entry)) {
Peter Xubc70fbf2022-05-12 20:22:55 -07005101 restore_reserve_on_error(h, dst_vma, addr,
Mike Kravetz846be082021-06-15 18:23:29 -07005102 new);
Peter Xu4eae4ef2021-03-12 21:07:33 -08005103 put_page(new);
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005104 /* huge_ptep of dst_pte won't change as in child */
Peter Xu4eae4ef2021-03-12 21:07:33 -08005105 goto again;
5106 }
Peter Xubc70fbf2022-05-12 20:22:55 -07005107 hugetlb_install_page(dst_vma, dst_pte, addr, new);
Peter Xu4eae4ef2021-03-12 21:07:33 -08005108 spin_unlock(src_ptl);
5109 spin_unlock(dst_ptl);
5110 continue;
5111 }
5112
Joerg Roedel34ee6452014-11-13 13:46:09 +11005113 if (cow) {
Jérôme Glisse0f108512017-11-15 17:34:07 -08005114 /*
5115 * No need to notify as we are downgrading page
5116 * table protection not changing it to point
5117 * to a new page.
5118 *
Mike Rapoportee657282022-06-27 09:00:26 +03005119 * See Documentation/mm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08005120 */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07005121 huge_ptep_set_wrprotect(src, addr, src_pte);
Peter Xu84894e12021-05-14 17:27:07 -07005122 entry = huge_pte_wrprotect(entry);
Joerg Roedel34ee6452014-11-13 13:46:09 +11005123 }
Peter Xu4eae4ef2021-03-12 21:07:33 -08005124
Hugh Dickins1c598272005-10-19 21:23:43 -07005125 set_huge_pte_at(dst, addr, dst_pte, entry);
Peter Xu4eae4ef2021-03-12 21:07:33 -08005126 hugetlb_count_add(npages, dst);
Hugh Dickins1c598272005-10-19 21:23:43 -07005127 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005128 spin_unlock(src_ptl);
5129 spin_unlock(dst_ptl);
David Gibson63551ae2005-06-21 17:14:44 -07005130 }
David Gibson63551ae2005-06-21 17:14:44 -07005131
David Hildenbrand623a1dd2022-05-09 18:20:42 -07005132 if (cow) {
5133 raw_write_seqcount_end(&src->write_protect_seq);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005134 mmu_notifier_invalidate_range_end(&range);
Mike Kravetz40549ba2022-09-14 15:18:09 -07005135 } else {
5136 hugetlb_vma_unlock_read(src_vma);
David Hildenbrand623a1dd2022-05-09 18:20:42 -07005137 }
Andreas Sandberge8569dd2014-01-21 15:49:09 -08005138
5139 return ret;
David Gibson63551ae2005-06-21 17:14:44 -07005140}
5141
Mina Almasry550a7d62021-11-05 13:41:40 -07005142static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
Aneesh Kumar K.Vdb110a92022-02-25 19:10:56 -08005143 unsigned long new_addr, pte_t *src_pte, pte_t *dst_pte)
Mina Almasry550a7d62021-11-05 13:41:40 -07005144{
5145 struct hstate *h = hstate_vma(vma);
5146 struct mm_struct *mm = vma->vm_mm;
Mina Almasry550a7d62021-11-05 13:41:40 -07005147 spinlock_t *src_ptl, *dst_ptl;
Aneesh Kumar K.Vdb110a92022-02-25 19:10:56 -08005148 pte_t pte;
Mina Almasry550a7d62021-11-05 13:41:40 -07005149
Mina Almasry550a7d62021-11-05 13:41:40 -07005150 dst_ptl = huge_pte_lock(h, mm, dst_pte);
5151 src_ptl = huge_pte_lockptr(h, mm, src_pte);
5152
5153 /*
5154 * We don't have to worry about the ordering of src and dst ptlocks
Lorenzo Stoakes8651a132023-01-07 00:00:05 +00005155 * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock.
Mina Almasry550a7d62021-11-05 13:41:40 -07005156 */
5157 if (src_ptl != dst_ptl)
5158 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5159
5160 pte = huge_ptep_get_and_clear(mm, old_addr, src_pte);
5161 set_huge_pte_at(mm, new_addr, dst_pte, pte);
5162
5163 if (src_ptl != dst_ptl)
5164 spin_unlock(src_ptl);
5165 spin_unlock(dst_ptl);
5166}
5167
5168int move_hugetlb_page_tables(struct vm_area_struct *vma,
5169 struct vm_area_struct *new_vma,
5170 unsigned long old_addr, unsigned long new_addr,
5171 unsigned long len)
5172{
5173 struct hstate *h = hstate_vma(vma);
5174 struct address_space *mapping = vma->vm_file->f_mapping;
5175 unsigned long sz = huge_page_size(h);
5176 struct mm_struct *mm = vma->vm_mm;
5177 unsigned long old_end = old_addr + len;
Mike Kravetze95a9852022-06-21 16:56:17 -07005178 unsigned long last_addr_mask;
Mina Almasry550a7d62021-11-05 13:41:40 -07005179 pte_t *src_pte, *dst_pte;
5180 struct mmu_notifier_range range;
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005181 bool shared_pmd = false;
Mina Almasry550a7d62021-11-05 13:41:40 -07005182
5183 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, old_addr,
5184 old_end);
5185 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005186 /*
5187 * In case of shared PMDs, we should cover the maximum possible
5188 * range.
5189 */
5190 flush_cache_range(vma, range.start, range.end);
5191
Mina Almasry550a7d62021-11-05 13:41:40 -07005192 mmu_notifier_invalidate_range_start(&range);
Mike Kravetze95a9852022-06-21 16:56:17 -07005193 last_addr_mask = hugetlb_mask_last_page(h);
Mina Almasry550a7d62021-11-05 13:41:40 -07005194 /* Prevent race with file truncation */
Mike Kravetz40549ba2022-09-14 15:18:09 -07005195 hugetlb_vma_lock_write(vma);
Mina Almasry550a7d62021-11-05 13:41:40 -07005196 i_mmap_lock_write(mapping);
5197 for (; old_addr < old_end; old_addr += sz, new_addr += sz) {
5198 src_pte = huge_pte_offset(mm, old_addr, sz);
Mike Kravetze95a9852022-06-21 16:56:17 -07005199 if (!src_pte) {
5200 old_addr |= last_addr_mask;
5201 new_addr |= last_addr_mask;
Mina Almasry550a7d62021-11-05 13:41:40 -07005202 continue;
Mike Kravetze95a9852022-06-21 16:56:17 -07005203 }
Mina Almasry550a7d62021-11-05 13:41:40 -07005204 if (huge_pte_none(huge_ptep_get(src_pte)))
5205 continue;
5206
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07005207 if (huge_pmd_unshare(mm, vma, old_addr, src_pte)) {
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005208 shared_pmd = true;
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07005209 old_addr |= last_addr_mask;
5210 new_addr |= last_addr_mask;
Mina Almasry550a7d62021-11-05 13:41:40 -07005211 continue;
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005212 }
Mina Almasry550a7d62021-11-05 13:41:40 -07005213
5214 dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz);
5215 if (!dst_pte)
5216 break;
5217
Aneesh Kumar K.Vdb110a92022-02-25 19:10:56 -08005218 move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte);
Mina Almasry550a7d62021-11-05 13:41:40 -07005219 }
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005220
5221 if (shared_pmd)
5222 flush_tlb_range(vma, range.start, range.end);
5223 else
5224 flush_tlb_range(vma, old_end - len, old_end);
Mina Almasry550a7d62021-11-05 13:41:40 -07005225 mmu_notifier_invalidate_range_end(&range);
Nadav Amit13e4ad22021-11-21 12:40:08 -08005226 i_mmap_unlock_write(mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07005227 hugetlb_vma_unlock_write(vma);
Mina Almasry550a7d62021-11-05 13:41:40 -07005228
5229 return len + old_addr - old_end;
5230}
5231
Peter Xu73c54762021-11-05 13:41:17 -07005232static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
5233 unsigned long start, unsigned long end,
Peter Xu05e90bd2022-05-12 20:22:55 -07005234 struct page *ref_page, zap_flags_t zap_flags)
David Gibson63551ae2005-06-21 17:14:44 -07005235{
5236 struct mm_struct *mm = vma->vm_mm;
5237 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07005238 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07005239 pte_t pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005240 spinlock_t *ptl;
David Gibson63551ae2005-06-21 17:14:44 -07005241 struct page *page;
Andi Kleena5516432008-07-23 21:27:41 -07005242 struct hstate *h = hstate_vma(vma);
5243 unsigned long sz = huge_page_size(h);
Mike Kravetze95a9852022-06-21 16:56:17 -07005244 unsigned long last_addr_mask;
Nadav Amita4a118f2021-11-21 12:40:07 -08005245 bool force_flush = false;
Andi Kleena5516432008-07-23 21:27:41 -07005246
David Gibson63551ae2005-06-21 17:14:44 -07005247 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07005248 BUG_ON(start & ~huge_page_mask(h));
5249 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07005250
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08005251 /*
5252 * This is a hugetlb vma, all the pte entries should point
5253 * to huge page.
5254 */
Peter Zijlstraed6a7932018-08-31 14:46:08 +02005255 tlb_change_page_size(tlb, sz);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07005256 tlb_start_vma(tlb, vma);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07005257
Mike Kravetze95a9852022-06-21 16:56:17 -07005258 last_addr_mask = hugetlb_mask_last_page(h);
Hillf Danton569f48b82014-12-10 15:44:41 -08005259 address = start;
Hillf Danton569f48b82014-12-10 15:44:41 -08005260 for (; address < end; address += sz) {
Punit Agrawal7868a202017-07-06 15:39:42 -07005261 ptep = huge_pte_offset(mm, address, sz);
Mike Kravetze95a9852022-06-21 16:56:17 -07005262 if (!ptep) {
5263 address |= last_addr_mask;
David Gibsonc7546f82005-08-05 11:59:35 -07005264 continue;
Mike Kravetze95a9852022-06-21 16:56:17 -07005265 }
David Gibsonc7546f82005-08-05 11:59:35 -07005266
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005267 ptl = huge_pte_lock(h, mm, ptep);
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07005268 if (huge_pmd_unshare(mm, vma, address, ptep)) {
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005269 spin_unlock(ptl);
Nadav Amita4a118f2021-11-21 12:40:07 -08005270 tlb_flush_pmd_range(tlb, address & PUD_MASK, PUD_SIZE);
5271 force_flush = true;
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07005272 address |= last_addr_mask;
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005273 continue;
5274 }
Chen, Kenneth W39dde652006-12-06 20:32:03 -08005275
Hillf Danton66293262012-03-23 15:01:48 -07005276 pte = huge_ptep_get(ptep);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005277 if (huge_pte_none(pte)) {
5278 spin_unlock(ptl);
5279 continue;
5280 }
Hillf Danton66293262012-03-23 15:01:48 -07005281
5282 /*
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08005283 * Migrating hugepage or HWPoisoned hugepage is already
5284 * unmapped and its refcount is dropped, so just clear pte here.
Hillf Danton66293262012-03-23 15:01:48 -07005285 */
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08005286 if (unlikely(!pte_present(pte))) {
Peter Xu05e90bd2022-05-12 20:22:55 -07005287 /*
5288 * If the pte was wr-protected by uffd-wp in any of the
5289 * swap forms, meanwhile the caller does not want to
5290 * drop the uffd-wp bit in this zap, then replace the
5291 * pte with a marker.
5292 */
5293 if (pte_swp_uffd_wp_any(pte) &&
5294 !(zap_flags & ZAP_FLAG_DROP_MARKER))
5295 set_huge_pte_at(mm, address, ptep,
5296 make_pte_marker(PTE_MARKER_UFFD_WP));
5297 else
5298 huge_pte_clear(mm, address, ptep, sz);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005299 spin_unlock(ptl);
5300 continue;
Naoya Horiguchi8c4894c2012-12-12 13:52:28 -08005301 }
Hillf Danton66293262012-03-23 15:01:48 -07005302
5303 page = pte_page(pte);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005304 /*
5305 * If a reference page is supplied, it is because a specific
5306 * page is being unmapped, not a range. Ensure the page we
5307 * are about to unmap is the actual page of interest.
5308 */
5309 if (ref_page) {
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005310 if (page != ref_page) {
5311 spin_unlock(ptl);
5312 continue;
5313 }
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005314 /*
5315 * Mark the VMA as having unmapped its page so that
5316 * future faults in this VMA will fail rather than
5317 * looking like data was lost
5318 */
5319 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
5320 }
5321
David Gibsonc7546f82005-08-05 11:59:35 -07005322 pte = huge_ptep_get_and_clear(mm, address, ptep);
Aneesh Kumar K.Vb528e4b2016-12-12 16:42:37 -08005323 tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
Gerald Schaefer106c9922013-04-29 15:07:23 -07005324 if (huge_pte_dirty(pte))
Ken Chen6649a382007-02-08 14:20:27 -08005325 set_page_dirty(page);
Peter Xu05e90bd2022-05-12 20:22:55 -07005326 /* Leave a uffd-wp pte marker if needed */
5327 if (huge_pte_uffd_wp(pte) &&
5328 !(zap_flags & ZAP_FLAG_DROP_MARKER))
5329 set_huge_pte_at(mm, address, ptep,
5330 make_pte_marker(PTE_MARKER_UFFD_WP));
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08005331 hugetlb_count_sub(pages_per_huge_page(h), mm);
Hugh Dickinscea86fe2022-02-14 18:26:39 -08005332 page_remove_rmap(page, vma, true);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005333
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005334 spin_unlock(ptl);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -07005335 tlb_remove_page_size(tlb, page, huge_page_size(h));
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005336 /*
5337 * Bail out after unmapping reference page if supplied
5338 */
5339 if (ref_page)
5340 break;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07005341 }
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07005342 tlb_end_vma(tlb, vma);
Nadav Amita4a118f2021-11-21 12:40:07 -08005343
5344 /*
5345 * If we unshared PMDs, the TLB flush was not recorded in mmu_gather. We
5346 * could defer the flush until now, since by holding i_mmap_rwsem we
5347 * guaranteed that the last refernece would not be dropped. But we must
5348 * do the flushing before we return, as otherwise i_mmap_rwsem will be
5349 * dropped and the last reference to the shared PMDs page might be
5350 * dropped as well.
5351 *
5352 * In theory we could defer the freeing of the PMD pages as well, but
5353 * huge_pmd_unshare() relies on the exact page_count for the PMD page to
5354 * detect sharing, so we cannot defer the release of the page either.
5355 * Instead, do flush now.
5356 */
5357 if (force_flush)
5358 tlb_flush_mmu_tlbonly(tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359}
David Gibson63551ae2005-06-21 17:14:44 -07005360
Mel Gormand8333522012-07-31 16:46:20 -07005361void __unmap_hugepage_range_final(struct mmu_gather *tlb,
5362 struct vm_area_struct *vma, unsigned long start,
Peter Xu05e90bd2022-05-12 20:22:55 -07005363 unsigned long end, struct page *ref_page,
5364 zap_flags_t zap_flags)
Mel Gormand8333522012-07-31 16:46:20 -07005365{
Mike Kravetz131a79b2022-10-04 18:17:05 -07005366 hugetlb_vma_lock_write(vma);
5367 i_mmap_lock_write(vma->vm_file->f_mapping);
5368
Mike Kravetz369258c2022-11-14 15:55:07 -08005369 /* mmu notification performed in caller */
Peter Xu05e90bd2022-05-12 20:22:55 -07005370 __unmap_hugepage_range(tlb, vma, start, end, ref_page, zap_flags);
Mel Gormand8333522012-07-31 16:46:20 -07005371
Mike Kravetz04ada092022-11-14 15:55:06 -08005372 if (zap_flags & ZAP_FLAG_UNMAP) { /* final unmap */
5373 /*
5374 * Unlock and free the vma lock before releasing i_mmap_rwsem.
5375 * When the vma_lock is freed, this makes the vma ineligible
5376 * for pmd sharing. And, i_mmap_rwsem is required to set up
5377 * pmd sharing. This is important as page tables for this
5378 * unmapped range will be asynchrously deleted. If the page
5379 * tables are shared, there will be issues when accessed by
5380 * someone else.
5381 */
5382 __hugetlb_vma_unlock_write_free(vma);
5383 i_mmap_unlock_write(vma->vm_file->f_mapping);
5384 } else {
5385 i_mmap_unlock_write(vma->vm_file->f_mapping);
5386 hugetlb_vma_unlock_write(vma);
5387 }
Mel Gormand8333522012-07-31 16:46:20 -07005388}
5389
Chen, Kenneth W502717f2006-10-11 01:20:46 -07005390void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Peter Xu05e90bd2022-05-12 20:22:55 -07005391 unsigned long end, struct page *ref_page,
5392 zap_flags_t zap_flags)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07005393{
Mike Kravetz369258c2022-11-14 15:55:07 -08005394 struct mmu_notifier_range range;
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07005395 struct mmu_gather tlb;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07005396
Mike Kravetz369258c2022-11-14 15:55:07 -08005397 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
5398 start, end);
5399 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
5400 mmu_notifier_invalidate_range_start(&range);
Will Deacona72afd82021-01-27 23:53:45 +00005401 tlb_gather_mmu(&tlb, vma->vm_mm);
Mike Kravetz369258c2022-11-14 15:55:07 -08005402
Peter Xu05e90bd2022-05-12 20:22:55 -07005403 __unmap_hugepage_range(&tlb, vma, start, end, ref_page, zap_flags);
Mike Kravetz369258c2022-11-14 15:55:07 -08005404
5405 mmu_notifier_invalidate_range_end(&range);
Will Deaconae8eba82021-01-27 23:53:43 +00005406 tlb_finish_mmu(&tlb);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07005407}
5408
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005409/*
5410 * This is called when the original mapper is failing to COW a MAP_PRIVATE
Zhiyuan Dai578b7722021-02-24 12:07:26 -08005411 * mapping it owns the reserve page for. The intention is to unmap the page
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005412 * from other VMAs and let the children be SIGKILLed if they are faulting the
5413 * same region.
5414 */
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07005415static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
5416 struct page *page, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005417{
Adam Litke75266742008-11-12 13:24:56 -08005418 struct hstate *h = hstate_vma(vma);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005419 struct vm_area_struct *iter_vma;
5420 struct address_space *mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005421 pgoff_t pgoff;
5422
5423 /*
5424 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
5425 * from page cache lookup which is in HPAGE_SIZE units.
5426 */
Adam Litke75266742008-11-12 13:24:56 -08005427 address = address & huge_page_mask(h);
Michal Hocko36e4f202012-10-08 16:33:31 -07005428 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
5429 vma->vm_pgoff;
Al Viro93c76a32015-12-04 23:45:44 -05005430 mapping = vma->vm_file->f_mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005431
Mel Gorman4eb2b1d2009-12-14 17:59:53 -08005432 /*
5433 * Take the mapping lock for the duration of the table walk. As
5434 * this mapping should be shared between all the VMAs,
5435 * __unmap_hugepage_range() is called as the lock is already held
5436 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08005437 i_mmap_lock_write(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07005438 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005439 /* Do not unmap the current VMA */
5440 if (iter_vma == vma)
5441 continue;
5442
5443 /*
Mel Gorman2f84a892015-10-01 15:36:57 -07005444 * Shared VMAs have their own reserves and do not affect
5445 * MAP_PRIVATE accounting but it is possible that a shared
5446 * VMA is using the same page so check and skip such VMAs.
5447 */
5448 if (iter_vma->vm_flags & VM_MAYSHARE)
5449 continue;
5450
5451 /*
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005452 * Unmap the page from other VMAs without their own reserves.
5453 * They get marked to be SIGKILLed if they fault in these
5454 * areas. This is because a future no-page fault on this VMA
5455 * could insert a zeroed page instead of the data existing
5456 * from the time of fork. This would look like data corruption
5457 */
5458 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07005459 unmap_hugepage_range(iter_vma, address,
Peter Xu05e90bd2022-05-12 20:22:55 -07005460 address + huge_page_size(h), page, 0);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005461 }
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08005462 i_mmap_unlock_write(mapping);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005463}
5464
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005465/*
David Hildenbrandc89357e2022-05-09 18:20:45 -07005466 * hugetlb_wp() should be called with page lock of the original hugepage held.
Baolin Wangaa6d2e82021-11-05 13:41:55 -07005467 * Called with hugetlb_fault_mutex_table held and pte_page locked so we
Michal Hockoef009b22012-01-10 15:07:21 -08005468 * cannot race with other handlers or page migration.
5469 * Keep the pte_same checks anyway to make transition from the mutex easier.
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005470 */
David Hildenbrandc89357e2022-05-09 18:20:45 -07005471static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
5472 unsigned long address, pte_t *ptep, unsigned int flags,
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08005473 struct page *pagecache_page, spinlock_t *ptl)
David Gibson1e8f8892006-01-06 00:10:44 -08005474{
David Hildenbrandc89357e2022-05-09 18:20:45 -07005475 const bool unshare = flags & FAULT_FLAG_UNSHARE;
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08005476 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07005477 struct hstate *h = hstate_vma(vma);
David Gibson1e8f8892006-01-06 00:10:44 -08005478 struct page *old_page, *new_page;
Souptick Joarder2b740302018-08-23 17:01:36 -07005479 int outside_reserve = 0;
5480 vm_fault_t ret = 0;
Huang Ying974e6d62018-08-17 15:45:57 -07005481 unsigned long haddr = address & huge_page_mask(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005482 struct mmu_notifier_range range;
David Gibson1e8f8892006-01-06 00:10:44 -08005483
David Hildenbrand1d8d14642022-08-11 12:34:35 +02005484 /*
5485 * hugetlb does not support FOLL_FORCE-style write faults that keep the
5486 * PTE mapped R/O such as maybe_mkwrite() would do.
5487 */
5488 if (WARN_ON_ONCE(!unshare && !(vma->vm_flags & VM_WRITE)))
5489 return VM_FAULT_SIGSEGV;
5490
5491 /* Let's take out MAP_SHARED mappings first. */
5492 if (vma->vm_flags & VM_MAYSHARE) {
David Hildenbrand1d8d14642022-08-11 12:34:35 +02005493 set_huge_ptep_writable(vma, haddr, ptep);
5494 return 0;
5495 }
5496
Aneesh Kumar K.V3999f522016-12-12 16:41:56 -08005497 pte = huge_ptep_get(ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08005498 old_page = pte_page(pte);
5499
Yang Yang662ce1d2022-06-01 15:55:25 -07005500 delayacct_wpcopy_start();
5501
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005502retry_avoidcopy:
David Hildenbrandc89357e2022-05-09 18:20:45 -07005503 /*
5504 * If no-one else is actually using this page, we're the exclusive
5505 * owner and can reuse this page.
5506 */
Joonsoo Kim37a21402013-09-11 14:21:04 -07005507 if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
David Hildenbrandc89357e2022-05-09 18:20:45 -07005508 if (!PageAnonExclusive(old_page))
5509 page_move_anon_rmap(old_page, vma);
5510 if (likely(!unshare))
5511 set_huge_ptep_writable(vma, haddr, ptep);
Yang Yang662ce1d2022-06-01 15:55:25 -07005512
5513 delayacct_wpcopy_end();
Nick Piggin83c54072007-07-19 01:47:05 -07005514 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08005515 }
David Hildenbrand6c287602022-05-09 18:20:44 -07005516 VM_BUG_ON_PAGE(PageAnon(old_page) && PageAnonExclusive(old_page),
5517 old_page);
David Gibson1e8f8892006-01-06 00:10:44 -08005518
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005519 /*
5520 * If the process that created a MAP_PRIVATE mapping is about to
5521 * perform a COW due to a shared page count, attempt to satisfy
5522 * the allocation without using the existing reserves. The pagecache
5523 * page is used to determine if the reserve at this address was
5524 * consumed or not. If reserves were used, a partial faulted mapping
5525 * at the time of fork() could consume its reserves on COW instead
5526 * of the full address range.
5527 */
Joonsoo Kim5944d012013-09-11 14:21:55 -07005528 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005529 old_page != pagecache_page)
5530 outside_reserve = 1;
5531
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005532 get_page(old_page);
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08005533
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005534 /*
5535 * Drop page table lock as buddy allocator may be called. It will
5536 * be acquired again before returning to the caller, as expected.
5537 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005538 spin_unlock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07005539 new_page = alloc_huge_page(vma, haddr, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08005540
Adam Litke2fc39ce2007-11-14 16:59:39 -08005541 if (IS_ERR(new_page)) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005542 /*
5543 * If a process owning a MAP_PRIVATE mapping fails to COW,
5544 * it is due to references held by a child and an insufficient
5545 * huge page pool. To guarantee the original mappers
5546 * reliability, unmap the page from child processes. The child
5547 * may get SIGKILLed if it later faults.
5548 */
5549 if (outside_reserve) {
Mike Kravetz40549ba2022-09-14 15:18:09 -07005550 struct address_space *mapping = vma->vm_file->f_mapping;
5551 pgoff_t idx;
5552 u32 hash;
5553
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005554 put_page(old_page);
Mike Kravetz40549ba2022-09-14 15:18:09 -07005555 /*
5556 * Drop hugetlb_fault_mutex and vma_lock before
5557 * unmapping. unmapping needs to hold vma_lock
5558 * in write mode. Dropping vma_lock in read mode
5559 * here is OK as COW mappings do not interact with
5560 * PMD sharing.
5561 *
5562 * Reacquire both after unmap operation.
5563 */
5564 idx = vma_hugecache_offset(h, vma, haddr);
5565 hash = hugetlb_fault_mutex_hash(mapping, idx);
5566 hugetlb_vma_unlock_read(vma);
5567 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
5568
Huang Ying5b7a1d42018-08-17 15:45:53 -07005569 unmap_ref_private(mm, vma, old_page, haddr);
Mike Kravetz40549ba2022-09-14 15:18:09 -07005570
5571 mutex_lock(&hugetlb_fault_mutex_table[hash]);
5572 hugetlb_vma_lock_read(vma);
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07005573 spin_lock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07005574 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07005575 if (likely(ptep &&
5576 pte_same(huge_ptep_get(ptep), pte)))
5577 goto retry_avoidcopy;
5578 /*
5579 * race occurs while re-acquiring page table
5580 * lock, and our job is done.
5581 */
Yang Yang662ce1d2022-06-01 15:55:25 -07005582 delayacct_wpcopy_end();
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07005583 return 0;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005584 }
5585
Souptick Joarder2b740302018-08-23 17:01:36 -07005586 ret = vmf_error(PTR_ERR(new_page));
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005587 goto out_release_old;
David Gibson1e8f8892006-01-06 00:10:44 -08005588 }
5589
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005590 /*
5591 * When the original hugepage is shared one, it does not have
5592 * anon_vma prepared.
5593 */
Dean Nelson44e2aa92010-10-26 14:22:08 -07005594 if (unlikely(anon_vma_prepare(vma))) {
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005595 ret = VM_FAULT_OOM;
5596 goto out_release_all;
Dean Nelson44e2aa92010-10-26 14:22:08 -07005597 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005598
Huang Ying974e6d62018-08-17 15:45:57 -07005599 copy_user_huge_page(new_page, old_page, address, vma,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005600 pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08005601 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08005602
Jérôme Glisse7269f992019-05-13 17:20:53 -07005603 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, haddr,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07005604 haddr + huge_page_size(h));
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005605 mmu_notifier_invalidate_range_start(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005606
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08005607 /*
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005608 * Retake the page table lock to check for racing updates
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08005609 * before the page tables are altered
5610 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005611 spin_lock(ptl);
Huang Ying5b7a1d42018-08-17 15:45:53 -07005612 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Naoya Horiguchia9af0c52014-04-07 15:36:54 -07005613 if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
David Hildenbrandc89357e2022-05-09 18:20:45 -07005614 /* Break COW or unshare */
Huang Ying5b7a1d42018-08-17 15:45:53 -07005615 huge_ptep_clear_flush(vma, haddr, ptep);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005616 mmu_notifier_invalidate_range(mm, range.start, range.end);
Hugh Dickinscea86fe2022-02-14 18:26:39 -08005617 page_remove_rmap(old_page, vma, true);
Huang Ying5b7a1d42018-08-17 15:45:53 -07005618 hugepage_add_new_anon_rmap(new_page, vma, haddr);
Pasha Tatashin1eba86c2022-01-14 14:06:29 -08005619 set_huge_pte_at(mm, haddr, ptep,
David Hildenbrandc89357e2022-05-09 18:20:45 -07005620 make_huge_pte(vma, new_page, !unshare));
Mike Kravetz8f251a32021-02-24 12:08:56 -08005621 SetHPageMigratable(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08005622 /* Make the old page be freed below */
5623 new_page = old_page;
5624 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005625 spin_unlock(ptl);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005626 mmu_notifier_invalidate_range_end(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005627out_release_all:
David Hildenbrandc89357e2022-05-09 18:20:45 -07005628 /*
5629 * No restore in case of successful pagetable update (Break COW or
5630 * unshare)
5631 */
Mike Kravetzc7b18502021-08-19 19:04:33 -07005632 if (new_page != old_page)
5633 restore_reserve_on_error(h, vma, haddr, new_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005634 put_page(new_page);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005635out_release_old:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005636 put_page(old_page);
Joonsoo Kim83120342013-09-11 14:21:57 -07005637
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005638 spin_lock(ptl); /* Caller expects lock to be held */
Yang Yang662ce1d2022-06-01 15:55:25 -07005639
5640 delayacct_wpcopy_end();
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005641 return ret;
David Gibson1e8f8892006-01-06 00:10:44 -08005642}
5643
Hugh Dickins3ae77f42009-09-21 17:03:33 -07005644/*
5645 * Return whether there is a pagecache page to back given address within VMA.
5646 * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
5647 */
5648static bool hugetlbfs_pagecache_present(struct hstate *h,
Hugh Dickins2a15efc2009-09-21 17:03:27 -07005649 struct vm_area_struct *vma, unsigned long address)
5650{
5651 struct address_space *mapping;
5652 pgoff_t idx;
5653 struct page *page;
5654
5655 mapping = vma->vm_file->f_mapping;
5656 idx = vma_hugecache_offset(h, vma, address);
5657
5658 page = find_get_page(mapping, idx);
5659 if (page)
5660 put_page(page);
5661 return page != NULL;
5662}
5663
Mike Kravetz7e1813d2022-09-14 15:18:04 -07005664int hugetlb_add_to_page_cache(struct page *page, struct address_space *mapping,
Mike Kravetzab76ad52015-09-08 15:01:50 -07005665 pgoff_t idx)
5666{
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005667 struct folio *folio = page_folio(page);
Mike Kravetzab76ad52015-09-08 15:01:50 -07005668 struct inode *inode = mapping->host;
5669 struct hstate *h = hstate_inode(inode);
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005670 int err;
Mike Kravetzab76ad52015-09-08 15:01:50 -07005671
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005672 __folio_set_locked(folio);
5673 err = __filemap_add_folio(mapping, folio, idx, GFP_KERNEL, NULL);
5674
5675 if (unlikely(err)) {
5676 __folio_clear_locked(folio);
Mike Kravetzab76ad52015-09-08 15:01:50 -07005677 return err;
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005678 }
Mike Kravetzd6995da2021-02-24 12:08:51 -08005679 ClearHPageRestoreReserve(page);
Mike Kravetzab76ad52015-09-08 15:01:50 -07005680
Mike Kravetz22146c32018-10-26 15:10:58 -07005681 /*
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005682 * mark folio dirty so that it will not be removed from cache/file
Mike Kravetz22146c32018-10-26 15:10:58 -07005683 * by non-hugetlbfs specific code paths.
5684 */
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005685 folio_mark_dirty(folio);
Mike Kravetz22146c32018-10-26 15:10:58 -07005686
Mike Kravetzab76ad52015-09-08 15:01:50 -07005687 spin_lock(&inode->i_lock);
5688 inode->i_blocks += blocks_per_huge_page(h);
5689 spin_unlock(&inode->i_lock);
5690 return 0;
5691}
5692
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005693static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma,
5694 struct address_space *mapping,
5695 pgoff_t idx,
5696 unsigned int flags,
5697 unsigned long haddr,
Nadav Amit824ddc62022-03-22 14:45:32 -07005698 unsigned long addr,
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005699 unsigned long reason)
5700{
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005701 u32 hash;
5702 struct vm_fault vmf = {
5703 .vma = vma,
5704 .address = haddr,
Nadav Amit824ddc62022-03-22 14:45:32 -07005705 .real_address = addr,
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005706 .flags = flags,
5707
5708 /*
5709 * Hard to debug if it ends up being
5710 * used by a callee that assumes
5711 * something about the other
5712 * uninitialized fields... same as in
5713 * memory.c
5714 */
5715 };
5716
5717 /*
Liu Shixin958f32c2022-09-23 12:21:13 +08005718 * vma_lock and hugetlb_fault_mutex must be dropped before handling
5719 * userfault. Also mmap_lock could be dropped due to handling
5720 * userfault, any vma operation should be careful from here.
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005721 */
Mike Kravetz40549ba2022-09-14 15:18:09 -07005722 hugetlb_vma_unlock_read(vma);
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005723 hash = hugetlb_fault_mutex_hash(mapping, idx);
5724 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Liu Shixin958f32c2022-09-23 12:21:13 +08005725 return handle_userfault(&vmf, reason);
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005726}
5727
Peter Xu2ea7ff12022-10-04 15:33:58 -04005728/*
5729 * Recheck pte with pgtable lock. Returns true if pte didn't change, or
5730 * false if pte changed or is changing.
5731 */
5732static bool hugetlb_pte_stable(struct hstate *h, struct mm_struct *mm,
5733 pte_t *ptep, pte_t old_pte)
5734{
5735 spinlock_t *ptl;
5736 bool same;
5737
5738 ptl = huge_pte_lock(h, mm, ptep);
5739 same = pte_same(huge_ptep_get(ptep), old_pte);
5740 spin_unlock(ptl);
5741
5742 return same;
5743}
5744
Souptick Joarder2b740302018-08-23 17:01:36 -07005745static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
5746 struct vm_area_struct *vma,
5747 struct address_space *mapping, pgoff_t idx,
Peter Xuc64e9122022-05-12 20:22:54 -07005748 unsigned long address, pte_t *ptep,
5749 pte_t old_pte, unsigned int flags)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01005750{
Andi Kleena5516432008-07-23 21:27:41 -07005751 struct hstate *h = hstate_vma(vma);
Souptick Joarder2b740302018-08-23 17:01:36 -07005752 vm_fault_t ret = VM_FAULT_SIGBUS;
Hillf Danton409eb8c2012-01-20 14:34:13 -08005753 int anon_rmap = 0;
Adam Litke4c887262005-10-29 18:16:46 -07005754 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07005755 struct page *page;
David Gibson1e8f8892006-01-06 00:10:44 -08005756 pte_t new_pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005757 spinlock_t *ptl;
Huang Ying285b8dc2018-06-07 17:08:08 -07005758 unsigned long haddr = address & huge_page_mask(h);
Mike Kravetzc7b18502021-08-19 19:04:33 -07005759 bool new_page, new_pagecache_page = false;
Liu Shixin958f32c2022-09-23 12:21:13 +08005760 u32 hash = hugetlb_fault_mutex_hash(mapping, idx);
Adam Litke4c887262005-10-29 18:16:46 -07005761
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005762 /*
5763 * Currently, we are forced to kill the process in the event the
5764 * original mapper has unmapped pages from the child due to a failed
David Hildenbrandc89357e2022-05-09 18:20:45 -07005765 * COW/unsharing. Warn that such a situation has occurred as it may not
5766 * be obvious.
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005767 */
5768 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
Geoffrey Thomas910154d2016-03-09 14:08:04 -08005769 pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
Andrew Mortonffb22af2013-02-22 16:32:08 -08005770 current->pid);
Liu Shixin958f32c2022-09-23 12:21:13 +08005771 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005772 }
5773
Adam Litke4c887262005-10-29 18:16:46 -07005774 /*
Mike Kravetz188a3972022-09-14 15:18:02 -07005775 * Use page lock to guard against racing truncation
5776 * before we get page_table_lock.
Adam Litke4c887262005-10-29 18:16:46 -07005777 */
Mike Kravetzc7b18502021-08-19 19:04:33 -07005778 new_page = false;
Christoph Lameter6bda6662006-01-06 00:10:49 -08005779 page = find_lock_page(mapping, idx);
5780 if (!page) {
Mike Kravetz188a3972022-09-14 15:18:02 -07005781 size = i_size_read(mapping->host) >> huge_page_shift(h);
5782 if (idx >= size)
5783 goto out;
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005784 /* Check for page in userfault range */
Peter Xu2ea7ff12022-10-04 15:33:58 -04005785 if (userfaultfd_missing(vma)) {
5786 /*
5787 * Since hugetlb_no_page() was examining pte
5788 * without pgtable lock, we need to re-test under
5789 * lock because the pte may not be stable and could
5790 * have changed from under us. Try to detect
5791 * either changed or during-changing ptes and retry
5792 * properly when needed.
5793 *
5794 * Note that userfaultfd is actually fine with
5795 * false positives (e.g. caused by pte changed),
5796 * but not wrong logical events (e.g. caused by
5797 * reading a pte during changing). The latter can
5798 * confuse the userspace, so the strictness is very
5799 * much preferred. E.g., MISSING event should
5800 * never happen on the page after UFFDIO_COPY has
5801 * correctly installed the page and returned.
5802 */
5803 if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) {
5804 ret = 0;
5805 goto out;
5806 }
5807
5808 return hugetlb_handle_userfault(vma, mapping, idx, flags,
5809 haddr, address,
5810 VM_UFFD_MISSING);
5811 }
Mike Kravetz1a1aad82017-02-22 15:43:01 -08005812
Huang Ying285b8dc2018-06-07 17:08:08 -07005813 page = alloc_huge_page(vma, haddr, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08005814 if (IS_ERR(page)) {
Mike Kravetz4643d672019-08-13 15:38:00 -07005815 /*
5816 * Returning error will result in faulting task being
5817 * sent SIGBUS. The hugetlb fault mutex prevents two
5818 * tasks from racing to fault in the same page which
5819 * could result in false unable to allocate errors.
5820 * Page migration does not take the fault mutex, but
5821 * does a clear then write of pte's under page table
5822 * lock. Page fault code could race with migration,
5823 * notice the clear pte and try to allocate a page
5824 * here. Before returning error, get ptl and make
5825 * sure there really is no pte entry.
5826 */
Peter Xuf9bf6c02022-10-04 15:33:59 -04005827 if (hugetlb_pte_stable(h, mm, ptep, old_pte))
Miaohe Lind83e6c8a2021-05-04 18:33:31 -07005828 ret = vmf_error(PTR_ERR(page));
Peter Xuf9bf6c02022-10-04 15:33:59 -04005829 else
5830 ret = 0;
Christoph Lameter6bda6662006-01-06 00:10:49 -08005831 goto out;
5832 }
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005833 clear_huge_page(page, address, pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08005834 __SetPageUptodate(page);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08005835 new_page = true;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01005836
Mel Gormanf83a2752009-05-28 14:34:40 -07005837 if (vma->vm_flags & VM_MAYSHARE) {
Mike Kravetz7e1813d2022-09-14 15:18:04 -07005838 int err = hugetlb_add_to_page_cache(page, mapping, idx);
Christoph Lameter6bda6662006-01-06 00:10:49 -08005839 if (err) {
Miaohe Lin3a5497a2022-08-16 21:05:50 +08005840 /*
5841 * err can't be -EEXIST which implies someone
5842 * else consumed the reservation since hugetlb
5843 * fault mutex is held when add a hugetlb page
5844 * to the page cache. So it's safe to call
5845 * restore_reserve_on_error() here.
5846 */
5847 restore_reserve_on_error(h, vma, haddr, page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08005848 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08005849 goto out;
5850 }
Mike Kravetzc7b18502021-08-19 19:04:33 -07005851 new_pagecache_page = true;
Mel Gorman23be7462010-04-23 13:17:56 -04005852 } else {
Christoph Lameter6bda6662006-01-06 00:10:49 -08005853 lock_page(page);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005854 if (unlikely(anon_vma_prepare(vma))) {
5855 ret = VM_FAULT_OOM;
5856 goto backout_unlocked;
5857 }
Hillf Danton409eb8c2012-01-20 14:34:13 -08005858 anon_rmap = 1;
Mel Gorman23be7462010-04-23 13:17:56 -04005859 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005860 } else {
Naoya Horiguchi998b4382010-09-08 10:19:32 +09005861 /*
5862 * If memory error occurs between mmap() and fault, some process
5863 * don't have hwpoisoned swap entry for errored virtual address.
5864 * So we need to block hugepage fault by PG_hwpoison bit check.
5865 */
5866 if (unlikely(PageHWPoison(page))) {
Miaohe Lin0eb98f12021-01-12 15:49:24 -08005867 ret = VM_FAULT_HWPOISON_LARGE |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07005868 VM_FAULT_SET_HINDEX(hstate_index(h));
Naoya Horiguchi998b4382010-09-08 10:19:32 +09005869 goto backout_unlocked;
5870 }
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005871
5872 /* Check for page in userfault range. */
5873 if (userfaultfd_minor(vma)) {
5874 unlock_page(page);
5875 put_page(page);
Peter Xu2ea7ff12022-10-04 15:33:58 -04005876 /* See comment in userfaultfd_missing() block above */
5877 if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) {
5878 ret = 0;
5879 goto out;
5880 }
5881 return hugetlb_handle_userfault(vma, mapping, idx, flags,
5882 haddr, address,
5883 VM_UFFD_MINOR);
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005884 }
Christoph Lameter6bda6662006-01-06 00:10:49 -08005885 }
David Gibson1e8f8892006-01-06 00:10:44 -08005886
Andy Whitcroft57303d82008-08-12 15:08:47 -07005887 /*
5888 * If we are going to COW a private mapping later, we examine the
5889 * pending reservations for this page now. This will ensure that
5890 * any allocations necessary to record that reservation occur outside
5891 * the spinlock.
5892 */
Mike Kravetz5e911372015-09-08 15:01:28 -07005893 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07005894 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07005895 ret = VM_FAULT_OOM;
5896 goto backout_unlocked;
5897 }
Mike Kravetz5e911372015-09-08 15:01:28 -07005898 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07005899 vma_end_reservation(h, vma, haddr);
Mike Kravetz5e911372015-09-08 15:01:28 -07005900 }
Andy Whitcroft57303d82008-08-12 15:08:47 -07005901
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08005902 ptl = huge_pte_lock(h, mm, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07005903 ret = 0;
Peter Xuc64e9122022-05-12 20:22:54 -07005904 /* If pte changed from under us, retry */
5905 if (!pte_same(huge_ptep_get(ptep), old_pte))
Adam Litke4c887262005-10-29 18:16:46 -07005906 goto backout;
5907
Peter Xu47815932022-10-20 15:38:32 -04005908 if (anon_rmap)
Huang Ying285b8dc2018-06-07 17:08:08 -07005909 hugepage_add_new_anon_rmap(page, vma, haddr);
Peter Xu47815932022-10-20 15:38:32 -04005910 else
David Hildenbrandfb3d8242022-05-09 18:20:43 -07005911 page_dup_file_rmap(page, true);
David Gibson1e8f8892006-01-06 00:10:44 -08005912 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
5913 && (vma->vm_flags & VM_SHARED)));
Peter Xuc64e9122022-05-12 20:22:54 -07005914 /*
5915 * If this pte was previously wr-protected, keep it wr-protected even
5916 * if populated.
5917 */
5918 if (unlikely(pte_marker_uffd_wp(old_pte)))
5919 new_pte = huge_pte_wrprotect(huge_pte_mkuffd_wp(new_pte));
Huang Ying285b8dc2018-06-07 17:08:08 -07005920 set_huge_pte_at(mm, haddr, ptep, new_pte);
David Gibson1e8f8892006-01-06 00:10:44 -08005921
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08005922 hugetlb_count_add(pages_per_huge_page(h), mm);
Hugh Dickins788c7df2009-06-23 13:49:05 +01005923 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
David Gibson1e8f8892006-01-06 00:10:44 -08005924 /* Optimization, do the COW without a second fault */
David Hildenbrandc89357e2022-05-09 18:20:45 -07005925 ret = hugetlb_wp(mm, vma, address, ptep, flags, page, ptl);
David Gibson1e8f8892006-01-06 00:10:44 -08005926 }
5927
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005928 spin_unlock(ptl);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08005929
5930 /*
Mike Kravetz8f251a32021-02-24 12:08:56 -08005931 * Only set HPageMigratable in newly allocated pages. Existing pages
5932 * found in the pagecache may not have HPageMigratableset if they have
5933 * been isolated for migration.
Mike Kravetzcb6acd02019-02-28 16:22:02 -08005934 */
5935 if (new_page)
Mike Kravetz8f251a32021-02-24 12:08:56 -08005936 SetHPageMigratable(page);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08005937
Adam Litke4c887262005-10-29 18:16:46 -07005938 unlock_page(page);
5939out:
Liu Shixin958f32c2022-09-23 12:21:13 +08005940 hugetlb_vma_unlock_read(vma);
5941 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01005942 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07005943
5944backout:
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005945 spin_unlock(ptl);
Andy Whitcroft2b267362008-08-12 15:08:49 -07005946backout_unlocked:
Mike Kravetzc7b18502021-08-19 19:04:33 -07005947 if (new_page && !new_pagecache_page)
5948 restore_reserve_on_error(h, vma, haddr, page);
Mike Kravetzfa277592022-09-14 15:18:10 -07005949
5950 unlock_page(page);
Adam Litke4c887262005-10-29 18:16:46 -07005951 put_page(page);
5952 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01005953}
5954
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005955#ifdef CONFIG_SMP
Wei Yang188b04a2019-11-30 17:57:02 -08005956u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005957{
5958 unsigned long key[2];
5959 u32 hash;
5960
Mike Kravetz1b426ba2019-05-13 17:19:41 -07005961 key[0] = (unsigned long) mapping;
5962 key[1] = idx;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005963
Mike Kravetz55254632019-11-30 17:56:30 -08005964 hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005965
5966 return hash & (num_fault_mutexes - 1);
5967}
5968#else
5969/*
Miaohe Lin6c26d312021-02-24 12:07:19 -08005970 * For uniprocessor systems we always use a single mutex, so just
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005971 * return 0 and avoid the hashing overhead.
5972 */
Wei Yang188b04a2019-11-30 17:57:02 -08005973u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005974{
5975 return 0;
5976}
5977#endif
5978
Souptick Joarder2b740302018-08-23 17:01:36 -07005979vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
Hugh Dickins788c7df2009-06-23 13:49:05 +01005980 unsigned long address, unsigned int flags)
Adam Litke86e52162006-01-06 00:10:43 -08005981{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005982 pte_t *ptep, entry;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005983 spinlock_t *ptl;
Souptick Joarder2b740302018-08-23 17:01:36 -07005984 vm_fault_t ret;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005985 u32 hash;
5986 pgoff_t idx;
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005987 struct page *page = NULL;
Andy Whitcroft57303d82008-08-12 15:08:47 -07005988 struct page *pagecache_page = NULL;
Andi Kleena5516432008-07-23 21:27:41 -07005989 struct hstate *h = hstate_vma(vma);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005990 struct address_space *mapping;
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08005991 int need_wait_lock = 0;
Huang Ying285b8dc2018-06-07 17:08:08 -07005992 unsigned long haddr = address & huge_page_mask(h);
Adam Litke86e52162006-01-06 00:10:43 -08005993
Huang Ying285b8dc2018-06-07 17:08:08 -07005994 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09005995 if (ptep) {
Mike Kravetz40549ba2022-09-14 15:18:09 -07005996 /*
5997 * Since we hold no locks, ptep could be stale. That is
5998 * OK as we are only making decisions based on content and
5999 * not actually modifying content here.
6000 */
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09006001 entry = huge_ptep_get(ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09006002 if (unlikely(is_hugetlb_entry_migration(entry))) {
Miaohe Linad1ac592022-05-30 19:30:16 +08006003 migration_entry_wait_huge(vma, ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09006004 return 0;
6005 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
Chris Forbes32f84522011-07-25 17:12:14 -07006006 return VM_FAULT_HWPOISON_LARGE |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07006007 VM_FAULT_SET_HINDEX(hstate_index(h));
Naoya Horiguchifd6a03e2010-05-28 09:29:21 +09006008 }
6009
David Gibson3935baa2006-03-22 00:08:53 -08006010 /*
6011 * Serialize hugepage allocation and instantiation, so that we don't
6012 * get spurious allocation failures if two CPUs race to instantiate
6013 * the same page in the page cache.
6014 */
Mike Kravetz40549ba2022-09-14 15:18:09 -07006015 mapping = vma->vm_file->f_mapping;
6016 idx = vma_hugecache_offset(h, vma, haddr);
Wei Yang188b04a2019-11-30 17:57:02 -08006017 hash = hugetlb_fault_mutex_hash(mapping, idx);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07006018 mutex_lock(&hugetlb_fault_mutex_table[hash]);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07006019
Mike Kravetz40549ba2022-09-14 15:18:09 -07006020 /*
6021 * Acquire vma lock before calling huge_pte_alloc and hold
6022 * until finished with ptep. This prevents huge_pmd_unshare from
6023 * being called elsewhere and making the ptep no longer valid.
6024 *
6025 * ptep could have already be assigned via huge_pte_offset. That
6026 * is OK, as huge_pte_alloc will return the same value unless
6027 * something has changed.
6028 */
6029 hugetlb_vma_lock_read(vma);
6030 ptep = huge_pte_alloc(mm, vma, haddr, huge_page_size(h));
6031 if (!ptep) {
6032 hugetlb_vma_unlock_read(vma);
6033 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6034 return VM_FAULT_OOM;
6035 }
6036
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07006037 entry = huge_ptep_get(ptep);
Peter Xuc64e9122022-05-12 20:22:54 -07006038 /* PTE markers should be handled the same way as none pte */
Liu Shixin958f32c2022-09-23 12:21:13 +08006039 if (huge_pte_none_mostly(entry))
6040 /*
6041 * hugetlb_no_page will drop vma lock and hugetlb fault
6042 * mutex internally, which make us return immediately.
6043 */
6044 return hugetlb_no_page(mm, vma, mapping, idx, address, ptep,
Peter Xuc64e9122022-05-12 20:22:54 -07006045 entry, flags);
Adam Litke86e52162006-01-06 00:10:43 -08006046
Nick Piggin83c54072007-07-19 01:47:05 -07006047 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08006048
Andy Whitcroft57303d82008-08-12 15:08:47 -07006049 /*
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006050 * entry could be a migration/hwpoison entry at this point, so this
6051 * check prevents the kernel from going below assuming that we have
Ethon Paul7c8de352020-06-04 16:49:07 -07006052 * an active hugepage in pagecache. This goto expects the 2nd page
6053 * fault, and is_hugetlb_entry_(migration|hwpoisoned) check will
6054 * properly handle it.
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006055 */
6056 if (!pte_present(entry))
6057 goto out_mutex;
6058
6059 /*
David Hildenbrandc89357e2022-05-09 18:20:45 -07006060 * If we are going to COW/unshare the mapping later, we examine the
6061 * pending reservations for this page now. This will ensure that any
Andy Whitcroft57303d82008-08-12 15:08:47 -07006062 * allocations necessary to record that reservation occur outside the
David Hildenbrand1d8d14642022-08-11 12:34:35 +02006063 * spinlock. Also lookup the pagecache page now as it is used to
6064 * determine if a reservation has been consumed.
Andy Whitcroft57303d82008-08-12 15:08:47 -07006065 */
David Hildenbrandc89357e2022-05-09 18:20:45 -07006066 if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) &&
David Hildenbrand1d8d14642022-08-11 12:34:35 +02006067 !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(entry)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07006068 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07006069 ret = VM_FAULT_OOM;
David Gibsonb4d1d992008-10-15 22:01:11 -07006070 goto out_mutex;
Andy Whitcroft2b267362008-08-12 15:08:49 -07006071 }
Mike Kravetz5e911372015-09-08 15:01:28 -07006072 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07006073 vma_end_reservation(h, vma, haddr);
Andy Whitcroft57303d82008-08-12 15:08:47 -07006074
Miaohe Lin29be8422022-09-01 20:00:27 +08006075 pagecache_page = find_lock_page(mapping, idx);
Andy Whitcroft57303d82008-08-12 15:08:47 -07006076 }
6077
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006078 ptl = huge_pte_lock(h, mm, ptep);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09006079
David Hildenbrandc89357e2022-05-09 18:20:45 -07006080 /* Check for a racing update before calling hugetlb_wp() */
David Gibsonb4d1d992008-10-15 22:01:11 -07006081 if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006082 goto out_ptl;
David Gibsonb4d1d992008-10-15 22:01:11 -07006083
Peter Xu166f3ec2022-05-12 20:22:54 -07006084 /* Handle userfault-wp first, before trying to lock more pages */
6085 if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(ptep)) &&
6086 (flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
6087 struct vm_fault vmf = {
6088 .vma = vma,
6089 .address = haddr,
6090 .real_address = address,
6091 .flags = flags,
6092 };
6093
6094 spin_unlock(ptl);
6095 if (pagecache_page) {
6096 unlock_page(pagecache_page);
6097 put_page(pagecache_page);
6098 }
Mike Kravetz40549ba2022-09-14 15:18:09 -07006099 hugetlb_vma_unlock_read(vma);
Peter Xu166f3ec2022-05-12 20:22:54 -07006100 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Peter Xu166f3ec2022-05-12 20:22:54 -07006101 return handle_userfault(&vmf, VM_UFFD_WP);
6102 }
6103
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006104 /*
David Hildenbrandc89357e2022-05-09 18:20:45 -07006105 * hugetlb_wp() requires page locks of pte_page(entry) and
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006106 * pagecache_page, so here we need take the former one
6107 * when page != pagecache_page or !pagecache_page.
6108 */
6109 page = pte_page(entry);
6110 if (page != pagecache_page)
6111 if (!trylock_page(page)) {
6112 need_wait_lock = 1;
6113 goto out_ptl;
6114 }
6115
6116 get_page(page);
David Gibsonb4d1d992008-10-15 22:01:11 -07006117
David Hildenbrandc89357e2022-05-09 18:20:45 -07006118 if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07006119 if (!huge_pte_write(entry)) {
David Hildenbrandc89357e2022-05-09 18:20:45 -07006120 ret = hugetlb_wp(mm, vma, address, ptep, flags,
6121 pagecache_page, ptl);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006122 goto out_put_page;
David Hildenbrandc89357e2022-05-09 18:20:45 -07006123 } else if (likely(flags & FAULT_FLAG_WRITE)) {
6124 entry = huge_pte_mkdirty(entry);
David Gibsonb4d1d992008-10-15 22:01:11 -07006125 }
David Gibsonb4d1d992008-10-15 22:01:11 -07006126 }
6127 entry = pte_mkyoung(entry);
Huang Ying285b8dc2018-06-07 17:08:08 -07006128 if (huge_ptep_set_access_flags(vma, haddr, ptep, entry,
Hugh Dickins788c7df2009-06-23 13:49:05 +01006129 flags & FAULT_FLAG_WRITE))
Huang Ying285b8dc2018-06-07 17:08:08 -07006130 update_mmu_cache(vma, haddr, ptep);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006131out_put_page:
6132 if (page != pagecache_page)
6133 unlock_page(page);
6134 put_page(page);
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006135out_ptl:
6136 spin_unlock(ptl);
Andy Whitcroft57303d82008-08-12 15:08:47 -07006137
6138 if (pagecache_page) {
6139 unlock_page(pagecache_page);
6140 put_page(pagecache_page);
6141 }
David Gibsonb4d1d992008-10-15 22:01:11 -07006142out_mutex:
Mike Kravetz40549ba2022-09-14 15:18:09 -07006143 hugetlb_vma_unlock_read(vma);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07006144 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006145 /*
6146 * Generally it's safe to hold refcount during waiting page lock. But
6147 * here we just wait to defer the next page fault to avoid busy loop and
6148 * the page is not used after unlocked before returning from the current
6149 * page fault. So we are safe from accessing freed page, even if we wait
6150 * here without taking refcount.
6151 */
6152 if (need_wait_lock)
6153 wait_on_page_locked(page);
David Gibson1e8f8892006-01-06 00:10:44 -08006154 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08006155}
6156
Axel Rasmussen714c1892021-05-04 18:35:45 -07006157#ifdef CONFIG_USERFAULTFD
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006158/*
6159 * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with
6160 * modifications for huge pages.
6161 */
6162int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
6163 pte_t *dst_pte,
6164 struct vm_area_struct *dst_vma,
6165 unsigned long dst_addr,
6166 unsigned long src_addr,
Axel Rasmussenf6191472021-05-04 18:35:49 -07006167 enum mcopy_atomic_mode mode,
Peter Xu6041c692022-05-12 20:22:54 -07006168 struct page **pagep,
6169 bool wp_copy)
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006170{
Axel Rasmussenf6191472021-05-04 18:35:49 -07006171 bool is_continue = (mode == MCOPY_ATOMIC_CONTINUE);
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006172 struct hstate *h = hstate_vma(dst_vma);
6173 struct address_space *mapping = dst_vma->vm_file->f_mapping;
6174 pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr);
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006175 unsigned long size;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006176 int vm_shared = dst_vma->vm_flags & VM_SHARED;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006177 pte_t _dst_pte;
6178 spinlock_t *ptl;
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006179 int ret = -ENOMEM;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006180 struct page *page;
Axel Rasmussenf6191472021-05-04 18:35:49 -07006181 int writable;
Mina Almasrycc300422021-11-19 16:43:43 -08006182 bool page_in_pagecache = false;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006183
Axel Rasmussenf6191472021-05-04 18:35:49 -07006184 if (is_continue) {
6185 ret = -EFAULT;
6186 page = find_lock_page(mapping, idx);
6187 if (!page)
6188 goto out;
Mina Almasrycc300422021-11-19 16:43:43 -08006189 page_in_pagecache = true;
Axel Rasmussenf6191472021-05-04 18:35:49 -07006190 } else if (!*pagep) {
Mina Almasryd84cf062021-06-04 20:01:36 -07006191 /* If a page already exists, then it's UFFDIO_COPY for
6192 * a non-missing case. Return -EEXIST.
6193 */
6194 if (vm_shared &&
6195 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
6196 ret = -EEXIST;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006197 goto out;
Mina Almasryd84cf062021-06-04 20:01:36 -07006198 }
6199
6200 page = alloc_huge_page(dst_vma, dst_addr, 0);
6201 if (IS_ERR(page)) {
6202 ret = -ENOMEM;
6203 goto out;
6204 }
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006205
6206 ret = copy_huge_page_from_user(page,
6207 (const void __user *) src_addr,
Mike Kravetz810a56b2017-02-22 15:42:58 -08006208 pages_per_huge_page(h), false);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006209
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07006210 /* fallback to copy_from_user outside mmap_lock */
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006211 if (unlikely(ret)) {
Andrea Arcangeli9e368252018-11-30 14:09:25 -08006212 ret = -ENOENT;
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006213 /* Free the allocated page which may have
6214 * consumed a reservation.
6215 */
6216 restore_reserve_on_error(h, dst_vma, dst_addr, page);
6217 put_page(page);
6218
6219 /* Allocate a temporary page to hold the copied
6220 * contents.
6221 */
6222 page = alloc_huge_page_vma(h, dst_vma, dst_addr);
6223 if (!page) {
6224 ret = -ENOMEM;
6225 goto out;
6226 }
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006227 *pagep = page;
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006228 /* Set the outparam pagep and return to the caller to
6229 * copy the contents outside the lock. Don't free the
6230 * page.
6231 */
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006232 goto out;
6233 }
6234 } else {
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006235 if (vm_shared &&
6236 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
6237 put_page(*pagep);
6238 ret = -EEXIST;
6239 *pagep = NULL;
6240 goto out;
6241 }
6242
6243 page = alloc_huge_page(dst_vma, dst_addr, 0);
6244 if (IS_ERR(page)) {
Miaohe Linda9a2982022-07-09 17:26:29 +08006245 put_page(*pagep);
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006246 ret = -ENOMEM;
6247 *pagep = NULL;
6248 goto out;
6249 }
Muchun Song34892362022-03-22 14:42:02 -07006250 copy_user_huge_page(page, *pagep, dst_addr, dst_vma,
6251 pages_per_huge_page(h));
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006252 put_page(*pagep);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006253 *pagep = NULL;
6254 }
6255
6256 /*
6257 * The memory barrier inside __SetPageUptodate makes sure that
6258 * preceding stores to the page contents become visible before
6259 * the set_pte_at() write.
6260 */
6261 __SetPageUptodate(page);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006262
Axel Rasmussenf6191472021-05-04 18:35:49 -07006263 /* Add shared, newly allocated pages to the page cache. */
6264 if (vm_shared && !is_continue) {
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006265 size = i_size_read(mapping->host) >> huge_page_shift(h);
6266 ret = -EFAULT;
6267 if (idx >= size)
6268 goto out_release_nounlock;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006269
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006270 /*
6271 * Serialization between remove_inode_hugepages() and
Mike Kravetz7e1813d2022-09-14 15:18:04 -07006272 * hugetlb_add_to_page_cache() below happens through the
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006273 * hugetlb_fault_mutex_table that here must be hold by
6274 * the caller.
6275 */
Mike Kravetz7e1813d2022-09-14 15:18:04 -07006276 ret = hugetlb_add_to_page_cache(page, mapping, idx);
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006277 if (ret)
6278 goto out_release_nounlock;
Mina Almasrycc300422021-11-19 16:43:43 -08006279 page_in_pagecache = true;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006280 }
6281
Miaohe Linbcc66542022-09-01 20:00:25 +08006282 ptl = huge_pte_lock(h, dst_mm, dst_pte);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006283
James Houghton86251472022-10-18 20:01:25 +00006284 ret = -EIO;
6285 if (PageHWPoison(page))
6286 goto out_release_unlock;
6287
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006288 /*
Peter Xu6041c692022-05-12 20:22:54 -07006289 * We allow to overwrite a pte marker: consider when both MISSING|WP
6290 * registered, we firstly wr-protect a none pte which has no page cache
6291 * page backing it, then access the page.
6292 */
Mike Kravetzfa277592022-09-14 15:18:10 -07006293 ret = -EEXIST;
Peter Xu6041c692022-05-12 20:22:54 -07006294 if (!huge_pte_none_mostly(huge_ptep_get(dst_pte)))
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006295 goto out_release_unlock;
6296
Peter Xu47815932022-10-20 15:38:32 -04006297 if (page_in_pagecache)
David Hildenbrandfb3d8242022-05-09 18:20:43 -07006298 page_dup_file_rmap(page, true);
Peter Xu47815932022-10-20 15:38:32 -04006299 else
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006300 hugepage_add_new_anon_rmap(page, dst_vma, dst_addr);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006301
Peter Xu6041c692022-05-12 20:22:54 -07006302 /*
6303 * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY
6304 * with wp flag set, don't set pte write bit.
6305 */
6306 if (wp_copy || (is_continue && !vm_shared))
Axel Rasmussenf6191472021-05-04 18:35:49 -07006307 writable = 0;
6308 else
6309 writable = dst_vma->vm_flags & VM_WRITE;
6310
6311 _dst_pte = make_huge_pte(dst_vma, page, writable);
Peter Xu6041c692022-05-12 20:22:54 -07006312 /*
6313 * Always mark UFFDIO_COPY page dirty; note that this may not be
6314 * extremely important for hugetlbfs for now since swapping is not
6315 * supported, but we should still be clear in that this page cannot be
6316 * thrown away at will, even if write bit not set.
6317 */
6318 _dst_pte = huge_pte_mkdirty(_dst_pte);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006319 _dst_pte = pte_mkyoung(_dst_pte);
6320
Peter Xu6041c692022-05-12 20:22:54 -07006321 if (wp_copy)
6322 _dst_pte = huge_pte_mkuffd_wp(_dst_pte);
6323
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006324 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
6325
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006326 hugetlb_count_add(pages_per_huge_page(h), dst_mm);
6327
6328 /* No need to invalidate - it was non-present before */
6329 update_mmu_cache(dst_vma, dst_addr, dst_pte);
6330
6331 spin_unlock(ptl);
Axel Rasmussenf6191472021-05-04 18:35:49 -07006332 if (!is_continue)
6333 SetHPageMigratable(page);
6334 if (vm_shared || is_continue)
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006335 unlock_page(page);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006336 ret = 0;
6337out:
6338 return ret;
6339out_release_unlock:
6340 spin_unlock(ptl);
Axel Rasmussenf6191472021-05-04 18:35:49 -07006341 if (vm_shared || is_continue)
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006342 unlock_page(page);
Andrea Arcangeli5af10df2017-08-10 15:23:38 -07006343out_release_nounlock:
Mina Almasrycc300422021-11-19 16:43:43 -08006344 if (!page_in_pagecache)
Mike Kravetzc7b18502021-08-19 19:04:33 -07006345 restore_reserve_on_error(h, dst_vma, dst_addr, page);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006346 put_page(page);
6347 goto out;
6348}
Axel Rasmussen714c1892021-05-04 18:35:45 -07006349#endif /* CONFIG_USERFAULTFD */
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006350
Joao Martins82e5d372021-02-24 12:07:16 -08006351static void record_subpages_vmas(struct page *page, struct vm_area_struct *vma,
6352 int refs, struct page **pages,
6353 struct vm_area_struct **vmas)
6354{
6355 int nr;
6356
6357 for (nr = 0; nr < refs; nr++) {
6358 if (likely(pages))
Cheng Li14455ea2022-09-09 07:31:09 +00006359 pages[nr] = nth_page(page, nr);
Joao Martins82e5d372021-02-24 12:07:16 -08006360 if (vmas)
6361 vmas[nr] = vma;
6362 }
6363}
6364
David Hildenbrand84209e82022-11-16 11:26:48 +01006365static inline bool __follow_hugetlb_must_fault(struct vm_area_struct *vma,
6366 unsigned int flags, pte_t *pte,
David Hildenbranda7f22662022-05-09 18:20:45 -07006367 bool *unshare)
6368{
6369 pte_t pteval = huge_ptep_get(pte);
6370
6371 *unshare = false;
6372 if (is_swap_pte(pteval))
6373 return true;
6374 if (huge_pte_write(pteval))
6375 return false;
6376 if (flags & FOLL_WRITE)
6377 return true;
David Hildenbrand84209e82022-11-16 11:26:48 +01006378 if (gup_must_unshare(vma, flags, pte_page(pteval))) {
David Hildenbranda7f22662022-05-09 18:20:45 -07006379 *unshare = true;
6380 return true;
6381 }
6382 return false;
6383}
6384
Mike Kravetz57a196a2022-09-18 19:13:48 -07006385struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma,
6386 unsigned long address, unsigned int flags)
6387{
6388 struct hstate *h = hstate_vma(vma);
6389 struct mm_struct *mm = vma->vm_mm;
6390 unsigned long haddr = address & huge_page_mask(h);
6391 struct page *page = NULL;
6392 spinlock_t *ptl;
6393 pte_t *pte, entry;
6394
6395 /*
6396 * FOLL_PIN is not supported for follow_page(). Ordinary GUP goes via
6397 * follow_hugetlb_page().
6398 */
6399 if (WARN_ON_ONCE(flags & FOLL_PIN))
6400 return NULL;
6401
6402retry:
6403 pte = huge_pte_offset(mm, haddr, huge_page_size(h));
6404 if (!pte)
6405 return NULL;
6406
6407 ptl = huge_pte_lock(h, mm, pte);
6408 entry = huge_ptep_get(pte);
6409 if (pte_present(entry)) {
6410 page = pte_page(entry) +
6411 ((address & ~huge_page_mask(h)) >> PAGE_SHIFT);
6412 /*
6413 * Note that page may be a sub-page, and with vmemmap
6414 * optimizations the page struct may be read only.
6415 * try_grab_page() will increase the ref count on the
6416 * head page, so this will be OK.
6417 *
Linus Torvaldse2ca6ba2022-12-13 19:29:45 -08006418 * try_grab_page() should always be able to get the page here,
6419 * because we hold the ptl lock and have verified pte_present().
Mike Kravetz57a196a2022-09-18 19:13:48 -07006420 */
Linus Torvaldse2ca6ba2022-12-13 19:29:45 -08006421 if (try_grab_page(page, flags)) {
Mike Kravetz57a196a2022-09-18 19:13:48 -07006422 page = NULL;
6423 goto out;
6424 }
6425 } else {
6426 if (is_hugetlb_entry_migration(entry)) {
6427 spin_unlock(ptl);
6428 __migration_entry_wait_huge(pte, ptl);
6429 goto retry;
6430 }
6431 /*
6432 * hwpoisoned entry is treated as no_page_table in
6433 * follow_page_mask().
6434 */
6435 }
6436out:
6437 spin_unlock(ptl);
6438 return page;
6439}
6440
Michel Lespinasse28a35712013-02-22 16:35:55 -08006441long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
6442 struct page **pages, struct vm_area_struct **vmas,
6443 unsigned long *position, unsigned long *nr_pages,
Peter Xu4f6da932020-04-01 21:07:58 -07006444 long i, unsigned int flags, int *locked)
David Gibson63551ae2005-06-21 17:14:44 -07006445{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08006446 unsigned long pfn_offset;
6447 unsigned long vaddr = *position;
Michel Lespinasse28a35712013-02-22 16:35:55 -08006448 unsigned long remainder = *nr_pages;
Andi Kleena5516432008-07-23 21:27:41 -07006449 struct hstate *h = hstate_vma(vma);
Joao Martins0fa5bc42021-02-24 12:07:12 -08006450 int err = -EFAULT, refs;
David Gibson63551ae2005-06-21 17:14:44 -07006451
David Gibson63551ae2005-06-21 17:14:44 -07006452 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07006453 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006454 spinlock_t *ptl = NULL;
David Hildenbranda7f22662022-05-09 18:20:45 -07006455 bool unshare = false;
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006456 int absent;
Adam Litke4c887262005-10-29 18:16:46 -07006457 struct page *page;
6458
6459 /*
David Rientjes02057962015-04-14 15:48:24 -07006460 * If we have a pending SIGKILL, don't keep faulting pages and
6461 * potentially allocating memory.
6462 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -08006463 if (fatal_signal_pending(current)) {
David Rientjes02057962015-04-14 15:48:24 -07006464 remainder = 0;
6465 break;
6466 }
6467
6468 /*
Adam Litke4c887262005-10-29 18:16:46 -07006469 * Some archs (sparc64, sh*) have multiple pte_ts to
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006470 * each hugepage. We have to make sure we get the
Adam Litke4c887262005-10-29 18:16:46 -07006471 * first, for the page indexing below to work.
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006472 *
6473 * Note that page table lock is not held when pte is null.
Adam Litke4c887262005-10-29 18:16:46 -07006474 */
Punit Agrawal7868a202017-07-06 15:39:42 -07006475 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h),
6476 huge_page_size(h));
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006477 if (pte)
6478 ptl = huge_pte_lock(h, mm, pte);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006479 absent = !pte || huge_pte_none(huge_ptep_get(pte));
Adam Litke4c887262005-10-29 18:16:46 -07006480
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006481 /*
6482 * When coredumping, it suits get_dump_page if we just return
Hugh Dickins3ae77f42009-09-21 17:03:33 -07006483 * an error where there's an empty slot with no huge pagecache
6484 * to back it. This way, we avoid allocating a hugepage, and
6485 * the sparse dumpfile avoids allocating disk blocks, but its
6486 * huge holes still show up with zeroes where they need to be.
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006487 */
Hugh Dickins3ae77f42009-09-21 17:03:33 -07006488 if (absent && (flags & FOLL_DUMP) &&
6489 !hugetlbfs_pagecache_present(h, vma, vaddr)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006490 if (pte)
6491 spin_unlock(ptl);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006492 remainder = 0;
6493 break;
6494 }
6495
Naoya Horiguchi9cc3a5b2013-04-17 15:58:30 -07006496 /*
6497 * We need call hugetlb_fault for both hugepages under migration
6498 * (in which case hugetlb_fault waits for the migration,) and
6499 * hwpoisoned hugepages (in which case we need to prevent the
6500 * caller from accessing to them.) In order to do this, we use
6501 * here is_swap_pte instead of is_hugetlb_entry_migration and
6502 * is_hugetlb_entry_hwpoisoned. This is because it simply covers
6503 * both cases, and because we can't follow correct pages
6504 * directly from any kind of swap entries.
6505 */
David Hildenbranda7f22662022-05-09 18:20:45 -07006506 if (absent ||
David Hildenbrand84209e82022-11-16 11:26:48 +01006507 __follow_hugetlb_must_fault(vma, flags, pte, &unshare)) {
Souptick Joarder2b740302018-08-23 17:01:36 -07006508 vm_fault_t ret;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006509 unsigned int fault_flags = 0;
Adam Litke4c887262005-10-29 18:16:46 -07006510
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006511 if (pte)
6512 spin_unlock(ptl);
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006513 if (flags & FOLL_WRITE)
6514 fault_flags |= FAULT_FLAG_WRITE;
David Hildenbranda7f22662022-05-09 18:20:45 -07006515 else if (unshare)
6516 fault_flags |= FAULT_FLAG_UNSHARE;
Peter Xu93c5c612022-10-11 15:58:06 -04006517 if (locked) {
Peter Xu71335f32020-04-01 21:08:53 -07006518 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
6519 FAULT_FLAG_KILLABLE;
Peter Xu93c5c612022-10-11 15:58:06 -04006520 if (flags & FOLL_INTERRUPTIBLE)
6521 fault_flags |= FAULT_FLAG_INTERRUPTIBLE;
6522 }
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006523 if (flags & FOLL_NOWAIT)
6524 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
6525 FAULT_FLAG_RETRY_NOWAIT;
6526 if (flags & FOLL_TRIED) {
Peter Xu4426e942020-04-01 21:08:49 -07006527 /*
6528 * Note: FAULT_FLAG_ALLOW_RETRY and
6529 * FAULT_FLAG_TRIED can co-exist
6530 */
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006531 fault_flags |= FAULT_FLAG_TRIED;
6532 }
6533 ret = hugetlb_fault(mm, vma, vaddr, fault_flags);
6534 if (ret & VM_FAULT_ERROR) {
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07006535 err = vm_fault_to_errno(ret, flags);
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006536 remainder = 0;
6537 break;
6538 }
6539 if (ret & VM_FAULT_RETRY) {
Peter Xu4f6da932020-04-01 21:07:58 -07006540 if (locked &&
Andrea Arcangeli1ac25012019-02-01 14:20:16 -08006541 !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Peter Xu4f6da932020-04-01 21:07:58 -07006542 *locked = 0;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006543 *nr_pages = 0;
6544 /*
6545 * VM_FAULT_RETRY must not return an
6546 * error, it will return zero
6547 * instead.
6548 *
6549 * No need to update "position" as the
6550 * caller will not check it after
6551 * *nr_pages is set to 0.
6552 */
6553 return i;
6554 }
6555 continue;
Adam Litke4c887262005-10-29 18:16:46 -07006556 }
David Gibson63551ae2005-06-21 17:14:44 -07006557
Andi Kleena5516432008-07-23 21:27:41 -07006558 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07006559 page = pte_page(huge_ptep_get(pte));
Linus Torvalds8fde12c2019-04-11 10:49:19 -07006560
David Hildenbrandb6a26192022-05-09 18:20:45 -07006561 VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
6562 !PageAnonExclusive(page), page);
6563
Linus Torvalds8fde12c2019-04-11 10:49:19 -07006564 /*
Zhigang Luacbfb082019-11-30 17:57:06 -08006565 * If subpage information not requested, update counters
6566 * and skip the same_page loop below.
6567 */
6568 if (!pages && !vmas && !pfn_offset &&
6569 (vaddr + huge_page_size(h) < vma->vm_end) &&
6570 (remainder >= pages_per_huge_page(h))) {
6571 vaddr += huge_page_size(h);
6572 remainder -= pages_per_huge_page(h);
6573 i += pages_per_huge_page(h);
6574 spin_unlock(ptl);
6575 continue;
6576 }
6577
Joao Martinsd08af0a2021-07-14 21:27:11 -07006578 /* vaddr may not be aligned to PAGE_SIZE */
6579 refs = min3(pages_per_huge_page(h) - pfn_offset, remainder,
6580 (vma->vm_end - ALIGN_DOWN(vaddr, PAGE_SIZE)) >> PAGE_SHIFT);
Joao Martins0fa5bc42021-02-24 12:07:12 -08006581
Joao Martins82e5d372021-02-24 12:07:16 -08006582 if (pages || vmas)
Cheng Li14455ea2022-09-09 07:31:09 +00006583 record_subpages_vmas(nth_page(page, pfn_offset),
Joao Martins82e5d372021-02-24 12:07:16 -08006584 vma, refs,
6585 likely(pages) ? pages + i : NULL,
6586 vmas ? vmas + i : NULL);
David Gibson63551ae2005-06-21 17:14:44 -07006587
Joao Martins82e5d372021-02-24 12:07:16 -08006588 if (pages) {
Joao Martins0fa5bc42021-02-24 12:07:12 -08006589 /*
Matthew Wilcox (Oracle)822951d2022-01-08 00:15:04 -05006590 * try_grab_folio() should always succeed here,
Joao Martins0fa5bc42021-02-24 12:07:12 -08006591 * because: a) we hold the ptl lock, and b) we've just
6592 * checked that the huge page is present in the page
6593 * tables. If the huge page is present, then the tail
6594 * pages must also be present. The ptl prevents the
6595 * head page and tail pages from being rearranged in
Logan Gunthorpe4003f102022-10-21 11:41:09 -06006596 * any way. As this is hugetlb, the pages will never
6597 * be p2pdma or not longterm pinable. So this page
6598 * must be available at this point, unless the page
6599 * refcount overflowed:
Joao Martins0fa5bc42021-02-24 12:07:12 -08006600 */
Matthew Wilcox (Oracle)822951d2022-01-08 00:15:04 -05006601 if (WARN_ON_ONCE(!try_grab_folio(pages[i], refs,
6602 flags))) {
Joao Martins0fa5bc42021-02-24 12:07:12 -08006603 spin_unlock(ptl);
6604 remainder = 0;
6605 err = -ENOMEM;
6606 break;
6607 }
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08006608 }
Joao Martins82e5d372021-02-24 12:07:16 -08006609
6610 vaddr += (refs << PAGE_SHIFT);
6611 remainder -= refs;
6612 i += refs;
6613
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006614 spin_unlock(ptl);
David Gibson63551ae2005-06-21 17:14:44 -07006615 }
Michel Lespinasse28a35712013-02-22 16:35:55 -08006616 *nr_pages = remainder;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006617 /*
6618 * setting position is actually required only if remainder is
6619 * not zero but it's faster not to add a "if (remainder)"
6620 * branch.
6621 */
David Gibson63551ae2005-06-21 17:14:44 -07006622 *position = vaddr;
6623
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07006624 return i ? i : err;
David Gibson63551ae2005-06-21 17:14:44 -07006625}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006626
Peter Zijlstra7da4d642012-11-19 03:14:23 +01006627unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
Peter Xu5a90d5a2022-05-12 20:22:54 -07006628 unsigned long address, unsigned long end,
6629 pgprot_t newprot, unsigned long cp_flags)
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006630{
6631 struct mm_struct *mm = vma->vm_mm;
6632 unsigned long start = address;
6633 pte_t *ptep;
6634 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07006635 struct hstate *h = hstate_vma(vma);
Peter Xu60dfaad2022-05-12 20:22:55 -07006636 unsigned long pages = 0, psize = huge_page_size(h);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006637 bool shared_pmd = false;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006638 struct mmu_notifier_range range;
Mike Kravetze95a9852022-06-21 16:56:17 -07006639 unsigned long last_addr_mask;
Peter Xu5a90d5a2022-05-12 20:22:54 -07006640 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
6641 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006642
6643 /*
6644 * In the case of shared PMDs, the area to flush could be beyond
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006645 * start/end. Set range.start/range.end to cover the maximum possible
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006646 * range if PMD sharing is possible.
6647 */
Jérôme Glisse7269f992019-05-13 17:20:53 -07006648 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
6649 0, vma, mm, start, end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006650 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006651
6652 BUG_ON(address >= end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006653 flush_cache_range(vma, range.start, range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006654
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006655 mmu_notifier_invalidate_range_start(&range);
Mike Kravetz40549ba2022-09-14 15:18:09 -07006656 hugetlb_vma_lock_write(vma);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08006657 i_mmap_lock_write(vma->vm_file->f_mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07006658 last_addr_mask = hugetlb_mask_last_page(h);
Peter Xu60dfaad2022-05-12 20:22:55 -07006659 for (; address < end; address += psize) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006660 spinlock_t *ptl;
Peter Xu60dfaad2022-05-12 20:22:55 -07006661 ptep = huge_pte_offset(mm, address, psize);
Mike Kravetze95a9852022-06-21 16:56:17 -07006662 if (!ptep) {
Peter Xufed15f12023-01-04 17:52:05 -05006663 if (!uffd_wp) {
6664 address |= last_addr_mask;
6665 continue;
6666 }
6667 /*
6668 * Userfaultfd wr-protect requires pgtable
6669 * pre-allocations to install pte markers.
6670 */
6671 ptep = huge_pte_alloc(mm, vma, address, psize);
6672 if (!ptep)
6673 break;
Mike Kravetze95a9852022-06-21 16:56:17 -07006674 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006675 ptl = huge_pte_lock(h, mm, ptep);
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07006676 if (huge_pmd_unshare(mm, vma, address, ptep)) {
Peter Xu60dfaad2022-05-12 20:22:55 -07006677 /*
6678 * When uffd-wp is enabled on the vma, unshare
6679 * shouldn't happen at all. Warn about it if it
6680 * happened due to some reason.
6681 */
6682 WARN_ON_ONCE(uffd_wp || uffd_wp_resolve);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01006683 pages++;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006684 spin_unlock(ptl);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006685 shared_pmd = true;
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07006686 address |= last_addr_mask;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08006687 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +01006688 }
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006689 pte = huge_ptep_get(ptep);
6690 if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
David Hildenbrand0e678152022-12-22 21:55:10 +01006691 /* Nothing to do. */
6692 } else if (unlikely(is_hugetlb_entry_migration(pte))) {
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006693 swp_entry_t entry = pte_to_swp_entry(pte);
David Hildenbrand6c287602022-05-09 18:20:44 -07006694 struct page *page = pfn_swap_entry_to_page(entry);
David Hildenbrand44f86392022-12-22 21:55:11 +01006695 pte_t newpte = pte;
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006696
David Hildenbrand44f86392022-12-22 21:55:11 +01006697 if (is_writable_migration_entry(entry)) {
David Hildenbrand6c287602022-05-09 18:20:44 -07006698 if (PageAnon(page))
6699 entry = make_readable_exclusive_migration_entry(
6700 swp_offset(entry));
6701 else
6702 entry = make_readable_migration_entry(
6703 swp_offset(entry));
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006704 newpte = swp_entry_to_pte(entry);
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006705 pages++;
6706 }
David Hildenbrand44f86392022-12-22 21:55:11 +01006707
6708 if (uffd_wp)
6709 newpte = pte_swp_mkuffd_wp(newpte);
6710 else if (uffd_wp_resolve)
6711 newpte = pte_swp_clear_uffd_wp(newpte);
6712 if (!pte_same(pte, newpte))
6713 set_huge_pte_at(mm, address, ptep, newpte);
David Hildenbrand0e678152022-12-22 21:55:10 +01006714 } else if (unlikely(is_pte_marker(pte))) {
6715 /* No other markers apply for now. */
6716 WARN_ON_ONCE(!pte_marker_uffd_wp(pte));
Peter Xu60dfaad2022-05-12 20:22:55 -07006717 if (uffd_wp_resolve)
David Hildenbrand0e678152022-12-22 21:55:10 +01006718 /* Safe to modify directly (non-present->none). */
Peter Xu60dfaad2022-05-12 20:22:55 -07006719 huge_pte_clear(mm, address, ptep, psize);
David Hildenbrand0e678152022-12-22 21:55:10 +01006720 } else if (!huge_pte_none(pte)) {
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08006721 pte_t old_pte;
Christophe Leroy79c1c592021-06-30 18:48:00 -07006722 unsigned int shift = huge_page_shift(hstate_vma(vma));
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08006723
6724 old_pte = huge_ptep_modify_prot_start(vma, address, ptep);
Anshuman Khandual16785bd2022-03-22 14:41:47 -07006725 pte = huge_pte_modify(old_pte, newprot);
Christophe Leroy79c1c592021-06-30 18:48:00 -07006726 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
Peter Xu5a90d5a2022-05-12 20:22:54 -07006727 if (uffd_wp)
6728 pte = huge_pte_mkuffd_wp(huge_pte_wrprotect(pte));
6729 else if (uffd_wp_resolve)
6730 pte = huge_pte_clear_uffd_wp(pte);
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08006731 huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01006732 pages++;
Peter Xu60dfaad2022-05-12 20:22:55 -07006733 } else {
6734 /* None pte */
6735 if (unlikely(uffd_wp))
6736 /* Safe to modify directly (none->non-present). */
6737 set_huge_pte_at(mm, address, ptep,
6738 make_pte_marker(PTE_MARKER_UFFD_WP));
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006739 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006740 spin_unlock(ptl);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006741 }
Mel Gormand8333522012-07-31 16:46:20 -07006742 /*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08006743 * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
Mel Gormand8333522012-07-31 16:46:20 -07006744 * may have cleared our pud entry and done put_page on the page table:
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08006745 * once we release i_mmap_rwsem, another task can do the final put_page
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006746 * and that page table be reused and filled with junk. If we actually
6747 * did unshare a page of pmds, flush the range corresponding to the pud.
Mel Gormand8333522012-07-31 16:46:20 -07006748 */
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006749 if (shared_pmd)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006750 flush_hugetlb_tlb_range(vma, range.start, range.end);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006751 else
6752 flush_hugetlb_tlb_range(vma, start, end);
Jérôme Glisse0f108512017-11-15 17:34:07 -08006753 /*
6754 * No need to call mmu_notifier_invalidate_range() we are downgrading
6755 * page table protection not changing it to point to a new page.
6756 *
Mike Rapoportee657282022-06-27 09:00:26 +03006757 * See Documentation/mm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08006758 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08006759 i_mmap_unlock_write(vma->vm_file->f_mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07006760 hugetlb_vma_unlock_write(vma);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006761 mmu_notifier_invalidate_range_end(&range);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01006762
6763 return pages << h->order;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006764}
6765
Mike Kravetz33b8f842021-02-24 12:09:54 -08006766/* Return true if reservation was successful, false otherwise. */
6767bool hugetlb_reserve_pages(struct inode *inode,
Mel Gormana1e78772008-07-23 21:27:23 -07006768 long from, long to,
Mel Gorman5a6fe122009-02-10 14:02:27 +00006769 struct vm_area_struct *vma,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09006770 vm_flags_t vm_flags)
Adam Litkee4e574b2007-10-16 01:26:19 -07006771{
Mike Kravetz33b8f842021-02-24 12:09:54 -08006772 long chg, add = -1;
Andi Kleena5516432008-07-23 21:27:41 -07006773 struct hstate *h = hstate_inode(inode);
David Gibson90481622012-03-21 16:34:12 -07006774 struct hugepage_subpool *spool = subpool_inode(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07006775 struct resv_map *resv_map;
Mina Almasry075a61d2020-04-01 21:11:28 -07006776 struct hugetlb_cgroup *h_cg = NULL;
Mina Almasry0db9d742020-04-01 21:11:25 -07006777 long gbl_reserve, regions_needed = 0;
Adam Litkee4e574b2007-10-16 01:26:19 -07006778
Mike Kravetz63489f82018-03-22 16:17:13 -07006779 /* This should never happen */
6780 if (from > to) {
6781 VM_WARN(1, "%s called with a negative range\n", __func__);
Mike Kravetz33b8f842021-02-24 12:09:54 -08006782 return false;
Mike Kravetz63489f82018-03-22 16:17:13 -07006783 }
6784
Mel Gormana1e78772008-07-23 21:27:23 -07006785 /*
Mike Kravetze7008982022-12-12 15:50:41 -08006786 * vma specific semaphore used for pmd sharing and fault/truncation
6787 * synchronization
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07006788 */
6789 hugetlb_vma_lock_alloc(vma);
6790
6791 /*
Mel Gorman17c9d122009-02-11 16:34:16 +00006792 * Only apply hugepage reservation if asked. At fault time, an
6793 * attempt will be made for VM_NORESERVE to allocate a page
David Gibson90481622012-03-21 16:34:12 -07006794 * without using reserves
Mel Gorman17c9d122009-02-11 16:34:16 +00006795 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09006796 if (vm_flags & VM_NORESERVE)
Mike Kravetz33b8f842021-02-24 12:09:54 -08006797 return true;
Mel Gorman17c9d122009-02-11 16:34:16 +00006798
6799 /*
Mel Gormana1e78772008-07-23 21:27:23 -07006800 * Shared mappings base their reservation on the number of pages that
6801 * are already allocated on behalf of the file. Private mappings need
6802 * to reserve the full area even if read-only as mprotect() may be
6803 * called to make the mapping read-write. Assume !vma is a shm mapping
6804 */
Joonsoo Kim9119a412014-04-03 14:47:25 -07006805 if (!vma || vma->vm_flags & VM_MAYSHARE) {
Mike Kravetzf27a5132019-05-13 17:22:55 -07006806 /*
6807 * resv_map can not be NULL as hugetlb_reserve_pages is only
6808 * called for inodes for which resv_maps were created (see
6809 * hugetlbfs_get_inode).
6810 */
Joonsoo Kim4e35f482014-04-03 14:47:30 -07006811 resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07006812
Mina Almasry0db9d742020-04-01 21:11:25 -07006813 chg = region_chg(resv_map, from, to, &regions_needed);
Joonsoo Kim9119a412014-04-03 14:47:25 -07006814 } else {
Mina Almasrye9fe92a2020-04-01 21:11:21 -07006815 /* Private mapping. */
Joonsoo Kim9119a412014-04-03 14:47:25 -07006816 resv_map = resv_map_alloc();
Mel Gorman5a6fe122009-02-10 14:02:27 +00006817 if (!resv_map)
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07006818 goto out_err;
Mel Gorman5a6fe122009-02-10 14:02:27 +00006819
Mel Gorman17c9d122009-02-11 16:34:16 +00006820 chg = to - from;
6821
Mel Gorman5a6fe122009-02-10 14:02:27 +00006822 set_vma_resv_map(vma, resv_map);
6823 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
6824 }
6825
Mike Kravetz33b8f842021-02-24 12:09:54 -08006826 if (chg < 0)
Dave Hansenc50ac052012-05-29 15:06:46 -07006827 goto out_err;
Mel Gorman17c9d122009-02-11 16:34:16 +00006828
Mike Kravetz33b8f842021-02-24 12:09:54 -08006829 if (hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h),
6830 chg * pages_per_huge_page(h), &h_cg) < 0)
Mina Almasry075a61d2020-04-01 21:11:28 -07006831 goto out_err;
Mina Almasry075a61d2020-04-01 21:11:28 -07006832
6833 if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) {
6834 /* For private mappings, the hugetlb_cgroup uncharge info hangs
6835 * of the resv_map.
6836 */
6837 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h);
6838 }
6839
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07006840 /*
6841 * There must be enough pages in the subpool for the mapping. If
6842 * the subpool has a minimum size, there may be some global
6843 * reservations already in place (gbl_reserve).
6844 */
6845 gbl_reserve = hugepage_subpool_get_pages(spool, chg);
Mike Kravetz33b8f842021-02-24 12:09:54 -08006846 if (gbl_reserve < 0)
Mina Almasry075a61d2020-04-01 21:11:28 -07006847 goto out_uncharge_cgroup;
Mel Gorman17c9d122009-02-11 16:34:16 +00006848
6849 /*
6850 * Check enough hugepages are available for the reservation.
David Gibson90481622012-03-21 16:34:12 -07006851 * Hand the pages back to the subpool if there are not
Mel Gorman17c9d122009-02-11 16:34:16 +00006852 */
Mike Kravetz33b8f842021-02-24 12:09:54 -08006853 if (hugetlb_acct_memory(h, gbl_reserve) < 0)
Mina Almasry075a61d2020-04-01 21:11:28 -07006854 goto out_put_pages;
Mel Gorman17c9d122009-02-11 16:34:16 +00006855
6856 /*
6857 * Account for the reservations made. Shared mappings record regions
6858 * that have reservations as they are shared by multiple VMAs.
6859 * When the last VMA disappears, the region map says how much
6860 * the reservation was and the page cache tells how much of
6861 * the reservation was consumed. Private mappings are per-VMA and
6862 * only the consumed reservations are tracked. When the VMA
6863 * disappears, the original reservation is the VMA size and the
6864 * consumed reservations are stored in the map. Hence, nothing
6865 * else has to be done for private mappings here
6866 */
Mike Kravetz33039672015-06-24 16:57:58 -07006867 if (!vma || vma->vm_flags & VM_MAYSHARE) {
Mina Almasry075a61d2020-04-01 21:11:28 -07006868 add = region_add(resv_map, from, to, regions_needed, h, h_cg);
Mike Kravetz33039672015-06-24 16:57:58 -07006869
Mina Almasry0db9d742020-04-01 21:11:25 -07006870 if (unlikely(add < 0)) {
6871 hugetlb_acct_memory(h, -gbl_reserve);
Mina Almasry075a61d2020-04-01 21:11:28 -07006872 goto out_put_pages;
Mina Almasry0db9d742020-04-01 21:11:25 -07006873 } else if (unlikely(chg > add)) {
Mike Kravetz33039672015-06-24 16:57:58 -07006874 /*
6875 * pages in this range were added to the reserve
6876 * map between region_chg and region_add. This
6877 * indicates a race with alloc_huge_page. Adjust
6878 * the subpool and reserve counts modified above
6879 * based on the difference.
6880 */
6881 long rsv_adjust;
6882
Miaohe Lind85aecf2021-03-24 21:37:17 -07006883 /*
6884 * hugetlb_cgroup_uncharge_cgroup_rsvd() will put the
6885 * reference to h_cg->css. See comment below for detail.
6886 */
Mina Almasry075a61d2020-04-01 21:11:28 -07006887 hugetlb_cgroup_uncharge_cgroup_rsvd(
6888 hstate_index(h),
6889 (chg - add) * pages_per_huge_page(h), h_cg);
6890
Mike Kravetz33039672015-06-24 16:57:58 -07006891 rsv_adjust = hugepage_subpool_put_pages(spool,
6892 chg - add);
6893 hugetlb_acct_memory(h, -rsv_adjust);
Miaohe Lind85aecf2021-03-24 21:37:17 -07006894 } else if (h_cg) {
6895 /*
6896 * The file_regions will hold their own reference to
6897 * h_cg->css. So we should release the reference held
6898 * via hugetlb_cgroup_charge_cgroup_rsvd() when we are
6899 * done.
6900 */
6901 hugetlb_cgroup_put_rsvd_cgroup(h_cg);
Mike Kravetz33039672015-06-24 16:57:58 -07006902 }
6903 }
Mike Kravetz33b8f842021-02-24 12:09:54 -08006904 return true;
6905
Mina Almasry075a61d2020-04-01 21:11:28 -07006906out_put_pages:
6907 /* put back original number of pages, chg */
6908 (void)hugepage_subpool_put_pages(spool, chg);
6909out_uncharge_cgroup:
6910 hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h),
6911 chg * pages_per_huge_page(h), h_cg);
Dave Hansenc50ac052012-05-29 15:06:46 -07006912out_err:
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07006913 hugetlb_vma_lock_free(vma);
Mike Kravetz5e911372015-09-08 15:01:28 -07006914 if (!vma || vma->vm_flags & VM_MAYSHARE)
Mina Almasry0db9d742020-04-01 21:11:25 -07006915 /* Only call region_abort if the region_chg succeeded but the
6916 * region_add failed or didn't run.
6917 */
6918 if (chg >= 0 && add < 0)
6919 region_abort(resv_map, from, to, regions_needed);
Joonsoo Kimf031dd22014-04-03 14:47:28 -07006920 if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
6921 kref_put(&resv_map->refs, resv_map_release);
Mike Kravetz33b8f842021-02-24 12:09:54 -08006922 return false;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07006923}
6924
Mike Kravetzb5cec282015-09-08 15:01:41 -07006925long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
6926 long freed)
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07006927{
Andi Kleena5516432008-07-23 21:27:41 -07006928 struct hstate *h = hstate_inode(inode);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07006929 struct resv_map *resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07006930 long chg = 0;
David Gibson90481622012-03-21 16:34:12 -07006931 struct hugepage_subpool *spool = subpool_inode(inode);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07006932 long gbl_reserve;
Ken Chen45c682a2007-11-14 16:59:44 -08006933
Mike Kravetzf27a5132019-05-13 17:22:55 -07006934 /*
6935 * Since this routine can be called in the evict inode path for all
6936 * hugetlbfs inodes, resv_map could be NULL.
6937 */
Mike Kravetzb5cec282015-09-08 15:01:41 -07006938 if (resv_map) {
6939 chg = region_del(resv_map, start, end);
6940 /*
6941 * region_del() can fail in the rare case where a region
6942 * must be split and another region descriptor can not be
6943 * allocated. If end == LONG_MAX, it will not fail.
6944 */
6945 if (chg < 0)
6946 return chg;
6947 }
6948
Ken Chen45c682a2007-11-14 16:59:44 -08006949 spin_lock(&inode->i_lock);
Eric Sandeene4c6f8b2009-07-29 15:02:16 -07006950 inode->i_blocks -= (blocks_per_huge_page(h) * freed);
Ken Chen45c682a2007-11-14 16:59:44 -08006951 spin_unlock(&inode->i_lock);
6952
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07006953 /*
6954 * If the subpool has a minimum size, the number of global
6955 * reservations to be released may be adjusted.
Miaohe Lindddf31a2021-05-04 18:34:35 -07006956 *
6957 * Note that !resv_map implies freed == 0. So (chg - freed)
6958 * won't go negative.
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07006959 */
6960 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
6961 hugetlb_acct_memory(h, -gbl_reserve);
Mike Kravetzb5cec282015-09-08 15:01:41 -07006962
6963 return 0;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07006964}
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09006965
Steve Capper3212b532013-04-23 12:35:02 +01006966#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
6967static unsigned long page_table_shareable(struct vm_area_struct *svma,
6968 struct vm_area_struct *vma,
6969 unsigned long addr, pgoff_t idx)
6970{
6971 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
6972 svma->vm_start;
6973 unsigned long sbase = saddr & PUD_MASK;
6974 unsigned long s_end = sbase + PUD_SIZE;
6975
6976 /* Allow segments to share if only one is marked locked */
Eric B Munsonde60f5f2015-11-05 18:51:36 -08006977 unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
6978 unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK;
Steve Capper3212b532013-04-23 12:35:02 +01006979
6980 /*
6981 * match the virtual addresses, permission and the alignment of the
6982 * page table page.
Mike Kravetz131a79b2022-10-04 18:17:05 -07006983 *
6984 * Also, vma_lock (vm_private_data) is required for sharing.
Steve Capper3212b532013-04-23 12:35:02 +01006985 */
6986 if (pmd_index(addr) != pmd_index(saddr) ||
6987 vm_flags != svm_flags ||
Mike Kravetz131a79b2022-10-04 18:17:05 -07006988 !range_in_vma(svma, sbase, s_end) ||
6989 !svma->vm_private_data)
Steve Capper3212b532013-04-23 12:35:02 +01006990 return 0;
6991
6992 return saddr;
6993}
6994
Mike Kravetzbbff39c2022-10-04 18:17:07 -07006995bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
Steve Capper3212b532013-04-23 12:35:02 +01006996{
Mike Kravetzbbff39c2022-10-04 18:17:07 -07006997 unsigned long start = addr & PUD_MASK;
6998 unsigned long end = start + PUD_SIZE;
6999
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07007000#ifdef CONFIG_USERFAULTFD
7001 if (uffd_disable_huge_pmd_share(vma))
7002 return false;
7003#endif
Steve Capper3212b532013-04-23 12:35:02 +01007004 /*
7005 * check on proper vm_flags and page table alignment
7006 */
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07007007 if (!(vma->vm_flags & VM_MAYSHARE))
7008 return false;
Mike Kravetzbbff39c2022-10-04 18:17:07 -07007009 if (!vma->vm_private_data) /* vma lock required for sharing */
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07007010 return false;
7011 if (!range_in_vma(vma, start, end))
7012 return false;
7013 return true;
7014}
7015
Steve Capper3212b532013-04-23 12:35:02 +01007016/*
Mike Kravetz017b1662018-10-05 15:51:29 -07007017 * Determine if start,end range within vma could be mapped by shared pmd.
7018 * If yes, adjust start and end to cover range associated with possible
7019 * shared pmd mappings.
7020 */
7021void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
7022 unsigned long *start, unsigned long *end)
7023{
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007024 unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE),
7025 v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE);
Mike Kravetz017b1662018-10-05 15:51:29 -07007026
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007027 /*
Ingo Molnarf0953a12021-05-06 18:06:47 -07007028 * vma needs to span at least one aligned PUD size, and the range
7029 * must be at least partially within in.
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007030 */
7031 if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) ||
7032 (*end <= v_start) || (*start >= v_end))
Mike Kravetz017b1662018-10-05 15:51:29 -07007033 return;
7034
Peter Xu75802ca62020-08-06 23:26:11 -07007035 /* Extend the range to be PUD aligned for a worst case scenario */
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007036 if (*start > v_start)
7037 *start = ALIGN_DOWN(*start, PUD_SIZE);
Mike Kravetz017b1662018-10-05 15:51:29 -07007038
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007039 if (*end < v_end)
7040 *end = ALIGN(*end, PUD_SIZE);
Mike Kravetz017b1662018-10-05 15:51:29 -07007041}
7042
7043/*
Steve Capper3212b532013-04-23 12:35:02 +01007044 * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
7045 * and returns the corresponding pte. While this is not necessary for the
7046 * !shared pmd case because we can allocate the pmd later as well, it makes the
Mike Kravetz3a47c542022-09-14 15:18:03 -07007047 * code much cleaner. pmd allocation is essential for the shared case because
7048 * pud has to be populated inside the same i_mmap_rwsem section - otherwise
7049 * racing tasks could either miss the sharing (see huge_pte_offset) or select a
7050 * bad pmd for sharing.
Steve Capper3212b532013-04-23 12:35:02 +01007051 */
Peter Xuaec44e02021-05-04 18:33:00 -07007052pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
7053 unsigned long addr, pud_t *pud)
Steve Capper3212b532013-04-23 12:35:02 +01007054{
Steve Capper3212b532013-04-23 12:35:02 +01007055 struct address_space *mapping = vma->vm_file->f_mapping;
7056 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
7057 vma->vm_pgoff;
7058 struct vm_area_struct *svma;
7059 unsigned long saddr;
7060 pte_t *spte = NULL;
7061 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08007062 spinlock_t *ptl;
Steve Capper3212b532013-04-23 12:35:02 +01007063
Mike Kravetz3a47c542022-09-14 15:18:03 -07007064 i_mmap_lock_read(mapping);
Steve Capper3212b532013-04-23 12:35:02 +01007065 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
7066 if (svma == vma)
7067 continue;
7068
7069 saddr = page_table_shareable(svma, vma, addr, idx);
7070 if (saddr) {
Punit Agrawal7868a202017-07-06 15:39:42 -07007071 spte = huge_pte_offset(svma->vm_mm, saddr,
7072 vma_mmu_pagesize(svma));
Steve Capper3212b532013-04-23 12:35:02 +01007073 if (spte) {
7074 get_page(virt_to_page(spte));
7075 break;
7076 }
7077 }
7078 }
7079
7080 if (!spte)
7081 goto out;
7082
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08007083 ptl = huge_pte_lock(hstate_vma(vma), mm, spte);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08007084 if (pud_none(*pud)) {
Steve Capper3212b532013-04-23 12:35:02 +01007085 pud_populate(mm, pud,
7086 (pmd_t *)((unsigned long)spte & PAGE_MASK));
Kirill A. Shutemovc17b1f42016-06-24 14:49:51 -07007087 mm_inc_nr_pmds(mm);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08007088 } else {
Steve Capper3212b532013-04-23 12:35:02 +01007089 put_page(virt_to_page(spte));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08007090 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08007091 spin_unlock(ptl);
Steve Capper3212b532013-04-23 12:35:02 +01007092out:
7093 pte = (pte_t *)pmd_alloc(mm, pud, addr);
Mike Kravetz3a47c542022-09-14 15:18:03 -07007094 i_mmap_unlock_read(mapping);
Steve Capper3212b532013-04-23 12:35:02 +01007095 return pte;
7096}
7097
7098/*
7099 * unmap huge page backed by shared pte.
7100 *
7101 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
7102 * indicated by page_count > 1, unmap is achieved by clearing pud and
7103 * decrementing the ref count. If count == 1, the pte page is not shared.
7104 *
Mike Kravetz3a47c542022-09-14 15:18:03 -07007105 * Called with page table lock held.
Steve Capper3212b532013-04-23 12:35:02 +01007106 *
7107 * returns: 1 successfully unmapped a shared pte page
7108 * 0 the underlying pte page is not shared, or it is the last user
7109 */
Mike Kravetz34ae2042020-08-11 18:31:38 -07007110int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007111 unsigned long addr, pte_t *ptep)
Steve Capper3212b532013-04-23 12:35:02 +01007112{
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007113 pgd_t *pgd = pgd_offset(mm, addr);
7114 p4d_t *p4d = p4d_offset(pgd, addr);
7115 pud_t *pud = pud_offset(p4d, addr);
Steve Capper3212b532013-04-23 12:35:02 +01007116
Mike Kravetz34ae2042020-08-11 18:31:38 -07007117 i_mmap_assert_write_locked(vma->vm_file->f_mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07007118 hugetlb_vma_assert_locked(vma);
Steve Capper3212b532013-04-23 12:35:02 +01007119 BUG_ON(page_count(virt_to_page(ptep)) == 0);
7120 if (page_count(virt_to_page(ptep)) == 1)
7121 return 0;
7122
7123 pud_clear(pud);
7124 put_page(virt_to_page(ptep));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08007125 mm_dec_nr_pmds(mm);
Steve Capper3212b532013-04-23 12:35:02 +01007126 return 1;
7127}
Peter Xuc1991e02021-05-04 18:33:04 -07007128
Steve Capper9e5fc742013-04-30 08:02:03 +01007129#else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07007130
Peter Xuaec44e02021-05-04 18:33:00 -07007131pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
7132 unsigned long addr, pud_t *pud)
Steve Capper9e5fc742013-04-30 08:02:03 +01007133{
7134 return NULL;
7135}
Zhang Zhene81f2d22015-06-24 16:56:13 -07007136
Mike Kravetz34ae2042020-08-11 18:31:38 -07007137int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007138 unsigned long addr, pte_t *ptep)
Zhang Zhene81f2d22015-06-24 16:56:13 -07007139{
7140 return 0;
7141}
Mike Kravetz017b1662018-10-05 15:51:29 -07007142
7143void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
7144 unsigned long *start, unsigned long *end)
7145{
7146}
Peter Xuc1991e02021-05-04 18:33:04 -07007147
7148bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
7149{
7150 return false;
7151}
Steve Capper3212b532013-04-23 12:35:02 +01007152#endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
7153
Steve Capper9e5fc742013-04-30 08:02:03 +01007154#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
Peter Xuaec44e02021-05-04 18:33:00 -07007155pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
Steve Capper9e5fc742013-04-30 08:02:03 +01007156 unsigned long addr, unsigned long sz)
7157{
7158 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007159 p4d_t *p4d;
Steve Capper9e5fc742013-04-30 08:02:03 +01007160 pud_t *pud;
7161 pte_t *pte = NULL;
7162
7163 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovf4f0a3d2017-11-29 16:11:30 -08007164 p4d = p4d_alloc(mm, pgd, addr);
7165 if (!p4d)
7166 return NULL;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007167 pud = pud_alloc(mm, p4d, addr);
Steve Capper9e5fc742013-04-30 08:02:03 +01007168 if (pud) {
7169 if (sz == PUD_SIZE) {
7170 pte = (pte_t *)pud;
7171 } else {
7172 BUG_ON(sz != PMD_SIZE);
Peter Xuc1991e02021-05-04 18:33:04 -07007173 if (want_pmd_share(vma, addr) && pud_none(*pud))
Peter Xuaec44e02021-05-04 18:33:00 -07007174 pte = huge_pmd_share(mm, vma, addr, pud);
Steve Capper9e5fc742013-04-30 08:02:03 +01007175 else
7176 pte = (pte_t *)pmd_alloc(mm, pud, addr);
7177 }
7178 }
Michal Hocko4e666312016-08-02 14:02:34 -07007179 BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte));
Steve Capper9e5fc742013-04-30 08:02:03 +01007180
7181 return pte;
7182}
7183
Punit Agrawal9b19df22017-09-06 16:21:01 -07007184/*
7185 * huge_pte_offset() - Walk the page table to resolve the hugepage
7186 * entry at address @addr
7187 *
Li Xinhai8ac0b812020-06-03 16:00:53 -07007188 * Return: Pointer to page table entry (PUD or PMD) for
7189 * address @addr, or NULL if a !p*d_present() entry is encountered and the
Punit Agrawal9b19df22017-09-06 16:21:01 -07007190 * size @sz doesn't match the hugepage size at this level of the page
7191 * table.
7192 */
Punit Agrawal7868a202017-07-06 15:39:42 -07007193pte_t *huge_pte_offset(struct mm_struct *mm,
7194 unsigned long addr, unsigned long sz)
Steve Capper9e5fc742013-04-30 08:02:03 +01007195{
7196 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007197 p4d_t *p4d;
Li Xinhai8ac0b812020-06-03 16:00:53 -07007198 pud_t *pud;
7199 pmd_t *pmd;
Steve Capper9e5fc742013-04-30 08:02:03 +01007200
7201 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007202 if (!pgd_present(*pgd))
7203 return NULL;
7204 p4d = p4d_offset(pgd, addr);
7205 if (!p4d_present(*p4d))
7206 return NULL;
Punit Agrawal9b19df22017-09-06 16:21:01 -07007207
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007208 pud = pud_offset(p4d, addr);
Li Xinhai8ac0b812020-06-03 16:00:53 -07007209 if (sz == PUD_SIZE)
7210 /* must be pud huge, non-present or none */
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007211 return (pte_t *)pud;
Li Xinhai8ac0b812020-06-03 16:00:53 -07007212 if (!pud_present(*pud))
7213 return NULL;
7214 /* must have a valid entry and size to go further */
Punit Agrawal9b19df22017-09-06 16:21:01 -07007215
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007216 pmd = pmd_offset(pud, addr);
Li Xinhai8ac0b812020-06-03 16:00:53 -07007217 /* must be pmd huge, non-present or none */
7218 return (pte_t *)pmd;
Steve Capper9e5fc742013-04-30 08:02:03 +01007219}
7220
Mike Kravetze95a9852022-06-21 16:56:17 -07007221/*
7222 * Return a mask that can be used to update an address to the last huge
7223 * page in a page table page mapping size. Used to skip non-present
7224 * page table entries when linearly scanning address ranges. Architectures
7225 * with unique huge page to page table relationships can define their own
7226 * version of this routine.
7227 */
7228unsigned long hugetlb_mask_last_page(struct hstate *h)
7229{
7230 unsigned long hp_size = huge_page_size(h);
7231
7232 if (hp_size == PUD_SIZE)
7233 return P4D_SIZE - PUD_SIZE;
7234 else if (hp_size == PMD_SIZE)
7235 return PUD_SIZE - PMD_SIZE;
7236 else
7237 return 0UL;
7238}
7239
7240#else
7241
7242/* See description above. Architectures can provide their own version. */
7243__weak unsigned long hugetlb_mask_last_page(struct hstate *h)
7244{
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007245#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
7246 if (huge_page_size(h) == PMD_SIZE)
7247 return PUD_SIZE - PMD_SIZE;
7248#endif
Mike Kravetze95a9852022-06-21 16:56:17 -07007249 return 0UL;
7250}
7251
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08007252#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
7253
7254/*
7255 * These functions are overwritable if your architecture needs its own
7256 * behavior.
7257 */
Miaohe Lin7ce82f42022-05-30 19:30:15 +08007258int isolate_hugetlb(struct page *page, struct list_head *list)
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007259{
Miaohe Lin7ce82f42022-05-30 19:30:15 +08007260 int ret = 0;
Naoya Horiguchibcc54222015-04-15 16:14:38 -07007261
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007262 spin_lock_irq(&hugetlb_lock);
Mike Kravetz8f251a32021-02-24 12:08:56 -08007263 if (!PageHeadHuge(page) ||
7264 !HPageMigratable(page) ||
Muchun Song0eb2df22021-02-04 18:32:10 -08007265 !get_page_unless_zero(page)) {
Miaohe Lin7ce82f42022-05-30 19:30:15 +08007266 ret = -EBUSY;
Naoya Horiguchibcc54222015-04-15 16:14:38 -07007267 goto unlock;
7268 }
Mike Kravetz8f251a32021-02-24 12:08:56 -08007269 ClearHPageMigratable(page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007270 list_move_tail(&page->lru, list);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07007271unlock:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007272 spin_unlock_irq(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07007273 return ret;
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007274}
7275
Naoya Horiguchie591ef72022-10-24 15:20:09 +09007276int get_hwpoison_huge_page(struct page *page, bool *hugetlb, bool unpoison)
Naoya Horiguchi25182f02021-06-15 18:23:13 -07007277{
7278 int ret = 0;
7279
7280 *hugetlb = false;
7281 spin_lock_irq(&hugetlb_lock);
7282 if (PageHeadHuge(page)) {
7283 *hugetlb = true;
Naoya Horiguchib283d9832022-04-28 23:16:02 -07007284 if (HPageFreed(page))
7285 ret = 0;
Naoya Horiguchie591ef72022-10-24 15:20:09 +09007286 else if (HPageMigratable(page) || unpoison)
Naoya Horiguchi25182f02021-06-15 18:23:13 -07007287 ret = get_page_unless_zero(page);
Naoya Horiguchi0ed950d2021-06-28 19:43:17 -07007288 else
7289 ret = -EBUSY;
Naoya Horiguchi25182f02021-06-15 18:23:13 -07007290 }
7291 spin_unlock_irq(&hugetlb_lock);
7292 return ret;
7293}
7294
Naoya Horiguchie591ef72022-10-24 15:20:09 +09007295int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
7296 bool *migratable_cleared)
Naoya Horiguchi405ce052022-04-21 16:35:33 -07007297{
7298 int ret;
7299
7300 spin_lock_irq(&hugetlb_lock);
Naoya Horiguchie591ef72022-10-24 15:20:09 +09007301 ret = __get_huge_page_for_hwpoison(pfn, flags, migratable_cleared);
Naoya Horiguchi405ce052022-04-21 16:35:33 -07007302 spin_unlock_irq(&hugetlb_lock);
7303 return ret;
7304}
7305
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007306void putback_active_hugepage(struct page *page)
7307{
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007308 spin_lock_irq(&hugetlb_lock);
Mike Kravetz8f251a32021-02-24 12:08:56 -08007309 SetHPageMigratable(page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007310 list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007311 spin_unlock_irq(&hugetlb_lock);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007312 put_page(page);
7313}
Michal Hockoab5ac902018-01-31 16:20:48 -08007314
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007315void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason)
Michal Hockoab5ac902018-01-31 16:20:48 -08007316{
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007317 struct hstate *h = folio_hstate(old_folio);
Michal Hockoab5ac902018-01-31 16:20:48 -08007318
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007319 hugetlb_cgroup_migrate(old_folio, new_folio);
7320 set_page_owner_migrate_reason(&new_folio->page, reason);
Michal Hockoab5ac902018-01-31 16:20:48 -08007321
7322 /*
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007323 * transfer temporary state of the new hugetlb folio. This is
Michal Hockoab5ac902018-01-31 16:20:48 -08007324 * reverse to other transitions because the newpage is going to
7325 * be final while the old one will be freed so it takes over
7326 * the temporary status.
7327 *
7328 * Also note that we have to transfer the per-node surplus state
7329 * here as well otherwise the global surplus count will not match
7330 * the per-node's.
7331 */
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007332 if (folio_test_hugetlb_temporary(new_folio)) {
7333 int old_nid = folio_nid(old_folio);
7334 int new_nid = folio_nid(new_folio);
Michal Hockoab5ac902018-01-31 16:20:48 -08007335
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007336 folio_set_hugetlb_temporary(old_folio);
7337 folio_clear_hugetlb_temporary(new_folio);
7338
Michal Hockoab5ac902018-01-31 16:20:48 -08007339
Miaohe Lin5af1ab12021-05-04 18:33:25 -07007340 /*
7341 * There is no need to transfer the per-node surplus state
7342 * when we do not cross the node.
7343 */
7344 if (new_nid == old_nid)
7345 return;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007346 spin_lock_irq(&hugetlb_lock);
Michal Hockoab5ac902018-01-31 16:20:48 -08007347 if (h->surplus_huge_pages_node[old_nid]) {
7348 h->surplus_huge_pages_node[old_nid]--;
7349 h->surplus_huge_pages_node[new_nid]++;
7350 }
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007351 spin_unlock_irq(&hugetlb_lock);
Michal Hockoab5ac902018-01-31 16:20:48 -08007352 }
7353}
Roman Gushchincf11e852020-04-10 14:32:45 -07007354
James Houghtonb30c14c2023-01-04 23:19:10 +00007355static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
7356 unsigned long start,
7357 unsigned long end)
Peter Xu6dfeaff2021-05-04 18:33:13 -07007358{
7359 struct hstate *h = hstate_vma(vma);
7360 unsigned long sz = huge_page_size(h);
7361 struct mm_struct *mm = vma->vm_mm;
7362 struct mmu_notifier_range range;
James Houghtonb30c14c2023-01-04 23:19:10 +00007363 unsigned long address;
Peter Xu6dfeaff2021-05-04 18:33:13 -07007364 spinlock_t *ptl;
7365 pte_t *ptep;
7366
7367 if (!(vma->vm_flags & VM_MAYSHARE))
7368 return;
7369
Peter Xu6dfeaff2021-05-04 18:33:13 -07007370 if (start >= end)
7371 return;
7372
Baolin Wang9c8bbfa2022-04-29 14:36:58 -07007373 flush_cache_range(vma, start, end);
Peter Xu6dfeaff2021-05-04 18:33:13 -07007374 /*
7375 * No need to call adjust_range_if_pmd_sharing_possible(), because
7376 * we have already done the PUD_SIZE alignment.
7377 */
7378 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
7379 start, end);
7380 mmu_notifier_invalidate_range_start(&range);
Mike Kravetz40549ba2022-09-14 15:18:09 -07007381 hugetlb_vma_lock_write(vma);
Peter Xu6dfeaff2021-05-04 18:33:13 -07007382 i_mmap_lock_write(vma->vm_file->f_mapping);
7383 for (address = start; address < end; address += PUD_SIZE) {
Peter Xu6dfeaff2021-05-04 18:33:13 -07007384 ptep = huge_pte_offset(mm, address, sz);
7385 if (!ptep)
7386 continue;
7387 ptl = huge_pte_lock(h, mm, ptep);
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007388 huge_pmd_unshare(mm, vma, address, ptep);
Peter Xu6dfeaff2021-05-04 18:33:13 -07007389 spin_unlock(ptl);
7390 }
7391 flush_hugetlb_tlb_range(vma, start, end);
7392 i_mmap_unlock_write(vma->vm_file->f_mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07007393 hugetlb_vma_unlock_write(vma);
Peter Xu6dfeaff2021-05-04 18:33:13 -07007394 /*
7395 * No need to call mmu_notifier_invalidate_range(), see
Mike Rapoportee657282022-06-27 09:00:26 +03007396 * Documentation/mm/mmu_notifier.rst.
Peter Xu6dfeaff2021-05-04 18:33:13 -07007397 */
7398 mmu_notifier_invalidate_range_end(&range);
7399}
7400
James Houghtonb30c14c2023-01-04 23:19:10 +00007401/*
7402 * This function will unconditionally remove all the shared pmd pgtable entries
7403 * within the specific vma for a hugetlbfs memory range.
7404 */
7405void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)
7406{
7407 hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE),
7408 ALIGN_DOWN(vma->vm_end, PUD_SIZE));
7409}
7410
Roman Gushchincf11e852020-04-10 14:32:45 -07007411#ifdef CONFIG_CMA
Roman Gushchincf11e852020-04-10 14:32:45 -07007412static bool cma_reserve_called __initdata;
7413
7414static int __init cmdline_parse_hugetlb_cma(char *p)
7415{
Baolin Wang38e719a2021-11-05 13:41:46 -07007416 int nid, count = 0;
7417 unsigned long tmp;
7418 char *s = p;
7419
7420 while (*s) {
7421 if (sscanf(s, "%lu%n", &tmp, &count) != 1)
7422 break;
7423
7424 if (s[count] == ':') {
Mike Kravetzf9317f72022-03-22 14:45:20 -07007425 if (tmp >= MAX_NUMNODES)
Baolin Wang38e719a2021-11-05 13:41:46 -07007426 break;
Mike Kravetzf9317f72022-03-22 14:45:20 -07007427 nid = array_index_nospec(tmp, MAX_NUMNODES);
Baolin Wang38e719a2021-11-05 13:41:46 -07007428
7429 s += count + 1;
7430 tmp = memparse(s, &s);
7431 hugetlb_cma_size_in_node[nid] = tmp;
7432 hugetlb_cma_size += tmp;
7433
7434 /*
7435 * Skip the separator if have one, otherwise
7436 * break the parsing.
7437 */
7438 if (*s == ',')
7439 s++;
7440 else
7441 break;
7442 } else {
7443 hugetlb_cma_size = memparse(p, &p);
7444 break;
7445 }
7446 }
7447
Roman Gushchincf11e852020-04-10 14:32:45 -07007448 return 0;
7449}
7450
7451early_param("hugetlb_cma", cmdline_parse_hugetlb_cma);
7452
7453void __init hugetlb_cma_reserve(int order)
7454{
7455 unsigned long size, reserved, per_node;
Baolin Wang38e719a2021-11-05 13:41:46 -07007456 bool node_specific_cma_alloc = false;
Roman Gushchincf11e852020-04-10 14:32:45 -07007457 int nid;
7458
7459 cma_reserve_called = true;
7460
7461 if (!hugetlb_cma_size)
7462 return;
7463
Baolin Wang38e719a2021-11-05 13:41:46 -07007464 for (nid = 0; nid < MAX_NUMNODES; nid++) {
7465 if (hugetlb_cma_size_in_node[nid] == 0)
7466 continue;
7467
Peng Liu30a51402022-04-29 14:36:58 -07007468 if (!node_online(nid)) {
Baolin Wang38e719a2021-11-05 13:41:46 -07007469 pr_warn("hugetlb_cma: invalid node %d specified\n", nid);
7470 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid];
7471 hugetlb_cma_size_in_node[nid] = 0;
7472 continue;
7473 }
7474
7475 if (hugetlb_cma_size_in_node[nid] < (PAGE_SIZE << order)) {
7476 pr_warn("hugetlb_cma: cma area of node %d should be at least %lu MiB\n",
7477 nid, (PAGE_SIZE << order) / SZ_1M);
7478 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid];
7479 hugetlb_cma_size_in_node[nid] = 0;
7480 } else {
7481 node_specific_cma_alloc = true;
7482 }
7483 }
7484
7485 /* Validate the CMA size again in case some invalid nodes specified. */
7486 if (!hugetlb_cma_size)
7487 return;
7488
Roman Gushchincf11e852020-04-10 14:32:45 -07007489 if (hugetlb_cma_size < (PAGE_SIZE << order)) {
7490 pr_warn("hugetlb_cma: cma area should be at least %lu MiB\n",
7491 (PAGE_SIZE << order) / SZ_1M);
Mike Kravetza01f4392021-11-05 13:41:27 -07007492 hugetlb_cma_size = 0;
Roman Gushchincf11e852020-04-10 14:32:45 -07007493 return;
7494 }
7495
Baolin Wang38e719a2021-11-05 13:41:46 -07007496 if (!node_specific_cma_alloc) {
7497 /*
7498 * If 3 GB area is requested on a machine with 4 numa nodes,
7499 * let's allocate 1 GB on first three nodes and ignore the last one.
7500 */
7501 per_node = DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes);
7502 pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n",
7503 hugetlb_cma_size / SZ_1M, per_node / SZ_1M);
7504 }
Roman Gushchincf11e852020-04-10 14:32:45 -07007505
7506 reserved = 0;
Peng Liu30a51402022-04-29 14:36:58 -07007507 for_each_online_node(nid) {
Roman Gushchincf11e852020-04-10 14:32:45 -07007508 int res;
Barry Song2281f792020-08-24 11:03:09 +12007509 char name[CMA_MAX_NAME];
Roman Gushchincf11e852020-04-10 14:32:45 -07007510
Baolin Wang38e719a2021-11-05 13:41:46 -07007511 if (node_specific_cma_alloc) {
7512 if (hugetlb_cma_size_in_node[nid] == 0)
7513 continue;
7514
7515 size = hugetlb_cma_size_in_node[nid];
7516 } else {
7517 size = min(per_node, hugetlb_cma_size - reserved);
7518 }
7519
Roman Gushchincf11e852020-04-10 14:32:45 -07007520 size = round_up(size, PAGE_SIZE << order);
7521
Barry Song2281f792020-08-24 11:03:09 +12007522 snprintf(name, sizeof(name), "hugetlb%d", nid);
Mike Kravetza01f4392021-11-05 13:41:27 -07007523 /*
7524 * Note that 'order per bit' is based on smallest size that
7525 * may be returned to CMA allocator in the case of
7526 * huge page demotion.
7527 */
7528 res = cma_declare_contiguous_nid(0, size, 0,
7529 PAGE_SIZE << HUGETLB_PAGE_ORDER,
Barry Song29d0f412020-08-11 18:32:00 -07007530 0, false, name,
Roman Gushchincf11e852020-04-10 14:32:45 -07007531 &hugetlb_cma[nid], nid);
7532 if (res) {
7533 pr_warn("hugetlb_cma: reservation failed: err %d, node %d",
7534 res, nid);
7535 continue;
7536 }
7537
7538 reserved += size;
7539 pr_info("hugetlb_cma: reserved %lu MiB on node %d\n",
7540 size / SZ_1M, nid);
7541
7542 if (reserved >= hugetlb_cma_size)
7543 break;
7544 }
Mike Kravetza01f4392021-11-05 13:41:27 -07007545
7546 if (!reserved)
7547 /*
7548 * hugetlb_cma_size is used to determine if allocations from
7549 * cma are possible. Set to zero if no cma regions are set up.
7550 */
7551 hugetlb_cma_size = 0;
Roman Gushchincf11e852020-04-10 14:32:45 -07007552}
7553
Miaohe Lin263b8992022-09-01 20:00:21 +08007554static void __init hugetlb_cma_check(void)
Roman Gushchincf11e852020-04-10 14:32:45 -07007555{
7556 if (!hugetlb_cma_size || cma_reserve_called)
7557 return;
7558
7559 pr_warn("hugetlb_cma: the option isn't supported by current arch\n");
7560}
7561
7562#endif /* CONFIG_CMA */