blob: 245038a9fe4eaa27e51ad83ba0abf4cb2c403deb [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 */
Mike Kravetze7008982022-12-12 15:50:41 -0800263void hugetlb_vma_lock_read(struct vm_area_struct *vma)
264{
265 if (__vma_shareable_lock(vma)) {
266 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
267
268 down_read(&vma_lock->rw_sema);
269 }
270}
271
272void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
273{
274 if (__vma_shareable_lock(vma)) {
275 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
276
277 up_read(&vma_lock->rw_sema);
278 }
279}
280
281void hugetlb_vma_lock_write(struct vm_area_struct *vma)
282{
283 if (__vma_shareable_lock(vma)) {
284 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
285
286 down_write(&vma_lock->rw_sema);
287 }
288}
289
290void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
291{
292 if (__vma_shareable_lock(vma)) {
293 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
294
295 up_write(&vma_lock->rw_sema);
296 }
297}
298
299int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
300{
301 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
302
303 if (!__vma_shareable_lock(vma))
304 return 1;
305
306 return down_write_trylock(&vma_lock->rw_sema);
307}
308
309void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
310{
311 if (__vma_shareable_lock(vma)) {
312 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
313
314 lockdep_assert_held(&vma_lock->rw_sema);
315 }
316}
317
318void hugetlb_vma_lock_release(struct kref *kref)
319{
320 struct hugetlb_vma_lock *vma_lock = container_of(kref,
321 struct hugetlb_vma_lock, refs);
322
323 kfree(vma_lock);
324}
325
326static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock)
327{
328 struct vm_area_struct *vma = vma_lock->vma;
329
330 /*
331 * vma_lock structure may or not be released as a result of put,
332 * it certainly will no longer be attached to vma so clear pointer.
333 * Semaphore synchronizes access to vma_lock->vma field.
334 */
335 vma_lock->vma = NULL;
336 vma->vm_private_data = NULL;
337 up_write(&vma_lock->rw_sema);
338 kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
339}
340
341static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)
342{
343 if (__vma_shareable_lock(vma)) {
344 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
345
346 __hugetlb_vma_unlock_write_put(vma_lock);
347 }
348}
349
350static void hugetlb_vma_lock_free(struct vm_area_struct *vma)
351{
352 /*
353 * Only present in sharable vmas.
354 */
355 if (!vma || !__vma_shareable_lock(vma))
356 return;
357
358 if (vma->vm_private_data) {
359 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
360
361 down_write(&vma_lock->rw_sema);
362 __hugetlb_vma_unlock_write_put(vma_lock);
363 }
364}
365
366static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma)
367{
368 struct hugetlb_vma_lock *vma_lock;
369
370 /* Only establish in (flags) sharable vmas */
371 if (!vma || !(vma->vm_flags & VM_MAYSHARE))
372 return;
373
374 /* Should never get here with non-NULL vm_private_data */
375 if (vma->vm_private_data)
376 return;
377
378 vma_lock = kmalloc(sizeof(*vma_lock), GFP_KERNEL);
379 if (!vma_lock) {
380 /*
381 * If we can not allocate structure, then vma can not
382 * participate in pmd sharing. This is only a possible
383 * performance enhancement and memory saving issue.
384 * However, the lock is also used to synchronize page
385 * faults with truncation. If the lock is not present,
386 * unlikely races could leave pages in a file past i_size
387 * until the file is removed. Warn in the unlikely case of
388 * allocation failure.
389 */
390 pr_warn_once("HugeTLB: unable to allocate vma specific lock\n");
391 return;
392 }
393
394 kref_init(&vma_lock->refs);
395 init_rwsem(&vma_lock->rw_sema);
396 vma_lock->vma = vma;
397 vma->vm_private_data = vma_lock;
398}
399
Mina Almasry0db9d742020-04-01 21:11:25 -0700400/* Helper that removes a struct file_region from the resv_map cache and returns
401 * it for use.
402 */
403static struct file_region *
404get_file_region_entry_from_cache(struct resv_map *resv, long from, long to)
405{
XU pengfei32599142022-09-14 09:21:14 +0800406 struct file_region *nrg;
Mina Almasry0db9d742020-04-01 21:11:25 -0700407
408 VM_BUG_ON(resv->region_cache_count <= 0);
409
410 resv->region_cache_count--;
411 nrg = list_first_entry(&resv->region_cache, struct file_region, link);
Mina Almasry0db9d742020-04-01 21:11:25 -0700412 list_del(&nrg->link);
413
414 nrg->from = from;
415 nrg->to = to;
416
417 return nrg;
418}
419
Mina Almasry075a61d2020-04-01 21:11:28 -0700420static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg,
421 struct file_region *rg)
422{
423#ifdef CONFIG_CGROUP_HUGETLB
424 nrg->reservation_counter = rg->reservation_counter;
425 nrg->css = rg->css;
426 if (rg->css)
427 css_get(rg->css);
428#endif
429}
430
431/* Helper that records hugetlb_cgroup uncharge info. */
432static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,
433 struct hstate *h,
434 struct resv_map *resv,
435 struct file_region *nrg)
436{
437#ifdef CONFIG_CGROUP_HUGETLB
438 if (h_cg) {
439 nrg->reservation_counter =
440 &h_cg->rsvd_hugepage[hstate_index(h)];
441 nrg->css = &h_cg->css;
Miaohe Lind85aecf2021-03-24 21:37:17 -0700442 /*
443 * The caller will hold exactly one h_cg->css reference for the
444 * whole contiguous reservation region. But this area might be
445 * scattered when there are already some file_regions reside in
446 * it. As a result, many file_regions may share only one css
447 * reference. In order to ensure that one file_region must hold
448 * exactly one h_cg->css reference, we should do css_get for
449 * each file_region and leave the reference held by caller
450 * untouched.
451 */
452 css_get(&h_cg->css);
Mina Almasry075a61d2020-04-01 21:11:28 -0700453 if (!resv->pages_per_hpage)
454 resv->pages_per_hpage = pages_per_huge_page(h);
455 /* pages_per_hpage should be the same for all entries in
456 * a resv_map.
457 */
458 VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h));
459 } else {
460 nrg->reservation_counter = NULL;
461 nrg->css = NULL;
462 }
463#endif
464}
465
Miaohe Lind85aecf2021-03-24 21:37:17 -0700466static void put_uncharge_info(struct file_region *rg)
467{
468#ifdef CONFIG_CGROUP_HUGETLB
469 if (rg->css)
470 css_put(rg->css);
471#endif
472}
473
Mina Almasrya9b3f862020-04-01 21:11:35 -0700474static bool has_same_uncharge_info(struct file_region *rg,
475 struct file_region *org)
476{
477#ifdef CONFIG_CGROUP_HUGETLB
Baolin Wang0739eb42021-11-05 13:41:58 -0700478 return rg->reservation_counter == org->reservation_counter &&
Mina Almasrya9b3f862020-04-01 21:11:35 -0700479 rg->css == org->css;
480
481#else
482 return true;
483#endif
484}
485
486static void coalesce_file_region(struct resv_map *resv, struct file_region *rg)
487{
XU pengfei32599142022-09-14 09:21:14 +0800488 struct file_region *nrg, *prg;
Mina Almasrya9b3f862020-04-01 21:11:35 -0700489
490 prg = list_prev_entry(rg, link);
491 if (&prg->link != &resv->regions && prg->to == rg->from &&
492 has_same_uncharge_info(prg, rg)) {
493 prg->to = rg->to;
494
495 list_del(&rg->link);
Miaohe Lind85aecf2021-03-24 21:37:17 -0700496 put_uncharge_info(rg);
Mina Almasrya9b3f862020-04-01 21:11:35 -0700497 kfree(rg);
498
Wei Yang7db5e7b2020-10-13 16:56:20 -0700499 rg = prg;
Mina Almasrya9b3f862020-04-01 21:11:35 -0700500 }
501
502 nrg = list_next_entry(rg, link);
503 if (&nrg->link != &resv->regions && nrg->from == rg->to &&
504 has_same_uncharge_info(nrg, rg)) {
505 nrg->from = rg->from;
506
507 list_del(&rg->link);
Miaohe Lind85aecf2021-03-24 21:37:17 -0700508 put_uncharge_info(rg);
Mina Almasrya9b3f862020-04-01 21:11:35 -0700509 kfree(rg);
Mina Almasrya9b3f862020-04-01 21:11:35 -0700510 }
511}
512
Peter Xu2103cf92021-03-12 21:07:18 -0800513static inline long
Jakob Koschel84448c82022-04-28 23:16:03 -0700514hugetlb_resv_map_add(struct resv_map *map, struct list_head *rg, long from,
Peter Xu2103cf92021-03-12 21:07:18 -0800515 long to, struct hstate *h, struct hugetlb_cgroup *cg,
516 long *regions_needed)
517{
518 struct file_region *nrg;
519
520 if (!regions_needed) {
521 nrg = get_file_region_entry_from_cache(map, from, to);
522 record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg);
Jakob Koschel84448c82022-04-28 23:16:03 -0700523 list_add(&nrg->link, rg);
Peter Xu2103cf92021-03-12 21:07:18 -0800524 coalesce_file_region(map, nrg);
525 } else
526 *regions_needed += 1;
527
528 return to - from;
529}
530
Wei Yang972a3da32020-10-13 16:56:30 -0700531/*
532 * Must be called with resv->lock held.
533 *
534 * Calling this with regions_needed != NULL will count the number of pages
535 * to be added but will not modify the linked list. And regions_needed will
536 * indicate the number of file_regions needed in the cache to carry out to add
537 * the regions for this range.
Mina Almasryd75c6af2019-11-30 17:56:59 -0800538 */
539static long add_reservation_in_range(struct resv_map *resv, long f, long t,
Mina Almasry075a61d2020-04-01 21:11:28 -0700540 struct hugetlb_cgroup *h_cg,
Wei Yang972a3da32020-10-13 16:56:30 -0700541 struct hstate *h, long *regions_needed)
Mina Almasryd75c6af2019-11-30 17:56:59 -0800542{
Mina Almasry0db9d742020-04-01 21:11:25 -0700543 long add = 0;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800544 struct list_head *head = &resv->regions;
Mina Almasry0db9d742020-04-01 21:11:25 -0700545 long last_accounted_offset = f;
Jakob Koschel84448c82022-04-28 23:16:03 -0700546 struct file_region *iter, *trg = NULL;
547 struct list_head *rg = NULL;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800548
Mina Almasry0db9d742020-04-01 21:11:25 -0700549 if (regions_needed)
550 *regions_needed = 0;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800551
Mina Almasry0db9d742020-04-01 21:11:25 -0700552 /* In this loop, we essentially handle an entry for the range
Jakob Koschel84448c82022-04-28 23:16:03 -0700553 * [last_accounted_offset, iter->from), at every iteration, with some
Mina Almasry0db9d742020-04-01 21:11:25 -0700554 * bounds checking.
555 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700556 list_for_each_entry_safe(iter, trg, head, link) {
Mina Almasry0db9d742020-04-01 21:11:25 -0700557 /* Skip irrelevant regions that start before our range. */
Jakob Koschel84448c82022-04-28 23:16:03 -0700558 if (iter->from < f) {
Mina Almasry0db9d742020-04-01 21:11:25 -0700559 /* If this region ends after the last accounted offset,
560 * then we need to update last_accounted_offset.
561 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700562 if (iter->to > last_accounted_offset)
563 last_accounted_offset = iter->to;
Mina Almasry0db9d742020-04-01 21:11:25 -0700564 continue;
565 }
Mina Almasryd75c6af2019-11-30 17:56:59 -0800566
Mina Almasry0db9d742020-04-01 21:11:25 -0700567 /* When we find a region that starts beyond our range, we've
568 * finished.
569 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700570 if (iter->from >= t) {
571 rg = iter->link.prev;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800572 break;
Jakob Koschel84448c82022-04-28 23:16:03 -0700573 }
Mina Almasryd75c6af2019-11-30 17:56:59 -0800574
Jakob Koschel84448c82022-04-28 23:16:03 -0700575 /* Add an entry for last_accounted_offset -> iter->from, and
Mina Almasry0db9d742020-04-01 21:11:25 -0700576 * update last_accounted_offset.
Mina Almasryd75c6af2019-11-30 17:56:59 -0800577 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700578 if (iter->from > last_accounted_offset)
579 add += hugetlb_resv_map_add(resv, iter->link.prev,
Peter Xu2103cf92021-03-12 21:07:18 -0800580 last_accounted_offset,
Jakob Koschel84448c82022-04-28 23:16:03 -0700581 iter->from, h, h_cg,
Peter Xu2103cf92021-03-12 21:07:18 -0800582 regions_needed);
Mina Almasryd75c6af2019-11-30 17:56:59 -0800583
Jakob Koschel84448c82022-04-28 23:16:03 -0700584 last_accounted_offset = iter->to;
Mina Almasry0db9d742020-04-01 21:11:25 -0700585 }
586
587 /* Handle the case where our range extends beyond
588 * last_accounted_offset.
589 */
Jakob Koschel84448c82022-04-28 23:16:03 -0700590 if (!rg)
591 rg = head->prev;
Peter Xu2103cf92021-03-12 21:07:18 -0800592 if (last_accounted_offset < t)
593 add += hugetlb_resv_map_add(resv, rg, last_accounted_offset,
594 t, h, h_cg, regions_needed);
Mina Almasry0db9d742020-04-01 21:11:25 -0700595
Mina Almasry0db9d742020-04-01 21:11:25 -0700596 return add;
597}
598
599/* Must be called with resv->lock acquired. Will drop lock to allocate entries.
600 */
601static int allocate_file_region_entries(struct resv_map *resv,
602 int regions_needed)
603 __must_hold(&resv->lock)
604{
Miaohe Lin34665342022-09-01 20:00:23 +0800605 LIST_HEAD(allocated_regions);
Mina Almasry0db9d742020-04-01 21:11:25 -0700606 int to_allocate = 0, i = 0;
607 struct file_region *trg = NULL, *rg = NULL;
608
609 VM_BUG_ON(regions_needed < 0);
610
Mina Almasry0db9d742020-04-01 21:11:25 -0700611 /*
612 * Check for sufficient descriptors in the cache to accommodate
613 * the number of in progress add operations plus regions_needed.
614 *
615 * This is a while loop because when we drop the lock, some other call
616 * to region_add or region_del may have consumed some region_entries,
617 * so we keep looping here until we finally have enough entries for
618 * (adds_in_progress + regions_needed).
619 */
620 while (resv->region_cache_count <
621 (resv->adds_in_progress + regions_needed)) {
622 to_allocate = resv->adds_in_progress + regions_needed -
623 resv->region_cache_count;
624
625 /* At this point, we should have enough entries in the cache
Ingo Molnarf0953a12021-05-06 18:06:47 -0700626 * for all the existing adds_in_progress. We should only be
Mina Almasry0db9d742020-04-01 21:11:25 -0700627 * needing to allocate for regions_needed.
628 */
629 VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress);
630
631 spin_unlock(&resv->lock);
632 for (i = 0; i < to_allocate; i++) {
633 trg = kmalloc(sizeof(*trg), GFP_KERNEL);
634 if (!trg)
635 goto out_of_memory;
636 list_add(&trg->link, &allocated_regions);
637 }
638
639 spin_lock(&resv->lock);
640
Wei Yangd3ec7b62020-10-13 16:56:27 -0700641 list_splice(&allocated_regions, &resv->region_cache);
642 resv->region_cache_count += to_allocate;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800643 }
644
Mina Almasry0db9d742020-04-01 21:11:25 -0700645 return 0;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800646
Mina Almasry0db9d742020-04-01 21:11:25 -0700647out_of_memory:
648 list_for_each_entry_safe(rg, trg, &allocated_regions, link) {
649 list_del(&rg->link);
650 kfree(rg);
651 }
652 return -ENOMEM;
Mina Almasryd75c6af2019-11-30 17:56:59 -0800653}
654
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700655/*
656 * Add the huge page range represented by [f, t) to the reserve
Mina Almasry0db9d742020-04-01 21:11:25 -0700657 * map. Regions will be taken from the cache to fill in this range.
658 * Sufficient regions should exist in the cache due to the previous
659 * call to region_chg with the same range, but in some cases the cache will not
660 * have sufficient entries due to races with other code doing region_add or
661 * region_del. The extra needed entries will be allocated.
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700662 *
Mina Almasry0db9d742020-04-01 21:11:25 -0700663 * regions_needed is the out value provided by a previous call to region_chg.
664 *
665 * Return the number of new huge pages added to the map. This number is greater
666 * than or equal to zero. If file_region entries needed to be allocated for
Ethon Paul7c8de352020-06-04 16:49:07 -0700667 * this operation and we were not able to allocate, it returns -ENOMEM.
Mina Almasry0db9d742020-04-01 21:11:25 -0700668 * region_add of regions of length 1 never allocate file_regions and cannot
669 * fail; region_chg will always allocate at least 1 entry and a region_add for
670 * 1 page will only require at most 1 entry.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700671 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700672static long region_add(struct resv_map *resv, long f, long t,
Mina Almasry075a61d2020-04-01 21:11:28 -0700673 long in_regions_needed, struct hstate *h,
674 struct hugetlb_cgroup *h_cg)
Andy Whitcroft96822902008-07-23 21:27:29 -0700675{
Mina Almasry0db9d742020-04-01 21:11:25 -0700676 long add = 0, actual_regions_needed = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700677
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700678 spin_lock(&resv->lock);
Mina Almasry0db9d742020-04-01 21:11:25 -0700679retry:
680
681 /* Count how many regions are actually needed to execute this add. */
Wei Yang972a3da32020-10-13 16:56:30 -0700682 add_reservation_in_range(resv, f, t, NULL, NULL,
683 &actual_regions_needed);
Andy Whitcroft96822902008-07-23 21:27:29 -0700684
Mike Kravetz5e911372015-09-08 15:01:28 -0700685 /*
Mina Almasry0db9d742020-04-01 21:11:25 -0700686 * Check for sufficient descriptors in the cache to accommodate
687 * this add operation. Note that actual_regions_needed may be greater
688 * than in_regions_needed, as the resv_map may have been modified since
689 * the region_chg call. In this case, we need to make sure that we
690 * allocate extra entries, such that we have enough for all the
691 * existing adds_in_progress, plus the excess needed for this
692 * operation.
Mike Kravetz5e911372015-09-08 15:01:28 -0700693 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700694 if (actual_regions_needed > in_regions_needed &&
695 resv->region_cache_count <
696 resv->adds_in_progress +
697 (actual_regions_needed - in_regions_needed)) {
698 /* region_add operation of range 1 should never need to
699 * allocate file_region entries.
700 */
701 VM_BUG_ON(t - f <= 1);
Mike Kravetz5e911372015-09-08 15:01:28 -0700702
Mina Almasry0db9d742020-04-01 21:11:25 -0700703 if (allocate_file_region_entries(
704 resv, actual_regions_needed - in_regions_needed)) {
705 return -ENOMEM;
706 }
Mike Kravetz5e911372015-09-08 15:01:28 -0700707
Mina Almasry0db9d742020-04-01 21:11:25 -0700708 goto retry;
Mike Kravetz5e911372015-09-08 15:01:28 -0700709 }
710
Wei Yang972a3da32020-10-13 16:56:30 -0700711 add = add_reservation_in_range(resv, f, t, h_cg, h, NULL);
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700712
Mina Almasry0db9d742020-04-01 21:11:25 -0700713 resv->adds_in_progress -= in_regions_needed;
714
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700715 spin_unlock(&resv->lock);
Mike Kravetzcf3ad202015-06-24 16:57:55 -0700716 return add;
Andy Whitcroft96822902008-07-23 21:27:29 -0700717}
718
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700719/*
720 * Examine the existing reserve map and determine how many
721 * huge pages in the specified range [f, t) are NOT currently
722 * represented. This routine is called before a subsequent
723 * call to region_add that will actually modify the reserve
724 * map to add the specified range [f, t). region_chg does
725 * not change the number of huge pages represented by the
Mina Almasry0db9d742020-04-01 21:11:25 -0700726 * map. A number of new file_region structures is added to the cache as a
727 * placeholder, for the subsequent region_add call to use. At least 1
728 * file_region structure is added.
729 *
730 * out_regions_needed is the number of regions added to the
731 * resv->adds_in_progress. This value needs to be provided to a follow up call
732 * to region_add or region_abort for proper accounting.
Mike Kravetz5e911372015-09-08 15:01:28 -0700733 *
734 * Returns the number of huge pages that need to be added to the existing
735 * reservation map for the range [f, t). This number is greater or equal to
736 * zero. -ENOMEM is returned if a new file_region structure or cache entry
737 * is needed and can not be allocated.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700738 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700739static long region_chg(struct resv_map *resv, long f, long t,
740 long *out_regions_needed)
Andy Whitcroft96822902008-07-23 21:27:29 -0700741{
Andy Whitcroft96822902008-07-23 21:27:29 -0700742 long chg = 0;
743
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700744 spin_lock(&resv->lock);
Mike Kravetz5e911372015-09-08 15:01:28 -0700745
Wei Yang972a3da32020-10-13 16:56:30 -0700746 /* Count how many hugepages in this range are NOT represented. */
Mina Almasry075a61d2020-04-01 21:11:28 -0700747 chg = add_reservation_in_range(resv, f, t, NULL, NULL,
Wei Yang972a3da32020-10-13 16:56:30 -0700748 out_regions_needed);
Mike Kravetz5e911372015-09-08 15:01:28 -0700749
Mina Almasry0db9d742020-04-01 21:11:25 -0700750 if (*out_regions_needed == 0)
751 *out_regions_needed = 1;
Mike Kravetz5e911372015-09-08 15:01:28 -0700752
Mina Almasry0db9d742020-04-01 21:11:25 -0700753 if (allocate_file_region_entries(resv, *out_regions_needed))
754 return -ENOMEM;
Mike Kravetz5e911372015-09-08 15:01:28 -0700755
Mina Almasry0db9d742020-04-01 21:11:25 -0700756 resv->adds_in_progress += *out_regions_needed;
Andy Whitcroft96822902008-07-23 21:27:29 -0700757
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700758 spin_unlock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700759 return chg;
760}
761
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700762/*
Mike Kravetz5e911372015-09-08 15:01:28 -0700763 * Abort the in progress add operation. The adds_in_progress field
764 * of the resv_map keeps track of the operations in progress between
765 * calls to region_chg and region_add. Operations are sometimes
766 * aborted after the call to region_chg. In such cases, region_abort
Mina Almasry0db9d742020-04-01 21:11:25 -0700767 * is called to decrement the adds_in_progress counter. regions_needed
768 * is the value returned by the region_chg call, it is used to decrement
769 * the adds_in_progress counter.
Mike Kravetz5e911372015-09-08 15:01:28 -0700770 *
771 * NOTE: The range arguments [f, t) are not needed or used in this
772 * routine. They are kept to make reading the calling code easier as
773 * arguments will match the associated region_chg call.
774 */
Mina Almasry0db9d742020-04-01 21:11:25 -0700775static void region_abort(struct resv_map *resv, long f, long t,
776 long regions_needed)
Mike Kravetz5e911372015-09-08 15:01:28 -0700777{
778 spin_lock(&resv->lock);
779 VM_BUG_ON(!resv->region_cache_count);
Mina Almasry0db9d742020-04-01 21:11:25 -0700780 resv->adds_in_progress -= regions_needed;
Mike Kravetz5e911372015-09-08 15:01:28 -0700781 spin_unlock(&resv->lock);
782}
783
784/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700785 * Delete the specified range [f, t) from the reserve map. If the
786 * t parameter is LONG_MAX, this indicates that ALL regions after f
787 * should be deleted. Locate the regions which intersect [f, t)
788 * and either trim, delete or split the existing regions.
789 *
790 * Returns the number of huge pages deleted from the reserve map.
791 * In the normal case, the return value is zero or more. In the
792 * case where a region must be split, a new region descriptor must
793 * be allocated. If the allocation fails, -ENOMEM will be returned.
794 * NOTE: If the parameter t == LONG_MAX, then we will never split
795 * a region and possibly return -ENOMEM. Callers specifying
796 * t == LONG_MAX do not need to check for -ENOMEM error.
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700797 */
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700798static long region_del(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700799{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700800 struct list_head *head = &resv->regions;
Andy Whitcroft96822902008-07-23 21:27:29 -0700801 struct file_region *rg, *trg;
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700802 struct file_region *nrg = NULL;
803 long del = 0;
Andy Whitcroft96822902008-07-23 21:27:29 -0700804
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700805retry:
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700806 spin_lock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700807 list_for_each_entry_safe(rg, trg, head, link) {
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800808 /*
809 * Skip regions before the range to be deleted. file_region
810 * ranges are normally of the form [from, to). However, there
811 * may be a "placeholder" entry in the map which is of the form
812 * (from, to) with from == to. Check for placeholder entries
813 * at the beginning of the range to be deleted.
814 */
815 if (rg->to <= f && (rg->to != rg->from || rg->to != f))
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700816 continue;
Mike Kravetzdbe409e2015-12-11 13:40:52 -0800817
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700818 if (rg->from >= t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700819 break;
Andy Whitcroft96822902008-07-23 21:27:29 -0700820
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700821 if (f > rg->from && t < rg->to) { /* Must split region */
822 /*
823 * Check for an entry in the cache before dropping
824 * lock and attempting allocation.
825 */
826 if (!nrg &&
827 resv->region_cache_count > resv->adds_in_progress) {
828 nrg = list_first_entry(&resv->region_cache,
829 struct file_region,
830 link);
831 list_del(&nrg->link);
832 resv->region_cache_count--;
833 }
834
835 if (!nrg) {
836 spin_unlock(&resv->lock);
837 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
838 if (!nrg)
839 return -ENOMEM;
840 goto retry;
841 }
842
843 del += t - f;
Mike Kravetz79aa9252020-11-01 17:07:27 -0800844 hugetlb_cgroup_uncharge_file_region(
Miaohe Lind85aecf2021-03-24 21:37:17 -0700845 resv, rg, t - f, false);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700846
847 /* New entry for end of split region */
848 nrg->from = t;
849 nrg->to = rg->to;
Mina Almasry075a61d2020-04-01 21:11:28 -0700850
851 copy_hugetlb_cgroup_uncharge_info(nrg, rg);
852
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700853 INIT_LIST_HEAD(&nrg->link);
854
855 /* Original entry is trimmed */
856 rg->to = f;
857
858 list_add(&nrg->link, &rg->link);
859 nrg = NULL;
860 break;
861 }
862
863 if (f <= rg->from && t >= rg->to) { /* Remove entire region */
864 del += rg->to - rg->from;
Mina Almasry075a61d2020-04-01 21:11:28 -0700865 hugetlb_cgroup_uncharge_file_region(resv, rg,
Miaohe Lind85aecf2021-03-24 21:37:17 -0700866 rg->to - rg->from, true);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700867 list_del(&rg->link);
868 kfree(rg);
869 continue;
870 }
871
872 if (f <= rg->from) { /* Trim beginning of region */
Mina Almasry075a61d2020-04-01 21:11:28 -0700873 hugetlb_cgroup_uncharge_file_region(resv, rg,
Miaohe Lind85aecf2021-03-24 21:37:17 -0700874 t - rg->from, false);
Mina Almasry075a61d2020-04-01 21:11:28 -0700875
Mike Kravetz79aa9252020-11-01 17:07:27 -0800876 del += t - rg->from;
877 rg->from = t;
878 } else { /* Trim end 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 rg->to - f, false);
Mike Kravetz79aa9252020-11-01 17:07:27 -0800881
882 del += rg->to - f;
883 rg->to = f;
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700884 }
Andy Whitcroft96822902008-07-23 21:27:29 -0700885 }
886
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700887 spin_unlock(&resv->lock);
Mike Kravetzfeba16e2015-09-08 15:01:31 -0700888 kfree(nrg);
889 return del;
Andy Whitcroft96822902008-07-23 21:27:29 -0700890}
891
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700892/*
Mike Kravetzb5cec282015-09-08 15:01:41 -0700893 * A rare out of memory error was encountered which prevented removal of
894 * the reserve map region for a page. The huge page itself was free'ed
895 * and removed from the page cache. This routine will adjust the subpool
896 * usage count, and the global reserve count if needed. By incrementing
897 * these counts, the reserve map entry which could not be deleted will
898 * appear as a "reserved" entry instead of simply dangling with incorrect
899 * counts.
900 */
zhong jiang72e29362016-10-07 17:02:01 -0700901void hugetlb_fix_reserve_counts(struct inode *inode)
Mike Kravetzb5cec282015-09-08 15:01:41 -0700902{
903 struct hugepage_subpool *spool = subpool_inode(inode);
904 long rsv_adjust;
Miaohe Linda563882021-05-04 18:34:38 -0700905 bool reserved = false;
Mike Kravetzb5cec282015-09-08 15:01:41 -0700906
907 rsv_adjust = hugepage_subpool_get_pages(spool, 1);
Miaohe Linda563882021-05-04 18:34:38 -0700908 if (rsv_adjust > 0) {
Mike Kravetzb5cec282015-09-08 15:01:41 -0700909 struct hstate *h = hstate_inode(inode);
910
Miaohe Linda563882021-05-04 18:34:38 -0700911 if (!hugetlb_acct_memory(h, 1))
912 reserved = true;
913 } else if (!rsv_adjust) {
914 reserved = true;
Mike Kravetzb5cec282015-09-08 15:01:41 -0700915 }
Miaohe Linda563882021-05-04 18:34:38 -0700916
917 if (!reserved)
918 pr_warn("hugetlb: Huge Page Reserved count may go negative.\n");
Mike Kravetzb5cec282015-09-08 15:01:41 -0700919}
920
921/*
Mike Kravetz1dd308a2015-06-24 16:57:52 -0700922 * Count and return the number of huge pages in the reserve map
923 * that intersect with the range [f, t).
924 */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700925static long region_count(struct resv_map *resv, long f, long t)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700926{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700927 struct list_head *head = &resv->regions;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700928 struct file_region *rg;
929 long chg = 0;
930
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700931 spin_lock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700932 /* Locate each segment we overlap with, and count that overlap. */
933 list_for_each_entry(rg, head, link) {
Wang Sheng-Huif2135a42012-05-29 15:06:17 -0700934 long seg_from;
935 long seg_to;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700936
937 if (rg->to <= f)
938 continue;
939 if (rg->from >= t)
940 break;
941
942 seg_from = max(rg->from, f);
943 seg_to = min(rg->to, t);
944
945 chg += seg_to - seg_from;
946 }
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -0700947 spin_unlock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700948
949 return chg;
950}
951
Andy Whitcroft96822902008-07-23 21:27:29 -0700952/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700953 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700954 * the mapping, in pagecache page units; huge pages here.
955 */
Andi Kleena5516432008-07-23 21:27:41 -0700956static pgoff_t vma_hugecache_offset(struct hstate *h,
957 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700958{
Andi Kleena5516432008-07-23 21:27:41 -0700959 return ((address - vma->vm_start) >> huge_page_shift(h)) +
960 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700961}
962
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900963pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
964 unsigned long address)
965{
966 return vma_hugecache_offset(hstate_vma(vma), vma, address);
967}
Dan Williamsdee41072016-05-14 12:20:44 -0700968EXPORT_SYMBOL_GPL(linear_hugepage_index);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900969
Andy Whitcroft84afd992008-07-23 21:27:32 -0700970/*
Mel Gorman08fba692009-01-06 14:38:53 -0800971 * Return the size of the pages allocated when backing a VMA. In the majority
972 * cases this will be same size as used by the page table entries.
973 */
974unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
975{
Dan Williams05ea8862018-04-05 16:24:25 -0700976 if (vma->vm_ops && vma->vm_ops->pagesize)
977 return vma->vm_ops->pagesize(vma);
978 return PAGE_SIZE;
Mel Gorman08fba692009-01-06 14:38:53 -0800979}
Joerg Roedelf340ca02009-06-19 15:16:22 +0200980EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
Mel Gorman08fba692009-01-06 14:38:53 -0800981
982/*
Mel Gorman33402892009-01-06 14:38:54 -0800983 * Return the page size being used by the MMU to back a VMA. In the majority
984 * of cases, the page size used by the kernel matches the MMU size. On
Dan Williams09135cc2018-04-05 16:24:21 -0700985 * architectures where it differs, an architecture-specific 'strong'
986 * version of this symbol is required.
Mel Gorman33402892009-01-06 14:38:54 -0800987 */
Dan Williams09135cc2018-04-05 16:24:21 -0700988__weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
Mel Gorman33402892009-01-06 14:38:54 -0800989{
990 return vma_kernel_pagesize(vma);
991}
Mel Gorman33402892009-01-06 14:38:54 -0800992
993/*
Andy Whitcroft84afd992008-07-23 21:27:32 -0700994 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
995 * bits of the reservation map pointer, which are always clear due to
996 * alignment.
997 */
998#define HPAGE_RESV_OWNER (1UL << 0)
999#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001000#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -07001001
Mel Gormana1e78772008-07-23 21:27:23 -07001002/*
1003 * These helpers are used to track how many pages are reserved for
1004 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
1005 * is guaranteed to have their future faults succeed.
1006 *
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001007 * With the exception of hugetlb_dup_vma_private() which is called at fork(),
Mel Gormana1e78772008-07-23 21:27:23 -07001008 * the reserve counters are updated with the hugetlb_lock held. It is safe
1009 * to reset the VMA at fork() time as it is not in use yet and there is no
1010 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -07001011 *
1012 * The private mapping reservation is represented in a subtly different
1013 * manner to a shared mapping. A shared mapping has a region map associated
1014 * with the underlying file, this region map represents the backing file
1015 * pages which have ever had a reservation assigned which this persists even
1016 * after the page is instantiated. A private mapping has a region map
1017 * associated with the original mmap which is attached to all VMAs which
1018 * reference it, this region map represents those offsets which have consumed
1019 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -07001020 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001021static unsigned long get_vma_private_data(struct vm_area_struct *vma)
1022{
1023 return (unsigned long)vma->vm_private_data;
1024}
1025
1026static void set_vma_private_data(struct vm_area_struct *vma,
1027 unsigned long value)
1028{
1029 vma->vm_private_data = (void *)value;
1030}
1031
Mina Almasrye9fe92a2020-04-01 21:11:21 -07001032static void
1033resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map,
1034 struct hugetlb_cgroup *h_cg,
1035 struct hstate *h)
1036{
1037#ifdef CONFIG_CGROUP_HUGETLB
1038 if (!h_cg || !h) {
1039 resv_map->reservation_counter = NULL;
1040 resv_map->pages_per_hpage = 0;
1041 resv_map->css = NULL;
1042 } else {
1043 resv_map->reservation_counter =
1044 &h_cg->rsvd_hugepage[hstate_index(h)];
1045 resv_map->pages_per_hpage = pages_per_huge_page(h);
1046 resv_map->css = &h_cg->css;
1047 }
1048#endif
1049}
1050
Joonsoo Kim9119a412014-04-03 14:47:25 -07001051struct resv_map *resv_map_alloc(void)
Andy Whitcroft84afd992008-07-23 21:27:32 -07001052{
1053 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
Mike Kravetz5e911372015-09-08 15:01:28 -07001054 struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL);
1055
1056 if (!resv_map || !rg) {
1057 kfree(resv_map);
1058 kfree(rg);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001059 return NULL;
Mike Kravetz5e911372015-09-08 15:01:28 -07001060 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07001061
1062 kref_init(&resv_map->refs);
Davidlohr Bueso7b24d8612014-04-03 14:47:27 -07001063 spin_lock_init(&resv_map->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001064 INIT_LIST_HEAD(&resv_map->regions);
1065
Mike Kravetz5e911372015-09-08 15:01:28 -07001066 resv_map->adds_in_progress = 0;
Mina Almasrye9fe92a2020-04-01 21:11:21 -07001067 /*
1068 * Initialize these to 0. On shared mappings, 0's here indicate these
1069 * fields don't do cgroup accounting. On private mappings, these will be
1070 * re-initialized to the proper values, to indicate that hugetlb cgroup
1071 * reservations are to be un-charged from here.
1072 */
1073 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL);
Mike Kravetz5e911372015-09-08 15:01:28 -07001074
1075 INIT_LIST_HEAD(&resv_map->region_cache);
1076 list_add(&rg->link, &resv_map->region_cache);
1077 resv_map->region_cache_count = 1;
1078
Andy Whitcroft84afd992008-07-23 21:27:32 -07001079 return resv_map;
1080}
1081
Joonsoo Kim9119a412014-04-03 14:47:25 -07001082void resv_map_release(struct kref *ref)
Andy Whitcroft84afd992008-07-23 21:27:32 -07001083{
1084 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
Mike Kravetz5e911372015-09-08 15:01:28 -07001085 struct list_head *head = &resv_map->region_cache;
1086 struct file_region *rg, *trg;
Andy Whitcroft84afd992008-07-23 21:27:32 -07001087
1088 /* Clear out any active regions before we release the map. */
Mike Kravetzfeba16e2015-09-08 15:01:31 -07001089 region_del(resv_map, 0, LONG_MAX);
Mike Kravetz5e911372015-09-08 15:01:28 -07001090
1091 /* ... and any entries left in the cache */
1092 list_for_each_entry_safe(rg, trg, head, link) {
1093 list_del(&rg->link);
1094 kfree(rg);
1095 }
1096
1097 VM_BUG_ON(resv_map->adds_in_progress);
1098
Andy Whitcroft84afd992008-07-23 21:27:32 -07001099 kfree(resv_map);
1100}
1101
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001102static inline struct resv_map *inode_resv_map(struct inode *inode)
1103{
Mike Kravetzf27a5132019-05-13 17:22:55 -07001104 /*
1105 * At inode evict time, i_mapping may not point to the original
1106 * address space within the inode. This original address space
1107 * contains the pointer to the resv_map. So, always use the
1108 * address space embedded within the inode.
1109 * The VERY common case is inode->mapping == &inode->i_data but,
1110 * this may not be true for device special inodes.
1111 */
1112 return (struct resv_map *)(&inode->i_data)->private_data;
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001113}
1114
Andy Whitcroft84afd992008-07-23 21:27:32 -07001115static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -07001116{
Sasha Levin81d1b092014-10-09 15:28:10 -07001117 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001118 if (vma->vm_flags & VM_MAYSHARE) {
1119 struct address_space *mapping = vma->vm_file->f_mapping;
1120 struct inode *inode = mapping->host;
1121
1122 return inode_resv_map(inode);
1123
1124 } else {
Andy Whitcroft84afd992008-07-23 21:27:32 -07001125 return (struct resv_map *)(get_vma_private_data(vma) &
1126 ~HPAGE_RESV_MASK);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001127 }
Mel Gormana1e78772008-07-23 21:27:23 -07001128}
1129
Andy Whitcroft84afd992008-07-23 21:27:32 -07001130static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -07001131{
Sasha Levin81d1b092014-10-09 15:28:10 -07001132 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1133 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Mel Gormana1e78772008-07-23 21:27:23 -07001134
Andy Whitcroft84afd992008-07-23 21:27:32 -07001135 set_vma_private_data(vma, (get_vma_private_data(vma) &
1136 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001137}
1138
1139static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
1140{
Sasha Levin81d1b092014-10-09 15:28:10 -07001141 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1142 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001143
1144 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001145}
1146
1147static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
1148{
Sasha Levin81d1b092014-10-09 15:28:10 -07001149 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07001150
1151 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -07001152}
1153
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001154void hugetlb_dup_vma_private(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -07001155{
Sasha Levin81d1b092014-10-09 15:28:10 -07001156 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001157 /*
1158 * Clear vm_private_data
Mike Kravetz612b8a32022-10-19 13:19:57 -07001159 * - For shared mappings this is a per-vma semaphore that may be
1160 * allocated in a subsequent call to hugetlb_vm_op_open.
1161 * Before clearing, make sure pointer is not associated with vma
1162 * as this will leak the structure. This is the case when called
1163 * via clear_vma_resv_huge_pages() and hugetlb_vm_op_open has already
1164 * been called to allocate a new structure.
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001165 * - For MAP_PRIVATE mappings, this is the reserve map which does
1166 * not apply to children. Faults generated by the children are
1167 * not guaranteed to succeed, even if read-only.
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001168 */
Mike Kravetz612b8a32022-10-19 13:19:57 -07001169 if (vma->vm_flags & VM_MAYSHARE) {
1170 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
1171
1172 if (vma_lock && vma_lock->vma != vma)
1173 vma->vm_private_data = NULL;
1174 } else
1175 vma->vm_private_data = NULL;
Mel Gormana1e78772008-07-23 21:27:23 -07001176}
1177
Mina Almasry550a7d62021-11-05 13:41:40 -07001178/*
1179 * Reset and decrement one ref on hugepage private reservation.
Lorenzo Stoakes8651a132023-01-07 00:00:05 +00001180 * Called with mm->mmap_lock writer semaphore held.
Mina Almasry550a7d62021-11-05 13:41:40 -07001181 * This function should be only used by move_vma() and operate on
1182 * same sized vma. It should never come here with last ref on the
1183 * reservation.
1184 */
1185void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
1186{
1187 /*
1188 * Clear the old hugetlb private page reservation.
1189 * It has already been transferred to new_vma.
1190 *
1191 * During a mremap() operation of a hugetlb vma we call move_vma()
1192 * which copies vma into new_vma and unmaps vma. After the copy
1193 * operation both new_vma and vma share a reference to the resv_map
1194 * struct, and at that point vma is about to be unmapped. We don't
1195 * want to return the reservation to the pool at unmap of vma because
1196 * the reservation still lives on in new_vma, so simply decrement the
1197 * ref here and remove the resv_map reference from this vma.
1198 */
1199 struct resv_map *reservations = vma_resv_map(vma);
1200
Bui Quang Minhafe041c2021-11-19 16:43:40 -08001201 if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1202 resv_map_put_hugetlb_cgroup_uncharge_info(reservations);
Mina Almasry550a7d62021-11-05 13:41:40 -07001203 kref_put(&reservations->refs, resv_map_release);
Bui Quang Minhafe041c2021-11-19 16:43:40 -08001204 }
Mina Almasry550a7d62021-11-05 13:41:40 -07001205
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07001206 hugetlb_dup_vma_private(vma);
Mina Almasry550a7d62021-11-05 13:41:40 -07001207}
1208
Mel Gormana1e78772008-07-23 21:27:23 -07001209/* Returns true if the VMA has associated reserve pages */
Nicholas Krause559ec2f2015-09-04 15:48:27 -07001210static bool vma_has_reserves(struct vm_area_struct *vma, long chg)
Mel Gormana1e78772008-07-23 21:27:23 -07001211{
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001212 if (vma->vm_flags & VM_NORESERVE) {
1213 /*
1214 * This address is already reserved by other process(chg == 0),
1215 * so, we should decrement reserved count. Without decrementing,
1216 * reserve count remains after releasing inode, because this
1217 * allocated page will go into page cache and is regarded as
1218 * coming from reserved pool in releasing step. Currently, we
1219 * don't have any other solution to deal with this situation
1220 * properly, so add work-around here.
1221 */
1222 if (vma->vm_flags & VM_MAYSHARE && chg == 0)
Nicholas Krause559ec2f2015-09-04 15:48:27 -07001223 return true;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001224 else
Nicholas Krause559ec2f2015-09-04 15:48:27 -07001225 return false;
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001226 }
Joonsoo Kima63884e2013-09-11 14:21:07 -07001227
1228 /* Shared mappings always use reserves */
Mike Kravetz1fb1b0e2015-09-08 15:01:44 -07001229 if (vma->vm_flags & VM_MAYSHARE) {
1230 /*
1231 * We know VM_NORESERVE is not set. Therefore, there SHOULD
1232 * be a region map for all pages. The only situation where
1233 * there is no region map is if a hole was punched via
Ethon Paul7c8de352020-06-04 16:49:07 -07001234 * fallocate. In this case, there really are no reserves to
Mike Kravetz1fb1b0e2015-09-08 15:01:44 -07001235 * use. This situation is indicated if chg != 0.
1236 */
1237 if (chg)
1238 return false;
1239 else
1240 return true;
1241 }
Joonsoo Kima63884e2013-09-11 14:21:07 -07001242
1243 /*
1244 * Only the process that called mmap() has reserves for
1245 * private mappings.
1246 */
Mike Kravetz67961f92016-06-08 15:33:42 -07001247 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1248 /*
1249 * Like the shared case above, a hole punch or truncate
1250 * could have been performed on the private mapping.
1251 * Examine the value of chg to determine if reserves
1252 * actually exist or were previously consumed.
1253 * Very Subtle - The value of chg comes from a previous
1254 * call to vma_needs_reserves(). The reserve map for
1255 * private mappings has different (opposite) semantics
1256 * than that of shared mappings. vma_needs_reserves()
1257 * has already taken this difference in semantics into
1258 * account. Therefore, the meaning of chg is the same
1259 * as in the shared case above. Code could easily be
1260 * combined, but keeping it separate draws attention to
1261 * subtle differences.
1262 */
1263 if (chg)
1264 return false;
1265 else
1266 return true;
1267 }
Joonsoo Kima63884e2013-09-11 14:21:07 -07001268
Nicholas Krause559ec2f2015-09-04 15:48:27 -07001269 return false;
Mel Gormana1e78772008-07-23 21:27:23 -07001270}
1271
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001272static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001274 int nid = folio_nid(folio);
Mike Kravetz9487ca62021-05-04 18:35:10 -07001275
1276 lockdep_assert_held(&hugetlb_lock);
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001277 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07001278
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001279 list_move(&folio->lru, &h->hugepage_freelists[nid]);
Andi Kleena5516432008-07-23 21:27:41 -07001280 h->free_huge_pages++;
1281 h->free_huge_pages_node[nid]++;
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001282 folio_set_hugetlb_freed(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001285static struct folio *dequeue_hugetlb_folio_node_exact(struct hstate *h,
1286 int nid)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001287{
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001288 struct folio *folio;
Pavel Tatashin1a08ae32021-05-04 18:38:53 -07001289 bool pin = !!(current->flags & PF_MEMALLOC_PIN);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001290
Mike Kravetz9487ca62021-05-04 18:35:10 -07001291 lockdep_assert_held(&hugetlb_lock);
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001292 list_for_each_entry(folio, &h->hugepage_freelists[nid], lru) {
1293 if (pin && !folio_is_longterm_pinnable(folio))
Joonsoo Kimbbe88752020-08-11 18:37:38 -07001294 continue;
1295
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001296 if (folio_test_hwpoison(folio))
Wei Yang6664bfc2020-10-13 16:56:39 -07001297 continue;
1298
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001299 list_move(&folio->lru, &h->hugepage_activelist);
1300 folio_ref_unfreeze(folio, 1);
1301 folio_clear_hugetlb_freed(folio);
Wei Yang6664bfc2020-10-13 16:56:39 -07001302 h->free_huge_pages--;
1303 h->free_huge_pages_node[nid]--;
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001304 return folio;
Joonsoo Kimbbe88752020-08-11 18:37:38 -07001305 }
1306
Wei Yang6664bfc2020-10-13 16:56:39 -07001307 return NULL;
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001308}
1309
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001310static struct folio *dequeue_hugetlb_folio_nodemask(struct hstate *h, gfp_t gfp_mask,
1311 int nid, nodemask_t *nmask)
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001312{
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001313 unsigned int cpuset_mems_cookie;
1314 struct zonelist *zonelist;
1315 struct zone *zone;
1316 struct zoneref *z;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08001317 int node = NUMA_NO_NODE;
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001318
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001319 zonelist = node_zonelist(nid, gfp_mask);
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001320
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001321retry_cpuset:
1322 cpuset_mems_cookie = read_mems_allowed_begin();
1323 for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) {
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001324 struct folio *folio;
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001325
1326 if (!cpuset_zone_allowed(zone, gfp_mask))
1327 continue;
1328 /*
1329 * no need to ask again on the same node. Pool is node rather than
1330 * zone aware
1331 */
1332 if (zone_to_nid(zone) == node)
1333 continue;
1334 node = zone_to_nid(zone);
1335
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001336 folio = dequeue_hugetlb_folio_node_exact(h, node);
1337 if (folio)
1338 return folio;
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001339 }
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001340 if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie)))
1341 goto retry_cpuset;
1342
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001343 return NULL;
1344}
1345
Xin Hao8346d692022-09-22 10:19:29 +08001346static unsigned long available_huge_pages(struct hstate *h)
1347{
1348 return h->free_huge_pages - h->resv_huge_pages;
1349}
1350
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06001351static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h,
Andi Kleena5516432008-07-23 21:27:41 -07001352 struct vm_area_struct *vma,
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001353 unsigned long address, int avoid_reserve,
1354 long chg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001356 struct folio *folio = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -07001357 struct mempolicy *mpol;
Vlastimil Babka04ec6262017-07-06 15:40:03 -07001358 gfp_t gfp_mask;
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001359 nodemask_t *nodemask;
Vlastimil Babka04ec6262017-07-06 15:40:03 -07001360 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Mel Gormana1e78772008-07-23 21:27:23 -07001362 /*
1363 * A child process with MAP_PRIVATE mappings created by their parent
1364 * have no page reserves. This check ensures that reservations are
1365 * not "stolen". The child may still get SIGKILLed
1366 */
Xin Hao8346d692022-09-22 10:19:29 +08001367 if (!vma_has_reserves(vma, chg) && !available_huge_pages(h))
Miao Xiec0ff7452010-05-24 14:32:08 -07001368 goto err;
Mel Gormana1e78772008-07-23 21:27:23 -07001369
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001370 /* If reserves cannot be used, ensure enough pages are in the pool */
Xin Hao8346d692022-09-22 10:19:29 +08001371 if (avoid_reserve && !available_huge_pages(h))
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001372 goto err;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001373
Vlastimil Babka04ec6262017-07-06 15:40:03 -07001374 gfp_mask = htlb_alloc_mask(h);
1375 nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07001376
1377 if (mpol_is_preferred_many(mpol)) {
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001378 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask,
1379 nid, nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07001380
1381 /* Fallback to all nodes if page==NULL */
1382 nodemask = NULL;
1383 }
1384
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001385 if (!folio)
1386 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask,
1387 nid, nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07001388
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06001389 if (folio && !avoid_reserve && vma_has_reserves(vma, chg)) {
1390 folio_set_hugetlb_restore_reserve(folio);
Michal Hocko3e59fcb2017-07-10 15:49:11 -07001391 h->resv_huge_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001393
1394 mpol_cond_put(mpol);
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06001395 return folio;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001396
Miao Xiec0ff7452010-05-24 14:32:08 -07001397err:
Mel Gormancc9a6c82012-03-21 16:34:11 -07001398 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
Luiz Capitulino1cac6f22014-06-04 16:07:11 -07001401/*
1402 * common helper functions for hstate_next_node_to_{alloc|free}.
1403 * We may have allocated or freed a huge page based on a different
1404 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
1405 * be outside of *nodes_allowed. Ensure that we use an allowed
1406 * node for alloc or free.
1407 */
1408static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
1409{
Andrew Morton0edaf862016-05-19 17:10:58 -07001410 nid = next_node_in(nid, *nodes_allowed);
Luiz Capitulino1cac6f22014-06-04 16:07:11 -07001411 VM_BUG_ON(nid >= MAX_NUMNODES);
1412
1413 return nid;
1414}
1415
1416static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
1417{
1418 if (!node_isset(nid, *nodes_allowed))
1419 nid = next_node_allowed(nid, nodes_allowed);
1420 return nid;
1421}
1422
1423/*
1424 * returns the previously saved node ["this node"] from which to
1425 * allocate a persistent huge page for the pool and advance the
1426 * next node from which to allocate, handling wrap at end of node
1427 * mask.
1428 */
1429static int hstate_next_node_to_alloc(struct hstate *h,
1430 nodemask_t *nodes_allowed)
1431{
1432 int nid;
1433
1434 VM_BUG_ON(!nodes_allowed);
1435
1436 nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
1437 h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
1438
1439 return nid;
1440}
1441
1442/*
Mike Kravetz10c6ec42021-05-04 18:35:03 -07001443 * helper for remove_pool_huge_page() - return the previously saved
Luiz Capitulino1cac6f22014-06-04 16:07:11 -07001444 * node ["this node"] from which to free a huge page. Advance the
1445 * next node id whether or not we find a free huge page to free so
1446 * that the next attempt to free addresses the next node.
1447 */
1448static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
1449{
1450 int nid;
1451
1452 VM_BUG_ON(!nodes_allowed);
1453
1454 nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
1455 h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
1456
1457 return nid;
1458}
1459
1460#define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
1461 for (nr_nodes = nodes_weight(*mask); \
1462 nr_nodes > 0 && \
1463 ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
1464 nr_nodes--)
1465
1466#define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
1467 for (nr_nodes = nodes_weight(*mask); \
1468 nr_nodes > 0 && \
1469 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1470 nr_nodes--)
1471
Mike Kravetz8531fc62021-11-05 13:41:33 -07001472/* used to demote non-gigantic_huge pages as well */
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001473static void __destroy_compound_gigantic_folio(struct folio *folio,
Mike Kravetz34d9e352021-11-05 13:41:30 -07001474 unsigned int order, bool demote)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001475{
1476 int i;
1477 int nr_pages = 1 << order;
Cheng Li14455ea2022-09-09 07:31:09 +00001478 struct page *p;
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001479
Matthew Wilcox (Oracle)46f27222023-01-11 14:28:59 +00001480 atomic_set(&folio->_entire_mapcount, 0);
Matthew Wilcox (Oracle)eec20422023-01-11 14:28:48 +00001481 atomic_set(&folio->_nr_pages_mapped, 0);
Matthew Wilcox (Oracle)94688e82023-01-11 14:28:47 +00001482 atomic_set(&folio->_pincount, 0);
John Hubbard47e29d32020-04-01 21:05:33 -07001483
Cheng Li14455ea2022-09-09 07:31:09 +00001484 for (i = 1; i < nr_pages; i++) {
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001485 p = folio_page(folio, i);
Mike Kravetza01f4392021-11-05 13:41:27 -07001486 p->mapping = NULL;
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001487 clear_compound_head(p);
Mike Kravetz34d9e352021-11-05 13:41:30 -07001488 if (!demote)
1489 set_page_refcounted(p);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001490 }
1491
Sidhartha Kumar04a42e72022-12-14 22:17:57 -08001492 folio_set_order(folio, 0);
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001493 __folio_clear_head(folio);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001494}
1495
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001496static void destroy_compound_hugetlb_folio_for_demote(struct folio *folio,
Mike Kravetz8531fc62021-11-05 13:41:33 -07001497 unsigned int order)
1498{
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001499 __destroy_compound_gigantic_folio(folio, order, true);
Mike Kravetz8531fc62021-11-05 13:41:33 -07001500}
1501
1502#ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001503static void destroy_compound_gigantic_folio(struct folio *folio,
Mike Kravetz34d9e352021-11-05 13:41:30 -07001504 unsigned int order)
1505{
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001506 __destroy_compound_gigantic_folio(folio, order, false);
Mike Kravetz34d9e352021-11-05 13:41:30 -07001507}
1508
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001509static void free_gigantic_folio(struct folio *folio, unsigned int order)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001510{
Roman Gushchincf11e852020-04-10 14:32:45 -07001511 /*
1512 * If the page isn't allocated using the cma allocator,
1513 * cma_release() returns false.
1514 */
Barry Songdbda8fe2020-07-23 21:15:30 -07001515#ifdef CONFIG_CMA
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001516 int nid = folio_nid(folio);
1517
1518 if (cma_release(hugetlb_cma[nid], &folio->page, 1 << order))
Roman Gushchincf11e852020-04-10 14:32:45 -07001519 return;
Barry Songdbda8fe2020-07-23 21:15:30 -07001520#endif
Roman Gushchincf11e852020-04-10 14:32:45 -07001521
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001522 free_contig_range(folio_pfn(folio), 1 << order);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001523}
1524
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001525#ifdef CONFIG_CONTIG_ALLOC
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001526static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
Michal Hockod9cc948f2018-01-31 16:20:44 -08001527 int nid, nodemask_t *nodemask)
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001528{
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001529 struct page *page;
Miaohe Lin04adbc32021-05-04 18:33:22 -07001530 unsigned long nr_pages = pages_per_huge_page(h);
Li Xinhai953f0642020-09-04 16:36:10 -07001531 if (nid == NUMA_NO_NODE)
1532 nid = numa_mem_id();
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001533
Barry Songdbda8fe2020-07-23 21:15:30 -07001534#ifdef CONFIG_CMA
1535 {
Roman Gushchincf11e852020-04-10 14:32:45 -07001536 int node;
1537
Li Xinhai953f0642020-09-04 16:36:10 -07001538 if (hugetlb_cma[nid]) {
1539 page = cma_alloc(hugetlb_cma[nid], nr_pages,
1540 huge_page_order(h), true);
Roman Gushchincf11e852020-04-10 14:32:45 -07001541 if (page)
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001542 return page_folio(page);
Roman Gushchincf11e852020-04-10 14:32:45 -07001543 }
Li Xinhai953f0642020-09-04 16:36:10 -07001544
1545 if (!(gfp_mask & __GFP_THISNODE)) {
1546 for_each_node_mask(node, *nodemask) {
1547 if (node == nid || !hugetlb_cma[node])
1548 continue;
1549
1550 page = cma_alloc(hugetlb_cma[node], nr_pages,
1551 huge_page_order(h), true);
1552 if (page)
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001553 return page_folio(page);
Li Xinhai953f0642020-09-04 16:36:10 -07001554 }
1555 }
Roman Gushchincf11e852020-04-10 14:32:45 -07001556 }
Barry Songdbda8fe2020-07-23 21:15:30 -07001557#endif
Roman Gushchincf11e852020-04-10 14:32:45 -07001558
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001559 page = alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask);
1560 return page ? page_folio(page) : NULL;
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001561}
1562
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001563#else /* !CONFIG_CONTIG_ALLOC */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001564static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001565 int nid, nodemask_t *nodemask)
1566{
1567 return NULL;
1568}
1569#endif /* CONFIG_CONTIG_ALLOC */
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001570
Aneesh Kumar K.Ve1073d12017-07-06 15:39:17 -07001571#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08001572static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001573 int nid, nodemask_t *nodemask)
1574{
1575 return NULL;
1576}
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001577static inline void free_gigantic_folio(struct folio *folio,
1578 unsigned int order) { }
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001579static inline void destroy_compound_gigantic_folio(struct folio *folio,
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001580 unsigned int order) { }
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001581#endif
1582
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001583/*
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001584 * Remove hugetlb folio from lists, and update dtor so that the folio appears
Mike Kravetz34d9e352021-11-05 13:41:30 -07001585 * as just a compound page.
1586 *
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001587 * A reference is held on the folio, except in the case of demote.
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001588 *
1589 * Must be called with hugetlb lock held.
1590 */
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001591static void __remove_hugetlb_folio(struct hstate *h, struct folio *folio,
Mike Kravetz34d9e352021-11-05 13:41:30 -07001592 bool adjust_surplus,
1593 bool demote)
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001594{
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001595 int nid = folio_nid(folio);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001596
Sidhartha Kumarf0747322022-11-01 15:30:52 -07001597 VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio(folio), folio);
1598 VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio_rsvd(folio), folio);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001599
Mike Kravetz9487ca62021-05-04 18:35:10 -07001600 lockdep_assert_held(&hugetlb_lock);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001601 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
1602 return;
1603
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001604 list_del(&folio->lru);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001605
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001606 if (folio_test_hugetlb_freed(folio)) {
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001607 h->free_huge_pages--;
1608 h->free_huge_pages_node[nid]--;
1609 }
1610 if (adjust_surplus) {
1611 h->surplus_huge_pages--;
1612 h->surplus_huge_pages_node[nid]--;
1613 }
1614
Mike Kravetze32d20c2021-09-02 14:58:50 -07001615 /*
1616 * Very subtle
1617 *
1618 * For non-gigantic pages set the destructor to the normal compound
1619 * page dtor. This is needed in case someone takes an additional
1620 * temporary ref to the page, and freeing is delayed until they drop
1621 * their reference.
1622 *
1623 * For gigantic pages set the destructor to the null dtor. This
1624 * destructor will never be called. Before freeing the gigantic
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001625 * page destroy_compound_gigantic_folio will turn the folio into a
1626 * simple group of pages. After this the destructor does not
Mike Kravetze32d20c2021-09-02 14:58:50 -07001627 * apply.
1628 *
1629 * This handles the case where more than one ref is held when and
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001630 * after update_and_free_hugetlb_folio is called.
Mike Kravetz34d9e352021-11-05 13:41:30 -07001631 *
1632 * In the case of demote we do not ref count the page as it will soon
1633 * be turned into a page of smaller size.
Mike Kravetze32d20c2021-09-02 14:58:50 -07001634 */
Mike Kravetz34d9e352021-11-05 13:41:30 -07001635 if (!demote)
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001636 folio_ref_unfreeze(folio, 1);
Mike Kravetze32d20c2021-09-02 14:58:50 -07001637 if (hstate_is_gigantic(h))
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001638 folio_set_compound_dtor(folio, NULL_COMPOUND_DTOR);
Mike Kravetze32d20c2021-09-02 14:58:50 -07001639 else
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001640 folio_set_compound_dtor(folio, COMPOUND_PAGE_DTOR);
Mike Kravetz6eb4e882021-05-04 18:34:55 -07001641
1642 h->nr_huge_pages--;
1643 h->nr_huge_pages_node[nid]--;
1644}
1645
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001646static void remove_hugetlb_folio(struct hstate *h, struct folio *folio,
Mike Kravetz34d9e352021-11-05 13:41:30 -07001647 bool adjust_surplus)
1648{
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001649 __remove_hugetlb_folio(h, folio, adjust_surplus, false);
Mike Kravetz34d9e352021-11-05 13:41:30 -07001650}
1651
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001652static void remove_hugetlb_folio_for_demote(struct hstate *h, struct folio *folio,
Mike Kravetz8531fc62021-11-05 13:41:33 -07001653 bool adjust_surplus)
1654{
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001655 __remove_hugetlb_folio(h, folio, adjust_surplus, true);
Mike Kravetz8531fc62021-11-05 13:41:33 -07001656}
1657
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001658static void add_hugetlb_folio(struct hstate *h, struct folio *folio,
Muchun Songad2fa372021-06-30 18:47:21 -07001659 bool adjust_surplus)
1660{
1661 int zeroed;
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001662 int nid = folio_nid(folio);
Muchun Songad2fa372021-06-30 18:47:21 -07001663
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001664 VM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio);
Muchun Songad2fa372021-06-30 18:47:21 -07001665
1666 lockdep_assert_held(&hugetlb_lock);
1667
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001668 INIT_LIST_HEAD(&folio->lru);
Muchun Songad2fa372021-06-30 18:47:21 -07001669 h->nr_huge_pages++;
1670 h->nr_huge_pages_node[nid]++;
1671
1672 if (adjust_surplus) {
1673 h->surplus_huge_pages++;
1674 h->surplus_huge_pages_node[nid]++;
1675 }
1676
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001677 folio_set_compound_dtor(folio, HUGETLB_PAGE_DTOR);
1678 folio_change_private(folio, NULL);
Miaohe Lina9e1eab2022-09-01 20:00:28 +08001679 /*
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001680 * We have to set hugetlb_vmemmap_optimized again as above
1681 * folio_change_private(folio, NULL) cleared it.
Miaohe Lina9e1eab2022-09-01 20:00:28 +08001682 */
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001683 folio_set_hugetlb_vmemmap_optimized(folio);
Muchun Songad2fa372021-06-30 18:47:21 -07001684
1685 /*
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001686 * This folio is about to be managed by the hugetlb allocator and
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07001687 * should have no users. Drop our reference, and check for others
1688 * just in case.
Muchun Songad2fa372021-06-30 18:47:21 -07001689 */
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001690 zeroed = folio_put_testzero(folio);
1691 if (unlikely(!zeroed))
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07001692 /*
1693 * It is VERY unlikely soneone else has taken a ref on
1694 * the page. In this case, we simply return as the
1695 * hugetlb destructor (free_huge_page) will be called
1696 * when this other ref is dropped.
1697 */
1698 return;
1699
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001700 arch_clear_hugepage_flags(&folio->page);
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001701 enqueue_hugetlb_folio(h, folio);
Muchun Songad2fa372021-06-30 18:47:21 -07001702}
1703
Sidhartha Kumar6f6956c2023-01-13 16:30:51 -06001704static void __update_and_free_hugetlb_folio(struct hstate *h,
1705 struct folio *folio)
Adam Litke6af2acb2007-10-16 01:26:16 -07001706{
1707 int i;
Cheng Li14455ea2022-09-09 07:31:09 +00001708 struct page *subpage;
Andi Kleena5516432008-07-23 21:27:41 -07001709
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07001710 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001711 return;
Andy Whitcroft18229df2008-11-06 12:53:27 -08001712
Naoya Horiguchi161df602022-07-14 13:24:15 +09001713 /*
1714 * If we don't know which subpages are hwpoisoned, we can't free
1715 * the hugepage, so it's leaked intentionally.
1716 */
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001717 if (folio_test_hugetlb_raw_hwp_unreliable(folio))
Naoya Horiguchi161df602022-07-14 13:24:15 +09001718 return;
1719
Sidhartha Kumar6f6956c2023-01-13 16:30:51 -06001720 if (hugetlb_vmemmap_restore(h, &folio->page)) {
Muchun Songad2fa372021-06-30 18:47:21 -07001721 spin_lock_irq(&hugetlb_lock);
1722 /*
1723 * If we cannot allocate vmemmap pages, just refuse to free the
1724 * page and put the page back on the hugetlb free list and treat
1725 * as a surplus page.
1726 */
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001727 add_hugetlb_folio(h, folio, true);
Muchun Songad2fa372021-06-30 18:47:21 -07001728 spin_unlock_irq(&hugetlb_lock);
1729 return;
1730 }
1731
Naoya Horiguchi161df602022-07-14 13:24:15 +09001732 /*
1733 * Move PageHWPoison flag from head page to the raw error pages,
1734 * which makes any healthy subpages reusable.
1735 */
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001736 if (unlikely(folio_test_hwpoison(folio)))
Sidhartha Kumar2ff6cec2023-01-12 14:46:03 -06001737 folio_clear_hugetlb_hwpoison(folio);
Naoya Horiguchi161df602022-07-14 13:24:15 +09001738
Cheng Li14455ea2022-09-09 07:31:09 +00001739 for (i = 0; i < pages_per_huge_page(h); i++) {
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001740 subpage = folio_page(folio, i);
Mike Kravetzdbfee5a2021-02-24 12:07:50 -08001741 subpage->flags &= ~(1 << PG_locked | 1 << PG_error |
Chris Forbes32f84522011-07-25 17:12:14 -07001742 1 << PG_referenced | 1 << PG_dirty |
Luiz Capitulinoa7407a22014-06-04 16:07:09 -07001743 1 << PG_active | 1 << PG_private |
1744 1 << PG_writeback);
Adam Litke6af2acb2007-10-16 01:26:16 -07001745 }
Mike Kravetza01f4392021-11-05 13:41:27 -07001746
1747 /*
1748 * Non-gigantic pages demoted from CMA allocated gigantic pages
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001749 * need to be given back to CMA in free_gigantic_folio.
Mike Kravetza01f4392021-11-05 13:41:27 -07001750 */
1751 if (hstate_is_gigantic(h) ||
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08001752 hugetlb_cma_folio(folio, huge_page_order(h))) {
Sidhartha Kumar911565b2022-11-29 14:50:31 -08001753 destroy_compound_gigantic_folio(folio, huge_page_order(h));
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08001754 free_gigantic_folio(folio, huge_page_order(h));
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001755 } else {
Sidhartha Kumar6f6956c2023-01-13 16:30:51 -06001756 __free_pages(&folio->page, huge_page_order(h));
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001757 }
Adam Litke6af2acb2007-10-16 01:26:16 -07001758}
1759
Muchun Songb65d4ad2021-06-30 18:47:17 -07001760/*
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001761 * As update_and_free_hugetlb_folio() can be called under any context, so we cannot
Muchun Songb65d4ad2021-06-30 18:47:17 -07001762 * use GFP_KERNEL to allocate vmemmap pages. However, we can defer the
1763 * actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate
1764 * the vmemmap pages.
1765 *
1766 * free_hpage_workfn() locklessly retrieves the linked list of pages to be
1767 * freed and frees them one-by-one. As the page->mapping pointer is going
1768 * to be cleared in free_hpage_workfn() anyway, it is reused as the llist_node
1769 * structure of a lockless linked list of huge pages to be freed.
1770 */
1771static LLIST_HEAD(hpage_freelist);
1772
1773static void free_hpage_workfn(struct work_struct *work)
1774{
1775 struct llist_node *node;
1776
1777 node = llist_del_all(&hpage_freelist);
1778
1779 while (node) {
1780 struct page *page;
1781 struct hstate *h;
1782
1783 page = container_of((struct address_space **)node,
1784 struct page, mapping);
1785 node = node->next;
1786 page->mapping = NULL;
1787 /*
1788 * The VM_BUG_ON_PAGE(!PageHuge(page), page) in page_hstate()
1789 * is going to trigger because a previous call to
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001790 * remove_hugetlb_folio() will call folio_set_compound_dtor
1791 * (folio, NULL_COMPOUND_DTOR), so do not use page_hstate()
1792 * directly.
Muchun Songb65d4ad2021-06-30 18:47:17 -07001793 */
1794 h = size_to_hstate(page_size(page));
1795
Sidhartha Kumar6f6956c2023-01-13 16:30:51 -06001796 __update_and_free_hugetlb_folio(h, page_folio(page));
Muchun Songb65d4ad2021-06-30 18:47:17 -07001797
1798 cond_resched();
1799 }
1800}
1801static DECLARE_WORK(free_hpage_work, free_hpage_workfn);
1802
1803static inline void flush_free_hpage_work(struct hstate *h)
1804{
Muchun Song62138342022-06-28 17:22:33 +08001805 if (hugetlb_vmemmap_optimizable(h))
Muchun Songb65d4ad2021-06-30 18:47:17 -07001806 flush_work(&free_hpage_work);
1807}
1808
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001809static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio,
Muchun Songb65d4ad2021-06-30 18:47:17 -07001810 bool atomic)
1811{
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001812 if (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) {
Sidhartha Kumar6f6956c2023-01-13 16:30:51 -06001813 __update_and_free_hugetlb_folio(h, folio);
Muchun Songb65d4ad2021-06-30 18:47:17 -07001814 return;
1815 }
1816
1817 /*
1818 * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap pages.
1819 *
1820 * Only call schedule_work() if hpage_freelist is previously
1821 * empty. Otherwise, schedule_work() had been called but the workfn
1822 * hasn't retrieved the list yet.
1823 */
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001824 if (llist_add((struct llist_node *)&folio->mapping, &hpage_freelist))
Muchun Songb65d4ad2021-06-30 18:47:17 -07001825 schedule_work(&free_hpage_work);
1826}
1827
Mike Kravetz10c6ec42021-05-04 18:35:03 -07001828static void update_and_free_pages_bulk(struct hstate *h, struct list_head *list)
1829{
1830 struct page *page, *t_page;
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001831 struct folio *folio;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07001832
1833 list_for_each_entry_safe(page, t_page, list, lru) {
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001834 folio = page_folio(page);
1835 update_and_free_hugetlb_folio(h, folio, false);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07001836 cond_resched();
1837 }
1838}
1839
Andi Kleene5ff2152008-07-23 21:27:42 -07001840struct hstate *size_to_hstate(unsigned long size)
1841{
1842 struct hstate *h;
1843
1844 for_each_hstate(h) {
1845 if (huge_page_size(h) == size)
1846 return h;
1847 }
1848 return NULL;
1849}
1850
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001851void free_huge_page(struct page *page)
David Gibson27a85ef2006-03-22 00:08:56 -08001852{
Andi Kleena5516432008-07-23 21:27:41 -07001853 /*
1854 * Can't pass hstate in here because it is called from the
1855 * compound page destructor.
1856 */
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001857 struct folio *folio = page_folio(page);
1858 struct hstate *h = folio_hstate(folio);
1859 int nid = folio_nid(folio);
1860 struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
Joonsoo Kim07443a82013-09-11 14:21:58 -07001861 bool restore_reserve;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001862 unsigned long flags;
David Gibson27a85ef2006-03-22 00:08:56 -08001863
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001864 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
1865 VM_BUG_ON_FOLIO(folio_mapcount(folio), folio);
Yongkai Wu8ace22b2018-12-14 14:17:10 -08001866
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001867 hugetlb_set_folio_subpool(folio, NULL);
1868 if (folio_test_anon(folio))
1869 __ClearPageAnonExclusive(&folio->page);
1870 folio->mapping = NULL;
1871 restore_reserve = folio_test_hugetlb_restore_reserve(folio);
1872 folio_clear_hugetlb_restore_reserve(folio);
David Gibson27a85ef2006-03-22 00:08:56 -08001873
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001874 /*
Mike Kravetzd6995da2021-02-24 12:08:51 -08001875 * If HPageRestoreReserve was set on page, page allocation consumed a
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001876 * reservation. If the page was associated with a subpool, there
1877 * would have been a page reserved in the subpool before allocation
1878 * via hugepage_subpool_get_pages(). Since we are 'restoring' the
Miaohe Lin6c26d312021-02-24 12:07:19 -08001879 * reservation, do not call hugepage_subpool_put_pages() as this will
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001880 * remove the reserved page from the subpool.
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001881 */
Mike Kravetz0919e1b2019-05-13 17:19:38 -07001882 if (!restore_reserve) {
1883 /*
1884 * A return code of zero implies that the subpool will be
1885 * under its minimum size if the reservation is not restored
1886 * after page is free. Therefore, force restore_reserve
1887 * operation.
1888 */
1889 if (hugepage_subpool_put_pages(spool, 1) == 0)
1890 restore_reserve = true;
1891 }
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001892
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001893 spin_lock_irqsave(&hugetlb_lock, flags);
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001894 folio_clear_hugetlb_migratable(folio);
Sidhartha Kumard4ab0312022-11-01 15:30:57 -07001895 hugetlb_cgroup_uncharge_folio(hstate_index(h),
1896 pages_per_huge_page(h), folio);
1897 hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h),
1898 pages_per_huge_page(h), folio);
Joonsoo Kim07443a82013-09-11 14:21:58 -07001899 if (restore_reserve)
1900 h->resv_huge_pages++;
1901
Sidhartha Kumar0356c4b2022-11-01 15:30:56 -07001902 if (folio_test_hugetlb_temporary(folio)) {
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001903 remove_hugetlb_folio(h, folio, false);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001904 spin_unlock_irqrestore(&hugetlb_lock, flags);
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001905 update_and_free_hugetlb_folio(h, folio, true);
Michal Hockoab5ac902018-01-31 16:20:48 -08001906 } else if (h->surplus_huge_pages_node[nid]) {
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07001907 /* remove the page from active list */
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08001908 remove_hugetlb_folio(h, folio, true);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001909 spin_unlock_irqrestore(&hugetlb_lock, flags);
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08001910 update_and_free_hugetlb_folio(h, folio, true);
Adam Litke7893d1d2007-10-16 01:26:18 -07001911 } else {
Will Deacon5d3a5512012-10-08 16:29:32 -07001912 arch_clear_hugepage_flags(page);
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08001913 enqueue_hugetlb_folio(h, folio);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001914 spin_unlock_irqrestore(&hugetlb_lock, flags);
Adam Litke7893d1d2007-10-16 01:26:18 -07001915 }
David Gibson27a85ef2006-03-22 00:08:56 -08001916}
1917
Oscar Salvadord3d99fc2021-05-04 18:35:23 -07001918/*
1919 * Must be called with the hugetlb lock held
1920 */
1921static void __prep_account_new_huge_page(struct hstate *h, int nid)
1922{
1923 lockdep_assert_held(&hugetlb_lock);
1924 h->nr_huge_pages++;
1925 h->nr_huge_pages_node[nid]++;
1926}
1927
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07001928static void __prep_new_hugetlb_folio(struct hstate *h, struct folio *folio)
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001929{
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07001930 hugetlb_vmemmap_optimize(h, &folio->page);
1931 INIT_LIST_HEAD(&folio->lru);
Sidhartha Kumar9fd33052022-11-29 14:50:30 -08001932 folio_set_compound_dtor(folio, HUGETLB_PAGE_DTOR);
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07001933 hugetlb_set_folio_subpool(folio, NULL);
1934 set_hugetlb_cgroup(folio, NULL);
1935 set_hugetlb_cgroup_rsvd(folio, NULL);
Oscar Salvadord3d99fc2021-05-04 18:35:23 -07001936}
1937
Sidhartha Kumard1c60952022-11-29 14:50:38 -08001938static void prep_new_hugetlb_folio(struct hstate *h, struct folio *folio, int nid)
Oscar Salvadord3d99fc2021-05-04 18:35:23 -07001939{
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07001940 __prep_new_hugetlb_folio(h, folio);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001941 spin_lock_irq(&hugetlb_lock);
Oscar Salvadord3d99fc2021-05-04 18:35:23 -07001942 __prep_account_new_huge_page(h, nid);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07001943 spin_unlock_irq(&hugetlb_lock);
Andi Kleenb7ba30c2008-07-23 21:27:40 -07001944}
1945
Sidhartha Kumard1c60952022-11-29 14:50:38 -08001946static bool __prep_compound_gigantic_folio(struct folio *folio,
1947 unsigned int order, bool demote)
Wu Fengguang20a03072009-06-16 15:32:22 -07001948{
Mike Kravetz7118fc22021-06-30 18:48:34 -07001949 int i, j;
Wu Fengguang20a03072009-06-16 15:32:22 -07001950 int nr_pages = 1 << order;
Cheng Li14455ea2022-09-09 07:31:09 +00001951 struct page *p;
Wu Fengguang20a03072009-06-16 15:32:22 -07001952
Sidhartha Kumard1c60952022-11-29 14:50:38 -08001953 __folio_clear_reserved(folio);
1954 __folio_set_head(folio);
Sidhartha Kumarc45bc55a2022-12-12 14:55:29 -08001955 /* we rely on prep_new_hugetlb_folio to set the destructor */
Sidhartha Kumar04a42e72022-12-14 22:17:57 -08001956 folio_set_order(folio, order);
Mike Kravetz2b216242022-09-16 14:46:38 -07001957 for (i = 0; i < nr_pages; i++) {
Sidhartha Kumard1c60952022-11-29 14:50:38 -08001958 p = folio_page(folio, i);
Cheng Li14455ea2022-09-09 07:31:09 +00001959
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001960 /*
1961 * For gigantic hugepages allocated through bootmem at
1962 * boot, it's safer to be consistent with the not-gigantic
1963 * hugepages and clear the PG_reserved bit from all tail pages
Ethon Paul7c8de352020-06-04 16:49:07 -07001964 * too. Otherwise drivers using get_user_pages() to access tail
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001965 * pages may get the reference counting wrong if they see
1966 * PG_reserved set on a tail page (despite the head page not
1967 * having PG_reserved set). Enforcing this consistency between
1968 * head and tail pages allows drivers to optimize away a check
1969 * on the head page when they need know if put_page() is needed
1970 * after get_user_pages().
1971 */
Mike Kravetz7fb07282022-11-18 11:52:49 -08001972 if (i != 0) /* head page cleared above */
1973 __ClearPageReserved(p);
Mike Kravetz7118fc22021-06-30 18:48:34 -07001974 /*
1975 * Subtle and very unlikely
1976 *
1977 * Gigantic 'page allocators' such as memblock or cma will
1978 * return a set of pages with each page ref counted. We need
1979 * to turn this set of pages into a compound page with tail
1980 * page ref counts set to zero. Code such as speculative page
1981 * cache adding could take a ref on a 'to be' tail page.
1982 * We need to respect any increased ref count, and only set
1983 * the ref count to zero if count is currently 1. If count
Mike Kravetz416d85e2021-09-02 14:58:43 -07001984 * is not 1, we return an error. An error return indicates
1985 * the set of pages can not be converted to a gigantic page.
1986 * The caller who allocated the pages should then discard the
1987 * pages using the appropriate free interface.
Mike Kravetz34d9e352021-11-05 13:41:30 -07001988 *
1989 * In the case of demote, the ref count will be zero.
Mike Kravetz7118fc22021-06-30 18:48:34 -07001990 */
Mike Kravetz34d9e352021-11-05 13:41:30 -07001991 if (!demote) {
1992 if (!page_ref_freeze(p, 1)) {
1993 pr_warn("HugeTLB page can not be used due to unexpected inflated ref count\n");
1994 goto out_error;
1995 }
1996 } else {
1997 VM_BUG_ON_PAGE(page_count(p), p);
Mike Kravetz7118fc22021-06-30 18:48:34 -07001998 }
Mike Kravetz2b216242022-09-16 14:46:38 -07001999 if (i != 0)
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002000 set_compound_head(p, &folio->page);
Wu Fengguang20a03072009-06-16 15:32:22 -07002001 }
Matthew Wilcox (Oracle)46f27222023-01-11 14:28:59 +00002002 atomic_set(&folio->_entire_mapcount, -1);
Matthew Wilcox (Oracle)eec20422023-01-11 14:28:48 +00002003 atomic_set(&folio->_nr_pages_mapped, 0);
Matthew Wilcox (Oracle)94688e82023-01-11 14:28:47 +00002004 atomic_set(&folio->_pincount, 0);
Mike Kravetz7118fc22021-06-30 18:48:34 -07002005 return true;
2006
2007out_error:
Mike Kravetz2b216242022-09-16 14:46:38 -07002008 /* undo page modifications made above */
2009 for (j = 0; j < i; j++) {
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002010 p = folio_page(folio, j);
Mike Kravetz2b216242022-09-16 14:46:38 -07002011 if (j != 0)
2012 clear_compound_head(p);
Mike Kravetz7118fc22021-06-30 18:48:34 -07002013 set_page_refcounted(p);
2014 }
2015 /* need to clear PG_reserved on remaining tail pages */
Cheng Li14455ea2022-09-09 07:31:09 +00002016 for (; j < nr_pages; j++) {
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002017 p = folio_page(folio, j);
Mike Kravetz7118fc22021-06-30 18:48:34 -07002018 __ClearPageReserved(p);
Cheng Li14455ea2022-09-09 07:31:09 +00002019 }
Sidhartha Kumar04a42e72022-12-14 22:17:57 -08002020 folio_set_order(folio, 0);
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002021 __folio_clear_head(folio);
Mike Kravetz7118fc22021-06-30 18:48:34 -07002022 return false;
Wu Fengguang20a03072009-06-16 15:32:22 -07002023}
2024
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002025static bool prep_compound_gigantic_folio(struct folio *folio,
Mike Kravetz8531fc62021-11-05 13:41:33 -07002026 unsigned int order)
2027{
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002028 return __prep_compound_gigantic_folio(folio, order, false);
2029}
2030
2031static bool prep_compound_gigantic_folio_for_demote(struct folio *folio,
2032 unsigned int order)
2033{
2034 return __prep_compound_gigantic_folio(folio, order, true);
Mike Kravetz8531fc62021-11-05 13:41:33 -07002035}
2036
Andrew Morton77959122012-10-08 16:34:11 -07002037/*
2038 * PageHuge() only returns true for hugetlbfs pages, but not for normal or
2039 * transparent huge pages. See the PageTransHuge() documentation for more
2040 * details.
2041 */
Wu Fengguang20a03072009-06-16 15:32:22 -07002042int PageHuge(struct page *page)
2043{
Matthew Wilcox (Oracle)2d678c62023-01-11 14:29:07 +00002044 struct folio *folio;
2045
Wu Fengguang20a03072009-06-16 15:32:22 -07002046 if (!PageCompound(page))
2047 return 0;
Matthew Wilcox (Oracle)2d678c62023-01-11 14:29:07 +00002048 folio = page_folio(page);
2049 return folio->_folio_dtor == HUGETLB_PAGE_DTOR;
Wu Fengguang20a03072009-06-16 15:32:22 -07002050}
Naoya Horiguchi43131e12010-05-28 09:29:22 +09002051EXPORT_SYMBOL_GPL(PageHuge);
2052
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08002053/*
2054 * PageHeadHuge() only returns true for hugetlbfs head page, but not for
2055 * normal or transparent huge pages.
2056 */
2057int PageHeadHuge(struct page *page_head)
2058{
Matthew Wilcox (Oracle)2d678c62023-01-11 14:29:07 +00002059 struct folio *folio = (struct folio *)page_head;
2060 if (!folio_test_large(folio))
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08002061 return 0;
2062
Matthew Wilcox (Oracle)2d678c62023-01-11 14:29:07 +00002063 return folio->_folio_dtor == HUGETLB_PAGE_DTOR;
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08002064}
David Howells4e936ec2022-03-22 14:45:26 -07002065EXPORT_SYMBOL_GPL(PageHeadHuge);
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08002066
Mike Kravetzc0d03812020-04-01 21:11:05 -07002067/*
Mike Kravetzc0d03812020-04-01 21:11:05 -07002068 * Find and lock address space (mapping) in write mode.
2069 *
Mike Kravetz336bf302020-11-13 22:52:16 -08002070 * Upon entry, the page is locked which means that page_mapping() is
2071 * stable. Due to locking order, we can only trylock_write. If we can
2072 * not get the lock, simply return NULL to caller.
Mike Kravetzc0d03812020-04-01 21:11:05 -07002073 */
2074struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage)
2075{
Mike Kravetz336bf302020-11-13 22:52:16 -08002076 struct address_space *mapping = page_mapping(hpage);
Mike Kravetzc0d03812020-04-01 21:11:05 -07002077
Mike Kravetzc0d03812020-04-01 21:11:05 -07002078 if (!mapping)
2079 return mapping;
2080
Mike Kravetzc0d03812020-04-01 21:11:05 -07002081 if (i_mmap_trylock_write(mapping))
2082 return mapping;
2083
Mike Kravetz336bf302020-11-13 22:52:16 -08002084 return NULL;
Mike Kravetzc0d03812020-04-01 21:11:05 -07002085}
2086
Hugh Dickinsfe19bd32021-06-24 18:39:52 -07002087pgoff_t hugetlb_basepage_index(struct page *page)
Zhang Yi13d60f42013-06-25 21:19:31 +08002088{
2089 struct page *page_head = compound_head(page);
2090 pgoff_t index = page_index(page_head);
2091 unsigned long compound_idx;
2092
Zhang Yi13d60f42013-06-25 21:19:31 +08002093 if (compound_order(page_head) >= MAX_ORDER)
2094 compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
2095 else
2096 compound_idx = page - page_head;
2097
2098 return (index << compound_order(page_head)) + compound_idx;
2099}
2100
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002101static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07002102 gfp_t gfp_mask, int nid, nodemask_t *nmask,
2103 nodemask_t *node_alloc_noretry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002105 int order = huge_page_order(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 struct page *page;
Mike Kravetzf60858f2019-09-23 15:37:35 -07002107 bool alloc_try_hard = true;
Mike Kravetz2b216242022-09-16 14:46:38 -07002108 bool retry = true;
Joe Jinf96efd52007-07-15 23:38:12 -07002109
Mike Kravetzf60858f2019-09-23 15:37:35 -07002110 /*
2111 * By default we always try hard to allocate the page with
2112 * __GFP_RETRY_MAYFAIL flag. However, if we are allocating pages in
2113 * a loop (to adjust global huge page counts) and previous allocation
2114 * failed, do not continue to try hard on the same node. Use the
2115 * node_alloc_noretry bitmap to manage this state information.
2116 */
2117 if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry))
2118 alloc_try_hard = false;
2119 gfp_mask |= __GFP_COMP|__GFP_NOWARN;
2120 if (alloc_try_hard)
2121 gfp_mask |= __GFP_RETRY_MAYFAIL;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002122 if (nid == NUMA_NO_NODE)
2123 nid = numa_mem_id();
Mike Kravetz2b216242022-09-16 14:46:38 -07002124retry:
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07002125 page = __alloc_pages(gfp_mask, order, nid, nmask);
Mike Kravetz2b216242022-09-16 14:46:38 -07002126
2127 /* Freeze head page */
2128 if (page && !page_ref_freeze(page, 1)) {
2129 __free_pages(page, order);
2130 if (retry) { /* retry once */
2131 retry = false;
2132 goto retry;
2133 }
2134 /* WOW! twice in a row. */
2135 pr_warn("HugeTLB head page unexpected inflated ref count\n");
2136 page = NULL;
2137 }
2138
Mike Kravetzf60858f2019-09-23 15:37:35 -07002139 /*
2140 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this
2141 * indicates an overall state change. Clear bit so that we resume
2142 * normal 'try hard' allocations.
2143 */
2144 if (node_alloc_noretry && page && !alloc_try_hard)
2145 node_clear(nid, *node_alloc_noretry);
2146
2147 /*
2148 * If we tried hard to get a page but failed, set bit so that
2149 * subsequent attempts will not try as hard until there is an
2150 * overall state change.
2151 */
2152 if (node_alloc_noretry && !page && alloc_try_hard)
2153 node_set(nid, *node_alloc_noretry);
2154
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002155 if (!page) {
2156 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
2157 return NULL;
2158 }
2159
2160 __count_vm_event(HTLB_BUDDY_PGALLOC);
2161 return page_folio(page);
Nishanth Aravamudan63b46132007-10-16 01:26:24 -07002162}
2163
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002164/*
Michal Hocko0c397da2018-01-31 16:20:56 -08002165 * Common helper to allocate a fresh hugetlb page. All specific allocators
2166 * should use this function to get new hugetlb pages
Mike Kravetz2b216242022-09-16 14:46:38 -07002167 *
2168 * Note that returned page is 'frozen': ref count of head page and all tail
2169 * pages is zero.
Michal Hocko0c397da2018-01-31 16:20:56 -08002170 */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002171static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07002172 gfp_t gfp_mask, int nid, nodemask_t *nmask,
2173 nodemask_t *node_alloc_noretry)
Michal Hocko0c397da2018-01-31 16:20:56 -08002174{
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08002175 struct folio *folio;
Mike Kravetz7118fc22021-06-30 18:48:34 -07002176 bool retry = false;
Michal Hocko0c397da2018-01-31 16:20:56 -08002177
Mike Kravetz7118fc22021-06-30 18:48:34 -07002178retry:
Michal Hocko0c397da2018-01-31 16:20:56 -08002179 if (hstate_is_gigantic(h))
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002180 folio = alloc_gigantic_folio(h, gfp_mask, nid, nmask);
Michal Hocko0c397da2018-01-31 16:20:56 -08002181 else
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002182 folio = alloc_buddy_hugetlb_folio(h, gfp_mask,
Mike Kravetzf60858f2019-09-23 15:37:35 -07002183 nid, nmask, node_alloc_noretry);
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002184 if (!folio)
Michal Hocko0c397da2018-01-31 16:20:56 -08002185 return NULL;
Mike Kravetz7118fc22021-06-30 18:48:34 -07002186 if (hstate_is_gigantic(h)) {
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002187 if (!prep_compound_gigantic_folio(folio, huge_page_order(h))) {
Mike Kravetz7118fc22021-06-30 18:48:34 -07002188 /*
2189 * Rare failure to convert pages to compound page.
2190 * Free pages and try again - ONCE!
2191 */
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08002192 free_gigantic_folio(folio, huge_page_order(h));
Mike Kravetz7118fc22021-06-30 18:48:34 -07002193 if (!retry) {
2194 retry = true;
2195 goto retry;
2196 }
Mike Kravetz7118fc22021-06-30 18:48:34 -07002197 return NULL;
2198 }
2199 }
Sidhartha Kumard1c60952022-11-29 14:50:38 -08002200 prep_new_hugetlb_folio(h, folio, folio_nid(folio));
Michal Hocko0c397da2018-01-31 16:20:56 -08002201
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002202 return folio;
Michal Hocko0c397da2018-01-31 16:20:56 -08002203}
2204
2205/*
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002206 * Allocates a fresh page to the hugetlb allocator pool in the node interleaved
2207 * manner.
2208 */
Mike Kravetzf60858f2019-09-23 15:37:35 -07002209static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
2210 nodemask_t *node_alloc_noretry)
Joonsoo Kimb2261022013-09-11 14:21:00 -07002211{
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002212 struct folio *folio;
Joonsoo Kimb2261022013-09-11 14:21:00 -07002213 int nr_nodes, node;
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08002214 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
Joonsoo Kimb2261022013-09-11 14:21:00 -07002215
2216 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002217 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, node,
2218 nodes_allowed, node_alloc_noretry);
2219 if (folio) {
2220 free_huge_page(&folio->page); /* free it into the hugepage allocator */
2221 return 1;
2222 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002223 }
2224
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002225 return 0;
Joonsoo Kimb2261022013-09-11 14:21:00 -07002226}
2227
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002228/*
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002229 * Remove huge page from pool from next node to free. Attempt to keep
2230 * persistent huge pages more or less balanced over allowed nodes.
2231 * This routine only 'removes' the hugetlb page. The caller must make
2232 * an additional call to free the page to low level allocators.
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002233 * Called with hugetlb_lock locked.
2234 */
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002235static struct page *remove_pool_huge_page(struct hstate *h,
2236 nodemask_t *nodes_allowed,
2237 bool acct_surplus)
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002238{
Joonsoo Kimb2261022013-09-11 14:21:00 -07002239 int nr_nodes, node;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002240 struct page *page = NULL;
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08002241 struct folio *folio;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002242
Mike Kravetz9487ca62021-05-04 18:35:10 -07002243 lockdep_assert_held(&hugetlb_lock);
Joonsoo Kimb2261022013-09-11 14:21:00 -07002244 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
Lee Schermerhorn685f3452009-09-21 17:01:23 -07002245 /*
2246 * If we're returning unused surplus pages, only examine
2247 * nodes with surplus pages.
2248 */
Joonsoo Kimb2261022013-09-11 14:21:00 -07002249 if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
2250 !list_empty(&h->hugepage_freelists[node])) {
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002251 page = list_entry(h->hugepage_freelists[node].next,
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002252 struct page, lru);
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08002253 folio = page_folio(page);
2254 remove_hugetlb_folio(h, folio, acct_surplus);
Lee Schermerhorn9a76db02009-12-14 17:58:15 -08002255 break;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002256 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07002257 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002258
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002259 return page;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07002260}
2261
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002262/*
2263 * Dissolve a given free hugepage into free buddy pages. This function does
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002264 * nothing for in-use hugepages and non-hugepages.
2265 * This function returns values like below:
2266 *
Muchun Songad2fa372021-06-30 18:47:21 -07002267 * -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages
2268 * when the system is under memory pressure and the feature of
2269 * freeing unused vmemmap pages associated with each hugetlb page
2270 * is enabled.
2271 * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
2272 * (allocated or reserved.)
2273 * 0: successfully dissolved free hugepages or the page is not a
2274 * hugepage (considered as already dissolved)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002275 */
Anshuman Khandualc3114a82017-07-10 15:47:41 -07002276int dissolve_free_huge_page(struct page *page)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002277{
Naoya Horiguchi6bc9b562018-08-23 17:00:38 -07002278 int rc = -EBUSY;
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002279 struct folio *folio = page_folio(page);
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002280
Muchun Song7ffddd42021-02-04 18:32:06 -08002281retry:
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002282 /* Not to disrupt normal path by vainly holding hugetlb_lock */
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002283 if (!folio_test_hugetlb(folio))
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002284 return 0;
2285
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002286 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002287 if (!folio_test_hugetlb(folio)) {
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002288 rc = 0;
2289 goto out;
2290 }
2291
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002292 if (!folio_ref_count(folio)) {
2293 struct hstate *h = folio_hstate(folio);
Xin Hao8346d692022-09-22 10:19:29 +08002294 if (!available_huge_pages(h))
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002295 goto out;
Muchun Song7ffddd42021-02-04 18:32:06 -08002296
2297 /*
2298 * We should make sure that the page is already on the free list
2299 * when it is dissolved.
2300 */
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002301 if (unlikely(!folio_test_hugetlb_freed(folio))) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002302 spin_unlock_irq(&hugetlb_lock);
Muchun Song7ffddd42021-02-04 18:32:06 -08002303 cond_resched();
2304
2305 /*
2306 * Theoretically, we should return -EBUSY when we
2307 * encounter this race. In fact, we have a chance
2308 * to successfully dissolve the page if we do a
2309 * retry. Because the race window is quite small.
2310 * If we seize this opportunity, it is an optimization
2311 * for increasing the success rate of dissolving page.
2312 */
2313 goto retry;
2314 }
2315
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08002316 remove_hugetlb_folio(h, folio, false);
zhong jiangc1470b32016-08-11 15:32:55 -07002317 h->max_huge_pages--;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002318 spin_unlock_irq(&hugetlb_lock);
Muchun Songad2fa372021-06-30 18:47:21 -07002319
2320 /*
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08002321 * Normally update_and_free_hugtlb_folio will allocate required vmemmmap
2322 * before freeing the page. update_and_free_hugtlb_folio will fail to
Muchun Songad2fa372021-06-30 18:47:21 -07002323 * free the page if it can not allocate required vmemmap. We
2324 * need to adjust max_huge_pages if the page is not freed.
2325 * Attempt to allocate vmemmmap here so that we can take
2326 * appropriate action on failure.
2327 */
Sidhartha Kumar1a7cdab2022-11-29 14:50:32 -08002328 rc = hugetlb_vmemmap_restore(h, &folio->page);
Muchun Songad2fa372021-06-30 18:47:21 -07002329 if (!rc) {
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08002330 update_and_free_hugetlb_folio(h, folio, false);
Muchun Songad2fa372021-06-30 18:47:21 -07002331 } else {
2332 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumar2f6c57d2022-11-29 14:50:35 -08002333 add_hugetlb_folio(h, folio, false);
Muchun Songad2fa372021-06-30 18:47:21 -07002334 h->max_huge_pages++;
2335 spin_unlock_irq(&hugetlb_lock);
2336 }
2337
2338 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002339 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002340out:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002341 spin_unlock_irq(&hugetlb_lock);
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002342 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002343}
2344
2345/*
2346 * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
2347 * make specified memory blocks removable from the system.
Gerald Schaefer2247bb32016-10-07 17:01:07 -07002348 * Note that this will dissolve a free gigantic hugepage completely, if any
2349 * part of it lies within the given range.
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002350 * Also note that if dissolve_free_huge_page() returns with an error, all
2351 * free hugepages that were dissolved before that error are lost.
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002352 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002353int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002354{
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002355 unsigned long pfn;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07002356 struct page *page;
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002357 int rc = 0;
Muchun Songdc2628f2022-06-16 11:38:46 +08002358 unsigned int order;
2359 struct hstate *h;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002360
Li Zhongd0177632014-08-06 16:07:56 -07002361 if (!hugepages_supported())
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002362 return rc;
Li Zhongd0177632014-08-06 16:07:56 -07002363
Muchun Songdc2628f2022-06-16 11:38:46 +08002364 order = huge_page_order(&default_hstate);
2365 for_each_hstate(h)
2366 order = min(order, huge_page_order(h));
2367
2368 for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) {
Gerald Schaefereb03aa02016-10-07 17:01:13 -07002369 page = pfn_to_page(pfn);
Naoya Horiguchifaf53de2019-06-28 12:06:56 -07002370 rc = dissolve_free_huge_page(page);
2371 if (rc)
2372 break;
Gerald Schaefereb03aa02016-10-07 17:01:13 -07002373 }
Gerald Schaefer082d5b62016-10-07 17:01:10 -07002374
2375 return rc;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07002376}
2377
Michal Hockoab5ac902018-01-31 16:20:48 -08002378/*
2379 * Allocates a fresh surplus page from the page allocator.
2380 */
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06002381static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h,
2382 gfp_t gfp_mask, int nid, nodemask_t *nmask)
Adam Litke7893d1d2007-10-16 01:26:18 -07002383{
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002384 struct folio *folio = NULL;
Adam Litke7893d1d2007-10-16 01:26:18 -07002385
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002386 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07002387 return NULL;
2388
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002389 spin_lock_irq(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08002390 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages)
2391 goto out_unlock;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002392 spin_unlock_irq(&hugetlb_lock);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002393
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002394 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL);
2395 if (!folio)
Michal Hocko0c397da2018-01-31 16:20:56 -08002396 return NULL;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08002397
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002398 spin_lock_irq(&hugetlb_lock);
Michal Hocko9980d742018-01-31 16:20:52 -08002399 /*
2400 * We could have raced with the pool size change.
2401 * Double check that and simply deallocate the new page
2402 * if we would end up overcommiting the surpluses. Abuse
2403 * temporary page to workaround the nasty free_huge_page
2404 * codeflow
2405 */
2406 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002407 folio_set_hugetlb_temporary(folio);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002408 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002409 free_huge_page(&folio->page);
Kai Shen2bf753e2019-05-13 17:15:37 -07002410 return NULL;
Adam Litke7893d1d2007-10-16 01:26:18 -07002411 }
Michal Hocko9980d742018-01-31 16:20:52 -08002412
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002413 h->surplus_huge_pages++;
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002414 h->surplus_huge_pages_node[folio_nid(folio)]++;
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002415
Michal Hocko9980d742018-01-31 16:20:52 -08002416out_unlock:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002417 spin_unlock_irq(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07002418
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06002419 return folio;
Adam Litke7893d1d2007-10-16 01:26:18 -07002420}
2421
Sidhartha Kumare37d3e82023-01-13 16:30:55 -06002422static struct folio *alloc_migrate_hugetlb_folio(struct hstate *h, gfp_t gfp_mask,
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08002423 int nid, nodemask_t *nmask)
Michal Hockoab5ac902018-01-31 16:20:48 -08002424{
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002425 struct folio *folio;
Michal Hockoab5ac902018-01-31 16:20:48 -08002426
2427 if (hstate_is_gigantic(h))
2428 return NULL;
2429
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002430 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL);
2431 if (!folio)
Michal Hockoab5ac902018-01-31 16:20:48 -08002432 return NULL;
2433
Mike Kravetz2b216242022-09-16 14:46:38 -07002434 /* fresh huge pages are frozen */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002435 folio_ref_unfreeze(folio, 1);
Michal Hockoab5ac902018-01-31 16:20:48 -08002436 /*
2437 * We do not account these pages as surplus because they are only
2438 * temporary and will be released properly on the last reference
2439 */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002440 folio_set_hugetlb_temporary(folio);
Michal Hockoab5ac902018-01-31 16:20:48 -08002441
Sidhartha Kumare37d3e82023-01-13 16:30:55 -06002442 return folio;
Michal Hockoab5ac902018-01-31 16:20:48 -08002443}
2444
Adam Litkee4e574b2007-10-16 01:26:19 -07002445/*
Dave Hansen099730d2015-11-05 18:50:17 -08002446 * Use the VMA's mpolicy to allocate a huge page from the buddy.
2447 */
Dave Hansene0ec90e2015-11-05 18:50:20 -08002448static
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06002449struct folio *alloc_buddy_hugetlb_folio_with_mpol(struct hstate *h,
Dave Hansen099730d2015-11-05 18:50:17 -08002450 struct vm_area_struct *vma, unsigned long addr)
2451{
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06002452 struct folio *folio = NULL;
Michal Hockoaaf14e42017-07-10 15:49:08 -07002453 struct mempolicy *mpol;
2454 gfp_t gfp_mask = htlb_alloc_mask(h);
2455 int nid;
2456 nodemask_t *nodemask;
2457
2458 nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07002459 if (mpol_is_preferred_many(mpol)) {
2460 gfp_t gfp = gfp_mask | __GFP_NOWARN;
Michal Hockoaaf14e42017-07-10 15:49:08 -07002461
Ben Widawskycfcaa662021-09-02 15:00:13 -07002462 gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06002463 folio = alloc_surplus_hugetlb_folio(h, gfp, nid, nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07002464
2465 /* Fallback to all nodes if page==NULL */
2466 nodemask = NULL;
2467 }
2468
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06002469 if (!folio)
2470 folio = alloc_surplus_hugetlb_folio(h, gfp_mask, nid, nodemask);
Ben Widawskycfcaa662021-09-02 15:00:13 -07002471 mpol_cond_put(mpol);
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06002472 return folio;
Dave Hansen099730d2015-11-05 18:50:17 -08002473}
2474
Sidhartha Kumare37d3e82023-01-13 16:30:55 -06002475/* folio migration callback function */
2476struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07002477 nodemask_t *nmask, gfp_t gfp_mask)
Michal Hocko4db9b2e2017-07-10 15:48:44 -07002478{
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002479 spin_lock_irq(&hugetlb_lock);
Xin Hao8346d692022-09-22 10:19:29 +08002480 if (available_huge_pages(h)) {
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06002481 struct folio *folio;
Michal Hocko3e59fcb2017-07-10 15:49:11 -07002482
Sidhartha Kumara36f1e92023-01-13 16:30:52 -06002483 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask,
2484 preferred_nid, nmask);
2485 if (folio) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002486 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumare37d3e82023-01-13 16:30:55 -06002487 return folio;
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
Sidhartha Kumare37d3e82023-01-13 16:30:55 -06002492 return alloc_migrate_hugetlb_folio(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 */
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08002496struct folio *alloc_hugetlb_folio_vma(struct hstate *h, struct vm_area_struct *vma,
Michal Hocko389c8172018-01-31 16:21:03 -08002497 unsigned long address)
Michal Hockoebd63722018-01-31 16:21:00 -08002498{
2499 struct mempolicy *mpol;
2500 nodemask_t *nodemask;
Sidhartha Kumare37d3e82023-01-13 16:30:55 -06002501 struct folio *folio;
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);
Sidhartha Kumare37d3e82023-01-13 16:30:55 -06002507 folio = alloc_hugetlb_folio_nodemask(h, node, nodemask, gfp_mask);
Michal Hockoebd63722018-01-31 16:21:00 -08002508 mpol_cond_put(mpol);
2509
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08002510 return folio;
Michal Hockoebd63722018-01-31 16:21:00 -08002511}
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);
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06002521 struct folio *folio;
Adam Litkee4e574b2007-10-16 01:26:19 -07002522 struct page *page, *tmp;
Liu Xiang0a4f3d12020-12-14 19:12:05 -08002523 int ret;
2524 long i;
2525 long needed, allocated;
Hillf Danton28073b02012-03-21 16:34:00 -07002526 bool alloc_ok = true;
Adam Litkee4e574b2007-10-16 01:26:19 -07002527
Mike Kravetz9487ca62021-05-04 18:35:10 -07002528 lockdep_assert_held(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002529 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -08002530 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -07002531 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07002532 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -08002533 }
Adam Litkee4e574b2007-10-16 01:26:19 -07002534
2535 allocated = 0;
Adam Litkee4e574b2007-10-16 01:26:19 -07002536
2537 ret = -ENOMEM;
2538retry:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002539 spin_unlock_irq(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07002540 for (i = 0; i < needed; i++) {
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06002541 folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h),
Mike Kravetz2b216242022-09-16 14:46:38 -07002542 NUMA_NO_NODE, NULL);
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06002543 if (!folio) {
Hillf Danton28073b02012-03-21 16:34:00 -07002544 alloc_ok = false;
2545 break;
2546 }
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06002547 list_add(&folio->lru, &surplus_list);
David Rientjes69ed7792017-07-10 15:48:50 -07002548 cond_resched();
Adam Litkee4e574b2007-10-16 01:26:19 -07002549 }
Hillf Danton28073b02012-03-21 16:34:00 -07002550 allocated += i;
Adam Litkee4e574b2007-10-16 01:26:19 -07002551
2552 /*
2553 * After retaking hugetlb_lock, we need to recalculate 'needed'
2554 * because either resv_huge_pages or free_huge_pages may have changed.
2555 */
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002556 spin_lock_irq(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07002557 needed = (h->resv_huge_pages + delta) -
2558 (h->free_huge_pages + allocated);
Hillf Danton28073b02012-03-21 16:34:00 -07002559 if (needed > 0) {
2560 if (alloc_ok)
2561 goto retry;
2562 /*
2563 * We were not able to allocate enough pages to
2564 * satisfy the entire reservation so we free what
2565 * we've allocated so far.
2566 */
2567 goto free;
2568 }
Adam Litkee4e574b2007-10-16 01:26:19 -07002569 /*
2570 * The surplus_list now contains _at_least_ the number of extra pages
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002571 * needed to accommodate the reservation. Add the appropriate number
Adam Litkee4e574b2007-10-16 01:26:19 -07002572 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -08002573 * allocator. Commit the entire reservation here to prevent another
2574 * process from stealing the pages as they are added to the pool but
2575 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -07002576 */
2577 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -07002578 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07002579 ret = 0;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09002580
Adam Litke19fc3f02008-04-28 02:12:20 -07002581 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -07002582 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -07002583 if ((--needed) < 0)
2584 break;
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002585 /* Add the page to the hugetlb allocator */
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08002586 enqueue_hugetlb_folio(h, page_folio(page));
Adam Litke19fc3f02008-04-28 02:12:20 -07002587 }
Hillf Danton28073b02012-03-21 16:34:00 -07002588free:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002589 spin_unlock_irq(&hugetlb_lock);
Adam Litke19fc3f02008-04-28 02:12:20 -07002590
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002591 /*
2592 * Free unnecessary surplus pages to the buddy allocator.
2593 * Pages have no ref count, call free_huge_page directly.
2594 */
Joonsoo Kimc0d934b2013-09-11 14:21:02 -07002595 list_for_each_entry_safe(page, tmp, &surplus_list, lru)
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002596 free_huge_page(page);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002597 spin_lock_irq(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07002598
2599 return ret;
2600}
2601
2602/*
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002603 * This routine has two main purposes:
2604 * 1) Decrement the reservation count (resv_huge_pages) by the value passed
2605 * in unused_resv_pages. This corresponds to the prior adjustments made
2606 * to the associated reservation map.
2607 * 2) Free any unused surplus pages that may have been allocated to satisfy
2608 * the reservation. As many as unused_resv_pages may be freed.
Adam Litkee4e574b2007-10-16 01:26:19 -07002609 */
Andi Kleena5516432008-07-23 21:27:41 -07002610static void return_unused_surplus_pages(struct hstate *h,
2611 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -07002612{
Adam Litkee4e574b2007-10-16 01:26:19 -07002613 unsigned long nr_pages;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002614 struct page *page;
2615 LIST_HEAD(page_list);
2616
Mike Kravetz9487ca62021-05-04 18:35:10 -07002617 lockdep_assert_held(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002618 /* Uncommit the reservation */
2619 h->resv_huge_pages -= unused_resv_pages;
Adam Litkee4e574b2007-10-16 01:26:19 -07002620
Naoya Horiguchic0531712022-07-14 13:24:13 +09002621 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002622 goto out;
Andi Kleenaa888a72008-07-23 21:27:47 -07002623
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002624 /*
2625 * Part (or even all) of the reservation could have been backed
2626 * by pre-allocated pages. Only free surplus pages.
2627 */
Andi Kleena5516432008-07-23 21:27:41 -07002628 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -07002629
Lee Schermerhorn685f3452009-09-21 17:01:23 -07002630 /*
2631 * We want to release as many surplus pages as possible, spread
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002632 * evenly across all nodes with memory. Iterate across these nodes
2633 * until we can no longer free unreserved surplus pages. This occurs
2634 * when the nodes with surplus pages have no free pages.
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002635 * remove_pool_huge_page() will balance the freed pages across the
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002636 * on-line nodes with memory and will handle the hstate accounting.
Lee Schermerhorn685f3452009-09-21 17:01:23 -07002637 */
2638 while (nr_pages--) {
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002639 page = remove_pool_huge_page(h, &node_states[N_MEMORY], 1);
2640 if (!page)
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002641 goto out;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002642
2643 list_add(&page->lru, &page_list);
Adam Litkee4e574b2007-10-16 01:26:19 -07002644 }
Mike Kravetze5bbc8a2017-01-10 16:58:27 -08002645
2646out:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002647 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07002648 update_and_free_pages_bulk(h, &page_list);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07002649 spin_lock_irq(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07002650}
2651
Mike Kravetz5e911372015-09-08 15:01:28 -07002652
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002653/*
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002654 * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
Mike Kravetz5e911372015-09-08 15:01:28 -07002655 * are used by the huge page allocation routines to manage reservations.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002656 *
2657 * vma_needs_reservation is called to determine if the huge page at addr
2658 * within the vma has an associated reservation. If a reservation is
2659 * needed, the value 1 is returned. The caller is then responsible for
2660 * managing the global reservation and subpool usage counts. After
2661 * the huge page has been allocated, vma_commit_reservation is called
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002662 * to add the page to the reservation map. If the page allocation fails,
2663 * the reservation must be ended instead of committed. vma_end_reservation
2664 * is called in such cases.
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002665 *
2666 * In the normal case, vma_commit_reservation returns the same value
2667 * as the preceding vma_needs_reservation call. The only time this
2668 * is not the case is if a reserve map was changed between calls. It
2669 * is the responsibility of the caller to notice the difference and
2670 * take appropriate action.
Mike Kravetz96b96a92016-11-10 10:46:32 -08002671 *
2672 * vma_add_reservation is used in error paths where a reservation must
2673 * be restored when a newly allocated huge page must be freed. It is
2674 * to be called after calling vma_needs_reservation to determine if a
2675 * reservation exists.
Mike Kravetz846be082021-06-15 18:23:29 -07002676 *
2677 * vma_del_reservation is used in error paths where an entry in the reserve
2678 * map was created during huge page allocation and must be removed. It is to
2679 * be called after calling vma_needs_reservation to determine if a reservation
2680 * exists.
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002681 */
Mike Kravetz5e911372015-09-08 15:01:28 -07002682enum vma_resv_mode {
2683 VMA_NEEDS_RESV,
2684 VMA_COMMIT_RESV,
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002685 VMA_END_RESV,
Mike Kravetz96b96a92016-11-10 10:46:32 -08002686 VMA_ADD_RESV,
Mike Kravetz846be082021-06-15 18:23:29 -07002687 VMA_DEL_RESV,
Mike Kravetz5e911372015-09-08 15:01:28 -07002688};
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002689static long __vma_reservation_common(struct hstate *h,
2690 struct vm_area_struct *vma, unsigned long addr,
Mike Kravetz5e911372015-09-08 15:01:28 -07002691 enum vma_resv_mode mode)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002692{
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002693 struct resv_map *resv;
2694 pgoff_t idx;
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002695 long ret;
Mina Almasry0db9d742020-04-01 21:11:25 -07002696 long dummy_out_regions_needed;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002697
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002698 resv = vma_resv_map(vma);
2699 if (!resv)
Andy Whitcroft84afd992008-07-23 21:27:32 -07002700 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002701
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002702 idx = vma_hugecache_offset(h, vma, addr);
Mike Kravetz5e911372015-09-08 15:01:28 -07002703 switch (mode) {
2704 case VMA_NEEDS_RESV:
Mina Almasry0db9d742020-04-01 21:11:25 -07002705 ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed);
2706 /* We assume that vma_reservation_* routines always operate on
2707 * 1 page, and that adding to resv map a 1 page entry can only
2708 * ever require 1 region.
2709 */
2710 VM_BUG_ON(dummy_out_regions_needed != 1);
Mike Kravetz5e911372015-09-08 15:01:28 -07002711 break;
2712 case VMA_COMMIT_RESV:
Mina Almasry075a61d2020-04-01 21:11:28 -07002713 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
Mina Almasry0db9d742020-04-01 21:11:25 -07002714 /* region_add calls of range 1 should never fail. */
2715 VM_BUG_ON(ret < 0);
Mike Kravetz5e911372015-09-08 15:01:28 -07002716 break;
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002717 case VMA_END_RESV:
Mina Almasry0db9d742020-04-01 21:11:25 -07002718 region_abort(resv, idx, idx + 1, 1);
Mike Kravetz5e911372015-09-08 15:01:28 -07002719 ret = 0;
2720 break;
Mike Kravetz96b96a92016-11-10 10:46:32 -08002721 case VMA_ADD_RESV:
Mina Almasry0db9d742020-04-01 21:11:25 -07002722 if (vma->vm_flags & VM_MAYSHARE) {
Mina Almasry075a61d2020-04-01 21:11:28 -07002723 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
Mina Almasry0db9d742020-04-01 21:11:25 -07002724 /* region_add calls of range 1 should never fail. */
2725 VM_BUG_ON(ret < 0);
2726 } else {
2727 region_abort(resv, idx, idx + 1, 1);
Mike Kravetz96b96a92016-11-10 10:46:32 -08002728 ret = region_del(resv, idx, idx + 1);
2729 }
2730 break;
Mike Kravetz846be082021-06-15 18:23:29 -07002731 case VMA_DEL_RESV:
2732 if (vma->vm_flags & VM_MAYSHARE) {
2733 region_abort(resv, idx, idx + 1, 1);
2734 ret = region_del(resv, idx, idx + 1);
2735 } else {
2736 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2737 /* region_add calls of range 1 should never fail. */
2738 VM_BUG_ON(ret < 0);
2739 }
2740 break;
Mike Kravetz5e911372015-09-08 15:01:28 -07002741 default:
2742 BUG();
2743 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07002744
Mike Kravetz846be082021-06-15 18:23:29 -07002745 if (vma->vm_flags & VM_MAYSHARE || mode == VMA_DEL_RESV)
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002746 return ret;
Miaohe Linbf3d12b2021-05-04 18:34:32 -07002747 /*
2748 * We know private mapping must have HPAGE_RESV_OWNER set.
2749 *
2750 * In most cases, reserves always exist for private mappings.
2751 * However, a file associated with mapping could have been
2752 * hole punched or truncated after reserves were consumed.
2753 * As subsequent fault on such a range will not use reserves.
2754 * Subtle - The reserve map for private mappings has the
2755 * opposite meaning than that of shared mappings. If NO
2756 * entry is in the reserve map, it means a reservation exists.
2757 * If an entry exists in the reserve map, it means the
2758 * reservation has already been consumed. As a result, the
2759 * return value of this routine is the opposite of the
2760 * value returned from reserve map manipulation routines above.
2761 */
2762 if (ret > 0)
2763 return 0;
2764 if (ret == 0)
2765 return 1;
2766 return ret;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002767}
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002768
2769static long vma_needs_reservation(struct hstate *h,
Andi Kleena5516432008-07-23 21:27:41 -07002770 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002771{
Mike Kravetz5e911372015-09-08 15:01:28 -07002772 return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002773}
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002774
Mike Kravetzcf3ad202015-06-24 16:57:55 -07002775static long vma_commit_reservation(struct hstate *h,
2776 struct vm_area_struct *vma, unsigned long addr)
2777{
Mike Kravetz5e911372015-09-08 15:01:28 -07002778 return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
2779}
2780
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002781static void vma_end_reservation(struct hstate *h,
Mike Kravetz5e911372015-09-08 15:01:28 -07002782 struct vm_area_struct *vma, unsigned long addr)
2783{
Mike Kravetzfeba16e2015-09-08 15:01:31 -07002784 (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07002785}
2786
Mike Kravetz96b96a92016-11-10 10:46:32 -08002787static long vma_add_reservation(struct hstate *h,
2788 struct vm_area_struct *vma, unsigned long addr)
2789{
2790 return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
2791}
2792
Mike Kravetz846be082021-06-15 18:23:29 -07002793static long vma_del_reservation(struct hstate *h,
2794 struct vm_area_struct *vma, unsigned long addr)
Mike Kravetz96b96a92016-11-10 10:46:32 -08002795{
Mike Kravetz846be082021-06-15 18:23:29 -07002796 return __vma_reservation_common(h, vma, addr, VMA_DEL_RESV);
2797}
Mike Kravetz96b96a92016-11-10 10:46:32 -08002798
Mike Kravetz846be082021-06-15 18:23:29 -07002799/*
2800 * This routine is called to restore reservation information on error paths.
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08002801 * It should ONLY be called for folios allocated via alloc_hugetlb_folio(),
2802 * and the hugetlb mutex should remain held when calling this routine.
Mike Kravetz846be082021-06-15 18:23:29 -07002803 *
2804 * It handles two specific cases:
Sidhartha Kumard2d7bb42023-01-25 09:05:34 -08002805 * 1) A reservation was in place and the folio consumed the reservation.
2806 * hugetlb_restore_reserve is set in the folio.
2807 * 2) No reservation was in place for the page, so hugetlb_restore_reserve is
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08002808 * not set. However, alloc_hugetlb_folio always updates the reserve map.
Mike Kravetz846be082021-06-15 18:23:29 -07002809 *
2810 * In case 1, free_huge_page later in the error path will increment the
2811 * global reserve count. But, free_huge_page does not have enough context
2812 * to adjust the reservation map. This case deals primarily with private
2813 * mappings. Adjust the reserve map here to be consistent with global
2814 * reserve count adjustments to be made by free_huge_page. Make sure the
2815 * reserve map indicates there is a reservation present.
2816 *
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08002817 * In case 2, simply undo reserve map modifications done by alloc_hugetlb_folio.
Mike Kravetz846be082021-06-15 18:23:29 -07002818 */
2819void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
Sidhartha Kumard2d7bb42023-01-25 09:05:34 -08002820 unsigned long address, struct folio *folio)
Mike Kravetz846be082021-06-15 18:23:29 -07002821{
2822 long rc = vma_needs_reservation(h, vma, address);
2823
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002824 if (folio_test_hugetlb_restore_reserve(folio)) {
Mike Kravetz846be082021-06-15 18:23:29 -07002825 if (unlikely(rc < 0))
Mike Kravetz96b96a92016-11-10 10:46:32 -08002826 /*
2827 * Rare out of memory condition in reserve map
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002828 * manipulation. Clear hugetlb_restore_reserve so
2829 * that global reserve count will not be incremented
Mike Kravetz96b96a92016-11-10 10:46:32 -08002830 * by free_huge_page. This will make it appear
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002831 * as though the reservation for this folio was
Mike Kravetz96b96a92016-11-10 10:46:32 -08002832 * consumed. This may prevent the task from
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002833 * faulting in the folio at a later time. This
Mike Kravetz96b96a92016-11-10 10:46:32 -08002834 * is better than inconsistent global huge page
2835 * accounting of reserve counts.
2836 */
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002837 folio_clear_hugetlb_restore_reserve(folio);
Mike Kravetz846be082021-06-15 18:23:29 -07002838 else if (rc)
2839 (void)vma_add_reservation(h, vma, address);
2840 else
Mike Kravetz96b96a92016-11-10 10:46:32 -08002841 vma_end_reservation(h, vma, address);
Mike Kravetz846be082021-06-15 18:23:29 -07002842 } else {
2843 if (!rc) {
2844 /*
2845 * This indicates there is an entry in the reserve map
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08002846 * not added by alloc_hugetlb_folio. We know it was added
2847 * before the alloc_hugetlb_folio call, otherwise
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002848 * hugetlb_restore_reserve would be set on the folio.
Mike Kravetz846be082021-06-15 18:23:29 -07002849 * Remove the entry so that a subsequent allocation
2850 * does not consume a reservation.
2851 */
2852 rc = vma_del_reservation(h, vma, address);
2853 if (rc < 0)
2854 /*
2855 * VERY rare out of memory condition. Since
2856 * we can not delete the entry, set
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002857 * hugetlb_restore_reserve so that the reserve
2858 * count will be incremented when the folio
Mike Kravetz846be082021-06-15 18:23:29 -07002859 * is freed. This reserve will be consumed
2860 * on a subsequent allocation.
2861 */
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002862 folio_set_hugetlb_restore_reserve(folio);
Mike Kravetz846be082021-06-15 18:23:29 -07002863 } else if (rc < 0) {
2864 /*
2865 * Rare out of memory condition from
2866 * vma_needs_reservation call. Memory allocation is
2867 * only attempted if a new entry is needed. Therefore,
2868 * this implies there is not an entry in the
2869 * reserve map.
2870 *
2871 * For shared mappings, no entry in the map indicates
2872 * no reservation. We are done.
2873 */
2874 if (!(vma->vm_flags & VM_MAYSHARE))
2875 /*
2876 * For private mappings, no entry indicates
2877 * a reservation is present. Since we can
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002878 * not add an entry, set hugetlb_restore_reserve
2879 * on the folio so reserve count will be
Mike Kravetz846be082021-06-15 18:23:29 -07002880 * incremented when freed. This reserve will
2881 * be consumed on a subsequent allocation.
2882 */
Sidhartha Kumar0ffdc382023-01-13 16:30:56 -06002883 folio_set_hugetlb_restore_reserve(folio);
Mike Kravetz846be082021-06-15 18:23:29 -07002884 } else
2885 /*
2886 * No reservation present, do nothing
2887 */
2888 vma_end_reservation(h, vma, address);
Mike Kravetz96b96a92016-11-10 10:46:32 -08002889 }
2890}
2891
Oscar Salvador369fa222021-05-04 18:35:26 -07002892/*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002893 * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve
2894 * the old one
Oscar Salvador369fa222021-05-04 18:35:26 -07002895 * @h: struct hstate old page belongs to
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002896 * @old_folio: Old folio to dissolve
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002897 * @list: List to isolate the page in case we need to
Oscar Salvador369fa222021-05-04 18:35:26 -07002898 * Returns 0 on success, otherwise negated error.
2899 */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002900static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
2901 struct folio *old_folio, struct list_head *list)
Oscar Salvador369fa222021-05-04 18:35:26 -07002902{
2903 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002904 int nid = folio_nid(old_folio);
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002905 struct folio *new_folio;
Oscar Salvador369fa222021-05-04 18:35:26 -07002906 int ret = 0;
2907
2908 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002909 * Before dissolving the folio, we need to allocate a new one for the
2910 * pool to remain stable. Here, we allocate the folio and 'prep' it
Muchun Songf41f2ed2021-06-30 18:47:13 -07002911 * by doing everything but actually updating counters and adding to
2912 * the pool. This simplifies and let us do most of the processing
2913 * under the lock.
Oscar Salvador369fa222021-05-04 18:35:26 -07002914 */
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002915 new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL);
2916 if (!new_folio)
Oscar Salvador369fa222021-05-04 18:35:26 -07002917 return -ENOMEM;
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002918 __prep_new_hugetlb_folio(h, new_folio);
Oscar Salvador369fa222021-05-04 18:35:26 -07002919
2920retry:
2921 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002922 if (!folio_test_hugetlb(old_folio)) {
Oscar Salvador369fa222021-05-04 18:35:26 -07002923 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002924 * Freed from under us. Drop new_folio too.
Oscar Salvador369fa222021-05-04 18:35:26 -07002925 */
2926 goto free_new;
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002927 } else if (folio_ref_count(old_folio)) {
Baolin Wang9747b9e2023-02-15 18:39:36 +08002928 bool isolated;
2929
Oscar Salvador369fa222021-05-04 18:35:26 -07002930 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002931 * Someone has grabbed the folio, try to isolate it here.
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002932 * Fail with -EBUSY if not possible.
Oscar Salvador369fa222021-05-04 18:35:26 -07002933 */
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002934 spin_unlock_irq(&hugetlb_lock);
Baolin Wang9747b9e2023-02-15 18:39:36 +08002935 isolated = isolate_hugetlb(old_folio, list);
2936 ret = isolated ? 0 : -EBUSY;
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002937 spin_lock_irq(&hugetlb_lock);
Oscar Salvador369fa222021-05-04 18:35:26 -07002938 goto free_new;
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002939 } else if (!folio_test_hugetlb_freed(old_folio)) {
Oscar Salvador369fa222021-05-04 18:35:26 -07002940 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002941 * Folio's refcount is 0 but it has not been enqueued in the
Oscar Salvador369fa222021-05-04 18:35:26 -07002942 * freelist yet. Race window is small, so we can succeed here if
2943 * we retry.
2944 */
2945 spin_unlock_irq(&hugetlb_lock);
2946 cond_resched();
2947 goto retry;
2948 } else {
2949 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002950 * Ok, old_folio is still a genuine free hugepage. Remove it from
Oscar Salvador369fa222021-05-04 18:35:26 -07002951 * the freelist and decrease the counters. These will be
2952 * incremented again when calling __prep_account_new_huge_page()
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08002953 * and enqueue_hugetlb_folio() for new_folio. The counters will
2954 * remain stable since this happens under the lock.
Oscar Salvador369fa222021-05-04 18:35:26 -07002955 */
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08002956 remove_hugetlb_folio(h, old_folio, false);
Oscar Salvador369fa222021-05-04 18:35:26 -07002957
2958 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002959 * Ref count on new_folio is already zero as it was dropped
Mike Kravetzb65a4ed2021-09-02 14:58:47 -07002960 * earlier. It can be directly added to the pool free list.
Oscar Salvador369fa222021-05-04 18:35:26 -07002961 */
Oscar Salvador369fa222021-05-04 18:35:26 -07002962 __prep_account_new_huge_page(h, nid);
Sidhartha Kumar240d67a2022-11-29 14:50:36 -08002963 enqueue_hugetlb_folio(h, new_folio);
Oscar Salvador369fa222021-05-04 18:35:26 -07002964
2965 /*
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002966 * Folio has been replaced, we can safely free the old one.
Oscar Salvador369fa222021-05-04 18:35:26 -07002967 */
2968 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08002969 update_and_free_hugetlb_folio(h, old_folio, false);
Oscar Salvador369fa222021-05-04 18:35:26 -07002970 }
2971
2972 return ret;
2973
2974free_new:
2975 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08002976 /* Folio has a zero ref count, but needs a ref to be freed */
Sidhartha Kumarde656ed2022-11-01 15:30:53 -07002977 folio_ref_unfreeze(new_folio, 1);
Sidhartha Kumard6ef19e2022-11-29 14:50:34 -08002978 update_and_free_hugetlb_folio(h, new_folio, false);
Oscar Salvador369fa222021-05-04 18:35:26 -07002979
2980 return ret;
2981}
2982
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002983int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list)
Oscar Salvador369fa222021-05-04 18:35:26 -07002984{
2985 struct hstate *h;
Sidhartha Kumard5e33bd2022-11-01 15:30:55 -07002986 struct folio *folio = page_folio(page);
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07002987 int ret = -EBUSY;
Oscar Salvador369fa222021-05-04 18:35:26 -07002988
2989 /*
2990 * The page might have been dissolved from under our feet, so make sure
2991 * to carefully check the state under the lock.
2992 * Return success when racing as if we dissolved the page ourselves.
2993 */
2994 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumard5e33bd2022-11-01 15:30:55 -07002995 if (folio_test_hugetlb(folio)) {
2996 h = folio_hstate(folio);
Oscar Salvador369fa222021-05-04 18:35:26 -07002997 } else {
2998 spin_unlock_irq(&hugetlb_lock);
2999 return 0;
3000 }
3001 spin_unlock_irq(&hugetlb_lock);
3002
3003 /*
3004 * Fence off gigantic pages as there is a cyclic dependency between
3005 * alloc_contig_range and them. Return -ENOMEM as this has the effect
3006 * of bailing out right away without further retrying.
3007 */
3008 if (hstate_is_gigantic(h))
3009 return -ENOMEM;
3010
Baolin Wang9747b9e2023-02-15 18:39:36 +08003011 if (folio_ref_count(folio) && isolate_hugetlb(folio, list))
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07003012 ret = 0;
Sidhartha Kumard5e33bd2022-11-01 15:30:55 -07003013 else if (!folio_ref_count(folio))
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003014 ret = alloc_and_dissolve_hugetlb_folio(h, folio, list);
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07003015
3016 return ret;
Oscar Salvador369fa222021-05-04 18:35:26 -07003017}
3018
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08003019struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003020 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
David Gibson90481622012-03-21 16:34:12 -07003022 struct hugepage_subpool *spool = subpool_vma(vma);
Andi Kleena5516432008-07-23 21:27:41 -07003023 struct hstate *h = hstate_vma(vma);
Sidhartha Kumard4ab0312022-11-01 15:30:57 -07003024 struct folio *folio;
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003025 long map_chg, map_commit;
3026 long gbl_chg;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07003027 int ret, idx;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08003028 struct hugetlb_cgroup *h_cg = NULL;
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003029 bool deferred_reserve;
Adam Litke2fc39ce2007-11-14 16:59:39 -08003030
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07003031 idx = hstate_index(h);
Mel Gormana1e78772008-07-23 21:27:23 -07003032 /*
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003033 * Examine the region/reserve map to determine if the process
3034 * has a reservation for the page to be allocated. A return
3035 * code of zero indicates a reservation exists (no change).
Mel Gormana1e78772008-07-23 21:27:23 -07003036 */
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003037 map_chg = gbl_chg = vma_needs_reservation(h, vma, addr);
3038 if (map_chg < 0)
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07003039 return ERR_PTR(-ENOMEM);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003040
3041 /*
3042 * Processes that did not create the mapping will have no
3043 * reserves as indicated by the region/reserve map. Check
3044 * that the allocation will not exceed the subpool limit.
3045 * Allocations for MAP_NORESERVE mappings also need to be
3046 * checked against any subpool limit.
3047 */
3048 if (map_chg || avoid_reserve) {
3049 gbl_chg = hugepage_subpool_get_pages(spool, 1);
3050 if (gbl_chg < 0) {
Mike Kravetzfeba16e2015-09-08 15:01:31 -07003051 vma_end_reservation(h, vma, addr);
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07003052 return ERR_PTR(-ENOSPC);
Mike Kravetz5e911372015-09-08 15:01:28 -07003053 }
Mel Gormana1e78772008-07-23 21:27:23 -07003054
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003055 /*
3056 * Even though there was no reservation in the region/reserve
3057 * map, there could be reservations associated with the
3058 * subpool that can be used. This would be indicated if the
3059 * return value of hugepage_subpool_get_pages() is zero.
3060 * However, if avoid_reserve is specified we still avoid even
3061 * the subpool reservations.
3062 */
3063 if (avoid_reserve)
3064 gbl_chg = 1;
3065 }
3066
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003067 /* If this allocation is not consuming a reservation, charge it now.
3068 */
Miaohe Lin6501fe52021-05-04 18:33:16 -07003069 deferred_reserve = map_chg || avoid_reserve;
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003070 if (deferred_reserve) {
3071 ret = hugetlb_cgroup_charge_cgroup_rsvd(
3072 idx, pages_per_huge_page(h), &h_cg);
3073 if (ret)
3074 goto out_subpool_put;
3075 }
3076
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07003077 ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003078 if (ret)
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003079 goto out_uncharge_cgroup_reservation;
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003080
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003081 spin_lock_irq(&hugetlb_lock);
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003082 /*
3083 * glb_chg is passed to indicate whether or not a page must be taken
3084 * from the global free pool (global change). gbl_chg == 0 indicates
3085 * a reservation exists for the allocation.
3086 */
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06003087 folio = dequeue_hugetlb_folio_vma(h, vma, addr, avoid_reserve, gbl_chg);
3088 if (!folio) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003089 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06003090 folio = alloc_buddy_hugetlb_folio_with_mpol(h, vma, addr);
3091 if (!folio)
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003092 goto out_uncharge_cgroup;
Rik van Riel12df1402022-10-17 20:25:05 -04003093 spin_lock_irq(&hugetlb_lock);
Naoya Horiguchia88c7692015-12-11 13:40:24 -08003094 if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06003095 folio_set_hugetlb_restore_reserve(folio);
Naoya Horiguchia88c7692015-12-11 13:40:24 -08003096 h->resv_huge_pages--;
3097 }
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06003098 list_add(&folio->lru, &h->hugepage_activelist);
3099 folio_ref_unfreeze(folio, 1);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07003100 /* Fall through */
Mel Gormana1e78772008-07-23 21:27:23 -07003101 }
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06003102
3103 hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, folio);
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003104 /* If allocation is not consuming a reservation, also store the
3105 * hugetlb_cgroup pointer on the page.
3106 */
3107 if (deferred_reserve) {
3108 hugetlb_cgroup_commit_charge_rsvd(idx, pages_per_huge_page(h),
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06003109 h_cg, folio);
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003110 }
3111
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003112 spin_unlock_irq(&hugetlb_lock);
Mel Gormana1e78772008-07-23 21:27:23 -07003113
Sidhartha Kumarff7d8532023-01-13 16:30:54 -06003114 hugetlb_set_folio_subpool(folio, spool);
Mel Gormana1e78772008-07-23 21:27:23 -07003115
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003116 map_commit = vma_commit_reservation(h, vma, addr);
3117 if (unlikely(map_chg > map_commit)) {
Mike Kravetz33039672015-06-24 16:57:58 -07003118 /*
3119 * The page was added to the reservation map between
3120 * vma_needs_reservation and vma_commit_reservation.
3121 * This indicates a race with hugetlb_reserve_pages.
3122 * Adjust for the subpool count incremented above AND
3123 * in hugetlb_reserve_pages for the same page. Also,
3124 * the reservation count added in hugetlb_reserve_pages
3125 * no longer applies.
3126 */
3127 long rsv_adjust;
3128
3129 rsv_adjust = hugepage_subpool_put_pages(spool, 1);
3130 hugetlb_acct_memory(h, -rsv_adjust);
Mike Kravetz79aa9252020-11-01 17:07:27 -08003131 if (deferred_reserve)
Sidhartha Kumard4ab0312022-11-01 15:30:57 -07003132 hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h),
3133 pages_per_huge_page(h), folio);
Mike Kravetz33039672015-06-24 16:57:58 -07003134 }
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08003135 return folio;
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003136
3137out_uncharge_cgroup:
3138 hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
Mina Almasry08cf9fa2020-04-01 21:11:31 -07003139out_uncharge_cgroup_reservation:
3140 if (deferred_reserve)
3141 hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h),
3142 h_cg);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003143out_subpool_put:
Mike Kravetzd85f69b2015-09-08 15:01:47 -07003144 if (map_chg || avoid_reserve)
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003145 hugepage_subpool_put_pages(spool, 1);
Mike Kravetzfeba16e2015-09-08 15:01:31 -07003146 vma_end_reservation(h, vma, addr);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07003147 return ERR_PTR(-ENOSPC);
David Gibsonb45b5bd2006-03-22 00:08:55 -08003148}
3149
Zhenguo Yaob5389082021-11-05 13:43:28 -07003150int alloc_bootmem_huge_page(struct hstate *h, int nid)
Aneesh Kumar K.Ve24a1302017-07-28 10:31:25 +05303151 __attribute__ ((weak, alias("__alloc_bootmem_huge_page")));
Zhenguo Yaob5389082021-11-05 13:43:28 -07003152int __alloc_bootmem_huge_page(struct hstate *h, int nid)
Andi Kleenaa888a72008-07-23 21:27:47 -07003153{
Zhenguo Yaob5389082021-11-05 13:43:28 -07003154 struct huge_bootmem_page *m = NULL; /* initialize for clang */
Joonsoo Kimb2261022013-09-11 14:21:00 -07003155 int nr_nodes, node;
Andi Kleenaa888a72008-07-23 21:27:47 -07003156
Zhenguo Yaob5389082021-11-05 13:43:28 -07003157 /* do node specific alloc */
3158 if (nid != NUMA_NO_NODE) {
3159 m = memblock_alloc_try_nid_raw(huge_page_size(h), huge_page_size(h),
3160 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
3161 if (!m)
3162 return 0;
3163 goto found;
3164 }
3165 /* allocate from next node when distributing huge pages */
Joonsoo Kimb2261022013-09-11 14:21:00 -07003166 for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
Zhenguo Yaob5389082021-11-05 13:43:28 -07003167 m = memblock_alloc_try_nid_raw(
Grygorii Strashko8b89a112014-01-21 15:50:36 -08003168 huge_page_size(h), huge_page_size(h),
Mike Rapoport97ad1082018-10-30 15:09:44 -07003169 0, MEMBLOCK_ALLOC_ACCESSIBLE, node);
Zhenguo Yaob5389082021-11-05 13:43:28 -07003170 /*
3171 * Use the beginning of the huge page to store the
3172 * huge_bootmem_page struct (until gather_bootmem
3173 * puts them into the mem_map).
3174 */
3175 if (!m)
3176 return 0;
3177 goto found;
Andi Kleenaa888a72008-07-23 21:27:47 -07003178 }
Andi Kleenaa888a72008-07-23 21:27:47 -07003179
3180found:
Andi Kleenaa888a72008-07-23 21:27:47 -07003181 /* Put them into a private list first because mem_map is not up yet */
Cannon Matthews330d6e42018-08-17 15:49:17 -07003182 INIT_LIST_HEAD(&m->list);
Andi Kleenaa888a72008-07-23 21:27:47 -07003183 list_add(&m->list, &huge_boot_pages);
3184 m->hstate = h;
3185 return 1;
3186}
3187
Mike Kravetz48b8d742021-06-30 18:48:31 -07003188/*
3189 * Put bootmem huge pages into the standard lists after mem_map is up.
3190 * Note: This only applies to gigantic (order > MAX_ORDER) pages.
3191 */
Andi Kleenaa888a72008-07-23 21:27:47 -07003192static void __init gather_bootmem_prealloc(void)
3193{
3194 struct huge_bootmem_page *m;
3195
3196 list_for_each_entry(m, &huge_boot_pages, list) {
Mike Kravetz40d18eb2018-08-17 15:49:07 -07003197 struct page *page = virt_to_page(m);
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08003198 struct folio *folio = page_folio(page);
Andi Kleenaa888a72008-07-23 21:27:47 -07003199 struct hstate *h = m->hstate;
Becky Bruceee8f2482011-07-25 17:11:50 -07003200
Mike Kravetz48b8d742021-06-30 18:48:31 -07003201 VM_BUG_ON(!hstate_is_gigantic(h));
Sidhartha Kumard1c60952022-11-29 14:50:38 -08003202 WARN_ON(folio_ref_count(folio) != 1);
3203 if (prep_compound_gigantic_folio(folio, huge_page_order(h))) {
3204 WARN_ON(folio_test_reserved(folio));
3205 prep_new_hugetlb_folio(h, folio, folio_nid(folio));
Mike Kravetz2b216242022-09-16 14:46:38 -07003206 free_huge_page(page); /* add to the hugepage allocator */
Mike Kravetz7118fc22021-06-30 18:48:34 -07003207 } else {
Mike Kravetz416d85e2021-09-02 14:58:43 -07003208 /* VERY unlikely inflated ref count on a tail page */
Sidhartha Kumar7f325a82022-11-29 14:50:37 -08003209 free_gigantic_folio(folio, huge_page_order(h));
Mike Kravetz7118fc22021-06-30 18:48:34 -07003210 }
Michal Hockoaf0fb9d2018-01-31 16:20:41 -08003211
Rafael Aquinib0320c72011-06-15 15:08:39 -07003212 /*
Mike Kravetz48b8d742021-06-30 18:48:31 -07003213 * We need to restore the 'stolen' pages to totalram_pages
3214 * in order to fix confusing memory reports from free(1) and
3215 * other side-effects, like CommitLimit going negative.
Rafael Aquinib0320c72011-06-15 15:08:39 -07003216 */
Mike Kravetz48b8d742021-06-30 18:48:31 -07003217 adjust_managed_page_count(page, pages_per_huge_page(h));
Cannon Matthews520495f2018-07-03 17:02:43 -07003218 cond_resched();
Andi Kleenaa888a72008-07-23 21:27:47 -07003219 }
3220}
Zhenguo Yaob5389082021-11-05 13:43:28 -07003221static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)
3222{
3223 unsigned long i;
3224 char buf[32];
3225
3226 for (i = 0; i < h->max_huge_pages_node[nid]; ++i) {
3227 if (hstate_is_gigantic(h)) {
3228 if (!alloc_bootmem_huge_page(h, nid))
3229 break;
3230 } else {
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003231 struct folio *folio;
Zhenguo Yaob5389082021-11-05 13:43:28 -07003232 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
3233
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003234 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid,
Zhenguo Yaob5389082021-11-05 13:43:28 -07003235 &node_states[N_MEMORY], NULL);
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003236 if (!folio)
Zhenguo Yaob5389082021-11-05 13:43:28 -07003237 break;
Sidhartha Kumar19fc1a72022-11-29 14:50:39 -08003238 free_huge_page(&folio->page); /* free it into the hugepage allocator */
Zhenguo Yaob5389082021-11-05 13:43:28 -07003239 }
3240 cond_resched();
3241 }
3242 if (i == h->max_huge_pages_node[nid])
3243 return;
3244
3245 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3246 pr_warn("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n",
3247 h->max_huge_pages_node[nid], buf, nid, i);
3248 h->max_huge_pages -= (h->max_huge_pages_node[nid] - i);
3249 h->max_huge_pages_node[nid] = i;
3250}
Andi Kleenaa888a72008-07-23 21:27:47 -07003251
Andi Kleen8faa8b02008-07-23 21:27:48 -07003252static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
3254 unsigned long i;
Mike Kravetzf60858f2019-09-23 15:37:35 -07003255 nodemask_t *node_alloc_noretry;
Zhenguo Yaob5389082021-11-05 13:43:28 -07003256 bool node_specific_alloc = false;
Mike Kravetzf60858f2019-09-23 15:37:35 -07003257
Zhenguo Yaob5389082021-11-05 13:43:28 -07003258 /* skip gigantic hugepages allocation if hugetlb_cma enabled */
3259 if (hstate_is_gigantic(h) && hugetlb_cma_size) {
3260 pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n");
3261 return;
3262 }
3263
3264 /* do node specific alloc */
Peng Liu0a7a0f62022-04-29 14:36:57 -07003265 for_each_online_node(i) {
Zhenguo Yaob5389082021-11-05 13:43:28 -07003266 if (h->max_huge_pages_node[i] > 0) {
3267 hugetlb_hstate_alloc_pages_onenode(h, i);
3268 node_specific_alloc = true;
3269 }
3270 }
3271
3272 if (node_specific_alloc)
3273 return;
3274
3275 /* below will do all node balanced alloc */
Mike Kravetzf60858f2019-09-23 15:37:35 -07003276 if (!hstate_is_gigantic(h)) {
3277 /*
3278 * Bit mask controlling how hard we retry per-node allocations.
3279 * Ignore errors as lower level routines can deal with
3280 * node_alloc_noretry == NULL. If this kmalloc fails at boot
3281 * time, we are likely in bigger trouble.
3282 */
3283 node_alloc_noretry = kmalloc(sizeof(*node_alloc_noretry),
3284 GFP_KERNEL);
3285 } else {
3286 /* allocations done at boot time */
3287 node_alloc_noretry = NULL;
3288 }
3289
3290 /* bit mask controlling how hard we retry per-node allocations */
3291 if (node_alloc_noretry)
3292 nodes_clear(*node_alloc_noretry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Andi Kleene5ff2152008-07-23 21:27:42 -07003294 for (i = 0; i < h->max_huge_pages; ++i) {
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003295 if (hstate_is_gigantic(h)) {
Zhenguo Yaob5389082021-11-05 13:43:28 -07003296 if (!alloc_bootmem_huge_page(h, NUMA_NO_NODE))
Andi Kleenaa888a72008-07-23 21:27:47 -07003297 break;
Michal Hocko0c397da2018-01-31 16:20:56 -08003298 } else if (!alloc_pool_huge_page(h,
Mike Kravetzf60858f2019-09-23 15:37:35 -07003299 &node_states[N_MEMORY],
3300 node_alloc_noretry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 break;
David Rientjes69ed7792017-07-10 15:48:50 -07003302 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
Liam R. Howlettd715cf82017-07-10 15:48:15 -07003304 if (i < h->max_huge_pages) {
3305 char buf[32];
3306
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07003307 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Liam R. Howlettd715cf82017-07-10 15:48:15 -07003308 pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n",
3309 h->max_huge_pages, buf, i);
3310 h->max_huge_pages = i;
3311 }
Mike Kravetzf60858f2019-09-23 15:37:35 -07003312 kfree(node_alloc_noretry);
Andi Kleene5ff2152008-07-23 21:27:42 -07003313}
3314
3315static void __init hugetlb_init_hstates(void)
3316{
Mike Kravetz79dfc692021-11-05 13:41:20 -07003317 struct hstate *h, *h2;
Andi Kleene5ff2152008-07-23 21:27:42 -07003318
3319 for_each_hstate(h) {
Andi Kleen8faa8b02008-07-23 21:27:48 -07003320 /* oversize hugepages were init'ed in early boot */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003321 if (!hstate_is_gigantic(h))
Andi Kleen8faa8b02008-07-23 21:27:48 -07003322 hugetlb_hstate_alloc_pages(h);
Mike Kravetz79dfc692021-11-05 13:41:20 -07003323
3324 /*
3325 * Set demote order for each hstate. Note that
3326 * h->demote_order is initially 0.
3327 * - We can not demote gigantic pages if runtime freeing
3328 * is not supported, so skip this.
Mike Kravetza01f4392021-11-05 13:41:27 -07003329 * - If CMA allocation is possible, we can not demote
3330 * HUGETLB_PAGE_ORDER or smaller size pages.
Mike Kravetz79dfc692021-11-05 13:41:20 -07003331 */
3332 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
3333 continue;
Mike Kravetza01f4392021-11-05 13:41:27 -07003334 if (hugetlb_cma_size && h->order <= HUGETLB_PAGE_ORDER)
3335 continue;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003336 for_each_hstate(h2) {
3337 if (h2 == h)
3338 continue;
3339 if (h2->order < h->order &&
3340 h2->order > h->demote_order)
3341 h->demote_order = h2->order;
3342 }
Andi Kleene5ff2152008-07-23 21:27:42 -07003343 }
3344}
3345
3346static void __init report_hugepages(void)
3347{
3348 struct hstate *h;
3349
3350 for_each_hstate(h) {
Andi Kleen4abd32d2008-07-23 21:27:49 -07003351 char buf[32];
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07003352
3353 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
Muchun Song62138342022-06-28 17:22:33 +08003354 pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n",
Matthew Wilcoxc6247f72017-07-10 15:48:56 -07003355 buf, h->free_huge_pages);
Muchun Song62138342022-06-28 17:22:33 +08003356 pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n",
3357 hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf);
Andi Kleene5ff2152008-07-23 21:27:42 -07003358 }
3359}
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361#ifdef CONFIG_HIGHMEM
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003362static void try_to_free_low(struct hstate *h, unsigned long count,
3363 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
Christoph Lameter4415cc82006-09-25 23:31:55 -07003365 int i;
Mike Kravetz11218282021-05-04 18:34:59 -07003366 LIST_HEAD(page_list);
Christoph Lameter4415cc82006-09-25 23:31:55 -07003367
Mike Kravetz9487ca62021-05-04 18:35:10 -07003368 lockdep_assert_held(&hugetlb_lock);
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003369 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07003370 return;
3371
Mike Kravetz11218282021-05-04 18:34:59 -07003372 /*
3373 * Collect pages to be freed on a list, and free after dropping lock
3374 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003375 for_each_node_mask(i, *nodes_allowed) {
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003376 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -07003377 struct list_head *freel = &h->hugepage_freelists[i];
3378 list_for_each_entry_safe(page, next, freel, lru) {
3379 if (count >= h->nr_huge_pages)
Mike Kravetz11218282021-05-04 18:34:59 -07003380 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (PageHighMem(page))
3382 continue;
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08003383 remove_hugetlb_folio(h, page_folio(page), false);
Mike Kravetz11218282021-05-04 18:34:59 -07003384 list_add(&page->lru, &page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
3386 }
Mike Kravetz11218282021-05-04 18:34:59 -07003387
3388out:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003389 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003390 update_and_free_pages_bulk(h, &page_list);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003391 spin_lock_irq(&hugetlb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393#else
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003394static inline void try_to_free_low(struct hstate *h, unsigned long count,
3395 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
3397}
3398#endif
3399
Wu Fengguang20a03072009-06-16 15:32:22 -07003400/*
3401 * Increment or decrement surplus_huge_pages. Keep node-specific counters
3402 * balanced by operating on them in a round-robin fashion.
3403 * Returns 1 if an adjustment was made.
3404 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003405static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
3406 int delta)
Wu Fengguang20a03072009-06-16 15:32:22 -07003407{
Joonsoo Kimb2261022013-09-11 14:21:00 -07003408 int nr_nodes, node;
Wu Fengguang20a03072009-06-16 15:32:22 -07003409
Mike Kravetz9487ca62021-05-04 18:35:10 -07003410 lockdep_assert_held(&hugetlb_lock);
Wu Fengguang20a03072009-06-16 15:32:22 -07003411 VM_BUG_ON(delta != -1 && delta != 1);
Wu Fengguang20a03072009-06-16 15:32:22 -07003412
Joonsoo Kimb2261022013-09-11 14:21:00 -07003413 if (delta < 0) {
3414 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
3415 if (h->surplus_huge_pages_node[node])
3416 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07003417 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07003418 } else {
3419 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
3420 if (h->surplus_huge_pages_node[node] <
3421 h->nr_huge_pages_node[node])
3422 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07003423 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07003424 }
3425 return 0;
Wu Fengguang20a03072009-06-16 15:32:22 -07003426
Joonsoo Kimb2261022013-09-11 14:21:00 -07003427found:
3428 h->surplus_huge_pages += delta;
3429 h->surplus_huge_pages_node[node] += delta;
3430 return 1;
Wu Fengguang20a03072009-06-16 15:32:22 -07003431}
3432
Andi Kleena5516432008-07-23 21:27:41 -07003433#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Mike Kravetzfd875dc2019-05-13 17:19:20 -07003434static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003435 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
Adam Litke7893d1d2007-10-16 01:26:18 -07003437 unsigned long min_count, ret;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003438 struct page *page;
3439 LIST_HEAD(page_list);
Mike Kravetzf60858f2019-09-23 15:37:35 -07003440 NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL);
3441
3442 /*
3443 * Bit mask controlling how hard we retry per-node allocations.
3444 * If we can not allocate the bit mask, do not attempt to allocate
3445 * the requested huge pages.
3446 */
3447 if (node_alloc_noretry)
3448 nodes_clear(*node_alloc_noretry);
3449 else
3450 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Mike Kravetz29383962021-05-04 18:34:52 -07003452 /*
3453 * resize_lock mutex prevents concurrent adjustments to number of
3454 * pages in hstate via the proc/sysfs interfaces.
3455 */
3456 mutex_lock(&h->resize_lock);
Muchun Songb65d4ad2021-06-30 18:47:17 -07003457 flush_free_hpage_work(h);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003458 spin_lock_irq(&hugetlb_lock);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003459
3460 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07003461 * Check for a node specific request.
3462 * Changing node specific huge page count may require a corresponding
3463 * change to the global count. In any case, the passed node mask
3464 * (nodes_allowed) will restrict alloc/free to the specified node.
3465 */
3466 if (nid != NUMA_NO_NODE) {
3467 unsigned long old_count = count;
3468
3469 count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
3470 /*
3471 * User may have specified a large count value which caused the
3472 * above calculation to overflow. In this case, they wanted
3473 * to allocate as many huge pages as possible. Set count to
3474 * largest possible value to align with their intention.
3475 */
3476 if (count < old_count)
3477 count = ULONG_MAX;
3478 }
3479
3480 /*
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003481 * Gigantic pages runtime allocation depend on the capability for large
3482 * page range allocation.
3483 * If the system does not provide this feature, return an error when
3484 * the user tries to allocate gigantic pages but let the user free the
3485 * boottime allocated gigantic pages.
3486 */
3487 if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) {
3488 if (count > persistent_huge_pages(h)) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003489 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz29383962021-05-04 18:34:52 -07003490 mutex_unlock(&h->resize_lock);
Mike Kravetzf60858f2019-09-23 15:37:35 -07003491 NODEMASK_FREE(node_alloc_noretry);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003492 return -EINVAL;
3493 }
3494 /* Fall through to decrease pool */
3495 }
Andi Kleenaa888a72008-07-23 21:27:47 -07003496
Adam Litke7893d1d2007-10-16 01:26:18 -07003497 /*
3498 * Increase the pool size
3499 * First take pages out of surplus state. Then make up the
3500 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08003501 *
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06003502 * We might race with alloc_surplus_hugetlb_folio() here and be unable
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08003503 * to convert a surplus huge page to a normal huge page. That is
3504 * not critical, though, it just means the overall size of the
3505 * pool might be one hugepage larger than it needs to be, but
3506 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -07003507 */
Andi Kleena5516432008-07-23 21:27:41 -07003508 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003509 if (!adjust_pool_surplus(h, nodes_allowed, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -07003510 break;
3511 }
3512
Andi Kleena5516432008-07-23 21:27:41 -07003513 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -07003514 /*
3515 * If this allocation races such that we no longer need the
3516 * page, free_huge_page will handle it by freeing the page
3517 * and reducing the surplus.
3518 */
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003519 spin_unlock_irq(&hugetlb_lock);
Jia He649920c2016-08-02 14:02:31 -07003520
3521 /* yield cpu to avoid soft lockup */
3522 cond_resched();
3523
Mike Kravetzf60858f2019-09-23 15:37:35 -07003524 ret = alloc_pool_huge_page(h, nodes_allowed,
3525 node_alloc_noretry);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003526 spin_lock_irq(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07003527 if (!ret)
3528 goto out;
3529
Mel Gorman536240f22009-12-14 17:59:56 -08003530 /* Bail for signals. Probably ctrl-c from user */
3531 if (signal_pending(current))
3532 goto out;
Adam Litke7893d1d2007-10-16 01:26:18 -07003533 }
Adam Litke7893d1d2007-10-16 01:26:18 -07003534
3535 /*
3536 * Decrease the pool size
3537 * First return free pages to the buddy allocator (being careful
3538 * to keep enough around to satisfy reservations). Then place
3539 * pages into surplus state as needed so the pool will shrink
3540 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08003541 *
3542 * By placing pages into the surplus state independent of the
3543 * overcommit value, we are allowing the surplus pool size to
3544 * exceed overcommit. There are few sane options here. Since
Sidhartha Kumar3a740e82023-01-13 16:30:53 -06003545 * alloc_surplus_hugetlb_folio() is checking the global counter,
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08003546 * though, we'll note that we're not allowed to exceed surplus
3547 * and won't grow the pool anywhere else. Not until one of the
3548 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07003549 */
Andi Kleena5516432008-07-23 21:27:41 -07003550 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07003551 min_count = max(count, min_count);
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003552 try_to_free_low(h, min_count, nodes_allowed);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003553
3554 /*
3555 * Collect pages to be removed on list without dropping lock
3556 */
Andi Kleena5516432008-07-23 21:27:41 -07003557 while (min_count < persistent_huge_pages(h)) {
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003558 page = remove_pool_huge_page(h, nodes_allowed, 0);
3559 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 break;
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003561
3562 list_add(&page->lru, &page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 }
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003564 /* free the pages after dropping lock */
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003565 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003566 update_and_free_pages_bulk(h, &page_list);
Muchun Songb65d4ad2021-06-30 18:47:17 -07003567 flush_free_hpage_work(h);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003568 spin_lock_irq(&hugetlb_lock);
Mike Kravetz10c6ec42021-05-04 18:35:03 -07003569
Andi Kleena5516432008-07-23 21:27:41 -07003570 while (count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08003571 if (!adjust_pool_surplus(h, nodes_allowed, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07003572 break;
3573 }
3574out:
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003575 h->max_huge_pages = persistent_huge_pages(h);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003576 spin_unlock_irq(&hugetlb_lock);
Mike Kravetz29383962021-05-04 18:34:52 -07003577 mutex_unlock(&h->resize_lock);
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003578
Mike Kravetzf60858f2019-09-23 15:37:35 -07003579 NODEMASK_FREE(node_alloc_noretry);
3580
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582}
3583
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003584static int demote_free_hugetlb_folio(struct hstate *h, struct folio *folio)
Mike Kravetz8531fc62021-11-05 13:41:33 -07003585{
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003586 int i, nid = folio_nid(folio);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003587 struct hstate *target_hstate;
Doug Berger31731452022-09-14 12:09:17 -07003588 struct page *subpage;
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003589 struct folio *inner_folio;
Mike Kravetz8531fc62021-11-05 13:41:33 -07003590 int rc = 0;
3591
3592 target_hstate = size_to_hstate(PAGE_SIZE << h->demote_order);
3593
Sidhartha Kumarcfd50822022-11-29 14:50:33 -08003594 remove_hugetlb_folio_for_demote(h, folio, false);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003595 spin_unlock_irq(&hugetlb_lock);
3596
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003597 rc = hugetlb_vmemmap_restore(h, &folio->page);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003598 if (rc) {
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003599 /* Allocation of vmemmmap failed, we can not demote folio */
Mike Kravetz8531fc62021-11-05 13:41:33 -07003600 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003601 folio_ref_unfreeze(folio, 1);
3602 add_hugetlb_folio(h, folio, false);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003603 return rc;
3604 }
3605
3606 /*
Sidhartha Kumar911565b2022-11-29 14:50:31 -08003607 * Use destroy_compound_hugetlb_folio_for_demote for all huge page
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003608 * sizes as it will not ref count folios.
Mike Kravetz8531fc62021-11-05 13:41:33 -07003609 */
Sidhartha Kumar911565b2022-11-29 14:50:31 -08003610 destroy_compound_hugetlb_folio_for_demote(folio, huge_page_order(h));
Mike Kravetz8531fc62021-11-05 13:41:33 -07003611
3612 /*
3613 * Taking target hstate mutex synchronizes with set_max_huge_pages.
3614 * Without the mutex, pages added to target hstate could be marked
3615 * as surplus.
3616 *
3617 * Note that we already hold h->resize_lock. To prevent deadlock,
3618 * use the convention of always taking larger size hstate mutex first.
3619 */
3620 mutex_lock(&target_hstate->resize_lock);
3621 for (i = 0; i < pages_per_huge_page(h);
3622 i += pages_per_huge_page(target_hstate)) {
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003623 subpage = folio_page(folio, i);
3624 inner_folio = page_folio(subpage);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003625 if (hstate_is_gigantic(target_hstate))
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003626 prep_compound_gigantic_folio_for_demote(inner_folio,
Mike Kravetz8531fc62021-11-05 13:41:33 -07003627 target_hstate->order);
3628 else
Doug Berger31731452022-09-14 12:09:17 -07003629 prep_compound_page(subpage, target_hstate->order);
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003630 folio_change_private(inner_folio, NULL);
3631 prep_new_hugetlb_folio(target_hstate, inner_folio, nid);
Mike Kravetz2b216242022-09-16 14:46:38 -07003632 free_huge_page(subpage);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003633 }
3634 mutex_unlock(&target_hstate->resize_lock);
3635
3636 spin_lock_irq(&hugetlb_lock);
3637
3638 /*
3639 * Not absolutely necessary, but for consistency update max_huge_pages
3640 * based on pool changes for the demoted page.
3641 */
3642 h->max_huge_pages--;
Miaohe Lina43a83c2022-08-16 21:05:48 +08003643 target_hstate->max_huge_pages +=
3644 pages_per_huge_page(h) / pages_per_huge_page(target_hstate);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003645
3646 return rc;
3647}
3648
Mike Kravetz79dfc692021-11-05 13:41:20 -07003649static int demote_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
3650 __must_hold(&hugetlb_lock)
3651{
Mike Kravetz8531fc62021-11-05 13:41:33 -07003652 int nr_nodes, node;
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003653 struct folio *folio;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003654
3655 lockdep_assert_held(&hugetlb_lock);
3656
3657 /* We should never get here if no demote order */
3658 if (!h->demote_order) {
3659 pr_warn("HugeTLB: NULL demote order passed to demote_pool_huge_page.\n");
3660 return -EINVAL; /* internal error */
3661 }
3662
Mike Kravetz8531fc62021-11-05 13:41:33 -07003663 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003664 list_for_each_entry(folio, &h->hugepage_freelists[node], lru) {
3665 if (folio_test_hwpoison(folio))
Mike Kravetz5a317412022-04-14 19:13:52 -07003666 continue;
Sidhartha Kumarbdd7be02023-01-13 16:30:57 -06003667 return demote_free_hugetlb_folio(h, folio);
Mike Kravetz8531fc62021-11-05 13:41:33 -07003668 }
3669 }
3670
Mike Kravetz5a317412022-04-14 19:13:52 -07003671 /*
3672 * Only way to get here is if all pages on free lists are poisoned.
3673 * Return -EBUSY so that caller will not retry.
3674 */
3675 return -EBUSY;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003676}
3677
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003678#define HSTATE_ATTR_RO(_name) \
3679 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
3680
Mike Kravetz79dfc692021-11-05 13:41:20 -07003681#define HSTATE_ATTR_WO(_name) \
3682 static struct kobj_attribute _name##_attr = __ATTR_WO(_name)
3683
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003684#define HSTATE_ATTR(_name) \
Miaohe Lin98bc26a2022-03-22 14:45:23 -07003685 static struct kobj_attribute _name##_attr = __ATTR_RW(_name)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003686
3687static struct kobject *hugepages_kobj;
3688static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
3689
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003690static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
3691
3692static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003693{
3694 int i;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003695
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003696 for (i = 0; i < HUGE_MAX_HSTATE; i++)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003697 if (hstate_kobjs[i] == kobj) {
3698 if (nidp)
3699 *nidp = NUMA_NO_NODE;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003700 return &hstates[i];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003701 }
3702
3703 return kobj_to_node_hstate(kobj, nidp);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003704}
3705
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003706static ssize_t nr_hugepages_show_common(struct kobject *kobj,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003707 struct kobj_attribute *attr, char *buf)
3708{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003709 struct hstate *h;
3710 unsigned long nr_huge_pages;
3711 int nid;
3712
3713 h = kobj_to_hstate(kobj, &nid);
3714 if (nid == NUMA_NO_NODE)
3715 nr_huge_pages = h->nr_huge_pages;
3716 else
3717 nr_huge_pages = h->nr_huge_pages_node[nid];
3718
Joe Perchesae7a9272020-12-14 19:14:42 -08003719 return sysfs_emit(buf, "%lu\n", nr_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003720}
Eric B Munsonadbe8722011-01-13 15:47:27 -08003721
David Rientjes238d3c12014-08-06 16:06:51 -07003722static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
3723 struct hstate *h, int nid,
3724 unsigned long count, size_t len)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003725{
3726 int err;
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003727 nodemask_t nodes_allowed, *n_mask;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003728
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003729 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
3730 return -EINVAL;
Eric B Munsonadbe8722011-01-13 15:47:27 -08003731
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003732 if (nid == NUMA_NO_NODE) {
3733 /*
3734 * global hstate attribute
3735 */
3736 if (!(obey_mempolicy &&
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003737 init_nodemask_of_mempolicy(&nodes_allowed)))
3738 n_mask = &node_states[N_MEMORY];
3739 else
3740 n_mask = &nodes_allowed;
3741 } else {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003742 /*
Mike Kravetzfd875dc2019-05-13 17:19:20 -07003743 * Node specific request. count adjustment happens in
3744 * set_max_huge_pages() after acquiring hugetlb_lock.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003745 */
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003746 init_nodemask_of_node(&nodes_allowed, nid);
3747 n_mask = &nodes_allowed;
Mike Kravetzfd875dc2019-05-13 17:19:20 -07003748 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003749
Oscar Salvador2d0adf72019-05-13 17:19:23 -07003750 err = set_max_huge_pages(h, count, nid, n_mask);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003751
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07003752 return err ? err : len;
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003753}
3754
David Rientjes238d3c12014-08-06 16:06:51 -07003755static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
3756 struct kobject *kobj, const char *buf,
3757 size_t len)
3758{
3759 struct hstate *h;
3760 unsigned long count;
3761 int nid;
3762 int err;
3763
3764 err = kstrtoul(buf, 10, &count);
3765 if (err)
3766 return err;
3767
3768 h = kobj_to_hstate(kobj, &nid);
3769 return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
3770}
3771
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003772static ssize_t nr_hugepages_show(struct kobject *kobj,
3773 struct kobj_attribute *attr, char *buf)
3774{
3775 return nr_hugepages_show_common(kobj, attr, buf);
3776}
3777
3778static ssize_t nr_hugepages_store(struct kobject *kobj,
3779 struct kobj_attribute *attr, const char *buf, size_t len)
3780{
David Rientjes238d3c12014-08-06 16:06:51 -07003781 return nr_hugepages_store_common(false, kobj, buf, len);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003782}
3783HSTATE_ATTR(nr_hugepages);
3784
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003785#ifdef CONFIG_NUMA
3786
3787/*
3788 * hstate attribute for optionally mempolicy-based constraint on persistent
3789 * huge page alloc/free.
3790 */
3791static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
Joe Perchesae7a9272020-12-14 19:14:42 -08003792 struct kobj_attribute *attr,
3793 char *buf)
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003794{
3795 return nr_hugepages_show_common(kobj, attr, buf);
3796}
3797
3798static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
3799 struct kobj_attribute *attr, const char *buf, size_t len)
3800{
David Rientjes238d3c12014-08-06 16:06:51 -07003801 return nr_hugepages_store_common(true, kobj, buf, len);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003802}
3803HSTATE_ATTR(nr_hugepages_mempolicy);
3804#endif
3805
3806
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003807static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
3808 struct kobj_attribute *attr, char *buf)
3809{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003810 struct hstate *h = kobj_to_hstate(kobj, NULL);
Joe Perchesae7a9272020-12-14 19:14:42 -08003811 return sysfs_emit(buf, "%lu\n", h->nr_overcommit_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003812}
Eric B Munsonadbe8722011-01-13 15:47:27 -08003813
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003814static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
3815 struct kobj_attribute *attr, const char *buf, size_t count)
3816{
3817 int err;
3818 unsigned long input;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003819 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003820
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07003821 if (hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08003822 return -EINVAL;
3823
Jingoo Han3dbb95f2013-09-11 14:20:25 -07003824 err = kstrtoul(buf, 10, &input);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003825 if (err)
Eric B Munson73ae31e2011-01-13 15:47:28 -08003826 return err;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003827
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003828 spin_lock_irq(&hugetlb_lock);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003829 h->nr_overcommit_huge_pages = input;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07003830 spin_unlock_irq(&hugetlb_lock);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003831
3832 return count;
3833}
3834HSTATE_ATTR(nr_overcommit_hugepages);
3835
3836static ssize_t free_hugepages_show(struct kobject *kobj,
3837 struct kobj_attribute *attr, char *buf)
3838{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003839 struct hstate *h;
3840 unsigned long free_huge_pages;
3841 int nid;
3842
3843 h = kobj_to_hstate(kobj, &nid);
3844 if (nid == NUMA_NO_NODE)
3845 free_huge_pages = h->free_huge_pages;
3846 else
3847 free_huge_pages = h->free_huge_pages_node[nid];
3848
Joe Perchesae7a9272020-12-14 19:14:42 -08003849 return sysfs_emit(buf, "%lu\n", free_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003850}
3851HSTATE_ATTR_RO(free_hugepages);
3852
3853static ssize_t resv_hugepages_show(struct kobject *kobj,
3854 struct kobj_attribute *attr, char *buf)
3855{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003856 struct hstate *h = kobj_to_hstate(kobj, NULL);
Joe Perchesae7a9272020-12-14 19:14:42 -08003857 return sysfs_emit(buf, "%lu\n", h->resv_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003858}
3859HSTATE_ATTR_RO(resv_hugepages);
3860
3861static ssize_t surplus_hugepages_show(struct kobject *kobj,
3862 struct kobj_attribute *attr, char *buf)
3863{
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08003864 struct hstate *h;
3865 unsigned long surplus_huge_pages;
3866 int nid;
3867
3868 h = kobj_to_hstate(kobj, &nid);
3869 if (nid == NUMA_NO_NODE)
3870 surplus_huge_pages = h->surplus_huge_pages;
3871 else
3872 surplus_huge_pages = h->surplus_huge_pages_node[nid];
3873
Joe Perchesae7a9272020-12-14 19:14:42 -08003874 return sysfs_emit(buf, "%lu\n", surplus_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003875}
3876HSTATE_ATTR_RO(surplus_hugepages);
3877
Mike Kravetz79dfc692021-11-05 13:41:20 -07003878static ssize_t demote_store(struct kobject *kobj,
3879 struct kobj_attribute *attr, const char *buf, size_t len)
3880{
3881 unsigned long nr_demote;
3882 unsigned long nr_available;
3883 nodemask_t nodes_allowed, *n_mask;
3884 struct hstate *h;
Li zeming8eeda552022-09-05 10:09:18 +08003885 int err;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003886 int nid;
3887
3888 err = kstrtoul(buf, 10, &nr_demote);
3889 if (err)
3890 return err;
3891 h = kobj_to_hstate(kobj, &nid);
3892
3893 if (nid != NUMA_NO_NODE) {
3894 init_nodemask_of_node(&nodes_allowed, nid);
3895 n_mask = &nodes_allowed;
3896 } else {
3897 n_mask = &node_states[N_MEMORY];
3898 }
3899
3900 /* Synchronize with other sysfs operations modifying huge pages */
3901 mutex_lock(&h->resize_lock);
3902 spin_lock_irq(&hugetlb_lock);
3903
3904 while (nr_demote) {
3905 /*
3906 * Check for available pages to demote each time thorough the
3907 * loop as demote_pool_huge_page will drop hugetlb_lock.
Mike Kravetz79dfc692021-11-05 13:41:20 -07003908 */
3909 if (nid != NUMA_NO_NODE)
3910 nr_available = h->free_huge_pages_node[nid];
3911 else
3912 nr_available = h->free_huge_pages;
3913 nr_available -= h->resv_huge_pages;
3914 if (!nr_available)
3915 break;
3916
3917 err = demote_pool_huge_page(h, n_mask);
3918 if (err)
3919 break;
3920
3921 nr_demote--;
3922 }
3923
3924 spin_unlock_irq(&hugetlb_lock);
3925 mutex_unlock(&h->resize_lock);
3926
3927 if (err)
3928 return err;
3929 return len;
3930}
3931HSTATE_ATTR_WO(demote);
3932
3933static ssize_t demote_size_show(struct kobject *kobj,
3934 struct kobj_attribute *attr, char *buf)
3935{
Miaohe Lin12658ab2022-09-01 20:00:26 +08003936 struct hstate *h = kobj_to_hstate(kobj, NULL);
Mike Kravetz79dfc692021-11-05 13:41:20 -07003937 unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K;
3938
3939 return sysfs_emit(buf, "%lukB\n", demote_size);
3940}
3941
3942static ssize_t demote_size_store(struct kobject *kobj,
3943 struct kobj_attribute *attr,
3944 const char *buf, size_t count)
3945{
3946 struct hstate *h, *demote_hstate;
3947 unsigned long demote_size;
3948 unsigned int demote_order;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003949
3950 demote_size = (unsigned long)memparse(buf, NULL);
3951
3952 demote_hstate = size_to_hstate(demote_size);
3953 if (!demote_hstate)
3954 return -EINVAL;
3955 demote_order = demote_hstate->order;
Mike Kravetza01f4392021-11-05 13:41:27 -07003956 if (demote_order < HUGETLB_PAGE_ORDER)
3957 return -EINVAL;
Mike Kravetz79dfc692021-11-05 13:41:20 -07003958
3959 /* demote order must be smaller than hstate order */
Miaohe Lin12658ab2022-09-01 20:00:26 +08003960 h = kobj_to_hstate(kobj, NULL);
Mike Kravetz79dfc692021-11-05 13:41:20 -07003961 if (demote_order >= h->order)
3962 return -EINVAL;
3963
3964 /* resize_lock synchronizes access to demote size and writes */
3965 mutex_lock(&h->resize_lock);
3966 h->demote_order = demote_order;
3967 mutex_unlock(&h->resize_lock);
3968
3969 return count;
3970}
3971HSTATE_ATTR(demote_size);
3972
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003973static struct attribute *hstate_attrs[] = {
3974 &nr_hugepages_attr.attr,
3975 &nr_overcommit_hugepages_attr.attr,
3976 &free_hugepages_attr.attr,
3977 &resv_hugepages_attr.attr,
3978 &surplus_hugepages_attr.attr,
Lee Schermerhorn06808b02009-12-14 17:58:21 -08003979#ifdef CONFIG_NUMA
3980 &nr_hugepages_mempolicy_attr.attr,
3981#endif
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003982 NULL,
3983};
3984
Arvind Yadav67e5ed92017-09-06 16:22:06 -07003985static const struct attribute_group hstate_attr_group = {
Nishanth Aravamudana3437872008-07-23 21:27:44 -07003986 .attrs = hstate_attrs,
3987};
3988
Mike Kravetz79dfc692021-11-05 13:41:20 -07003989static struct attribute *hstate_demote_attrs[] = {
3990 &demote_size_attr.attr,
3991 &demote_attr.attr,
3992 NULL,
3993};
3994
3995static const struct attribute_group hstate_demote_attr_group = {
3996 .attrs = hstate_demote_attrs,
3997};
3998
Jeff Mahoney094e9532010-02-02 13:44:14 -08003999static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
4000 struct kobject **hstate_kobjs,
Arvind Yadav67e5ed92017-09-06 16:22:06 -07004001 const struct attribute_group *hstate_attr_group)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004002{
4003 int retval;
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004004 int hi = hstate_index(h);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004005
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004006 hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
4007 if (!hstate_kobjs[hi])
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004008 return -ENOMEM;
4009
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004010 retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
Miaohe Lincc2205a2021-02-24 12:06:50 -08004011 if (retval) {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004012 kobject_put(hstate_kobjs[hi]);
Miaohe Lincc2205a2021-02-24 12:06:50 -08004013 hstate_kobjs[hi] = NULL;
Miaohe Lin3a6bdda2022-08-16 21:05:49 +08004014 return retval;
Miaohe Lincc2205a2021-02-24 12:06:50 -08004015 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004016
Mike Kravetz79dfc692021-11-05 13:41:20 -07004017 if (h->demote_order) {
Miaohe Lin01088a62022-08-16 21:05:52 +08004018 retval = sysfs_create_group(hstate_kobjs[hi],
4019 &hstate_demote_attr_group);
4020 if (retval) {
Mike Kravetz79dfc692021-11-05 13:41:20 -07004021 pr_warn("HugeTLB unable to create demote interfaces for %s\n", h->name);
Miaohe Lin01088a62022-08-16 21:05:52 +08004022 sysfs_remove_group(hstate_kobjs[hi], hstate_attr_group);
4023 kobject_put(hstate_kobjs[hi]);
4024 hstate_kobjs[hi] = NULL;
4025 return retval;
4026 }
Mike Kravetz79dfc692021-11-05 13:41:20 -07004027 }
4028
Miaohe Lin01088a62022-08-16 21:05:52 +08004029 return 0;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004030}
4031
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004032#ifdef CONFIG_NUMA
Muchun Songa4a00b42022-09-14 15:26:03 +08004033static bool hugetlb_sysfs_initialized __ro_after_init;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004034
4035/*
4036 * node_hstate/s - associate per node hstate attributes, via their kobjects,
Kay Sievers10fbcf42011-12-21 14:48:43 -08004037 * with node devices in node_devices[] using a parallel array. The array
4038 * index of a node device or _hstate == node id.
4039 * This is here to avoid any static dependency of the node device driver, in
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004040 * the base kernel, on the hugetlb module.
4041 */
4042struct node_hstate {
4043 struct kobject *hugepages_kobj;
4044 struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
4045};
Alexander Kuleshovb4e289a2015-11-05 18:50:14 -08004046static struct node_hstate node_hstates[MAX_NUMNODES];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004047
4048/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08004049 * A subset of global hstate attributes for node devices
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004050 */
4051static struct attribute *per_node_hstate_attrs[] = {
4052 &nr_hugepages_attr.attr,
4053 &free_hugepages_attr.attr,
4054 &surplus_hugepages_attr.attr,
4055 NULL,
4056};
4057
Arvind Yadav67e5ed92017-09-06 16:22:06 -07004058static const struct attribute_group per_node_hstate_attr_group = {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004059 .attrs = per_node_hstate_attrs,
4060};
4061
4062/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08004063 * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004064 * Returns node id via non-NULL nidp.
4065 */
4066static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
4067{
4068 int nid;
4069
4070 for (nid = 0; nid < nr_node_ids; nid++) {
4071 struct node_hstate *nhs = &node_hstates[nid];
4072 int i;
4073 for (i = 0; i < HUGE_MAX_HSTATE; i++)
4074 if (nhs->hstate_kobjs[i] == kobj) {
4075 if (nidp)
4076 *nidp = nid;
4077 return &hstates[i];
4078 }
4079 }
4080
4081 BUG();
4082 return NULL;
4083}
4084
4085/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08004086 * Unregister hstate attributes from a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004087 * No-op if no hstate attributes attached.
4088 */
Muchun Songa4a00b42022-09-14 15:26:03 +08004089void hugetlb_unregister_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004090{
4091 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08004092 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004093
4094 if (!nhs->hugepages_kobj)
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08004095 return; /* no hstate attributes */
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004096
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004097 for_each_hstate(h) {
4098 int idx = hstate_index(h);
Miaohe Lin01088a62022-08-16 21:05:52 +08004099 struct kobject *hstate_kobj = nhs->hstate_kobjs[idx];
4100
4101 if (!hstate_kobj)
4102 continue;
4103 if (h->demote_order)
4104 sysfs_remove_group(hstate_kobj, &hstate_demote_attr_group);
4105 sysfs_remove_group(hstate_kobj, &per_node_hstate_attr_group);
4106 kobject_put(hstate_kobj);
4107 nhs->hstate_kobjs[idx] = NULL;
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07004108 }
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004109
4110 kobject_put(nhs->hugepages_kobj);
4111 nhs->hugepages_kobj = NULL;
4112}
4113
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004114
4115/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08004116 * Register hstate attributes for a single node device.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004117 * No-op if attributes already registered.
4118 */
Muchun Songa4a00b42022-09-14 15:26:03 +08004119void hugetlb_register_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004120{
4121 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08004122 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004123 int err;
4124
Muchun Songa4a00b42022-09-14 15:26:03 +08004125 if (!hugetlb_sysfs_initialized)
4126 return;
4127
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004128 if (nhs->hugepages_kobj)
4129 return; /* already allocated */
4130
4131 nhs->hugepages_kobj = kobject_create_and_add("hugepages",
Kay Sievers10fbcf42011-12-21 14:48:43 -08004132 &node->dev.kobj);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004133 if (!nhs->hugepages_kobj)
4134 return;
4135
4136 for_each_hstate(h) {
4137 err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
4138 nhs->hstate_kobjs,
4139 &per_node_hstate_attr_group);
4140 if (err) {
Mike Kravetz282f4212020-06-03 16:00:46 -07004141 pr_err("HugeTLB: Unable to add hstate %s for node %d\n",
Andrew Mortonffb22af2013-02-22 16:32:08 -08004142 h->name, node->dev.id);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004143 hugetlb_unregister_node(node);
4144 break;
4145 }
4146 }
4147}
4148
4149/*
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08004150 * hugetlb init time: register hstate attributes for all registered node
Kay Sievers10fbcf42011-12-21 14:48:43 -08004151 * devices of nodes that have memory. All on-line nodes should have
4152 * registered their associated device by this time.
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004153 */
Luiz Capitulino7d9ca002014-12-12 16:55:24 -08004154static void __init hugetlb_register_all_nodes(void)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004155{
4156 int nid;
4157
Muchun Songa4a00b42022-09-14 15:26:03 +08004158 for_each_online_node(nid)
Muchun Songb958d4d2022-09-14 15:26:02 +08004159 hugetlb_register_node(node_devices[nid]);
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004160}
4161#else /* !CONFIG_NUMA */
4162
4163static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
4164{
4165 BUG();
4166 if (nidp)
4167 *nidp = -1;
4168 return NULL;
4169}
4170
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004171static void hugetlb_register_all_nodes(void) { }
4172
4173#endif
4174
Miaohe Lin263b8992022-09-01 20:00:21 +08004175#ifdef CONFIG_CMA
4176static void __init hugetlb_cma_check(void);
4177#else
4178static inline __init void hugetlb_cma_check(void)
4179{
4180}
4181#endif
4182
Muchun Songa4a00b42022-09-14 15:26:03 +08004183static void __init hugetlb_sysfs_init(void)
4184{
4185 struct hstate *h;
4186 int err;
4187
4188 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
4189 if (!hugepages_kobj)
4190 return;
4191
4192 for_each_hstate(h) {
4193 err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
4194 hstate_kobjs, &hstate_attr_group);
4195 if (err)
4196 pr_err("HugeTLB: Unable to add hstate %s", h->name);
4197 }
4198
4199#ifdef CONFIG_NUMA
4200 hugetlb_sysfs_initialized = true;
4201#endif
4202 hugetlb_register_all_nodes();
4203}
4204
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004205static int __init hugetlb_init(void)
4206{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004207 int i;
4208
Mike Kravetzd6995da2021-02-24 12:08:51 -08004209 BUILD_BUG_ON(sizeof_field(struct page, private) * BITS_PER_BYTE <
4210 __NR_HPAGEFLAGS);
4211
Mike Kravetzc2833a52020-06-03 16:00:50 -07004212 if (!hugepages_supported()) {
4213 if (hugetlb_max_hstate || default_hstate_max_huge_pages)
4214 pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n");
Benjamin Herrenschmidt0ef89d22008-07-31 00:07:30 -07004215 return 0;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004216 }
Vaishali Thakkarf8b74812016-02-17 13:11:26 -08004217
Mike Kravetz282f4212020-06-03 16:00:46 -07004218 /*
4219 * Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists. Some
4220 * architectures depend on setup being done here.
4221 */
4222 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
4223 if (!parsed_default_hugepagesz) {
4224 /*
4225 * If we did not parse a default huge page size, set
4226 * default_hstate_idx to HPAGE_SIZE hstate. And, if the
4227 * number of huge pages for this default size was implicitly
4228 * specified, set that here as well.
4229 * Note that the implicit setting will overwrite an explicit
4230 * setting. A warning will be printed in this case.
4231 */
4232 default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE));
4233 if (default_hstate_max_huge_pages) {
4234 if (default_hstate.max_huge_pages) {
4235 char buf[32];
Andi Kleenaa888a72008-07-23 21:27:47 -07004236
Mike Kravetz282f4212020-06-03 16:00:46 -07004237 string_get_size(huge_page_size(&default_hstate),
4238 1, STRING_UNITS_2, buf, 32);
4239 pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n",
4240 default_hstate.max_huge_pages, buf);
4241 pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n",
4242 default_hstate_max_huge_pages);
4243 }
4244 default_hstate.max_huge_pages =
4245 default_hstate_max_huge_pages;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004246
Peng Liu0a7a0f62022-04-29 14:36:57 -07004247 for_each_online_node(i)
Zhenguo Yaob5389082021-11-05 13:43:28 -07004248 default_hstate.max_huge_pages_node[i] =
4249 default_hugepages_in_node[i];
Mike Kravetz282f4212020-06-03 16:00:46 -07004250 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004251 }
Andi Kleenaa888a72008-07-23 21:27:47 -07004252
Roman Gushchincf11e852020-04-10 14:32:45 -07004253 hugetlb_cma_check();
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004254 hugetlb_init_hstates();
Andi Kleenaa888a72008-07-23 21:27:47 -07004255 gather_bootmem_prealloc();
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004256 report_hugepages();
4257
4258 hugetlb_sysfs_init();
Jianguo Wu7179e7b2012-12-18 14:23:19 -08004259 hugetlb_cgroup_file_init();
Lee Schermerhorn9a3052302009-12-14 17:58:25 -08004260
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004261#ifdef CONFIG_SMP
4262 num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
4263#else
4264 num_fault_mutexes = 1;
4265#endif
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004266 hugetlb_fault_mutex_table =
Kees Cook6da2ec52018-06-12 13:55:00 -07004267 kmalloc_array(num_fault_mutexes, sizeof(struct mutex),
4268 GFP_KERNEL);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004269 BUG_ON(!hugetlb_fault_mutex_table);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07004270
4271 for (i = 0; i < num_fault_mutexes; i++)
Mike Kravetzc672c7f2015-09-08 15:01:35 -07004272 mutex_init(&hugetlb_fault_mutex_table[i]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004273 return 0;
4274}
Paul Gortmaker3e89e1c2016-01-14 15:21:52 -08004275subsys_initcall(hugetlb_init);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004276
Mike Kravetzae94da82020-06-03 16:00:34 -07004277/* Overwritten by architectures with more huge page sizes */
4278bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size)
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004279{
Mike Kravetzae94da82020-06-03 16:00:34 -07004280 return size == HPAGE_SIZE;
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004281}
4282
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08004283void __init hugetlb_add_hstate(unsigned int order)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004284{
4285 struct hstate *h;
Andi Kleen8faa8b02008-07-23 21:27:48 -07004286 unsigned long i;
4287
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004288 if (size_to_hstate(PAGE_SIZE << order)) {
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004289 return;
4290 }
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07004291 BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004292 BUG_ON(order == 0);
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07004293 h = &hstates[hugetlb_max_hstate++];
Mike Kravetz29383962021-05-04 18:34:52 -07004294 mutex_init(&h->resize_lock);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004295 h->order = order;
Miaohe Linaca78302021-02-24 12:07:46 -08004296 h->mask = ~(huge_page_size(h) - 1);
Andi Kleen8faa8b02008-07-23 21:27:48 -07004297 for (i = 0; i < MAX_NUMNODES; ++i)
4298 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07004299 INIT_LIST_HEAD(&h->hugepage_activelist);
Andrew Morton54f18d32016-05-19 17:11:40 -07004300 h->next_nid_to_alloc = first_memory_node;
4301 h->next_nid_to_free = first_memory_node;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004302 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
Miaohe Linc2c3a602022-09-01 20:00:22 +08004303 huge_page_size(h)/SZ_1K);
Andi Kleen8faa8b02008-07-23 21:27:48 -07004304
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004305 parsed_hstate = h;
4306}
4307
Zhenguo Yaob5389082021-11-05 13:43:28 -07004308bool __init __weak hugetlb_node_alloc_supported(void)
4309{
4310 return true;
4311}
Peng Liuf87442f2022-04-29 14:36:57 -07004312
4313static void __init hugepages_clear_pages_in_node(void)
4314{
4315 if (!hugetlb_max_hstate) {
4316 default_hstate_max_huge_pages = 0;
4317 memset(default_hugepages_in_node, 0,
Miaohe Lin10395682022-09-01 20:00:24 +08004318 sizeof(default_hugepages_in_node));
Peng Liuf87442f2022-04-29 14:36:57 -07004319 } else {
4320 parsed_hstate->max_huge_pages = 0;
4321 memset(parsed_hstate->max_huge_pages_node, 0,
Miaohe Lin10395682022-09-01 20:00:24 +08004322 sizeof(parsed_hstate->max_huge_pages_node));
Peng Liuf87442f2022-04-29 14:36:57 -07004323 }
4324}
4325
Mike Kravetz282f4212020-06-03 16:00:46 -07004326/*
4327 * hugepages command line processing
4328 * hugepages normally follows a valid hugepagsz or default_hugepagsz
4329 * specification. If not, ignore the hugepages value. hugepages can also
4330 * be the first huge page command line option in which case it implicitly
4331 * specifies the number of huge pages for the default size.
4332 */
4333static int __init hugepages_setup(char *s)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004334{
4335 unsigned long *mhp;
Andi Kleen8faa8b02008-07-23 21:27:48 -07004336 static unsigned long *last_mhp;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004337 int node = NUMA_NO_NODE;
4338 int count;
4339 unsigned long tmp;
4340 char *p = s;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004341
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004342 if (!parsed_valid_hugepagesz) {
Mike Kravetz282f4212020-06-03 16:00:46 -07004343 pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s);
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004344 parsed_valid_hugepagesz = true;
Peng Liuf81f6e42022-04-29 14:36:57 -07004345 return 1;
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004346 }
Mike Kravetz282f4212020-06-03 16:00:46 -07004347
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004348 /*
Mike Kravetz282f4212020-06-03 16:00:46 -07004349 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
4350 * yet, so this hugepages= parameter goes to the "default hstate".
4351 * Otherwise, it goes with the previously parsed hugepagesz or
4352 * default_hugepagesz.
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004353 */
Vaishali Thakkar9fee0212016-05-19 17:11:04 -07004354 else if (!hugetlb_max_hstate)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004355 mhp = &default_hstate_max_huge_pages;
4356 else
4357 mhp = &parsed_hstate->max_huge_pages;
4358
Andi Kleen8faa8b02008-07-23 21:27:48 -07004359 if (mhp == last_mhp) {
Mike Kravetz282f4212020-06-03 16:00:46 -07004360 pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004361 return 1;
Andi Kleen8faa8b02008-07-23 21:27:48 -07004362 }
4363
Zhenguo Yaob5389082021-11-05 13:43:28 -07004364 while (*p) {
4365 count = 0;
4366 if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4367 goto invalid;
4368 /* Parameter is node format */
4369 if (p[count] == ':') {
4370 if (!hugetlb_node_alloc_supported()) {
4371 pr_warn("HugeTLB: architecture can't support node specific alloc, ignoring!\n");
Peng Liuf81f6e42022-04-29 14:36:57 -07004372 return 1;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004373 }
Peng Liu0a7a0f62022-04-29 14:36:57 -07004374 if (tmp >= MAX_NUMNODES || !node_online(tmp))
Liu Yuntaoe79ce982022-02-25 19:11:02 -08004375 goto invalid;
Peng Liu0a7a0f62022-04-29 14:36:57 -07004376 node = array_index_nospec(tmp, MAX_NUMNODES);
Zhenguo Yaob5389082021-11-05 13:43:28 -07004377 p += count + 1;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004378 /* Parse hugepages */
4379 if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4380 goto invalid;
4381 if (!hugetlb_max_hstate)
4382 default_hugepages_in_node[node] = tmp;
4383 else
4384 parsed_hstate->max_huge_pages_node[node] = tmp;
4385 *mhp += tmp;
4386 /* Go to parse next node*/
4387 if (p[count] == ',')
4388 p += count + 1;
4389 else
4390 break;
4391 } else {
4392 if (p != s)
4393 goto invalid;
4394 *mhp = tmp;
4395 break;
4396 }
4397 }
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004398
Andi Kleen8faa8b02008-07-23 21:27:48 -07004399 /*
4400 * Global state is always initialized later in hugetlb_init.
Miaohe Lin04adbc32021-05-04 18:33:22 -07004401 * But we need to allocate gigantic hstates here early to still
Andi Kleen8faa8b02008-07-23 21:27:48 -07004402 * use the bootmem allocator.
4403 */
Miaohe Lin04adbc32021-05-04 18:33:22 -07004404 if (hugetlb_max_hstate && hstate_is_gigantic(parsed_hstate))
Andi Kleen8faa8b02008-07-23 21:27:48 -07004405 hugetlb_hstate_alloc_pages(parsed_hstate);
4406
4407 last_mhp = mhp;
4408
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004409 return 1;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004410
4411invalid:
4412 pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p);
Peng Liuf87442f2022-04-29 14:36:57 -07004413 hugepages_clear_pages_in_node();
Peng Liuf81f6e42022-04-29 14:36:57 -07004414 return 1;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004415}
Mike Kravetz282f4212020-06-03 16:00:46 -07004416__setup("hugepages=", hugepages_setup);
Nick Piggine11bfbf2008-07-23 21:27:52 -07004417
Mike Kravetz282f4212020-06-03 16:00:46 -07004418/*
4419 * hugepagesz command line processing
4420 * A specific huge page size can only be specified once with hugepagesz.
4421 * hugepagesz is followed by hugepages on the command line. The global
4422 * variable 'parsed_valid_hugepagesz' is used to determine if prior
4423 * hugepagesz argument was valid.
4424 */
Mike Kravetz359f2542020-06-03 16:00:38 -07004425static int __init hugepagesz_setup(char *s)
Nick Piggine11bfbf2008-07-23 21:27:52 -07004426{
Mike Kravetz359f2542020-06-03 16:00:38 -07004427 unsigned long size;
Mike Kravetz282f4212020-06-03 16:00:46 -07004428 struct hstate *h;
4429
4430 parsed_valid_hugepagesz = false;
Mike Kravetz359f2542020-06-03 16:00:38 -07004431 size = (unsigned long)memparse(s, NULL);
4432
4433 if (!arch_hugetlb_valid_size(size)) {
Mike Kravetz282f4212020-06-03 16:00:46 -07004434 pr_err("HugeTLB: unsupported hugepagesz=%s\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004435 return 1;
Mike Kravetz359f2542020-06-03 16:00:38 -07004436 }
4437
Mike Kravetz282f4212020-06-03 16:00:46 -07004438 h = size_to_hstate(size);
4439 if (h) {
4440 /*
4441 * hstate for this size already exists. This is normally
4442 * an error, but is allowed if the existing hstate is the
4443 * default hstate. More specifically, it is only allowed if
4444 * the number of huge pages for the default hstate was not
4445 * previously specified.
4446 */
4447 if (!parsed_default_hugepagesz || h != &default_hstate ||
4448 default_hstate.max_huge_pages) {
4449 pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004450 return 1;
Mike Kravetz282f4212020-06-03 16:00:46 -07004451 }
4452
4453 /*
4454 * No need to call hugetlb_add_hstate() as hstate already
4455 * exists. But, do set parsed_hstate so that a following
4456 * hugepages= parameter will be applied to this hstate.
4457 */
4458 parsed_hstate = h;
4459 parsed_valid_hugepagesz = true;
4460 return 1;
Mike Kravetz38237832020-06-03 16:00:42 -07004461 }
4462
Mike Kravetz359f2542020-06-03 16:00:38 -07004463 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
Mike Kravetz282f4212020-06-03 16:00:46 -07004464 parsed_valid_hugepagesz = true;
Nick Piggine11bfbf2008-07-23 21:27:52 -07004465 return 1;
4466}
Mike Kravetz359f2542020-06-03 16:00:38 -07004467__setup("hugepagesz=", hugepagesz_setup);
4468
Mike Kravetz282f4212020-06-03 16:00:46 -07004469/*
4470 * default_hugepagesz command line input
4471 * Only one instance of default_hugepagesz allowed on command line.
4472 */
Mike Kravetzae94da82020-06-03 16:00:34 -07004473static int __init default_hugepagesz_setup(char *s)
Nick Piggine11bfbf2008-07-23 21:27:52 -07004474{
Mike Kravetzae94da82020-06-03 16:00:34 -07004475 unsigned long size;
Zhenguo Yaob5389082021-11-05 13:43:28 -07004476 int i;
Mike Kravetzae94da82020-06-03 16:00:34 -07004477
Mike Kravetz282f4212020-06-03 16:00:46 -07004478 parsed_valid_hugepagesz = false;
Mike Kravetz282f4212020-06-03 16:00:46 -07004479 if (parsed_default_hugepagesz) {
4480 pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004481 return 1;
Mike Kravetz282f4212020-06-03 16:00:46 -07004482 }
4483
4484 size = (unsigned long)memparse(s, NULL);
4485
4486 if (!arch_hugetlb_valid_size(size)) {
4487 pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s);
Peng Liuf81f6e42022-04-29 14:36:57 -07004488 return 1;
Mike Kravetz282f4212020-06-03 16:00:46 -07004489 }
4490
4491 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
4492 parsed_valid_hugepagesz = true;
4493 parsed_default_hugepagesz = true;
4494 default_hstate_idx = hstate_index(size_to_hstate(size));
4495
4496 /*
4497 * The number of default huge pages (for this size) could have been
4498 * specified as the first hugetlb parameter: hugepages=X. If so,
4499 * then default_hstate_max_huge_pages is set. If the default huge
4500 * page size is gigantic (>= MAX_ORDER), then the pages must be
4501 * allocated here from bootmem allocator.
4502 */
4503 if (default_hstate_max_huge_pages) {
4504 default_hstate.max_huge_pages = default_hstate_max_huge_pages;
Peng Liu0a7a0f62022-04-29 14:36:57 -07004505 for_each_online_node(i)
Zhenguo Yaob5389082021-11-05 13:43:28 -07004506 default_hstate.max_huge_pages_node[i] =
4507 default_hugepages_in_node[i];
Mike Kravetz282f4212020-06-03 16:00:46 -07004508 if (hstate_is_gigantic(&default_hstate))
4509 hugetlb_hstate_alloc_pages(&default_hstate);
4510 default_hstate_max_huge_pages = 0;
4511 }
4512
Nick Piggine11bfbf2008-07-23 21:27:52 -07004513 return 1;
4514}
Mike Kravetzae94da82020-06-03 16:00:34 -07004515__setup("default_hugepagesz=", default_hugepagesz_setup);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07004516
Feng Tangd2226eb2022-08-05 08:59:03 +08004517static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
4518{
4519#ifdef CONFIG_NUMA
4520 struct mempolicy *mpol = get_task_policy(current);
4521
4522 /*
4523 * Only enforce MPOL_BIND policy which overlaps with cpuset policy
4524 * (from policy_nodemask) specifically for hugetlb case
4525 */
4526 if (mpol->mode == MPOL_BIND &&
4527 (apply_policy_zone(mpol, gfp_zone(gfp)) &&
4528 cpuset_nodemask_valid_mems_allowed(&mpol->nodes)))
4529 return &mpol->nodes;
4530#endif
4531 return NULL;
4532}
4533
Muchun Song8ca39e62020-08-11 18:30:32 -07004534static unsigned int allowed_mems_nr(struct hstate *h)
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07004535{
4536 int node;
4537 unsigned int nr = 0;
Feng Tangd2226eb2022-08-05 08:59:03 +08004538 nodemask_t *mbind_nodemask;
Muchun Song8ca39e62020-08-11 18:30:32 -07004539 unsigned int *array = h->free_huge_pages_node;
4540 gfp_t gfp_mask = htlb_alloc_mask(h);
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07004541
Feng Tangd2226eb2022-08-05 08:59:03 +08004542 mbind_nodemask = policy_mbind_nodemask(gfp_mask);
Muchun Song8ca39e62020-08-11 18:30:32 -07004543 for_each_node_mask(node, cpuset_current_mems_allowed) {
Feng Tangd2226eb2022-08-05 08:59:03 +08004544 if (!mbind_nodemask || node_isset(node, *mbind_nodemask))
Muchun Song8ca39e62020-08-11 18:30:32 -07004545 nr += array[node];
4546 }
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07004547
4548 return nr;
4549}
4550
4551#ifdef CONFIG_SYSCTL
Muchun Song17743792020-09-04 16:36:13 -07004552static int proc_hugetlb_doulongvec_minmax(struct ctl_table *table, int write,
4553 void *buffer, size_t *length,
4554 loff_t *ppos, unsigned long *out)
4555{
4556 struct ctl_table dup_table;
4557
4558 /*
4559 * In order to avoid races with __do_proc_doulongvec_minmax(), we
4560 * can duplicate the @table and alter the duplicate of it.
4561 */
4562 dup_table = *table;
4563 dup_table.data = out;
4564
4565 return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos);
4566}
4567
Lee Schermerhorn06808b02009-12-14 17:58:21 -08004568static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
4569 struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02004570 void *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571{
Andi Kleene5ff2152008-07-23 21:27:42 -07004572 struct hstate *h = &default_hstate;
David Rientjes238d3c12014-08-06 16:06:51 -07004573 unsigned long tmp = h->max_huge_pages;
Michal Hocko08d4a242011-01-13 15:47:26 -08004574 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07004575
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004576 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08004577 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004578
Muchun Song17743792020-09-04 16:36:13 -07004579 ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
4580 &tmp);
Michal Hocko08d4a242011-01-13 15:47:26 -08004581 if (ret)
4582 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07004583
David Rientjes238d3c12014-08-06 16:06:51 -07004584 if (write)
4585 ret = __nr_hugepages_store_common(obey_mempolicy, h,
4586 NUMA_NO_NODE, tmp, *length);
Michal Hocko08d4a242011-01-13 15:47:26 -08004587out:
4588 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589}
Mel Gorman396faf02007-07-17 04:03:13 -07004590
Lee Schermerhorn06808b02009-12-14 17:58:21 -08004591int hugetlb_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02004592 void *buffer, size_t *length, loff_t *ppos)
Lee Schermerhorn06808b02009-12-14 17:58:21 -08004593{
4594
4595 return hugetlb_sysctl_handler_common(false, table, write,
4596 buffer, length, ppos);
4597}
4598
4599#ifdef CONFIG_NUMA
4600int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02004601 void *buffer, size_t *length, loff_t *ppos)
Lee Schermerhorn06808b02009-12-14 17:58:21 -08004602{
4603 return hugetlb_sysctl_handler_common(true, table, write,
4604 buffer, length, ppos);
4605}
4606#endif /* CONFIG_NUMA */
4607
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08004608int hugetlb_overcommit_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02004609 void *buffer, size_t *length, loff_t *ppos)
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08004610{
Andi Kleena5516432008-07-23 21:27:41 -07004611 struct hstate *h = &default_hstate;
Andi Kleene5ff2152008-07-23 21:27:42 -07004612 unsigned long tmp;
Michal Hocko08d4a242011-01-13 15:47:26 -08004613 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07004614
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004615 if (!hugepages_supported())
Jan Stancek86613622016-03-09 14:08:35 -08004616 return -EOPNOTSUPP;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004617
Petr Holasekc033a932011-03-22 16:33:05 -07004618 tmp = h->nr_overcommit_huge_pages;
Andi Kleene5ff2152008-07-23 21:27:42 -07004619
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07004620 if (write && hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08004621 return -EINVAL;
4622
Muchun Song17743792020-09-04 16:36:13 -07004623 ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
4624 &tmp);
Michal Hocko08d4a242011-01-13 15:47:26 -08004625 if (ret)
4626 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07004627
4628 if (write) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -07004629 spin_lock_irq(&hugetlb_lock);
Andi Kleene5ff2152008-07-23 21:27:42 -07004630 h->nr_overcommit_huge_pages = tmp;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07004631 spin_unlock_irq(&hugetlb_lock);
Andi Kleene5ff2152008-07-23 21:27:42 -07004632 }
Michal Hocko08d4a242011-01-13 15:47:26 -08004633out:
4634 return ret;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08004635}
4636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637#endif /* CONFIG_SYSCTL */
4638
Alexey Dobriyane1759c22008-10-15 23:50:22 +04004639void hugetlb_report_meminfo(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640{
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08004641 struct hstate *h;
4642 unsigned long total = 0;
4643
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004644 if (!hugepages_supported())
4645 return;
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08004646
4647 for_each_hstate(h) {
4648 unsigned long count = h->nr_huge_pages;
4649
Miaohe Linaca78302021-02-24 12:07:46 -08004650 total += huge_page_size(h) * count;
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08004651
4652 if (h == &default_hstate)
4653 seq_printf(m,
4654 "HugePages_Total: %5lu\n"
4655 "HugePages_Free: %5lu\n"
4656 "HugePages_Rsvd: %5lu\n"
4657 "HugePages_Surp: %5lu\n"
4658 "Hugepagesize: %8lu kB\n",
4659 count,
4660 h->free_huge_pages,
4661 h->resv_huge_pages,
4662 h->surplus_huge_pages,
Miaohe Linaca78302021-02-24 12:07:46 -08004663 huge_page_size(h) / SZ_1K);
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -08004664 }
4665
Miaohe Linaca78302021-02-24 12:07:46 -08004666 seq_printf(m, "Hugetlb: %8lu kB\n", total / SZ_1K);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667}
4668
Joe Perches79815932020-09-16 13:40:43 -07004669int hugetlb_report_node_meminfo(char *buf, int len, int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
Andi Kleena5516432008-07-23 21:27:41 -07004671 struct hstate *h = &default_hstate;
Joe Perches79815932020-09-16 13:40:43 -07004672
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004673 if (!hugepages_supported())
4674 return 0;
Joe Perches79815932020-09-16 13:40:43 -07004675
4676 return sysfs_emit_at(buf, len,
4677 "Node %d HugePages_Total: %5u\n"
4678 "Node %d HugePages_Free: %5u\n"
4679 "Node %d HugePages_Surp: %5u\n",
4680 nid, h->nr_huge_pages_node[nid],
4681 nid, h->free_huge_pages_node[nid],
4682 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
Gang Lidcadcf12022-07-06 11:46:54 +08004685void hugetlb_show_meminfo_node(int nid)
David Rientjes949f7ec2013-04-29 15:07:48 -07004686{
4687 struct hstate *h;
David Rientjes949f7ec2013-04-29 15:07:48 -07004688
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07004689 if (!hugepages_supported())
4690 return;
4691
Gang Lidcadcf12022-07-06 11:46:54 +08004692 for_each_hstate(h)
4693 printk("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
4694 nid,
4695 h->nr_huge_pages_node[nid],
4696 h->free_huge_pages_node[nid],
4697 h->surplus_huge_pages_node[nid],
4698 huge_page_size(h) / SZ_1K);
David Rientjes949f7ec2013-04-29 15:07:48 -07004699}
4700
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08004701void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
4702{
4703 seq_printf(m, "HugetlbPages:\t%8lu kB\n",
4704 atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10));
4705}
4706
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
4708unsigned long hugetlb_total_pages(void)
4709{
Wanpeng Lid0028582013-03-22 15:04:40 -07004710 struct hstate *h;
4711 unsigned long nr_total_pages = 0;
4712
4713 for_each_hstate(h)
4714 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
4715 return nr_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Andi Kleena5516432008-07-23 21:27:41 -07004718static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004719{
4720 int ret = -ENOMEM;
4721
Miaohe Lin0aa7f352021-02-24 12:06:57 -08004722 if (!delta)
4723 return 0;
4724
Mike Kravetzdb71ef72021-05-04 18:35:07 -07004725 spin_lock_irq(&hugetlb_lock);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004726 /*
4727 * When cpuset is configured, it breaks the strict hugetlb page
4728 * reservation as the accounting is done on a global variable. Such
4729 * reservation is completely rubbish in the presence of cpuset because
4730 * the reservation is not checked against page availability for the
4731 * current cpuset. Application can still potentially OOM'ed by kernel
4732 * with lack of free htlb page in cpuset that the task is in.
4733 * Attempt to enforce strict accounting with cpuset is almost
4734 * impossible (or too ugly) because cpuset is too fluid that
4735 * task or memory node can be dynamically moved between cpusets.
4736 *
4737 * The change of semantics for shared hugetlb mapping with cpuset is
4738 * undesirable. However, in order to preserve some of the semantics,
4739 * we fall back to check against current free page availability as
4740 * a best attempt and hopefully to minimize the impact of changing
4741 * semantics that cpuset has.
Muchun Song8ca39e62020-08-11 18:30:32 -07004742 *
4743 * Apart from cpuset, we also have memory policy mechanism that
4744 * also determines from which node the kernel will allocate memory
4745 * in a NUMA system. So similar to cpuset, we also should consider
4746 * the memory policy of the current task. Similar to the description
4747 * above.
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004748 */
4749 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07004750 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004751 goto out;
4752
Muchun Song8ca39e62020-08-11 18:30:32 -07004753 if (delta > allowed_mems_nr(h)) {
Andi Kleena5516432008-07-23 21:27:41 -07004754 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004755 goto out;
4756 }
4757 }
4758
4759 ret = 0;
4760 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07004761 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004762
4763out:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07004764 spin_unlock_irq(&hugetlb_lock);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07004765 return ret;
4766}
4767
Andy Whitcroft84afd992008-07-23 21:27:32 -07004768static void hugetlb_vm_op_open(struct vm_area_struct *vma)
4769{
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07004770 struct resv_map *resv = vma_resv_map(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07004771
4772 /*
Mike Kravetz612b8a32022-10-19 13:19:57 -07004773 * HPAGE_RESV_OWNER indicates a private mapping.
Andy Whitcroft84afd992008-07-23 21:27:32 -07004774 * This new VMA should share its siblings reservation map if present.
4775 * The VMA will only ever have a valid reservation map pointer where
4776 * it is being copied for another still existing VMA. As that VMA
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004777 * has a reference to the reservation map it cannot disappear until
Andy Whitcroft84afd992008-07-23 21:27:32 -07004778 * after this open call completes. It is therefore safe to take a
4779 * new reference here without additional locking.
4780 */
Mike Kravetz09a26e82021-09-02 14:58:53 -07004781 if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
4782 resv_map_dup_hugetlb_cgroup_uncharge_info(resv);
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07004783 kref_get(&resv->refs);
Mike Kravetz09a26e82021-09-02 14:58:53 -07004784 }
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07004785
Mike Kravetz131a79b2022-10-04 18:17:05 -07004786 /*
4787 * vma_lock structure for sharable mappings is vma specific.
Mike Kravetz612b8a32022-10-19 13:19:57 -07004788 * Clear old pointer (if copied via vm_area_dup) and allocate
4789 * new structure. Before clearing, make sure vma_lock is not
4790 * for this vma.
Mike Kravetz131a79b2022-10-04 18:17:05 -07004791 */
4792 if (vma->vm_flags & VM_MAYSHARE) {
Mike Kravetz612b8a32022-10-19 13:19:57 -07004793 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
4794
4795 if (vma_lock) {
4796 if (vma_lock->vma != vma) {
4797 vma->vm_private_data = NULL;
4798 hugetlb_vma_lock_alloc(vma);
4799 } else
4800 pr_warn("HugeTLB: vma_lock already exists in %s.\n", __func__);
4801 } else
4802 hugetlb_vma_lock_alloc(vma);
Mike Kravetz131a79b2022-10-04 18:17:05 -07004803 }
Andy Whitcroft84afd992008-07-23 21:27:32 -07004804}
4805
Mel Gormana1e78772008-07-23 21:27:23 -07004806static void hugetlb_vm_op_close(struct vm_area_struct *vma)
4807{
Andi Kleena5516432008-07-23 21:27:41 -07004808 struct hstate *h = hstate_vma(vma);
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07004809 struct resv_map *resv;
David Gibson90481622012-03-21 16:34:12 -07004810 struct hugepage_subpool *spool = subpool_vma(vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004811 unsigned long reserve, start, end;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004812 long gbl_reserve;
Andy Whitcroft84afd992008-07-23 21:27:32 -07004813
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07004814 hugetlb_vma_lock_free(vma);
4815
4816 resv = vma_resv_map(vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004817 if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
4818 return;
Andy Whitcroft84afd992008-07-23 21:27:32 -07004819
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004820 start = vma_hugecache_offset(h, vma, vma->vm_start);
4821 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07004822
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004823 reserve = (end - start) - region_count(resv, start, end);
Mina Almasrye9fe92a2020-04-01 21:11:21 -07004824 hugetlb_cgroup_uncharge_counter(resv, start, end);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07004825 if (reserve) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07004826 /*
4827 * Decrement reserve counts. The global reserve count may be
4828 * adjusted if the subpool has a minimum size.
4829 */
4830 gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
4831 hugetlb_acct_memory(h, -gbl_reserve);
Andy Whitcroft84afd992008-07-23 21:27:32 -07004832 }
Mina Almasrye9fe92a2020-04-01 21:11:21 -07004833
4834 kref_put(&resv->refs, resv_map_release);
Mel Gormana1e78772008-07-23 21:27:23 -07004835}
4836
Dan Williams31383c62017-11-29 16:10:28 -08004837static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
4838{
4839 if (addr & ~(huge_page_mask(hstate_vma(vma))))
4840 return -EINVAL;
James Houghtonb30c14c2023-01-04 23:19:10 +00004841
4842 /*
4843 * PMD sharing is only possible for PUD_SIZE-aligned address ranges
4844 * in HugeTLB VMAs. If we will lose PUD_SIZE alignment due to this
4845 * split, unshare PMDs in the PUD_SIZE interval surrounding addr now.
4846 */
4847 if (addr & ~PUD_MASK) {
4848 /*
4849 * hugetlb_vm_op_split is called right before we attempt to
4850 * split the VMA. We will need to unshare PMDs in the old and
4851 * new VMAs, so let's unshare before we split.
4852 */
4853 unsigned long floor = addr & PUD_MASK;
4854 unsigned long ceil = floor + PUD_SIZE;
4855
4856 if (floor >= vma->vm_start && ceil <= vma->vm_end)
4857 hugetlb_unshare_pmds(vma, floor, ceil);
4858 }
4859
Dan Williams31383c62017-11-29 16:10:28 -08004860 return 0;
4861}
4862
Dan Williams05ea8862018-04-05 16:24:25 -07004863static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
4864{
Miaohe Linaca78302021-02-24 12:07:46 -08004865 return huge_page_size(hstate_vma(vma));
Dan Williams05ea8862018-04-05 16:24:25 -07004866}
4867
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868/*
4869 * We cannot handle pagefaults against hugetlb pages at all. They cause
4870 * handle_mm_fault() to try to instantiate regular-sized pages in the
Miaohe Lin6c26d312021-02-24 12:07:19 -08004871 * hugepage VMA. do_page_fault() is supposed to trap this, so BUG is we get
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 * this far.
4873 */
Souptick Joarderb3ec9f32018-06-07 17:08:04 -07004874static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
4876 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07004877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878}
4879
Jane Chueec36362018-08-02 15:36:05 -07004880/*
4881 * When a new function is introduced to vm_operations_struct and added
4882 * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
4883 * This is because under System V memory model, mappings created via
4884 * shmget/shmat with "huge page" specified are backed by hugetlbfs files,
4885 * their original vm_ops are overwritten with shm_vm_ops.
4886 */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004887const struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07004888 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07004889 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07004890 .close = hugetlb_vm_op_close,
Dmitry Safonovdd3b6142020-12-14 19:08:17 -08004891 .may_split = hugetlb_vm_op_split,
Dan Williams05ea8862018-04-05 16:24:25 -07004892 .pagesize = hugetlb_vm_op_pagesize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893};
4894
David Gibson1e8f8892006-01-06 00:10:44 -08004895static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
4896 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07004897{
4898 pte_t entry;
Christophe Leroy79c1c592021-06-30 18:48:00 -07004899 unsigned int shift = huge_page_shift(hstate_vma(vma));
David Gibson63551ae2005-06-21 17:14:44 -07004900
David Gibson1e8f8892006-01-06 00:10:44 -08004901 if (writable) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07004902 entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
4903 vma->vm_page_prot)));
David Gibson63551ae2005-06-21 17:14:44 -07004904 } else {
Gerald Schaefer106c9922013-04-29 15:07:23 -07004905 entry = huge_pte_wrprotect(mk_huge_pte(page,
4906 vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07004907 }
4908 entry = pte_mkyoung(entry);
Christophe Leroy79c1c592021-06-30 18:48:00 -07004909 entry = arch_make_huge_pte(entry, shift, vma->vm_flags);
David Gibson63551ae2005-06-21 17:14:44 -07004910
4911 return entry;
4912}
4913
David Gibson1e8f8892006-01-06 00:10:44 -08004914static void set_huge_ptep_writable(struct vm_area_struct *vma,
4915 unsigned long address, pte_t *ptep)
4916{
4917 pte_t entry;
4918
Gerald Schaefer106c9922013-04-29 15:07:23 -07004919 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
Chris Forbes32f84522011-07-25 17:12:14 -07004920 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
Russell King4b3073e2009-12-18 16:40:18 +00004921 update_mmu_cache(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08004922}
4923
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07004924bool is_hugetlb_entry_migration(pte_t pte)
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004925{
4926 swp_entry_t swp;
4927
4928 if (huge_pte_none(pte) || pte_present(pte))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07004929 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004930 swp = pte_to_swp_entry(pte);
Baoquan Hed79d1762020-10-13 16:56:14 -07004931 if (is_migration_entry(swp))
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07004932 return true;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004933 else
Aneesh Kumar K.Vd5ed7442017-07-06 15:38:47 -07004934 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004935}
4936
Baoquan He3e5c3602020-10-13 16:56:10 -07004937static bool is_hugetlb_entry_hwpoisoned(pte_t pte)
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004938{
4939 swp_entry_t swp;
4940
4941 if (huge_pte_none(pte) || pte_present(pte))
Baoquan He3e5c3602020-10-13 16:56:10 -07004942 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004943 swp = pte_to_swp_entry(pte);
Baoquan Hed79d1762020-10-13 16:56:14 -07004944 if (is_hwpoison_entry(swp))
Baoquan He3e5c3602020-10-13 16:56:10 -07004945 return true;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004946 else
Baoquan He3e5c3602020-10-13 16:56:10 -07004947 return false;
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07004948}
David Gibson1e8f8892006-01-06 00:10:44 -08004949
Peter Xu4eae4ef2021-03-12 21:07:33 -08004950static void
Sidhartha Kumarea4c3532023-01-25 09:05:30 -08004951hugetlb_install_folio(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr,
4952 struct folio *new_folio)
Peter Xu4eae4ef2021-03-12 21:07:33 -08004953{
Sidhartha Kumarea4c3532023-01-25 09:05:30 -08004954 __folio_mark_uptodate(new_folio);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08004955 hugepage_add_new_anon_rmap(new_folio, vma, addr);
Sidhartha Kumarea4c3532023-01-25 09:05:30 -08004956 set_huge_pte_at(vma->vm_mm, addr, ptep, make_huge_pte(vma, &new_folio->page, 1));
Peter Xu4eae4ef2021-03-12 21:07:33 -08004957 hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm);
Sidhartha Kumarea4c3532023-01-25 09:05:30 -08004958 folio_set_hugetlb_migratable(new_folio);
Peter Xu4eae4ef2021-03-12 21:07:33 -08004959}
4960
David Gibson63551ae2005-06-21 17:14:44 -07004961int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
Peter Xubc70fbf2022-05-12 20:22:55 -07004962 struct vm_area_struct *dst_vma,
4963 struct vm_area_struct *src_vma)
David Gibson63551ae2005-06-21 17:14:44 -07004964{
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08004965 pte_t *src_pte, *dst_pte, entry;
David Gibson63551ae2005-06-21 17:14:44 -07004966 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07004967 unsigned long addr;
Peter Xubc70fbf2022-05-12 20:22:55 -07004968 bool cow = is_cow_mapping(src_vma->vm_flags);
4969 struct hstate *h = hstate_vma(src_vma);
Andi Kleena5516432008-07-23 21:27:41 -07004970 unsigned long sz = huge_page_size(h);
Peter Xu4eae4ef2021-03-12 21:07:33 -08004971 unsigned long npages = pages_per_huge_page(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004972 struct mmu_notifier_range range;
Mike Kravetze95a9852022-06-21 16:56:17 -07004973 unsigned long last_addr_mask;
Andreas Sandberge8569dd2014-01-21 15:49:09 -08004974 int ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08004975
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004976 if (cow) {
Alistair Popple7d4a8be2023-01-10 13:57:22 +11004977 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, src,
Peter Xubc70fbf2022-05-12 20:22:55 -07004978 src_vma->vm_start,
4979 src_vma->vm_end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004980 mmu_notifier_invalidate_range_start(&range);
David Hildenbrand623a1dd2022-05-09 18:20:42 -07004981 mmap_assert_write_locked(src);
4982 raw_write_seqcount_begin(&src->write_protect_seq);
Mike Kravetz40549ba2022-09-14 15:18:09 -07004983 } else {
4984 /*
4985 * For shared mappings the vma lock must be held before
Peter Xu9c67a202022-12-16 10:52:29 -05004986 * calling hugetlb_walk() in the src vma. Otherwise, the
Mike Kravetz40549ba2022-09-14 15:18:09 -07004987 * returned ptep could go away if part of a shared pmd and
4988 * another thread calls huge_pmd_unshare.
4989 */
4990 hugetlb_vma_lock_read(src_vma);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004991 }
Andreas Sandberge8569dd2014-01-21 15:49:09 -08004992
Mike Kravetze95a9852022-06-21 16:56:17 -07004993 last_addr_mask = hugetlb_mask_last_page(h);
Peter Xubc70fbf2022-05-12 20:22:55 -07004994 for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08004995 spinlock_t *src_ptl, *dst_ptl;
Peter Xu9c67a202022-12-16 10:52:29 -05004996 src_pte = hugetlb_walk(src_vma, addr, sz);
Mike Kravetze95a9852022-06-21 16:56:17 -07004997 if (!src_pte) {
4998 addr |= last_addr_mask;
Hugh Dickinsc74df322005-10-29 18:16:23 -07004999 continue;
Mike Kravetze95a9852022-06-21 16:56:17 -07005000 }
Peter Xubc70fbf2022-05-12 20:22:55 -07005001 dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08005002 if (!dst_pte) {
5003 ret = -ENOMEM;
5004 break;
5005 }
Larry Woodmanc5c99422008-01-24 05:49:25 -08005006
Mike Kravetz5e415402018-11-16 15:08:04 -08005007 /*
5008 * If the pagetables are shared don't copy or take references.
Mike Kravetz5e415402018-11-16 15:08:04 -08005009 *
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005010 * dst_pte == src_pte is the common case of src/dest sharing.
Mike Kravetz5e415402018-11-16 15:08:04 -08005011 * However, src could have 'unshared' and dst shares with
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005012 * another vma. So page_count of ptep page is checked instead
5013 * to reliably determine whether pte is shared.
Mike Kravetz5e415402018-11-16 15:08:04 -08005014 */
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005015 if (page_count(virt_to_page(dst_pte)) > 1) {
Mike Kravetze95a9852022-06-21 16:56:17 -07005016 addr |= last_addr_mask;
Larry Woodmanc5c99422008-01-24 05:49:25 -08005017 continue;
Mike Kravetze95a9852022-06-21 16:56:17 -07005018 }
Larry Woodmanc5c99422008-01-24 05:49:25 -08005019
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005020 dst_ptl = huge_pte_lock(h, dst, dst_pte);
5021 src_ptl = huge_pte_lockptr(h, src, src_pte);
5022 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005023 entry = huge_ptep_get(src_pte);
Peter Xu4eae4ef2021-03-12 21:07:33 -08005024again:
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005025 if (huge_pte_none(entry)) {
Mike Kravetz5e415402018-11-16 15:08:04 -08005026 /*
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005027 * Skip if src entry none.
Mike Kravetz5e415402018-11-16 15:08:04 -08005028 */
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005029 ;
Naoya Horiguchic2cb0dc2022-07-04 10:33:05 +09005030 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) {
5031 bool uffd_wp = huge_pte_uffd_wp(entry);
5032
5033 if (!userfaultfd_wp(dst_vma) && uffd_wp)
5034 entry = huge_pte_clear_uffd_wp(entry);
5035 set_huge_pte_at(dst, addr, dst_pte, entry);
5036 } else if (unlikely(is_hugetlb_entry_migration(entry))) {
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005037 swp_entry_t swp_entry = pte_to_swp_entry(entry);
Peter Xubc70fbf2022-05-12 20:22:55 -07005038 bool uffd_wp = huge_pte_uffd_wp(entry);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005039
David Hildenbrand6c287602022-05-09 18:20:44 -07005040 if (!is_readable_migration_entry(swp_entry) && cow) {
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005041 /*
5042 * COW mappings require pages in both
5043 * parent and child to be set to read.
5044 */
Alistair Popple4dd845b2021-06-30 18:54:09 -07005045 swp_entry = make_readable_migration_entry(
5046 swp_offset(swp_entry));
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005047 entry = swp_entry_to_pte(swp_entry);
Peter Xubc70fbf2022-05-12 20:22:55 -07005048 if (userfaultfd_wp(src_vma) && uffd_wp)
5049 entry = huge_pte_mkuffd_wp(entry);
Qi Zheng18f39622022-06-26 22:57:17 +08005050 set_huge_pte_at(src, addr, src_pte, entry);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005051 }
Peter Xubc70fbf2022-05-12 20:22:55 -07005052 if (!userfaultfd_wp(dst_vma) && uffd_wp)
5053 entry = huge_pte_clear_uffd_wp(entry);
Qi Zheng18f39622022-06-26 22:57:17 +08005054 set_huge_pte_at(dst, addr, dst_pte, entry);
Peter Xubc70fbf2022-05-12 20:22:55 -07005055 } else if (unlikely(is_pte_marker(entry))) {
Peter Xu7e3ce3f2022-12-14 15:04:53 -05005056 /* No swap on hugetlb */
5057 WARN_ON_ONCE(
5058 is_swapin_error_entry(pte_to_swp_entry(entry)));
Peter Xubc70fbf2022-05-12 20:22:55 -07005059 /*
5060 * We copy the pte marker only if the dst vma has
5061 * uffd-wp enabled.
5062 */
5063 if (userfaultfd_wp(dst_vma))
5064 set_huge_pte_at(dst, addr, dst_pte, entry);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07005065 } else {
Peter Xu4eae4ef2021-03-12 21:07:33 -08005066 entry = huge_ptep_get(src_pte);
5067 ptepage = pte_page(entry);
5068 get_page(ptepage);
5069
5070 /*
David Hildenbrandfb3d8242022-05-09 18:20:43 -07005071 * Failing to duplicate the anon rmap is a rare case
5072 * where we see pinned hugetlb pages while they're
5073 * prone to COW. We need to do the COW earlier during
5074 * fork.
Peter Xu4eae4ef2021-03-12 21:07:33 -08005075 *
5076 * When pre-allocating the page or copying data, we
5077 * need to be without the pgtable locks since we could
5078 * sleep during the process.
5079 */
David Hildenbrandfb3d8242022-05-09 18:20:43 -07005080 if (!PageAnon(ptepage)) {
5081 page_dup_file_rmap(ptepage, true);
Peter Xubc70fbf2022-05-12 20:22:55 -07005082 } else if (page_try_dup_anon_rmap(ptepage, true,
5083 src_vma)) {
Peter Xu4eae4ef2021-03-12 21:07:33 -08005084 pte_t src_pte_old = entry;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005085 struct folio *new_folio;
Peter Xu4eae4ef2021-03-12 21:07:33 -08005086
5087 spin_unlock(src_ptl);
5088 spin_unlock(dst_ptl);
5089 /* Do not use reserve as it's private owned */
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005090 new_folio = alloc_hugetlb_folio(dst_vma, addr, 1);
5091 if (IS_ERR(new_folio)) {
Peter Xu4eae4ef2021-03-12 21:07:33 -08005092 put_page(ptepage);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005093 ret = PTR_ERR(new_folio);
Peter Xu4eae4ef2021-03-12 21:07:33 -08005094 break;
5095 }
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005096 copy_user_huge_page(&new_folio->page, ptepage, addr, dst_vma,
Peter Xu4eae4ef2021-03-12 21:07:33 -08005097 npages);
5098 put_page(ptepage);
5099
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005100 /* Install the new hugetlb folio if src pte stable */
Peter Xu4eae4ef2021-03-12 21:07:33 -08005101 dst_ptl = huge_pte_lock(h, dst, dst_pte);
5102 src_ptl = huge_pte_lockptr(h, src, src_pte);
5103 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5104 entry = huge_ptep_get(src_pte);
5105 if (!pte_same(src_pte_old, entry)) {
Peter Xubc70fbf2022-05-12 20:22:55 -07005106 restore_reserve_on_error(h, dst_vma, addr,
Sidhartha Kumard2d7bb42023-01-25 09:05:34 -08005107 new_folio);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005108 folio_put(new_folio);
Miaohe Lin3aa4ed82022-08-16 21:05:53 +08005109 /* huge_ptep of dst_pte won't change as in child */
Peter Xu4eae4ef2021-03-12 21:07:33 -08005110 goto again;
5111 }
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005112 hugetlb_install_folio(dst_vma, dst_pte, addr, new_folio);
Peter Xu4eae4ef2021-03-12 21:07:33 -08005113 spin_unlock(src_ptl);
5114 spin_unlock(dst_ptl);
5115 continue;
5116 }
5117
Joerg Roedel34ee6452014-11-13 13:46:09 +11005118 if (cow) {
Jérôme Glisse0f108512017-11-15 17:34:07 -08005119 /*
5120 * No need to notify as we are downgrading page
5121 * table protection not changing it to point
5122 * to a new page.
5123 *
Mike Rapoportee657282022-06-27 09:00:26 +03005124 * See Documentation/mm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08005125 */
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07005126 huge_ptep_set_wrprotect(src, addr, src_pte);
Peter Xu84894e12021-05-14 17:27:07 -07005127 entry = huge_pte_wrprotect(entry);
Joerg Roedel34ee6452014-11-13 13:46:09 +11005128 }
Peter Xu4eae4ef2021-03-12 21:07:33 -08005129
Hugh Dickins1c598272005-10-19 21:23:43 -07005130 set_huge_pte_at(dst, addr, dst_pte, entry);
Peter Xu4eae4ef2021-03-12 21:07:33 -08005131 hugetlb_count_add(npages, dst);
Hugh Dickins1c598272005-10-19 21:23:43 -07005132 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005133 spin_unlock(src_ptl);
5134 spin_unlock(dst_ptl);
David Gibson63551ae2005-06-21 17:14:44 -07005135 }
David Gibson63551ae2005-06-21 17:14:44 -07005136
David Hildenbrand623a1dd2022-05-09 18:20:42 -07005137 if (cow) {
5138 raw_write_seqcount_end(&src->write_protect_seq);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005139 mmu_notifier_invalidate_range_end(&range);
Mike Kravetz40549ba2022-09-14 15:18:09 -07005140 } else {
5141 hugetlb_vma_unlock_read(src_vma);
David Hildenbrand623a1dd2022-05-09 18:20:42 -07005142 }
Andreas Sandberge8569dd2014-01-21 15:49:09 -08005143
5144 return ret;
David Gibson63551ae2005-06-21 17:14:44 -07005145}
5146
Mina Almasry550a7d62021-11-05 13:41:40 -07005147static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
Aneesh Kumar K.Vdb110a92022-02-25 19:10:56 -08005148 unsigned long new_addr, pte_t *src_pte, pte_t *dst_pte)
Mina Almasry550a7d62021-11-05 13:41:40 -07005149{
5150 struct hstate *h = hstate_vma(vma);
5151 struct mm_struct *mm = vma->vm_mm;
Mina Almasry550a7d62021-11-05 13:41:40 -07005152 spinlock_t *src_ptl, *dst_ptl;
Aneesh Kumar K.Vdb110a92022-02-25 19:10:56 -08005153 pte_t pte;
Mina Almasry550a7d62021-11-05 13:41:40 -07005154
Mina Almasry550a7d62021-11-05 13:41:40 -07005155 dst_ptl = huge_pte_lock(h, mm, dst_pte);
5156 src_ptl = huge_pte_lockptr(h, mm, src_pte);
5157
5158 /*
5159 * We don't have to worry about the ordering of src and dst ptlocks
Lorenzo Stoakes8651a132023-01-07 00:00:05 +00005160 * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock.
Mina Almasry550a7d62021-11-05 13:41:40 -07005161 */
5162 if (src_ptl != dst_ptl)
5163 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5164
5165 pte = huge_ptep_get_and_clear(mm, old_addr, src_pte);
5166 set_huge_pte_at(mm, new_addr, dst_pte, pte);
5167
5168 if (src_ptl != dst_ptl)
5169 spin_unlock(src_ptl);
5170 spin_unlock(dst_ptl);
5171}
5172
5173int move_hugetlb_page_tables(struct vm_area_struct *vma,
5174 struct vm_area_struct *new_vma,
5175 unsigned long old_addr, unsigned long new_addr,
5176 unsigned long len)
5177{
5178 struct hstate *h = hstate_vma(vma);
5179 struct address_space *mapping = vma->vm_file->f_mapping;
5180 unsigned long sz = huge_page_size(h);
5181 struct mm_struct *mm = vma->vm_mm;
5182 unsigned long old_end = old_addr + len;
Mike Kravetze95a9852022-06-21 16:56:17 -07005183 unsigned long last_addr_mask;
Mina Almasry550a7d62021-11-05 13:41:40 -07005184 pte_t *src_pte, *dst_pte;
5185 struct mmu_notifier_range range;
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005186 bool shared_pmd = false;
Mina Almasry550a7d62021-11-05 13:41:40 -07005187
Alistair Popple7d4a8be2023-01-10 13:57:22 +11005188 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, old_addr,
Mina Almasry550a7d62021-11-05 13:41:40 -07005189 old_end);
5190 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005191 /*
5192 * In case of shared PMDs, we should cover the maximum possible
5193 * range.
5194 */
5195 flush_cache_range(vma, range.start, range.end);
5196
Mina Almasry550a7d62021-11-05 13:41:40 -07005197 mmu_notifier_invalidate_range_start(&range);
Mike Kravetze95a9852022-06-21 16:56:17 -07005198 last_addr_mask = hugetlb_mask_last_page(h);
Mina Almasry550a7d62021-11-05 13:41:40 -07005199 /* Prevent race with file truncation */
Mike Kravetz40549ba2022-09-14 15:18:09 -07005200 hugetlb_vma_lock_write(vma);
Mina Almasry550a7d62021-11-05 13:41:40 -07005201 i_mmap_lock_write(mapping);
5202 for (; old_addr < old_end; old_addr += sz, new_addr += sz) {
Peter Xu9c67a202022-12-16 10:52:29 -05005203 src_pte = hugetlb_walk(vma, old_addr, sz);
Mike Kravetze95a9852022-06-21 16:56:17 -07005204 if (!src_pte) {
5205 old_addr |= last_addr_mask;
5206 new_addr |= last_addr_mask;
Mina Almasry550a7d62021-11-05 13:41:40 -07005207 continue;
Mike Kravetze95a9852022-06-21 16:56:17 -07005208 }
Mina Almasry550a7d62021-11-05 13:41:40 -07005209 if (huge_pte_none(huge_ptep_get(src_pte)))
5210 continue;
5211
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07005212 if (huge_pmd_unshare(mm, vma, old_addr, src_pte)) {
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005213 shared_pmd = true;
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07005214 old_addr |= last_addr_mask;
5215 new_addr |= last_addr_mask;
Mina Almasry550a7d62021-11-05 13:41:40 -07005216 continue;
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005217 }
Mina Almasry550a7d62021-11-05 13:41:40 -07005218
5219 dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz);
5220 if (!dst_pte)
5221 break;
5222
Aneesh Kumar K.Vdb110a92022-02-25 19:10:56 -08005223 move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte);
Mina Almasry550a7d62021-11-05 13:41:40 -07005224 }
Baolin Wang3d0b95c2022-05-09 18:20:52 -07005225
5226 if (shared_pmd)
5227 flush_tlb_range(vma, range.start, range.end);
5228 else
5229 flush_tlb_range(vma, old_end - len, old_end);
Mina Almasry550a7d62021-11-05 13:41:40 -07005230 mmu_notifier_invalidate_range_end(&range);
Nadav Amit13e4ad22021-11-21 12:40:08 -08005231 i_mmap_unlock_write(mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07005232 hugetlb_vma_unlock_write(vma);
Mina Almasry550a7d62021-11-05 13:41:40 -07005233
5234 return len + old_addr - old_end;
5235}
5236
Peter Xu73c54762021-11-05 13:41:17 -07005237static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
5238 unsigned long start, unsigned long end,
Peter Xu05e90bd2022-05-12 20:22:55 -07005239 struct page *ref_page, zap_flags_t zap_flags)
David Gibson63551ae2005-06-21 17:14:44 -07005240{
5241 struct mm_struct *mm = vma->vm_mm;
5242 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07005243 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07005244 pte_t pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005245 spinlock_t *ptl;
David Gibson63551ae2005-06-21 17:14:44 -07005246 struct page *page;
Andi Kleena5516432008-07-23 21:27:41 -07005247 struct hstate *h = hstate_vma(vma);
5248 unsigned long sz = huge_page_size(h);
Mike Kravetze95a9852022-06-21 16:56:17 -07005249 unsigned long last_addr_mask;
Nadav Amita4a118f2021-11-21 12:40:07 -08005250 bool force_flush = false;
Andi Kleena5516432008-07-23 21:27:41 -07005251
David Gibson63551ae2005-06-21 17:14:44 -07005252 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07005253 BUG_ON(start & ~huge_page_mask(h));
5254 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07005255
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08005256 /*
5257 * This is a hugetlb vma, all the pte entries should point
5258 * to huge page.
5259 */
Peter Zijlstraed6a7932018-08-31 14:46:08 +02005260 tlb_change_page_size(tlb, sz);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07005261 tlb_start_vma(tlb, vma);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07005262
Mike Kravetze95a9852022-06-21 16:56:17 -07005263 last_addr_mask = hugetlb_mask_last_page(h);
Hillf Danton569f48b82014-12-10 15:44:41 -08005264 address = start;
Hillf Danton569f48b82014-12-10 15:44:41 -08005265 for (; address < end; address += sz) {
Peter Xu9c67a202022-12-16 10:52:29 -05005266 ptep = hugetlb_walk(vma, address, sz);
Mike Kravetze95a9852022-06-21 16:56:17 -07005267 if (!ptep) {
5268 address |= last_addr_mask;
David Gibsonc7546f82005-08-05 11:59:35 -07005269 continue;
Mike Kravetze95a9852022-06-21 16:56:17 -07005270 }
David Gibsonc7546f82005-08-05 11:59:35 -07005271
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005272 ptl = huge_pte_lock(h, mm, ptep);
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07005273 if (huge_pmd_unshare(mm, vma, address, ptep)) {
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005274 spin_unlock(ptl);
Nadav Amita4a118f2021-11-21 12:40:07 -08005275 tlb_flush_pmd_range(tlb, address & PUD_MASK, PUD_SIZE);
5276 force_flush = true;
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07005277 address |= last_addr_mask;
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005278 continue;
5279 }
Chen, Kenneth W39dde652006-12-06 20:32:03 -08005280
Hillf Danton66293262012-03-23 15:01:48 -07005281 pte = huge_ptep_get(ptep);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005282 if (huge_pte_none(pte)) {
5283 spin_unlock(ptl);
5284 continue;
5285 }
Hillf Danton66293262012-03-23 15:01:48 -07005286
5287 /*
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08005288 * Migrating hugepage or HWPoisoned hugepage is already
5289 * unmapped and its refcount is dropped, so just clear pte here.
Hillf Danton66293262012-03-23 15:01:48 -07005290 */
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08005291 if (unlikely(!pte_present(pte))) {
Peter Xu05e90bd2022-05-12 20:22:55 -07005292 /*
5293 * If the pte was wr-protected by uffd-wp in any of the
5294 * swap forms, meanwhile the caller does not want to
5295 * drop the uffd-wp bit in this zap, then replace the
5296 * pte with a marker.
5297 */
5298 if (pte_swp_uffd_wp_any(pte) &&
5299 !(zap_flags & ZAP_FLAG_DROP_MARKER))
5300 set_huge_pte_at(mm, address, ptep,
5301 make_pte_marker(PTE_MARKER_UFFD_WP));
5302 else
5303 huge_pte_clear(mm, address, ptep, sz);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005304 spin_unlock(ptl);
5305 continue;
Naoya Horiguchi8c4894c2012-12-12 13:52:28 -08005306 }
Hillf Danton66293262012-03-23 15:01:48 -07005307
5308 page = pte_page(pte);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005309 /*
5310 * If a reference page is supplied, it is because a specific
5311 * page is being unmapped, not a range. Ensure the page we
5312 * are about to unmap is the actual page of interest.
5313 */
5314 if (ref_page) {
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005315 if (page != ref_page) {
5316 spin_unlock(ptl);
5317 continue;
5318 }
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005319 /*
5320 * Mark the VMA as having unmapped its page so that
5321 * future faults in this VMA will fail rather than
5322 * looking like data was lost
5323 */
5324 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
5325 }
5326
David Gibsonc7546f82005-08-05 11:59:35 -07005327 pte = huge_ptep_get_and_clear(mm, address, ptep);
Aneesh Kumar K.Vb528e4b2016-12-12 16:42:37 -08005328 tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
Gerald Schaefer106c9922013-04-29 15:07:23 -07005329 if (huge_pte_dirty(pte))
Ken Chen6649a382007-02-08 14:20:27 -08005330 set_page_dirty(page);
Peter Xu05e90bd2022-05-12 20:22:55 -07005331 /* Leave a uffd-wp pte marker if needed */
5332 if (huge_pte_uffd_wp(pte) &&
5333 !(zap_flags & ZAP_FLAG_DROP_MARKER))
5334 set_huge_pte_at(mm, address, ptep,
5335 make_pte_marker(PTE_MARKER_UFFD_WP));
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08005336 hugetlb_count_sub(pages_per_huge_page(h), mm);
Hugh Dickinscea86fe2022-02-14 18:26:39 -08005337 page_remove_rmap(page, vma, true);
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005338
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005339 spin_unlock(ptl);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -07005340 tlb_remove_page_size(tlb, page, huge_page_size(h));
Aneesh Kumar K.V31d49da2016-07-26 15:24:06 -07005341 /*
5342 * Bail out after unmapping reference page if supplied
5343 */
5344 if (ref_page)
5345 break;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07005346 }
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07005347 tlb_end_vma(tlb, vma);
Nadav Amita4a118f2021-11-21 12:40:07 -08005348
5349 /*
5350 * If we unshared PMDs, the TLB flush was not recorded in mmu_gather. We
5351 * could defer the flush until now, since by holding i_mmap_rwsem we
5352 * guaranteed that the last refernece would not be dropped. But we must
5353 * do the flushing before we return, as otherwise i_mmap_rwsem will be
5354 * dropped and the last reference to the shared PMDs page might be
5355 * dropped as well.
5356 *
5357 * In theory we could defer the freeing of the PMD pages as well, but
5358 * huge_pmd_unshare() relies on the exact page_count for the PMD page to
5359 * detect sharing, so we cannot defer the release of the page either.
5360 * Instead, do flush now.
5361 */
5362 if (force_flush)
5363 tlb_flush_mmu_tlbonly(tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364}
David Gibson63551ae2005-06-21 17:14:44 -07005365
Mel Gormand8333522012-07-31 16:46:20 -07005366void __unmap_hugepage_range_final(struct mmu_gather *tlb,
5367 struct vm_area_struct *vma, unsigned long start,
Peter Xu05e90bd2022-05-12 20:22:55 -07005368 unsigned long end, struct page *ref_page,
5369 zap_flags_t zap_flags)
Mel Gormand8333522012-07-31 16:46:20 -07005370{
Mike Kravetz131a79b2022-10-04 18:17:05 -07005371 hugetlb_vma_lock_write(vma);
5372 i_mmap_lock_write(vma->vm_file->f_mapping);
5373
Mike Kravetz369258c2022-11-14 15:55:07 -08005374 /* mmu notification performed in caller */
Peter Xu05e90bd2022-05-12 20:22:55 -07005375 __unmap_hugepage_range(tlb, vma, start, end, ref_page, zap_flags);
Mel Gormand8333522012-07-31 16:46:20 -07005376
Mike Kravetz04ada092022-11-14 15:55:06 -08005377 if (zap_flags & ZAP_FLAG_UNMAP) { /* final unmap */
5378 /*
5379 * Unlock and free the vma lock before releasing i_mmap_rwsem.
5380 * When the vma_lock is freed, this makes the vma ineligible
5381 * for pmd sharing. And, i_mmap_rwsem is required to set up
5382 * pmd sharing. This is important as page tables for this
5383 * unmapped range will be asynchrously deleted. If the page
5384 * tables are shared, there will be issues when accessed by
5385 * someone else.
5386 */
5387 __hugetlb_vma_unlock_write_free(vma);
5388 i_mmap_unlock_write(vma->vm_file->f_mapping);
5389 } else {
5390 i_mmap_unlock_write(vma->vm_file->f_mapping);
5391 hugetlb_vma_unlock_write(vma);
5392 }
Mel Gormand8333522012-07-31 16:46:20 -07005393}
5394
Chen, Kenneth W502717f2006-10-11 01:20:46 -07005395void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Peter Xu05e90bd2022-05-12 20:22:55 -07005396 unsigned long end, struct page *ref_page,
5397 zap_flags_t zap_flags)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07005398{
Mike Kravetz369258c2022-11-14 15:55:07 -08005399 struct mmu_notifier_range range;
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07005400 struct mmu_gather tlb;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07005401
Alistair Popple7d4a8be2023-01-10 13:57:22 +11005402 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
Mike Kravetz369258c2022-11-14 15:55:07 -08005403 start, end);
5404 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
5405 mmu_notifier_invalidate_range_start(&range);
Will Deacona72afd82021-01-27 23:53:45 +00005406 tlb_gather_mmu(&tlb, vma->vm_mm);
Mike Kravetz369258c2022-11-14 15:55:07 -08005407
Peter Xu05e90bd2022-05-12 20:22:55 -07005408 __unmap_hugepage_range(&tlb, vma, start, end, ref_page, zap_flags);
Mike Kravetz369258c2022-11-14 15:55:07 -08005409
5410 mmu_notifier_invalidate_range_end(&range);
Will Deaconae8eba82021-01-27 23:53:43 +00005411 tlb_finish_mmu(&tlb);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07005412}
5413
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005414/*
5415 * This is called when the original mapper is failing to COW a MAP_PRIVATE
Zhiyuan Dai578b7722021-02-24 12:07:26 -08005416 * mapping it owns the reserve page for. The intention is to unmap the page
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005417 * from other VMAs and let the children be SIGKILLed if they are faulting the
5418 * same region.
5419 */
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07005420static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
5421 struct page *page, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005422{
Adam Litke75266742008-11-12 13:24:56 -08005423 struct hstate *h = hstate_vma(vma);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005424 struct vm_area_struct *iter_vma;
5425 struct address_space *mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005426 pgoff_t pgoff;
5427
5428 /*
5429 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
5430 * from page cache lookup which is in HPAGE_SIZE units.
5431 */
Adam Litke75266742008-11-12 13:24:56 -08005432 address = address & huge_page_mask(h);
Michal Hocko36e4f202012-10-08 16:33:31 -07005433 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
5434 vma->vm_pgoff;
Al Viro93c76a32015-12-04 23:45:44 -05005435 mapping = vma->vm_file->f_mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005436
Mel Gorman4eb2b1d2009-12-14 17:59:53 -08005437 /*
5438 * Take the mapping lock for the duration of the table walk. As
5439 * this mapping should be shared between all the VMAs,
5440 * __unmap_hugepage_range() is called as the lock is already held
5441 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08005442 i_mmap_lock_write(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07005443 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005444 /* Do not unmap the current VMA */
5445 if (iter_vma == vma)
5446 continue;
5447
5448 /*
Mel Gorman2f84a892015-10-01 15:36:57 -07005449 * Shared VMAs have their own reserves and do not affect
5450 * MAP_PRIVATE accounting but it is possible that a shared
5451 * VMA is using the same page so check and skip such VMAs.
5452 */
5453 if (iter_vma->vm_flags & VM_MAYSHARE)
5454 continue;
5455
5456 /*
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005457 * Unmap the page from other VMAs without their own reserves.
5458 * They get marked to be SIGKILLed if they fault in these
5459 * areas. This is because a future no-page fault on this VMA
5460 * could insert a zeroed page instead of the data existing
5461 * from the time of fork. This would look like data corruption
5462 */
5463 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07005464 unmap_hugepage_range(iter_vma, address,
Peter Xu05e90bd2022-05-12 20:22:55 -07005465 address + huge_page_size(h), page, 0);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005466 }
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08005467 i_mmap_unlock_write(mapping);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005468}
5469
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005470/*
David Hildenbrandc89357e2022-05-09 18:20:45 -07005471 * hugetlb_wp() should be called with page lock of the original hugepage held.
Baolin Wangaa6d2e82021-11-05 13:41:55 -07005472 * Called with hugetlb_fault_mutex_table held and pte_page locked so we
Michal Hockoef009b22012-01-10 15:07:21 -08005473 * cannot race with other handlers or page migration.
5474 * Keep the pte_same checks anyway to make transition from the mutex easier.
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005475 */
David Hildenbrandc89357e2022-05-09 18:20:45 -07005476static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
5477 unsigned long address, pte_t *ptep, unsigned int flags,
Sidhartha Kumar371607a2023-01-25 09:05:36 -08005478 struct folio *pagecache_folio, spinlock_t *ptl)
David Gibson1e8f8892006-01-06 00:10:44 -08005479{
David Hildenbrandc89357e2022-05-09 18:20:45 -07005480 const bool unshare = flags & FAULT_FLAG_UNSHARE;
Peter Xu60d5b472023-03-21 15:18:40 -04005481 pte_t pte = huge_ptep_get(ptep);
Andi Kleena5516432008-07-23 21:27:41 -07005482 struct hstate *h = hstate_vma(vma);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005483 struct page *old_page;
5484 struct folio *new_folio;
Souptick Joarder2b740302018-08-23 17:01:36 -07005485 int outside_reserve = 0;
5486 vm_fault_t ret = 0;
Huang Ying974e6d62018-08-17 15:45:57 -07005487 unsigned long haddr = address & huge_page_mask(h);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005488 struct mmu_notifier_range range;
David Gibson1e8f8892006-01-06 00:10:44 -08005489
David Hildenbrand1d8d14642022-08-11 12:34:35 +02005490 /*
Peter Xu60d5b472023-03-21 15:18:40 -04005491 * Never handle CoW for uffd-wp protected pages. It should be only
5492 * handled when the uffd-wp protection is removed.
5493 *
5494 * Note that only the CoW optimization path (in hugetlb_no_page())
5495 * can trigger this, because hugetlb_fault() will always resolve
5496 * uffd-wp bit first.
5497 */
5498 if (!unshare && huge_pte_uffd_wp(pte))
5499 return 0;
5500
5501 /*
David Hildenbrand1d8d14642022-08-11 12:34:35 +02005502 * hugetlb does not support FOLL_FORCE-style write faults that keep the
5503 * PTE mapped R/O such as maybe_mkwrite() would do.
5504 */
5505 if (WARN_ON_ONCE(!unshare && !(vma->vm_flags & VM_WRITE)))
5506 return VM_FAULT_SIGSEGV;
5507
5508 /* Let's take out MAP_SHARED mappings first. */
5509 if (vma->vm_flags & VM_MAYSHARE) {
David Hildenbrand1d8d14642022-08-11 12:34:35 +02005510 set_huge_ptep_writable(vma, haddr, ptep);
5511 return 0;
5512 }
5513
David Gibson1e8f8892006-01-06 00:10:44 -08005514 old_page = pte_page(pte);
5515
Yang Yang662ce1d2022-06-01 15:55:25 -07005516 delayacct_wpcopy_start();
5517
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005518retry_avoidcopy:
David Hildenbrandc89357e2022-05-09 18:20:45 -07005519 /*
5520 * If no-one else is actually using this page, we're the exclusive
5521 * owner and can reuse this page.
5522 */
Joonsoo Kim37a21402013-09-11 14:21:04 -07005523 if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
David Hildenbrandc89357e2022-05-09 18:20:45 -07005524 if (!PageAnonExclusive(old_page))
5525 page_move_anon_rmap(old_page, vma);
5526 if (likely(!unshare))
5527 set_huge_ptep_writable(vma, haddr, ptep);
Yang Yang662ce1d2022-06-01 15:55:25 -07005528
5529 delayacct_wpcopy_end();
Nick Piggin83c54072007-07-19 01:47:05 -07005530 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08005531 }
David Hildenbrand6c287602022-05-09 18:20:44 -07005532 VM_BUG_ON_PAGE(PageAnon(old_page) && PageAnonExclusive(old_page),
5533 old_page);
David Gibson1e8f8892006-01-06 00:10:44 -08005534
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005535 /*
5536 * If the process that created a MAP_PRIVATE mapping is about to
5537 * perform a COW due to a shared page count, attempt to satisfy
5538 * the allocation without using the existing reserves. The pagecache
5539 * page is used to determine if the reserve at this address was
5540 * consumed or not. If reserves were used, a partial faulted mapping
5541 * at the time of fork() could consume its reserves on COW instead
5542 * of the full address range.
5543 */
Joonsoo Kim5944d012013-09-11 14:21:55 -07005544 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
Sidhartha Kumar371607a2023-01-25 09:05:36 -08005545 page_folio(old_page) != pagecache_folio)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005546 outside_reserve = 1;
5547
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005548 get_page(old_page);
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08005549
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005550 /*
5551 * Drop page table lock as buddy allocator may be called. It will
5552 * be acquired again before returning to the caller, as expected.
5553 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005554 spin_unlock(ptl);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005555 new_folio = alloc_hugetlb_folio(vma, haddr, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08005556
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005557 if (IS_ERR(new_folio)) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005558 /*
5559 * If a process owning a MAP_PRIVATE mapping fails to COW,
5560 * it is due to references held by a child and an insufficient
5561 * huge page pool. To guarantee the original mappers
5562 * reliability, unmap the page from child processes. The child
5563 * may get SIGKILLed if it later faults.
5564 */
5565 if (outside_reserve) {
Mike Kravetz40549ba2022-09-14 15:18:09 -07005566 struct address_space *mapping = vma->vm_file->f_mapping;
5567 pgoff_t idx;
5568 u32 hash;
5569
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005570 put_page(old_page);
Mike Kravetz40549ba2022-09-14 15:18:09 -07005571 /*
5572 * Drop hugetlb_fault_mutex and vma_lock before
5573 * unmapping. unmapping needs to hold vma_lock
5574 * in write mode. Dropping vma_lock in read mode
5575 * here is OK as COW mappings do not interact with
5576 * PMD sharing.
5577 *
5578 * Reacquire both after unmap operation.
5579 */
5580 idx = vma_hugecache_offset(h, vma, haddr);
5581 hash = hugetlb_fault_mutex_hash(mapping, idx);
5582 hugetlb_vma_unlock_read(vma);
5583 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
5584
Huang Ying5b7a1d42018-08-17 15:45:53 -07005585 unmap_ref_private(mm, vma, old_page, haddr);
Mike Kravetz40549ba2022-09-14 15:18:09 -07005586
5587 mutex_lock(&hugetlb_fault_mutex_table[hash]);
5588 hugetlb_vma_lock_read(vma);
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07005589 spin_lock(ptl);
Peter Xu9c67a202022-12-16 10:52:29 -05005590 ptep = hugetlb_walk(vma, haddr, huge_page_size(h));
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07005591 if (likely(ptep &&
5592 pte_same(huge_ptep_get(ptep), pte)))
5593 goto retry_avoidcopy;
5594 /*
5595 * race occurs while re-acquiring page table
5596 * lock, and our job is done.
5597 */
Yang Yang662ce1d2022-06-01 15:55:25 -07005598 delayacct_wpcopy_end();
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07005599 return 0;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005600 }
5601
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005602 ret = vmf_error(PTR_ERR(new_folio));
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005603 goto out_release_old;
David Gibson1e8f8892006-01-06 00:10:44 -08005604 }
5605
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005606 /*
5607 * When the original hugepage is shared one, it does not have
5608 * anon_vma prepared.
5609 */
Dean Nelson44e2aa92010-10-26 14:22:08 -07005610 if (unlikely(anon_vma_prepare(vma))) {
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005611 ret = VM_FAULT_OOM;
5612 goto out_release_all;
Dean Nelson44e2aa92010-10-26 14:22:08 -07005613 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005614
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005615 copy_user_huge_page(&new_folio->page, old_page, address, vma,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005616 pages_per_huge_page(h));
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005617 __folio_mark_uptodate(new_folio);
David Gibson1e8f8892006-01-06 00:10:44 -08005618
Alistair Popple7d4a8be2023-01-10 13:57:22 +11005619 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, haddr,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07005620 haddr + huge_page_size(h));
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005621 mmu_notifier_invalidate_range_start(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005622
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08005623 /*
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005624 * Retake the page table lock to check for racing updates
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08005625 * before the page tables are altered
5626 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005627 spin_lock(ptl);
Peter Xu9c67a202022-12-16 10:52:29 -05005628 ptep = hugetlb_walk(vma, haddr, huge_page_size(h));
Naoya Horiguchia9af0c52014-04-07 15:36:54 -07005629 if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
David Hildenbrandc89357e2022-05-09 18:20:45 -07005630 /* Break COW or unshare */
Huang Ying5b7a1d42018-08-17 15:45:53 -07005631 huge_ptep_clear_flush(vma, haddr, ptep);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005632 mmu_notifier_invalidate_range(mm, range.start, range.end);
Hugh Dickinscea86fe2022-02-14 18:26:39 -08005633 page_remove_rmap(old_page, vma, true);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005634 hugepage_add_new_anon_rmap(new_folio, vma, haddr);
Pasha Tatashin1eba86c2022-01-14 14:06:29 -08005635 set_huge_pte_at(mm, haddr, ptep,
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005636 make_huge_pte(vma, &new_folio->page, !unshare));
5637 folio_set_hugetlb_migratable(new_folio);
David Gibson1e8f8892006-01-06 00:10:44 -08005638 /* Make the old page be freed below */
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005639 new_folio = page_folio(old_page);
David Gibson1e8f8892006-01-06 00:10:44 -08005640 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005641 spin_unlock(ptl);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08005642 mmu_notifier_invalidate_range_end(&range);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005643out_release_all:
David Hildenbrandc89357e2022-05-09 18:20:45 -07005644 /*
5645 * No restore in case of successful pagetable update (Break COW or
5646 * unshare)
5647 */
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005648 if (new_folio != page_folio(old_page))
Sidhartha Kumard2d7bb42023-01-25 09:05:34 -08005649 restore_reserve_on_error(h, vma, haddr, new_folio);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005650 folio_put(new_folio);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005651out_release_old:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005652 put_page(old_page);
Joonsoo Kim83120342013-09-11 14:21:57 -07005653
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005654 spin_lock(ptl); /* Caller expects lock to be held */
Yang Yang662ce1d2022-06-01 15:55:25 -07005655
5656 delayacct_wpcopy_end();
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07005657 return ret;
David Gibson1e8f8892006-01-06 00:10:44 -08005658}
5659
Hugh Dickins3ae77f42009-09-21 17:03:33 -07005660/*
5661 * Return whether there is a pagecache page to back given address within VMA.
5662 * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
5663 */
5664static bool hugetlbfs_pagecache_present(struct hstate *h,
Hugh Dickins2a15efc2009-09-21 17:03:27 -07005665 struct vm_area_struct *vma, unsigned long address)
5666{
Sidhartha Kumar91a2fb952023-01-25 09:05:31 -08005667 struct address_space *mapping = vma->vm_file->f_mapping;
5668 pgoff_t idx = vma_hugecache_offset(h, vma, address);
5669 bool present;
Hugh Dickins2a15efc2009-09-21 17:03:27 -07005670
Sidhartha Kumar91a2fb952023-01-25 09:05:31 -08005671 rcu_read_lock();
5672 present = page_cache_next_miss(mapping, idx, 1) != idx;
5673 rcu_read_unlock();
Hugh Dickins2a15efc2009-09-21 17:03:27 -07005674
Sidhartha Kumar91a2fb952023-01-25 09:05:31 -08005675 return present;
Hugh Dickins2a15efc2009-09-21 17:03:27 -07005676}
5677
Sidhartha Kumar9b91c0e2023-01-25 09:05:35 -08005678int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping,
Mike Kravetzab76ad52015-09-08 15:01:50 -07005679 pgoff_t idx)
5680{
5681 struct inode *inode = mapping->host;
5682 struct hstate *h = hstate_inode(inode);
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005683 int err;
Mike Kravetzab76ad52015-09-08 15:01:50 -07005684
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005685 __folio_set_locked(folio);
5686 err = __filemap_add_folio(mapping, folio, idx, GFP_KERNEL, NULL);
5687
5688 if (unlikely(err)) {
5689 __folio_clear_locked(folio);
Mike Kravetzab76ad52015-09-08 15:01:50 -07005690 return err;
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005691 }
Sidhartha Kumar9b91c0e2023-01-25 09:05:35 -08005692 folio_clear_hugetlb_restore_reserve(folio);
Mike Kravetzab76ad52015-09-08 15:01:50 -07005693
Mike Kravetz22146c32018-10-26 15:10:58 -07005694 /*
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005695 * mark folio dirty so that it will not be removed from cache/file
Mike Kravetz22146c32018-10-26 15:10:58 -07005696 * by non-hugetlbfs specific code paths.
5697 */
Matthew Wilcox (Oracle)d9ef44d2022-06-01 15:11:01 -04005698 folio_mark_dirty(folio);
Mike Kravetz22146c32018-10-26 15:10:58 -07005699
Mike Kravetzab76ad52015-09-08 15:01:50 -07005700 spin_lock(&inode->i_lock);
5701 inode->i_blocks += blocks_per_huge_page(h);
5702 spin_unlock(&inode->i_lock);
5703 return 0;
5704}
5705
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005706static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma,
5707 struct address_space *mapping,
5708 pgoff_t idx,
5709 unsigned int flags,
5710 unsigned long haddr,
Nadav Amit824ddc62022-03-22 14:45:32 -07005711 unsigned long addr,
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005712 unsigned long reason)
5713{
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005714 u32 hash;
5715 struct vm_fault vmf = {
5716 .vma = vma,
5717 .address = haddr,
Nadav Amit824ddc62022-03-22 14:45:32 -07005718 .real_address = addr,
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005719 .flags = flags,
5720
5721 /*
5722 * Hard to debug if it ends up being
5723 * used by a callee that assumes
5724 * something about the other
5725 * uninitialized fields... same as in
5726 * memory.c
5727 */
5728 };
5729
5730 /*
Liu Shixin958f32c2022-09-23 12:21:13 +08005731 * vma_lock and hugetlb_fault_mutex must be dropped before handling
5732 * userfault. Also mmap_lock could be dropped due to handling
5733 * userfault, any vma operation should be careful from here.
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005734 */
Mike Kravetz40549ba2022-09-14 15:18:09 -07005735 hugetlb_vma_unlock_read(vma);
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005736 hash = hugetlb_fault_mutex_hash(mapping, idx);
5737 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Liu Shixin958f32c2022-09-23 12:21:13 +08005738 return handle_userfault(&vmf, reason);
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005739}
5740
Peter Xu2ea7ff12022-10-04 15:33:58 -04005741/*
5742 * Recheck pte with pgtable lock. Returns true if pte didn't change, or
5743 * false if pte changed or is changing.
5744 */
5745static bool hugetlb_pte_stable(struct hstate *h, struct mm_struct *mm,
5746 pte_t *ptep, pte_t old_pte)
5747{
5748 spinlock_t *ptl;
5749 bool same;
5750
5751 ptl = huge_pte_lock(h, mm, ptep);
5752 same = pte_same(huge_ptep_get(ptep), old_pte);
5753 spin_unlock(ptl);
5754
5755 return same;
5756}
5757
Souptick Joarder2b740302018-08-23 17:01:36 -07005758static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
5759 struct vm_area_struct *vma,
5760 struct address_space *mapping, pgoff_t idx,
Peter Xuc64e9122022-05-12 20:22:54 -07005761 unsigned long address, pte_t *ptep,
5762 pte_t old_pte, unsigned int flags)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01005763{
Andi Kleena5516432008-07-23 21:27:41 -07005764 struct hstate *h = hstate_vma(vma);
Souptick Joarder2b740302018-08-23 17:01:36 -07005765 vm_fault_t ret = VM_FAULT_SIGBUS;
Hillf Danton409eb8c2012-01-20 14:34:13 -08005766 int anon_rmap = 0;
Adam Litke4c887262005-10-29 18:16:46 -07005767 unsigned long size;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005768 struct folio *folio;
David Gibson1e8f8892006-01-06 00:10:44 -08005769 pte_t new_pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005770 spinlock_t *ptl;
Huang Ying285b8dc2018-06-07 17:08:08 -07005771 unsigned long haddr = address & huge_page_mask(h);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005772 bool new_folio, new_pagecache_folio = false;
Liu Shixin958f32c2022-09-23 12:21:13 +08005773 u32 hash = hugetlb_fault_mutex_hash(mapping, idx);
Adam Litke4c887262005-10-29 18:16:46 -07005774
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005775 /*
5776 * Currently, we are forced to kill the process in the event the
5777 * original mapper has unmapped pages from the child due to a failed
David Hildenbrandc89357e2022-05-09 18:20:45 -07005778 * COW/unsharing. Warn that such a situation has occurred as it may not
5779 * be obvious.
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005780 */
5781 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
Geoffrey Thomas910154d2016-03-09 14:08:04 -08005782 pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
Andrew Mortonffb22af2013-02-22 16:32:08 -08005783 current->pid);
Liu Shixin958f32c2022-09-23 12:21:13 +08005784 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07005785 }
5786
Adam Litke4c887262005-10-29 18:16:46 -07005787 /*
Mike Kravetz188a3972022-09-14 15:18:02 -07005788 * Use page lock to guard against racing truncation
5789 * before we get page_table_lock.
Adam Litke4c887262005-10-29 18:16:46 -07005790 */
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005791 new_folio = false;
5792 folio = filemap_lock_folio(mapping, idx);
5793 if (!folio) {
Mike Kravetz188a3972022-09-14 15:18:02 -07005794 size = i_size_read(mapping->host) >> huge_page_shift(h);
5795 if (idx >= size)
5796 goto out;
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005797 /* Check for page in userfault range */
Peter Xu2ea7ff12022-10-04 15:33:58 -04005798 if (userfaultfd_missing(vma)) {
5799 /*
5800 * Since hugetlb_no_page() was examining pte
5801 * without pgtable lock, we need to re-test under
5802 * lock because the pte may not be stable and could
5803 * have changed from under us. Try to detect
5804 * either changed or during-changing ptes and retry
5805 * properly when needed.
5806 *
5807 * Note that userfaultfd is actually fine with
5808 * false positives (e.g. caused by pte changed),
5809 * but not wrong logical events (e.g. caused by
5810 * reading a pte during changing). The latter can
5811 * confuse the userspace, so the strictness is very
5812 * much preferred. E.g., MISSING event should
5813 * never happen on the page after UFFDIO_COPY has
5814 * correctly installed the page and returned.
5815 */
5816 if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) {
5817 ret = 0;
5818 goto out;
5819 }
5820
5821 return hugetlb_handle_userfault(vma, mapping, idx, flags,
5822 haddr, address,
5823 VM_UFFD_MISSING);
5824 }
Mike Kravetz1a1aad82017-02-22 15:43:01 -08005825
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005826 folio = alloc_hugetlb_folio(vma, haddr, 0);
5827 if (IS_ERR(folio)) {
Mike Kravetz4643d672019-08-13 15:38:00 -07005828 /*
5829 * Returning error will result in faulting task being
5830 * sent SIGBUS. The hugetlb fault mutex prevents two
5831 * tasks from racing to fault in the same page which
5832 * could result in false unable to allocate errors.
5833 * Page migration does not take the fault mutex, but
5834 * does a clear then write of pte's under page table
5835 * lock. Page fault code could race with migration,
5836 * notice the clear pte and try to allocate a page
5837 * here. Before returning error, get ptl and make
5838 * sure there really is no pte entry.
5839 */
Peter Xuf9bf6c02022-10-04 15:33:59 -04005840 if (hugetlb_pte_stable(h, mm, ptep, old_pte))
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005841 ret = vmf_error(PTR_ERR(folio));
Peter Xuf9bf6c02022-10-04 15:33:59 -04005842 else
5843 ret = 0;
Christoph Lameter6bda6662006-01-06 00:10:49 -08005844 goto out;
5845 }
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005846 clear_huge_page(&folio->page, address, pages_per_huge_page(h));
5847 __folio_mark_uptodate(folio);
5848 new_folio = true;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01005849
Mel Gormanf83a2752009-05-28 14:34:40 -07005850 if (vma->vm_flags & VM_MAYSHARE) {
Sidhartha Kumar9b91c0e2023-01-25 09:05:35 -08005851 int err = hugetlb_add_to_page_cache(folio, mapping, idx);
Christoph Lameter6bda6662006-01-06 00:10:49 -08005852 if (err) {
Miaohe Lin3a5497a2022-08-16 21:05:50 +08005853 /*
5854 * err can't be -EEXIST which implies someone
5855 * else consumed the reservation since hugetlb
5856 * fault mutex is held when add a hugetlb page
5857 * to the page cache. So it's safe to call
5858 * restore_reserve_on_error() here.
5859 */
Sidhartha Kumard2d7bb42023-01-25 09:05:34 -08005860 restore_reserve_on_error(h, vma, haddr, folio);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005861 folio_put(folio);
Christoph Lameter6bda6662006-01-06 00:10:49 -08005862 goto out;
5863 }
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005864 new_pagecache_folio = true;
Mel Gorman23be7462010-04-23 13:17:56 -04005865 } else {
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005866 folio_lock(folio);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005867 if (unlikely(anon_vma_prepare(vma))) {
5868 ret = VM_FAULT_OOM;
5869 goto backout_unlocked;
5870 }
Hillf Danton409eb8c2012-01-20 14:34:13 -08005871 anon_rmap = 1;
Mel Gorman23be7462010-04-23 13:17:56 -04005872 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09005873 } else {
Naoya Horiguchi998b4382010-09-08 10:19:32 +09005874 /*
5875 * If memory error occurs between mmap() and fault, some process
5876 * don't have hwpoisoned swap entry for errored virtual address.
5877 * So we need to block hugepage fault by PG_hwpoison bit check.
5878 */
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005879 if (unlikely(folio_test_hwpoison(folio))) {
Miaohe Lin0eb98f12021-01-12 15:49:24 -08005880 ret = VM_FAULT_HWPOISON_LARGE |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07005881 VM_FAULT_SET_HINDEX(hstate_index(h));
Naoya Horiguchi998b4382010-09-08 10:19:32 +09005882 goto backout_unlocked;
5883 }
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005884
5885 /* Check for page in userfault range. */
5886 if (userfaultfd_minor(vma)) {
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005887 folio_unlock(folio);
5888 folio_put(folio);
Peter Xu2ea7ff12022-10-04 15:33:58 -04005889 /* See comment in userfaultfd_missing() block above */
5890 if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) {
5891 ret = 0;
5892 goto out;
5893 }
5894 return hugetlb_handle_userfault(vma, mapping, idx, flags,
5895 haddr, address,
5896 VM_UFFD_MINOR);
Axel Rasmussen7677f7f2021-05-04 18:35:36 -07005897 }
Christoph Lameter6bda6662006-01-06 00:10:49 -08005898 }
David Gibson1e8f8892006-01-06 00:10:44 -08005899
Andy Whitcroft57303d82008-08-12 15:08:47 -07005900 /*
5901 * If we are going to COW a private mapping later, we examine the
5902 * pending reservations for this page now. This will ensure that
5903 * any allocations necessary to record that reservation occur outside
5904 * the spinlock.
5905 */
Mike Kravetz5e911372015-09-08 15:01:28 -07005906 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07005907 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07005908 ret = VM_FAULT_OOM;
5909 goto backout_unlocked;
5910 }
Mike Kravetz5e911372015-09-08 15:01:28 -07005911 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07005912 vma_end_reservation(h, vma, haddr);
Mike Kravetz5e911372015-09-08 15:01:28 -07005913 }
Andy Whitcroft57303d82008-08-12 15:08:47 -07005914
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08005915 ptl = huge_pte_lock(h, mm, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07005916 ret = 0;
Peter Xuc64e9122022-05-12 20:22:54 -07005917 /* If pte changed from under us, retry */
5918 if (!pte_same(huge_ptep_get(ptep), old_pte))
Adam Litke4c887262005-10-29 18:16:46 -07005919 goto backout;
5920
Peter Xu47815932022-10-20 15:38:32 -04005921 if (anon_rmap)
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005922 hugepage_add_new_anon_rmap(folio, vma, haddr);
Peter Xu47815932022-10-20 15:38:32 -04005923 else
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005924 page_dup_file_rmap(&folio->page, true);
5925 new_pte = make_huge_pte(vma, &folio->page, ((vma->vm_flags & VM_WRITE)
David Gibson1e8f8892006-01-06 00:10:44 -08005926 && (vma->vm_flags & VM_SHARED)));
Peter Xuc64e9122022-05-12 20:22:54 -07005927 /*
5928 * If this pte was previously wr-protected, keep it wr-protected even
5929 * if populated.
5930 */
5931 if (unlikely(pte_marker_uffd_wp(old_pte)))
Peter Xuf1eb1ba2022-12-14 15:15:33 -05005932 new_pte = huge_pte_mkuffd_wp(new_pte);
Huang Ying285b8dc2018-06-07 17:08:08 -07005933 set_huge_pte_at(mm, haddr, ptep, new_pte);
David Gibson1e8f8892006-01-06 00:10:44 -08005934
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08005935 hugetlb_count_add(pages_per_huge_page(h), mm);
Hugh Dickins788c7df2009-06-23 13:49:05 +01005936 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
David Gibson1e8f8892006-01-06 00:10:44 -08005937 /* Optimization, do the COW without a second fault */
Sidhartha Kumar371607a2023-01-25 09:05:36 -08005938 ret = hugetlb_wp(mm, vma, address, ptep, flags, folio, ptl);
David Gibson1e8f8892006-01-06 00:10:44 -08005939 }
5940
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005941 spin_unlock(ptl);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08005942
5943 /*
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005944 * Only set hugetlb_migratable in newly allocated pages. Existing pages
5945 * found in the pagecache may not have hugetlb_migratable if they have
Mike Kravetz8f251a32021-02-24 12:08:56 -08005946 * been isolated for migration.
Mike Kravetzcb6acd02019-02-28 16:22:02 -08005947 */
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005948 if (new_folio)
5949 folio_set_hugetlb_migratable(folio);
Mike Kravetzcb6acd02019-02-28 16:22:02 -08005950
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005951 folio_unlock(folio);
Adam Litke4c887262005-10-29 18:16:46 -07005952out:
Liu Shixin958f32c2022-09-23 12:21:13 +08005953 hugetlb_vma_unlock_read(vma);
5954 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01005955 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07005956
5957backout:
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005958 spin_unlock(ptl);
Andy Whitcroft2b267362008-08-12 15:08:49 -07005959backout_unlocked:
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005960 if (new_folio && !new_pagecache_folio)
Sidhartha Kumard2d7bb42023-01-25 09:05:34 -08005961 restore_reserve_on_error(h, vma, haddr, folio);
Mike Kravetzfa277592022-09-14 15:18:10 -07005962
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08005963 folio_unlock(folio);
5964 folio_put(folio);
Adam Litke4c887262005-10-29 18:16:46 -07005965 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01005966}
5967
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005968#ifdef CONFIG_SMP
Wei Yang188b04a2019-11-30 17:57:02 -08005969u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005970{
5971 unsigned long key[2];
5972 u32 hash;
5973
Mike Kravetz1b426ba2019-05-13 17:19:41 -07005974 key[0] = (unsigned long) mapping;
5975 key[1] = idx;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005976
Mike Kravetz55254632019-11-30 17:56:30 -08005977 hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005978
5979 return hash & (num_fault_mutexes - 1);
5980}
5981#else
5982/*
Miaohe Lin6c26d312021-02-24 12:07:19 -08005983 * For uniprocessor systems we always use a single mutex, so just
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005984 * return 0 and avoid the hashing overhead.
5985 */
Wei Yang188b04a2019-11-30 17:57:02 -08005986u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005987{
5988 return 0;
5989}
5990#endif
5991
Souptick Joarder2b740302018-08-23 17:01:36 -07005992vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
Hugh Dickins788c7df2009-06-23 13:49:05 +01005993 unsigned long address, unsigned int flags)
Adam Litke86e52162006-01-06 00:10:43 -08005994{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005995 pte_t *ptep, entry;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08005996 spinlock_t *ptl;
Souptick Joarder2b740302018-08-23 17:01:36 -07005997 vm_fault_t ret;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07005998 u32 hash;
5999 pgoff_t idx;
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09006000 struct page *page = NULL;
Sidhartha Kumar371607a2023-01-25 09:05:36 -08006001 struct folio *pagecache_folio = NULL;
Andi Kleena5516432008-07-23 21:27:41 -07006002 struct hstate *h = hstate_vma(vma);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07006003 struct address_space *mapping;
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006004 int need_wait_lock = 0;
Huang Ying285b8dc2018-06-07 17:08:08 -07006005 unsigned long haddr = address & huge_page_mask(h);
Adam Litke86e52162006-01-06 00:10:43 -08006006
David Gibson3935baa2006-03-22 00:08:53 -08006007 /*
6008 * Serialize hugepage allocation and instantiation, so that we don't
6009 * get spurious allocation failures if two CPUs race to instantiate
6010 * the same page in the page cache.
6011 */
Mike Kravetz40549ba2022-09-14 15:18:09 -07006012 mapping = vma->vm_file->f_mapping;
6013 idx = vma_hugecache_offset(h, vma, haddr);
Wei Yang188b04a2019-11-30 17:57:02 -08006014 hash = hugetlb_fault_mutex_hash(mapping, idx);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07006015 mutex_lock(&hugetlb_fault_mutex_table[hash]);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07006016
Mike Kravetz40549ba2022-09-14 15:18:09 -07006017 /*
6018 * Acquire vma lock before calling huge_pte_alloc and hold
6019 * until finished with ptep. This prevents huge_pmd_unshare from
6020 * being called elsewhere and making the ptep no longer valid.
Mike Kravetz40549ba2022-09-14 15:18:09 -07006021 */
6022 hugetlb_vma_lock_read(vma);
6023 ptep = huge_pte_alloc(mm, vma, haddr, huge_page_size(h));
6024 if (!ptep) {
6025 hugetlb_vma_unlock_read(vma);
6026 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6027 return VM_FAULT_OOM;
6028 }
6029
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07006030 entry = huge_ptep_get(ptep);
Peter Xuc64e9122022-05-12 20:22:54 -07006031 /* PTE markers should be handled the same way as none pte */
Liu Shixin958f32c2022-09-23 12:21:13 +08006032 if (huge_pte_none_mostly(entry))
6033 /*
6034 * hugetlb_no_page will drop vma lock and hugetlb fault
6035 * mutex internally, which make us return immediately.
6036 */
6037 return hugetlb_no_page(mm, vma, mapping, idx, address, ptep,
Peter Xuc64e9122022-05-12 20:22:54 -07006038 entry, flags);
Adam Litke86e52162006-01-06 00:10:43 -08006039
Nick Piggin83c54072007-07-19 01:47:05 -07006040 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08006041
Andy Whitcroft57303d82008-08-12 15:08:47 -07006042 /*
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006043 * entry could be a migration/hwpoison entry at this point, so this
6044 * check prevents the kernel from going below assuming that we have
Ethon Paul7c8de352020-06-04 16:49:07 -07006045 * an active hugepage in pagecache. This goto expects the 2nd page
6046 * fault, and is_hugetlb_entry_(migration|hwpoisoned) check will
6047 * properly handle it.
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006048 */
Peter Xufcd48542022-12-16 10:50:55 -05006049 if (!pte_present(entry)) {
6050 if (unlikely(is_hugetlb_entry_migration(entry))) {
6051 /*
6052 * Release the hugetlb fault lock now, but retain
6053 * the vma lock, because it is needed to guard the
6054 * huge_pte_lockptr() later in
6055 * migration_entry_wait_huge(). The vma lock will
6056 * be released there.
6057 */
6058 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6059 migration_entry_wait_huge(vma, ptep);
6060 return 0;
6061 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
6062 ret = VM_FAULT_HWPOISON_LARGE |
6063 VM_FAULT_SET_HINDEX(hstate_index(h));
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006064 goto out_mutex;
Peter Xufcd48542022-12-16 10:50:55 -05006065 }
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006066
6067 /*
David Hildenbrandc89357e2022-05-09 18:20:45 -07006068 * If we are going to COW/unshare the mapping later, we examine the
6069 * pending reservations for this page now. This will ensure that any
Andy Whitcroft57303d82008-08-12 15:08:47 -07006070 * allocations necessary to record that reservation occur outside the
David Hildenbrand1d8d14642022-08-11 12:34:35 +02006071 * spinlock. Also lookup the pagecache page now as it is used to
6072 * determine if a reservation has been consumed.
Andy Whitcroft57303d82008-08-12 15:08:47 -07006073 */
David Hildenbrandc89357e2022-05-09 18:20:45 -07006074 if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) &&
David Hildenbrand1d8d14642022-08-11 12:34:35 +02006075 !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(entry)) {
Huang Ying285b8dc2018-06-07 17:08:08 -07006076 if (vma_needs_reservation(h, vma, haddr) < 0) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07006077 ret = VM_FAULT_OOM;
David Gibsonb4d1d992008-10-15 22:01:11 -07006078 goto out_mutex;
Andy Whitcroft2b267362008-08-12 15:08:49 -07006079 }
Mike Kravetz5e911372015-09-08 15:01:28 -07006080 /* Just decrements count, does not deallocate */
Huang Ying285b8dc2018-06-07 17:08:08 -07006081 vma_end_reservation(h, vma, haddr);
Andy Whitcroft57303d82008-08-12 15:08:47 -07006082
Sidhartha Kumar371607a2023-01-25 09:05:36 -08006083 pagecache_folio = filemap_lock_folio(mapping, idx);
Andy Whitcroft57303d82008-08-12 15:08:47 -07006084 }
6085
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006086 ptl = huge_pte_lock(h, mm, ptep);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09006087
David Hildenbrandc89357e2022-05-09 18:20:45 -07006088 /* Check for a racing update before calling hugetlb_wp() */
David Gibsonb4d1d992008-10-15 22:01:11 -07006089 if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006090 goto out_ptl;
David Gibsonb4d1d992008-10-15 22:01:11 -07006091
Peter Xu166f3ec2022-05-12 20:22:54 -07006092 /* Handle userfault-wp first, before trying to lock more pages */
6093 if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(ptep)) &&
6094 (flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
6095 struct vm_fault vmf = {
6096 .vma = vma,
6097 .address = haddr,
6098 .real_address = address,
6099 .flags = flags,
6100 };
6101
6102 spin_unlock(ptl);
Sidhartha Kumar371607a2023-01-25 09:05:36 -08006103 if (pagecache_folio) {
6104 folio_unlock(pagecache_folio);
6105 folio_put(pagecache_folio);
Peter Xu166f3ec2022-05-12 20:22:54 -07006106 }
Mike Kravetz40549ba2022-09-14 15:18:09 -07006107 hugetlb_vma_unlock_read(vma);
Peter Xu166f3ec2022-05-12 20:22:54 -07006108 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Peter Xu166f3ec2022-05-12 20:22:54 -07006109 return handle_userfault(&vmf, VM_UFFD_WP);
6110 }
6111
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006112 /*
David Hildenbrandc89357e2022-05-09 18:20:45 -07006113 * hugetlb_wp() requires page locks of pte_page(entry) and
Sidhartha Kumar371607a2023-01-25 09:05:36 -08006114 * pagecache_folio, so here we need take the former one
6115 * when page != pagecache_folio or !pagecache_folio.
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006116 */
6117 page = pte_page(entry);
Sidhartha Kumar371607a2023-01-25 09:05:36 -08006118 if (page_folio(page) != pagecache_folio)
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006119 if (!trylock_page(page)) {
6120 need_wait_lock = 1;
6121 goto out_ptl;
6122 }
6123
6124 get_page(page);
David Gibsonb4d1d992008-10-15 22:01:11 -07006125
David Hildenbrandc89357e2022-05-09 18:20:45 -07006126 if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07006127 if (!huge_pte_write(entry)) {
David Hildenbrandc89357e2022-05-09 18:20:45 -07006128 ret = hugetlb_wp(mm, vma, address, ptep, flags,
Sidhartha Kumar371607a2023-01-25 09:05:36 -08006129 pagecache_folio, ptl);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006130 goto out_put_page;
David Hildenbrandc89357e2022-05-09 18:20:45 -07006131 } else if (likely(flags & FAULT_FLAG_WRITE)) {
6132 entry = huge_pte_mkdirty(entry);
David Gibsonb4d1d992008-10-15 22:01:11 -07006133 }
David Gibsonb4d1d992008-10-15 22:01:11 -07006134 }
6135 entry = pte_mkyoung(entry);
Huang Ying285b8dc2018-06-07 17:08:08 -07006136 if (huge_ptep_set_access_flags(vma, haddr, ptep, entry,
Hugh Dickins788c7df2009-06-23 13:49:05 +01006137 flags & FAULT_FLAG_WRITE))
Huang Ying285b8dc2018-06-07 17:08:08 -07006138 update_mmu_cache(vma, haddr, ptep);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006139out_put_page:
Sidhartha Kumar371607a2023-01-25 09:05:36 -08006140 if (page_folio(page) != pagecache_folio)
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006141 unlock_page(page);
6142 put_page(page);
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006143out_ptl:
6144 spin_unlock(ptl);
Andy Whitcroft57303d82008-08-12 15:08:47 -07006145
Sidhartha Kumar371607a2023-01-25 09:05:36 -08006146 if (pagecache_folio) {
6147 folio_unlock(pagecache_folio);
6148 folio_put(pagecache_folio);
Andy Whitcroft57303d82008-08-12 15:08:47 -07006149 }
David Gibsonb4d1d992008-10-15 22:01:11 -07006150out_mutex:
Mike Kravetz40549ba2022-09-14 15:18:09 -07006151 hugetlb_vma_unlock_read(vma);
Mike Kravetzc672c7f2015-09-08 15:01:35 -07006152 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08006153 /*
6154 * Generally it's safe to hold refcount during waiting page lock. But
6155 * here we just wait to defer the next page fault to avoid busy loop and
6156 * the page is not used after unlocked before returning from the current
6157 * page fault. So we are safe from accessing freed page, even if we wait
6158 * here without taking refcount.
6159 */
6160 if (need_wait_lock)
6161 wait_on_page_locked(page);
David Gibson1e8f8892006-01-06 00:10:44 -08006162 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08006163}
6164
Axel Rasmussen714c1892021-05-04 18:35:45 -07006165#ifdef CONFIG_USERFAULTFD
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006166/*
6167 * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with
6168 * modifications for huge pages.
6169 */
6170int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
6171 pte_t *dst_pte,
6172 struct vm_area_struct *dst_vma,
6173 unsigned long dst_addr,
6174 unsigned long src_addr,
Axel Rasmussenf6191472021-05-04 18:35:49 -07006175 enum mcopy_atomic_mode mode,
Peter Xu6041c692022-05-12 20:22:54 -07006176 struct page **pagep,
6177 bool wp_copy)
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006178{
Axel Rasmussenf6191472021-05-04 18:35:49 -07006179 bool is_continue = (mode == MCOPY_ATOMIC_CONTINUE);
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006180 struct hstate *h = hstate_vma(dst_vma);
6181 struct address_space *mapping = dst_vma->vm_file->f_mapping;
6182 pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr);
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006183 unsigned long size;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006184 int vm_shared = dst_vma->vm_flags & VM_SHARED;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006185 pte_t _dst_pte;
6186 spinlock_t *ptl;
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006187 int ret = -ENOMEM;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006188 struct folio *folio;
Axel Rasmussenf6191472021-05-04 18:35:49 -07006189 int writable;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006190 bool folio_in_pagecache = false;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006191
Axel Rasmussenf6191472021-05-04 18:35:49 -07006192 if (is_continue) {
6193 ret = -EFAULT;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006194 folio = filemap_lock_folio(mapping, idx);
6195 if (!folio)
Axel Rasmussenf6191472021-05-04 18:35:49 -07006196 goto out;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006197 folio_in_pagecache = true;
Axel Rasmussenf6191472021-05-04 18:35:49 -07006198 } else if (!*pagep) {
Mina Almasryd84cf062021-06-04 20:01:36 -07006199 /* If a page already exists, then it's UFFDIO_COPY for
6200 * a non-missing case. Return -EEXIST.
6201 */
6202 if (vm_shared &&
6203 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
6204 ret = -EEXIST;
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006205 goto out;
Mina Almasryd84cf062021-06-04 20:01:36 -07006206 }
6207
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006208 folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0);
6209 if (IS_ERR(folio)) {
Mina Almasryd84cf062021-06-04 20:01:36 -07006210 ret = -ENOMEM;
6211 goto out;
6212 }
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006213
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006214 ret = copy_huge_page_from_user(&folio->page,
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006215 (const void __user *) src_addr,
Mike Kravetz810a56b2017-02-22 15:42:58 -08006216 pages_per_huge_page(h), false);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006217
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07006218 /* fallback to copy_from_user outside mmap_lock */
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006219 if (unlikely(ret)) {
Andrea Arcangeli9e368252018-11-30 14:09:25 -08006220 ret = -ENOENT;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006221 /* Free the allocated folio which may have
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006222 * consumed a reservation.
6223 */
Sidhartha Kumard2d7bb42023-01-25 09:05:34 -08006224 restore_reserve_on_error(h, dst_vma, dst_addr, folio);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006225 folio_put(folio);
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006226
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006227 /* Allocate a temporary folio to hold the copied
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006228 * contents.
6229 */
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006230 folio = alloc_hugetlb_folio_vma(h, dst_vma, dst_addr);
6231 if (!folio) {
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006232 ret = -ENOMEM;
6233 goto out;
6234 }
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006235 *pagep = &folio->page;
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006236 /* Set the outparam pagep and return to the caller to
6237 * copy the contents outside the lock. Don't free the
6238 * page.
6239 */
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006240 goto out;
6241 }
6242 } else {
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006243 if (vm_shared &&
6244 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
6245 put_page(*pagep);
6246 ret = -EEXIST;
6247 *pagep = NULL;
6248 goto out;
6249 }
6250
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006251 folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0);
6252 if (IS_ERR(folio)) {
Miaohe Linda9a2982022-07-09 17:26:29 +08006253 put_page(*pagep);
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006254 ret = -ENOMEM;
6255 *pagep = NULL;
6256 goto out;
6257 }
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006258 copy_user_huge_page(&folio->page, *pagep, dst_addr, dst_vma,
Muchun Song34892362022-03-22 14:42:02 -07006259 pages_per_huge_page(h));
Mina Almasry8cc5fcb2021-06-30 18:48:19 -07006260 put_page(*pagep);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006261 *pagep = NULL;
6262 }
6263
6264 /*
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006265 * The memory barrier inside __folio_mark_uptodate makes sure that
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006266 * preceding stores to the page contents become visible before
6267 * the set_pte_at() write.
6268 */
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006269 __folio_mark_uptodate(folio);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006270
Axel Rasmussenf6191472021-05-04 18:35:49 -07006271 /* Add shared, newly allocated pages to the page cache. */
6272 if (vm_shared && !is_continue) {
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006273 size = i_size_read(mapping->host) >> huge_page_shift(h);
6274 ret = -EFAULT;
6275 if (idx >= size)
6276 goto out_release_nounlock;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006277
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006278 /*
6279 * Serialization between remove_inode_hugepages() and
Mike Kravetz7e1813d2022-09-14 15:18:04 -07006280 * hugetlb_add_to_page_cache() below happens through the
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006281 * hugetlb_fault_mutex_table that here must be hold by
6282 * the caller.
6283 */
Sidhartha Kumar9b91c0e2023-01-25 09:05:35 -08006284 ret = hugetlb_add_to_page_cache(folio, mapping, idx);
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006285 if (ret)
6286 goto out_release_nounlock;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006287 folio_in_pagecache = true;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -08006288 }
6289
Miaohe Linbcc66542022-09-01 20:00:25 +08006290 ptl = huge_pte_lock(h, dst_mm, dst_pte);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006291
James Houghton86251472022-10-18 20:01:25 +00006292 ret = -EIO;
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006293 if (folio_test_hwpoison(folio))
James Houghton86251472022-10-18 20:01:25 +00006294 goto out_release_unlock;
6295
Andrea Arcangeli1e3921472017-11-02 15:59:29 -07006296 /*
Peter Xu6041c692022-05-12 20:22:54 -07006297 * We allow to overwrite a pte marker: consider when both MISSING|WP
6298 * registered, we firstly wr-protect a none pte which has no page cache
6299 * page backing it, then access the page.
6300 */
Mike Kravetzfa277592022-09-14 15:18:10 -07006301 ret = -EEXIST;
Peter Xu6041c692022-05-12 20:22:54 -07006302 if (!huge_pte_none_mostly(huge_ptep_get(dst_pte)))
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006303 goto out_release_unlock;
6304
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006305 if (folio_in_pagecache)
6306 page_dup_file_rmap(&folio->page, true);
Peter Xu47815932022-10-20 15:38:32 -04006307 else
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006308 hugepage_add_new_anon_rmap(folio, dst_vma, dst_addr);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006309
Peter Xu6041c692022-05-12 20:22:54 -07006310 /*
6311 * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY
6312 * with wp flag set, don't set pte write bit.
6313 */
6314 if (wp_copy || (is_continue && !vm_shared))
Axel Rasmussenf6191472021-05-04 18:35:49 -07006315 writable = 0;
6316 else
6317 writable = dst_vma->vm_flags & VM_WRITE;
6318
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006319 _dst_pte = make_huge_pte(dst_vma, &folio->page, writable);
Peter Xu6041c692022-05-12 20:22:54 -07006320 /*
6321 * Always mark UFFDIO_COPY page dirty; note that this may not be
6322 * extremely important for hugetlbfs for now since swapping is not
6323 * supported, but we should still be clear in that this page cannot be
6324 * thrown away at will, even if write bit not set.
6325 */
6326 _dst_pte = huge_pte_mkdirty(_dst_pte);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006327 _dst_pte = pte_mkyoung(_dst_pte);
6328
Peter Xu6041c692022-05-12 20:22:54 -07006329 if (wp_copy)
6330 _dst_pte = huge_pte_mkuffd_wp(_dst_pte);
6331
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006332 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
6333
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006334 hugetlb_count_add(pages_per_huge_page(h), dst_mm);
6335
6336 /* No need to invalidate - it was non-present before */
6337 update_mmu_cache(dst_vma, dst_addr, dst_pte);
6338
6339 spin_unlock(ptl);
Axel Rasmussenf6191472021-05-04 18:35:49 -07006340 if (!is_continue)
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006341 folio_set_hugetlb_migratable(folio);
Axel Rasmussenf6191472021-05-04 18:35:49 -07006342 if (vm_shared || is_continue)
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006343 folio_unlock(folio);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006344 ret = 0;
6345out:
6346 return ret;
6347out_release_unlock:
6348 spin_unlock(ptl);
Axel Rasmussenf6191472021-05-04 18:35:49 -07006349 if (vm_shared || is_continue)
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006350 folio_unlock(folio);
Andrea Arcangeli5af10df2017-08-10 15:23:38 -07006351out_release_nounlock:
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006352 if (!folio_in_pagecache)
Sidhartha Kumard2d7bb42023-01-25 09:05:34 -08006353 restore_reserve_on_error(h, dst_vma, dst_addr, folio);
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006354 folio_put(folio);
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006355 goto out;
6356}
Axel Rasmussen714c1892021-05-04 18:35:45 -07006357#endif /* CONFIG_USERFAULTFD */
Mike Kravetz8fb5deb2017-02-22 15:42:52 -08006358
Joao Martins82e5d372021-02-24 12:07:16 -08006359static void record_subpages_vmas(struct page *page, struct vm_area_struct *vma,
6360 int refs, struct page **pages,
6361 struct vm_area_struct **vmas)
6362{
6363 int nr;
6364
6365 for (nr = 0; nr < refs; nr++) {
6366 if (likely(pages))
Cheng Li14455ea2022-09-09 07:31:09 +00006367 pages[nr] = nth_page(page, nr);
Joao Martins82e5d372021-02-24 12:07:16 -08006368 if (vmas)
6369 vmas[nr] = vma;
6370 }
6371}
6372
David Hildenbrand84209e82022-11-16 11:26:48 +01006373static inline bool __follow_hugetlb_must_fault(struct vm_area_struct *vma,
6374 unsigned int flags, pte_t *pte,
David Hildenbranda7f22662022-05-09 18:20:45 -07006375 bool *unshare)
6376{
6377 pte_t pteval = huge_ptep_get(pte);
6378
6379 *unshare = false;
6380 if (is_swap_pte(pteval))
6381 return true;
6382 if (huge_pte_write(pteval))
6383 return false;
6384 if (flags & FOLL_WRITE)
6385 return true;
David Hildenbrand84209e82022-11-16 11:26:48 +01006386 if (gup_must_unshare(vma, flags, pte_page(pteval))) {
David Hildenbranda7f22662022-05-09 18:20:45 -07006387 *unshare = true;
6388 return true;
6389 }
6390 return false;
6391}
6392
Mike Kravetz57a196a2022-09-18 19:13:48 -07006393struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma,
6394 unsigned long address, unsigned int flags)
6395{
6396 struct hstate *h = hstate_vma(vma);
6397 struct mm_struct *mm = vma->vm_mm;
6398 unsigned long haddr = address & huge_page_mask(h);
6399 struct page *page = NULL;
6400 spinlock_t *ptl;
6401 pte_t *pte, entry;
6402
6403 /*
6404 * FOLL_PIN is not supported for follow_page(). Ordinary GUP goes via
6405 * follow_hugetlb_page().
6406 */
6407 if (WARN_ON_ONCE(flags & FOLL_PIN))
6408 return NULL;
6409
Peter Xu7d049f32022-12-16 10:52:19 -05006410 hugetlb_vma_lock_read(vma);
Peter Xu9c67a202022-12-16 10:52:29 -05006411 pte = hugetlb_walk(vma, haddr, huge_page_size(h));
Mike Kravetz57a196a2022-09-18 19:13:48 -07006412 if (!pte)
Peter Xu7d049f32022-12-16 10:52:19 -05006413 goto out_unlock;
Mike Kravetz57a196a2022-09-18 19:13:48 -07006414
6415 ptl = huge_pte_lock(h, mm, pte);
6416 entry = huge_ptep_get(pte);
6417 if (pte_present(entry)) {
6418 page = pte_page(entry) +
6419 ((address & ~huge_page_mask(h)) >> PAGE_SHIFT);
6420 /*
6421 * Note that page may be a sub-page, and with vmemmap
6422 * optimizations the page struct may be read only.
6423 * try_grab_page() will increase the ref count on the
6424 * head page, so this will be OK.
6425 *
Linus Torvaldse2ca6ba2022-12-13 19:29:45 -08006426 * try_grab_page() should always be able to get the page here,
6427 * because we hold the ptl lock and have verified pte_present().
Mike Kravetz57a196a2022-09-18 19:13:48 -07006428 */
Linus Torvaldse2ca6ba2022-12-13 19:29:45 -08006429 if (try_grab_page(page, flags)) {
Mike Kravetz57a196a2022-09-18 19:13:48 -07006430 page = NULL;
6431 goto out;
6432 }
Mike Kravetz57a196a2022-09-18 19:13:48 -07006433 }
6434out:
6435 spin_unlock(ptl);
Peter Xu7d049f32022-12-16 10:52:19 -05006436out_unlock:
6437 hugetlb_vma_unlock_read(vma);
Mike Kravetz57a196a2022-09-18 19:13:48 -07006438 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
Peter Xueefc7fa2022-12-16 10:52:23 -05006468 hugetlb_vma_lock_read(vma);
David Rientjes02057962015-04-14 15:48:24 -07006469 /*
Adam Litke4c887262005-10-29 18:16:46 -07006470 * Some archs (sparc64, sh*) have multiple pte_ts to
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006471 * each hugepage. We have to make sure we get the
Adam Litke4c887262005-10-29 18:16:46 -07006472 * first, for the page indexing below to work.
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006473 *
6474 * Note that page table lock is not held when pte is null.
Adam Litke4c887262005-10-29 18:16:46 -07006475 */
Peter Xu9c67a202022-12-16 10:52:29 -05006476 pte = hugetlb_walk(vma, vaddr & huge_page_mask(h),
6477 huge_page_size(h));
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006478 if (pte)
6479 ptl = huge_pte_lock(h, mm, pte);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006480 absent = !pte || huge_pte_none(huge_ptep_get(pte));
Adam Litke4c887262005-10-29 18:16:46 -07006481
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006482 /*
6483 * When coredumping, it suits get_dump_page if we just return
Hugh Dickins3ae77f42009-09-21 17:03:33 -07006484 * an error where there's an empty slot with no huge pagecache
6485 * to back it. This way, we avoid allocating a hugepage, and
6486 * the sparse dumpfile avoids allocating disk blocks, but its
6487 * huge holes still show up with zeroes where they need to be.
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006488 */
Hugh Dickins3ae77f42009-09-21 17:03:33 -07006489 if (absent && (flags & FOLL_DUMP) &&
6490 !hugetlbfs_pagecache_present(h, vma, vaddr)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006491 if (pte)
6492 spin_unlock(ptl);
Peter Xueefc7fa2022-12-16 10:52:23 -05006493 hugetlb_vma_unlock_read(vma);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07006494 remainder = 0;
6495 break;
6496 }
6497
Naoya Horiguchi9cc3a5b2013-04-17 15:58:30 -07006498 /*
6499 * We need call hugetlb_fault for both hugepages under migration
6500 * (in which case hugetlb_fault waits for the migration,) and
6501 * hwpoisoned hugepages (in which case we need to prevent the
6502 * caller from accessing to them.) In order to do this, we use
6503 * here is_swap_pte instead of is_hugetlb_entry_migration and
6504 * is_hugetlb_entry_hwpoisoned. This is because it simply covers
6505 * both cases, and because we can't follow correct pages
6506 * directly from any kind of swap entries.
6507 */
David Hildenbranda7f22662022-05-09 18:20:45 -07006508 if (absent ||
David Hildenbrand84209e82022-11-16 11:26:48 +01006509 __follow_hugetlb_must_fault(vma, flags, pte, &unshare)) {
Souptick Joarder2b740302018-08-23 17:01:36 -07006510 vm_fault_t ret;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006511 unsigned int fault_flags = 0;
Adam Litke4c887262005-10-29 18:16:46 -07006512
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006513 if (pte)
6514 spin_unlock(ptl);
Peter Xueefc7fa2022-12-16 10:52:23 -05006515 hugetlb_vma_unlock_read(vma);
6516
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006517 if (flags & FOLL_WRITE)
6518 fault_flags |= FAULT_FLAG_WRITE;
David Hildenbranda7f22662022-05-09 18:20:45 -07006519 else if (unshare)
6520 fault_flags |= FAULT_FLAG_UNSHARE;
Peter Xu93c5c612022-10-11 15:58:06 -04006521 if (locked) {
Peter Xu71335f32020-04-01 21:08:53 -07006522 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
6523 FAULT_FLAG_KILLABLE;
Peter Xu93c5c612022-10-11 15:58:06 -04006524 if (flags & FOLL_INTERRUPTIBLE)
6525 fault_flags |= FAULT_FLAG_INTERRUPTIBLE;
6526 }
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006527 if (flags & FOLL_NOWAIT)
6528 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
6529 FAULT_FLAG_RETRY_NOWAIT;
6530 if (flags & FOLL_TRIED) {
Peter Xu4426e942020-04-01 21:08:49 -07006531 /*
6532 * Note: FAULT_FLAG_ALLOW_RETRY and
6533 * FAULT_FLAG_TRIED can co-exist
6534 */
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006535 fault_flags |= FAULT_FLAG_TRIED;
6536 }
6537 ret = hugetlb_fault(mm, vma, vaddr, fault_flags);
6538 if (ret & VM_FAULT_ERROR) {
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07006539 err = vm_fault_to_errno(ret, flags);
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006540 remainder = 0;
6541 break;
6542 }
6543 if (ret & VM_FAULT_RETRY) {
Peter Xu4f6da932020-04-01 21:07:58 -07006544 if (locked &&
Andrea Arcangeli1ac25012019-02-01 14:20:16 -08006545 !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Peter Xu4f6da932020-04-01 21:07:58 -07006546 *locked = 0;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006547 *nr_pages = 0;
6548 /*
6549 * VM_FAULT_RETRY must not return an
6550 * error, it will return zero
6551 * instead.
6552 *
6553 * No need to update "position" as the
6554 * caller will not check it after
6555 * *nr_pages is set to 0.
6556 */
6557 return i;
6558 }
6559 continue;
Adam Litke4c887262005-10-29 18:16:46 -07006560 }
David Gibson63551ae2005-06-21 17:14:44 -07006561
Andi Kleena5516432008-07-23 21:27:41 -07006562 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07006563 page = pte_page(huge_ptep_get(pte));
Linus Torvalds8fde12c2019-04-11 10:49:19 -07006564
David Hildenbrandb6a26192022-05-09 18:20:45 -07006565 VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
6566 !PageAnonExclusive(page), page);
6567
Linus Torvalds8fde12c2019-04-11 10:49:19 -07006568 /*
Zhigang Luacbfb082019-11-30 17:57:06 -08006569 * If subpage information not requested, update counters
6570 * and skip the same_page loop below.
6571 */
6572 if (!pages && !vmas && !pfn_offset &&
6573 (vaddr + huge_page_size(h) < vma->vm_end) &&
6574 (remainder >= pages_per_huge_page(h))) {
6575 vaddr += huge_page_size(h);
6576 remainder -= pages_per_huge_page(h);
6577 i += pages_per_huge_page(h);
6578 spin_unlock(ptl);
Peter Xueefc7fa2022-12-16 10:52:23 -05006579 hugetlb_vma_unlock_read(vma);
Zhigang Luacbfb082019-11-30 17:57:06 -08006580 continue;
6581 }
6582
Joao Martinsd08af0a2021-07-14 21:27:11 -07006583 /* vaddr may not be aligned to PAGE_SIZE */
6584 refs = min3(pages_per_huge_page(h) - pfn_offset, remainder,
6585 (vma->vm_end - ALIGN_DOWN(vaddr, PAGE_SIZE)) >> PAGE_SHIFT);
Joao Martins0fa5bc42021-02-24 12:07:12 -08006586
Joao Martins82e5d372021-02-24 12:07:16 -08006587 if (pages || vmas)
Cheng Li14455ea2022-09-09 07:31:09 +00006588 record_subpages_vmas(nth_page(page, pfn_offset),
Joao Martins82e5d372021-02-24 12:07:16 -08006589 vma, refs,
6590 likely(pages) ? pages + i : NULL,
6591 vmas ? vmas + i : NULL);
David Gibson63551ae2005-06-21 17:14:44 -07006592
Joao Martins82e5d372021-02-24 12:07:16 -08006593 if (pages) {
Joao Martins0fa5bc42021-02-24 12:07:12 -08006594 /*
Matthew Wilcox (Oracle)822951d2022-01-08 00:15:04 -05006595 * try_grab_folio() should always succeed here,
Joao Martins0fa5bc42021-02-24 12:07:12 -08006596 * because: a) we hold the ptl lock, and b) we've just
6597 * checked that the huge page is present in the page
6598 * tables. If the huge page is present, then the tail
6599 * pages must also be present. The ptl prevents the
6600 * head page and tail pages from being rearranged in
Logan Gunthorpe4003f102022-10-21 11:41:09 -06006601 * any way. As this is hugetlb, the pages will never
6602 * be p2pdma or not longterm pinable. So this page
6603 * must be available at this point, unless the page
6604 * refcount overflowed:
Joao Martins0fa5bc42021-02-24 12:07:12 -08006605 */
Matthew Wilcox (Oracle)822951d2022-01-08 00:15:04 -05006606 if (WARN_ON_ONCE(!try_grab_folio(pages[i], refs,
6607 flags))) {
Joao Martins0fa5bc42021-02-24 12:07:12 -08006608 spin_unlock(ptl);
Peter Xueefc7fa2022-12-16 10:52:23 -05006609 hugetlb_vma_unlock_read(vma);
Joao Martins0fa5bc42021-02-24 12:07:12 -08006610 remainder = 0;
6611 err = -ENOMEM;
6612 break;
6613 }
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08006614 }
Joao Martins82e5d372021-02-24 12:07:16 -08006615
6616 vaddr += (refs << PAGE_SHIFT);
6617 remainder -= refs;
6618 i += refs;
6619
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006620 spin_unlock(ptl);
Peter Xueefc7fa2022-12-16 10:52:23 -05006621 hugetlb_vma_unlock_read(vma);
David Gibson63551ae2005-06-21 17:14:44 -07006622 }
Michel Lespinasse28a35712013-02-22 16:35:55 -08006623 *nr_pages = remainder;
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08006624 /*
6625 * setting position is actually required only if remainder is
6626 * not zero but it's faster not to add a "if (remainder)"
6627 * branch.
6628 */
David Gibson63551ae2005-06-21 17:14:44 -07006629 *position = vaddr;
6630
Daniel Jordan2be7cfe2017-08-02 13:31:47 -07006631 return i ? i : err;
David Gibson63551ae2005-06-21 17:14:44 -07006632}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006633
Peter Xua79390f2023-01-04 17:52:06 -05006634long hugetlb_change_protection(struct vm_area_struct *vma,
Peter Xu5a90d5a2022-05-12 20:22:54 -07006635 unsigned long address, unsigned long end,
6636 pgprot_t newprot, unsigned long cp_flags)
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006637{
6638 struct mm_struct *mm = vma->vm_mm;
6639 unsigned long start = address;
6640 pte_t *ptep;
6641 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07006642 struct hstate *h = hstate_vma(vma);
Peter Xua79390f2023-01-04 17:52:06 -05006643 long pages = 0, psize = huge_page_size(h);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006644 bool shared_pmd = false;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006645 struct mmu_notifier_range range;
Mike Kravetze95a9852022-06-21 16:56:17 -07006646 unsigned long last_addr_mask;
Peter Xu5a90d5a2022-05-12 20:22:54 -07006647 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
6648 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006649
6650 /*
6651 * In the case of shared PMDs, the area to flush could be beyond
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006652 * start/end. Set range.start/range.end to cover the maximum possible
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006653 * range if PMD sharing is possible.
6654 */
Jérôme Glisse7269f992019-05-13 17:20:53 -07006655 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
Alistair Popple7d4a8be2023-01-10 13:57:22 +11006656 0, mm, start, end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006657 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006658
6659 BUG_ON(address >= end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006660 flush_cache_range(vma, range.start, range.end);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006661
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006662 mmu_notifier_invalidate_range_start(&range);
Mike Kravetz40549ba2022-09-14 15:18:09 -07006663 hugetlb_vma_lock_write(vma);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08006664 i_mmap_lock_write(vma->vm_file->f_mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07006665 last_addr_mask = hugetlb_mask_last_page(h);
Peter Xu60dfaad2022-05-12 20:22:55 -07006666 for (; address < end; address += psize) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006667 spinlock_t *ptl;
Peter Xu9c67a202022-12-16 10:52:29 -05006668 ptep = hugetlb_walk(vma, address, psize);
Mike Kravetze95a9852022-06-21 16:56:17 -07006669 if (!ptep) {
Peter Xufed15f12023-01-04 17:52:05 -05006670 if (!uffd_wp) {
6671 address |= last_addr_mask;
6672 continue;
6673 }
6674 /*
6675 * Userfaultfd wr-protect requires pgtable
6676 * pre-allocations to install pte markers.
6677 */
6678 ptep = huge_pte_alloc(mm, vma, address, psize);
Peter Xud1751112023-01-04 17:52:07 -05006679 if (!ptep) {
6680 pages = -ENOMEM;
Peter Xufed15f12023-01-04 17:52:05 -05006681 break;
Peter Xud1751112023-01-04 17:52:07 -05006682 }
Mike Kravetze95a9852022-06-21 16:56:17 -07006683 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006684 ptl = huge_pte_lock(h, mm, ptep);
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07006685 if (huge_pmd_unshare(mm, vma, address, ptep)) {
Peter Xu60dfaad2022-05-12 20:22:55 -07006686 /*
6687 * When uffd-wp is enabled on the vma, unshare
6688 * shouldn't happen at all. Warn about it if it
6689 * happened due to some reason.
6690 */
6691 WARN_ON_ONCE(uffd_wp || uffd_wp_resolve);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01006692 pages++;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006693 spin_unlock(ptl);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006694 shared_pmd = true;
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07006695 address |= last_addr_mask;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08006696 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +01006697 }
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006698 pte = huge_ptep_get(ptep);
6699 if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
David Hildenbrand0e678152022-12-22 21:55:10 +01006700 /* Nothing to do. */
6701 } else if (unlikely(is_hugetlb_entry_migration(pte))) {
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006702 swp_entry_t entry = pte_to_swp_entry(pte);
David Hildenbrand6c287602022-05-09 18:20:44 -07006703 struct page *page = pfn_swap_entry_to_page(entry);
David Hildenbrand44f86392022-12-22 21:55:11 +01006704 pte_t newpte = pte;
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006705
David Hildenbrand44f86392022-12-22 21:55:11 +01006706 if (is_writable_migration_entry(entry)) {
David Hildenbrand6c287602022-05-09 18:20:44 -07006707 if (PageAnon(page))
6708 entry = make_readable_exclusive_migration_entry(
6709 swp_offset(entry));
6710 else
6711 entry = make_readable_migration_entry(
6712 swp_offset(entry));
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006713 newpte = swp_entry_to_pte(entry);
Naoya Horiguchia8bda282015-02-11 15:25:28 -08006714 pages++;
6715 }
David Hildenbrand44f86392022-12-22 21:55:11 +01006716
6717 if (uffd_wp)
6718 newpte = pte_swp_mkuffd_wp(newpte);
6719 else if (uffd_wp_resolve)
6720 newpte = pte_swp_clear_uffd_wp(newpte);
6721 if (!pte_same(pte, newpte))
6722 set_huge_pte_at(mm, address, ptep, newpte);
David Hildenbrand0e678152022-12-22 21:55:10 +01006723 } else if (unlikely(is_pte_marker(pte))) {
6724 /* No other markers apply for now. */
6725 WARN_ON_ONCE(!pte_marker_uffd_wp(pte));
Peter Xu60dfaad2022-05-12 20:22:55 -07006726 if (uffd_wp_resolve)
David Hildenbrand0e678152022-12-22 21:55:10 +01006727 /* Safe to modify directly (non-present->none). */
Peter Xu60dfaad2022-05-12 20:22:55 -07006728 huge_pte_clear(mm, address, ptep, psize);
David Hildenbrand0e678152022-12-22 21:55:10 +01006729 } else if (!huge_pte_none(pte)) {
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08006730 pte_t old_pte;
Christophe Leroy79c1c592021-06-30 18:48:00 -07006731 unsigned int shift = huge_page_shift(hstate_vma(vma));
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08006732
6733 old_pte = huge_ptep_modify_prot_start(vma, address, ptep);
Anshuman Khandual16785bd2022-03-22 14:41:47 -07006734 pte = huge_pte_modify(old_pte, newprot);
Christophe Leroy79c1c592021-06-30 18:48:00 -07006735 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
Peter Xu5a90d5a2022-05-12 20:22:54 -07006736 if (uffd_wp)
Peter Xuf1eb1ba2022-12-14 15:15:33 -05006737 pte = huge_pte_mkuffd_wp(pte);
Peter Xu5a90d5a2022-05-12 20:22:54 -07006738 else if (uffd_wp_resolve)
6739 pte = huge_pte_clear_uffd_wp(pte);
Aneesh Kumar K.V023bdd02019-03-05 15:46:37 -08006740 huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01006741 pages++;
Peter Xu60dfaad2022-05-12 20:22:55 -07006742 } else {
6743 /* None pte */
6744 if (unlikely(uffd_wp))
6745 /* Safe to modify directly (none->non-present). */
6746 set_huge_pte_at(mm, address, ptep,
6747 make_pte_marker(PTE_MARKER_UFFD_WP));
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006748 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08006749 spin_unlock(ptl);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006750 }
Mel Gormand8333522012-07-31 16:46:20 -07006751 /*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08006752 * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
Mel Gormand8333522012-07-31 16:46:20 -07006753 * may have cleared our pud entry and done put_page on the page table:
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08006754 * once we release i_mmap_rwsem, another task can do the final put_page
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006755 * and that page table be reused and filled with junk. If we actually
6756 * did unshare a page of pmds, flush the range corresponding to the pud.
Mel Gormand8333522012-07-31 16:46:20 -07006757 */
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006758 if (shared_pmd)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006759 flush_hugetlb_tlb_range(vma, range.start, range.end);
Mike Kravetzdff11ab2018-10-05 15:51:33 -07006760 else
6761 flush_hugetlb_tlb_range(vma, start, end);
Jérôme Glisse0f108512017-11-15 17:34:07 -08006762 /*
6763 * No need to call mmu_notifier_invalidate_range() we are downgrading
6764 * page table protection not changing it to point to a new page.
6765 *
Mike Rapoportee657282022-06-27 09:00:26 +03006766 * See Documentation/mm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08006767 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08006768 i_mmap_unlock_write(vma->vm_file->f_mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07006769 hugetlb_vma_unlock_write(vma);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08006770 mmu_notifier_invalidate_range_end(&range);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01006771
Peter Xud1751112023-01-04 17:52:07 -05006772 return pages > 0 ? (pages << h->order) : pages;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08006773}
6774
Mike Kravetz33b8f842021-02-24 12:09:54 -08006775/* Return true if reservation was successful, false otherwise. */
6776bool hugetlb_reserve_pages(struct inode *inode,
Mel Gormana1e78772008-07-23 21:27:23 -07006777 long from, long to,
Mel Gorman5a6fe122009-02-10 14:02:27 +00006778 struct vm_area_struct *vma,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09006779 vm_flags_t vm_flags)
Adam Litkee4e574b2007-10-16 01:26:19 -07006780{
Mike Kravetzc5094ec2022-12-16 14:45:07 -08006781 long chg = -1, add = -1;
Andi Kleena5516432008-07-23 21:27:41 -07006782 struct hstate *h = hstate_inode(inode);
David Gibson90481622012-03-21 16:34:12 -07006783 struct hugepage_subpool *spool = subpool_inode(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07006784 struct resv_map *resv_map;
Mina Almasry075a61d2020-04-01 21:11:28 -07006785 struct hugetlb_cgroup *h_cg = NULL;
Mina Almasry0db9d742020-04-01 21:11:25 -07006786 long gbl_reserve, regions_needed = 0;
Adam Litkee4e574b2007-10-16 01:26:19 -07006787
Mike Kravetz63489f82018-03-22 16:17:13 -07006788 /* This should never happen */
6789 if (from > to) {
6790 VM_WARN(1, "%s called with a negative range\n", __func__);
Mike Kravetz33b8f842021-02-24 12:09:54 -08006791 return false;
Mike Kravetz63489f82018-03-22 16:17:13 -07006792 }
6793
Mel Gormana1e78772008-07-23 21:27:23 -07006794 /*
Mike Kravetze7008982022-12-12 15:50:41 -08006795 * vma specific semaphore used for pmd sharing and fault/truncation
6796 * synchronization
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07006797 */
6798 hugetlb_vma_lock_alloc(vma);
6799
6800 /*
Mel Gorman17c9d122009-02-11 16:34:16 +00006801 * Only apply hugepage reservation if asked. At fault time, an
6802 * attempt will be made for VM_NORESERVE to allocate a page
David Gibson90481622012-03-21 16:34:12 -07006803 * without using reserves
Mel Gorman17c9d122009-02-11 16:34:16 +00006804 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09006805 if (vm_flags & VM_NORESERVE)
Mike Kravetz33b8f842021-02-24 12:09:54 -08006806 return true;
Mel Gorman17c9d122009-02-11 16:34:16 +00006807
6808 /*
Mel Gormana1e78772008-07-23 21:27:23 -07006809 * Shared mappings base their reservation on the number of pages that
6810 * are already allocated on behalf of the file. Private mappings need
6811 * to reserve the full area even if read-only as mprotect() may be
6812 * called to make the mapping read-write. Assume !vma is a shm mapping
6813 */
Joonsoo Kim9119a412014-04-03 14:47:25 -07006814 if (!vma || vma->vm_flags & VM_MAYSHARE) {
Mike Kravetzf27a5132019-05-13 17:22:55 -07006815 /*
6816 * resv_map can not be NULL as hugetlb_reserve_pages is only
6817 * called for inodes for which resv_maps were created (see
6818 * hugetlbfs_get_inode).
6819 */
Joonsoo Kim4e35f482014-04-03 14:47:30 -07006820 resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07006821
Mina Almasry0db9d742020-04-01 21:11:25 -07006822 chg = region_chg(resv_map, from, to, &regions_needed);
Joonsoo Kim9119a412014-04-03 14:47:25 -07006823 } else {
Mina Almasrye9fe92a2020-04-01 21:11:21 -07006824 /* Private mapping. */
Joonsoo Kim9119a412014-04-03 14:47:25 -07006825 resv_map = resv_map_alloc();
Mel Gorman5a6fe122009-02-10 14:02:27 +00006826 if (!resv_map)
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07006827 goto out_err;
Mel Gorman5a6fe122009-02-10 14:02:27 +00006828
Mel Gorman17c9d122009-02-11 16:34:16 +00006829 chg = to - from;
6830
Mel Gorman5a6fe122009-02-10 14:02:27 +00006831 set_vma_resv_map(vma, resv_map);
6832 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
6833 }
6834
Mike Kravetz33b8f842021-02-24 12:09:54 -08006835 if (chg < 0)
Dave Hansenc50ac052012-05-29 15:06:46 -07006836 goto out_err;
Mel Gorman17c9d122009-02-11 16:34:16 +00006837
Mike Kravetz33b8f842021-02-24 12:09:54 -08006838 if (hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h),
6839 chg * pages_per_huge_page(h), &h_cg) < 0)
Mina Almasry075a61d2020-04-01 21:11:28 -07006840 goto out_err;
Mina Almasry075a61d2020-04-01 21:11:28 -07006841
6842 if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) {
6843 /* For private mappings, the hugetlb_cgroup uncharge info hangs
6844 * of the resv_map.
6845 */
6846 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h);
6847 }
6848
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07006849 /*
6850 * There must be enough pages in the subpool for the mapping. If
6851 * the subpool has a minimum size, there may be some global
6852 * reservations already in place (gbl_reserve).
6853 */
6854 gbl_reserve = hugepage_subpool_get_pages(spool, chg);
Mike Kravetz33b8f842021-02-24 12:09:54 -08006855 if (gbl_reserve < 0)
Mina Almasry075a61d2020-04-01 21:11:28 -07006856 goto out_uncharge_cgroup;
Mel Gorman17c9d122009-02-11 16:34:16 +00006857
6858 /*
6859 * Check enough hugepages are available for the reservation.
David Gibson90481622012-03-21 16:34:12 -07006860 * Hand the pages back to the subpool if there are not
Mel Gorman17c9d122009-02-11 16:34:16 +00006861 */
Mike Kravetz33b8f842021-02-24 12:09:54 -08006862 if (hugetlb_acct_memory(h, gbl_reserve) < 0)
Mina Almasry075a61d2020-04-01 21:11:28 -07006863 goto out_put_pages;
Mel Gorman17c9d122009-02-11 16:34:16 +00006864
6865 /*
6866 * Account for the reservations made. Shared mappings record regions
6867 * that have reservations as they are shared by multiple VMAs.
6868 * When the last VMA disappears, the region map says how much
6869 * the reservation was and the page cache tells how much of
6870 * the reservation was consumed. Private mappings are per-VMA and
6871 * only the consumed reservations are tracked. When the VMA
6872 * disappears, the original reservation is the VMA size and the
6873 * consumed reservations are stored in the map. Hence, nothing
6874 * else has to be done for private mappings here
6875 */
Mike Kravetz33039672015-06-24 16:57:58 -07006876 if (!vma || vma->vm_flags & VM_MAYSHARE) {
Mina Almasry075a61d2020-04-01 21:11:28 -07006877 add = region_add(resv_map, from, to, regions_needed, h, h_cg);
Mike Kravetz33039672015-06-24 16:57:58 -07006878
Mina Almasry0db9d742020-04-01 21:11:25 -07006879 if (unlikely(add < 0)) {
6880 hugetlb_acct_memory(h, -gbl_reserve);
Mina Almasry075a61d2020-04-01 21:11:28 -07006881 goto out_put_pages;
Mina Almasry0db9d742020-04-01 21:11:25 -07006882 } else if (unlikely(chg > add)) {
Mike Kravetz33039672015-06-24 16:57:58 -07006883 /*
6884 * pages in this range were added to the reserve
6885 * map between region_chg and region_add. This
Sidhartha Kumard0ce0e42023-01-25 09:05:33 -08006886 * indicates a race with alloc_hugetlb_folio. Adjust
Mike Kravetz33039672015-06-24 16:57:58 -07006887 * the subpool and reserve counts modified above
6888 * based on the difference.
6889 */
6890 long rsv_adjust;
6891
Miaohe Lind85aecf2021-03-24 21:37:17 -07006892 /*
6893 * hugetlb_cgroup_uncharge_cgroup_rsvd() will put the
6894 * reference to h_cg->css. See comment below for detail.
6895 */
Mina Almasry075a61d2020-04-01 21:11:28 -07006896 hugetlb_cgroup_uncharge_cgroup_rsvd(
6897 hstate_index(h),
6898 (chg - add) * pages_per_huge_page(h), h_cg);
6899
Mike Kravetz33039672015-06-24 16:57:58 -07006900 rsv_adjust = hugepage_subpool_put_pages(spool,
6901 chg - add);
6902 hugetlb_acct_memory(h, -rsv_adjust);
Miaohe Lind85aecf2021-03-24 21:37:17 -07006903 } else if (h_cg) {
6904 /*
6905 * The file_regions will hold their own reference to
6906 * h_cg->css. So we should release the reference held
6907 * via hugetlb_cgroup_charge_cgroup_rsvd() when we are
6908 * done.
6909 */
6910 hugetlb_cgroup_put_rsvd_cgroup(h_cg);
Mike Kravetz33039672015-06-24 16:57:58 -07006911 }
6912 }
Mike Kravetz33b8f842021-02-24 12:09:54 -08006913 return true;
6914
Mina Almasry075a61d2020-04-01 21:11:28 -07006915out_put_pages:
6916 /* put back original number of pages, chg */
6917 (void)hugepage_subpool_put_pages(spool, chg);
6918out_uncharge_cgroup:
6919 hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h),
6920 chg * pages_per_huge_page(h), h_cg);
Dave Hansenc50ac052012-05-29 15:06:46 -07006921out_err:
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07006922 hugetlb_vma_lock_free(vma);
Mike Kravetz5e911372015-09-08 15:01:28 -07006923 if (!vma || vma->vm_flags & VM_MAYSHARE)
Mina Almasry0db9d742020-04-01 21:11:25 -07006924 /* Only call region_abort if the region_chg succeeded but the
6925 * region_add failed or didn't run.
6926 */
6927 if (chg >= 0 && add < 0)
6928 region_abort(resv_map, from, to, regions_needed);
Joonsoo Kimf031dd22014-04-03 14:47:28 -07006929 if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
6930 kref_put(&resv_map->refs, resv_map_release);
Mike Kravetz33b8f842021-02-24 12:09:54 -08006931 return false;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07006932}
6933
Mike Kravetzb5cec282015-09-08 15:01:41 -07006934long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
6935 long freed)
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07006936{
Andi Kleena5516432008-07-23 21:27:41 -07006937 struct hstate *h = hstate_inode(inode);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07006938 struct resv_map *resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07006939 long chg = 0;
David Gibson90481622012-03-21 16:34:12 -07006940 struct hugepage_subpool *spool = subpool_inode(inode);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07006941 long gbl_reserve;
Ken Chen45c682a2007-11-14 16:59:44 -08006942
Mike Kravetzf27a5132019-05-13 17:22:55 -07006943 /*
6944 * Since this routine can be called in the evict inode path for all
6945 * hugetlbfs inodes, resv_map could be NULL.
6946 */
Mike Kravetzb5cec282015-09-08 15:01:41 -07006947 if (resv_map) {
6948 chg = region_del(resv_map, start, end);
6949 /*
6950 * region_del() can fail in the rare case where a region
6951 * must be split and another region descriptor can not be
6952 * allocated. If end == LONG_MAX, it will not fail.
6953 */
6954 if (chg < 0)
6955 return chg;
6956 }
6957
Ken Chen45c682a2007-11-14 16:59:44 -08006958 spin_lock(&inode->i_lock);
Eric Sandeene4c6f8b2009-07-29 15:02:16 -07006959 inode->i_blocks -= (blocks_per_huge_page(h) * freed);
Ken Chen45c682a2007-11-14 16:59:44 -08006960 spin_unlock(&inode->i_lock);
6961
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07006962 /*
6963 * If the subpool has a minimum size, the number of global
6964 * reservations to be released may be adjusted.
Miaohe Lindddf31a2021-05-04 18:34:35 -07006965 *
6966 * Note that !resv_map implies freed == 0. So (chg - freed)
6967 * won't go negative.
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07006968 */
6969 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
6970 hugetlb_acct_memory(h, -gbl_reserve);
Mike Kravetzb5cec282015-09-08 15:01:41 -07006971
6972 return 0;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07006973}
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09006974
Steve Capper3212b532013-04-23 12:35:02 +01006975#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
6976static unsigned long page_table_shareable(struct vm_area_struct *svma,
6977 struct vm_area_struct *vma,
6978 unsigned long addr, pgoff_t idx)
6979{
6980 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
6981 svma->vm_start;
6982 unsigned long sbase = saddr & PUD_MASK;
6983 unsigned long s_end = sbase + PUD_SIZE;
6984
6985 /* Allow segments to share if only one is marked locked */
Suren Baghdasaryane430a952023-01-26 11:37:48 -08006986 unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED_MASK;
6987 unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED_MASK;
Steve Capper3212b532013-04-23 12:35:02 +01006988
6989 /*
6990 * match the virtual addresses, permission and the alignment of the
6991 * page table page.
Mike Kravetz131a79b2022-10-04 18:17:05 -07006992 *
6993 * Also, vma_lock (vm_private_data) is required for sharing.
Steve Capper3212b532013-04-23 12:35:02 +01006994 */
6995 if (pmd_index(addr) != pmd_index(saddr) ||
6996 vm_flags != svm_flags ||
Mike Kravetz131a79b2022-10-04 18:17:05 -07006997 !range_in_vma(svma, sbase, s_end) ||
6998 !svma->vm_private_data)
Steve Capper3212b532013-04-23 12:35:02 +01006999 return 0;
7000
7001 return saddr;
7002}
7003
Mike Kravetzbbff39c2022-10-04 18:17:07 -07007004bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
Steve Capper3212b532013-04-23 12:35:02 +01007005{
Mike Kravetzbbff39c2022-10-04 18:17:07 -07007006 unsigned long start = addr & PUD_MASK;
7007 unsigned long end = start + PUD_SIZE;
7008
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07007009#ifdef CONFIG_USERFAULTFD
7010 if (uffd_disable_huge_pmd_share(vma))
7011 return false;
7012#endif
Steve Capper3212b532013-04-23 12:35:02 +01007013 /*
7014 * check on proper vm_flags and page table alignment
7015 */
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07007016 if (!(vma->vm_flags & VM_MAYSHARE))
7017 return false;
Mike Kravetzbbff39c2022-10-04 18:17:07 -07007018 if (!vma->vm_private_data) /* vma lock required for sharing */
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07007019 return false;
7020 if (!range_in_vma(vma, start, end))
7021 return false;
7022 return true;
7023}
7024
Steve Capper3212b532013-04-23 12:35:02 +01007025/*
Mike Kravetz017b1662018-10-05 15:51:29 -07007026 * Determine if start,end range within vma could be mapped by shared pmd.
7027 * If yes, adjust start and end to cover range associated with possible
7028 * shared pmd mappings.
7029 */
7030void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
7031 unsigned long *start, unsigned long *end)
7032{
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007033 unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE),
7034 v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE);
Mike Kravetz017b1662018-10-05 15:51:29 -07007035
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007036 /*
Ingo Molnarf0953a12021-05-06 18:06:47 -07007037 * vma needs to span at least one aligned PUD size, and the range
7038 * must be at least partially within in.
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007039 */
7040 if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) ||
7041 (*end <= v_start) || (*start >= v_end))
Mike Kravetz017b1662018-10-05 15:51:29 -07007042 return;
7043
Peter Xu75802ca62020-08-06 23:26:11 -07007044 /* Extend the range to be PUD aligned for a worst case scenario */
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007045 if (*start > v_start)
7046 *start = ALIGN_DOWN(*start, PUD_SIZE);
Mike Kravetz017b1662018-10-05 15:51:29 -07007047
Li Xinhaia1ba9da2021-02-24 12:06:54 -08007048 if (*end < v_end)
7049 *end = ALIGN(*end, PUD_SIZE);
Mike Kravetz017b1662018-10-05 15:51:29 -07007050}
7051
7052/*
Steve Capper3212b532013-04-23 12:35:02 +01007053 * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
7054 * and returns the corresponding pte. While this is not necessary for the
7055 * !shared pmd case because we can allocate the pmd later as well, it makes the
Mike Kravetz3a47c542022-09-14 15:18:03 -07007056 * code much cleaner. pmd allocation is essential for the shared case because
7057 * pud has to be populated inside the same i_mmap_rwsem section - otherwise
7058 * racing tasks could either miss the sharing (see huge_pte_offset) or select a
7059 * bad pmd for sharing.
Steve Capper3212b532013-04-23 12:35:02 +01007060 */
Peter Xuaec44e02021-05-04 18:33:00 -07007061pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
7062 unsigned long addr, pud_t *pud)
Steve Capper3212b532013-04-23 12:35:02 +01007063{
Steve Capper3212b532013-04-23 12:35:02 +01007064 struct address_space *mapping = vma->vm_file->f_mapping;
7065 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
7066 vma->vm_pgoff;
7067 struct vm_area_struct *svma;
7068 unsigned long saddr;
7069 pte_t *spte = NULL;
7070 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08007071 spinlock_t *ptl;
Steve Capper3212b532013-04-23 12:35:02 +01007072
Mike Kravetz3a47c542022-09-14 15:18:03 -07007073 i_mmap_lock_read(mapping);
Steve Capper3212b532013-04-23 12:35:02 +01007074 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
7075 if (svma == vma)
7076 continue;
7077
7078 saddr = page_table_shareable(svma, vma, addr, idx);
7079 if (saddr) {
Peter Xu9c67a202022-12-16 10:52:29 -05007080 spte = hugetlb_walk(svma, saddr,
7081 vma_mmu_pagesize(svma));
Steve Capper3212b532013-04-23 12:35:02 +01007082 if (spte) {
7083 get_page(virt_to_page(spte));
7084 break;
7085 }
7086 }
7087 }
7088
7089 if (!spte)
7090 goto out;
7091
Aneesh Kumar K.V8bea8052016-12-12 16:41:59 -08007092 ptl = huge_pte_lock(hstate_vma(vma), mm, spte);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08007093 if (pud_none(*pud)) {
Steve Capper3212b532013-04-23 12:35:02 +01007094 pud_populate(mm, pud,
7095 (pmd_t *)((unsigned long)spte & PAGE_MASK));
Kirill A. Shutemovc17b1f42016-06-24 14:49:51 -07007096 mm_inc_nr_pmds(mm);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08007097 } else {
Steve Capper3212b532013-04-23 12:35:02 +01007098 put_page(virt_to_page(spte));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08007099 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08007100 spin_unlock(ptl);
Steve Capper3212b532013-04-23 12:35:02 +01007101out:
7102 pte = (pte_t *)pmd_alloc(mm, pud, addr);
Mike Kravetz3a47c542022-09-14 15:18:03 -07007103 i_mmap_unlock_read(mapping);
Steve Capper3212b532013-04-23 12:35:02 +01007104 return pte;
7105}
7106
7107/*
7108 * unmap huge page backed by shared pte.
7109 *
7110 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
7111 * indicated by page_count > 1, unmap is achieved by clearing pud and
7112 * decrementing the ref count. If count == 1, the pte page is not shared.
7113 *
Mike Kravetz3a47c542022-09-14 15:18:03 -07007114 * Called with page table lock held.
Steve Capper3212b532013-04-23 12:35:02 +01007115 *
7116 * returns: 1 successfully unmapped a shared pte page
7117 * 0 the underlying pte page is not shared, or it is the last user
7118 */
Mike Kravetz34ae2042020-08-11 18:31:38 -07007119int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007120 unsigned long addr, pte_t *ptep)
Steve Capper3212b532013-04-23 12:35:02 +01007121{
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007122 pgd_t *pgd = pgd_offset(mm, addr);
7123 p4d_t *p4d = p4d_offset(pgd, addr);
7124 pud_t *pud = pud_offset(p4d, addr);
Steve Capper3212b532013-04-23 12:35:02 +01007125
Mike Kravetz34ae2042020-08-11 18:31:38 -07007126 i_mmap_assert_write_locked(vma->vm_file->f_mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07007127 hugetlb_vma_assert_locked(vma);
Steve Capper3212b532013-04-23 12:35:02 +01007128 BUG_ON(page_count(virt_to_page(ptep)) == 0);
7129 if (page_count(virt_to_page(ptep)) == 1)
7130 return 0;
7131
7132 pud_clear(pud);
7133 put_page(virt_to_page(ptep));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08007134 mm_dec_nr_pmds(mm);
Steve Capper3212b532013-04-23 12:35:02 +01007135 return 1;
7136}
Peter Xuc1991e02021-05-04 18:33:04 -07007137
Steve Capper9e5fc742013-04-30 08:02:03 +01007138#else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
Mike Kravetz8d9bfb22022-09-14 15:18:07 -07007139
Peter Xuaec44e02021-05-04 18:33:00 -07007140pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
7141 unsigned long addr, pud_t *pud)
Steve Capper9e5fc742013-04-30 08:02:03 +01007142{
7143 return NULL;
7144}
Zhang Zhene81f2d22015-06-24 16:56:13 -07007145
Mike Kravetz34ae2042020-08-11 18:31:38 -07007146int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007147 unsigned long addr, pte_t *ptep)
Zhang Zhene81f2d22015-06-24 16:56:13 -07007148{
7149 return 0;
7150}
Mike Kravetz017b1662018-10-05 15:51:29 -07007151
7152void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
7153 unsigned long *start, unsigned long *end)
7154{
7155}
Peter Xuc1991e02021-05-04 18:33:04 -07007156
7157bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
7158{
7159 return false;
7160}
Steve Capper3212b532013-04-23 12:35:02 +01007161#endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
7162
Steve Capper9e5fc742013-04-30 08:02:03 +01007163#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
Peter Xuaec44e02021-05-04 18:33:00 -07007164pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
Steve Capper9e5fc742013-04-30 08:02:03 +01007165 unsigned long addr, unsigned long sz)
7166{
7167 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007168 p4d_t *p4d;
Steve Capper9e5fc742013-04-30 08:02:03 +01007169 pud_t *pud;
7170 pte_t *pte = NULL;
7171
7172 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovf4f0a3d2017-11-29 16:11:30 -08007173 p4d = p4d_alloc(mm, pgd, addr);
7174 if (!p4d)
7175 return NULL;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007176 pud = pud_alloc(mm, p4d, addr);
Steve Capper9e5fc742013-04-30 08:02:03 +01007177 if (pud) {
7178 if (sz == PUD_SIZE) {
7179 pte = (pte_t *)pud;
7180 } else {
7181 BUG_ON(sz != PMD_SIZE);
Peter Xuc1991e02021-05-04 18:33:04 -07007182 if (want_pmd_share(vma, addr) && pud_none(*pud))
Peter Xuaec44e02021-05-04 18:33:00 -07007183 pte = huge_pmd_share(mm, vma, addr, pud);
Steve Capper9e5fc742013-04-30 08:02:03 +01007184 else
7185 pte = (pte_t *)pmd_alloc(mm, pud, addr);
7186 }
7187 }
Michal Hocko4e666312016-08-02 14:02:34 -07007188 BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte));
Steve Capper9e5fc742013-04-30 08:02:03 +01007189
7190 return pte;
7191}
7192
Punit Agrawal9b19df22017-09-06 16:21:01 -07007193/*
7194 * huge_pte_offset() - Walk the page table to resolve the hugepage
7195 * entry at address @addr
7196 *
Li Xinhai8ac0b812020-06-03 16:00:53 -07007197 * Return: Pointer to page table entry (PUD or PMD) for
7198 * address @addr, or NULL if a !p*d_present() entry is encountered and the
Punit Agrawal9b19df22017-09-06 16:21:01 -07007199 * size @sz doesn't match the hugepage size at this level of the page
7200 * table.
7201 */
Punit Agrawal7868a202017-07-06 15:39:42 -07007202pte_t *huge_pte_offset(struct mm_struct *mm,
7203 unsigned long addr, unsigned long sz)
Steve Capper9e5fc742013-04-30 08:02:03 +01007204{
7205 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007206 p4d_t *p4d;
Li Xinhai8ac0b812020-06-03 16:00:53 -07007207 pud_t *pud;
7208 pmd_t *pmd;
Steve Capper9e5fc742013-04-30 08:02:03 +01007209
7210 pgd = pgd_offset(mm, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007211 if (!pgd_present(*pgd))
7212 return NULL;
7213 p4d = p4d_offset(pgd, addr);
7214 if (!p4d_present(*p4d))
7215 return NULL;
Punit Agrawal9b19df22017-09-06 16:21:01 -07007216
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007217 pud = pud_offset(p4d, addr);
Li Xinhai8ac0b812020-06-03 16:00:53 -07007218 if (sz == PUD_SIZE)
7219 /* must be pud huge, non-present or none */
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007220 return (pte_t *)pud;
Li Xinhai8ac0b812020-06-03 16:00:53 -07007221 if (!pud_present(*pud))
7222 return NULL;
7223 /* must have a valid entry and size to go further */
Punit Agrawal9b19df22017-09-06 16:21:01 -07007224
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03007225 pmd = pmd_offset(pud, addr);
Li Xinhai8ac0b812020-06-03 16:00:53 -07007226 /* must be pmd huge, non-present or none */
7227 return (pte_t *)pmd;
Steve Capper9e5fc742013-04-30 08:02:03 +01007228}
7229
Mike Kravetze95a9852022-06-21 16:56:17 -07007230/*
7231 * Return a mask that can be used to update an address to the last huge
7232 * page in a page table page mapping size. Used to skip non-present
7233 * page table entries when linearly scanning address ranges. Architectures
7234 * with unique huge page to page table relationships can define their own
7235 * version of this routine.
7236 */
7237unsigned long hugetlb_mask_last_page(struct hstate *h)
7238{
7239 unsigned long hp_size = huge_page_size(h);
7240
7241 if (hp_size == PUD_SIZE)
7242 return P4D_SIZE - PUD_SIZE;
7243 else if (hp_size == PMD_SIZE)
7244 return PUD_SIZE - PMD_SIZE;
7245 else
7246 return 0UL;
7247}
7248
7249#else
7250
7251/* See description above. Architectures can provide their own version. */
7252__weak unsigned long hugetlb_mask_last_page(struct hstate *h)
7253{
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007254#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
7255 if (huge_page_size(h) == PMD_SIZE)
7256 return PUD_SIZE - PMD_SIZE;
7257#endif
Mike Kravetze95a9852022-06-21 16:56:17 -07007258 return 0UL;
7259}
7260
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08007261#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
7262
7263/*
7264 * These functions are overwritable if your architecture needs its own
7265 * behavior.
7266 */
Baolin Wang9747b9e2023-02-15 18:39:36 +08007267bool isolate_hugetlb(struct folio *folio, struct list_head *list)
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007268{
Baolin Wang9747b9e2023-02-15 18:39:36 +08007269 bool ret = true;
Naoya Horiguchibcc54222015-04-15 16:14:38 -07007270
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007271 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumar6aa3a922023-01-13 16:30:50 -06007272 if (!folio_test_hugetlb(folio) ||
7273 !folio_test_hugetlb_migratable(folio) ||
7274 !folio_try_get(folio)) {
Baolin Wang9747b9e2023-02-15 18:39:36 +08007275 ret = false;
Naoya Horiguchibcc54222015-04-15 16:14:38 -07007276 goto unlock;
7277 }
Sidhartha Kumar6aa3a922023-01-13 16:30:50 -06007278 folio_clear_hugetlb_migratable(folio);
7279 list_move_tail(&folio->lru, list);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07007280unlock:
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007281 spin_unlock_irq(&hugetlb_lock);
Naoya Horiguchibcc54222015-04-15 16:14:38 -07007282 return ret;
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007283}
7284
Sidhartha Kumar04bac042023-01-18 09:40:39 -08007285int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison)
Naoya Horiguchi25182f02021-06-15 18:23:13 -07007286{
7287 int ret = 0;
7288
7289 *hugetlb = false;
7290 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumar04bac042023-01-18 09:40:39 -08007291 if (folio_test_hugetlb(folio)) {
Naoya Horiguchi25182f02021-06-15 18:23:13 -07007292 *hugetlb = true;
Sidhartha Kumar04bac042023-01-18 09:40:39 -08007293 if (folio_test_hugetlb_freed(folio))
Naoya Horiguchib283d9832022-04-28 23:16:02 -07007294 ret = 0;
Sidhartha Kumar04bac042023-01-18 09:40:39 -08007295 else if (folio_test_hugetlb_migratable(folio) || unpoison)
7296 ret = folio_try_get(folio);
Naoya Horiguchi0ed950d2021-06-28 19:43:17 -07007297 else
7298 ret = -EBUSY;
Naoya Horiguchi25182f02021-06-15 18:23:13 -07007299 }
7300 spin_unlock_irq(&hugetlb_lock);
7301 return ret;
7302}
7303
Naoya Horiguchie591ef72022-10-24 15:20:09 +09007304int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
7305 bool *migratable_cleared)
Naoya Horiguchi405ce052022-04-21 16:35:33 -07007306{
7307 int ret;
7308
7309 spin_lock_irq(&hugetlb_lock);
Naoya Horiguchie591ef72022-10-24 15:20:09 +09007310 ret = __get_huge_page_for_hwpoison(pfn, flags, migratable_cleared);
Naoya Horiguchi405ce052022-04-21 16:35:33 -07007311 spin_unlock_irq(&hugetlb_lock);
7312 return ret;
7313}
7314
Sidhartha Kumarea8e72f2023-01-25 09:05:32 -08007315void folio_putback_active_hugetlb(struct folio *folio)
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007316{
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007317 spin_lock_irq(&hugetlb_lock);
Sidhartha Kumarea8e72f2023-01-25 09:05:32 -08007318 folio_set_hugetlb_migratable(folio);
7319 list_move_tail(&folio->lru, &(folio_hstate(folio))->hugepage_activelist);
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007320 spin_unlock_irq(&hugetlb_lock);
Sidhartha Kumarea8e72f2023-01-25 09:05:32 -08007321 folio_put(folio);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07007322}
Michal Hockoab5ac902018-01-31 16:20:48 -08007323
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007324void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason)
Michal Hockoab5ac902018-01-31 16:20:48 -08007325{
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007326 struct hstate *h = folio_hstate(old_folio);
Michal Hockoab5ac902018-01-31 16:20:48 -08007327
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007328 hugetlb_cgroup_migrate(old_folio, new_folio);
7329 set_page_owner_migrate_reason(&new_folio->page, reason);
Michal Hockoab5ac902018-01-31 16:20:48 -08007330
7331 /*
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007332 * transfer temporary state of the new hugetlb folio. This is
Michal Hockoab5ac902018-01-31 16:20:48 -08007333 * reverse to other transitions because the newpage is going to
7334 * be final while the old one will be freed so it takes over
7335 * the temporary status.
7336 *
7337 * Also note that we have to transfer the per-node surplus state
7338 * here as well otherwise the global surplus count will not match
7339 * the per-node's.
7340 */
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007341 if (folio_test_hugetlb_temporary(new_folio)) {
7342 int old_nid = folio_nid(old_folio);
7343 int new_nid = folio_nid(new_folio);
Michal Hockoab5ac902018-01-31 16:20:48 -08007344
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07007345 folio_set_hugetlb_temporary(old_folio);
7346 folio_clear_hugetlb_temporary(new_folio);
7347
Michal Hockoab5ac902018-01-31 16:20:48 -08007348
Miaohe Lin5af1ab12021-05-04 18:33:25 -07007349 /*
7350 * There is no need to transfer the per-node surplus state
7351 * when we do not cross the node.
7352 */
7353 if (new_nid == old_nid)
7354 return;
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007355 spin_lock_irq(&hugetlb_lock);
Michal Hockoab5ac902018-01-31 16:20:48 -08007356 if (h->surplus_huge_pages_node[old_nid]) {
7357 h->surplus_huge_pages_node[old_nid]--;
7358 h->surplus_huge_pages_node[new_nid]++;
7359 }
Mike Kravetzdb71ef72021-05-04 18:35:07 -07007360 spin_unlock_irq(&hugetlb_lock);
Michal Hockoab5ac902018-01-31 16:20:48 -08007361 }
7362}
Roman Gushchincf11e852020-04-10 14:32:45 -07007363
James Houghtonb30c14c2023-01-04 23:19:10 +00007364static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
7365 unsigned long start,
7366 unsigned long end)
Peter Xu6dfeaff2021-05-04 18:33:13 -07007367{
7368 struct hstate *h = hstate_vma(vma);
7369 unsigned long sz = huge_page_size(h);
7370 struct mm_struct *mm = vma->vm_mm;
7371 struct mmu_notifier_range range;
James Houghtonb30c14c2023-01-04 23:19:10 +00007372 unsigned long address;
Peter Xu6dfeaff2021-05-04 18:33:13 -07007373 spinlock_t *ptl;
7374 pte_t *ptep;
7375
7376 if (!(vma->vm_flags & VM_MAYSHARE))
7377 return;
7378
Peter Xu6dfeaff2021-05-04 18:33:13 -07007379 if (start >= end)
7380 return;
7381
Baolin Wang9c8bbfa2022-04-29 14:36:58 -07007382 flush_cache_range(vma, start, end);
Peter Xu6dfeaff2021-05-04 18:33:13 -07007383 /*
7384 * No need to call adjust_range_if_pmd_sharing_possible(), because
7385 * we have already done the PUD_SIZE alignment.
7386 */
Alistair Popple7d4a8be2023-01-10 13:57:22 +11007387 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
Peter Xu6dfeaff2021-05-04 18:33:13 -07007388 start, end);
7389 mmu_notifier_invalidate_range_start(&range);
Mike Kravetz40549ba2022-09-14 15:18:09 -07007390 hugetlb_vma_lock_write(vma);
Peter Xu6dfeaff2021-05-04 18:33:13 -07007391 i_mmap_lock_write(vma->vm_file->f_mapping);
7392 for (address = start; address < end; address += PUD_SIZE) {
Peter Xu9c67a202022-12-16 10:52:29 -05007393 ptep = hugetlb_walk(vma, address, sz);
Peter Xu6dfeaff2021-05-04 18:33:13 -07007394 if (!ptep)
7395 continue;
7396 ptl = huge_pte_lock(h, mm, ptep);
Mike Kravetz4ddb4d92022-06-21 16:56:19 -07007397 huge_pmd_unshare(mm, vma, address, ptep);
Peter Xu6dfeaff2021-05-04 18:33:13 -07007398 spin_unlock(ptl);
7399 }
7400 flush_hugetlb_tlb_range(vma, start, end);
7401 i_mmap_unlock_write(vma->vm_file->f_mapping);
Mike Kravetz40549ba2022-09-14 15:18:09 -07007402 hugetlb_vma_unlock_write(vma);
Peter Xu6dfeaff2021-05-04 18:33:13 -07007403 /*
7404 * No need to call mmu_notifier_invalidate_range(), see
Mike Rapoportee657282022-06-27 09:00:26 +03007405 * Documentation/mm/mmu_notifier.rst.
Peter Xu6dfeaff2021-05-04 18:33:13 -07007406 */
7407 mmu_notifier_invalidate_range_end(&range);
7408}
7409
James Houghtonb30c14c2023-01-04 23:19:10 +00007410/*
7411 * This function will unconditionally remove all the shared pmd pgtable entries
7412 * within the specific vma for a hugetlbfs memory range.
7413 */
7414void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)
7415{
7416 hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE),
7417 ALIGN_DOWN(vma->vm_end, PUD_SIZE));
7418}
7419
Roman Gushchincf11e852020-04-10 14:32:45 -07007420#ifdef CONFIG_CMA
Roman Gushchincf11e852020-04-10 14:32:45 -07007421static bool cma_reserve_called __initdata;
7422
7423static int __init cmdline_parse_hugetlb_cma(char *p)
7424{
Baolin Wang38e719a2021-11-05 13:41:46 -07007425 int nid, count = 0;
7426 unsigned long tmp;
7427 char *s = p;
7428
7429 while (*s) {
7430 if (sscanf(s, "%lu%n", &tmp, &count) != 1)
7431 break;
7432
7433 if (s[count] == ':') {
Mike Kravetzf9317f72022-03-22 14:45:20 -07007434 if (tmp >= MAX_NUMNODES)
Baolin Wang38e719a2021-11-05 13:41:46 -07007435 break;
Mike Kravetzf9317f72022-03-22 14:45:20 -07007436 nid = array_index_nospec(tmp, MAX_NUMNODES);
Baolin Wang38e719a2021-11-05 13:41:46 -07007437
7438 s += count + 1;
7439 tmp = memparse(s, &s);
7440 hugetlb_cma_size_in_node[nid] = tmp;
7441 hugetlb_cma_size += tmp;
7442
7443 /*
7444 * Skip the separator if have one, otherwise
7445 * break the parsing.
7446 */
7447 if (*s == ',')
7448 s++;
7449 else
7450 break;
7451 } else {
7452 hugetlb_cma_size = memparse(p, &p);
7453 break;
7454 }
7455 }
7456
Roman Gushchincf11e852020-04-10 14:32:45 -07007457 return 0;
7458}
7459
7460early_param("hugetlb_cma", cmdline_parse_hugetlb_cma);
7461
7462void __init hugetlb_cma_reserve(int order)
7463{
7464 unsigned long size, reserved, per_node;
Baolin Wang38e719a2021-11-05 13:41:46 -07007465 bool node_specific_cma_alloc = false;
Roman Gushchincf11e852020-04-10 14:32:45 -07007466 int nid;
7467
7468 cma_reserve_called = true;
7469
7470 if (!hugetlb_cma_size)
7471 return;
7472
Baolin Wang38e719a2021-11-05 13:41:46 -07007473 for (nid = 0; nid < MAX_NUMNODES; nid++) {
7474 if (hugetlb_cma_size_in_node[nid] == 0)
7475 continue;
7476
Peng Liu30a51402022-04-29 14:36:58 -07007477 if (!node_online(nid)) {
Baolin Wang38e719a2021-11-05 13:41:46 -07007478 pr_warn("hugetlb_cma: invalid node %d specified\n", nid);
7479 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid];
7480 hugetlb_cma_size_in_node[nid] = 0;
7481 continue;
7482 }
7483
7484 if (hugetlb_cma_size_in_node[nid] < (PAGE_SIZE << order)) {
7485 pr_warn("hugetlb_cma: cma area of node %d should be at least %lu MiB\n",
7486 nid, (PAGE_SIZE << order) / SZ_1M);
7487 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid];
7488 hugetlb_cma_size_in_node[nid] = 0;
7489 } else {
7490 node_specific_cma_alloc = true;
7491 }
7492 }
7493
7494 /* Validate the CMA size again in case some invalid nodes specified. */
7495 if (!hugetlb_cma_size)
7496 return;
7497
Roman Gushchincf11e852020-04-10 14:32:45 -07007498 if (hugetlb_cma_size < (PAGE_SIZE << order)) {
7499 pr_warn("hugetlb_cma: cma area should be at least %lu MiB\n",
7500 (PAGE_SIZE << order) / SZ_1M);
Mike Kravetza01f4392021-11-05 13:41:27 -07007501 hugetlb_cma_size = 0;
Roman Gushchincf11e852020-04-10 14:32:45 -07007502 return;
7503 }
7504
Baolin Wang38e719a2021-11-05 13:41:46 -07007505 if (!node_specific_cma_alloc) {
7506 /*
7507 * If 3 GB area is requested on a machine with 4 numa nodes,
7508 * let's allocate 1 GB on first three nodes and ignore the last one.
7509 */
7510 per_node = DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes);
7511 pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n",
7512 hugetlb_cma_size / SZ_1M, per_node / SZ_1M);
7513 }
Roman Gushchincf11e852020-04-10 14:32:45 -07007514
7515 reserved = 0;
Peng Liu30a51402022-04-29 14:36:58 -07007516 for_each_online_node(nid) {
Roman Gushchincf11e852020-04-10 14:32:45 -07007517 int res;
Barry Song2281f792020-08-24 11:03:09 +12007518 char name[CMA_MAX_NAME];
Roman Gushchincf11e852020-04-10 14:32:45 -07007519
Baolin Wang38e719a2021-11-05 13:41:46 -07007520 if (node_specific_cma_alloc) {
7521 if (hugetlb_cma_size_in_node[nid] == 0)
7522 continue;
7523
7524 size = hugetlb_cma_size_in_node[nid];
7525 } else {
7526 size = min(per_node, hugetlb_cma_size - reserved);
7527 }
7528
Roman Gushchincf11e852020-04-10 14:32:45 -07007529 size = round_up(size, PAGE_SIZE << order);
7530
Barry Song2281f792020-08-24 11:03:09 +12007531 snprintf(name, sizeof(name), "hugetlb%d", nid);
Mike Kravetza01f4392021-11-05 13:41:27 -07007532 /*
7533 * Note that 'order per bit' is based on smallest size that
7534 * may be returned to CMA allocator in the case of
7535 * huge page demotion.
7536 */
7537 res = cma_declare_contiguous_nid(0, size, 0,
7538 PAGE_SIZE << HUGETLB_PAGE_ORDER,
Barry Song29d0f412020-08-11 18:32:00 -07007539 0, false, name,
Roman Gushchincf11e852020-04-10 14:32:45 -07007540 &hugetlb_cma[nid], nid);
7541 if (res) {
7542 pr_warn("hugetlb_cma: reservation failed: err %d, node %d",
7543 res, nid);
7544 continue;
7545 }
7546
7547 reserved += size;
7548 pr_info("hugetlb_cma: reserved %lu MiB on node %d\n",
7549 size / SZ_1M, nid);
7550
7551 if (reserved >= hugetlb_cma_size)
7552 break;
7553 }
Mike Kravetza01f4392021-11-05 13:41:27 -07007554
7555 if (!reserved)
7556 /*
7557 * hugetlb_cma_size is used to determine if allocations from
7558 * cma are possible. Set to zero if no cma regions are set up.
7559 */
7560 hugetlb_cma_size = 0;
Roman Gushchincf11e852020-04-10 14:32:45 -07007561}
7562
Miaohe Lin263b8992022-09-01 20:00:21 +08007563static void __init hugetlb_cma_check(void)
Roman Gushchincf11e852020-04-10 14:32:45 -07007564{
7565 if (!hugetlb_cma_size || cma_reserve_called)
7566 return;
7567
7568 pr_warn("hugetlb_cma: the option isn't supported by current arch\n");
7569}
7570
7571#endif /* CONFIG_CMA */