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 static bool __vma_shareable_lock(struct vm_area_struct *vma) 264e700898fSMike Kravetz { 265379c2e60SMike Kravetz return vma->vm_flags & VM_MAYSHARE && vma->vm_private_data; 266e700898fSMike Kravetz } 267e700898fSMike Kravetz 268e700898fSMike Kravetz void hugetlb_vma_lock_read(struct vm_area_struct *vma) 269e700898fSMike Kravetz { 270e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 271e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 272e700898fSMike Kravetz 273e700898fSMike Kravetz down_read(&vma_lock->rw_sema); 274e700898fSMike Kravetz } 275e700898fSMike Kravetz } 276e700898fSMike Kravetz 277e700898fSMike Kravetz void hugetlb_vma_unlock_read(struct vm_area_struct *vma) 278e700898fSMike Kravetz { 279e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 280e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 281e700898fSMike Kravetz 282e700898fSMike Kravetz up_read(&vma_lock->rw_sema); 283e700898fSMike Kravetz } 284e700898fSMike Kravetz } 285e700898fSMike Kravetz 286e700898fSMike Kravetz void hugetlb_vma_lock_write(struct vm_area_struct *vma) 287e700898fSMike Kravetz { 288e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 289e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 290e700898fSMike Kravetz 291e700898fSMike Kravetz down_write(&vma_lock->rw_sema); 292e700898fSMike Kravetz } 293e700898fSMike Kravetz } 294e700898fSMike Kravetz 295e700898fSMike Kravetz void hugetlb_vma_unlock_write(struct vm_area_struct *vma) 296e700898fSMike Kravetz { 297e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 298e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 299e700898fSMike Kravetz 300e700898fSMike Kravetz up_write(&vma_lock->rw_sema); 301e700898fSMike Kravetz } 302e700898fSMike Kravetz } 303e700898fSMike Kravetz 304e700898fSMike Kravetz int hugetlb_vma_trylock_write(struct vm_area_struct *vma) 305e700898fSMike Kravetz { 306e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 307e700898fSMike Kravetz 308e700898fSMike Kravetz if (!__vma_shareable_lock(vma)) 309e700898fSMike Kravetz return 1; 310e700898fSMike Kravetz 311e700898fSMike Kravetz return down_write_trylock(&vma_lock->rw_sema); 312e700898fSMike Kravetz } 313e700898fSMike Kravetz 314e700898fSMike Kravetz void hugetlb_vma_assert_locked(struct vm_area_struct *vma) 315e700898fSMike Kravetz { 316e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 317e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 318e700898fSMike Kravetz 319e700898fSMike Kravetz lockdep_assert_held(&vma_lock->rw_sema); 320e700898fSMike Kravetz } 321e700898fSMike Kravetz } 322e700898fSMike Kravetz 323e700898fSMike Kravetz void hugetlb_vma_lock_release(struct kref *kref) 324e700898fSMike Kravetz { 325e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = container_of(kref, 326e700898fSMike Kravetz struct hugetlb_vma_lock, refs); 327e700898fSMike Kravetz 328e700898fSMike Kravetz kfree(vma_lock); 329e700898fSMike Kravetz } 330e700898fSMike Kravetz 331e700898fSMike Kravetz static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock) 332e700898fSMike Kravetz { 333e700898fSMike Kravetz struct vm_area_struct *vma = vma_lock->vma; 334e700898fSMike Kravetz 335e700898fSMike Kravetz /* 336e700898fSMike Kravetz * vma_lock structure may or not be released as a result of put, 337e700898fSMike Kravetz * it certainly will no longer be attached to vma so clear pointer. 338e700898fSMike Kravetz * Semaphore synchronizes access to vma_lock->vma field. 339e700898fSMike Kravetz */ 340e700898fSMike Kravetz vma_lock->vma = NULL; 341e700898fSMike Kravetz vma->vm_private_data = NULL; 342e700898fSMike Kravetz up_write(&vma_lock->rw_sema); 343e700898fSMike Kravetz kref_put(&vma_lock->refs, hugetlb_vma_lock_release); 344e700898fSMike Kravetz } 345e700898fSMike Kravetz 346e700898fSMike Kravetz static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma) 347e700898fSMike Kravetz { 348e700898fSMike Kravetz if (__vma_shareable_lock(vma)) { 349e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 350e700898fSMike Kravetz 351e700898fSMike Kravetz __hugetlb_vma_unlock_write_put(vma_lock); 352e700898fSMike Kravetz } 353e700898fSMike Kravetz } 354e700898fSMike Kravetz 355e700898fSMike Kravetz static void hugetlb_vma_lock_free(struct vm_area_struct *vma) 356e700898fSMike Kravetz { 357e700898fSMike Kravetz /* 358e700898fSMike Kravetz * Only present in sharable vmas. 359e700898fSMike Kravetz */ 360e700898fSMike Kravetz if (!vma || !__vma_shareable_lock(vma)) 361e700898fSMike Kravetz return; 362e700898fSMike Kravetz 363e700898fSMike Kravetz if (vma->vm_private_data) { 364e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 365e700898fSMike Kravetz 366e700898fSMike Kravetz down_write(&vma_lock->rw_sema); 367e700898fSMike Kravetz __hugetlb_vma_unlock_write_put(vma_lock); 368e700898fSMike Kravetz } 369e700898fSMike Kravetz } 370e700898fSMike Kravetz 371e700898fSMike Kravetz static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma) 372e700898fSMike Kravetz { 373e700898fSMike Kravetz struct hugetlb_vma_lock *vma_lock; 374e700898fSMike Kravetz 375e700898fSMike Kravetz /* Only establish in (flags) sharable vmas */ 376e700898fSMike Kravetz if (!vma || !(vma->vm_flags & VM_MAYSHARE)) 377e700898fSMike Kravetz return; 378e700898fSMike Kravetz 379e700898fSMike Kravetz /* Should never get here with non-NULL vm_private_data */ 380e700898fSMike Kravetz if (vma->vm_private_data) 381e700898fSMike Kravetz return; 382e700898fSMike Kravetz 383e700898fSMike Kravetz vma_lock = kmalloc(sizeof(*vma_lock), GFP_KERNEL); 384e700898fSMike Kravetz if (!vma_lock) { 385e700898fSMike Kravetz /* 386e700898fSMike Kravetz * If we can not allocate structure, then vma can not 387e700898fSMike Kravetz * participate in pmd sharing. This is only a possible 388e700898fSMike Kravetz * performance enhancement and memory saving issue. 389e700898fSMike Kravetz * However, the lock is also used to synchronize page 390e700898fSMike Kravetz * faults with truncation. If the lock is not present, 391e700898fSMike Kravetz * unlikely races could leave pages in a file past i_size 392e700898fSMike Kravetz * until the file is removed. Warn in the unlikely case of 393e700898fSMike Kravetz * allocation failure. 394e700898fSMike Kravetz */ 395e700898fSMike Kravetz pr_warn_once("HugeTLB: unable to allocate vma specific lock\n"); 396e700898fSMike Kravetz return; 397e700898fSMike Kravetz } 398e700898fSMike Kravetz 399e700898fSMike Kravetz kref_init(&vma_lock->refs); 400e700898fSMike Kravetz init_rwsem(&vma_lock->rw_sema); 401e700898fSMike Kravetz vma_lock->vma = vma; 402e700898fSMike Kravetz vma->vm_private_data = vma_lock; 403e700898fSMike Kravetz } 404e700898fSMike Kravetz 4050db9d74eSMina Almasry /* Helper that removes a struct file_region from the resv_map cache and returns 4060db9d74eSMina Almasry * it for use. 4070db9d74eSMina Almasry */ 4080db9d74eSMina Almasry static struct file_region * 4090db9d74eSMina Almasry get_file_region_entry_from_cache(struct resv_map *resv, long from, long to) 4100db9d74eSMina Almasry { 4113259914fSXU pengfei struct file_region *nrg; 4120db9d74eSMina Almasry 4130db9d74eSMina Almasry VM_BUG_ON(resv->region_cache_count <= 0); 4140db9d74eSMina Almasry 4150db9d74eSMina Almasry resv->region_cache_count--; 4160db9d74eSMina Almasry nrg = list_first_entry(&resv->region_cache, struct file_region, link); 4170db9d74eSMina Almasry list_del(&nrg->link); 4180db9d74eSMina Almasry 4190db9d74eSMina Almasry nrg->from = from; 4200db9d74eSMina Almasry nrg->to = to; 4210db9d74eSMina Almasry 4220db9d74eSMina Almasry return nrg; 4230db9d74eSMina Almasry } 4240db9d74eSMina Almasry 425075a61d0SMina Almasry static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg, 426075a61d0SMina Almasry struct file_region *rg) 427075a61d0SMina Almasry { 428075a61d0SMina Almasry #ifdef CONFIG_CGROUP_HUGETLB 429075a61d0SMina Almasry nrg->reservation_counter = rg->reservation_counter; 430075a61d0SMina Almasry nrg->css = rg->css; 431075a61d0SMina Almasry if (rg->css) 432075a61d0SMina Almasry css_get(rg->css); 433075a61d0SMina Almasry #endif 434075a61d0SMina Almasry } 435075a61d0SMina Almasry 436075a61d0SMina Almasry /* Helper that records hugetlb_cgroup uncharge info. */ 437075a61d0SMina Almasry static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg, 438075a61d0SMina Almasry struct hstate *h, 439075a61d0SMina Almasry struct resv_map *resv, 440075a61d0SMina Almasry struct file_region *nrg) 441075a61d0SMina Almasry { 442075a61d0SMina Almasry #ifdef CONFIG_CGROUP_HUGETLB 443075a61d0SMina Almasry if (h_cg) { 444075a61d0SMina Almasry nrg->reservation_counter = 445075a61d0SMina Almasry &h_cg->rsvd_hugepage[hstate_index(h)]; 446075a61d0SMina Almasry nrg->css = &h_cg->css; 447d85aecf2SMiaohe Lin /* 448d85aecf2SMiaohe Lin * The caller will hold exactly one h_cg->css reference for the 449d85aecf2SMiaohe Lin * whole contiguous reservation region. But this area might be 450d85aecf2SMiaohe Lin * scattered when there are already some file_regions reside in 451d85aecf2SMiaohe Lin * it. As a result, many file_regions may share only one css 452d85aecf2SMiaohe Lin * reference. In order to ensure that one file_region must hold 453d85aecf2SMiaohe Lin * exactly one h_cg->css reference, we should do css_get for 454d85aecf2SMiaohe Lin * each file_region and leave the reference held by caller 455d85aecf2SMiaohe Lin * untouched. 456d85aecf2SMiaohe Lin */ 457d85aecf2SMiaohe Lin css_get(&h_cg->css); 458075a61d0SMina Almasry if (!resv->pages_per_hpage) 459075a61d0SMina Almasry resv->pages_per_hpage = pages_per_huge_page(h); 460075a61d0SMina Almasry /* pages_per_hpage should be the same for all entries in 461075a61d0SMina Almasry * a resv_map. 462075a61d0SMina Almasry */ 463075a61d0SMina Almasry VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h)); 464075a61d0SMina Almasry } else { 465075a61d0SMina Almasry nrg->reservation_counter = NULL; 466075a61d0SMina Almasry nrg->css = NULL; 467075a61d0SMina Almasry } 468075a61d0SMina Almasry #endif 469075a61d0SMina Almasry } 470075a61d0SMina Almasry 471d85aecf2SMiaohe Lin static void put_uncharge_info(struct file_region *rg) 472d85aecf2SMiaohe Lin { 473d85aecf2SMiaohe Lin #ifdef CONFIG_CGROUP_HUGETLB 474d85aecf2SMiaohe Lin if (rg->css) 475d85aecf2SMiaohe Lin css_put(rg->css); 476d85aecf2SMiaohe Lin #endif 477d85aecf2SMiaohe Lin } 478d85aecf2SMiaohe Lin 479a9b3f867SMina Almasry static bool has_same_uncharge_info(struct file_region *rg, 480a9b3f867SMina Almasry struct file_region *org) 481a9b3f867SMina Almasry { 482a9b3f867SMina Almasry #ifdef CONFIG_CGROUP_HUGETLB 4830739eb43SBaolin Wang return rg->reservation_counter == org->reservation_counter && 484a9b3f867SMina Almasry rg->css == org->css; 485a9b3f867SMina Almasry 486a9b3f867SMina Almasry #else 487a9b3f867SMina Almasry return true; 488a9b3f867SMina Almasry #endif 489a9b3f867SMina Almasry } 490a9b3f867SMina Almasry 491a9b3f867SMina Almasry static void coalesce_file_region(struct resv_map *resv, struct file_region *rg) 492a9b3f867SMina Almasry { 4933259914fSXU pengfei struct file_region *nrg, *prg; 494a9b3f867SMina Almasry 495a9b3f867SMina Almasry prg = list_prev_entry(rg, link); 496a9b3f867SMina Almasry if (&prg->link != &resv->regions && prg->to == rg->from && 497a9b3f867SMina Almasry has_same_uncharge_info(prg, rg)) { 498a9b3f867SMina Almasry prg->to = rg->to; 499a9b3f867SMina Almasry 500a9b3f867SMina Almasry list_del(&rg->link); 501d85aecf2SMiaohe Lin put_uncharge_info(rg); 502a9b3f867SMina Almasry kfree(rg); 503a9b3f867SMina Almasry 5047db5e7b6SWei Yang rg = prg; 505a9b3f867SMina Almasry } 506a9b3f867SMina Almasry 507a9b3f867SMina Almasry nrg = list_next_entry(rg, link); 508a9b3f867SMina Almasry if (&nrg->link != &resv->regions && nrg->from == rg->to && 509a9b3f867SMina Almasry has_same_uncharge_info(nrg, rg)) { 510a9b3f867SMina Almasry nrg->from = rg->from; 511a9b3f867SMina Almasry 512a9b3f867SMina Almasry list_del(&rg->link); 513d85aecf2SMiaohe Lin put_uncharge_info(rg); 514a9b3f867SMina Almasry kfree(rg); 515a9b3f867SMina Almasry } 516a9b3f867SMina Almasry } 517a9b3f867SMina Almasry 5182103cf9cSPeter Xu static inline long 51984448c8eSJakob Koschel hugetlb_resv_map_add(struct resv_map *map, struct list_head *rg, long from, 5202103cf9cSPeter Xu long to, struct hstate *h, struct hugetlb_cgroup *cg, 5212103cf9cSPeter Xu long *regions_needed) 5222103cf9cSPeter Xu { 5232103cf9cSPeter Xu struct file_region *nrg; 5242103cf9cSPeter Xu 5252103cf9cSPeter Xu if (!regions_needed) { 5262103cf9cSPeter Xu nrg = get_file_region_entry_from_cache(map, from, to); 5272103cf9cSPeter Xu record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg); 52884448c8eSJakob Koschel list_add(&nrg->link, rg); 5292103cf9cSPeter Xu coalesce_file_region(map, nrg); 5302103cf9cSPeter Xu } else 5312103cf9cSPeter Xu *regions_needed += 1; 5322103cf9cSPeter Xu 5332103cf9cSPeter Xu return to - from; 5342103cf9cSPeter Xu } 5352103cf9cSPeter Xu 536972a3da3SWei Yang /* 537972a3da3SWei Yang * Must be called with resv->lock held. 538972a3da3SWei Yang * 539972a3da3SWei Yang * Calling this with regions_needed != NULL will count the number of pages 540972a3da3SWei Yang * to be added but will not modify the linked list. And regions_needed will 541972a3da3SWei Yang * indicate the number of file_regions needed in the cache to carry out to add 542972a3da3SWei Yang * the regions for this range. 543d75c6af9SMina Almasry */ 544d75c6af9SMina Almasry static long add_reservation_in_range(struct resv_map *resv, long f, long t, 545075a61d0SMina Almasry struct hugetlb_cgroup *h_cg, 546972a3da3SWei Yang struct hstate *h, long *regions_needed) 547d75c6af9SMina Almasry { 5480db9d74eSMina Almasry long add = 0; 549d75c6af9SMina Almasry struct list_head *head = &resv->regions; 5500db9d74eSMina Almasry long last_accounted_offset = f; 55184448c8eSJakob Koschel struct file_region *iter, *trg = NULL; 55284448c8eSJakob Koschel struct list_head *rg = NULL; 553d75c6af9SMina Almasry 5540db9d74eSMina Almasry if (regions_needed) 5550db9d74eSMina Almasry *regions_needed = 0; 556d75c6af9SMina Almasry 5570db9d74eSMina Almasry /* In this loop, we essentially handle an entry for the range 55884448c8eSJakob Koschel * [last_accounted_offset, iter->from), at every iteration, with some 5590db9d74eSMina Almasry * bounds checking. 5600db9d74eSMina Almasry */ 56184448c8eSJakob Koschel list_for_each_entry_safe(iter, trg, head, link) { 5620db9d74eSMina Almasry /* Skip irrelevant regions that start before our range. */ 56384448c8eSJakob Koschel if (iter->from < f) { 5640db9d74eSMina Almasry /* If this region ends after the last accounted offset, 5650db9d74eSMina Almasry * then we need to update last_accounted_offset. 5660db9d74eSMina Almasry */ 56784448c8eSJakob Koschel if (iter->to > last_accounted_offset) 56884448c8eSJakob Koschel last_accounted_offset = iter->to; 5690db9d74eSMina Almasry continue; 5700db9d74eSMina Almasry } 571d75c6af9SMina Almasry 5720db9d74eSMina Almasry /* When we find a region that starts beyond our range, we've 5730db9d74eSMina Almasry * finished. 5740db9d74eSMina Almasry */ 57584448c8eSJakob Koschel if (iter->from >= t) { 57684448c8eSJakob Koschel rg = iter->link.prev; 577d75c6af9SMina Almasry break; 57884448c8eSJakob Koschel } 579d75c6af9SMina Almasry 58084448c8eSJakob Koschel /* Add an entry for last_accounted_offset -> iter->from, and 5810db9d74eSMina Almasry * update last_accounted_offset. 582d75c6af9SMina Almasry */ 58384448c8eSJakob Koschel if (iter->from > last_accounted_offset) 58484448c8eSJakob Koschel add += hugetlb_resv_map_add(resv, iter->link.prev, 5852103cf9cSPeter Xu last_accounted_offset, 58684448c8eSJakob Koschel iter->from, h, h_cg, 5872103cf9cSPeter Xu regions_needed); 588d75c6af9SMina Almasry 58984448c8eSJakob Koschel last_accounted_offset = iter->to; 5900db9d74eSMina Almasry } 5910db9d74eSMina Almasry 5920db9d74eSMina Almasry /* Handle the case where our range extends beyond 5930db9d74eSMina Almasry * last_accounted_offset. 5940db9d74eSMina Almasry */ 59584448c8eSJakob Koschel if (!rg) 59684448c8eSJakob Koschel rg = head->prev; 5972103cf9cSPeter Xu if (last_accounted_offset < t) 5982103cf9cSPeter Xu add += hugetlb_resv_map_add(resv, rg, last_accounted_offset, 5992103cf9cSPeter Xu t, h, h_cg, regions_needed); 6000db9d74eSMina Almasry 6010db9d74eSMina Almasry return add; 6020db9d74eSMina Almasry } 6030db9d74eSMina Almasry 6040db9d74eSMina Almasry /* Must be called with resv->lock acquired. Will drop lock to allocate entries. 6050db9d74eSMina Almasry */ 6060db9d74eSMina Almasry static int allocate_file_region_entries(struct resv_map *resv, 6070db9d74eSMina Almasry int regions_needed) 6080db9d74eSMina Almasry __must_hold(&resv->lock) 6090db9d74eSMina Almasry { 61034665341SMiaohe Lin LIST_HEAD(allocated_regions); 6110db9d74eSMina Almasry int to_allocate = 0, i = 0; 6120db9d74eSMina Almasry struct file_region *trg = NULL, *rg = NULL; 6130db9d74eSMina Almasry 6140db9d74eSMina Almasry VM_BUG_ON(regions_needed < 0); 6150db9d74eSMina Almasry 6160db9d74eSMina Almasry /* 6170db9d74eSMina Almasry * Check for sufficient descriptors in the cache to accommodate 6180db9d74eSMina Almasry * the number of in progress add operations plus regions_needed. 6190db9d74eSMina Almasry * 6200db9d74eSMina Almasry * This is a while loop because when we drop the lock, some other call 6210db9d74eSMina Almasry * to region_add or region_del may have consumed some region_entries, 6220db9d74eSMina Almasry * so we keep looping here until we finally have enough entries for 6230db9d74eSMina Almasry * (adds_in_progress + regions_needed). 6240db9d74eSMina Almasry */ 6250db9d74eSMina Almasry while (resv->region_cache_count < 6260db9d74eSMina Almasry (resv->adds_in_progress + regions_needed)) { 6270db9d74eSMina Almasry to_allocate = resv->adds_in_progress + regions_needed - 6280db9d74eSMina Almasry resv->region_cache_count; 6290db9d74eSMina Almasry 6300db9d74eSMina Almasry /* At this point, we should have enough entries in the cache 631f0953a1bSIngo Molnar * for all the existing adds_in_progress. We should only be 6320db9d74eSMina Almasry * needing to allocate for regions_needed. 6330db9d74eSMina Almasry */ 6340db9d74eSMina Almasry VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress); 6350db9d74eSMina Almasry 6360db9d74eSMina Almasry spin_unlock(&resv->lock); 6370db9d74eSMina Almasry for (i = 0; i < to_allocate; i++) { 6380db9d74eSMina Almasry trg = kmalloc(sizeof(*trg), GFP_KERNEL); 6390db9d74eSMina Almasry if (!trg) 6400db9d74eSMina Almasry goto out_of_memory; 6410db9d74eSMina Almasry list_add(&trg->link, &allocated_regions); 6420db9d74eSMina Almasry } 6430db9d74eSMina Almasry 6440db9d74eSMina Almasry spin_lock(&resv->lock); 6450db9d74eSMina Almasry 646d3ec7b6eSWei Yang list_splice(&allocated_regions, &resv->region_cache); 647d3ec7b6eSWei Yang resv->region_cache_count += to_allocate; 6480db9d74eSMina Almasry } 6490db9d74eSMina Almasry 6500db9d74eSMina Almasry return 0; 6510db9d74eSMina Almasry 6520db9d74eSMina Almasry out_of_memory: 6530db9d74eSMina Almasry list_for_each_entry_safe(rg, trg, &allocated_regions, link) { 654d75c6af9SMina Almasry list_del(&rg->link); 655d75c6af9SMina Almasry kfree(rg); 656d75c6af9SMina Almasry } 6570db9d74eSMina Almasry return -ENOMEM; 658d75c6af9SMina Almasry } 659d75c6af9SMina Almasry 6601dd308a7SMike Kravetz /* 6611dd308a7SMike Kravetz * Add the huge page range represented by [f, t) to the reserve 6620db9d74eSMina Almasry * map. Regions will be taken from the cache to fill in this range. 6630db9d74eSMina Almasry * Sufficient regions should exist in the cache due to the previous 6640db9d74eSMina Almasry * call to region_chg with the same range, but in some cases the cache will not 6650db9d74eSMina Almasry * have sufficient entries due to races with other code doing region_add or 6660db9d74eSMina Almasry * region_del. The extra needed entries will be allocated. 667cf3ad20bSMike Kravetz * 6680db9d74eSMina Almasry * regions_needed is the out value provided by a previous call to region_chg. 6690db9d74eSMina Almasry * 6700db9d74eSMina Almasry * Return the number of new huge pages added to the map. This number is greater 6710db9d74eSMina Almasry * than or equal to zero. If file_region entries needed to be allocated for 6727c8de358SEthon Paul * this operation and we were not able to allocate, it returns -ENOMEM. 6730db9d74eSMina Almasry * region_add of regions of length 1 never allocate file_regions and cannot 6740db9d74eSMina Almasry * fail; region_chg will always allocate at least 1 entry and a region_add for 6750db9d74eSMina Almasry * 1 page will only require at most 1 entry. 6761dd308a7SMike Kravetz */ 6770db9d74eSMina Almasry static long region_add(struct resv_map *resv, long f, long t, 678075a61d0SMina Almasry long in_regions_needed, struct hstate *h, 679075a61d0SMina Almasry struct hugetlb_cgroup *h_cg) 68096822904SAndy Whitcroft { 6810db9d74eSMina Almasry long add = 0, actual_regions_needed = 0; 68296822904SAndy Whitcroft 6837b24d861SDavidlohr Bueso spin_lock(&resv->lock); 6840db9d74eSMina Almasry retry: 6850db9d74eSMina Almasry 6860db9d74eSMina Almasry /* Count how many regions are actually needed to execute this add. */ 687972a3da3SWei Yang add_reservation_in_range(resv, f, t, NULL, NULL, 688972a3da3SWei Yang &actual_regions_needed); 68996822904SAndy Whitcroft 6905e911373SMike Kravetz /* 6910db9d74eSMina Almasry * Check for sufficient descriptors in the cache to accommodate 6920db9d74eSMina Almasry * this add operation. Note that actual_regions_needed may be greater 6930db9d74eSMina Almasry * than in_regions_needed, as the resv_map may have been modified since 6940db9d74eSMina Almasry * the region_chg call. In this case, we need to make sure that we 6950db9d74eSMina Almasry * allocate extra entries, such that we have enough for all the 6960db9d74eSMina Almasry * existing adds_in_progress, plus the excess needed for this 6970db9d74eSMina Almasry * operation. 6985e911373SMike Kravetz */ 6990db9d74eSMina Almasry if (actual_regions_needed > in_regions_needed && 7000db9d74eSMina Almasry resv->region_cache_count < 7010db9d74eSMina Almasry resv->adds_in_progress + 7020db9d74eSMina Almasry (actual_regions_needed - in_regions_needed)) { 7030db9d74eSMina Almasry /* region_add operation of range 1 should never need to 7040db9d74eSMina Almasry * allocate file_region entries. 7050db9d74eSMina Almasry */ 7060db9d74eSMina Almasry VM_BUG_ON(t - f <= 1); 7075e911373SMike Kravetz 7080db9d74eSMina Almasry if (allocate_file_region_entries( 7090db9d74eSMina Almasry resv, actual_regions_needed - in_regions_needed)) { 7100db9d74eSMina Almasry return -ENOMEM; 7115e911373SMike Kravetz } 7125e911373SMike Kravetz 7130db9d74eSMina Almasry goto retry; 7140db9d74eSMina Almasry } 715cf3ad20bSMike Kravetz 716972a3da3SWei Yang add = add_reservation_in_range(resv, f, t, h_cg, h, NULL); 7170db9d74eSMina Almasry 7180db9d74eSMina Almasry resv->adds_in_progress -= in_regions_needed; 7190db9d74eSMina Almasry 7207b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 721cf3ad20bSMike Kravetz return add; 72296822904SAndy Whitcroft } 72396822904SAndy Whitcroft 7241dd308a7SMike Kravetz /* 7251dd308a7SMike Kravetz * Examine the existing reserve map and determine how many 7261dd308a7SMike Kravetz * huge pages in the specified range [f, t) are NOT currently 7271dd308a7SMike Kravetz * represented. This routine is called before a subsequent 7281dd308a7SMike Kravetz * call to region_add that will actually modify the reserve 7291dd308a7SMike Kravetz * map to add the specified range [f, t). region_chg does 7301dd308a7SMike Kravetz * not change the number of huge pages represented by the 7310db9d74eSMina Almasry * map. A number of new file_region structures is added to the cache as a 7320db9d74eSMina Almasry * placeholder, for the subsequent region_add call to use. At least 1 7330db9d74eSMina Almasry * file_region structure is added. 7340db9d74eSMina Almasry * 7350db9d74eSMina Almasry * out_regions_needed is the number of regions added to the 7360db9d74eSMina Almasry * resv->adds_in_progress. This value needs to be provided to a follow up call 7370db9d74eSMina Almasry * to region_add or region_abort for proper accounting. 7385e911373SMike Kravetz * 7395e911373SMike Kravetz * Returns the number of huge pages that need to be added to the existing 7405e911373SMike Kravetz * reservation map for the range [f, t). This number is greater or equal to 7415e911373SMike Kravetz * zero. -ENOMEM is returned if a new file_region structure or cache entry 7425e911373SMike Kravetz * is needed and can not be allocated. 7431dd308a7SMike Kravetz */ 7440db9d74eSMina Almasry static long region_chg(struct resv_map *resv, long f, long t, 7450db9d74eSMina Almasry long *out_regions_needed) 74696822904SAndy Whitcroft { 74796822904SAndy Whitcroft long chg = 0; 74896822904SAndy Whitcroft 7497b24d861SDavidlohr Bueso spin_lock(&resv->lock); 7505e911373SMike Kravetz 751972a3da3SWei Yang /* Count how many hugepages in this range are NOT represented. */ 752075a61d0SMina Almasry chg = add_reservation_in_range(resv, f, t, NULL, NULL, 753972a3da3SWei Yang out_regions_needed); 7545e911373SMike Kravetz 7550db9d74eSMina Almasry if (*out_regions_needed == 0) 7560db9d74eSMina Almasry *out_regions_needed = 1; 7575e911373SMike Kravetz 7580db9d74eSMina Almasry if (allocate_file_region_entries(resv, *out_regions_needed)) 7595e911373SMike Kravetz return -ENOMEM; 7605e911373SMike Kravetz 7610db9d74eSMina Almasry resv->adds_in_progress += *out_regions_needed; 76296822904SAndy Whitcroft 7637b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 76496822904SAndy Whitcroft return chg; 76596822904SAndy Whitcroft } 76696822904SAndy Whitcroft 7671dd308a7SMike Kravetz /* 7685e911373SMike Kravetz * Abort the in progress add operation. The adds_in_progress field 7695e911373SMike Kravetz * of the resv_map keeps track of the operations in progress between 7705e911373SMike Kravetz * calls to region_chg and region_add. Operations are sometimes 7715e911373SMike Kravetz * aborted after the call to region_chg. In such cases, region_abort 7720db9d74eSMina Almasry * is called to decrement the adds_in_progress counter. regions_needed 7730db9d74eSMina Almasry * is the value returned by the region_chg call, it is used to decrement 7740db9d74eSMina Almasry * the adds_in_progress counter. 7755e911373SMike Kravetz * 7765e911373SMike Kravetz * NOTE: The range arguments [f, t) are not needed or used in this 7775e911373SMike Kravetz * routine. They are kept to make reading the calling code easier as 7785e911373SMike Kravetz * arguments will match the associated region_chg call. 7795e911373SMike Kravetz */ 7800db9d74eSMina Almasry static void region_abort(struct resv_map *resv, long f, long t, 7810db9d74eSMina Almasry long regions_needed) 7825e911373SMike Kravetz { 7835e911373SMike Kravetz spin_lock(&resv->lock); 7845e911373SMike Kravetz VM_BUG_ON(!resv->region_cache_count); 7850db9d74eSMina Almasry resv->adds_in_progress -= regions_needed; 7865e911373SMike Kravetz spin_unlock(&resv->lock); 7875e911373SMike Kravetz } 7885e911373SMike Kravetz 7895e911373SMike Kravetz /* 790feba16e2SMike Kravetz * Delete the specified range [f, t) from the reserve map. If the 791feba16e2SMike Kravetz * t parameter is LONG_MAX, this indicates that ALL regions after f 792feba16e2SMike Kravetz * should be deleted. Locate the regions which intersect [f, t) 793feba16e2SMike Kravetz * and either trim, delete or split the existing regions. 794feba16e2SMike Kravetz * 795feba16e2SMike Kravetz * Returns the number of huge pages deleted from the reserve map. 796feba16e2SMike Kravetz * In the normal case, the return value is zero or more. In the 797feba16e2SMike Kravetz * case where a region must be split, a new region descriptor must 798feba16e2SMike Kravetz * be allocated. If the allocation fails, -ENOMEM will be returned. 799feba16e2SMike Kravetz * NOTE: If the parameter t == LONG_MAX, then we will never split 800feba16e2SMike Kravetz * a region and possibly return -ENOMEM. Callers specifying 801feba16e2SMike Kravetz * t == LONG_MAX do not need to check for -ENOMEM error. 8021dd308a7SMike Kravetz */ 803feba16e2SMike Kravetz static long region_del(struct resv_map *resv, long f, long t) 80496822904SAndy Whitcroft { 8051406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 80696822904SAndy Whitcroft struct file_region *rg, *trg; 807feba16e2SMike Kravetz struct file_region *nrg = NULL; 808feba16e2SMike Kravetz long del = 0; 80996822904SAndy Whitcroft 810feba16e2SMike Kravetz retry: 8117b24d861SDavidlohr Bueso spin_lock(&resv->lock); 812feba16e2SMike Kravetz list_for_each_entry_safe(rg, trg, head, link) { 813dbe409e4SMike Kravetz /* 814dbe409e4SMike Kravetz * Skip regions before the range to be deleted. file_region 815dbe409e4SMike Kravetz * ranges are normally of the form [from, to). However, there 816dbe409e4SMike Kravetz * may be a "placeholder" entry in the map which is of the form 817dbe409e4SMike Kravetz * (from, to) with from == to. Check for placeholder entries 818dbe409e4SMike Kravetz * at the beginning of the range to be deleted. 819dbe409e4SMike Kravetz */ 820dbe409e4SMike Kravetz if (rg->to <= f && (rg->to != rg->from || rg->to != f)) 821feba16e2SMike Kravetz continue; 822dbe409e4SMike Kravetz 823feba16e2SMike Kravetz if (rg->from >= t) 82496822904SAndy Whitcroft break; 82596822904SAndy Whitcroft 826feba16e2SMike Kravetz if (f > rg->from && t < rg->to) { /* Must split region */ 827feba16e2SMike Kravetz /* 828feba16e2SMike Kravetz * Check for an entry in the cache before dropping 829feba16e2SMike Kravetz * lock and attempting allocation. 830feba16e2SMike Kravetz */ 831feba16e2SMike Kravetz if (!nrg && 832feba16e2SMike Kravetz resv->region_cache_count > resv->adds_in_progress) { 833feba16e2SMike Kravetz nrg = list_first_entry(&resv->region_cache, 834feba16e2SMike Kravetz struct file_region, 835feba16e2SMike Kravetz link); 836feba16e2SMike Kravetz list_del(&nrg->link); 837feba16e2SMike Kravetz resv->region_cache_count--; 83896822904SAndy Whitcroft } 83996822904SAndy Whitcroft 840feba16e2SMike Kravetz if (!nrg) { 841feba16e2SMike Kravetz spin_unlock(&resv->lock); 842feba16e2SMike Kravetz nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 843feba16e2SMike Kravetz if (!nrg) 844feba16e2SMike Kravetz return -ENOMEM; 845feba16e2SMike Kravetz goto retry; 846feba16e2SMike Kravetz } 847feba16e2SMike Kravetz 848feba16e2SMike Kravetz del += t - f; 84979aa925bSMike Kravetz hugetlb_cgroup_uncharge_file_region( 850d85aecf2SMiaohe Lin resv, rg, t - f, false); 851feba16e2SMike Kravetz 852feba16e2SMike Kravetz /* New entry for end of split region */ 853feba16e2SMike Kravetz nrg->from = t; 854feba16e2SMike Kravetz nrg->to = rg->to; 855075a61d0SMina Almasry 856075a61d0SMina Almasry copy_hugetlb_cgroup_uncharge_info(nrg, rg); 857075a61d0SMina Almasry 858feba16e2SMike Kravetz INIT_LIST_HEAD(&nrg->link); 859feba16e2SMike Kravetz 860feba16e2SMike Kravetz /* Original entry is trimmed */ 861feba16e2SMike Kravetz rg->to = f; 862feba16e2SMike Kravetz 863feba16e2SMike Kravetz list_add(&nrg->link, &rg->link); 864feba16e2SMike Kravetz nrg = NULL; 86596822904SAndy Whitcroft break; 866feba16e2SMike Kravetz } 867feba16e2SMike Kravetz 868feba16e2SMike Kravetz if (f <= rg->from && t >= rg->to) { /* Remove entire region */ 869feba16e2SMike Kravetz del += rg->to - rg->from; 870075a61d0SMina Almasry hugetlb_cgroup_uncharge_file_region(resv, rg, 871d85aecf2SMiaohe Lin rg->to - rg->from, true); 87296822904SAndy Whitcroft list_del(&rg->link); 87396822904SAndy Whitcroft kfree(rg); 874feba16e2SMike Kravetz continue; 87596822904SAndy Whitcroft } 8767b24d861SDavidlohr Bueso 877feba16e2SMike Kravetz if (f <= rg->from) { /* Trim beginning of region */ 878075a61d0SMina Almasry hugetlb_cgroup_uncharge_file_region(resv, rg, 879d85aecf2SMiaohe Lin t - rg->from, false); 880075a61d0SMina Almasry 88179aa925bSMike Kravetz del += t - rg->from; 88279aa925bSMike Kravetz rg->from = t; 88379aa925bSMike Kravetz } else { /* Trim end of region */ 884075a61d0SMina Almasry hugetlb_cgroup_uncharge_file_region(resv, rg, 885d85aecf2SMiaohe Lin rg->to - f, false); 88679aa925bSMike Kravetz 88779aa925bSMike Kravetz del += rg->to - f; 88879aa925bSMike Kravetz rg->to = f; 889feba16e2SMike Kravetz } 890feba16e2SMike Kravetz } 891feba16e2SMike Kravetz 8927b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 893feba16e2SMike Kravetz kfree(nrg); 894feba16e2SMike Kravetz return del; 89596822904SAndy Whitcroft } 89696822904SAndy Whitcroft 8971dd308a7SMike Kravetz /* 898b5cec28dSMike Kravetz * A rare out of memory error was encountered which prevented removal of 899b5cec28dSMike Kravetz * the reserve map region for a page. The huge page itself was free'ed 900b5cec28dSMike Kravetz * and removed from the page cache. This routine will adjust the subpool 901b5cec28dSMike Kravetz * usage count, and the global reserve count if needed. By incrementing 902b5cec28dSMike Kravetz * these counts, the reserve map entry which could not be deleted will 903b5cec28dSMike Kravetz * appear as a "reserved" entry instead of simply dangling with incorrect 904b5cec28dSMike Kravetz * counts. 905b5cec28dSMike Kravetz */ 90672e2936cSzhong jiang void hugetlb_fix_reserve_counts(struct inode *inode) 907b5cec28dSMike Kravetz { 908b5cec28dSMike Kravetz struct hugepage_subpool *spool = subpool_inode(inode); 909b5cec28dSMike Kravetz long rsv_adjust; 910da56388cSMiaohe Lin bool reserved = false; 911b5cec28dSMike Kravetz 912b5cec28dSMike Kravetz rsv_adjust = hugepage_subpool_get_pages(spool, 1); 913da56388cSMiaohe Lin if (rsv_adjust > 0) { 914b5cec28dSMike Kravetz struct hstate *h = hstate_inode(inode); 915b5cec28dSMike Kravetz 916da56388cSMiaohe Lin if (!hugetlb_acct_memory(h, 1)) 917da56388cSMiaohe Lin reserved = true; 918da56388cSMiaohe Lin } else if (!rsv_adjust) { 919da56388cSMiaohe Lin reserved = true; 920b5cec28dSMike Kravetz } 921da56388cSMiaohe Lin 922da56388cSMiaohe Lin if (!reserved) 923da56388cSMiaohe Lin pr_warn("hugetlb: Huge Page Reserved count may go negative.\n"); 924b5cec28dSMike Kravetz } 925b5cec28dSMike Kravetz 926b5cec28dSMike Kravetz /* 9271dd308a7SMike Kravetz * Count and return the number of huge pages in the reserve map 9281dd308a7SMike Kravetz * that intersect with the range [f, t). 9291dd308a7SMike Kravetz */ 9301406ec9bSJoonsoo Kim static long region_count(struct resv_map *resv, long f, long t) 93184afd99bSAndy Whitcroft { 9321406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 93384afd99bSAndy Whitcroft struct file_region *rg; 93484afd99bSAndy Whitcroft long chg = 0; 93584afd99bSAndy Whitcroft 9367b24d861SDavidlohr Bueso spin_lock(&resv->lock); 93784afd99bSAndy Whitcroft /* Locate each segment we overlap with, and count that overlap. */ 93884afd99bSAndy Whitcroft list_for_each_entry(rg, head, link) { 939f2135a4aSWang Sheng-Hui long seg_from; 940f2135a4aSWang Sheng-Hui long seg_to; 94184afd99bSAndy Whitcroft 94284afd99bSAndy Whitcroft if (rg->to <= f) 94384afd99bSAndy Whitcroft continue; 94484afd99bSAndy Whitcroft if (rg->from >= t) 94584afd99bSAndy Whitcroft break; 94684afd99bSAndy Whitcroft 94784afd99bSAndy Whitcroft seg_from = max(rg->from, f); 94884afd99bSAndy Whitcroft seg_to = min(rg->to, t); 94984afd99bSAndy Whitcroft 95084afd99bSAndy Whitcroft chg += seg_to - seg_from; 95184afd99bSAndy Whitcroft } 9527b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 95384afd99bSAndy Whitcroft 95484afd99bSAndy Whitcroft return chg; 95584afd99bSAndy Whitcroft } 95684afd99bSAndy Whitcroft 95796822904SAndy Whitcroft /* 958e7c4b0bfSAndy Whitcroft * Convert the address within this vma to the page offset within 959e7c4b0bfSAndy Whitcroft * the mapping, in pagecache page units; huge pages here. 960e7c4b0bfSAndy Whitcroft */ 961a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h, 962a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 963e7c4b0bfSAndy Whitcroft { 964a5516438SAndi Kleen return ((address - vma->vm_start) >> huge_page_shift(h)) + 965a5516438SAndi Kleen (vma->vm_pgoff >> huge_page_order(h)); 966e7c4b0bfSAndy Whitcroft } 967e7c4b0bfSAndy Whitcroft 9680fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma, 9690fe6e20bSNaoya Horiguchi unsigned long address) 9700fe6e20bSNaoya Horiguchi { 9710fe6e20bSNaoya Horiguchi return vma_hugecache_offset(hstate_vma(vma), vma, address); 9720fe6e20bSNaoya Horiguchi } 973dee41079SDan Williams EXPORT_SYMBOL_GPL(linear_hugepage_index); 9740fe6e20bSNaoya Horiguchi 97584afd99bSAndy Whitcroft /* 97608fba699SMel Gorman * Return the size of the pages allocated when backing a VMA. In the majority 97708fba699SMel Gorman * cases this will be same size as used by the page table entries. 97808fba699SMel Gorman */ 97908fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) 98008fba699SMel Gorman { 98105ea8860SDan Williams if (vma->vm_ops && vma->vm_ops->pagesize) 98205ea8860SDan Williams return vma->vm_ops->pagesize(vma); 98308fba699SMel Gorman return PAGE_SIZE; 98408fba699SMel Gorman } 985f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize); 98608fba699SMel Gorman 98708fba699SMel Gorman /* 9883340289dSMel Gorman * Return the page size being used by the MMU to back a VMA. In the majority 9893340289dSMel Gorman * of cases, the page size used by the kernel matches the MMU size. On 99009135cc5SDan Williams * architectures where it differs, an architecture-specific 'strong' 99109135cc5SDan Williams * version of this symbol is required. 9923340289dSMel Gorman */ 99309135cc5SDan Williams __weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) 9943340289dSMel Gorman { 9953340289dSMel Gorman return vma_kernel_pagesize(vma); 9963340289dSMel Gorman } 9973340289dSMel Gorman 9983340289dSMel Gorman /* 99984afd99bSAndy Whitcroft * Flags for MAP_PRIVATE reservations. These are stored in the bottom 100084afd99bSAndy Whitcroft * bits of the reservation map pointer, which are always clear due to 100184afd99bSAndy Whitcroft * alignment. 100284afd99bSAndy Whitcroft */ 100384afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER (1UL << 0) 100484afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1) 100504f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) 100684afd99bSAndy Whitcroft 1007a1e78772SMel Gorman /* 1008a1e78772SMel Gorman * These helpers are used to track how many pages are reserved for 1009a1e78772SMel Gorman * faults in a MAP_PRIVATE mapping. Only the process that called mmap() 1010a1e78772SMel Gorman * is guaranteed to have their future faults succeed. 1011a1e78772SMel Gorman * 10128d9bfb26SMike Kravetz * With the exception of hugetlb_dup_vma_private() which is called at fork(), 1013a1e78772SMel Gorman * the reserve counters are updated with the hugetlb_lock held. It is safe 1014a1e78772SMel Gorman * to reset the VMA at fork() time as it is not in use yet and there is no 1015a1e78772SMel Gorman * chance of the global counters getting corrupted as a result of the values. 101684afd99bSAndy Whitcroft * 101784afd99bSAndy Whitcroft * The private mapping reservation is represented in a subtly different 101884afd99bSAndy Whitcroft * manner to a shared mapping. A shared mapping has a region map associated 101984afd99bSAndy Whitcroft * with the underlying file, this region map represents the backing file 102084afd99bSAndy Whitcroft * pages which have ever had a reservation assigned which this persists even 102184afd99bSAndy Whitcroft * after the page is instantiated. A private mapping has a region map 102284afd99bSAndy Whitcroft * associated with the original mmap which is attached to all VMAs which 102384afd99bSAndy Whitcroft * reference it, this region map represents those offsets which have consumed 102484afd99bSAndy Whitcroft * reservation ie. where pages have been instantiated. 1025a1e78772SMel Gorman */ 1026e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma) 1027e7c4b0bfSAndy Whitcroft { 1028e7c4b0bfSAndy Whitcroft return (unsigned long)vma->vm_private_data; 1029e7c4b0bfSAndy Whitcroft } 1030e7c4b0bfSAndy Whitcroft 1031e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma, 1032e7c4b0bfSAndy Whitcroft unsigned long value) 1033e7c4b0bfSAndy Whitcroft { 1034e7c4b0bfSAndy Whitcroft vma->vm_private_data = (void *)value; 1035e7c4b0bfSAndy Whitcroft } 1036e7c4b0bfSAndy Whitcroft 1037e9fe92aeSMina Almasry static void 1038e9fe92aeSMina Almasry resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map, 1039e9fe92aeSMina Almasry struct hugetlb_cgroup *h_cg, 1040e9fe92aeSMina Almasry struct hstate *h) 1041e9fe92aeSMina Almasry { 1042e9fe92aeSMina Almasry #ifdef CONFIG_CGROUP_HUGETLB 1043e9fe92aeSMina Almasry if (!h_cg || !h) { 1044e9fe92aeSMina Almasry resv_map->reservation_counter = NULL; 1045e9fe92aeSMina Almasry resv_map->pages_per_hpage = 0; 1046e9fe92aeSMina Almasry resv_map->css = NULL; 1047e9fe92aeSMina Almasry } else { 1048e9fe92aeSMina Almasry resv_map->reservation_counter = 1049e9fe92aeSMina Almasry &h_cg->rsvd_hugepage[hstate_index(h)]; 1050e9fe92aeSMina Almasry resv_map->pages_per_hpage = pages_per_huge_page(h); 1051e9fe92aeSMina Almasry resv_map->css = &h_cg->css; 1052e9fe92aeSMina Almasry } 1053e9fe92aeSMina Almasry #endif 1054e9fe92aeSMina Almasry } 1055e9fe92aeSMina Almasry 10569119a41eSJoonsoo Kim struct resv_map *resv_map_alloc(void) 105784afd99bSAndy Whitcroft { 105884afd99bSAndy Whitcroft struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); 10595e911373SMike Kravetz struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL); 10605e911373SMike Kravetz 10615e911373SMike Kravetz if (!resv_map || !rg) { 10625e911373SMike Kravetz kfree(resv_map); 10635e911373SMike Kravetz kfree(rg); 106484afd99bSAndy Whitcroft return NULL; 10655e911373SMike Kravetz } 106684afd99bSAndy Whitcroft 106784afd99bSAndy Whitcroft kref_init(&resv_map->refs); 10687b24d861SDavidlohr Bueso spin_lock_init(&resv_map->lock); 106984afd99bSAndy Whitcroft INIT_LIST_HEAD(&resv_map->regions); 107084afd99bSAndy Whitcroft 10715e911373SMike Kravetz resv_map->adds_in_progress = 0; 1072e9fe92aeSMina Almasry /* 1073e9fe92aeSMina Almasry * Initialize these to 0. On shared mappings, 0's here indicate these 1074e9fe92aeSMina Almasry * fields don't do cgroup accounting. On private mappings, these will be 1075e9fe92aeSMina Almasry * re-initialized to the proper values, to indicate that hugetlb cgroup 1076e9fe92aeSMina Almasry * reservations are to be un-charged from here. 1077e9fe92aeSMina Almasry */ 1078e9fe92aeSMina Almasry resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL); 10795e911373SMike Kravetz 10805e911373SMike Kravetz INIT_LIST_HEAD(&resv_map->region_cache); 10815e911373SMike Kravetz list_add(&rg->link, &resv_map->region_cache); 10825e911373SMike Kravetz resv_map->region_cache_count = 1; 10835e911373SMike Kravetz 108484afd99bSAndy Whitcroft return resv_map; 108584afd99bSAndy Whitcroft } 108684afd99bSAndy Whitcroft 10879119a41eSJoonsoo Kim void resv_map_release(struct kref *ref) 108884afd99bSAndy Whitcroft { 108984afd99bSAndy Whitcroft struct resv_map *resv_map = container_of(ref, struct resv_map, refs); 10905e911373SMike Kravetz struct list_head *head = &resv_map->region_cache; 10915e911373SMike Kravetz struct file_region *rg, *trg; 109284afd99bSAndy Whitcroft 109384afd99bSAndy Whitcroft /* Clear out any active regions before we release the map. */ 1094feba16e2SMike Kravetz region_del(resv_map, 0, LONG_MAX); 10955e911373SMike Kravetz 10965e911373SMike Kravetz /* ... and any entries left in the cache */ 10975e911373SMike Kravetz list_for_each_entry_safe(rg, trg, head, link) { 10985e911373SMike Kravetz list_del(&rg->link); 10995e911373SMike Kravetz kfree(rg); 11005e911373SMike Kravetz } 11015e911373SMike Kravetz 11025e911373SMike Kravetz VM_BUG_ON(resv_map->adds_in_progress); 11035e911373SMike Kravetz 110484afd99bSAndy Whitcroft kfree(resv_map); 110584afd99bSAndy Whitcroft } 110684afd99bSAndy Whitcroft 11074e35f483SJoonsoo Kim static inline struct resv_map *inode_resv_map(struct inode *inode) 11084e35f483SJoonsoo Kim { 1109f27a5136SMike Kravetz /* 1110f27a5136SMike Kravetz * At inode evict time, i_mapping may not point to the original 1111f27a5136SMike Kravetz * address space within the inode. This original address space 1112f27a5136SMike Kravetz * contains the pointer to the resv_map. So, always use the 1113f27a5136SMike Kravetz * address space embedded within the inode. 1114f27a5136SMike Kravetz * The VERY common case is inode->mapping == &inode->i_data but, 1115f27a5136SMike Kravetz * this may not be true for device special inodes. 1116f27a5136SMike Kravetz */ 1117f27a5136SMike Kravetz return (struct resv_map *)(&inode->i_data)->private_data; 11184e35f483SJoonsoo Kim } 11194e35f483SJoonsoo Kim 112084afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma) 1121a1e78772SMel Gorman { 112281d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 11234e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) { 11244e35f483SJoonsoo Kim struct address_space *mapping = vma->vm_file->f_mapping; 11254e35f483SJoonsoo Kim struct inode *inode = mapping->host; 11264e35f483SJoonsoo Kim 11274e35f483SJoonsoo Kim return inode_resv_map(inode); 11284e35f483SJoonsoo Kim 11294e35f483SJoonsoo Kim } else { 113084afd99bSAndy Whitcroft return (struct resv_map *)(get_vma_private_data(vma) & 113184afd99bSAndy Whitcroft ~HPAGE_RESV_MASK); 11324e35f483SJoonsoo Kim } 1133a1e78772SMel Gorman } 1134a1e78772SMel Gorman 113584afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) 1136a1e78772SMel Gorman { 113781d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 113881d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 1139a1e78772SMel Gorman 114084afd99bSAndy Whitcroft set_vma_private_data(vma, (get_vma_private_data(vma) & 114184afd99bSAndy Whitcroft HPAGE_RESV_MASK) | (unsigned long)map); 114204f2cbe3SMel Gorman } 114304f2cbe3SMel Gorman 114404f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) 114504f2cbe3SMel Gorman { 114681d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 114781d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 1148e7c4b0bfSAndy Whitcroft 1149e7c4b0bfSAndy Whitcroft set_vma_private_data(vma, get_vma_private_data(vma) | flags); 115004f2cbe3SMel Gorman } 115104f2cbe3SMel Gorman 115204f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) 115304f2cbe3SMel Gorman { 115481d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 1155e7c4b0bfSAndy Whitcroft 1156e7c4b0bfSAndy Whitcroft return (get_vma_private_data(vma) & flag) != 0; 1157a1e78772SMel Gorman } 1158a1e78772SMel Gorman 11598d9bfb26SMike Kravetz void hugetlb_dup_vma_private(struct vm_area_struct *vma) 1160a1e78772SMel Gorman { 116181d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 11628d9bfb26SMike Kravetz /* 11638d9bfb26SMike Kravetz * Clear vm_private_data 1164612b8a31SMike Kravetz * - For shared mappings this is a per-vma semaphore that may be 1165612b8a31SMike Kravetz * allocated in a subsequent call to hugetlb_vm_op_open. 1166612b8a31SMike Kravetz * Before clearing, make sure pointer is not associated with vma 1167612b8a31SMike Kravetz * as this will leak the structure. This is the case when called 1168612b8a31SMike Kravetz * via clear_vma_resv_huge_pages() and hugetlb_vm_op_open has already 1169612b8a31SMike Kravetz * been called to allocate a new structure. 11708d9bfb26SMike Kravetz * - For MAP_PRIVATE mappings, this is the reserve map which does 11718d9bfb26SMike Kravetz * not apply to children. Faults generated by the children are 11728d9bfb26SMike Kravetz * not guaranteed to succeed, even if read-only. 11738d9bfb26SMike Kravetz */ 1174612b8a31SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 1175612b8a31SMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 1176612b8a31SMike Kravetz 1177612b8a31SMike Kravetz if (vma_lock && vma_lock->vma != vma) 1178612b8a31SMike Kravetz vma->vm_private_data = NULL; 1179612b8a31SMike Kravetz } else 1180612b8a31SMike Kravetz vma->vm_private_data = NULL; 1181a1e78772SMel Gorman } 1182a1e78772SMel Gorman 1183550a7d60SMina Almasry /* 1184550a7d60SMina Almasry * Reset and decrement one ref on hugepage private reservation. 11858651a137SLorenzo Stoakes * Called with mm->mmap_lock writer semaphore held. 1186550a7d60SMina Almasry * This function should be only used by move_vma() and operate on 1187550a7d60SMina Almasry * same sized vma. It should never come here with last ref on the 1188550a7d60SMina Almasry * reservation. 1189550a7d60SMina Almasry */ 1190550a7d60SMina Almasry void clear_vma_resv_huge_pages(struct vm_area_struct *vma) 1191550a7d60SMina Almasry { 1192550a7d60SMina Almasry /* 1193550a7d60SMina Almasry * Clear the old hugetlb private page reservation. 1194550a7d60SMina Almasry * It has already been transferred to new_vma. 1195550a7d60SMina Almasry * 1196550a7d60SMina Almasry * During a mremap() operation of a hugetlb vma we call move_vma() 1197550a7d60SMina Almasry * which copies vma into new_vma and unmaps vma. After the copy 1198550a7d60SMina Almasry * operation both new_vma and vma share a reference to the resv_map 1199550a7d60SMina Almasry * struct, and at that point vma is about to be unmapped. We don't 1200550a7d60SMina Almasry * want to return the reservation to the pool at unmap of vma because 1201550a7d60SMina Almasry * the reservation still lives on in new_vma, so simply decrement the 1202550a7d60SMina Almasry * ref here and remove the resv_map reference from this vma. 1203550a7d60SMina Almasry */ 1204550a7d60SMina Almasry struct resv_map *reservations = vma_resv_map(vma); 1205550a7d60SMina Almasry 1206afe041c2SBui Quang Minh if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { 1207afe041c2SBui Quang Minh resv_map_put_hugetlb_cgroup_uncharge_info(reservations); 1208550a7d60SMina Almasry kref_put(&reservations->refs, resv_map_release); 1209afe041c2SBui Quang Minh } 1210550a7d60SMina Almasry 12118d9bfb26SMike Kravetz hugetlb_dup_vma_private(vma); 1212550a7d60SMina Almasry } 1213550a7d60SMina Almasry 1214a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */ 1215559ec2f8SNicholas Krause static bool vma_has_reserves(struct vm_area_struct *vma, long chg) 1216a1e78772SMel Gorman { 1217af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_NORESERVE) { 1218af0ed73eSJoonsoo Kim /* 1219af0ed73eSJoonsoo Kim * This address is already reserved by other process(chg == 0), 1220af0ed73eSJoonsoo Kim * so, we should decrement reserved count. Without decrementing, 1221af0ed73eSJoonsoo Kim * reserve count remains after releasing inode, because this 1222af0ed73eSJoonsoo Kim * allocated page will go into page cache and is regarded as 1223af0ed73eSJoonsoo Kim * coming from reserved pool in releasing step. Currently, we 1224af0ed73eSJoonsoo Kim * don't have any other solution to deal with this situation 1225af0ed73eSJoonsoo Kim * properly, so add work-around here. 1226af0ed73eSJoonsoo Kim */ 1227af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE && chg == 0) 1228559ec2f8SNicholas Krause return true; 1229af0ed73eSJoonsoo Kim else 1230559ec2f8SNicholas Krause return false; 1231af0ed73eSJoonsoo Kim } 1232a63884e9SJoonsoo Kim 1233a63884e9SJoonsoo Kim /* Shared mappings always use reserves */ 12341fb1b0e9SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 12351fb1b0e9SMike Kravetz /* 12361fb1b0e9SMike Kravetz * We know VM_NORESERVE is not set. Therefore, there SHOULD 12371fb1b0e9SMike Kravetz * be a region map for all pages. The only situation where 12381fb1b0e9SMike Kravetz * there is no region map is if a hole was punched via 12397c8de358SEthon Paul * fallocate. In this case, there really are no reserves to 12401fb1b0e9SMike Kravetz * use. This situation is indicated if chg != 0. 12411fb1b0e9SMike Kravetz */ 12421fb1b0e9SMike Kravetz if (chg) 12431fb1b0e9SMike Kravetz return false; 12441fb1b0e9SMike Kravetz else 1245559ec2f8SNicholas Krause return true; 12461fb1b0e9SMike Kravetz } 1247a63884e9SJoonsoo Kim 1248a63884e9SJoonsoo Kim /* 1249a63884e9SJoonsoo Kim * Only the process that called mmap() has reserves for 1250a63884e9SJoonsoo Kim * private mappings. 1251a63884e9SJoonsoo Kim */ 125267961f9dSMike Kravetz if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { 125367961f9dSMike Kravetz /* 125467961f9dSMike Kravetz * Like the shared case above, a hole punch or truncate 125567961f9dSMike Kravetz * could have been performed on the private mapping. 125667961f9dSMike Kravetz * Examine the value of chg to determine if reserves 125767961f9dSMike Kravetz * actually exist or were previously consumed. 125867961f9dSMike Kravetz * Very Subtle - The value of chg comes from a previous 125967961f9dSMike Kravetz * call to vma_needs_reserves(). The reserve map for 126067961f9dSMike Kravetz * private mappings has different (opposite) semantics 126167961f9dSMike Kravetz * than that of shared mappings. vma_needs_reserves() 126267961f9dSMike Kravetz * has already taken this difference in semantics into 126367961f9dSMike Kravetz * account. Therefore, the meaning of chg is the same 126467961f9dSMike Kravetz * as in the shared case above. Code could easily be 126567961f9dSMike Kravetz * combined, but keeping it separate draws attention to 126667961f9dSMike Kravetz * subtle differences. 126767961f9dSMike Kravetz */ 126867961f9dSMike Kravetz if (chg) 126967961f9dSMike Kravetz return false; 127067961f9dSMike Kravetz else 1271559ec2f8SNicholas Krause return true; 127267961f9dSMike Kravetz } 1273a63884e9SJoonsoo Kim 1274559ec2f8SNicholas Krause return false; 1275a1e78772SMel Gorman } 1276a1e78772SMel Gorman 1277240d67a8SSidhartha Kumar static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio) 12781da177e4SLinus Torvalds { 1279240d67a8SSidhartha Kumar int nid = folio_nid(folio); 12809487ca60SMike Kravetz 12819487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 1282240d67a8SSidhartha Kumar VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); 1283b65a4edaSMike Kravetz 1284240d67a8SSidhartha Kumar list_move(&folio->lru, &h->hugepage_freelists[nid]); 1285a5516438SAndi Kleen h->free_huge_pages++; 1286a5516438SAndi Kleen h->free_huge_pages_node[nid]++; 1287240d67a8SSidhartha Kumar folio_set_hugetlb_freed(folio); 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 129094310cbcSAnshuman Khandual static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid) 1291bf50bab2SNaoya Horiguchi { 1292bf50bab2SNaoya Horiguchi struct page *page; 12931a08ae36SPavel Tatashin bool pin = !!(current->flags & PF_MEMALLOC_PIN); 1294bf50bab2SNaoya Horiguchi 12959487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 1296bbe88753SJoonsoo Kim list_for_each_entry(page, &h->hugepage_freelists[nid], lru) { 12976077c943SAlex Sierra if (pin && !is_longterm_pinnable_page(page)) 1298bbe88753SJoonsoo Kim continue; 1299bbe88753SJoonsoo Kim 13006664bfc8SWei Yang if (PageHWPoison(page)) 13016664bfc8SWei Yang continue; 1302bbe88753SJoonsoo Kim 13030edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 1304a9869b83SNaoya Horiguchi set_page_refcounted(page); 13056c037149SMike Kravetz ClearHPageFreed(page); 1306bf50bab2SNaoya Horiguchi h->free_huge_pages--; 1307bf50bab2SNaoya Horiguchi h->free_huge_pages_node[nid]--; 1308bf50bab2SNaoya Horiguchi return page; 1309bf50bab2SNaoya Horiguchi } 1310bf50bab2SNaoya Horiguchi 13116664bfc8SWei Yang return NULL; 13126664bfc8SWei Yang } 13136664bfc8SWei Yang 13143e59fcb0SMichal Hocko static struct page *dequeue_huge_page_nodemask(struct hstate *h, gfp_t gfp_mask, int nid, 13153e59fcb0SMichal Hocko nodemask_t *nmask) 131694310cbcSAnshuman Khandual { 13173e59fcb0SMichal Hocko unsigned int cpuset_mems_cookie; 13183e59fcb0SMichal Hocko struct zonelist *zonelist; 13193e59fcb0SMichal Hocko struct zone *zone; 13203e59fcb0SMichal Hocko struct zoneref *z; 132198fa15f3SAnshuman Khandual int node = NUMA_NO_NODE; 13223e59fcb0SMichal Hocko 13233e59fcb0SMichal Hocko zonelist = node_zonelist(nid, gfp_mask); 13243e59fcb0SMichal Hocko 13253e59fcb0SMichal Hocko retry_cpuset: 13263e59fcb0SMichal Hocko cpuset_mems_cookie = read_mems_allowed_begin(); 13273e59fcb0SMichal Hocko for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) { 132894310cbcSAnshuman Khandual struct page *page; 132994310cbcSAnshuman Khandual 13303e59fcb0SMichal Hocko if (!cpuset_zone_allowed(zone, gfp_mask)) 13313e59fcb0SMichal Hocko continue; 13323e59fcb0SMichal Hocko /* 13333e59fcb0SMichal Hocko * no need to ask again on the same node. Pool is node rather than 13343e59fcb0SMichal Hocko * zone aware 13353e59fcb0SMichal Hocko */ 13363e59fcb0SMichal Hocko if (zone_to_nid(zone) == node) 13373e59fcb0SMichal Hocko continue; 13383e59fcb0SMichal Hocko node = zone_to_nid(zone); 133994310cbcSAnshuman Khandual 134094310cbcSAnshuman Khandual page = dequeue_huge_page_node_exact(h, node); 134194310cbcSAnshuman Khandual if (page) 134294310cbcSAnshuman Khandual return page; 134394310cbcSAnshuman Khandual } 13443e59fcb0SMichal Hocko if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie))) 13453e59fcb0SMichal Hocko goto retry_cpuset; 13463e59fcb0SMichal Hocko 134794310cbcSAnshuman Khandual return NULL; 134894310cbcSAnshuman Khandual } 134994310cbcSAnshuman Khandual 13508346d69dSXin Hao static unsigned long available_huge_pages(struct hstate *h) 13518346d69dSXin Hao { 13528346d69dSXin Hao return h->free_huge_pages - h->resv_huge_pages; 13538346d69dSXin Hao } 13548346d69dSXin Hao 1355a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h, 1356a5516438SAndi Kleen struct vm_area_struct *vma, 1357af0ed73eSJoonsoo Kim unsigned long address, int avoid_reserve, 1358af0ed73eSJoonsoo Kim long chg) 13591da177e4SLinus Torvalds { 1360cfcaa66fSBen Widawsky struct page *page = NULL; 1361480eccf9SLee Schermerhorn struct mempolicy *mpol; 136204ec6264SVlastimil Babka gfp_t gfp_mask; 13633e59fcb0SMichal Hocko nodemask_t *nodemask; 136404ec6264SVlastimil Babka int nid; 13651da177e4SLinus Torvalds 1366a1e78772SMel Gorman /* 1367a1e78772SMel Gorman * A child process with MAP_PRIVATE mappings created by their parent 1368a1e78772SMel Gorman * have no page reserves. This check ensures that reservations are 1369a1e78772SMel Gorman * not "stolen". The child may still get SIGKILLed 1370a1e78772SMel Gorman */ 13718346d69dSXin Hao if (!vma_has_reserves(vma, chg) && !available_huge_pages(h)) 1372c0ff7453SMiao Xie goto err; 1373a1e78772SMel Gorman 137404f2cbe3SMel Gorman /* If reserves cannot be used, ensure enough pages are in the pool */ 13758346d69dSXin Hao if (avoid_reserve && !available_huge_pages(h)) 13766eab04a8SJustin P. Mattock goto err; 137704f2cbe3SMel Gorman 137804ec6264SVlastimil Babka gfp_mask = htlb_alloc_mask(h); 137904ec6264SVlastimil Babka nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask); 1380cfcaa66fSBen Widawsky 1381cfcaa66fSBen Widawsky if (mpol_is_preferred_many(mpol)) { 13823e59fcb0SMichal Hocko page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask); 1383cfcaa66fSBen Widawsky 1384cfcaa66fSBen Widawsky /* Fallback to all nodes if page==NULL */ 1385cfcaa66fSBen Widawsky nodemask = NULL; 1386cfcaa66fSBen Widawsky } 1387cfcaa66fSBen Widawsky 1388cfcaa66fSBen Widawsky if (!page) 1389cfcaa66fSBen Widawsky page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask); 1390cfcaa66fSBen Widawsky 13913e59fcb0SMichal Hocko if (page && !avoid_reserve && vma_has_reserves(vma, chg)) { 1392d6995da3SMike Kravetz SetHPageRestoreReserve(page); 1393a63884e9SJoonsoo Kim h->resv_huge_pages--; 1394bf50bab2SNaoya Horiguchi } 1395cc9a6c87SMel Gorman 1396cc9a6c87SMel Gorman mpol_cond_put(mpol); 1397cc9a6c87SMel Gorman return page; 1398cc9a6c87SMel Gorman 1399c0ff7453SMiao Xie err: 1400cc9a6c87SMel Gorman return NULL; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14031cac6f2cSLuiz Capitulino /* 14041cac6f2cSLuiz Capitulino * common helper functions for hstate_next_node_to_{alloc|free}. 14051cac6f2cSLuiz Capitulino * We may have allocated or freed a huge page based on a different 14061cac6f2cSLuiz Capitulino * nodes_allowed previously, so h->next_node_to_{alloc|free} might 14071cac6f2cSLuiz Capitulino * be outside of *nodes_allowed. Ensure that we use an allowed 14081cac6f2cSLuiz Capitulino * node for alloc or free. 14091cac6f2cSLuiz Capitulino */ 14101cac6f2cSLuiz Capitulino static int next_node_allowed(int nid, nodemask_t *nodes_allowed) 14111cac6f2cSLuiz Capitulino { 14120edaf86cSAndrew Morton nid = next_node_in(nid, *nodes_allowed); 14131cac6f2cSLuiz Capitulino VM_BUG_ON(nid >= MAX_NUMNODES); 14141cac6f2cSLuiz Capitulino 14151cac6f2cSLuiz Capitulino return nid; 14161cac6f2cSLuiz Capitulino } 14171cac6f2cSLuiz Capitulino 14181cac6f2cSLuiz Capitulino static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) 14191cac6f2cSLuiz Capitulino { 14201cac6f2cSLuiz Capitulino if (!node_isset(nid, *nodes_allowed)) 14211cac6f2cSLuiz Capitulino nid = next_node_allowed(nid, nodes_allowed); 14221cac6f2cSLuiz Capitulino return nid; 14231cac6f2cSLuiz Capitulino } 14241cac6f2cSLuiz Capitulino 14251cac6f2cSLuiz Capitulino /* 14261cac6f2cSLuiz Capitulino * returns the previously saved node ["this node"] from which to 14271cac6f2cSLuiz Capitulino * allocate a persistent huge page for the pool and advance the 14281cac6f2cSLuiz Capitulino * next node from which to allocate, handling wrap at end of node 14291cac6f2cSLuiz Capitulino * mask. 14301cac6f2cSLuiz Capitulino */ 14311cac6f2cSLuiz Capitulino static int hstate_next_node_to_alloc(struct hstate *h, 14321cac6f2cSLuiz Capitulino nodemask_t *nodes_allowed) 14331cac6f2cSLuiz Capitulino { 14341cac6f2cSLuiz Capitulino int nid; 14351cac6f2cSLuiz Capitulino 14361cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 14371cac6f2cSLuiz Capitulino 14381cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); 14391cac6f2cSLuiz Capitulino h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); 14401cac6f2cSLuiz Capitulino 14411cac6f2cSLuiz Capitulino return nid; 14421cac6f2cSLuiz Capitulino } 14431cac6f2cSLuiz Capitulino 14441cac6f2cSLuiz Capitulino /* 144510c6ec49SMike Kravetz * helper for remove_pool_huge_page() - return the previously saved 14461cac6f2cSLuiz Capitulino * node ["this node"] from which to free a huge page. Advance the 14471cac6f2cSLuiz Capitulino * next node id whether or not we find a free huge page to free so 14481cac6f2cSLuiz Capitulino * that the next attempt to free addresses the next node. 14491cac6f2cSLuiz Capitulino */ 14501cac6f2cSLuiz Capitulino static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) 14511cac6f2cSLuiz Capitulino { 14521cac6f2cSLuiz Capitulino int nid; 14531cac6f2cSLuiz Capitulino 14541cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 14551cac6f2cSLuiz Capitulino 14561cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); 14571cac6f2cSLuiz Capitulino h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); 14581cac6f2cSLuiz Capitulino 14591cac6f2cSLuiz Capitulino return nid; 14601cac6f2cSLuiz Capitulino } 14611cac6f2cSLuiz Capitulino 14621cac6f2cSLuiz Capitulino #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \ 14631cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 14641cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 14651cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ 14661cac6f2cSLuiz Capitulino nr_nodes--) 14671cac6f2cSLuiz Capitulino 14681cac6f2cSLuiz Capitulino #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ 14691cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 14701cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 14711cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_free(hs, mask)) || 1); \ 14721cac6f2cSLuiz Capitulino nr_nodes--) 14731cac6f2cSLuiz Capitulino 14748531fc6fSMike Kravetz /* used to demote non-gigantic_huge pages as well */ 1475911565b8SSidhartha Kumar static void __destroy_compound_gigantic_folio(struct folio *folio, 147634d9e35bSMike Kravetz unsigned int order, bool demote) 1477944d9fecSLuiz Capitulino { 1478944d9fecSLuiz Capitulino int i; 1479944d9fecSLuiz Capitulino int nr_pages = 1 << order; 148014455eabSCheng Li struct page *p; 1481944d9fecSLuiz Capitulino 1482911565b8SSidhartha Kumar atomic_set(folio_mapcount_ptr(folio), 0); 1483911565b8SSidhartha Kumar atomic_set(folio_subpages_mapcount_ptr(folio), 0); 1484911565b8SSidhartha Kumar atomic_set(folio_pincount_ptr(folio), 0); 148547e29d32SJohn Hubbard 148614455eabSCheng Li for (i = 1; i < nr_pages; i++) { 1487911565b8SSidhartha Kumar p = folio_page(folio, i); 1488a01f4390SMike Kravetz p->mapping = NULL; 14891d798ca3SKirill A. Shutemov clear_compound_head(p); 149034d9e35bSMike Kravetz if (!demote) 1491944d9fecSLuiz Capitulino set_page_refcounted(p); 1492944d9fecSLuiz Capitulino } 1493944d9fecSLuiz Capitulino 149404a42e72SSidhartha Kumar folio_set_order(folio, 0); 1495911565b8SSidhartha Kumar __folio_clear_head(folio); 1496944d9fecSLuiz Capitulino } 1497944d9fecSLuiz Capitulino 1498911565b8SSidhartha Kumar static void destroy_compound_hugetlb_folio_for_demote(struct folio *folio, 14998531fc6fSMike Kravetz unsigned int order) 15008531fc6fSMike Kravetz { 1501911565b8SSidhartha Kumar __destroy_compound_gigantic_folio(folio, order, true); 15028531fc6fSMike Kravetz } 15038531fc6fSMike Kravetz 15048531fc6fSMike Kravetz #ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE 1505911565b8SSidhartha Kumar static void destroy_compound_gigantic_folio(struct folio *folio, 150634d9e35bSMike Kravetz unsigned int order) 150734d9e35bSMike Kravetz { 1508911565b8SSidhartha Kumar __destroy_compound_gigantic_folio(folio, order, false); 150934d9e35bSMike Kravetz } 151034d9e35bSMike Kravetz 15117f325a8dSSidhartha Kumar static void free_gigantic_folio(struct folio *folio, unsigned int order) 1512944d9fecSLuiz Capitulino { 1513cf11e85fSRoman Gushchin /* 1514cf11e85fSRoman Gushchin * If the page isn't allocated using the cma allocator, 1515cf11e85fSRoman Gushchin * cma_release() returns false. 1516cf11e85fSRoman Gushchin */ 1517dbda8feaSBarry Song #ifdef CONFIG_CMA 15187f325a8dSSidhartha Kumar int nid = folio_nid(folio); 15197f325a8dSSidhartha Kumar 15207f325a8dSSidhartha Kumar if (cma_release(hugetlb_cma[nid], &folio->page, 1 << order)) 1521cf11e85fSRoman Gushchin return; 1522dbda8feaSBarry Song #endif 1523cf11e85fSRoman Gushchin 15247f325a8dSSidhartha Kumar free_contig_range(folio_pfn(folio), 1 << order); 1525944d9fecSLuiz Capitulino } 1526944d9fecSLuiz Capitulino 15274eb0716eSAlexandre Ghiti #ifdef CONFIG_CONTIG_ALLOC 152819fc1a7eSSidhartha Kumar static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask, 1529d9cc948fSMichal Hocko int nid, nodemask_t *nodemask) 1530944d9fecSLuiz Capitulino { 153119fc1a7eSSidhartha Kumar struct page *page; 153204adbc3fSMiaohe Lin unsigned long nr_pages = pages_per_huge_page(h); 1533953f064aSLi Xinhai if (nid == NUMA_NO_NODE) 1534953f064aSLi Xinhai nid = numa_mem_id(); 1535944d9fecSLuiz Capitulino 1536dbda8feaSBarry Song #ifdef CONFIG_CMA 1537dbda8feaSBarry Song { 1538cf11e85fSRoman Gushchin int node; 1539cf11e85fSRoman Gushchin 1540953f064aSLi Xinhai if (hugetlb_cma[nid]) { 1541953f064aSLi Xinhai page = cma_alloc(hugetlb_cma[nid], nr_pages, 1542953f064aSLi Xinhai huge_page_order(h), true); 1543953f064aSLi Xinhai if (page) 154419fc1a7eSSidhartha Kumar return page_folio(page); 1545953f064aSLi Xinhai } 1546953f064aSLi Xinhai 1547953f064aSLi Xinhai if (!(gfp_mask & __GFP_THISNODE)) { 1548cf11e85fSRoman Gushchin for_each_node_mask(node, *nodemask) { 1549953f064aSLi Xinhai if (node == nid || !hugetlb_cma[node]) 1550cf11e85fSRoman Gushchin continue; 1551cf11e85fSRoman Gushchin 1552cf11e85fSRoman Gushchin page = cma_alloc(hugetlb_cma[node], nr_pages, 1553cf11e85fSRoman Gushchin huge_page_order(h), true); 1554cf11e85fSRoman Gushchin if (page) 155519fc1a7eSSidhartha Kumar return page_folio(page); 1556cf11e85fSRoman Gushchin } 1557cf11e85fSRoman Gushchin } 1558953f064aSLi Xinhai } 1559dbda8feaSBarry Song #endif 1560cf11e85fSRoman Gushchin 156119fc1a7eSSidhartha Kumar page = alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask); 156219fc1a7eSSidhartha Kumar return page ? page_folio(page) : NULL; 1563944d9fecSLuiz Capitulino } 1564944d9fecSLuiz Capitulino 15654eb0716eSAlexandre Ghiti #else /* !CONFIG_CONTIG_ALLOC */ 156619fc1a7eSSidhartha Kumar static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask, 15674eb0716eSAlexandre Ghiti int nid, nodemask_t *nodemask) 15684eb0716eSAlexandre Ghiti { 15694eb0716eSAlexandre Ghiti return NULL; 15704eb0716eSAlexandre Ghiti } 15714eb0716eSAlexandre Ghiti #endif /* CONFIG_CONTIG_ALLOC */ 1572944d9fecSLuiz Capitulino 1573e1073d1eSAneesh Kumar K.V #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */ 157419fc1a7eSSidhartha Kumar static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask, 15754eb0716eSAlexandre Ghiti int nid, nodemask_t *nodemask) 15764eb0716eSAlexandre Ghiti { 15774eb0716eSAlexandre Ghiti return NULL; 15784eb0716eSAlexandre Ghiti } 15797f325a8dSSidhartha Kumar static inline void free_gigantic_folio(struct folio *folio, 15807f325a8dSSidhartha Kumar unsigned int order) { } 1581911565b8SSidhartha Kumar static inline void destroy_compound_gigantic_folio(struct folio *folio, 1582d00181b9SKirill A. Shutemov unsigned int order) { } 1583944d9fecSLuiz Capitulino #endif 1584944d9fecSLuiz Capitulino 15856eb4e88aSMike Kravetz /* 1586cfd5082bSSidhartha Kumar * Remove hugetlb folio from lists, and update dtor so that the folio appears 158734d9e35bSMike Kravetz * as just a compound page. 158834d9e35bSMike Kravetz * 1589cfd5082bSSidhartha Kumar * A reference is held on the folio, except in the case of demote. 15906eb4e88aSMike Kravetz * 15916eb4e88aSMike Kravetz * Must be called with hugetlb lock held. 15926eb4e88aSMike Kravetz */ 1593cfd5082bSSidhartha Kumar static void __remove_hugetlb_folio(struct hstate *h, struct folio *folio, 159434d9e35bSMike Kravetz bool adjust_surplus, 159534d9e35bSMike Kravetz bool demote) 15966eb4e88aSMike Kravetz { 1597cfd5082bSSidhartha Kumar int nid = folio_nid(folio); 15986eb4e88aSMike Kravetz 1599f074732dSSidhartha Kumar VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio(folio), folio); 1600f074732dSSidhartha Kumar VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio_rsvd(folio), folio); 16016eb4e88aSMike Kravetz 16029487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 16036eb4e88aSMike Kravetz if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 16046eb4e88aSMike Kravetz return; 16056eb4e88aSMike Kravetz 1606cfd5082bSSidhartha Kumar list_del(&folio->lru); 16076eb4e88aSMike Kravetz 1608cfd5082bSSidhartha Kumar if (folio_test_hugetlb_freed(folio)) { 16096eb4e88aSMike Kravetz h->free_huge_pages--; 16106eb4e88aSMike Kravetz h->free_huge_pages_node[nid]--; 16116eb4e88aSMike Kravetz } 16126eb4e88aSMike Kravetz if (adjust_surplus) { 16136eb4e88aSMike Kravetz h->surplus_huge_pages--; 16146eb4e88aSMike Kravetz h->surplus_huge_pages_node[nid]--; 16156eb4e88aSMike Kravetz } 16166eb4e88aSMike Kravetz 1617e32d20c0SMike Kravetz /* 1618e32d20c0SMike Kravetz * Very subtle 1619e32d20c0SMike Kravetz * 1620e32d20c0SMike Kravetz * For non-gigantic pages set the destructor to the normal compound 1621e32d20c0SMike Kravetz * page dtor. This is needed in case someone takes an additional 1622e32d20c0SMike Kravetz * temporary ref to the page, and freeing is delayed until they drop 1623e32d20c0SMike Kravetz * their reference. 1624e32d20c0SMike Kravetz * 1625e32d20c0SMike Kravetz * For gigantic pages set the destructor to the null dtor. This 1626e32d20c0SMike Kravetz * destructor will never be called. Before freeing the gigantic 1627911565b8SSidhartha Kumar * page destroy_compound_gigantic_folio will turn the folio into a 1628911565b8SSidhartha Kumar * simple group of pages. After this the destructor does not 1629e32d20c0SMike Kravetz * apply. 1630e32d20c0SMike Kravetz * 1631e32d20c0SMike Kravetz * This handles the case where more than one ref is held when and 1632d6ef19e2SSidhartha Kumar * after update_and_free_hugetlb_folio is called. 163334d9e35bSMike Kravetz * 163434d9e35bSMike Kravetz * In the case of demote we do not ref count the page as it will soon 163534d9e35bSMike Kravetz * be turned into a page of smaller size. 1636e32d20c0SMike Kravetz */ 163734d9e35bSMike Kravetz if (!demote) 1638cfd5082bSSidhartha Kumar folio_ref_unfreeze(folio, 1); 1639e32d20c0SMike Kravetz if (hstate_is_gigantic(h)) 1640cfd5082bSSidhartha Kumar folio_set_compound_dtor(folio, NULL_COMPOUND_DTOR); 1641e32d20c0SMike Kravetz else 1642cfd5082bSSidhartha Kumar folio_set_compound_dtor(folio, COMPOUND_PAGE_DTOR); 16436eb4e88aSMike Kravetz 16446eb4e88aSMike Kravetz h->nr_huge_pages--; 16456eb4e88aSMike Kravetz h->nr_huge_pages_node[nid]--; 16466eb4e88aSMike Kravetz } 16476eb4e88aSMike Kravetz 1648cfd5082bSSidhartha Kumar static void remove_hugetlb_folio(struct hstate *h, struct folio *folio, 164934d9e35bSMike Kravetz bool adjust_surplus) 165034d9e35bSMike Kravetz { 1651cfd5082bSSidhartha Kumar __remove_hugetlb_folio(h, folio, adjust_surplus, false); 165234d9e35bSMike Kravetz } 165334d9e35bSMike Kravetz 1654cfd5082bSSidhartha Kumar static void remove_hugetlb_folio_for_demote(struct hstate *h, struct folio *folio, 16558531fc6fSMike Kravetz bool adjust_surplus) 16568531fc6fSMike Kravetz { 1657cfd5082bSSidhartha Kumar __remove_hugetlb_folio(h, folio, adjust_surplus, true); 16588531fc6fSMike Kravetz } 16598531fc6fSMike Kravetz 16602f6c57d6SSidhartha Kumar static void add_hugetlb_folio(struct hstate *h, struct folio *folio, 1661ad2fa371SMuchun Song bool adjust_surplus) 1662ad2fa371SMuchun Song { 1663ad2fa371SMuchun Song int zeroed; 16642f6c57d6SSidhartha Kumar int nid = folio_nid(folio); 1665ad2fa371SMuchun Song 16662f6c57d6SSidhartha Kumar VM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio); 1667ad2fa371SMuchun Song 1668ad2fa371SMuchun Song lockdep_assert_held(&hugetlb_lock); 1669ad2fa371SMuchun Song 16702f6c57d6SSidhartha Kumar INIT_LIST_HEAD(&folio->lru); 1671ad2fa371SMuchun Song h->nr_huge_pages++; 1672ad2fa371SMuchun Song h->nr_huge_pages_node[nid]++; 1673ad2fa371SMuchun Song 1674ad2fa371SMuchun Song if (adjust_surplus) { 1675ad2fa371SMuchun Song h->surplus_huge_pages++; 1676ad2fa371SMuchun Song h->surplus_huge_pages_node[nid]++; 1677ad2fa371SMuchun Song } 1678ad2fa371SMuchun Song 16792f6c57d6SSidhartha Kumar folio_set_compound_dtor(folio, HUGETLB_PAGE_DTOR); 16802f6c57d6SSidhartha Kumar folio_change_private(folio, NULL); 1681a9e1eab2SMiaohe Lin /* 16822f6c57d6SSidhartha Kumar * We have to set hugetlb_vmemmap_optimized again as above 16832f6c57d6SSidhartha Kumar * folio_change_private(folio, NULL) cleared it. 1684a9e1eab2SMiaohe Lin */ 16852f6c57d6SSidhartha Kumar folio_set_hugetlb_vmemmap_optimized(folio); 1686ad2fa371SMuchun Song 1687ad2fa371SMuchun Song /* 16882f6c57d6SSidhartha Kumar * This folio is about to be managed by the hugetlb allocator and 1689b65a4edaSMike Kravetz * should have no users. Drop our reference, and check for others 1690b65a4edaSMike Kravetz * just in case. 1691ad2fa371SMuchun Song */ 16922f6c57d6SSidhartha Kumar zeroed = folio_put_testzero(folio); 16932f6c57d6SSidhartha Kumar if (unlikely(!zeroed)) 1694b65a4edaSMike Kravetz /* 1695b65a4edaSMike Kravetz * It is VERY unlikely soneone else has taken a ref on 1696b65a4edaSMike Kravetz * the page. In this case, we simply return as the 1697b65a4edaSMike Kravetz * hugetlb destructor (free_huge_page) will be called 1698b65a4edaSMike Kravetz * when this other ref is dropped. 1699b65a4edaSMike Kravetz */ 1700b65a4edaSMike Kravetz return; 1701b65a4edaSMike Kravetz 17022f6c57d6SSidhartha Kumar arch_clear_hugepage_flags(&folio->page); 1703240d67a8SSidhartha Kumar enqueue_hugetlb_folio(h, folio); 1704ad2fa371SMuchun Song } 1705ad2fa371SMuchun Song 1706b65d4adbSMuchun Song static void __update_and_free_page(struct hstate *h, struct page *page) 17076af2acb6SAdam Litke { 17086af2acb6SAdam Litke int i; 1709911565b8SSidhartha Kumar struct folio *folio = page_folio(page); 171014455eabSCheng Li struct page *subpage; 1711a5516438SAndi Kleen 17124eb0716eSAlexandre Ghiti if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 1713944d9fecSLuiz Capitulino return; 171418229df5SAndy Whitcroft 1715161df60eSNaoya Horiguchi /* 1716161df60eSNaoya Horiguchi * If we don't know which subpages are hwpoisoned, we can't free 1717161df60eSNaoya Horiguchi * the hugepage, so it's leaked intentionally. 1718161df60eSNaoya Horiguchi */ 17197f325a8dSSidhartha Kumar if (folio_test_hugetlb_raw_hwp_unreliable(folio)) 1720161df60eSNaoya Horiguchi return; 1721161df60eSNaoya Horiguchi 17226213834cSMuchun Song if (hugetlb_vmemmap_restore(h, page)) { 1723ad2fa371SMuchun Song spin_lock_irq(&hugetlb_lock); 1724ad2fa371SMuchun Song /* 1725ad2fa371SMuchun Song * If we cannot allocate vmemmap pages, just refuse to free the 1726ad2fa371SMuchun Song * page and put the page back on the hugetlb free list and treat 1727ad2fa371SMuchun Song * as a surplus page. 1728ad2fa371SMuchun Song */ 17297f325a8dSSidhartha Kumar add_hugetlb_folio(h, folio, true); 1730ad2fa371SMuchun Song spin_unlock_irq(&hugetlb_lock); 1731ad2fa371SMuchun Song return; 1732ad2fa371SMuchun Song } 1733ad2fa371SMuchun Song 1734161df60eSNaoya Horiguchi /* 1735161df60eSNaoya Horiguchi * Move PageHWPoison flag from head page to the raw error pages, 1736161df60eSNaoya Horiguchi * which makes any healthy subpages reusable. 1737161df60eSNaoya Horiguchi */ 1738911565b8SSidhartha Kumar if (unlikely(folio_test_hwpoison(folio))) 1739911565b8SSidhartha Kumar hugetlb_clear_page_hwpoison(&folio->page); 1740161df60eSNaoya Horiguchi 174114455eabSCheng Li for (i = 0; i < pages_per_huge_page(h); i++) { 17427f325a8dSSidhartha Kumar subpage = folio_page(folio, i); 1743dbfee5aeSMike Kravetz subpage->flags &= ~(1 << PG_locked | 1 << PG_error | 174432f84528SChris Forbes 1 << PG_referenced | 1 << PG_dirty | 1745a7407a27SLuiz Capitulino 1 << PG_active | 1 << PG_private | 1746a7407a27SLuiz Capitulino 1 << PG_writeback); 17476af2acb6SAdam Litke } 1748a01f4390SMike Kravetz 1749a01f4390SMike Kravetz /* 1750a01f4390SMike Kravetz * Non-gigantic pages demoted from CMA allocated gigantic pages 17517f325a8dSSidhartha Kumar * need to be given back to CMA in free_gigantic_folio. 1752a01f4390SMike Kravetz */ 1753a01f4390SMike Kravetz if (hstate_is_gigantic(h) || 17542f6c57d6SSidhartha Kumar hugetlb_cma_folio(folio, huge_page_order(h))) { 1755911565b8SSidhartha Kumar destroy_compound_gigantic_folio(folio, huge_page_order(h)); 17567f325a8dSSidhartha Kumar free_gigantic_folio(folio, huge_page_order(h)); 1757944d9fecSLuiz Capitulino } else { 1758a5516438SAndi Kleen __free_pages(page, huge_page_order(h)); 17596af2acb6SAdam Litke } 1760944d9fecSLuiz Capitulino } 17616af2acb6SAdam Litke 1762b65d4adbSMuchun Song /* 1763d6ef19e2SSidhartha Kumar * As update_and_free_hugetlb_folio() can be called under any context, so we cannot 1764b65d4adbSMuchun Song * use GFP_KERNEL to allocate vmemmap pages. However, we can defer the 1765b65d4adbSMuchun Song * actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate 1766b65d4adbSMuchun Song * the vmemmap pages. 1767b65d4adbSMuchun Song * 1768b65d4adbSMuchun Song * free_hpage_workfn() locklessly retrieves the linked list of pages to be 1769b65d4adbSMuchun Song * freed and frees them one-by-one. As the page->mapping pointer is going 1770b65d4adbSMuchun Song * to be cleared in free_hpage_workfn() anyway, it is reused as the llist_node 1771b65d4adbSMuchun Song * structure of a lockless linked list of huge pages to be freed. 1772b65d4adbSMuchun Song */ 1773b65d4adbSMuchun Song static LLIST_HEAD(hpage_freelist); 1774b65d4adbSMuchun Song 1775b65d4adbSMuchun Song static void free_hpage_workfn(struct work_struct *work) 1776b65d4adbSMuchun Song { 1777b65d4adbSMuchun Song struct llist_node *node; 1778b65d4adbSMuchun Song 1779b65d4adbSMuchun Song node = llist_del_all(&hpage_freelist); 1780b65d4adbSMuchun Song 1781b65d4adbSMuchun Song while (node) { 1782b65d4adbSMuchun Song struct page *page; 1783b65d4adbSMuchun Song struct hstate *h; 1784b65d4adbSMuchun Song 1785b65d4adbSMuchun Song page = container_of((struct address_space **)node, 1786b65d4adbSMuchun Song struct page, mapping); 1787b65d4adbSMuchun Song node = node->next; 1788b65d4adbSMuchun Song page->mapping = NULL; 1789b65d4adbSMuchun Song /* 1790b65d4adbSMuchun Song * The VM_BUG_ON_PAGE(!PageHuge(page), page) in page_hstate() 1791b65d4adbSMuchun Song * is going to trigger because a previous call to 1792cfd5082bSSidhartha Kumar * remove_hugetlb_folio() will call folio_set_compound_dtor 1793cfd5082bSSidhartha Kumar * (folio, NULL_COMPOUND_DTOR), so do not use page_hstate() 1794cfd5082bSSidhartha Kumar * directly. 1795b65d4adbSMuchun Song */ 1796b65d4adbSMuchun Song h = size_to_hstate(page_size(page)); 1797b65d4adbSMuchun Song 1798b65d4adbSMuchun Song __update_and_free_page(h, page); 1799b65d4adbSMuchun Song 1800b65d4adbSMuchun Song cond_resched(); 1801b65d4adbSMuchun Song } 1802b65d4adbSMuchun Song } 1803b65d4adbSMuchun Song static DECLARE_WORK(free_hpage_work, free_hpage_workfn); 1804b65d4adbSMuchun Song 1805b65d4adbSMuchun Song static inline void flush_free_hpage_work(struct hstate *h) 1806b65d4adbSMuchun Song { 18076213834cSMuchun Song if (hugetlb_vmemmap_optimizable(h)) 1808b65d4adbSMuchun Song flush_work(&free_hpage_work); 1809b65d4adbSMuchun Song } 1810b65d4adbSMuchun Song 1811d6ef19e2SSidhartha Kumar static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio, 1812b65d4adbSMuchun Song bool atomic) 1813b65d4adbSMuchun Song { 1814d6ef19e2SSidhartha Kumar if (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) { 1815d6ef19e2SSidhartha Kumar __update_and_free_page(h, &folio->page); 1816b65d4adbSMuchun Song return; 1817b65d4adbSMuchun Song } 1818b65d4adbSMuchun Song 1819b65d4adbSMuchun Song /* 1820b65d4adbSMuchun Song * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap pages. 1821b65d4adbSMuchun Song * 1822b65d4adbSMuchun Song * Only call schedule_work() if hpage_freelist is previously 1823b65d4adbSMuchun Song * empty. Otherwise, schedule_work() had been called but the workfn 1824b65d4adbSMuchun Song * hasn't retrieved the list yet. 1825b65d4adbSMuchun Song */ 1826d6ef19e2SSidhartha Kumar if (llist_add((struct llist_node *)&folio->mapping, &hpage_freelist)) 1827b65d4adbSMuchun Song schedule_work(&free_hpage_work); 1828b65d4adbSMuchun Song } 1829b65d4adbSMuchun Song 183010c6ec49SMike Kravetz static void update_and_free_pages_bulk(struct hstate *h, struct list_head *list) 183110c6ec49SMike Kravetz { 183210c6ec49SMike Kravetz struct page *page, *t_page; 1833d6ef19e2SSidhartha Kumar struct folio *folio; 183410c6ec49SMike Kravetz 183510c6ec49SMike Kravetz list_for_each_entry_safe(page, t_page, list, lru) { 1836d6ef19e2SSidhartha Kumar folio = page_folio(page); 1837d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, folio, false); 183810c6ec49SMike Kravetz cond_resched(); 183910c6ec49SMike Kravetz } 184010c6ec49SMike Kravetz } 184110c6ec49SMike Kravetz 1842e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size) 1843e5ff2159SAndi Kleen { 1844e5ff2159SAndi Kleen struct hstate *h; 1845e5ff2159SAndi Kleen 1846e5ff2159SAndi Kleen for_each_hstate(h) { 1847e5ff2159SAndi Kleen if (huge_page_size(h) == size) 1848e5ff2159SAndi Kleen return h; 1849e5ff2159SAndi Kleen } 1850e5ff2159SAndi Kleen return NULL; 1851e5ff2159SAndi Kleen } 1852e5ff2159SAndi Kleen 1853db71ef79SMike Kravetz void free_huge_page(struct page *page) 185427a85ef1SDavid Gibson { 1855a5516438SAndi Kleen /* 1856a5516438SAndi Kleen * Can't pass hstate in here because it is called from the 1857a5516438SAndi Kleen * compound page destructor. 1858a5516438SAndi Kleen */ 18590356c4b9SSidhartha Kumar struct folio *folio = page_folio(page); 18600356c4b9SSidhartha Kumar struct hstate *h = folio_hstate(folio); 18610356c4b9SSidhartha Kumar int nid = folio_nid(folio); 18620356c4b9SSidhartha Kumar struct hugepage_subpool *spool = hugetlb_folio_subpool(folio); 186307443a85SJoonsoo Kim bool restore_reserve; 1864db71ef79SMike Kravetz unsigned long flags; 186527a85ef1SDavid Gibson 18660356c4b9SSidhartha Kumar VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); 18670356c4b9SSidhartha Kumar VM_BUG_ON_FOLIO(folio_mapcount(folio), folio); 18688ace22bcSYongkai Wu 18690356c4b9SSidhartha Kumar hugetlb_set_folio_subpool(folio, NULL); 18700356c4b9SSidhartha Kumar if (folio_test_anon(folio)) 18710356c4b9SSidhartha Kumar __ClearPageAnonExclusive(&folio->page); 18720356c4b9SSidhartha Kumar folio->mapping = NULL; 18730356c4b9SSidhartha Kumar restore_reserve = folio_test_hugetlb_restore_reserve(folio); 18740356c4b9SSidhartha Kumar folio_clear_hugetlb_restore_reserve(folio); 187527a85ef1SDavid Gibson 18761c5ecae3SMike Kravetz /* 1877d6995da3SMike Kravetz * If HPageRestoreReserve was set on page, page allocation consumed a 18780919e1b6SMike Kravetz * reservation. If the page was associated with a subpool, there 18790919e1b6SMike Kravetz * would have been a page reserved in the subpool before allocation 18800919e1b6SMike Kravetz * via hugepage_subpool_get_pages(). Since we are 'restoring' the 18816c26d310SMiaohe Lin * reservation, do not call hugepage_subpool_put_pages() as this will 18820919e1b6SMike Kravetz * remove the reserved page from the subpool. 18830919e1b6SMike Kravetz */ 18840919e1b6SMike Kravetz if (!restore_reserve) { 18850919e1b6SMike Kravetz /* 18860919e1b6SMike Kravetz * A return code of zero implies that the subpool will be 18870919e1b6SMike Kravetz * under its minimum size if the reservation is not restored 18880919e1b6SMike Kravetz * after page is free. Therefore, force restore_reserve 18890919e1b6SMike Kravetz * operation. 18901c5ecae3SMike Kravetz */ 18911c5ecae3SMike Kravetz if (hugepage_subpool_put_pages(spool, 1) == 0) 18921c5ecae3SMike Kravetz restore_reserve = true; 18930919e1b6SMike Kravetz } 18941c5ecae3SMike Kravetz 1895db71ef79SMike Kravetz spin_lock_irqsave(&hugetlb_lock, flags); 18960356c4b9SSidhartha Kumar folio_clear_hugetlb_migratable(folio); 1897d4ab0316SSidhartha Kumar hugetlb_cgroup_uncharge_folio(hstate_index(h), 1898d4ab0316SSidhartha Kumar pages_per_huge_page(h), folio); 1899d4ab0316SSidhartha Kumar hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h), 1900d4ab0316SSidhartha Kumar pages_per_huge_page(h), folio); 190107443a85SJoonsoo Kim if (restore_reserve) 190207443a85SJoonsoo Kim h->resv_huge_pages++; 190307443a85SJoonsoo Kim 19040356c4b9SSidhartha Kumar if (folio_test_hugetlb_temporary(folio)) { 1905cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, folio, false); 1906db71ef79SMike Kravetz spin_unlock_irqrestore(&hugetlb_lock, flags); 1907d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, folio, true); 1908ab5ac90aSMichal Hocko } else if (h->surplus_huge_pages_node[nid]) { 19090edaecfaSAneesh Kumar K.V /* remove the page from active list */ 1910cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, folio, true); 1911db71ef79SMike Kravetz spin_unlock_irqrestore(&hugetlb_lock, flags); 1912d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, folio, true); 19137893d1d5SAdam Litke } else { 19145d3a551cSWill Deacon arch_clear_hugepage_flags(page); 1915240d67a8SSidhartha Kumar enqueue_hugetlb_folio(h, folio); 1916db71ef79SMike Kravetz spin_unlock_irqrestore(&hugetlb_lock, flags); 191727a85ef1SDavid Gibson } 19181121828aSMike Kravetz } 191927a85ef1SDavid Gibson 1920d3d99fccSOscar Salvador /* 1921d3d99fccSOscar Salvador * Must be called with the hugetlb lock held 1922d3d99fccSOscar Salvador */ 1923d3d99fccSOscar Salvador static void __prep_account_new_huge_page(struct hstate *h, int nid) 1924d3d99fccSOscar Salvador { 1925d3d99fccSOscar Salvador lockdep_assert_held(&hugetlb_lock); 1926d3d99fccSOscar Salvador h->nr_huge_pages++; 1927d3d99fccSOscar Salvador h->nr_huge_pages_node[nid]++; 1928d3d99fccSOscar Salvador } 1929d3d99fccSOscar Salvador 1930de656ed3SSidhartha Kumar static void __prep_new_hugetlb_folio(struct hstate *h, struct folio *folio) 1931b7ba30c6SAndi Kleen { 1932de656ed3SSidhartha Kumar hugetlb_vmemmap_optimize(h, &folio->page); 1933de656ed3SSidhartha Kumar INIT_LIST_HEAD(&folio->lru); 19349fd33058SSidhartha Kumar folio_set_compound_dtor(folio, HUGETLB_PAGE_DTOR); 1935de656ed3SSidhartha Kumar hugetlb_set_folio_subpool(folio, NULL); 1936de656ed3SSidhartha Kumar set_hugetlb_cgroup(folio, NULL); 1937de656ed3SSidhartha Kumar set_hugetlb_cgroup_rsvd(folio, NULL); 1938d3d99fccSOscar Salvador } 1939d3d99fccSOscar Salvador 1940d1c60955SSidhartha Kumar static void prep_new_hugetlb_folio(struct hstate *h, struct folio *folio, int nid) 1941d3d99fccSOscar Salvador { 1942de656ed3SSidhartha Kumar __prep_new_hugetlb_folio(h, folio); 1943db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 1944d3d99fccSOscar Salvador __prep_account_new_huge_page(h, nid); 1945db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 1946b7ba30c6SAndi Kleen } 1947b7ba30c6SAndi Kleen 1948d1c60955SSidhartha Kumar static bool __prep_compound_gigantic_folio(struct folio *folio, 1949d1c60955SSidhartha Kumar unsigned int order, bool demote) 195020a0307cSWu Fengguang { 19517118fc29SMike Kravetz int i, j; 195220a0307cSWu Fengguang int nr_pages = 1 << order; 195314455eabSCheng Li struct page *p; 195420a0307cSWu Fengguang 1955d1c60955SSidhartha Kumar __folio_clear_reserved(folio); 1956d1c60955SSidhartha Kumar __folio_set_head(folio); 1957c45bc55aSSidhartha Kumar /* we rely on prep_new_hugetlb_folio to set the destructor */ 195804a42e72SSidhartha Kumar folio_set_order(folio, order); 19592b21624fSMike Kravetz for (i = 0; i < nr_pages; i++) { 1960d1c60955SSidhartha Kumar p = folio_page(folio, i); 196114455eabSCheng Li 1962ef5a22beSAndrea Arcangeli /* 1963ef5a22beSAndrea Arcangeli * For gigantic hugepages allocated through bootmem at 1964ef5a22beSAndrea Arcangeli * boot, it's safer to be consistent with the not-gigantic 1965ef5a22beSAndrea Arcangeli * hugepages and clear the PG_reserved bit from all tail pages 19667c8de358SEthon Paul * too. Otherwise drivers using get_user_pages() to access tail 1967ef5a22beSAndrea Arcangeli * pages may get the reference counting wrong if they see 1968ef5a22beSAndrea Arcangeli * PG_reserved set on a tail page (despite the head page not 1969ef5a22beSAndrea Arcangeli * having PG_reserved set). Enforcing this consistency between 1970ef5a22beSAndrea Arcangeli * head and tail pages allows drivers to optimize away a check 1971ef5a22beSAndrea Arcangeli * on the head page when they need know if put_page() is needed 1972ef5a22beSAndrea Arcangeli * after get_user_pages(). 1973ef5a22beSAndrea Arcangeli */ 19747fb0728aSMike Kravetz if (i != 0) /* head page cleared above */ 1975ef5a22beSAndrea Arcangeli __ClearPageReserved(p); 19767118fc29SMike Kravetz /* 19777118fc29SMike Kravetz * Subtle and very unlikely 19787118fc29SMike Kravetz * 19797118fc29SMike Kravetz * Gigantic 'page allocators' such as memblock or cma will 19807118fc29SMike Kravetz * return a set of pages with each page ref counted. We need 19817118fc29SMike Kravetz * to turn this set of pages into a compound page with tail 19827118fc29SMike Kravetz * page ref counts set to zero. Code such as speculative page 19837118fc29SMike Kravetz * cache adding could take a ref on a 'to be' tail page. 19847118fc29SMike Kravetz * We need to respect any increased ref count, and only set 19857118fc29SMike Kravetz * the ref count to zero if count is currently 1. If count 1986416d85edSMike Kravetz * is not 1, we return an error. An error return indicates 1987416d85edSMike Kravetz * the set of pages can not be converted to a gigantic page. 1988416d85edSMike Kravetz * The caller who allocated the pages should then discard the 1989416d85edSMike Kravetz * pages using the appropriate free interface. 199034d9e35bSMike Kravetz * 199134d9e35bSMike Kravetz * In the case of demote, the ref count will be zero. 19927118fc29SMike Kravetz */ 199334d9e35bSMike Kravetz if (!demote) { 19947118fc29SMike Kravetz if (!page_ref_freeze(p, 1)) { 1995416d85edSMike Kravetz pr_warn("HugeTLB page can not be used due to unexpected inflated ref count\n"); 19967118fc29SMike Kravetz goto out_error; 19977118fc29SMike Kravetz } 199834d9e35bSMike Kravetz } else { 199934d9e35bSMike Kravetz VM_BUG_ON_PAGE(page_count(p), p); 200034d9e35bSMike Kravetz } 20012b21624fSMike Kravetz if (i != 0) 2002d1c60955SSidhartha Kumar set_compound_head(p, &folio->page); 200320a0307cSWu Fengguang } 2004d1c60955SSidhartha Kumar atomic_set(folio_mapcount_ptr(folio), -1); 2005d1c60955SSidhartha Kumar atomic_set(folio_subpages_mapcount_ptr(folio), 0); 2006d1c60955SSidhartha Kumar atomic_set(folio_pincount_ptr(folio), 0); 20077118fc29SMike Kravetz return true; 20087118fc29SMike Kravetz 20097118fc29SMike Kravetz out_error: 20102b21624fSMike Kravetz /* undo page modifications made above */ 20112b21624fSMike Kravetz for (j = 0; j < i; j++) { 2012d1c60955SSidhartha Kumar p = folio_page(folio, j); 20132b21624fSMike Kravetz if (j != 0) 20147118fc29SMike Kravetz clear_compound_head(p); 20157118fc29SMike Kravetz set_page_refcounted(p); 20167118fc29SMike Kravetz } 20177118fc29SMike Kravetz /* need to clear PG_reserved on remaining tail pages */ 201814455eabSCheng Li for (; j < nr_pages; j++) { 2019d1c60955SSidhartha Kumar p = folio_page(folio, j); 20207118fc29SMike Kravetz __ClearPageReserved(p); 202114455eabSCheng Li } 202204a42e72SSidhartha Kumar folio_set_order(folio, 0); 2023d1c60955SSidhartha Kumar __folio_clear_head(folio); 20247118fc29SMike Kravetz return false; 202520a0307cSWu Fengguang } 202620a0307cSWu Fengguang 2027d1c60955SSidhartha Kumar static bool prep_compound_gigantic_folio(struct folio *folio, 20288531fc6fSMike Kravetz unsigned int order) 20298531fc6fSMike Kravetz { 2030d1c60955SSidhartha Kumar return __prep_compound_gigantic_folio(folio, order, false); 2031d1c60955SSidhartha Kumar } 2032d1c60955SSidhartha Kumar 2033d1c60955SSidhartha Kumar static bool prep_compound_gigantic_folio_for_demote(struct folio *folio, 2034d1c60955SSidhartha Kumar unsigned int order) 2035d1c60955SSidhartha Kumar { 2036d1c60955SSidhartha Kumar return __prep_compound_gigantic_folio(folio, order, true); 20378531fc6fSMike Kravetz } 20388531fc6fSMike Kravetz 20397795912cSAndrew Morton /* 20407795912cSAndrew Morton * PageHuge() only returns true for hugetlbfs pages, but not for normal or 20417795912cSAndrew Morton * transparent huge pages. See the PageTransHuge() documentation for more 20427795912cSAndrew Morton * details. 20437795912cSAndrew Morton */ 204420a0307cSWu Fengguang int PageHuge(struct page *page) 204520a0307cSWu Fengguang { 204620a0307cSWu Fengguang if (!PageCompound(page)) 204720a0307cSWu Fengguang return 0; 204820a0307cSWu Fengguang 204920a0307cSWu Fengguang page = compound_head(page); 2050f1e61557SKirill A. Shutemov return page[1].compound_dtor == HUGETLB_PAGE_DTOR; 205120a0307cSWu Fengguang } 205243131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge); 205343131e14SNaoya Horiguchi 205427c73ae7SAndrea Arcangeli /* 205527c73ae7SAndrea Arcangeli * PageHeadHuge() only returns true for hugetlbfs head page, but not for 205627c73ae7SAndrea Arcangeli * normal or transparent huge pages. 205727c73ae7SAndrea Arcangeli */ 205827c73ae7SAndrea Arcangeli int PageHeadHuge(struct page *page_head) 205927c73ae7SAndrea Arcangeli { 206027c73ae7SAndrea Arcangeli if (!PageHead(page_head)) 206127c73ae7SAndrea Arcangeli return 0; 206227c73ae7SAndrea Arcangeli 2063d4af73e3SVlastimil Babka return page_head[1].compound_dtor == HUGETLB_PAGE_DTOR; 206427c73ae7SAndrea Arcangeli } 20654e936eccSDavid Howells EXPORT_SYMBOL_GPL(PageHeadHuge); 206627c73ae7SAndrea Arcangeli 2067c0d0381aSMike Kravetz /* 2068c0d0381aSMike Kravetz * Find and lock address space (mapping) in write mode. 2069c0d0381aSMike Kravetz * 2070336bf30eSMike Kravetz * Upon entry, the page is locked which means that page_mapping() is 2071336bf30eSMike Kravetz * stable. Due to locking order, we can only trylock_write. If we can 2072336bf30eSMike Kravetz * not get the lock, simply return NULL to caller. 2073c0d0381aSMike Kravetz */ 2074c0d0381aSMike Kravetz struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage) 2075c0d0381aSMike Kravetz { 2076336bf30eSMike Kravetz struct address_space *mapping = page_mapping(hpage); 2077c0d0381aSMike Kravetz 2078c0d0381aSMike Kravetz if (!mapping) 2079c0d0381aSMike Kravetz return mapping; 2080c0d0381aSMike Kravetz 2081c0d0381aSMike Kravetz if (i_mmap_trylock_write(mapping)) 2082c0d0381aSMike Kravetz return mapping; 2083c0d0381aSMike Kravetz 2084c0d0381aSMike Kravetz return NULL; 2085c0d0381aSMike Kravetz } 2086c0d0381aSMike Kravetz 2087fe19bd3dSHugh Dickins pgoff_t hugetlb_basepage_index(struct page *page) 208813d60f4bSZhang Yi { 208913d60f4bSZhang Yi struct page *page_head = compound_head(page); 209013d60f4bSZhang Yi pgoff_t index = page_index(page_head); 209113d60f4bSZhang Yi unsigned long compound_idx; 209213d60f4bSZhang Yi 209313d60f4bSZhang Yi if (compound_order(page_head) >= MAX_ORDER) 209413d60f4bSZhang Yi compound_idx = page_to_pfn(page) - page_to_pfn(page_head); 209513d60f4bSZhang Yi else 209613d60f4bSZhang Yi compound_idx = page - page_head; 209713d60f4bSZhang Yi 209813d60f4bSZhang Yi return (index << compound_order(page_head)) + compound_idx; 209913d60f4bSZhang Yi } 210013d60f4bSZhang Yi 210119fc1a7eSSidhartha Kumar static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, 2102f60858f9SMike Kravetz gfp_t gfp_mask, int nid, nodemask_t *nmask, 2103f60858f9SMike Kravetz nodemask_t *node_alloc_noretry) 21041da177e4SLinus Torvalds { 2105af0fb9dfSMichal Hocko int order = huge_page_order(h); 21061da177e4SLinus Torvalds struct page *page; 2107f60858f9SMike Kravetz bool alloc_try_hard = true; 21082b21624fSMike Kravetz bool retry = true; 2109f96efd58SJoe Jin 2110f60858f9SMike Kravetz /* 2111f60858f9SMike Kravetz * By default we always try hard to allocate the page with 2112f60858f9SMike Kravetz * __GFP_RETRY_MAYFAIL flag. However, if we are allocating pages in 2113f60858f9SMike Kravetz * a loop (to adjust global huge page counts) and previous allocation 2114f60858f9SMike Kravetz * failed, do not continue to try hard on the same node. Use the 2115f60858f9SMike Kravetz * node_alloc_noretry bitmap to manage this state information. 2116f60858f9SMike Kravetz */ 2117f60858f9SMike Kravetz if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry)) 2118f60858f9SMike Kravetz alloc_try_hard = false; 2119f60858f9SMike Kravetz gfp_mask |= __GFP_COMP|__GFP_NOWARN; 2120f60858f9SMike Kravetz if (alloc_try_hard) 2121f60858f9SMike Kravetz gfp_mask |= __GFP_RETRY_MAYFAIL; 2122af0fb9dfSMichal Hocko if (nid == NUMA_NO_NODE) 2123af0fb9dfSMichal Hocko nid = numa_mem_id(); 21242b21624fSMike Kravetz retry: 212584172f4bSMatthew Wilcox (Oracle) page = __alloc_pages(gfp_mask, order, nid, nmask); 21262b21624fSMike Kravetz 21272b21624fSMike Kravetz /* Freeze head page */ 21282b21624fSMike Kravetz if (page && !page_ref_freeze(page, 1)) { 21292b21624fSMike Kravetz __free_pages(page, order); 21302b21624fSMike Kravetz if (retry) { /* retry once */ 21312b21624fSMike Kravetz retry = false; 21322b21624fSMike Kravetz goto retry; 21332b21624fSMike Kravetz } 21342b21624fSMike Kravetz /* WOW! twice in a row. */ 21352b21624fSMike Kravetz pr_warn("HugeTLB head page unexpected inflated ref count\n"); 21362b21624fSMike Kravetz page = NULL; 21372b21624fSMike Kravetz } 21382b21624fSMike Kravetz 2139f60858f9SMike Kravetz /* 2140f60858f9SMike Kravetz * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this 2141f60858f9SMike Kravetz * indicates an overall state change. Clear bit so that we resume 2142f60858f9SMike Kravetz * normal 'try hard' allocations. 2143f60858f9SMike Kravetz */ 2144f60858f9SMike Kravetz if (node_alloc_noretry && page && !alloc_try_hard) 2145f60858f9SMike Kravetz node_clear(nid, *node_alloc_noretry); 2146f60858f9SMike Kravetz 2147f60858f9SMike Kravetz /* 2148f60858f9SMike Kravetz * If we tried hard to get a page but failed, set bit so that 2149f60858f9SMike Kravetz * subsequent attempts will not try as hard until there is an 2150f60858f9SMike Kravetz * overall state change. 2151f60858f9SMike Kravetz */ 2152f60858f9SMike Kravetz if (node_alloc_noretry && !page && alloc_try_hard) 2153f60858f9SMike Kravetz node_set(nid, *node_alloc_noretry); 2154f60858f9SMike Kravetz 215519fc1a7eSSidhartha Kumar if (!page) { 215619fc1a7eSSidhartha Kumar __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 215719fc1a7eSSidhartha Kumar return NULL; 215819fc1a7eSSidhartha Kumar } 215919fc1a7eSSidhartha Kumar 216019fc1a7eSSidhartha Kumar __count_vm_event(HTLB_BUDDY_PGALLOC); 216119fc1a7eSSidhartha Kumar return page_folio(page); 216263b4613cSNishanth Aravamudan } 216363b4613cSNishanth Aravamudan 2164af0fb9dfSMichal Hocko /* 21650c397daeSMichal Hocko * Common helper to allocate a fresh hugetlb page. All specific allocators 21660c397daeSMichal Hocko * should use this function to get new hugetlb pages 21672b21624fSMike Kravetz * 21682b21624fSMike Kravetz * Note that returned page is 'frozen': ref count of head page and all tail 21692b21624fSMike Kravetz * pages is zero. 21700c397daeSMichal Hocko */ 217119fc1a7eSSidhartha Kumar static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h, 2172f60858f9SMike Kravetz gfp_t gfp_mask, int nid, nodemask_t *nmask, 2173f60858f9SMike Kravetz nodemask_t *node_alloc_noretry) 21740c397daeSMichal Hocko { 21757f325a8dSSidhartha Kumar struct folio *folio; 21767118fc29SMike Kravetz bool retry = false; 21770c397daeSMichal Hocko 21787118fc29SMike Kravetz retry: 21790c397daeSMichal Hocko if (hstate_is_gigantic(h)) 218019fc1a7eSSidhartha Kumar folio = alloc_gigantic_folio(h, gfp_mask, nid, nmask); 21810c397daeSMichal Hocko else 218219fc1a7eSSidhartha Kumar folio = alloc_buddy_hugetlb_folio(h, gfp_mask, 2183f60858f9SMike Kravetz nid, nmask, node_alloc_noretry); 218419fc1a7eSSidhartha Kumar if (!folio) 21850c397daeSMichal Hocko return NULL; 21867118fc29SMike Kravetz if (hstate_is_gigantic(h)) { 2187d1c60955SSidhartha Kumar if (!prep_compound_gigantic_folio(folio, huge_page_order(h))) { 21887118fc29SMike Kravetz /* 21897118fc29SMike Kravetz * Rare failure to convert pages to compound page. 21907118fc29SMike Kravetz * Free pages and try again - ONCE! 21917118fc29SMike Kravetz */ 21927f325a8dSSidhartha Kumar free_gigantic_folio(folio, huge_page_order(h)); 21937118fc29SMike Kravetz if (!retry) { 21947118fc29SMike Kravetz retry = true; 21957118fc29SMike Kravetz goto retry; 21967118fc29SMike Kravetz } 21977118fc29SMike Kravetz return NULL; 21987118fc29SMike Kravetz } 21997118fc29SMike Kravetz } 2200d1c60955SSidhartha Kumar prep_new_hugetlb_folio(h, folio, folio_nid(folio)); 22010c397daeSMichal Hocko 220219fc1a7eSSidhartha Kumar return folio; 22030c397daeSMichal Hocko } 22040c397daeSMichal Hocko 22050c397daeSMichal Hocko /* 2206af0fb9dfSMichal Hocko * Allocates a fresh page to the hugetlb allocator pool in the node interleaved 2207af0fb9dfSMichal Hocko * manner. 2208af0fb9dfSMichal Hocko */ 2209f60858f9SMike Kravetz static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, 2210f60858f9SMike Kravetz nodemask_t *node_alloc_noretry) 2211b2261026SJoonsoo Kim { 221219fc1a7eSSidhartha Kumar struct folio *folio; 2213b2261026SJoonsoo Kim int nr_nodes, node; 2214af0fb9dfSMichal Hocko gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; 2215b2261026SJoonsoo Kim 2216b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 221719fc1a7eSSidhartha Kumar folio = alloc_fresh_hugetlb_folio(h, gfp_mask, node, 221819fc1a7eSSidhartha Kumar nodes_allowed, node_alloc_noretry); 221919fc1a7eSSidhartha Kumar if (folio) { 222019fc1a7eSSidhartha Kumar free_huge_page(&folio->page); /* free it into the hugepage allocator */ 222119fc1a7eSSidhartha Kumar return 1; 222219fc1a7eSSidhartha Kumar } 2223b2261026SJoonsoo Kim } 2224b2261026SJoonsoo Kim 2225af0fb9dfSMichal Hocko return 0; 2226b2261026SJoonsoo Kim } 2227b2261026SJoonsoo Kim 2228e8c5c824SLee Schermerhorn /* 222910c6ec49SMike Kravetz * Remove huge page from pool from next node to free. Attempt to keep 223010c6ec49SMike Kravetz * persistent huge pages more or less balanced over allowed nodes. 223110c6ec49SMike Kravetz * This routine only 'removes' the hugetlb page. The caller must make 223210c6ec49SMike Kravetz * an additional call to free the page to low level allocators. 2233e8c5c824SLee Schermerhorn * Called with hugetlb_lock locked. 2234e8c5c824SLee Schermerhorn */ 223510c6ec49SMike Kravetz static struct page *remove_pool_huge_page(struct hstate *h, 223610c6ec49SMike Kravetz nodemask_t *nodes_allowed, 22376ae11b27SLee Schermerhorn bool acct_surplus) 2238e8c5c824SLee Schermerhorn { 2239b2261026SJoonsoo Kim int nr_nodes, node; 224010c6ec49SMike Kravetz struct page *page = NULL; 2241cfd5082bSSidhartha Kumar struct folio *folio; 2242e8c5c824SLee Schermerhorn 22439487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 2244b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 2245685f3457SLee Schermerhorn /* 2246685f3457SLee Schermerhorn * If we're returning unused surplus pages, only examine 2247685f3457SLee Schermerhorn * nodes with surplus pages. 2248685f3457SLee Schermerhorn */ 2249b2261026SJoonsoo Kim if ((!acct_surplus || h->surplus_huge_pages_node[node]) && 2250b2261026SJoonsoo Kim !list_empty(&h->hugepage_freelists[node])) { 225110c6ec49SMike Kravetz page = list_entry(h->hugepage_freelists[node].next, 2252e8c5c824SLee Schermerhorn struct page, lru); 2253cfd5082bSSidhartha Kumar folio = page_folio(page); 2254cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, folio, acct_surplus); 22559a76db09SLee Schermerhorn break; 2256e8c5c824SLee Schermerhorn } 2257b2261026SJoonsoo Kim } 2258e8c5c824SLee Schermerhorn 225910c6ec49SMike Kravetz return page; 2260e8c5c824SLee Schermerhorn } 2261e8c5c824SLee Schermerhorn 2262c8721bbbSNaoya Horiguchi /* 2263c8721bbbSNaoya Horiguchi * Dissolve a given free hugepage into free buddy pages. This function does 2264faf53defSNaoya Horiguchi * nothing for in-use hugepages and non-hugepages. 2265faf53defSNaoya Horiguchi * This function returns values like below: 2266faf53defSNaoya Horiguchi * 2267ad2fa371SMuchun Song * -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages 2268ad2fa371SMuchun Song * when the system is under memory pressure and the feature of 2269ad2fa371SMuchun Song * freeing unused vmemmap pages associated with each hugetlb page 2270ad2fa371SMuchun Song * is enabled. 2271faf53defSNaoya Horiguchi * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use 2272faf53defSNaoya Horiguchi * (allocated or reserved.) 2273faf53defSNaoya Horiguchi * 0: successfully dissolved free hugepages or the page is not a 2274faf53defSNaoya Horiguchi * hugepage (considered as already dissolved) 2275c8721bbbSNaoya Horiguchi */ 2276c3114a84SAnshuman Khandual int dissolve_free_huge_page(struct page *page) 2277c8721bbbSNaoya Horiguchi { 22786bc9b564SNaoya Horiguchi int rc = -EBUSY; 22791a7cdab5SSidhartha Kumar struct folio *folio = page_folio(page); 2280082d5b6bSGerald Schaefer 22817ffddd49SMuchun Song retry: 2282faf53defSNaoya Horiguchi /* Not to disrupt normal path by vainly holding hugetlb_lock */ 22831a7cdab5SSidhartha Kumar if (!folio_test_hugetlb(folio)) 2284faf53defSNaoya Horiguchi return 0; 2285faf53defSNaoya Horiguchi 2286db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 22871a7cdab5SSidhartha Kumar if (!folio_test_hugetlb(folio)) { 2288faf53defSNaoya Horiguchi rc = 0; 2289faf53defSNaoya Horiguchi goto out; 2290faf53defSNaoya Horiguchi } 2291faf53defSNaoya Horiguchi 22921a7cdab5SSidhartha Kumar if (!folio_ref_count(folio)) { 22931a7cdab5SSidhartha Kumar struct hstate *h = folio_hstate(folio); 22948346d69dSXin Hao if (!available_huge_pages(h)) 2295082d5b6bSGerald Schaefer goto out; 22967ffddd49SMuchun Song 22977ffddd49SMuchun Song /* 22987ffddd49SMuchun Song * We should make sure that the page is already on the free list 22997ffddd49SMuchun Song * when it is dissolved. 23007ffddd49SMuchun Song */ 23011a7cdab5SSidhartha Kumar if (unlikely(!folio_test_hugetlb_freed(folio))) { 2302db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 23037ffddd49SMuchun Song cond_resched(); 23047ffddd49SMuchun Song 23057ffddd49SMuchun Song /* 23067ffddd49SMuchun Song * Theoretically, we should return -EBUSY when we 23077ffddd49SMuchun Song * encounter this race. In fact, we have a chance 23087ffddd49SMuchun Song * to successfully dissolve the page if we do a 23097ffddd49SMuchun Song * retry. Because the race window is quite small. 23107ffddd49SMuchun Song * If we seize this opportunity, it is an optimization 23117ffddd49SMuchun Song * for increasing the success rate of dissolving page. 23127ffddd49SMuchun Song */ 23137ffddd49SMuchun Song goto retry; 23147ffddd49SMuchun Song } 23157ffddd49SMuchun Song 2316cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, folio, false); 2317ad2fa371SMuchun Song h->max_huge_pages--; 2318ad2fa371SMuchun Song spin_unlock_irq(&hugetlb_lock); 2319ad2fa371SMuchun Song 2320c3114a84SAnshuman Khandual /* 2321d6ef19e2SSidhartha Kumar * Normally update_and_free_hugtlb_folio will allocate required vmemmmap 2322d6ef19e2SSidhartha Kumar * before freeing the page. update_and_free_hugtlb_folio will fail to 2323ad2fa371SMuchun Song * free the page if it can not allocate required vmemmap. We 2324ad2fa371SMuchun Song * need to adjust max_huge_pages if the page is not freed. 2325ad2fa371SMuchun Song * Attempt to allocate vmemmmap here so that we can take 2326ad2fa371SMuchun Song * appropriate action on failure. 2327ad2fa371SMuchun Song */ 23281a7cdab5SSidhartha Kumar rc = hugetlb_vmemmap_restore(h, &folio->page); 2329ad2fa371SMuchun Song if (!rc) { 2330d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, folio, false); 2331ad2fa371SMuchun Song } else { 2332ad2fa371SMuchun Song spin_lock_irq(&hugetlb_lock); 23332f6c57d6SSidhartha Kumar add_hugetlb_folio(h, folio, false); 2334ad2fa371SMuchun Song h->max_huge_pages++; 2335ad2fa371SMuchun Song spin_unlock_irq(&hugetlb_lock); 2336ad2fa371SMuchun Song } 2337ad2fa371SMuchun Song 2338ad2fa371SMuchun Song return rc; 2339c8721bbbSNaoya Horiguchi } 2340082d5b6bSGerald Schaefer out: 2341db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 2342082d5b6bSGerald Schaefer return rc; 2343c8721bbbSNaoya Horiguchi } 2344c8721bbbSNaoya Horiguchi 2345c8721bbbSNaoya Horiguchi /* 2346c8721bbbSNaoya Horiguchi * Dissolve free hugepages in a given pfn range. Used by memory hotplug to 2347c8721bbbSNaoya Horiguchi * make specified memory blocks removable from the system. 23482247bb33SGerald Schaefer * Note that this will dissolve a free gigantic hugepage completely, if any 23492247bb33SGerald Schaefer * part of it lies within the given range. 2350082d5b6bSGerald Schaefer * Also note that if dissolve_free_huge_page() returns with an error, all 2351082d5b6bSGerald Schaefer * free hugepages that were dissolved before that error are lost. 2352c8721bbbSNaoya Horiguchi */ 2353082d5b6bSGerald Schaefer int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) 2354c8721bbbSNaoya Horiguchi { 2355c8721bbbSNaoya Horiguchi unsigned long pfn; 2356eb03aa00SGerald Schaefer struct page *page; 2357082d5b6bSGerald Schaefer int rc = 0; 2358dc2628f3SMuchun Song unsigned int order; 2359dc2628f3SMuchun Song struct hstate *h; 2360c8721bbbSNaoya Horiguchi 2361d0177639SLi Zhong if (!hugepages_supported()) 2362082d5b6bSGerald Schaefer return rc; 2363d0177639SLi Zhong 2364dc2628f3SMuchun Song order = huge_page_order(&default_hstate); 2365dc2628f3SMuchun Song for_each_hstate(h) 2366dc2628f3SMuchun Song order = min(order, huge_page_order(h)); 2367dc2628f3SMuchun Song 2368dc2628f3SMuchun Song for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) { 2369eb03aa00SGerald Schaefer page = pfn_to_page(pfn); 2370eb03aa00SGerald Schaefer rc = dissolve_free_huge_page(page); 2371eb03aa00SGerald Schaefer if (rc) 2372082d5b6bSGerald Schaefer break; 2373eb03aa00SGerald Schaefer } 2374082d5b6bSGerald Schaefer 2375082d5b6bSGerald Schaefer return rc; 2376c8721bbbSNaoya Horiguchi } 2377c8721bbbSNaoya Horiguchi 2378ab5ac90aSMichal Hocko /* 2379ab5ac90aSMichal Hocko * Allocates a fresh surplus page from the page allocator. 2380ab5ac90aSMichal Hocko */ 23810c397daeSMichal Hocko static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask, 23822b21624fSMike Kravetz int nid, nodemask_t *nmask) 23837893d1d5SAdam Litke { 238419fc1a7eSSidhartha Kumar struct folio *folio = NULL; 23857893d1d5SAdam Litke 2386bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 2387aa888a74SAndi Kleen return NULL; 2388aa888a74SAndi Kleen 2389db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 23909980d744SMichal Hocko if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) 23919980d744SMichal Hocko goto out_unlock; 2392db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 2393d1c3fb1fSNishanth Aravamudan 239419fc1a7eSSidhartha Kumar folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); 239519fc1a7eSSidhartha Kumar if (!folio) 23960c397daeSMichal Hocko return NULL; 2397d1c3fb1fSNishanth Aravamudan 2398db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 23999980d744SMichal Hocko /* 24009980d744SMichal Hocko * We could have raced with the pool size change. 24019980d744SMichal Hocko * Double check that and simply deallocate the new page 24029980d744SMichal Hocko * if we would end up overcommiting the surpluses. Abuse 24039980d744SMichal Hocko * temporary page to workaround the nasty free_huge_page 24049980d744SMichal Hocko * codeflow 24059980d744SMichal Hocko */ 24069980d744SMichal Hocko if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { 240719fc1a7eSSidhartha Kumar folio_set_hugetlb_temporary(folio); 2408db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 240919fc1a7eSSidhartha Kumar free_huge_page(&folio->page); 24102bf753e6SKai Shen return NULL; 2411b65a4edaSMike Kravetz } 2412b65a4edaSMike Kravetz 24139980d744SMichal Hocko h->surplus_huge_pages++; 241419fc1a7eSSidhartha Kumar h->surplus_huge_pages_node[folio_nid(folio)]++; 24159980d744SMichal Hocko 24169980d744SMichal Hocko out_unlock: 2417db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 24187893d1d5SAdam Litke 241919fc1a7eSSidhartha Kumar return &folio->page; 24207893d1d5SAdam Litke } 24217893d1d5SAdam Litke 2422bbe88753SJoonsoo Kim static struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask, 2423ab5ac90aSMichal Hocko int nid, nodemask_t *nmask) 2424ab5ac90aSMichal Hocko { 242519fc1a7eSSidhartha Kumar struct folio *folio; 2426ab5ac90aSMichal Hocko 2427ab5ac90aSMichal Hocko if (hstate_is_gigantic(h)) 2428ab5ac90aSMichal Hocko return NULL; 2429ab5ac90aSMichal Hocko 243019fc1a7eSSidhartha Kumar folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); 243119fc1a7eSSidhartha Kumar if (!folio) 2432ab5ac90aSMichal Hocko return NULL; 2433ab5ac90aSMichal Hocko 24342b21624fSMike Kravetz /* fresh huge pages are frozen */ 243519fc1a7eSSidhartha Kumar folio_ref_unfreeze(folio, 1); 2436ab5ac90aSMichal Hocko /* 2437ab5ac90aSMichal Hocko * We do not account these pages as surplus because they are only 2438ab5ac90aSMichal Hocko * temporary and will be released properly on the last reference 2439ab5ac90aSMichal Hocko */ 244019fc1a7eSSidhartha Kumar folio_set_hugetlb_temporary(folio); 2441ab5ac90aSMichal Hocko 244219fc1a7eSSidhartha Kumar return &folio->page; 2443ab5ac90aSMichal Hocko } 2444ab5ac90aSMichal Hocko 2445e4e574b7SAdam Litke /* 2446099730d6SDave Hansen * Use the VMA's mpolicy to allocate a huge page from the buddy. 2447099730d6SDave Hansen */ 2448e0ec90eeSDave Hansen static 24490c397daeSMichal Hocko struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h, 2450099730d6SDave Hansen struct vm_area_struct *vma, unsigned long addr) 2451099730d6SDave Hansen { 2452cfcaa66fSBen Widawsky struct page *page = NULL; 2453aaf14e40SMichal Hocko struct mempolicy *mpol; 2454aaf14e40SMichal Hocko gfp_t gfp_mask = htlb_alloc_mask(h); 2455aaf14e40SMichal Hocko int nid; 2456aaf14e40SMichal Hocko nodemask_t *nodemask; 2457aaf14e40SMichal Hocko 2458aaf14e40SMichal Hocko nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask); 2459cfcaa66fSBen Widawsky if (mpol_is_preferred_many(mpol)) { 2460cfcaa66fSBen Widawsky gfp_t gfp = gfp_mask | __GFP_NOWARN; 2461cfcaa66fSBen Widawsky 2462cfcaa66fSBen Widawsky gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); 24632b21624fSMike Kravetz page = alloc_surplus_huge_page(h, gfp, nid, nodemask); 2464cfcaa66fSBen Widawsky 2465cfcaa66fSBen Widawsky /* Fallback to all nodes if page==NULL */ 2466cfcaa66fSBen Widawsky nodemask = NULL; 2467cfcaa66fSBen Widawsky } 2468cfcaa66fSBen Widawsky 2469cfcaa66fSBen Widawsky if (!page) 24702b21624fSMike Kravetz page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask); 2471aaf14e40SMichal Hocko mpol_cond_put(mpol); 2472aaf14e40SMichal Hocko return page; 2473099730d6SDave Hansen } 2474099730d6SDave Hansen 2475ab5ac90aSMichal Hocko /* page migration callback function */ 24763e59fcb0SMichal Hocko struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid, 2477d92bbc27SJoonsoo Kim nodemask_t *nmask, gfp_t gfp_mask) 24784db9b2efSMichal Hocko { 2479db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 24808346d69dSXin Hao if (available_huge_pages(h)) { 24813e59fcb0SMichal Hocko struct page *page; 24823e59fcb0SMichal Hocko 24833e59fcb0SMichal Hocko page = dequeue_huge_page_nodemask(h, gfp_mask, preferred_nid, nmask); 24843e59fcb0SMichal Hocko if (page) { 2485db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 24863e59fcb0SMichal Hocko return page; 24874db9b2efSMichal Hocko } 24884db9b2efSMichal Hocko } 2489db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 24904db9b2efSMichal Hocko 24910c397daeSMichal Hocko return alloc_migrate_huge_page(h, gfp_mask, preferred_nid, nmask); 24924db9b2efSMichal Hocko } 24934db9b2efSMichal Hocko 2494ebd63723SMichal Hocko /* mempolicy aware migration callback */ 2495389c8178SMichal Hocko struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma, 2496389c8178SMichal Hocko unsigned long address) 2497ebd63723SMichal Hocko { 2498ebd63723SMichal Hocko struct mempolicy *mpol; 2499ebd63723SMichal Hocko nodemask_t *nodemask; 2500ebd63723SMichal Hocko struct page *page; 2501ebd63723SMichal Hocko gfp_t gfp_mask; 2502ebd63723SMichal Hocko int node; 2503ebd63723SMichal Hocko 2504ebd63723SMichal Hocko gfp_mask = htlb_alloc_mask(h); 2505ebd63723SMichal Hocko node = huge_node(vma, address, gfp_mask, &mpol, &nodemask); 2506d92bbc27SJoonsoo Kim page = alloc_huge_page_nodemask(h, node, nodemask, gfp_mask); 2507ebd63723SMichal Hocko mpol_cond_put(mpol); 2508ebd63723SMichal Hocko 2509ebd63723SMichal Hocko return page; 2510ebd63723SMichal Hocko } 2511ebd63723SMichal Hocko 2512bf50bab2SNaoya Horiguchi /* 251325985edcSLucas De Marchi * Increase the hugetlb pool such that it can accommodate a reservation 2514e4e574b7SAdam Litke * of size 'delta'. 2515e4e574b7SAdam Litke */ 25160a4f3d1bSLiu Xiang static int gather_surplus_pages(struct hstate *h, long delta) 25171b2a1e7bSJules Irenge __must_hold(&hugetlb_lock) 2518e4e574b7SAdam Litke { 251934665341SMiaohe Lin LIST_HEAD(surplus_list); 2520e4e574b7SAdam Litke struct page *page, *tmp; 25210a4f3d1bSLiu Xiang int ret; 25220a4f3d1bSLiu Xiang long i; 25230a4f3d1bSLiu Xiang long needed, allocated; 252428073b02SHillf Danton bool alloc_ok = true; 2525e4e574b7SAdam Litke 25269487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 2527a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - h->free_huge_pages; 2528ac09b3a1SAdam Litke if (needed <= 0) { 2529a5516438SAndi Kleen h->resv_huge_pages += delta; 2530e4e574b7SAdam Litke return 0; 2531ac09b3a1SAdam Litke } 2532e4e574b7SAdam Litke 2533e4e574b7SAdam Litke allocated = 0; 2534e4e574b7SAdam Litke 2535e4e574b7SAdam Litke ret = -ENOMEM; 2536e4e574b7SAdam Litke retry: 2537db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 2538e4e574b7SAdam Litke for (i = 0; i < needed; i++) { 25390c397daeSMichal Hocko page = alloc_surplus_huge_page(h, htlb_alloc_mask(h), 25402b21624fSMike Kravetz NUMA_NO_NODE, NULL); 254128073b02SHillf Danton if (!page) { 254228073b02SHillf Danton alloc_ok = false; 254328073b02SHillf Danton break; 254428073b02SHillf Danton } 2545e4e574b7SAdam Litke list_add(&page->lru, &surplus_list); 254669ed779aSDavid Rientjes cond_resched(); 2547e4e574b7SAdam Litke } 254828073b02SHillf Danton allocated += i; 2549e4e574b7SAdam Litke 2550e4e574b7SAdam Litke /* 2551e4e574b7SAdam Litke * After retaking hugetlb_lock, we need to recalculate 'needed' 2552e4e574b7SAdam Litke * because either resv_huge_pages or free_huge_pages may have changed. 2553e4e574b7SAdam Litke */ 2554db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 2555a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - 2556a5516438SAndi Kleen (h->free_huge_pages + allocated); 255728073b02SHillf Danton if (needed > 0) { 255828073b02SHillf Danton if (alloc_ok) 2559e4e574b7SAdam Litke goto retry; 256028073b02SHillf Danton /* 256128073b02SHillf Danton * We were not able to allocate enough pages to 256228073b02SHillf Danton * satisfy the entire reservation so we free what 256328073b02SHillf Danton * we've allocated so far. 256428073b02SHillf Danton */ 256528073b02SHillf Danton goto free; 256628073b02SHillf Danton } 2567e4e574b7SAdam Litke /* 2568e4e574b7SAdam Litke * The surplus_list now contains _at_least_ the number of extra pages 256925985edcSLucas De Marchi * needed to accommodate the reservation. Add the appropriate number 2570e4e574b7SAdam Litke * of pages to the hugetlb pool and free the extras back to the buddy 2571ac09b3a1SAdam Litke * allocator. Commit the entire reservation here to prevent another 2572ac09b3a1SAdam Litke * process from stealing the pages as they are added to the pool but 2573ac09b3a1SAdam Litke * before they are reserved. 2574e4e574b7SAdam Litke */ 2575e4e574b7SAdam Litke needed += allocated; 2576a5516438SAndi Kleen h->resv_huge_pages += delta; 2577e4e574b7SAdam Litke ret = 0; 2578a9869b83SNaoya Horiguchi 257919fc3f0aSAdam Litke /* Free the needed pages to the hugetlb pool */ 258019fc3f0aSAdam Litke list_for_each_entry_safe(page, tmp, &surplus_list, lru) { 258119fc3f0aSAdam Litke if ((--needed) < 0) 258219fc3f0aSAdam Litke break; 2583b65a4edaSMike Kravetz /* Add the page to the hugetlb allocator */ 2584240d67a8SSidhartha Kumar enqueue_hugetlb_folio(h, page_folio(page)); 258519fc3f0aSAdam Litke } 258628073b02SHillf Danton free: 2587db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 258819fc3f0aSAdam Litke 2589b65a4edaSMike Kravetz /* 2590b65a4edaSMike Kravetz * Free unnecessary surplus pages to the buddy allocator. 2591b65a4edaSMike Kravetz * Pages have no ref count, call free_huge_page directly. 2592b65a4edaSMike Kravetz */ 2593c0d934baSJoonsoo Kim list_for_each_entry_safe(page, tmp, &surplus_list, lru) 2594b65a4edaSMike Kravetz free_huge_page(page); 2595db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 2596e4e574b7SAdam Litke 2597e4e574b7SAdam Litke return ret; 2598e4e574b7SAdam Litke } 2599e4e574b7SAdam Litke 2600e4e574b7SAdam Litke /* 2601e5bbc8a6SMike Kravetz * This routine has two main purposes: 2602e5bbc8a6SMike Kravetz * 1) Decrement the reservation count (resv_huge_pages) by the value passed 2603e5bbc8a6SMike Kravetz * in unused_resv_pages. This corresponds to the prior adjustments made 2604e5bbc8a6SMike Kravetz * to the associated reservation map. 2605e5bbc8a6SMike Kravetz * 2) Free any unused surplus pages that may have been allocated to satisfy 2606e5bbc8a6SMike Kravetz * the reservation. As many as unused_resv_pages may be freed. 2607e4e574b7SAdam Litke */ 2608a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h, 2609a5516438SAndi Kleen unsigned long unused_resv_pages) 2610e4e574b7SAdam Litke { 2611e4e574b7SAdam Litke unsigned long nr_pages; 261210c6ec49SMike Kravetz struct page *page; 261310c6ec49SMike Kravetz LIST_HEAD(page_list); 261410c6ec49SMike Kravetz 26159487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 261610c6ec49SMike Kravetz /* Uncommit the reservation */ 261710c6ec49SMike Kravetz h->resv_huge_pages -= unused_resv_pages; 2618e4e574b7SAdam Litke 2619c0531714SNaoya Horiguchi if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 2620e5bbc8a6SMike Kravetz goto out; 2621aa888a74SAndi Kleen 2622e5bbc8a6SMike Kravetz /* 2623e5bbc8a6SMike Kravetz * Part (or even all) of the reservation could have been backed 2624e5bbc8a6SMike Kravetz * by pre-allocated pages. Only free surplus pages. 2625e5bbc8a6SMike Kravetz */ 2626a5516438SAndi Kleen nr_pages = min(unused_resv_pages, h->surplus_huge_pages); 2627e4e574b7SAdam Litke 2628685f3457SLee Schermerhorn /* 2629685f3457SLee Schermerhorn * We want to release as many surplus pages as possible, spread 26309b5e5d0fSLee Schermerhorn * evenly across all nodes with memory. Iterate across these nodes 26319b5e5d0fSLee Schermerhorn * until we can no longer free unreserved surplus pages. This occurs 26329b5e5d0fSLee Schermerhorn * when the nodes with surplus pages have no free pages. 263310c6ec49SMike Kravetz * remove_pool_huge_page() will balance the freed pages across the 26349b5e5d0fSLee Schermerhorn * on-line nodes with memory and will handle the hstate accounting. 2635685f3457SLee Schermerhorn */ 2636685f3457SLee Schermerhorn while (nr_pages--) { 263710c6ec49SMike Kravetz page = remove_pool_huge_page(h, &node_states[N_MEMORY], 1); 263810c6ec49SMike Kravetz if (!page) 2639e5bbc8a6SMike Kravetz goto out; 264010c6ec49SMike Kravetz 264110c6ec49SMike Kravetz list_add(&page->lru, &page_list); 2642e4e574b7SAdam Litke } 2643e5bbc8a6SMike Kravetz 2644e5bbc8a6SMike Kravetz out: 2645db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 264610c6ec49SMike Kravetz update_and_free_pages_bulk(h, &page_list); 2647db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 2648e4e574b7SAdam Litke } 2649e4e574b7SAdam Litke 26505e911373SMike Kravetz 2651c37f9fb1SAndy Whitcroft /* 2652feba16e2SMike Kravetz * vma_needs_reservation, vma_commit_reservation and vma_end_reservation 26535e911373SMike Kravetz * are used by the huge page allocation routines to manage reservations. 2654cf3ad20bSMike Kravetz * 2655cf3ad20bSMike Kravetz * vma_needs_reservation is called to determine if the huge page at addr 2656cf3ad20bSMike Kravetz * within the vma has an associated reservation. If a reservation is 2657cf3ad20bSMike Kravetz * needed, the value 1 is returned. The caller is then responsible for 2658cf3ad20bSMike Kravetz * managing the global reservation and subpool usage counts. After 2659cf3ad20bSMike Kravetz * the huge page has been allocated, vma_commit_reservation is called 2660feba16e2SMike Kravetz * to add the page to the reservation map. If the page allocation fails, 2661feba16e2SMike Kravetz * the reservation must be ended instead of committed. vma_end_reservation 2662feba16e2SMike Kravetz * is called in such cases. 2663cf3ad20bSMike Kravetz * 2664cf3ad20bSMike Kravetz * In the normal case, vma_commit_reservation returns the same value 2665cf3ad20bSMike Kravetz * as the preceding vma_needs_reservation call. The only time this 2666cf3ad20bSMike Kravetz * is not the case is if a reserve map was changed between calls. It 2667cf3ad20bSMike Kravetz * is the responsibility of the caller to notice the difference and 2668cf3ad20bSMike Kravetz * take appropriate action. 266996b96a96SMike Kravetz * 267096b96a96SMike Kravetz * vma_add_reservation is used in error paths where a reservation must 267196b96a96SMike Kravetz * be restored when a newly allocated huge page must be freed. It is 267296b96a96SMike Kravetz * to be called after calling vma_needs_reservation to determine if a 267396b96a96SMike Kravetz * reservation exists. 2674846be085SMike Kravetz * 2675846be085SMike Kravetz * vma_del_reservation is used in error paths where an entry in the reserve 2676846be085SMike Kravetz * map was created during huge page allocation and must be removed. It is to 2677846be085SMike Kravetz * be called after calling vma_needs_reservation to determine if a reservation 2678846be085SMike Kravetz * exists. 2679c37f9fb1SAndy Whitcroft */ 26805e911373SMike Kravetz enum vma_resv_mode { 26815e911373SMike Kravetz VMA_NEEDS_RESV, 26825e911373SMike Kravetz VMA_COMMIT_RESV, 2683feba16e2SMike Kravetz VMA_END_RESV, 268496b96a96SMike Kravetz VMA_ADD_RESV, 2685846be085SMike Kravetz VMA_DEL_RESV, 26865e911373SMike Kravetz }; 2687cf3ad20bSMike Kravetz static long __vma_reservation_common(struct hstate *h, 2688cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr, 26895e911373SMike Kravetz enum vma_resv_mode mode) 2690c37f9fb1SAndy Whitcroft { 26914e35f483SJoonsoo Kim struct resv_map *resv; 26924e35f483SJoonsoo Kim pgoff_t idx; 2693cf3ad20bSMike Kravetz long ret; 26940db9d74eSMina Almasry long dummy_out_regions_needed; 2695c37f9fb1SAndy Whitcroft 26964e35f483SJoonsoo Kim resv = vma_resv_map(vma); 26974e35f483SJoonsoo Kim if (!resv) 2698c37f9fb1SAndy Whitcroft return 1; 2699c37f9fb1SAndy Whitcroft 27004e35f483SJoonsoo Kim idx = vma_hugecache_offset(h, vma, addr); 27015e911373SMike Kravetz switch (mode) { 27025e911373SMike Kravetz case VMA_NEEDS_RESV: 27030db9d74eSMina Almasry ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed); 27040db9d74eSMina Almasry /* We assume that vma_reservation_* routines always operate on 27050db9d74eSMina Almasry * 1 page, and that adding to resv map a 1 page entry can only 27060db9d74eSMina Almasry * ever require 1 region. 27070db9d74eSMina Almasry */ 27080db9d74eSMina Almasry VM_BUG_ON(dummy_out_regions_needed != 1); 27095e911373SMike Kravetz break; 27105e911373SMike Kravetz case VMA_COMMIT_RESV: 2711075a61d0SMina Almasry ret = region_add(resv, idx, idx + 1, 1, NULL, NULL); 27120db9d74eSMina Almasry /* region_add calls of range 1 should never fail. */ 27130db9d74eSMina Almasry VM_BUG_ON(ret < 0); 27145e911373SMike Kravetz break; 2715feba16e2SMike Kravetz case VMA_END_RESV: 27160db9d74eSMina Almasry region_abort(resv, idx, idx + 1, 1); 27175e911373SMike Kravetz ret = 0; 27185e911373SMike Kravetz break; 271996b96a96SMike Kravetz case VMA_ADD_RESV: 27200db9d74eSMina Almasry if (vma->vm_flags & VM_MAYSHARE) { 2721075a61d0SMina Almasry ret = region_add(resv, idx, idx + 1, 1, NULL, NULL); 27220db9d74eSMina Almasry /* region_add calls of range 1 should never fail. */ 27230db9d74eSMina Almasry VM_BUG_ON(ret < 0); 27240db9d74eSMina Almasry } else { 27250db9d74eSMina Almasry region_abort(resv, idx, idx + 1, 1); 272696b96a96SMike Kravetz ret = region_del(resv, idx, idx + 1); 272796b96a96SMike Kravetz } 272896b96a96SMike Kravetz break; 2729846be085SMike Kravetz case VMA_DEL_RESV: 2730846be085SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 2731846be085SMike Kravetz region_abort(resv, idx, idx + 1, 1); 2732846be085SMike Kravetz ret = region_del(resv, idx, idx + 1); 2733846be085SMike Kravetz } else { 2734846be085SMike Kravetz ret = region_add(resv, idx, idx + 1, 1, NULL, NULL); 2735846be085SMike Kravetz /* region_add calls of range 1 should never fail. */ 2736846be085SMike Kravetz VM_BUG_ON(ret < 0); 2737846be085SMike Kravetz } 2738846be085SMike Kravetz break; 27395e911373SMike Kravetz default: 27405e911373SMike Kravetz BUG(); 27415e911373SMike Kravetz } 274284afd99bSAndy Whitcroft 2743846be085SMike Kravetz if (vma->vm_flags & VM_MAYSHARE || mode == VMA_DEL_RESV) 2744cf3ad20bSMike Kravetz return ret; 274567961f9dSMike Kravetz /* 2746bf3d12b9SMiaohe Lin * We know private mapping must have HPAGE_RESV_OWNER set. 2747bf3d12b9SMiaohe Lin * 274867961f9dSMike Kravetz * In most cases, reserves always exist for private mappings. 274967961f9dSMike Kravetz * However, a file associated with mapping could have been 275067961f9dSMike Kravetz * hole punched or truncated after reserves were consumed. 275167961f9dSMike Kravetz * As subsequent fault on such a range will not use reserves. 275267961f9dSMike Kravetz * Subtle - The reserve map for private mappings has the 275367961f9dSMike Kravetz * opposite meaning than that of shared mappings. If NO 275467961f9dSMike Kravetz * entry is in the reserve map, it means a reservation exists. 275567961f9dSMike Kravetz * If an entry exists in the reserve map, it means the 275667961f9dSMike Kravetz * reservation has already been consumed. As a result, the 275767961f9dSMike Kravetz * return value of this routine is the opposite of the 275867961f9dSMike Kravetz * value returned from reserve map manipulation routines above. 275967961f9dSMike Kravetz */ 2760bf3d12b9SMiaohe Lin if (ret > 0) 276167961f9dSMike Kravetz return 0; 2762bf3d12b9SMiaohe Lin if (ret == 0) 276367961f9dSMike Kravetz return 1; 2764bf3d12b9SMiaohe Lin return ret; 276584afd99bSAndy Whitcroft } 2766cf3ad20bSMike Kravetz 2767cf3ad20bSMike Kravetz static long vma_needs_reservation(struct hstate *h, 2768a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long addr) 2769c37f9fb1SAndy Whitcroft { 27705e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV); 2771cf3ad20bSMike Kravetz } 2772c37f9fb1SAndy Whitcroft 2773cf3ad20bSMike Kravetz static long vma_commit_reservation(struct hstate *h, 2774cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr) 2775cf3ad20bSMike Kravetz { 27765e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV); 27775e911373SMike Kravetz } 27785e911373SMike Kravetz 2779feba16e2SMike Kravetz static void vma_end_reservation(struct hstate *h, 27805e911373SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 27815e911373SMike Kravetz { 2782feba16e2SMike Kravetz (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV); 2783c37f9fb1SAndy Whitcroft } 2784c37f9fb1SAndy Whitcroft 278596b96a96SMike Kravetz static long vma_add_reservation(struct hstate *h, 278696b96a96SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 278796b96a96SMike Kravetz { 278896b96a96SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV); 278996b96a96SMike Kravetz } 279096b96a96SMike Kravetz 2791846be085SMike Kravetz static long vma_del_reservation(struct hstate *h, 2792846be085SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 279396b96a96SMike Kravetz { 2794846be085SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_DEL_RESV); 2795846be085SMike Kravetz } 2796846be085SMike Kravetz 2797846be085SMike Kravetz /* 2798846be085SMike Kravetz * This routine is called to restore reservation information on error paths. 2799846be085SMike Kravetz * It should ONLY be called for pages allocated via alloc_huge_page(), and 2800846be085SMike Kravetz * the hugetlb mutex should remain held when calling this routine. 2801846be085SMike Kravetz * 2802846be085SMike Kravetz * It handles two specific cases: 2803846be085SMike Kravetz * 1) A reservation was in place and the page consumed the reservation. 2804846be085SMike Kravetz * HPageRestoreReserve is set in the page. 2805846be085SMike Kravetz * 2) No reservation was in place for the page, so HPageRestoreReserve is 2806846be085SMike Kravetz * not set. However, alloc_huge_page always updates the reserve map. 2807846be085SMike Kravetz * 2808846be085SMike Kravetz * In case 1, free_huge_page later in the error path will increment the 2809846be085SMike Kravetz * global reserve count. But, free_huge_page does not have enough context 2810846be085SMike Kravetz * to adjust the reservation map. This case deals primarily with private 2811846be085SMike Kravetz * mappings. Adjust the reserve map here to be consistent with global 2812846be085SMike Kravetz * reserve count adjustments to be made by free_huge_page. Make sure the 2813846be085SMike Kravetz * reserve map indicates there is a reservation present. 2814846be085SMike Kravetz * 2815846be085SMike Kravetz * In case 2, simply undo reserve map modifications done by alloc_huge_page. 2816846be085SMike Kravetz */ 2817846be085SMike Kravetz void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, 2818846be085SMike Kravetz unsigned long address, struct page *page) 2819846be085SMike Kravetz { 282096b96a96SMike Kravetz long rc = vma_needs_reservation(h, vma, address); 282196b96a96SMike Kravetz 2822846be085SMike Kravetz if (HPageRestoreReserve(page)) { 2823846be085SMike Kravetz if (unlikely(rc < 0)) 282496b96a96SMike Kravetz /* 282596b96a96SMike Kravetz * Rare out of memory condition in reserve map 2826d6995da3SMike Kravetz * manipulation. Clear HPageRestoreReserve so that 282796b96a96SMike Kravetz * global reserve count will not be incremented 282896b96a96SMike Kravetz * by free_huge_page. This will make it appear 282996b96a96SMike Kravetz * as though the reservation for this page was 283096b96a96SMike Kravetz * consumed. This may prevent the task from 283196b96a96SMike Kravetz * faulting in the page at a later time. This 283296b96a96SMike Kravetz * is better than inconsistent global huge page 283396b96a96SMike Kravetz * accounting of reserve counts. 283496b96a96SMike Kravetz */ 2835d6995da3SMike Kravetz ClearHPageRestoreReserve(page); 2836846be085SMike Kravetz else if (rc) 2837846be085SMike Kravetz (void)vma_add_reservation(h, vma, address); 2838846be085SMike Kravetz else 2839846be085SMike Kravetz vma_end_reservation(h, vma, address); 2840846be085SMike Kravetz } else { 2841846be085SMike Kravetz if (!rc) { 284296b96a96SMike Kravetz /* 2843846be085SMike Kravetz * This indicates there is an entry in the reserve map 2844c7b1850dSMike Kravetz * not added by alloc_huge_page. We know it was added 2845846be085SMike Kravetz * before the alloc_huge_page call, otherwise 2846846be085SMike Kravetz * HPageRestoreReserve would be set on the page. 2847846be085SMike Kravetz * Remove the entry so that a subsequent allocation 2848846be085SMike Kravetz * does not consume a reservation. 284996b96a96SMike Kravetz */ 2850846be085SMike Kravetz rc = vma_del_reservation(h, vma, address); 2851846be085SMike Kravetz if (rc < 0) 2852846be085SMike Kravetz /* 2853846be085SMike Kravetz * VERY rare out of memory condition. Since 2854846be085SMike Kravetz * we can not delete the entry, set 2855846be085SMike Kravetz * HPageRestoreReserve so that the reserve 2856846be085SMike Kravetz * count will be incremented when the page 2857846be085SMike Kravetz * is freed. This reserve will be consumed 2858846be085SMike Kravetz * on a subsequent allocation. 2859846be085SMike Kravetz */ 2860846be085SMike Kravetz SetHPageRestoreReserve(page); 2861846be085SMike Kravetz } else if (rc < 0) { 2862846be085SMike Kravetz /* 2863846be085SMike Kravetz * Rare out of memory condition from 2864846be085SMike Kravetz * vma_needs_reservation call. Memory allocation is 2865846be085SMike Kravetz * only attempted if a new entry is needed. Therefore, 2866846be085SMike Kravetz * this implies there is not an entry in the 2867846be085SMike Kravetz * reserve map. 2868846be085SMike Kravetz * 2869846be085SMike Kravetz * For shared mappings, no entry in the map indicates 2870846be085SMike Kravetz * no reservation. We are done. 2871846be085SMike Kravetz */ 2872846be085SMike Kravetz if (!(vma->vm_flags & VM_MAYSHARE)) 2873846be085SMike Kravetz /* 2874846be085SMike Kravetz * For private mappings, no entry indicates 2875846be085SMike Kravetz * a reservation is present. Since we can 2876846be085SMike Kravetz * not add an entry, set SetHPageRestoreReserve 2877846be085SMike Kravetz * on the page so reserve count will be 2878846be085SMike Kravetz * incremented when freed. This reserve will 2879846be085SMike Kravetz * be consumed on a subsequent allocation. 2880846be085SMike Kravetz */ 2881846be085SMike Kravetz SetHPageRestoreReserve(page); 288296b96a96SMike Kravetz } else 2883846be085SMike Kravetz /* 2884846be085SMike Kravetz * No reservation present, do nothing 2885846be085SMike Kravetz */ 288696b96a96SMike Kravetz vma_end_reservation(h, vma, address); 288796b96a96SMike Kravetz } 288896b96a96SMike Kravetz } 288996b96a96SMike Kravetz 2890369fa227SOscar Salvador /* 289119fc1a7eSSidhartha Kumar * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve 289219fc1a7eSSidhartha Kumar * the old one 2893369fa227SOscar Salvador * @h: struct hstate old page belongs to 289419fc1a7eSSidhartha Kumar * @old_folio: Old folio to dissolve 2895ae37c7ffSOscar Salvador * @list: List to isolate the page in case we need to 2896369fa227SOscar Salvador * Returns 0 on success, otherwise negated error. 2897369fa227SOscar Salvador */ 289819fc1a7eSSidhartha Kumar static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, 289919fc1a7eSSidhartha Kumar struct folio *old_folio, struct list_head *list) 2900369fa227SOscar Salvador { 2901369fa227SOscar Salvador gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; 2902de656ed3SSidhartha Kumar int nid = folio_nid(old_folio); 2903de656ed3SSidhartha Kumar struct folio *new_folio; 2904369fa227SOscar Salvador int ret = 0; 2905369fa227SOscar Salvador 2906369fa227SOscar Salvador /* 290719fc1a7eSSidhartha Kumar * Before dissolving the folio, we need to allocate a new one for the 290819fc1a7eSSidhartha Kumar * pool to remain stable. Here, we allocate the folio and 'prep' it 2909f41f2ed4SMuchun Song * by doing everything but actually updating counters and adding to 2910f41f2ed4SMuchun Song * the pool. This simplifies and let us do most of the processing 2911f41f2ed4SMuchun Song * under the lock. 2912369fa227SOscar Salvador */ 291319fc1a7eSSidhartha Kumar new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); 291419fc1a7eSSidhartha Kumar if (!new_folio) 2915369fa227SOscar Salvador return -ENOMEM; 2916de656ed3SSidhartha Kumar __prep_new_hugetlb_folio(h, new_folio); 2917369fa227SOscar Salvador 2918369fa227SOscar Salvador retry: 2919369fa227SOscar Salvador spin_lock_irq(&hugetlb_lock); 2920de656ed3SSidhartha Kumar if (!folio_test_hugetlb(old_folio)) { 2921369fa227SOscar Salvador /* 292219fc1a7eSSidhartha Kumar * Freed from under us. Drop new_folio too. 2923369fa227SOscar Salvador */ 2924369fa227SOscar Salvador goto free_new; 2925de656ed3SSidhartha Kumar } else if (folio_ref_count(old_folio)) { 2926369fa227SOscar Salvador /* 292719fc1a7eSSidhartha Kumar * Someone has grabbed the folio, try to isolate it here. 2928ae37c7ffSOscar Salvador * Fail with -EBUSY if not possible. 2929369fa227SOscar Salvador */ 2930ae37c7ffSOscar Salvador spin_unlock_irq(&hugetlb_lock); 293119fc1a7eSSidhartha Kumar ret = isolate_hugetlb(&old_folio->page, list); 2932ae37c7ffSOscar Salvador spin_lock_irq(&hugetlb_lock); 2933369fa227SOscar Salvador goto free_new; 2934de656ed3SSidhartha Kumar } else if (!folio_test_hugetlb_freed(old_folio)) { 2935369fa227SOscar Salvador /* 293619fc1a7eSSidhartha Kumar * Folio's refcount is 0 but it has not been enqueued in the 2937369fa227SOscar Salvador * freelist yet. Race window is small, so we can succeed here if 2938369fa227SOscar Salvador * we retry. 2939369fa227SOscar Salvador */ 2940369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 2941369fa227SOscar Salvador cond_resched(); 2942369fa227SOscar Salvador goto retry; 2943369fa227SOscar Salvador } else { 2944369fa227SOscar Salvador /* 294519fc1a7eSSidhartha Kumar * Ok, old_folio is still a genuine free hugepage. Remove it from 2946369fa227SOscar Salvador * the freelist and decrease the counters. These will be 2947369fa227SOscar Salvador * incremented again when calling __prep_account_new_huge_page() 2948240d67a8SSidhartha Kumar * and enqueue_hugetlb_folio() for new_folio. The counters will 2949240d67a8SSidhartha Kumar * remain stable since this happens under the lock. 2950369fa227SOscar Salvador */ 2951cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, old_folio, false); 2952369fa227SOscar Salvador 2953369fa227SOscar Salvador /* 295419fc1a7eSSidhartha Kumar * Ref count on new_folio is already zero as it was dropped 2955b65a4edaSMike Kravetz * earlier. It can be directly added to the pool free list. 2956369fa227SOscar Salvador */ 2957369fa227SOscar Salvador __prep_account_new_huge_page(h, nid); 2958240d67a8SSidhartha Kumar enqueue_hugetlb_folio(h, new_folio); 2959369fa227SOscar Salvador 2960369fa227SOscar Salvador /* 296119fc1a7eSSidhartha Kumar * Folio has been replaced, we can safely free the old one. 2962369fa227SOscar Salvador */ 2963369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 2964d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, old_folio, false); 2965369fa227SOscar Salvador } 2966369fa227SOscar Salvador 2967369fa227SOscar Salvador return ret; 2968369fa227SOscar Salvador 2969369fa227SOscar Salvador free_new: 2970369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 297119fc1a7eSSidhartha Kumar /* Folio has a zero ref count, but needs a ref to be freed */ 2972de656ed3SSidhartha Kumar folio_ref_unfreeze(new_folio, 1); 2973d6ef19e2SSidhartha Kumar update_and_free_hugetlb_folio(h, new_folio, false); 2974369fa227SOscar Salvador 2975369fa227SOscar Salvador return ret; 2976369fa227SOscar Salvador } 2977369fa227SOscar Salvador 2978ae37c7ffSOscar Salvador int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list) 2979369fa227SOscar Salvador { 2980369fa227SOscar Salvador struct hstate *h; 2981d5e33bd8SSidhartha Kumar struct folio *folio = page_folio(page); 2982ae37c7ffSOscar Salvador int ret = -EBUSY; 2983369fa227SOscar Salvador 2984369fa227SOscar Salvador /* 2985369fa227SOscar Salvador * The page might have been dissolved from under our feet, so make sure 2986369fa227SOscar Salvador * to carefully check the state under the lock. 2987369fa227SOscar Salvador * Return success when racing as if we dissolved the page ourselves. 2988369fa227SOscar Salvador */ 2989369fa227SOscar Salvador spin_lock_irq(&hugetlb_lock); 2990d5e33bd8SSidhartha Kumar if (folio_test_hugetlb(folio)) { 2991d5e33bd8SSidhartha Kumar h = folio_hstate(folio); 2992369fa227SOscar Salvador } else { 2993369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 2994369fa227SOscar Salvador return 0; 2995369fa227SOscar Salvador } 2996369fa227SOscar Salvador spin_unlock_irq(&hugetlb_lock); 2997369fa227SOscar Salvador 2998369fa227SOscar Salvador /* 2999369fa227SOscar Salvador * Fence off gigantic pages as there is a cyclic dependency between 3000369fa227SOscar Salvador * alloc_contig_range and them. Return -ENOMEM as this has the effect 3001369fa227SOscar Salvador * of bailing out right away without further retrying. 3002369fa227SOscar Salvador */ 3003369fa227SOscar Salvador if (hstate_is_gigantic(h)) 3004369fa227SOscar Salvador return -ENOMEM; 3005369fa227SOscar Salvador 3006d5e33bd8SSidhartha Kumar if (folio_ref_count(folio) && !isolate_hugetlb(&folio->page, list)) 3007ae37c7ffSOscar Salvador ret = 0; 3008d5e33bd8SSidhartha Kumar else if (!folio_ref_count(folio)) 300919fc1a7eSSidhartha Kumar ret = alloc_and_dissolve_hugetlb_folio(h, folio, list); 3010ae37c7ffSOscar Salvador 3011ae37c7ffSOscar Salvador return ret; 3012369fa227SOscar Salvador } 3013369fa227SOscar Salvador 301470c3547eSMike Kravetz struct page *alloc_huge_page(struct vm_area_struct *vma, 301504f2cbe3SMel Gorman unsigned long addr, int avoid_reserve) 3016348ea204SAdam Litke { 301790481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 3018a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3019348ea204SAdam Litke struct page *page; 3020d4ab0316SSidhartha Kumar struct folio *folio; 3021d85f69b0SMike Kravetz long map_chg, map_commit; 3022d85f69b0SMike Kravetz long gbl_chg; 30236d76dcf4SAneesh Kumar K.V int ret, idx; 30246d76dcf4SAneesh Kumar K.V struct hugetlb_cgroup *h_cg; 302508cf9fafSMina Almasry bool deferred_reserve; 30262fc39cecSAdam Litke 30276d76dcf4SAneesh Kumar K.V idx = hstate_index(h); 3028a1e78772SMel Gorman /* 3029d85f69b0SMike Kravetz * Examine the region/reserve map to determine if the process 3030d85f69b0SMike Kravetz * has a reservation for the page to be allocated. A return 3031d85f69b0SMike Kravetz * code of zero indicates a reservation exists (no change). 3032a1e78772SMel Gorman */ 3033d85f69b0SMike Kravetz map_chg = gbl_chg = vma_needs_reservation(h, vma, addr); 3034d85f69b0SMike Kravetz if (map_chg < 0) 303576dcee75SAneesh Kumar K.V return ERR_PTR(-ENOMEM); 3036d85f69b0SMike Kravetz 3037d85f69b0SMike Kravetz /* 3038d85f69b0SMike Kravetz * Processes that did not create the mapping will have no 3039d85f69b0SMike Kravetz * reserves as indicated by the region/reserve map. Check 3040d85f69b0SMike Kravetz * that the allocation will not exceed the subpool limit. 3041d85f69b0SMike Kravetz * Allocations for MAP_NORESERVE mappings also need to be 3042d85f69b0SMike Kravetz * checked against any subpool limit. 3043d85f69b0SMike Kravetz */ 3044d85f69b0SMike Kravetz if (map_chg || avoid_reserve) { 3045d85f69b0SMike Kravetz gbl_chg = hugepage_subpool_get_pages(spool, 1); 3046d85f69b0SMike Kravetz if (gbl_chg < 0) { 3047feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 304876dcee75SAneesh Kumar K.V return ERR_PTR(-ENOSPC); 30495e911373SMike Kravetz } 305090d8b7e6SAdam Litke 3051d85f69b0SMike Kravetz /* 3052d85f69b0SMike Kravetz * Even though there was no reservation in the region/reserve 3053d85f69b0SMike Kravetz * map, there could be reservations associated with the 3054d85f69b0SMike Kravetz * subpool that can be used. This would be indicated if the 3055d85f69b0SMike Kravetz * return value of hugepage_subpool_get_pages() is zero. 3056d85f69b0SMike Kravetz * However, if avoid_reserve is specified we still avoid even 3057d85f69b0SMike Kravetz * the subpool reservations. 3058d85f69b0SMike Kravetz */ 3059d85f69b0SMike Kravetz if (avoid_reserve) 3060d85f69b0SMike Kravetz gbl_chg = 1; 3061d85f69b0SMike Kravetz } 3062d85f69b0SMike Kravetz 306308cf9fafSMina Almasry /* If this allocation is not consuming a reservation, charge it now. 306408cf9fafSMina Almasry */ 30656501fe5fSMiaohe Lin deferred_reserve = map_chg || avoid_reserve; 306608cf9fafSMina Almasry if (deferred_reserve) { 306708cf9fafSMina Almasry ret = hugetlb_cgroup_charge_cgroup_rsvd( 306808cf9fafSMina Almasry idx, pages_per_huge_page(h), &h_cg); 30698f34af6fSJianyu Zhan if (ret) 30708f34af6fSJianyu Zhan goto out_subpool_put; 307108cf9fafSMina Almasry } 307208cf9fafSMina Almasry 307308cf9fafSMina Almasry ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); 307408cf9fafSMina Almasry if (ret) 307508cf9fafSMina Almasry goto out_uncharge_cgroup_reservation; 30768f34af6fSJianyu Zhan 3077db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 3078d85f69b0SMike Kravetz /* 3079d85f69b0SMike Kravetz * glb_chg is passed to indicate whether or not a page must be taken 3080d85f69b0SMike Kravetz * from the global free pool (global change). gbl_chg == 0 indicates 3081d85f69b0SMike Kravetz * a reservation exists for the allocation. 3082d85f69b0SMike Kravetz */ 3083d85f69b0SMike Kravetz page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg); 308481a6fcaeSJoonsoo Kim if (!page) { 3085db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 30860c397daeSMichal Hocko page = alloc_buddy_huge_page_with_mpol(h, vma, addr); 30878f34af6fSJianyu Zhan if (!page) 30888f34af6fSJianyu Zhan goto out_uncharge_cgroup; 308912df140fSRik van Riel spin_lock_irq(&hugetlb_lock); 3090a88c7695SNaoya Horiguchi if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) { 3091d6995da3SMike Kravetz SetHPageRestoreReserve(page); 3092a88c7695SNaoya Horiguchi h->resv_huge_pages--; 3093a88c7695SNaoya Horiguchi } 309415a8d68eSWei Yang list_add(&page->lru, &h->hugepage_activelist); 30952b21624fSMike Kravetz set_page_refcounted(page); 309681a6fcaeSJoonsoo Kim /* Fall through */ 3097a1e78772SMel Gorman } 3098d4ab0316SSidhartha Kumar folio = page_folio(page); 309981a6fcaeSJoonsoo Kim hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page); 310008cf9fafSMina Almasry /* If allocation is not consuming a reservation, also store the 310108cf9fafSMina Almasry * hugetlb_cgroup pointer on the page. 310208cf9fafSMina Almasry */ 310308cf9fafSMina Almasry if (deferred_reserve) { 310408cf9fafSMina Almasry hugetlb_cgroup_commit_charge_rsvd(idx, pages_per_huge_page(h), 310508cf9fafSMina Almasry h_cg, page); 310608cf9fafSMina Almasry } 310708cf9fafSMina Almasry 3108db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 3109a1e78772SMel Gorman 3110d6995da3SMike Kravetz hugetlb_set_page_subpool(page, spool); 3111a1e78772SMel Gorman 3112d85f69b0SMike Kravetz map_commit = vma_commit_reservation(h, vma, addr); 3113d85f69b0SMike Kravetz if (unlikely(map_chg > map_commit)) { 311433039678SMike Kravetz /* 311533039678SMike Kravetz * The page was added to the reservation map between 311633039678SMike Kravetz * vma_needs_reservation and vma_commit_reservation. 311733039678SMike Kravetz * This indicates a race with hugetlb_reserve_pages. 311833039678SMike Kravetz * Adjust for the subpool count incremented above AND 311933039678SMike Kravetz * in hugetlb_reserve_pages for the same page. Also, 312033039678SMike Kravetz * the reservation count added in hugetlb_reserve_pages 312133039678SMike Kravetz * no longer applies. 312233039678SMike Kravetz */ 312333039678SMike Kravetz long rsv_adjust; 312433039678SMike Kravetz 312533039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 1); 312633039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 312779aa925bSMike Kravetz if (deferred_reserve) 3128d4ab0316SSidhartha Kumar hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h), 3129d4ab0316SSidhartha Kumar pages_per_huge_page(h), folio); 313033039678SMike Kravetz } 31317893d1d5SAdam Litke return page; 31328f34af6fSJianyu Zhan 31338f34af6fSJianyu Zhan out_uncharge_cgroup: 31348f34af6fSJianyu Zhan hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); 313508cf9fafSMina Almasry out_uncharge_cgroup_reservation: 313608cf9fafSMina Almasry if (deferred_reserve) 313708cf9fafSMina Almasry hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h), 313808cf9fafSMina Almasry h_cg); 31398f34af6fSJianyu Zhan out_subpool_put: 3140d85f69b0SMike Kravetz if (map_chg || avoid_reserve) 31418f34af6fSJianyu Zhan hugepage_subpool_put_pages(spool, 1); 3142feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 31438f34af6fSJianyu Zhan return ERR_PTR(-ENOSPC); 3144b45b5bd6SDavid Gibson } 3145b45b5bd6SDavid Gibson 3146b5389086SZhenguo Yao int alloc_bootmem_huge_page(struct hstate *h, int nid) 3147e24a1307SAneesh Kumar K.V __attribute__ ((weak, alias("__alloc_bootmem_huge_page"))); 3148b5389086SZhenguo Yao int __alloc_bootmem_huge_page(struct hstate *h, int nid) 3149aa888a74SAndi Kleen { 3150b5389086SZhenguo Yao struct huge_bootmem_page *m = NULL; /* initialize for clang */ 3151b2261026SJoonsoo Kim int nr_nodes, node; 3152aa888a74SAndi Kleen 3153b5389086SZhenguo Yao /* do node specific alloc */ 3154b5389086SZhenguo Yao if (nid != NUMA_NO_NODE) { 3155b5389086SZhenguo Yao m = memblock_alloc_try_nid_raw(huge_page_size(h), huge_page_size(h), 3156b5389086SZhenguo Yao 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); 3157b5389086SZhenguo Yao if (!m) 3158b5389086SZhenguo Yao return 0; 3159b5389086SZhenguo Yao goto found; 3160b5389086SZhenguo Yao } 3161b5389086SZhenguo Yao /* allocate from next node when distributing huge pages */ 3162b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) { 3163b5389086SZhenguo Yao m = memblock_alloc_try_nid_raw( 31648b89a116SGrygorii Strashko huge_page_size(h), huge_page_size(h), 316597ad1087SMike Rapoport 0, MEMBLOCK_ALLOC_ACCESSIBLE, node); 3166aa888a74SAndi Kleen /* 3167aa888a74SAndi Kleen * Use the beginning of the huge page to store the 3168aa888a74SAndi Kleen * huge_bootmem_page struct (until gather_bootmem 3169aa888a74SAndi Kleen * puts them into the mem_map). 3170aa888a74SAndi Kleen */ 3171b5389086SZhenguo Yao if (!m) 3172b5389086SZhenguo Yao return 0; 3173aa888a74SAndi Kleen goto found; 3174aa888a74SAndi Kleen } 3175aa888a74SAndi Kleen 3176aa888a74SAndi Kleen found: 3177aa888a74SAndi Kleen /* Put them into a private list first because mem_map is not up yet */ 3178330d6e48SCannon Matthews INIT_LIST_HEAD(&m->list); 3179aa888a74SAndi Kleen list_add(&m->list, &huge_boot_pages); 3180aa888a74SAndi Kleen m->hstate = h; 3181aa888a74SAndi Kleen return 1; 3182aa888a74SAndi Kleen } 3183aa888a74SAndi Kleen 318448b8d744SMike Kravetz /* 318548b8d744SMike Kravetz * Put bootmem huge pages into the standard lists after mem_map is up. 318648b8d744SMike Kravetz * Note: This only applies to gigantic (order > MAX_ORDER) pages. 318748b8d744SMike Kravetz */ 3188aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void) 3189aa888a74SAndi Kleen { 3190aa888a74SAndi Kleen struct huge_bootmem_page *m; 3191aa888a74SAndi Kleen 3192aa888a74SAndi Kleen list_for_each_entry(m, &huge_boot_pages, list) { 319340d18ebfSMike Kravetz struct page *page = virt_to_page(m); 31947f325a8dSSidhartha Kumar struct folio *folio = page_folio(page); 3195aa888a74SAndi Kleen struct hstate *h = m->hstate; 3196ee8f248dSBecky Bruce 319748b8d744SMike Kravetz VM_BUG_ON(!hstate_is_gigantic(h)); 3198d1c60955SSidhartha Kumar WARN_ON(folio_ref_count(folio) != 1); 3199d1c60955SSidhartha Kumar if (prep_compound_gigantic_folio(folio, huge_page_order(h))) { 3200d1c60955SSidhartha Kumar WARN_ON(folio_test_reserved(folio)); 3201d1c60955SSidhartha Kumar prep_new_hugetlb_folio(h, folio, folio_nid(folio)); 32022b21624fSMike Kravetz free_huge_page(page); /* add to the hugepage allocator */ 32037118fc29SMike Kravetz } else { 3204416d85edSMike Kravetz /* VERY unlikely inflated ref count on a tail page */ 32057f325a8dSSidhartha Kumar free_gigantic_folio(folio, huge_page_order(h)); 32067118fc29SMike Kravetz } 3207af0fb9dfSMichal Hocko 3208b0320c7bSRafael Aquini /* 320948b8d744SMike Kravetz * We need to restore the 'stolen' pages to totalram_pages 321048b8d744SMike Kravetz * in order to fix confusing memory reports from free(1) and 321148b8d744SMike Kravetz * other side-effects, like CommitLimit going negative. 3212b0320c7bSRafael Aquini */ 3213c78a7f36SMiaohe Lin adjust_managed_page_count(page, pages_per_huge_page(h)); 3214520495feSCannon Matthews cond_resched(); 3215aa888a74SAndi Kleen } 3216aa888a74SAndi Kleen } 3217b5389086SZhenguo Yao static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid) 3218b5389086SZhenguo Yao { 3219b5389086SZhenguo Yao unsigned long i; 3220b5389086SZhenguo Yao char buf[32]; 3221b5389086SZhenguo Yao 3222b5389086SZhenguo Yao for (i = 0; i < h->max_huge_pages_node[nid]; ++i) { 3223b5389086SZhenguo Yao if (hstate_is_gigantic(h)) { 3224b5389086SZhenguo Yao if (!alloc_bootmem_huge_page(h, nid)) 3225b5389086SZhenguo Yao break; 3226b5389086SZhenguo Yao } else { 322719fc1a7eSSidhartha Kumar struct folio *folio; 3228b5389086SZhenguo Yao gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; 3229b5389086SZhenguo Yao 323019fc1a7eSSidhartha Kumar folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, 3231b5389086SZhenguo Yao &node_states[N_MEMORY], NULL); 323219fc1a7eSSidhartha Kumar if (!folio) 3233b5389086SZhenguo Yao break; 323419fc1a7eSSidhartha Kumar free_huge_page(&folio->page); /* free it into the hugepage allocator */ 3235b5389086SZhenguo Yao } 3236b5389086SZhenguo Yao cond_resched(); 3237b5389086SZhenguo Yao } 3238b5389086SZhenguo Yao if (i == h->max_huge_pages_node[nid]) 3239b5389086SZhenguo Yao return; 3240b5389086SZhenguo Yao 3241b5389086SZhenguo Yao string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); 3242b5389086SZhenguo Yao pr_warn("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n", 3243b5389086SZhenguo Yao h->max_huge_pages_node[nid], buf, nid, i); 3244b5389086SZhenguo Yao h->max_huge_pages -= (h->max_huge_pages_node[nid] - i); 3245b5389086SZhenguo Yao h->max_huge_pages_node[nid] = i; 3246b5389086SZhenguo Yao } 3247aa888a74SAndi Kleen 32488faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h) 32491da177e4SLinus Torvalds { 32501da177e4SLinus Torvalds unsigned long i; 3251f60858f9SMike Kravetz nodemask_t *node_alloc_noretry; 3252b5389086SZhenguo Yao bool node_specific_alloc = false; 3253f60858f9SMike Kravetz 3254b5389086SZhenguo Yao /* skip gigantic hugepages allocation if hugetlb_cma enabled */ 3255b5389086SZhenguo Yao if (hstate_is_gigantic(h) && hugetlb_cma_size) { 3256b5389086SZhenguo Yao pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n"); 3257b5389086SZhenguo Yao return; 3258b5389086SZhenguo Yao } 3259b5389086SZhenguo Yao 3260b5389086SZhenguo Yao /* do node specific alloc */ 32610a7a0f6fSPeng Liu for_each_online_node(i) { 3262b5389086SZhenguo Yao if (h->max_huge_pages_node[i] > 0) { 3263b5389086SZhenguo Yao hugetlb_hstate_alloc_pages_onenode(h, i); 3264b5389086SZhenguo Yao node_specific_alloc = true; 3265b5389086SZhenguo Yao } 3266b5389086SZhenguo Yao } 3267b5389086SZhenguo Yao 3268b5389086SZhenguo Yao if (node_specific_alloc) 3269b5389086SZhenguo Yao return; 3270b5389086SZhenguo Yao 3271b5389086SZhenguo Yao /* below will do all node balanced alloc */ 3272f60858f9SMike Kravetz if (!hstate_is_gigantic(h)) { 3273f60858f9SMike Kravetz /* 3274f60858f9SMike Kravetz * Bit mask controlling how hard we retry per-node allocations. 3275f60858f9SMike Kravetz * Ignore errors as lower level routines can deal with 3276f60858f9SMike Kravetz * node_alloc_noretry == NULL. If this kmalloc fails at boot 3277f60858f9SMike Kravetz * time, we are likely in bigger trouble. 3278f60858f9SMike Kravetz */ 3279f60858f9SMike Kravetz node_alloc_noretry = kmalloc(sizeof(*node_alloc_noretry), 3280f60858f9SMike Kravetz GFP_KERNEL); 3281f60858f9SMike Kravetz } else { 3282f60858f9SMike Kravetz /* allocations done at boot time */ 3283f60858f9SMike Kravetz node_alloc_noretry = NULL; 3284f60858f9SMike Kravetz } 3285f60858f9SMike Kravetz 3286f60858f9SMike Kravetz /* bit mask controlling how hard we retry per-node allocations */ 3287f60858f9SMike Kravetz if (node_alloc_noretry) 3288f60858f9SMike Kravetz nodes_clear(*node_alloc_noretry); 32891da177e4SLinus Torvalds 3290e5ff2159SAndi Kleen for (i = 0; i < h->max_huge_pages; ++i) { 3291bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) { 3292b5389086SZhenguo Yao if (!alloc_bootmem_huge_page(h, NUMA_NO_NODE)) 3293aa888a74SAndi Kleen break; 32940c397daeSMichal Hocko } else if (!alloc_pool_huge_page(h, 3295f60858f9SMike Kravetz &node_states[N_MEMORY], 3296f60858f9SMike Kravetz node_alloc_noretry)) 32971da177e4SLinus Torvalds break; 329869ed779aSDavid Rientjes cond_resched(); 32991da177e4SLinus Torvalds } 3300d715cf80SLiam R. Howlett if (i < h->max_huge_pages) { 3301d715cf80SLiam R. Howlett char buf[32]; 3302d715cf80SLiam R. Howlett 3303c6247f72SMatthew Wilcox string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); 3304d715cf80SLiam R. Howlett pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n", 3305d715cf80SLiam R. Howlett h->max_huge_pages, buf, i); 33068faa8b07SAndi Kleen h->max_huge_pages = i; 3307e5ff2159SAndi Kleen } 3308f60858f9SMike Kravetz kfree(node_alloc_noretry); 3309d715cf80SLiam R. Howlett } 3310e5ff2159SAndi Kleen 3311e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void) 3312e5ff2159SAndi Kleen { 331379dfc695SMike Kravetz struct hstate *h, *h2; 3314e5ff2159SAndi Kleen 3315e5ff2159SAndi Kleen for_each_hstate(h) { 33168faa8b07SAndi Kleen /* oversize hugepages were init'ed in early boot */ 3317bae7f4aeSLuiz Capitulino if (!hstate_is_gigantic(h)) 33188faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(h); 331979dfc695SMike Kravetz 332079dfc695SMike Kravetz /* 332179dfc695SMike Kravetz * Set demote order for each hstate. Note that 332279dfc695SMike Kravetz * h->demote_order is initially 0. 332379dfc695SMike Kravetz * - We can not demote gigantic pages if runtime freeing 332479dfc695SMike Kravetz * is not supported, so skip this. 3325a01f4390SMike Kravetz * - If CMA allocation is possible, we can not demote 3326a01f4390SMike Kravetz * HUGETLB_PAGE_ORDER or smaller size pages. 332779dfc695SMike Kravetz */ 332879dfc695SMike Kravetz if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 332979dfc695SMike Kravetz continue; 3330a01f4390SMike Kravetz if (hugetlb_cma_size && h->order <= HUGETLB_PAGE_ORDER) 3331a01f4390SMike Kravetz continue; 333279dfc695SMike Kravetz for_each_hstate(h2) { 333379dfc695SMike Kravetz if (h2 == h) 333479dfc695SMike Kravetz continue; 333579dfc695SMike Kravetz if (h2->order < h->order && 333679dfc695SMike Kravetz h2->order > h->demote_order) 333779dfc695SMike Kravetz h->demote_order = h2->order; 333879dfc695SMike Kravetz } 3339e5ff2159SAndi Kleen } 3340e5ff2159SAndi Kleen } 3341e5ff2159SAndi Kleen 3342e5ff2159SAndi Kleen static void __init report_hugepages(void) 3343e5ff2159SAndi Kleen { 3344e5ff2159SAndi Kleen struct hstate *h; 3345e5ff2159SAndi Kleen 3346e5ff2159SAndi Kleen for_each_hstate(h) { 33474abd32dbSAndi Kleen char buf[32]; 3348c6247f72SMatthew Wilcox 3349c6247f72SMatthew Wilcox string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); 33506213834cSMuchun Song pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n", 3351c6247f72SMatthew Wilcox buf, h->free_huge_pages); 33526213834cSMuchun Song pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n", 33536213834cSMuchun Song hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf); 3354e5ff2159SAndi Kleen } 3355e5ff2159SAndi Kleen } 3356e5ff2159SAndi Kleen 33571da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM 33586ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count, 33596ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 33601da177e4SLinus Torvalds { 33614415cc8dSChristoph Lameter int i; 33621121828aSMike Kravetz LIST_HEAD(page_list); 33634415cc8dSChristoph Lameter 33649487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 3365bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 3366aa888a74SAndi Kleen return; 3367aa888a74SAndi Kleen 33681121828aSMike Kravetz /* 33691121828aSMike Kravetz * Collect pages to be freed on a list, and free after dropping lock 33701121828aSMike Kravetz */ 33716ae11b27SLee Schermerhorn for_each_node_mask(i, *nodes_allowed) { 337210c6ec49SMike Kravetz struct page *page, *next; 3373a5516438SAndi Kleen struct list_head *freel = &h->hugepage_freelists[i]; 3374a5516438SAndi Kleen list_for_each_entry_safe(page, next, freel, lru) { 3375a5516438SAndi Kleen if (count >= h->nr_huge_pages) 33761121828aSMike Kravetz goto out; 33771da177e4SLinus Torvalds if (PageHighMem(page)) 33781da177e4SLinus Torvalds continue; 3379cfd5082bSSidhartha Kumar remove_hugetlb_folio(h, page_folio(page), false); 33801121828aSMike Kravetz list_add(&page->lru, &page_list); 33811121828aSMike Kravetz } 33821121828aSMike Kravetz } 33831121828aSMike Kravetz 33841121828aSMike Kravetz out: 3385db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 338610c6ec49SMike Kravetz update_and_free_pages_bulk(h, &page_list); 3387db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 33881da177e4SLinus Torvalds } 33891da177e4SLinus Torvalds #else 33906ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count, 33916ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 33921da177e4SLinus Torvalds { 33931da177e4SLinus Torvalds } 33941da177e4SLinus Torvalds #endif 33951da177e4SLinus Torvalds 339620a0307cSWu Fengguang /* 339720a0307cSWu Fengguang * Increment or decrement surplus_huge_pages. Keep node-specific counters 339820a0307cSWu Fengguang * balanced by operating on them in a round-robin fashion. 339920a0307cSWu Fengguang * Returns 1 if an adjustment was made. 340020a0307cSWu Fengguang */ 34016ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, 34026ae11b27SLee Schermerhorn int delta) 340320a0307cSWu Fengguang { 3404b2261026SJoonsoo Kim int nr_nodes, node; 340520a0307cSWu Fengguang 34069487ca60SMike Kravetz lockdep_assert_held(&hugetlb_lock); 340720a0307cSWu Fengguang VM_BUG_ON(delta != -1 && delta != 1); 340820a0307cSWu Fengguang 3409e8c5c824SLee Schermerhorn if (delta < 0) { 3410b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 3411b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node]) 3412b2261026SJoonsoo Kim goto found; 3413b2261026SJoonsoo Kim } 3414b2261026SJoonsoo Kim } else { 3415b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 3416b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node] < 3417b2261026SJoonsoo Kim h->nr_huge_pages_node[node]) 3418b2261026SJoonsoo Kim goto found; 3419e8c5c824SLee Schermerhorn } 34209a76db09SLee Schermerhorn } 3421b2261026SJoonsoo Kim return 0; 342220a0307cSWu Fengguang 3423b2261026SJoonsoo Kim found: 342420a0307cSWu Fengguang h->surplus_huge_pages += delta; 3425b2261026SJoonsoo Kim h->surplus_huge_pages_node[node] += delta; 3426b2261026SJoonsoo Kim return 1; 342720a0307cSWu Fengguang } 342820a0307cSWu Fengguang 3429a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) 3430fd875dcaSMike Kravetz static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid, 34316ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 34321da177e4SLinus Torvalds { 34337893d1d5SAdam Litke unsigned long min_count, ret; 343410c6ec49SMike Kravetz struct page *page; 343510c6ec49SMike Kravetz LIST_HEAD(page_list); 3436f60858f9SMike Kravetz NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL); 3437f60858f9SMike Kravetz 3438f60858f9SMike Kravetz /* 3439f60858f9SMike Kravetz * Bit mask controlling how hard we retry per-node allocations. 3440f60858f9SMike Kravetz * If we can not allocate the bit mask, do not attempt to allocate 3441f60858f9SMike Kravetz * the requested huge pages. 3442f60858f9SMike Kravetz */ 3443f60858f9SMike Kravetz if (node_alloc_noretry) 3444f60858f9SMike Kravetz nodes_clear(*node_alloc_noretry); 3445f60858f9SMike Kravetz else 3446f60858f9SMike Kravetz return -ENOMEM; 34471da177e4SLinus Torvalds 344829383967SMike Kravetz /* 344929383967SMike Kravetz * resize_lock mutex prevents concurrent adjustments to number of 345029383967SMike Kravetz * pages in hstate via the proc/sysfs interfaces. 345129383967SMike Kravetz */ 345229383967SMike Kravetz mutex_lock(&h->resize_lock); 3453b65d4adbSMuchun Song flush_free_hpage_work(h); 3454db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 34554eb0716eSAlexandre Ghiti 34564eb0716eSAlexandre Ghiti /* 3457fd875dcaSMike Kravetz * Check for a node specific request. 3458fd875dcaSMike Kravetz * Changing node specific huge page count may require a corresponding 3459fd875dcaSMike Kravetz * change to the global count. In any case, the passed node mask 3460fd875dcaSMike Kravetz * (nodes_allowed) will restrict alloc/free to the specified node. 3461fd875dcaSMike Kravetz */ 3462fd875dcaSMike Kravetz if (nid != NUMA_NO_NODE) { 3463fd875dcaSMike Kravetz unsigned long old_count = count; 3464fd875dcaSMike Kravetz 3465fd875dcaSMike Kravetz count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; 3466fd875dcaSMike Kravetz /* 3467fd875dcaSMike Kravetz * User may have specified a large count value which caused the 3468fd875dcaSMike Kravetz * above calculation to overflow. In this case, they wanted 3469fd875dcaSMike Kravetz * to allocate as many huge pages as possible. Set count to 3470fd875dcaSMike Kravetz * largest possible value to align with their intention. 3471fd875dcaSMike Kravetz */ 3472fd875dcaSMike Kravetz if (count < old_count) 3473fd875dcaSMike Kravetz count = ULONG_MAX; 3474fd875dcaSMike Kravetz } 3475fd875dcaSMike Kravetz 3476fd875dcaSMike Kravetz /* 34774eb0716eSAlexandre Ghiti * Gigantic pages runtime allocation depend on the capability for large 34784eb0716eSAlexandre Ghiti * page range allocation. 34794eb0716eSAlexandre Ghiti * If the system does not provide this feature, return an error when 34804eb0716eSAlexandre Ghiti * the user tries to allocate gigantic pages but let the user free the 34814eb0716eSAlexandre Ghiti * boottime allocated gigantic pages. 34824eb0716eSAlexandre Ghiti */ 34834eb0716eSAlexandre Ghiti if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) { 34844eb0716eSAlexandre Ghiti if (count > persistent_huge_pages(h)) { 3485db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 348629383967SMike Kravetz mutex_unlock(&h->resize_lock); 3487f60858f9SMike Kravetz NODEMASK_FREE(node_alloc_noretry); 34884eb0716eSAlexandre Ghiti return -EINVAL; 34894eb0716eSAlexandre Ghiti } 34904eb0716eSAlexandre Ghiti /* Fall through to decrease pool */ 34914eb0716eSAlexandre Ghiti } 3492aa888a74SAndi Kleen 34937893d1d5SAdam Litke /* 34947893d1d5SAdam Litke * Increase the pool size 34957893d1d5SAdam Litke * First take pages out of surplus state. Then make up the 34967893d1d5SAdam Litke * remaining difference by allocating fresh huge pages. 3497d1c3fb1fSNishanth Aravamudan * 34980c397daeSMichal Hocko * We might race with alloc_surplus_huge_page() here and be unable 3499d1c3fb1fSNishanth Aravamudan * to convert a surplus huge page to a normal huge page. That is 3500d1c3fb1fSNishanth Aravamudan * not critical, though, it just means the overall size of the 3501d1c3fb1fSNishanth Aravamudan * pool might be one hugepage larger than it needs to be, but 3502d1c3fb1fSNishanth Aravamudan * within all the constraints specified by the sysctls. 35037893d1d5SAdam Litke */ 3504a5516438SAndi Kleen while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { 35056ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, -1)) 35067893d1d5SAdam Litke break; 35077893d1d5SAdam Litke } 35087893d1d5SAdam Litke 3509a5516438SAndi Kleen while (count > persistent_huge_pages(h)) { 35107893d1d5SAdam Litke /* 35117893d1d5SAdam Litke * If this allocation races such that we no longer need the 35127893d1d5SAdam Litke * page, free_huge_page will handle it by freeing the page 35137893d1d5SAdam Litke * and reducing the surplus. 35147893d1d5SAdam Litke */ 3515db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 3516649920c6SJia He 3517649920c6SJia He /* yield cpu to avoid soft lockup */ 3518649920c6SJia He cond_resched(); 3519649920c6SJia He 3520f60858f9SMike Kravetz ret = alloc_pool_huge_page(h, nodes_allowed, 3521f60858f9SMike Kravetz node_alloc_noretry); 3522db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 35237893d1d5SAdam Litke if (!ret) 35247893d1d5SAdam Litke goto out; 35257893d1d5SAdam Litke 3526536240f2SMel Gorman /* Bail for signals. Probably ctrl-c from user */ 3527536240f2SMel Gorman if (signal_pending(current)) 3528536240f2SMel Gorman goto out; 35297893d1d5SAdam Litke } 35307893d1d5SAdam Litke 35317893d1d5SAdam Litke /* 35327893d1d5SAdam Litke * Decrease the pool size 35337893d1d5SAdam Litke * First return free pages to the buddy allocator (being careful 35347893d1d5SAdam Litke * to keep enough around to satisfy reservations). Then place 35357893d1d5SAdam Litke * pages into surplus state as needed so the pool will shrink 35367893d1d5SAdam Litke * to the desired size as pages become free. 3537d1c3fb1fSNishanth Aravamudan * 3538d1c3fb1fSNishanth Aravamudan * By placing pages into the surplus state independent of the 3539d1c3fb1fSNishanth Aravamudan * overcommit value, we are allowing the surplus pool size to 3540d1c3fb1fSNishanth Aravamudan * exceed overcommit. There are few sane options here. Since 35410c397daeSMichal Hocko * alloc_surplus_huge_page() is checking the global counter, 3542d1c3fb1fSNishanth Aravamudan * though, we'll note that we're not allowed to exceed surplus 3543d1c3fb1fSNishanth Aravamudan * and won't grow the pool anywhere else. Not until one of the 3544d1c3fb1fSNishanth Aravamudan * sysctls are changed, or the surplus pages go out of use. 35457893d1d5SAdam Litke */ 3546a5516438SAndi Kleen min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; 35476b0c880dSAdam Litke min_count = max(count, min_count); 35486ae11b27SLee Schermerhorn try_to_free_low(h, min_count, nodes_allowed); 354910c6ec49SMike Kravetz 355010c6ec49SMike Kravetz /* 355110c6ec49SMike Kravetz * Collect pages to be removed on list without dropping lock 355210c6ec49SMike Kravetz */ 3553a5516438SAndi Kleen while (min_count < persistent_huge_pages(h)) { 355410c6ec49SMike Kravetz page = remove_pool_huge_page(h, nodes_allowed, 0); 355510c6ec49SMike Kravetz if (!page) 35561da177e4SLinus Torvalds break; 355710c6ec49SMike Kravetz 355810c6ec49SMike Kravetz list_add(&page->lru, &page_list); 35591da177e4SLinus Torvalds } 356010c6ec49SMike Kravetz /* free the pages after dropping lock */ 3561db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 356210c6ec49SMike Kravetz update_and_free_pages_bulk(h, &page_list); 3563b65d4adbSMuchun Song flush_free_hpage_work(h); 3564db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 356510c6ec49SMike Kravetz 3566a5516438SAndi Kleen while (count < persistent_huge_pages(h)) { 35676ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, 1)) 35687893d1d5SAdam Litke break; 35697893d1d5SAdam Litke } 35707893d1d5SAdam Litke out: 35714eb0716eSAlexandre Ghiti h->max_huge_pages = persistent_huge_pages(h); 3572db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 357329383967SMike Kravetz mutex_unlock(&h->resize_lock); 35744eb0716eSAlexandre Ghiti 3575f60858f9SMike Kravetz NODEMASK_FREE(node_alloc_noretry); 3576f60858f9SMike Kravetz 35774eb0716eSAlexandre Ghiti return 0; 35781da177e4SLinus Torvalds } 35791da177e4SLinus Torvalds 35808531fc6fSMike Kravetz static int demote_free_huge_page(struct hstate *h, struct page *page) 35818531fc6fSMike Kravetz { 35828531fc6fSMike Kravetz int i, nid = page_to_nid(page); 35838531fc6fSMike Kravetz struct hstate *target_hstate; 3584911565b8SSidhartha Kumar struct folio *folio = page_folio(page); 358531731452SDoug Berger struct page *subpage; 35868531fc6fSMike Kravetz int rc = 0; 35878531fc6fSMike Kravetz 35888531fc6fSMike Kravetz target_hstate = size_to_hstate(PAGE_SIZE << h->demote_order); 35898531fc6fSMike Kravetz 3590cfd5082bSSidhartha Kumar remove_hugetlb_folio_for_demote(h, folio, false); 35918531fc6fSMike Kravetz spin_unlock_irq(&hugetlb_lock); 35928531fc6fSMike Kravetz 35936213834cSMuchun Song rc = hugetlb_vmemmap_restore(h, page); 35948531fc6fSMike Kravetz if (rc) { 35958531fc6fSMike Kravetz /* Allocation of vmemmmap failed, we can not demote page */ 35968531fc6fSMike Kravetz spin_lock_irq(&hugetlb_lock); 35978531fc6fSMike Kravetz set_page_refcounted(page); 35982f6c57d6SSidhartha Kumar add_hugetlb_folio(h, page_folio(page), false); 35998531fc6fSMike Kravetz return rc; 36008531fc6fSMike Kravetz } 36018531fc6fSMike Kravetz 36028531fc6fSMike Kravetz /* 3603911565b8SSidhartha Kumar * Use destroy_compound_hugetlb_folio_for_demote for all huge page 36048531fc6fSMike Kravetz * sizes as it will not ref count pages. 36058531fc6fSMike Kravetz */ 3606911565b8SSidhartha Kumar destroy_compound_hugetlb_folio_for_demote(folio, huge_page_order(h)); 36078531fc6fSMike Kravetz 36088531fc6fSMike Kravetz /* 36098531fc6fSMike Kravetz * Taking target hstate mutex synchronizes with set_max_huge_pages. 36108531fc6fSMike Kravetz * Without the mutex, pages added to target hstate could be marked 36118531fc6fSMike Kravetz * as surplus. 36128531fc6fSMike Kravetz * 36138531fc6fSMike Kravetz * Note that we already hold h->resize_lock. To prevent deadlock, 36148531fc6fSMike Kravetz * use the convention of always taking larger size hstate mutex first. 36158531fc6fSMike Kravetz */ 36168531fc6fSMike Kravetz mutex_lock(&target_hstate->resize_lock); 36178531fc6fSMike Kravetz for (i = 0; i < pages_per_huge_page(h); 36188531fc6fSMike Kravetz i += pages_per_huge_page(target_hstate)) { 361931731452SDoug Berger subpage = nth_page(page, i); 3620d1c60955SSidhartha Kumar folio = page_folio(subpage); 36218531fc6fSMike Kravetz if (hstate_is_gigantic(target_hstate)) 3622d1c60955SSidhartha Kumar prep_compound_gigantic_folio_for_demote(folio, 36238531fc6fSMike Kravetz target_hstate->order); 36248531fc6fSMike Kravetz else 362531731452SDoug Berger prep_compound_page(subpage, target_hstate->order); 362631731452SDoug Berger set_page_private(subpage, 0); 3627d1c60955SSidhartha Kumar prep_new_hugetlb_folio(target_hstate, folio, nid); 36282b21624fSMike Kravetz free_huge_page(subpage); 36298531fc6fSMike Kravetz } 36308531fc6fSMike Kravetz mutex_unlock(&target_hstate->resize_lock); 36318531fc6fSMike Kravetz 36328531fc6fSMike Kravetz spin_lock_irq(&hugetlb_lock); 36338531fc6fSMike Kravetz 36348531fc6fSMike Kravetz /* 36358531fc6fSMike Kravetz * Not absolutely necessary, but for consistency update max_huge_pages 36368531fc6fSMike Kravetz * based on pool changes for the demoted page. 36378531fc6fSMike Kravetz */ 36388531fc6fSMike Kravetz h->max_huge_pages--; 3639a43a83c7SMiaohe Lin target_hstate->max_huge_pages += 3640a43a83c7SMiaohe Lin pages_per_huge_page(h) / pages_per_huge_page(target_hstate); 36418531fc6fSMike Kravetz 36428531fc6fSMike Kravetz return rc; 36438531fc6fSMike Kravetz } 36448531fc6fSMike Kravetz 364579dfc695SMike Kravetz static int demote_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed) 364679dfc695SMike Kravetz __must_hold(&hugetlb_lock) 364779dfc695SMike Kravetz { 36488531fc6fSMike Kravetz int nr_nodes, node; 36498531fc6fSMike Kravetz struct page *page; 365079dfc695SMike Kravetz 365179dfc695SMike Kravetz lockdep_assert_held(&hugetlb_lock); 365279dfc695SMike Kravetz 365379dfc695SMike Kravetz /* We should never get here if no demote order */ 365479dfc695SMike Kravetz if (!h->demote_order) { 365579dfc695SMike Kravetz pr_warn("HugeTLB: NULL demote order passed to demote_pool_huge_page.\n"); 365679dfc695SMike Kravetz return -EINVAL; /* internal error */ 365779dfc695SMike Kravetz } 365879dfc695SMike Kravetz 36598531fc6fSMike Kravetz for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 36605a317412SMike Kravetz list_for_each_entry(page, &h->hugepage_freelists[node], lru) { 36615a317412SMike Kravetz if (PageHWPoison(page)) 36625a317412SMike Kravetz continue; 36635a317412SMike Kravetz 36645a317412SMike Kravetz return demote_free_huge_page(h, page); 36658531fc6fSMike Kravetz } 36668531fc6fSMike Kravetz } 36678531fc6fSMike Kravetz 36685a317412SMike Kravetz /* 36695a317412SMike Kravetz * Only way to get here is if all pages on free lists are poisoned. 36705a317412SMike Kravetz * Return -EBUSY so that caller will not retry. 36715a317412SMike Kravetz */ 36725a317412SMike Kravetz return -EBUSY; 367379dfc695SMike Kravetz } 367479dfc695SMike Kravetz 3675a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \ 3676a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 3677a3437870SNishanth Aravamudan 367879dfc695SMike Kravetz #define HSTATE_ATTR_WO(_name) \ 367979dfc695SMike Kravetz static struct kobj_attribute _name##_attr = __ATTR_WO(_name) 368079dfc695SMike Kravetz 3681a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \ 368298bc26acSMiaohe Lin static struct kobj_attribute _name##_attr = __ATTR_RW(_name) 3683a3437870SNishanth Aravamudan 3684a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj; 3685a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 3686a3437870SNishanth Aravamudan 36879a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp); 36889a305230SLee Schermerhorn 36899a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp) 3690a3437870SNishanth Aravamudan { 3691a3437870SNishanth Aravamudan int i; 36929a305230SLee Schermerhorn 3693a3437870SNishanth Aravamudan for (i = 0; i < HUGE_MAX_HSTATE; i++) 36949a305230SLee Schermerhorn if (hstate_kobjs[i] == kobj) { 36959a305230SLee Schermerhorn if (nidp) 36969a305230SLee Schermerhorn *nidp = NUMA_NO_NODE; 3697a3437870SNishanth Aravamudan return &hstates[i]; 36989a305230SLee Schermerhorn } 36999a305230SLee Schermerhorn 37009a305230SLee Schermerhorn return kobj_to_node_hstate(kobj, nidp); 3701a3437870SNishanth Aravamudan } 3702a3437870SNishanth Aravamudan 370306808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj, 3704a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3705a3437870SNishanth Aravamudan { 37069a305230SLee Schermerhorn struct hstate *h; 37079a305230SLee Schermerhorn unsigned long nr_huge_pages; 37089a305230SLee Schermerhorn int nid; 37099a305230SLee Schermerhorn 37109a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 37119a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 37129a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages; 37139a305230SLee Schermerhorn else 37149a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages_node[nid]; 37159a305230SLee Schermerhorn 3716ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", nr_huge_pages); 3717a3437870SNishanth Aravamudan } 3718adbe8726SEric B Munson 3719238d3c13SDavid Rientjes static ssize_t __nr_hugepages_store_common(bool obey_mempolicy, 3720238d3c13SDavid Rientjes struct hstate *h, int nid, 3721238d3c13SDavid Rientjes unsigned long count, size_t len) 3722a3437870SNishanth Aravamudan { 3723a3437870SNishanth Aravamudan int err; 37242d0adf7eSOscar Salvador nodemask_t nodes_allowed, *n_mask; 3725a3437870SNishanth Aravamudan 37262d0adf7eSOscar Salvador if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) 37272d0adf7eSOscar Salvador return -EINVAL; 3728adbe8726SEric B Munson 37299a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) { 37309a305230SLee Schermerhorn /* 37319a305230SLee Schermerhorn * global hstate attribute 37329a305230SLee Schermerhorn */ 37339a305230SLee Schermerhorn if (!(obey_mempolicy && 37342d0adf7eSOscar Salvador init_nodemask_of_mempolicy(&nodes_allowed))) 37352d0adf7eSOscar Salvador n_mask = &node_states[N_MEMORY]; 37362d0adf7eSOscar Salvador else 37372d0adf7eSOscar Salvador n_mask = &nodes_allowed; 37382d0adf7eSOscar Salvador } else { 37399a305230SLee Schermerhorn /* 3740fd875dcaSMike Kravetz * Node specific request. count adjustment happens in 3741fd875dcaSMike Kravetz * set_max_huge_pages() after acquiring hugetlb_lock. 37429a305230SLee Schermerhorn */ 37432d0adf7eSOscar Salvador init_nodemask_of_node(&nodes_allowed, nid); 37442d0adf7eSOscar Salvador n_mask = &nodes_allowed; 3745fd875dcaSMike Kravetz } 37469a305230SLee Schermerhorn 37472d0adf7eSOscar Salvador err = set_max_huge_pages(h, count, nid, n_mask); 374806808b08SLee Schermerhorn 37494eb0716eSAlexandre Ghiti return err ? err : len; 375006808b08SLee Schermerhorn } 375106808b08SLee Schermerhorn 3752238d3c13SDavid Rientjes static ssize_t nr_hugepages_store_common(bool obey_mempolicy, 3753238d3c13SDavid Rientjes struct kobject *kobj, const char *buf, 3754238d3c13SDavid Rientjes size_t len) 3755238d3c13SDavid Rientjes { 3756238d3c13SDavid Rientjes struct hstate *h; 3757238d3c13SDavid Rientjes unsigned long count; 3758238d3c13SDavid Rientjes int nid; 3759238d3c13SDavid Rientjes int err; 3760238d3c13SDavid Rientjes 3761238d3c13SDavid Rientjes err = kstrtoul(buf, 10, &count); 3762238d3c13SDavid Rientjes if (err) 3763238d3c13SDavid Rientjes return err; 3764238d3c13SDavid Rientjes 3765238d3c13SDavid Rientjes h = kobj_to_hstate(kobj, &nid); 3766238d3c13SDavid Rientjes return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len); 3767238d3c13SDavid Rientjes } 3768238d3c13SDavid Rientjes 376906808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj, 377006808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 377106808b08SLee Schermerhorn { 377206808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 377306808b08SLee Schermerhorn } 377406808b08SLee Schermerhorn 377506808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj, 377606808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 377706808b08SLee Schermerhorn { 3778238d3c13SDavid Rientjes return nr_hugepages_store_common(false, kobj, buf, len); 3779a3437870SNishanth Aravamudan } 3780a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages); 3781a3437870SNishanth Aravamudan 378206808b08SLee Schermerhorn #ifdef CONFIG_NUMA 378306808b08SLee Schermerhorn 378406808b08SLee Schermerhorn /* 378506808b08SLee Schermerhorn * hstate attribute for optionally mempolicy-based constraint on persistent 378606808b08SLee Schermerhorn * huge page alloc/free. 378706808b08SLee Schermerhorn */ 378806808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj, 3789ae7a927dSJoe Perches struct kobj_attribute *attr, 3790ae7a927dSJoe Perches char *buf) 379106808b08SLee Schermerhorn { 379206808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 379306808b08SLee Schermerhorn } 379406808b08SLee Schermerhorn 379506808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj, 379606808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 379706808b08SLee Schermerhorn { 3798238d3c13SDavid Rientjes return nr_hugepages_store_common(true, kobj, buf, len); 379906808b08SLee Schermerhorn } 380006808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy); 380106808b08SLee Schermerhorn #endif 380206808b08SLee Schermerhorn 380306808b08SLee Schermerhorn 3804a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, 3805a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3806a3437870SNishanth Aravamudan { 38079a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 3808ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", h->nr_overcommit_huge_pages); 3809a3437870SNishanth Aravamudan } 3810adbe8726SEric B Munson 3811a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, 3812a3437870SNishanth Aravamudan struct kobj_attribute *attr, const char *buf, size_t count) 3813a3437870SNishanth Aravamudan { 3814a3437870SNishanth Aravamudan int err; 3815a3437870SNishanth Aravamudan unsigned long input; 38169a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 3817a3437870SNishanth Aravamudan 3818bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 3819adbe8726SEric B Munson return -EINVAL; 3820adbe8726SEric B Munson 38213dbb95f7SJingoo Han err = kstrtoul(buf, 10, &input); 3822a3437870SNishanth Aravamudan if (err) 382373ae31e5SEric B Munson return err; 3824a3437870SNishanth Aravamudan 3825db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 3826a3437870SNishanth Aravamudan h->nr_overcommit_huge_pages = input; 3827db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 3828a3437870SNishanth Aravamudan 3829a3437870SNishanth Aravamudan return count; 3830a3437870SNishanth Aravamudan } 3831a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages); 3832a3437870SNishanth Aravamudan 3833a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj, 3834a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3835a3437870SNishanth Aravamudan { 38369a305230SLee Schermerhorn struct hstate *h; 38379a305230SLee Schermerhorn unsigned long free_huge_pages; 38389a305230SLee Schermerhorn int nid; 38399a305230SLee Schermerhorn 38409a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 38419a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 38429a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages; 38439a305230SLee Schermerhorn else 38449a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages_node[nid]; 38459a305230SLee Schermerhorn 3846ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", free_huge_pages); 3847a3437870SNishanth Aravamudan } 3848a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages); 3849a3437870SNishanth Aravamudan 3850a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj, 3851a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3852a3437870SNishanth Aravamudan { 38539a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 3854ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", h->resv_huge_pages); 3855a3437870SNishanth Aravamudan } 3856a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages); 3857a3437870SNishanth Aravamudan 3858a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj, 3859a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 3860a3437870SNishanth Aravamudan { 38619a305230SLee Schermerhorn struct hstate *h; 38629a305230SLee Schermerhorn unsigned long surplus_huge_pages; 38639a305230SLee Schermerhorn int nid; 38649a305230SLee Schermerhorn 38659a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 38669a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 38679a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages; 38689a305230SLee Schermerhorn else 38699a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages_node[nid]; 38709a305230SLee Schermerhorn 3871ae7a927dSJoe Perches return sysfs_emit(buf, "%lu\n", surplus_huge_pages); 3872a3437870SNishanth Aravamudan } 3873a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages); 3874a3437870SNishanth Aravamudan 387579dfc695SMike Kravetz static ssize_t demote_store(struct kobject *kobj, 387679dfc695SMike Kravetz struct kobj_attribute *attr, const char *buf, size_t len) 387779dfc695SMike Kravetz { 387879dfc695SMike Kravetz unsigned long nr_demote; 387979dfc695SMike Kravetz unsigned long nr_available; 388079dfc695SMike Kravetz nodemask_t nodes_allowed, *n_mask; 388179dfc695SMike Kravetz struct hstate *h; 38828eeda55fSLi zeming int err; 388379dfc695SMike Kravetz int nid; 388479dfc695SMike Kravetz 388579dfc695SMike Kravetz err = kstrtoul(buf, 10, &nr_demote); 388679dfc695SMike Kravetz if (err) 388779dfc695SMike Kravetz return err; 388879dfc695SMike Kravetz h = kobj_to_hstate(kobj, &nid); 388979dfc695SMike Kravetz 389079dfc695SMike Kravetz if (nid != NUMA_NO_NODE) { 389179dfc695SMike Kravetz init_nodemask_of_node(&nodes_allowed, nid); 389279dfc695SMike Kravetz n_mask = &nodes_allowed; 389379dfc695SMike Kravetz } else { 389479dfc695SMike Kravetz n_mask = &node_states[N_MEMORY]; 389579dfc695SMike Kravetz } 389679dfc695SMike Kravetz 389779dfc695SMike Kravetz /* Synchronize with other sysfs operations modifying huge pages */ 389879dfc695SMike Kravetz mutex_lock(&h->resize_lock); 389979dfc695SMike Kravetz spin_lock_irq(&hugetlb_lock); 390079dfc695SMike Kravetz 390179dfc695SMike Kravetz while (nr_demote) { 390279dfc695SMike Kravetz /* 390379dfc695SMike Kravetz * Check for available pages to demote each time thorough the 390479dfc695SMike Kravetz * loop as demote_pool_huge_page will drop hugetlb_lock. 390579dfc695SMike Kravetz */ 390679dfc695SMike Kravetz if (nid != NUMA_NO_NODE) 390779dfc695SMike Kravetz nr_available = h->free_huge_pages_node[nid]; 390879dfc695SMike Kravetz else 390979dfc695SMike Kravetz nr_available = h->free_huge_pages; 391079dfc695SMike Kravetz nr_available -= h->resv_huge_pages; 391179dfc695SMike Kravetz if (!nr_available) 391279dfc695SMike Kravetz break; 391379dfc695SMike Kravetz 391479dfc695SMike Kravetz err = demote_pool_huge_page(h, n_mask); 391579dfc695SMike Kravetz if (err) 391679dfc695SMike Kravetz break; 391779dfc695SMike Kravetz 391879dfc695SMike Kravetz nr_demote--; 391979dfc695SMike Kravetz } 392079dfc695SMike Kravetz 392179dfc695SMike Kravetz spin_unlock_irq(&hugetlb_lock); 392279dfc695SMike Kravetz mutex_unlock(&h->resize_lock); 392379dfc695SMike Kravetz 392479dfc695SMike Kravetz if (err) 392579dfc695SMike Kravetz return err; 392679dfc695SMike Kravetz return len; 392779dfc695SMike Kravetz } 392879dfc695SMike Kravetz HSTATE_ATTR_WO(demote); 392979dfc695SMike Kravetz 393079dfc695SMike Kravetz static ssize_t demote_size_show(struct kobject *kobj, 393179dfc695SMike Kravetz struct kobj_attribute *attr, char *buf) 393279dfc695SMike Kravetz { 393312658abfSMiaohe Lin struct hstate *h = kobj_to_hstate(kobj, NULL); 393479dfc695SMike Kravetz unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K; 393579dfc695SMike Kravetz 393679dfc695SMike Kravetz return sysfs_emit(buf, "%lukB\n", demote_size); 393779dfc695SMike Kravetz } 393879dfc695SMike Kravetz 393979dfc695SMike Kravetz static ssize_t demote_size_store(struct kobject *kobj, 394079dfc695SMike Kravetz struct kobj_attribute *attr, 394179dfc695SMike Kravetz const char *buf, size_t count) 394279dfc695SMike Kravetz { 394379dfc695SMike Kravetz struct hstate *h, *demote_hstate; 394479dfc695SMike Kravetz unsigned long demote_size; 394579dfc695SMike Kravetz unsigned int demote_order; 394679dfc695SMike Kravetz 394779dfc695SMike Kravetz demote_size = (unsigned long)memparse(buf, NULL); 394879dfc695SMike Kravetz 394979dfc695SMike Kravetz demote_hstate = size_to_hstate(demote_size); 395079dfc695SMike Kravetz if (!demote_hstate) 395179dfc695SMike Kravetz return -EINVAL; 395279dfc695SMike Kravetz demote_order = demote_hstate->order; 3953a01f4390SMike Kravetz if (demote_order < HUGETLB_PAGE_ORDER) 3954a01f4390SMike Kravetz return -EINVAL; 395579dfc695SMike Kravetz 395679dfc695SMike Kravetz /* demote order must be smaller than hstate order */ 395712658abfSMiaohe Lin h = kobj_to_hstate(kobj, NULL); 395879dfc695SMike Kravetz if (demote_order >= h->order) 395979dfc695SMike Kravetz return -EINVAL; 396079dfc695SMike Kravetz 396179dfc695SMike Kravetz /* resize_lock synchronizes access to demote size and writes */ 396279dfc695SMike Kravetz mutex_lock(&h->resize_lock); 396379dfc695SMike Kravetz h->demote_order = demote_order; 396479dfc695SMike Kravetz mutex_unlock(&h->resize_lock); 396579dfc695SMike Kravetz 396679dfc695SMike Kravetz return count; 396779dfc695SMike Kravetz } 396879dfc695SMike Kravetz HSTATE_ATTR(demote_size); 396979dfc695SMike Kravetz 3970a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = { 3971a3437870SNishanth Aravamudan &nr_hugepages_attr.attr, 3972a3437870SNishanth Aravamudan &nr_overcommit_hugepages_attr.attr, 3973a3437870SNishanth Aravamudan &free_hugepages_attr.attr, 3974a3437870SNishanth Aravamudan &resv_hugepages_attr.attr, 3975a3437870SNishanth Aravamudan &surplus_hugepages_attr.attr, 397606808b08SLee Schermerhorn #ifdef CONFIG_NUMA 397706808b08SLee Schermerhorn &nr_hugepages_mempolicy_attr.attr, 397806808b08SLee Schermerhorn #endif 3979a3437870SNishanth Aravamudan NULL, 3980a3437870SNishanth Aravamudan }; 3981a3437870SNishanth Aravamudan 398267e5ed96SArvind Yadav static const struct attribute_group hstate_attr_group = { 3983a3437870SNishanth Aravamudan .attrs = hstate_attrs, 3984a3437870SNishanth Aravamudan }; 3985a3437870SNishanth Aravamudan 398679dfc695SMike Kravetz static struct attribute *hstate_demote_attrs[] = { 398779dfc695SMike Kravetz &demote_size_attr.attr, 398879dfc695SMike Kravetz &demote_attr.attr, 398979dfc695SMike Kravetz NULL, 399079dfc695SMike Kravetz }; 399179dfc695SMike Kravetz 399279dfc695SMike Kravetz static const struct attribute_group hstate_demote_attr_group = { 399379dfc695SMike Kravetz .attrs = hstate_demote_attrs, 399479dfc695SMike Kravetz }; 399579dfc695SMike Kravetz 3996094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, 39979a305230SLee Schermerhorn struct kobject **hstate_kobjs, 399867e5ed96SArvind Yadav const struct attribute_group *hstate_attr_group) 3999a3437870SNishanth Aravamudan { 4000a3437870SNishanth Aravamudan int retval; 4001972dc4deSAneesh Kumar K.V int hi = hstate_index(h); 4002a3437870SNishanth Aravamudan 40039a305230SLee Schermerhorn hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); 40049a305230SLee Schermerhorn if (!hstate_kobjs[hi]) 4005a3437870SNishanth Aravamudan return -ENOMEM; 4006a3437870SNishanth Aravamudan 40079a305230SLee Schermerhorn retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group); 4008cc2205a6SMiaohe Lin if (retval) { 40099a305230SLee Schermerhorn kobject_put(hstate_kobjs[hi]); 4010cc2205a6SMiaohe Lin hstate_kobjs[hi] = NULL; 40113a6bdda0SMiaohe Lin return retval; 4012cc2205a6SMiaohe Lin } 4013a3437870SNishanth Aravamudan 401479dfc695SMike Kravetz if (h->demote_order) { 401501088a60SMiaohe Lin retval = sysfs_create_group(hstate_kobjs[hi], 401601088a60SMiaohe Lin &hstate_demote_attr_group); 401701088a60SMiaohe Lin if (retval) { 401879dfc695SMike Kravetz pr_warn("HugeTLB unable to create demote interfaces for %s\n", h->name); 401901088a60SMiaohe Lin sysfs_remove_group(hstate_kobjs[hi], hstate_attr_group); 402001088a60SMiaohe Lin kobject_put(hstate_kobjs[hi]); 402101088a60SMiaohe Lin hstate_kobjs[hi] = NULL; 402201088a60SMiaohe Lin return retval; 402301088a60SMiaohe Lin } 402479dfc695SMike Kravetz } 402579dfc695SMike Kravetz 402601088a60SMiaohe Lin return 0; 4027a3437870SNishanth Aravamudan } 4028a3437870SNishanth Aravamudan 40299a305230SLee Schermerhorn #ifdef CONFIG_NUMA 4030a4a00b45SMuchun Song static bool hugetlb_sysfs_initialized __ro_after_init; 40319a305230SLee Schermerhorn 40329a305230SLee Schermerhorn /* 40339a305230SLee Schermerhorn * node_hstate/s - associate per node hstate attributes, via their kobjects, 403410fbcf4cSKay Sievers * with node devices in node_devices[] using a parallel array. The array 403510fbcf4cSKay Sievers * index of a node device or _hstate == node id. 403610fbcf4cSKay Sievers * This is here to avoid any static dependency of the node device driver, in 40379a305230SLee Schermerhorn * the base kernel, on the hugetlb module. 40389a305230SLee Schermerhorn */ 40399a305230SLee Schermerhorn struct node_hstate { 40409a305230SLee Schermerhorn struct kobject *hugepages_kobj; 40419a305230SLee Schermerhorn struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 40429a305230SLee Schermerhorn }; 4043b4e289a6SAlexander Kuleshov static struct node_hstate node_hstates[MAX_NUMNODES]; 40449a305230SLee Schermerhorn 40459a305230SLee Schermerhorn /* 404610fbcf4cSKay Sievers * A subset of global hstate attributes for node devices 40479a305230SLee Schermerhorn */ 40489a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = { 40499a305230SLee Schermerhorn &nr_hugepages_attr.attr, 40509a305230SLee Schermerhorn &free_hugepages_attr.attr, 40519a305230SLee Schermerhorn &surplus_hugepages_attr.attr, 40529a305230SLee Schermerhorn NULL, 40539a305230SLee Schermerhorn }; 40549a305230SLee Schermerhorn 405567e5ed96SArvind Yadav static const struct attribute_group per_node_hstate_attr_group = { 40569a305230SLee Schermerhorn .attrs = per_node_hstate_attrs, 40579a305230SLee Schermerhorn }; 40589a305230SLee Schermerhorn 40599a305230SLee Schermerhorn /* 406010fbcf4cSKay Sievers * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj. 40619a305230SLee Schermerhorn * Returns node id via non-NULL nidp. 40629a305230SLee Schermerhorn */ 40639a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 40649a305230SLee Schermerhorn { 40659a305230SLee Schermerhorn int nid; 40669a305230SLee Schermerhorn 40679a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) { 40689a305230SLee Schermerhorn struct node_hstate *nhs = &node_hstates[nid]; 40699a305230SLee Schermerhorn int i; 40709a305230SLee Schermerhorn for (i = 0; i < HUGE_MAX_HSTATE; i++) 40719a305230SLee Schermerhorn if (nhs->hstate_kobjs[i] == kobj) { 40729a305230SLee Schermerhorn if (nidp) 40739a305230SLee Schermerhorn *nidp = nid; 40749a305230SLee Schermerhorn return &hstates[i]; 40759a305230SLee Schermerhorn } 40769a305230SLee Schermerhorn } 40779a305230SLee Schermerhorn 40789a305230SLee Schermerhorn BUG(); 40799a305230SLee Schermerhorn return NULL; 40809a305230SLee Schermerhorn } 40819a305230SLee Schermerhorn 40829a305230SLee Schermerhorn /* 408310fbcf4cSKay Sievers * Unregister hstate attributes from a single node device. 40849a305230SLee Schermerhorn * No-op if no hstate attributes attached. 40859a305230SLee Schermerhorn */ 4086a4a00b45SMuchun Song void hugetlb_unregister_node(struct node *node) 40879a305230SLee Schermerhorn { 40889a305230SLee Schermerhorn struct hstate *h; 408910fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 40909a305230SLee Schermerhorn 40919a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 40929b5e5d0fSLee Schermerhorn return; /* no hstate attributes */ 40939a305230SLee Schermerhorn 4094972dc4deSAneesh Kumar K.V for_each_hstate(h) { 4095972dc4deSAneesh Kumar K.V int idx = hstate_index(h); 409601088a60SMiaohe Lin struct kobject *hstate_kobj = nhs->hstate_kobjs[idx]; 409701088a60SMiaohe Lin 409801088a60SMiaohe Lin if (!hstate_kobj) 409901088a60SMiaohe Lin continue; 410001088a60SMiaohe Lin if (h->demote_order) 410101088a60SMiaohe Lin sysfs_remove_group(hstate_kobj, &hstate_demote_attr_group); 410201088a60SMiaohe Lin sysfs_remove_group(hstate_kobj, &per_node_hstate_attr_group); 410301088a60SMiaohe Lin kobject_put(hstate_kobj); 4104972dc4deSAneesh Kumar K.V nhs->hstate_kobjs[idx] = NULL; 4105972dc4deSAneesh Kumar K.V } 41069a305230SLee Schermerhorn 41079a305230SLee Schermerhorn kobject_put(nhs->hugepages_kobj); 41089a305230SLee Schermerhorn nhs->hugepages_kobj = NULL; 41099a305230SLee Schermerhorn } 41109a305230SLee Schermerhorn 41119a305230SLee Schermerhorn 41129a305230SLee Schermerhorn /* 411310fbcf4cSKay Sievers * Register hstate attributes for a single node device. 41149a305230SLee Schermerhorn * No-op if attributes already registered. 41159a305230SLee Schermerhorn */ 4116a4a00b45SMuchun Song void hugetlb_register_node(struct node *node) 41179a305230SLee Schermerhorn { 41189a305230SLee Schermerhorn struct hstate *h; 411910fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 41209a305230SLee Schermerhorn int err; 41219a305230SLee Schermerhorn 4122a4a00b45SMuchun Song if (!hugetlb_sysfs_initialized) 4123a4a00b45SMuchun Song return; 4124a4a00b45SMuchun Song 41259a305230SLee Schermerhorn if (nhs->hugepages_kobj) 41269a305230SLee Schermerhorn return; /* already allocated */ 41279a305230SLee Schermerhorn 41289a305230SLee Schermerhorn nhs->hugepages_kobj = kobject_create_and_add("hugepages", 412910fbcf4cSKay Sievers &node->dev.kobj); 41309a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 41319a305230SLee Schermerhorn return; 41329a305230SLee Schermerhorn 41339a305230SLee Schermerhorn for_each_hstate(h) { 41349a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, 41359a305230SLee Schermerhorn nhs->hstate_kobjs, 41369a305230SLee Schermerhorn &per_node_hstate_attr_group); 41379a305230SLee Schermerhorn if (err) { 4138282f4214SMike Kravetz pr_err("HugeTLB: Unable to add hstate %s for node %d\n", 413910fbcf4cSKay Sievers h->name, node->dev.id); 41409a305230SLee Schermerhorn hugetlb_unregister_node(node); 41419a305230SLee Schermerhorn break; 41429a305230SLee Schermerhorn } 41439a305230SLee Schermerhorn } 41449a305230SLee Schermerhorn } 41459a305230SLee Schermerhorn 41469a305230SLee Schermerhorn /* 41479b5e5d0fSLee Schermerhorn * hugetlb init time: register hstate attributes for all registered node 414810fbcf4cSKay Sievers * devices of nodes that have memory. All on-line nodes should have 414910fbcf4cSKay Sievers * registered their associated device by this time. 41509a305230SLee Schermerhorn */ 41517d9ca000SLuiz Capitulino static void __init hugetlb_register_all_nodes(void) 41529a305230SLee Schermerhorn { 41539a305230SLee Schermerhorn int nid; 41549a305230SLee Schermerhorn 4155a4a00b45SMuchun Song for_each_online_node(nid) 4156b958d4d0SMuchun Song hugetlb_register_node(node_devices[nid]); 41579a305230SLee Schermerhorn } 41589a305230SLee Schermerhorn #else /* !CONFIG_NUMA */ 41599a305230SLee Schermerhorn 41609a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 41619a305230SLee Schermerhorn { 41629a305230SLee Schermerhorn BUG(); 41639a305230SLee Schermerhorn if (nidp) 41649a305230SLee Schermerhorn *nidp = -1; 41659a305230SLee Schermerhorn return NULL; 41669a305230SLee Schermerhorn } 41679a305230SLee Schermerhorn 41689a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { } 41699a305230SLee Schermerhorn 41709a305230SLee Schermerhorn #endif 41719a305230SLee Schermerhorn 4172263b8998SMiaohe Lin #ifdef CONFIG_CMA 4173263b8998SMiaohe Lin static void __init hugetlb_cma_check(void); 4174263b8998SMiaohe Lin #else 4175263b8998SMiaohe Lin static inline __init void hugetlb_cma_check(void) 4176263b8998SMiaohe Lin { 4177263b8998SMiaohe Lin } 4178263b8998SMiaohe Lin #endif 4179263b8998SMiaohe Lin 4180a4a00b45SMuchun Song static void __init hugetlb_sysfs_init(void) 4181a4a00b45SMuchun Song { 4182a4a00b45SMuchun Song struct hstate *h; 4183a4a00b45SMuchun Song int err; 4184a4a00b45SMuchun Song 4185a4a00b45SMuchun Song hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); 4186a4a00b45SMuchun Song if (!hugepages_kobj) 4187a4a00b45SMuchun Song return; 4188a4a00b45SMuchun Song 4189a4a00b45SMuchun Song for_each_hstate(h) { 4190a4a00b45SMuchun Song err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, 4191a4a00b45SMuchun Song hstate_kobjs, &hstate_attr_group); 4192a4a00b45SMuchun Song if (err) 4193a4a00b45SMuchun Song pr_err("HugeTLB: Unable to add hstate %s", h->name); 4194a4a00b45SMuchun Song } 4195a4a00b45SMuchun Song 4196a4a00b45SMuchun Song #ifdef CONFIG_NUMA 4197a4a00b45SMuchun Song hugetlb_sysfs_initialized = true; 4198a4a00b45SMuchun Song #endif 4199a4a00b45SMuchun Song hugetlb_register_all_nodes(); 4200a4a00b45SMuchun Song } 4201a4a00b45SMuchun Song 4202a3437870SNishanth Aravamudan static int __init hugetlb_init(void) 4203a3437870SNishanth Aravamudan { 42048382d914SDavidlohr Bueso int i; 42058382d914SDavidlohr Bueso 4206d6995da3SMike Kravetz BUILD_BUG_ON(sizeof_field(struct page, private) * BITS_PER_BYTE < 4207d6995da3SMike Kravetz __NR_HPAGEFLAGS); 4208d6995da3SMike Kravetz 4209c2833a5bSMike Kravetz if (!hugepages_supported()) { 4210c2833a5bSMike Kravetz if (hugetlb_max_hstate || default_hstate_max_huge_pages) 4211c2833a5bSMike Kravetz pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n"); 42120ef89d25SBenjamin Herrenschmidt return 0; 4213d715cf80SLiam R. Howlett } 4214d715cf80SLiam R. Howlett 4215282f4214SMike Kravetz /* 4216282f4214SMike Kravetz * Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists. Some 4217282f4214SMike Kravetz * architectures depend on setup being done here. 4218282f4214SMike Kravetz */ 4219a3437870SNishanth Aravamudan hugetlb_add_hstate(HUGETLB_PAGE_ORDER); 4220282f4214SMike Kravetz if (!parsed_default_hugepagesz) { 4221282f4214SMike Kravetz /* 4222282f4214SMike Kravetz * If we did not parse a default huge page size, set 4223282f4214SMike Kravetz * default_hstate_idx to HPAGE_SIZE hstate. And, if the 4224282f4214SMike Kravetz * number of huge pages for this default size was implicitly 4225282f4214SMike Kravetz * specified, set that here as well. 4226282f4214SMike Kravetz * Note that the implicit setting will overwrite an explicit 4227282f4214SMike Kravetz * setting. A warning will be printed in this case. 4228282f4214SMike Kravetz */ 4229282f4214SMike Kravetz default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE)); 4230f8b74815SVaishali Thakkar if (default_hstate_max_huge_pages) { 4231282f4214SMike Kravetz if (default_hstate.max_huge_pages) { 4232282f4214SMike Kravetz char buf[32]; 4233282f4214SMike Kravetz 4234282f4214SMike Kravetz string_get_size(huge_page_size(&default_hstate), 4235282f4214SMike Kravetz 1, STRING_UNITS_2, buf, 32); 4236282f4214SMike Kravetz pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n", 4237282f4214SMike Kravetz default_hstate.max_huge_pages, buf); 4238282f4214SMike Kravetz pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n", 4239282f4214SMike Kravetz default_hstate_max_huge_pages); 4240282f4214SMike Kravetz } 4241282f4214SMike Kravetz default_hstate.max_huge_pages = 4242282f4214SMike Kravetz default_hstate_max_huge_pages; 4243b5389086SZhenguo Yao 42440a7a0f6fSPeng Liu for_each_online_node(i) 4245b5389086SZhenguo Yao default_hstate.max_huge_pages_node[i] = 4246b5389086SZhenguo Yao default_hugepages_in_node[i]; 4247282f4214SMike Kravetz } 4248f8b74815SVaishali Thakkar } 4249a3437870SNishanth Aravamudan 4250cf11e85fSRoman Gushchin hugetlb_cma_check(); 4251a3437870SNishanth Aravamudan hugetlb_init_hstates(); 4252aa888a74SAndi Kleen gather_bootmem_prealloc(); 4253a3437870SNishanth Aravamudan report_hugepages(); 4254a3437870SNishanth Aravamudan 4255a3437870SNishanth Aravamudan hugetlb_sysfs_init(); 42567179e7bfSJianguo Wu hugetlb_cgroup_file_init(); 42579a305230SLee Schermerhorn 42588382d914SDavidlohr Bueso #ifdef CONFIG_SMP 42598382d914SDavidlohr Bueso num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); 42608382d914SDavidlohr Bueso #else 42618382d914SDavidlohr Bueso num_fault_mutexes = 1; 42628382d914SDavidlohr Bueso #endif 4263c672c7f2SMike Kravetz hugetlb_fault_mutex_table = 42646da2ec56SKees Cook kmalloc_array(num_fault_mutexes, sizeof(struct mutex), 42656da2ec56SKees Cook GFP_KERNEL); 4266c672c7f2SMike Kravetz BUG_ON(!hugetlb_fault_mutex_table); 42678382d914SDavidlohr Bueso 42688382d914SDavidlohr Bueso for (i = 0; i < num_fault_mutexes; i++) 4269c672c7f2SMike Kravetz mutex_init(&hugetlb_fault_mutex_table[i]); 4270a3437870SNishanth Aravamudan return 0; 4271a3437870SNishanth Aravamudan } 42723e89e1c5SPaul Gortmaker subsys_initcall(hugetlb_init); 4273a3437870SNishanth Aravamudan 4274ae94da89SMike Kravetz /* Overwritten by architectures with more huge page sizes */ 4275ae94da89SMike Kravetz bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size) 42769fee021dSVaishali Thakkar { 4277ae94da89SMike Kravetz return size == HPAGE_SIZE; 42789fee021dSVaishali Thakkar } 42799fee021dSVaishali Thakkar 4280d00181b9SKirill A. Shutemov void __init hugetlb_add_hstate(unsigned int order) 4281a3437870SNishanth Aravamudan { 4282a3437870SNishanth Aravamudan struct hstate *h; 42838faa8b07SAndi Kleen unsigned long i; 42848faa8b07SAndi Kleen 4285a3437870SNishanth Aravamudan if (size_to_hstate(PAGE_SIZE << order)) { 4286a3437870SNishanth Aravamudan return; 4287a3437870SNishanth Aravamudan } 428847d38344SAneesh Kumar K.V BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); 4289a3437870SNishanth Aravamudan BUG_ON(order == 0); 429047d38344SAneesh Kumar K.V h = &hstates[hugetlb_max_hstate++]; 429129383967SMike Kravetz mutex_init(&h->resize_lock); 4292a3437870SNishanth Aravamudan h->order = order; 4293aca78307SMiaohe Lin h->mask = ~(huge_page_size(h) - 1); 42948faa8b07SAndi Kleen for (i = 0; i < MAX_NUMNODES; ++i) 42958faa8b07SAndi Kleen INIT_LIST_HEAD(&h->hugepage_freelists[i]); 42960edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&h->hugepage_activelist); 429754f18d35SAndrew Morton h->next_nid_to_alloc = first_memory_node; 429854f18d35SAndrew Morton h->next_nid_to_free = first_memory_node; 4299a3437870SNishanth Aravamudan snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", 4300c2c3a60aSMiaohe Lin huge_page_size(h)/SZ_1K); 43018faa8b07SAndi Kleen 4302a3437870SNishanth Aravamudan parsed_hstate = h; 4303a3437870SNishanth Aravamudan } 4304a3437870SNishanth Aravamudan 4305b5389086SZhenguo Yao bool __init __weak hugetlb_node_alloc_supported(void) 4306b5389086SZhenguo Yao { 4307b5389086SZhenguo Yao return true; 4308b5389086SZhenguo Yao } 4309f87442f4SPeng Liu 4310f87442f4SPeng Liu static void __init hugepages_clear_pages_in_node(void) 4311f87442f4SPeng Liu { 4312f87442f4SPeng Liu if (!hugetlb_max_hstate) { 4313f87442f4SPeng Liu default_hstate_max_huge_pages = 0; 4314f87442f4SPeng Liu memset(default_hugepages_in_node, 0, 431510395680SMiaohe Lin sizeof(default_hugepages_in_node)); 4316f87442f4SPeng Liu } else { 4317f87442f4SPeng Liu parsed_hstate->max_huge_pages = 0; 4318f87442f4SPeng Liu memset(parsed_hstate->max_huge_pages_node, 0, 431910395680SMiaohe Lin sizeof(parsed_hstate->max_huge_pages_node)); 4320f87442f4SPeng Liu } 4321f87442f4SPeng Liu } 4322f87442f4SPeng Liu 4323282f4214SMike Kravetz /* 4324282f4214SMike Kravetz * hugepages command line processing 4325282f4214SMike Kravetz * hugepages normally follows a valid hugepagsz or default_hugepagsz 4326282f4214SMike Kravetz * specification. If not, ignore the hugepages value. hugepages can also 4327282f4214SMike Kravetz * be the first huge page command line option in which case it implicitly 4328282f4214SMike Kravetz * specifies the number of huge pages for the default size. 4329282f4214SMike Kravetz */ 4330282f4214SMike Kravetz static int __init hugepages_setup(char *s) 4331a3437870SNishanth Aravamudan { 4332a3437870SNishanth Aravamudan unsigned long *mhp; 43338faa8b07SAndi Kleen static unsigned long *last_mhp; 4334b5389086SZhenguo Yao int node = NUMA_NO_NODE; 4335b5389086SZhenguo Yao int count; 4336b5389086SZhenguo Yao unsigned long tmp; 4337b5389086SZhenguo Yao char *p = s; 4338a3437870SNishanth Aravamudan 43399fee021dSVaishali Thakkar if (!parsed_valid_hugepagesz) { 4340282f4214SMike Kravetz pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s); 43419fee021dSVaishali Thakkar parsed_valid_hugepagesz = true; 4342f81f6e4bSPeng Liu return 1; 43439fee021dSVaishali Thakkar } 4344282f4214SMike Kravetz 4345a3437870SNishanth Aravamudan /* 4346282f4214SMike Kravetz * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter 4347282f4214SMike Kravetz * yet, so this hugepages= parameter goes to the "default hstate". 4348282f4214SMike Kravetz * Otherwise, it goes with the previously parsed hugepagesz or 4349282f4214SMike Kravetz * default_hugepagesz. 4350a3437870SNishanth Aravamudan */ 43519fee021dSVaishali Thakkar else if (!hugetlb_max_hstate) 4352a3437870SNishanth Aravamudan mhp = &default_hstate_max_huge_pages; 4353a3437870SNishanth Aravamudan else 4354a3437870SNishanth Aravamudan mhp = &parsed_hstate->max_huge_pages; 4355a3437870SNishanth Aravamudan 43568faa8b07SAndi Kleen if (mhp == last_mhp) { 4357282f4214SMike Kravetz pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s); 4358f81f6e4bSPeng Liu return 1; 43598faa8b07SAndi Kleen } 43608faa8b07SAndi Kleen 4361b5389086SZhenguo Yao while (*p) { 4362b5389086SZhenguo Yao count = 0; 4363b5389086SZhenguo Yao if (sscanf(p, "%lu%n", &tmp, &count) != 1) 4364b5389086SZhenguo Yao goto invalid; 4365b5389086SZhenguo Yao /* Parameter is node format */ 4366b5389086SZhenguo Yao if (p[count] == ':') { 4367b5389086SZhenguo Yao if (!hugetlb_node_alloc_supported()) { 4368b5389086SZhenguo Yao pr_warn("HugeTLB: architecture can't support node specific alloc, ignoring!\n"); 4369f81f6e4bSPeng Liu return 1; 4370b5389086SZhenguo Yao } 43710a7a0f6fSPeng Liu if (tmp >= MAX_NUMNODES || !node_online(tmp)) 4372e79ce983SLiu Yuntao goto invalid; 43730a7a0f6fSPeng Liu node = array_index_nospec(tmp, MAX_NUMNODES); 4374b5389086SZhenguo Yao p += count + 1; 4375b5389086SZhenguo Yao /* Parse hugepages */ 4376b5389086SZhenguo Yao if (sscanf(p, "%lu%n", &tmp, &count) != 1) 4377b5389086SZhenguo Yao goto invalid; 4378b5389086SZhenguo Yao if (!hugetlb_max_hstate) 4379b5389086SZhenguo Yao default_hugepages_in_node[node] = tmp; 4380b5389086SZhenguo Yao else 4381b5389086SZhenguo Yao parsed_hstate->max_huge_pages_node[node] = tmp; 4382b5389086SZhenguo Yao *mhp += tmp; 4383b5389086SZhenguo Yao /* Go to parse next node*/ 4384b5389086SZhenguo Yao if (p[count] == ',') 4385b5389086SZhenguo Yao p += count + 1; 4386b5389086SZhenguo Yao else 4387b5389086SZhenguo Yao break; 4388b5389086SZhenguo Yao } else { 4389b5389086SZhenguo Yao if (p != s) 4390b5389086SZhenguo Yao goto invalid; 4391b5389086SZhenguo Yao *mhp = tmp; 4392b5389086SZhenguo Yao break; 4393b5389086SZhenguo Yao } 4394b5389086SZhenguo Yao } 4395a3437870SNishanth Aravamudan 43968faa8b07SAndi Kleen /* 43978faa8b07SAndi Kleen * Global state is always initialized later in hugetlb_init. 439804adbc3fSMiaohe Lin * But we need to allocate gigantic hstates here early to still 43998faa8b07SAndi Kleen * use the bootmem allocator. 44008faa8b07SAndi Kleen */ 440104adbc3fSMiaohe Lin if (hugetlb_max_hstate && hstate_is_gigantic(parsed_hstate)) 44028faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(parsed_hstate); 44038faa8b07SAndi Kleen 44048faa8b07SAndi Kleen last_mhp = mhp; 44058faa8b07SAndi Kleen 4406a3437870SNishanth Aravamudan return 1; 4407b5389086SZhenguo Yao 4408b5389086SZhenguo Yao invalid: 4409b5389086SZhenguo Yao pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p); 4410f87442f4SPeng Liu hugepages_clear_pages_in_node(); 4411f81f6e4bSPeng Liu return 1; 4412a3437870SNishanth Aravamudan } 4413282f4214SMike Kravetz __setup("hugepages=", hugepages_setup); 4414e11bfbfcSNick Piggin 4415282f4214SMike Kravetz /* 4416282f4214SMike Kravetz * hugepagesz command line processing 4417282f4214SMike Kravetz * A specific huge page size can only be specified once with hugepagesz. 4418282f4214SMike Kravetz * hugepagesz is followed by hugepages on the command line. The global 4419282f4214SMike Kravetz * variable 'parsed_valid_hugepagesz' is used to determine if prior 4420282f4214SMike Kravetz * hugepagesz argument was valid. 4421282f4214SMike Kravetz */ 4422359f2544SMike Kravetz static int __init hugepagesz_setup(char *s) 4423e11bfbfcSNick Piggin { 4424359f2544SMike Kravetz unsigned long size; 4425282f4214SMike Kravetz struct hstate *h; 4426282f4214SMike Kravetz 4427282f4214SMike Kravetz parsed_valid_hugepagesz = false; 4428359f2544SMike Kravetz size = (unsigned long)memparse(s, NULL); 4429359f2544SMike Kravetz 4430359f2544SMike Kravetz if (!arch_hugetlb_valid_size(size)) { 4431282f4214SMike Kravetz pr_err("HugeTLB: unsupported hugepagesz=%s\n", s); 4432f81f6e4bSPeng Liu return 1; 4433359f2544SMike Kravetz } 4434359f2544SMike Kravetz 4435282f4214SMike Kravetz h = size_to_hstate(size); 4436282f4214SMike Kravetz if (h) { 4437282f4214SMike Kravetz /* 4438282f4214SMike Kravetz * hstate for this size already exists. This is normally 4439282f4214SMike Kravetz * an error, but is allowed if the existing hstate is the 4440282f4214SMike Kravetz * default hstate. More specifically, it is only allowed if 4441282f4214SMike Kravetz * the number of huge pages for the default hstate was not 4442282f4214SMike Kravetz * previously specified. 4443282f4214SMike Kravetz */ 4444282f4214SMike Kravetz if (!parsed_default_hugepagesz || h != &default_hstate || 4445282f4214SMike Kravetz default_hstate.max_huge_pages) { 4446282f4214SMike Kravetz pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s); 4447f81f6e4bSPeng Liu return 1; 444838237830SMike Kravetz } 444938237830SMike Kravetz 4450282f4214SMike Kravetz /* 4451282f4214SMike Kravetz * No need to call hugetlb_add_hstate() as hstate already 4452282f4214SMike Kravetz * exists. But, do set parsed_hstate so that a following 4453282f4214SMike Kravetz * hugepages= parameter will be applied to this hstate. 4454282f4214SMike Kravetz */ 4455282f4214SMike Kravetz parsed_hstate = h; 4456282f4214SMike Kravetz parsed_valid_hugepagesz = true; 4457e11bfbfcSNick Piggin return 1; 4458e11bfbfcSNick Piggin } 4459282f4214SMike Kravetz 4460359f2544SMike Kravetz hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); 4461282f4214SMike Kravetz parsed_valid_hugepagesz = true; 4462359f2544SMike Kravetz return 1; 4463359f2544SMike Kravetz } 4464359f2544SMike Kravetz __setup("hugepagesz=", hugepagesz_setup); 4465359f2544SMike Kravetz 4466282f4214SMike Kravetz /* 4467282f4214SMike Kravetz * default_hugepagesz command line input 4468282f4214SMike Kravetz * Only one instance of default_hugepagesz allowed on command line. 4469282f4214SMike Kravetz */ 4470ae94da89SMike Kravetz static int __init default_hugepagesz_setup(char *s) 4471e11bfbfcSNick Piggin { 4472ae94da89SMike Kravetz unsigned long size; 4473b5389086SZhenguo Yao int i; 4474ae94da89SMike Kravetz 4475282f4214SMike Kravetz parsed_valid_hugepagesz = false; 4476282f4214SMike Kravetz if (parsed_default_hugepagesz) { 4477282f4214SMike Kravetz pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s); 4478f81f6e4bSPeng Liu return 1; 4479282f4214SMike Kravetz } 4480282f4214SMike Kravetz 4481282f4214SMike Kravetz size = (unsigned long)memparse(s, NULL); 4482282f4214SMike Kravetz 4483282f4214SMike Kravetz if (!arch_hugetlb_valid_size(size)) { 4484282f4214SMike Kravetz pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s); 4485f81f6e4bSPeng Liu return 1; 4486282f4214SMike Kravetz } 4487282f4214SMike Kravetz 4488282f4214SMike Kravetz hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); 4489282f4214SMike Kravetz parsed_valid_hugepagesz = true; 4490282f4214SMike Kravetz parsed_default_hugepagesz = true; 4491282f4214SMike Kravetz default_hstate_idx = hstate_index(size_to_hstate(size)); 4492282f4214SMike Kravetz 4493282f4214SMike Kravetz /* 4494282f4214SMike Kravetz * The number of default huge pages (for this size) could have been 4495282f4214SMike Kravetz * specified as the first hugetlb parameter: hugepages=X. If so, 4496282f4214SMike Kravetz * then default_hstate_max_huge_pages is set. If the default huge 4497282f4214SMike Kravetz * page size is gigantic (>= MAX_ORDER), then the pages must be 4498282f4214SMike Kravetz * allocated here from bootmem allocator. 4499282f4214SMike Kravetz */ 4500282f4214SMike Kravetz if (default_hstate_max_huge_pages) { 4501282f4214SMike Kravetz default_hstate.max_huge_pages = default_hstate_max_huge_pages; 45020a7a0f6fSPeng Liu for_each_online_node(i) 4503b5389086SZhenguo Yao default_hstate.max_huge_pages_node[i] = 4504b5389086SZhenguo Yao default_hugepages_in_node[i]; 4505282f4214SMike Kravetz if (hstate_is_gigantic(&default_hstate)) 4506282f4214SMike Kravetz hugetlb_hstate_alloc_pages(&default_hstate); 4507282f4214SMike Kravetz default_hstate_max_huge_pages = 0; 4508282f4214SMike Kravetz } 4509282f4214SMike Kravetz 4510e11bfbfcSNick Piggin return 1; 4511e11bfbfcSNick Piggin } 4512ae94da89SMike Kravetz __setup("default_hugepagesz=", default_hugepagesz_setup); 4513a3437870SNishanth Aravamudan 4514d2226ebdSFeng Tang static nodemask_t *policy_mbind_nodemask(gfp_t gfp) 4515d2226ebdSFeng Tang { 4516d2226ebdSFeng Tang #ifdef CONFIG_NUMA 4517d2226ebdSFeng Tang struct mempolicy *mpol = get_task_policy(current); 4518d2226ebdSFeng Tang 4519d2226ebdSFeng Tang /* 4520d2226ebdSFeng Tang * Only enforce MPOL_BIND policy which overlaps with cpuset policy 4521d2226ebdSFeng Tang * (from policy_nodemask) specifically for hugetlb case 4522d2226ebdSFeng Tang */ 4523d2226ebdSFeng Tang if (mpol->mode == MPOL_BIND && 4524d2226ebdSFeng Tang (apply_policy_zone(mpol, gfp_zone(gfp)) && 4525d2226ebdSFeng Tang cpuset_nodemask_valid_mems_allowed(&mpol->nodes))) 4526d2226ebdSFeng Tang return &mpol->nodes; 4527d2226ebdSFeng Tang #endif 4528d2226ebdSFeng Tang return NULL; 4529d2226ebdSFeng Tang } 4530d2226ebdSFeng Tang 45318ca39e68SMuchun Song static unsigned int allowed_mems_nr(struct hstate *h) 45328a213460SNishanth Aravamudan { 45338a213460SNishanth Aravamudan int node; 45348a213460SNishanth Aravamudan unsigned int nr = 0; 4535d2226ebdSFeng Tang nodemask_t *mbind_nodemask; 45368ca39e68SMuchun Song unsigned int *array = h->free_huge_pages_node; 45378ca39e68SMuchun Song gfp_t gfp_mask = htlb_alloc_mask(h); 45388a213460SNishanth Aravamudan 4539d2226ebdSFeng Tang mbind_nodemask = policy_mbind_nodemask(gfp_mask); 45408ca39e68SMuchun Song for_each_node_mask(node, cpuset_current_mems_allowed) { 4541d2226ebdSFeng Tang if (!mbind_nodemask || node_isset(node, *mbind_nodemask)) 45428a213460SNishanth Aravamudan nr += array[node]; 45438ca39e68SMuchun Song } 45448a213460SNishanth Aravamudan 45458a213460SNishanth Aravamudan return nr; 45468a213460SNishanth Aravamudan } 45478a213460SNishanth Aravamudan 45488a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL 454917743798SMuchun Song static int proc_hugetlb_doulongvec_minmax(struct ctl_table *table, int write, 455017743798SMuchun Song void *buffer, size_t *length, 455117743798SMuchun Song loff_t *ppos, unsigned long *out) 455217743798SMuchun Song { 455317743798SMuchun Song struct ctl_table dup_table; 455417743798SMuchun Song 455517743798SMuchun Song /* 455617743798SMuchun Song * In order to avoid races with __do_proc_doulongvec_minmax(), we 455717743798SMuchun Song * can duplicate the @table and alter the duplicate of it. 455817743798SMuchun Song */ 455917743798SMuchun Song dup_table = *table; 456017743798SMuchun Song dup_table.data = out; 456117743798SMuchun Song 456217743798SMuchun Song return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos); 456317743798SMuchun Song } 456417743798SMuchun Song 456506808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy, 456606808b08SLee Schermerhorn struct ctl_table *table, int write, 456732927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 45681da177e4SLinus Torvalds { 4569e5ff2159SAndi Kleen struct hstate *h = &default_hstate; 4570238d3c13SDavid Rientjes unsigned long tmp = h->max_huge_pages; 457108d4a246SMichal Hocko int ret; 4572e5ff2159SAndi Kleen 4573457c1b27SNishanth Aravamudan if (!hugepages_supported()) 457486613628SJan Stancek return -EOPNOTSUPP; 4575457c1b27SNishanth Aravamudan 457617743798SMuchun Song ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, 457717743798SMuchun Song &tmp); 457808d4a246SMichal Hocko if (ret) 457908d4a246SMichal Hocko goto out; 4580e5ff2159SAndi Kleen 4581238d3c13SDavid Rientjes if (write) 4582238d3c13SDavid Rientjes ret = __nr_hugepages_store_common(obey_mempolicy, h, 4583238d3c13SDavid Rientjes NUMA_NO_NODE, tmp, *length); 458408d4a246SMichal Hocko out: 458508d4a246SMichal Hocko return ret; 45861da177e4SLinus Torvalds } 4587396faf03SMel Gorman 458806808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write, 458932927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 459006808b08SLee Schermerhorn { 459106808b08SLee Schermerhorn 459206808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(false, table, write, 459306808b08SLee Schermerhorn buffer, length, ppos); 459406808b08SLee Schermerhorn } 459506808b08SLee Schermerhorn 459606808b08SLee Schermerhorn #ifdef CONFIG_NUMA 459706808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write, 459832927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 459906808b08SLee Schermerhorn { 460006808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(true, table, write, 460106808b08SLee Schermerhorn buffer, length, ppos); 460206808b08SLee Schermerhorn } 460306808b08SLee Schermerhorn #endif /* CONFIG_NUMA */ 460406808b08SLee Schermerhorn 4605a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write, 460632927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 4607a3d0c6aaSNishanth Aravamudan { 4608a5516438SAndi Kleen struct hstate *h = &default_hstate; 4609e5ff2159SAndi Kleen unsigned long tmp; 461008d4a246SMichal Hocko int ret; 4611e5ff2159SAndi Kleen 4612457c1b27SNishanth Aravamudan if (!hugepages_supported()) 461386613628SJan Stancek return -EOPNOTSUPP; 4614457c1b27SNishanth Aravamudan 4615e5ff2159SAndi Kleen tmp = h->nr_overcommit_huge_pages; 4616e5ff2159SAndi Kleen 4617bae7f4aeSLuiz Capitulino if (write && hstate_is_gigantic(h)) 4618adbe8726SEric B Munson return -EINVAL; 4619adbe8726SEric B Munson 462017743798SMuchun Song ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, 462117743798SMuchun Song &tmp); 462208d4a246SMichal Hocko if (ret) 462308d4a246SMichal Hocko goto out; 4624e5ff2159SAndi Kleen 4625e5ff2159SAndi Kleen if (write) { 4626db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 4627e5ff2159SAndi Kleen h->nr_overcommit_huge_pages = tmp; 4628db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 4629e5ff2159SAndi Kleen } 463008d4a246SMichal Hocko out: 463108d4a246SMichal Hocko return ret; 4632a3d0c6aaSNishanth Aravamudan } 4633a3d0c6aaSNishanth Aravamudan 46341da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 46351da177e4SLinus Torvalds 4636e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m) 46371da177e4SLinus Torvalds { 4638fcb2b0c5SRoman Gushchin struct hstate *h; 4639fcb2b0c5SRoman Gushchin unsigned long total = 0; 4640fcb2b0c5SRoman Gushchin 4641457c1b27SNishanth Aravamudan if (!hugepages_supported()) 4642457c1b27SNishanth Aravamudan return; 4643fcb2b0c5SRoman Gushchin 4644fcb2b0c5SRoman Gushchin for_each_hstate(h) { 4645fcb2b0c5SRoman Gushchin unsigned long count = h->nr_huge_pages; 4646fcb2b0c5SRoman Gushchin 4647aca78307SMiaohe Lin total += huge_page_size(h) * count; 4648fcb2b0c5SRoman Gushchin 4649fcb2b0c5SRoman Gushchin if (h == &default_hstate) 4650e1759c21SAlexey Dobriyan seq_printf(m, 46511da177e4SLinus Torvalds "HugePages_Total: %5lu\n" 46521da177e4SLinus Torvalds "HugePages_Free: %5lu\n" 4653b45b5bd6SDavid Gibson "HugePages_Rsvd: %5lu\n" 46547893d1d5SAdam Litke "HugePages_Surp: %5lu\n" 46554f98a2feSRik van Riel "Hugepagesize: %8lu kB\n", 4656fcb2b0c5SRoman Gushchin count, 4657a5516438SAndi Kleen h->free_huge_pages, 4658a5516438SAndi Kleen h->resv_huge_pages, 4659a5516438SAndi Kleen h->surplus_huge_pages, 4660aca78307SMiaohe Lin huge_page_size(h) / SZ_1K); 4661fcb2b0c5SRoman Gushchin } 4662fcb2b0c5SRoman Gushchin 4663aca78307SMiaohe Lin seq_printf(m, "Hugetlb: %8lu kB\n", total / SZ_1K); 46641da177e4SLinus Torvalds } 46651da177e4SLinus Torvalds 46667981593bSJoe Perches int hugetlb_report_node_meminfo(char *buf, int len, int nid) 46671da177e4SLinus Torvalds { 4668a5516438SAndi Kleen struct hstate *h = &default_hstate; 46697981593bSJoe Perches 4670457c1b27SNishanth Aravamudan if (!hugepages_supported()) 4671457c1b27SNishanth Aravamudan return 0; 46727981593bSJoe Perches 46737981593bSJoe Perches return sysfs_emit_at(buf, len, 46741da177e4SLinus Torvalds "Node %d HugePages_Total: %5u\n" 4675a1de0919SNishanth Aravamudan "Node %d HugePages_Free: %5u\n" 4676a1de0919SNishanth Aravamudan "Node %d HugePages_Surp: %5u\n", 4677a5516438SAndi Kleen nid, h->nr_huge_pages_node[nid], 4678a5516438SAndi Kleen nid, h->free_huge_pages_node[nid], 4679a5516438SAndi Kleen nid, h->surplus_huge_pages_node[nid]); 46801da177e4SLinus Torvalds } 46811da177e4SLinus Torvalds 4682dcadcf1cSGang Li void hugetlb_show_meminfo_node(int nid) 4683949f7ec5SDavid Rientjes { 4684949f7ec5SDavid Rientjes struct hstate *h; 4685949f7ec5SDavid Rientjes 4686457c1b27SNishanth Aravamudan if (!hugepages_supported()) 4687457c1b27SNishanth Aravamudan return; 4688457c1b27SNishanth Aravamudan 4689949f7ec5SDavid Rientjes for_each_hstate(h) 4690dcadcf1cSGang Li printk("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", 4691949f7ec5SDavid Rientjes nid, 4692949f7ec5SDavid Rientjes h->nr_huge_pages_node[nid], 4693949f7ec5SDavid Rientjes h->free_huge_pages_node[nid], 4694949f7ec5SDavid Rientjes h->surplus_huge_pages_node[nid], 4695aca78307SMiaohe Lin huge_page_size(h) / SZ_1K); 4696949f7ec5SDavid Rientjes } 4697949f7ec5SDavid Rientjes 46985d317b2bSNaoya Horiguchi void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm) 46995d317b2bSNaoya Horiguchi { 47005d317b2bSNaoya Horiguchi seq_printf(m, "HugetlbPages:\t%8lu kB\n", 47015d317b2bSNaoya Horiguchi atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10)); 47025d317b2bSNaoya Horiguchi } 47035d317b2bSNaoya Horiguchi 47041da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 47051da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void) 47061da177e4SLinus Torvalds { 4707d0028588SWanpeng Li struct hstate *h; 4708d0028588SWanpeng Li unsigned long nr_total_pages = 0; 4709d0028588SWanpeng Li 4710d0028588SWanpeng Li for_each_hstate(h) 4711d0028588SWanpeng Li nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); 4712d0028588SWanpeng Li return nr_total_pages; 47131da177e4SLinus Torvalds } 47141da177e4SLinus Torvalds 4715a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta) 4716fc1b8a73SMel Gorman { 4717fc1b8a73SMel Gorman int ret = -ENOMEM; 4718fc1b8a73SMel Gorman 47190aa7f354SMiaohe Lin if (!delta) 47200aa7f354SMiaohe Lin return 0; 47210aa7f354SMiaohe Lin 4722db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 4723fc1b8a73SMel Gorman /* 4724fc1b8a73SMel Gorman * When cpuset is configured, it breaks the strict hugetlb page 4725fc1b8a73SMel Gorman * reservation as the accounting is done on a global variable. Such 4726fc1b8a73SMel Gorman * reservation is completely rubbish in the presence of cpuset because 4727fc1b8a73SMel Gorman * the reservation is not checked against page availability for the 4728fc1b8a73SMel Gorman * current cpuset. Application can still potentially OOM'ed by kernel 4729fc1b8a73SMel Gorman * with lack of free htlb page in cpuset that the task is in. 4730fc1b8a73SMel Gorman * Attempt to enforce strict accounting with cpuset is almost 4731fc1b8a73SMel Gorman * impossible (or too ugly) because cpuset is too fluid that 4732fc1b8a73SMel Gorman * task or memory node can be dynamically moved between cpusets. 4733fc1b8a73SMel Gorman * 4734fc1b8a73SMel Gorman * The change of semantics for shared hugetlb mapping with cpuset is 4735fc1b8a73SMel Gorman * undesirable. However, in order to preserve some of the semantics, 4736fc1b8a73SMel Gorman * we fall back to check against current free page availability as 4737fc1b8a73SMel Gorman * a best attempt and hopefully to minimize the impact of changing 4738fc1b8a73SMel Gorman * semantics that cpuset has. 47398ca39e68SMuchun Song * 47408ca39e68SMuchun Song * Apart from cpuset, we also have memory policy mechanism that 47418ca39e68SMuchun Song * also determines from which node the kernel will allocate memory 47428ca39e68SMuchun Song * in a NUMA system. So similar to cpuset, we also should consider 47438ca39e68SMuchun Song * the memory policy of the current task. Similar to the description 47448ca39e68SMuchun Song * above. 4745fc1b8a73SMel Gorman */ 4746fc1b8a73SMel Gorman if (delta > 0) { 4747a5516438SAndi Kleen if (gather_surplus_pages(h, delta) < 0) 4748fc1b8a73SMel Gorman goto out; 4749fc1b8a73SMel Gorman 47508ca39e68SMuchun Song if (delta > allowed_mems_nr(h)) { 4751a5516438SAndi Kleen return_unused_surplus_pages(h, delta); 4752fc1b8a73SMel Gorman goto out; 4753fc1b8a73SMel Gorman } 4754fc1b8a73SMel Gorman } 4755fc1b8a73SMel Gorman 4756fc1b8a73SMel Gorman ret = 0; 4757fc1b8a73SMel Gorman if (delta < 0) 4758a5516438SAndi Kleen return_unused_surplus_pages(h, (unsigned long) -delta); 4759fc1b8a73SMel Gorman 4760fc1b8a73SMel Gorman out: 4761db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 4762fc1b8a73SMel Gorman return ret; 4763fc1b8a73SMel Gorman } 4764fc1b8a73SMel Gorman 476584afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma) 476684afd99bSAndy Whitcroft { 4767f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 476884afd99bSAndy Whitcroft 476984afd99bSAndy Whitcroft /* 4770612b8a31SMike Kravetz * HPAGE_RESV_OWNER indicates a private mapping. 477184afd99bSAndy Whitcroft * This new VMA should share its siblings reservation map if present. 477284afd99bSAndy Whitcroft * The VMA will only ever have a valid reservation map pointer where 477384afd99bSAndy Whitcroft * it is being copied for another still existing VMA. As that VMA 477425985edcSLucas De Marchi * has a reference to the reservation map it cannot disappear until 477584afd99bSAndy Whitcroft * after this open call completes. It is therefore safe to take a 477684afd99bSAndy Whitcroft * new reference here without additional locking. 477784afd99bSAndy Whitcroft */ 477809a26e83SMike Kravetz if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { 477909a26e83SMike Kravetz resv_map_dup_hugetlb_cgroup_uncharge_info(resv); 4780f522c3acSJoonsoo Kim kref_get(&resv->refs); 478184afd99bSAndy Whitcroft } 47828d9bfb26SMike Kravetz 4783131a79b4SMike Kravetz /* 4784131a79b4SMike Kravetz * vma_lock structure for sharable mappings is vma specific. 4785612b8a31SMike Kravetz * Clear old pointer (if copied via vm_area_dup) and allocate 4786612b8a31SMike Kravetz * new structure. Before clearing, make sure vma_lock is not 4787612b8a31SMike Kravetz * for this vma. 4788131a79b4SMike Kravetz */ 4789131a79b4SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 4790612b8a31SMike Kravetz struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 4791612b8a31SMike Kravetz 4792612b8a31SMike Kravetz if (vma_lock) { 4793612b8a31SMike Kravetz if (vma_lock->vma != vma) { 4794131a79b4SMike Kravetz vma->vm_private_data = NULL; 47958d9bfb26SMike Kravetz hugetlb_vma_lock_alloc(vma); 4796612b8a31SMike Kravetz } else 4797612b8a31SMike Kravetz pr_warn("HugeTLB: vma_lock already exists in %s.\n", __func__); 4798612b8a31SMike Kravetz } else 4799612b8a31SMike Kravetz hugetlb_vma_lock_alloc(vma); 480009a26e83SMike Kravetz } 4801131a79b4SMike Kravetz } 480284afd99bSAndy Whitcroft 4803a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma) 4804a1e78772SMel Gorman { 4805a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 48068d9bfb26SMike Kravetz struct resv_map *resv; 480790481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 48084e35f483SJoonsoo Kim unsigned long reserve, start, end; 48091c5ecae3SMike Kravetz long gbl_reserve; 481084afd99bSAndy Whitcroft 48118d9bfb26SMike Kravetz hugetlb_vma_lock_free(vma); 48128d9bfb26SMike Kravetz 48138d9bfb26SMike Kravetz resv = vma_resv_map(vma); 48144e35f483SJoonsoo Kim if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 48154e35f483SJoonsoo Kim return; 48164e35f483SJoonsoo Kim 4817a5516438SAndi Kleen start = vma_hugecache_offset(h, vma, vma->vm_start); 4818a5516438SAndi Kleen end = vma_hugecache_offset(h, vma, vma->vm_end); 481984afd99bSAndy Whitcroft 48204e35f483SJoonsoo Kim reserve = (end - start) - region_count(resv, start, end); 4821e9fe92aeSMina Almasry hugetlb_cgroup_uncharge_counter(resv, start, end); 48227251ff78SAdam Litke if (reserve) { 48231c5ecae3SMike Kravetz /* 48241c5ecae3SMike Kravetz * Decrement reserve counts. The global reserve count may be 48251c5ecae3SMike Kravetz * adjusted if the subpool has a minimum size. 48261c5ecae3SMike Kravetz */ 48271c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, reserve); 48281c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 48297251ff78SAdam Litke } 4830e9fe92aeSMina Almasry 4831e9fe92aeSMina Almasry kref_put(&resv->refs, resv_map_release); 4832a1e78772SMel Gorman } 4833a1e78772SMel Gorman 483431383c68SDan Williams static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr) 483531383c68SDan Williams { 483631383c68SDan Williams if (addr & ~(huge_page_mask(hstate_vma(vma)))) 483731383c68SDan Williams return -EINVAL; 4838b30c14cdSJames Houghton 4839b30c14cdSJames Houghton /* 4840b30c14cdSJames Houghton * PMD sharing is only possible for PUD_SIZE-aligned address ranges 4841b30c14cdSJames Houghton * in HugeTLB VMAs. If we will lose PUD_SIZE alignment due to this 4842b30c14cdSJames Houghton * split, unshare PMDs in the PUD_SIZE interval surrounding addr now. 4843b30c14cdSJames Houghton */ 4844b30c14cdSJames Houghton if (addr & ~PUD_MASK) { 4845b30c14cdSJames Houghton /* 4846b30c14cdSJames Houghton * hugetlb_vm_op_split is called right before we attempt to 4847b30c14cdSJames Houghton * split the VMA. We will need to unshare PMDs in the old and 4848b30c14cdSJames Houghton * new VMAs, so let's unshare before we split. 4849b30c14cdSJames Houghton */ 4850b30c14cdSJames Houghton unsigned long floor = addr & PUD_MASK; 4851b30c14cdSJames Houghton unsigned long ceil = floor + PUD_SIZE; 4852b30c14cdSJames Houghton 4853b30c14cdSJames Houghton if (floor >= vma->vm_start && ceil <= vma->vm_end) 4854b30c14cdSJames Houghton hugetlb_unshare_pmds(vma, floor, ceil); 4855b30c14cdSJames Houghton } 4856b30c14cdSJames Houghton 485731383c68SDan Williams return 0; 485831383c68SDan Williams } 485931383c68SDan Williams 486005ea8860SDan Williams static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma) 486105ea8860SDan Williams { 4862aca78307SMiaohe Lin return huge_page_size(hstate_vma(vma)); 486305ea8860SDan Williams } 486405ea8860SDan Williams 48651da177e4SLinus Torvalds /* 48661da177e4SLinus Torvalds * We cannot handle pagefaults against hugetlb pages at all. They cause 48671da177e4SLinus Torvalds * handle_mm_fault() to try to instantiate regular-sized pages in the 48686c26d310SMiaohe Lin * hugepage VMA. do_page_fault() is supposed to trap this, so BUG is we get 48691da177e4SLinus Torvalds * this far. 48701da177e4SLinus Torvalds */ 4871b3ec9f33SSouptick Joarder static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf) 48721da177e4SLinus Torvalds { 48731da177e4SLinus Torvalds BUG(); 4874d0217ac0SNick Piggin return 0; 48751da177e4SLinus Torvalds } 48761da177e4SLinus Torvalds 4877eec3636aSJane Chu /* 4878eec3636aSJane Chu * When a new function is introduced to vm_operations_struct and added 4879eec3636aSJane Chu * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops. 4880eec3636aSJane Chu * This is because under System V memory model, mappings created via 4881eec3636aSJane Chu * shmget/shmat with "huge page" specified are backed by hugetlbfs files, 4882eec3636aSJane Chu * their original vm_ops are overwritten with shm_vm_ops. 4883eec3636aSJane Chu */ 4884f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = { 4885d0217ac0SNick Piggin .fault = hugetlb_vm_op_fault, 488684afd99bSAndy Whitcroft .open = hugetlb_vm_op_open, 4887a1e78772SMel Gorman .close = hugetlb_vm_op_close, 4888dd3b614fSDmitry Safonov .may_split = hugetlb_vm_op_split, 488905ea8860SDan Williams .pagesize = hugetlb_vm_op_pagesize, 48901da177e4SLinus Torvalds }; 48911da177e4SLinus Torvalds 48921e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, 48931e8f889bSDavid Gibson int writable) 489463551ae0SDavid Gibson { 489563551ae0SDavid Gibson pte_t entry; 489679c1c594SChristophe Leroy unsigned int shift = huge_page_shift(hstate_vma(vma)); 489763551ae0SDavid Gibson 48981e8f889bSDavid Gibson if (writable) { 4899106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, 4900106c992aSGerald Schaefer vma->vm_page_prot))); 490163551ae0SDavid Gibson } else { 4902106c992aSGerald Schaefer entry = huge_pte_wrprotect(mk_huge_pte(page, 4903106c992aSGerald Schaefer vma->vm_page_prot)); 490463551ae0SDavid Gibson } 490563551ae0SDavid Gibson entry = pte_mkyoung(entry); 490679c1c594SChristophe Leroy entry = arch_make_huge_pte(entry, shift, vma->vm_flags); 490763551ae0SDavid Gibson 490863551ae0SDavid Gibson return entry; 490963551ae0SDavid Gibson } 491063551ae0SDavid Gibson 49111e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma, 49121e8f889bSDavid Gibson unsigned long address, pte_t *ptep) 49131e8f889bSDavid Gibson { 49141e8f889bSDavid Gibson pte_t entry; 49151e8f889bSDavid Gibson 4916106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); 491732f84528SChris Forbes if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) 49184b3073e1SRussell King update_mmu_cache(vma, address, ptep); 49191e8f889bSDavid Gibson } 49201e8f889bSDavid Gibson 4921d5ed7444SAneesh Kumar K.V bool is_hugetlb_entry_migration(pte_t pte) 49224a705fefSNaoya Horiguchi { 49234a705fefSNaoya Horiguchi swp_entry_t swp; 49244a705fefSNaoya Horiguchi 49254a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 4926d5ed7444SAneesh Kumar K.V return false; 49274a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 4928d79d176aSBaoquan He if (is_migration_entry(swp)) 4929d5ed7444SAneesh Kumar K.V return true; 49304a705fefSNaoya Horiguchi else 4931d5ed7444SAneesh Kumar K.V return false; 49324a705fefSNaoya Horiguchi } 49334a705fefSNaoya Horiguchi 49343e5c3600SBaoquan He static bool is_hugetlb_entry_hwpoisoned(pte_t pte) 49354a705fefSNaoya Horiguchi { 49364a705fefSNaoya Horiguchi swp_entry_t swp; 49374a705fefSNaoya Horiguchi 49384a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 49393e5c3600SBaoquan He return false; 49404a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 4941d79d176aSBaoquan He if (is_hwpoison_entry(swp)) 49423e5c3600SBaoquan He return true; 49434a705fefSNaoya Horiguchi else 49443e5c3600SBaoquan He return false; 49454a705fefSNaoya Horiguchi } 49461e8f889bSDavid Gibson 49474eae4efaSPeter Xu static void 49484eae4efaSPeter Xu hugetlb_install_page(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr, 49494eae4efaSPeter Xu struct page *new_page) 49504eae4efaSPeter Xu { 49514eae4efaSPeter Xu __SetPageUptodate(new_page); 49524eae4efaSPeter Xu hugepage_add_new_anon_rmap(new_page, vma, addr); 49531eba86c0SPasha Tatashin set_huge_pte_at(vma->vm_mm, addr, ptep, make_huge_pte(vma, new_page, 1)); 49544eae4efaSPeter Xu hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm); 49554eae4efaSPeter Xu SetHPageMigratable(new_page); 49564eae4efaSPeter Xu } 49574eae4efaSPeter Xu 495863551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 4959bc70fbf2SPeter Xu struct vm_area_struct *dst_vma, 4960bc70fbf2SPeter Xu struct vm_area_struct *src_vma) 496163551ae0SDavid Gibson { 49623aa4ed80SMiaohe Lin pte_t *src_pte, *dst_pte, entry; 496363551ae0SDavid Gibson struct page *ptepage; 49641c59827dSHugh Dickins unsigned long addr; 4965bc70fbf2SPeter Xu bool cow = is_cow_mapping(src_vma->vm_flags); 4966bc70fbf2SPeter Xu struct hstate *h = hstate_vma(src_vma); 4967a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 49684eae4efaSPeter Xu unsigned long npages = pages_per_huge_page(h); 4969ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 4970e95a9851SMike Kravetz unsigned long last_addr_mask; 4971e8569dd2SAndreas Sandberg int ret = 0; 49721e8f889bSDavid Gibson 4973ac46d4f3SJérôme Glisse if (cow) { 4974bc70fbf2SPeter Xu mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, src_vma, src, 4975bc70fbf2SPeter Xu src_vma->vm_start, 4976bc70fbf2SPeter Xu src_vma->vm_end); 4977ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 4978623a1ddfSDavid Hildenbrand mmap_assert_write_locked(src); 4979623a1ddfSDavid Hildenbrand raw_write_seqcount_begin(&src->write_protect_seq); 498040549ba8SMike Kravetz } else { 498140549ba8SMike Kravetz /* 498240549ba8SMike Kravetz * For shared mappings the vma lock must be held before 498340549ba8SMike Kravetz * calling huge_pte_offset in the src vma. Otherwise, the 498440549ba8SMike Kravetz * returned ptep could go away if part of a shared pmd and 498540549ba8SMike Kravetz * another thread calls huge_pmd_unshare. 498640549ba8SMike Kravetz */ 498740549ba8SMike Kravetz hugetlb_vma_lock_read(src_vma); 4988ac46d4f3SJérôme Glisse } 4989e8569dd2SAndreas Sandberg 4990e95a9851SMike Kravetz last_addr_mask = hugetlb_mask_last_page(h); 4991bc70fbf2SPeter Xu for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) { 4992cb900f41SKirill A. Shutemov spinlock_t *src_ptl, *dst_ptl; 49937868a208SPunit Agrawal src_pte = huge_pte_offset(src, addr, sz); 4994e95a9851SMike Kravetz if (!src_pte) { 4995e95a9851SMike Kravetz addr |= last_addr_mask; 4996c74df32cSHugh Dickins continue; 4997e95a9851SMike Kravetz } 4998bc70fbf2SPeter Xu dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz); 4999e8569dd2SAndreas Sandberg if (!dst_pte) { 5000e8569dd2SAndreas Sandberg ret = -ENOMEM; 5001e8569dd2SAndreas Sandberg break; 5002e8569dd2SAndreas Sandberg } 5003c5c99429SLarry Woodman 50045e41540cSMike Kravetz /* 50055e41540cSMike Kravetz * If the pagetables are shared don't copy or take references. 50065e41540cSMike Kravetz * 50073aa4ed80SMiaohe Lin * dst_pte == src_pte is the common case of src/dest sharing. 50085e41540cSMike Kravetz * However, src could have 'unshared' and dst shares with 50093aa4ed80SMiaohe Lin * another vma. So page_count of ptep page is checked instead 50103aa4ed80SMiaohe Lin * to reliably determine whether pte is shared. 50115e41540cSMike Kravetz */ 50123aa4ed80SMiaohe Lin if (page_count(virt_to_page(dst_pte)) > 1) { 5013e95a9851SMike Kravetz addr |= last_addr_mask; 5014c5c99429SLarry Woodman continue; 5015e95a9851SMike Kravetz } 5016c5c99429SLarry Woodman 5017cb900f41SKirill A. Shutemov dst_ptl = huge_pte_lock(h, dst, dst_pte); 5018cb900f41SKirill A. Shutemov src_ptl = huge_pte_lockptr(h, src, src_pte); 5019cb900f41SKirill A. Shutemov spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 50204a705fefSNaoya Horiguchi entry = huge_ptep_get(src_pte); 50214eae4efaSPeter Xu again: 50223aa4ed80SMiaohe Lin if (huge_pte_none(entry)) { 50235e41540cSMike Kravetz /* 50243aa4ed80SMiaohe Lin * Skip if src entry none. 50255e41540cSMike Kravetz */ 50264a705fefSNaoya Horiguchi ; 5027c2cb0dccSNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) { 5028c2cb0dccSNaoya Horiguchi bool uffd_wp = huge_pte_uffd_wp(entry); 5029c2cb0dccSNaoya Horiguchi 5030c2cb0dccSNaoya Horiguchi if (!userfaultfd_wp(dst_vma) && uffd_wp) 5031c2cb0dccSNaoya Horiguchi entry = huge_pte_clear_uffd_wp(entry); 5032c2cb0dccSNaoya Horiguchi set_huge_pte_at(dst, addr, dst_pte, entry); 5033c2cb0dccSNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_migration(entry))) { 50344a705fefSNaoya Horiguchi swp_entry_t swp_entry = pte_to_swp_entry(entry); 5035bc70fbf2SPeter Xu bool uffd_wp = huge_pte_uffd_wp(entry); 50364a705fefSNaoya Horiguchi 50376c287605SDavid Hildenbrand if (!is_readable_migration_entry(swp_entry) && cow) { 50384a705fefSNaoya Horiguchi /* 50394a705fefSNaoya Horiguchi * COW mappings require pages in both 50404a705fefSNaoya Horiguchi * parent and child to be set to read. 50414a705fefSNaoya Horiguchi */ 50424dd845b5SAlistair Popple swp_entry = make_readable_migration_entry( 50434dd845b5SAlistair Popple swp_offset(swp_entry)); 50444a705fefSNaoya Horiguchi entry = swp_entry_to_pte(swp_entry); 5045bc70fbf2SPeter Xu if (userfaultfd_wp(src_vma) && uffd_wp) 5046bc70fbf2SPeter Xu entry = huge_pte_mkuffd_wp(entry); 504718f39629SQi Zheng set_huge_pte_at(src, addr, src_pte, entry); 50484a705fefSNaoya Horiguchi } 5049bc70fbf2SPeter Xu if (!userfaultfd_wp(dst_vma) && uffd_wp) 5050bc70fbf2SPeter Xu entry = huge_pte_clear_uffd_wp(entry); 505118f39629SQi Zheng set_huge_pte_at(dst, addr, dst_pte, entry); 5052bc70fbf2SPeter Xu } else if (unlikely(is_pte_marker(entry))) { 50537e3ce3f8SPeter Xu /* No swap on hugetlb */ 50547e3ce3f8SPeter Xu WARN_ON_ONCE( 50557e3ce3f8SPeter Xu is_swapin_error_entry(pte_to_swp_entry(entry))); 5056bc70fbf2SPeter Xu /* 5057bc70fbf2SPeter Xu * We copy the pte marker only if the dst vma has 5058bc70fbf2SPeter Xu * uffd-wp enabled. 5059bc70fbf2SPeter Xu */ 5060bc70fbf2SPeter Xu if (userfaultfd_wp(dst_vma)) 5061bc70fbf2SPeter Xu set_huge_pte_at(dst, addr, dst_pte, entry); 50624a705fefSNaoya Horiguchi } else { 50634eae4efaSPeter Xu entry = huge_ptep_get(src_pte); 50644eae4efaSPeter Xu ptepage = pte_page(entry); 50654eae4efaSPeter Xu get_page(ptepage); 50664eae4efaSPeter Xu 50674eae4efaSPeter Xu /* 5068fb3d824dSDavid Hildenbrand * Failing to duplicate the anon rmap is a rare case 5069fb3d824dSDavid Hildenbrand * where we see pinned hugetlb pages while they're 5070fb3d824dSDavid Hildenbrand * prone to COW. We need to do the COW earlier during 5071fb3d824dSDavid Hildenbrand * fork. 50724eae4efaSPeter Xu * 50734eae4efaSPeter Xu * When pre-allocating the page or copying data, we 50744eae4efaSPeter Xu * need to be without the pgtable locks since we could 50754eae4efaSPeter Xu * sleep during the process. 50764eae4efaSPeter Xu */ 5077fb3d824dSDavid Hildenbrand if (!PageAnon(ptepage)) { 5078fb3d824dSDavid Hildenbrand page_dup_file_rmap(ptepage, true); 5079bc70fbf2SPeter Xu } else if (page_try_dup_anon_rmap(ptepage, true, 5080bc70fbf2SPeter Xu src_vma)) { 50814eae4efaSPeter Xu pte_t src_pte_old = entry; 50824eae4efaSPeter Xu struct page *new; 50834eae4efaSPeter Xu 50844eae4efaSPeter Xu spin_unlock(src_ptl); 50854eae4efaSPeter Xu spin_unlock(dst_ptl); 50864eae4efaSPeter Xu /* Do not use reserve as it's private owned */ 5087bc70fbf2SPeter Xu new = alloc_huge_page(dst_vma, addr, 1); 50884eae4efaSPeter Xu if (IS_ERR(new)) { 50894eae4efaSPeter Xu put_page(ptepage); 50904eae4efaSPeter Xu ret = PTR_ERR(new); 50914eae4efaSPeter Xu break; 50924eae4efaSPeter Xu } 5093bc70fbf2SPeter Xu copy_user_huge_page(new, ptepage, addr, dst_vma, 50944eae4efaSPeter Xu npages); 50954eae4efaSPeter Xu put_page(ptepage); 50964eae4efaSPeter Xu 50974eae4efaSPeter Xu /* Install the new huge page if src pte stable */ 50984eae4efaSPeter Xu dst_ptl = huge_pte_lock(h, dst, dst_pte); 50994eae4efaSPeter Xu src_ptl = huge_pte_lockptr(h, src, src_pte); 51004eae4efaSPeter Xu spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 51014eae4efaSPeter Xu entry = huge_ptep_get(src_pte); 51024eae4efaSPeter Xu if (!pte_same(src_pte_old, entry)) { 5103bc70fbf2SPeter Xu restore_reserve_on_error(h, dst_vma, addr, 5104846be085SMike Kravetz new); 51054eae4efaSPeter Xu put_page(new); 51063aa4ed80SMiaohe Lin /* huge_ptep of dst_pte won't change as in child */ 51074eae4efaSPeter Xu goto again; 51084eae4efaSPeter Xu } 5109bc70fbf2SPeter Xu hugetlb_install_page(dst_vma, dst_pte, addr, new); 51104eae4efaSPeter Xu spin_unlock(src_ptl); 51114eae4efaSPeter Xu spin_unlock(dst_ptl); 51124eae4efaSPeter Xu continue; 51134eae4efaSPeter Xu } 51144eae4efaSPeter Xu 511534ee645eSJoerg Roedel if (cow) { 51160f10851eSJérôme Glisse /* 51170f10851eSJérôme Glisse * No need to notify as we are downgrading page 51180f10851eSJérôme Glisse * table protection not changing it to point 51190f10851eSJérôme Glisse * to a new page. 51200f10851eSJérôme Glisse * 5121ee65728eSMike Rapoport * See Documentation/mm/mmu_notifier.rst 51220f10851eSJérôme Glisse */ 51237f2e9525SGerald Schaefer huge_ptep_set_wrprotect(src, addr, src_pte); 512484894e1cSPeter Xu entry = huge_pte_wrprotect(entry); 512534ee645eSJoerg Roedel } 51264eae4efaSPeter Xu 512763551ae0SDavid Gibson set_huge_pte_at(dst, addr, dst_pte, entry); 51284eae4efaSPeter Xu hugetlb_count_add(npages, dst); 51291c59827dSHugh Dickins } 5130cb900f41SKirill A. Shutemov spin_unlock(src_ptl); 5131cb900f41SKirill A. Shutemov spin_unlock(dst_ptl); 513263551ae0SDavid Gibson } 513363551ae0SDavid Gibson 5134623a1ddfSDavid Hildenbrand if (cow) { 5135623a1ddfSDavid Hildenbrand raw_write_seqcount_end(&src->write_protect_seq); 5136ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 513740549ba8SMike Kravetz } else { 513840549ba8SMike Kravetz hugetlb_vma_unlock_read(src_vma); 5139623a1ddfSDavid Hildenbrand } 5140e8569dd2SAndreas Sandberg 5141e8569dd2SAndreas Sandberg return ret; 514263551ae0SDavid Gibson } 514363551ae0SDavid Gibson 5144550a7d60SMina Almasry static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr, 5145db110a99SAneesh Kumar K.V unsigned long new_addr, pte_t *src_pte, pte_t *dst_pte) 5146550a7d60SMina Almasry { 5147550a7d60SMina Almasry struct hstate *h = hstate_vma(vma); 5148550a7d60SMina Almasry struct mm_struct *mm = vma->vm_mm; 5149550a7d60SMina Almasry spinlock_t *src_ptl, *dst_ptl; 5150db110a99SAneesh Kumar K.V pte_t pte; 5151550a7d60SMina Almasry 5152550a7d60SMina Almasry dst_ptl = huge_pte_lock(h, mm, dst_pte); 5153550a7d60SMina Almasry src_ptl = huge_pte_lockptr(h, mm, src_pte); 5154550a7d60SMina Almasry 5155550a7d60SMina Almasry /* 5156550a7d60SMina Almasry * We don't have to worry about the ordering of src and dst ptlocks 51578651a137SLorenzo Stoakes * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock. 5158550a7d60SMina Almasry */ 5159550a7d60SMina Almasry if (src_ptl != dst_ptl) 5160550a7d60SMina Almasry spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 5161550a7d60SMina Almasry 5162550a7d60SMina Almasry pte = huge_ptep_get_and_clear(mm, old_addr, src_pte); 5163550a7d60SMina Almasry set_huge_pte_at(mm, new_addr, dst_pte, pte); 5164550a7d60SMina Almasry 5165550a7d60SMina Almasry if (src_ptl != dst_ptl) 5166550a7d60SMina Almasry spin_unlock(src_ptl); 5167550a7d60SMina Almasry spin_unlock(dst_ptl); 5168550a7d60SMina Almasry } 5169550a7d60SMina Almasry 5170550a7d60SMina Almasry int move_hugetlb_page_tables(struct vm_area_struct *vma, 5171550a7d60SMina Almasry struct vm_area_struct *new_vma, 5172550a7d60SMina Almasry unsigned long old_addr, unsigned long new_addr, 5173550a7d60SMina Almasry unsigned long len) 5174550a7d60SMina Almasry { 5175550a7d60SMina Almasry struct hstate *h = hstate_vma(vma); 5176550a7d60SMina Almasry struct address_space *mapping = vma->vm_file->f_mapping; 5177550a7d60SMina Almasry unsigned long sz = huge_page_size(h); 5178550a7d60SMina Almasry struct mm_struct *mm = vma->vm_mm; 5179550a7d60SMina Almasry unsigned long old_end = old_addr + len; 5180e95a9851SMike Kravetz unsigned long last_addr_mask; 5181550a7d60SMina Almasry pte_t *src_pte, *dst_pte; 5182550a7d60SMina Almasry struct mmu_notifier_range range; 51833d0b95cdSBaolin Wang bool shared_pmd = false; 5184550a7d60SMina Almasry 5185550a7d60SMina Almasry mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, old_addr, 5186550a7d60SMina Almasry old_end); 5187550a7d60SMina Almasry adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); 51883d0b95cdSBaolin Wang /* 51893d0b95cdSBaolin Wang * In case of shared PMDs, we should cover the maximum possible 51903d0b95cdSBaolin Wang * range. 51913d0b95cdSBaolin Wang */ 51923d0b95cdSBaolin Wang flush_cache_range(vma, range.start, range.end); 51933d0b95cdSBaolin Wang 5194550a7d60SMina Almasry mmu_notifier_invalidate_range_start(&range); 5195e95a9851SMike Kravetz last_addr_mask = hugetlb_mask_last_page(h); 5196550a7d60SMina Almasry /* Prevent race with file truncation */ 519740549ba8SMike Kravetz hugetlb_vma_lock_write(vma); 5198550a7d60SMina Almasry i_mmap_lock_write(mapping); 5199550a7d60SMina Almasry for (; old_addr < old_end; old_addr += sz, new_addr += sz) { 5200550a7d60SMina Almasry src_pte = huge_pte_offset(mm, old_addr, sz); 5201e95a9851SMike Kravetz if (!src_pte) { 5202e95a9851SMike Kravetz old_addr |= last_addr_mask; 5203e95a9851SMike Kravetz new_addr |= last_addr_mask; 5204550a7d60SMina Almasry continue; 5205e95a9851SMike Kravetz } 5206550a7d60SMina Almasry if (huge_pte_none(huge_ptep_get(src_pte))) 5207550a7d60SMina Almasry continue; 5208550a7d60SMina Almasry 52094ddb4d91SMike Kravetz if (huge_pmd_unshare(mm, vma, old_addr, src_pte)) { 52103d0b95cdSBaolin Wang shared_pmd = true; 52114ddb4d91SMike Kravetz old_addr |= last_addr_mask; 52124ddb4d91SMike Kravetz new_addr |= last_addr_mask; 5213550a7d60SMina Almasry continue; 52143d0b95cdSBaolin Wang } 5215550a7d60SMina Almasry 5216550a7d60SMina Almasry dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz); 5217550a7d60SMina Almasry if (!dst_pte) 5218550a7d60SMina Almasry break; 5219550a7d60SMina Almasry 5220db110a99SAneesh Kumar K.V move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte); 5221550a7d60SMina Almasry } 52223d0b95cdSBaolin Wang 52233d0b95cdSBaolin Wang if (shared_pmd) 52243d0b95cdSBaolin Wang flush_tlb_range(vma, range.start, range.end); 52253d0b95cdSBaolin Wang else 5226550a7d60SMina Almasry flush_tlb_range(vma, old_end - len, old_end); 5227550a7d60SMina Almasry mmu_notifier_invalidate_range_end(&range); 522813e4ad2cSNadav Amit i_mmap_unlock_write(mapping); 522940549ba8SMike Kravetz hugetlb_vma_unlock_write(vma); 5230550a7d60SMina Almasry 5231550a7d60SMina Almasry return len + old_addr - old_end; 5232550a7d60SMina Almasry } 5233550a7d60SMina Almasry 523473c54763SPeter Xu static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, 523524669e58SAneesh Kumar K.V unsigned long start, unsigned long end, 523605e90bd0SPeter Xu struct page *ref_page, zap_flags_t zap_flags) 523763551ae0SDavid Gibson { 523863551ae0SDavid Gibson struct mm_struct *mm = vma->vm_mm; 523963551ae0SDavid Gibson unsigned long address; 5240c7546f8fSDavid Gibson pte_t *ptep; 524163551ae0SDavid Gibson pte_t pte; 5242cb900f41SKirill A. Shutemov spinlock_t *ptl; 524363551ae0SDavid Gibson struct page *page; 5244a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 5245a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 5246e95a9851SMike Kravetz unsigned long last_addr_mask; 5247a4a118f2SNadav Amit bool force_flush = false; 5248a5516438SAndi Kleen 524963551ae0SDavid Gibson WARN_ON(!is_vm_hugetlb_page(vma)); 5250a5516438SAndi Kleen BUG_ON(start & ~huge_page_mask(h)); 5251a5516438SAndi Kleen BUG_ON(end & ~huge_page_mask(h)); 525263551ae0SDavid Gibson 525307e32661SAneesh Kumar K.V /* 525407e32661SAneesh Kumar K.V * This is a hugetlb vma, all the pte entries should point 525507e32661SAneesh Kumar K.V * to huge page. 525607e32661SAneesh Kumar K.V */ 5257ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, sz); 525824669e58SAneesh Kumar K.V tlb_start_vma(tlb, vma); 5259dff11abeSMike Kravetz 5260e95a9851SMike Kravetz last_addr_mask = hugetlb_mask_last_page(h); 5261569f48b8SHillf Danton address = start; 5262569f48b8SHillf Danton for (; address < end; address += sz) { 52637868a208SPunit Agrawal ptep = huge_pte_offset(mm, address, sz); 5264e95a9851SMike Kravetz if (!ptep) { 5265e95a9851SMike Kravetz address |= last_addr_mask; 5266c7546f8fSDavid Gibson continue; 5267e95a9851SMike Kravetz } 5268c7546f8fSDavid Gibson 5269cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 52704ddb4d91SMike Kravetz if (huge_pmd_unshare(mm, vma, address, ptep)) { 527131d49da5SAneesh Kumar K.V spin_unlock(ptl); 5272a4a118f2SNadav Amit tlb_flush_pmd_range(tlb, address & PUD_MASK, PUD_SIZE); 5273a4a118f2SNadav Amit force_flush = true; 52744ddb4d91SMike Kravetz address |= last_addr_mask; 527531d49da5SAneesh Kumar K.V continue; 527631d49da5SAneesh Kumar K.V } 527739dde65cSChen, Kenneth W 52786629326bSHillf Danton pte = huge_ptep_get(ptep); 527931d49da5SAneesh Kumar K.V if (huge_pte_none(pte)) { 528031d49da5SAneesh Kumar K.V spin_unlock(ptl); 528131d49da5SAneesh Kumar K.V continue; 528231d49da5SAneesh Kumar K.V } 52836629326bSHillf Danton 52846629326bSHillf Danton /* 52859fbc1f63SNaoya Horiguchi * Migrating hugepage or HWPoisoned hugepage is already 52869fbc1f63SNaoya Horiguchi * unmapped and its refcount is dropped, so just clear pte here. 52876629326bSHillf Danton */ 52889fbc1f63SNaoya Horiguchi if (unlikely(!pte_present(pte))) { 528905e90bd0SPeter Xu /* 529005e90bd0SPeter Xu * If the pte was wr-protected by uffd-wp in any of the 529105e90bd0SPeter Xu * swap forms, meanwhile the caller does not want to 529205e90bd0SPeter Xu * drop the uffd-wp bit in this zap, then replace the 529305e90bd0SPeter Xu * pte with a marker. 529405e90bd0SPeter Xu */ 529505e90bd0SPeter Xu if (pte_swp_uffd_wp_any(pte) && 529605e90bd0SPeter Xu !(zap_flags & ZAP_FLAG_DROP_MARKER)) 529705e90bd0SPeter Xu set_huge_pte_at(mm, address, ptep, 529805e90bd0SPeter Xu make_pte_marker(PTE_MARKER_UFFD_WP)); 529905e90bd0SPeter Xu else 53009386fac3SPunit Agrawal huge_pte_clear(mm, address, ptep, sz); 530131d49da5SAneesh Kumar K.V spin_unlock(ptl); 530231d49da5SAneesh Kumar K.V continue; 53038c4894c6SNaoya Horiguchi } 53046629326bSHillf Danton 53056629326bSHillf Danton page = pte_page(pte); 530604f2cbe3SMel Gorman /* 530704f2cbe3SMel Gorman * If a reference page is supplied, it is because a specific 530804f2cbe3SMel Gorman * page is being unmapped, not a range. Ensure the page we 530904f2cbe3SMel Gorman * are about to unmap is the actual page of interest. 531004f2cbe3SMel Gorman */ 531104f2cbe3SMel Gorman if (ref_page) { 531231d49da5SAneesh Kumar K.V if (page != ref_page) { 531331d49da5SAneesh Kumar K.V spin_unlock(ptl); 531431d49da5SAneesh Kumar K.V continue; 531531d49da5SAneesh Kumar K.V } 531604f2cbe3SMel Gorman /* 531704f2cbe3SMel Gorman * Mark the VMA as having unmapped its page so that 531804f2cbe3SMel Gorman * future faults in this VMA will fail rather than 531904f2cbe3SMel Gorman * looking like data was lost 532004f2cbe3SMel Gorman */ 532104f2cbe3SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); 532204f2cbe3SMel Gorman } 532304f2cbe3SMel Gorman 5324c7546f8fSDavid Gibson pte = huge_ptep_get_and_clear(mm, address, ptep); 5325b528e4b6SAneesh Kumar K.V tlb_remove_huge_tlb_entry(h, tlb, ptep, address); 5326106c992aSGerald Schaefer if (huge_pte_dirty(pte)) 53276649a386SKen Chen set_page_dirty(page); 532805e90bd0SPeter Xu /* Leave a uffd-wp pte marker if needed */ 532905e90bd0SPeter Xu if (huge_pte_uffd_wp(pte) && 533005e90bd0SPeter Xu !(zap_flags & ZAP_FLAG_DROP_MARKER)) 533105e90bd0SPeter Xu set_huge_pte_at(mm, address, ptep, 533205e90bd0SPeter Xu make_pte_marker(PTE_MARKER_UFFD_WP)); 53335d317b2bSNaoya Horiguchi hugetlb_count_sub(pages_per_huge_page(h), mm); 5334cea86fe2SHugh Dickins page_remove_rmap(page, vma, true); 533531d49da5SAneesh Kumar K.V 5336cb900f41SKirill A. Shutemov spin_unlock(ptl); 5337e77b0852SAneesh Kumar K.V tlb_remove_page_size(tlb, page, huge_page_size(h)); 533824669e58SAneesh Kumar K.V /* 533931d49da5SAneesh Kumar K.V * Bail out after unmapping reference page if supplied 534024669e58SAneesh Kumar K.V */ 534131d49da5SAneesh Kumar K.V if (ref_page) 534231d49da5SAneesh Kumar K.V break; 5343fe1668aeSChen, Kenneth W } 534424669e58SAneesh Kumar K.V tlb_end_vma(tlb, vma); 5345a4a118f2SNadav Amit 5346a4a118f2SNadav Amit /* 5347a4a118f2SNadav Amit * If we unshared PMDs, the TLB flush was not recorded in mmu_gather. We 5348a4a118f2SNadav Amit * could defer the flush until now, since by holding i_mmap_rwsem we 5349a4a118f2SNadav Amit * guaranteed that the last refernece would not be dropped. But we must 5350a4a118f2SNadav Amit * do the flushing before we return, as otherwise i_mmap_rwsem will be 5351a4a118f2SNadav Amit * dropped and the last reference to the shared PMDs page might be 5352a4a118f2SNadav Amit * dropped as well. 5353a4a118f2SNadav Amit * 5354a4a118f2SNadav Amit * In theory we could defer the freeing of the PMD pages as well, but 5355a4a118f2SNadav Amit * huge_pmd_unshare() relies on the exact page_count for the PMD page to 5356a4a118f2SNadav Amit * detect sharing, so we cannot defer the release of the page either. 5357a4a118f2SNadav Amit * Instead, do flush now. 5358a4a118f2SNadav Amit */ 5359a4a118f2SNadav Amit if (force_flush) 5360a4a118f2SNadav Amit tlb_flush_mmu_tlbonly(tlb); 53611da177e4SLinus Torvalds } 536263551ae0SDavid Gibson 5363d833352aSMel Gorman void __unmap_hugepage_range_final(struct mmu_gather *tlb, 5364d833352aSMel Gorman struct vm_area_struct *vma, unsigned long start, 536505e90bd0SPeter Xu unsigned long end, struct page *ref_page, 536605e90bd0SPeter Xu zap_flags_t zap_flags) 5367d833352aSMel Gorman { 5368131a79b4SMike Kravetz hugetlb_vma_lock_write(vma); 5369131a79b4SMike Kravetz i_mmap_lock_write(vma->vm_file->f_mapping); 5370131a79b4SMike Kravetz 5371369258ceSMike Kravetz /* mmu notification performed in caller */ 537205e90bd0SPeter Xu __unmap_hugepage_range(tlb, vma, start, end, ref_page, zap_flags); 5373d833352aSMel Gorman 537404ada095SMike Kravetz if (zap_flags & ZAP_FLAG_UNMAP) { /* final unmap */ 5375d833352aSMel Gorman /* 537604ada095SMike Kravetz * Unlock and free the vma lock before releasing i_mmap_rwsem. 537704ada095SMike Kravetz * When the vma_lock is freed, this makes the vma ineligible 537804ada095SMike Kravetz * for pmd sharing. And, i_mmap_rwsem is required to set up 537904ada095SMike Kravetz * pmd sharing. This is important as page tables for this 538004ada095SMike Kravetz * unmapped range will be asynchrously deleted. If the page 538104ada095SMike Kravetz * tables are shared, there will be issues when accessed by 538204ada095SMike Kravetz * someone else. 5383d833352aSMel Gorman */ 5384ecfbd733SMike Kravetz __hugetlb_vma_unlock_write_free(vma); 5385131a79b4SMike Kravetz i_mmap_unlock_write(vma->vm_file->f_mapping); 538604ada095SMike Kravetz } else { 538704ada095SMike Kravetz i_mmap_unlock_write(vma->vm_file->f_mapping); 538804ada095SMike Kravetz hugetlb_vma_unlock_write(vma); 538904ada095SMike Kravetz } 5390d833352aSMel Gorman } 5391d833352aSMel Gorman 5392502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 539305e90bd0SPeter Xu unsigned long end, struct page *ref_page, 539405e90bd0SPeter Xu zap_flags_t zap_flags) 5395502717f4SChen, Kenneth W { 5396369258ceSMike Kravetz struct mmu_notifier_range range; 539724669e58SAneesh Kumar K.V struct mmu_gather tlb; 5398dff11abeSMike Kravetz 5399369258ceSMike Kravetz mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, 5400369258ceSMike Kravetz start, end); 5401369258ceSMike Kravetz adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); 5402369258ceSMike Kravetz mmu_notifier_invalidate_range_start(&range); 5403a72afd87SWill Deacon tlb_gather_mmu(&tlb, vma->vm_mm); 5404369258ceSMike Kravetz 540505e90bd0SPeter Xu __unmap_hugepage_range(&tlb, vma, start, end, ref_page, zap_flags); 5406369258ceSMike Kravetz 5407369258ceSMike Kravetz mmu_notifier_invalidate_range_end(&range); 5408ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 5409502717f4SChen, Kenneth W } 5410502717f4SChen, Kenneth W 541104f2cbe3SMel Gorman /* 541204f2cbe3SMel Gorman * This is called when the original mapper is failing to COW a MAP_PRIVATE 5413578b7725SZhiyuan Dai * mapping it owns the reserve page for. The intention is to unmap the page 541404f2cbe3SMel Gorman * from other VMAs and let the children be SIGKILLed if they are faulting the 541504f2cbe3SMel Gorman * same region. 541604f2cbe3SMel Gorman */ 54172f4612afSDavidlohr Bueso static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, 54182a4b3dedSHarvey Harrison struct page *page, unsigned long address) 541904f2cbe3SMel Gorman { 54207526674dSAdam Litke struct hstate *h = hstate_vma(vma); 542104f2cbe3SMel Gorman struct vm_area_struct *iter_vma; 542204f2cbe3SMel Gorman struct address_space *mapping; 542304f2cbe3SMel Gorman pgoff_t pgoff; 542404f2cbe3SMel Gorman 542504f2cbe3SMel Gorman /* 542604f2cbe3SMel Gorman * vm_pgoff is in PAGE_SIZE units, hence the different calculation 542704f2cbe3SMel Gorman * from page cache lookup which is in HPAGE_SIZE units. 542804f2cbe3SMel Gorman */ 54297526674dSAdam Litke address = address & huge_page_mask(h); 543036e4f20aSMichal Hocko pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + 543136e4f20aSMichal Hocko vma->vm_pgoff; 543293c76a3dSAl Viro mapping = vma->vm_file->f_mapping; 543304f2cbe3SMel Gorman 54344eb2b1dcSMel Gorman /* 54354eb2b1dcSMel Gorman * Take the mapping lock for the duration of the table walk. As 54364eb2b1dcSMel Gorman * this mapping should be shared between all the VMAs, 54374eb2b1dcSMel Gorman * __unmap_hugepage_range() is called as the lock is already held 54384eb2b1dcSMel Gorman */ 543983cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 54406b2dbba8SMichel Lespinasse vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { 544104f2cbe3SMel Gorman /* Do not unmap the current VMA */ 544204f2cbe3SMel Gorman if (iter_vma == vma) 544304f2cbe3SMel Gorman continue; 544404f2cbe3SMel Gorman 544504f2cbe3SMel Gorman /* 54462f84a899SMel Gorman * Shared VMAs have their own reserves and do not affect 54472f84a899SMel Gorman * MAP_PRIVATE accounting but it is possible that a shared 54482f84a899SMel Gorman * VMA is using the same page so check and skip such VMAs. 54492f84a899SMel Gorman */ 54502f84a899SMel Gorman if (iter_vma->vm_flags & VM_MAYSHARE) 54512f84a899SMel Gorman continue; 54522f84a899SMel Gorman 54532f84a899SMel Gorman /* 545404f2cbe3SMel Gorman * Unmap the page from other VMAs without their own reserves. 545504f2cbe3SMel Gorman * They get marked to be SIGKILLed if they fault in these 545604f2cbe3SMel Gorman * areas. This is because a future no-page fault on this VMA 545704f2cbe3SMel Gorman * could insert a zeroed page instead of the data existing 545804f2cbe3SMel Gorman * from the time of fork. This would look like data corruption 545904f2cbe3SMel Gorman */ 546004f2cbe3SMel Gorman if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) 546124669e58SAneesh Kumar K.V unmap_hugepage_range(iter_vma, address, 546205e90bd0SPeter Xu address + huge_page_size(h), page, 0); 546304f2cbe3SMel Gorman } 546483cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 546504f2cbe3SMel Gorman } 546604f2cbe3SMel Gorman 54670fe6e20bSNaoya Horiguchi /* 5468c89357e2SDavid Hildenbrand * hugetlb_wp() should be called with page lock of the original hugepage held. 5469aa6d2e8cSBaolin Wang * Called with hugetlb_fault_mutex_table held and pte_page locked so we 5470ef009b25SMichal Hocko * cannot race with other handlers or page migration. 5471ef009b25SMichal Hocko * Keep the pte_same checks anyway to make transition from the mutex easier. 54720fe6e20bSNaoya Horiguchi */ 5473c89357e2SDavid Hildenbrand static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, 5474c89357e2SDavid Hildenbrand unsigned long address, pte_t *ptep, unsigned int flags, 5475cb900f41SKirill A. Shutemov struct page *pagecache_page, spinlock_t *ptl) 54761e8f889bSDavid Gibson { 5477c89357e2SDavid Hildenbrand const bool unshare = flags & FAULT_FLAG_UNSHARE; 54783999f52eSAneesh Kumar K.V pte_t pte; 5479a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 54801e8f889bSDavid Gibson struct page *old_page, *new_page; 54812b740303SSouptick Joarder int outside_reserve = 0; 54822b740303SSouptick Joarder vm_fault_t ret = 0; 5483974e6d66SHuang Ying unsigned long haddr = address & huge_page_mask(h); 5484ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 54851e8f889bSDavid Gibson 54861d8d1464SDavid Hildenbrand /* 54871d8d1464SDavid Hildenbrand * hugetlb does not support FOLL_FORCE-style write faults that keep the 54881d8d1464SDavid Hildenbrand * PTE mapped R/O such as maybe_mkwrite() would do. 54891d8d1464SDavid Hildenbrand */ 54901d8d1464SDavid Hildenbrand if (WARN_ON_ONCE(!unshare && !(vma->vm_flags & VM_WRITE))) 54911d8d1464SDavid Hildenbrand return VM_FAULT_SIGSEGV; 54921d8d1464SDavid Hildenbrand 54931d8d1464SDavid Hildenbrand /* Let's take out MAP_SHARED mappings first. */ 54941d8d1464SDavid Hildenbrand if (vma->vm_flags & VM_MAYSHARE) { 54951d8d1464SDavid Hildenbrand set_huge_ptep_writable(vma, haddr, ptep); 54961d8d1464SDavid Hildenbrand return 0; 54971d8d1464SDavid Hildenbrand } 54981d8d1464SDavid Hildenbrand 54993999f52eSAneesh Kumar K.V pte = huge_ptep_get(ptep); 55001e8f889bSDavid Gibson old_page = pte_page(pte); 55011e8f889bSDavid Gibson 5502662ce1dcSYang Yang delayacct_wpcopy_start(); 5503662ce1dcSYang Yang 550404f2cbe3SMel Gorman retry_avoidcopy: 5505c89357e2SDavid Hildenbrand /* 5506c89357e2SDavid Hildenbrand * If no-one else is actually using this page, we're the exclusive 5507c89357e2SDavid Hildenbrand * owner and can reuse this page. 5508c89357e2SDavid Hildenbrand */ 550937a2140dSJoonsoo Kim if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { 5510c89357e2SDavid Hildenbrand if (!PageAnonExclusive(old_page)) 55115a49973dSHugh Dickins page_move_anon_rmap(old_page, vma); 5512c89357e2SDavid Hildenbrand if (likely(!unshare)) 55135b7a1d40SHuang Ying set_huge_ptep_writable(vma, haddr, ptep); 5514662ce1dcSYang Yang 5515662ce1dcSYang Yang delayacct_wpcopy_end(); 551683c54070SNick Piggin return 0; 55171e8f889bSDavid Gibson } 55186c287605SDavid Hildenbrand VM_BUG_ON_PAGE(PageAnon(old_page) && PageAnonExclusive(old_page), 55196c287605SDavid Hildenbrand old_page); 55201e8f889bSDavid Gibson 552104f2cbe3SMel Gorman /* 552204f2cbe3SMel Gorman * If the process that created a MAP_PRIVATE mapping is about to 552304f2cbe3SMel Gorman * perform a COW due to a shared page count, attempt to satisfy 552404f2cbe3SMel Gorman * the allocation without using the existing reserves. The pagecache 552504f2cbe3SMel Gorman * page is used to determine if the reserve at this address was 552604f2cbe3SMel Gorman * consumed or not. If reserves were used, a partial faulted mapping 552704f2cbe3SMel Gorman * at the time of fork() could consume its reserves on COW instead 552804f2cbe3SMel Gorman * of the full address range. 552904f2cbe3SMel Gorman */ 55305944d011SJoonsoo Kim if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && 553104f2cbe3SMel Gorman old_page != pagecache_page) 553204f2cbe3SMel Gorman outside_reserve = 1; 553304f2cbe3SMel Gorman 553409cbfeafSKirill A. Shutemov get_page(old_page); 5535b76c8cfbSLarry Woodman 5536ad4404a2SDavidlohr Bueso /* 5537ad4404a2SDavidlohr Bueso * Drop page table lock as buddy allocator may be called. It will 5538ad4404a2SDavidlohr Bueso * be acquired again before returning to the caller, as expected. 5539ad4404a2SDavidlohr Bueso */ 5540cb900f41SKirill A. Shutemov spin_unlock(ptl); 55415b7a1d40SHuang Ying new_page = alloc_huge_page(vma, haddr, outside_reserve); 55421e8f889bSDavid Gibson 55432fc39cecSAdam Litke if (IS_ERR(new_page)) { 554404f2cbe3SMel Gorman /* 554504f2cbe3SMel Gorman * If a process owning a MAP_PRIVATE mapping fails to COW, 554604f2cbe3SMel Gorman * it is due to references held by a child and an insufficient 554704f2cbe3SMel Gorman * huge page pool. To guarantee the original mappers 554804f2cbe3SMel Gorman * reliability, unmap the page from child processes. The child 554904f2cbe3SMel Gorman * may get SIGKILLed if it later faults. 555004f2cbe3SMel Gorman */ 555104f2cbe3SMel Gorman if (outside_reserve) { 555240549ba8SMike Kravetz struct address_space *mapping = vma->vm_file->f_mapping; 555340549ba8SMike Kravetz pgoff_t idx; 555440549ba8SMike Kravetz u32 hash; 555540549ba8SMike Kravetz 555609cbfeafSKirill A. Shutemov put_page(old_page); 555740549ba8SMike Kravetz /* 555840549ba8SMike Kravetz * Drop hugetlb_fault_mutex and vma_lock before 555940549ba8SMike Kravetz * unmapping. unmapping needs to hold vma_lock 556040549ba8SMike Kravetz * in write mode. Dropping vma_lock in read mode 556140549ba8SMike Kravetz * here is OK as COW mappings do not interact with 556240549ba8SMike Kravetz * PMD sharing. 556340549ba8SMike Kravetz * 556440549ba8SMike Kravetz * Reacquire both after unmap operation. 556540549ba8SMike Kravetz */ 556640549ba8SMike Kravetz idx = vma_hugecache_offset(h, vma, haddr); 556740549ba8SMike Kravetz hash = hugetlb_fault_mutex_hash(mapping, idx); 556840549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 556940549ba8SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 557040549ba8SMike Kravetz 55715b7a1d40SHuang Ying unmap_ref_private(mm, vma, old_page, haddr); 557240549ba8SMike Kravetz 557340549ba8SMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 557440549ba8SMike Kravetz hugetlb_vma_lock_read(vma); 5575cb900f41SKirill A. Shutemov spin_lock(ptl); 55765b7a1d40SHuang Ying ptep = huge_pte_offset(mm, haddr, huge_page_size(h)); 5577a9af0c5dSNaoya Horiguchi if (likely(ptep && 5578a9af0c5dSNaoya Horiguchi pte_same(huge_ptep_get(ptep), pte))) 557904f2cbe3SMel Gorman goto retry_avoidcopy; 5580a734bcc8SHillf Danton /* 5581cb900f41SKirill A. Shutemov * race occurs while re-acquiring page table 5582cb900f41SKirill A. Shutemov * lock, and our job is done. 5583a734bcc8SHillf Danton */ 5584662ce1dcSYang Yang delayacct_wpcopy_end(); 5585a734bcc8SHillf Danton return 0; 558604f2cbe3SMel Gorman } 558704f2cbe3SMel Gorman 55882b740303SSouptick Joarder ret = vmf_error(PTR_ERR(new_page)); 5589ad4404a2SDavidlohr Bueso goto out_release_old; 55901e8f889bSDavid Gibson } 55911e8f889bSDavid Gibson 55920fe6e20bSNaoya Horiguchi /* 55930fe6e20bSNaoya Horiguchi * When the original hugepage is shared one, it does not have 55940fe6e20bSNaoya Horiguchi * anon_vma prepared. 55950fe6e20bSNaoya Horiguchi */ 559644e2aa93SDean Nelson if (unlikely(anon_vma_prepare(vma))) { 5597ad4404a2SDavidlohr Bueso ret = VM_FAULT_OOM; 5598ad4404a2SDavidlohr Bueso goto out_release_all; 559944e2aa93SDean Nelson } 56000fe6e20bSNaoya Horiguchi 5601974e6d66SHuang Ying copy_user_huge_page(new_page, old_page, address, vma, 560247ad8475SAndrea Arcangeli pages_per_huge_page(h)); 56030ed361deSNick Piggin __SetPageUptodate(new_page); 56041e8f889bSDavid Gibson 56057269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, haddr, 56066f4f13e8SJérôme Glisse haddr + huge_page_size(h)); 5607ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 5608ad4404a2SDavidlohr Bueso 5609b76c8cfbSLarry Woodman /* 5610cb900f41SKirill A. Shutemov * Retake the page table lock to check for racing updates 5611b76c8cfbSLarry Woodman * before the page tables are altered 5612b76c8cfbSLarry Woodman */ 5613cb900f41SKirill A. Shutemov spin_lock(ptl); 56145b7a1d40SHuang Ying ptep = huge_pte_offset(mm, haddr, huge_page_size(h)); 5615a9af0c5dSNaoya Horiguchi if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { 5616c89357e2SDavid Hildenbrand /* Break COW or unshare */ 56175b7a1d40SHuang Ying huge_ptep_clear_flush(vma, haddr, ptep); 5618ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range(mm, range.start, range.end); 5619cea86fe2SHugh Dickins page_remove_rmap(old_page, vma, true); 56205b7a1d40SHuang Ying hugepage_add_new_anon_rmap(new_page, vma, haddr); 56211eba86c0SPasha Tatashin set_huge_pte_at(mm, haddr, ptep, 5622c89357e2SDavid Hildenbrand make_huge_pte(vma, new_page, !unshare)); 56238f251a3dSMike Kravetz SetHPageMigratable(new_page); 56241e8f889bSDavid Gibson /* Make the old page be freed below */ 56251e8f889bSDavid Gibson new_page = old_page; 56261e8f889bSDavid Gibson } 5627cb900f41SKirill A. Shutemov spin_unlock(ptl); 5628ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 5629ad4404a2SDavidlohr Bueso out_release_all: 5630c89357e2SDavid Hildenbrand /* 5631c89357e2SDavid Hildenbrand * No restore in case of successful pagetable update (Break COW or 5632c89357e2SDavid Hildenbrand * unshare) 5633c89357e2SDavid Hildenbrand */ 5634c7b1850dSMike Kravetz if (new_page != old_page) 56355b7a1d40SHuang Ying restore_reserve_on_error(h, vma, haddr, new_page); 563609cbfeafSKirill A. Shutemov put_page(new_page); 5637ad4404a2SDavidlohr Bueso out_release_old: 563809cbfeafSKirill A. Shutemov put_page(old_page); 56398312034fSJoonsoo Kim 5640ad4404a2SDavidlohr Bueso spin_lock(ptl); /* Caller expects lock to be held */ 5641662ce1dcSYang Yang 5642662ce1dcSYang Yang delayacct_wpcopy_end(); 5643ad4404a2SDavidlohr Bueso return ret; 56441e8f889bSDavid Gibson } 56451e8f889bSDavid Gibson 56463ae77f43SHugh Dickins /* 56473ae77f43SHugh Dickins * Return whether there is a pagecache page to back given address within VMA. 56483ae77f43SHugh Dickins * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page. 56493ae77f43SHugh Dickins */ 56503ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h, 56512a15efc9SHugh Dickins struct vm_area_struct *vma, unsigned long address) 56522a15efc9SHugh Dickins { 56532a15efc9SHugh Dickins struct address_space *mapping; 56542a15efc9SHugh Dickins pgoff_t idx; 56552a15efc9SHugh Dickins struct page *page; 56562a15efc9SHugh Dickins 56572a15efc9SHugh Dickins mapping = vma->vm_file->f_mapping; 56582a15efc9SHugh Dickins idx = vma_hugecache_offset(h, vma, address); 56592a15efc9SHugh Dickins 56602a15efc9SHugh Dickins page = find_get_page(mapping, idx); 56612a15efc9SHugh Dickins if (page) 56622a15efc9SHugh Dickins put_page(page); 56632a15efc9SHugh Dickins return page != NULL; 56642a15efc9SHugh Dickins } 56652a15efc9SHugh Dickins 56667e1813d4SMike Kravetz int hugetlb_add_to_page_cache(struct page *page, struct address_space *mapping, 5667ab76ad54SMike Kravetz pgoff_t idx) 5668ab76ad54SMike Kravetz { 5669d9ef44deSMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 5670ab76ad54SMike Kravetz struct inode *inode = mapping->host; 5671ab76ad54SMike Kravetz struct hstate *h = hstate_inode(inode); 5672d9ef44deSMatthew Wilcox (Oracle) int err; 5673ab76ad54SMike Kravetz 5674d9ef44deSMatthew Wilcox (Oracle) __folio_set_locked(folio); 5675d9ef44deSMatthew Wilcox (Oracle) err = __filemap_add_folio(mapping, folio, idx, GFP_KERNEL, NULL); 5676d9ef44deSMatthew Wilcox (Oracle) 5677d9ef44deSMatthew Wilcox (Oracle) if (unlikely(err)) { 5678d9ef44deSMatthew Wilcox (Oracle) __folio_clear_locked(folio); 5679ab76ad54SMike Kravetz return err; 5680d9ef44deSMatthew Wilcox (Oracle) } 5681d6995da3SMike Kravetz ClearHPageRestoreReserve(page); 5682ab76ad54SMike Kravetz 568322146c3cSMike Kravetz /* 5684d9ef44deSMatthew Wilcox (Oracle) * mark folio dirty so that it will not be removed from cache/file 568522146c3cSMike Kravetz * by non-hugetlbfs specific code paths. 568622146c3cSMike Kravetz */ 5687d9ef44deSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 568822146c3cSMike Kravetz 5689ab76ad54SMike Kravetz spin_lock(&inode->i_lock); 5690ab76ad54SMike Kravetz inode->i_blocks += blocks_per_huge_page(h); 5691ab76ad54SMike Kravetz spin_unlock(&inode->i_lock); 5692ab76ad54SMike Kravetz return 0; 5693ab76ad54SMike Kravetz } 5694ab76ad54SMike Kravetz 56957677f7fdSAxel Rasmussen static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma, 56967677f7fdSAxel Rasmussen struct address_space *mapping, 56977677f7fdSAxel Rasmussen pgoff_t idx, 56987677f7fdSAxel Rasmussen unsigned int flags, 56997677f7fdSAxel Rasmussen unsigned long haddr, 5700824ddc60SNadav Amit unsigned long addr, 57017677f7fdSAxel Rasmussen unsigned long reason) 57027677f7fdSAxel Rasmussen { 57037677f7fdSAxel Rasmussen u32 hash; 57047677f7fdSAxel Rasmussen struct vm_fault vmf = { 57057677f7fdSAxel Rasmussen .vma = vma, 57067677f7fdSAxel Rasmussen .address = haddr, 5707824ddc60SNadav Amit .real_address = addr, 57087677f7fdSAxel Rasmussen .flags = flags, 57097677f7fdSAxel Rasmussen 57107677f7fdSAxel Rasmussen /* 57117677f7fdSAxel Rasmussen * Hard to debug if it ends up being 57127677f7fdSAxel Rasmussen * used by a callee that assumes 57137677f7fdSAxel Rasmussen * something about the other 57147677f7fdSAxel Rasmussen * uninitialized fields... same as in 57157677f7fdSAxel Rasmussen * memory.c 57167677f7fdSAxel Rasmussen */ 57177677f7fdSAxel Rasmussen }; 57187677f7fdSAxel Rasmussen 57197677f7fdSAxel Rasmussen /* 5720958f32ceSLiu Shixin * vma_lock and hugetlb_fault_mutex must be dropped before handling 5721958f32ceSLiu Shixin * userfault. Also mmap_lock could be dropped due to handling 5722958f32ceSLiu Shixin * userfault, any vma operation should be careful from here. 57237677f7fdSAxel Rasmussen */ 572440549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 57257677f7fdSAxel Rasmussen hash = hugetlb_fault_mutex_hash(mapping, idx); 57267677f7fdSAxel Rasmussen mutex_unlock(&hugetlb_fault_mutex_table[hash]); 5727958f32ceSLiu Shixin return handle_userfault(&vmf, reason); 57287677f7fdSAxel Rasmussen } 57297677f7fdSAxel Rasmussen 57302ea7ff1eSPeter Xu /* 57312ea7ff1eSPeter Xu * Recheck pte with pgtable lock. Returns true if pte didn't change, or 57322ea7ff1eSPeter Xu * false if pte changed or is changing. 57332ea7ff1eSPeter Xu */ 57342ea7ff1eSPeter Xu static bool hugetlb_pte_stable(struct hstate *h, struct mm_struct *mm, 57352ea7ff1eSPeter Xu pte_t *ptep, pte_t old_pte) 57362ea7ff1eSPeter Xu { 57372ea7ff1eSPeter Xu spinlock_t *ptl; 57382ea7ff1eSPeter Xu bool same; 57392ea7ff1eSPeter Xu 57402ea7ff1eSPeter Xu ptl = huge_pte_lock(h, mm, ptep); 57412ea7ff1eSPeter Xu same = pte_same(huge_ptep_get(ptep), old_pte); 57422ea7ff1eSPeter Xu spin_unlock(ptl); 57432ea7ff1eSPeter Xu 57442ea7ff1eSPeter Xu return same; 57452ea7ff1eSPeter Xu } 57462ea7ff1eSPeter Xu 57472b740303SSouptick Joarder static vm_fault_t hugetlb_no_page(struct mm_struct *mm, 57482b740303SSouptick Joarder struct vm_area_struct *vma, 57498382d914SDavidlohr Bueso struct address_space *mapping, pgoff_t idx, 5750c64e912cSPeter Xu unsigned long address, pte_t *ptep, 5751c64e912cSPeter Xu pte_t old_pte, unsigned int flags) 5752ac9b9c66SHugh Dickins { 5753a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 57542b740303SSouptick Joarder vm_fault_t ret = VM_FAULT_SIGBUS; 5755409eb8c2SHillf Danton int anon_rmap = 0; 57564c887265SAdam Litke unsigned long size; 57574c887265SAdam Litke struct page *page; 57581e8f889bSDavid Gibson pte_t new_pte; 5759cb900f41SKirill A. Shutemov spinlock_t *ptl; 5760285b8dcaSHuang Ying unsigned long haddr = address & huge_page_mask(h); 5761c7b1850dSMike Kravetz bool new_page, new_pagecache_page = false; 5762958f32ceSLiu Shixin u32 hash = hugetlb_fault_mutex_hash(mapping, idx); 57634c887265SAdam Litke 576404f2cbe3SMel Gorman /* 576504f2cbe3SMel Gorman * Currently, we are forced to kill the process in the event the 576604f2cbe3SMel Gorman * original mapper has unmapped pages from the child due to a failed 5767c89357e2SDavid Hildenbrand * COW/unsharing. Warn that such a situation has occurred as it may not 5768c89357e2SDavid Hildenbrand * be obvious. 576904f2cbe3SMel Gorman */ 577004f2cbe3SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { 5771910154d5SGeoffrey Thomas pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n", 577204f2cbe3SMel Gorman current->pid); 5773958f32ceSLiu Shixin goto out; 577404f2cbe3SMel Gorman } 577504f2cbe3SMel Gorman 57764c887265SAdam Litke /* 5777188a3972SMike Kravetz * Use page lock to guard against racing truncation 5778188a3972SMike Kravetz * before we get page_table_lock. 57794c887265SAdam Litke */ 5780c7b1850dSMike Kravetz new_page = false; 578187bf91d3SMike Kravetz page = find_lock_page(mapping, idx); 578287bf91d3SMike Kravetz if (!page) { 5783188a3972SMike Kravetz size = i_size_read(mapping->host) >> huge_page_shift(h); 5784188a3972SMike Kravetz if (idx >= size) 5785188a3972SMike Kravetz goto out; 57867677f7fdSAxel Rasmussen /* Check for page in userfault range */ 57872ea7ff1eSPeter Xu if (userfaultfd_missing(vma)) { 57882ea7ff1eSPeter Xu /* 57892ea7ff1eSPeter Xu * Since hugetlb_no_page() was examining pte 57902ea7ff1eSPeter Xu * without pgtable lock, we need to re-test under 57912ea7ff1eSPeter Xu * lock because the pte may not be stable and could 57922ea7ff1eSPeter Xu * have changed from under us. Try to detect 57932ea7ff1eSPeter Xu * either changed or during-changing ptes and retry 57942ea7ff1eSPeter Xu * properly when needed. 57952ea7ff1eSPeter Xu * 57962ea7ff1eSPeter Xu * Note that userfaultfd is actually fine with 57972ea7ff1eSPeter Xu * false positives (e.g. caused by pte changed), 57982ea7ff1eSPeter Xu * but not wrong logical events (e.g. caused by 57992ea7ff1eSPeter Xu * reading a pte during changing). The latter can 58002ea7ff1eSPeter Xu * confuse the userspace, so the strictness is very 58012ea7ff1eSPeter Xu * much preferred. E.g., MISSING event should 58022ea7ff1eSPeter Xu * never happen on the page after UFFDIO_COPY has 58032ea7ff1eSPeter Xu * correctly installed the page and returned. 58042ea7ff1eSPeter Xu */ 58052ea7ff1eSPeter Xu if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) { 58062ea7ff1eSPeter Xu ret = 0; 58072ea7ff1eSPeter Xu goto out; 58082ea7ff1eSPeter Xu } 58092ea7ff1eSPeter Xu 58102ea7ff1eSPeter Xu return hugetlb_handle_userfault(vma, mapping, idx, flags, 58112ea7ff1eSPeter Xu haddr, address, 58127677f7fdSAxel Rasmussen VM_UFFD_MISSING); 58132ea7ff1eSPeter Xu } 58141a1aad8aSMike Kravetz 5815285b8dcaSHuang Ying page = alloc_huge_page(vma, haddr, 0); 58162fc39cecSAdam Litke if (IS_ERR(page)) { 58174643d67eSMike Kravetz /* 58184643d67eSMike Kravetz * Returning error will result in faulting task being 58194643d67eSMike Kravetz * sent SIGBUS. The hugetlb fault mutex prevents two 58204643d67eSMike Kravetz * tasks from racing to fault in the same page which 58214643d67eSMike Kravetz * could result in false unable to allocate errors. 58224643d67eSMike Kravetz * Page migration does not take the fault mutex, but 58234643d67eSMike Kravetz * does a clear then write of pte's under page table 58244643d67eSMike Kravetz * lock. Page fault code could race with migration, 58254643d67eSMike Kravetz * notice the clear pte and try to allocate a page 58264643d67eSMike Kravetz * here. Before returning error, get ptl and make 58274643d67eSMike Kravetz * sure there really is no pte entry. 58284643d67eSMike Kravetz */ 5829f9bf6c03SPeter Xu if (hugetlb_pte_stable(h, mm, ptep, old_pte)) 58302b740303SSouptick Joarder ret = vmf_error(PTR_ERR(page)); 5831f9bf6c03SPeter Xu else 5832f9bf6c03SPeter Xu ret = 0; 58336bda666aSChristoph Lameter goto out; 58346bda666aSChristoph Lameter } 583547ad8475SAndrea Arcangeli clear_huge_page(page, address, pages_per_huge_page(h)); 58360ed361deSNick Piggin __SetPageUptodate(page); 5837cb6acd01SMike Kravetz new_page = true; 5838ac9b9c66SHugh Dickins 5839f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) { 58407e1813d4SMike Kravetz int err = hugetlb_add_to_page_cache(page, mapping, idx); 58416bda666aSChristoph Lameter if (err) { 58423a5497a2SMiaohe Lin /* 58433a5497a2SMiaohe Lin * err can't be -EEXIST which implies someone 58443a5497a2SMiaohe Lin * else consumed the reservation since hugetlb 58453a5497a2SMiaohe Lin * fault mutex is held when add a hugetlb page 58463a5497a2SMiaohe Lin * to the page cache. So it's safe to call 58473a5497a2SMiaohe Lin * restore_reserve_on_error() here. 58483a5497a2SMiaohe Lin */ 58493a5497a2SMiaohe Lin restore_reserve_on_error(h, vma, haddr, page); 58506bda666aSChristoph Lameter put_page(page); 58516bda666aSChristoph Lameter goto out; 58526bda666aSChristoph Lameter } 5853c7b1850dSMike Kravetz new_pagecache_page = true; 585423be7468SMel Gorman } else { 58556bda666aSChristoph Lameter lock_page(page); 58560fe6e20bSNaoya Horiguchi if (unlikely(anon_vma_prepare(vma))) { 58570fe6e20bSNaoya Horiguchi ret = VM_FAULT_OOM; 58580fe6e20bSNaoya Horiguchi goto backout_unlocked; 585923be7468SMel Gorman } 5860409eb8c2SHillf Danton anon_rmap = 1; 58610fe6e20bSNaoya Horiguchi } 58620fe6e20bSNaoya Horiguchi } else { 586357303d80SAndy Whitcroft /* 5864998b4382SNaoya Horiguchi * If memory error occurs between mmap() and fault, some process 5865998b4382SNaoya Horiguchi * don't have hwpoisoned swap entry for errored virtual address. 5866998b4382SNaoya Horiguchi * So we need to block hugepage fault by PG_hwpoison bit check. 5867fd6a03edSNaoya Horiguchi */ 5868fd6a03edSNaoya Horiguchi if (unlikely(PageHWPoison(page))) { 58690eb98f15SMiaohe Lin ret = VM_FAULT_HWPOISON_LARGE | 5870972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 5871fd6a03edSNaoya Horiguchi goto backout_unlocked; 58726bda666aSChristoph Lameter } 58737677f7fdSAxel Rasmussen 58747677f7fdSAxel Rasmussen /* Check for page in userfault range. */ 58757677f7fdSAxel Rasmussen if (userfaultfd_minor(vma)) { 58767677f7fdSAxel Rasmussen unlock_page(page); 58777677f7fdSAxel Rasmussen put_page(page); 58782ea7ff1eSPeter Xu /* See comment in userfaultfd_missing() block above */ 58792ea7ff1eSPeter Xu if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) { 58802ea7ff1eSPeter Xu ret = 0; 58812ea7ff1eSPeter Xu goto out; 58822ea7ff1eSPeter Xu } 58832ea7ff1eSPeter Xu return hugetlb_handle_userfault(vma, mapping, idx, flags, 58842ea7ff1eSPeter Xu haddr, address, 58857677f7fdSAxel Rasmussen VM_UFFD_MINOR); 58867677f7fdSAxel Rasmussen } 5887998b4382SNaoya Horiguchi } 58881e8f889bSDavid Gibson 588957303d80SAndy Whitcroft /* 589057303d80SAndy Whitcroft * If we are going to COW a private mapping later, we examine the 589157303d80SAndy Whitcroft * pending reservations for this page now. This will ensure that 589257303d80SAndy Whitcroft * any allocations necessary to record that reservation occur outside 589357303d80SAndy Whitcroft * the spinlock. 589457303d80SAndy Whitcroft */ 58955e911373SMike Kravetz if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 5896285b8dcaSHuang Ying if (vma_needs_reservation(h, vma, haddr) < 0) { 58972b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 58982b26736cSAndy Whitcroft goto backout_unlocked; 58992b26736cSAndy Whitcroft } 59005e911373SMike Kravetz /* Just decrements count, does not deallocate */ 5901285b8dcaSHuang Ying vma_end_reservation(h, vma, haddr); 59025e911373SMike Kravetz } 590357303d80SAndy Whitcroft 59048bea8052SAneesh Kumar K.V ptl = huge_pte_lock(h, mm, ptep); 590583c54070SNick Piggin ret = 0; 5906c64e912cSPeter Xu /* If pte changed from under us, retry */ 5907c64e912cSPeter Xu if (!pte_same(huge_ptep_get(ptep), old_pte)) 59084c887265SAdam Litke goto backout; 59094c887265SAdam Litke 59104781593dSPeter Xu if (anon_rmap) 5911285b8dcaSHuang Ying hugepage_add_new_anon_rmap(page, vma, haddr); 59124781593dSPeter Xu else 5913fb3d824dSDavid Hildenbrand page_dup_file_rmap(page, true); 59141e8f889bSDavid Gibson new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) 59151e8f889bSDavid Gibson && (vma->vm_flags & VM_SHARED))); 5916c64e912cSPeter Xu /* 5917c64e912cSPeter Xu * If this pte was previously wr-protected, keep it wr-protected even 5918c64e912cSPeter Xu * if populated. 5919c64e912cSPeter Xu */ 5920c64e912cSPeter Xu if (unlikely(pte_marker_uffd_wp(old_pte))) 5921f1eb1bacSPeter Xu new_pte = huge_pte_mkuffd_wp(new_pte); 5922285b8dcaSHuang Ying set_huge_pte_at(mm, haddr, ptep, new_pte); 59231e8f889bSDavid Gibson 59245d317b2bSNaoya Horiguchi hugetlb_count_add(pages_per_huge_page(h), mm); 5925788c7df4SHugh Dickins if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 59261e8f889bSDavid Gibson /* Optimization, do the COW without a second fault */ 5927c89357e2SDavid Hildenbrand ret = hugetlb_wp(mm, vma, address, ptep, flags, page, ptl); 59281e8f889bSDavid Gibson } 59291e8f889bSDavid Gibson 5930cb900f41SKirill A. Shutemov spin_unlock(ptl); 5931cb6acd01SMike Kravetz 5932cb6acd01SMike Kravetz /* 59338f251a3dSMike Kravetz * Only set HPageMigratable in newly allocated pages. Existing pages 59348f251a3dSMike Kravetz * found in the pagecache may not have HPageMigratableset if they have 59358f251a3dSMike Kravetz * been isolated for migration. 5936cb6acd01SMike Kravetz */ 5937cb6acd01SMike Kravetz if (new_page) 59388f251a3dSMike Kravetz SetHPageMigratable(page); 5939cb6acd01SMike Kravetz 59404c887265SAdam Litke unlock_page(page); 59414c887265SAdam Litke out: 5942958f32ceSLiu Shixin hugetlb_vma_unlock_read(vma); 5943958f32ceSLiu Shixin mutex_unlock(&hugetlb_fault_mutex_table[hash]); 5944ac9b9c66SHugh Dickins return ret; 59454c887265SAdam Litke 59464c887265SAdam Litke backout: 5947cb900f41SKirill A. Shutemov spin_unlock(ptl); 59482b26736cSAndy Whitcroft backout_unlocked: 5949c7b1850dSMike Kravetz if (new_page && !new_pagecache_page) 5950285b8dcaSHuang Ying restore_reserve_on_error(h, vma, haddr, page); 5951fa27759aSMike Kravetz 5952fa27759aSMike Kravetz unlock_page(page); 59534c887265SAdam Litke put_page(page); 59544c887265SAdam Litke goto out; 5955ac9b9c66SHugh Dickins } 5956ac9b9c66SHugh Dickins 59578382d914SDavidlohr Bueso #ifdef CONFIG_SMP 5958188b04a7SWei Yang u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx) 59598382d914SDavidlohr Bueso { 59608382d914SDavidlohr Bueso unsigned long key[2]; 59618382d914SDavidlohr Bueso u32 hash; 59628382d914SDavidlohr Bueso 59638382d914SDavidlohr Bueso key[0] = (unsigned long) mapping; 59648382d914SDavidlohr Bueso key[1] = idx; 59658382d914SDavidlohr Bueso 596655254636SMike Kravetz hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0); 59678382d914SDavidlohr Bueso 59688382d914SDavidlohr Bueso return hash & (num_fault_mutexes - 1); 59698382d914SDavidlohr Bueso } 59708382d914SDavidlohr Bueso #else 59718382d914SDavidlohr Bueso /* 59726c26d310SMiaohe Lin * For uniprocessor systems we always use a single mutex, so just 59738382d914SDavidlohr Bueso * return 0 and avoid the hashing overhead. 59748382d914SDavidlohr Bueso */ 5975188b04a7SWei Yang u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx) 59768382d914SDavidlohr Bueso { 59778382d914SDavidlohr Bueso return 0; 59788382d914SDavidlohr Bueso } 59798382d914SDavidlohr Bueso #endif 59808382d914SDavidlohr Bueso 59812b740303SSouptick Joarder vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 5982788c7df4SHugh Dickins unsigned long address, unsigned int flags) 598386e5216fSAdam Litke { 59848382d914SDavidlohr Bueso pte_t *ptep, entry; 5985cb900f41SKirill A. Shutemov spinlock_t *ptl; 59862b740303SSouptick Joarder vm_fault_t ret; 59878382d914SDavidlohr Bueso u32 hash; 59888382d914SDavidlohr Bueso pgoff_t idx; 59890fe6e20bSNaoya Horiguchi struct page *page = NULL; 599057303d80SAndy Whitcroft struct page *pagecache_page = NULL; 5991a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 59928382d914SDavidlohr Bueso struct address_space *mapping; 59930f792cf9SNaoya Horiguchi int need_wait_lock = 0; 5994285b8dcaSHuang Ying unsigned long haddr = address & huge_page_mask(h); 599586e5216fSAdam Litke 59963935baa9SDavid Gibson /* 59973935baa9SDavid Gibson * Serialize hugepage allocation and instantiation, so that we don't 59983935baa9SDavid Gibson * get spurious allocation failures if two CPUs race to instantiate 59993935baa9SDavid Gibson * the same page in the page cache. 60003935baa9SDavid Gibson */ 600140549ba8SMike Kravetz mapping = vma->vm_file->f_mapping; 600240549ba8SMike Kravetz idx = vma_hugecache_offset(h, vma, haddr); 6003188b04a7SWei Yang hash = hugetlb_fault_mutex_hash(mapping, idx); 6004c672c7f2SMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 60058382d914SDavidlohr Bueso 600640549ba8SMike Kravetz /* 600740549ba8SMike Kravetz * Acquire vma lock before calling huge_pte_alloc and hold 600840549ba8SMike Kravetz * until finished with ptep. This prevents huge_pmd_unshare from 600940549ba8SMike Kravetz * being called elsewhere and making the ptep no longer valid. 601040549ba8SMike Kravetz */ 601140549ba8SMike Kravetz hugetlb_vma_lock_read(vma); 601240549ba8SMike Kravetz ptep = huge_pte_alloc(mm, vma, haddr, huge_page_size(h)); 601340549ba8SMike Kravetz if (!ptep) { 601440549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 601540549ba8SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 601640549ba8SMike Kravetz return VM_FAULT_OOM; 601740549ba8SMike Kravetz } 601840549ba8SMike Kravetz 60197f2e9525SGerald Schaefer entry = huge_ptep_get(ptep); 6020c64e912cSPeter Xu /* PTE markers should be handled the same way as none pte */ 6021958f32ceSLiu Shixin if (huge_pte_none_mostly(entry)) 6022958f32ceSLiu Shixin /* 6023958f32ceSLiu Shixin * hugetlb_no_page will drop vma lock and hugetlb fault 6024958f32ceSLiu Shixin * mutex internally, which make us return immediately. 6025958f32ceSLiu Shixin */ 6026958f32ceSLiu Shixin return hugetlb_no_page(mm, vma, mapping, idx, address, ptep, 6027c64e912cSPeter Xu entry, flags); 602886e5216fSAdam Litke 602983c54070SNick Piggin ret = 0; 60301e8f889bSDavid Gibson 603157303d80SAndy Whitcroft /* 60320f792cf9SNaoya Horiguchi * entry could be a migration/hwpoison entry at this point, so this 60330f792cf9SNaoya Horiguchi * check prevents the kernel from going below assuming that we have 60347c8de358SEthon Paul * an active hugepage in pagecache. This goto expects the 2nd page 60357c8de358SEthon Paul * fault, and is_hugetlb_entry_(migration|hwpoisoned) check will 60367c8de358SEthon Paul * properly handle it. 60370f792cf9SNaoya Horiguchi */ 6038*fcd48540SPeter Xu if (!pte_present(entry)) { 6039*fcd48540SPeter Xu if (unlikely(is_hugetlb_entry_migration(entry))) { 6040*fcd48540SPeter Xu /* 6041*fcd48540SPeter Xu * Release the hugetlb fault lock now, but retain 6042*fcd48540SPeter Xu * the vma lock, because it is needed to guard the 6043*fcd48540SPeter Xu * huge_pte_lockptr() later in 6044*fcd48540SPeter Xu * migration_entry_wait_huge(). The vma lock will 6045*fcd48540SPeter Xu * be released there. 6046*fcd48540SPeter Xu */ 6047*fcd48540SPeter Xu mutex_unlock(&hugetlb_fault_mutex_table[hash]); 6048*fcd48540SPeter Xu migration_entry_wait_huge(vma, ptep); 6049*fcd48540SPeter Xu return 0; 6050*fcd48540SPeter Xu } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) 6051*fcd48540SPeter Xu ret = VM_FAULT_HWPOISON_LARGE | 6052*fcd48540SPeter Xu VM_FAULT_SET_HINDEX(hstate_index(h)); 60530f792cf9SNaoya Horiguchi goto out_mutex; 6054*fcd48540SPeter Xu } 60550f792cf9SNaoya Horiguchi 60560f792cf9SNaoya Horiguchi /* 6057c89357e2SDavid Hildenbrand * If we are going to COW/unshare the mapping later, we examine the 6058c89357e2SDavid Hildenbrand * pending reservations for this page now. This will ensure that any 605957303d80SAndy Whitcroft * allocations necessary to record that reservation occur outside the 60601d8d1464SDavid Hildenbrand * spinlock. Also lookup the pagecache page now as it is used to 60611d8d1464SDavid Hildenbrand * determine if a reservation has been consumed. 606257303d80SAndy Whitcroft */ 6063c89357e2SDavid Hildenbrand if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && 60641d8d1464SDavid Hildenbrand !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(entry)) { 6065285b8dcaSHuang Ying if (vma_needs_reservation(h, vma, haddr) < 0) { 60662b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 6067b4d1d99fSDavid Gibson goto out_mutex; 60682b26736cSAndy Whitcroft } 60695e911373SMike Kravetz /* Just decrements count, does not deallocate */ 6070285b8dcaSHuang Ying vma_end_reservation(h, vma, haddr); 607157303d80SAndy Whitcroft 607229be8426SMiaohe Lin pagecache_page = find_lock_page(mapping, idx); 607357303d80SAndy Whitcroft } 607457303d80SAndy Whitcroft 60750f792cf9SNaoya Horiguchi ptl = huge_pte_lock(h, mm, ptep); 60760fe6e20bSNaoya Horiguchi 6077c89357e2SDavid Hildenbrand /* Check for a racing update before calling hugetlb_wp() */ 6078b4d1d99fSDavid Gibson if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) 6079cb900f41SKirill A. Shutemov goto out_ptl; 6080b4d1d99fSDavid Gibson 6081166f3eccSPeter Xu /* Handle userfault-wp first, before trying to lock more pages */ 6082166f3eccSPeter Xu if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(ptep)) && 6083166f3eccSPeter Xu (flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { 6084166f3eccSPeter Xu struct vm_fault vmf = { 6085166f3eccSPeter Xu .vma = vma, 6086166f3eccSPeter Xu .address = haddr, 6087166f3eccSPeter Xu .real_address = address, 6088166f3eccSPeter Xu .flags = flags, 6089166f3eccSPeter Xu }; 6090166f3eccSPeter Xu 6091166f3eccSPeter Xu spin_unlock(ptl); 6092166f3eccSPeter Xu if (pagecache_page) { 6093166f3eccSPeter Xu unlock_page(pagecache_page); 6094166f3eccSPeter Xu put_page(pagecache_page); 6095166f3eccSPeter Xu } 609640549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 6097166f3eccSPeter Xu mutex_unlock(&hugetlb_fault_mutex_table[hash]); 6098166f3eccSPeter Xu return handle_userfault(&vmf, VM_UFFD_WP); 6099166f3eccSPeter Xu } 6100166f3eccSPeter Xu 61010f792cf9SNaoya Horiguchi /* 6102c89357e2SDavid Hildenbrand * hugetlb_wp() requires page locks of pte_page(entry) and 61030f792cf9SNaoya Horiguchi * pagecache_page, so here we need take the former one 61040f792cf9SNaoya Horiguchi * when page != pagecache_page or !pagecache_page. 61050f792cf9SNaoya Horiguchi */ 61060f792cf9SNaoya Horiguchi page = pte_page(entry); 61070f792cf9SNaoya Horiguchi if (page != pagecache_page) 61080f792cf9SNaoya Horiguchi if (!trylock_page(page)) { 61090f792cf9SNaoya Horiguchi need_wait_lock = 1; 61100f792cf9SNaoya Horiguchi goto out_ptl; 61110f792cf9SNaoya Horiguchi } 61120f792cf9SNaoya Horiguchi 61130f792cf9SNaoya Horiguchi get_page(page); 6114b4d1d99fSDavid Gibson 6115c89357e2SDavid Hildenbrand if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { 6116106c992aSGerald Schaefer if (!huge_pte_write(entry)) { 6117c89357e2SDavid Hildenbrand ret = hugetlb_wp(mm, vma, address, ptep, flags, 6118cb900f41SKirill A. Shutemov pagecache_page, ptl); 61190f792cf9SNaoya Horiguchi goto out_put_page; 6120c89357e2SDavid Hildenbrand } else if (likely(flags & FAULT_FLAG_WRITE)) { 6121106c992aSGerald Schaefer entry = huge_pte_mkdirty(entry); 6122b4d1d99fSDavid Gibson } 6123c89357e2SDavid Hildenbrand } 6124b4d1d99fSDavid Gibson entry = pte_mkyoung(entry); 6125285b8dcaSHuang Ying if (huge_ptep_set_access_flags(vma, haddr, ptep, entry, 6126788c7df4SHugh Dickins flags & FAULT_FLAG_WRITE)) 6127285b8dcaSHuang Ying update_mmu_cache(vma, haddr, ptep); 61280f792cf9SNaoya Horiguchi out_put_page: 61290f792cf9SNaoya Horiguchi if (page != pagecache_page) 61300f792cf9SNaoya Horiguchi unlock_page(page); 61310f792cf9SNaoya Horiguchi put_page(page); 6132cb900f41SKirill A. Shutemov out_ptl: 6133cb900f41SKirill A. Shutemov spin_unlock(ptl); 613457303d80SAndy Whitcroft 613557303d80SAndy Whitcroft if (pagecache_page) { 613657303d80SAndy Whitcroft unlock_page(pagecache_page); 613757303d80SAndy Whitcroft put_page(pagecache_page); 613857303d80SAndy Whitcroft } 6139b4d1d99fSDavid Gibson out_mutex: 614040549ba8SMike Kravetz hugetlb_vma_unlock_read(vma); 6141c672c7f2SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 61420f792cf9SNaoya Horiguchi /* 61430f792cf9SNaoya Horiguchi * Generally it's safe to hold refcount during waiting page lock. But 61440f792cf9SNaoya Horiguchi * here we just wait to defer the next page fault to avoid busy loop and 61450f792cf9SNaoya Horiguchi * the page is not used after unlocked before returning from the current 61460f792cf9SNaoya Horiguchi * page fault. So we are safe from accessing freed page, even if we wait 61470f792cf9SNaoya Horiguchi * here without taking refcount. 61480f792cf9SNaoya Horiguchi */ 61490f792cf9SNaoya Horiguchi if (need_wait_lock) 61500f792cf9SNaoya Horiguchi wait_on_page_locked(page); 61511e8f889bSDavid Gibson return ret; 615286e5216fSAdam Litke } 615386e5216fSAdam Litke 6154714c1891SAxel Rasmussen #ifdef CONFIG_USERFAULTFD 61558fb5debcSMike Kravetz /* 61568fb5debcSMike Kravetz * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with 61578fb5debcSMike Kravetz * modifications for huge pages. 61588fb5debcSMike Kravetz */ 61598fb5debcSMike Kravetz int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, 61608fb5debcSMike Kravetz pte_t *dst_pte, 61618fb5debcSMike Kravetz struct vm_area_struct *dst_vma, 61628fb5debcSMike Kravetz unsigned long dst_addr, 61638fb5debcSMike Kravetz unsigned long src_addr, 6164f6191471SAxel Rasmussen enum mcopy_atomic_mode mode, 61656041c691SPeter Xu struct page **pagep, 61666041c691SPeter Xu bool wp_copy) 61678fb5debcSMike Kravetz { 6168f6191471SAxel Rasmussen bool is_continue = (mode == MCOPY_ATOMIC_CONTINUE); 61698cc5fcbbSMina Almasry struct hstate *h = hstate_vma(dst_vma); 61708cc5fcbbSMina Almasry struct address_space *mapping = dst_vma->vm_file->f_mapping; 61718cc5fcbbSMina Almasry pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr); 61721e392147SAndrea Arcangeli unsigned long size; 61731c9e8defSMike Kravetz int vm_shared = dst_vma->vm_flags & VM_SHARED; 61748fb5debcSMike Kravetz pte_t _dst_pte; 61758fb5debcSMike Kravetz spinlock_t *ptl; 61768cc5fcbbSMina Almasry int ret = -ENOMEM; 61778fb5debcSMike Kravetz struct page *page; 6178f6191471SAxel Rasmussen int writable; 6179cc30042dSMina Almasry bool page_in_pagecache = false; 61808fb5debcSMike Kravetz 6181f6191471SAxel Rasmussen if (is_continue) { 6182f6191471SAxel Rasmussen ret = -EFAULT; 6183f6191471SAxel Rasmussen page = find_lock_page(mapping, idx); 6184f6191471SAxel Rasmussen if (!page) 6185f6191471SAxel Rasmussen goto out; 6186cc30042dSMina Almasry page_in_pagecache = true; 6187f6191471SAxel Rasmussen } else if (!*pagep) { 6188d84cf06eSMina Almasry /* If a page already exists, then it's UFFDIO_COPY for 6189d84cf06eSMina Almasry * a non-missing case. Return -EEXIST. 6190d84cf06eSMina Almasry */ 6191d84cf06eSMina Almasry if (vm_shared && 6192d84cf06eSMina Almasry hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { 6193d84cf06eSMina Almasry ret = -EEXIST; 61948fb5debcSMike Kravetz goto out; 6195d84cf06eSMina Almasry } 6196d84cf06eSMina Almasry 6197d84cf06eSMina Almasry page = alloc_huge_page(dst_vma, dst_addr, 0); 6198d84cf06eSMina Almasry if (IS_ERR(page)) { 6199d84cf06eSMina Almasry ret = -ENOMEM; 6200d84cf06eSMina Almasry goto out; 6201d84cf06eSMina Almasry } 62028fb5debcSMike Kravetz 62038fb5debcSMike Kravetz ret = copy_huge_page_from_user(page, 62048fb5debcSMike Kravetz (const void __user *) src_addr, 6205810a56b9SMike Kravetz pages_per_huge_page(h), false); 62068fb5debcSMike Kravetz 6207c1e8d7c6SMichel Lespinasse /* fallback to copy_from_user outside mmap_lock */ 62088fb5debcSMike Kravetz if (unlikely(ret)) { 62099e368259SAndrea Arcangeli ret = -ENOENT; 62108cc5fcbbSMina Almasry /* Free the allocated page which may have 62118cc5fcbbSMina Almasry * consumed a reservation. 62128cc5fcbbSMina Almasry */ 62138cc5fcbbSMina Almasry restore_reserve_on_error(h, dst_vma, dst_addr, page); 62148cc5fcbbSMina Almasry put_page(page); 62158cc5fcbbSMina Almasry 62168cc5fcbbSMina Almasry /* Allocate a temporary page to hold the copied 62178cc5fcbbSMina Almasry * contents. 62188cc5fcbbSMina Almasry */ 62198cc5fcbbSMina Almasry page = alloc_huge_page_vma(h, dst_vma, dst_addr); 62208cc5fcbbSMina Almasry if (!page) { 62218cc5fcbbSMina Almasry ret = -ENOMEM; 62228cc5fcbbSMina Almasry goto out; 62238cc5fcbbSMina Almasry } 62248fb5debcSMike Kravetz *pagep = page; 62258cc5fcbbSMina Almasry /* Set the outparam pagep and return to the caller to 62268cc5fcbbSMina Almasry * copy the contents outside the lock. Don't free the 62278cc5fcbbSMina Almasry * page. 62288cc5fcbbSMina Almasry */ 62298fb5debcSMike Kravetz goto out; 62308fb5debcSMike Kravetz } 62318fb5debcSMike Kravetz } else { 62328cc5fcbbSMina Almasry if (vm_shared && 62338cc5fcbbSMina Almasry hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { 62348cc5fcbbSMina Almasry put_page(*pagep); 62358cc5fcbbSMina Almasry ret = -EEXIST; 62368cc5fcbbSMina Almasry *pagep = NULL; 62378cc5fcbbSMina Almasry goto out; 62388cc5fcbbSMina Almasry } 62398cc5fcbbSMina Almasry 62408cc5fcbbSMina Almasry page = alloc_huge_page(dst_vma, dst_addr, 0); 62418cc5fcbbSMina Almasry if (IS_ERR(page)) { 6242da9a298fSMiaohe Lin put_page(*pagep); 62438cc5fcbbSMina Almasry ret = -ENOMEM; 62448cc5fcbbSMina Almasry *pagep = NULL; 62458cc5fcbbSMina Almasry goto out; 62468cc5fcbbSMina Almasry } 624734892366SMuchun Song copy_user_huge_page(page, *pagep, dst_addr, dst_vma, 624834892366SMuchun Song pages_per_huge_page(h)); 62498cc5fcbbSMina Almasry put_page(*pagep); 62508fb5debcSMike Kravetz *pagep = NULL; 62518fb5debcSMike Kravetz } 62528fb5debcSMike Kravetz 62538fb5debcSMike Kravetz /* 62548fb5debcSMike Kravetz * The memory barrier inside __SetPageUptodate makes sure that 62558fb5debcSMike Kravetz * preceding stores to the page contents become visible before 62568fb5debcSMike Kravetz * the set_pte_at() write. 62578fb5debcSMike Kravetz */ 62588fb5debcSMike Kravetz __SetPageUptodate(page); 62598fb5debcSMike Kravetz 6260f6191471SAxel Rasmussen /* Add shared, newly allocated pages to the page cache. */ 6261f6191471SAxel Rasmussen if (vm_shared && !is_continue) { 62621e392147SAndrea Arcangeli size = i_size_read(mapping->host) >> huge_page_shift(h); 62631e392147SAndrea Arcangeli ret = -EFAULT; 62641e392147SAndrea Arcangeli if (idx >= size) 62651e392147SAndrea Arcangeli goto out_release_nounlock; 62661c9e8defSMike Kravetz 62671e392147SAndrea Arcangeli /* 62681e392147SAndrea Arcangeli * Serialization between remove_inode_hugepages() and 62697e1813d4SMike Kravetz * hugetlb_add_to_page_cache() below happens through the 62701e392147SAndrea Arcangeli * hugetlb_fault_mutex_table that here must be hold by 62711e392147SAndrea Arcangeli * the caller. 62721e392147SAndrea Arcangeli */ 62737e1813d4SMike Kravetz ret = hugetlb_add_to_page_cache(page, mapping, idx); 62741c9e8defSMike Kravetz if (ret) 62751c9e8defSMike Kravetz goto out_release_nounlock; 6276cc30042dSMina Almasry page_in_pagecache = true; 62771c9e8defSMike Kravetz } 62781c9e8defSMike Kravetz 6279bcc66543SMiaohe Lin ptl = huge_pte_lock(h, dst_mm, dst_pte); 62808fb5debcSMike Kravetz 62818625147cSJames Houghton ret = -EIO; 62828625147cSJames Houghton if (PageHWPoison(page)) 62838625147cSJames Houghton goto out_release_unlock; 62848625147cSJames Houghton 62851e392147SAndrea Arcangeli /* 62866041c691SPeter Xu * We allow to overwrite a pte marker: consider when both MISSING|WP 62876041c691SPeter Xu * registered, we firstly wr-protect a none pte which has no page cache 62886041c691SPeter Xu * page backing it, then access the page. 62896041c691SPeter Xu */ 6290fa27759aSMike Kravetz ret = -EEXIST; 62916041c691SPeter Xu if (!huge_pte_none_mostly(huge_ptep_get(dst_pte))) 62928fb5debcSMike Kravetz goto out_release_unlock; 62938fb5debcSMike Kravetz 62944781593dSPeter Xu if (page_in_pagecache) 6295fb3d824dSDavid Hildenbrand page_dup_file_rmap(page, true); 62964781593dSPeter Xu else 62978fb5debcSMike Kravetz hugepage_add_new_anon_rmap(page, dst_vma, dst_addr); 62988fb5debcSMike Kravetz 62996041c691SPeter Xu /* 63006041c691SPeter Xu * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY 63016041c691SPeter Xu * with wp flag set, don't set pte write bit. 63026041c691SPeter Xu */ 63036041c691SPeter Xu if (wp_copy || (is_continue && !vm_shared)) 6304f6191471SAxel Rasmussen writable = 0; 6305f6191471SAxel Rasmussen else 6306f6191471SAxel Rasmussen writable = dst_vma->vm_flags & VM_WRITE; 6307f6191471SAxel Rasmussen 6308f6191471SAxel Rasmussen _dst_pte = make_huge_pte(dst_vma, page, writable); 63096041c691SPeter Xu /* 63106041c691SPeter Xu * Always mark UFFDIO_COPY page dirty; note that this may not be 63116041c691SPeter Xu * extremely important for hugetlbfs for now since swapping is not 63126041c691SPeter Xu * supported, but we should still be clear in that this page cannot be 63136041c691SPeter Xu * thrown away at will, even if write bit not set. 63146041c691SPeter Xu */ 63158fb5debcSMike Kravetz _dst_pte = huge_pte_mkdirty(_dst_pte); 63168fb5debcSMike Kravetz _dst_pte = pte_mkyoung(_dst_pte); 63178fb5debcSMike Kravetz 63186041c691SPeter Xu if (wp_copy) 63196041c691SPeter Xu _dst_pte = huge_pte_mkuffd_wp(_dst_pte); 63206041c691SPeter Xu 63218fb5debcSMike Kravetz set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 63228fb5debcSMike Kravetz 63238fb5debcSMike Kravetz hugetlb_count_add(pages_per_huge_page(h), dst_mm); 63248fb5debcSMike Kravetz 63258fb5debcSMike Kravetz /* No need to invalidate - it was non-present before */ 63268fb5debcSMike Kravetz update_mmu_cache(dst_vma, dst_addr, dst_pte); 63278fb5debcSMike Kravetz 63288fb5debcSMike Kravetz spin_unlock(ptl); 6329f6191471SAxel Rasmussen if (!is_continue) 63308f251a3dSMike Kravetz SetHPageMigratable(page); 6331f6191471SAxel Rasmussen if (vm_shared || is_continue) 63321c9e8defSMike Kravetz unlock_page(page); 63338fb5debcSMike Kravetz ret = 0; 63348fb5debcSMike Kravetz out: 63358fb5debcSMike Kravetz return ret; 63368fb5debcSMike Kravetz out_release_unlock: 63378fb5debcSMike Kravetz spin_unlock(ptl); 6338f6191471SAxel Rasmussen if (vm_shared || is_continue) 63391c9e8defSMike Kravetz unlock_page(page); 63405af10dfdSAndrea Arcangeli out_release_nounlock: 6341cc30042dSMina Almasry if (!page_in_pagecache) 6342846be085SMike Kravetz restore_reserve_on_error(h, dst_vma, dst_addr, page); 63438fb5debcSMike Kravetz put_page(page); 63448fb5debcSMike Kravetz goto out; 63458fb5debcSMike Kravetz } 6346714c1891SAxel Rasmussen #endif /* CONFIG_USERFAULTFD */ 63478fb5debcSMike Kravetz 634882e5d378SJoao Martins static void record_subpages_vmas(struct page *page, struct vm_area_struct *vma, 634982e5d378SJoao Martins int refs, struct page **pages, 635082e5d378SJoao Martins struct vm_area_struct **vmas) 635182e5d378SJoao Martins { 635282e5d378SJoao Martins int nr; 635382e5d378SJoao Martins 635482e5d378SJoao Martins for (nr = 0; nr < refs; nr++) { 635582e5d378SJoao Martins if (likely(pages)) 635614455eabSCheng Li pages[nr] = nth_page(page, nr); 635782e5d378SJoao Martins if (vmas) 635882e5d378SJoao Martins vmas[nr] = vma; 635982e5d378SJoao Martins } 636082e5d378SJoao Martins } 636182e5d378SJoao Martins 636284209e87SDavid Hildenbrand static inline bool __follow_hugetlb_must_fault(struct vm_area_struct *vma, 636384209e87SDavid Hildenbrand unsigned int flags, pte_t *pte, 6364a7f22660SDavid Hildenbrand bool *unshare) 6365a7f22660SDavid Hildenbrand { 6366a7f22660SDavid Hildenbrand pte_t pteval = huge_ptep_get(pte); 6367a7f22660SDavid Hildenbrand 6368a7f22660SDavid Hildenbrand *unshare = false; 6369a7f22660SDavid Hildenbrand if (is_swap_pte(pteval)) 6370a7f22660SDavid Hildenbrand return true; 6371a7f22660SDavid Hildenbrand if (huge_pte_write(pteval)) 6372a7f22660SDavid Hildenbrand return false; 6373a7f22660SDavid Hildenbrand if (flags & FOLL_WRITE) 6374a7f22660SDavid Hildenbrand return true; 637584209e87SDavid Hildenbrand if (gup_must_unshare(vma, flags, pte_page(pteval))) { 6376a7f22660SDavid Hildenbrand *unshare = true; 6377a7f22660SDavid Hildenbrand return true; 6378a7f22660SDavid Hildenbrand } 6379a7f22660SDavid Hildenbrand return false; 6380a7f22660SDavid Hildenbrand } 6381a7f22660SDavid Hildenbrand 638257a196a5SMike Kravetz struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma, 638357a196a5SMike Kravetz unsigned long address, unsigned int flags) 638457a196a5SMike Kravetz { 638557a196a5SMike Kravetz struct hstate *h = hstate_vma(vma); 638657a196a5SMike Kravetz struct mm_struct *mm = vma->vm_mm; 638757a196a5SMike Kravetz unsigned long haddr = address & huge_page_mask(h); 638857a196a5SMike Kravetz struct page *page = NULL; 638957a196a5SMike Kravetz spinlock_t *ptl; 639057a196a5SMike Kravetz pte_t *pte, entry; 639157a196a5SMike Kravetz 639257a196a5SMike Kravetz /* 639357a196a5SMike Kravetz * FOLL_PIN is not supported for follow_page(). Ordinary GUP goes via 639457a196a5SMike Kravetz * follow_hugetlb_page(). 639557a196a5SMike Kravetz */ 639657a196a5SMike Kravetz if (WARN_ON_ONCE(flags & FOLL_PIN)) 639757a196a5SMike Kravetz return NULL; 639857a196a5SMike Kravetz 639957a196a5SMike Kravetz pte = huge_pte_offset(mm, haddr, huge_page_size(h)); 640057a196a5SMike Kravetz if (!pte) 640157a196a5SMike Kravetz return NULL; 640257a196a5SMike Kravetz 640357a196a5SMike Kravetz ptl = huge_pte_lock(h, mm, pte); 640457a196a5SMike Kravetz entry = huge_ptep_get(pte); 640557a196a5SMike Kravetz if (pte_present(entry)) { 640657a196a5SMike Kravetz page = pte_page(entry) + 640757a196a5SMike Kravetz ((address & ~huge_page_mask(h)) >> PAGE_SHIFT); 640857a196a5SMike Kravetz /* 640957a196a5SMike Kravetz * Note that page may be a sub-page, and with vmemmap 641057a196a5SMike Kravetz * optimizations the page struct may be read only. 641157a196a5SMike Kravetz * try_grab_page() will increase the ref count on the 641257a196a5SMike Kravetz * head page, so this will be OK. 641357a196a5SMike Kravetz * 6414e2ca6ba6SLinus Torvalds * try_grab_page() should always be able to get the page here, 6415e2ca6ba6SLinus Torvalds * because we hold the ptl lock and have verified pte_present(). 641657a196a5SMike Kravetz */ 6417e2ca6ba6SLinus Torvalds if (try_grab_page(page, flags)) { 641857a196a5SMike Kravetz page = NULL; 641957a196a5SMike Kravetz goto out; 642057a196a5SMike Kravetz } 642157a196a5SMike Kravetz } 642257a196a5SMike Kravetz out: 642357a196a5SMike Kravetz spin_unlock(ptl); 642457a196a5SMike Kravetz return page; 642557a196a5SMike Kravetz } 642657a196a5SMike Kravetz 642728a35716SMichel Lespinasse long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, 642863551ae0SDavid Gibson struct page **pages, struct vm_area_struct **vmas, 642928a35716SMichel Lespinasse unsigned long *position, unsigned long *nr_pages, 64304f6da934SPeter Xu long i, unsigned int flags, int *locked) 643163551ae0SDavid Gibson { 6432d5d4b0aaSChen, Kenneth W unsigned long pfn_offset; 6433d5d4b0aaSChen, Kenneth W unsigned long vaddr = *position; 643428a35716SMichel Lespinasse unsigned long remainder = *nr_pages; 6435a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 64360fa5bc40SJoao Martins int err = -EFAULT, refs; 643763551ae0SDavid Gibson 643863551ae0SDavid Gibson while (vaddr < vma->vm_end && remainder) { 643963551ae0SDavid Gibson pte_t *pte; 6440cb900f41SKirill A. Shutemov spinlock_t *ptl = NULL; 6441a7f22660SDavid Hildenbrand bool unshare = false; 64422a15efc9SHugh Dickins int absent; 644363551ae0SDavid Gibson struct page *page; 644463551ae0SDavid Gibson 64454c887265SAdam Litke /* 644602057967SDavid Rientjes * If we have a pending SIGKILL, don't keep faulting pages and 644702057967SDavid Rientjes * potentially allocating memory. 644802057967SDavid Rientjes */ 6449fa45f116SDavidlohr Bueso if (fatal_signal_pending(current)) { 645002057967SDavid Rientjes remainder = 0; 645102057967SDavid Rientjes break; 645202057967SDavid Rientjes } 645302057967SDavid Rientjes 645402057967SDavid Rientjes /* 64554c887265SAdam Litke * Some archs (sparc64, sh*) have multiple pte_ts to 64562a15efc9SHugh Dickins * each hugepage. We have to make sure we get the 64574c887265SAdam Litke * first, for the page indexing below to work. 6458cb900f41SKirill A. Shutemov * 6459cb900f41SKirill A. Shutemov * Note that page table lock is not held when pte is null. 64604c887265SAdam Litke */ 64617868a208SPunit Agrawal pte = huge_pte_offset(mm, vaddr & huge_page_mask(h), 64627868a208SPunit Agrawal huge_page_size(h)); 6463cb900f41SKirill A. Shutemov if (pte) 6464cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, pte); 64652a15efc9SHugh Dickins absent = !pte || huge_pte_none(huge_ptep_get(pte)); 646663551ae0SDavid Gibson 64672a15efc9SHugh Dickins /* 64682a15efc9SHugh Dickins * When coredumping, it suits get_dump_page if we just return 64693ae77f43SHugh Dickins * an error where there's an empty slot with no huge pagecache 64703ae77f43SHugh Dickins * to back it. This way, we avoid allocating a hugepage, and 64713ae77f43SHugh Dickins * the sparse dumpfile avoids allocating disk blocks, but its 64723ae77f43SHugh Dickins * huge holes still show up with zeroes where they need to be. 64732a15efc9SHugh Dickins */ 64743ae77f43SHugh Dickins if (absent && (flags & FOLL_DUMP) && 64753ae77f43SHugh Dickins !hugetlbfs_pagecache_present(h, vma, vaddr)) { 6476cb900f41SKirill A. Shutemov if (pte) 6477cb900f41SKirill A. Shutemov spin_unlock(ptl); 64782a15efc9SHugh Dickins remainder = 0; 64792a15efc9SHugh Dickins break; 64802a15efc9SHugh Dickins } 64812a15efc9SHugh Dickins 64829cc3a5bdSNaoya Horiguchi /* 64839cc3a5bdSNaoya Horiguchi * We need call hugetlb_fault for both hugepages under migration 64849cc3a5bdSNaoya Horiguchi * (in which case hugetlb_fault waits for the migration,) and 64859cc3a5bdSNaoya Horiguchi * hwpoisoned hugepages (in which case we need to prevent the 64869cc3a5bdSNaoya Horiguchi * caller from accessing to them.) In order to do this, we use 64879cc3a5bdSNaoya Horiguchi * here is_swap_pte instead of is_hugetlb_entry_migration and 64889cc3a5bdSNaoya Horiguchi * is_hugetlb_entry_hwpoisoned. This is because it simply covers 64899cc3a5bdSNaoya Horiguchi * both cases, and because we can't follow correct pages 64909cc3a5bdSNaoya Horiguchi * directly from any kind of swap entries. 64919cc3a5bdSNaoya Horiguchi */ 6492a7f22660SDavid Hildenbrand if (absent || 649384209e87SDavid Hildenbrand __follow_hugetlb_must_fault(vma, flags, pte, &unshare)) { 64942b740303SSouptick Joarder vm_fault_t ret; 649587ffc118SAndrea Arcangeli unsigned int fault_flags = 0; 64964c887265SAdam Litke 6497cb900f41SKirill A. Shutemov if (pte) 6498cb900f41SKirill A. Shutemov spin_unlock(ptl); 649987ffc118SAndrea Arcangeli if (flags & FOLL_WRITE) 650087ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_WRITE; 6501a7f22660SDavid Hildenbrand else if (unshare) 6502a7f22660SDavid Hildenbrand fault_flags |= FAULT_FLAG_UNSHARE; 650393c5c61dSPeter Xu if (locked) { 650471335f37SPeter Xu fault_flags |= FAULT_FLAG_ALLOW_RETRY | 650571335f37SPeter Xu FAULT_FLAG_KILLABLE; 650693c5c61dSPeter Xu if (flags & FOLL_INTERRUPTIBLE) 650793c5c61dSPeter Xu fault_flags |= FAULT_FLAG_INTERRUPTIBLE; 650893c5c61dSPeter Xu } 650987ffc118SAndrea Arcangeli if (flags & FOLL_NOWAIT) 651087ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_ALLOW_RETRY | 651187ffc118SAndrea Arcangeli FAULT_FLAG_RETRY_NOWAIT; 651287ffc118SAndrea Arcangeli if (flags & FOLL_TRIED) { 65134426e945SPeter Xu /* 65144426e945SPeter Xu * Note: FAULT_FLAG_ALLOW_RETRY and 65154426e945SPeter Xu * FAULT_FLAG_TRIED can co-exist 65164426e945SPeter Xu */ 651787ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_TRIED; 651887ffc118SAndrea Arcangeli } 651987ffc118SAndrea Arcangeli ret = hugetlb_fault(mm, vma, vaddr, fault_flags); 652087ffc118SAndrea Arcangeli if (ret & VM_FAULT_ERROR) { 65212be7cfedSDaniel Jordan err = vm_fault_to_errno(ret, flags); 65221c59827dSHugh Dickins remainder = 0; 65231c59827dSHugh Dickins break; 65241c59827dSHugh Dickins } 652587ffc118SAndrea Arcangeli if (ret & VM_FAULT_RETRY) { 65264f6da934SPeter Xu if (locked && 65271ac25013SAndrea Arcangeli !(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) 65284f6da934SPeter Xu *locked = 0; 652987ffc118SAndrea Arcangeli *nr_pages = 0; 653087ffc118SAndrea Arcangeli /* 653187ffc118SAndrea Arcangeli * VM_FAULT_RETRY must not return an 653287ffc118SAndrea Arcangeli * error, it will return zero 653387ffc118SAndrea Arcangeli * instead. 653487ffc118SAndrea Arcangeli * 653587ffc118SAndrea Arcangeli * No need to update "position" as the 653687ffc118SAndrea Arcangeli * caller will not check it after 653787ffc118SAndrea Arcangeli * *nr_pages is set to 0. 653887ffc118SAndrea Arcangeli */ 653987ffc118SAndrea Arcangeli return i; 654087ffc118SAndrea Arcangeli } 654187ffc118SAndrea Arcangeli continue; 654287ffc118SAndrea Arcangeli } 654363551ae0SDavid Gibson 6544a5516438SAndi Kleen pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; 65457f2e9525SGerald Schaefer page = pte_page(huge_ptep_get(pte)); 65468fde12caSLinus Torvalds 6547b6a2619cSDavid Hildenbrand VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) && 6548b6a2619cSDavid Hildenbrand !PageAnonExclusive(page), page); 6549b6a2619cSDavid Hildenbrand 65508fde12caSLinus Torvalds /* 6551acbfb087SZhigang Lu * If subpage information not requested, update counters 6552acbfb087SZhigang Lu * and skip the same_page loop below. 6553acbfb087SZhigang Lu */ 6554acbfb087SZhigang Lu if (!pages && !vmas && !pfn_offset && 6555acbfb087SZhigang Lu (vaddr + huge_page_size(h) < vma->vm_end) && 6556acbfb087SZhigang Lu (remainder >= pages_per_huge_page(h))) { 6557acbfb087SZhigang Lu vaddr += huge_page_size(h); 6558acbfb087SZhigang Lu remainder -= pages_per_huge_page(h); 6559acbfb087SZhigang Lu i += pages_per_huge_page(h); 6560acbfb087SZhigang Lu spin_unlock(ptl); 6561acbfb087SZhigang Lu continue; 6562acbfb087SZhigang Lu } 6563acbfb087SZhigang Lu 6564d08af0a5SJoao Martins /* vaddr may not be aligned to PAGE_SIZE */ 6565d08af0a5SJoao Martins refs = min3(pages_per_huge_page(h) - pfn_offset, remainder, 6566d08af0a5SJoao Martins (vma->vm_end - ALIGN_DOWN(vaddr, PAGE_SIZE)) >> PAGE_SHIFT); 65670fa5bc40SJoao Martins 656882e5d378SJoao Martins if (pages || vmas) 656914455eabSCheng Li record_subpages_vmas(nth_page(page, pfn_offset), 657082e5d378SJoao Martins vma, refs, 657182e5d378SJoao Martins likely(pages) ? pages + i : NULL, 657282e5d378SJoao Martins vmas ? vmas + i : NULL); 657363551ae0SDavid Gibson 657482e5d378SJoao Martins if (pages) { 65750fa5bc40SJoao Martins /* 6576822951d8SMatthew Wilcox (Oracle) * try_grab_folio() should always succeed here, 65770fa5bc40SJoao Martins * because: a) we hold the ptl lock, and b) we've just 65780fa5bc40SJoao Martins * checked that the huge page is present in the page 65790fa5bc40SJoao Martins * tables. If the huge page is present, then the tail 65800fa5bc40SJoao Martins * pages must also be present. The ptl prevents the 65810fa5bc40SJoao Martins * head page and tail pages from being rearranged in 65824003f107SLogan Gunthorpe * any way. As this is hugetlb, the pages will never 65834003f107SLogan Gunthorpe * be p2pdma or not longterm pinable. So this page 65844003f107SLogan Gunthorpe * must be available at this point, unless the page 65854003f107SLogan Gunthorpe * refcount overflowed: 65860fa5bc40SJoao Martins */ 6587822951d8SMatthew Wilcox (Oracle) if (WARN_ON_ONCE(!try_grab_folio(pages[i], refs, 65880fa5bc40SJoao Martins flags))) { 65890fa5bc40SJoao Martins spin_unlock(ptl); 65900fa5bc40SJoao Martins remainder = 0; 65910fa5bc40SJoao Martins err = -ENOMEM; 65920fa5bc40SJoao Martins break; 65930fa5bc40SJoao Martins } 6594d5d4b0aaSChen, Kenneth W } 659582e5d378SJoao Martins 659682e5d378SJoao Martins vaddr += (refs << PAGE_SHIFT); 659782e5d378SJoao Martins remainder -= refs; 659882e5d378SJoao Martins i += refs; 659982e5d378SJoao Martins 6600cb900f41SKirill A. Shutemov spin_unlock(ptl); 660163551ae0SDavid Gibson } 660228a35716SMichel Lespinasse *nr_pages = remainder; 660387ffc118SAndrea Arcangeli /* 660487ffc118SAndrea Arcangeli * setting position is actually required only if remainder is 660587ffc118SAndrea Arcangeli * not zero but it's faster not to add a "if (remainder)" 660687ffc118SAndrea Arcangeli * branch. 660787ffc118SAndrea Arcangeli */ 660863551ae0SDavid Gibson *position = vaddr; 660963551ae0SDavid Gibson 66102be7cfedSDaniel Jordan return i ? i : err; 661163551ae0SDavid Gibson } 66128f860591SZhang, Yanmin 66137da4d641SPeter Zijlstra unsigned long hugetlb_change_protection(struct vm_area_struct *vma, 66145a90d5a1SPeter Xu unsigned long address, unsigned long end, 66155a90d5a1SPeter Xu pgprot_t newprot, unsigned long cp_flags) 66168f860591SZhang, Yanmin { 66178f860591SZhang, Yanmin struct mm_struct *mm = vma->vm_mm; 66188f860591SZhang, Yanmin unsigned long start = address; 66198f860591SZhang, Yanmin pte_t *ptep; 66208f860591SZhang, Yanmin pte_t pte; 6621a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 662260dfaad6SPeter Xu unsigned long pages = 0, psize = huge_page_size(h); 6623dff11abeSMike Kravetz bool shared_pmd = false; 6624ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 6625e95a9851SMike Kravetz unsigned long last_addr_mask; 66265a90d5a1SPeter Xu bool uffd_wp = cp_flags & MM_CP_UFFD_WP; 66275a90d5a1SPeter Xu bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; 6628dff11abeSMike Kravetz 6629dff11abeSMike Kravetz /* 6630dff11abeSMike Kravetz * In the case of shared PMDs, the area to flush could be beyond 6631ac46d4f3SJérôme Glisse * start/end. Set range.start/range.end to cover the maximum possible 6632dff11abeSMike Kravetz * range if PMD sharing is possible. 6633dff11abeSMike Kravetz */ 66347269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 66357269f999SJérôme Glisse 0, vma, mm, start, end); 6636ac46d4f3SJérôme Glisse adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); 66378f860591SZhang, Yanmin 66388f860591SZhang, Yanmin BUG_ON(address >= end); 6639ac46d4f3SJérôme Glisse flush_cache_range(vma, range.start, range.end); 66408f860591SZhang, Yanmin 6641ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 664240549ba8SMike Kravetz hugetlb_vma_lock_write(vma); 664383cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 664440549ba8SMike Kravetz last_addr_mask = hugetlb_mask_last_page(h); 664560dfaad6SPeter Xu for (; address < end; address += psize) { 6646cb900f41SKirill A. Shutemov spinlock_t *ptl; 664760dfaad6SPeter Xu ptep = huge_pte_offset(mm, address, psize); 6648e95a9851SMike Kravetz if (!ptep) { 6649fed15f13SPeter Xu if (!uffd_wp) { 6650e95a9851SMike Kravetz address |= last_addr_mask; 66518f860591SZhang, Yanmin continue; 6652e95a9851SMike Kravetz } 6653fed15f13SPeter Xu /* 6654fed15f13SPeter Xu * Userfaultfd wr-protect requires pgtable 6655fed15f13SPeter Xu * pre-allocations to install pte markers. 6656fed15f13SPeter Xu */ 6657fed15f13SPeter Xu ptep = huge_pte_alloc(mm, vma, address, psize); 6658fed15f13SPeter Xu if (!ptep) 6659fed15f13SPeter Xu break; 6660fed15f13SPeter Xu } 6661cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 66624ddb4d91SMike Kravetz if (huge_pmd_unshare(mm, vma, address, ptep)) { 666360dfaad6SPeter Xu /* 666460dfaad6SPeter Xu * When uffd-wp is enabled on the vma, unshare 666560dfaad6SPeter Xu * shouldn't happen at all. Warn about it if it 666660dfaad6SPeter Xu * happened due to some reason. 666760dfaad6SPeter Xu */ 666860dfaad6SPeter Xu WARN_ON_ONCE(uffd_wp || uffd_wp_resolve); 66697da4d641SPeter Zijlstra pages++; 6670cb900f41SKirill A. Shutemov spin_unlock(ptl); 6671dff11abeSMike Kravetz shared_pmd = true; 66724ddb4d91SMike Kravetz address |= last_addr_mask; 667339dde65cSChen, Kenneth W continue; 66747da4d641SPeter Zijlstra } 6675a8bda28dSNaoya Horiguchi pte = huge_ptep_get(ptep); 6676a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { 66770e678153SDavid Hildenbrand /* Nothing to do. */ 66780e678153SDavid Hildenbrand } else if (unlikely(is_hugetlb_entry_migration(pte))) { 6679a8bda28dSNaoya Horiguchi swp_entry_t entry = pte_to_swp_entry(pte); 66806c287605SDavid Hildenbrand struct page *page = pfn_swap_entry_to_page(entry); 668144f86392SDavid Hildenbrand pte_t newpte = pte; 6682a8bda28dSNaoya Horiguchi 668344f86392SDavid Hildenbrand if (is_writable_migration_entry(entry)) { 66846c287605SDavid Hildenbrand if (PageAnon(page)) 66856c287605SDavid Hildenbrand entry = make_readable_exclusive_migration_entry( 66866c287605SDavid Hildenbrand swp_offset(entry)); 66876c287605SDavid Hildenbrand else 66884dd845b5SAlistair Popple entry = make_readable_migration_entry( 66894dd845b5SAlistair Popple swp_offset(entry)); 6690a8bda28dSNaoya Horiguchi newpte = swp_entry_to_pte(entry); 669144f86392SDavid Hildenbrand pages++; 669244f86392SDavid Hildenbrand } 669344f86392SDavid Hildenbrand 66945a90d5a1SPeter Xu if (uffd_wp) 66955a90d5a1SPeter Xu newpte = pte_swp_mkuffd_wp(newpte); 66965a90d5a1SPeter Xu else if (uffd_wp_resolve) 66975a90d5a1SPeter Xu newpte = pte_swp_clear_uffd_wp(newpte); 669844f86392SDavid Hildenbrand if (!pte_same(pte, newpte)) 669918f39629SQi Zheng set_huge_pte_at(mm, address, ptep, newpte); 67000e678153SDavid Hildenbrand } else if (unlikely(is_pte_marker(pte))) { 67010e678153SDavid Hildenbrand /* No other markers apply for now. */ 67020e678153SDavid Hildenbrand WARN_ON_ONCE(!pte_marker_uffd_wp(pte)); 670360dfaad6SPeter Xu if (uffd_wp_resolve) 67040e678153SDavid Hildenbrand /* Safe to modify directly (non-present->none). */ 670560dfaad6SPeter Xu huge_pte_clear(mm, address, ptep, psize); 67060e678153SDavid Hildenbrand } else if (!huge_pte_none(pte)) { 6707023bdd00SAneesh Kumar K.V pte_t old_pte; 670879c1c594SChristophe Leroy unsigned int shift = huge_page_shift(hstate_vma(vma)); 6709023bdd00SAneesh Kumar K.V 6710023bdd00SAneesh Kumar K.V old_pte = huge_ptep_modify_prot_start(vma, address, ptep); 671116785bd7SAnshuman Khandual pte = huge_pte_modify(old_pte, newprot); 671279c1c594SChristophe Leroy pte = arch_make_huge_pte(pte, shift, vma->vm_flags); 67135a90d5a1SPeter Xu if (uffd_wp) 6714f1eb1bacSPeter Xu pte = huge_pte_mkuffd_wp(pte); 67155a90d5a1SPeter Xu else if (uffd_wp_resolve) 67165a90d5a1SPeter Xu pte = huge_pte_clear_uffd_wp(pte); 6717023bdd00SAneesh Kumar K.V huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte); 67187da4d641SPeter Zijlstra pages++; 671960dfaad6SPeter Xu } else { 672060dfaad6SPeter Xu /* None pte */ 672160dfaad6SPeter Xu if (unlikely(uffd_wp)) 672260dfaad6SPeter Xu /* Safe to modify directly (none->non-present). */ 672360dfaad6SPeter Xu set_huge_pte_at(mm, address, ptep, 672460dfaad6SPeter Xu make_pte_marker(PTE_MARKER_UFFD_WP)); 67258f860591SZhang, Yanmin } 6726cb900f41SKirill A. Shutemov spin_unlock(ptl); 67278f860591SZhang, Yanmin } 6728d833352aSMel Gorman /* 6729c8c06efaSDavidlohr Bueso * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare 6730d833352aSMel Gorman * may have cleared our pud entry and done put_page on the page table: 6731c8c06efaSDavidlohr Bueso * once we release i_mmap_rwsem, another task can do the final put_page 6732dff11abeSMike Kravetz * and that page table be reused and filled with junk. If we actually 6733dff11abeSMike Kravetz * did unshare a page of pmds, flush the range corresponding to the pud. 6734d833352aSMel Gorman */ 6735dff11abeSMike Kravetz if (shared_pmd) 6736ac46d4f3SJérôme Glisse flush_hugetlb_tlb_range(vma, range.start, range.end); 6737dff11abeSMike Kravetz else 67385491ae7bSAneesh Kumar K.V flush_hugetlb_tlb_range(vma, start, end); 67390f10851eSJérôme Glisse /* 67400f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() we are downgrading 67410f10851eSJérôme Glisse * page table protection not changing it to point to a new page. 67420f10851eSJérôme Glisse * 6743ee65728eSMike Rapoport * See Documentation/mm/mmu_notifier.rst 67440f10851eSJérôme Glisse */ 674583cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 674640549ba8SMike Kravetz hugetlb_vma_unlock_write(vma); 6747ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 67487da4d641SPeter Zijlstra 67497da4d641SPeter Zijlstra return pages << h->order; 67508f860591SZhang, Yanmin } 67518f860591SZhang, Yanmin 675233b8f84aSMike Kravetz /* Return true if reservation was successful, false otherwise. */ 675333b8f84aSMike Kravetz bool hugetlb_reserve_pages(struct inode *inode, 6754a1e78772SMel Gorman long from, long to, 67555a6fe125SMel Gorman struct vm_area_struct *vma, 6756ca16d140SKOSAKI Motohiro vm_flags_t vm_flags) 6757e4e574b7SAdam Litke { 675833b8f84aSMike Kravetz long chg, add = -1; 6759a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 676090481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 67619119a41eSJoonsoo Kim struct resv_map *resv_map; 6762075a61d0SMina Almasry struct hugetlb_cgroup *h_cg = NULL; 67630db9d74eSMina Almasry long gbl_reserve, regions_needed = 0; 6764e4e574b7SAdam Litke 676563489f8eSMike Kravetz /* This should never happen */ 676663489f8eSMike Kravetz if (from > to) { 676763489f8eSMike Kravetz VM_WARN(1, "%s called with a negative range\n", __func__); 676833b8f84aSMike Kravetz return false; 676963489f8eSMike Kravetz } 677063489f8eSMike Kravetz 6771a1e78772SMel Gorman /* 6772e700898fSMike Kravetz * vma specific semaphore used for pmd sharing and fault/truncation 6773e700898fSMike Kravetz * synchronization 67748d9bfb26SMike Kravetz */ 67758d9bfb26SMike Kravetz hugetlb_vma_lock_alloc(vma); 67768d9bfb26SMike Kravetz 67778d9bfb26SMike Kravetz /* 677817c9d12eSMel Gorman * Only apply hugepage reservation if asked. At fault time, an 677917c9d12eSMel Gorman * attempt will be made for VM_NORESERVE to allocate a page 678090481622SDavid Gibson * without using reserves 678117c9d12eSMel Gorman */ 6782ca16d140SKOSAKI Motohiro if (vm_flags & VM_NORESERVE) 678333b8f84aSMike Kravetz return true; 678417c9d12eSMel Gorman 678517c9d12eSMel Gorman /* 6786a1e78772SMel Gorman * Shared mappings base their reservation on the number of pages that 6787a1e78772SMel Gorman * are already allocated on behalf of the file. Private mappings need 6788a1e78772SMel Gorman * to reserve the full area even if read-only as mprotect() may be 6789a1e78772SMel Gorman * called to make the mapping read-write. Assume !vma is a shm mapping 6790a1e78772SMel Gorman */ 67919119a41eSJoonsoo Kim if (!vma || vma->vm_flags & VM_MAYSHARE) { 6792f27a5136SMike Kravetz /* 6793f27a5136SMike Kravetz * resv_map can not be NULL as hugetlb_reserve_pages is only 6794f27a5136SMike Kravetz * called for inodes for which resv_maps were created (see 6795f27a5136SMike Kravetz * hugetlbfs_get_inode). 6796f27a5136SMike Kravetz */ 67974e35f483SJoonsoo Kim resv_map = inode_resv_map(inode); 67989119a41eSJoonsoo Kim 67990db9d74eSMina Almasry chg = region_chg(resv_map, from, to, ®ions_needed); 68009119a41eSJoonsoo Kim } else { 6801e9fe92aeSMina Almasry /* Private mapping. */ 68029119a41eSJoonsoo Kim resv_map = resv_map_alloc(); 68035a6fe125SMel Gorman if (!resv_map) 68048d9bfb26SMike Kravetz goto out_err; 68055a6fe125SMel Gorman 680617c9d12eSMel Gorman chg = to - from; 680717c9d12eSMel Gorman 68085a6fe125SMel Gorman set_vma_resv_map(vma, resv_map); 68095a6fe125SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_OWNER); 68105a6fe125SMel Gorman } 68115a6fe125SMel Gorman 681233b8f84aSMike Kravetz if (chg < 0) 6813c50ac050SDave Hansen goto out_err; 681417c9d12eSMel Gorman 681533b8f84aSMike Kravetz if (hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h), 681633b8f84aSMike Kravetz chg * pages_per_huge_page(h), &h_cg) < 0) 6817075a61d0SMina Almasry goto out_err; 6818075a61d0SMina Almasry 6819075a61d0SMina Almasry if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) { 6820075a61d0SMina Almasry /* For private mappings, the hugetlb_cgroup uncharge info hangs 6821075a61d0SMina Almasry * of the resv_map. 6822075a61d0SMina Almasry */ 6823075a61d0SMina Almasry resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h); 6824075a61d0SMina Almasry } 6825075a61d0SMina Almasry 68261c5ecae3SMike Kravetz /* 68271c5ecae3SMike Kravetz * There must be enough pages in the subpool for the mapping. If 68281c5ecae3SMike Kravetz * the subpool has a minimum size, there may be some global 68291c5ecae3SMike Kravetz * reservations already in place (gbl_reserve). 68301c5ecae3SMike Kravetz */ 68311c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_get_pages(spool, chg); 683233b8f84aSMike Kravetz if (gbl_reserve < 0) 6833075a61d0SMina Almasry goto out_uncharge_cgroup; 683417c9d12eSMel Gorman 683517c9d12eSMel Gorman /* 683617c9d12eSMel Gorman * Check enough hugepages are available for the reservation. 683790481622SDavid Gibson * Hand the pages back to the subpool if there are not 683817c9d12eSMel Gorman */ 683933b8f84aSMike Kravetz if (hugetlb_acct_memory(h, gbl_reserve) < 0) 6840075a61d0SMina Almasry goto out_put_pages; 684117c9d12eSMel Gorman 684217c9d12eSMel Gorman /* 684317c9d12eSMel Gorman * Account for the reservations made. Shared mappings record regions 684417c9d12eSMel Gorman * that have reservations as they are shared by multiple VMAs. 684517c9d12eSMel Gorman * When the last VMA disappears, the region map says how much 684617c9d12eSMel Gorman * the reservation was and the page cache tells how much of 684717c9d12eSMel Gorman * the reservation was consumed. Private mappings are per-VMA and 684817c9d12eSMel Gorman * only the consumed reservations are tracked. When the VMA 684917c9d12eSMel Gorman * disappears, the original reservation is the VMA size and the 685017c9d12eSMel Gorman * consumed reservations are stored in the map. Hence, nothing 685117c9d12eSMel Gorman * else has to be done for private mappings here 685217c9d12eSMel Gorman */ 685333039678SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) { 6854075a61d0SMina Almasry add = region_add(resv_map, from, to, regions_needed, h, h_cg); 685533039678SMike Kravetz 68560db9d74eSMina Almasry if (unlikely(add < 0)) { 68570db9d74eSMina Almasry hugetlb_acct_memory(h, -gbl_reserve); 6858075a61d0SMina Almasry goto out_put_pages; 68590db9d74eSMina Almasry } else if (unlikely(chg > add)) { 686033039678SMike Kravetz /* 686133039678SMike Kravetz * pages in this range were added to the reserve 686233039678SMike Kravetz * map between region_chg and region_add. This 686333039678SMike Kravetz * indicates a race with alloc_huge_page. Adjust 686433039678SMike Kravetz * the subpool and reserve counts modified above 686533039678SMike Kravetz * based on the difference. 686633039678SMike Kravetz */ 686733039678SMike Kravetz long rsv_adjust; 686833039678SMike Kravetz 6869d85aecf2SMiaohe Lin /* 6870d85aecf2SMiaohe Lin * hugetlb_cgroup_uncharge_cgroup_rsvd() will put the 6871d85aecf2SMiaohe Lin * reference to h_cg->css. See comment below for detail. 6872d85aecf2SMiaohe Lin */ 6873075a61d0SMina Almasry hugetlb_cgroup_uncharge_cgroup_rsvd( 6874075a61d0SMina Almasry hstate_index(h), 6875075a61d0SMina Almasry (chg - add) * pages_per_huge_page(h), h_cg); 6876075a61d0SMina Almasry 687733039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 687833039678SMike Kravetz chg - add); 687933039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 6880d85aecf2SMiaohe Lin } else if (h_cg) { 6881d85aecf2SMiaohe Lin /* 6882d85aecf2SMiaohe Lin * The file_regions will hold their own reference to 6883d85aecf2SMiaohe Lin * h_cg->css. So we should release the reference held 6884d85aecf2SMiaohe Lin * via hugetlb_cgroup_charge_cgroup_rsvd() when we are 6885d85aecf2SMiaohe Lin * done. 6886d85aecf2SMiaohe Lin */ 6887d85aecf2SMiaohe Lin hugetlb_cgroup_put_rsvd_cgroup(h_cg); 688833039678SMike Kravetz } 688933039678SMike Kravetz } 689033b8f84aSMike Kravetz return true; 689133b8f84aSMike Kravetz 6892075a61d0SMina Almasry out_put_pages: 6893075a61d0SMina Almasry /* put back original number of pages, chg */ 6894075a61d0SMina Almasry (void)hugepage_subpool_put_pages(spool, chg); 6895075a61d0SMina Almasry out_uncharge_cgroup: 6896075a61d0SMina Almasry hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h), 6897075a61d0SMina Almasry chg * pages_per_huge_page(h), h_cg); 6898c50ac050SDave Hansen out_err: 68998d9bfb26SMike Kravetz hugetlb_vma_lock_free(vma); 69005e911373SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) 69010db9d74eSMina Almasry /* Only call region_abort if the region_chg succeeded but the 69020db9d74eSMina Almasry * region_add failed or didn't run. 69030db9d74eSMina Almasry */ 69040db9d74eSMina Almasry if (chg >= 0 && add < 0) 69050db9d74eSMina Almasry region_abort(resv_map, from, to, regions_needed); 6906f031dd27SJoonsoo Kim if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 6907f031dd27SJoonsoo Kim kref_put(&resv_map->refs, resv_map_release); 690833b8f84aSMike Kravetz return false; 6909a43a8c39SChen, Kenneth W } 6910a43a8c39SChen, Kenneth W 6911b5cec28dSMike Kravetz long hugetlb_unreserve_pages(struct inode *inode, long start, long end, 6912b5cec28dSMike Kravetz long freed) 6913a43a8c39SChen, Kenneth W { 6914a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 69154e35f483SJoonsoo Kim struct resv_map *resv_map = inode_resv_map(inode); 69169119a41eSJoonsoo Kim long chg = 0; 691790481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 69181c5ecae3SMike Kravetz long gbl_reserve; 691945c682a6SKen Chen 6920f27a5136SMike Kravetz /* 6921f27a5136SMike Kravetz * Since this routine can be called in the evict inode path for all 6922f27a5136SMike Kravetz * hugetlbfs inodes, resv_map could be NULL. 6923f27a5136SMike Kravetz */ 6924b5cec28dSMike Kravetz if (resv_map) { 6925b5cec28dSMike Kravetz chg = region_del(resv_map, start, end); 6926b5cec28dSMike Kravetz /* 6927b5cec28dSMike Kravetz * region_del() can fail in the rare case where a region 6928b5cec28dSMike Kravetz * must be split and another region descriptor can not be 6929b5cec28dSMike Kravetz * allocated. If end == LONG_MAX, it will not fail. 6930b5cec28dSMike Kravetz */ 6931b5cec28dSMike Kravetz if (chg < 0) 6932b5cec28dSMike Kravetz return chg; 6933b5cec28dSMike Kravetz } 6934b5cec28dSMike Kravetz 693545c682a6SKen Chen spin_lock(&inode->i_lock); 6936e4c6f8beSEric Sandeen inode->i_blocks -= (blocks_per_huge_page(h) * freed); 693745c682a6SKen Chen spin_unlock(&inode->i_lock); 693845c682a6SKen Chen 69391c5ecae3SMike Kravetz /* 69401c5ecae3SMike Kravetz * If the subpool has a minimum size, the number of global 69411c5ecae3SMike Kravetz * reservations to be released may be adjusted. 6942dddf31a4SMiaohe Lin * 6943dddf31a4SMiaohe Lin * Note that !resv_map implies freed == 0. So (chg - freed) 6944dddf31a4SMiaohe Lin * won't go negative. 69451c5ecae3SMike Kravetz */ 69461c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); 69471c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 6948b5cec28dSMike Kravetz 6949b5cec28dSMike Kravetz return 0; 6950a43a8c39SChen, Kenneth W } 695193f70f90SNaoya Horiguchi 69523212b535SSteve Capper #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE 69533212b535SSteve Capper static unsigned long page_table_shareable(struct vm_area_struct *svma, 69543212b535SSteve Capper struct vm_area_struct *vma, 69553212b535SSteve Capper unsigned long addr, pgoff_t idx) 69563212b535SSteve Capper { 69573212b535SSteve Capper unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + 69583212b535SSteve Capper svma->vm_start; 69593212b535SSteve Capper unsigned long sbase = saddr & PUD_MASK; 69603212b535SSteve Capper unsigned long s_end = sbase + PUD_SIZE; 69613212b535SSteve Capper 69623212b535SSteve Capper /* Allow segments to share if only one is marked locked */ 6963de60f5f1SEric B Munson unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK; 6964de60f5f1SEric B Munson unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK; 69653212b535SSteve Capper 69663212b535SSteve Capper /* 69673212b535SSteve Capper * match the virtual addresses, permission and the alignment of the 69683212b535SSteve Capper * page table page. 6969131a79b4SMike Kravetz * 6970131a79b4SMike Kravetz * Also, vma_lock (vm_private_data) is required for sharing. 69713212b535SSteve Capper */ 69723212b535SSteve Capper if (pmd_index(addr) != pmd_index(saddr) || 69733212b535SSteve Capper vm_flags != svm_flags || 6974131a79b4SMike Kravetz !range_in_vma(svma, sbase, s_end) || 6975131a79b4SMike Kravetz !svma->vm_private_data) 69763212b535SSteve Capper return 0; 69773212b535SSteve Capper 69783212b535SSteve Capper return saddr; 69793212b535SSteve Capper } 69803212b535SSteve Capper 6981bbff39ccSMike Kravetz bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr) 69823212b535SSteve Capper { 6983bbff39ccSMike Kravetz unsigned long start = addr & PUD_MASK; 6984bbff39ccSMike Kravetz unsigned long end = start + PUD_SIZE; 6985bbff39ccSMike Kravetz 69868d9bfb26SMike Kravetz #ifdef CONFIG_USERFAULTFD 69878d9bfb26SMike Kravetz if (uffd_disable_huge_pmd_share(vma)) 69888d9bfb26SMike Kravetz return false; 69898d9bfb26SMike Kravetz #endif 69903212b535SSteve Capper /* 69913212b535SSteve Capper * check on proper vm_flags and page table alignment 69923212b535SSteve Capper */ 69938d9bfb26SMike Kravetz if (!(vma->vm_flags & VM_MAYSHARE)) 699431aafb45SNicholas Krause return false; 6995bbff39ccSMike Kravetz if (!vma->vm_private_data) /* vma lock required for sharing */ 69968d9bfb26SMike Kravetz return false; 69978d9bfb26SMike Kravetz if (!range_in_vma(vma, start, end)) 69988d9bfb26SMike Kravetz return false; 69998d9bfb26SMike Kravetz return true; 70008d9bfb26SMike Kravetz } 70018d9bfb26SMike Kravetz 70023212b535SSteve Capper /* 7003017b1660SMike Kravetz * Determine if start,end range within vma could be mapped by shared pmd. 7004017b1660SMike Kravetz * If yes, adjust start and end to cover range associated with possible 7005017b1660SMike Kravetz * shared pmd mappings. 7006017b1660SMike Kravetz */ 7007017b1660SMike Kravetz void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, 7008017b1660SMike Kravetz unsigned long *start, unsigned long *end) 7009017b1660SMike Kravetz { 7010a1ba9da8SLi Xinhai unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE), 7011a1ba9da8SLi Xinhai v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE); 7012017b1660SMike Kravetz 7013a1ba9da8SLi Xinhai /* 7014f0953a1bSIngo Molnar * vma needs to span at least one aligned PUD size, and the range 7015f0953a1bSIngo Molnar * must be at least partially within in. 7016a1ba9da8SLi Xinhai */ 7017a1ba9da8SLi Xinhai if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) || 7018a1ba9da8SLi Xinhai (*end <= v_start) || (*start >= v_end)) 7019017b1660SMike Kravetz return; 7020017b1660SMike Kravetz 702175802ca6SPeter Xu /* Extend the range to be PUD aligned for a worst case scenario */ 7022a1ba9da8SLi Xinhai if (*start > v_start) 7023a1ba9da8SLi Xinhai *start = ALIGN_DOWN(*start, PUD_SIZE); 7024017b1660SMike Kravetz 7025a1ba9da8SLi Xinhai if (*end < v_end) 7026a1ba9da8SLi Xinhai *end = ALIGN(*end, PUD_SIZE); 7027017b1660SMike Kravetz } 7028017b1660SMike Kravetz 7029017b1660SMike Kravetz /* 70303212b535SSteve Capper * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() 70313212b535SSteve Capper * and returns the corresponding pte. While this is not necessary for the 70323212b535SSteve Capper * !shared pmd case because we can allocate the pmd later as well, it makes the 70333a47c54fSMike Kravetz * code much cleaner. pmd allocation is essential for the shared case because 70343a47c54fSMike Kravetz * pud has to be populated inside the same i_mmap_rwsem section - otherwise 70353a47c54fSMike Kravetz * racing tasks could either miss the sharing (see huge_pte_offset) or select a 70363a47c54fSMike Kravetz * bad pmd for sharing. 70373212b535SSteve Capper */ 7038aec44e0fSPeter Xu pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, 7039aec44e0fSPeter Xu unsigned long addr, pud_t *pud) 70403212b535SSteve Capper { 70413212b535SSteve Capper struct address_space *mapping = vma->vm_file->f_mapping; 70423212b535SSteve Capper pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + 70433212b535SSteve Capper vma->vm_pgoff; 70443212b535SSteve Capper struct vm_area_struct *svma; 70453212b535SSteve Capper unsigned long saddr; 70463212b535SSteve Capper pte_t *spte = NULL; 70473212b535SSteve Capper pte_t *pte; 7048cb900f41SKirill A. Shutemov spinlock_t *ptl; 70493212b535SSteve Capper 70503a47c54fSMike Kravetz i_mmap_lock_read(mapping); 70513212b535SSteve Capper vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { 70523212b535SSteve Capper if (svma == vma) 70533212b535SSteve Capper continue; 70543212b535SSteve Capper 70553212b535SSteve Capper saddr = page_table_shareable(svma, vma, addr, idx); 70563212b535SSteve Capper if (saddr) { 70577868a208SPunit Agrawal spte = huge_pte_offset(svma->vm_mm, saddr, 70587868a208SPunit Agrawal vma_mmu_pagesize(svma)); 70593212b535SSteve Capper if (spte) { 70603212b535SSteve Capper get_page(virt_to_page(spte)); 70613212b535SSteve Capper break; 70623212b535SSteve Capper } 70633212b535SSteve Capper } 70643212b535SSteve Capper } 70653212b535SSteve Capper 70663212b535SSteve Capper if (!spte) 70673212b535SSteve Capper goto out; 70683212b535SSteve Capper 70698bea8052SAneesh Kumar K.V ptl = huge_pte_lock(hstate_vma(vma), mm, spte); 7070dc6c9a35SKirill A. Shutemov if (pud_none(*pud)) { 70713212b535SSteve Capper pud_populate(mm, pud, 70723212b535SSteve Capper (pmd_t *)((unsigned long)spte & PAGE_MASK)); 7073c17b1f42SKirill A. Shutemov mm_inc_nr_pmds(mm); 7074dc6c9a35SKirill A. Shutemov } else { 70753212b535SSteve Capper put_page(virt_to_page(spte)); 7076dc6c9a35SKirill A. Shutemov } 7077cb900f41SKirill A. Shutemov spin_unlock(ptl); 70783212b535SSteve Capper out: 70793212b535SSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 70803a47c54fSMike Kravetz i_mmap_unlock_read(mapping); 70813212b535SSteve Capper return pte; 70823212b535SSteve Capper } 70833212b535SSteve Capper 70843212b535SSteve Capper /* 70853212b535SSteve Capper * unmap huge page backed by shared pte. 70863212b535SSteve Capper * 70873212b535SSteve Capper * Hugetlb pte page is ref counted at the time of mapping. If pte is shared 70883212b535SSteve Capper * indicated by page_count > 1, unmap is achieved by clearing pud and 70893212b535SSteve Capper * decrementing the ref count. If count == 1, the pte page is not shared. 70903212b535SSteve Capper * 70913a47c54fSMike Kravetz * Called with page table lock held. 70923212b535SSteve Capper * 70933212b535SSteve Capper * returns: 1 successfully unmapped a shared pte page 70943212b535SSteve Capper * 0 the underlying pte page is not shared, or it is the last user 70953212b535SSteve Capper */ 709634ae204fSMike Kravetz int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma, 70974ddb4d91SMike Kravetz unsigned long addr, pte_t *ptep) 70983212b535SSteve Capper { 70994ddb4d91SMike Kravetz pgd_t *pgd = pgd_offset(mm, addr); 71004ddb4d91SMike Kravetz p4d_t *p4d = p4d_offset(pgd, addr); 71014ddb4d91SMike Kravetz pud_t *pud = pud_offset(p4d, addr); 71023212b535SSteve Capper 710334ae204fSMike Kravetz i_mmap_assert_write_locked(vma->vm_file->f_mapping); 710440549ba8SMike Kravetz hugetlb_vma_assert_locked(vma); 71053212b535SSteve Capper BUG_ON(page_count(virt_to_page(ptep)) == 0); 71063212b535SSteve Capper if (page_count(virt_to_page(ptep)) == 1) 71073212b535SSteve Capper return 0; 71083212b535SSteve Capper 71093212b535SSteve Capper pud_clear(pud); 71103212b535SSteve Capper put_page(virt_to_page(ptep)); 7111dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(mm); 71123212b535SSteve Capper return 1; 71133212b535SSteve Capper } 7114c1991e07SPeter Xu 71159e5fc74cSSteve Capper #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 71168d9bfb26SMike Kravetz 7117aec44e0fSPeter Xu pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, 7118aec44e0fSPeter Xu unsigned long addr, pud_t *pud) 71199e5fc74cSSteve Capper { 71209e5fc74cSSteve Capper return NULL; 71219e5fc74cSSteve Capper } 7122e81f2d22SZhang Zhen 712334ae204fSMike Kravetz int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma, 71244ddb4d91SMike Kravetz unsigned long addr, pte_t *ptep) 7125e81f2d22SZhang Zhen { 7126e81f2d22SZhang Zhen return 0; 7127e81f2d22SZhang Zhen } 7128017b1660SMike Kravetz 7129017b1660SMike Kravetz void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, 7130017b1660SMike Kravetz unsigned long *start, unsigned long *end) 7131017b1660SMike Kravetz { 7132017b1660SMike Kravetz } 7133c1991e07SPeter Xu 7134c1991e07SPeter Xu bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr) 7135c1991e07SPeter Xu { 7136c1991e07SPeter Xu return false; 7137c1991e07SPeter Xu } 71383212b535SSteve Capper #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 71393212b535SSteve Capper 71409e5fc74cSSteve Capper #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 7141aec44e0fSPeter Xu pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, 71429e5fc74cSSteve Capper unsigned long addr, unsigned long sz) 71439e5fc74cSSteve Capper { 71449e5fc74cSSteve Capper pgd_t *pgd; 7145c2febafcSKirill A. Shutemov p4d_t *p4d; 71469e5fc74cSSteve Capper pud_t *pud; 71479e5fc74cSSteve Capper pte_t *pte = NULL; 71489e5fc74cSSteve Capper 71499e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 7150f4f0a3d8SKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 7151f4f0a3d8SKirill A. Shutemov if (!p4d) 7152f4f0a3d8SKirill A. Shutemov return NULL; 7153c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 71549e5fc74cSSteve Capper if (pud) { 71559e5fc74cSSteve Capper if (sz == PUD_SIZE) { 71569e5fc74cSSteve Capper pte = (pte_t *)pud; 71579e5fc74cSSteve Capper } else { 71589e5fc74cSSteve Capper BUG_ON(sz != PMD_SIZE); 7159c1991e07SPeter Xu if (want_pmd_share(vma, addr) && pud_none(*pud)) 7160aec44e0fSPeter Xu pte = huge_pmd_share(mm, vma, addr, pud); 71619e5fc74cSSteve Capper else 71629e5fc74cSSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 71639e5fc74cSSteve Capper } 71649e5fc74cSSteve Capper } 71654e666314SMichal Hocko BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte)); 71669e5fc74cSSteve Capper 71679e5fc74cSSteve Capper return pte; 71689e5fc74cSSteve Capper } 71699e5fc74cSSteve Capper 71709b19df29SPunit Agrawal /* 71719b19df29SPunit Agrawal * huge_pte_offset() - Walk the page table to resolve the hugepage 71729b19df29SPunit Agrawal * entry at address @addr 71739b19df29SPunit Agrawal * 71748ac0b81aSLi Xinhai * Return: Pointer to page table entry (PUD or PMD) for 71758ac0b81aSLi Xinhai * address @addr, or NULL if a !p*d_present() entry is encountered and the 71769b19df29SPunit Agrawal * size @sz doesn't match the hugepage size at this level of the page 71779b19df29SPunit Agrawal * table. 71789b19df29SPunit Agrawal */ 71797868a208SPunit Agrawal pte_t *huge_pte_offset(struct mm_struct *mm, 71807868a208SPunit Agrawal unsigned long addr, unsigned long sz) 71819e5fc74cSSteve Capper { 71829e5fc74cSSteve Capper pgd_t *pgd; 7183c2febafcSKirill A. Shutemov p4d_t *p4d; 71848ac0b81aSLi Xinhai pud_t *pud; 71858ac0b81aSLi Xinhai pmd_t *pmd; 71869e5fc74cSSteve Capper 71879e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 7188c2febafcSKirill A. Shutemov if (!pgd_present(*pgd)) 7189c2febafcSKirill A. Shutemov return NULL; 7190c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 7191c2febafcSKirill A. Shutemov if (!p4d_present(*p4d)) 7192c2febafcSKirill A. Shutemov return NULL; 71939b19df29SPunit Agrawal 7194c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 71958ac0b81aSLi Xinhai if (sz == PUD_SIZE) 71968ac0b81aSLi Xinhai /* must be pud huge, non-present or none */ 71979e5fc74cSSteve Capper return (pte_t *)pud; 71988ac0b81aSLi Xinhai if (!pud_present(*pud)) 71998ac0b81aSLi Xinhai return NULL; 72008ac0b81aSLi Xinhai /* must have a valid entry and size to go further */ 72019b19df29SPunit Agrawal 72029e5fc74cSSteve Capper pmd = pmd_offset(pud, addr); 72038ac0b81aSLi Xinhai /* must be pmd huge, non-present or none */ 72049e5fc74cSSteve Capper return (pte_t *)pmd; 72059e5fc74cSSteve Capper } 72069e5fc74cSSteve Capper 7207e95a9851SMike Kravetz /* 7208e95a9851SMike Kravetz * Return a mask that can be used to update an address to the last huge 7209e95a9851SMike Kravetz * page in a page table page mapping size. Used to skip non-present 7210e95a9851SMike Kravetz * page table entries when linearly scanning address ranges. Architectures 7211e95a9851SMike Kravetz * with unique huge page to page table relationships can define their own 7212e95a9851SMike Kravetz * version of this routine. 7213e95a9851SMike Kravetz */ 7214e95a9851SMike Kravetz unsigned long hugetlb_mask_last_page(struct hstate *h) 7215e95a9851SMike Kravetz { 7216e95a9851SMike Kravetz unsigned long hp_size = huge_page_size(h); 7217e95a9851SMike Kravetz 7218e95a9851SMike Kravetz if (hp_size == PUD_SIZE) 7219e95a9851SMike Kravetz return P4D_SIZE - PUD_SIZE; 7220e95a9851SMike Kravetz else if (hp_size == PMD_SIZE) 7221e95a9851SMike Kravetz return PUD_SIZE - PMD_SIZE; 7222e95a9851SMike Kravetz else 7223e95a9851SMike Kravetz return 0UL; 7224e95a9851SMike Kravetz } 7225e95a9851SMike Kravetz 7226e95a9851SMike Kravetz #else 7227e95a9851SMike Kravetz 7228e95a9851SMike Kravetz /* See description above. Architectures can provide their own version. */ 7229e95a9851SMike Kravetz __weak unsigned long hugetlb_mask_last_page(struct hstate *h) 7230e95a9851SMike Kravetz { 72314ddb4d91SMike Kravetz #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE 72324ddb4d91SMike Kravetz if (huge_page_size(h) == PMD_SIZE) 72334ddb4d91SMike Kravetz return PUD_SIZE - PMD_SIZE; 72344ddb4d91SMike Kravetz #endif 7235e95a9851SMike Kravetz return 0UL; 7236e95a9851SMike Kravetz } 7237e95a9851SMike Kravetz 723861f77edaSNaoya Horiguchi #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 723961f77edaSNaoya Horiguchi 724061f77edaSNaoya Horiguchi /* 724161f77edaSNaoya Horiguchi * These functions are overwritable if your architecture needs its own 724261f77edaSNaoya Horiguchi * behavior. 724361f77edaSNaoya Horiguchi */ 72447ce82f4cSMiaohe Lin int isolate_hugetlb(struct page *page, struct list_head *list) 724531caf665SNaoya Horiguchi { 72467ce82f4cSMiaohe Lin int ret = 0; 7247bcc54222SNaoya Horiguchi 7248db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 72498f251a3dSMike Kravetz if (!PageHeadHuge(page) || 72508f251a3dSMike Kravetz !HPageMigratable(page) || 72510eb2df2bSMuchun Song !get_page_unless_zero(page)) { 72527ce82f4cSMiaohe Lin ret = -EBUSY; 7253bcc54222SNaoya Horiguchi goto unlock; 7254bcc54222SNaoya Horiguchi } 72558f251a3dSMike Kravetz ClearHPageMigratable(page); 725631caf665SNaoya Horiguchi list_move_tail(&page->lru, list); 7257bcc54222SNaoya Horiguchi unlock: 7258db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 7259bcc54222SNaoya Horiguchi return ret; 726031caf665SNaoya Horiguchi } 726131caf665SNaoya Horiguchi 7262e591ef7dSNaoya Horiguchi int get_hwpoison_huge_page(struct page *page, bool *hugetlb, bool unpoison) 726325182f05SNaoya Horiguchi { 726425182f05SNaoya Horiguchi int ret = 0; 726525182f05SNaoya Horiguchi 726625182f05SNaoya Horiguchi *hugetlb = false; 726725182f05SNaoya Horiguchi spin_lock_irq(&hugetlb_lock); 726825182f05SNaoya Horiguchi if (PageHeadHuge(page)) { 726925182f05SNaoya Horiguchi *hugetlb = true; 7270b283d983SNaoya Horiguchi if (HPageFreed(page)) 7271b283d983SNaoya Horiguchi ret = 0; 7272e591ef7dSNaoya Horiguchi else if (HPageMigratable(page) || unpoison) 727325182f05SNaoya Horiguchi ret = get_page_unless_zero(page); 72740ed950d1SNaoya Horiguchi else 72750ed950d1SNaoya Horiguchi ret = -EBUSY; 727625182f05SNaoya Horiguchi } 727725182f05SNaoya Horiguchi spin_unlock_irq(&hugetlb_lock); 727825182f05SNaoya Horiguchi return ret; 727925182f05SNaoya Horiguchi } 728025182f05SNaoya Horiguchi 7281e591ef7dSNaoya Horiguchi int get_huge_page_for_hwpoison(unsigned long pfn, int flags, 7282e591ef7dSNaoya Horiguchi bool *migratable_cleared) 7283405ce051SNaoya Horiguchi { 7284405ce051SNaoya Horiguchi int ret; 7285405ce051SNaoya Horiguchi 7286405ce051SNaoya Horiguchi spin_lock_irq(&hugetlb_lock); 7287e591ef7dSNaoya Horiguchi ret = __get_huge_page_for_hwpoison(pfn, flags, migratable_cleared); 7288405ce051SNaoya Horiguchi spin_unlock_irq(&hugetlb_lock); 7289405ce051SNaoya Horiguchi return ret; 7290405ce051SNaoya Horiguchi } 7291405ce051SNaoya Horiguchi 729231caf665SNaoya Horiguchi void putback_active_hugepage(struct page *page) 729331caf665SNaoya Horiguchi { 7294db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 72958f251a3dSMike Kravetz SetHPageMigratable(page); 729631caf665SNaoya Horiguchi list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist); 7297db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 729831caf665SNaoya Horiguchi put_page(page); 729931caf665SNaoya Horiguchi } 7300ab5ac90aSMichal Hocko 7301345c62d1SSidhartha Kumar void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason) 7302ab5ac90aSMichal Hocko { 7303345c62d1SSidhartha Kumar struct hstate *h = folio_hstate(old_folio); 7304ab5ac90aSMichal Hocko 7305345c62d1SSidhartha Kumar hugetlb_cgroup_migrate(old_folio, new_folio); 7306345c62d1SSidhartha Kumar set_page_owner_migrate_reason(&new_folio->page, reason); 7307ab5ac90aSMichal Hocko 7308ab5ac90aSMichal Hocko /* 7309345c62d1SSidhartha Kumar * transfer temporary state of the new hugetlb folio. This is 7310ab5ac90aSMichal Hocko * reverse to other transitions because the newpage is going to 7311ab5ac90aSMichal Hocko * be final while the old one will be freed so it takes over 7312ab5ac90aSMichal Hocko * the temporary status. 7313ab5ac90aSMichal Hocko * 7314ab5ac90aSMichal Hocko * Also note that we have to transfer the per-node surplus state 7315ab5ac90aSMichal Hocko * here as well otherwise the global surplus count will not match 7316ab5ac90aSMichal Hocko * the per-node's. 7317ab5ac90aSMichal Hocko */ 7318345c62d1SSidhartha Kumar if (folio_test_hugetlb_temporary(new_folio)) { 7319345c62d1SSidhartha Kumar int old_nid = folio_nid(old_folio); 7320345c62d1SSidhartha Kumar int new_nid = folio_nid(new_folio); 7321ab5ac90aSMichal Hocko 7322345c62d1SSidhartha Kumar folio_set_hugetlb_temporary(old_folio); 7323345c62d1SSidhartha Kumar folio_clear_hugetlb_temporary(new_folio); 7324345c62d1SSidhartha Kumar 7325ab5ac90aSMichal Hocko 73265af1ab1dSMiaohe Lin /* 73275af1ab1dSMiaohe Lin * There is no need to transfer the per-node surplus state 73285af1ab1dSMiaohe Lin * when we do not cross the node. 73295af1ab1dSMiaohe Lin */ 73305af1ab1dSMiaohe Lin if (new_nid == old_nid) 73315af1ab1dSMiaohe Lin return; 7332db71ef79SMike Kravetz spin_lock_irq(&hugetlb_lock); 7333ab5ac90aSMichal Hocko if (h->surplus_huge_pages_node[old_nid]) { 7334ab5ac90aSMichal Hocko h->surplus_huge_pages_node[old_nid]--; 7335ab5ac90aSMichal Hocko h->surplus_huge_pages_node[new_nid]++; 7336ab5ac90aSMichal Hocko } 7337db71ef79SMike Kravetz spin_unlock_irq(&hugetlb_lock); 7338ab5ac90aSMichal Hocko } 7339ab5ac90aSMichal Hocko } 7340cf11e85fSRoman Gushchin 7341b30c14cdSJames Houghton static void hugetlb_unshare_pmds(struct vm_area_struct *vma, 7342b30c14cdSJames Houghton unsigned long start, 7343b30c14cdSJames Houghton unsigned long end) 73446dfeaff9SPeter Xu { 73456dfeaff9SPeter Xu struct hstate *h = hstate_vma(vma); 73466dfeaff9SPeter Xu unsigned long sz = huge_page_size(h); 73476dfeaff9SPeter Xu struct mm_struct *mm = vma->vm_mm; 73486dfeaff9SPeter Xu struct mmu_notifier_range range; 7349b30c14cdSJames Houghton unsigned long address; 73506dfeaff9SPeter Xu spinlock_t *ptl; 73516dfeaff9SPeter Xu pte_t *ptep; 73526dfeaff9SPeter Xu 73536dfeaff9SPeter Xu if (!(vma->vm_flags & VM_MAYSHARE)) 73546dfeaff9SPeter Xu return; 73556dfeaff9SPeter Xu 73566dfeaff9SPeter Xu if (start >= end) 73576dfeaff9SPeter Xu return; 73586dfeaff9SPeter Xu 73599c8bbfacSBaolin Wang flush_cache_range(vma, start, end); 73606dfeaff9SPeter Xu /* 73616dfeaff9SPeter Xu * No need to call adjust_range_if_pmd_sharing_possible(), because 73626dfeaff9SPeter Xu * we have already done the PUD_SIZE alignment. 73636dfeaff9SPeter Xu */ 73646dfeaff9SPeter Xu mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, 73656dfeaff9SPeter Xu start, end); 73666dfeaff9SPeter Xu mmu_notifier_invalidate_range_start(&range); 736740549ba8SMike Kravetz hugetlb_vma_lock_write(vma); 73686dfeaff9SPeter Xu i_mmap_lock_write(vma->vm_file->f_mapping); 73696dfeaff9SPeter Xu for (address = start; address < end; address += PUD_SIZE) { 73706dfeaff9SPeter Xu ptep = huge_pte_offset(mm, address, sz); 73716dfeaff9SPeter Xu if (!ptep) 73726dfeaff9SPeter Xu continue; 73736dfeaff9SPeter Xu ptl = huge_pte_lock(h, mm, ptep); 73744ddb4d91SMike Kravetz huge_pmd_unshare(mm, vma, address, ptep); 73756dfeaff9SPeter Xu spin_unlock(ptl); 73766dfeaff9SPeter Xu } 73776dfeaff9SPeter Xu flush_hugetlb_tlb_range(vma, start, end); 73786dfeaff9SPeter Xu i_mmap_unlock_write(vma->vm_file->f_mapping); 737940549ba8SMike Kravetz hugetlb_vma_unlock_write(vma); 73806dfeaff9SPeter Xu /* 73816dfeaff9SPeter Xu * No need to call mmu_notifier_invalidate_range(), see 7382ee65728eSMike Rapoport * Documentation/mm/mmu_notifier.rst. 73836dfeaff9SPeter Xu */ 73846dfeaff9SPeter Xu mmu_notifier_invalidate_range_end(&range); 73856dfeaff9SPeter Xu } 73866dfeaff9SPeter Xu 7387b30c14cdSJames Houghton /* 7388b30c14cdSJames Houghton * This function will unconditionally remove all the shared pmd pgtable entries 7389b30c14cdSJames Houghton * within the specific vma for a hugetlbfs memory range. 7390b30c14cdSJames Houghton */ 7391b30c14cdSJames Houghton void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) 7392b30c14cdSJames Houghton { 7393b30c14cdSJames Houghton hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE), 7394b30c14cdSJames Houghton ALIGN_DOWN(vma->vm_end, PUD_SIZE)); 7395b30c14cdSJames Houghton } 7396b30c14cdSJames Houghton 7397cf11e85fSRoman Gushchin #ifdef CONFIG_CMA 7398cf11e85fSRoman Gushchin static bool cma_reserve_called __initdata; 7399cf11e85fSRoman Gushchin 7400cf11e85fSRoman Gushchin static int __init cmdline_parse_hugetlb_cma(char *p) 7401cf11e85fSRoman Gushchin { 740238e719abSBaolin Wang int nid, count = 0; 740338e719abSBaolin Wang unsigned long tmp; 740438e719abSBaolin Wang char *s = p; 740538e719abSBaolin Wang 740638e719abSBaolin Wang while (*s) { 740738e719abSBaolin Wang if (sscanf(s, "%lu%n", &tmp, &count) != 1) 740838e719abSBaolin Wang break; 740938e719abSBaolin Wang 741038e719abSBaolin Wang if (s[count] == ':') { 7411f9317f77SMike Kravetz if (tmp >= MAX_NUMNODES) 741238e719abSBaolin Wang break; 7413f9317f77SMike Kravetz nid = array_index_nospec(tmp, MAX_NUMNODES); 741438e719abSBaolin Wang 741538e719abSBaolin Wang s += count + 1; 741638e719abSBaolin Wang tmp = memparse(s, &s); 741738e719abSBaolin Wang hugetlb_cma_size_in_node[nid] = tmp; 741838e719abSBaolin Wang hugetlb_cma_size += tmp; 741938e719abSBaolin Wang 742038e719abSBaolin Wang /* 742138e719abSBaolin Wang * Skip the separator if have one, otherwise 742238e719abSBaolin Wang * break the parsing. 742338e719abSBaolin Wang */ 742438e719abSBaolin Wang if (*s == ',') 742538e719abSBaolin Wang s++; 742638e719abSBaolin Wang else 742738e719abSBaolin Wang break; 742838e719abSBaolin Wang } else { 7429cf11e85fSRoman Gushchin hugetlb_cma_size = memparse(p, &p); 743038e719abSBaolin Wang break; 743138e719abSBaolin Wang } 743238e719abSBaolin Wang } 743338e719abSBaolin Wang 7434cf11e85fSRoman Gushchin return 0; 7435cf11e85fSRoman Gushchin } 7436cf11e85fSRoman Gushchin 7437cf11e85fSRoman Gushchin early_param("hugetlb_cma", cmdline_parse_hugetlb_cma); 7438cf11e85fSRoman Gushchin 7439cf11e85fSRoman Gushchin void __init hugetlb_cma_reserve(int order) 7440cf11e85fSRoman Gushchin { 7441cf11e85fSRoman Gushchin unsigned long size, reserved, per_node; 744238e719abSBaolin Wang bool node_specific_cma_alloc = false; 7443cf11e85fSRoman Gushchin int nid; 7444cf11e85fSRoman Gushchin 7445cf11e85fSRoman Gushchin cma_reserve_called = true; 7446cf11e85fSRoman Gushchin 7447cf11e85fSRoman Gushchin if (!hugetlb_cma_size) 7448cf11e85fSRoman Gushchin return; 7449cf11e85fSRoman Gushchin 745038e719abSBaolin Wang for (nid = 0; nid < MAX_NUMNODES; nid++) { 745138e719abSBaolin Wang if (hugetlb_cma_size_in_node[nid] == 0) 745238e719abSBaolin Wang continue; 745338e719abSBaolin Wang 745430a51400SPeng Liu if (!node_online(nid)) { 745538e719abSBaolin Wang pr_warn("hugetlb_cma: invalid node %d specified\n", nid); 745638e719abSBaolin Wang hugetlb_cma_size -= hugetlb_cma_size_in_node[nid]; 745738e719abSBaolin Wang hugetlb_cma_size_in_node[nid] = 0; 745838e719abSBaolin Wang continue; 745938e719abSBaolin Wang } 746038e719abSBaolin Wang 746138e719abSBaolin Wang if (hugetlb_cma_size_in_node[nid] < (PAGE_SIZE << order)) { 746238e719abSBaolin Wang pr_warn("hugetlb_cma: cma area of node %d should be at least %lu MiB\n", 746338e719abSBaolin Wang nid, (PAGE_SIZE << order) / SZ_1M); 746438e719abSBaolin Wang hugetlb_cma_size -= hugetlb_cma_size_in_node[nid]; 746538e719abSBaolin Wang hugetlb_cma_size_in_node[nid] = 0; 746638e719abSBaolin Wang } else { 746738e719abSBaolin Wang node_specific_cma_alloc = true; 746838e719abSBaolin Wang } 746938e719abSBaolin Wang } 747038e719abSBaolin Wang 747138e719abSBaolin Wang /* Validate the CMA size again in case some invalid nodes specified. */ 747238e719abSBaolin Wang if (!hugetlb_cma_size) 747338e719abSBaolin Wang return; 747438e719abSBaolin Wang 7475cf11e85fSRoman Gushchin if (hugetlb_cma_size < (PAGE_SIZE << order)) { 7476cf11e85fSRoman Gushchin pr_warn("hugetlb_cma: cma area should be at least %lu MiB\n", 7477cf11e85fSRoman Gushchin (PAGE_SIZE << order) / SZ_1M); 7478a01f4390SMike Kravetz hugetlb_cma_size = 0; 7479cf11e85fSRoman Gushchin return; 7480cf11e85fSRoman Gushchin } 7481cf11e85fSRoman Gushchin 748238e719abSBaolin Wang if (!node_specific_cma_alloc) { 7483cf11e85fSRoman Gushchin /* 7484cf11e85fSRoman Gushchin * If 3 GB area is requested on a machine with 4 numa nodes, 7485cf11e85fSRoman Gushchin * let's allocate 1 GB on first three nodes and ignore the last one. 7486cf11e85fSRoman Gushchin */ 7487cf11e85fSRoman Gushchin per_node = DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes); 7488cf11e85fSRoman Gushchin pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n", 7489cf11e85fSRoman Gushchin hugetlb_cma_size / SZ_1M, per_node / SZ_1M); 749038e719abSBaolin Wang } 7491cf11e85fSRoman Gushchin 7492cf11e85fSRoman Gushchin reserved = 0; 749330a51400SPeng Liu for_each_online_node(nid) { 7494cf11e85fSRoman Gushchin int res; 74952281f797SBarry Song char name[CMA_MAX_NAME]; 7496cf11e85fSRoman Gushchin 749738e719abSBaolin Wang if (node_specific_cma_alloc) { 749838e719abSBaolin Wang if (hugetlb_cma_size_in_node[nid] == 0) 749938e719abSBaolin Wang continue; 750038e719abSBaolin Wang 750138e719abSBaolin Wang size = hugetlb_cma_size_in_node[nid]; 750238e719abSBaolin Wang } else { 7503cf11e85fSRoman Gushchin size = min(per_node, hugetlb_cma_size - reserved); 750438e719abSBaolin Wang } 750538e719abSBaolin Wang 7506cf11e85fSRoman Gushchin size = round_up(size, PAGE_SIZE << order); 7507cf11e85fSRoman Gushchin 75082281f797SBarry Song snprintf(name, sizeof(name), "hugetlb%d", nid); 7509a01f4390SMike Kravetz /* 7510a01f4390SMike Kravetz * Note that 'order per bit' is based on smallest size that 7511a01f4390SMike Kravetz * may be returned to CMA allocator in the case of 7512a01f4390SMike Kravetz * huge page demotion. 7513a01f4390SMike Kravetz */ 7514a01f4390SMike Kravetz res = cma_declare_contiguous_nid(0, size, 0, 7515a01f4390SMike Kravetz PAGE_SIZE << HUGETLB_PAGE_ORDER, 751629d0f41dSBarry Song 0, false, name, 7517cf11e85fSRoman Gushchin &hugetlb_cma[nid], nid); 7518cf11e85fSRoman Gushchin if (res) { 7519cf11e85fSRoman Gushchin pr_warn("hugetlb_cma: reservation failed: err %d, node %d", 7520cf11e85fSRoman Gushchin res, nid); 7521cf11e85fSRoman Gushchin continue; 7522cf11e85fSRoman Gushchin } 7523cf11e85fSRoman Gushchin 7524cf11e85fSRoman Gushchin reserved += size; 7525cf11e85fSRoman Gushchin pr_info("hugetlb_cma: reserved %lu MiB on node %d\n", 7526cf11e85fSRoman Gushchin size / SZ_1M, nid); 7527cf11e85fSRoman Gushchin 7528cf11e85fSRoman Gushchin if (reserved >= hugetlb_cma_size) 7529cf11e85fSRoman Gushchin break; 7530cf11e85fSRoman Gushchin } 7531a01f4390SMike Kravetz 7532a01f4390SMike Kravetz if (!reserved) 7533a01f4390SMike Kravetz /* 7534a01f4390SMike Kravetz * hugetlb_cma_size is used to determine if allocations from 7535a01f4390SMike Kravetz * cma are possible. Set to zero if no cma regions are set up. 7536a01f4390SMike Kravetz */ 7537a01f4390SMike Kravetz hugetlb_cma_size = 0; 7538cf11e85fSRoman Gushchin } 7539cf11e85fSRoman Gushchin 7540263b8998SMiaohe Lin static void __init hugetlb_cma_check(void) 7541cf11e85fSRoman Gushchin { 7542cf11e85fSRoman Gushchin if (!hugetlb_cma_size || cma_reserve_called) 7543cf11e85fSRoman Gushchin return; 7544cf11e85fSRoman Gushchin 7545cf11e85fSRoman Gushchin pr_warn("hugetlb_cma: the option isn't supported by current arch\n"); 7546cf11e85fSRoman Gushchin } 7547cf11e85fSRoman Gushchin 7548cf11e85fSRoman Gushchin #endif /* CONFIG_CMA */ 7549