1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Generic hugetlb support. 46d49e352SNadia Yvette Chambers * (C) Nadia Yvette Chambers, April 2004 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds #include <linux/list.h> 71da177e4SLinus Torvalds #include <linux/init.h> 81da177e4SLinus Torvalds #include <linux/mm.h> 9e1759c21SAlexey Dobriyan #include <linux/seq_file.h> 101da177e4SLinus Torvalds #include <linux/sysctl.h> 111da177e4SLinus Torvalds #include <linux/highmem.h> 12cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 131da177e4SLinus Torvalds #include <linux/nodemask.h> 1463551ae0SDavid Gibson #include <linux/pagemap.h> 155da7ca86SChristoph Lameter #include <linux/mempolicy.h> 163b32123dSGideon Israel Dsouza #include <linux/compiler.h> 17aea47ff3SChristoph Lameter #include <linux/cpuset.h> 183935baa9SDavid Gibson #include <linux/mutex.h> 1997ad1087SMike Rapoport #include <linux/memblock.h> 20a3437870SNishanth Aravamudan #include <linux/sysfs.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 22bbe88753SJoonsoo Kim #include <linux/sched/mm.h> 2363489f8eSMike Kravetz #include <linux/mmdebug.h> 24174cd4b1SIngo Molnar #include <linux/sched/signal.h> 250fe6e20bSNaoya Horiguchi #include <linux/rmap.h> 26c6247f72SMatthew Wilcox #include <linux/string_helpers.h> 27fd6a03edSNaoya Horiguchi #include <linux/swap.h> 28fd6a03edSNaoya Horiguchi #include <linux/swapops.h> 298382d914SDavidlohr Bueso #include <linux/jhash.h> 3098fa15f3SAnshuman Khandual #include <linux/numa.h> 31c77c0a8aSWaiman Long #include <linux/llist.h> 32cf11e85fSRoman Gushchin #include <linux/cma.h> 338cc5fcbbSMina Almasry #include <linux/migrate.h> 34f9317f77SMike Kravetz #include <linux/nospec.h> 35662ce1dcSYang Yang #include <linux/delayacct.h> 36b958d4d0SMuchun Song #include <linux/memory.h> 37d6606683SLinus Torvalds 3863551ae0SDavid Gibson #include <asm/page.h> 39ca15ca40SMike Rapoport #include <asm/pgalloc.h> 4024669e58SAneesh Kumar K.V #include <asm/tlb.h> 4163551ae0SDavid Gibson 4224669e58SAneesh Kumar K.V #include <linux/io.h> 4363551ae0SDavid Gibson #include <linux/hugetlb.h> 449dd540e2SAneesh Kumar K.V #include <linux/hugetlb_cgroup.h> 459a305230SLee Schermerhorn #include <linux/node.h> 46ab5ac90aSMichal Hocko #include <linux/page_owner.h> 477835e98bSNick Piggin #include "internal.h" 48f41f2ed4SMuchun Song #include "hugetlb_vmemmap.h" 491da177e4SLinus Torvalds 50c3f38a38SAneesh Kumar K.V int hugetlb_max_hstate __read_mostly; 51e5ff2159SAndi Kleen unsigned int default_hstate_idx; 52e5ff2159SAndi Kleen struct hstate hstates[HUGE_MAX_HSTATE]; 53cf11e85fSRoman Gushchin 54dbda8feaSBarry Song #ifdef CONFIG_CMA 55cf11e85fSRoman Gushchin static struct cma *hugetlb_cma[MAX_NUMNODES]; 5638e719abSBaolin Wang static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata; 572f6c57d6SSidhartha Kumar static bool hugetlb_cma_folio(struct folio *folio, unsigned int order) 58a01f4390SMike Kravetz { 592f6c57d6SSidhartha Kumar return cma_pages_valid(hugetlb_cma[folio_nid(folio)], &folio->page, 60a01f4390SMike Kravetz 1 << order); 61a01f4390SMike Kravetz } 62a01f4390SMike Kravetz #else 632f6c57d6SSidhartha Kumar static bool hugetlb_cma_folio(struct folio *folio, unsigned int order) 64a01f4390SMike Kravetz { 65a01f4390SMike Kravetz return false; 66a01f4390SMike Kravetz } 67dbda8feaSBarry Song #endif 68dbda8feaSBarry Song static unsigned long hugetlb_cma_size __initdata; 69cf11e85fSRoman Gushchin 7053ba51d2SJon Tollefson __initdata LIST_HEAD(huge_boot_pages); 7153ba51d2SJon Tollefson 72e5ff2159SAndi Kleen /* for command line parsing */ 73e5ff2159SAndi Kleen static struct hstate * __initdata parsed_hstate; 74e5ff2159SAndi Kleen static unsigned long __initdata default_hstate_max_huge_pages; 759fee021dSVaishali Thakkar static bool __initdata parsed_valid_hugepagesz = true; 76282f4214SMike Kravetz static bool __initdata parsed_default_hugepagesz; 77b5389086SZhenguo Yao static unsigned int default_hugepages_in_node[MAX_NUMNODES] __initdata; 78e5ff2159SAndi Kleen 793935baa9SDavid Gibson /* 8031caf665SNaoya Horiguchi * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages, 8131caf665SNaoya Horiguchi * free_huge_pages, and surplus_huge_pages. 823935baa9SDavid Gibson */ 83c3f38a38SAneesh Kumar K.V DEFINE_SPINLOCK(hugetlb_lock); 840bd0f9fbSEric Paris 858382d914SDavidlohr Bueso /* 868382d914SDavidlohr Bueso * Serializes faults on the same logical page. This is used to 878382d914SDavidlohr Bueso * prevent spurious OOMs when the hugepage pool is fully utilized. 888382d914SDavidlohr Bueso */ 898382d914SDavidlohr Bueso static int num_fault_mutexes; 90c672c7f2SMike Kravetz struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp; 918382d914SDavidlohr Bueso 927ca02d0aSMike Kravetz /* Forward declaration */ 937ca02d0aSMike Kravetz static int hugetlb_acct_memory(struct hstate *h, long delta); 948d9bfb26SMike Kravetz static void hugetlb_vma_lock_free(struct vm_area_struct *vma); 958d9bfb26SMike Kravetz static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma); 96ecfbd733SMike Kravetz static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma); 97b30c14cdSJames Houghton static void hugetlb_unshare_pmds(struct vm_area_struct *vma, 98b30c14cdSJames Houghton unsigned long start, unsigned long end); 997ca02d0aSMike Kravetz 1001d88433bSMiaohe Lin static inline bool subpool_is_free(struct hugepage_subpool *spool) 1011d88433bSMiaohe Lin { 1021d88433bSMiaohe Lin if (spool->count) 1031d88433bSMiaohe Lin return false; 1041d88433bSMiaohe Lin if (spool->max_hpages != -1) 1051d88433bSMiaohe Lin return spool->used_hpages == 0; 1061d88433bSMiaohe Lin if (spool->min_hpages != -1) 1071d88433bSMiaohe Lin return spool->rsv_hpages == spool->min_hpages; 1081d88433bSMiaohe Lin 1091d88433bSMiaohe Lin return true; 1101d88433bSMiaohe Lin } 1111d88433bSMiaohe Lin 112db71ef79SMike Kravetz static inline void unlock_or_release_subpool(struct hugepage_subpool *spool, 113db71ef79SMike Kravetz unsigned long irq_flags) 11490481622SDavid Gibson { 115db71ef79SMike Kravetz spin_unlock_irqrestore(&spool->lock, irq_flags); 11690481622SDavid Gibson 11790481622SDavid Gibson /* If no pages are used, and no other handles to the subpool 1187c8de358SEthon Paul * remain, give up any reservations based on minimum size and 1197ca02d0aSMike Kravetz * free the subpool */ 1201d88433bSMiaohe Lin if (subpool_is_free(spool)) { 1217ca02d0aSMike Kravetz if (spool->min_hpages != -1) 1227ca02d0aSMike Kravetz hugetlb_acct_memory(spool->hstate, 1237ca02d0aSMike Kravetz -spool->min_hpages); 12490481622SDavid Gibson kfree(spool); 12590481622SDavid Gibson } 1267ca02d0aSMike Kravetz } 12790481622SDavid Gibson 1287ca02d0aSMike Kravetz struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages, 1297ca02d0aSMike Kravetz long min_hpages) 13090481622SDavid Gibson { 13190481622SDavid Gibson struct hugepage_subpool *spool; 13290481622SDavid Gibson 133c6a91820SMike Kravetz spool = kzalloc(sizeof(*spool), GFP_KERNEL); 13490481622SDavid Gibson if (!spool) 13590481622SDavid Gibson return NULL; 13690481622SDavid Gibson 13790481622SDavid Gibson spin_lock_init(&spool->lock); 13890481622SDavid Gibson spool->count = 1; 1397ca02d0aSMike Kravetz spool->max_hpages = max_hpages; 1407ca02d0aSMike Kravetz spool->hstate = h; 1417ca02d0aSMike Kravetz spool->min_hpages = min_hpages; 1427ca02d0aSMike Kravetz 1437ca02d0aSMike Kravetz if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) { 1447ca02d0aSMike Kravetz kfree(spool); 1457ca02d0aSMike Kravetz return NULL; 1467ca02d0aSMike Kravetz } 1477ca02d0aSMike Kravetz spool->rsv_hpages = min_hpages; 14890481622SDavid Gibson 14990481622SDavid Gibson return spool; 15090481622SDavid Gibson } 15190481622SDavid Gibson 15290481622SDavid Gibson void hugepage_put_subpool(struct hugepage_subpool *spool) 15390481622SDavid Gibson { 154db71ef79SMike Kravetz unsigned long flags; 155db71ef79SMike Kravetz 156db71ef79SMike Kravetz spin_lock_irqsave(&spool->lock, flags); 15790481622SDavid Gibson BUG_ON(!spool->count); 15890481622SDavid Gibson spool->count--; 159db71ef79SMike Kravetz unlock_or_release_subpool(spool, flags); 16090481622SDavid Gibson } 16190481622SDavid Gibson 1621c5ecae3SMike Kravetz /* 1631c5ecae3SMike Kravetz * Subpool accounting for allocating and reserving pages. 1641c5ecae3SMike Kravetz * Return -ENOMEM if there are not enough resources to satisfy the 1659e7ee400SRandy Dunlap * request. Otherwise, return the number of pages by which the 1661c5ecae3SMike Kravetz * global pools must be adjusted (upward). The returned value may 1671c5ecae3SMike Kravetz * only be different than the passed value (delta) in the case where 1687c8de358SEthon Paul * a subpool minimum size must be maintained. 1691c5ecae3SMike Kravetz */ 1701c5ecae3SMike Kravetz static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, 17190481622SDavid Gibson long delta) 17290481622SDavid Gibson { 1731c5ecae3SMike Kravetz long ret = delta; 17490481622SDavid Gibson 17590481622SDavid Gibson if (!spool) 1761c5ecae3SMike Kravetz return ret; 17790481622SDavid Gibson 178db71ef79SMike Kravetz spin_lock_irq(&spool->lock); 17990481622SDavid Gibson 1801c5ecae3SMike Kravetz if (spool->max_hpages != -1) { /* maximum size accounting */ 1811c5ecae3SMike Kravetz if ((spool->used_hpages + delta) <= spool->max_hpages) 1821c5ecae3SMike Kravetz spool->used_hpages += delta; 1831c5ecae3SMike Kravetz else { 1841c5ecae3SMike Kravetz ret = -ENOMEM; 1851c5ecae3SMike Kravetz goto unlock_ret; 1861c5ecae3SMike Kravetz } 1871c5ecae3SMike Kravetz } 1881c5ecae3SMike Kravetz 18909a95e29SMike Kravetz /* minimum size accounting */ 19009a95e29SMike Kravetz if (spool->min_hpages != -1 && spool->rsv_hpages) { 1911c5ecae3SMike Kravetz if (delta > spool->rsv_hpages) { 1921c5ecae3SMike Kravetz /* 1931c5ecae3SMike Kravetz * Asking for more reserves than those already taken on 1941c5ecae3SMike Kravetz * behalf of subpool. Return difference. 1951c5ecae3SMike Kravetz */ 1961c5ecae3SMike Kravetz ret = delta - spool->rsv_hpages; 1971c5ecae3SMike Kravetz spool->rsv_hpages = 0; 1981c5ecae3SMike Kravetz } else { 1991c5ecae3SMike Kravetz ret = 0; /* reserves already accounted for */ 2001c5ecae3SMike Kravetz spool->rsv_hpages -= delta; 2011c5ecae3SMike Kravetz } 2021c5ecae3SMike Kravetz } 2031c5ecae3SMike Kravetz 2041c5ecae3SMike Kravetz unlock_ret: 205db71ef79SMike Kravetz spin_unlock_irq(&spool->lock); 20690481622SDavid Gibson return ret; 20790481622SDavid Gibson } 20890481622SDavid Gibson 2091c5ecae3SMike Kravetz /* 2101c5ecae3SMike Kravetz * Subpool accounting for freeing and unreserving pages. 2111c5ecae3SMike Kravetz * Return the number of global page reservations that must be dropped. 2121c5ecae3SMike Kravetz * The return value may only be different than the passed value (delta) 2131c5ecae3SMike Kravetz * in the case where a subpool minimum size must be maintained. 2141c5ecae3SMike Kravetz */ 2151c5ecae3SMike Kravetz static long hugepage_subpool_put_pages(struct hugepage_subpool *spool, 21690481622SDavid Gibson long delta) 21790481622SDavid Gibson { 2181c5ecae3SMike Kravetz long ret = delta; 219db71ef79SMike Kravetz unsigned long flags; 2201c5ecae3SMike Kravetz 22190481622SDavid Gibson if (!spool) 2221c5ecae3SMike Kravetz return delta; 22390481622SDavid Gibson 224db71ef79SMike Kravetz spin_lock_irqsave(&spool->lock, flags); 2251c5ecae3SMike Kravetz 2261c5ecae3SMike Kravetz if (spool->max_hpages != -1) /* maximum size accounting */ 22790481622SDavid Gibson spool->used_hpages -= delta; 2281c5ecae3SMike Kravetz 22909a95e29SMike Kravetz /* minimum size accounting */ 23009a95e29SMike Kravetz if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) { 2311c5ecae3SMike Kravetz if (spool->rsv_hpages + delta <= spool->min_hpages) 2321c5ecae3SMike Kravetz ret = 0; 2331c5ecae3SMike Kravetz else 2341c5ecae3SMike Kravetz ret = spool->rsv_hpages + delta - spool->min_hpages; 2351c5ecae3SMike Kravetz 2361c5ecae3SMike Kravetz spool->rsv_hpages += delta; 2371c5ecae3SMike Kravetz if (spool->rsv_hpages > spool->min_hpages) 2381c5ecae3SMike Kravetz spool->rsv_hpages = spool->min_hpages; 2391c5ecae3SMike Kravetz } 2401c5ecae3SMike Kravetz 2411c5ecae3SMike Kravetz /* 2421c5ecae3SMike Kravetz * If hugetlbfs_put_super couldn't free spool due to an outstanding 2431c5ecae3SMike Kravetz * quota reference, free it now. 2441c5ecae3SMike Kravetz */ 245db71ef79SMike Kravetz unlock_or_release_subpool(spool, flags); 2461c5ecae3SMike Kravetz 2471c5ecae3SMike Kravetz return ret; 24890481622SDavid Gibson } 24990481622SDavid Gibson 25090481622SDavid Gibson static inline struct hugepage_subpool *subpool_inode(struct inode *inode) 25190481622SDavid Gibson { 25290481622SDavid Gibson return HUGETLBFS_SB(inode->i_sb)->spool; 25390481622SDavid Gibson } 25490481622SDavid Gibson 25590481622SDavid Gibson static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma) 25690481622SDavid Gibson { 257496ad9aaSAl Viro return subpool_inode(file_inode(vma->vm_file)); 25890481622SDavid Gibson } 25990481622SDavid Gibson 260e700898fSMike Kravetz /* 261e700898fSMike Kravetz * hugetlb vma_lock helper routines 262e700898fSMike Kravetz */ 263e700898fSMike Kravetz void hugetlb_vma_lock_read(struct vm_area_struct *vma) 264e700898fSMike Kravetz { 265e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 266e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 267e700898fSMike Kravetz 268e700898fSMike Kravetz down_read(&vma_lock->rw_sema); 269e700898fSMike Kravetz } 270e700898fSMike Kravetz } 271e700898fSMike Kravetz 272e700898fSMike Kravetz void hugetlb_vma_unlock_read(struct vm_area_struct *vma) 273e700898fSMike Kravetz { 274e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 275e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 276e700898fSMike Kravetz 277e700898fSMike Kravetz up_read(&vma_lock->rw_sema); 278e700898fSMike Kravetz } 279e700898fSMike Kravetz } 280e700898fSMike Kravetz 281e700898fSMike Kravetz void hugetlb_vma_lock_write(struct vm_area_struct *vma) 282e700898fSMike Kravetz { 283e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 284e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 285e700898fSMike Kravetz 286e700898fSMike Kravetz down_write(&vma_lock->rw_sema); 287e700898fSMike Kravetz } 288e700898fSMike Kravetz } 289e700898fSMike Kravetz 290e700898fSMike Kravetz void hugetlb_vma_unlock_write(struct vm_area_struct *vma) 291e700898fSMike Kravetz { 292e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 293e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 294e700898fSMike Kravetz 295e700898fSMike Kravetz up_write(&vma_lock->rw_sema); 296e700898fSMike Kravetz } 297e700898fSMike Kravetz } 298e700898fSMike Kravetz 299e700898fSMike Kravetz int hugetlb_vma_trylock_write(struct vm_area_struct *vma) 300e700898fSMike Kravetz { 301e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 302e700898fSMike Kravetz 303e700898fSMike Kravetz if (!__vma_shareable_lock(vma)) 304e700898fSMike Kravetz return 1; 305e700898fSMike Kravetz 306e700898fSMike Kravetz return down_write_trylock(&vma_lock->rw_sema); 307e700898fSMike Kravetz } 308e700898fSMike Kravetz 309e700898fSMike Kravetz void hugetlb_vma_assert_locked(struct vm_area_struct *vma) 310e700898fSMike Kravetz { 311e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 312e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 313e700898fSMike Kravetz 314e700898fSMike Kravetz lockdep_assert_held(&vma_lock->rw_sema); 315e700898fSMike Kravetz } 316e700898fSMike Kravetz } 317e700898fSMike Kravetz 318e700898fSMike Kravetz void hugetlb_vma_lock_release(struct kref *kref) 319e700898fSMike Kravetz { 320e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = container_of(kref, 321e700898fSMike Kravetz struct hugetlb_vma_lock, refs); 322e700898fSMike Kravetz 323e700898fSMike Kravetz kfree(vma_lock); 324e700898fSMike Kravetz } 325e700898fSMike Kravetz 326e700898fSMike Kravetz static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock) 327e700898fSMike Kravetz { 328e700898fSMike Kravetz struct vm_area_struct *vma = vma_lock->vma; 329e700898fSMike Kravetz 330e700898fSMike Kravetz /* 331e700898fSMike Kravetz * vma_lock structure may or not be released as a result of put, 332e700898fSMike Kravetz * it certainly will no longer be attached to vma so clear pointer. 333e700898fSMike Kravetz * Semaphore synchronizes access to vma_lock->vma field. 334e700898fSMike Kravetz */ 335e700898fSMike Kravetz vma_lock->vma = NULL; 336e700898fSMike Kravetz vma->vm_private_data = NULL; 337e700898fSMike Kravetz up_write(&vma_lock->rw_sema); 338e700898fSMike Kravetz kref_put(&vma_lock->refs, hugetlb_vma_lock_release); 339e700898fSMike Kravetz } 340e700898fSMike Kravetz 341e700898fSMike Kravetz static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma) 342e700898fSMike Kravetz { 343e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 344e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 345e700898fSMike Kravetz 346e700898fSMike Kravetz __hugetlb_vma_unlock_write_put(vma_lock); 347e700898fSMike Kravetz } 348e700898fSMike Kravetz } 349e700898fSMike Kravetz 350e700898fSMike Kravetz static void hugetlb_vma_lock_free(struct vm_area_struct *vma) 351e700898fSMike Kravetz { 352e700898fSMike Kravetz /* 353e700898fSMike Kravetz * Only present in sharable vmas. 354e700898fSMike Kravetz */ 355e700898fSMike Kravetz if (!vma || !__vma_shareable_lock(vma)) 356e700898fSMike Kravetz return; 357e700898fSMike Kravetz 358e700898fSMike Kravetz if (vma->vm_private_data) { 359e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 360e700898fSMike Kravetz 361e700898fSMike Kravetz down_write(&vma_lock->rw_sema); 362e700898fSMike Kravetz __hugetlb_vma_unlock_write_put(vma_lock); 363e700898fSMike Kravetz } 364e700898fSMike Kravetz } 365e700898fSMike Kravetz 366e700898fSMike Kravetz static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma) 367e700898fSMike Kravetz { 368e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock; 369e700898fSMike Kravetz 370e700898fSMike Kravetz /* Only establish in (flags) sharable vmas */ 371e700898fSMike Kravetz if (!vma || !(vma->vm_flags & VM_MAYSHARE)) 372e700898fSMike Kravetz return; 373e700898fSMike Kravetz 374e700898fSMike Kravetz /* Should never get here with non-NULL vm_private_data */ 375e700898fSMike Kravetz if (vma->vm_private_data) 376e700898fSMike Kravetz return; 377e700898fSMike Kravetz 378e700898fSMike Kravetz vma_lock = kmalloc(sizeof(*vma_lock), GFP_KERNEL); 379e700898fSMike Kravetz if (!vma_lock) { 380e700898fSMike Kravetz /* 381e700898fSMike Kravetz * If we can not allocate structure, then vma can not 382e700898fSMike Kravetz * participate in pmd sharing. This is only a possible 383e700898fSMike Kravetz * performance enhancement and memory saving issue. 384e700898fSMike Kravetz * However, the lock is also used to synchronize page 385e700898fSMike Kravetz * faults with truncation. If the lock is not present, 386e700898fSMike Kravetz * unlikely races could leave pages in a file past i_size 387e700898fSMike Kravetz * until the file is removed. Warn in the unlikely case of 388e700898fSMike Kravetz * allocation failure. 389e700898fSMike Kravetz */ 390e700898fSMike Kravetz pr_warn_once("HugeTLB: unable to allocate vma specific lock\n"); 391e700898fSMike Kravetz return; 392e700898fSMike Kravetz } 393e700898fSMike Kravetz 394e700898fSMike Kravetz kref_init(&vma_lock->refs); 395e700898fSMike Kravetz init_rwsem(&vma_lock->rw_sema); 396e700898fSMike Kravetz vma_lock->vma = vma; 397e700898fSMike Kravetz vma->vm_private_data = vma_lock; 398e700898fSMike Kravetz } 399e700898fSMike Kravetz 4000db9d74eSMina Almasry /* Helper that removes a struct file_region from the resv_map cache and returns 4010db9d74eSMina Almasry * it for use. 4020db9d74eSMina Almasry */ 4030db9d74eSMina Almasry static struct file_region * 4040db9d74eSMina Almasry get_file_region_entry_from_cache(struct resv_map *resv, long from, long to) 4050db9d74eSMina Almasry { 4063259914fSXU pengfei struct file_region *nrg; 4070db9d74eSMina Almasry 4080db9d74eSMina Almasry VM_BUG_ON(resv->region_cache_count <= 0); 4090db9d74eSMina Almasry 4100db9d74eSMina Almasry resv->region_cache_count--; 4110db9d74eSMina Almasry nrg = list_first_entry(&resv->region_cache, struct file_region, link); 4120db9d74eSMina Almasry list_del(&nrg->link); 4130db9d74eSMina Almasry 4140db9d74eSMina Almasry nrg->from = from; 4150db9d74eSMina Almasry nrg->to = to; 4160db9d74eSMina Almasry 4170db9d74eSMina Almasry return nrg; 4180db9d74eSMina Almasry } 4190db9d74eSMina Almasry 420075a61d0SMina Almasry static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg, 421075a61d0SMina Almasry struct file_region *rg) 422075a61d0SMina Almasry { 423075a61d0SMina Almasry #ifdef CONFIG_CGROUP_HUGETLB 424075a61d0SMina Almasry nrg->reservation_counter = rg->reservation_counter; 425075a61d0SMina Almasry nrg->css = rg->css; 426075a61d0SMina Almasry if (rg->css) 427075a61d0SMina Almasry css_get(rg->css); 428075a61d0SMina Almasry #endif 429075a61d0SMina Almasry } 430075a61d0SMina Almasry 431075a61d0SMina Almasry /* Helper that records hugetlb_cgroup uncharge info. */ 432075a61d0SMina Almasry static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg, 433075a61d0SMina Almasry struct hstate *h, 434075a61d0SMina Almasry struct resv_map *resv, 435075a61d0SMina Almasry struct file_region *nrg) 436075a61d0SMina Almasry { 437075a61d0SMina Almasry #ifdef CONFIG_CGROUP_HUGETLB 438075a61d0SMina Almasry if (h_cg) { 439075a61d0SMina Almasry nrg->reservation_counter = 440075a61d0SMina Almasry &h_cg->rsvd_hugepage[hstate_index(h)]; 441075a61d0SMina Almasry nrg->css = &h_cg->css; 442d85aecf2SMiaohe Lin /* 443d85aecf2SMiaohe Lin * The caller will hold exactly one h_cg->css reference for the 444d85aecf2SMiaohe Lin * whole contiguous reservation region. But this area might be 445d85aecf2SMiaohe Lin * scattered when there are already some file_regions reside in 446d85aecf2SMiaohe Lin * it. As a result, many file_regions may share only one css 447d85aecf2SMiaohe Lin * reference. In order to ensure that one file_region must hold 448d85aecf2SMiaohe Lin * exactly one h_cg->css reference, we should do css_get for 449d85aecf2SMiaohe Lin * each file_region and leave the reference held by caller 450d85aecf2SMiaohe Lin * untouched. 451d85aecf2SMiaohe Lin */ 452d85aecf2SMiaohe Lin css_get(&h_cg->css); 453075a61d0SMina Almasry if (!resv->pages_per_hpage) 454075a61d0SMina Almasry resv->pages_per_hpage = pages_per_huge_page(h); 455075a61d0SMina Almasry /* pages_per_hpage should be the same for all entries in 456075a61d0SMina Almasry * a resv_map. 457075a61d0SMina Almasry */ 458075a61d0SMina Almasry VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h)); 459075a61d0SMina Almasry } else { 460075a61d0SMina Almasry nrg->reservation_counter = NULL; 461075a61d0SMina Almasry nrg->css = NULL; 462075a61d0SMina Almasry } 463075a61d0SMina Almasry #endif 464075a61d0SMina Almasry } 465075a61d0SMina Almasry 466d85aecf2SMiaohe Lin static void put_uncharge_info(struct file_region *rg) 467d85aecf2SMiaohe Lin { 468d85aecf2SMiaohe Lin #ifdef CONFIG_CGROUP_HUGETLB 469d85aecf2SMiaohe Lin if (rg->css) 470d85aecf2SMiaohe Lin css_put(rg->css); 471d85aecf2SMiaohe Lin #endif 472d85aecf2SMiaohe Lin } 473d85aecf2SMiaohe Lin 474a9b3f867SMina Almasry static bool has_same_uncharge_info(struct file_region *rg, 475a9b3f867SMina Almasry struct file_region *org) 476a9b3f867SMina Almasry { 477a9b3f867SMina Almasry #ifdef CONFIG_CGROUP_HUGETLB 4780739eb43SBaolin Wang return rg->reservation_counter == org->reservation_counter && 479a9b3f867SMina Almasry rg->css == org->css; 480a9b3f867SMina Almasry 481a9b3f867SMina Almasry #else 482a9b3f867SMina Almasry return true; 483a9b3f867SMina Almasry #endif 484a9b3f867SMina Almasry } 485a9b3f867SMina Almasry 486a9b3f867SMina Almasry static void coalesce_file_region(struct resv_map *resv, struct file_region *rg) 487a9b3f867SMina Almasry { 4883259914fSXU pengfei struct file_region *nrg, *prg; 489a9b3f867SMina Almasry 490a9b3f867SMina Almasry prg = list_prev_entry(rg, link); 491a9b3f867SMina Almasry if (&prg->link != &resv->regions && prg->to == rg->from && 492a9b3f867SMina Almasry has_same_uncharge_info(prg, rg)) { 493a9b3f867SMina Almasry prg->to = rg->to; 494a9b3f867SMina Almasry 495a9b3f867SMina Almasry list_del(&rg->link); 496d85aecf2SMiaohe Lin put_uncharge_info(rg); 497a9b3f867SMina Almasry kfree(rg); 498a9b3f867SMina Almasry 4997db5e7b6SWei Yang rg = prg; 500a9b3f867SMina Almasry } 501a9b3f867SMina Almasry 502a9b3f867SMina Almasry nrg = list_next_entry(rg, link); 503a9b3f867SMina Almasry if (&nrg->link != &resv->regions && nrg->from == rg->to && 504a9b3f867SMina Almasry has_same_uncharge_info(nrg, rg)) { 505a9b3f867SMina Almasry nrg->from = rg->from; 506a9b3f867SMina Almasry 507a9b3f867SMina Almasry list_del(&rg->link); 508d85aecf2SMiaohe Lin put_uncharge_info(rg); 509a9b3f867SMina Almasry kfree(rg); 510a9b3f867SMina Almasry } 511a9b3f867SMina Almasry } 512a9b3f867SMina Almasry 5132103cf9cSPeter Xu static inline long 51484448c8eSJakob Koschel hugetlb_resv_map_add(struct resv_map *map, struct list_head *rg, long from, 5152103cf9cSPeter Xu long to, struct hstate *h, struct hugetlb_cgroup *cg, 5162103cf9cSPeter Xu long *regions_needed) 5172103cf9cSPeter Xu { 5182103cf9cSPeter Xu struct file_region *nrg; 5192103cf9cSPeter Xu 5202103cf9cSPeter Xu if (!regions_needed) { 5212103cf9cSPeter Xu nrg = get_file_region_entry_from_cache(map, from, to); 5222103cf9cSPeter Xu record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg); 52384448c8eSJakob Koschel list_add(&nrg->link, rg); 5242103cf9cSPeter Xu coalesce_file_region(map, nrg); 5252103cf9cSPeter Xu } else 5262103cf9cSPeter Xu *regions_needed += 1; 5272103cf9cSPeter Xu 5282103cf9cSPeter Xu return to - from; 5292103cf9cSPeter Xu } 5302103cf9cSPeter Xu 531972a3da3SWei Yang /* 532972a3da3SWei Yang * Must be called with resv->lock held. 533972a3da3SWei Yang * 534972a3da3SWei Yang * Calling this with regions_needed != NULL will count the number of pages 535972a3da3SWei Yang * to be added but will not modify the linked list. And regions_needed will 536972a3da3SWei Yang * indicate the number of file_regions needed in the cache to carry out to add 537972a3da3SWei Yang * the regions for this range. 538d75c6af9SMina Almasry */ 539d75c6af9SMina Almasry static long add_reservation_in_range(struct resv_map *resv, long f, long t, 540075a61d0SMina Almasry struct hugetlb_cgroup *h_cg, 541972a3da3SWei Yang struct hstate *h, long *regions_needed) 542d75c6af9SMina Almasry { 5430db9d74eSMina Almasry long add = 0; 544d75c6af9SMina Almasry struct list_head *head = &resv->regions; 5450db9d74eSMina Almasry long last_accounted_offset = f; 54684448c8eSJakob Koschel struct file_region *iter, *trg = NULL; 54784448c8eSJakob Koschel struct list_head *rg = NULL; 548d75c6af9SMina Almasry 5490db9d74eSMina Almasry if (regions_needed) 5500db9d74eSMina Almasry *regions_needed = 0; 551d75c6af9SMina Almasry 5520db9d74eSMina Almasry /* In this loop, we essentially handle an entry for the range 55384448c8eSJakob Koschel * [last_accounted_offset, iter->from), at every iteration, with some 5540db9d74eSMina Almasry * bounds checking. 5550db9d74eSMina Almasry */ 55684448c8eSJakob Koschel list_for_each_entry_safe(iter, trg, head, link) { 5570db9d74eSMina Almasry /* Skip irrelevant regions that start before our range. */ 55884448c8eSJakob Koschel if (iter->from < f) { 5590db9d74eSMina Almasry /* If this region ends after the last accounted offset, 5600db9d74eSMina Almasry * then we need to update last_accounted_offset. 5610db9d74eSMina Almasry */ 56284448c8eSJakob Koschel if (iter->to > last_accounted_offset) 56384448c8eSJakob Koschel last_accounted_offset = iter->to; 5640db9d74eSMina Almasry continue; 5650db9d74eSMina Almasry } 566d75c6af9SMina Almasry 5670db9d74eSMina Almasry /* When we find a region that starts beyond our range, we've 5680db9d74eSMina Almasry * finished. 5690db9d74eSMina Almasry */ 57084448c8eSJakob Koschel if (iter->from >= t) { 57184448c8eSJakob Koschel rg = iter->link.prev; 572d75c6af9SMina Almasry break; 57384448c8eSJakob Koschel } 574d75c6af9SMina Almasry 57584448c8eSJakob Koschel /* Add an entry for last_accounted_offset -> iter->from, and 5760db9d74eSMina Almasry * update last_accounted_offset. 577d75c6af9SMina Almasry */ 57884448c8eSJakob Koschel if (iter->from > last_accounted_offset) 57984448c8eSJakob Koschel add += hugetlb_resv_map_add(resv, iter->link.prev, 5802103cf9cSPeter Xu last_accounted_offset, 58184448c8eSJakob Koschel iter->from, h, h_cg, 5822103cf9cSPeter Xu regions_needed); 583d75c6af9SMina Almasry 58484448c8eSJakob Koschel last_accounted_offset = iter->to; 5850db9d74eSMina Almasry } 5860db9d74eSMina Almasry 5870db9d74eSMina Almasry /* Handle the case where our range extends beyond 5880db9d74eSMina Almasry * last_accounted_offset. 5890db9d74eSMina Almasry */ 59084448c8eSJakob Koschel if (!rg) 59184448c8eSJakob Koschel rg = head->prev; 5922103cf9cSPeter Xu if (last_accounted_offset < t) 5932103cf9cSPeter Xu add += hugetlb_resv_map_add(resv, rg, last_accounted_offset, 5942103cf9cSPeter Xu t, h, h_cg, regions_needed); 5950db9d74eSMina Almasry 5960db9d74eSMina Almasry return add; 5970db9d74eSMina Almasry } 5980db9d74eSMina Almasry 5990db9d74eSMina Almasry /* Must be called with resv->lock acquired. Will drop lock to allocate entries. 6000db9d74eSMina Almasry */ 6010db9d74eSMina Almasry static int allocate_file_region_entries(struct resv_map *resv, 6020db9d74eSMina Almasry int regions_needed) 6030db9d74eSMina Almasry __must_hold(&resv->lock) 6040db9d74eSMina Almasry { 60534665341SMiaohe Lin LIST_HEAD(allocated_regions); 6060db9d74eSMina Almasry int to_allocate = 0, i = 0; 6070db9d74eSMina Almasry struct file_region *trg = NULL, *rg = NULL; 6080db9d74eSMina Almasry 6090db9d74eSMina Almasry VM_BUG_ON(regions_needed < 0); 6100db9d74eSMina Almasry 6110db9d74eSMina Almasry /* 6120db9d74eSMina Almasry * Check for sufficient descriptors in the cache to accommodate 6130db9d74eSMina Almasry * the number of in progress add operations plus regions_needed. 6140db9d74eSMina Almasry * 6150db9d74eSMina Almasry * This is a while loop because when we drop the lock, some other call 6160db9d74eSMina Almasry * to region_add or region_del may have consumed some region_entries, 6170db9d74eSMina Almasry * so we keep looping here until we finally have enough entries for 6180db9d74eSMina Almasry * (adds_in_progress + regions_needed). 6190db9d74eSMina Almasry */ 6200db9d74eSMina Almasry while (resv->region_cache_count < 6210db9d74eSMina Almasry (resv->adds_in_progress + regions_needed)) { 6220db9d74eSMina Almasry to_allocate = resv->adds_in_progress + regions_needed - 6230db9d74eSMina Almasry resv->region_cache_count; 6240db9d74eSMina Almasry 6250db9d74eSMina Almasry /* At this point, we should have enough entries in the cache 626f0953a1bSIngo Molnar * for all the existing adds_in_progress. We should only be 6270db9d74eSMina Almasry * needing to allocate for regions_needed. 6280db9d74eSMina Almasry */ 6290db9d74eSMina Almasry VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress); 6300db9d74eSMina Almasry 6310db9d74eSMina Almasry spin_unlock(&resv->lock); 6320db9d74eSMina Almasry for (i = 0; i < to_allocate; i++) { 6330db9d74eSMina Almasry trg = kmalloc(sizeof(*trg), GFP_KERNEL); 6340db9d74eSMina Almasry if (!trg) 6350db9d74eSMina Almasry goto out_of_memory; 6360db9d74eSMina Almasry list_add(&trg->link, &allocated_regions); 6370db9d74eSMina Almasry } 6380db9d74eSMina Almasry 6390db9d74eSMina Almasry spin_lock(&resv->lock); 6400db9d74eSMina Almasry 641d3ec7b6eSWei Yang list_splice(&allocated_regions, &resv->region_cache); 642d3ec7b6eSWei Yang resv->region_cache_count += to_allocate; 6430db9d74eSMina Almasry } 6440db9d74eSMina Almasry 6450db9d74eSMina Almasry return 0; 6460db9d74eSMina Almasry 6470db9d74eSMina Almasry out_of_memory: 6480db9d74eSMina Almasry list_for_each_entry_safe(rg, trg, &allocated_regions, link) { 649d75c6af9SMina Almasry list_del(&rg->link); 650d75c6af9SMina Almasry kfree(rg); 651d75c6af9SMina Almasry } 6520db9d74eSMina Almasry return -ENOMEM; 653d75c6af9SMina Almasry } 654d75c6af9SMina Almasry 6551dd308a7SMike Kravetz /* 6561dd308a7SMike Kravetz * Add the huge page range represented by [f, t) to the reserve 6570db9d74eSMina Almasry * map. Regions will be taken from the cache to fill in this range. 6580db9d74eSMina Almasry * Sufficient regions should exist in the cache due to the previous 6590db9d74eSMina Almasry * call to region_chg with the same range, but in some cases the cache will not 6600db9d74eSMina Almasry * have sufficient entries due to races with other code doing region_add or 6610db9d74eSMina Almasry * region_del. The extra needed entries will be allocated. 662cf3ad20bSMike Kravetz * 6630db9d74eSMina Almasry * regions_needed is the out value provided by a previous call to region_chg. 6640db9d74eSMina Almasry * 6650db9d74eSMina Almasry * Return the number of new huge pages added to the map. This number is greater 6660db9d74eSMina Almasry * than or equal to zero. If file_region entries needed to be allocated for 6677c8de358SEthon Paul * this operation and we were not able to allocate, it returns -ENOMEM. 6680db9d74eSMina Almasry * region_add of regions of length 1 never allocate file_regions and cannot 6690db9d74eSMina Almasry * fail; region_chg will always allocate at least 1 entry and a region_add for 6700db9d74eSMina Almasry * 1 page will only require at most 1 entry. 6711dd308a7SMike Kravetz */ 6720db9d74eSMina Almasry static long region_add(struct resv_map *resv, long f, long t, 673075a61d0SMina Almasry long in_regions_needed, struct hstate *h, 674075a61d0SMina Almasry struct hugetlb_cgroup *h_cg) 67596822904SAndy Whitcroft { 6760db9d74eSMina Almasry long add = 0, actual_regions_needed = 0; 67796822904SAndy Whitcroft 6787b24d861SDavidlohr Bueso spin_lock(&resv->lock); 6790db9d74eSMina Almasry retry: 6800db9d74eSMina Almasry 6810db9d74eSMina Almasry /* Count how many regions are actually needed to execute this add. */ 682972a3da3SWei Yang add_reservation_in_range(resv, f, t, NULL, NULL, 683972a3da3SWei Yang &actual_regions_needed); 68496822904SAndy Whitcroft 6855e911373SMike Kravetz /* 6860db9d74eSMina Almasry * Check for sufficient descriptors in the cache to accommodate 6870db9d74eSMina Almasry * this add operation. Note that actual_regions_needed may be greater 6880db9d74eSMina Almasry * than in_regions_needed, as the resv_map may have been modified since 6890db9d74eSMina Almasry * the region_chg call. In this case, we need to make sure that we 6900db9d74eSMina Almasry * allocate extra entries, such that we have enough for all the 6910db9d74eSMina Almasry * existing adds_in_progress, plus the excess needed for this 6920db9d74eSMina Almasry * operation. 6935e911373SMike Kravetz */ 6940db9d74eSMina Almasry if (actual_regions_needed > in_regions_needed && 6950db9d74eSMina Almasry resv->region_cache_count < 6960db9d74eSMina Almasry resv->adds_in_progress + 6970db9d74eSMina Almasry (actual_regions_needed - in_regions_needed)) { 6980db9d74eSMina Almasry /* region_add operation of range 1 should never need to 6990db9d74eSMina Almasry * allocate file_region entries. 7000db9d74eSMina Almasry */ 7010db9d74eSMina Almasry VM_BUG_ON(t - f <= 1); 7025e911373SMike Kravetz 7030db9d74eSMina Almasry if (allocate_file_region_entries( 7040db9d74eSMina Almasry resv, actual_regions_needed - in_regions_needed)) { 7050db9d74eSMina Almasry return -ENOMEM; 7065e911373SMike Kravetz } 7075e911373SMike Kravetz 7080db9d74eSMina Almasry goto retry; 7090db9d74eSMina Almasry } 710cf3ad20bSMike Kravetz 711972a3da3SWei Yang add = add_reservation_in_range(resv, f, t, h_cg, h, NULL); 7120db9d74eSMina Almasry 7130db9d74eSMina Almasry resv->adds_in_progress -= in_regions_needed; 7140db9d74eSMina Almasry 7157b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 716cf3ad20bSMike Kravetz return add; 71796822904SAndy Whitcroft } 71896822904SAndy Whitcroft 7191dd308a7SMike Kravetz /* 7201dd308a7SMike Kravetz * Examine the existing reserve map and determine how many 7211dd308a7SMike Kravetz * huge pages in the specified range [f, t) are NOT currently 7221dd308a7SMike Kravetz * represented. This routine is called before a subsequent 7231dd308a7SMike Kravetz * call to region_add that will actually modify the reserve 7241dd308a7SMike Kravetz * map to add the specified range [f, t). region_chg does 7251dd308a7SMike Kravetz * not change the number of huge pages represented by the 7260db9d74eSMina Almasry * map. A number of new file_region structures is added to the cache as a 7270db9d74eSMina Almasry * placeholder, for the subsequent region_add call to use. At least 1 7280db9d74eSMina Almasry * file_region structure is added. 7290db9d74eSMina Almasry * 7300db9d74eSMina Almasry * out_regions_needed is the number of regions added to the 7310db9d74eSMina Almasry * resv->adds_in_progress. This value needs to be provided to a follow up call 7320db9d74eSMina Almasry * to region_add or region_abort for proper accounting. 7335e911373SMike Kravetz * 7345e911373SMike Kravetz * Returns the number of huge pages that need to be added to the existing 7355e911373SMike Kravetz * reservation map for the range [f, t). This number is greater or equal to 7365e911373SMike Kravetz * zero. -ENOMEM is returned if a new file_region structure or cache entry 7375e911373SMike Kravetz * is needed and can not be allocated. 7381dd308a7SMike Kravetz */ 7390db9d74eSMina Almasry static long region_chg(struct resv_map *resv, long f, long t, 7400db9d74eSMina Almasry long *out_regions_needed) 74196822904SAndy Whitcroft { 74296822904SAndy Whitcroft long chg = 0; 74396822904SAndy Whitcroft 7447b24d861SDavidlohr Bueso spin_lock(&resv->lock); 7455e911373SMike Kravetz 746972a3da3SWei Yang /* Count how many hugepages in this range are NOT represented. */ 747075a61d0SMina Almasry chg = add_reservation_in_range(resv, f, t, NULL, NULL, 748972a3da3SWei Yang out_regions_needed); 7495e911373SMike Kravetz 7500db9d74eSMina Almasry if (*out_regions_needed == 0) 7510db9d74eSMina Almasry *out_regions_needed = 1; 7525e911373SMike Kravetz 7530db9d74eSMina Almasry if (allocate_file_region_entries(resv, *out_regions_needed)) 7545e911373SMike Kravetz return -ENOMEM; 7555e911373SMike Kravetz 7560db9d74eSMina Almasry resv->adds_in_progress += *out_regions_needed; 75796822904SAndy Whitcroft 7587b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 75996822904SAndy Whitcroft return chg; 76096822904SAndy Whitcroft } 76196822904SAndy Whitcroft 7621dd308a7SMike Kravetz /* 7635e911373SMike Kravetz * Abort the in progress add operation. The adds_in_progress field 7645e911373SMike Kravetz * of the resv_map keeps track of the operations in progress between 7655e911373SMike Kravetz * calls to region_chg and region_add. Operations are sometimes 7665e911373SMike Kravetz * aborted after the call to region_chg. In such cases, region_abort 7670db9d74eSMina Almasry * is called to decrement the adds_in_progress counter. regions_needed 7680db9d74eSMina Almasry * is the value returned by the region_chg call, it is used to decrement 7690db9d74eSMina Almasry * the adds_in_progress counter. 7705e911373SMike Kravetz * 7715e911373SMike Kravetz * NOTE: The range arguments [f, t) are not needed or used in this 7725e911373SMike Kravetz * routine. They are kept to make reading the calling code easier as 7735e911373SMike Kravetz * arguments will match the associated region_chg call. 7745e911373SMike Kravetz */ 7750db9d74eSMina Almasry static void region_abort(struct resv_map *resv, long f, long t, 7760db9d74eSMina Almasry long regions_needed) 7775e911373SMike Kravetz { 7785e911373SMike Kravetz spin_lock(&resv->lock); 7795e911373SMike Kravetz VM_BUG_ON(!resv->region_cache_count); 7800db9d74eSMina Almasry resv->adds_in_progress -= regions_needed; 7815e911373SMike Kravetz spin_unlock(&resv->lock); 7825e911373SMike Kravetz } 7835e911373SMike Kravetz 7845e911373SMike Kravetz /* 785feba16e2SMike Kravetz * Delete the specified range [f, t) from the reserve map. If the 786feba16e2SMike Kravetz * t parameter is LONG_MAX, this indicates that ALL regions after f 787feba16e2SMike Kravetz * should be deleted. Locate the regions which intersect [f, t) 788feba16e2SMike Kravetz * and either trim, delete or split the existing regions. 789feba16e2SMike Kravetz * 790feba16e2SMike Kravetz * Returns the number of huge pages deleted from the reserve map. 791feba16e2SMike Kravetz * In the normal case, the return value is zero or more. In the 792feba16e2SMike Kravetz * case where a region must be split, a new region descriptor must 793feba16e2SMike Kravetz * be allocated. If the allocation fails, -ENOMEM will be returned. 794feba16e2SMike Kravetz * NOTE: If the parameter t == LONG_MAX, then we will never split 795feba16e2SMike Kravetz * a region and possibly return -ENOMEM. Callers specifying 796feba16e2SMike Kravetz * t == LONG_MAX do not need to check for -ENOMEM error. 7971dd308a7SMike Kravetz */ 798feba16e2SMike Kravetz static long region_del(struct resv_map *resv, long f, long t) 79996822904SAndy Whitcroft { 8001406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 80196822904SAndy Whitcroft struct file_region *rg, *trg; 802feba16e2SMike Kravetz struct file_region *nrg = NULL; 803feba16e2SMike Kravetz long del = 0; 80496822904SAndy Whitcroft 805feba16e2SMike Kravetz retry: 8067b24d861SDavidlohr Bueso spin_lock(&resv->lock); 807feba16e2SMike Kravetz list_for_each_entry_safe(rg, trg, head, link) { 808dbe409e4SMike Kravetz /* 809dbe409e4SMike Kravetz * Skip regions before the range to be deleted. file_region 810dbe409e4SMike Kravetz * ranges are normally of the form [from, to). However, there 811dbe409e4SMike Kravetz * may be a "placeholder" entry in the map which is of the form 812dbe409e4SMike Kravetz * (from, to) with from == to. Check for placeholder entries 813dbe409e4SMike Kravetz * at the beginning of the range to be deleted. 814dbe409e4SMike Kravetz */ 815dbe409e4SMike Kravetz if (rg->to <= f && (rg->to != rg->from || rg->to != f)) 816feba16e2SMike Kravetz continue; 817dbe409e4SMike Kravetz 818feba16e2SMike Kravetz if (rg->from >= t) 81996822904SAndy Whitcroft break; 82096822904SAndy Whitcroft 821feba16e2SMike Kravetz if (f > rg->from && t < rg->to) { /* Must split region */ 822feba16e2SMike Kravetz /* 823feba16e2SMike Kravetz * Check for an entry in the cache before dropping 824feba16e2SMike Kravetz * lock and attempting allocation. 825feba16e2SMike Kravetz */ 826feba16e2SMike Kravetz if (!nrg && 827feba16e2SMike Kravetz resv->region_cache_count > resv->adds_in_progress) { 828feba16e2SMike Kravetz nrg = list_first_entry(&resv->region_cache, 829feba16e2SMike Kravetz struct file_region, 830feba16e2SMike Kravetz link); 831feba16e2SMike Kravetz list_del(&nrg->link); 832feba16e2SMike Kravetz resv->region_cache_count--; 83396822904SAndy Whitcroft } 83496822904SAndy Whitcroft 835feba16e2SMike Kravetz if (!nrg) { 836feba16e2SMike Kravetz spin_unlock(&resv->lock); 837feba16e2SMike Kravetz nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 838feba16e2SMike Kravetz if (!nrg) 839feba16e2SMike Kravetz return -ENOMEM; 840feba16e2SMike Kravetz goto retry; 841feba16e2SMike Kravetz } 842feba16e2SMike Kravetz 843feba16e2SMike Kravetz del += t - f; 84479aa925bSMike Kravetz hugetlb_cgroup_uncharge_file_region( 845d85aecf2SMiaohe Lin resv, rg, t - f, false); 846feba16e2SMike Kravetz 847feba16e2SMike Kravetz /* New entry for end of split region */ 848feba16e2SMike Kravetz nrg->from = t; 849feba16e2SMike Kravetz nrg->to = rg->to; 850075a61d0SMina Almasry 851075a61d0SMina Almasry copy_hugetlb_cgroup_uncharge_info(nrg, rg); 852075a61d0SMina Almasry 853feba16e2SMike Kravetz INIT_LIST_HEAD(&nrg->link); 854feba16e2SMike Kravetz 855feba16e2SMike Kravetz /* Original entry is trimmed */ 856feba16e2SMike Kravetz rg->to = f; 857feba16e2SMike Kravetz 858feba16e2SMike Kravetz list_add(&nrg->link, &rg->link); 859feba16e2SMike Kravetz nrg = NULL; 86096822904SAndy Whitcroft break; 861feba16e2SMike Kravetz } 862feba16e2SMike Kravetz 863feba16e2SMike Kravetz if (f <= rg->from && t >= rg->to) { /* Remove entire region */ 864feba16e2SMike Kravetz del += rg->to - rg->from; 865075a61d0SMina Almasry hugetlb_cgroup_uncharge_file_region(resv, rg, 866d85aecf2SMiaohe Lin rg->to - rg->from, true); 86796822904SAndy Whitcroft list_del(&rg->link); 86896822904SAndy Whitcroft kfree(rg); 869feba16e2SMike Kravetz continue; 87096822904SAndy Whitcroft } 8717b24d861SDavidlohr Bueso 872feba16e2SMike Kravetz if (f <= rg->from) { /* Trim beginning of region */ 873075a61d0SMina Almasry hugetlb_cgroup_uncharge_file_region(resv, rg, 874d85aecf2SMiaohe Lin t - rg->from, false); 875075a61d0SMina Almasry 87679aa925bSMike Kravetz del += t - rg->from; 87779aa925bSMike Kravetz rg->from = t; 87879aa925bSMike Kravetz } else { /* Trim end of region */ 879075a61d0SMina Almasry hugetlb_cgroup_uncharge_file_region(resv, rg, 880d85aecf2SMiaohe Lin rg->to - f, false); 88179aa925bSMike Kravetz 88279aa925bSMike Kravetz del += rg->to - f; 88379aa925bSMike Kravetz rg->to = f; 884feba16e2SMike Kravetz } 885feba16e2SMike Kravetz } 886feba16e2SMike Kravetz 8877b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 888feba16e2SMike Kravetz kfree(nrg); 889feba16e2SMike Kravetz return del; 89096822904SAndy Whitcroft } 89196822904SAndy Whitcroft 8921dd308a7SMike Kravetz /* 893b5cec28dSMike Kravetz * A rare out of memory error was encountered which prevented removal of 894b5cec28dSMike Kravetz * the reserve map region for a page. The huge page itself was free'ed 895b5cec28dSMike Kravetz * and removed from the page cache. This routine will adjust the subpool 896b5cec28dSMike Kravetz * usage count, and the global reserve count if needed. By incrementing 897b5cec28dSMike Kravetz * these counts, the reserve map entry which could not be deleted will 898b5cec28dSMike Kravetz * appear as a "reserved" entry instead of simply dangling with incorrect 899b5cec28dSMike Kravetz * counts. 900b5cec28dSMike Kravetz */ 90172e2936cSzhong jiang void hugetlb_fix_reserve_counts(struct inode *inode) 902b5cec28dSMike Kravetz { 903b5cec28dSMike Kravetz struct hugepage_subpool *spool = subpool_inode(inode); 904b5cec28dSMike Kravetz long rsv_adjust; 905da56388cSMiaohe Lin bool reserved = false; 906b5cec28dSMike Kravetz 907b5cec28dSMike Kravetz rsv_adjust = hugepage_subpool_get_pages(spool, 1); 908da56388cSMiaohe Lin if (rsv_adjust > 0) { 909b5cec28dSMike Kravetz struct hstate *h = hstate_inode(inode); 910b5cec28dSMike Kravetz 911da56388cSMiaohe Lin if (!hugetlb_acct_memory(h, 1)) 912da56388cSMiaohe Lin reserved = true; 913da56388cSMiaohe Lin } else if (!rsv_adjust) { 914da56388cSMiaohe Lin reserved = true; 915b5cec28dSMike Kravetz } 916da56388cSMiaohe Lin 917da56388cSMiaohe Lin if (!reserved) 918da56388cSMiaohe Lin pr_warn("hugetlb: Huge Page Reserved count may go negative.\n"); 919b5cec28dSMike Kravetz } 920b5cec28dSMike Kravetz 921b5cec28dSMike Kravetz /* 9221dd308a7SMike Kravetz * Count and return the number of huge pages in the reserve map 9231dd308a7SMike Kravetz * that intersect with the range [f, t). 9241dd308a7SMike Kravetz */ 9251406ec9bSJoonsoo Kim static long region_count(struct resv_map *resv, long f, long t) 92684afd99bSAndy Whitcroft { 9271406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 92884afd99bSAndy Whitcroft struct file_region *rg; 92984afd99bSAndy Whitcroft long chg = 0; 93084afd99bSAndy Whitcroft 9317b24d861SDavidlohr Bueso spin_lock(&resv->lock); 93284afd99bSAndy Whitcroft /* Locate each segment we overlap with, and count that overlap. */ 93384afd99bSAndy Whitcroft list_for_each_entry(rg, head, link) { 934f2135a4aSWang Sheng-Hui long seg_from; 935f2135a4aSWang Sheng-Hui long seg_to; 93684afd99bSAndy Whitcroft 93784afd99bSAndy Whitcroft if (rg->to <= f) 93884afd99bSAndy Whitcroft continue; 93984afd99bSAndy Whitcroft if (rg->from >= t) 94084afd99bSAndy Whitcroft break; 94184afd99bSAndy Whitcroft 94284afd99bSAndy Whitcroft seg_from = max(rg->from, f); 94384afd99bSAndy Whitcroft seg_to = min(rg->to, t); 94484afd99bSAndy Whitcroft 94584afd99bSAndy Whitcroft chg += seg_to - seg_from; 94684afd99bSAndy Whitcroft } 9477b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 94884afd99bSAndy Whitcroft 94984afd99bSAndy Whitcroft return chg; 95084afd99bSAndy Whitcroft } 95184afd99bSAndy Whitcroft 95296822904SAndy Whitcroft /* 953e7c4b0bfSAndy Whitcroft * Convert the address within this vma to the page offset within 954e7c4b0bfSAndy Whitcroft * the mapping, in pagecache page units; huge pages here. 955e7c4b0bfSAndy Whitcroft */ 956a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h, 957a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 958e7c4b0bfSAndy Whitcroft { 959a5516438SAndi Kleen return ((address - vma->vm_start) >> huge_page_shift(h)) + 960a5516438SAndi Kleen (vma->vm_pgoff >> huge_page_order(h)); 961e7c4b0bfSAndy Whitcroft } 962e7c4b0bfSAndy Whitcroft 9630fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma, 9640fe6e20bSNaoya Horiguchi unsigned long address) 9650fe6e20bSNaoya Horiguchi { 9660fe6e20bSNaoya Horiguchi return vma_hugecache_offset(hstate_vma(vma), vma, address); 9670fe6e20bSNaoya Horiguchi } 968dee41079SDan Williams EXPORT_SYMBOL_GPL(linear_hugepage_index); 9690fe6e20bSNaoya Horiguchi 97084afd99bSAndy Whitcroft /* 97108fba699SMel Gorman * Return the size of the pages allocated when backing a VMA. In the majority 97208fba699SMel Gorman * cases this will be same size as used by the page table entries. 97308fba699SMel Gorman */ 97408fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) 97508fba699SMel Gorman { 97605ea8860SDan Williams if (vma->vm_ops && vma->vm_ops->pagesize) 97705ea8860SDan Williams return vma->vm_ops->pagesize(vma); 97808fba699SMel Gorman return PAGE_SIZE; 97908fba699SMel Gorman } 980f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize); 98108fba699SMel Gorman 98208fba699SMel Gorman /* 9833340289dSMel Gorman * Return the page size being used by the MMU to back a VMA. In the majority 9843340289dSMel Gorman * of cases, the page size used by the kernel matches the MMU size. On 98509135cc5SDan Williams * architectures where it differs, an architecture-specific 'strong' 98609135cc5SDan Williams * version of this symbol is required. 9873340289dSMel Gorman */ 98809135cc5SDan Williams __weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) 9893340289dSMel Gorman { 9903340289dSMel Gorman return vma_kernel_pagesize(vma); 9913340289dSMel Gorman } 9923340289dSMel Gorman 9933340289dSMel Gorman /* 99484afd99bSAndy Whitcroft * Flags for MAP_PRIVATE reservations. These are stored in the bottom 99584afd99bSAndy Whitcroft * bits of the reservation map pointer, which are always clear due to 99684afd99bSAndy Whitcroft * alignment. 99784afd99bSAndy Whitcroft */ 99884afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER (1UL << 0) 99984afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1) 100004f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) 100184afd99bSAndy Whitcroft 1002a1e78772SMel Gorman /* 1003a1e78772SMel Gorman * These helpers are used to track how many pages are reserved for 1004a1e78772SMel Gorman * faults in a MAP_PRIVATE mapping. Only the process that called mmap() 1005a1e78772SMel Gorman * is guaranteed to have their future faults succeed. 1006a1e78772SMel Gorman * 10078d9bfb26SMike Kravetz * With the exception of hugetlb_dup_vma_private() which is called at fork(), 1008a1e78772SMel Gorman * the reserve counters are updated with the hugetlb_lock held. It is safe 1009a1e78772SMel Gorman * to reset the VMA at fork() time as it is not in use yet and there is no 1010a1e78772SMel Gorman * chance of the global counters getting corrupted as a result of the values. 101184afd99bSAndy Whitcroft * 101284afd99bSAndy Whitcroft * The private mapping reservation is represented in a subtly different 101384afd99bSAndy Whitcroft * manner to a shared mapping. A shared mapping has a region map associated 101484afd99bSAndy Whitcroft * with the underlying file, this region map represents the backing file 101584afd99bSAndy Whitcroft * pages which have ever had a reservation assigned which this persists even 101684afd99bSAndy Whitcroft * after the page is instantiated. A private mapping has a region map 101784afd99bSAndy Whitcroft * associated with the original mmap which is attached to all VMAs which 101884afd99bSAndy Whitcroft * reference it, this region map represents those offsets which have consumed 101984afd99bSAndy Whitcroft * reservation ie. where pages have been instantiated. 1020a1e78772SMel Gorman */ 1021e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma) 1022e7c4b0bfSAndy Whitcroft { 1023e7c4b0bfSAndy Whitcroft return (unsigned long)vma->vm_private_data; 1024e7c4b0bfSAndy Whitcroft } 1025e7c4b0bfSAndy Whitcroft 1026e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma, 1027e7c4b0bfSAndy Whitcroft unsigned long value) 1028e7c4b0bfSAndy Whitcroft { 1029e7c4b0bfSAndy Whitcroft vma->vm_private_data = (void *)value; 1030e7c4b0bfSAndy Whitcroft } 1031e7c4b0bfSAndy Whitcroft 1032e9fe92aeSMina Almasry static void 1033e9fe92aeSMina Almasry resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map, 1034e9fe92aeSMina Almasry struct hugetlb_cgroup *h_cg, 1035e9fe92aeSMina Almasry struct hstate *h) 1036e9fe92aeSMina Almasry { 1037e9fe92aeSMina Almasry #ifdef CONFIG_CGROUP_HUGETLB 1038e9fe92aeSMina Almasry if (!h_cg || !h) { 1039e9fe92aeSMina Almasry resv_map->reservation_counter = NULL; 1040e9fe92aeSMina Almasry resv_map->pages_per_hpage = 0; 1041e9fe92aeSMina Almasry resv_map->css = NULL; 1042e9fe92aeSMina Almasry } else { 1043e9fe92aeSMina Almasry resv_map->reservation_counter = 1044e9fe92aeSMina Almasry &h_cg->rsvd_hugepage[hstate_index(h)]; 1045e9fe92aeSMina Almasry resv_map->pages_per_hpage = pages_per_huge_page(h); 1046e9fe92aeSMina Almasry resv_map->css = &h_cg->css; 1047e9fe92aeSMina Almasry } 1048e9fe92aeSMina Almasry #endif 1049e9fe92aeSMina Almasry } 1050e9fe92aeSMina Almasry 10519119a41eSJoonsoo Kim struct resv_map *resv_map_alloc(void) 105284afd99bSAndy Whitcroft { 105384afd99bSAndy Whitcroft struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); 10545e911373SMike Kravetz struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL); 10555e911373SMike Kravetz 10565e911373SMike Kravetz if (!resv_map || !rg) { 10575e911373SMike Kravetz kfree(resv_map); 10585e911373SMike Kravetz kfree(rg); 105984afd99bSAndy Whitcroft return NULL; 10605e911373SMike Kravetz } 106184afd99bSAndy Whitcroft 106284afd99bSAndy Whitcroft kref_init(&resv_map->refs); 10637b24d861SDavidlohr Bueso spin_lock_init(&resv_map->lock); 106484afd99bSAndy Whitcroft INIT_LIST_HEAD(&resv_map->regions); 106584afd99bSAndy Whitcroft 10665e911373SMike Kravetz resv_map->adds_in_progress = 0; 1067e9fe92aeSMina Almasry /* 1068e9fe92aeSMina Almasry * Initialize these to 0. On shared mappings, 0's here indicate these 1069e9fe92aeSMina Almasry * fields don't do cgroup accounting. On private mappings, these will be 1070e9fe92aeSMina Almasry * re-initialized to the proper values, to indicate that hugetlb cgroup 1071e9fe92aeSMina Almasry * reservations are to be un-charged from here. 1072e9fe92aeSMina Almasry */ 1073e9fe92aeSMina Almasry resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL); 10745e911373SMike Kravetz 10755e911373SMike Kravetz INIT_LIST_HEAD(&resv_map->region_cache); 10765e911373SMike Kravetz list_add(&rg->link, &resv_map->region_cache); 10775e911373SMike Kravetz resv_map->region_cache_count = 1; 10785e911373SMike Kravetz 107984afd99bSAndy Whitcroft return resv_map; 108084afd99bSAndy Whitcroft } 108184afd99bSAndy Whitcroft 10829119a41eSJoonsoo Kim void resv_map_release(struct kref *ref) 108384afd99bSAndy Whitcroft { 108484afd99bSAndy Whitcroft struct resv_map *resv_map = container_of(ref, struct resv_map, refs); 10855e911373SMike Kravetz struct list_head *head = &resv_map->region_cache; 10865e911373SMike Kravetz struct file_region *rg, *trg; 108784afd99bSAndy Whitcroft 108884afd99bSAndy Whitcroft /* Clear out any active regions before we release the map. */ 1089feba16e2SMike Kravetz region_del(resv_map, 0, LONG_MAX); 10905e911373SMike Kravetz 10915e911373SMike Kravetz /* ... and any entries left in the cache */ 10925e911373SMike Kravetz list_for_each_entry_safe(rg, trg, head, link) { 10935e911373SMike Kravetz list_del(&rg->link); 10945e911373SMike Kravetz kfree(rg); 10955e911373SMike Kravetz } 10965e911373SMike Kravetz 10975e911373SMike Kravetz VM_BUG_ON(resv_map->adds_in_progress); 10985e911373SMike Kravetz 109984afd99bSAndy Whitcroft kfree(resv_map); 110084afd99bSAndy Whitcroft } 110184afd99bSAndy Whitcroft 11024e35f483SJoonsoo Kim static inline struct resv_map *inode_resv_map(struct inode *inode) 11034e35f483SJoonsoo Kim { 1104f27a5136SMike Kravetz /* 1105f27a5136SMike Kravetz * At inode evict time, i_mapping may not point to the original 1106f27a5136SMike Kravetz * address space within the inode. This original address space 1107f27a5136SMike Kravetz * contains the pointer to the resv_map. So, always use the 1108f27a5136SMike Kravetz * address space embedded within the inode. 1109f27a5136SMike Kravetz * The VERY common case is inode->mapping == &inode->i_data but, 1110f27a5136SMike Kravetz * this may not be true for device special inodes. 1111f27a5136SMike Kravetz */ 1112f27a5136SMike Kravetz return (struct resv_map *)(&inode->i_data)->private_data; 11134e35f483SJoonsoo Kim } 11144e35f483SJoonsoo Kim 111584afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma) 1116a1e78772SMel Gorman { 111781d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 11184e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) { 11194e35f483SJoonsoo Kim struct address_space *mapping = vma->vm_file->f_mapping; 11204e35f483SJoonsoo Kim struct inode *inode = mapping->host; 11214e35f483SJoonsoo Kim 11224e35f483SJoonsoo Kim return inode_resv_map(inode); 11234e35f483SJoonsoo Kim 11244e35f483SJoonsoo Kim } else { 112584afd99bSAndy Whitcroft return (struct resv_map *)(get_vma_private_data(vma) & 112684afd99bSAndy Whitcroft ~HPAGE_RESV_MASK); 11274e35f483SJoonsoo Kim } 1128a1e78772SMel Gorman } 1129a1e78772SMel Gorman 113084afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) 1131a1e78772SMel Gorman { 113281d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 113381d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 1134a1e78772SMel Gorman 113584afd99bSAndy Whitcroft set_vma_private_data(vma, (get_vma_private_data(vma) & 113684afd99bSAndy Whitcroft HPAGE_RESV_MASK) | (unsigned long)map); 113704f2cbe3SMel Gorman } 113804f2cbe3SMel Gorman 113904f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) 114004f2cbe3SMel Gorman { 114181d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 114281d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 1143e7c4b0bfSAndy Whitcroft 1144e7c4b0bfSAndy Whitcroft set_vma_private_data(vma, get_vma_private_data(vma) | flags); 114504f2cbe3SMel Gorman } 114604f2cbe3SMel Gorman 114704f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) 114804f2cbe3SMel Gorman { 114981d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 1150e7c4b0bfSAndy Whitcroft 1151e7c4b0bfSAndy Whitcroft return (get_vma_private_data(vma) & flag) != 0; 1152a1e78772SMel Gorman } 1153a1e78772SMel Gorman 11548d9bfb26SMike Kravetz void hugetlb_dup_vma_private(struct vm_area_struct *vma) 1155a1e78772SMel Gorman { 115681d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 11578d9bfb26SMike Kravetz /* 11588d9bfb26SMike Kravetz * Clear vm_private_data 1159612b8a31SMike Kravetz * - For shared mappings this is a per-vma semaphore that may be 1160612b8a31SMike Kravetz * allocated in a subsequent call to hugetlb_vm_op_open. 1161612b8a31SMike Kravetz * Before clearing, make sure pointer is not associated with vma 1162612b8a31SMike Kravetz * as this will leak the structure. This is the case when called 1163612b8a31SMike Kravetz * via clear_vma_resv_huge_pages() and hugetlb_vm_op_open has already 1164612b8a31SMike Kravetz * been called to allocate a new structure. 11658d9bfb26SMike Kravetz * - For MAP_PRIVATE mappings, this is the reserve map which does 11668d9bfb26SMike Kravetz * not apply to children. Faults generated by the children are 11678d9bfb26SMike Kravetz * not guaranteed to succeed, even if read-only. 11688d9bfb26SMike Kravetz */ 1169612b8a31SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 1170612b8a31SMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 1171612b8a31SMike Kravetz 1172612b8a31SMike Kravetz if (vma_lock && vma_lock->vma != vma) 1173612b8a31SMike Kravetz vma->vm_private_data = NULL; 1174612b8a31SMike Kravetz } else 1175612b8a31SMike Kravetz vma->vm_private_data = NULL; 1176a1e78772SMel Gorman } 1177a1e78772SMel Gorman 1178550a7d60SMina Almasry /* 1179550a7d60SMina Almasry * Reset and decrement one ref on hugepage private reservation. 11808651a137SLorenzo Stoakes * Called with mm->mmap_lock writer semaphore held. 1181550a7d60SMina Almasry * This function should be only used by move_vma() and operate on 1182550a7d60SMina Almasry * same sized vma. It should never come here with last ref on the 1183550a7d60SMina Almasry * reservation. 1184550a7d60SMina Almasry */ 1185550a7d60SMina Almasry void clear_vma_resv_huge_pages(struct vm_area_struct *vma) 1186550a7d60SMina Almasry { 1187550a7d60SMina Almasry /* 1188550a7d60SMina Almasry * Clear the old hugetlb private page reservation. 1189550a7d60SMina Almasry * It has already been transferred to new_vma. 1190550a7d60SMina Almasry * 1191550a7d60SMina Almasry * During a mremap() operation of a hugetlb vma we call move_vma() 1192550a7d60SMina Almasry * which copies vma into new_vma and unmaps vma. After the copy 1193550a7d60SMina Almasry * operation both new_vma and vma share a reference to the resv_map 1194550a7d60SMina Almasry * struct, and at that point vma is about to be unmapped. We don't 1195550a7d60SMina Almasry * want to return the reservation to the pool at unmap of vma because 1196550a7d60SMina Almasry * the reservation still lives on in new_vma, so simply decrement the 1197550a7d60SMina Almasry * ref here and remove the resv_map reference from this vma. 1198550a7d60SMina Almasry */ 1199550a7d60SMina Almasry struct resv_map *reservations = vma_resv_map(vma); 1200550a7d60SMina Almasry 1201afe041c2SBui Quang Minh if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { 1202afe041c2SBui Quang Minh resv_map_put_hugetlb_cgroup_uncharge_info(reservations); 1203550a7d60SMina Almasry kref_put(&reservations->refs, resv_map_release); 1204afe041c2SBui Quang Minh } 1205550a7d60SMina Almasry 12068d9bfb26SMike Kravetz hugetlb_dup_vma_private(vma); 1207550a7d60SMina Almasry } 1208550a7d60SMina Almasry 1209a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */ 1210559ec2f8SNicholas Krause static bool vma_has_reserves(struct vm_area_struct *vma, long chg) 1211a1e78772SMel Gorman { 1212af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_NORESERVE) { 1213af0ed73eSJoonsoo Kim /* 1214af0ed73eSJoonsoo Kim * This address is already reserved by other process(chg == 0), 1215af0ed73eSJoonsoo Kim * so, we should decrement reserved count. Without decrementing, 1216af0ed73eSJoonsoo Kim * reserve count remains after releasing inode, because this 1217af0ed73eSJoonsoo Kim * allocated page will go into page cache and is regarded as 1218af0ed73eSJoonsoo Kim * coming from reserved pool in releasing step. Currently, we 1219af0ed73eSJoonsoo Kim * don't have any other solution to deal with this situation 1220af0ed73eSJoonsoo Kim * properly, so add work-around here. 1221af0ed73eSJoonsoo Kim */ 1222af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE && chg == 0) 1223559ec2f8SNicholas Krause return true; 1224af0ed73eSJoonsoo Kim else 1225559ec2f8SNicholas Krause return false; 1226af0ed73eSJoonsoo Kim } 1227a63884e9SJoonsoo Kim 1228a63884e9SJoonsoo Kim /* Shared mappings always use reserves */ 12291fb1b0e9SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 12301fb1b0e9SMike Kravetz /* 12311fb1b0e9SMike Kravetz * We know VM_NORESERVE is not set. Therefore, there SHOULD 12321fb1b0e9SMike Kravetz * be a region map for all pages. The only situation where 12331fb1b0e9SMike Kravetz * there is no region map is if a hole was punched via 12347c8de358SEthon Paul * fallocate. In this case, there really are no reserves to 12351fb1b0e9SMike Kravetz * use. This situation is indicated if chg != 0. 12361fb1b0e9SMike Kravetz */ 12371fb1b0e9SMike Kravetz if (chg) 12381fb1b0e9SMike Kravetz return false; 12391fb1b0e9SMike Kravetz else 1240559ec2f8SNicholas Krause return true; 12411fb1b0e9SMike Kravetz } 1242a63884e9SJoonsoo Kim 1243a63884e9SJoonsoo Kim /* 1244a63884e9SJoonsoo Kim * Only the process that called mmap() has reserves for 1245a63884e9SJoonsoo Kim * private mappings. 1246a63884e9SJoonsoo Kim */ 124767961f9dSMike Kravetz if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { 124867961f9dSMike Kravetz /* 124967961f9dSMike Kravetz * Like the shared case above, a hole punch or truncate 125067961f9dSMike Kravetz * could have been performed on the private mapping. 125167961f9dSMike Kravetz * Examine the value of chg to determine if reserves 125267961f9dSMike Kravetz * actually exist or were previously consumed. 125367961f9dSMike Kravetz * Very Subtle - The value of chg comes from a previous 125467961f9dSMike Kravetz * call to vma_needs_reserves(). The reserve map for 125567961f9dSMike Kravetz * private mappings has different (opposite) semantics 125667961f9dSMike Kravetz * than that of shared mappings. vma_needs_reserves() 125767961f9dSMike Kravetz * has already taken this difference in semantics into 125867961f9dSMike Kravetz * account. Therefore, the meaning of chg is the same 125967961f9dSMike Kravetz * as in the shared case above. Code could easily be 126067961f9dSMike Kravetz * combined, but keeping it separate draws attention to 126167961f9dSMike Kravetz * subtle differences. 126267961f9dSMike Kravetz */ 126367961f9dSMike Kravetz if (chg) 126467961f9dSMike Kravetz return false; 126567961f9dSMike Kravetz else 1266559ec2f8SNicholas Krause return true; 126767961f9dSMike Kravetz } 1268a63884e9SJoonsoo Kim 1269559ec2f8SNicholas Krause return false; 1270a1e78772SMel Gorman } 1271a1e78772SMel Gorman 1272240d67a8SSidhartha Kumar static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio) 12731da177e4SLinus Torvalds { 1274240d67a8SSidhartha Kumar int nid = folio_nid(folio); 12759487ca60SMike Kravetz 12769487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 1277240d67a8SSidhartha Kumar VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); 1278b65a4edaSMike Kravetz 1279240d67a8SSidhartha Kumar list_move(&folio->lru, &h->hugepage_freelists[nid]); 1280a5516438SAndi Kleen h->free_huge_pages++; 1281a5516438SAndi Kleen h->free_huge_pages_node[nid]++; 1282240d67a8SSidhartha Kumar folio_set_hugetlb_freed(folio); 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 1285a36f1e90SSidhartha Kumar static struct folio *dequeue_hugetlb_folio_node_exact(struct hstate *h, 1286a36f1e90SSidhartha Kumar int nid) 1287bf50bab2SNaoya Horiguchi { 1288a36f1e90SSidhartha Kumar struct folio *folio; 12891a08ae36SPavel Tatashin bool pin = !!(current->flags & PF_MEMALLOC_PIN); 1290bf50bab2SNaoya Horiguchi 12919487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 1292a36f1e90SSidhartha Kumar list_for_each_entry(folio, &h->hugepage_freelists[nid], lru) { 1293a36f1e90SSidhartha Kumar if (pin && !folio_is_longterm_pinnable(folio)) 1294bbe88753SJoonsoo Kim continue; 1295bbe88753SJoonsoo Kim 1296a36f1e90SSidhartha Kumar if (folio_test_hwpoison(folio)) 12976664bfc8SWei Yang continue; 1298bbe88753SJoonsoo Kim 1299a36f1e90SSidhartha Kumar list_move(&folio->lru, &h->hugepage_activelist); 1300a36f1e90SSidhartha Kumar folio_ref_unfreeze(folio, 1); 1301a36f1e90SSidhartha Kumar folio_clear_hugetlb_freed(folio); 1302bf50bab2SNaoya Horiguchi h->free_huge_pages--; 1303bf50bab2SNaoya Horiguchi h->free_huge_pages_node[nid]--; 1304a36f1e90SSidhartha Kumar return folio; 1305bf50bab2SNaoya Horiguchi } 1306bf50bab2SNaoya Horiguchi 13076664bfc8SWei Yang return NULL; 13086664bfc8SWei Yang } 13096664bfc8SWei Yang 1310a36f1e90SSidhartha Kumar static struct folio *dequeue_hugetlb_folio_nodemask(struct hstate *h, gfp_t gfp_mask, 1311a36f1e90SSidhartha Kumar int nid, nodemask_t *nmask) 131294310cbcSAnshuman Khandual { 13133e59fcb0SMichal Hocko unsigned int cpuset_mems_cookie; 13143e59fcb0SMichal Hocko struct zonelist *zonelist; 13153e59fcb0SMichal Hocko struct zone *zone; 13163e59fcb0SMichal Hocko struct zoneref *z; 131798fa15f3SAnshuman Khandual int node = NUMA_NO_NODE; 13183e59fcb0SMichal Hocko 13193e59fcb0SMichal Hocko zonelist = node_zonelist(nid, gfp_mask); 13203e59fcb0SMichal Hocko 13213e59fcb0SMichal Hocko retry_cpuset: 13223e59fcb0SMichal Hocko cpuset_mems_cookie = read_mems_allowed_begin(); 13233e59fcb0SMichal Hocko for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) { 1324a36f1e90SSidhartha Kumar struct folio *folio; 132594310cbcSAnshuman Khandual 13263e59fcb0SMichal Hocko if (!cpuset_zone_allowed(zone, gfp_mask)) 13273e59fcb0SMichal Hocko continue; 13283e59fcb0SMichal Hocko /* 13293e59fcb0SMichal Hocko * no need to ask again on the same node. Pool is node rather than 13303e59fcb0SMichal Hocko * zone aware 13313e59fcb0SMichal Hocko */ 13323e59fcb0SMichal Hocko if (zone_to_nid(zone) == node) 13333e59fcb0SMichal Hocko continue; 13343e59fcb0SMichal Hocko node = zone_to_nid(zone); 133594310cbcSAnshuman Khandual 1336a36f1e90SSidhartha Kumar folio = dequeue_hugetlb_folio_node_exact(h, node); 1337a36f1e90SSidhartha Kumar if (folio) 1338a36f1e90SSidhartha Kumar return folio; 133994310cbcSAnshuman Khandual } 13403e59fcb0SMichal Hocko if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie))) 13413e59fcb0SMichal Hocko goto retry_cpuset; 13423e59fcb0SMichal Hocko 134394310cbcSAnshuman Khandual return NULL; 134494310cbcSAnshuman Khandual } 134594310cbcSAnshuman Khandual 13468346d69dSXin Hao static unsigned long available_huge_pages(struct hstate *h) 13478346d69dSXin Hao { 13488346d69dSXin Hao return h->free_huge_pages - h->resv_huge_pages; 13498346d69dSXin Hao } 13508346d69dSXin Hao 1351ff7d853bSSidhartha Kumar static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h, 1352a5516438SAndi Kleen struct vm_area_struct *vma, 1353af0ed73eSJoonsoo Kim unsigned long address, int avoid_reserve, 1354af0ed73eSJoonsoo Kim long chg) 13551da177e4SLinus Torvalds { 1356a36f1e90SSidhartha Kumar struct folio *folio = NULL; 1357480eccf9SLee Schermerhorn struct mempolicy *mpol; 135804ec6264SVlastimil Babka gfp_t gfp_mask; 13593e59fcb0SMichal Hocko nodemask_t *nodemask; 136004ec6264SVlastimil Babka int nid; 13611da177e4SLinus Torvalds 1362a1e78772SMel Gorman /* 1363a1e78772SMel Gorman * A child process with MAP_PRIVATE mappings created by their parent 1364a1e78772SMel Gorman * have no page reserves. This check ensures that reservations are 1365a1e78772SMel Gorman * not "stolen". The child may still get SIGKILLed 1366a1e78772SMel Gorman */ 13678346d69dSXin Hao if (!vma_has_reserves(vma, chg) && !available_huge_pages(h)) 1368c0ff7453SMiao Xie goto err; 1369a1e78772SMel Gorman 137004f2cbe3SMel Gorman /* If reserves cannot be used, ensure enough pages are in the pool */ 13718346d69dSXin Hao if (avoid_reserve && !available_huge_pages(h)) 13726eab04a8SJustin P. Mattock goto err; 137304f2cbe3SMel Gorman 137404ec6264SVlastimil Babka gfp_mask = htlb_alloc_mask(h); 137504ec6264SVlastimil Babka nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask); 1376cfcaa66fSBen Widawsky 1377cfcaa66fSBen Widawsky if (mpol_is_preferred_many(mpol)) { 1378a36f1e90SSidhartha Kumar folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, 1379a36f1e90SSidhartha Kumar nid, nodemask); 1380cfcaa66fSBen Widawsky 1381cfcaa66fSBen Widawsky /* Fallback to all nodes if page==NULL */ 1382cfcaa66fSBen Widawsky nodemask = NULL; 1383cfcaa66fSBen Widawsky } 1384cfcaa66fSBen Widawsky 1385a36f1e90SSidhartha Kumar if (!folio) 1386a36f1e90SSidhartha Kumar folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, 1387a36f1e90SSidhartha Kumar nid, nodemask); 1388cfcaa66fSBen Widawsky 1389a36f1e90SSidhartha Kumar if (folio && !avoid_reserve && vma_has_reserves(vma, chg)) { 1390a36f1e90SSidhartha Kumar folio_set_hugetlb_restore_reserve(folio); 1391a63884e9SJoonsoo Kim h->resv_huge_pages--; 1392bf50bab2SNaoya Horiguchi } 1393cc9a6c87SMel Gorman 1394cc9a6c87SMel Gorman mpol_cond_put(mpol); 1395ff7d853bSSidhartha Kumar return folio; 1396cc9a6c87SMel Gorman 1397c0ff7453SMiao Xie err: 1398cc9a6c87SMel Gorman return NULL; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14011cac6f2cSLuiz Capitulino /* 14021cac6f2cSLuiz Capitulino * common helper functions for hstate_next_node_to_{alloc|free}. 14031cac6f2cSLuiz Capitulino * We may have allocated or freed a huge page based on a different 14041cac6f2cSLuiz Capitulino * nodes_allowed previously, so h->next_node_to_{alloc|free} might 14051cac6f2cSLuiz Capitulino * be outside of *nodes_allowed. Ensure that we use an allowed 14061cac6f2cSLuiz Capitulino * node for alloc or free. 14071cac6f2cSLuiz Capitulino */ 14081cac6f2cSLuiz Capitulino static int next_node_allowed(int nid, nodemask_t *nodes_allowed) 14091cac6f2cSLuiz Capitulino { 14100edaf86cSAndrew Morton nid = next_node_in(nid, *nodes_allowed); 14111cac6f2cSLuiz Capitulino VM_BUG_ON(nid >= MAX_NUMNODES); 14121cac6f2cSLuiz Capitulino 14131cac6f2cSLuiz Capitulino return nid; 14141cac6f2cSLuiz Capitulino } 14151cac6f2cSLuiz Capitulino 14161cac6f2cSLuiz Capitulino static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) 14171cac6f2cSLuiz Capitulino { 14181cac6f2cSLuiz Capitulino if (!node_isset(nid, *nodes_allowed)) 14191cac6f2cSLuiz Capitulino nid = next_node_allowed(nid, nodes_allowed); 14201cac6f2cSLuiz Capitulino return nid; 14211cac6f2cSLuiz Capitulino } 14221cac6f2cSLuiz Capitulino 14231cac6f2cSLuiz Capitulino /* 14241cac6f2cSLuiz Capitulino * returns the previously saved node ["this node"] from which to 14251cac6f2cSLuiz Capitulino * allocate a persistent huge page for the pool and advance the 14261cac6f2cSLuiz Capitulino * next node from which to allocate, handling wrap at end of node 14271cac6f2cSLuiz Capitulino * mask. 14281cac6f2cSLuiz Capitulino */ 14291cac6f2cSLuiz Capitulino static int hstate_next_node_to_alloc(struct hstate *h, 14301cac6f2cSLuiz Capitulino nodemask_t *nodes_allowed) 14311cac6f2cSLuiz Capitulino { 14321cac6f2cSLuiz Capitulino int nid; 14331cac6f2cSLuiz Capitulino 14341cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 14351cac6f2cSLuiz Capitulino 14361cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); 14371cac6f2cSLuiz Capitulino h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); 14381cac6f2cSLuiz Capitulino 14391cac6f2cSLuiz Capitulino return nid; 14401cac6f2cSLuiz Capitulino } 14411cac6f2cSLuiz Capitulino 14421cac6f2cSLuiz Capitulino /* 144310c6ec49SMike Kravetz * helper for remove_pool_huge_page() - return the previously saved 14441cac6f2cSLuiz Capitulino * node ["this node"] from which to free a huge page. Advance the 14451cac6f2cSLuiz Capitulino * next node id whether or not we find a free huge page to free so 14461cac6f2cSLuiz Capitulino * that the next attempt to free addresses the next node. 14471cac6f2cSLuiz Capitulino */ 14481cac6f2cSLuiz Capitulino static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) 14491cac6f2cSLuiz Capitulino { 14501cac6f2cSLuiz Capitulino int nid; 14511cac6f2cSLuiz Capitulino 14521cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 14531cac6f2cSLuiz Capitulino 14541cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); 14551cac6f2cSLuiz Capitulino h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); 14561cac6f2cSLuiz Capitulino 14571cac6f2cSLuiz Capitulino return nid; 14581cac6f2cSLuiz Capitulino } 14591cac6f2cSLuiz Capitulino 14601cac6f2cSLuiz Capitulino #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \ 14611cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 14621cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 14631cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ 14641cac6f2cSLuiz Capitulino nr_nodes--) 14651cac6f2cSLuiz Capitulino 14661cac6f2cSLuiz Capitulino #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ 14671cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 14681cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 14691cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_free(hs, mask)) || 1); \ 14701cac6f2cSLuiz Capitulino nr_nodes--) 14711cac6f2cSLuiz Capitulino 14728531fc6fSMike Kravetz /* used to demote non-gigantic_huge pages as well */ 1473911565b8SSidhartha Kumar static void __destroy_compound_gigantic_folio(struct folio *folio, 147434d9e35bSMike Kravetz unsigned int order, bool demote) 1475944d9fecSLuiz Capitulino { 1476944d9fecSLuiz Capitulino int i; 1477944d9fecSLuiz Capitulino int nr_pages = 1 << order; 147814455eabSCheng Li struct page *p; 1479944d9fecSLuiz Capitulino 148046f27228SMatthew Wilcox (Oracle) atomic_set(&folio->_entire_mapcount, 0); 1481eec20426SMatthew Wilcox (Oracle) atomic_set(&folio->_nr_pages_mapped, 0); 148294688e8eSMatthew Wilcox (Oracle) atomic_set(&folio->_pincount, 0); 148347e29d32SJohn Hubbard 148414455eabSCheng Li for (i = 1; i < nr_pages; i++) { 1485911565b8SSidhartha Kumar p = folio_page(folio, i); 1486a01f4390SMike Kravetz p->mapping = NULL; 14871d798ca3SKirill A. Shutemov clear_compound_head(p); 148834d9e35bSMike Kravetz if (!demote) 1489944d9fecSLuiz Capitulino set_page_refcounted(p); 1490944d9fecSLuiz Capitulino } 1491944d9fecSLuiz Capitulino 1492911565b8SSidhartha Kumar __folio_clear_head(folio); 1493944d9fecSLuiz Capitulino } 1494944d9fecSLuiz Capitulino 1495911565b8SSidhartha Kumar static void destroy_compound_hugetlb_folio_for_demote(struct folio *folio, 14968531fc6fSMike Kravetz unsigned int order) 14978531fc6fSMike Kravetz { 1498911565b8SSidhartha Kumar __destroy_compound_gigantic_folio(folio, order, true); 14998531fc6fSMike Kravetz } 15008531fc6fSMike Kravetz 15018531fc6fSMike Kravetz #ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE 1502911565b8SSidhartha Kumar static void destroy_compound_gigantic_folio(struct folio *folio, 150334d9e35bSMike Kravetz unsigned int order) 150434d9e35bSMike Kravetz { 1505911565b8SSidhartha Kumar __destroy_compound_gigantic_folio(folio, order, false); 150634d9e35bSMike Kravetz } 150734d9e35bSMike Kravetz 15087f325a8dSSidhartha Kumar static void free_gigantic_folio(struct folio *folio, unsigned int order) 1509944d9fecSLuiz Capitulino { 1510cf11e85fSRoman Gushchin /* 1511cf11e85fSRoman Gushchin * If the page isn't allocated using the cma allocator, 1512cf11e85fSRoman Gushchin * cma_release() returns false. 1513cf11e85fSRoman Gushchin */ 1514dbda8feaSBarry Song #ifdef CONFIG_CMA 15157f325a8dSSidhartha Kumar int nid = folio_nid(folio); 15167f325a8dSSidhartha Kumar 15177f325a8dSSidhartha Kumar if (cma_release(hugetlb_cma[nid], &folio->page, 1 << order)) 1518cf11e85fSRoman Gushchin return; 1519dbda8feaSBarry Song #endif 1520cf11e85fSRoman Gushchin 15217f325a8dSSidhartha Kumar free_contig_range(folio_pfn(folio), 1 << order); 1522944d9fecSLuiz Capitulino } 1523944d9fecSLuiz Capitulino 15244eb0716eSAlexandre Ghiti #ifdef CONFIG_CONTIG_ALLOC 152519fc1a7eSSidhartha Kumar static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask, 1526d9cc948fSMichal Hocko int nid, nodemask_t *nodemask) 1527944d9fecSLuiz Capitulino { 152819fc1a7eSSidhartha Kumar struct page *page; 152904adbc3fSMiaohe Lin unsigned long nr_pages = pages_per_huge_page(h); 1530953f064aSLi Xinhai if (nid == NUMA_NO_NODE) 1531953f064aSLi Xinhai nid = numa_mem_id(); 1532944d9fecSLuiz Capitulino 1533dbda8feaSBarry Song #ifdef CONFIG_CMA 1534dbda8feaSBarry Song { 1535cf11e85fSRoman Gushchin int node; 1536cf11e85fSRoman Gushchin 1537953f064aSLi Xinhai if (hugetlb_cma[nid]) { 1538953f064aSLi Xinhai page = cma_alloc(hugetlb_cma[nid], nr_pages, 1539953f064aSLi Xinhai huge_page_order(h), true); 1540953f064aSLi Xinhai if (page) 154119fc1a7eSSidhartha Kumar return page_folio(page); 1542953f064aSLi Xinhai } 1543953f064aSLi Xinhai 1544953f064aSLi Xinhai if (!(gfp_mask & __GFP_THISNODE)) { 1545cf11e85fSRoman Gushchin for_each_node_mask(node, *nodemask) { 1546953f064aSLi Xinhai if (node == nid || !hugetlb_cma[node]) 1547cf11e85fSRoman Gushchin continue; 1548cf11e85fSRoman Gushchin 1549cf11e85fSRoman Gushchin page = cma_alloc(hugetlb_cma[node], nr_pages, 1550cf11e85fSRoman Gushchin huge_page_order(h), true); 1551cf11e85fSRoman Gushchin if (page) 155219fc1a7eSSidhartha Kumar return page_folio(page); 1553cf11e85fSRoman Gushchin } 1554cf11e85fSRoman Gushchin } 1555953f064aSLi Xinhai } 1556dbda8feaSBarry Song #endif 1557cf11e85fSRoman Gushchin 155819fc1a7eSSidhartha Kumar page = alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask); 155919fc1a7eSSidhartha Kumar return page ? page_folio(page) : NULL; 1560944d9fecSLuiz Capitulino } 1561944d9fecSLuiz Capitulino 15624eb0716eSAlexandre Ghiti #else /* !CONFIG_CONTIG_ALLOC */ 156319fc1a7eSSidhartha Kumar static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask, 15644eb0716eSAlexandre Ghiti int nid, nodemask_t *nodemask) 15654eb0716eSAlexandre Ghiti { 15664eb0716eSAlexandre Ghiti return NULL; 15674eb0716eSAlexandre Ghiti } 15684eb0716eSAlexandre Ghiti #endif /* CONFIG_CONTIG_ALLOC */ 1569944d9fecSLuiz Capitulino 1570e1073d1eSAneesh Kumar K.V #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */ 157119fc1a7eSSidhartha Kumar static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask, 15724eb0716eSAlexandre Ghiti int nid, nodemask_t *nodemask) 15734eb0716eSAlexandre Ghiti { 15744eb0716eSAlexandre Ghiti return NULL; 15754eb0716eSAlexandre Ghiti } 15767f325a8dSSidhartha Kumar static inline void free_gigantic_folio(struct folio *folio, 15777f325a8dSSidhartha Kumar unsigned int order) { } 1578911565b8SSidhartha Kumar static inline void destroy_compound_gigantic_folio(struct folio *folio, 1579d00181b9SKirill A. Shutemov unsigned int order) { } 1580944d9fecSLuiz Capitulino #endif 1581944d9fecSLuiz Capitulino 15826eb4e88aSMike Kravetz /* 1583cfd5082bSSidhartha Kumar * Remove hugetlb folio from lists, and update dtor so that the folio appears 158434d9e35bSMike Kravetz * as just a compound page. 158534d9e35bSMike Kravetz * 1586cfd5082bSSidhartha Kumar * A reference is held on the folio, except in the case of demote. 15876eb4e88aSMike Kravetz * 15886eb4e88aSMike Kravetz * Must be called with hugetlb lock held. 15896eb4e88aSMike Kravetz */ 1590cfd5082bSSidhartha Kumar static void __remove_hugetlb_folio(struct hstate *h, struct folio *folio, 159134d9e35bSMike Kravetz bool adjust_surplus, 159234d9e35bSMike Kravetz bool demote) 15936eb4e88aSMike Kravetz { 1594cfd5082bSSidhartha Kumar int nid = folio_nid(folio); 15956eb4e88aSMike Kravetz 1596f074732dSSidhartha Kumar VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio(folio), folio); 1597f074732dSSidhartha Kumar VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio_rsvd(folio), folio); 15986eb4e88aSMike Kravetz 15999487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 16006eb4e88aSMike Kravetz if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 16016eb4e88aSMike Kravetz return; 16026eb4e88aSMike Kravetz 1603cfd5082bSSidhartha Kumar list_del(&folio->lru); 16046eb4e88aSMike Kravetz 1605cfd5082bSSidhartha Kumar if (folio_test_hugetlb_freed(folio)) { 16066eb4e88aSMike Kravetz h->free_huge_pages--; 16076eb4e88aSMike Kravetz h->free_huge_pages_node[nid]--; 16086eb4e88aSMike Kravetz } 16096eb4e88aSMike Kravetz if (adjust_surplus) { 16106eb4e88aSMike Kravetz h->surplus_huge_pages--; 16116eb4e88aSMike Kravetz h->surplus_huge_pages_node[nid]--; 16126eb4e88aSMike Kravetz } 16136eb4e88aSMike Kravetz 1614e32d20c0SMike Kravetz /* 1615e32d20c0SMike Kravetz * Very subtle 1616e32d20c0SMike Kravetz * 1617e32d20c0SMike Kravetz * For non-gigantic pages set the destructor to the normal compound 1618e32d20c0SMike Kravetz * page dtor. This is needed in case someone takes an additional 1619e32d20c0SMike Kravetz * temporary ref to the page, and freeing is delayed until they drop 1620e32d20c0SMike Kravetz * their reference. 1621e32d20c0SMike Kravetz * 1622e32d20c0SMike Kravetz * For gigantic pages set the destructor to the null dtor. This 1623e32d20c0SMike Kravetz * destructor will never be called. Before freeing the gigantic 1624911565b8SSidhartha Kumar * page destroy_compound_gigantic_folio will turn the folio into a 1625911565b8SSidhartha Kumar * simple group of pages. After this the destructor does not 1626e32d20c0SMike Kravetz * apply. 1627e32d20c0SMike Kravetz * 1628e32d20c0SMike Kravetz * This handles the case where more than one ref is held when and 1629d6ef19e2SSidhartha Kumar * after update_and_free_hugetlb_folio is called. 163034d9e35bSMike Kravetz * 163134d9e35bSMike Kravetz * In the case of demote we do not ref count the page as it will soon 163234d9e35bSMike Kravetz * be turned into a page of smaller size. 1633e32d20c0SMike Kravetz */ 163434d9e35bSMike Kravetz if (!demote) 1635cfd5082bSSidhartha Kumar folio_ref_unfreeze(folio, 1); 1636e32d20c0SMike Kravetz if (hstate_is_gigantic(h)) 1637cfd5082bSSidhartha Kumar folio_set_compound_dtor(folio, NULL_COMPOUND_DTOR); 1638e32d20c0SMike Kravetz else 1639cfd5082bSSidhartha Kumar folio_set_compound_dtor(folio, COMPOUND_PAGE_DTOR); 16406eb4e88aSMike Kravetz 16416eb4e88aSMike Kravetz h->nr_huge_pages--; 16426eb4e88aSMike Kravetz h->nr_huge_pages_node[nid]--; 16436eb4e88aSMike Kravetz } 16446eb4e88aSMike Kravetz 1645cfd5082bSSidhartha Kumar static void remove_hugetlb_folio(struct hstate *h, struct folio *folio, 164634d9e35bSMike Kravetz bool adjust_surplus) 164734d9e35bSMike Kravetz { 1648cfd5082bSSidhartha Kumar __remove_hugetlb_folio(h, folio, adjust_surplus, false); 164934d9e35bSMike Kravetz } 165034d9e35bSMike Kravetz 1651cfd5082bSSidhartha Kumar static void remove_hugetlb_folio_for_demote(struct hstate *h, struct folio *folio, 16528531fc6fSMike Kravetz bool adjust_surplus) 16538531fc6fSMike Kravetz { 1654cfd5082bSSidhartha Kumar __remove_hugetlb_folio(h, folio, adjust_surplus, true); 16558531fc6fSMike Kravetz } 16568531fc6fSMike Kravetz 16572f6c57d6SSidhartha Kumar static void add_hugetlb_folio(struct hstate *h, struct folio *folio, 1658ad2fa371SMuchun Song bool adjust_surplus) 1659ad2fa371SMuchun Song { 1660ad2fa371SMuchun Song int zeroed; 16612f6c57d6SSidhartha Kumar int nid = folio_nid(folio); 1662ad2fa371SMuchun Song 16632f6c57d6SSidhartha Kumar VM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio); 1664ad2fa371SMuchun Song 1665ad2fa371SMuchun Song lockdep_assert_held(&hugetlb_lock); 1666ad2fa371SMuchun Song 16672f6c57d6SSidhartha Kumar INIT_LIST_HEAD(&folio->lru); 1668ad2fa371SMuchun Song h->nr_huge_pages++; 1669ad2fa371SMuchun Song h->nr_huge_pages_node[nid]++; 1670ad2fa371SMuchun Song 1671ad2fa371SMuchun Song if (adjust_surplus) { 1672ad2fa371SMuchun Song h->surplus_huge_pages++; 1673ad2fa371SMuchun Song h->surplus_huge_pages_node[nid]++; 1674ad2fa371SMuchun Song } 1675ad2fa371SMuchun Song 16762f6c57d6SSidhartha Kumar folio_set_compound_dtor(folio, HUGETLB_PAGE_DTOR); 16772f6c57d6SSidhartha Kumar folio_change_private(folio, NULL); 1678a9e1eab2SMiaohe Lin /* 16792f6c57d6SSidhartha Kumar * We have to set hugetlb_vmemmap_optimized again as above 16802f6c57d6SSidhartha Kumar * folio_change_private(folio, NULL) cleared it. 1681a9e1eab2SMiaohe Lin */ 16822f6c57d6SSidhartha Kumar folio_set_hugetlb_vmemmap_optimized(folio); 1683ad2fa371SMuchun Song 1684ad2fa371SMuchun Song /* 16852f6c57d6SSidhartha Kumar * This folio is about to be managed by the hugetlb allocator and 1686b65a4edaSMike Kravetz * should have no users. Drop our reference, and check for others 1687b65a4edaSMike Kravetz * just in case. 1688ad2fa371SMuchun Song */ 16892f6c57d6SSidhartha Kumar zeroed = folio_put_testzero(folio); 16902f6c57d6SSidhartha Kumar if (unlikely(!zeroed)) 1691b65a4edaSMike Kravetz /* 1692b65a4edaSMike Kravetz * It is VERY unlikely soneone else has taken a ref on 1693b65a4edaSMike Kravetz * the page. In this case, we simply return as the 1694b65a4edaSMike Kravetz * hugetlb destructor (free_huge_page) will be called 1695b65a4edaSMike Kravetz * when this other ref is dropped. 1696b65a4edaSMike Kravetz */ 1697b65a4edaSMike Kravetz return; 1698b65a4edaSMike Kravetz 16992f6c57d6SSidhartha Kumar arch_clear_hugepage_flags(&folio->page); 1700240d67a8SSidhartha Kumar enqueue_hugetlb_folio(h, folio); 1701ad2fa371SMuchun Song } 1702ad2fa371SMuchun Song 17036f6956cfSSidhartha Kumar static void __update_and_free_hugetlb_folio(struct hstate *h, 17046f6956cfSSidhartha Kumar struct folio *folio) 17056af2acb6SAdam Litke { 17066af2acb6SAdam Litke int i; 170714455eabSCheng Li struct page *subpage; 1708a5516438SAndi Kleen 17094eb0716eSAlexandre Ghiti if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 1710944d9fecSLuiz Capitulino return; 171118229df5SAndy Whitcroft 1712161df60eSNaoya Horiguchi /* 1713161df60eSNaoya Horiguchi * If we don't know which subpages are hwpoisoned, we can't free 1714161df60eSNaoya Horiguchi * the hugepage, so it's leaked intentionally. 1715161df60eSNaoya Horiguchi */ 17167f325a8dSSidhartha Kumar if (folio_test_hugetlb_raw_hwp_unreliable(folio)) 1717161df60eSNaoya Horiguchi return; 1718161df60eSNaoya Horiguchi 17196f6956cfSSidhartha Kumar if (hugetlb_vmemmap_restore(h, &folio->page)) { 1720ad2fa371SMuchun Song spin_lock_irq(&hugetlb_lock); 1721ad2fa371SMuchun Song /* 1722ad2fa371SMuchun Song * If we cannot allocate vmemmap pages, just refuse to free the 1723ad2fa371SMuchun Song * page and put the page back on the hugetlb free list and treat 1724ad2fa371SMuchun Song * as a surplus page. 1725ad2fa371SMuchun Song */ 17267f325a8dSSidhartha Kumar add_hugetlb_folio(h, folio, true); 1727ad2fa371SMuchun Song spin_unlock_irq(&hugetlb_lock); 1728ad2fa371SMuchun Song return; 1729ad2fa371SMuchun Song } 1730ad2fa371SMuchun Song 1731161df60eSNaoya Horiguchi /* 1732161df60eSNaoya Horiguchi * Move PageHWPoison flag from head page to the raw error pages, 1733161df60eSNaoya Horiguchi * which makes any healthy subpages reusable. 1734161df60eSNaoya Horiguchi */ 1735911565b8SSidhartha Kumar if (unlikely(folio_test_hwpoison(folio))) 17362ff6ceceSSidhartha Kumar folio_clear_hugetlb_hwpoison(folio); 1737161df60eSNaoya Horiguchi 173814455eabSCheng Li for (i = 0; i < pages_per_huge_page(h); i++) { 17397f325a8dSSidhartha Kumar subpage = folio_page(folio, i); 1740dbfee5aeSMike Kravetz subpage->flags &= ~(1 << PG_locked | 1 << PG_error | 174132f84528SChris Forbes 1 << PG_referenced | 1 << PG_dirty | 1742a7407a27SLuiz Capitulino 1 << PG_active | 1 << PG_private | 1743a7407a27SLuiz Capitulino 1 << PG_writeback); 17446af2acb6SAdam Litke } 1745a01f4390SMike Kravetz 1746a01f4390SMike Kravetz /* 1747a01f4390SMike Kravetz * Non-gigantic pages demoted from CMA allocated gigantic pages 17487f325a8dSSidhartha Kumar * need to be given back to CMA in free_gigantic_folio. 1749a01f4390SMike Kravetz */ 1750a01f4390SMike Kravetz if (hstate_is_gigantic(h) || 17512f6c57d6SSidhartha Kumar hugetlb_cma_folio(folio, huge_page_order(h))) { 1752911565b8SSidhartha Kumar destroy_compound_gigantic_folio(folio, huge_page_order(h)); 17537f325a8dSSidhartha Kumar free_gigantic_folio(folio, huge_page_order(h)); 1754944d9fecSLuiz Capitulino } else { 17556f6956cfSSidhartha Kumar __free_pages(&folio->page, huge_page_order(h)); 17566af2acb6SAdam Litke } 1757944d9fecSLuiz Capitulino } 17586af2acb6SAdam Litke 1759b65d4adbSMuchun Song /* 1760d6ef19e2SSidhartha Kumar * As update_and_free_hugetlb_folio() can be called under any context, so we cannot 1761b65d4adbSMuchun Song * use GFP_KERNEL to allocate vmemmap pages. However, we can defer the 1762b65d4adbSMuchun Song * actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate 1763b65d4adbSMuchun Song * the vmemmap pages. 1764b65d4adbSMuchun Song * 1765b65d4adbSMuchun Song * free_hpage_workfn() locklessly retrieves the linked list of pages to be 1766b65d4adbSMuchun Song * freed and frees them one-by-one. As the page->mapping pointer is going 1767b65d4adbSMuchun Song * to be cleared in free_hpage_workfn() anyway, it is reused as the llist_node 1768b65d4adbSMuchun Song * structure of a lockless linked list of huge pages to be freed. 1769b65d4adbSMuchun Song */ 1770b65d4adbSMuchun Song static LLIST_HEAD(hpage_freelist); 1771b65d4adbSMuchun Song 1772b65d4adbSMuchun Song static void free_hpage_workfn(struct work_struct *work) 1773b65d4adbSMuchun Song { 1774b65d4adbSMuchun Song struct llist_node *node; 1775b65d4adbSMuchun Song 1776b65d4adbSMuchun Song node = llist_del_all(&hpage_freelist); 1777b65d4adbSMuchun Song 1778b65d4adbSMuchun Song while (node) { 1779b65d4adbSMuchun Song struct page *page; 1780b65d4adbSMuchun Song struct hstate *h; 1781b65d4adbSMuchun Song 1782b65d4adbSMuchun Song page = container_of((struct address_space **)node, 1783b65d4adbSMuchun Song struct page, mapping); 1784b65d4adbSMuchun Song node = node->next; 1785b65d4adbSMuchun Song page->mapping = NULL; 1786b65d4adbSMuchun Song /* 1787b65d4adbSMuchun Song * The VM_BUG_ON_PAGE(!PageHuge(page), page) in page_hstate() 1788b65d4adbSMuchun Song * is going to trigger because a previous call to 1789cfd5082bSSidhartha Kumar * remove_hugetlb_folio() will call folio_set_compound_dtor 1790cfd5082bSSidhartha Kumar * (folio, NULL_COMPOUND_DTOR), so do not use page_hstate() 1791cfd5082bSSidhartha Kumar * directly. 1792b65d4adbSMuchun Song */ 1793b65d4adbSMuchun Song h = size_to_hstate(page_size(page)); 1794b65d4adbSMuchun Song 17956f6956cfSSidhartha Kumar __update_and_free_hugetlb_folio(h, page_folio(page)); 1796b65d4adbSMuchun Song 1797b65d4adbSMuchun Song cond_resched(); 1798b65d4adbSMuchun Song } 1799b65d4adbSMuchun Song } 1800b65d4adbSMuchun Song static DECLARE_WORK(free_hpage_work, free_hpage_workfn); 1801b65d4adbSMuchun Song 1802b65d4adbSMuchun Song static inline void flush_free_hpage_work(struct hstate *h) 1803b65d4adbSMuchun Song { 18046213834cSMuchun Song if (hugetlb_vmemmap_optimizable(h)) 1805b65d4adbSMuchun Song flush_work(&free_hpage_work); 1806b65d4adbSMuchun Song } 1807b65d4adbSMuchun Song 1808d6ef19e2SSidhartha Kumar static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio, 1809b65d4adbSMuchun Song bool atomic) 1810b65d4adbSMuchun Song { 1811d6ef19e2SSidhartha Kumar if (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) { 18126f6956cfSSidhartha Kumar __update_and_free_hugetlb_folio(h, folio); 1813b65d4adbSMuchun Song return; 1814b65d4adbSMuchun Song } 1815b65d4adbSMuchun Song 1816b65d4adbSMuchun Song /* 1817b65d4adbSMuchun Song * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap pages. 1818b65d4adbSMuchun Song * 1819b65d4adbSMuchun Song * Only call schedule_work() if hpage_freelist is previously 1820b65d4adbSMuchun Song * empty. Otherwise, schedule_work() had been called but the workfn 1821b65d4adbSMuchun Song * hasn't retrieved the list yet. 1822b65d4adbSMuchun Song */ 1823d6ef19e2SSidhartha Kumar if (llist_add((struct llist_node *)&folio->mapping, &hpage_freelist)) 1824b65d4adbSMuchun Song schedule_work(&free_hpage_work); 1825b65d4adbSMuchun Song } 1826b65d4adbSMuchun Song 182710c6ec49SMike Kravetz static void update_and_free_pages_bulk(struct hstate *h, struct list_head *list) 182810c6ec49SMike Kravetz { 182910c6ec49SMike Kravetz struct page *page, *t_page; 1830d6ef19e2SSidhartha Kumar struct folio *folio; 183110c6ec49SMike Kravetz 183210c6ec49SMike Kravetz list_for_each_entry_safe(page, t_page, list, lru) { 1833d6ef19e2SSidhartha Kumar folio = page_folio(page); 1834d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, folio, false); 183510c6ec49SMike Kravetz cond_resched(); 183610c6ec49SMike Kravetz } 183710c6ec49SMike Kravetz } 183810c6ec49SMike Kravetz 1839e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size) 1840e5ff2159SAndi Kleen { 1841e5ff2159SAndi Kleen struct hstate *h; 1842e5ff2159SAndi Kleen 1843e5ff2159SAndi Kleen for_each_hstate(h) { 1844e5ff2159SAndi Kleen if (huge_page_size(h) == size) 1845e5ff2159SAndi Kleen return h; 1846e5ff2159SAndi Kleen } 1847e5ff2159SAndi Kleen return NULL; 1848e5ff2159SAndi Kleen } 1849e5ff2159SAndi Kleen 1850db71ef79SMike Kravetz void free_huge_page(struct page *page) 185127a85ef1SDavid Gibson { 1852a5516438SAndi Kleen /* 1853a5516438SAndi Kleen * Can't pass hstate in here because it is called from the 1854a5516438SAndi Kleen * compound page destructor. 1855a5516438SAndi Kleen */ 18560356c4b9SSidhartha Kumar struct folio *folio = page_folio(page); 18570356c4b9SSidhartha Kumar struct hstate *h = folio_hstate(folio); 18580356c4b9SSidhartha Kumar int nid = folio_nid(folio); 18590356c4b9SSidhartha Kumar struct hugepage_subpool *spool = hugetlb_folio_subpool(folio); 186007443a85SJoonsoo Kim bool restore_reserve; 1861db71ef79SMike Kravetz unsigned long flags; 186227a85ef1SDavid Gibson 18630356c4b9SSidhartha Kumar VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); 18640356c4b9SSidhartha Kumar VM_BUG_ON_FOLIO(folio_mapcount(folio), folio); 18658ace22bcSYongkai Wu 18660356c4b9SSidhartha Kumar hugetlb_set_folio_subpool(folio, NULL); 18670356c4b9SSidhartha Kumar if (folio_test_anon(folio)) 18680356c4b9SSidhartha Kumar __ClearPageAnonExclusive(&folio->page); 18690356c4b9SSidhartha Kumar folio->mapping = NULL; 18700356c4b9SSidhartha Kumar restore_reserve = folio_test_hugetlb_restore_reserve(folio); 18710356c4b9SSidhartha Kumar folio_clear_hugetlb_restore_reserve(folio); 187227a85ef1SDavid Gibson 18731c5ecae3SMike Kravetz /* 1874d6995da3SMike Kravetz * If HPageRestoreReserve was set on page, page allocation consumed a 18750919e1b6SMike Kravetz * reservation. If the page was associated with a subpool, there 18760919e1b6SMike Kravetz * would have been a page reserved in the subpool before allocation 18770919e1b6SMike Kravetz * via hugepage_subpool_get_pages(). Since we are 'restoring' the 18786c26d310SMiaohe Lin * reservation, do not call hugepage_subpool_put_pages() as this will 18790919e1b6SMike Kravetz * remove the reserved page from the subpool. 18800919e1b6SMike Kravetz */ 18810919e1b6SMike Kravetz if (!restore_reserve) { 18820919e1b6SMike Kravetz /* 18830919e1b6SMike Kravetz * A return code of zero implies that the subpool will be 18840919e1b6SMike Kravetz * under its minimum size if the reservation is not restored 18850919e1b6SMike Kravetz * after page is free. Therefore, force restore_reserve 18860919e1b6SMike Kravetz * operation. 18871c5ecae3SMike Kravetz */ 18881c5ecae3SMike Kravetz if (hugepage_subpool_put_pages(spool, 1) == 0) 18891c5ecae3SMike Kravetz restore_reserve = true; 18900919e1b6SMike Kravetz } 18911c5ecae3SMike Kravetz 1892db71ef79SMike Kravetz spin_lock_irqsave(&hugetlb_lock, flags); 18930356c4b9SSidhartha Kumar folio_clear_hugetlb_migratable(folio); 1894d4ab0316SSidhartha Kumar hugetlb_cgroup_uncharge_folio(hstate_index(h), 1895d4ab0316SSidhartha Kumar pages_per_huge_page(h), folio); 1896d4ab0316SSidhartha Kumar hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h), 1897d4ab0316SSidhartha Kumar pages_per_huge_page(h), folio); 189807443a85SJoonsoo Kim if (restore_reserve) 189907443a85SJoonsoo Kim h->resv_huge_pages++; 190007443a85SJoonsoo Kim 19010356c4b9SSidhartha Kumar if (folio_test_hugetlb_temporary(folio)) { 1902cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, folio, false); 1903db71ef79SMike Kravetz spin_unlock_irqrestore(&hugetlb_lock, flags); 1904d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, folio, true); 1905ab5ac90aSMichal Hocko } else if (h->surplus_huge_pages_node[nid]) { 19060edaecfaSAneesh Kumar K.V /* remove the page from active list */ 1907cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, folio, true); 1908db71ef79SMike Kravetz spin_unlock_irqrestore(&hugetlb_lock, flags); 1909d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, folio, true); 19107893d1d5SAdam Litke } else { 19115d3a551cSWill Deacon arch_clear_hugepage_flags(page); 1912240d67a8SSidhartha Kumar enqueue_hugetlb_folio(h, folio); 1913db71ef79SMike Kravetz spin_unlock_irqrestore(&hugetlb_lock, flags); 191427a85ef1SDavid Gibson } 19151121828aSMike Kravetz } 191627a85ef1SDavid Gibson 1917d3d99fccSOscar Salvador /* 1918d3d99fccSOscar Salvador * Must be called with the hugetlb lock held 1919d3d99fccSOscar Salvador */ 1920d3d99fccSOscar Salvador static void __prep_account_new_huge_page(struct hstate *h, int nid) 1921d3d99fccSOscar Salvador { 1922d3d99fccSOscar Salvador lockdep_assert_held(&hugetlb_lock); 1923d3d99fccSOscar Salvador h->nr_huge_pages++; 1924d3d99fccSOscar Salvador h->nr_huge_pages_node[nid]++; 1925d3d99fccSOscar Salvador } 1926d3d99fccSOscar Salvador 1927de656ed3SSidhartha Kumar static void __prep_new_hugetlb_folio(struct hstate *h, struct folio *folio) 1928b7ba30c6SAndi Kleen { 1929de656ed3SSidhartha Kumar hugetlb_vmemmap_optimize(h, &folio->page); 1930de656ed3SSidhartha Kumar INIT_LIST_HEAD(&folio->lru); 19319fd33058SSidhartha Kumar folio_set_compound_dtor(folio, HUGETLB_PAGE_DTOR); 1932de656ed3SSidhartha Kumar hugetlb_set_folio_subpool(folio, NULL); 1933de656ed3SSidhartha Kumar set_hugetlb_cgroup(folio, NULL); 1934de656ed3SSidhartha Kumar set_hugetlb_cgroup_rsvd(folio, NULL); 1935d3d99fccSOscar Salvador } 1936d3d99fccSOscar Salvador 1937d1c60955SSidhartha Kumar static void prep_new_hugetlb_folio(struct hstate *h, struct folio *folio, int nid) 1938d3d99fccSOscar Salvador { 1939de656ed3SSidhartha Kumar __prep_new_hugetlb_folio(h, folio); 1940db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 1941d3d99fccSOscar Salvador __prep_account_new_huge_page(h, nid); 1942db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 1943b7ba30c6SAndi Kleen } 1944b7ba30c6SAndi Kleen 1945d1c60955SSidhartha Kumar static bool __prep_compound_gigantic_folio(struct folio *folio, 1946d1c60955SSidhartha Kumar unsigned int order, bool demote) 194720a0307cSWu Fengguang { 19487118fc29SMike Kravetz int i, j; 194920a0307cSWu Fengguang int nr_pages = 1 << order; 195014455eabSCheng Li struct page *p; 195120a0307cSWu Fengguang 1952d1c60955SSidhartha Kumar __folio_clear_reserved(folio); 19532b21624fSMike Kravetz for (i = 0; i < nr_pages; i++) { 1954d1c60955SSidhartha Kumar p = folio_page(folio, i); 195514455eabSCheng Li 1956ef5a22beSAndrea Arcangeli /* 1957ef5a22beSAndrea Arcangeli * For gigantic hugepages allocated through bootmem at 1958ef5a22beSAndrea Arcangeli * boot, it's safer to be consistent with the not-gigantic 1959ef5a22beSAndrea Arcangeli * hugepages and clear the PG_reserved bit from all tail pages 19607c8de358SEthon Paul * too. Otherwise drivers using get_user_pages() to access tail 1961ef5a22beSAndrea Arcangeli * pages may get the reference counting wrong if they see 1962ef5a22beSAndrea Arcangeli * PG_reserved set on a tail page (despite the head page not 1963ef5a22beSAndrea Arcangeli * having PG_reserved set). Enforcing this consistency between 1964ef5a22beSAndrea Arcangeli * head and tail pages allows drivers to optimize away a check 1965ef5a22beSAndrea Arcangeli * on the head page when they need know if put_page() is needed 1966ef5a22beSAndrea Arcangeli * after get_user_pages(). 1967ef5a22beSAndrea Arcangeli */ 19687fb0728aSMike Kravetz if (i != 0) /* head page cleared above */ 1969ef5a22beSAndrea Arcangeli __ClearPageReserved(p); 19707118fc29SMike Kravetz /* 19717118fc29SMike Kravetz * Subtle and very unlikely 19727118fc29SMike Kravetz * 19737118fc29SMike Kravetz * Gigantic 'page allocators' such as memblock or cma will 19747118fc29SMike Kravetz * return a set of pages with each page ref counted. We need 19757118fc29SMike Kravetz * to turn this set of pages into a compound page with tail 19767118fc29SMike Kravetz * page ref counts set to zero. Code such as speculative page 19777118fc29SMike Kravetz * cache adding could take a ref on a 'to be' tail page. 19787118fc29SMike Kravetz * We need to respect any increased ref count, and only set 19797118fc29SMike Kravetz * the ref count to zero if count is currently 1. If count 1980416d85edSMike Kravetz * is not 1, we return an error. An error return indicates 1981416d85edSMike Kravetz * the set of pages can not be converted to a gigantic page. 1982416d85edSMike Kravetz * The caller who allocated the pages should then discard the 1983416d85edSMike Kravetz * pages using the appropriate free interface. 198434d9e35bSMike Kravetz * 198534d9e35bSMike Kravetz * In the case of demote, the ref count will be zero. 19867118fc29SMike Kravetz */ 198734d9e35bSMike Kravetz if (!demote) { 19887118fc29SMike Kravetz if (!page_ref_freeze(p, 1)) { 1989416d85edSMike Kravetz pr_warn("HugeTLB page can not be used due to unexpected inflated ref count\n"); 19907118fc29SMike Kravetz goto out_error; 19917118fc29SMike Kravetz } 199234d9e35bSMike Kravetz } else { 199334d9e35bSMike Kravetz VM_BUG_ON_PAGE(page_count(p), p); 199434d9e35bSMike Kravetz } 19952b21624fSMike Kravetz if (i != 0) 1996d1c60955SSidhartha Kumar set_compound_head(p, &folio->page); 199720a0307cSWu Fengguang } 1998e3b7bf97STarun Sahu __folio_set_head(folio); 1999e3b7bf97STarun Sahu /* we rely on prep_new_hugetlb_folio to set the destructor */ 2000e3b7bf97STarun Sahu folio_set_order(folio, order); 200146f27228SMatthew Wilcox (Oracle) atomic_set(&folio->_entire_mapcount, -1); 2002eec20426SMatthew Wilcox (Oracle) atomic_set(&folio->_nr_pages_mapped, 0); 200394688e8eSMatthew Wilcox (Oracle) atomic_set(&folio->_pincount, 0); 20047118fc29SMike Kravetz return true; 20057118fc29SMike Kravetz 20067118fc29SMike Kravetz out_error: 20072b21624fSMike Kravetz /* undo page modifications made above */ 20082b21624fSMike Kravetz for (j = 0; j < i; j++) { 2009d1c60955SSidhartha Kumar p = folio_page(folio, j); 20102b21624fSMike Kravetz if (j != 0) 20117118fc29SMike Kravetz clear_compound_head(p); 20127118fc29SMike Kravetz set_page_refcounted(p); 20137118fc29SMike Kravetz } 20147118fc29SMike Kravetz /* need to clear PG_reserved on remaining tail pages */ 201514455eabSCheng Li for (; j < nr_pages; j++) { 2016d1c60955SSidhartha Kumar p = folio_page(folio, j); 20177118fc29SMike Kravetz __ClearPageReserved(p); 201814455eabSCheng Li } 20197118fc29SMike Kravetz return false; 202020a0307cSWu Fengguang } 202120a0307cSWu Fengguang 2022d1c60955SSidhartha Kumar static bool prep_compound_gigantic_folio(struct folio *folio, 20238531fc6fSMike Kravetz unsigned int order) 20248531fc6fSMike Kravetz { 2025d1c60955SSidhartha Kumar return __prep_compound_gigantic_folio(folio, order, false); 2026d1c60955SSidhartha Kumar } 2027d1c60955SSidhartha Kumar 2028d1c60955SSidhartha Kumar static bool prep_compound_gigantic_folio_for_demote(struct folio *folio, 2029d1c60955SSidhartha Kumar unsigned int order) 2030d1c60955SSidhartha Kumar { 2031d1c60955SSidhartha Kumar return __prep_compound_gigantic_folio(folio, order, true); 20328531fc6fSMike Kravetz } 20338531fc6fSMike Kravetz 20347795912cSAndrew Morton /* 20357795912cSAndrew Morton * PageHuge() only returns true for hugetlbfs pages, but not for normal or 20367795912cSAndrew Morton * transparent huge pages. See the PageTransHuge() documentation for more 20377795912cSAndrew Morton * details. 20387795912cSAndrew Morton */ 203920a0307cSWu Fengguang int PageHuge(struct page *page) 204020a0307cSWu Fengguang { 20412d678c64SMatthew Wilcox (Oracle) struct folio *folio; 20422d678c64SMatthew Wilcox (Oracle) 204320a0307cSWu Fengguang if (!PageCompound(page)) 204420a0307cSWu Fengguang return 0; 20452d678c64SMatthew Wilcox (Oracle) folio = page_folio(page); 20462d678c64SMatthew Wilcox (Oracle) return folio->_folio_dtor == HUGETLB_PAGE_DTOR; 204720a0307cSWu Fengguang } 204843131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge); 204943131e14SNaoya Horiguchi 2050957ebbdfSMatthew Wilcox (Oracle) /** 2051957ebbdfSMatthew Wilcox (Oracle) * folio_test_hugetlb - Determine if the folio belongs to hugetlbfs 2052957ebbdfSMatthew Wilcox (Oracle) * @folio: The folio to test. 2053957ebbdfSMatthew Wilcox (Oracle) * 2054957ebbdfSMatthew Wilcox (Oracle) * Context: Any context. Caller should have a reference on the folio to 2055957ebbdfSMatthew Wilcox (Oracle) * prevent it from being turned into a tail page. 2056957ebbdfSMatthew Wilcox (Oracle) * Return: True for hugetlbfs folios, false for anon folios or folios 2057957ebbdfSMatthew Wilcox (Oracle) * belonging to other filesystems. 205827c73ae7SAndrea Arcangeli */ 2059957ebbdfSMatthew Wilcox (Oracle) bool folio_test_hugetlb(struct folio *folio) 206027c73ae7SAndrea Arcangeli { 20612d678c64SMatthew Wilcox (Oracle) if (!folio_test_large(folio)) 2062957ebbdfSMatthew Wilcox (Oracle) return false; 206327c73ae7SAndrea Arcangeli 20642d678c64SMatthew Wilcox (Oracle) return folio->_folio_dtor == HUGETLB_PAGE_DTOR; 206527c73ae7SAndrea Arcangeli } 2066957ebbdfSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(folio_test_hugetlb); 206727c73ae7SAndrea Arcangeli 2068c0d0381aSMike Kravetz /* 2069c0d0381aSMike Kravetz * Find and lock address space (mapping) in write mode. 2070c0d0381aSMike Kravetz * 2071336bf30eSMike Kravetz * Upon entry, the page is locked which means that page_mapping() is 2072336bf30eSMike Kravetz * stable. Due to locking order, we can only trylock_write. If we can 2073336bf30eSMike Kravetz * not get the lock, simply return NULL to caller. 2074c0d0381aSMike Kravetz */ 2075c0d0381aSMike Kravetz struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage) 2076c0d0381aSMike Kravetz { 2077336bf30eSMike Kravetz struct address_space *mapping = page_mapping(hpage); 2078c0d0381aSMike Kravetz 2079c0d0381aSMike Kravetz if (!mapping) 2080c0d0381aSMike Kravetz return mapping; 2081c0d0381aSMike Kravetz 2082c0d0381aSMike Kravetz if (i_mmap_trylock_write(mapping)) 2083c0d0381aSMike Kravetz return mapping; 2084c0d0381aSMike Kravetz 2085c0d0381aSMike Kravetz return NULL; 2086c0d0381aSMike Kravetz } 2087c0d0381aSMike Kravetz 2088fe19bd3dSHugh Dickins pgoff_t hugetlb_basepage_index(struct page *page) 208913d60f4bSZhang Yi { 209013d60f4bSZhang Yi struct page *page_head = compound_head(page); 209113d60f4bSZhang Yi pgoff_t index = page_index(page_head); 209213d60f4bSZhang Yi unsigned long compound_idx; 209313d60f4bSZhang Yi 209423baf831SKirill A. Shutemov if (compound_order(page_head) > MAX_ORDER) 209513d60f4bSZhang Yi compound_idx = page_to_pfn(page) - page_to_pfn(page_head); 209613d60f4bSZhang Yi else 209713d60f4bSZhang Yi compound_idx = page - page_head; 209813d60f4bSZhang Yi 209913d60f4bSZhang Yi return (index << compound_order(page_head)) + compound_idx; 210013d60f4bSZhang Yi } 210113d60f4bSZhang Yi 210219fc1a7eSSidhartha Kumar static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, 2103f60858f9SMike Kravetz gfp_t gfp_mask, int nid, nodemask_t *nmask, 2104f60858f9SMike Kravetz nodemask_t *node_alloc_noretry) 21051da177e4SLinus Torvalds { 2106af0fb9dfSMichal Hocko int order = huge_page_order(h); 21071da177e4SLinus Torvalds struct page *page; 2108f60858f9SMike Kravetz bool alloc_try_hard = true; 21092b21624fSMike Kravetz bool retry = true; 2110f96efd58SJoe Jin 2111f60858f9SMike Kravetz /* 2112f60858f9SMike Kravetz * By default we always try hard to allocate the page with 2113f60858f9SMike Kravetz * __GFP_RETRY_MAYFAIL flag. However, if we are allocating pages in 2114f60858f9SMike Kravetz * a loop (to adjust global huge page counts) and previous allocation 2115f60858f9SMike Kravetz * failed, do not continue to try hard on the same node. Use the 2116f60858f9SMike Kravetz * node_alloc_noretry bitmap to manage this state information. 2117f60858f9SMike Kravetz */ 2118f60858f9SMike Kravetz if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry)) 2119f60858f9SMike Kravetz alloc_try_hard = false; 2120f60858f9SMike Kravetz gfp_mask |= __GFP_COMP|__GFP_NOWARN; 2121f60858f9SMike Kravetz if (alloc_try_hard) 2122f60858f9SMike Kravetz gfp_mask |= __GFP_RETRY_MAYFAIL; 2123af0fb9dfSMichal Hocko if (nid == NUMA_NO_NODE) 2124af0fb9dfSMichal Hocko nid = numa_mem_id(); 21252b21624fSMike Kravetz retry: 212684172f4bSMatthew Wilcox (Oracle) page = __alloc_pages(gfp_mask, order, nid, nmask); 21272b21624fSMike Kravetz 21282b21624fSMike Kravetz /* Freeze head page */ 21292b21624fSMike Kravetz if (page && !page_ref_freeze(page, 1)) { 21302b21624fSMike Kravetz __free_pages(page, order); 21312b21624fSMike Kravetz if (retry) { /* retry once */ 21322b21624fSMike Kravetz retry = false; 21332b21624fSMike Kravetz goto retry; 21342b21624fSMike Kravetz } 21352b21624fSMike Kravetz /* WOW! twice in a row. */ 21362b21624fSMike Kravetz pr_warn("HugeTLB head page unexpected inflated ref count\n"); 21372b21624fSMike Kravetz page = NULL; 21382b21624fSMike Kravetz } 21392b21624fSMike Kravetz 2140f60858f9SMike Kravetz /* 2141f60858f9SMike Kravetz * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this 2142f60858f9SMike Kravetz * indicates an overall state change. Clear bit so that we resume 2143f60858f9SMike Kravetz * normal 'try hard' allocations. 2144f60858f9SMike Kravetz */ 2145f60858f9SMike Kravetz if (node_alloc_noretry && page && !alloc_try_hard) 2146f60858f9SMike Kravetz node_clear(nid, *node_alloc_noretry); 2147f60858f9SMike Kravetz 2148f60858f9SMike Kravetz /* 2149f60858f9SMike Kravetz * If we tried hard to get a page but failed, set bit so that 2150f60858f9SMike Kravetz * subsequent attempts will not try as hard until there is an 2151f60858f9SMike Kravetz * overall state change. 2152f60858f9SMike Kravetz */ 2153f60858f9SMike Kravetz if (node_alloc_noretry && !page && alloc_try_hard) 2154f60858f9SMike Kravetz node_set(nid, *node_alloc_noretry); 2155f60858f9SMike Kravetz 215619fc1a7eSSidhartha Kumar if (!page) { 215719fc1a7eSSidhartha Kumar __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 215819fc1a7eSSidhartha Kumar return NULL; 215919fc1a7eSSidhartha Kumar } 216019fc1a7eSSidhartha Kumar 216119fc1a7eSSidhartha Kumar __count_vm_event(HTLB_BUDDY_PGALLOC); 216219fc1a7eSSidhartha Kumar return page_folio(page); 216363b4613cSNishanth Aravamudan } 216463b4613cSNishanth Aravamudan 2165af0fb9dfSMichal Hocko /* 21660c397daeSMichal Hocko * Common helper to allocate a fresh hugetlb page. All specific allocators 21670c397daeSMichal Hocko * should use this function to get new hugetlb pages 21682b21624fSMike Kravetz * 21692b21624fSMike Kravetz * Note that returned page is 'frozen': ref count of head page and all tail 21702b21624fSMike Kravetz * pages is zero. 21710c397daeSMichal Hocko */ 217219fc1a7eSSidhartha Kumar static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h, 2173f60858f9SMike Kravetz gfp_t gfp_mask, int nid, nodemask_t *nmask, 2174f60858f9SMike Kravetz nodemask_t *node_alloc_noretry) 21750c397daeSMichal Hocko { 21767f325a8dSSidhartha Kumar struct folio *folio; 21777118fc29SMike Kravetz bool retry = false; 21780c397daeSMichal Hocko 21797118fc29SMike Kravetz retry: 21800c397daeSMichal Hocko if (hstate_is_gigantic(h)) 218119fc1a7eSSidhartha Kumar folio = alloc_gigantic_folio(h, gfp_mask, nid, nmask); 21820c397daeSMichal Hocko else 218319fc1a7eSSidhartha Kumar folio = alloc_buddy_hugetlb_folio(h, gfp_mask, 2184f60858f9SMike Kravetz nid, nmask, node_alloc_noretry); 218519fc1a7eSSidhartha Kumar if (!folio) 21860c397daeSMichal Hocko return NULL; 21877118fc29SMike Kravetz if (hstate_is_gigantic(h)) { 2188d1c60955SSidhartha Kumar if (!prep_compound_gigantic_folio(folio, huge_page_order(h))) { 21897118fc29SMike Kravetz /* 21907118fc29SMike Kravetz * Rare failure to convert pages to compound page. 21917118fc29SMike Kravetz * Free pages and try again - ONCE! 21927118fc29SMike Kravetz */ 21937f325a8dSSidhartha Kumar free_gigantic_folio(folio, huge_page_order(h)); 21947118fc29SMike Kravetz if (!retry) { 21957118fc29SMike Kravetz retry = true; 21967118fc29SMike Kravetz goto retry; 21977118fc29SMike Kravetz } 21987118fc29SMike Kravetz return NULL; 21997118fc29SMike Kravetz } 22007118fc29SMike Kravetz } 2201d1c60955SSidhartha Kumar prep_new_hugetlb_folio(h, folio, folio_nid(folio)); 22020c397daeSMichal Hocko 220319fc1a7eSSidhartha Kumar return folio; 22040c397daeSMichal Hocko } 22050c397daeSMichal Hocko 22060c397daeSMichal Hocko /* 2207af0fb9dfSMichal Hocko * Allocates a fresh page to the hugetlb allocator pool in the node interleaved 2208af0fb9dfSMichal Hocko * manner. 2209af0fb9dfSMichal Hocko */ 2210f60858f9SMike Kravetz static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, 2211f60858f9SMike Kravetz nodemask_t *node_alloc_noretry) 2212b2261026SJoonsoo Kim { 221319fc1a7eSSidhartha Kumar struct folio *folio; 2214b2261026SJoonsoo Kim int nr_nodes, node; 2215af0fb9dfSMichal Hocko gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; 2216b2261026SJoonsoo Kim 2217b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 221819fc1a7eSSidhartha Kumar folio = alloc_fresh_hugetlb_folio(h, gfp_mask, node, 221919fc1a7eSSidhartha Kumar nodes_allowed, node_alloc_noretry); 222019fc1a7eSSidhartha Kumar if (folio) { 222119fc1a7eSSidhartha Kumar free_huge_page(&folio->page); /* free it into the hugepage allocator */ 222219fc1a7eSSidhartha Kumar return 1; 222319fc1a7eSSidhartha Kumar } 2224b2261026SJoonsoo Kim } 2225b2261026SJoonsoo Kim 2226af0fb9dfSMichal Hocko return 0; 2227b2261026SJoonsoo Kim } 2228b2261026SJoonsoo Kim 2229e8c5c824SLee Schermerhorn /* 223010c6ec49SMike Kravetz * Remove huge page from pool from next node to free. Attempt to keep 223110c6ec49SMike Kravetz * persistent huge pages more or less balanced over allowed nodes. 223210c6ec49SMike Kravetz * This routine only 'removes' the hugetlb page. The caller must make 223310c6ec49SMike Kravetz * an additional call to free the page to low level allocators. 2234e8c5c824SLee Schermerhorn * Called with hugetlb_lock locked. 2235e8c5c824SLee Schermerhorn */ 223610c6ec49SMike Kravetz static struct page *remove_pool_huge_page(struct hstate *h, 223710c6ec49SMike Kravetz nodemask_t *nodes_allowed, 22386ae11b27SLee Schermerhorn bool acct_surplus) 2239e8c5c824SLee Schermerhorn { 2240b2261026SJoonsoo Kim int nr_nodes, node; 224110c6ec49SMike Kravetz struct page *page = NULL; 2242cfd5082bSSidhartha Kumar struct folio *folio; 2243e8c5c824SLee Schermerhorn 22449487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 2245b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 2246685f3457SLee Schermerhorn /* 2247685f3457SLee Schermerhorn * If we're returning unused surplus pages, only examine 2248685f3457SLee Schermerhorn * nodes with surplus pages. 2249685f3457SLee Schermerhorn */ 2250b2261026SJoonsoo Kim if ((!acct_surplus || h->surplus_huge_pages_node[node]) && 2251b2261026SJoonsoo Kim !list_empty(&h->hugepage_freelists[node])) { 225210c6ec49SMike Kravetz page = list_entry(h->hugepage_freelists[node].next, 2253e8c5c824SLee Schermerhorn struct page, lru); 2254cfd5082bSSidhartha Kumar folio = page_folio(page); 2255cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, folio, acct_surplus); 22569a76db09SLee Schermerhorn break; 2257e8c5c824SLee Schermerhorn } 2258b2261026SJoonsoo Kim } 2259e8c5c824SLee Schermerhorn 226010c6ec49SMike Kravetz return page; 2261e8c5c824SLee Schermerhorn } 2262e8c5c824SLee Schermerhorn 2263c8721bbbSNaoya Horiguchi /* 2264c8721bbbSNaoya Horiguchi * Dissolve a given free hugepage into free buddy pages. This function does 2265faf53defSNaoya Horiguchi * nothing for in-use hugepages and non-hugepages. 2266faf53defSNaoya Horiguchi * This function returns values like below: 2267faf53defSNaoya Horiguchi * 2268ad2fa371SMuchun Song * -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages 2269ad2fa371SMuchun Song * when the system is under memory pressure and the feature of 2270ad2fa371SMuchun Song * freeing unused vmemmap pages associated with each hugetlb page 2271ad2fa371SMuchun Song * is enabled. 2272faf53defSNaoya Horiguchi * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use 2273faf53defSNaoya Horiguchi * (allocated or reserved.) 2274faf53defSNaoya Horiguchi * 0: successfully dissolved free hugepages or the page is not a 2275faf53defSNaoya Horiguchi * hugepage (considered as already dissolved) 2276c8721bbbSNaoya Horiguchi */ 2277c3114a84SAnshuman Khandual int dissolve_free_huge_page(struct page *page) 2278c8721bbbSNaoya Horiguchi { 22796bc9b564SNaoya Horiguchi int rc = -EBUSY; 22801a7cdab5SSidhartha Kumar struct folio *folio = page_folio(page); 2281082d5b6bSGerald Schaefer 22827ffddd49SMuchun Song retry: 2283faf53defSNaoya Horiguchi /* Not to disrupt normal path by vainly holding hugetlb_lock */ 22841a7cdab5SSidhartha Kumar if (!folio_test_hugetlb(folio)) 2285faf53defSNaoya Horiguchi return 0; 2286faf53defSNaoya Horiguchi 2287db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 22881a7cdab5SSidhartha Kumar if (!folio_test_hugetlb(folio)) { 2289faf53defSNaoya Horiguchi rc = 0; 2290faf53defSNaoya Horiguchi goto out; 2291faf53defSNaoya Horiguchi } 2292faf53defSNaoya Horiguchi 22931a7cdab5SSidhartha Kumar if (!folio_ref_count(folio)) { 22941a7cdab5SSidhartha Kumar struct hstate *h = folio_hstate(folio); 22958346d69dSXin Hao if (!available_huge_pages(h)) 2296082d5b6bSGerald Schaefer goto out; 22977ffddd49SMuchun Song 22987ffddd49SMuchun Song /* 22997ffddd49SMuchun Song * We should make sure that the page is already on the free list 23007ffddd49SMuchun Song * when it is dissolved. 23017ffddd49SMuchun Song */ 23021a7cdab5SSidhartha Kumar if (unlikely(!folio_test_hugetlb_freed(folio))) { 2303db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 23047ffddd49SMuchun Song cond_resched(); 23057ffddd49SMuchun Song 23067ffddd49SMuchun Song /* 23077ffddd49SMuchun Song * Theoretically, we should return -EBUSY when we 23087ffddd49SMuchun Song * encounter this race. In fact, we have a chance 23097ffddd49SMuchun Song * to successfully dissolve the page if we do a 23107ffddd49SMuchun Song * retry. Because the race window is quite small. 23117ffddd49SMuchun Song * If we seize this opportunity, it is an optimization 23127ffddd49SMuchun Song * for increasing the success rate of dissolving page. 23137ffddd49SMuchun Song */ 23147ffddd49SMuchun Song goto retry; 23157ffddd49SMuchun Song } 23167ffddd49SMuchun Song 2317cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, folio, false); 2318ad2fa371SMuchun Song h->max_huge_pages--; 2319ad2fa371SMuchun Song spin_unlock_irq(&hugetlb_lock); 2320ad2fa371SMuchun Song 2321c3114a84SAnshuman Khandual /* 2322d6ef19e2SSidhartha Kumar * Normally update_and_free_hugtlb_folio will allocate required vmemmmap 2323d6ef19e2SSidhartha Kumar * before freeing the page. update_and_free_hugtlb_folio will fail to 2324ad2fa371SMuchun Song * free the page if it can not allocate required vmemmap. We 2325ad2fa371SMuchun Song * need to adjust max_huge_pages if the page is not freed. 2326ad2fa371SMuchun Song * Attempt to allocate vmemmmap here so that we can take 2327ad2fa371SMuchun Song * appropriate action on failure. 2328ad2fa371SMuchun Song */ 23291a7cdab5SSidhartha Kumar rc = hugetlb_vmemmap_restore(h, &folio->page); 2330ad2fa371SMuchun Song if (!rc) { 2331d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, folio, false); 2332ad2fa371SMuchun Song } else { 2333ad2fa371SMuchun Song spin_lock_irq(&hugetlb_lock); 23342f6c57d6SSidhartha Kumar add_hugetlb_folio(h, folio, false); 2335ad2fa371SMuchun Song h->max_huge_pages++; 2336ad2fa371SMuchun Song spin_unlock_irq(&hugetlb_lock); 2337ad2fa371SMuchun Song } 2338ad2fa371SMuchun Song 2339ad2fa371SMuchun Song return rc; 2340c8721bbbSNaoya Horiguchi } 2341082d5b6bSGerald Schaefer out: 2342db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 2343082d5b6bSGerald Schaefer return rc; 2344c8721bbbSNaoya Horiguchi } 2345c8721bbbSNaoya Horiguchi 2346c8721bbbSNaoya Horiguchi /* 2347c8721bbbSNaoya Horiguchi * Dissolve free hugepages in a given pfn range. Used by memory hotplug to 2348c8721bbbSNaoya Horiguchi * make specified memory blocks removable from the system. 23492247bb33SGerald Schaefer * Note that this will dissolve a free gigantic hugepage completely, if any 23502247bb33SGerald Schaefer * part of it lies within the given range. 2351082d5b6bSGerald Schaefer * Also note that if dissolve_free_huge_page() returns with an error, all 2352082d5b6bSGerald Schaefer * free hugepages that were dissolved before that error are lost. 2353c8721bbbSNaoya Horiguchi */ 2354082d5b6bSGerald Schaefer int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) 2355c8721bbbSNaoya Horiguchi { 2356c8721bbbSNaoya Horiguchi unsigned long pfn; 2357eb03aa00SGerald Schaefer struct page *page; 2358082d5b6bSGerald Schaefer int rc = 0; 2359dc2628f3SMuchun Song unsigned int order; 2360dc2628f3SMuchun Song struct hstate *h; 2361c8721bbbSNaoya Horiguchi 2362d0177639SLi Zhong if (!hugepages_supported()) 2363082d5b6bSGerald Schaefer return rc; 2364d0177639SLi Zhong 2365dc2628f3SMuchun Song order = huge_page_order(&default_hstate); 2366dc2628f3SMuchun Song for_each_hstate(h) 2367dc2628f3SMuchun Song order = min(order, huge_page_order(h)); 2368dc2628f3SMuchun Song 2369dc2628f3SMuchun Song for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) { 2370eb03aa00SGerald Schaefer page = pfn_to_page(pfn); 2371eb03aa00SGerald Schaefer rc = dissolve_free_huge_page(page); 2372eb03aa00SGerald Schaefer if (rc) 2373082d5b6bSGerald Schaefer break; 2374eb03aa00SGerald Schaefer } 2375082d5b6bSGerald Schaefer 2376082d5b6bSGerald Schaefer return rc; 2377c8721bbbSNaoya Horiguchi } 2378c8721bbbSNaoya Horiguchi 2379ab5ac90aSMichal Hocko /* 2380ab5ac90aSMichal Hocko * Allocates a fresh surplus page from the page allocator. 2381ab5ac90aSMichal Hocko */ 23823a740e8bSSidhartha Kumar static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h, 23833a740e8bSSidhartha Kumar gfp_t gfp_mask, int nid, nodemask_t *nmask) 23847893d1d5SAdam Litke { 238519fc1a7eSSidhartha Kumar struct folio *folio = NULL; 23867893d1d5SAdam Litke 2387bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 2388aa888a74SAndi Kleen return NULL; 2389aa888a74SAndi Kleen 2390db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 23919980d744SMichal Hocko if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) 23929980d744SMichal Hocko goto out_unlock; 2393db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 2394d1c3fb1fSNishanth Aravamudan 239519fc1a7eSSidhartha Kumar folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); 239619fc1a7eSSidhartha Kumar if (!folio) 23970c397daeSMichal Hocko return NULL; 2398d1c3fb1fSNishanth Aravamudan 2399db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 24009980d744SMichal Hocko /* 24019980d744SMichal Hocko * We could have raced with the pool size change. 24029980d744SMichal Hocko * Double check that and simply deallocate the new page 24039980d744SMichal Hocko * if we would end up overcommiting the surpluses. Abuse 24049980d744SMichal Hocko * temporary page to workaround the nasty free_huge_page 24059980d744SMichal Hocko * codeflow 24069980d744SMichal Hocko */ 24079980d744SMichal Hocko if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { 240819fc1a7eSSidhartha Kumar folio_set_hugetlb_temporary(folio); 2409db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 241019fc1a7eSSidhartha Kumar free_huge_page(&folio->page); 24112bf753e6SKai Shen return NULL; 2412b65a4edaSMike Kravetz } 2413b65a4edaSMike Kravetz 24149980d744SMichal Hocko h->surplus_huge_pages++; 241519fc1a7eSSidhartha Kumar h->surplus_huge_pages_node[folio_nid(folio)]++; 24169980d744SMichal Hocko 24179980d744SMichal Hocko out_unlock: 2418db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 24197893d1d5SAdam Litke 24203a740e8bSSidhartha Kumar return folio; 24217893d1d5SAdam Litke } 24227893d1d5SAdam Litke 2423e37d3e83SSidhartha Kumar static struct folio *alloc_migrate_hugetlb_folio(struct hstate *h, gfp_t gfp_mask, 2424ab5ac90aSMichal Hocko int nid, nodemask_t *nmask) 2425ab5ac90aSMichal Hocko { 242619fc1a7eSSidhartha Kumar struct folio *folio; 2427ab5ac90aSMichal Hocko 2428ab5ac90aSMichal Hocko if (hstate_is_gigantic(h)) 2429ab5ac90aSMichal Hocko return NULL; 2430ab5ac90aSMichal Hocko 243119fc1a7eSSidhartha Kumar folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); 243219fc1a7eSSidhartha Kumar if (!folio) 2433ab5ac90aSMichal Hocko return NULL; 2434ab5ac90aSMichal Hocko 24352b21624fSMike Kravetz /* fresh huge pages are frozen */ 243619fc1a7eSSidhartha Kumar folio_ref_unfreeze(folio, 1); 2437ab5ac90aSMichal Hocko /* 2438ab5ac90aSMichal Hocko * We do not account these pages as surplus because they are only 2439ab5ac90aSMichal Hocko * temporary and will be released properly on the last reference 2440ab5ac90aSMichal Hocko */ 244119fc1a7eSSidhartha Kumar folio_set_hugetlb_temporary(folio); 2442ab5ac90aSMichal Hocko 2443e37d3e83SSidhartha Kumar return folio; 2444ab5ac90aSMichal Hocko } 2445ab5ac90aSMichal Hocko 2446e4e574b7SAdam Litke /* 2447099730d6SDave Hansen * Use the VMA's mpolicy to allocate a huge page from the buddy. 2448099730d6SDave Hansen */ 2449e0ec90eeSDave Hansen static 2450ff7d853bSSidhartha Kumar struct folio *alloc_buddy_hugetlb_folio_with_mpol(struct hstate *h, 2451099730d6SDave Hansen struct vm_area_struct *vma, unsigned long addr) 2452099730d6SDave Hansen { 24533a740e8bSSidhartha Kumar struct folio *folio = NULL; 2454aaf14e40SMichal Hocko struct mempolicy *mpol; 2455aaf14e40SMichal Hocko gfp_t gfp_mask = htlb_alloc_mask(h); 2456aaf14e40SMichal Hocko int nid; 2457aaf14e40SMichal Hocko nodemask_t *nodemask; 2458aaf14e40SMichal Hocko 2459aaf14e40SMichal Hocko nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask); 2460cfcaa66fSBen Widawsky if (mpol_is_preferred_many(mpol)) { 2461cfcaa66fSBen Widawsky gfp_t gfp = gfp_mask | __GFP_NOWARN; 2462cfcaa66fSBen Widawsky 2463cfcaa66fSBen Widawsky gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); 24643a740e8bSSidhartha Kumar folio = alloc_surplus_hugetlb_folio(h, gfp, nid, nodemask); 2465cfcaa66fSBen Widawsky 2466cfcaa66fSBen Widawsky /* Fallback to all nodes if page==NULL */ 2467cfcaa66fSBen Widawsky nodemask = NULL; 2468cfcaa66fSBen Widawsky } 2469cfcaa66fSBen Widawsky 24703a740e8bSSidhartha Kumar if (!folio) 24713a740e8bSSidhartha Kumar folio = alloc_surplus_hugetlb_folio(h, gfp_mask, nid, nodemask); 2472aaf14e40SMichal Hocko mpol_cond_put(mpol); 2473ff7d853bSSidhartha Kumar return folio; 2474099730d6SDave Hansen } 2475099730d6SDave Hansen 2476e37d3e83SSidhartha Kumar /* folio migration callback function */ 2477e37d3e83SSidhartha Kumar struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid, 2478d92bbc27SJoonsoo Kim nodemask_t *nmask, gfp_t gfp_mask) 24794db9b2efSMichal Hocko { 2480db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 24818346d69dSXin Hao if (available_huge_pages(h)) { 2482a36f1e90SSidhartha Kumar struct folio *folio; 24833e59fcb0SMichal Hocko 2484a36f1e90SSidhartha Kumar folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, 2485a36f1e90SSidhartha Kumar preferred_nid, nmask); 2486a36f1e90SSidhartha Kumar if (folio) { 2487db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 2488e37d3e83SSidhartha Kumar return folio; 24894db9b2efSMichal Hocko } 24904db9b2efSMichal Hocko } 2491db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 24924db9b2efSMichal Hocko 2493e37d3e83SSidhartha Kumar return alloc_migrate_hugetlb_folio(h, gfp_mask, preferred_nid, nmask); 24944db9b2efSMichal Hocko } 24954db9b2efSMichal Hocko 2496ebd63723SMichal Hocko /* mempolicy aware migration callback */ 2497d0ce0e47SSidhartha Kumar struct folio *alloc_hugetlb_folio_vma(struct hstate *h, struct vm_area_struct *vma, 2498389c8178SMichal Hocko unsigned long address) 2499ebd63723SMichal Hocko { 2500ebd63723SMichal Hocko struct mempolicy *mpol; 2501ebd63723SMichal Hocko nodemask_t *nodemask; 2502e37d3e83SSidhartha Kumar struct folio *folio; 2503ebd63723SMichal Hocko gfp_t gfp_mask; 2504ebd63723SMichal Hocko int node; 2505ebd63723SMichal Hocko 2506ebd63723SMichal Hocko gfp_mask = htlb_alloc_mask(h); 2507ebd63723SMichal Hocko node = huge_node(vma, address, gfp_mask, &mpol, &nodemask); 2508e37d3e83SSidhartha Kumar folio = alloc_hugetlb_folio_nodemask(h, node, nodemask, gfp_mask); 2509ebd63723SMichal Hocko mpol_cond_put(mpol); 2510ebd63723SMichal Hocko 2511d0ce0e47SSidhartha Kumar return folio; 2512ebd63723SMichal Hocko } 2513ebd63723SMichal Hocko 2514bf50bab2SNaoya Horiguchi /* 251525985edcSLucas De Marchi * Increase the hugetlb pool such that it can accommodate a reservation 2516e4e574b7SAdam Litke * of size 'delta'. 2517e4e574b7SAdam Litke */ 25180a4f3d1bSLiu Xiang static int gather_surplus_pages(struct hstate *h, long delta) 25191b2a1e7bSJules Irenge __must_hold(&hugetlb_lock) 2520e4e574b7SAdam Litke { 252134665341SMiaohe Lin LIST_HEAD(surplus_list); 25223a740e8bSSidhartha Kumar struct folio *folio; 2523e4e574b7SAdam Litke struct page *page, *tmp; 25240a4f3d1bSLiu Xiang int ret; 25250a4f3d1bSLiu Xiang long i; 25260a4f3d1bSLiu Xiang long needed, allocated; 252728073b02SHillf Danton bool alloc_ok = true; 2528e4e574b7SAdam Litke 25299487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 2530a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - h->free_huge_pages; 2531ac09b3a1SAdam Litke if (needed <= 0) { 2532a5516438SAndi Kleen h->resv_huge_pages += delta; 2533e4e574b7SAdam Litke return 0; 2534ac09b3a1SAdam Litke } 2535e4e574b7SAdam Litke 2536e4e574b7SAdam Litke allocated = 0; 2537e4e574b7SAdam Litke 2538e4e574b7SAdam Litke ret = -ENOMEM; 2539e4e574b7SAdam Litke retry: 2540db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 2541e4e574b7SAdam Litke for (i = 0; i < needed; i++) { 25423a740e8bSSidhartha Kumar folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h), 25432b21624fSMike Kravetz NUMA_NO_NODE, NULL); 25443a740e8bSSidhartha Kumar if (!folio) { 254528073b02SHillf Danton alloc_ok = false; 254628073b02SHillf Danton break; 254728073b02SHillf Danton } 25483a740e8bSSidhartha Kumar list_add(&folio->lru, &surplus_list); 254969ed779aSDavid Rientjes cond_resched(); 2550e4e574b7SAdam Litke } 255128073b02SHillf Danton allocated += i; 2552e4e574b7SAdam Litke 2553e4e574b7SAdam Litke /* 2554e4e574b7SAdam Litke * After retaking hugetlb_lock, we need to recalculate 'needed' 2555e4e574b7SAdam Litke * because either resv_huge_pages or free_huge_pages may have changed. 2556e4e574b7SAdam Litke */ 2557db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 2558a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - 2559a5516438SAndi Kleen (h->free_huge_pages + allocated); 256028073b02SHillf Danton if (needed > 0) { 256128073b02SHillf Danton if (alloc_ok) 2562e4e574b7SAdam Litke goto retry; 256328073b02SHillf Danton /* 256428073b02SHillf Danton * We were not able to allocate enough pages to 256528073b02SHillf Danton * satisfy the entire reservation so we free what 256628073b02SHillf Danton * we've allocated so far. 256728073b02SHillf Danton */ 256828073b02SHillf Danton goto free; 256928073b02SHillf Danton } 2570e4e574b7SAdam Litke /* 2571e4e574b7SAdam Litke * The surplus_list now contains _at_least_ the number of extra pages 257225985edcSLucas De Marchi * needed to accommodate the reservation. Add the appropriate number 2573e4e574b7SAdam Litke * of pages to the hugetlb pool and free the extras back to the buddy 2574ac09b3a1SAdam Litke * allocator. Commit the entire reservation here to prevent another 2575ac09b3a1SAdam Litke * process from stealing the pages as they are added to the pool but 2576ac09b3a1SAdam Litke * before they are reserved. 2577e4e574b7SAdam Litke */ 2578e4e574b7SAdam Litke needed += allocated; 2579a5516438SAndi Kleen h->resv_huge_pages += delta; 2580e4e574b7SAdam Litke ret = 0; 2581a9869b83SNaoya Horiguchi 258219fc3f0aSAdam Litke /* Free the needed pages to the hugetlb pool */ 258319fc3f0aSAdam Litke list_for_each_entry_safe(page, tmp, &surplus_list, lru) { 258419fc3f0aSAdam Litke if ((--needed) < 0) 258519fc3f0aSAdam Litke break; 2586b65a4edaSMike Kravetz /* Add the page to the hugetlb allocator */ 2587240d67a8SSidhartha Kumar enqueue_hugetlb_folio(h, page_folio(page)); 258819fc3f0aSAdam Litke } 258928073b02SHillf Danton free: 2590db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 259119fc3f0aSAdam Litke 2592b65a4edaSMike Kravetz /* 2593b65a4edaSMike Kravetz * Free unnecessary surplus pages to the buddy allocator. 2594b65a4edaSMike Kravetz * Pages have no ref count, call free_huge_page directly. 2595b65a4edaSMike Kravetz */ 2596c0d934baSJoonsoo Kim list_for_each_entry_safe(page, tmp, &surplus_list, lru) 2597b65a4edaSMike Kravetz free_huge_page(page); 2598db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 2599e4e574b7SAdam Litke 2600e4e574b7SAdam Litke return ret; 2601e4e574b7SAdam Litke } 2602e4e574b7SAdam Litke 2603e4e574b7SAdam Litke /* 2604e5bbc8a6SMike Kravetz * This routine has two main purposes: 2605e5bbc8a6SMike Kravetz * 1) Decrement the reservation count (resv_huge_pages) by the value passed 2606e5bbc8a6SMike Kravetz * in unused_resv_pages. This corresponds to the prior adjustments made 2607e5bbc8a6SMike Kravetz * to the associated reservation map. 2608e5bbc8a6SMike Kravetz * 2) Free any unused surplus pages that may have been allocated to satisfy 2609e5bbc8a6SMike Kravetz * the reservation. As many as unused_resv_pages may be freed. 2610e4e574b7SAdam Litke */ 2611a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h, 2612a5516438SAndi Kleen unsigned long unused_resv_pages) 2613e4e574b7SAdam Litke { 2614e4e574b7SAdam Litke unsigned long nr_pages; 261510c6ec49SMike Kravetz struct page *page; 261610c6ec49SMike Kravetz LIST_HEAD(page_list); 261710c6ec49SMike Kravetz 26189487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 261910c6ec49SMike Kravetz /* Uncommit the reservation */ 262010c6ec49SMike Kravetz h->resv_huge_pages -= unused_resv_pages; 2621e4e574b7SAdam Litke 2622c0531714SNaoya Horiguchi if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 2623e5bbc8a6SMike Kravetz goto out; 2624aa888a74SAndi Kleen 2625e5bbc8a6SMike Kravetz /* 2626e5bbc8a6SMike Kravetz * Part (or even all) of the reservation could have been backed 2627e5bbc8a6SMike Kravetz * by pre-allocated pages. Only free surplus pages. 2628e5bbc8a6SMike Kravetz */ 2629a5516438SAndi Kleen nr_pages = min(unused_resv_pages, h->surplus_huge_pages); 2630e4e574b7SAdam Litke 2631685f3457SLee Schermerhorn /* 2632685f3457SLee Schermerhorn * We want to release as many surplus pages as possible, spread 26339b5e5d0fSLee Schermerhorn * evenly across all nodes with memory. Iterate across these nodes 26349b5e5d0fSLee Schermerhorn * until we can no longer free unreserved surplus pages. This occurs 26359b5e5d0fSLee Schermerhorn * when the nodes with surplus pages have no free pages. 263610c6ec49SMike Kravetz * remove_pool_huge_page() will balance the freed pages across the 26379b5e5d0fSLee Schermerhorn * on-line nodes with memory and will handle the hstate accounting. 2638685f3457SLee Schermerhorn */ 2639685f3457SLee Schermerhorn while (nr_pages--) { 264010c6ec49SMike Kravetz page = remove_pool_huge_page(h, &node_states[N_MEMORY], 1); 264110c6ec49SMike Kravetz if (!page) 2642e5bbc8a6SMike Kravetz goto out; 264310c6ec49SMike Kravetz 264410c6ec49SMike Kravetz list_add(&page->lru, &page_list); 2645e4e574b7SAdam Litke } 2646e5bbc8a6SMike Kravetz 2647e5bbc8a6SMike Kravetz out: 2648db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 264910c6ec49SMike Kravetz update_and_free_pages_bulk(h, &page_list); 2650db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 2651e4e574b7SAdam Litke } 2652e4e574b7SAdam Litke 26535e911373SMike Kravetz 2654c37f9fb1SAndy Whitcroft /* 2655feba16e2SMike Kravetz * vma_needs_reservation, vma_commit_reservation and vma_end_reservation 26565e911373SMike Kravetz * are used by the huge page allocation routines to manage reservations. 2657cf3ad20bSMike Kravetz * 2658cf3ad20bSMike Kravetz * vma_needs_reservation is called to determine if the huge page at addr 2659cf3ad20bSMike Kravetz * within the vma has an associated reservation. If a reservation is 2660cf3ad20bSMike Kravetz * needed, the value 1 is returned. The caller is then responsible for 2661cf3ad20bSMike Kravetz * managing the global reservation and subpool usage counts. After 2662cf3ad20bSMike Kravetz * the huge page has been allocated, vma_commit_reservation is called 2663feba16e2SMike Kravetz * to add the page to the reservation map. If the page allocation fails, 2664feba16e2SMike Kravetz * the reservation must be ended instead of committed. vma_end_reservation 2665feba16e2SMike Kravetz * is called in such cases. 2666cf3ad20bSMike Kravetz * 2667cf3ad20bSMike Kravetz * In the normal case, vma_commit_reservation returns the same value 2668cf3ad20bSMike Kravetz * as the preceding vma_needs_reservation call. The only time this 2669cf3ad20bSMike Kravetz * is not the case is if a reserve map was changed between calls. It 2670cf3ad20bSMike Kravetz * is the responsibility of the caller to notice the difference and 2671cf3ad20bSMike Kravetz * take appropriate action. 267296b96a96SMike Kravetz * 267396b96a96SMike Kravetz * vma_add_reservation is used in error paths where a reservation must 267496b96a96SMike Kravetz * be restored when a newly allocated huge page must be freed. It is 267596b96a96SMike Kravetz * to be called after calling vma_needs_reservation to determine if a 267696b96a96SMike Kravetz * reservation exists. 2677846be085SMike Kravetz * 2678846be085SMike Kravetz * vma_del_reservation is used in error paths where an entry in the reserve 2679846be085SMike Kravetz * map was created during huge page allocation and must be removed. It is to 2680846be085SMike Kravetz * be called after calling vma_needs_reservation to determine if a reservation 2681846be085SMike Kravetz * exists. 2682c37f9fb1SAndy Whitcroft */ 26835e911373SMike Kravetz enum vma_resv_mode { 26845e911373SMike Kravetz VMA_NEEDS_RESV, 26855e911373SMike Kravetz VMA_COMMIT_RESV, 2686feba16e2SMike Kravetz VMA_END_RESV, 268796b96a96SMike Kravetz VMA_ADD_RESV, 2688846be085SMike Kravetz VMA_DEL_RESV, 26895e911373SMike Kravetz }; 2690cf3ad20bSMike Kravetz static long __vma_reservation_common(struct hstate *h, 2691cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr, 26925e911373SMike Kravetz enum vma_resv_mode mode) 2693c37f9fb1SAndy Whitcroft { 26944e35f483SJoonsoo Kim struct resv_map *resv; 26954e35f483SJoonsoo Kim pgoff_t idx; 2696cf3ad20bSMike Kravetz long ret; 26970db9d74eSMina Almasry long dummy_out_regions_needed; 2698c37f9fb1SAndy Whitcroft 26994e35f483SJoonsoo Kim resv = vma_resv_map(vma); 27004e35f483SJoonsoo Kim if (!resv) 2701c37f9fb1SAndy Whitcroft return 1; 2702c37f9fb1SAndy Whitcroft 27034e35f483SJoonsoo Kim idx = vma_hugecache_offset(h, vma, addr); 27045e911373SMike Kravetz switch (mode) { 27055e911373SMike Kravetz case VMA_NEEDS_RESV: 27060db9d74eSMina Almasry ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed); 27070db9d74eSMina Almasry /* We assume that vma_reservation_* routines always operate on 27080db9d74eSMina Almasry * 1 page, and that adding to resv map a 1 page entry can only 27090db9d74eSMina Almasry * ever require 1 region. 27100db9d74eSMina Almasry */ 27110db9d74eSMina Almasry VM_BUG_ON(dummy_out_regions_needed != 1); 27125e911373SMike Kravetz break; 27135e911373SMike Kravetz case VMA_COMMIT_RESV: 2714075a61d0SMina Almasry ret = region_add(resv, idx, idx + 1, 1, NULL, NULL); 27150db9d74eSMina Almasry /* region_add calls of range 1 should never fail. */ 27160db9d74eSMina Almasry VM_BUG_ON(ret < 0); 27175e911373SMike Kravetz break; 2718feba16e2SMike Kravetz case VMA_END_RESV: 27190db9d74eSMina Almasry region_abort(resv, idx, idx + 1, 1); 27205e911373SMike Kravetz ret = 0; 27215e911373SMike Kravetz break; 272296b96a96SMike Kravetz case VMA_ADD_RESV: 27230db9d74eSMina Almasry if (vma->vm_flags & VM_MAYSHARE) { 2724075a61d0SMina Almasry ret = region_add(resv, idx, idx + 1, 1, NULL, NULL); 27250db9d74eSMina Almasry /* region_add calls of range 1 should never fail. */ 27260db9d74eSMina Almasry VM_BUG_ON(ret < 0); 27270db9d74eSMina Almasry } else { 27280db9d74eSMina Almasry region_abort(resv, idx, idx + 1, 1); 272996b96a96SMike Kravetz ret = region_del(resv, idx, idx + 1); 273096b96a96SMike Kravetz } 273196b96a96SMike Kravetz break; 2732846be085SMike Kravetz case VMA_DEL_RESV: 2733846be085SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 2734846be085SMike Kravetz region_abort(resv, idx, idx + 1, 1); 2735846be085SMike Kravetz ret = region_del(resv, idx, idx + 1); 2736846be085SMike Kravetz } else { 2737846be085SMike Kravetz ret = region_add(resv, idx, idx + 1, 1, NULL, NULL); 2738846be085SMike Kravetz /* region_add calls of range 1 should never fail. */ 2739846be085SMike Kravetz VM_BUG_ON(ret < 0); 2740846be085SMike Kravetz } 2741846be085SMike Kravetz break; 27425e911373SMike Kravetz default: 27435e911373SMike Kravetz BUG(); 27445e911373SMike Kravetz } 274584afd99bSAndy Whitcroft 2746846be085SMike Kravetz if (vma->vm_flags & VM_MAYSHARE || mode == VMA_DEL_RESV) 2747cf3ad20bSMike Kravetz return ret; 274867961f9dSMike Kravetz /* 2749bf3d12b9SMiaohe Lin * We know private mapping must have HPAGE_RESV_OWNER set. 2750bf3d12b9SMiaohe Lin * 275167961f9dSMike Kravetz * In most cases, reserves always exist for private mappings. 275267961f9dSMike Kravetz * However, a file associated with mapping could have been 275367961f9dSMike Kravetz * hole punched or truncated after reserves were consumed. 275467961f9dSMike Kravetz * As subsequent fault on such a range will not use reserves. 275567961f9dSMike Kravetz * Subtle - The reserve map for private mappings has the 275667961f9dSMike Kravetz * opposite meaning than that of shared mappings. If NO 275767961f9dSMike Kravetz * entry is in the reserve map, it means a reservation exists. 275867961f9dSMike Kravetz * If an entry exists in the reserve map, it means the 275967961f9dSMike Kravetz * reservation has already been consumed. As a result, the 276067961f9dSMike Kravetz * return value of this routine is the opposite of the 276167961f9dSMike Kravetz * value returned from reserve map manipulation routines above. 276267961f9dSMike Kravetz */ 2763bf3d12b9SMiaohe Lin if (ret > 0) 276467961f9dSMike Kravetz return 0; 2765bf3d12b9SMiaohe Lin if (ret == 0) 276667961f9dSMike Kravetz return 1; 2767bf3d12b9SMiaohe Lin return ret; 276884afd99bSAndy Whitcroft } 2769cf3ad20bSMike Kravetz 2770cf3ad20bSMike Kravetz static long vma_needs_reservation(struct hstate *h, 2771a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long addr) 2772c37f9fb1SAndy Whitcroft { 27735e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV); 2774cf3ad20bSMike Kravetz } 2775c37f9fb1SAndy Whitcroft 2776cf3ad20bSMike Kravetz static long vma_commit_reservation(struct hstate *h, 2777cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr) 2778cf3ad20bSMike Kravetz { 27795e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV); 27805e911373SMike Kravetz } 27815e911373SMike Kravetz 2782feba16e2SMike Kravetz static void vma_end_reservation(struct hstate *h, 27835e911373SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 27845e911373SMike Kravetz { 2785feba16e2SMike Kravetz (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV); 2786c37f9fb1SAndy Whitcroft } 2787c37f9fb1SAndy Whitcroft 278896b96a96SMike Kravetz static long vma_add_reservation(struct hstate *h, 278996b96a96SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 279096b96a96SMike Kravetz { 279196b96a96SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV); 279296b96a96SMike Kravetz } 279396b96a96SMike Kravetz 2794846be085SMike Kravetz static long vma_del_reservation(struct hstate *h, 2795846be085SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 279696b96a96SMike Kravetz { 2797846be085SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_DEL_RESV); 2798846be085SMike Kravetz } 2799846be085SMike Kravetz 2800846be085SMike Kravetz /* 2801846be085SMike Kravetz * This routine is called to restore reservation information on error paths. 2802d0ce0e47SSidhartha Kumar * It should ONLY be called for folios allocated via alloc_hugetlb_folio(), 2803d0ce0e47SSidhartha Kumar * and the hugetlb mutex should remain held when calling this routine. 2804846be085SMike Kravetz * 2805846be085SMike Kravetz * It handles two specific cases: 2806d2d7bb44SSidhartha Kumar * 1) A reservation was in place and the folio consumed the reservation. 2807d2d7bb44SSidhartha Kumar * hugetlb_restore_reserve is set in the folio. 2808d2d7bb44SSidhartha Kumar * 2) No reservation was in place for the page, so hugetlb_restore_reserve is 2809d0ce0e47SSidhartha Kumar * not set. However, alloc_hugetlb_folio always updates the reserve map. 2810846be085SMike Kravetz * 2811846be085SMike Kravetz * In case 1, free_huge_page later in the error path will increment the 2812846be085SMike Kravetz * global reserve count. But, free_huge_page does not have enough context 2813846be085SMike Kravetz * to adjust the reservation map. This case deals primarily with private 2814846be085SMike Kravetz * mappings. Adjust the reserve map here to be consistent with global 2815846be085SMike Kravetz * reserve count adjustments to be made by free_huge_page. Make sure the 2816846be085SMike Kravetz * reserve map indicates there is a reservation present. 2817846be085SMike Kravetz * 2818d0ce0e47SSidhartha Kumar * In case 2, simply undo reserve map modifications done by alloc_hugetlb_folio. 2819846be085SMike Kravetz */ 2820846be085SMike Kravetz void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, 2821d2d7bb44SSidhartha Kumar unsigned long address, struct folio *folio) 2822846be085SMike Kravetz { 282396b96a96SMike Kravetz long rc = vma_needs_reservation(h, vma, address); 282496b96a96SMike Kravetz 28250ffdc38eSSidhartha Kumar if (folio_test_hugetlb_restore_reserve(folio)) { 2826846be085SMike Kravetz if (unlikely(rc < 0)) 282796b96a96SMike Kravetz /* 282896b96a96SMike Kravetz * Rare out of memory condition in reserve map 28290ffdc38eSSidhartha Kumar * manipulation. Clear hugetlb_restore_reserve so 28300ffdc38eSSidhartha Kumar * that global reserve count will not be incremented 283196b96a96SMike Kravetz * by free_huge_page. This will make it appear 28320ffdc38eSSidhartha Kumar * as though the reservation for this folio was 283396b96a96SMike Kravetz * consumed. This may prevent the task from 28340ffdc38eSSidhartha Kumar * faulting in the folio at a later time. This 283596b96a96SMike Kravetz * is better than inconsistent global huge page 283696b96a96SMike Kravetz * accounting of reserve counts. 283796b96a96SMike Kravetz */ 28380ffdc38eSSidhartha Kumar folio_clear_hugetlb_restore_reserve(folio); 2839846be085SMike Kravetz else if (rc) 2840846be085SMike Kravetz (void)vma_add_reservation(h, vma, address); 2841846be085SMike Kravetz else 2842846be085SMike Kravetz vma_end_reservation(h, vma, address); 2843846be085SMike Kravetz } else { 2844846be085SMike Kravetz if (!rc) { 284596b96a96SMike Kravetz /* 2846846be085SMike Kravetz * This indicates there is an entry in the reserve map 2847d0ce0e47SSidhartha Kumar * not added by alloc_hugetlb_folio. We know it was added 2848d0ce0e47SSidhartha Kumar * before the alloc_hugetlb_folio call, otherwise 28490ffdc38eSSidhartha Kumar * hugetlb_restore_reserve would be set on the folio. 2850846be085SMike Kravetz * Remove the entry so that a subsequent allocation 2851846be085SMike Kravetz * does not consume a reservation. 285296b96a96SMike Kravetz */ 2853846be085SMike Kravetz rc = vma_del_reservation(h, vma, address); 2854846be085SMike Kravetz if (rc < 0) 2855846be085SMike Kravetz /* 2856846be085SMike Kravetz * VERY rare out of memory condition. Since 2857846be085SMike Kravetz * we can not delete the entry, set 28580ffdc38eSSidhartha Kumar * hugetlb_restore_reserve so that the reserve 28590ffdc38eSSidhartha Kumar * count will be incremented when the folio 2860846be085SMike Kravetz * is freed. This reserve will be consumed 2861846be085SMike Kravetz * on a subsequent allocation. 2862846be085SMike Kravetz */ 28630ffdc38eSSidhartha Kumar folio_set_hugetlb_restore_reserve(folio); 2864846be085SMike Kravetz } else if (rc < 0) { 2865846be085SMike Kravetz /* 2866846be085SMike Kravetz * Rare out of memory condition from 2867846be085SMike Kravetz * vma_needs_reservation call. Memory allocation is 2868846be085SMike Kravetz * only attempted if a new entry is needed. Therefore, 2869846be085SMike Kravetz * this implies there is not an entry in the 2870846be085SMike Kravetz * reserve map. 2871846be085SMike Kravetz * 2872846be085SMike Kravetz * For shared mappings, no entry in the map indicates 2873846be085SMike Kravetz * no reservation. We are done. 2874846be085SMike Kravetz */ 2875846be085SMike Kravetz if (!(vma->vm_flags & VM_MAYSHARE)) 2876846be085SMike Kravetz /* 2877846be085SMike Kravetz * For private mappings, no entry indicates 2878846be085SMike Kravetz * a reservation is present. Since we can 28790ffdc38eSSidhartha Kumar * not add an entry, set hugetlb_restore_reserve 28800ffdc38eSSidhartha Kumar * on the folio so reserve count will be 2881846be085SMike Kravetz * incremented when freed. This reserve will 2882846be085SMike Kravetz * be consumed on a subsequent allocation. 2883846be085SMike Kravetz */ 28840ffdc38eSSidhartha Kumar folio_set_hugetlb_restore_reserve(folio); 288596b96a96SMike Kravetz } else 2886846be085SMike Kravetz /* 2887846be085SMike Kravetz * No reservation present, do nothing 2888846be085SMike Kravetz */ 288996b96a96SMike Kravetz vma_end_reservation(h, vma, address); 289096b96a96SMike Kravetz } 289196b96a96SMike Kravetz } 289296b96a96SMike Kravetz 2893369fa227SOscar Salvador /* 289419fc1a7eSSidhartha Kumar * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve 289519fc1a7eSSidhartha Kumar * the old one 2896369fa227SOscar Salvador * @h: struct hstate old page belongs to 289719fc1a7eSSidhartha Kumar * @old_folio: Old folio to dissolve 2898ae37c7ffSOscar Salvador * @list: List to isolate the page in case we need to 2899369fa227SOscar Salvador * Returns 0 on success, otherwise negated error. 2900369fa227SOscar Salvador */ 290119fc1a7eSSidhartha Kumar static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, 290219fc1a7eSSidhartha Kumar struct folio *old_folio, struct list_head *list) 2903369fa227SOscar Salvador { 2904369fa227SOscar Salvador gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; 2905de656ed3SSidhartha Kumar int nid = folio_nid(old_folio); 2906de656ed3SSidhartha Kumar struct folio *new_folio; 2907369fa227SOscar Salvador int ret = 0; 2908369fa227SOscar Salvador 2909369fa227SOscar Salvador /* 291019fc1a7eSSidhartha Kumar * Before dissolving the folio, we need to allocate a new one for the 291119fc1a7eSSidhartha Kumar * pool to remain stable. Here, we allocate the folio and 'prep' it 2912f41f2ed4SMuchun Song * by doing everything but actually updating counters and adding to 2913f41f2ed4SMuchun Song * the pool. This simplifies and let us do most of the processing 2914f41f2ed4SMuchun Song * under the lock. 2915369fa227SOscar Salvador */ 291619fc1a7eSSidhartha Kumar new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); 291719fc1a7eSSidhartha Kumar if (!new_folio) 2918369fa227SOscar Salvador return -ENOMEM; 2919de656ed3SSidhartha Kumar __prep_new_hugetlb_folio(h, new_folio); 2920369fa227SOscar Salvador 2921369fa227SOscar Salvador retry: 2922369fa227SOscar Salvador spin_lock_irq(&hugetlb_lock); 2923de656ed3SSidhartha Kumar if (!folio_test_hugetlb(old_folio)) { 2924369fa227SOscar Salvador /* 292519fc1a7eSSidhartha Kumar * Freed from under us. Drop new_folio too. 2926369fa227SOscar Salvador */ 2927369fa227SOscar Salvador goto free_new; 2928de656ed3SSidhartha Kumar } else if (folio_ref_count(old_folio)) { 29299747b9e9SBaolin Wang bool isolated; 29309747b9e9SBaolin Wang 2931369fa227SOscar Salvador /* 293219fc1a7eSSidhartha Kumar * Someone has grabbed the folio, try to isolate it here. 2933ae37c7ffSOscar Salvador * Fail with -EBUSY if not possible. 2934369fa227SOscar Salvador */ 2935ae37c7ffSOscar Salvador spin_unlock_irq(&hugetlb_lock); 29369747b9e9SBaolin Wang isolated = isolate_hugetlb(old_folio, list); 29379747b9e9SBaolin Wang ret = isolated ? 0 : -EBUSY; 2938ae37c7ffSOscar Salvador spin_lock_irq(&hugetlb_lock); 2939369fa227SOscar Salvador goto free_new; 2940de656ed3SSidhartha Kumar } else if (!folio_test_hugetlb_freed(old_folio)) { 2941369fa227SOscar Salvador /* 294219fc1a7eSSidhartha Kumar * Folio's refcount is 0 but it has not been enqueued in the 2943369fa227SOscar Salvador * freelist yet. Race window is small, so we can succeed here if 2944369fa227SOscar Salvador * we retry. 2945369fa227SOscar Salvador */ 2946369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 2947369fa227SOscar Salvador cond_resched(); 2948369fa227SOscar Salvador goto retry; 2949369fa227SOscar Salvador } else { 2950369fa227SOscar Salvador /* 295119fc1a7eSSidhartha Kumar * Ok, old_folio is still a genuine free hugepage. Remove it from 2952369fa227SOscar Salvador * the freelist and decrease the counters. These will be 2953369fa227SOscar Salvador * incremented again when calling __prep_account_new_huge_page() 2954240d67a8SSidhartha Kumar * and enqueue_hugetlb_folio() for new_folio. The counters will 2955240d67a8SSidhartha Kumar * remain stable since this happens under the lock. 2956369fa227SOscar Salvador */ 2957cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, old_folio, false); 2958369fa227SOscar Salvador 2959369fa227SOscar Salvador /* 296019fc1a7eSSidhartha Kumar * Ref count on new_folio is already zero as it was dropped 2961b65a4edaSMike Kravetz * earlier. It can be directly added to the pool free list. 2962369fa227SOscar Salvador */ 2963369fa227SOscar Salvador __prep_account_new_huge_page(h, nid); 2964240d67a8SSidhartha Kumar enqueue_hugetlb_folio(h, new_folio); 2965369fa227SOscar Salvador 2966369fa227SOscar Salvador /* 296719fc1a7eSSidhartha Kumar * Folio has been replaced, we can safely free the old one. 2968369fa227SOscar Salvador */ 2969369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 2970d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, old_folio, false); 2971369fa227SOscar Salvador } 2972369fa227SOscar Salvador 2973369fa227SOscar Salvador return ret; 2974369fa227SOscar Salvador 2975369fa227SOscar Salvador free_new: 2976369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 297719fc1a7eSSidhartha Kumar /* Folio has a zero ref count, but needs a ref to be freed */ 2978de656ed3SSidhartha Kumar folio_ref_unfreeze(new_folio, 1); 2979d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, new_folio, false); 2980369fa227SOscar Salvador 2981369fa227SOscar Salvador return ret; 2982369fa227SOscar Salvador } 2983369fa227SOscar Salvador 2984ae37c7ffSOscar Salvador int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list) 2985369fa227SOscar Salvador { 2986369fa227SOscar Salvador struct hstate *h; 2987d5e33bd8SSidhartha Kumar struct folio *folio = page_folio(page); 2988ae37c7ffSOscar Salvador int ret = -EBUSY; 2989369fa227SOscar Salvador 2990369fa227SOscar Salvador /* 2991369fa227SOscar Salvador * The page might have been dissolved from under our feet, so make sure 2992369fa227SOscar Salvador * to carefully check the state under the lock. 2993369fa227SOscar Salvador * Return success when racing as if we dissolved the page ourselves. 2994369fa227SOscar Salvador */ 2995369fa227SOscar Salvador spin_lock_irq(&hugetlb_lock); 2996d5e33bd8SSidhartha Kumar if (folio_test_hugetlb(folio)) { 2997d5e33bd8SSidhartha Kumar h = folio_hstate(folio); 2998369fa227SOscar Salvador } else { 2999369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 3000369fa227SOscar Salvador return 0; 3001369fa227SOscar Salvador } 3002369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 3003369fa227SOscar Salvador 3004369fa227SOscar Salvador /* 3005369fa227SOscar Salvador * Fence off gigantic pages as there is a cyclic dependency between 3006369fa227SOscar Salvador * alloc_contig_range and them. Return -ENOMEM as this has the effect 3007369fa227SOscar Salvador * of bailing out right away without further retrying. 3008369fa227SOscar Salvador */ 3009369fa227SOscar Salvador if (hstate_is_gigantic(h)) 3010369fa227SOscar Salvador return -ENOMEM; 3011369fa227SOscar Salvador 30129747b9e9SBaolin Wang if (folio_ref_count(folio) && isolate_hugetlb(folio, list)) 3013ae37c7ffSOscar Salvador ret = 0; 3014d5e33bd8SSidhartha Kumar else if (!folio_ref_count(folio)) 301519fc1a7eSSidhartha Kumar ret = alloc_and_dissolve_hugetlb_folio(h, folio, list); 3016ae37c7ffSOscar Salvador 3017ae37c7ffSOscar Salvador return ret; 3018369fa227SOscar Salvador } 3019369fa227SOscar Salvador 3020d0ce0e47SSidhartha Kumar struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, 302104f2cbe3SMel Gorman unsigned long addr, int avoid_reserve) 3022348ea204SAdam Litke { 302390481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 3024a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3025d4ab0316SSidhartha Kumar struct folio *folio; 3026d85f69b0SMike Kravetz long map_chg, map_commit; 3027d85f69b0SMike Kravetz long gbl_chg; 30286d76dcf4SAneesh Kumar K.V int ret, idx; 3029d0ce0e47SSidhartha Kumar struct hugetlb_cgroup *h_cg = NULL; 303008cf9fafSMina Almasry bool deferred_reserve; 30312fc39cecSAdam Litke 30326d76dcf4SAneesh Kumar K.V idx = hstate_index(h); 3033a1e78772SMel Gorman /* 3034d85f69b0SMike Kravetz * Examine the region/reserve map to determine if the process 3035d85f69b0SMike Kravetz * has a reservation for the page to be allocated. A return 3036d85f69b0SMike Kravetz * code of zero indicates a reservation exists (no change). 3037a1e78772SMel Gorman */ 3038d85f69b0SMike Kravetz map_chg = gbl_chg = vma_needs_reservation(h, vma, addr); 3039d85f69b0SMike Kravetz if (map_chg < 0) 304076dcee75SAneesh Kumar K.V return ERR_PTR(-ENOMEM); 3041d85f69b0SMike Kravetz 3042d85f69b0SMike Kravetz /* 3043d85f69b0SMike Kravetz * Processes that did not create the mapping will have no 3044d85f69b0SMike Kravetz * reserves as indicated by the region/reserve map. Check 3045d85f69b0SMike Kravetz * that the allocation will not exceed the subpool limit. 3046d85f69b0SMike Kravetz * Allocations for MAP_NORESERVE mappings also need to be 3047d85f69b0SMike Kravetz * checked against any subpool limit. 3048d85f69b0SMike Kravetz */ 3049d85f69b0SMike Kravetz if (map_chg || avoid_reserve) { 3050d85f69b0SMike Kravetz gbl_chg = hugepage_subpool_get_pages(spool, 1); 3051d85f69b0SMike Kravetz if (gbl_chg < 0) { 3052feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 305376dcee75SAneesh Kumar K.V return ERR_PTR(-ENOSPC); 30545e911373SMike Kravetz } 305590d8b7e6SAdam Litke 3056d85f69b0SMike Kravetz /* 3057d85f69b0SMike Kravetz * Even though there was no reservation in the region/reserve 3058d85f69b0SMike Kravetz * map, there could be reservations associated with the 3059d85f69b0SMike Kravetz * subpool that can be used. This would be indicated if the 3060d85f69b0SMike Kravetz * return value of hugepage_subpool_get_pages() is zero. 3061d85f69b0SMike Kravetz * However, if avoid_reserve is specified we still avoid even 3062d85f69b0SMike Kravetz * the subpool reservations. 3063d85f69b0SMike Kravetz */ 3064d85f69b0SMike Kravetz if (avoid_reserve) 3065d85f69b0SMike Kravetz gbl_chg = 1; 3066d85f69b0SMike Kravetz } 3067d85f69b0SMike Kravetz 306808cf9fafSMina Almasry /* If this allocation is not consuming a reservation, charge it now. 306908cf9fafSMina Almasry */ 30706501fe5fSMiaohe Lin deferred_reserve = map_chg || avoid_reserve; 307108cf9fafSMina Almasry if (deferred_reserve) { 307208cf9fafSMina Almasry ret = hugetlb_cgroup_charge_cgroup_rsvd( 307308cf9fafSMina Almasry idx, pages_per_huge_page(h), &h_cg); 30748f34af6fSJianyu Zhan if (ret) 30758f34af6fSJianyu Zhan goto out_subpool_put; 307608cf9fafSMina Almasry } 307708cf9fafSMina Almasry 307808cf9fafSMina Almasry ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); 307908cf9fafSMina Almasry if (ret) 308008cf9fafSMina Almasry goto out_uncharge_cgroup_reservation; 30818f34af6fSJianyu Zhan 3082db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 3083d85f69b0SMike Kravetz /* 3084d85f69b0SMike Kravetz * glb_chg is passed to indicate whether or not a page must be taken 3085d85f69b0SMike Kravetz * from the global free pool (global change). gbl_chg == 0 indicates 3086d85f69b0SMike Kravetz * a reservation exists for the allocation. 3087d85f69b0SMike Kravetz */ 3088ff7d853bSSidhartha Kumar folio = dequeue_hugetlb_folio_vma(h, vma, addr, avoid_reserve, gbl_chg); 3089ff7d853bSSidhartha Kumar if (!folio) { 3090db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 3091ff7d853bSSidhartha Kumar folio = alloc_buddy_hugetlb_folio_with_mpol(h, vma, addr); 3092ff7d853bSSidhartha Kumar if (!folio) 30938f34af6fSJianyu Zhan goto out_uncharge_cgroup; 309412df140fSRik van Riel spin_lock_irq(&hugetlb_lock); 3095a88c7695SNaoya Horiguchi if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) { 3096ff7d853bSSidhartha Kumar folio_set_hugetlb_restore_reserve(folio); 3097a88c7695SNaoya Horiguchi h->resv_huge_pages--; 3098a88c7695SNaoya Horiguchi } 3099ff7d853bSSidhartha Kumar list_add(&folio->lru, &h->hugepage_activelist); 3100ff7d853bSSidhartha Kumar folio_ref_unfreeze(folio, 1); 310181a6fcaeSJoonsoo Kim /* Fall through */ 3102a1e78772SMel Gorman } 3103ff7d853bSSidhartha Kumar 3104ff7d853bSSidhartha Kumar hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, folio); 310508cf9fafSMina Almasry /* If allocation is not consuming a reservation, also store the 310608cf9fafSMina Almasry * hugetlb_cgroup pointer on the page. 310708cf9fafSMina Almasry */ 310808cf9fafSMina Almasry if (deferred_reserve) { 310908cf9fafSMina Almasry hugetlb_cgroup_commit_charge_rsvd(idx, pages_per_huge_page(h), 3110ff7d853bSSidhartha Kumar h_cg, folio); 311108cf9fafSMina Almasry } 311208cf9fafSMina Almasry 3113db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 3114a1e78772SMel Gorman 3115ff7d853bSSidhartha Kumar hugetlb_set_folio_subpool(folio, spool); 3116a1e78772SMel Gorman 3117d85f69b0SMike Kravetz map_commit = vma_commit_reservation(h, vma, addr); 3118d85f69b0SMike Kravetz if (unlikely(map_chg > map_commit)) { 311933039678SMike Kravetz /* 312033039678SMike Kravetz * The page was added to the reservation map between 312133039678SMike Kravetz * vma_needs_reservation and vma_commit_reservation. 312233039678SMike Kravetz * This indicates a race with hugetlb_reserve_pages. 312333039678SMike Kravetz * Adjust for the subpool count incremented above AND 312433039678SMike Kravetz * in hugetlb_reserve_pages for the same page. Also, 312533039678SMike Kravetz * the reservation count added in hugetlb_reserve_pages 312633039678SMike Kravetz * no longer applies. 312733039678SMike Kravetz */ 312833039678SMike Kravetz long rsv_adjust; 312933039678SMike Kravetz 313033039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 1); 313133039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 313279aa925bSMike Kravetz if (deferred_reserve) 3133d4ab0316SSidhartha Kumar hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h), 3134d4ab0316SSidhartha Kumar pages_per_huge_page(h), folio); 313533039678SMike Kravetz } 3136d0ce0e47SSidhartha Kumar return folio; 31378f34af6fSJianyu Zhan 31388f34af6fSJianyu Zhan out_uncharge_cgroup: 31398f34af6fSJianyu Zhan hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); 314008cf9fafSMina Almasry out_uncharge_cgroup_reservation: 314108cf9fafSMina Almasry if (deferred_reserve) 314208cf9fafSMina Almasry hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h), 314308cf9fafSMina Almasry h_cg); 31448f34af6fSJianyu Zhan out_subpool_put: 3145d85f69b0SMike Kravetz if (map_chg || avoid_reserve) 31468f34af6fSJianyu Zhan hugepage_subpool_put_pages(spool, 1); 3147feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 31488f34af6fSJianyu Zhan return ERR_PTR(-ENOSPC); 3149b45b5bd6SDavid Gibson } 3150b45b5bd6SDavid Gibson 3151b5389086SZhenguo Yao int alloc_bootmem_huge_page(struct hstate *h, int nid) 3152e24a1307SAneesh Kumar K.V __attribute__ ((weak, alias("__alloc_bootmem_huge_page"))); 3153b5389086SZhenguo Yao int __alloc_bootmem_huge_page(struct hstate *h, int nid) 3154aa888a74SAndi Kleen { 3155b5389086SZhenguo Yao struct huge_bootmem_page *m = NULL; /* initialize for clang */ 3156b2261026SJoonsoo Kim int nr_nodes, node; 3157aa888a74SAndi Kleen 3158b5389086SZhenguo Yao /* do node specific alloc */ 3159b5389086SZhenguo Yao if (nid != NUMA_NO_NODE) { 3160b5389086SZhenguo Yao m = memblock_alloc_try_nid_raw(huge_page_size(h), huge_page_size(h), 3161b5389086SZhenguo Yao 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); 3162b5389086SZhenguo Yao if (!m) 3163b5389086SZhenguo Yao return 0; 3164b5389086SZhenguo Yao goto found; 3165b5389086SZhenguo Yao } 3166b5389086SZhenguo Yao /* allocate from next node when distributing huge pages */ 3167b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) { 3168b5389086SZhenguo Yao m = memblock_alloc_try_nid_raw( 31698b89a116SGrygorii Strashko huge_page_size(h), huge_page_size(h), 317097ad1087SMike Rapoport 0, MEMBLOCK_ALLOC_ACCESSIBLE, node); 3171aa888a74SAndi Kleen /* 3172aa888a74SAndi Kleen * Use the beginning of the huge page to store the 3173aa888a74SAndi Kleen * huge_bootmem_page struct (until gather_bootmem 3174aa888a74SAndi Kleen * puts them into the mem_map). 3175aa888a74SAndi Kleen */ 3176b5389086SZhenguo Yao if (!m) 3177b5389086SZhenguo Yao return 0; 3178aa888a74SAndi Kleen goto found; 3179aa888a74SAndi Kleen } 3180aa888a74SAndi Kleen 3181aa888a74SAndi Kleen found: 3182aa888a74SAndi Kleen /* Put them into a private list first because mem_map is not up yet */ 3183330d6e48SCannon Matthews INIT_LIST_HEAD(&m->list); 3184aa888a74SAndi Kleen list_add(&m->list, &huge_boot_pages); 3185aa888a74SAndi Kleen m->hstate = h; 3186aa888a74SAndi Kleen return 1; 3187aa888a74SAndi Kleen } 3188aa888a74SAndi Kleen 318948b8d744SMike Kravetz /* 319048b8d744SMike Kravetz * Put bootmem huge pages into the standard lists after mem_map is up. 319148b8d744SMike Kravetz * Note: This only applies to gigantic (order > MAX_ORDER) pages. 319248b8d744SMike Kravetz */ 3193aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void) 3194aa888a74SAndi Kleen { 3195aa888a74SAndi Kleen struct huge_bootmem_page *m; 3196aa888a74SAndi Kleen 3197aa888a74SAndi Kleen list_for_each_entry(m, &huge_boot_pages, list) { 319840d18ebfSMike Kravetz struct page *page = virt_to_page(m); 31997f325a8dSSidhartha Kumar struct folio *folio = page_folio(page); 3200aa888a74SAndi Kleen struct hstate *h = m->hstate; 3201ee8f248dSBecky Bruce 320248b8d744SMike Kravetz VM_BUG_ON(!hstate_is_gigantic(h)); 3203d1c60955SSidhartha Kumar WARN_ON(folio_ref_count(folio) != 1); 3204d1c60955SSidhartha Kumar if (prep_compound_gigantic_folio(folio, huge_page_order(h))) { 3205d1c60955SSidhartha Kumar WARN_ON(folio_test_reserved(folio)); 3206d1c60955SSidhartha Kumar prep_new_hugetlb_folio(h, folio, folio_nid(folio)); 32072b21624fSMike Kravetz free_huge_page(page); /* add to the hugepage allocator */ 32087118fc29SMike Kravetz } else { 3209416d85edSMike Kravetz /* VERY unlikely inflated ref count on a tail page */ 32107f325a8dSSidhartha Kumar free_gigantic_folio(folio, huge_page_order(h)); 32117118fc29SMike Kravetz } 3212af0fb9dfSMichal Hocko 3213b0320c7bSRafael Aquini /* 321448b8d744SMike Kravetz * We need to restore the 'stolen' pages to totalram_pages 321548b8d744SMike Kravetz * in order to fix confusing memory reports from free(1) and 321648b8d744SMike Kravetz * other side-effects, like CommitLimit going negative. 3217b0320c7bSRafael Aquini */ 3218c78a7f36SMiaohe Lin adjust_managed_page_count(page, pages_per_huge_page(h)); 3219520495feSCannon Matthews cond_resched(); 3220aa888a74SAndi Kleen } 3221aa888a74SAndi Kleen } 3222b5389086SZhenguo Yao static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid) 3223b5389086SZhenguo Yao { 3224b5389086SZhenguo Yao unsigned long i; 3225b5389086SZhenguo Yao char buf[32]; 3226b5389086SZhenguo Yao 3227b5389086SZhenguo Yao for (i = 0; i < h->max_huge_pages_node[nid]; ++i) { 3228b5389086SZhenguo Yao if (hstate_is_gigantic(h)) { 3229b5389086SZhenguo Yao if (!alloc_bootmem_huge_page(h, nid)) 3230b5389086SZhenguo Yao break; 3231b5389086SZhenguo Yao } else { 323219fc1a7eSSidhartha Kumar struct folio *folio; 3233b5389086SZhenguo Yao gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; 3234b5389086SZhenguo Yao 323519fc1a7eSSidhartha Kumar folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, 3236b5389086SZhenguo Yao &node_states[N_MEMORY], NULL); 323719fc1a7eSSidhartha Kumar if (!folio) 3238b5389086SZhenguo Yao break; 323919fc1a7eSSidhartha Kumar free_huge_page(&folio->page); /* free it into the hugepage allocator */ 3240b5389086SZhenguo Yao } 3241b5389086SZhenguo Yao cond_resched(); 3242b5389086SZhenguo Yao } 3243b5389086SZhenguo Yao if (i == h->max_huge_pages_node[nid]) 3244b5389086SZhenguo Yao return; 3245b5389086SZhenguo Yao 3246b5389086SZhenguo Yao string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); 3247b5389086SZhenguo Yao pr_warn("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n", 3248b5389086SZhenguo Yao h->max_huge_pages_node[nid], buf, nid, i); 3249b5389086SZhenguo Yao h->max_huge_pages -= (h->max_huge_pages_node[nid] - i); 3250b5389086SZhenguo Yao h->max_huge_pages_node[nid] = i; 3251b5389086SZhenguo Yao } 3252aa888a74SAndi Kleen 32538faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h) 32541da177e4SLinus Torvalds { 32551da177e4SLinus Torvalds unsigned long i; 3256f60858f9SMike Kravetz nodemask_t *node_alloc_noretry; 3257b5389086SZhenguo Yao bool node_specific_alloc = false; 3258f60858f9SMike Kravetz 3259b5389086SZhenguo Yao /* skip gigantic hugepages allocation if hugetlb_cma enabled */ 3260b5389086SZhenguo Yao if (hstate_is_gigantic(h) && hugetlb_cma_size) { 3261b5389086SZhenguo Yao pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n"); 3262b5389086SZhenguo Yao return; 3263b5389086SZhenguo Yao } 3264b5389086SZhenguo Yao 3265b5389086SZhenguo Yao /* do node specific alloc */ 32660a7a0f6fSPeng Liu for_each_online_node(i) { 3267b5389086SZhenguo Yao if (h->max_huge_pages_node[i] > 0) { 3268b5389086SZhenguo Yao hugetlb_hstate_alloc_pages_onenode(h, i); 3269b5389086SZhenguo Yao node_specific_alloc = true; 3270b5389086SZhenguo Yao } 3271b5389086SZhenguo Yao } 3272b5389086SZhenguo Yao 3273b5389086SZhenguo Yao if (node_specific_alloc) 3274b5389086SZhenguo Yao return; 3275b5389086SZhenguo Yao 3276b5389086SZhenguo Yao /* below will do all node balanced alloc */ 3277f60858f9SMike Kravetz if (!hstate_is_gigantic(h)) { 3278f60858f9SMike Kravetz /* 3279f60858f9SMike Kravetz * Bit mask controlling how hard we retry per-node allocations. 3280f60858f9SMike Kravetz * Ignore errors as lower level routines can deal with 3281f60858f9SMike Kravetz * node_alloc_noretry == NULL. If this kmalloc fails at boot 3282f60858f9SMike Kravetz * time, we are likely in bigger trouble. 3283f60858f9SMike Kravetz */ 3284f60858f9SMike Kravetz node_alloc_noretry = kmalloc(sizeof(*node_alloc_noretry), 3285f60858f9SMike Kravetz GFP_KERNEL); 3286f60858f9SMike Kravetz } else { 3287f60858f9SMike Kravetz /* allocations done at boot time */ 3288f60858f9SMike Kravetz node_alloc_noretry = NULL; 3289f60858f9SMike Kravetz } 3290f60858f9SMike Kravetz 3291f60858f9SMike Kravetz /* bit mask controlling how hard we retry per-node allocations */ 3292f60858f9SMike Kravetz if (node_alloc_noretry) 3293f60858f9SMike Kravetz nodes_clear(*node_alloc_noretry); 32941da177e4SLinus Torvalds 3295e5ff2159SAndi Kleen for (i = 0; i < h->max_huge_pages; ++i) { 3296bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) { 3297b5389086SZhenguo Yao if (!alloc_bootmem_huge_page(h, NUMA_NO_NODE)) 3298aa888a74SAndi Kleen break; 32990c397daeSMichal Hocko } else if (!alloc_pool_huge_page(h, 3300f60858f9SMike Kravetz &node_states[N_MEMORY], 3301f60858f9SMike Kravetz node_alloc_noretry)) 33021da177e4SLinus Torvalds break; 330369ed779aSDavid Rientjes cond_resched(); 33041da177e4SLinus Torvalds } 3305d715cf80SLiam R. Howlett if (i < h->max_huge_pages) { 3306d715cf80SLiam R. Howlett char buf[32]; 3307d715cf80SLiam R. Howlett 3308c6247f72SMatthew Wilcox string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); 3309d715cf80SLiam R. Howlett pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n", 3310d715cf80SLiam R. Howlett h->max_huge_pages, buf, i); 33118faa8b07SAndi Kleen h->max_huge_pages = i; 3312e5ff2159SAndi Kleen } 3313f60858f9SMike Kravetz kfree(node_alloc_noretry); 3314d715cf80SLiam R. Howlett } 3315e5ff2159SAndi Kleen 3316e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void) 3317e5ff2159SAndi Kleen { 331879dfc695SMike Kravetz struct hstate *h, *h2; 3319e5ff2159SAndi Kleen 3320e5ff2159SAndi Kleen for_each_hstate(h) { 33218faa8b07SAndi Kleen /* oversize hugepages were init'ed in early boot */ 3322bae7f4aeSLuiz Capitulino if (!hstate_is_gigantic(h)) 33238faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(h); 332479dfc695SMike Kravetz 332579dfc695SMike Kravetz /* 332679dfc695SMike Kravetz * Set demote order for each hstate. Note that 332779dfc695SMike Kravetz * h->demote_order is initially 0. 332879dfc695SMike Kravetz * - We can not demote gigantic pages if runtime freeing 332979dfc695SMike Kravetz * is not supported, so skip this. 3330a01f4390SMike Kravetz * - If CMA allocation is possible, we can not demote 3331a01f4390SMike Kravetz * HUGETLB_PAGE_ORDER or smaller size pages. 333279dfc695SMike Kravetz */ 333379dfc695SMike Kravetz if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 333479dfc695SMike Kravetz continue; 3335a01f4390SMike Kravetz if (hugetlb_cma_size && h->order <= HUGETLB_PAGE_ORDER) 3336a01f4390SMike Kravetz continue; 333779dfc695SMike Kravetz for_each_hstate(h2) { 333879dfc695SMike Kravetz if (h2 == h) 333979dfc695SMike Kravetz continue; 334079dfc695SMike Kravetz if (h2->order < h->order && 334179dfc695SMike Kravetz h2->order > h->demote_order) 334279dfc695SMike Kravetz h->demote_order = h2->order; 334379dfc695SMike Kravetz } 3344e5ff2159SAndi Kleen } 3345e5ff2159SAndi Kleen } 3346e5ff2159SAndi Kleen 3347e5ff2159SAndi Kleen static void __init report_hugepages(void) 3348e5ff2159SAndi Kleen { 3349e5ff2159SAndi Kleen struct hstate *h; 3350e5ff2159SAndi Kleen 3351e5ff2159SAndi Kleen for_each_hstate(h) { 33524abd32dbSAndi Kleen char buf[32]; 3353c6247f72SMatthew Wilcox 3354c6247f72SMatthew Wilcox string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); 33556213834cSMuchun Song pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n", 3356c6247f72SMatthew Wilcox buf, h->free_huge_pages); 33576213834cSMuchun Song pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n", 33586213834cSMuchun Song hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf); 3359e5ff2159SAndi Kleen } 3360e5ff2159SAndi Kleen } 3361e5ff2159SAndi Kleen 33621da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM 33636ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count, 33646ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 33651da177e4SLinus Torvalds { 33664415cc8dSChristoph Lameter int i; 33671121828aSMike Kravetz LIST_HEAD(page_list); 33684415cc8dSChristoph Lameter 33699487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 3370bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 3371aa888a74SAndi Kleen return; 3372aa888a74SAndi Kleen 33731121828aSMike Kravetz /* 33741121828aSMike Kravetz * Collect pages to be freed on a list, and free after dropping lock 33751121828aSMike Kravetz */ 33766ae11b27SLee Schermerhorn for_each_node_mask(i, *nodes_allowed) { 337710c6ec49SMike Kravetz struct page *page, *next; 3378a5516438SAndi Kleen struct list_head *freel = &h->hugepage_freelists[i]; 3379a5516438SAndi Kleen list_for_each_entry_safe(page, next, freel, lru) { 3380a5516438SAndi Kleen if (count >= h->nr_huge_pages) 33811121828aSMike Kravetz goto out; 33821da177e4SLinus Torvalds if (PageHighMem(page)) 33831da177e4SLinus Torvalds continue; 3384cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, page_folio(page), false); 33851121828aSMike Kravetz list_add(&page->lru, &page_list); 33861121828aSMike Kravetz } 33871121828aSMike Kravetz } 33881121828aSMike Kravetz 33891121828aSMike Kravetz out: 3390db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 339110c6ec49SMike Kravetz update_and_free_pages_bulk(h, &page_list); 3392db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 33931da177e4SLinus Torvalds } 33941da177e4SLinus Torvalds #else 33956ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count, 33966ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 33971da177e4SLinus Torvalds { 33981da177e4SLinus Torvalds } 33991da177e4SLinus Torvalds #endif 34001da177e4SLinus Torvalds 340120a0307cSWu Fengguang /* 340220a0307cSWu Fengguang * Increment or decrement surplus_huge_pages. Keep node-specific counters 340320a0307cSWu Fengguang * balanced by operating on them in a round-robin fashion. 340420a0307cSWu Fengguang * Returns 1 if an adjustment was made. 340520a0307cSWu Fengguang */ 34066ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, 34076ae11b27SLee Schermerhorn int delta) 340820a0307cSWu Fengguang { 3409b2261026SJoonsoo Kim int nr_nodes, node; 341020a0307cSWu Fengguang 34119487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 341220a0307cSWu Fengguang VM_BUG_ON(delta != -1 && delta != 1); 341320a0307cSWu Fengguang 3414e8c5c824SLee Schermerhorn if (delta < 0) { 3415b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 3416b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node]) 3417b2261026SJoonsoo Kim goto found; 3418b2261026SJoonsoo Kim } 3419b2261026SJoonsoo Kim } else { 3420b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 3421b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node] < 3422b2261026SJoonsoo Kim h->nr_huge_pages_node[node]) 3423b2261026SJoonsoo Kim goto found; 3424e8c5c824SLee Schermerhorn } 34259a76db09SLee Schermerhorn } 3426b2261026SJoonsoo Kim return 0; 342720a0307cSWu Fengguang 3428b2261026SJoonsoo Kim found: 342920a0307cSWu Fengguang h->surplus_huge_pages += delta; 3430b2261026SJoonsoo Kim h->surplus_huge_pages_node[node] += delta; 3431b2261026SJoonsoo Kim return 1; 343220a0307cSWu Fengguang } 343320a0307cSWu Fengguang 3434a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) 3435fd875dcaSMike Kravetz static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid, 34366ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 34371da177e4SLinus Torvalds { 34387893d1d5SAdam Litke unsigned long min_count, ret; 343910c6ec49SMike Kravetz struct page *page; 344010c6ec49SMike Kravetz LIST_HEAD(page_list); 3441f60858f9SMike Kravetz NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL); 3442f60858f9SMike Kravetz 3443f60858f9SMike Kravetz /* 3444f60858f9SMike Kravetz * Bit mask controlling how hard we retry per-node allocations. 3445f60858f9SMike Kravetz * If we can not allocate the bit mask, do not attempt to allocate 3446f60858f9SMike Kravetz * the requested huge pages. 3447f60858f9SMike Kravetz */ 3448f60858f9SMike Kravetz if (node_alloc_noretry) 3449f60858f9SMike Kravetz nodes_clear(*node_alloc_noretry); 3450f60858f9SMike Kravetz else 3451f60858f9SMike Kravetz return -ENOMEM; 34521da177e4SLinus Torvalds 345329383967SMike Kravetz /* 345429383967SMike Kravetz * resize_lock mutex prevents concurrent adjustments to number of 345529383967SMike Kravetz * pages in hstate via the proc/sysfs interfaces. 345629383967SMike Kravetz */ 345729383967SMike Kravetz mutex_lock(&h->resize_lock); 3458b65d4adbSMuchun Song flush_free_hpage_work(h); 3459db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 34604eb0716eSAlexandre Ghiti 34614eb0716eSAlexandre Ghiti /* 3462fd875dcaSMike Kravetz * Check for a node specific request. 3463fd875dcaSMike Kravetz * Changing node specific huge page count may require a corresponding 3464fd875dcaSMike Kravetz * change to the global count. In any case, the passed node mask 3465fd875dcaSMike Kravetz * (nodes_allowed) will restrict alloc/free to the specified node. 3466fd875dcaSMike Kravetz */ 3467fd875dcaSMike Kravetz if (nid != NUMA_NO_NODE) { 3468fd875dcaSMike Kravetz unsigned long old_count = count; 3469fd875dcaSMike Kravetz 3470fd875dcaSMike Kravetz count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; 3471fd875dcaSMike Kravetz /* 3472fd875dcaSMike Kravetz * User may have specified a large count value which caused the 3473fd875dcaSMike Kravetz * above calculation to overflow. In this case, they wanted 3474fd875dcaSMike Kravetz * to allocate as many huge pages as possible. Set count to 3475fd875dcaSMike Kravetz * largest possible value to align with their intention. 3476fd875dcaSMike Kravetz */ 3477fd875dcaSMike Kravetz if (count < old_count) 3478fd875dcaSMike Kravetz count = ULONG_MAX; 3479fd875dcaSMike Kravetz } 3480fd875dcaSMike Kravetz 3481fd875dcaSMike Kravetz /* 34824eb0716eSAlexandre Ghiti * Gigantic pages runtime allocation depend on the capability for large 34834eb0716eSAlexandre Ghiti * page range allocation. 34844eb0716eSAlexandre Ghiti * If the system does not provide this feature, return an error when 34854eb0716eSAlexandre Ghiti * the user tries to allocate gigantic pages but let the user free the 34864eb0716eSAlexandre Ghiti * boottime allocated gigantic pages. 34874eb0716eSAlexandre Ghiti */ 34884eb0716eSAlexandre Ghiti if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) { 34894eb0716eSAlexandre Ghiti if (count > persistent_huge_pages(h)) { 3490db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 349129383967SMike Kravetz mutex_unlock(&h->resize_lock); 3492f60858f9SMike Kravetz NODEMASK_FREE(node_alloc_noretry); 34934eb0716eSAlexandre Ghiti return -EINVAL; 34944eb0716eSAlexandre Ghiti } 34954eb0716eSAlexandre Ghiti /* Fall through to decrease pool */ 34964eb0716eSAlexandre Ghiti } 3497aa888a74SAndi Kleen 34987893d1d5SAdam Litke /* 34997893d1d5SAdam Litke * Increase the pool size 35007893d1d5SAdam Litke * First take pages out of surplus state. Then make up the 35017893d1d5SAdam Litke * remaining difference by allocating fresh huge pages. 3502d1c3fb1fSNishanth Aravamudan * 35033a740e8bSSidhartha Kumar * We might race with alloc_surplus_hugetlb_folio() here and be unable 3504d1c3fb1fSNishanth Aravamudan * to convert a surplus huge page to a normal huge page. That is 3505d1c3fb1fSNishanth Aravamudan * not critical, though, it just means the overall size of the 3506d1c3fb1fSNishanth Aravamudan * pool might be one hugepage larger than it needs to be, but 3507d1c3fb1fSNishanth Aravamudan * within all the constraints specified by the sysctls. 35087893d1d5SAdam Litke */ 3509a5516438SAndi Kleen while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { 35106ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, -1)) 35117893d1d5SAdam Litke break; 35127893d1d5SAdam Litke } 35137893d1d5SAdam Litke 3514a5516438SAndi Kleen while (count > persistent_huge_pages(h)) { 35157893d1d5SAdam Litke /* 35167893d1d5SAdam Litke * If this allocation races such that we no longer need the 35177893d1d5SAdam Litke * page, free_huge_page will handle it by freeing the page 35187893d1d5SAdam Litke * and reducing the surplus. 35197893d1d5SAdam Litke */ 3520db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 3521649920c6SJia He 3522649920c6SJia He /* yield cpu to avoid soft lockup */ 3523649920c6SJia He cond_resched(); 3524649920c6SJia He 3525f60858f9SMike Kravetz ret = alloc_pool_huge_page(h, nodes_allowed, 3526f60858f9SMike Kravetz node_alloc_noretry); 3527db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 35287893d1d5SAdam Litke if (!ret) 35297893d1d5SAdam Litke goto out; 35307893d1d5SAdam Litke 3531536240f2SMel Gorman /* Bail for signals. Probably ctrl-c from user */ 3532536240f2SMel Gorman if (signal_pending(current)) 3533536240f2SMel Gorman goto out; 35347893d1d5SAdam Litke } 35357893d1d5SAdam Litke 35367893d1d5SAdam Litke /* 35377893d1d5SAdam Litke * Decrease the pool size 35387893d1d5SAdam Litke * First return free pages to the buddy allocator (being careful 35397893d1d5SAdam Litke * to keep enough around to satisfy reservations). Then place 35407893d1d5SAdam Litke * pages into surplus state as needed so the pool will shrink 35417893d1d5SAdam Litke * to the desired size as pages become free. 3542d1c3fb1fSNishanth Aravamudan * 3543d1c3fb1fSNishanth Aravamudan * By placing pages into the surplus state independent of the 3544d1c3fb1fSNishanth Aravamudan * overcommit value, we are allowing the surplus pool size to 3545d1c3fb1fSNishanth Aravamudan * exceed overcommit. There are few sane options here. Since 35463a740e8bSSidhartha Kumar * alloc_surplus_hugetlb_folio() is checking the global counter, 3547d1c3fb1fSNishanth Aravamudan * though, we'll note that we're not allowed to exceed surplus 3548d1c3fb1fSNishanth Aravamudan * and won't grow the pool anywhere else. Not until one of the 3549d1c3fb1fSNishanth Aravamudan * sysctls are changed, or the surplus pages go out of use. 35507893d1d5SAdam Litke */ 3551a5516438SAndi Kleen min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; 35526b0c880dSAdam Litke min_count = max(count, min_count); 35536ae11b27SLee Schermerhorn try_to_free_low(h, min_count, nodes_allowed); 355410c6ec49SMike Kravetz 355510c6ec49SMike Kravetz /* 355610c6ec49SMike Kravetz * Collect pages to be removed on list without dropping lock 355710c6ec49SMike Kravetz */ 3558a5516438SAndi Kleen while (min_count < persistent_huge_pages(h)) { 355910c6ec49SMike Kravetz page = remove_pool_huge_page(h, nodes_allowed, 0); 356010c6ec49SMike Kravetz if (!page) 35611da177e4SLinus Torvalds break; 356210c6ec49SMike Kravetz 356310c6ec49SMike Kravetz list_add(&page->lru, &page_list); 35641da177e4SLinus Torvalds } 356510c6ec49SMike Kravetz /* free the pages after dropping lock */ 3566db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 356710c6ec49SMike Kravetz update_and_free_pages_bulk(h, &page_list); 3568b65d4adbSMuchun Song flush_free_hpage_work(h); 3569db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 357010c6ec49SMike Kravetz 3571a5516438SAndi Kleen while (count < persistent_huge_pages(h)) { 35726ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, 1)) 35737893d1d5SAdam Litke break; 35747893d1d5SAdam Litke } 35757893d1d5SAdam Litke out: 35764eb0716eSAlexandre Ghiti h->max_huge_pages = persistent_huge_pages(h); 3577db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 357829383967SMike Kravetz mutex_unlock(&h->resize_lock); 35794eb0716eSAlexandre Ghiti 3580f60858f9SMike Kravetz NODEMASK_FREE(node_alloc_noretry); 3581f60858f9SMike Kravetz 35824eb0716eSAlexandre Ghiti return 0; 35831da177e4SLinus Torvalds } 35841da177e4SLinus Torvalds 3585bdd7be07SSidhartha Kumar static int demote_free_hugetlb_folio(struct hstate *h, struct folio *folio) 35868531fc6fSMike Kravetz { 3587bdd7be07SSidhartha Kumar int i, nid = folio_nid(folio); 35888531fc6fSMike Kravetz struct hstate *target_hstate; 358931731452SDoug Berger struct page *subpage; 3590bdd7be07SSidhartha Kumar struct folio *inner_folio; 35918531fc6fSMike Kravetz int rc = 0; 35928531fc6fSMike Kravetz 35938531fc6fSMike Kravetz target_hstate = size_to_hstate(PAGE_SIZE << h->demote_order); 35948531fc6fSMike Kravetz 3595cfd5082bSSidhartha Kumar remove_hugetlb_folio_for_demote(h, folio, false); 35968531fc6fSMike Kravetz spin_unlock_irq(&hugetlb_lock); 35978531fc6fSMike Kravetz 3598bdd7be07SSidhartha Kumar rc = hugetlb_vmemmap_restore(h, &folio->page); 35998531fc6fSMike Kravetz if (rc) { 3600bdd7be07SSidhartha Kumar /* Allocation of vmemmmap failed, we can not demote folio */ 36018531fc6fSMike Kravetz spin_lock_irq(&hugetlb_lock); 3602bdd7be07SSidhartha Kumar folio_ref_unfreeze(folio, 1); 3603bdd7be07SSidhartha Kumar add_hugetlb_folio(h, folio, false); 36048531fc6fSMike Kravetz return rc; 36058531fc6fSMike Kravetz } 36068531fc6fSMike Kravetz 36078531fc6fSMike Kravetz /* 3608911565b8SSidhartha Kumar * Use destroy_compound_hugetlb_folio_for_demote for all huge page 3609bdd7be07SSidhartha Kumar * sizes as it will not ref count folios. 36108531fc6fSMike Kravetz */ 3611911565b8SSidhartha Kumar destroy_compound_hugetlb_folio_for_demote(folio, huge_page_order(h)); 36128531fc6fSMike Kravetz 36138531fc6fSMike Kravetz /* 36148531fc6fSMike Kravetz * Taking target hstate mutex synchronizes with set_max_huge_pages. 36158531fc6fSMike Kravetz * Without the mutex, pages added to target hstate could be marked 36168531fc6fSMike Kravetz * as surplus. 36178531fc6fSMike Kravetz * 36188531fc6fSMike Kravetz * Note that we already hold h->resize_lock. To prevent deadlock, 36198531fc6fSMike Kravetz * use the convention of always taking larger size hstate mutex first. 36208531fc6fSMike Kravetz */ 36218531fc6fSMike Kravetz mutex_lock(&target_hstate->resize_lock); 36228531fc6fSMike Kravetz for (i = 0; i < pages_per_huge_page(h); 36238531fc6fSMike Kravetz i += pages_per_huge_page(target_hstate)) { 3624bdd7be07SSidhartha Kumar subpage = folio_page(folio, i); 3625bdd7be07SSidhartha Kumar inner_folio = page_folio(subpage); 36268531fc6fSMike Kravetz if (hstate_is_gigantic(target_hstate)) 3627bdd7be07SSidhartha Kumar prep_compound_gigantic_folio_for_demote(inner_folio, 36288531fc6fSMike Kravetz target_hstate->order); 36298531fc6fSMike Kravetz else 363031731452SDoug Berger prep_compound_page(subpage, target_hstate->order); 3631bdd7be07SSidhartha Kumar folio_change_private(inner_folio, NULL); 3632bdd7be07SSidhartha Kumar prep_new_hugetlb_folio(target_hstate, inner_folio, nid); 36332b21624fSMike Kravetz free_huge_page(subpage); 36348531fc6fSMike Kravetz } 36358531fc6fSMike Kravetz mutex_unlock(&target_hstate->resize_lock); 36368531fc6fSMike Kravetz 36378531fc6fSMike Kravetz spin_lock_irq(&hugetlb_lock); 36388531fc6fSMike Kravetz 36398531fc6fSMike Kravetz /* 36408531fc6fSMike Kravetz * Not absolutely necessary, but for consistency update max_huge_pages 36418531fc6fSMike Kravetz * based on pool changes for the demoted page. 36428531fc6fSMike Kravetz */ 36438531fc6fSMike Kravetz h->max_huge_pages--; 3644a43a83c7SMiaohe Lin target_hstate->max_huge_pages += 3645a43a83c7SMiaohe Lin pages_per_huge_page(h) / pages_per_huge_page(target_hstate); 36468531fc6fSMike Kravetz 36478531fc6fSMike Kravetz return rc; 36488531fc6fSMike Kravetz } 36498531fc6fSMike Kravetz 365079dfc695SMike Kravetz static int demote_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed) 365179dfc695SMike Kravetz __must_hold(&hugetlb_lock) 365279dfc695SMike Kravetz { 36538531fc6fSMike Kravetz int nr_nodes, node; 3654bdd7be07SSidhartha Kumar struct folio *folio; 365579dfc695SMike Kravetz 365679dfc695SMike Kravetz lockdep_assert_held(&hugetlb_lock); 365779dfc695SMike Kravetz 365879dfc695SMike Kravetz /* We should never get here if no demote order */ 365979dfc695SMike Kravetz if (!h->demote_order) { 366079dfc695SMike Kravetz pr_warn("HugeTLB: NULL demote order passed to demote_pool_huge_page.\n"); 366179dfc695SMike Kravetz return -EINVAL; /* internal error */ 366279dfc695SMike Kravetz } 366379dfc695SMike Kravetz 36648531fc6fSMike Kravetz for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 3665bdd7be07SSidhartha Kumar list_for_each_entry(folio, &h->hugepage_freelists[node], lru) { 3666bdd7be07SSidhartha Kumar if (folio_test_hwpoison(folio)) 36675a317412SMike Kravetz continue; 3668bdd7be07SSidhartha Kumar return demote_free_hugetlb_folio(h, folio); 36698531fc6fSMike Kravetz } 36708531fc6fSMike Kravetz } 36718531fc6fSMike Kravetz 36725a317412SMike Kravetz /* 36735a317412SMike Kravetz * Only way to get here is if all pages on free lists are poisoned. 36745a317412SMike Kravetz * Return -EBUSY so that caller will not retry. 36755a317412SMike Kravetz */ 36765a317412SMike Kravetz return -EBUSY; 367779dfc695SMike Kravetz } 367879dfc695SMike Kravetz 3679a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \ 3680a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 3681a3437870SNishanth Aravamudan 368279dfc695SMike Kravetz #define HSTATE_ATTR_WO(_name) \ 368379dfc695SMike Kravetz static struct kobj_attribute _name##_attr = __ATTR_WO(_name) 368479dfc695SMike Kravetz 3685a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \ 368698bc26acSMiaohe Lin static struct kobj_attribute _name##_attr = __ATTR_RW(_name) 3687a3437870SNishanth Aravamudan 3688a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj; 3689a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 3690a3437870SNishanth Aravamudan 36919a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp); 36929a305230SLee Schermerhorn 36939a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp) 3694a3437870SNishanth Aravamudan { 3695a3437870SNishanth Aravamudan int i; 36969a305230SLee Schermerhorn 3697a3437870SNishanth Aravamudan for (i = 0; i < HUGE_MAX_HSTATE; i++) 36989a305230SLee Schermerhorn if (hstate_kobjs[i] == kobj) { 36999a305230SLee Schermerhorn if (nidp) 37009a305230SLee Schermerhorn *nidp = NUMA_NO_NODE; 3701a3437870SNishanth Aravamudan return &hstates[i]; 37029a305230SLee Schermerhorn } 37039a305230SLee Schermerhorn 37049a305230SLee Schermerhorn return kobj_to_node_hstate(kobj, nidp); 3705a3437870SNishanth Aravamudan } 3706a3437870SNishanth Aravamudan 370706808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj, 3708a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3709a3437870SNishanth Aravamudan { 37109a305230SLee Schermerhorn struct hstate *h; 37119a305230SLee Schermerhorn unsigned long nr_huge_pages; 37129a305230SLee Schermerhorn int nid; 37139a305230SLee Schermerhorn 37149a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 37159a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 37169a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages; 37179a305230SLee Schermerhorn else 37189a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages_node[nid]; 37199a305230SLee Schermerhorn 3720ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", nr_huge_pages); 3721a3437870SNishanth Aravamudan } 3722adbe8726SEric B Munson 3723238d3c13SDavid Rientjes static ssize_t __nr_hugepages_store_common(bool obey_mempolicy, 3724238d3c13SDavid Rientjes struct hstate *h, int nid, 3725238d3c13SDavid Rientjes unsigned long count, size_t len) 3726a3437870SNishanth Aravamudan { 3727a3437870SNishanth Aravamudan int err; 37282d0adf7eSOscar Salvador nodemask_t nodes_allowed, *n_mask; 3729a3437870SNishanth Aravamudan 37302d0adf7eSOscar Salvador if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 37312d0adf7eSOscar Salvador return -EINVAL; 3732adbe8726SEric B Munson 37339a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) { 37349a305230SLee Schermerhorn /* 37359a305230SLee Schermerhorn * global hstate attribute 37369a305230SLee Schermerhorn */ 37379a305230SLee Schermerhorn if (!(obey_mempolicy && 37382d0adf7eSOscar Salvador init_nodemask_of_mempolicy(&nodes_allowed))) 37392d0adf7eSOscar Salvador n_mask = &node_states[N_MEMORY]; 37402d0adf7eSOscar Salvador else 37412d0adf7eSOscar Salvador n_mask = &nodes_allowed; 37422d0adf7eSOscar Salvador } else { 37439a305230SLee Schermerhorn /* 3744fd875dcaSMike Kravetz * Node specific request. count adjustment happens in 3745fd875dcaSMike Kravetz * set_max_huge_pages() after acquiring hugetlb_lock. 37469a305230SLee Schermerhorn */ 37472d0adf7eSOscar Salvador init_nodemask_of_node(&nodes_allowed, nid); 37482d0adf7eSOscar Salvador n_mask = &nodes_allowed; 3749fd875dcaSMike Kravetz } 37509a305230SLee Schermerhorn 37512d0adf7eSOscar Salvador err = set_max_huge_pages(h, count, nid, n_mask); 375206808b08SLee Schermerhorn 37534eb0716eSAlexandre Ghiti return err ? err : len; 375406808b08SLee Schermerhorn } 375506808b08SLee Schermerhorn 3756238d3c13SDavid Rientjes static ssize_t nr_hugepages_store_common(bool obey_mempolicy, 3757238d3c13SDavid Rientjes struct kobject *kobj, const char *buf, 3758238d3c13SDavid Rientjes size_t len) 3759238d3c13SDavid Rientjes { 3760238d3c13SDavid Rientjes struct hstate *h; 3761238d3c13SDavid Rientjes unsigned long count; 3762238d3c13SDavid Rientjes int nid; 3763238d3c13SDavid Rientjes int err; 3764238d3c13SDavid Rientjes 3765238d3c13SDavid Rientjes err = kstrtoul(buf, 10, &count); 3766238d3c13SDavid Rientjes if (err) 3767238d3c13SDavid Rientjes return err; 3768238d3c13SDavid Rientjes 3769238d3c13SDavid Rientjes h = kobj_to_hstate(kobj, &nid); 3770238d3c13SDavid Rientjes return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len); 3771238d3c13SDavid Rientjes } 3772238d3c13SDavid Rientjes 377306808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj, 377406808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 377506808b08SLee Schermerhorn { 377606808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 377706808b08SLee Schermerhorn } 377806808b08SLee Schermerhorn 377906808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj, 378006808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 378106808b08SLee Schermerhorn { 3782238d3c13SDavid Rientjes return nr_hugepages_store_common(false, kobj, buf, len); 3783a3437870SNishanth Aravamudan } 3784a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages); 3785a3437870SNishanth Aravamudan 378606808b08SLee Schermerhorn #ifdef CONFIG_NUMA 378706808b08SLee Schermerhorn 378806808b08SLee Schermerhorn /* 378906808b08SLee Schermerhorn * hstate attribute for optionally mempolicy-based constraint on persistent 379006808b08SLee Schermerhorn * huge page alloc/free. 379106808b08SLee Schermerhorn */ 379206808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj, 3793ae7a927dSJoe Perches struct kobj_attribute *attr, 3794ae7a927dSJoe Perches char *buf) 379506808b08SLee Schermerhorn { 379606808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 379706808b08SLee Schermerhorn } 379806808b08SLee Schermerhorn 379906808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj, 380006808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 380106808b08SLee Schermerhorn { 3802238d3c13SDavid Rientjes return nr_hugepages_store_common(true, kobj, buf, len); 380306808b08SLee Schermerhorn } 380406808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy); 380506808b08SLee Schermerhorn #endif 380606808b08SLee Schermerhorn 380706808b08SLee Schermerhorn 3808a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, 3809a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3810a3437870SNishanth Aravamudan { 38119a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 3812ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", h->nr_overcommit_huge_pages); 3813a3437870SNishanth Aravamudan } 3814adbe8726SEric B Munson 3815a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, 3816a3437870SNishanth Aravamudan struct kobj_attribute *attr, const char *buf, size_t count) 3817a3437870SNishanth Aravamudan { 3818a3437870SNishanth Aravamudan int err; 3819a3437870SNishanth Aravamudan unsigned long input; 38209a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 3821a3437870SNishanth Aravamudan 3822bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 3823adbe8726SEric B Munson return -EINVAL; 3824adbe8726SEric B Munson 38253dbb95f7SJingoo Han err = kstrtoul(buf, 10, &input); 3826a3437870SNishanth Aravamudan if (err) 382773ae31e5SEric B Munson return err; 3828a3437870SNishanth Aravamudan 3829db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 3830a3437870SNishanth Aravamudan h->nr_overcommit_huge_pages = input; 3831db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 3832a3437870SNishanth Aravamudan 3833a3437870SNishanth Aravamudan return count; 3834a3437870SNishanth Aravamudan } 3835a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages); 3836a3437870SNishanth Aravamudan 3837a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj, 3838a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3839a3437870SNishanth Aravamudan { 38409a305230SLee Schermerhorn struct hstate *h; 38419a305230SLee Schermerhorn unsigned long free_huge_pages; 38429a305230SLee Schermerhorn int nid; 38439a305230SLee Schermerhorn 38449a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 38459a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 38469a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages; 38479a305230SLee Schermerhorn else 38489a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages_node[nid]; 38499a305230SLee Schermerhorn 3850ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", free_huge_pages); 3851a3437870SNishanth Aravamudan } 3852a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages); 3853a3437870SNishanth Aravamudan 3854a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj, 3855a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3856a3437870SNishanth Aravamudan { 38579a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 3858ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", h->resv_huge_pages); 3859a3437870SNishanth Aravamudan } 3860a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages); 3861a3437870SNishanth Aravamudan 3862a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj, 3863a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3864a3437870SNishanth Aravamudan { 38659a305230SLee Schermerhorn struct hstate *h; 38669a305230SLee Schermerhorn unsigned long surplus_huge_pages; 38679a305230SLee Schermerhorn int nid; 38689a305230SLee Schermerhorn 38699a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 38709a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 38719a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages; 38729a305230SLee Schermerhorn else 38739a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages_node[nid]; 38749a305230SLee Schermerhorn 3875ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", surplus_huge_pages); 3876a3437870SNishanth Aravamudan } 3877a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages); 3878a3437870SNishanth Aravamudan 387979dfc695SMike Kravetz static ssize_t demote_store(struct kobject *kobj, 388079dfc695SMike Kravetz struct kobj_attribute *attr, const char *buf, size_t len) 388179dfc695SMike Kravetz { 388279dfc695SMike Kravetz unsigned long nr_demote; 388379dfc695SMike Kravetz unsigned long nr_available; 388479dfc695SMike Kravetz nodemask_t nodes_allowed, *n_mask; 388579dfc695SMike Kravetz struct hstate *h; 38868eeda55fSLi zeming int err; 388779dfc695SMike Kravetz int nid; 388879dfc695SMike Kravetz 388979dfc695SMike Kravetz err = kstrtoul(buf, 10, &nr_demote); 389079dfc695SMike Kravetz if (err) 389179dfc695SMike Kravetz return err; 389279dfc695SMike Kravetz h = kobj_to_hstate(kobj, &nid); 389379dfc695SMike Kravetz 389479dfc695SMike Kravetz if (nid != NUMA_NO_NODE) { 389579dfc695SMike Kravetz init_nodemask_of_node(&nodes_allowed, nid); 389679dfc695SMike Kravetz n_mask = &nodes_allowed; 389779dfc695SMike Kravetz } else { 389879dfc695SMike Kravetz n_mask = &node_states[N_MEMORY]; 389979dfc695SMike Kravetz } 390079dfc695SMike Kravetz 390179dfc695SMike Kravetz /* Synchronize with other sysfs operations modifying huge pages */ 390279dfc695SMike Kravetz mutex_lock(&h->resize_lock); 390379dfc695SMike Kravetz spin_lock_irq(&hugetlb_lock); 390479dfc695SMike Kravetz 390579dfc695SMike Kravetz while (nr_demote) { 390679dfc695SMike Kravetz /* 390779dfc695SMike Kravetz * Check for available pages to demote each time thorough the 390879dfc695SMike Kravetz * loop as demote_pool_huge_page will drop hugetlb_lock. 390979dfc695SMike Kravetz */ 391079dfc695SMike Kravetz if (nid != NUMA_NO_NODE) 391179dfc695SMike Kravetz nr_available = h->free_huge_pages_node[nid]; 391279dfc695SMike Kravetz else 391379dfc695SMike Kravetz nr_available = h->free_huge_pages; 391479dfc695SMike Kravetz nr_available -= h->resv_huge_pages; 391579dfc695SMike Kravetz if (!nr_available) 391679dfc695SMike Kravetz break; 391779dfc695SMike Kravetz 391879dfc695SMike Kravetz err = demote_pool_huge_page(h, n_mask); 391979dfc695SMike Kravetz if (err) 392079dfc695SMike Kravetz break; 392179dfc695SMike Kravetz 392279dfc695SMike Kravetz nr_demote--; 392379dfc695SMike Kravetz } 392479dfc695SMike Kravetz 392579dfc695SMike Kravetz spin_unlock_irq(&hugetlb_lock); 392679dfc695SMike Kravetz mutex_unlock(&h->resize_lock); 392779dfc695SMike Kravetz 392879dfc695SMike Kravetz if (err) 392979dfc695SMike Kravetz return err; 393079dfc695SMike Kravetz return len; 393179dfc695SMike Kravetz } 393279dfc695SMike Kravetz HSTATE_ATTR_WO(demote); 393379dfc695SMike Kravetz 393479dfc695SMike Kravetz static ssize_t demote_size_show(struct kobject *kobj, 393579dfc695SMike Kravetz struct kobj_attribute *attr, char *buf) 393679dfc695SMike Kravetz { 393712658abfSMiaohe Lin struct hstate *h = kobj_to_hstate(kobj, NULL); 393879dfc695SMike Kravetz unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K; 393979dfc695SMike Kravetz 394079dfc695SMike Kravetz return sysfs_emit(buf, "%lukB\n", demote_size); 394179dfc695SMike Kravetz } 394279dfc695SMike Kravetz 394379dfc695SMike Kravetz static ssize_t demote_size_store(struct kobject *kobj, 394479dfc695SMike Kravetz struct kobj_attribute *attr, 394579dfc695SMike Kravetz const char *buf, size_t count) 394679dfc695SMike Kravetz { 394779dfc695SMike Kravetz struct hstate *h, *demote_hstate; 394879dfc695SMike Kravetz unsigned long demote_size; 394979dfc695SMike Kravetz unsigned int demote_order; 395079dfc695SMike Kravetz 395179dfc695SMike Kravetz demote_size = (unsigned long)memparse(buf, NULL); 395279dfc695SMike Kravetz 395379dfc695SMike Kravetz demote_hstate = size_to_hstate(demote_size); 395479dfc695SMike Kravetz if (!demote_hstate) 395579dfc695SMike Kravetz return -EINVAL; 395679dfc695SMike Kravetz demote_order = demote_hstate->order; 3957a01f4390SMike Kravetz if (demote_order < HUGETLB_PAGE_ORDER) 3958a01f4390SMike Kravetz return -EINVAL; 395979dfc695SMike Kravetz 396079dfc695SMike Kravetz /* demote order must be smaller than hstate order */ 396112658abfSMiaohe Lin h = kobj_to_hstate(kobj, NULL); 396279dfc695SMike Kravetz if (demote_order >= h->order) 396379dfc695SMike Kravetz return -EINVAL; 396479dfc695SMike Kravetz 396579dfc695SMike Kravetz /* resize_lock synchronizes access to demote size and writes */ 396679dfc695SMike Kravetz mutex_lock(&h->resize_lock); 396779dfc695SMike Kravetz h->demote_order = demote_order; 396879dfc695SMike Kravetz mutex_unlock(&h->resize_lock); 396979dfc695SMike Kravetz 397079dfc695SMike Kravetz return count; 397179dfc695SMike Kravetz } 397279dfc695SMike Kravetz HSTATE_ATTR(demote_size); 397379dfc695SMike Kravetz 3974a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = { 3975a3437870SNishanth Aravamudan &nr_hugepages_attr.attr, 3976a3437870SNishanth Aravamudan &nr_overcommit_hugepages_attr.attr, 3977a3437870SNishanth Aravamudan &free_hugepages_attr.attr, 3978a3437870SNishanth Aravamudan &resv_hugepages_attr.attr, 3979a3437870SNishanth Aravamudan &surplus_hugepages_attr.attr, 398006808b08SLee Schermerhorn #ifdef CONFIG_NUMA 398106808b08SLee Schermerhorn &nr_hugepages_mempolicy_attr.attr, 398206808b08SLee Schermerhorn #endif 3983a3437870SNishanth Aravamudan NULL, 3984a3437870SNishanth Aravamudan }; 3985a3437870SNishanth Aravamudan 398667e5ed96SArvind Yadav static const struct attribute_group hstate_attr_group = { 3987a3437870SNishanth Aravamudan .attrs = hstate_attrs, 3988a3437870SNishanth Aravamudan }; 3989a3437870SNishanth Aravamudan 399079dfc695SMike Kravetz static struct attribute *hstate_demote_attrs[] = { 399179dfc695SMike Kravetz &demote_size_attr.attr, 399279dfc695SMike Kravetz &demote_attr.attr, 399379dfc695SMike Kravetz NULL, 399479dfc695SMike Kravetz }; 399579dfc695SMike Kravetz 399679dfc695SMike Kravetz static const struct attribute_group hstate_demote_attr_group = { 399779dfc695SMike Kravetz .attrs = hstate_demote_attrs, 399879dfc695SMike Kravetz }; 399979dfc695SMike Kravetz 4000094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, 40019a305230SLee Schermerhorn struct kobject **hstate_kobjs, 400267e5ed96SArvind Yadav const struct attribute_group *hstate_attr_group) 4003a3437870SNishanth Aravamudan { 4004a3437870SNishanth Aravamudan int retval; 4005972dc4deSAneesh Kumar K.V int hi = hstate_index(h); 4006a3437870SNishanth Aravamudan 40079a305230SLee Schermerhorn hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); 40089a305230SLee Schermerhorn if (!hstate_kobjs[hi]) 4009a3437870SNishanth Aravamudan return -ENOMEM; 4010a3437870SNishanth Aravamudan 40119a305230SLee Schermerhorn retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group); 4012cc2205a6SMiaohe Lin if (retval) { 40139a305230SLee Schermerhorn kobject_put(hstate_kobjs[hi]); 4014cc2205a6SMiaohe Lin hstate_kobjs[hi] = NULL; 40153a6bdda0SMiaohe Lin return retval; 4016cc2205a6SMiaohe Lin } 4017a3437870SNishanth Aravamudan 401879dfc695SMike Kravetz if (h->demote_order) { 401901088a60SMiaohe Lin retval = sysfs_create_group(hstate_kobjs[hi], 402001088a60SMiaohe Lin &hstate_demote_attr_group); 402101088a60SMiaohe Lin if (retval) { 402279dfc695SMike Kravetz pr_warn("HugeTLB unable to create demote interfaces for %s\n", h->name); 402301088a60SMiaohe Lin sysfs_remove_group(hstate_kobjs[hi], hstate_attr_group); 402401088a60SMiaohe Lin kobject_put(hstate_kobjs[hi]); 402501088a60SMiaohe Lin hstate_kobjs[hi] = NULL; 402601088a60SMiaohe Lin return retval; 402701088a60SMiaohe Lin } 402879dfc695SMike Kravetz } 402979dfc695SMike Kravetz 403001088a60SMiaohe Lin return 0; 4031a3437870SNishanth Aravamudan } 4032a3437870SNishanth Aravamudan 40339a305230SLee Schermerhorn #ifdef CONFIG_NUMA 4034a4a00b45SMuchun Song static bool hugetlb_sysfs_initialized __ro_after_init; 40359a305230SLee Schermerhorn 40369a305230SLee Schermerhorn /* 40379a305230SLee Schermerhorn * node_hstate/s - associate per node hstate attributes, via their kobjects, 403810fbcf4cSKay Sievers * with node devices in node_devices[] using a parallel array. The array 403910fbcf4cSKay Sievers * index of a node device or _hstate == node id. 404010fbcf4cSKay Sievers * This is here to avoid any static dependency of the node device driver, in 40419a305230SLee Schermerhorn * the base kernel, on the hugetlb module. 40429a305230SLee Schermerhorn */ 40439a305230SLee Schermerhorn struct node_hstate { 40449a305230SLee Schermerhorn struct kobject *hugepages_kobj; 40459a305230SLee Schermerhorn struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 40469a305230SLee Schermerhorn }; 4047b4e289a6SAlexander Kuleshov static struct node_hstate node_hstates[MAX_NUMNODES]; 40489a305230SLee Schermerhorn 40499a305230SLee Schermerhorn /* 405010fbcf4cSKay Sievers * A subset of global hstate attributes for node devices 40519a305230SLee Schermerhorn */ 40529a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = { 40539a305230SLee Schermerhorn &nr_hugepages_attr.attr, 40549a305230SLee Schermerhorn &free_hugepages_attr.attr, 40559a305230SLee Schermerhorn &surplus_hugepages_attr.attr, 40569a305230SLee Schermerhorn NULL, 40579a305230SLee Schermerhorn }; 40589a305230SLee Schermerhorn 405967e5ed96SArvind Yadav static const struct attribute_group per_node_hstate_attr_group = { 40609a305230SLee Schermerhorn .attrs = per_node_hstate_attrs, 40619a305230SLee Schermerhorn }; 40629a305230SLee Schermerhorn 40639a305230SLee Schermerhorn /* 406410fbcf4cSKay Sievers * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj. 40659a305230SLee Schermerhorn * Returns node id via non-NULL nidp. 40669a305230SLee Schermerhorn */ 40679a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 40689a305230SLee Schermerhorn { 40699a305230SLee Schermerhorn int nid; 40709a305230SLee Schermerhorn 40719a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) { 40729a305230SLee Schermerhorn struct node_hstate *nhs = &node_hstates[nid]; 40739a305230SLee Schermerhorn int i; 40749a305230SLee Schermerhorn for (i = 0; i < HUGE_MAX_HSTATE; i++) 40759a305230SLee Schermerhorn if (nhs->hstate_kobjs[i] == kobj) { 40769a305230SLee Schermerhorn if (nidp) 40779a305230SLee Schermerhorn *nidp = nid; 40789a305230SLee Schermerhorn return &hstates[i]; 40799a305230SLee Schermerhorn } 40809a305230SLee Schermerhorn } 40819a305230SLee Schermerhorn 40829a305230SLee Schermerhorn BUG(); 40839a305230SLee Schermerhorn return NULL; 40849a305230SLee Schermerhorn } 40859a305230SLee Schermerhorn 40869a305230SLee Schermerhorn /* 408710fbcf4cSKay Sievers * Unregister hstate attributes from a single node device. 40889a305230SLee Schermerhorn * No-op if no hstate attributes attached. 40899a305230SLee Schermerhorn */ 4090a4a00b45SMuchun Song void hugetlb_unregister_node(struct node *node) 40919a305230SLee Schermerhorn { 40929a305230SLee Schermerhorn struct hstate *h; 409310fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 40949a305230SLee Schermerhorn 40959a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 40969b5e5d0fSLee Schermerhorn return; /* no hstate attributes */ 40979a305230SLee Schermerhorn 4098972dc4deSAneesh Kumar K.V for_each_hstate(h) { 4099972dc4deSAneesh Kumar K.V int idx = hstate_index(h); 410001088a60SMiaohe Lin struct kobject *hstate_kobj = nhs->hstate_kobjs[idx]; 410101088a60SMiaohe Lin 410201088a60SMiaohe Lin if (!hstate_kobj) 410301088a60SMiaohe Lin continue; 410401088a60SMiaohe Lin if (h->demote_order) 410501088a60SMiaohe Lin sysfs_remove_group(hstate_kobj, &hstate_demote_attr_group); 410601088a60SMiaohe Lin sysfs_remove_group(hstate_kobj, &per_node_hstate_attr_group); 410701088a60SMiaohe Lin kobject_put(hstate_kobj); 4108972dc4deSAneesh Kumar K.V nhs->hstate_kobjs[idx] = NULL; 4109972dc4deSAneesh Kumar K.V } 41109a305230SLee Schermerhorn 41119a305230SLee Schermerhorn kobject_put(nhs->hugepages_kobj); 41129a305230SLee Schermerhorn nhs->hugepages_kobj = NULL; 41139a305230SLee Schermerhorn } 41149a305230SLee Schermerhorn 41159a305230SLee Schermerhorn 41169a305230SLee Schermerhorn /* 411710fbcf4cSKay Sievers * Register hstate attributes for a single node device. 41189a305230SLee Schermerhorn * No-op if attributes already registered. 41199a305230SLee Schermerhorn */ 4120a4a00b45SMuchun Song void hugetlb_register_node(struct node *node) 41219a305230SLee Schermerhorn { 41229a305230SLee Schermerhorn struct hstate *h; 412310fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 41249a305230SLee Schermerhorn int err; 41259a305230SLee Schermerhorn 4126a4a00b45SMuchun Song if (!hugetlb_sysfs_initialized) 4127a4a00b45SMuchun Song return; 4128a4a00b45SMuchun Song 41299a305230SLee Schermerhorn if (nhs->hugepages_kobj) 41309a305230SLee Schermerhorn return; /* already allocated */ 41319a305230SLee Schermerhorn 41329a305230SLee Schermerhorn nhs->hugepages_kobj = kobject_create_and_add("hugepages", 413310fbcf4cSKay Sievers &node->dev.kobj); 41349a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 41359a305230SLee Schermerhorn return; 41369a305230SLee Schermerhorn 41379a305230SLee Schermerhorn for_each_hstate(h) { 41389a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, 41399a305230SLee Schermerhorn nhs->hstate_kobjs, 41409a305230SLee Schermerhorn &per_node_hstate_attr_group); 41419a305230SLee Schermerhorn if (err) { 4142282f4214SMike Kravetz pr_err("HugeTLB: Unable to add hstate %s for node %d\n", 414310fbcf4cSKay Sievers h->name, node->dev.id); 41449a305230SLee Schermerhorn hugetlb_unregister_node(node); 41459a305230SLee Schermerhorn break; 41469a305230SLee Schermerhorn } 41479a305230SLee Schermerhorn } 41489a305230SLee Schermerhorn } 41499a305230SLee Schermerhorn 41509a305230SLee Schermerhorn /* 41519b5e5d0fSLee Schermerhorn * hugetlb init time: register hstate attributes for all registered node 415210fbcf4cSKay Sievers * devices of nodes that have memory. All on-line nodes should have 415310fbcf4cSKay Sievers * registered their associated device by this time. 41549a305230SLee Schermerhorn */ 41557d9ca000SLuiz Capitulino static void __init hugetlb_register_all_nodes(void) 41569a305230SLee Schermerhorn { 41579a305230SLee Schermerhorn int nid; 41589a305230SLee Schermerhorn 4159a4a00b45SMuchun Song for_each_online_node(nid) 4160b958d4d0SMuchun Song hugetlb_register_node(node_devices[nid]); 41619a305230SLee Schermerhorn } 41629a305230SLee Schermerhorn #else /* !CONFIG_NUMA */ 41639a305230SLee Schermerhorn 41649a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 41659a305230SLee Schermerhorn { 41669a305230SLee Schermerhorn BUG(); 41679a305230SLee Schermerhorn if (nidp) 41689a305230SLee Schermerhorn *nidp = -1; 41699a305230SLee Schermerhorn return NULL; 41709a305230SLee Schermerhorn } 41719a305230SLee Schermerhorn 41729a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { } 41739a305230SLee Schermerhorn 41749a305230SLee Schermerhorn #endif 41759a305230SLee Schermerhorn 4176263b8998SMiaohe Lin #ifdef CONFIG_CMA 4177263b8998SMiaohe Lin static void __init hugetlb_cma_check(void); 4178263b8998SMiaohe Lin #else 4179263b8998SMiaohe Lin static inline __init void hugetlb_cma_check(void) 4180263b8998SMiaohe Lin { 4181263b8998SMiaohe Lin } 4182263b8998SMiaohe Lin #endif 4183263b8998SMiaohe Lin 4184a4a00b45SMuchun Song static void __init hugetlb_sysfs_init(void) 4185a4a00b45SMuchun Song { 4186a4a00b45SMuchun Song struct hstate *h; 4187a4a00b45SMuchun Song int err; 4188a4a00b45SMuchun Song 4189a4a00b45SMuchun Song hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); 4190a4a00b45SMuchun Song if (!hugepages_kobj) 4191a4a00b45SMuchun Song return; 4192a4a00b45SMuchun Song 4193a4a00b45SMuchun Song for_each_hstate(h) { 4194a4a00b45SMuchun Song err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, 4195a4a00b45SMuchun Song hstate_kobjs, &hstate_attr_group); 4196a4a00b45SMuchun Song if (err) 4197a4a00b45SMuchun Song pr_err("HugeTLB: Unable to add hstate %s", h->name); 4198a4a00b45SMuchun Song } 4199a4a00b45SMuchun Song 4200a4a00b45SMuchun Song #ifdef CONFIG_NUMA 4201a4a00b45SMuchun Song hugetlb_sysfs_initialized = true; 4202a4a00b45SMuchun Song #endif 4203a4a00b45SMuchun Song hugetlb_register_all_nodes(); 4204a4a00b45SMuchun Song } 4205a4a00b45SMuchun Song 4206962de548SKefeng Wang #ifdef CONFIG_SYSCTL 4207962de548SKefeng Wang static void hugetlb_sysctl_init(void); 4208962de548SKefeng Wang #else 4209962de548SKefeng Wang static inline void hugetlb_sysctl_init(void) { } 4210962de548SKefeng Wang #endif 4211962de548SKefeng Wang 4212a3437870SNishanth Aravamudan static int __init hugetlb_init(void) 4213a3437870SNishanth Aravamudan { 42148382d914SDavidlohr Bueso int i; 42158382d914SDavidlohr Bueso 4216d6995da3SMike Kravetz BUILD_BUG_ON(sizeof_field(struct page, private) * BITS_PER_BYTE < 4217d6995da3SMike Kravetz __NR_HPAGEFLAGS); 4218d6995da3SMike Kravetz 4219c2833a5bSMike Kravetz if (!hugepages_supported()) { 4220c2833a5bSMike Kravetz if (hugetlb_max_hstate || default_hstate_max_huge_pages) 4221c2833a5bSMike Kravetz pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n"); 42220ef89d25SBenjamin Herrenschmidt return 0; 4223d715cf80SLiam R. Howlett } 4224d715cf80SLiam R. Howlett 4225282f4214SMike Kravetz /* 4226282f4214SMike Kravetz * Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists. Some 4227282f4214SMike Kravetz * architectures depend on setup being done here. 4228282f4214SMike Kravetz */ 4229a3437870SNishanth Aravamudan hugetlb_add_hstate(HUGETLB_PAGE_ORDER); 4230282f4214SMike Kravetz if (!parsed_default_hugepagesz) { 4231282f4214SMike Kravetz /* 4232282f4214SMike Kravetz * If we did not parse a default huge page size, set 4233282f4214SMike Kravetz * default_hstate_idx to HPAGE_SIZE hstate. And, if the 4234282f4214SMike Kravetz * number of huge pages for this default size was implicitly 4235282f4214SMike Kravetz * specified, set that here as well. 4236282f4214SMike Kravetz * Note that the implicit setting will overwrite an explicit 4237282f4214SMike Kravetz * setting. A warning will be printed in this case. 4238282f4214SMike Kravetz */ 4239282f4214SMike Kravetz default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE)); 4240f8b74815SVaishali Thakkar if (default_hstate_max_huge_pages) { 4241282f4214SMike Kravetz if (default_hstate.max_huge_pages) { 4242282f4214SMike Kravetz char buf[32]; 4243282f4214SMike Kravetz 4244282f4214SMike Kravetz string_get_size(huge_page_size(&default_hstate), 4245282f4214SMike Kravetz 1, STRING_UNITS_2, buf, 32); 4246282f4214SMike Kravetz pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n", 4247282f4214SMike Kravetz default_hstate.max_huge_pages, buf); 4248282f4214SMike Kravetz pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n", 4249282f4214SMike Kravetz default_hstate_max_huge_pages); 4250282f4214SMike Kravetz } 4251282f4214SMike Kravetz default_hstate.max_huge_pages = 4252282f4214SMike Kravetz default_hstate_max_huge_pages; 4253b5389086SZhenguo Yao 42540a7a0f6fSPeng Liu for_each_online_node(i) 4255b5389086SZhenguo Yao default_hstate.max_huge_pages_node[i] = 4256b5389086SZhenguo Yao default_hugepages_in_node[i]; 4257282f4214SMike Kravetz } 4258f8b74815SVaishali Thakkar } 4259a3437870SNishanth Aravamudan 4260cf11e85fSRoman Gushchin hugetlb_cma_check(); 4261a3437870SNishanth Aravamudan hugetlb_init_hstates(); 4262aa888a74SAndi Kleen gather_bootmem_prealloc(); 4263a3437870SNishanth Aravamudan report_hugepages(); 4264a3437870SNishanth Aravamudan 4265a3437870SNishanth Aravamudan hugetlb_sysfs_init(); 42667179e7bfSJianguo Wu hugetlb_cgroup_file_init(); 4267962de548SKefeng Wang hugetlb_sysctl_init(); 42689a305230SLee Schermerhorn 42698382d914SDavidlohr Bueso #ifdef CONFIG_SMP 42708382d914SDavidlohr Bueso num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); 42718382d914SDavidlohr Bueso #else 42728382d914SDavidlohr Bueso num_fault_mutexes = 1; 42738382d914SDavidlohr Bueso #endif 4274c672c7f2SMike Kravetz hugetlb_fault_mutex_table = 42756da2ec56SKees Cook kmalloc_array(num_fault_mutexes, sizeof(struct mutex), 42766da2ec56SKees Cook GFP_KERNEL); 4277c672c7f2SMike Kravetz BUG_ON(!hugetlb_fault_mutex_table); 42788382d914SDavidlohr Bueso 42798382d914SDavidlohr Bueso for (i = 0; i < num_fault_mutexes; i++) 4280c672c7f2SMike Kravetz mutex_init(&hugetlb_fault_mutex_table[i]); 4281a3437870SNishanth Aravamudan return 0; 4282a3437870SNishanth Aravamudan } 42833e89e1c5SPaul Gortmaker subsys_initcall(hugetlb_init); 4284a3437870SNishanth Aravamudan 4285ae94da89SMike Kravetz /* Overwritten by architectures with more huge page sizes */ 4286ae94da89SMike Kravetz bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size) 42879fee021dSVaishali Thakkar { 4288ae94da89SMike Kravetz return size == HPAGE_SIZE; 42899fee021dSVaishali Thakkar } 42909fee021dSVaishali Thakkar 4291d00181b9SKirill A. Shutemov void __init hugetlb_add_hstate(unsigned int order) 4292a3437870SNishanth Aravamudan { 4293a3437870SNishanth Aravamudan struct hstate *h; 42948faa8b07SAndi Kleen unsigned long i; 42958faa8b07SAndi Kleen 4296a3437870SNishanth Aravamudan if (size_to_hstate(PAGE_SIZE << order)) { 4297a3437870SNishanth Aravamudan return; 4298a3437870SNishanth Aravamudan } 429947d38344SAneesh Kumar K.V BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); 4300a3437870SNishanth Aravamudan BUG_ON(order == 0); 430147d38344SAneesh Kumar K.V h = &hstates[hugetlb_max_hstate++]; 430229383967SMike Kravetz mutex_init(&h->resize_lock); 4303a3437870SNishanth Aravamudan h->order = order; 4304aca78307SMiaohe Lin h->mask = ~(huge_page_size(h) - 1); 43058faa8b07SAndi Kleen for (i = 0; i < MAX_NUMNODES; ++i) 43068faa8b07SAndi Kleen INIT_LIST_HEAD(&h->hugepage_freelists[i]); 43070edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&h->hugepage_activelist); 430854f18d35SAndrew Morton h->next_nid_to_alloc = first_memory_node; 430954f18d35SAndrew Morton h->next_nid_to_free = first_memory_node; 4310a3437870SNishanth Aravamudan snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", 4311c2c3a60aSMiaohe Lin huge_page_size(h)/SZ_1K); 43128faa8b07SAndi Kleen 4313a3437870SNishanth Aravamudan parsed_hstate = h; 4314a3437870SNishanth Aravamudan } 4315a3437870SNishanth Aravamudan 4316b5389086SZhenguo Yao bool __init __weak hugetlb_node_alloc_supported(void) 4317b5389086SZhenguo Yao { 4318b5389086SZhenguo Yao return true; 4319b5389086SZhenguo Yao } 4320f87442f4SPeng Liu 4321f87442f4SPeng Liu static void __init hugepages_clear_pages_in_node(void) 4322f87442f4SPeng Liu { 4323f87442f4SPeng Liu if (!hugetlb_max_hstate) { 4324f87442f4SPeng Liu default_hstate_max_huge_pages = 0; 4325f87442f4SPeng Liu memset(default_hugepages_in_node, 0, 432610395680SMiaohe Lin sizeof(default_hugepages_in_node)); 4327f87442f4SPeng Liu } else { 4328f87442f4SPeng Liu parsed_hstate->max_huge_pages = 0; 4329f87442f4SPeng Liu memset(parsed_hstate->max_huge_pages_node, 0, 433010395680SMiaohe Lin sizeof(parsed_hstate->max_huge_pages_node)); 4331f87442f4SPeng Liu } 4332f87442f4SPeng Liu } 4333f87442f4SPeng Liu 4334282f4214SMike Kravetz /* 4335282f4214SMike Kravetz * hugepages command line processing 4336282f4214SMike Kravetz * hugepages normally follows a valid hugepagsz or default_hugepagsz 4337282f4214SMike Kravetz * specification. If not, ignore the hugepages value. hugepages can also 4338282f4214SMike Kravetz * be the first huge page command line option in which case it implicitly 4339282f4214SMike Kravetz * specifies the number of huge pages for the default size. 4340282f4214SMike Kravetz */ 4341282f4214SMike Kravetz static int __init hugepages_setup(char *s) 4342a3437870SNishanth Aravamudan { 4343a3437870SNishanth Aravamudan unsigned long *mhp; 43448faa8b07SAndi Kleen static unsigned long *last_mhp; 4345b5389086SZhenguo Yao int node = NUMA_NO_NODE; 4346b5389086SZhenguo Yao int count; 4347b5389086SZhenguo Yao unsigned long tmp; 4348b5389086SZhenguo Yao char *p = s; 4349a3437870SNishanth Aravamudan 43509fee021dSVaishali Thakkar if (!parsed_valid_hugepagesz) { 4351282f4214SMike Kravetz pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s); 43529fee021dSVaishali Thakkar parsed_valid_hugepagesz = true; 4353f81f6e4bSPeng Liu return 1; 43549fee021dSVaishali Thakkar } 4355282f4214SMike Kravetz 4356a3437870SNishanth Aravamudan /* 4357282f4214SMike Kravetz * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter 4358282f4214SMike Kravetz * yet, so this hugepages= parameter goes to the "default hstate". 4359282f4214SMike Kravetz * Otherwise, it goes with the previously parsed hugepagesz or 4360282f4214SMike Kravetz * default_hugepagesz. 4361a3437870SNishanth Aravamudan */ 43629fee021dSVaishali Thakkar else if (!hugetlb_max_hstate) 4363a3437870SNishanth Aravamudan mhp = &default_hstate_max_huge_pages; 4364a3437870SNishanth Aravamudan else 4365a3437870SNishanth Aravamudan mhp = &parsed_hstate->max_huge_pages; 4366a3437870SNishanth Aravamudan 43678faa8b07SAndi Kleen if (mhp == last_mhp) { 4368282f4214SMike Kravetz pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s); 4369f81f6e4bSPeng Liu return 1; 43708faa8b07SAndi Kleen } 43718faa8b07SAndi Kleen 4372b5389086SZhenguo Yao while (*p) { 4373b5389086SZhenguo Yao count = 0; 4374b5389086SZhenguo Yao if (sscanf(p, "%lu%n", &tmp, &count) != 1) 4375b5389086SZhenguo Yao goto invalid; 4376b5389086SZhenguo Yao /* Parameter is node format */ 4377b5389086SZhenguo Yao if (p[count] == ':') { 4378b5389086SZhenguo Yao if (!hugetlb_node_alloc_supported()) { 4379b5389086SZhenguo Yao pr_warn("HugeTLB: architecture can't support node specific alloc, ignoring!\n"); 4380f81f6e4bSPeng Liu return 1; 4381b5389086SZhenguo Yao } 43820a7a0f6fSPeng Liu if (tmp >= MAX_NUMNODES || !node_online(tmp)) 4383e79ce983SLiu Yuntao goto invalid; 43840a7a0f6fSPeng Liu node = array_index_nospec(tmp, MAX_NUMNODES); 4385b5389086SZhenguo Yao p += count + 1; 4386b5389086SZhenguo Yao /* Parse hugepages */ 4387b5389086SZhenguo Yao if (sscanf(p, "%lu%n", &tmp, &count) != 1) 4388b5389086SZhenguo Yao goto invalid; 4389b5389086SZhenguo Yao if (!hugetlb_max_hstate) 4390b5389086SZhenguo Yao default_hugepages_in_node[node] = tmp; 4391b5389086SZhenguo Yao else 4392b5389086SZhenguo Yao parsed_hstate->max_huge_pages_node[node] = tmp; 4393b5389086SZhenguo Yao *mhp += tmp; 4394b5389086SZhenguo Yao /* Go to parse next node*/ 4395b5389086SZhenguo Yao if (p[count] == ',') 4396b5389086SZhenguo Yao p += count + 1; 4397b5389086SZhenguo Yao else 4398b5389086SZhenguo Yao break; 4399b5389086SZhenguo Yao } else { 4400b5389086SZhenguo Yao if (p != s) 4401b5389086SZhenguo Yao goto invalid; 4402b5389086SZhenguo Yao *mhp = tmp; 4403b5389086SZhenguo Yao break; 4404b5389086SZhenguo Yao } 4405b5389086SZhenguo Yao } 4406a3437870SNishanth Aravamudan 44078faa8b07SAndi Kleen /* 44088faa8b07SAndi Kleen * Global state is always initialized later in hugetlb_init. 440904adbc3fSMiaohe Lin * But we need to allocate gigantic hstates here early to still 44108faa8b07SAndi Kleen * use the bootmem allocator. 44118faa8b07SAndi Kleen */ 441204adbc3fSMiaohe Lin if (hugetlb_max_hstate && hstate_is_gigantic(parsed_hstate)) 44138faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(parsed_hstate); 44148faa8b07SAndi Kleen 44158faa8b07SAndi Kleen last_mhp = mhp; 44168faa8b07SAndi Kleen 4417a3437870SNishanth Aravamudan return 1; 4418b5389086SZhenguo Yao 4419b5389086SZhenguo Yao invalid: 4420b5389086SZhenguo Yao pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p); 4421f87442f4SPeng Liu hugepages_clear_pages_in_node(); 4422f81f6e4bSPeng Liu return 1; 4423a3437870SNishanth Aravamudan } 4424282f4214SMike Kravetz __setup("hugepages=", hugepages_setup); 4425e11bfbfcSNick Piggin 4426282f4214SMike Kravetz /* 4427282f4214SMike Kravetz * hugepagesz command line processing 4428282f4214SMike Kravetz * A specific huge page size can only be specified once with hugepagesz. 4429282f4214SMike Kravetz * hugepagesz is followed by hugepages on the command line. The global 4430282f4214SMike Kravetz * variable 'parsed_valid_hugepagesz' is used to determine if prior 4431282f4214SMike Kravetz * hugepagesz argument was valid. 4432282f4214SMike Kravetz */ 4433359f2544SMike Kravetz static int __init hugepagesz_setup(char *s) 4434e11bfbfcSNick Piggin { 4435359f2544SMike Kravetz unsigned long size; 4436282f4214SMike Kravetz struct hstate *h; 4437282f4214SMike Kravetz 4438282f4214SMike Kravetz parsed_valid_hugepagesz = false; 4439359f2544SMike Kravetz size = (unsigned long)memparse(s, NULL); 4440359f2544SMike Kravetz 4441359f2544SMike Kravetz if (!arch_hugetlb_valid_size(size)) { 4442282f4214SMike Kravetz pr_err("HugeTLB: unsupported hugepagesz=%s\n", s); 4443f81f6e4bSPeng Liu return 1; 4444359f2544SMike Kravetz } 4445359f2544SMike Kravetz 4446282f4214SMike Kravetz h = size_to_hstate(size); 4447282f4214SMike Kravetz if (h) { 4448282f4214SMike Kravetz /* 4449282f4214SMike Kravetz * hstate for this size already exists. This is normally 4450282f4214SMike Kravetz * an error, but is allowed if the existing hstate is the 4451282f4214SMike Kravetz * default hstate. More specifically, it is only allowed if 4452282f4214SMike Kravetz * the number of huge pages for the default hstate was not 4453282f4214SMike Kravetz * previously specified. 4454282f4214SMike Kravetz */ 4455282f4214SMike Kravetz if (!parsed_default_hugepagesz || h != &default_hstate || 4456282f4214SMike Kravetz default_hstate.max_huge_pages) { 4457282f4214SMike Kravetz pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s); 4458f81f6e4bSPeng Liu return 1; 445938237830SMike Kravetz } 446038237830SMike Kravetz 4461282f4214SMike Kravetz /* 4462282f4214SMike Kravetz * No need to call hugetlb_add_hstate() as hstate already 4463282f4214SMike Kravetz * exists. But, do set parsed_hstate so that a following 4464282f4214SMike Kravetz * hugepages= parameter will be applied to this hstate. 4465282f4214SMike Kravetz */ 4466282f4214SMike Kravetz parsed_hstate = h; 4467282f4214SMike Kravetz parsed_valid_hugepagesz = true; 4468e11bfbfcSNick Piggin return 1; 4469e11bfbfcSNick Piggin } 4470282f4214SMike Kravetz 4471359f2544SMike Kravetz hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); 4472282f4214SMike Kravetz parsed_valid_hugepagesz = true; 4473359f2544SMike Kravetz return 1; 4474359f2544SMike Kravetz } 4475359f2544SMike Kravetz __setup("hugepagesz=", hugepagesz_setup); 4476359f2544SMike Kravetz 4477282f4214SMike Kravetz /* 4478282f4214SMike Kravetz * default_hugepagesz command line input 4479282f4214SMike Kravetz * Only one instance of default_hugepagesz allowed on command line. 4480282f4214SMike Kravetz */ 4481ae94da89SMike Kravetz static int __init default_hugepagesz_setup(char *s) 4482e11bfbfcSNick Piggin { 4483ae94da89SMike Kravetz unsigned long size; 4484b5389086SZhenguo Yao int i; 4485ae94da89SMike Kravetz 4486282f4214SMike Kravetz parsed_valid_hugepagesz = false; 4487282f4214SMike Kravetz if (parsed_default_hugepagesz) { 4488282f4214SMike Kravetz pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s); 4489f81f6e4bSPeng Liu return 1; 4490282f4214SMike Kravetz } 4491282f4214SMike Kravetz 4492282f4214SMike Kravetz size = (unsigned long)memparse(s, NULL); 4493282f4214SMike Kravetz 4494282f4214SMike Kravetz if (!arch_hugetlb_valid_size(size)) { 4495282f4214SMike Kravetz pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s); 4496f81f6e4bSPeng Liu return 1; 4497282f4214SMike Kravetz } 4498282f4214SMike Kravetz 4499282f4214SMike Kravetz hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); 4500282f4214SMike Kravetz parsed_valid_hugepagesz = true; 4501282f4214SMike Kravetz parsed_default_hugepagesz = true; 4502282f4214SMike Kravetz default_hstate_idx = hstate_index(size_to_hstate(size)); 4503282f4214SMike Kravetz 4504282f4214SMike Kravetz /* 4505282f4214SMike Kravetz * The number of default huge pages (for this size) could have been 4506282f4214SMike Kravetz * specified as the first hugetlb parameter: hugepages=X. If so, 4507282f4214SMike Kravetz * then default_hstate_max_huge_pages is set. If the default huge 450823baf831SKirill A. Shutemov * page size is gigantic (> MAX_ORDER), then the pages must be 4509282f4214SMike Kravetz * allocated here from bootmem allocator. 4510282f4214SMike Kravetz */ 4511282f4214SMike Kravetz if (default_hstate_max_huge_pages) { 4512282f4214SMike Kravetz default_hstate.max_huge_pages = default_hstate_max_huge_pages; 45130a7a0f6fSPeng Liu for_each_online_node(i) 4514b5389086SZhenguo Yao default_hstate.max_huge_pages_node[i] = 4515b5389086SZhenguo Yao default_hugepages_in_node[i]; 4516282f4214SMike Kravetz if (hstate_is_gigantic(&default_hstate)) 4517282f4214SMike Kravetz hugetlb_hstate_alloc_pages(&default_hstate); 4518282f4214SMike Kravetz default_hstate_max_huge_pages = 0; 4519282f4214SMike Kravetz } 4520282f4214SMike Kravetz 4521e11bfbfcSNick Piggin return 1; 4522e11bfbfcSNick Piggin } 4523ae94da89SMike Kravetz __setup("default_hugepagesz=", default_hugepagesz_setup); 4524a3437870SNishanth Aravamudan 4525d2226ebdSFeng Tang static nodemask_t *policy_mbind_nodemask(gfp_t gfp) 4526d2226ebdSFeng Tang { 4527d2226ebdSFeng Tang #ifdef CONFIG_NUMA 4528d2226ebdSFeng Tang struct mempolicy *mpol = get_task_policy(current); 4529d2226ebdSFeng Tang 4530d2226ebdSFeng Tang /* 4531d2226ebdSFeng Tang * Only enforce MPOL_BIND policy which overlaps with cpuset policy 4532d2226ebdSFeng Tang * (from policy_nodemask) specifically for hugetlb case 4533d2226ebdSFeng Tang */ 4534d2226ebdSFeng Tang if (mpol->mode == MPOL_BIND && 4535d2226ebdSFeng Tang (apply_policy_zone(mpol, gfp_zone(gfp)) && 4536d2226ebdSFeng Tang cpuset_nodemask_valid_mems_allowed(&mpol->nodes))) 4537d2226ebdSFeng Tang return &mpol->nodes; 4538d2226ebdSFeng Tang #endif 4539d2226ebdSFeng Tang return NULL; 4540d2226ebdSFeng Tang } 4541d2226ebdSFeng Tang 45428ca39e68SMuchun Song static unsigned int allowed_mems_nr(struct hstate *h) 45438a213460SNishanth Aravamudan { 45448a213460SNishanth Aravamudan int node; 45458a213460SNishanth Aravamudan unsigned int nr = 0; 4546d2226ebdSFeng Tang nodemask_t *mbind_nodemask; 45478ca39e68SMuchun Song unsigned int *array = h->free_huge_pages_node; 45488ca39e68SMuchun Song gfp_t gfp_mask = htlb_alloc_mask(h); 45498a213460SNishanth Aravamudan 4550d2226ebdSFeng Tang mbind_nodemask = policy_mbind_nodemask(gfp_mask); 45518ca39e68SMuchun Song for_each_node_mask(node, cpuset_current_mems_allowed) { 4552d2226ebdSFeng Tang if (!mbind_nodemask || node_isset(node, *mbind_nodemask)) 45538a213460SNishanth Aravamudan nr += array[node]; 45548ca39e68SMuchun Song } 45558a213460SNishanth Aravamudan 45568a213460SNishanth Aravamudan return nr; 45578a213460SNishanth Aravamudan } 45588a213460SNishanth Aravamudan 45598a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL 456017743798SMuchun Song static int proc_hugetlb_doulongvec_minmax(struct ctl_table *table, int write, 456117743798SMuchun Song void *buffer, size_t *length, 456217743798SMuchun Song loff_t *ppos, unsigned long *out) 456317743798SMuchun Song { 456417743798SMuchun Song struct ctl_table dup_table; 456517743798SMuchun Song 456617743798SMuchun Song /* 456717743798SMuchun Song * In order to avoid races with __do_proc_doulongvec_minmax(), we 456817743798SMuchun Song * can duplicate the @table and alter the duplicate of it. 456917743798SMuchun Song */ 457017743798SMuchun Song dup_table = *table; 457117743798SMuchun Song dup_table.data = out; 457217743798SMuchun Song 457317743798SMuchun Song return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos); 457417743798SMuchun Song } 457517743798SMuchun Song 457606808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy, 457706808b08SLee Schermerhorn struct ctl_table *table, int write, 457832927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 45791da177e4SLinus Torvalds { 4580e5ff2159SAndi Kleen struct hstate *h = &default_hstate; 4581238d3c13SDavid Rientjes unsigned long tmp = h->max_huge_pages; 458208d4a246SMichal Hocko int ret; 4583e5ff2159SAndi Kleen 4584457c1b27SNishanth Aravamudan if (!hugepages_supported()) 458586613628SJan Stancek return -EOPNOTSUPP; 4586457c1b27SNishanth Aravamudan 458717743798SMuchun Song ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, 458817743798SMuchun Song &tmp); 458908d4a246SMichal Hocko if (ret) 459008d4a246SMichal Hocko goto out; 4591e5ff2159SAndi Kleen 4592238d3c13SDavid Rientjes if (write) 4593238d3c13SDavid Rientjes ret = __nr_hugepages_store_common(obey_mempolicy, h, 4594238d3c13SDavid Rientjes NUMA_NO_NODE, tmp, *length); 459508d4a246SMichal Hocko out: 459608d4a246SMichal Hocko return ret; 45971da177e4SLinus Torvalds } 4598396faf03SMel Gorman 4599962de548SKefeng Wang static int hugetlb_sysctl_handler(struct ctl_table *table, int write, 460032927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 460106808b08SLee Schermerhorn { 460206808b08SLee Schermerhorn 460306808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(false, table, write, 460406808b08SLee Schermerhorn buffer, length, ppos); 460506808b08SLee Schermerhorn } 460606808b08SLee Schermerhorn 460706808b08SLee Schermerhorn #ifdef CONFIG_NUMA 4608962de548SKefeng Wang static int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write, 460932927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 461006808b08SLee Schermerhorn { 461106808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(true, table, write, 461206808b08SLee Schermerhorn buffer, length, ppos); 461306808b08SLee Schermerhorn } 461406808b08SLee Schermerhorn #endif /* CONFIG_NUMA */ 461506808b08SLee Schermerhorn 4616962de548SKefeng Wang static int hugetlb_overcommit_handler(struct ctl_table *table, int write, 461732927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 4618a3d0c6aaSNishanth Aravamudan { 4619a5516438SAndi Kleen struct hstate *h = &default_hstate; 4620e5ff2159SAndi Kleen unsigned long tmp; 462108d4a246SMichal Hocko int ret; 4622e5ff2159SAndi Kleen 4623457c1b27SNishanth Aravamudan if (!hugepages_supported()) 462486613628SJan Stancek return -EOPNOTSUPP; 4625457c1b27SNishanth Aravamudan 4626e5ff2159SAndi Kleen tmp = h->nr_overcommit_huge_pages; 4627e5ff2159SAndi Kleen 4628bae7f4aeSLuiz Capitulino if (write && hstate_is_gigantic(h)) 4629adbe8726SEric B Munson return -EINVAL; 4630adbe8726SEric B Munson 463117743798SMuchun Song ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, 463217743798SMuchun Song &tmp); 463308d4a246SMichal Hocko if (ret) 463408d4a246SMichal Hocko goto out; 4635e5ff2159SAndi Kleen 4636e5ff2159SAndi Kleen if (write) { 4637db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 4638e5ff2159SAndi Kleen h->nr_overcommit_huge_pages = tmp; 4639db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 4640e5ff2159SAndi Kleen } 464108d4a246SMichal Hocko out: 464208d4a246SMichal Hocko return ret; 4643a3d0c6aaSNishanth Aravamudan } 4644a3d0c6aaSNishanth Aravamudan 4645962de548SKefeng Wang static struct ctl_table hugetlb_table[] = { 4646962de548SKefeng Wang { 4647962de548SKefeng Wang .procname = "nr_hugepages", 4648962de548SKefeng Wang .data = NULL, 4649962de548SKefeng Wang .maxlen = sizeof(unsigned long), 4650962de548SKefeng Wang .mode = 0644, 4651962de548SKefeng Wang .proc_handler = hugetlb_sysctl_handler, 4652962de548SKefeng Wang }, 4653962de548SKefeng Wang #ifdef CONFIG_NUMA 4654962de548SKefeng Wang { 4655962de548SKefeng Wang .procname = "nr_hugepages_mempolicy", 4656962de548SKefeng Wang .data = NULL, 4657962de548SKefeng Wang .maxlen = sizeof(unsigned long), 4658962de548SKefeng Wang .mode = 0644, 4659962de548SKefeng Wang .proc_handler = &hugetlb_mempolicy_sysctl_handler, 4660962de548SKefeng Wang }, 4661962de548SKefeng Wang #endif 4662962de548SKefeng Wang { 4663962de548SKefeng Wang .procname = "hugetlb_shm_group", 4664962de548SKefeng Wang .data = &sysctl_hugetlb_shm_group, 4665962de548SKefeng Wang .maxlen = sizeof(gid_t), 4666962de548SKefeng Wang .mode = 0644, 4667962de548SKefeng Wang .proc_handler = proc_dointvec, 4668962de548SKefeng Wang }, 4669962de548SKefeng Wang { 4670962de548SKefeng Wang .procname = "nr_overcommit_hugepages", 4671962de548SKefeng Wang .data = NULL, 4672962de548SKefeng Wang .maxlen = sizeof(unsigned long), 4673962de548SKefeng Wang .mode = 0644, 4674962de548SKefeng Wang .proc_handler = hugetlb_overcommit_handler, 4675962de548SKefeng Wang }, 4676962de548SKefeng Wang { } 4677962de548SKefeng Wang }; 4678962de548SKefeng Wang 4679962de548SKefeng Wang static void hugetlb_sysctl_init(void) 4680962de548SKefeng Wang { 4681962de548SKefeng Wang register_sysctl_init("vm", hugetlb_table); 4682962de548SKefeng Wang } 46831da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 46841da177e4SLinus Torvalds 4685e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m) 46861da177e4SLinus Torvalds { 4687fcb2b0c5SRoman Gushchin struct hstate *h; 4688fcb2b0c5SRoman Gushchin unsigned long total = 0; 4689fcb2b0c5SRoman Gushchin 4690457c1b27SNishanth Aravamudan if (!hugepages_supported()) 4691457c1b27SNishanth Aravamudan return; 4692fcb2b0c5SRoman Gushchin 4693fcb2b0c5SRoman Gushchin for_each_hstate(h) { 4694fcb2b0c5SRoman Gushchin unsigned long count = h->nr_huge_pages; 4695fcb2b0c5SRoman Gushchin 4696aca78307SMiaohe Lin total += huge_page_size(h) * count; 4697fcb2b0c5SRoman Gushchin 4698fcb2b0c5SRoman Gushchin if (h == &default_hstate) 4699e1759c21SAlexey Dobriyan seq_printf(m, 47001da177e4SLinus Torvalds "HugePages_Total: %5lu\n" 47011da177e4SLinus Torvalds "HugePages_Free: %5lu\n" 4702b45b5bd6SDavid Gibson "HugePages_Rsvd: %5lu\n" 47037893d1d5SAdam Litke "HugePages_Surp: %5lu\n" 47044f98a2feSRik van Riel "Hugepagesize: %8lu kB\n", 4705fcb2b0c5SRoman Gushchin count, 4706a5516438SAndi Kleen h->free_huge_pages, 4707a5516438SAndi Kleen h->resv_huge_pages, 4708a5516438SAndi Kleen h->surplus_huge_pages, 4709aca78307SMiaohe Lin huge_page_size(h) / SZ_1K); 4710fcb2b0c5SRoman Gushchin } 4711fcb2b0c5SRoman Gushchin 4712aca78307SMiaohe Lin seq_printf(m, "Hugetlb: %8lu kB\n", total / SZ_1K); 47131da177e4SLinus Torvalds } 47141da177e4SLinus Torvalds 47157981593bSJoe Perches int hugetlb_report_node_meminfo(char *buf, int len, int nid) 47161da177e4SLinus Torvalds { 4717a5516438SAndi Kleen struct hstate *h = &default_hstate; 47187981593bSJoe Perches 4719457c1b27SNishanth Aravamudan if (!hugepages_supported()) 4720457c1b27SNishanth Aravamudan return 0; 47217981593bSJoe Perches 47227981593bSJoe Perches return sysfs_emit_at(buf, len, 47231da177e4SLinus Torvalds "Node %d HugePages_Total: %5u\n" 4724a1de0919SNishanth Aravamudan "Node %d HugePages_Free: %5u\n" 4725a1de0919SNishanth Aravamudan "Node %d HugePages_Surp: %5u\n", 4726a5516438SAndi Kleen nid, h->nr_huge_pages_node[nid], 4727a5516438SAndi Kleen nid, h->free_huge_pages_node[nid], 4728a5516438SAndi Kleen nid, h->surplus_huge_pages_node[nid]); 47291da177e4SLinus Torvalds } 47301da177e4SLinus Torvalds 4731dcadcf1cSGang Li void hugetlb_show_meminfo_node(int nid) 4732949f7ec5SDavid Rientjes { 4733949f7ec5SDavid Rientjes struct hstate *h; 4734949f7ec5SDavid Rientjes 4735457c1b27SNishanth Aravamudan if (!hugepages_supported()) 4736457c1b27SNishanth Aravamudan return; 4737457c1b27SNishanth Aravamudan 4738949f7ec5SDavid Rientjes for_each_hstate(h) 4739dcadcf1cSGang Li printk("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", 4740949f7ec5SDavid Rientjes nid, 4741949f7ec5SDavid Rientjes h->nr_huge_pages_node[nid], 4742949f7ec5SDavid Rientjes h->free_huge_pages_node[nid], 4743949f7ec5SDavid Rientjes h->surplus_huge_pages_node[nid], 4744aca78307SMiaohe Lin huge_page_size(h) / SZ_1K); 4745949f7ec5SDavid Rientjes } 4746949f7ec5SDavid Rientjes 47475d317b2bSNaoya Horiguchi void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm) 47485d317b2bSNaoya Horiguchi { 47495d317b2bSNaoya Horiguchi seq_printf(m, "HugetlbPages:\t%8lu kB\n", 47505d317b2bSNaoya Horiguchi atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10)); 47515d317b2bSNaoya Horiguchi } 47525d317b2bSNaoya Horiguchi 47531da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 47541da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void) 47551da177e4SLinus Torvalds { 4756d0028588SWanpeng Li struct hstate *h; 4757d0028588SWanpeng Li unsigned long nr_total_pages = 0; 4758d0028588SWanpeng Li 4759d0028588SWanpeng Li for_each_hstate(h) 4760d0028588SWanpeng Li nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); 4761d0028588SWanpeng Li return nr_total_pages; 47621da177e4SLinus Torvalds } 47631da177e4SLinus Torvalds 4764a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta) 4765fc1b8a73SMel Gorman { 4766fc1b8a73SMel Gorman int ret = -ENOMEM; 4767fc1b8a73SMel Gorman 47680aa7f354SMiaohe Lin if (!delta) 47690aa7f354SMiaohe Lin return 0; 47700aa7f354SMiaohe Lin 4771db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 4772fc1b8a73SMel Gorman /* 4773fc1b8a73SMel Gorman * When cpuset is configured, it breaks the strict hugetlb page 4774fc1b8a73SMel Gorman * reservation as the accounting is done on a global variable. Such 4775fc1b8a73SMel Gorman * reservation is completely rubbish in the presence of cpuset because 4776fc1b8a73SMel Gorman * the reservation is not checked against page availability for the 4777fc1b8a73SMel Gorman * current cpuset. Application can still potentially OOM'ed by kernel 4778fc1b8a73SMel Gorman * with lack of free htlb page in cpuset that the task is in. 4779fc1b8a73SMel Gorman * Attempt to enforce strict accounting with cpuset is almost 4780fc1b8a73SMel Gorman * impossible (or too ugly) because cpuset is too fluid that 4781fc1b8a73SMel Gorman * task or memory node can be dynamically moved between cpusets. 4782fc1b8a73SMel Gorman * 4783fc1b8a73SMel Gorman * The change of semantics for shared hugetlb mapping with cpuset is 4784fc1b8a73SMel Gorman * undesirable. However, in order to preserve some of the semantics, 4785fc1b8a73SMel Gorman * we fall back to check against current free page availability as 4786fc1b8a73SMel Gorman * a best attempt and hopefully to minimize the impact of changing 4787fc1b8a73SMel Gorman * semantics that cpuset has. 47888ca39e68SMuchun Song * 47898ca39e68SMuchun Song * Apart from cpuset, we also have memory policy mechanism that 47908ca39e68SMuchun Song * also determines from which node the kernel will allocate memory 47918ca39e68SMuchun Song * in a NUMA system. So similar to cpuset, we also should consider 47928ca39e68SMuchun Song * the memory policy of the current task. Similar to the description 47938ca39e68SMuchun Song * above. 4794fc1b8a73SMel Gorman */ 4795fc1b8a73SMel Gorman if (delta > 0) { 4796a5516438SAndi Kleen if (gather_surplus_pages(h, delta) < 0) 4797fc1b8a73SMel Gorman goto out; 4798fc1b8a73SMel Gorman 47998ca39e68SMuchun Song if (delta > allowed_mems_nr(h)) { 4800a5516438SAndi Kleen return_unused_surplus_pages(h, delta); 4801fc1b8a73SMel Gorman goto out; 4802fc1b8a73SMel Gorman } 4803fc1b8a73SMel Gorman } 4804fc1b8a73SMel Gorman 4805fc1b8a73SMel Gorman ret = 0; 4806fc1b8a73SMel Gorman if (delta < 0) 4807a5516438SAndi Kleen return_unused_surplus_pages(h, (unsigned long) -delta); 4808fc1b8a73SMel Gorman 4809fc1b8a73SMel Gorman out: 4810db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 4811fc1b8a73SMel Gorman return ret; 4812fc1b8a73SMel Gorman } 4813fc1b8a73SMel Gorman 481484afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma) 481584afd99bSAndy Whitcroft { 4816f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 481784afd99bSAndy Whitcroft 481884afd99bSAndy Whitcroft /* 4819612b8a31SMike Kravetz * HPAGE_RESV_OWNER indicates a private mapping. 482084afd99bSAndy Whitcroft * This new VMA should share its siblings reservation map if present. 482184afd99bSAndy Whitcroft * The VMA will only ever have a valid reservation map pointer where 482284afd99bSAndy Whitcroft * it is being copied for another still existing VMA. As that VMA 482325985edcSLucas De Marchi * has a reference to the reservation map it cannot disappear until 482484afd99bSAndy Whitcroft * after this open call completes. It is therefore safe to take a 482584afd99bSAndy Whitcroft * new reference here without additional locking. 482684afd99bSAndy Whitcroft */ 482709a26e83SMike Kravetz if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { 482809a26e83SMike Kravetz resv_map_dup_hugetlb_cgroup_uncharge_info(resv); 4829f522c3acSJoonsoo Kim kref_get(&resv->refs); 483084afd99bSAndy Whitcroft } 48318d9bfb26SMike Kravetz 4832131a79b4SMike Kravetz /* 4833131a79b4SMike Kravetz * vma_lock structure for sharable mappings is vma specific. 4834612b8a31SMike Kravetz * Clear old pointer (if copied via vm_area_dup) and allocate 4835612b8a31SMike Kravetz * new structure. Before clearing, make sure vma_lock is not 4836612b8a31SMike Kravetz * for this vma. 4837131a79b4SMike Kravetz */ 4838131a79b4SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 4839612b8a31SMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 4840612b8a31SMike Kravetz 4841612b8a31SMike Kravetz if (vma_lock) { 4842612b8a31SMike Kravetz if (vma_lock->vma != vma) { 4843131a79b4SMike Kravetz vma->vm_private_data = NULL; 48448d9bfb26SMike Kravetz hugetlb_vma_lock_alloc(vma); 4845612b8a31SMike Kravetz } else 4846612b8a31SMike Kravetz pr_warn("HugeTLB: vma_lock already exists in %s.\n", __func__); 4847612b8a31SMike Kravetz } else 4848612b8a31SMike Kravetz hugetlb_vma_lock_alloc(vma); 484909a26e83SMike Kravetz } 4850131a79b4SMike Kravetz } 485184afd99bSAndy Whitcroft 4852a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma) 4853a1e78772SMel Gorman { 4854a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 48558d9bfb26SMike Kravetz struct resv_map *resv; 485690481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 48574e35f483SJoonsoo Kim unsigned long reserve, start, end; 48581c5ecae3SMike Kravetz long gbl_reserve; 485984afd99bSAndy Whitcroft 48608d9bfb26SMike Kravetz hugetlb_vma_lock_free(vma); 48618d9bfb26SMike Kravetz 48628d9bfb26SMike Kravetz resv = vma_resv_map(vma); 48634e35f483SJoonsoo Kim if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 48644e35f483SJoonsoo Kim return; 48654e35f483SJoonsoo Kim 4866a5516438SAndi Kleen start = vma_hugecache_offset(h, vma, vma->vm_start); 4867a5516438SAndi Kleen end = vma_hugecache_offset(h, vma, vma->vm_end); 486884afd99bSAndy Whitcroft 48694e35f483SJoonsoo Kim reserve = (end - start) - region_count(resv, start, end); 4870e9fe92aeSMina Almasry hugetlb_cgroup_uncharge_counter(resv, start, end); 48717251ff78SAdam Litke if (reserve) { 48721c5ecae3SMike Kravetz /* 48731c5ecae3SMike Kravetz * Decrement reserve counts. The global reserve count may be 48741c5ecae3SMike Kravetz * adjusted if the subpool has a minimum size. 48751c5ecae3SMike Kravetz */ 48761c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, reserve); 48771c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 48787251ff78SAdam Litke } 4879e9fe92aeSMina Almasry 4880e9fe92aeSMina Almasry kref_put(&resv->refs, resv_map_release); 4881a1e78772SMel Gorman } 4882a1e78772SMel Gorman 488331383c68SDan Williams static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr) 488431383c68SDan Williams { 488531383c68SDan Williams if (addr & ~(huge_page_mask(hstate_vma(vma)))) 488631383c68SDan Williams return -EINVAL; 4887b30c14cdSJames Houghton 4888b30c14cdSJames Houghton /* 4889b30c14cdSJames Houghton * PMD sharing is only possible for PUD_SIZE-aligned address ranges 4890b30c14cdSJames Houghton * in HugeTLB VMAs. If we will lose PUD_SIZE alignment due to this 4891b30c14cdSJames Houghton * split, unshare PMDs in the PUD_SIZE interval surrounding addr now. 4892b30c14cdSJames Houghton */ 4893b30c14cdSJames Houghton if (addr & ~PUD_MASK) { 4894b30c14cdSJames Houghton /* 4895b30c14cdSJames Houghton * hugetlb_vm_op_split is called right before we attempt to 4896b30c14cdSJames Houghton * split the VMA. We will need to unshare PMDs in the old and 4897b30c14cdSJames Houghton * new VMAs, so let's unshare before we split. 4898b30c14cdSJames Houghton */ 4899b30c14cdSJames Houghton unsigned long floor = addr & PUD_MASK; 4900b30c14cdSJames Houghton unsigned long ceil = floor + PUD_SIZE; 4901b30c14cdSJames Houghton 4902b30c14cdSJames Houghton if (floor >= vma->vm_start && ceil <= vma->vm_end) 4903b30c14cdSJames Houghton hugetlb_unshare_pmds(vma, floor, ceil); 4904b30c14cdSJames Houghton } 4905b30c14cdSJames Houghton 490631383c68SDan Williams return 0; 490731383c68SDan Williams } 490831383c68SDan Williams 490905ea8860SDan Williams static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma) 491005ea8860SDan Williams { 4911aca78307SMiaohe Lin return huge_page_size(hstate_vma(vma)); 491205ea8860SDan Williams } 491305ea8860SDan Williams 49141da177e4SLinus Torvalds /* 49151da177e4SLinus Torvalds * We cannot handle pagefaults against hugetlb pages at all. They cause 49161da177e4SLinus Torvalds * handle_mm_fault() to try to instantiate regular-sized pages in the 49176c26d310SMiaohe Lin * hugepage VMA. do_page_fault() is supposed to trap this, so BUG is we get 49181da177e4SLinus Torvalds * this far. 49191da177e4SLinus Torvalds */ 4920b3ec9f33SSouptick Joarder static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf) 49211da177e4SLinus Torvalds { 49221da177e4SLinus Torvalds BUG(); 4923d0217ac0SNick Piggin return 0; 49241da177e4SLinus Torvalds } 49251da177e4SLinus Torvalds 4926eec3636aSJane Chu /* 4927eec3636aSJane Chu * When a new function is introduced to vm_operations_struct and added 4928eec3636aSJane Chu * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops. 4929eec3636aSJane Chu * This is because under System V memory model, mappings created via 4930eec3636aSJane Chu * shmget/shmat with "huge page" specified are backed by hugetlbfs files, 4931eec3636aSJane Chu * their original vm_ops are overwritten with shm_vm_ops. 4932eec3636aSJane Chu */ 4933f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = { 4934d0217ac0SNick Piggin .fault = hugetlb_vm_op_fault, 493584afd99bSAndy Whitcroft .open = hugetlb_vm_op_open, 4936a1e78772SMel Gorman .close = hugetlb_vm_op_close, 4937dd3b614fSDmitry Safonov .may_split = hugetlb_vm_op_split, 493805ea8860SDan Williams .pagesize = hugetlb_vm_op_pagesize, 49391da177e4SLinus Torvalds }; 49401da177e4SLinus Torvalds 49411e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, 49421e8f889bSDavid Gibson int writable) 494363551ae0SDavid Gibson { 494463551ae0SDavid Gibson pte_t entry; 494579c1c594SChristophe Leroy unsigned int shift = huge_page_shift(hstate_vma(vma)); 494663551ae0SDavid Gibson 49471e8f889bSDavid Gibson if (writable) { 4948106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, 4949106c992aSGerald Schaefer vma->vm_page_prot))); 495063551ae0SDavid Gibson } else { 4951106c992aSGerald Schaefer entry = huge_pte_wrprotect(mk_huge_pte(page, 4952106c992aSGerald Schaefer vma->vm_page_prot)); 495363551ae0SDavid Gibson } 495463551ae0SDavid Gibson entry = pte_mkyoung(entry); 495579c1c594SChristophe Leroy entry = arch_make_huge_pte(entry, shift, vma->vm_flags); 495663551ae0SDavid Gibson 495763551ae0SDavid Gibson return entry; 495863551ae0SDavid Gibson } 495963551ae0SDavid Gibson 49601e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma, 49611e8f889bSDavid Gibson unsigned long address, pte_t *ptep) 49621e8f889bSDavid Gibson { 49631e8f889bSDavid Gibson pte_t entry; 49641e8f889bSDavid Gibson 4965106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); 496632f84528SChris Forbes if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) 49674b3073e1SRussell King update_mmu_cache(vma, address, ptep); 49681e8f889bSDavid Gibson } 49691e8f889bSDavid Gibson 4970d5ed7444SAneesh Kumar K.V bool is_hugetlb_entry_migration(pte_t pte) 49714a705fefSNaoya Horiguchi { 49724a705fefSNaoya Horiguchi swp_entry_t swp; 49734a705fefSNaoya Horiguchi 49744a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 4975d5ed7444SAneesh Kumar K.V return false; 49764a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 4977d79d176aSBaoquan He if (is_migration_entry(swp)) 4978d5ed7444SAneesh Kumar K.V return true; 49794a705fefSNaoya Horiguchi else 4980d5ed7444SAneesh Kumar K.V return false; 49814a705fefSNaoya Horiguchi } 49824a705fefSNaoya Horiguchi 49833e5c3600SBaoquan He static bool is_hugetlb_entry_hwpoisoned(pte_t pte) 49844a705fefSNaoya Horiguchi { 49854a705fefSNaoya Horiguchi swp_entry_t swp; 49864a705fefSNaoya Horiguchi 49874a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 49883e5c3600SBaoquan He return false; 49894a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 4990d79d176aSBaoquan He if (is_hwpoison_entry(swp)) 49913e5c3600SBaoquan He return true; 49924a705fefSNaoya Horiguchi else 49933e5c3600SBaoquan He return false; 49944a705fefSNaoya Horiguchi } 49951e8f889bSDavid Gibson 49964eae4efaSPeter Xu static void 4997ea4c353dSSidhartha Kumar hugetlb_install_folio(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr, 49985a2f8d22SPeter Xu struct folio *new_folio, pte_t old) 49994eae4efaSPeter Xu { 50005a2f8d22SPeter Xu pte_t newpte = make_huge_pte(vma, &new_folio->page, 1); 50015a2f8d22SPeter Xu 5002ea4c353dSSidhartha Kumar __folio_mark_uptodate(new_folio); 5003d0ce0e47SSidhartha Kumar hugepage_add_new_anon_rmap(new_folio, vma, addr); 50045a2f8d22SPeter Xu if (userfaultfd_wp(vma) && huge_pte_uffd_wp(old)) 50055a2f8d22SPeter Xu newpte = huge_pte_mkuffd_wp(newpte); 50065a2f8d22SPeter Xu set_huge_pte_at(vma->vm_mm, addr, ptep, newpte); 50074eae4efaSPeter Xu hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm); 5008ea4c353dSSidhartha Kumar folio_set_hugetlb_migratable(new_folio); 50094eae4efaSPeter Xu } 50104eae4efaSPeter Xu 501163551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 5012bc70fbf2SPeter Xu struct vm_area_struct *dst_vma, 5013bc70fbf2SPeter Xu struct vm_area_struct *src_vma) 501463551ae0SDavid Gibson { 50153aa4ed80SMiaohe Lin pte_t *src_pte, *dst_pte, entry; 5016ad27ce20SZhangPeng struct folio *pte_folio; 50171c59827dSHugh Dickins unsigned long addr; 5018bc70fbf2SPeter Xu bool cow = is_cow_mapping(src_vma->vm_flags); 5019bc70fbf2SPeter Xu struct hstate *h = hstate_vma(src_vma); 5020a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 50214eae4efaSPeter Xu unsigned long npages = pages_per_huge_page(h); 5022ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 5023e95a9851SMike Kravetz unsigned long last_addr_mask; 5024e8569dd2SAndreas Sandberg int ret = 0; 50251e8f889bSDavid Gibson 5026ac46d4f3SJérôme Glisse if (cow) { 50277d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, src, 5028bc70fbf2SPeter Xu src_vma->vm_start, 5029bc70fbf2SPeter Xu src_vma->vm_end); 5030ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 5031623a1ddfSDavid Hildenbrand mmap_assert_write_locked(src); 5032623a1ddfSDavid Hildenbrand raw_write_seqcount_begin(&src->write_protect_seq); 503340549ba8SMike Kravetz } else { 503440549ba8SMike Kravetz /* 503540549ba8SMike Kravetz * For shared mappings the vma lock must be held before 50369c67a207SPeter Xu * calling hugetlb_walk() in the src vma. Otherwise, the 503740549ba8SMike Kravetz * returned ptep could go away if part of a shared pmd and 503840549ba8SMike Kravetz * another thread calls huge_pmd_unshare. 503940549ba8SMike Kravetz */ 504040549ba8SMike Kravetz hugetlb_vma_lock_read(src_vma); 5041ac46d4f3SJérôme Glisse } 5042e8569dd2SAndreas Sandberg 5043e95a9851SMike Kravetz last_addr_mask = hugetlb_mask_last_page(h); 5044bc70fbf2SPeter Xu for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) { 5045cb900f41SKirill A. Shutemov spinlock_t *src_ptl, *dst_ptl; 50469c67a207SPeter Xu src_pte = hugetlb_walk(src_vma, addr, sz); 5047e95a9851SMike Kravetz if (!src_pte) { 5048e95a9851SMike Kravetz addr |= last_addr_mask; 5049c74df32cSHugh Dickins continue; 5050e95a9851SMike Kravetz } 5051bc70fbf2SPeter Xu dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz); 5052e8569dd2SAndreas Sandberg if (!dst_pte) { 5053e8569dd2SAndreas Sandberg ret = -ENOMEM; 5054e8569dd2SAndreas Sandberg break; 5055e8569dd2SAndreas Sandberg } 5056c5c99429SLarry Woodman 50575e41540cSMike Kravetz /* 50585e41540cSMike Kravetz * If the pagetables are shared don't copy or take references. 50595e41540cSMike Kravetz * 50603aa4ed80SMiaohe Lin * dst_pte == src_pte is the common case of src/dest sharing. 50615e41540cSMike Kravetz * However, src could have 'unshared' and dst shares with 50623aa4ed80SMiaohe Lin * another vma. So page_count of ptep page is checked instead 50633aa4ed80SMiaohe Lin * to reliably determine whether pte is shared. 50645e41540cSMike Kravetz */ 50653aa4ed80SMiaohe Lin if (page_count(virt_to_page(dst_pte)) > 1) { 5066e95a9851SMike Kravetz addr |= last_addr_mask; 5067c5c99429SLarry Woodman continue; 5068e95a9851SMike Kravetz } 5069c5c99429SLarry Woodman 5070cb900f41SKirill A. Shutemov dst_ptl = huge_pte_lock(h, dst, dst_pte); 5071cb900f41SKirill A. Shutemov src_ptl = huge_pte_lockptr(h, src, src_pte); 5072cb900f41SKirill A. Shutemov spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 50734a705fefSNaoya Horiguchi entry = huge_ptep_get(src_pte); 50744eae4efaSPeter Xu again: 50753aa4ed80SMiaohe Lin if (huge_pte_none(entry)) { 50765e41540cSMike Kravetz /* 50773aa4ed80SMiaohe Lin * Skip if src entry none. 50785e41540cSMike Kravetz */ 50794a705fefSNaoya Horiguchi ; 5080c2cb0dccSNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) { 50815a2f8d22SPeter Xu if (!userfaultfd_wp(dst_vma)) 5082c2cb0dccSNaoya Horiguchi entry = huge_pte_clear_uffd_wp(entry); 5083c2cb0dccSNaoya Horiguchi set_huge_pte_at(dst, addr, dst_pte, entry); 5084c2cb0dccSNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_migration(entry))) { 50854a705fefSNaoya Horiguchi swp_entry_t swp_entry = pte_to_swp_entry(entry); 50865a2f8d22SPeter Xu bool uffd_wp = pte_swp_uffd_wp(entry); 50874a705fefSNaoya Horiguchi 50886c287605SDavid Hildenbrand if (!is_readable_migration_entry(swp_entry) && cow) { 50894a705fefSNaoya Horiguchi /* 50904a705fefSNaoya Horiguchi * COW mappings require pages in both 50914a705fefSNaoya Horiguchi * parent and child to be set to read. 50924a705fefSNaoya Horiguchi */ 50934dd845b5SAlistair Popple swp_entry = make_readable_migration_entry( 50944dd845b5SAlistair Popple swp_offset(swp_entry)); 50954a705fefSNaoya Horiguchi entry = swp_entry_to_pte(swp_entry); 5096bc70fbf2SPeter Xu if (userfaultfd_wp(src_vma) && uffd_wp) 50975a2f8d22SPeter Xu entry = pte_swp_mkuffd_wp(entry); 509818f39629SQi Zheng set_huge_pte_at(src, addr, src_pte, entry); 50994a705fefSNaoya Horiguchi } 51005a2f8d22SPeter Xu if (!userfaultfd_wp(dst_vma)) 5101bc70fbf2SPeter Xu entry = huge_pte_clear_uffd_wp(entry); 510218f39629SQi Zheng set_huge_pte_at(dst, addr, dst_pte, entry); 5103bc70fbf2SPeter Xu } else if (unlikely(is_pte_marker(entry))) { 51047e3ce3f8SPeter Xu /* No swap on hugetlb */ 51057e3ce3f8SPeter Xu WARN_ON_ONCE( 51067e3ce3f8SPeter Xu is_swapin_error_entry(pte_to_swp_entry(entry))); 5107bc70fbf2SPeter Xu /* 5108bc70fbf2SPeter Xu * We copy the pte marker only if the dst vma has 5109bc70fbf2SPeter Xu * uffd-wp enabled. 5110bc70fbf2SPeter Xu */ 5111bc70fbf2SPeter Xu if (userfaultfd_wp(dst_vma)) 5112bc70fbf2SPeter Xu set_huge_pte_at(dst, addr, dst_pte, entry); 51134a705fefSNaoya Horiguchi } else { 51144eae4efaSPeter Xu entry = huge_ptep_get(src_pte); 5115ad27ce20SZhangPeng pte_folio = page_folio(pte_page(entry)); 5116ad27ce20SZhangPeng folio_get(pte_folio); 51174eae4efaSPeter Xu 51184eae4efaSPeter Xu /* 5119fb3d824dSDavid Hildenbrand * Failing to duplicate the anon rmap is a rare case 5120fb3d824dSDavid Hildenbrand * where we see pinned hugetlb pages while they're 5121fb3d824dSDavid Hildenbrand * prone to COW. We need to do the COW earlier during 5122fb3d824dSDavid Hildenbrand * fork. 51234eae4efaSPeter Xu * 51244eae4efaSPeter Xu * When pre-allocating the page or copying data, we 51254eae4efaSPeter Xu * need to be without the pgtable locks since we could 51264eae4efaSPeter Xu * sleep during the process. 51274eae4efaSPeter Xu */ 5128ad27ce20SZhangPeng if (!folio_test_anon(pte_folio)) { 5129ad27ce20SZhangPeng page_dup_file_rmap(&pte_folio->page, true); 5130ad27ce20SZhangPeng } else if (page_try_dup_anon_rmap(&pte_folio->page, 5131ad27ce20SZhangPeng true, src_vma)) { 51324eae4efaSPeter Xu pte_t src_pte_old = entry; 5133d0ce0e47SSidhartha Kumar struct folio *new_folio; 51344eae4efaSPeter Xu 51354eae4efaSPeter Xu spin_unlock(src_ptl); 51364eae4efaSPeter Xu spin_unlock(dst_ptl); 51374eae4efaSPeter Xu /* Do not use reserve as it's private owned */ 5138d0ce0e47SSidhartha Kumar new_folio = alloc_hugetlb_folio(dst_vma, addr, 1); 5139d0ce0e47SSidhartha Kumar if (IS_ERR(new_folio)) { 5140ad27ce20SZhangPeng folio_put(pte_folio); 5141d0ce0e47SSidhartha Kumar ret = PTR_ERR(new_folio); 51424eae4efaSPeter Xu break; 51434eae4efaSPeter Xu } 51441cb9dc4bSLiu Shixin ret = copy_user_large_folio(new_folio, 5145ad27ce20SZhangPeng pte_folio, 5146c0e8150eSZhangPeng addr, dst_vma); 5147ad27ce20SZhangPeng folio_put(pte_folio); 51481cb9dc4bSLiu Shixin if (ret) { 51491cb9dc4bSLiu Shixin folio_put(new_folio); 51501cb9dc4bSLiu Shixin break; 51511cb9dc4bSLiu Shixin } 51524eae4efaSPeter Xu 5153d0ce0e47SSidhartha Kumar /* Install the new hugetlb folio if src pte stable */ 51544eae4efaSPeter Xu dst_ptl = huge_pte_lock(h, dst, dst_pte); 51554eae4efaSPeter Xu src_ptl = huge_pte_lockptr(h, src, src_pte); 51564eae4efaSPeter Xu spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 51574eae4efaSPeter Xu entry = huge_ptep_get(src_pte); 51584eae4efaSPeter Xu if (!pte_same(src_pte_old, entry)) { 5159bc70fbf2SPeter Xu restore_reserve_on_error(h, dst_vma, addr, 5160d2d7bb44SSidhartha Kumar new_folio); 5161d0ce0e47SSidhartha Kumar folio_put(new_folio); 51623aa4ed80SMiaohe Lin /* huge_ptep of dst_pte won't change as in child */ 51634eae4efaSPeter Xu goto again; 51644eae4efaSPeter Xu } 51655a2f8d22SPeter Xu hugetlb_install_folio(dst_vma, dst_pte, addr, 51665a2f8d22SPeter Xu new_folio, src_pte_old); 51674eae4efaSPeter Xu spin_unlock(src_ptl); 51684eae4efaSPeter Xu spin_unlock(dst_ptl); 51694eae4efaSPeter Xu continue; 51704eae4efaSPeter Xu } 51714eae4efaSPeter Xu 517234ee645eSJoerg Roedel if (cow) { 51730f10851eSJérôme Glisse /* 51740f10851eSJérôme Glisse * No need to notify as we are downgrading page 51750f10851eSJérôme Glisse * table protection not changing it to point 51760f10851eSJérôme Glisse * to a new page. 51770f10851eSJérôme Glisse * 5178ee65728eSMike Rapoport * See Documentation/mm/mmu_notifier.rst 51790f10851eSJérôme Glisse */ 51807f2e9525SGerald Schaefer huge_ptep_set_wrprotect(src, addr, src_pte); 518184894e1cSPeter Xu entry = huge_pte_wrprotect(entry); 518234ee645eSJoerg Roedel } 51834eae4efaSPeter Xu 51845a2f8d22SPeter Xu if (!userfaultfd_wp(dst_vma)) 51855a2f8d22SPeter Xu entry = huge_pte_clear_uffd_wp(entry); 51865a2f8d22SPeter Xu 518763551ae0SDavid Gibson set_huge_pte_at(dst, addr, dst_pte, entry); 51884eae4efaSPeter Xu hugetlb_count_add(npages, dst); 51891c59827dSHugh Dickins } 5190cb900f41SKirill A. Shutemov spin_unlock(src_ptl); 5191cb900f41SKirill A. Shutemov spin_unlock(dst_ptl); 519263551ae0SDavid Gibson } 519363551ae0SDavid Gibson 5194623a1ddfSDavid Hildenbrand if (cow) { 5195623a1ddfSDavid Hildenbrand raw_write_seqcount_end(&src->write_protect_seq); 5196ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 519740549ba8SMike Kravetz } else { 519840549ba8SMike Kravetz hugetlb_vma_unlock_read(src_vma); 5199623a1ddfSDavid Hildenbrand } 5200e8569dd2SAndreas Sandberg 5201e8569dd2SAndreas Sandberg return ret; 520263551ae0SDavid Gibson } 520363551ae0SDavid Gibson 5204550a7d60SMina Almasry static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr, 5205db110a99SAneesh Kumar K.V unsigned long new_addr, pte_t *src_pte, pte_t *dst_pte) 5206550a7d60SMina Almasry { 5207550a7d60SMina Almasry struct hstate *h = hstate_vma(vma); 5208550a7d60SMina Almasry struct mm_struct *mm = vma->vm_mm; 5209550a7d60SMina Almasry spinlock_t *src_ptl, *dst_ptl; 5210db110a99SAneesh Kumar K.V pte_t pte; 5211550a7d60SMina Almasry 5212550a7d60SMina Almasry dst_ptl = huge_pte_lock(h, mm, dst_pte); 5213550a7d60SMina Almasry src_ptl = huge_pte_lockptr(h, mm, src_pte); 5214550a7d60SMina Almasry 5215550a7d60SMina Almasry /* 5216550a7d60SMina Almasry * We don't have to worry about the ordering of src and dst ptlocks 52178651a137SLorenzo Stoakes * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock. 5218550a7d60SMina Almasry */ 5219550a7d60SMina Almasry if (src_ptl != dst_ptl) 5220550a7d60SMina Almasry spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 5221550a7d60SMina Almasry 5222550a7d60SMina Almasry pte = huge_ptep_get_and_clear(mm, old_addr, src_pte); 5223550a7d60SMina Almasry set_huge_pte_at(mm, new_addr, dst_pte, pte); 5224550a7d60SMina Almasry 5225550a7d60SMina Almasry if (src_ptl != dst_ptl) 5226550a7d60SMina Almasry spin_unlock(src_ptl); 5227550a7d60SMina Almasry spin_unlock(dst_ptl); 5228550a7d60SMina Almasry } 5229550a7d60SMina Almasry 5230550a7d60SMina Almasry int move_hugetlb_page_tables(struct vm_area_struct *vma, 5231550a7d60SMina Almasry struct vm_area_struct *new_vma, 5232550a7d60SMina Almasry unsigned long old_addr, unsigned long new_addr, 5233550a7d60SMina Almasry unsigned long len) 5234550a7d60SMina Almasry { 5235550a7d60SMina Almasry struct hstate *h = hstate_vma(vma); 5236550a7d60SMina Almasry struct address_space *mapping = vma->vm_file->f_mapping; 5237550a7d60SMina Almasry unsigned long sz = huge_page_size(h); 5238550a7d60SMina Almasry struct mm_struct *mm = vma->vm_mm; 5239550a7d60SMina Almasry unsigned long old_end = old_addr + len; 5240e95a9851SMike Kravetz unsigned long last_addr_mask; 5241550a7d60SMina Almasry pte_t *src_pte, *dst_pte; 5242550a7d60SMina Almasry struct mmu_notifier_range range; 52433d0b95cdSBaolin Wang bool shared_pmd = false; 5244550a7d60SMina Almasry 52457d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, old_addr, 5246550a7d60SMina Almasry old_end); 5247550a7d60SMina Almasry adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); 52483d0b95cdSBaolin Wang /* 52493d0b95cdSBaolin Wang * In case of shared PMDs, we should cover the maximum possible 52503d0b95cdSBaolin Wang * range. 52513d0b95cdSBaolin Wang */ 52523d0b95cdSBaolin Wang flush_cache_range(vma, range.start, range.end); 52533d0b95cdSBaolin Wang 5254550a7d60SMina Almasry mmu_notifier_invalidate_range_start(&range); 5255e95a9851SMike Kravetz last_addr_mask = hugetlb_mask_last_page(h); 5256550a7d60SMina Almasry /* Prevent race with file truncation */ 525740549ba8SMike Kravetz hugetlb_vma_lock_write(vma); 5258550a7d60SMina Almasry i_mmap_lock_write(mapping); 5259550a7d60SMina Almasry for (; old_addr < old_end; old_addr += sz, new_addr += sz) { 52609c67a207SPeter Xu src_pte = hugetlb_walk(vma, old_addr, sz); 5261e95a9851SMike Kravetz if (!src_pte) { 5262e95a9851SMike Kravetz old_addr |= last_addr_mask; 5263e95a9851SMike Kravetz new_addr |= last_addr_mask; 5264550a7d60SMina Almasry continue; 5265e95a9851SMike Kravetz } 5266550a7d60SMina Almasry if (huge_pte_none(huge_ptep_get(src_pte))) 5267550a7d60SMina Almasry continue; 5268550a7d60SMina Almasry 52694ddb4d91SMike Kravetz if (huge_pmd_unshare(mm, vma, old_addr, src_pte)) { 52703d0b95cdSBaolin Wang shared_pmd = true; 52714ddb4d91SMike Kravetz old_addr |= last_addr_mask; 52724ddb4d91SMike Kravetz new_addr |= last_addr_mask; 5273550a7d60SMina Almasry continue; 52743d0b95cdSBaolin Wang } 5275550a7d60SMina Almasry 5276550a7d60SMina Almasry dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz); 5277550a7d60SMina Almasry if (!dst_pte) 5278550a7d60SMina Almasry break; 5279550a7d60SMina Almasry 5280db110a99SAneesh Kumar K.V move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte); 5281550a7d60SMina Almasry } 52823d0b95cdSBaolin Wang 52833d0b95cdSBaolin Wang if (shared_pmd) 52843d0b95cdSBaolin Wang flush_tlb_range(vma, range.start, range.end); 52853d0b95cdSBaolin Wang else 5286550a7d60SMina Almasry flush_tlb_range(vma, old_end - len, old_end); 5287550a7d60SMina Almasry mmu_notifier_invalidate_range_end(&range); 528813e4ad2cSNadav Amit i_mmap_unlock_write(mapping); 528940549ba8SMike Kravetz hugetlb_vma_unlock_write(vma); 5290550a7d60SMina Almasry 5291550a7d60SMina Almasry return len + old_addr - old_end; 5292550a7d60SMina Almasry } 5293550a7d60SMina Almasry 529473c54763SPeter Xu static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, 529524669e58SAneesh Kumar K.V unsigned long start, unsigned long end, 529605e90bd0SPeter Xu struct page *ref_page, zap_flags_t zap_flags) 529763551ae0SDavid Gibson { 529863551ae0SDavid Gibson struct mm_struct *mm = vma->vm_mm; 529963551ae0SDavid Gibson unsigned long address; 5300c7546f8fSDavid Gibson pte_t *ptep; 530163551ae0SDavid Gibson pte_t pte; 5302cb900f41SKirill A. Shutemov spinlock_t *ptl; 530363551ae0SDavid Gibson struct page *page; 5304a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 5305a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 5306e95a9851SMike Kravetz unsigned long last_addr_mask; 5307a4a118f2SNadav Amit bool force_flush = false; 5308a5516438SAndi Kleen 530963551ae0SDavid Gibson WARN_ON(!is_vm_hugetlb_page(vma)); 5310a5516438SAndi Kleen BUG_ON(start & ~huge_page_mask(h)); 5311a5516438SAndi Kleen BUG_ON(end & ~huge_page_mask(h)); 531263551ae0SDavid Gibson 531307e32661SAneesh Kumar K.V /* 531407e32661SAneesh Kumar K.V * This is a hugetlb vma, all the pte entries should point 531507e32661SAneesh Kumar K.V * to huge page. 531607e32661SAneesh Kumar K.V */ 5317ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, sz); 531824669e58SAneesh Kumar K.V tlb_start_vma(tlb, vma); 5319dff11abeSMike Kravetz 5320e95a9851SMike Kravetz last_addr_mask = hugetlb_mask_last_page(h); 5321569f48b8SHillf Danton address = start; 5322569f48b8SHillf Danton for (; address < end; address += sz) { 53239c67a207SPeter Xu ptep = hugetlb_walk(vma, address, sz); 5324e95a9851SMike Kravetz if (!ptep) { 5325e95a9851SMike Kravetz address |= last_addr_mask; 5326c7546f8fSDavid Gibson continue; 5327e95a9851SMike Kravetz } 5328c7546f8fSDavid Gibson 5329cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 53304ddb4d91SMike Kravetz if (huge_pmd_unshare(mm, vma, address, ptep)) { 533131d49da5SAneesh Kumar K.V spin_unlock(ptl); 5332a4a118f2SNadav Amit tlb_flush_pmd_range(tlb, address & PUD_MASK, PUD_SIZE); 5333a4a118f2SNadav Amit force_flush = true; 53344ddb4d91SMike Kravetz address |= last_addr_mask; 533531d49da5SAneesh Kumar K.V continue; 533631d49da5SAneesh Kumar K.V } 533739dde65cSChen, Kenneth W 53386629326bSHillf Danton pte = huge_ptep_get(ptep); 533931d49da5SAneesh Kumar K.V if (huge_pte_none(pte)) { 534031d49da5SAneesh Kumar K.V spin_unlock(ptl); 534131d49da5SAneesh Kumar K.V continue; 534231d49da5SAneesh Kumar K.V } 53436629326bSHillf Danton 53446629326bSHillf Danton /* 53459fbc1f63SNaoya Horiguchi * Migrating hugepage or HWPoisoned hugepage is already 53469fbc1f63SNaoya Horiguchi * unmapped and its refcount is dropped, so just clear pte here. 53476629326bSHillf Danton */ 53489fbc1f63SNaoya Horiguchi if (unlikely(!pte_present(pte))) { 534905e90bd0SPeter Xu /* 535005e90bd0SPeter Xu * If the pte was wr-protected by uffd-wp in any of the 535105e90bd0SPeter Xu * swap forms, meanwhile the caller does not want to 535205e90bd0SPeter Xu * drop the uffd-wp bit in this zap, then replace the 535305e90bd0SPeter Xu * pte with a marker. 535405e90bd0SPeter Xu */ 535505e90bd0SPeter Xu if (pte_swp_uffd_wp_any(pte) && 535605e90bd0SPeter Xu !(zap_flags & ZAP_FLAG_DROP_MARKER)) 535705e90bd0SPeter Xu set_huge_pte_at(mm, address, ptep, 535805e90bd0SPeter Xu make_pte_marker(PTE_MARKER_UFFD_WP)); 535905e90bd0SPeter Xu else 53609386fac3SPunit Agrawal huge_pte_clear(mm, address, ptep, sz); 536131d49da5SAneesh Kumar K.V spin_unlock(ptl); 536231d49da5SAneesh Kumar K.V continue; 53638c4894c6SNaoya Horiguchi } 53646629326bSHillf Danton 53656629326bSHillf Danton page = pte_page(pte); 536604f2cbe3SMel Gorman /* 536704f2cbe3SMel Gorman * If a reference page is supplied, it is because a specific 536804f2cbe3SMel Gorman * page is being unmapped, not a range. Ensure the page we 536904f2cbe3SMel Gorman * are about to unmap is the actual page of interest. 537004f2cbe3SMel Gorman */ 537104f2cbe3SMel Gorman if (ref_page) { 537231d49da5SAneesh Kumar K.V if (page != ref_page) { 537331d49da5SAneesh Kumar K.V spin_unlock(ptl); 537431d49da5SAneesh Kumar K.V continue; 537531d49da5SAneesh Kumar K.V } 537604f2cbe3SMel Gorman /* 537704f2cbe3SMel Gorman * Mark the VMA as having unmapped its page so that 537804f2cbe3SMel Gorman * future faults in this VMA will fail rather than 537904f2cbe3SMel Gorman * looking like data was lost 538004f2cbe3SMel Gorman */ 538104f2cbe3SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); 538204f2cbe3SMel Gorman } 538304f2cbe3SMel Gorman 5384c7546f8fSDavid Gibson pte = huge_ptep_get_and_clear(mm, address, ptep); 5385b528e4b6SAneesh Kumar K.V tlb_remove_huge_tlb_entry(h, tlb, ptep, address); 5386106c992aSGerald Schaefer if (huge_pte_dirty(pte)) 53876649a386SKen Chen set_page_dirty(page); 538805e90bd0SPeter Xu /* Leave a uffd-wp pte marker if needed */ 538905e90bd0SPeter Xu if (huge_pte_uffd_wp(pte) && 539005e90bd0SPeter Xu !(zap_flags & ZAP_FLAG_DROP_MARKER)) 539105e90bd0SPeter Xu set_huge_pte_at(mm, address, ptep, 539205e90bd0SPeter Xu make_pte_marker(PTE_MARKER_UFFD_WP)); 53935d317b2bSNaoya Horiguchi hugetlb_count_sub(pages_per_huge_page(h), mm); 5394cea86fe2SHugh Dickins page_remove_rmap(page, vma, true); 539531d49da5SAneesh Kumar K.V 5396cb900f41SKirill A. Shutemov spin_unlock(ptl); 5397e77b0852SAneesh Kumar K.V tlb_remove_page_size(tlb, page, huge_page_size(h)); 539824669e58SAneesh Kumar K.V /* 539931d49da5SAneesh Kumar K.V * Bail out after unmapping reference page if supplied 540024669e58SAneesh Kumar K.V */ 540131d49da5SAneesh Kumar K.V if (ref_page) 540231d49da5SAneesh Kumar K.V break; 5403fe1668aeSChen, Kenneth W } 540424669e58SAneesh Kumar K.V tlb_end_vma(tlb, vma); 5405a4a118f2SNadav Amit 5406a4a118f2SNadav Amit /* 5407a4a118f2SNadav Amit * If we unshared PMDs, the TLB flush was not recorded in mmu_gather. We 5408a4a118f2SNadav Amit * could defer the flush until now, since by holding i_mmap_rwsem we 5409a4a118f2SNadav Amit * guaranteed that the last refernece would not be dropped. But we must 5410a4a118f2SNadav Amit * do the flushing before we return, as otherwise i_mmap_rwsem will be 5411a4a118f2SNadav Amit * dropped and the last reference to the shared PMDs page might be 5412a4a118f2SNadav Amit * dropped as well. 5413a4a118f2SNadav Amit * 5414a4a118f2SNadav Amit * In theory we could defer the freeing of the PMD pages as well, but 5415a4a118f2SNadav Amit * huge_pmd_unshare() relies on the exact page_count for the PMD page to 5416a4a118f2SNadav Amit * detect sharing, so we cannot defer the release of the page either. 5417a4a118f2SNadav Amit * Instead, do flush now. 5418a4a118f2SNadav Amit */ 5419a4a118f2SNadav Amit if (force_flush) 5420a4a118f2SNadav Amit tlb_flush_mmu_tlbonly(tlb); 54211da177e4SLinus Torvalds } 542263551ae0SDavid Gibson 5423d833352aSMel Gorman void __unmap_hugepage_range_final(struct mmu_gather *tlb, 5424d833352aSMel Gorman struct vm_area_struct *vma, unsigned long start, 542505e90bd0SPeter Xu unsigned long end, struct page *ref_page, 542605e90bd0SPeter Xu zap_flags_t zap_flags) 5427d833352aSMel Gorman { 5428131a79b4SMike Kravetz hugetlb_vma_lock_write(vma); 5429131a79b4SMike Kravetz i_mmap_lock_write(vma->vm_file->f_mapping); 5430131a79b4SMike Kravetz 5431369258ceSMike Kravetz /* mmu notification performed in caller */ 543205e90bd0SPeter Xu __unmap_hugepage_range(tlb, vma, start, end, ref_page, zap_flags); 5433d833352aSMel Gorman 543404ada095SMike Kravetz if (zap_flags & ZAP_FLAG_UNMAP) { /* final unmap */ 5435d833352aSMel Gorman /* 543604ada095SMike Kravetz * Unlock and free the vma lock before releasing i_mmap_rwsem. 543704ada095SMike Kravetz * When the vma_lock is freed, this makes the vma ineligible 543804ada095SMike Kravetz * for pmd sharing. And, i_mmap_rwsem is required to set up 543904ada095SMike Kravetz * pmd sharing. This is important as page tables for this 544004ada095SMike Kravetz * unmapped range will be asynchrously deleted. If the page 544104ada095SMike Kravetz * tables are shared, there will be issues when accessed by 544204ada095SMike Kravetz * someone else. 5443d833352aSMel Gorman */ 5444ecfbd733SMike Kravetz __hugetlb_vma_unlock_write_free(vma); 5445131a79b4SMike Kravetz i_mmap_unlock_write(vma->vm_file->f_mapping); 544604ada095SMike Kravetz } else { 544704ada095SMike Kravetz i_mmap_unlock_write(vma->vm_file->f_mapping); 544804ada095SMike Kravetz hugetlb_vma_unlock_write(vma); 544904ada095SMike Kravetz } 5450d833352aSMel Gorman } 5451d833352aSMel Gorman 5452502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 545305e90bd0SPeter Xu unsigned long end, struct page *ref_page, 545405e90bd0SPeter Xu zap_flags_t zap_flags) 5455502717f4SChen, Kenneth W { 5456369258ceSMike Kravetz struct mmu_notifier_range range; 545724669e58SAneesh Kumar K.V struct mmu_gather tlb; 5458dff11abeSMike Kravetz 54597d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, 5460369258ceSMike Kravetz start, end); 5461369258ceSMike Kravetz adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); 5462369258ceSMike Kravetz mmu_notifier_invalidate_range_start(&range); 5463a72afd87SWill Deacon tlb_gather_mmu(&tlb, vma->vm_mm); 5464369258ceSMike Kravetz 546505e90bd0SPeter Xu __unmap_hugepage_range(&tlb, vma, start, end, ref_page, zap_flags); 5466369258ceSMike Kravetz 5467369258ceSMike Kravetz mmu_notifier_invalidate_range_end(&range); 5468ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 5469502717f4SChen, Kenneth W } 5470502717f4SChen, Kenneth W 547104f2cbe3SMel Gorman /* 547204f2cbe3SMel Gorman * This is called when the original mapper is failing to COW a MAP_PRIVATE 5473578b7725SZhiyuan Dai * mapping it owns the reserve page for. The intention is to unmap the page 547404f2cbe3SMel Gorman * from other VMAs and let the children be SIGKILLed if they are faulting the 547504f2cbe3SMel Gorman * same region. 547604f2cbe3SMel Gorman */ 54772f4612afSDavidlohr Bueso static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, 54782a4b3dedSHarvey Harrison struct page *page, unsigned long address) 547904f2cbe3SMel Gorman { 54807526674dSAdam Litke struct hstate *h = hstate_vma(vma); 548104f2cbe3SMel Gorman struct vm_area_struct *iter_vma; 548204f2cbe3SMel Gorman struct address_space *mapping; 548304f2cbe3SMel Gorman pgoff_t pgoff; 548404f2cbe3SMel Gorman 548504f2cbe3SMel Gorman /* 548604f2cbe3SMel Gorman * vm_pgoff is in PAGE_SIZE units, hence the different calculation 548704f2cbe3SMel Gorman * from page cache lookup which is in HPAGE_SIZE units. 548804f2cbe3SMel Gorman */ 54897526674dSAdam Litke address = address & huge_page_mask(h); 549036e4f20aSMichal Hocko pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + 549136e4f20aSMichal Hocko vma->vm_pgoff; 549293c76a3dSAl Viro mapping = vma->vm_file->f_mapping; 549304f2cbe3SMel Gorman 54944eb2b1dcSMel Gorman /* 54954eb2b1dcSMel Gorman * Take the mapping lock for the duration of the table walk. As 54964eb2b1dcSMel Gorman * this mapping should be shared between all the VMAs, 54974eb2b1dcSMel Gorman * __unmap_hugepage_range() is called as the lock is already held 54984eb2b1dcSMel Gorman */ 549983cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 55006b2dbba8SMichel Lespinasse vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { 550104f2cbe3SMel Gorman /* Do not unmap the current VMA */ 550204f2cbe3SMel Gorman if (iter_vma == vma) 550304f2cbe3SMel Gorman continue; 550404f2cbe3SMel Gorman 550504f2cbe3SMel Gorman /* 55062f84a899SMel Gorman * Shared VMAs have their own reserves and do not affect 55072f84a899SMel Gorman * MAP_PRIVATE accounting but it is possible that a shared 55082f84a899SMel Gorman * VMA is using the same page so check and skip such VMAs. 55092f84a899SMel Gorman */ 55102f84a899SMel Gorman if (iter_vma->vm_flags & VM_MAYSHARE) 55112f84a899SMel Gorman continue; 55122f84a899SMel Gorman 55132f84a899SMel Gorman /* 551404f2cbe3SMel Gorman * Unmap the page from other VMAs without their own reserves. 551504f2cbe3SMel Gorman * They get marked to be SIGKILLed if they fault in these 551604f2cbe3SMel Gorman * areas. This is because a future no-page fault on this VMA 551704f2cbe3SMel Gorman * could insert a zeroed page instead of the data existing 551804f2cbe3SMel Gorman * from the time of fork. This would look like data corruption 551904f2cbe3SMel Gorman */ 552004f2cbe3SMel Gorman if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) 552124669e58SAneesh Kumar K.V unmap_hugepage_range(iter_vma, address, 552205e90bd0SPeter Xu address + huge_page_size(h), page, 0); 552304f2cbe3SMel Gorman } 552483cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 552504f2cbe3SMel Gorman } 552604f2cbe3SMel Gorman 55270fe6e20bSNaoya Horiguchi /* 5528c89357e2SDavid Hildenbrand * hugetlb_wp() should be called with page lock of the original hugepage held. 5529aa6d2e8cSBaolin Wang * Called with hugetlb_fault_mutex_table held and pte_page locked so we 5530ef009b25SMichal Hocko * cannot race with other handlers or page migration. 5531ef009b25SMichal Hocko * Keep the pte_same checks anyway to make transition from the mutex easier. 55320fe6e20bSNaoya Horiguchi */ 5533c89357e2SDavid Hildenbrand static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, 5534c89357e2SDavid Hildenbrand unsigned long address, pte_t *ptep, unsigned int flags, 5535371607a3SSidhartha Kumar struct folio *pagecache_folio, spinlock_t *ptl) 55361e8f889bSDavid Gibson { 5537c89357e2SDavid Hildenbrand const bool unshare = flags & FAULT_FLAG_UNSHARE; 553860d5b473SPeter Xu pte_t pte = huge_ptep_get(ptep); 5539a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 5540959a78b6SZhangPeng struct folio *old_folio; 5541d0ce0e47SSidhartha Kumar struct folio *new_folio; 55422b740303SSouptick Joarder int outside_reserve = 0; 55432b740303SSouptick Joarder vm_fault_t ret = 0; 5544974e6d66SHuang Ying unsigned long haddr = address & huge_page_mask(h); 5545ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 55461e8f889bSDavid Gibson 55471d8d1464SDavid Hildenbrand /* 554860d5b473SPeter Xu * Never handle CoW for uffd-wp protected pages. It should be only 554960d5b473SPeter Xu * handled when the uffd-wp protection is removed. 555060d5b473SPeter Xu * 555160d5b473SPeter Xu * Note that only the CoW optimization path (in hugetlb_no_page()) 555260d5b473SPeter Xu * can trigger this, because hugetlb_fault() will always resolve 555360d5b473SPeter Xu * uffd-wp bit first. 555460d5b473SPeter Xu */ 555560d5b473SPeter Xu if (!unshare && huge_pte_uffd_wp(pte)) 555660d5b473SPeter Xu return 0; 555760d5b473SPeter Xu 555860d5b473SPeter Xu /* 55591d8d1464SDavid Hildenbrand * hugetlb does not support FOLL_FORCE-style write faults that keep the 55601d8d1464SDavid Hildenbrand * PTE mapped R/O such as maybe_mkwrite() would do. 55611d8d1464SDavid Hildenbrand */ 55621d8d1464SDavid Hildenbrand if (WARN_ON_ONCE(!unshare && !(vma->vm_flags & VM_WRITE))) 55631d8d1464SDavid Hildenbrand return VM_FAULT_SIGSEGV; 55641d8d1464SDavid Hildenbrand 55651d8d1464SDavid Hildenbrand /* Let's take out MAP_SHARED mappings first. */ 55661d8d1464SDavid Hildenbrand if (vma->vm_flags & VM_MAYSHARE) { 55671d8d1464SDavid Hildenbrand set_huge_ptep_writable(vma, haddr, ptep); 55681d8d1464SDavid Hildenbrand return 0; 55691d8d1464SDavid Hildenbrand } 55701d8d1464SDavid Hildenbrand 5571959a78b6SZhangPeng old_folio = page_folio(pte_page(pte)); 55721e8f889bSDavid Gibson 5573662ce1dcSYang Yang delayacct_wpcopy_start(); 5574662ce1dcSYang Yang 557504f2cbe3SMel Gorman retry_avoidcopy: 5576c89357e2SDavid Hildenbrand /* 5577c89357e2SDavid Hildenbrand * If no-one else is actually using this page, we're the exclusive 5578c89357e2SDavid Hildenbrand * owner and can reuse this page. 5579c89357e2SDavid Hildenbrand */ 5580959a78b6SZhangPeng if (folio_mapcount(old_folio) == 1 && folio_test_anon(old_folio)) { 5581959a78b6SZhangPeng if (!PageAnonExclusive(&old_folio->page)) 5582959a78b6SZhangPeng page_move_anon_rmap(&old_folio->page, vma); 5583c89357e2SDavid Hildenbrand if (likely(!unshare)) 55845b7a1d40SHuang Ying set_huge_ptep_writable(vma, haddr, ptep); 5585662ce1dcSYang Yang 5586662ce1dcSYang Yang delayacct_wpcopy_end(); 558783c54070SNick Piggin return 0; 55881e8f889bSDavid Gibson } 5589959a78b6SZhangPeng VM_BUG_ON_PAGE(folio_test_anon(old_folio) && 5590959a78b6SZhangPeng PageAnonExclusive(&old_folio->page), &old_folio->page); 55911e8f889bSDavid Gibson 559204f2cbe3SMel Gorman /* 559304f2cbe3SMel Gorman * If the process that created a MAP_PRIVATE mapping is about to 559404f2cbe3SMel Gorman * perform a COW due to a shared page count, attempt to satisfy 559504f2cbe3SMel Gorman * the allocation without using the existing reserves. The pagecache 559604f2cbe3SMel Gorman * page is used to determine if the reserve at this address was 559704f2cbe3SMel Gorman * consumed or not. If reserves were used, a partial faulted mapping 559804f2cbe3SMel Gorman * at the time of fork() could consume its reserves on COW instead 559904f2cbe3SMel Gorman * of the full address range. 560004f2cbe3SMel Gorman */ 56015944d011SJoonsoo Kim if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && 5602959a78b6SZhangPeng old_folio != pagecache_folio) 560304f2cbe3SMel Gorman outside_reserve = 1; 560404f2cbe3SMel Gorman 5605959a78b6SZhangPeng folio_get(old_folio); 5606b76c8cfbSLarry Woodman 5607ad4404a2SDavidlohr Bueso /* 5608ad4404a2SDavidlohr Bueso * Drop page table lock as buddy allocator may be called. It will 5609ad4404a2SDavidlohr Bueso * be acquired again before returning to the caller, as expected. 5610ad4404a2SDavidlohr Bueso */ 5611cb900f41SKirill A. Shutemov spin_unlock(ptl); 5612d0ce0e47SSidhartha Kumar new_folio = alloc_hugetlb_folio(vma, haddr, outside_reserve); 56131e8f889bSDavid Gibson 5614d0ce0e47SSidhartha Kumar if (IS_ERR(new_folio)) { 561504f2cbe3SMel Gorman /* 561604f2cbe3SMel Gorman * If a process owning a MAP_PRIVATE mapping fails to COW, 561704f2cbe3SMel Gorman * it is due to references held by a child and an insufficient 561804f2cbe3SMel Gorman * huge page pool. To guarantee the original mappers 561904f2cbe3SMel Gorman * reliability, unmap the page from child processes. The child 562004f2cbe3SMel Gorman * may get SIGKILLed if it later faults. 562104f2cbe3SMel Gorman */ 562204f2cbe3SMel Gorman if (outside_reserve) { 562340549ba8SMike Kravetz struct address_space *mapping = vma->vm_file->f_mapping; 562440549ba8SMike Kravetz pgoff_t idx; 562540549ba8SMike Kravetz u32 hash; 562640549ba8SMike Kravetz 5627959a78b6SZhangPeng folio_put(old_folio); 562840549ba8SMike Kravetz /* 562940549ba8SMike Kravetz * Drop hugetlb_fault_mutex and vma_lock before 563040549ba8SMike Kravetz * unmapping. unmapping needs to hold vma_lock 563140549ba8SMike Kravetz * in write mode. Dropping vma_lock in read mode 563240549ba8SMike Kravetz * here is OK as COW mappings do not interact with 563340549ba8SMike Kravetz * PMD sharing. 563440549ba8SMike Kravetz * 563540549ba8SMike Kravetz * Reacquire both after unmap operation. 563640549ba8SMike Kravetz */ 563740549ba8SMike Kravetz idx = vma_hugecache_offset(h, vma, haddr); 563840549ba8SMike Kravetz hash = hugetlb_fault_mutex_hash(mapping, idx); 563940549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 564040549ba8SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 564140549ba8SMike Kravetz 5642959a78b6SZhangPeng unmap_ref_private(mm, vma, &old_folio->page, haddr); 564340549ba8SMike Kravetz 564440549ba8SMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 564540549ba8SMike Kravetz hugetlb_vma_lock_read(vma); 5646cb900f41SKirill A. Shutemov spin_lock(ptl); 56479c67a207SPeter Xu ptep = hugetlb_walk(vma, haddr, huge_page_size(h)); 5648a9af0c5dSNaoya Horiguchi if (likely(ptep && 5649a9af0c5dSNaoya Horiguchi pte_same(huge_ptep_get(ptep), pte))) 565004f2cbe3SMel Gorman goto retry_avoidcopy; 5651a734bcc8SHillf Danton /* 5652cb900f41SKirill A. Shutemov * race occurs while re-acquiring page table 5653cb900f41SKirill A. Shutemov * lock, and our job is done. 5654a734bcc8SHillf Danton */ 5655662ce1dcSYang Yang delayacct_wpcopy_end(); 5656a734bcc8SHillf Danton return 0; 565704f2cbe3SMel Gorman } 565804f2cbe3SMel Gorman 5659d0ce0e47SSidhartha Kumar ret = vmf_error(PTR_ERR(new_folio)); 5660ad4404a2SDavidlohr Bueso goto out_release_old; 56611e8f889bSDavid Gibson } 56621e8f889bSDavid Gibson 56630fe6e20bSNaoya Horiguchi /* 56640fe6e20bSNaoya Horiguchi * When the original hugepage is shared one, it does not have 56650fe6e20bSNaoya Horiguchi * anon_vma prepared. 56660fe6e20bSNaoya Horiguchi */ 566744e2aa93SDean Nelson if (unlikely(anon_vma_prepare(vma))) { 5668ad4404a2SDavidlohr Bueso ret = VM_FAULT_OOM; 5669ad4404a2SDavidlohr Bueso goto out_release_all; 567044e2aa93SDean Nelson } 56710fe6e20bSNaoya Horiguchi 5672959a78b6SZhangPeng if (copy_user_large_folio(new_folio, old_folio, address, vma)) { 56731cb9dc4bSLiu Shixin ret = VM_FAULT_HWPOISON_LARGE; 56741cb9dc4bSLiu Shixin goto out_release_all; 56751cb9dc4bSLiu Shixin } 5676d0ce0e47SSidhartha Kumar __folio_mark_uptodate(new_folio); 56771e8f889bSDavid Gibson 56787d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, haddr, 56796f4f13e8SJérôme Glisse haddr + huge_page_size(h)); 5680ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 5681ad4404a2SDavidlohr Bueso 5682b76c8cfbSLarry Woodman /* 5683cb900f41SKirill A. Shutemov * Retake the page table lock to check for racing updates 5684b76c8cfbSLarry Woodman * before the page tables are altered 5685b76c8cfbSLarry Woodman */ 5686cb900f41SKirill A. Shutemov spin_lock(ptl); 56879c67a207SPeter Xu ptep = hugetlb_walk(vma, haddr, huge_page_size(h)); 5688a9af0c5dSNaoya Horiguchi if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { 56890f230bc2SPeter Xu pte_t newpte = make_huge_pte(vma, &new_folio->page, !unshare); 56900f230bc2SPeter Xu 5691c89357e2SDavid Hildenbrand /* Break COW or unshare */ 56925b7a1d40SHuang Ying huge_ptep_clear_flush(vma, haddr, ptep); 5693ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range(mm, range.start, range.end); 5694959a78b6SZhangPeng page_remove_rmap(&old_folio->page, vma, true); 5695d0ce0e47SSidhartha Kumar hugepage_add_new_anon_rmap(new_folio, vma, haddr); 56960f230bc2SPeter Xu if (huge_pte_uffd_wp(pte)) 56970f230bc2SPeter Xu newpte = huge_pte_mkuffd_wp(newpte); 56980f230bc2SPeter Xu set_huge_pte_at(mm, haddr, ptep, newpte); 5699d0ce0e47SSidhartha Kumar folio_set_hugetlb_migratable(new_folio); 57001e8f889bSDavid Gibson /* Make the old page be freed below */ 5701959a78b6SZhangPeng new_folio = old_folio; 57021e8f889bSDavid Gibson } 5703cb900f41SKirill A. Shutemov spin_unlock(ptl); 5704ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 5705ad4404a2SDavidlohr Bueso out_release_all: 5706c89357e2SDavid Hildenbrand /* 5707c89357e2SDavid Hildenbrand * No restore in case of successful pagetable update (Break COW or 5708c89357e2SDavid Hildenbrand * unshare) 5709c89357e2SDavid Hildenbrand */ 5710959a78b6SZhangPeng if (new_folio != old_folio) 5711d2d7bb44SSidhartha Kumar restore_reserve_on_error(h, vma, haddr, new_folio); 5712d0ce0e47SSidhartha Kumar folio_put(new_folio); 5713ad4404a2SDavidlohr Bueso out_release_old: 5714959a78b6SZhangPeng folio_put(old_folio); 57158312034fSJoonsoo Kim 5716ad4404a2SDavidlohr Bueso spin_lock(ptl); /* Caller expects lock to be held */ 5717662ce1dcSYang Yang 5718662ce1dcSYang Yang delayacct_wpcopy_end(); 5719ad4404a2SDavidlohr Bueso return ret; 57201e8f889bSDavid Gibson } 57211e8f889bSDavid Gibson 57223ae77f43SHugh Dickins /* 57233ae77f43SHugh Dickins * Return whether there is a pagecache page to back given address within VMA. 57243ae77f43SHugh Dickins * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page. 57253ae77f43SHugh Dickins */ 57263ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h, 57272a15efc9SHugh Dickins struct vm_area_struct *vma, unsigned long address) 57282a15efc9SHugh Dickins { 572991a2fb95SSidhartha Kumar struct address_space *mapping = vma->vm_file->f_mapping; 573091a2fb95SSidhartha Kumar pgoff_t idx = vma_hugecache_offset(h, vma, address); 5731fd4aed8dSMike Kravetz struct folio *folio; 57322a15efc9SHugh Dickins 5733fd4aed8dSMike Kravetz folio = filemap_get_folio(mapping, idx); 5734fd4aed8dSMike Kravetz if (IS_ERR(folio)) 5735fd4aed8dSMike Kravetz return false; 5736fd4aed8dSMike Kravetz folio_put(folio); 5737fd4aed8dSMike Kravetz return true; 57382a15efc9SHugh Dickins } 57392a15efc9SHugh Dickins 57409b91c0e2SSidhartha Kumar int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping, 5741ab76ad54SMike Kravetz pgoff_t idx) 5742ab76ad54SMike Kravetz { 5743ab76ad54SMike Kravetz struct inode *inode = mapping->host; 5744ab76ad54SMike Kravetz struct hstate *h = hstate_inode(inode); 5745d9ef44deSMatthew Wilcox (Oracle) int err; 5746ab76ad54SMike Kravetz 5747d9ef44deSMatthew Wilcox (Oracle) __folio_set_locked(folio); 5748d9ef44deSMatthew Wilcox (Oracle) err = __filemap_add_folio(mapping, folio, idx, GFP_KERNEL, NULL); 5749d9ef44deSMatthew Wilcox (Oracle) 5750d9ef44deSMatthew Wilcox (Oracle) if (unlikely(err)) { 5751d9ef44deSMatthew Wilcox (Oracle) __folio_clear_locked(folio); 5752ab76ad54SMike Kravetz return err; 5753d9ef44deSMatthew Wilcox (Oracle) } 57549b91c0e2SSidhartha Kumar folio_clear_hugetlb_restore_reserve(folio); 5755ab76ad54SMike Kravetz 575622146c3cSMike Kravetz /* 5757d9ef44deSMatthew Wilcox (Oracle) * mark folio dirty so that it will not be removed from cache/file 575822146c3cSMike Kravetz * by non-hugetlbfs specific code paths. 575922146c3cSMike Kravetz */ 5760d9ef44deSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 576122146c3cSMike Kravetz 5762ab76ad54SMike Kravetz spin_lock(&inode->i_lock); 5763ab76ad54SMike Kravetz inode->i_blocks += blocks_per_huge_page(h); 5764ab76ad54SMike Kravetz spin_unlock(&inode->i_lock); 5765ab76ad54SMike Kravetz return 0; 5766ab76ad54SMike Kravetz } 5767ab76ad54SMike Kravetz 57687677f7fdSAxel Rasmussen static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma, 57697677f7fdSAxel Rasmussen struct address_space *mapping, 57707677f7fdSAxel Rasmussen pgoff_t idx, 57717677f7fdSAxel Rasmussen unsigned int flags, 57727677f7fdSAxel Rasmussen unsigned long haddr, 5773824ddc60SNadav Amit unsigned long addr, 57747677f7fdSAxel Rasmussen unsigned long reason) 57757677f7fdSAxel Rasmussen { 57767677f7fdSAxel Rasmussen u32 hash; 57777677f7fdSAxel Rasmussen struct vm_fault vmf = { 57787677f7fdSAxel Rasmussen .vma = vma, 57797677f7fdSAxel Rasmussen .address = haddr, 5780824ddc60SNadav Amit .real_address = addr, 57817677f7fdSAxel Rasmussen .flags = flags, 57827677f7fdSAxel Rasmussen 57837677f7fdSAxel Rasmussen /* 57847677f7fdSAxel Rasmussen * Hard to debug if it ends up being 57857677f7fdSAxel Rasmussen * used by a callee that assumes 57867677f7fdSAxel Rasmussen * something about the other 57877677f7fdSAxel Rasmussen * uninitialized fields... same as in 57887677f7fdSAxel Rasmussen * memory.c 57897677f7fdSAxel Rasmussen */ 57907677f7fdSAxel Rasmussen }; 57917677f7fdSAxel Rasmussen 57927677f7fdSAxel Rasmussen /* 5793958f32ceSLiu Shixin * vma_lock and hugetlb_fault_mutex must be dropped before handling 5794958f32ceSLiu Shixin * userfault. Also mmap_lock could be dropped due to handling 5795958f32ceSLiu Shixin * userfault, any vma operation should be careful from here. 57967677f7fdSAxel Rasmussen */ 579740549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 57987677f7fdSAxel Rasmussen hash = hugetlb_fault_mutex_hash(mapping, idx); 57997677f7fdSAxel Rasmussen mutex_unlock(&hugetlb_fault_mutex_table[hash]); 5800958f32ceSLiu Shixin return handle_userfault(&vmf, reason); 58017677f7fdSAxel Rasmussen } 58027677f7fdSAxel Rasmussen 58032ea7ff1eSPeter Xu /* 58042ea7ff1eSPeter Xu * Recheck pte with pgtable lock. Returns true if pte didn't change, or 58052ea7ff1eSPeter Xu * false if pte changed or is changing. 58062ea7ff1eSPeter Xu */ 58072ea7ff1eSPeter Xu static bool hugetlb_pte_stable(struct hstate *h, struct mm_struct *mm, 58082ea7ff1eSPeter Xu pte_t *ptep, pte_t old_pte) 58092ea7ff1eSPeter Xu { 58102ea7ff1eSPeter Xu spinlock_t *ptl; 58112ea7ff1eSPeter Xu bool same; 58122ea7ff1eSPeter Xu 58132ea7ff1eSPeter Xu ptl = huge_pte_lock(h, mm, ptep); 58142ea7ff1eSPeter Xu same = pte_same(huge_ptep_get(ptep), old_pte); 58152ea7ff1eSPeter Xu spin_unlock(ptl); 58162ea7ff1eSPeter Xu 58172ea7ff1eSPeter Xu return same; 58182ea7ff1eSPeter Xu } 58192ea7ff1eSPeter Xu 58202b740303SSouptick Joarder static vm_fault_t hugetlb_no_page(struct mm_struct *mm, 58212b740303SSouptick Joarder struct vm_area_struct *vma, 58228382d914SDavidlohr Bueso struct address_space *mapping, pgoff_t idx, 5823c64e912cSPeter Xu unsigned long address, pte_t *ptep, 5824c64e912cSPeter Xu pte_t old_pte, unsigned int flags) 5825ac9b9c66SHugh Dickins { 5826a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 58272b740303SSouptick Joarder vm_fault_t ret = VM_FAULT_SIGBUS; 5828409eb8c2SHillf Danton int anon_rmap = 0; 58294c887265SAdam Litke unsigned long size; 5830d0ce0e47SSidhartha Kumar struct folio *folio; 58311e8f889bSDavid Gibson pte_t new_pte; 5832cb900f41SKirill A. Shutemov spinlock_t *ptl; 5833285b8dcaSHuang Ying unsigned long haddr = address & huge_page_mask(h); 5834d0ce0e47SSidhartha Kumar bool new_folio, new_pagecache_folio = false; 5835958f32ceSLiu Shixin u32 hash = hugetlb_fault_mutex_hash(mapping, idx); 58364c887265SAdam Litke 583704f2cbe3SMel Gorman /* 583804f2cbe3SMel Gorman * Currently, we are forced to kill the process in the event the 583904f2cbe3SMel Gorman * original mapper has unmapped pages from the child due to a failed 5840c89357e2SDavid Hildenbrand * COW/unsharing. Warn that such a situation has occurred as it may not 5841c89357e2SDavid Hildenbrand * be obvious. 584204f2cbe3SMel Gorman */ 584304f2cbe3SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { 5844910154d5SGeoffrey Thomas pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n", 584504f2cbe3SMel Gorman current->pid); 5846958f32ceSLiu Shixin goto out; 584704f2cbe3SMel Gorman } 584804f2cbe3SMel Gorman 58494c887265SAdam Litke /* 5850188a3972SMike Kravetz * Use page lock to guard against racing truncation 5851188a3972SMike Kravetz * before we get page_table_lock. 58524c887265SAdam Litke */ 5853d0ce0e47SSidhartha Kumar new_folio = false; 5854d0ce0e47SSidhartha Kumar folio = filemap_lock_folio(mapping, idx); 585566dabbb6SChristoph Hellwig if (IS_ERR(folio)) { 5856188a3972SMike Kravetz size = i_size_read(mapping->host) >> huge_page_shift(h); 5857188a3972SMike Kravetz if (idx >= size) 5858188a3972SMike Kravetz goto out; 58597677f7fdSAxel Rasmussen /* Check for page in userfault range */ 58602ea7ff1eSPeter Xu if (userfaultfd_missing(vma)) { 58612ea7ff1eSPeter Xu /* 58622ea7ff1eSPeter Xu * Since hugetlb_no_page() was examining pte 58632ea7ff1eSPeter Xu * without pgtable lock, we need to re-test under 58642ea7ff1eSPeter Xu * lock because the pte may not be stable and could 58652ea7ff1eSPeter Xu * have changed from under us. Try to detect 58662ea7ff1eSPeter Xu * either changed or during-changing ptes and retry 58672ea7ff1eSPeter Xu * properly when needed. 58682ea7ff1eSPeter Xu * 58692ea7ff1eSPeter Xu * Note that userfaultfd is actually fine with 58702ea7ff1eSPeter Xu * false positives (e.g. caused by pte changed), 58712ea7ff1eSPeter Xu * but not wrong logical events (e.g. caused by 58722ea7ff1eSPeter Xu * reading a pte during changing). The latter can 58732ea7ff1eSPeter Xu * confuse the userspace, so the strictness is very 58742ea7ff1eSPeter Xu * much preferred. E.g., MISSING event should 58752ea7ff1eSPeter Xu * never happen on the page after UFFDIO_COPY has 58762ea7ff1eSPeter Xu * correctly installed the page and returned. 58772ea7ff1eSPeter Xu */ 58782ea7ff1eSPeter Xu if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) { 58792ea7ff1eSPeter Xu ret = 0; 58802ea7ff1eSPeter Xu goto out; 58812ea7ff1eSPeter Xu } 58822ea7ff1eSPeter Xu 58832ea7ff1eSPeter Xu return hugetlb_handle_userfault(vma, mapping, idx, flags, 58842ea7ff1eSPeter Xu haddr, address, 58857677f7fdSAxel Rasmussen VM_UFFD_MISSING); 58862ea7ff1eSPeter Xu } 58871a1aad8aSMike Kravetz 5888d0ce0e47SSidhartha Kumar folio = alloc_hugetlb_folio(vma, haddr, 0); 5889d0ce0e47SSidhartha Kumar if (IS_ERR(folio)) { 58904643d67eSMike Kravetz /* 58914643d67eSMike Kravetz * Returning error will result in faulting task being 58924643d67eSMike Kravetz * sent SIGBUS. The hugetlb fault mutex prevents two 58934643d67eSMike Kravetz * tasks from racing to fault in the same page which 58944643d67eSMike Kravetz * could result in false unable to allocate errors. 58954643d67eSMike Kravetz * Page migration does not take the fault mutex, but 58964643d67eSMike Kravetz * does a clear then write of pte's under page table 58974643d67eSMike Kravetz * lock. Page fault code could race with migration, 58984643d67eSMike Kravetz * notice the clear pte and try to allocate a page 58994643d67eSMike Kravetz * here. Before returning error, get ptl and make 59004643d67eSMike Kravetz * sure there really is no pte entry. 59014643d67eSMike Kravetz */ 5902f9bf6c03SPeter Xu if (hugetlb_pte_stable(h, mm, ptep, old_pte)) 5903d0ce0e47SSidhartha Kumar ret = vmf_error(PTR_ERR(folio)); 5904f9bf6c03SPeter Xu else 5905f9bf6c03SPeter Xu ret = 0; 59066bda666aSChristoph Lameter goto out; 59076bda666aSChristoph Lameter } 5908d0ce0e47SSidhartha Kumar clear_huge_page(&folio->page, address, pages_per_huge_page(h)); 5909d0ce0e47SSidhartha Kumar __folio_mark_uptodate(folio); 5910d0ce0e47SSidhartha Kumar new_folio = true; 5911ac9b9c66SHugh Dickins 5912f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) { 59139b91c0e2SSidhartha Kumar int err = hugetlb_add_to_page_cache(folio, mapping, idx); 59146bda666aSChristoph Lameter if (err) { 59153a5497a2SMiaohe Lin /* 59163a5497a2SMiaohe Lin * err can't be -EEXIST which implies someone 59173a5497a2SMiaohe Lin * else consumed the reservation since hugetlb 59183a5497a2SMiaohe Lin * fault mutex is held when add a hugetlb page 59193a5497a2SMiaohe Lin * to the page cache. So it's safe to call 59203a5497a2SMiaohe Lin * restore_reserve_on_error() here. 59213a5497a2SMiaohe Lin */ 5922d2d7bb44SSidhartha Kumar restore_reserve_on_error(h, vma, haddr, folio); 5923d0ce0e47SSidhartha Kumar folio_put(folio); 59246bda666aSChristoph Lameter goto out; 59256bda666aSChristoph Lameter } 5926d0ce0e47SSidhartha Kumar new_pagecache_folio = true; 592723be7468SMel Gorman } else { 5928d0ce0e47SSidhartha Kumar folio_lock(folio); 59290fe6e20bSNaoya Horiguchi if (unlikely(anon_vma_prepare(vma))) { 59300fe6e20bSNaoya Horiguchi ret = VM_FAULT_OOM; 59310fe6e20bSNaoya Horiguchi goto backout_unlocked; 593223be7468SMel Gorman } 5933409eb8c2SHillf Danton anon_rmap = 1; 59340fe6e20bSNaoya Horiguchi } 59350fe6e20bSNaoya Horiguchi } else { 593657303d80SAndy Whitcroft /* 5937998b4382SNaoya Horiguchi * If memory error occurs between mmap() and fault, some process 5938998b4382SNaoya Horiguchi * don't have hwpoisoned swap entry for errored virtual address. 5939998b4382SNaoya Horiguchi * So we need to block hugepage fault by PG_hwpoison bit check. 5940fd6a03edSNaoya Horiguchi */ 5941d0ce0e47SSidhartha Kumar if (unlikely(folio_test_hwpoison(folio))) { 59420eb98f15SMiaohe Lin ret = VM_FAULT_HWPOISON_LARGE | 5943972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 5944fd6a03edSNaoya Horiguchi goto backout_unlocked; 59456bda666aSChristoph Lameter } 59467677f7fdSAxel Rasmussen 59477677f7fdSAxel Rasmussen /* Check for page in userfault range. */ 59487677f7fdSAxel Rasmussen if (userfaultfd_minor(vma)) { 5949d0ce0e47SSidhartha Kumar folio_unlock(folio); 5950d0ce0e47SSidhartha Kumar folio_put(folio); 59512ea7ff1eSPeter Xu /* See comment in userfaultfd_missing() block above */ 59522ea7ff1eSPeter Xu if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) { 59532ea7ff1eSPeter Xu ret = 0; 59542ea7ff1eSPeter Xu goto out; 59552ea7ff1eSPeter Xu } 59562ea7ff1eSPeter Xu return hugetlb_handle_userfault(vma, mapping, idx, flags, 59572ea7ff1eSPeter Xu haddr, address, 59587677f7fdSAxel Rasmussen VM_UFFD_MINOR); 59597677f7fdSAxel Rasmussen } 5960998b4382SNaoya Horiguchi } 59611e8f889bSDavid Gibson 596257303d80SAndy Whitcroft /* 596357303d80SAndy Whitcroft * If we are going to COW a private mapping later, we examine the 596457303d80SAndy Whitcroft * pending reservations for this page now. This will ensure that 596557303d80SAndy Whitcroft * any allocations necessary to record that reservation occur outside 596657303d80SAndy Whitcroft * the spinlock. 596757303d80SAndy Whitcroft */ 59685e911373SMike Kravetz if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 5969285b8dcaSHuang Ying if (vma_needs_reservation(h, vma, haddr) < 0) { 59702b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 59712b26736cSAndy Whitcroft goto backout_unlocked; 59722b26736cSAndy Whitcroft } 59735e911373SMike Kravetz /* Just decrements count, does not deallocate */ 5974285b8dcaSHuang Ying vma_end_reservation(h, vma, haddr); 59755e911373SMike Kravetz } 597657303d80SAndy Whitcroft 59778bea8052SAneesh Kumar K.V ptl = huge_pte_lock(h, mm, ptep); 597883c54070SNick Piggin ret = 0; 5979c64e912cSPeter Xu /* If pte changed from under us, retry */ 5980c64e912cSPeter Xu if (!pte_same(huge_ptep_get(ptep), old_pte)) 59814c887265SAdam Litke goto backout; 59824c887265SAdam Litke 59834781593dSPeter Xu if (anon_rmap) 5984d0ce0e47SSidhartha Kumar hugepage_add_new_anon_rmap(folio, vma, haddr); 59854781593dSPeter Xu else 5986d0ce0e47SSidhartha Kumar page_dup_file_rmap(&folio->page, true); 5987d0ce0e47SSidhartha Kumar new_pte = make_huge_pte(vma, &folio->page, ((vma->vm_flags & VM_WRITE) 59881e8f889bSDavid Gibson && (vma->vm_flags & VM_SHARED))); 5989c64e912cSPeter Xu /* 5990c64e912cSPeter Xu * If this pte was previously wr-protected, keep it wr-protected even 5991c64e912cSPeter Xu * if populated. 5992c64e912cSPeter Xu */ 5993c64e912cSPeter Xu if (unlikely(pte_marker_uffd_wp(old_pte))) 5994f1eb1bacSPeter Xu new_pte = huge_pte_mkuffd_wp(new_pte); 5995285b8dcaSHuang Ying set_huge_pte_at(mm, haddr, ptep, new_pte); 59961e8f889bSDavid Gibson 59975d317b2bSNaoya Horiguchi hugetlb_count_add(pages_per_huge_page(h), mm); 5998788c7df4SHugh Dickins if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 59991e8f889bSDavid Gibson /* Optimization, do the COW without a second fault */ 6000371607a3SSidhartha Kumar ret = hugetlb_wp(mm, vma, address, ptep, flags, folio, ptl); 60011e8f889bSDavid Gibson } 60021e8f889bSDavid Gibson 6003cb900f41SKirill A. Shutemov spin_unlock(ptl); 6004cb6acd01SMike Kravetz 6005cb6acd01SMike Kravetz /* 6006d0ce0e47SSidhartha Kumar * Only set hugetlb_migratable in newly allocated pages. Existing pages 6007d0ce0e47SSidhartha Kumar * found in the pagecache may not have hugetlb_migratable if they have 60088f251a3dSMike Kravetz * been isolated for migration. 6009cb6acd01SMike Kravetz */ 6010d0ce0e47SSidhartha Kumar if (new_folio) 6011d0ce0e47SSidhartha Kumar folio_set_hugetlb_migratable(folio); 6012cb6acd01SMike Kravetz 6013d0ce0e47SSidhartha Kumar folio_unlock(folio); 60144c887265SAdam Litke out: 6015958f32ceSLiu Shixin hugetlb_vma_unlock_read(vma); 6016958f32ceSLiu Shixin mutex_unlock(&hugetlb_fault_mutex_table[hash]); 6017ac9b9c66SHugh Dickins return ret; 60184c887265SAdam Litke 60194c887265SAdam Litke backout: 6020cb900f41SKirill A. Shutemov spin_unlock(ptl); 60212b26736cSAndy Whitcroft backout_unlocked: 6022d0ce0e47SSidhartha Kumar if (new_folio && !new_pagecache_folio) 6023d2d7bb44SSidhartha Kumar restore_reserve_on_error(h, vma, haddr, folio); 6024fa27759aSMike Kravetz 6025d0ce0e47SSidhartha Kumar folio_unlock(folio); 6026d0ce0e47SSidhartha Kumar folio_put(folio); 60274c887265SAdam Litke goto out; 6028ac9b9c66SHugh Dickins } 6029ac9b9c66SHugh Dickins 60308382d914SDavidlohr Bueso #ifdef CONFIG_SMP 6031188b04a7SWei Yang u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx) 60328382d914SDavidlohr Bueso { 60338382d914SDavidlohr Bueso unsigned long key[2]; 60348382d914SDavidlohr Bueso u32 hash; 60358382d914SDavidlohr Bueso 60368382d914SDavidlohr Bueso key[0] = (unsigned long) mapping; 60378382d914SDavidlohr Bueso key[1] = idx; 60388382d914SDavidlohr Bueso 603955254636SMike Kravetz hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0); 60408382d914SDavidlohr Bueso 60418382d914SDavidlohr Bueso return hash & (num_fault_mutexes - 1); 60428382d914SDavidlohr Bueso } 60438382d914SDavidlohr Bueso #else 60448382d914SDavidlohr Bueso /* 60456c26d310SMiaohe Lin * For uniprocessor systems we always use a single mutex, so just 60468382d914SDavidlohr Bueso * return 0 and avoid the hashing overhead. 60478382d914SDavidlohr Bueso */ 6048188b04a7SWei Yang u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx) 60498382d914SDavidlohr Bueso { 60508382d914SDavidlohr Bueso return 0; 60518382d914SDavidlohr Bueso } 60528382d914SDavidlohr Bueso #endif 60538382d914SDavidlohr Bueso 60542b740303SSouptick Joarder vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 6055788c7df4SHugh Dickins unsigned long address, unsigned int flags) 605686e5216fSAdam Litke { 60578382d914SDavidlohr Bueso pte_t *ptep, entry; 6058cb900f41SKirill A. Shutemov spinlock_t *ptl; 60592b740303SSouptick Joarder vm_fault_t ret; 60608382d914SDavidlohr Bueso u32 hash; 60618382d914SDavidlohr Bueso pgoff_t idx; 6062061e62e8SZhangPeng struct folio *folio = NULL; 6063371607a3SSidhartha Kumar struct folio *pagecache_folio = NULL; 6064a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 60658382d914SDavidlohr Bueso struct address_space *mapping; 60660f792cf9SNaoya Horiguchi int need_wait_lock = 0; 6067285b8dcaSHuang Ying unsigned long haddr = address & huge_page_mask(h); 606886e5216fSAdam Litke 60693935baa9SDavid Gibson /* 60703935baa9SDavid Gibson * Serialize hugepage allocation and instantiation, so that we don't 60713935baa9SDavid Gibson * get spurious allocation failures if two CPUs race to instantiate 60723935baa9SDavid Gibson * the same page in the page cache. 60733935baa9SDavid Gibson */ 607440549ba8SMike Kravetz mapping = vma->vm_file->f_mapping; 607540549ba8SMike Kravetz idx = vma_hugecache_offset(h, vma, haddr); 6076188b04a7SWei Yang hash = hugetlb_fault_mutex_hash(mapping, idx); 6077c672c7f2SMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 60788382d914SDavidlohr Bueso 607940549ba8SMike Kravetz /* 608040549ba8SMike Kravetz * Acquire vma lock before calling huge_pte_alloc and hold 608140549ba8SMike Kravetz * until finished with ptep. This prevents huge_pmd_unshare from 608240549ba8SMike Kravetz * being called elsewhere and making the ptep no longer valid. 608340549ba8SMike Kravetz */ 608440549ba8SMike Kravetz hugetlb_vma_lock_read(vma); 608540549ba8SMike Kravetz ptep = huge_pte_alloc(mm, vma, haddr, huge_page_size(h)); 608640549ba8SMike Kravetz if (!ptep) { 608740549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 608840549ba8SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 608940549ba8SMike Kravetz return VM_FAULT_OOM; 609040549ba8SMike Kravetz } 609140549ba8SMike Kravetz 60927f2e9525SGerald Schaefer entry = huge_ptep_get(ptep); 6093c64e912cSPeter Xu /* PTE markers should be handled the same way as none pte */ 6094958f32ceSLiu Shixin if (huge_pte_none_mostly(entry)) 6095958f32ceSLiu Shixin /* 6096958f32ceSLiu Shixin * hugetlb_no_page will drop vma lock and hugetlb fault 6097958f32ceSLiu Shixin * mutex internally, which make us return immediately. 6098958f32ceSLiu Shixin */ 6099958f32ceSLiu Shixin return hugetlb_no_page(mm, vma, mapping, idx, address, ptep, 6100c64e912cSPeter Xu entry, flags); 610186e5216fSAdam Litke 610283c54070SNick Piggin ret = 0; 61031e8f889bSDavid Gibson 610457303d80SAndy Whitcroft /* 61050f792cf9SNaoya Horiguchi * entry could be a migration/hwpoison entry at this point, so this 61060f792cf9SNaoya Horiguchi * check prevents the kernel from going below assuming that we have 61077c8de358SEthon Paul * an active hugepage in pagecache. This goto expects the 2nd page 61087c8de358SEthon Paul * fault, and is_hugetlb_entry_(migration|hwpoisoned) check will 61097c8de358SEthon Paul * properly handle it. 61100f792cf9SNaoya Horiguchi */ 6111fcd48540SPeter Xu if (!pte_present(entry)) { 6112fcd48540SPeter Xu if (unlikely(is_hugetlb_entry_migration(entry))) { 6113fcd48540SPeter Xu /* 6114fcd48540SPeter Xu * Release the hugetlb fault lock now, but retain 6115fcd48540SPeter Xu * the vma lock, because it is needed to guard the 6116fcd48540SPeter Xu * huge_pte_lockptr() later in 6117fcd48540SPeter Xu * migration_entry_wait_huge(). The vma lock will 6118fcd48540SPeter Xu * be released there. 6119fcd48540SPeter Xu */ 6120fcd48540SPeter Xu mutex_unlock(&hugetlb_fault_mutex_table[hash]); 6121fcd48540SPeter Xu migration_entry_wait_huge(vma, ptep); 6122fcd48540SPeter Xu return 0; 6123fcd48540SPeter Xu } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) 6124fcd48540SPeter Xu ret = VM_FAULT_HWPOISON_LARGE | 6125fcd48540SPeter Xu VM_FAULT_SET_HINDEX(hstate_index(h)); 61260f792cf9SNaoya Horiguchi goto out_mutex; 6127fcd48540SPeter Xu } 61280f792cf9SNaoya Horiguchi 61290f792cf9SNaoya Horiguchi /* 6130c89357e2SDavid Hildenbrand * If we are going to COW/unshare the mapping later, we examine the 6131c89357e2SDavid Hildenbrand * pending reservations for this page now. This will ensure that any 613257303d80SAndy Whitcroft * allocations necessary to record that reservation occur outside the 61331d8d1464SDavid Hildenbrand * spinlock. Also lookup the pagecache page now as it is used to 61341d8d1464SDavid Hildenbrand * determine if a reservation has been consumed. 613557303d80SAndy Whitcroft */ 6136c89357e2SDavid Hildenbrand if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && 61371d8d1464SDavid Hildenbrand !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(entry)) { 6138285b8dcaSHuang Ying if (vma_needs_reservation(h, vma, haddr) < 0) { 61392b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 6140b4d1d99fSDavid Gibson goto out_mutex; 61412b26736cSAndy Whitcroft } 61425e911373SMike Kravetz /* Just decrements count, does not deallocate */ 6143285b8dcaSHuang Ying vma_end_reservation(h, vma, haddr); 614457303d80SAndy Whitcroft 6145371607a3SSidhartha Kumar pagecache_folio = filemap_lock_folio(mapping, idx); 614666dabbb6SChristoph Hellwig if (IS_ERR(pagecache_folio)) 614766dabbb6SChristoph Hellwig pagecache_folio = NULL; 614857303d80SAndy Whitcroft } 614957303d80SAndy Whitcroft 61500f792cf9SNaoya Horiguchi ptl = huge_pte_lock(h, mm, ptep); 61510fe6e20bSNaoya Horiguchi 6152c89357e2SDavid Hildenbrand /* Check for a racing update before calling hugetlb_wp() */ 6153b4d1d99fSDavid Gibson if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) 6154cb900f41SKirill A. Shutemov goto out_ptl; 6155b4d1d99fSDavid Gibson 6156166f3eccSPeter Xu /* Handle userfault-wp first, before trying to lock more pages */ 6157166f3eccSPeter Xu if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(ptep)) && 6158166f3eccSPeter Xu (flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { 6159166f3eccSPeter Xu struct vm_fault vmf = { 6160166f3eccSPeter Xu .vma = vma, 6161166f3eccSPeter Xu .address = haddr, 6162166f3eccSPeter Xu .real_address = address, 6163166f3eccSPeter Xu .flags = flags, 6164166f3eccSPeter Xu }; 6165166f3eccSPeter Xu 6166166f3eccSPeter Xu spin_unlock(ptl); 6167371607a3SSidhartha Kumar if (pagecache_folio) { 6168371607a3SSidhartha Kumar folio_unlock(pagecache_folio); 6169371607a3SSidhartha Kumar folio_put(pagecache_folio); 6170166f3eccSPeter Xu } 617140549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 6172166f3eccSPeter Xu mutex_unlock(&hugetlb_fault_mutex_table[hash]); 6173166f3eccSPeter Xu return handle_userfault(&vmf, VM_UFFD_WP); 6174166f3eccSPeter Xu } 6175166f3eccSPeter Xu 61760f792cf9SNaoya Horiguchi /* 6177c89357e2SDavid Hildenbrand * hugetlb_wp() requires page locks of pte_page(entry) and 6178371607a3SSidhartha Kumar * pagecache_folio, so here we need take the former one 6179061e62e8SZhangPeng * when folio != pagecache_folio or !pagecache_folio. 61800f792cf9SNaoya Horiguchi */ 6181061e62e8SZhangPeng folio = page_folio(pte_page(entry)); 6182061e62e8SZhangPeng if (folio != pagecache_folio) 6183061e62e8SZhangPeng if (!folio_trylock(folio)) { 61840f792cf9SNaoya Horiguchi need_wait_lock = 1; 61850f792cf9SNaoya Horiguchi goto out_ptl; 61860f792cf9SNaoya Horiguchi } 61870f792cf9SNaoya Horiguchi 6188061e62e8SZhangPeng folio_get(folio); 6189b4d1d99fSDavid Gibson 6190c89357e2SDavid Hildenbrand if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { 6191106c992aSGerald Schaefer if (!huge_pte_write(entry)) { 6192c89357e2SDavid Hildenbrand ret = hugetlb_wp(mm, vma, address, ptep, flags, 6193371607a3SSidhartha Kumar pagecache_folio, ptl); 61940f792cf9SNaoya Horiguchi goto out_put_page; 6195c89357e2SDavid Hildenbrand } else if (likely(flags & FAULT_FLAG_WRITE)) { 6196106c992aSGerald Schaefer entry = huge_pte_mkdirty(entry); 6197b4d1d99fSDavid Gibson } 6198c89357e2SDavid Hildenbrand } 6199b4d1d99fSDavid Gibson entry = pte_mkyoung(entry); 6200285b8dcaSHuang Ying if (huge_ptep_set_access_flags(vma, haddr, ptep, entry, 6201788c7df4SHugh Dickins flags & FAULT_FLAG_WRITE)) 6202285b8dcaSHuang Ying update_mmu_cache(vma, haddr, ptep); 62030f792cf9SNaoya Horiguchi out_put_page: 6204061e62e8SZhangPeng if (folio != pagecache_folio) 6205061e62e8SZhangPeng folio_unlock(folio); 6206061e62e8SZhangPeng folio_put(folio); 6207cb900f41SKirill A. Shutemov out_ptl: 6208cb900f41SKirill A. Shutemov spin_unlock(ptl); 620957303d80SAndy Whitcroft 6210371607a3SSidhartha Kumar if (pagecache_folio) { 6211371607a3SSidhartha Kumar folio_unlock(pagecache_folio); 6212371607a3SSidhartha Kumar folio_put(pagecache_folio); 621357303d80SAndy Whitcroft } 6214b4d1d99fSDavid Gibson out_mutex: 621540549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 6216c672c7f2SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 62170f792cf9SNaoya Horiguchi /* 62180f792cf9SNaoya Horiguchi * Generally it's safe to hold refcount during waiting page lock. But 62190f792cf9SNaoya Horiguchi * here we just wait to defer the next page fault to avoid busy loop and 62200f792cf9SNaoya Horiguchi * the page is not used after unlocked before returning from the current 62210f792cf9SNaoya Horiguchi * page fault. So we are safe from accessing freed page, even if we wait 62220f792cf9SNaoya Horiguchi * here without taking refcount. 62230f792cf9SNaoya Horiguchi */ 62240f792cf9SNaoya Horiguchi if (need_wait_lock) 6225061e62e8SZhangPeng folio_wait_locked(folio); 62261e8f889bSDavid Gibson return ret; 622786e5216fSAdam Litke } 622886e5216fSAdam Litke 6229714c1891SAxel Rasmussen #ifdef CONFIG_USERFAULTFD 62308fb5debcSMike Kravetz /* 6231a734991cSAxel Rasmussen * Used by userfaultfd UFFDIO_* ioctls. Based on userfaultfd's mfill_atomic_pte 6232a734991cSAxel Rasmussen * with modifications for hugetlb pages. 62338fb5debcSMike Kravetz */ 623461c50040SAxel Rasmussen int hugetlb_mfill_atomic_pte(pte_t *dst_pte, 62358fb5debcSMike Kravetz struct vm_area_struct *dst_vma, 62368fb5debcSMike Kravetz unsigned long dst_addr, 62378fb5debcSMike Kravetz unsigned long src_addr, 6238d9712937SAxel Rasmussen uffd_flags_t flags, 62390169fd51SZhangPeng struct folio **foliop) 62408fb5debcSMike Kravetz { 624161c50040SAxel Rasmussen struct mm_struct *dst_mm = dst_vma->vm_mm; 6242d9712937SAxel Rasmussen bool is_continue = uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE); 6243d9712937SAxel Rasmussen bool wp_enabled = (flags & MFILL_ATOMIC_WP); 62448cc5fcbbSMina Almasry struct hstate *h = hstate_vma(dst_vma); 62458cc5fcbbSMina Almasry struct address_space *mapping = dst_vma->vm_file->f_mapping; 62468cc5fcbbSMina Almasry pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr); 62471e392147SAndrea Arcangeli unsigned long size; 62481c9e8defSMike Kravetz int vm_shared = dst_vma->vm_flags & VM_SHARED; 62498fb5debcSMike Kravetz pte_t _dst_pte; 62508fb5debcSMike Kravetz spinlock_t *ptl; 62518cc5fcbbSMina Almasry int ret = -ENOMEM; 6252d0ce0e47SSidhartha Kumar struct folio *folio; 6253f6191471SAxel Rasmussen int writable; 6254d0ce0e47SSidhartha Kumar bool folio_in_pagecache = false; 62558fb5debcSMike Kravetz 6256f6191471SAxel Rasmussen if (is_continue) { 6257f6191471SAxel Rasmussen ret = -EFAULT; 6258d0ce0e47SSidhartha Kumar folio = filemap_lock_folio(mapping, idx); 625966dabbb6SChristoph Hellwig if (IS_ERR(folio)) 6260f6191471SAxel Rasmussen goto out; 6261d0ce0e47SSidhartha Kumar folio_in_pagecache = true; 62620169fd51SZhangPeng } else if (!*foliop) { 62630169fd51SZhangPeng /* If a folio already exists, then it's UFFDIO_COPY for 6264d84cf06eSMina Almasry * a non-missing case. Return -EEXIST. 6265d84cf06eSMina Almasry */ 6266d84cf06eSMina Almasry if (vm_shared && 6267d84cf06eSMina Almasry hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { 6268d84cf06eSMina Almasry ret = -EEXIST; 62698fb5debcSMike Kravetz goto out; 6270d84cf06eSMina Almasry } 6271d84cf06eSMina Almasry 6272d0ce0e47SSidhartha Kumar folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0); 6273d0ce0e47SSidhartha Kumar if (IS_ERR(folio)) { 6274d84cf06eSMina Almasry ret = -ENOMEM; 6275d84cf06eSMina Almasry goto out; 6276d84cf06eSMina Almasry } 62778fb5debcSMike Kravetz 6278e87340caSZhangPeng ret = copy_folio_from_user(folio, (const void __user *) src_addr, 6279e87340caSZhangPeng false); 62808fb5debcSMike Kravetz 6281c1e8d7c6SMichel Lespinasse /* fallback to copy_from_user outside mmap_lock */ 62828fb5debcSMike Kravetz if (unlikely(ret)) { 62839e368259SAndrea Arcangeli ret = -ENOENT; 6284d0ce0e47SSidhartha Kumar /* Free the allocated folio which may have 62858cc5fcbbSMina Almasry * consumed a reservation. 62868cc5fcbbSMina Almasry */ 6287d2d7bb44SSidhartha Kumar restore_reserve_on_error(h, dst_vma, dst_addr, folio); 6288d0ce0e47SSidhartha Kumar folio_put(folio); 62898cc5fcbbSMina Almasry 6290d0ce0e47SSidhartha Kumar /* Allocate a temporary folio to hold the copied 62918cc5fcbbSMina Almasry * contents. 62928cc5fcbbSMina Almasry */ 6293d0ce0e47SSidhartha Kumar folio = alloc_hugetlb_folio_vma(h, dst_vma, dst_addr); 6294d0ce0e47SSidhartha Kumar if (!folio) { 62958cc5fcbbSMina Almasry ret = -ENOMEM; 62968cc5fcbbSMina Almasry goto out; 62978cc5fcbbSMina Almasry } 62980169fd51SZhangPeng *foliop = folio; 62990169fd51SZhangPeng /* Set the outparam foliop and return to the caller to 63008cc5fcbbSMina Almasry * copy the contents outside the lock. Don't free the 63010169fd51SZhangPeng * folio. 63028cc5fcbbSMina Almasry */ 63038fb5debcSMike Kravetz goto out; 63048fb5debcSMike Kravetz } 63058fb5debcSMike Kravetz } else { 63068cc5fcbbSMina Almasry if (vm_shared && 63078cc5fcbbSMina Almasry hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { 63080169fd51SZhangPeng folio_put(*foliop); 63098cc5fcbbSMina Almasry ret = -EEXIST; 63100169fd51SZhangPeng *foliop = NULL; 63118cc5fcbbSMina Almasry goto out; 63128cc5fcbbSMina Almasry } 63138cc5fcbbSMina Almasry 6314d0ce0e47SSidhartha Kumar folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0); 6315d0ce0e47SSidhartha Kumar if (IS_ERR(folio)) { 63160169fd51SZhangPeng folio_put(*foliop); 63178cc5fcbbSMina Almasry ret = -ENOMEM; 63180169fd51SZhangPeng *foliop = NULL; 63198cc5fcbbSMina Almasry goto out; 63208cc5fcbbSMina Almasry } 63211cb9dc4bSLiu Shixin ret = copy_user_large_folio(folio, *foliop, dst_addr, dst_vma); 63220169fd51SZhangPeng folio_put(*foliop); 63230169fd51SZhangPeng *foliop = NULL; 63241cb9dc4bSLiu Shixin if (ret) { 63251cb9dc4bSLiu Shixin folio_put(folio); 63261cb9dc4bSLiu Shixin goto out; 63271cb9dc4bSLiu Shixin } 63288fb5debcSMike Kravetz } 63298fb5debcSMike Kravetz 63308fb5debcSMike Kravetz /* 6331d0ce0e47SSidhartha Kumar * The memory barrier inside __folio_mark_uptodate makes sure that 63328fb5debcSMike Kravetz * preceding stores to the page contents become visible before 63338fb5debcSMike Kravetz * the set_pte_at() write. 63348fb5debcSMike Kravetz */ 6335d0ce0e47SSidhartha Kumar __folio_mark_uptodate(folio); 63368fb5debcSMike Kravetz 6337f6191471SAxel Rasmussen /* Add shared, newly allocated pages to the page cache. */ 6338f6191471SAxel Rasmussen if (vm_shared && !is_continue) { 63391e392147SAndrea Arcangeli size = i_size_read(mapping->host) >> huge_page_shift(h); 63401e392147SAndrea Arcangeli ret = -EFAULT; 63411e392147SAndrea Arcangeli if (idx >= size) 63421e392147SAndrea Arcangeli goto out_release_nounlock; 63431c9e8defSMike Kravetz 63441e392147SAndrea Arcangeli /* 63451e392147SAndrea Arcangeli * Serialization between remove_inode_hugepages() and 63467e1813d4SMike Kravetz * hugetlb_add_to_page_cache() below happens through the 63471e392147SAndrea Arcangeli * hugetlb_fault_mutex_table that here must be hold by 63481e392147SAndrea Arcangeli * the caller. 63491e392147SAndrea Arcangeli */ 63509b91c0e2SSidhartha Kumar ret = hugetlb_add_to_page_cache(folio, mapping, idx); 63511c9e8defSMike Kravetz if (ret) 63521c9e8defSMike Kravetz goto out_release_nounlock; 6353d0ce0e47SSidhartha Kumar folio_in_pagecache = true; 63541c9e8defSMike Kravetz } 63551c9e8defSMike Kravetz 6356bcc66543SMiaohe Lin ptl = huge_pte_lock(h, dst_mm, dst_pte); 63578fb5debcSMike Kravetz 63588625147cSJames Houghton ret = -EIO; 6359d0ce0e47SSidhartha Kumar if (folio_test_hwpoison(folio)) 63608625147cSJames Houghton goto out_release_unlock; 63618625147cSJames Houghton 63621e392147SAndrea Arcangeli /* 63636041c691SPeter Xu * We allow to overwrite a pte marker: consider when both MISSING|WP 63646041c691SPeter Xu * registered, we firstly wr-protect a none pte which has no page cache 63656041c691SPeter Xu * page backing it, then access the page. 63666041c691SPeter Xu */ 6367fa27759aSMike Kravetz ret = -EEXIST; 63686041c691SPeter Xu if (!huge_pte_none_mostly(huge_ptep_get(dst_pte))) 63698fb5debcSMike Kravetz goto out_release_unlock; 63708fb5debcSMike Kravetz 6371d0ce0e47SSidhartha Kumar if (folio_in_pagecache) 6372d0ce0e47SSidhartha Kumar page_dup_file_rmap(&folio->page, true); 63734781593dSPeter Xu else 6374d0ce0e47SSidhartha Kumar hugepage_add_new_anon_rmap(folio, dst_vma, dst_addr); 63758fb5debcSMike Kravetz 63766041c691SPeter Xu /* 63776041c691SPeter Xu * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY 63786041c691SPeter Xu * with wp flag set, don't set pte write bit. 63796041c691SPeter Xu */ 6380d9712937SAxel Rasmussen if (wp_enabled || (is_continue && !vm_shared)) 6381f6191471SAxel Rasmussen writable = 0; 6382f6191471SAxel Rasmussen else 6383f6191471SAxel Rasmussen writable = dst_vma->vm_flags & VM_WRITE; 6384f6191471SAxel Rasmussen 6385d0ce0e47SSidhartha Kumar _dst_pte = make_huge_pte(dst_vma, &folio->page, writable); 63866041c691SPeter Xu /* 63876041c691SPeter Xu * Always mark UFFDIO_COPY page dirty; note that this may not be 63886041c691SPeter Xu * extremely important for hugetlbfs for now since swapping is not 63896041c691SPeter Xu * supported, but we should still be clear in that this page cannot be 63906041c691SPeter Xu * thrown away at will, even if write bit not set. 63916041c691SPeter Xu */ 63928fb5debcSMike Kravetz _dst_pte = huge_pte_mkdirty(_dst_pte); 63938fb5debcSMike Kravetz _dst_pte = pte_mkyoung(_dst_pte); 63948fb5debcSMike Kravetz 6395d9712937SAxel Rasmussen if (wp_enabled) 63966041c691SPeter Xu _dst_pte = huge_pte_mkuffd_wp(_dst_pte); 63976041c691SPeter Xu 63988fb5debcSMike Kravetz set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 63998fb5debcSMike Kravetz 64008fb5debcSMike Kravetz hugetlb_count_add(pages_per_huge_page(h), dst_mm); 64018fb5debcSMike Kravetz 64028fb5debcSMike Kravetz /* No need to invalidate - it was non-present before */ 64038fb5debcSMike Kravetz update_mmu_cache(dst_vma, dst_addr, dst_pte); 64048fb5debcSMike Kravetz 64058fb5debcSMike Kravetz spin_unlock(ptl); 6406f6191471SAxel Rasmussen if (!is_continue) 6407d0ce0e47SSidhartha Kumar folio_set_hugetlb_migratable(folio); 6408f6191471SAxel Rasmussen if (vm_shared || is_continue) 6409d0ce0e47SSidhartha Kumar folio_unlock(folio); 64108fb5debcSMike Kravetz ret = 0; 64118fb5debcSMike Kravetz out: 64128fb5debcSMike Kravetz return ret; 64138fb5debcSMike Kravetz out_release_unlock: 64148fb5debcSMike Kravetz spin_unlock(ptl); 6415f6191471SAxel Rasmussen if (vm_shared || is_continue) 6416d0ce0e47SSidhartha Kumar folio_unlock(folio); 64175af10dfdSAndrea Arcangeli out_release_nounlock: 6418d0ce0e47SSidhartha Kumar if (!folio_in_pagecache) 6419d2d7bb44SSidhartha Kumar restore_reserve_on_error(h, dst_vma, dst_addr, folio); 6420d0ce0e47SSidhartha Kumar folio_put(folio); 64218fb5debcSMike Kravetz goto out; 64228fb5debcSMike Kravetz } 6423714c1891SAxel Rasmussen #endif /* CONFIG_USERFAULTFD */ 64248fb5debcSMike Kravetz 6425b2cac248SLorenzo Stoakes static void record_subpages(struct page *page, struct vm_area_struct *vma, 6426b2cac248SLorenzo Stoakes int refs, struct page **pages) 642782e5d378SJoao Martins { 642882e5d378SJoao Martins int nr; 642982e5d378SJoao Martins 643082e5d378SJoao Martins for (nr = 0; nr < refs; nr++) { 643182e5d378SJoao Martins if (likely(pages)) 643214455eabSCheng Li pages[nr] = nth_page(page, nr); 643382e5d378SJoao Martins } 643482e5d378SJoao Martins } 643582e5d378SJoao Martins 643684209e87SDavid Hildenbrand static inline bool __follow_hugetlb_must_fault(struct vm_area_struct *vma, 643784209e87SDavid Hildenbrand unsigned int flags, pte_t *pte, 6438a7f22660SDavid Hildenbrand bool *unshare) 6439a7f22660SDavid Hildenbrand { 6440a7f22660SDavid Hildenbrand pte_t pteval = huge_ptep_get(pte); 6441a7f22660SDavid Hildenbrand 6442a7f22660SDavid Hildenbrand *unshare = false; 6443a7f22660SDavid Hildenbrand if (is_swap_pte(pteval)) 6444a7f22660SDavid Hildenbrand return true; 6445a7f22660SDavid Hildenbrand if (huge_pte_write(pteval)) 6446a7f22660SDavid Hildenbrand return false; 6447a7f22660SDavid Hildenbrand if (flags & FOLL_WRITE) 6448a7f22660SDavid Hildenbrand return true; 644984209e87SDavid Hildenbrand if (gup_must_unshare(vma, flags, pte_page(pteval))) { 6450a7f22660SDavid Hildenbrand *unshare = true; 6451a7f22660SDavid Hildenbrand return true; 6452a7f22660SDavid Hildenbrand } 6453a7f22660SDavid Hildenbrand return false; 6454a7f22660SDavid Hildenbrand } 6455a7f22660SDavid Hildenbrand 645657a196a5SMike Kravetz struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma, 645757a196a5SMike Kravetz unsigned long address, unsigned int flags) 645857a196a5SMike Kravetz { 645957a196a5SMike Kravetz struct hstate *h = hstate_vma(vma); 646057a196a5SMike Kravetz struct mm_struct *mm = vma->vm_mm; 646157a196a5SMike Kravetz unsigned long haddr = address & huge_page_mask(h); 646257a196a5SMike Kravetz struct page *page = NULL; 646357a196a5SMike Kravetz spinlock_t *ptl; 646457a196a5SMike Kravetz pte_t *pte, entry; 6465*458568c9SPeter Xu int ret; 646657a196a5SMike Kravetz 64677d049f3aSPeter Xu hugetlb_vma_lock_read(vma); 64689c67a207SPeter Xu pte = hugetlb_walk(vma, haddr, huge_page_size(h)); 646957a196a5SMike Kravetz if (!pte) 64707d049f3aSPeter Xu goto out_unlock; 647157a196a5SMike Kravetz 647257a196a5SMike Kravetz ptl = huge_pte_lock(h, mm, pte); 647357a196a5SMike Kravetz entry = huge_ptep_get(pte); 647457a196a5SMike Kravetz if (pte_present(entry)) { 6475*458568c9SPeter Xu page = pte_page(entry); 6476*458568c9SPeter Xu 6477*458568c9SPeter Xu if (!huge_pte_write(entry)) { 6478*458568c9SPeter Xu if (flags & FOLL_WRITE) { 6479*458568c9SPeter Xu page = NULL; 6480*458568c9SPeter Xu goto out; 6481*458568c9SPeter Xu } 6482*458568c9SPeter Xu 6483*458568c9SPeter Xu if (gup_must_unshare(vma, flags, page)) { 6484*458568c9SPeter Xu /* Tell the caller to do unsharing */ 6485*458568c9SPeter Xu page = ERR_PTR(-EMLINK); 6486*458568c9SPeter Xu goto out; 6487*458568c9SPeter Xu } 6488*458568c9SPeter Xu } 6489*458568c9SPeter Xu 6490*458568c9SPeter Xu page += ((address & ~huge_page_mask(h)) >> PAGE_SHIFT); 6491*458568c9SPeter Xu 649257a196a5SMike Kravetz /* 649357a196a5SMike Kravetz * Note that page may be a sub-page, and with vmemmap 649457a196a5SMike Kravetz * optimizations the page struct may be read only. 649557a196a5SMike Kravetz * try_grab_page() will increase the ref count on the 649657a196a5SMike Kravetz * head page, so this will be OK. 649757a196a5SMike Kravetz * 6498e2ca6ba6SLinus Torvalds * try_grab_page() should always be able to get the page here, 6499e2ca6ba6SLinus Torvalds * because we hold the ptl lock and have verified pte_present(). 650057a196a5SMike Kravetz */ 6501*458568c9SPeter Xu ret = try_grab_page(page, flags); 6502*458568c9SPeter Xu 6503*458568c9SPeter Xu if (WARN_ON_ONCE(ret)) { 6504*458568c9SPeter Xu page = ERR_PTR(ret); 650557a196a5SMike Kravetz goto out; 650657a196a5SMike Kravetz } 650757a196a5SMike Kravetz } 650857a196a5SMike Kravetz out: 650957a196a5SMike Kravetz spin_unlock(ptl); 65107d049f3aSPeter Xu out_unlock: 65117d049f3aSPeter Xu hugetlb_vma_unlock_read(vma); 6512dd767aaaSPeter Xu 6513dd767aaaSPeter Xu /* 6514dd767aaaSPeter Xu * Fixup retval for dump requests: if pagecache doesn't exist, 6515dd767aaaSPeter Xu * don't try to allocate a new page but just skip it. 6516dd767aaaSPeter Xu */ 6517dd767aaaSPeter Xu if (!page && (flags & FOLL_DUMP) && 6518dd767aaaSPeter Xu !hugetlbfs_pagecache_present(h, vma, address)) 6519dd767aaaSPeter Xu page = ERR_PTR(-EFAULT); 6520dd767aaaSPeter Xu 652157a196a5SMike Kravetz return page; 652257a196a5SMike Kravetz } 652357a196a5SMike Kravetz 652428a35716SMichel Lespinasse long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, 6525b2cac248SLorenzo Stoakes struct page **pages, unsigned long *position, 6526b2cac248SLorenzo Stoakes unsigned long *nr_pages, long i, unsigned int flags, 6527b2cac248SLorenzo Stoakes int *locked) 652863551ae0SDavid Gibson { 6529d5d4b0aaSChen, Kenneth W unsigned long pfn_offset; 6530d5d4b0aaSChen, Kenneth W unsigned long vaddr = *position; 653128a35716SMichel Lespinasse unsigned long remainder = *nr_pages; 6532a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 65330fa5bc40SJoao Martins int err = -EFAULT, refs; 653463551ae0SDavid Gibson 653563551ae0SDavid Gibson while (vaddr < vma->vm_end && remainder) { 653663551ae0SDavid Gibson pte_t *pte; 6537cb900f41SKirill A. Shutemov spinlock_t *ptl = NULL; 6538a7f22660SDavid Hildenbrand bool unshare = false; 65392a15efc9SHugh Dickins int absent; 654063551ae0SDavid Gibson struct page *page; 654163551ae0SDavid Gibson 65424c887265SAdam Litke /* 654302057967SDavid Rientjes * If we have a pending SIGKILL, don't keep faulting pages and 654402057967SDavid Rientjes * potentially allocating memory. 654502057967SDavid Rientjes */ 6546fa45f116SDavidlohr Bueso if (fatal_signal_pending(current)) { 654702057967SDavid Rientjes remainder = 0; 654802057967SDavid Rientjes break; 654902057967SDavid Rientjes } 655002057967SDavid Rientjes 6551eefc7fa5SPeter Xu hugetlb_vma_lock_read(vma); 655202057967SDavid Rientjes /* 65534c887265SAdam Litke * Some archs (sparc64, sh*) have multiple pte_ts to 65542a15efc9SHugh Dickins * each hugepage. We have to make sure we get the 65554c887265SAdam Litke * first, for the page indexing below to work. 6556cb900f41SKirill A. Shutemov * 6557cb900f41SKirill A. Shutemov * Note that page table lock is not held when pte is null. 65584c887265SAdam Litke */ 65599c67a207SPeter Xu pte = hugetlb_walk(vma, vaddr & huge_page_mask(h), 65607868a208SPunit Agrawal huge_page_size(h)); 6561cb900f41SKirill A. Shutemov if (pte) 6562cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, pte); 65632a15efc9SHugh Dickins absent = !pte || huge_pte_none(huge_ptep_get(pte)); 656463551ae0SDavid Gibson 65652a15efc9SHugh Dickins /* 65662a15efc9SHugh Dickins * When coredumping, it suits get_dump_page if we just return 65673ae77f43SHugh Dickins * an error where there's an empty slot with no huge pagecache 65683ae77f43SHugh Dickins * to back it. This way, we avoid allocating a hugepage, and 65693ae77f43SHugh Dickins * the sparse dumpfile avoids allocating disk blocks, but its 65703ae77f43SHugh Dickins * huge holes still show up with zeroes where they need to be. 65712a15efc9SHugh Dickins */ 65723ae77f43SHugh Dickins if (absent && (flags & FOLL_DUMP) && 65733ae77f43SHugh Dickins !hugetlbfs_pagecache_present(h, vma, vaddr)) { 6574cb900f41SKirill A. Shutemov if (pte) 6575cb900f41SKirill A. Shutemov spin_unlock(ptl); 6576eefc7fa5SPeter Xu hugetlb_vma_unlock_read(vma); 65772a15efc9SHugh Dickins remainder = 0; 65782a15efc9SHugh Dickins break; 65792a15efc9SHugh Dickins } 65802a15efc9SHugh Dickins 65819cc3a5bdSNaoya Horiguchi /* 65829cc3a5bdSNaoya Horiguchi * We need call hugetlb_fault for both hugepages under migration 65839cc3a5bdSNaoya Horiguchi * (in which case hugetlb_fault waits for the migration,) and 65849cc3a5bdSNaoya Horiguchi * hwpoisoned hugepages (in which case we need to prevent the 65859cc3a5bdSNaoya Horiguchi * caller from accessing to them.) In order to do this, we use 65869cc3a5bdSNaoya Horiguchi * here is_swap_pte instead of is_hugetlb_entry_migration and 65879cc3a5bdSNaoya Horiguchi * is_hugetlb_entry_hwpoisoned. This is because it simply covers 65889cc3a5bdSNaoya Horiguchi * both cases, and because we can't follow correct pages 65899cc3a5bdSNaoya Horiguchi * directly from any kind of swap entries. 65909cc3a5bdSNaoya Horiguchi */ 6591a7f22660SDavid Hildenbrand if (absent || 659284209e87SDavid Hildenbrand __follow_hugetlb_must_fault(vma, flags, pte, &unshare)) { 65932b740303SSouptick Joarder vm_fault_t ret; 659487ffc118SAndrea Arcangeli unsigned int fault_flags = 0; 65954c887265SAdam Litke 6596cb900f41SKirill A. Shutemov if (pte) 6597cb900f41SKirill A. Shutemov spin_unlock(ptl); 6598eefc7fa5SPeter Xu hugetlb_vma_unlock_read(vma); 6599eefc7fa5SPeter Xu 660087ffc118SAndrea Arcangeli if (flags & FOLL_WRITE) 660187ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_WRITE; 6602a7f22660SDavid Hildenbrand else if (unshare) 6603a7f22660SDavid Hildenbrand fault_flags |= FAULT_FLAG_UNSHARE; 660493c5c61dSPeter Xu if (locked) { 660571335f37SPeter Xu fault_flags |= FAULT_FLAG_ALLOW_RETRY | 660671335f37SPeter Xu FAULT_FLAG_KILLABLE; 660793c5c61dSPeter Xu if (flags & FOLL_INTERRUPTIBLE) 660893c5c61dSPeter Xu fault_flags |= FAULT_FLAG_INTERRUPTIBLE; 660993c5c61dSPeter Xu } 661087ffc118SAndrea Arcangeli if (flags & FOLL_NOWAIT) 661187ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_ALLOW_RETRY | 661287ffc118SAndrea Arcangeli FAULT_FLAG_RETRY_NOWAIT; 661387ffc118SAndrea Arcangeli if (flags & FOLL_TRIED) { 66144426e945SPeter Xu /* 66154426e945SPeter Xu * Note: FAULT_FLAG_ALLOW_RETRY and 66164426e945SPeter Xu * FAULT_FLAG_TRIED can co-exist 66174426e945SPeter Xu */ 661887ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_TRIED; 661987ffc118SAndrea Arcangeli } 662087ffc118SAndrea Arcangeli ret = hugetlb_fault(mm, vma, vaddr, fault_flags); 662187ffc118SAndrea Arcangeli if (ret & VM_FAULT_ERROR) { 66222be7cfedSDaniel Jordan err = vm_fault_to_errno(ret, flags); 66231c59827dSHugh Dickins remainder = 0; 66241c59827dSHugh Dickins break; 66251c59827dSHugh Dickins } 662687ffc118SAndrea Arcangeli if (ret & VM_FAULT_RETRY) { 66274f6da934SPeter Xu if (locked && 66281ac25013SAndrea Arcangeli !(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) 66294f6da934SPeter Xu *locked = 0; 663087ffc118SAndrea Arcangeli *nr_pages = 0; 663187ffc118SAndrea Arcangeli /* 663287ffc118SAndrea Arcangeli * VM_FAULT_RETRY must not return an 663387ffc118SAndrea Arcangeli * error, it will return zero 663487ffc118SAndrea Arcangeli * instead. 663587ffc118SAndrea Arcangeli * 663687ffc118SAndrea Arcangeli * No need to update "position" as the 663787ffc118SAndrea Arcangeli * caller will not check it after 663887ffc118SAndrea Arcangeli * *nr_pages is set to 0. 663987ffc118SAndrea Arcangeli */ 664087ffc118SAndrea Arcangeli return i; 664187ffc118SAndrea Arcangeli } 664287ffc118SAndrea Arcangeli continue; 664387ffc118SAndrea Arcangeli } 664463551ae0SDavid Gibson 6645a5516438SAndi Kleen pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; 66467f2e9525SGerald Schaefer page = pte_page(huge_ptep_get(pte)); 66478fde12caSLinus Torvalds 6648b6a2619cSDavid Hildenbrand VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) && 6649b6a2619cSDavid Hildenbrand !PageAnonExclusive(page), page); 6650b6a2619cSDavid Hildenbrand 66518fde12caSLinus Torvalds /* 6652acbfb087SZhigang Lu * If subpage information not requested, update counters 6653acbfb087SZhigang Lu * and skip the same_page loop below. 6654acbfb087SZhigang Lu */ 6655b2cac248SLorenzo Stoakes if (!pages && !pfn_offset && 6656acbfb087SZhigang Lu (vaddr + huge_page_size(h) < vma->vm_end) && 6657acbfb087SZhigang Lu (remainder >= pages_per_huge_page(h))) { 6658acbfb087SZhigang Lu vaddr += huge_page_size(h); 6659acbfb087SZhigang Lu remainder -= pages_per_huge_page(h); 6660acbfb087SZhigang Lu i += pages_per_huge_page(h); 6661acbfb087SZhigang Lu spin_unlock(ptl); 6662eefc7fa5SPeter Xu hugetlb_vma_unlock_read(vma); 6663acbfb087SZhigang Lu continue; 6664acbfb087SZhigang Lu } 6665acbfb087SZhigang Lu 6666d08af0a5SJoao Martins /* vaddr may not be aligned to PAGE_SIZE */ 6667d08af0a5SJoao Martins refs = min3(pages_per_huge_page(h) - pfn_offset, remainder, 6668d08af0a5SJoao Martins (vma->vm_end - ALIGN_DOWN(vaddr, PAGE_SIZE)) >> PAGE_SHIFT); 66690fa5bc40SJoao Martins 6670b2cac248SLorenzo Stoakes if (pages) 6671b2cac248SLorenzo Stoakes record_subpages(nth_page(page, pfn_offset), 667282e5d378SJoao Martins vma, refs, 6673b2cac248SLorenzo Stoakes likely(pages) ? pages + i : NULL); 667463551ae0SDavid Gibson 667582e5d378SJoao Martins if (pages) { 66760fa5bc40SJoao Martins /* 6677822951d8SMatthew Wilcox (Oracle) * try_grab_folio() should always succeed here, 66780fa5bc40SJoao Martins * because: a) we hold the ptl lock, and b) we've just 66790fa5bc40SJoao Martins * checked that the huge page is present in the page 66800fa5bc40SJoao Martins * tables. If the huge page is present, then the tail 66810fa5bc40SJoao Martins * pages must also be present. The ptl prevents the 66820fa5bc40SJoao Martins * head page and tail pages from being rearranged in 66834003f107SLogan Gunthorpe * any way. As this is hugetlb, the pages will never 66844003f107SLogan Gunthorpe * be p2pdma or not longterm pinable. So this page 66854003f107SLogan Gunthorpe * must be available at this point, unless the page 66864003f107SLogan Gunthorpe * refcount overflowed: 66870fa5bc40SJoao Martins */ 6688822951d8SMatthew Wilcox (Oracle) if (WARN_ON_ONCE(!try_grab_folio(pages[i], refs, 66890fa5bc40SJoao Martins flags))) { 66900fa5bc40SJoao Martins spin_unlock(ptl); 6691eefc7fa5SPeter Xu hugetlb_vma_unlock_read(vma); 66920fa5bc40SJoao Martins remainder = 0; 66930fa5bc40SJoao Martins err = -ENOMEM; 66940fa5bc40SJoao Martins break; 66950fa5bc40SJoao Martins } 6696d5d4b0aaSChen, Kenneth W } 669782e5d378SJoao Martins 669882e5d378SJoao Martins vaddr += (refs << PAGE_SHIFT); 669982e5d378SJoao Martins remainder -= refs; 670082e5d378SJoao Martins i += refs; 670182e5d378SJoao Martins 6702cb900f41SKirill A. Shutemov spin_unlock(ptl); 6703eefc7fa5SPeter Xu hugetlb_vma_unlock_read(vma); 670463551ae0SDavid Gibson } 670528a35716SMichel Lespinasse *nr_pages = remainder; 670687ffc118SAndrea Arcangeli /* 670787ffc118SAndrea Arcangeli * setting position is actually required only if remainder is 670887ffc118SAndrea Arcangeli * not zero but it's faster not to add a "if (remainder)" 670987ffc118SAndrea Arcangeli * branch. 671087ffc118SAndrea Arcangeli */ 671163551ae0SDavid Gibson *position = vaddr; 671263551ae0SDavid Gibson 67132be7cfedSDaniel Jordan return i ? i : err; 671463551ae0SDavid Gibson } 67158f860591SZhang, Yanmin 6716a79390f5SPeter Xu long hugetlb_change_protection(struct vm_area_struct *vma, 67175a90d5a1SPeter Xu unsigned long address, unsigned long end, 67185a90d5a1SPeter Xu pgprot_t newprot, unsigned long cp_flags) 67198f860591SZhang, Yanmin { 67208f860591SZhang, Yanmin struct mm_struct *mm = vma->vm_mm; 67218f860591SZhang, Yanmin unsigned long start = address; 67228f860591SZhang, Yanmin pte_t *ptep; 67238f860591SZhang, Yanmin pte_t pte; 6724a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 6725a79390f5SPeter Xu long pages = 0, psize = huge_page_size(h); 6726dff11abeSMike Kravetz bool shared_pmd = false; 6727ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 6728e95a9851SMike Kravetz unsigned long last_addr_mask; 67295a90d5a1SPeter Xu bool uffd_wp = cp_flags & MM_CP_UFFD_WP; 67305a90d5a1SPeter Xu bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; 6731dff11abeSMike Kravetz 6732dff11abeSMike Kravetz /* 6733dff11abeSMike Kravetz * In the case of shared PMDs, the area to flush could be beyond 6734ac46d4f3SJérôme Glisse * start/end. Set range.start/range.end to cover the maximum possible 6735dff11abeSMike Kravetz * range if PMD sharing is possible. 6736dff11abeSMike Kravetz */ 67377269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 67387d4a8be0SAlistair Popple 0, mm, start, end); 6739ac46d4f3SJérôme Glisse adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); 67408f860591SZhang, Yanmin 67418f860591SZhang, Yanmin BUG_ON(address >= end); 6742ac46d4f3SJérôme Glisse flush_cache_range(vma, range.start, range.end); 67438f860591SZhang, Yanmin 6744ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 674540549ba8SMike Kravetz hugetlb_vma_lock_write(vma); 674683cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 674740549ba8SMike Kravetz last_addr_mask = hugetlb_mask_last_page(h); 674860dfaad6SPeter Xu for (; address < end; address += psize) { 6749cb900f41SKirill A. Shutemov spinlock_t *ptl; 67509c67a207SPeter Xu ptep = hugetlb_walk(vma, address, psize); 6751e95a9851SMike Kravetz if (!ptep) { 6752fed15f13SPeter Xu if (!uffd_wp) { 6753e95a9851SMike Kravetz address |= last_addr_mask; 67548f860591SZhang, Yanmin continue; 6755e95a9851SMike Kravetz } 6756fed15f13SPeter Xu /* 6757fed15f13SPeter Xu * Userfaultfd wr-protect requires pgtable 6758fed15f13SPeter Xu * pre-allocations to install pte markers. 6759fed15f13SPeter Xu */ 6760fed15f13SPeter Xu ptep = huge_pte_alloc(mm, vma, address, psize); 6761d1751118SPeter Xu if (!ptep) { 6762d1751118SPeter Xu pages = -ENOMEM; 6763fed15f13SPeter Xu break; 6764fed15f13SPeter Xu } 6765d1751118SPeter Xu } 6766cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 67674ddb4d91SMike Kravetz if (huge_pmd_unshare(mm, vma, address, ptep)) { 676860dfaad6SPeter Xu /* 676960dfaad6SPeter Xu * When uffd-wp is enabled on the vma, unshare 677060dfaad6SPeter Xu * shouldn't happen at all. Warn about it if it 677160dfaad6SPeter Xu * happened due to some reason. 677260dfaad6SPeter Xu */ 677360dfaad6SPeter Xu WARN_ON_ONCE(uffd_wp || uffd_wp_resolve); 67747da4d641SPeter Zijlstra pages++; 6775cb900f41SKirill A. Shutemov spin_unlock(ptl); 6776dff11abeSMike Kravetz shared_pmd = true; 67774ddb4d91SMike Kravetz address |= last_addr_mask; 677839dde65cSChen, Kenneth W continue; 67797da4d641SPeter Zijlstra } 6780a8bda28dSNaoya Horiguchi pte = huge_ptep_get(ptep); 6781a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { 67820e678153SDavid Hildenbrand /* Nothing to do. */ 67830e678153SDavid Hildenbrand } else if (unlikely(is_hugetlb_entry_migration(pte))) { 6784a8bda28dSNaoya Horiguchi swp_entry_t entry = pte_to_swp_entry(pte); 67856c287605SDavid Hildenbrand struct page *page = pfn_swap_entry_to_page(entry); 678644f86392SDavid Hildenbrand pte_t newpte = pte; 6787a8bda28dSNaoya Horiguchi 678844f86392SDavid Hildenbrand if (is_writable_migration_entry(entry)) { 67896c287605SDavid Hildenbrand if (PageAnon(page)) 67906c287605SDavid Hildenbrand entry = make_readable_exclusive_migration_entry( 67916c287605SDavid Hildenbrand swp_offset(entry)); 67926c287605SDavid Hildenbrand else 67934dd845b5SAlistair Popple entry = make_readable_migration_entry( 67944dd845b5SAlistair Popple swp_offset(entry)); 6795a8bda28dSNaoya Horiguchi newpte = swp_entry_to_pte(entry); 679644f86392SDavid Hildenbrand pages++; 679744f86392SDavid Hildenbrand } 679844f86392SDavid Hildenbrand 67995a90d5a1SPeter Xu if (uffd_wp) 68005a90d5a1SPeter Xu newpte = pte_swp_mkuffd_wp(newpte); 68015a90d5a1SPeter Xu else if (uffd_wp_resolve) 68025a90d5a1SPeter Xu newpte = pte_swp_clear_uffd_wp(newpte); 680344f86392SDavid Hildenbrand if (!pte_same(pte, newpte)) 680418f39629SQi Zheng set_huge_pte_at(mm, address, ptep, newpte); 68050e678153SDavid Hildenbrand } else if (unlikely(is_pte_marker(pte))) { 68060e678153SDavid Hildenbrand /* No other markers apply for now. */ 68070e678153SDavid Hildenbrand WARN_ON_ONCE(!pte_marker_uffd_wp(pte)); 680860dfaad6SPeter Xu if (uffd_wp_resolve) 68090e678153SDavid Hildenbrand /* Safe to modify directly (non-present->none). */ 681060dfaad6SPeter Xu huge_pte_clear(mm, address, ptep, psize); 68110e678153SDavid Hildenbrand } else if (!huge_pte_none(pte)) { 6812023bdd00SAneesh Kumar K.V pte_t old_pte; 681379c1c594SChristophe Leroy unsigned int shift = huge_page_shift(hstate_vma(vma)); 6814023bdd00SAneesh Kumar K.V 6815023bdd00SAneesh Kumar K.V old_pte = huge_ptep_modify_prot_start(vma, address, ptep); 681616785bd7SAnshuman Khandual pte = huge_pte_modify(old_pte, newprot); 681779c1c594SChristophe Leroy pte = arch_make_huge_pte(pte, shift, vma->vm_flags); 68185a90d5a1SPeter Xu if (uffd_wp) 6819f1eb1bacSPeter Xu pte = huge_pte_mkuffd_wp(pte); 68205a90d5a1SPeter Xu else if (uffd_wp_resolve) 68215a90d5a1SPeter Xu pte = huge_pte_clear_uffd_wp(pte); 6822023bdd00SAneesh Kumar K.V huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte); 68237da4d641SPeter Zijlstra pages++; 682460dfaad6SPeter Xu } else { 682560dfaad6SPeter Xu /* None pte */ 682660dfaad6SPeter Xu if (unlikely(uffd_wp)) 682760dfaad6SPeter Xu /* Safe to modify directly (none->non-present). */ 682860dfaad6SPeter Xu set_huge_pte_at(mm, address, ptep, 682960dfaad6SPeter Xu make_pte_marker(PTE_MARKER_UFFD_WP)); 68308f860591SZhang, Yanmin } 6831cb900f41SKirill A. Shutemov spin_unlock(ptl); 68328f860591SZhang, Yanmin } 6833d833352aSMel Gorman /* 6834c8c06efaSDavidlohr Bueso * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare 6835d833352aSMel Gorman * may have cleared our pud entry and done put_page on the page table: 6836c8c06efaSDavidlohr Bueso * once we release i_mmap_rwsem, another task can do the final put_page 6837dff11abeSMike Kravetz * and that page table be reused and filled with junk. If we actually 6838dff11abeSMike Kravetz * did unshare a page of pmds, flush the range corresponding to the pud. 6839d833352aSMel Gorman */ 6840dff11abeSMike Kravetz if (shared_pmd) 6841ac46d4f3SJérôme Glisse flush_hugetlb_tlb_range(vma, range.start, range.end); 6842dff11abeSMike Kravetz else 68435491ae7bSAneesh Kumar K.V flush_hugetlb_tlb_range(vma, start, end); 68440f10851eSJérôme Glisse /* 68450f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() we are downgrading 68460f10851eSJérôme Glisse * page table protection not changing it to point to a new page. 68470f10851eSJérôme Glisse * 6848ee65728eSMike Rapoport * See Documentation/mm/mmu_notifier.rst 68490f10851eSJérôme Glisse */ 685083cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 685140549ba8SMike Kravetz hugetlb_vma_unlock_write(vma); 6852ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 68537da4d641SPeter Zijlstra 6854d1751118SPeter Xu return pages > 0 ? (pages << h->order) : pages; 68558f860591SZhang, Yanmin } 68568f860591SZhang, Yanmin 685733b8f84aSMike Kravetz /* Return true if reservation was successful, false otherwise. */ 685833b8f84aSMike Kravetz bool hugetlb_reserve_pages(struct inode *inode, 6859a1e78772SMel Gorman long from, long to, 68605a6fe125SMel Gorman struct vm_area_struct *vma, 6861ca16d140SKOSAKI Motohiro vm_flags_t vm_flags) 6862e4e574b7SAdam Litke { 6863c5094ec7SMike Kravetz long chg = -1, add = -1; 6864a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 686590481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 68669119a41eSJoonsoo Kim struct resv_map *resv_map; 6867075a61d0SMina Almasry struct hugetlb_cgroup *h_cg = NULL; 68680db9d74eSMina Almasry long gbl_reserve, regions_needed = 0; 6869e4e574b7SAdam Litke 687063489f8eSMike Kravetz /* This should never happen */ 687163489f8eSMike Kravetz if (from > to) { 687263489f8eSMike Kravetz VM_WARN(1, "%s called with a negative range\n", __func__); 687333b8f84aSMike Kravetz return false; 687463489f8eSMike Kravetz } 687563489f8eSMike Kravetz 6876a1e78772SMel Gorman /* 6877e700898fSMike Kravetz * vma specific semaphore used for pmd sharing and fault/truncation 6878e700898fSMike Kravetz * synchronization 68798d9bfb26SMike Kravetz */ 68808d9bfb26SMike Kravetz hugetlb_vma_lock_alloc(vma); 68818d9bfb26SMike Kravetz 68828d9bfb26SMike Kravetz /* 688317c9d12eSMel Gorman * Only apply hugepage reservation if asked. At fault time, an 688417c9d12eSMel Gorman * attempt will be made for VM_NORESERVE to allocate a page 688590481622SDavid Gibson * without using reserves 688617c9d12eSMel Gorman */ 6887ca16d140SKOSAKI Motohiro if (vm_flags & VM_NORESERVE) 688833b8f84aSMike Kravetz return true; 688917c9d12eSMel Gorman 689017c9d12eSMel Gorman /* 6891a1e78772SMel Gorman * Shared mappings base their reservation on the number of pages that 6892a1e78772SMel Gorman * are already allocated on behalf of the file. Private mappings need 6893a1e78772SMel Gorman * to reserve the full area even if read-only as mprotect() may be 6894a1e78772SMel Gorman * called to make the mapping read-write. Assume !vma is a shm mapping 6895a1e78772SMel Gorman */ 68969119a41eSJoonsoo Kim if (!vma || vma->vm_flags & VM_MAYSHARE) { 6897f27a5136SMike Kravetz /* 6898f27a5136SMike Kravetz * resv_map can not be NULL as hugetlb_reserve_pages is only 6899f27a5136SMike Kravetz * called for inodes for which resv_maps were created (see 6900f27a5136SMike Kravetz * hugetlbfs_get_inode). 6901f27a5136SMike Kravetz */ 69024e35f483SJoonsoo Kim resv_map = inode_resv_map(inode); 69039119a41eSJoonsoo Kim 69040db9d74eSMina Almasry chg = region_chg(resv_map, from, to, ®ions_needed); 69059119a41eSJoonsoo Kim } else { 6906e9fe92aeSMina Almasry /* Private mapping. */ 69079119a41eSJoonsoo Kim resv_map = resv_map_alloc(); 69085a6fe125SMel Gorman if (!resv_map) 69098d9bfb26SMike Kravetz goto out_err; 69105a6fe125SMel Gorman 691117c9d12eSMel Gorman chg = to - from; 691217c9d12eSMel Gorman 69135a6fe125SMel Gorman set_vma_resv_map(vma, resv_map); 69145a6fe125SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_OWNER); 69155a6fe125SMel Gorman } 69165a6fe125SMel Gorman 691733b8f84aSMike Kravetz if (chg < 0) 6918c50ac050SDave Hansen goto out_err; 691917c9d12eSMel Gorman 692033b8f84aSMike Kravetz if (hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h), 692133b8f84aSMike Kravetz chg * pages_per_huge_page(h), &h_cg) < 0) 6922075a61d0SMina Almasry goto out_err; 6923075a61d0SMina Almasry 6924075a61d0SMina Almasry if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) { 6925075a61d0SMina Almasry /* For private mappings, the hugetlb_cgroup uncharge info hangs 6926075a61d0SMina Almasry * of the resv_map. 6927075a61d0SMina Almasry */ 6928075a61d0SMina Almasry resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h); 6929075a61d0SMina Almasry } 6930075a61d0SMina Almasry 69311c5ecae3SMike Kravetz /* 69321c5ecae3SMike Kravetz * There must be enough pages in the subpool for the mapping. If 69331c5ecae3SMike Kravetz * the subpool has a minimum size, there may be some global 69341c5ecae3SMike Kravetz * reservations already in place (gbl_reserve). 69351c5ecae3SMike Kravetz */ 69361c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_get_pages(spool, chg); 693733b8f84aSMike Kravetz if (gbl_reserve < 0) 6938075a61d0SMina Almasry goto out_uncharge_cgroup; 693917c9d12eSMel Gorman 694017c9d12eSMel Gorman /* 694117c9d12eSMel Gorman * Check enough hugepages are available for the reservation. 694290481622SDavid Gibson * Hand the pages back to the subpool if there are not 694317c9d12eSMel Gorman */ 694433b8f84aSMike Kravetz if (hugetlb_acct_memory(h, gbl_reserve) < 0) 6945075a61d0SMina Almasry goto out_put_pages; 694617c9d12eSMel Gorman 694717c9d12eSMel Gorman /* 694817c9d12eSMel Gorman * Account for the reservations made. Shared mappings record regions 694917c9d12eSMel Gorman * that have reservations as they are shared by multiple VMAs. 695017c9d12eSMel Gorman * When the last VMA disappears, the region map says how much 695117c9d12eSMel Gorman * the reservation was and the page cache tells how much of 695217c9d12eSMel Gorman * the reservation was consumed. Private mappings are per-VMA and 695317c9d12eSMel Gorman * only the consumed reservations are tracked. When the VMA 695417c9d12eSMel Gorman * disappears, the original reservation is the VMA size and the 695517c9d12eSMel Gorman * consumed reservations are stored in the map. Hence, nothing 695617c9d12eSMel Gorman * else has to be done for private mappings here 695717c9d12eSMel Gorman */ 695833039678SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) { 6959075a61d0SMina Almasry add = region_add(resv_map, from, to, regions_needed, h, h_cg); 696033039678SMike Kravetz 69610db9d74eSMina Almasry if (unlikely(add < 0)) { 69620db9d74eSMina Almasry hugetlb_acct_memory(h, -gbl_reserve); 6963075a61d0SMina Almasry goto out_put_pages; 69640db9d74eSMina Almasry } else if (unlikely(chg > add)) { 696533039678SMike Kravetz /* 696633039678SMike Kravetz * pages in this range were added to the reserve 696733039678SMike Kravetz * map between region_chg and region_add. This 6968d0ce0e47SSidhartha Kumar * indicates a race with alloc_hugetlb_folio. Adjust 696933039678SMike Kravetz * the subpool and reserve counts modified above 697033039678SMike Kravetz * based on the difference. 697133039678SMike Kravetz */ 697233039678SMike Kravetz long rsv_adjust; 697333039678SMike Kravetz 6974d85aecf2SMiaohe Lin /* 6975d85aecf2SMiaohe Lin * hugetlb_cgroup_uncharge_cgroup_rsvd() will put the 6976d85aecf2SMiaohe Lin * reference to h_cg->css. See comment below for detail. 6977d85aecf2SMiaohe Lin */ 6978075a61d0SMina Almasry hugetlb_cgroup_uncharge_cgroup_rsvd( 6979075a61d0SMina Almasry hstate_index(h), 6980075a61d0SMina Almasry (chg - add) * pages_per_huge_page(h), h_cg); 6981075a61d0SMina Almasry 698233039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 698333039678SMike Kravetz chg - add); 698433039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 6985d85aecf2SMiaohe Lin } else if (h_cg) { 6986d85aecf2SMiaohe Lin /* 6987d85aecf2SMiaohe Lin * The file_regions will hold their own reference to 6988d85aecf2SMiaohe Lin * h_cg->css. So we should release the reference held 6989d85aecf2SMiaohe Lin * via hugetlb_cgroup_charge_cgroup_rsvd() when we are 6990d85aecf2SMiaohe Lin * done. 6991d85aecf2SMiaohe Lin */ 6992d85aecf2SMiaohe Lin hugetlb_cgroup_put_rsvd_cgroup(h_cg); 699333039678SMike Kravetz } 699433039678SMike Kravetz } 699533b8f84aSMike Kravetz return true; 699633b8f84aSMike Kravetz 6997075a61d0SMina Almasry out_put_pages: 6998075a61d0SMina Almasry /* put back original number of pages, chg */ 6999075a61d0SMina Almasry (void)hugepage_subpool_put_pages(spool, chg); 7000075a61d0SMina Almasry out_uncharge_cgroup: 7001075a61d0SMina Almasry hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h), 7002075a61d0SMina Almasry chg * pages_per_huge_page(h), h_cg); 7003c50ac050SDave Hansen out_err: 70048d9bfb26SMike Kravetz hugetlb_vma_lock_free(vma); 70055e911373SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) 70060db9d74eSMina Almasry /* Only call region_abort if the region_chg succeeded but the 70070db9d74eSMina Almasry * region_add failed or didn't run. 70080db9d74eSMina Almasry */ 70090db9d74eSMina Almasry if (chg >= 0 && add < 0) 70100db9d74eSMina Almasry region_abort(resv_map, from, to, regions_needed); 7011f031dd27SJoonsoo Kim if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 7012f031dd27SJoonsoo Kim kref_put(&resv_map->refs, resv_map_release); 701333b8f84aSMike Kravetz return false; 7014a43a8c39SChen, Kenneth W } 7015a43a8c39SChen, Kenneth W 7016b5cec28dSMike Kravetz long hugetlb_unreserve_pages(struct inode *inode, long start, long end, 7017b5cec28dSMike Kravetz long freed) 7018a43a8c39SChen, Kenneth W { 7019a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 70204e35f483SJoonsoo Kim struct resv_map *resv_map = inode_resv_map(inode); 70219119a41eSJoonsoo Kim long chg = 0; 702290481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 70231c5ecae3SMike Kravetz long gbl_reserve; 702445c682a6SKen Chen 7025f27a5136SMike Kravetz /* 7026f27a5136SMike Kravetz * Since this routine can be called in the evict inode path for all 7027f27a5136SMike Kravetz * hugetlbfs inodes, resv_map could be NULL. 7028f27a5136SMike Kravetz */ 7029b5cec28dSMike Kravetz if (resv_map) { 7030b5cec28dSMike Kravetz chg = region_del(resv_map, start, end); 7031b5cec28dSMike Kravetz /* 7032b5cec28dSMike Kravetz * region_del() can fail in the rare case where a region 7033b5cec28dSMike Kravetz * must be split and another region descriptor can not be 7034b5cec28dSMike Kravetz * allocated. If end == LONG_MAX, it will not fail. 7035b5cec28dSMike Kravetz */ 7036b5cec28dSMike Kravetz if (chg < 0) 7037b5cec28dSMike Kravetz return chg; 7038b5cec28dSMike Kravetz } 7039b5cec28dSMike Kravetz 704045c682a6SKen Chen spin_lock(&inode->i_lock); 7041e4c6f8beSEric Sandeen inode->i_blocks -= (blocks_per_huge_page(h) * freed); 704245c682a6SKen Chen spin_unlock(&inode->i_lock); 704345c682a6SKen Chen 70441c5ecae3SMike Kravetz /* 70451c5ecae3SMike Kravetz * If the subpool has a minimum size, the number of global 70461c5ecae3SMike Kravetz * reservations to be released may be adjusted. 7047dddf31a4SMiaohe Lin * 7048dddf31a4SMiaohe Lin * Note that !resv_map implies freed == 0. So (chg - freed) 7049dddf31a4SMiaohe Lin * won't go negative. 70501c5ecae3SMike Kravetz */ 70511c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); 70521c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 7053b5cec28dSMike Kravetz 7054b5cec28dSMike Kravetz return 0; 7055a43a8c39SChen, Kenneth W } 705693f70f90SNaoya Horiguchi 70573212b535SSteve Capper #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE 70583212b535SSteve Capper static unsigned long page_table_shareable(struct vm_area_struct *svma, 70593212b535SSteve Capper struct vm_area_struct *vma, 70603212b535SSteve Capper unsigned long addr, pgoff_t idx) 70613212b535SSteve Capper { 70623212b535SSteve Capper unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + 70633212b535SSteve Capper svma->vm_start; 70643212b535SSteve Capper unsigned long sbase = saddr & PUD_MASK; 70653212b535SSteve Capper unsigned long s_end = sbase + PUD_SIZE; 70663212b535SSteve Capper 70673212b535SSteve Capper /* Allow segments to share if only one is marked locked */ 7068e430a95aSSuren Baghdasaryan unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED_MASK; 7069e430a95aSSuren Baghdasaryan unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED_MASK; 70703212b535SSteve Capper 70713212b535SSteve Capper /* 70723212b535SSteve Capper * match the virtual addresses, permission and the alignment of the 70733212b535SSteve Capper * page table page. 7074131a79b4SMike Kravetz * 7075131a79b4SMike Kravetz * Also, vma_lock (vm_private_data) is required for sharing. 70763212b535SSteve Capper */ 70773212b535SSteve Capper if (pmd_index(addr) != pmd_index(saddr) || 70783212b535SSteve Capper vm_flags != svm_flags || 7079131a79b4SMike Kravetz !range_in_vma(svma, sbase, s_end) || 7080131a79b4SMike Kravetz !svma->vm_private_data) 70813212b535SSteve Capper return 0; 70823212b535SSteve Capper 70833212b535SSteve Capper return saddr; 70843212b535SSteve Capper } 70853212b535SSteve Capper 7086bbff39ccSMike Kravetz bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr) 70873212b535SSteve Capper { 7088bbff39ccSMike Kravetz unsigned long start = addr & PUD_MASK; 7089bbff39ccSMike Kravetz unsigned long end = start + PUD_SIZE; 7090bbff39ccSMike Kravetz 70918d9bfb26SMike Kravetz #ifdef CONFIG_USERFAULTFD 70928d9bfb26SMike Kravetz if (uffd_disable_huge_pmd_share(vma)) 70938d9bfb26SMike Kravetz return false; 70948d9bfb26SMike Kravetz #endif 70953212b535SSteve Capper /* 70963212b535SSteve Capper * check on proper vm_flags and page table alignment 70973212b535SSteve Capper */ 70988d9bfb26SMike Kravetz if (!(vma->vm_flags & VM_MAYSHARE)) 709931aafb45SNicholas Krause return false; 7100bbff39ccSMike Kravetz if (!vma->vm_private_data) /* vma lock required for sharing */ 71018d9bfb26SMike Kravetz return false; 71028d9bfb26SMike Kravetz if (!range_in_vma(vma, start, end)) 71038d9bfb26SMike Kravetz return false; 71048d9bfb26SMike Kravetz return true; 71058d9bfb26SMike Kravetz } 71068d9bfb26SMike Kravetz 71073212b535SSteve Capper /* 7108017b1660SMike Kravetz * Determine if start,end range within vma could be mapped by shared pmd. 7109017b1660SMike Kravetz * If yes, adjust start and end to cover range associated with possible 7110017b1660SMike Kravetz * shared pmd mappings. 7111017b1660SMike Kravetz */ 7112017b1660SMike Kravetz void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, 7113017b1660SMike Kravetz unsigned long *start, unsigned long *end) 7114017b1660SMike Kravetz { 7115a1ba9da8SLi Xinhai unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE), 7116a1ba9da8SLi Xinhai v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE); 7117017b1660SMike Kravetz 7118a1ba9da8SLi Xinhai /* 7119f0953a1bSIngo Molnar * vma needs to span at least one aligned PUD size, and the range 7120f0953a1bSIngo Molnar * must be at least partially within in. 7121a1ba9da8SLi Xinhai */ 7122a1ba9da8SLi Xinhai if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) || 7123a1ba9da8SLi Xinhai (*end <= v_start) || (*start >= v_end)) 7124017b1660SMike Kravetz return; 7125017b1660SMike Kravetz 712675802ca6SPeter Xu /* Extend the range to be PUD aligned for a worst case scenario */ 7127a1ba9da8SLi Xinhai if (*start > v_start) 7128a1ba9da8SLi Xinhai *start = ALIGN_DOWN(*start, PUD_SIZE); 7129017b1660SMike Kravetz 7130a1ba9da8SLi Xinhai if (*end < v_end) 7131a1ba9da8SLi Xinhai *end = ALIGN(*end, PUD_SIZE); 7132017b1660SMike Kravetz } 7133017b1660SMike Kravetz 7134017b1660SMike Kravetz /* 71353212b535SSteve Capper * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() 71363212b535SSteve Capper * and returns the corresponding pte. While this is not necessary for the 71373212b535SSteve Capper * !shared pmd case because we can allocate the pmd later as well, it makes the 71383a47c54fSMike Kravetz * code much cleaner. pmd allocation is essential for the shared case because 71393a47c54fSMike Kravetz * pud has to be populated inside the same i_mmap_rwsem section - otherwise 71403a47c54fSMike Kravetz * racing tasks could either miss the sharing (see huge_pte_offset) or select a 71413a47c54fSMike Kravetz * bad pmd for sharing. 71423212b535SSteve Capper */ 7143aec44e0fSPeter Xu pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, 7144aec44e0fSPeter Xu unsigned long addr, pud_t *pud) 71453212b535SSteve Capper { 71463212b535SSteve Capper struct address_space *mapping = vma->vm_file->f_mapping; 71473212b535SSteve Capper pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + 71483212b535SSteve Capper vma->vm_pgoff; 71493212b535SSteve Capper struct vm_area_struct *svma; 71503212b535SSteve Capper unsigned long saddr; 71513212b535SSteve Capper pte_t *spte = NULL; 71523212b535SSteve Capper pte_t *pte; 71533212b535SSteve Capper 71543a47c54fSMike Kravetz i_mmap_lock_read(mapping); 71553212b535SSteve Capper vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { 71563212b535SSteve Capper if (svma == vma) 71573212b535SSteve Capper continue; 71583212b535SSteve Capper 71593212b535SSteve Capper saddr = page_table_shareable(svma, vma, addr, idx); 71603212b535SSteve Capper if (saddr) { 71619c67a207SPeter Xu spte = hugetlb_walk(svma, saddr, 71627868a208SPunit Agrawal vma_mmu_pagesize(svma)); 71633212b535SSteve Capper if (spte) { 71643212b535SSteve Capper get_page(virt_to_page(spte)); 71653212b535SSteve Capper break; 71663212b535SSteve Capper } 71673212b535SSteve Capper } 71683212b535SSteve Capper } 71693212b535SSteve Capper 71703212b535SSteve Capper if (!spte) 71713212b535SSteve Capper goto out; 71723212b535SSteve Capper 7173349d1670SPeter Xu spin_lock(&mm->page_table_lock); 7174dc6c9a35SKirill A. Shutemov if (pud_none(*pud)) { 71753212b535SSteve Capper pud_populate(mm, pud, 71763212b535SSteve Capper (pmd_t *)((unsigned long)spte & PAGE_MASK)); 7177c17b1f42SKirill A. Shutemov mm_inc_nr_pmds(mm); 7178dc6c9a35SKirill A. Shutemov } else { 71793212b535SSteve Capper put_page(virt_to_page(spte)); 7180dc6c9a35SKirill A. Shutemov } 7181349d1670SPeter Xu spin_unlock(&mm->page_table_lock); 71823212b535SSteve Capper out: 71833212b535SSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 71843a47c54fSMike Kravetz i_mmap_unlock_read(mapping); 71853212b535SSteve Capper return pte; 71863212b535SSteve Capper } 71873212b535SSteve Capper 71883212b535SSteve Capper /* 71893212b535SSteve Capper * unmap huge page backed by shared pte. 71903212b535SSteve Capper * 71913212b535SSteve Capper * Hugetlb pte page is ref counted at the time of mapping. If pte is shared 71923212b535SSteve Capper * indicated by page_count > 1, unmap is achieved by clearing pud and 71933212b535SSteve Capper * decrementing the ref count. If count == 1, the pte page is not shared. 71943212b535SSteve Capper * 71953a47c54fSMike Kravetz * Called with page table lock held. 71963212b535SSteve Capper * 71973212b535SSteve Capper * returns: 1 successfully unmapped a shared pte page 71983212b535SSteve Capper * 0 the underlying pte page is not shared, or it is the last user 71993212b535SSteve Capper */ 720034ae204fSMike Kravetz int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma, 72014ddb4d91SMike Kravetz unsigned long addr, pte_t *ptep) 72023212b535SSteve Capper { 72034ddb4d91SMike Kravetz pgd_t *pgd = pgd_offset(mm, addr); 72044ddb4d91SMike Kravetz p4d_t *p4d = p4d_offset(pgd, addr); 72054ddb4d91SMike Kravetz pud_t *pud = pud_offset(p4d, addr); 72063212b535SSteve Capper 720734ae204fSMike Kravetz i_mmap_assert_write_locked(vma->vm_file->f_mapping); 720840549ba8SMike Kravetz hugetlb_vma_assert_locked(vma); 72093212b535SSteve Capper BUG_ON(page_count(virt_to_page(ptep)) == 0); 72103212b535SSteve Capper if (page_count(virt_to_page(ptep)) == 1) 72113212b535SSteve Capper return 0; 72123212b535SSteve Capper 72133212b535SSteve Capper pud_clear(pud); 72143212b535SSteve Capper put_page(virt_to_page(ptep)); 7215dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(mm); 72163212b535SSteve Capper return 1; 72173212b535SSteve Capper } 7218c1991e07SPeter Xu 72199e5fc74cSSteve Capper #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 72208d9bfb26SMike Kravetz 7221aec44e0fSPeter Xu pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, 7222aec44e0fSPeter Xu unsigned long addr, pud_t *pud) 72239e5fc74cSSteve Capper { 72249e5fc74cSSteve Capper return NULL; 72259e5fc74cSSteve Capper } 7226e81f2d22SZhang Zhen 722734ae204fSMike Kravetz int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma, 72284ddb4d91SMike Kravetz unsigned long addr, pte_t *ptep) 7229e81f2d22SZhang Zhen { 7230e81f2d22SZhang Zhen return 0; 7231e81f2d22SZhang Zhen } 7232017b1660SMike Kravetz 7233017b1660SMike Kravetz void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, 7234017b1660SMike Kravetz unsigned long *start, unsigned long *end) 7235017b1660SMike Kravetz { 7236017b1660SMike Kravetz } 7237c1991e07SPeter Xu 7238c1991e07SPeter Xu bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr) 7239c1991e07SPeter Xu { 7240c1991e07SPeter Xu return false; 7241c1991e07SPeter Xu } 72423212b535SSteve Capper #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 72433212b535SSteve Capper 72449e5fc74cSSteve Capper #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 7245aec44e0fSPeter Xu pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, 72469e5fc74cSSteve Capper unsigned long addr, unsigned long sz) 72479e5fc74cSSteve Capper { 72489e5fc74cSSteve Capper pgd_t *pgd; 7249c2febafcSKirill A. Shutemov p4d_t *p4d; 72509e5fc74cSSteve Capper pud_t *pud; 72519e5fc74cSSteve Capper pte_t *pte = NULL; 72529e5fc74cSSteve Capper 72539e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 7254f4f0a3d8SKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 7255f4f0a3d8SKirill A. Shutemov if (!p4d) 7256f4f0a3d8SKirill A. Shutemov return NULL; 7257c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 72589e5fc74cSSteve Capper if (pud) { 72599e5fc74cSSteve Capper if (sz == PUD_SIZE) { 72609e5fc74cSSteve Capper pte = (pte_t *)pud; 72619e5fc74cSSteve Capper } else { 72629e5fc74cSSteve Capper BUG_ON(sz != PMD_SIZE); 7263c1991e07SPeter Xu if (want_pmd_share(vma, addr) && pud_none(*pud)) 7264aec44e0fSPeter Xu pte = huge_pmd_share(mm, vma, addr, pud); 72659e5fc74cSSteve Capper else 72669e5fc74cSSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 72679e5fc74cSSteve Capper } 72689e5fc74cSSteve Capper } 7269191fcdb6SJohn Hubbard 7270191fcdb6SJohn Hubbard if (pte) { 7271191fcdb6SJohn Hubbard pte_t pteval = ptep_get_lockless(pte); 7272191fcdb6SJohn Hubbard 7273191fcdb6SJohn Hubbard BUG_ON(pte_present(pteval) && !pte_huge(pteval)); 7274191fcdb6SJohn Hubbard } 72759e5fc74cSSteve Capper 72769e5fc74cSSteve Capper return pte; 72779e5fc74cSSteve Capper } 72789e5fc74cSSteve Capper 72799b19df29SPunit Agrawal /* 72809b19df29SPunit Agrawal * huge_pte_offset() - Walk the page table to resolve the hugepage 72819b19df29SPunit Agrawal * entry at address @addr 72829b19df29SPunit Agrawal * 72838ac0b81aSLi Xinhai * Return: Pointer to page table entry (PUD or PMD) for 72848ac0b81aSLi Xinhai * address @addr, or NULL if a !p*d_present() entry is encountered and the 72859b19df29SPunit Agrawal * size @sz doesn't match the hugepage size at this level of the page 72869b19df29SPunit Agrawal * table. 72879b19df29SPunit Agrawal */ 72887868a208SPunit Agrawal pte_t *huge_pte_offset(struct mm_struct *mm, 72897868a208SPunit Agrawal unsigned long addr, unsigned long sz) 72909e5fc74cSSteve Capper { 72919e5fc74cSSteve Capper pgd_t *pgd; 7292c2febafcSKirill A. Shutemov p4d_t *p4d; 72938ac0b81aSLi Xinhai pud_t *pud; 72948ac0b81aSLi Xinhai pmd_t *pmd; 72959e5fc74cSSteve Capper 72969e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 7297c2febafcSKirill A. Shutemov if (!pgd_present(*pgd)) 7298c2febafcSKirill A. Shutemov return NULL; 7299c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 7300c2febafcSKirill A. Shutemov if (!p4d_present(*p4d)) 7301c2febafcSKirill A. Shutemov return NULL; 73029b19df29SPunit Agrawal 7303c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 73048ac0b81aSLi Xinhai if (sz == PUD_SIZE) 73058ac0b81aSLi Xinhai /* must be pud huge, non-present or none */ 73069e5fc74cSSteve Capper return (pte_t *)pud; 73078ac0b81aSLi Xinhai if (!pud_present(*pud)) 73088ac0b81aSLi Xinhai return NULL; 73098ac0b81aSLi Xinhai /* must have a valid entry and size to go further */ 73109b19df29SPunit Agrawal 73119e5fc74cSSteve Capper pmd = pmd_offset(pud, addr); 73128ac0b81aSLi Xinhai /* must be pmd huge, non-present or none */ 73139e5fc74cSSteve Capper return (pte_t *)pmd; 73149e5fc74cSSteve Capper } 73159e5fc74cSSteve Capper 7316e95a9851SMike Kravetz /* 7317e95a9851SMike Kravetz * Return a mask that can be used to update an address to the last huge 7318e95a9851SMike Kravetz * page in a page table page mapping size. Used to skip non-present 7319e95a9851SMike Kravetz * page table entries when linearly scanning address ranges. Architectures 7320e95a9851SMike Kravetz * with unique huge page to page table relationships can define their own 7321e95a9851SMike Kravetz * version of this routine. 7322e95a9851SMike Kravetz */ 7323e95a9851SMike Kravetz unsigned long hugetlb_mask_last_page(struct hstate *h) 7324e95a9851SMike Kravetz { 7325e95a9851SMike Kravetz unsigned long hp_size = huge_page_size(h); 7326e95a9851SMike Kravetz 7327e95a9851SMike Kravetz if (hp_size == PUD_SIZE) 7328e95a9851SMike Kravetz return P4D_SIZE - PUD_SIZE; 7329e95a9851SMike Kravetz else if (hp_size == PMD_SIZE) 7330e95a9851SMike Kravetz return PUD_SIZE - PMD_SIZE; 7331e95a9851SMike Kravetz else 7332e95a9851SMike Kravetz return 0UL; 7333e95a9851SMike Kravetz } 7334e95a9851SMike Kravetz 7335e95a9851SMike Kravetz #else 7336e95a9851SMike Kravetz 7337e95a9851SMike Kravetz /* See description above. Architectures can provide their own version. */ 7338e95a9851SMike Kravetz __weak unsigned long hugetlb_mask_last_page(struct hstate *h) 7339e95a9851SMike Kravetz { 73404ddb4d91SMike Kravetz #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE 73414ddb4d91SMike Kravetz if (huge_page_size(h) == PMD_SIZE) 73424ddb4d91SMike Kravetz return PUD_SIZE - PMD_SIZE; 73434ddb4d91SMike Kravetz #endif 7344e95a9851SMike Kravetz return 0UL; 7345e95a9851SMike Kravetz } 7346e95a9851SMike Kravetz 734761f77edaSNaoya Horiguchi #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 734861f77edaSNaoya Horiguchi 734961f77edaSNaoya Horiguchi /* 735061f77edaSNaoya Horiguchi * These functions are overwritable if your architecture needs its own 735161f77edaSNaoya Horiguchi * behavior. 735261f77edaSNaoya Horiguchi */ 73539747b9e9SBaolin Wang bool isolate_hugetlb(struct folio *folio, struct list_head *list) 735431caf665SNaoya Horiguchi { 73559747b9e9SBaolin Wang bool ret = true; 7356bcc54222SNaoya Horiguchi 7357db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 73586aa3a920SSidhartha Kumar if (!folio_test_hugetlb(folio) || 73596aa3a920SSidhartha Kumar !folio_test_hugetlb_migratable(folio) || 73606aa3a920SSidhartha Kumar !folio_try_get(folio)) { 73619747b9e9SBaolin Wang ret = false; 7362bcc54222SNaoya Horiguchi goto unlock; 7363bcc54222SNaoya Horiguchi } 73646aa3a920SSidhartha Kumar folio_clear_hugetlb_migratable(folio); 73656aa3a920SSidhartha Kumar list_move_tail(&folio->lru, list); 7366bcc54222SNaoya Horiguchi unlock: 7367db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 7368bcc54222SNaoya Horiguchi return ret; 736931caf665SNaoya Horiguchi } 737031caf665SNaoya Horiguchi 737104bac040SSidhartha Kumar int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison) 737225182f05SNaoya Horiguchi { 737325182f05SNaoya Horiguchi int ret = 0; 737425182f05SNaoya Horiguchi 737525182f05SNaoya Horiguchi *hugetlb = false; 737625182f05SNaoya Horiguchi spin_lock_irq(&hugetlb_lock); 737704bac040SSidhartha Kumar if (folio_test_hugetlb(folio)) { 737825182f05SNaoya Horiguchi *hugetlb = true; 737904bac040SSidhartha Kumar if (folio_test_hugetlb_freed(folio)) 7380b283d983SNaoya Horiguchi ret = 0; 738104bac040SSidhartha Kumar else if (folio_test_hugetlb_migratable(folio) || unpoison) 738204bac040SSidhartha Kumar ret = folio_try_get(folio); 73830ed950d1SNaoya Horiguchi else 73840ed950d1SNaoya Horiguchi ret = -EBUSY; 738525182f05SNaoya Horiguchi } 738625182f05SNaoya Horiguchi spin_unlock_irq(&hugetlb_lock); 738725182f05SNaoya Horiguchi return ret; 738825182f05SNaoya Horiguchi } 738925182f05SNaoya Horiguchi 7390e591ef7dSNaoya Horiguchi int get_huge_page_for_hwpoison(unsigned long pfn, int flags, 7391e591ef7dSNaoya Horiguchi bool *migratable_cleared) 7392405ce051SNaoya Horiguchi { 7393405ce051SNaoya Horiguchi int ret; 7394405ce051SNaoya Horiguchi 7395405ce051SNaoya Horiguchi spin_lock_irq(&hugetlb_lock); 7396e591ef7dSNaoya Horiguchi ret = __get_huge_page_for_hwpoison(pfn, flags, migratable_cleared); 7397405ce051SNaoya Horiguchi spin_unlock_irq(&hugetlb_lock); 7398405ce051SNaoya Horiguchi return ret; 7399405ce051SNaoya Horiguchi } 7400405ce051SNaoya Horiguchi 7401ea8e72f4SSidhartha Kumar void folio_putback_active_hugetlb(struct folio *folio) 740231caf665SNaoya Horiguchi { 7403db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 7404ea8e72f4SSidhartha Kumar folio_set_hugetlb_migratable(folio); 7405ea8e72f4SSidhartha Kumar list_move_tail(&folio->lru, &(folio_hstate(folio))->hugepage_activelist); 7406db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 7407ea8e72f4SSidhartha Kumar folio_put(folio); 740831caf665SNaoya Horiguchi } 7409ab5ac90aSMichal Hocko 7410345c62d1SSidhartha Kumar void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason) 7411ab5ac90aSMichal Hocko { 7412345c62d1SSidhartha Kumar struct hstate *h = folio_hstate(old_folio); 7413ab5ac90aSMichal Hocko 7414345c62d1SSidhartha Kumar hugetlb_cgroup_migrate(old_folio, new_folio); 7415345c62d1SSidhartha Kumar set_page_owner_migrate_reason(&new_folio->page, reason); 7416ab5ac90aSMichal Hocko 7417ab5ac90aSMichal Hocko /* 7418345c62d1SSidhartha Kumar * transfer temporary state of the new hugetlb folio. This is 7419ab5ac90aSMichal Hocko * reverse to other transitions because the newpage is going to 7420ab5ac90aSMichal Hocko * be final while the old one will be freed so it takes over 7421ab5ac90aSMichal Hocko * the temporary status. 7422ab5ac90aSMichal Hocko * 7423ab5ac90aSMichal Hocko * Also note that we have to transfer the per-node surplus state 7424ab5ac90aSMichal Hocko * here as well otherwise the global surplus count will not match 7425ab5ac90aSMichal Hocko * the per-node's. 7426ab5ac90aSMichal Hocko */ 7427345c62d1SSidhartha Kumar if (folio_test_hugetlb_temporary(new_folio)) { 7428345c62d1SSidhartha Kumar int old_nid = folio_nid(old_folio); 7429345c62d1SSidhartha Kumar int new_nid = folio_nid(new_folio); 7430ab5ac90aSMichal Hocko 7431345c62d1SSidhartha Kumar folio_set_hugetlb_temporary(old_folio); 7432345c62d1SSidhartha Kumar folio_clear_hugetlb_temporary(new_folio); 7433345c62d1SSidhartha Kumar 7434ab5ac90aSMichal Hocko 74355af1ab1dSMiaohe Lin /* 74365af1ab1dSMiaohe Lin * There is no need to transfer the per-node surplus state 74375af1ab1dSMiaohe Lin * when we do not cross the node. 74385af1ab1dSMiaohe Lin */ 74395af1ab1dSMiaohe Lin if (new_nid == old_nid) 74405af1ab1dSMiaohe Lin return; 7441db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 7442ab5ac90aSMichal Hocko if (h->surplus_huge_pages_node[old_nid]) { 7443ab5ac90aSMichal Hocko h->surplus_huge_pages_node[old_nid]--; 7444ab5ac90aSMichal Hocko h->surplus_huge_pages_node[new_nid]++; 7445ab5ac90aSMichal Hocko } 7446db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 7447ab5ac90aSMichal Hocko } 7448ab5ac90aSMichal Hocko } 7449cf11e85fSRoman Gushchin 7450b30c14cdSJames Houghton static void hugetlb_unshare_pmds(struct vm_area_struct *vma, 7451b30c14cdSJames Houghton unsigned long start, 7452b30c14cdSJames Houghton unsigned long end) 74536dfeaff9SPeter Xu { 74546dfeaff9SPeter Xu struct hstate *h = hstate_vma(vma); 74556dfeaff9SPeter Xu unsigned long sz = huge_page_size(h); 74566dfeaff9SPeter Xu struct mm_struct *mm = vma->vm_mm; 74576dfeaff9SPeter Xu struct mmu_notifier_range range; 7458b30c14cdSJames Houghton unsigned long address; 74596dfeaff9SPeter Xu spinlock_t *ptl; 74606dfeaff9SPeter Xu pte_t *ptep; 74616dfeaff9SPeter Xu 74626dfeaff9SPeter Xu if (!(vma->vm_flags & VM_MAYSHARE)) 74636dfeaff9SPeter Xu return; 74646dfeaff9SPeter Xu 74656dfeaff9SPeter Xu if (start >= end) 74666dfeaff9SPeter Xu return; 74676dfeaff9SPeter Xu 74689c8bbfacSBaolin Wang flush_cache_range(vma, start, end); 74696dfeaff9SPeter Xu /* 74706dfeaff9SPeter Xu * No need to call adjust_range_if_pmd_sharing_possible(), because 74716dfeaff9SPeter Xu * we have already done the PUD_SIZE alignment. 74726dfeaff9SPeter Xu */ 74737d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, 74746dfeaff9SPeter Xu start, end); 74756dfeaff9SPeter Xu mmu_notifier_invalidate_range_start(&range); 747640549ba8SMike Kravetz hugetlb_vma_lock_write(vma); 74776dfeaff9SPeter Xu i_mmap_lock_write(vma->vm_file->f_mapping); 74786dfeaff9SPeter Xu for (address = start; address < end; address += PUD_SIZE) { 74799c67a207SPeter Xu ptep = hugetlb_walk(vma, address, sz); 74806dfeaff9SPeter Xu if (!ptep) 74816dfeaff9SPeter Xu continue; 74826dfeaff9SPeter Xu ptl = huge_pte_lock(h, mm, ptep); 74834ddb4d91SMike Kravetz huge_pmd_unshare(mm, vma, address, ptep); 74846dfeaff9SPeter Xu spin_unlock(ptl); 74856dfeaff9SPeter Xu } 74866dfeaff9SPeter Xu flush_hugetlb_tlb_range(vma, start, end); 74876dfeaff9SPeter Xu i_mmap_unlock_write(vma->vm_file->f_mapping); 748840549ba8SMike Kravetz hugetlb_vma_unlock_write(vma); 74896dfeaff9SPeter Xu /* 74906dfeaff9SPeter Xu * No need to call mmu_notifier_invalidate_range(), see 7491ee65728eSMike Rapoport * Documentation/mm/mmu_notifier.rst. 74926dfeaff9SPeter Xu */ 74936dfeaff9SPeter Xu mmu_notifier_invalidate_range_end(&range); 74946dfeaff9SPeter Xu } 74956dfeaff9SPeter Xu 7496b30c14cdSJames Houghton /* 7497b30c14cdSJames Houghton * This function will unconditionally remove all the shared pmd pgtable entries 7498b30c14cdSJames Houghton * within the specific vma for a hugetlbfs memory range. 7499b30c14cdSJames Houghton */ 7500b30c14cdSJames Houghton void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) 7501b30c14cdSJames Houghton { 7502b30c14cdSJames Houghton hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE), 7503b30c14cdSJames Houghton ALIGN_DOWN(vma->vm_end, PUD_SIZE)); 7504b30c14cdSJames Houghton } 7505b30c14cdSJames Houghton 7506cf11e85fSRoman Gushchin #ifdef CONFIG_CMA 7507cf11e85fSRoman Gushchin static bool cma_reserve_called __initdata; 7508cf11e85fSRoman Gushchin 7509cf11e85fSRoman Gushchin static int __init cmdline_parse_hugetlb_cma(char *p) 7510cf11e85fSRoman Gushchin { 751138e719abSBaolin Wang int nid, count = 0; 751238e719abSBaolin Wang unsigned long tmp; 751338e719abSBaolin Wang char *s = p; 751438e719abSBaolin Wang 751538e719abSBaolin Wang while (*s) { 751638e719abSBaolin Wang if (sscanf(s, "%lu%n", &tmp, &count) != 1) 751738e719abSBaolin Wang break; 751838e719abSBaolin Wang 751938e719abSBaolin Wang if (s[count] == ':') { 7520f9317f77SMike Kravetz if (tmp >= MAX_NUMNODES) 752138e719abSBaolin Wang break; 7522f9317f77SMike Kravetz nid = array_index_nospec(tmp, MAX_NUMNODES); 752338e719abSBaolin Wang 752438e719abSBaolin Wang s += count + 1; 752538e719abSBaolin Wang tmp = memparse(s, &s); 752638e719abSBaolin Wang hugetlb_cma_size_in_node[nid] = tmp; 752738e719abSBaolin Wang hugetlb_cma_size += tmp; 752838e719abSBaolin Wang 752938e719abSBaolin Wang /* 753038e719abSBaolin Wang * Skip the separator if have one, otherwise 753138e719abSBaolin Wang * break the parsing. 753238e719abSBaolin Wang */ 753338e719abSBaolin Wang if (*s == ',') 753438e719abSBaolin Wang s++; 753538e719abSBaolin Wang else 753638e719abSBaolin Wang break; 753738e719abSBaolin Wang } else { 7538cf11e85fSRoman Gushchin hugetlb_cma_size = memparse(p, &p); 753938e719abSBaolin Wang break; 754038e719abSBaolin Wang } 754138e719abSBaolin Wang } 754238e719abSBaolin Wang 7543cf11e85fSRoman Gushchin return 0; 7544cf11e85fSRoman Gushchin } 7545cf11e85fSRoman Gushchin 7546cf11e85fSRoman Gushchin early_param("hugetlb_cma", cmdline_parse_hugetlb_cma); 7547cf11e85fSRoman Gushchin 7548cf11e85fSRoman Gushchin void __init hugetlb_cma_reserve(int order) 7549cf11e85fSRoman Gushchin { 7550cf11e85fSRoman Gushchin unsigned long size, reserved, per_node; 755138e719abSBaolin Wang bool node_specific_cma_alloc = false; 7552cf11e85fSRoman Gushchin int nid; 7553cf11e85fSRoman Gushchin 7554cf11e85fSRoman Gushchin cma_reserve_called = true; 7555cf11e85fSRoman Gushchin 7556cf11e85fSRoman Gushchin if (!hugetlb_cma_size) 7557cf11e85fSRoman Gushchin return; 7558cf11e85fSRoman Gushchin 755938e719abSBaolin Wang for (nid = 0; nid < MAX_NUMNODES; nid++) { 756038e719abSBaolin Wang if (hugetlb_cma_size_in_node[nid] == 0) 756138e719abSBaolin Wang continue; 756238e719abSBaolin Wang 756330a51400SPeng Liu if (!node_online(nid)) { 756438e719abSBaolin Wang pr_warn("hugetlb_cma: invalid node %d specified\n", nid); 756538e719abSBaolin Wang hugetlb_cma_size -= hugetlb_cma_size_in_node[nid]; 756638e719abSBaolin Wang hugetlb_cma_size_in_node[nid] = 0; 756738e719abSBaolin Wang continue; 756838e719abSBaolin Wang } 756938e719abSBaolin Wang 757038e719abSBaolin Wang if (hugetlb_cma_size_in_node[nid] < (PAGE_SIZE << order)) { 757138e719abSBaolin Wang pr_warn("hugetlb_cma: cma area of node %d should be at least %lu MiB\n", 757238e719abSBaolin Wang nid, (PAGE_SIZE << order) / SZ_1M); 757338e719abSBaolin Wang hugetlb_cma_size -= hugetlb_cma_size_in_node[nid]; 757438e719abSBaolin Wang hugetlb_cma_size_in_node[nid] = 0; 757538e719abSBaolin Wang } else { 757638e719abSBaolin Wang node_specific_cma_alloc = true; 757738e719abSBaolin Wang } 757838e719abSBaolin Wang } 757938e719abSBaolin Wang 758038e719abSBaolin Wang /* Validate the CMA size again in case some invalid nodes specified. */ 758138e719abSBaolin Wang if (!hugetlb_cma_size) 758238e719abSBaolin Wang return; 758338e719abSBaolin Wang 7584cf11e85fSRoman Gushchin if (hugetlb_cma_size < (PAGE_SIZE << order)) { 7585cf11e85fSRoman Gushchin pr_warn("hugetlb_cma: cma area should be at least %lu MiB\n", 7586cf11e85fSRoman Gushchin (PAGE_SIZE << order) / SZ_1M); 7587a01f4390SMike Kravetz hugetlb_cma_size = 0; 7588cf11e85fSRoman Gushchin return; 7589cf11e85fSRoman Gushchin } 7590cf11e85fSRoman Gushchin 759138e719abSBaolin Wang if (!node_specific_cma_alloc) { 7592cf11e85fSRoman Gushchin /* 7593cf11e85fSRoman Gushchin * If 3 GB area is requested on a machine with 4 numa nodes, 7594cf11e85fSRoman Gushchin * let's allocate 1 GB on first three nodes and ignore the last one. 7595cf11e85fSRoman Gushchin */ 7596cf11e85fSRoman Gushchin per_node = DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes); 7597cf11e85fSRoman Gushchin pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n", 7598cf11e85fSRoman Gushchin hugetlb_cma_size / SZ_1M, per_node / SZ_1M); 759938e719abSBaolin Wang } 7600cf11e85fSRoman Gushchin 7601cf11e85fSRoman Gushchin reserved = 0; 760230a51400SPeng Liu for_each_online_node(nid) { 7603cf11e85fSRoman Gushchin int res; 76042281f797SBarry Song char name[CMA_MAX_NAME]; 7605cf11e85fSRoman Gushchin 760638e719abSBaolin Wang if (node_specific_cma_alloc) { 760738e719abSBaolin Wang if (hugetlb_cma_size_in_node[nid] == 0) 760838e719abSBaolin Wang continue; 760938e719abSBaolin Wang 761038e719abSBaolin Wang size = hugetlb_cma_size_in_node[nid]; 761138e719abSBaolin Wang } else { 7612cf11e85fSRoman Gushchin size = min(per_node, hugetlb_cma_size - reserved); 761338e719abSBaolin Wang } 761438e719abSBaolin Wang 7615cf11e85fSRoman Gushchin size = round_up(size, PAGE_SIZE << order); 7616cf11e85fSRoman Gushchin 76172281f797SBarry Song snprintf(name, sizeof(name), "hugetlb%d", nid); 7618a01f4390SMike Kravetz /* 7619a01f4390SMike Kravetz * Note that 'order per bit' is based on smallest size that 7620a01f4390SMike Kravetz * may be returned to CMA allocator in the case of 7621a01f4390SMike Kravetz * huge page demotion. 7622a01f4390SMike Kravetz */ 7623a01f4390SMike Kravetz res = cma_declare_contiguous_nid(0, size, 0, 7624a01f4390SMike Kravetz PAGE_SIZE << HUGETLB_PAGE_ORDER, 762529d0f41dSBarry Song 0, false, name, 7626cf11e85fSRoman Gushchin &hugetlb_cma[nid], nid); 7627cf11e85fSRoman Gushchin if (res) { 7628cf11e85fSRoman Gushchin pr_warn("hugetlb_cma: reservation failed: err %d, node %d", 7629cf11e85fSRoman Gushchin res, nid); 7630cf11e85fSRoman Gushchin continue; 7631cf11e85fSRoman Gushchin } 7632cf11e85fSRoman Gushchin 7633cf11e85fSRoman Gushchin reserved += size; 7634cf11e85fSRoman Gushchin pr_info("hugetlb_cma: reserved %lu MiB on node %d\n", 7635cf11e85fSRoman Gushchin size / SZ_1M, nid); 7636cf11e85fSRoman Gushchin 7637cf11e85fSRoman Gushchin if (reserved >= hugetlb_cma_size) 7638cf11e85fSRoman Gushchin break; 7639cf11e85fSRoman Gushchin } 7640a01f4390SMike Kravetz 7641a01f4390SMike Kravetz if (!reserved) 7642a01f4390SMike Kravetz /* 7643a01f4390SMike Kravetz * hugetlb_cma_size is used to determine if allocations from 7644a01f4390SMike Kravetz * cma are possible. Set to zero if no cma regions are set up. 7645a01f4390SMike Kravetz */ 7646a01f4390SMike Kravetz hugetlb_cma_size = 0; 7647cf11e85fSRoman Gushchin } 7648cf11e85fSRoman Gushchin 7649263b8998SMiaohe Lin static void __init hugetlb_cma_check(void) 7650cf11e85fSRoman Gushchin { 7651cf11e85fSRoman Gushchin if (!hugetlb_cma_size || cma_reserve_called) 7652cf11e85fSRoman Gushchin return; 7653cf11e85fSRoman Gushchin 7654cf11e85fSRoman Gushchin pr_warn("hugetlb_cma: the option isn't supported by current arch\n"); 7655cf11e85fSRoman Gushchin } 7656cf11e85fSRoman Gushchin 7657cf11e85fSRoman Gushchin #endif /* CONFIG_CMA */ 7658