11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Generic hugetlb support. 36d49e352SNadia Yvette Chambers * (C) Nadia Yvette Chambers, April 2004 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds #include <linux/list.h> 61da177e4SLinus Torvalds #include <linux/init.h> 71da177e4SLinus Torvalds #include <linux/mm.h> 8e1759c21SAlexey Dobriyan #include <linux/seq_file.h> 91da177e4SLinus Torvalds #include <linux/sysctl.h> 101da177e4SLinus Torvalds #include <linux/highmem.h> 11cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 121da177e4SLinus Torvalds #include <linux/nodemask.h> 1363551ae0SDavid Gibson #include <linux/pagemap.h> 145da7ca86SChristoph Lameter #include <linux/mempolicy.h> 153b32123dSGideon Israel Dsouza #include <linux/compiler.h> 16aea47ff3SChristoph Lameter #include <linux/cpuset.h> 173935baa9SDavid Gibson #include <linux/mutex.h> 18aa888a74SAndi Kleen #include <linux/bootmem.h> 19a3437870SNishanth Aravamudan #include <linux/sysfs.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 21174cd4b1SIngo Molnar #include <linux/sched/signal.h> 220fe6e20bSNaoya Horiguchi #include <linux/rmap.h> 23fd6a03edSNaoya Horiguchi #include <linux/swap.h> 24fd6a03edSNaoya Horiguchi #include <linux/swapops.h> 25c8721bbbSNaoya Horiguchi #include <linux/page-isolation.h> 268382d914SDavidlohr Bueso #include <linux/jhash.h> 27d6606683SLinus Torvalds 2863551ae0SDavid Gibson #include <asm/page.h> 2963551ae0SDavid Gibson #include <asm/pgtable.h> 3024669e58SAneesh Kumar K.V #include <asm/tlb.h> 3163551ae0SDavid Gibson 3224669e58SAneesh Kumar K.V #include <linux/io.h> 3363551ae0SDavid Gibson #include <linux/hugetlb.h> 349dd540e2SAneesh Kumar K.V #include <linux/hugetlb_cgroup.h> 359a305230SLee Schermerhorn #include <linux/node.h> 361a1aad8aSMike Kravetz #include <linux/userfaultfd_k.h> 377835e98bSNick Piggin #include "internal.h" 381da177e4SLinus Torvalds 39753162cdSAndrey Ryabinin int hugepages_treat_as_movable; 40a5516438SAndi Kleen 41c3f38a38SAneesh Kumar K.V int hugetlb_max_hstate __read_mostly; 42e5ff2159SAndi Kleen unsigned int default_hstate_idx; 43e5ff2159SAndi Kleen struct hstate hstates[HUGE_MAX_HSTATE]; 44641844f5SNaoya Horiguchi /* 45641844f5SNaoya Horiguchi * Minimum page order among possible hugepage sizes, set to a proper value 46641844f5SNaoya Horiguchi * at boot time. 47641844f5SNaoya Horiguchi */ 48641844f5SNaoya Horiguchi static unsigned int minimum_order __read_mostly = UINT_MAX; 49e5ff2159SAndi Kleen 5053ba51d2SJon Tollefson __initdata LIST_HEAD(huge_boot_pages); 5153ba51d2SJon Tollefson 52e5ff2159SAndi Kleen /* for command line parsing */ 53e5ff2159SAndi Kleen static struct hstate * __initdata parsed_hstate; 54e5ff2159SAndi Kleen static unsigned long __initdata default_hstate_max_huge_pages; 55e11bfbfcSNick Piggin static unsigned long __initdata default_hstate_size; 569fee021dSVaishali Thakkar static bool __initdata parsed_valid_hugepagesz = true; 57e5ff2159SAndi Kleen 583935baa9SDavid Gibson /* 5931caf665SNaoya Horiguchi * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages, 6031caf665SNaoya Horiguchi * free_huge_pages, and surplus_huge_pages. 613935baa9SDavid Gibson */ 62c3f38a38SAneesh Kumar K.V DEFINE_SPINLOCK(hugetlb_lock); 630bd0f9fbSEric Paris 648382d914SDavidlohr Bueso /* 658382d914SDavidlohr Bueso * Serializes faults on the same logical page. This is used to 668382d914SDavidlohr Bueso * prevent spurious OOMs when the hugepage pool is fully utilized. 678382d914SDavidlohr Bueso */ 688382d914SDavidlohr Bueso static int num_fault_mutexes; 69c672c7f2SMike Kravetz struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp; 708382d914SDavidlohr Bueso 717ca02d0aSMike Kravetz /* Forward declaration */ 727ca02d0aSMike Kravetz static int hugetlb_acct_memory(struct hstate *h, long delta); 737ca02d0aSMike Kravetz 7490481622SDavid Gibson static inline void unlock_or_release_subpool(struct hugepage_subpool *spool) 7590481622SDavid Gibson { 7690481622SDavid Gibson bool free = (spool->count == 0) && (spool->used_hpages == 0); 7790481622SDavid Gibson 7890481622SDavid Gibson spin_unlock(&spool->lock); 7990481622SDavid Gibson 8090481622SDavid Gibson /* If no pages are used, and no other handles to the subpool 817ca02d0aSMike Kravetz * remain, give up any reservations mased on minimum size and 827ca02d0aSMike Kravetz * free the subpool */ 837ca02d0aSMike Kravetz if (free) { 847ca02d0aSMike Kravetz if (spool->min_hpages != -1) 857ca02d0aSMike Kravetz hugetlb_acct_memory(spool->hstate, 867ca02d0aSMike Kravetz -spool->min_hpages); 8790481622SDavid Gibson kfree(spool); 8890481622SDavid Gibson } 897ca02d0aSMike Kravetz } 9090481622SDavid Gibson 917ca02d0aSMike Kravetz struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages, 927ca02d0aSMike Kravetz long min_hpages) 9390481622SDavid Gibson { 9490481622SDavid Gibson struct hugepage_subpool *spool; 9590481622SDavid Gibson 96c6a91820SMike Kravetz spool = kzalloc(sizeof(*spool), GFP_KERNEL); 9790481622SDavid Gibson if (!spool) 9890481622SDavid Gibson return NULL; 9990481622SDavid Gibson 10090481622SDavid Gibson spin_lock_init(&spool->lock); 10190481622SDavid Gibson spool->count = 1; 1027ca02d0aSMike Kravetz spool->max_hpages = max_hpages; 1037ca02d0aSMike Kravetz spool->hstate = h; 1047ca02d0aSMike Kravetz spool->min_hpages = min_hpages; 1057ca02d0aSMike Kravetz 1067ca02d0aSMike Kravetz if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) { 1077ca02d0aSMike Kravetz kfree(spool); 1087ca02d0aSMike Kravetz return NULL; 1097ca02d0aSMike Kravetz } 1107ca02d0aSMike Kravetz spool->rsv_hpages = min_hpages; 11190481622SDavid Gibson 11290481622SDavid Gibson return spool; 11390481622SDavid Gibson } 11490481622SDavid Gibson 11590481622SDavid Gibson void hugepage_put_subpool(struct hugepage_subpool *spool) 11690481622SDavid Gibson { 11790481622SDavid Gibson spin_lock(&spool->lock); 11890481622SDavid Gibson BUG_ON(!spool->count); 11990481622SDavid Gibson spool->count--; 12090481622SDavid Gibson unlock_or_release_subpool(spool); 12190481622SDavid Gibson } 12290481622SDavid Gibson 1231c5ecae3SMike Kravetz /* 1241c5ecae3SMike Kravetz * Subpool accounting for allocating and reserving pages. 1251c5ecae3SMike Kravetz * Return -ENOMEM if there are not enough resources to satisfy the 1261c5ecae3SMike Kravetz * the request. Otherwise, return the number of pages by which the 1271c5ecae3SMike Kravetz * global pools must be adjusted (upward). The returned value may 1281c5ecae3SMike Kravetz * only be different than the passed value (delta) in the case where 1291c5ecae3SMike Kravetz * a subpool minimum size must be manitained. 1301c5ecae3SMike Kravetz */ 1311c5ecae3SMike Kravetz static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, 13290481622SDavid Gibson long delta) 13390481622SDavid Gibson { 1341c5ecae3SMike Kravetz long ret = delta; 13590481622SDavid Gibson 13690481622SDavid Gibson if (!spool) 1371c5ecae3SMike Kravetz return ret; 13890481622SDavid Gibson 13990481622SDavid Gibson spin_lock(&spool->lock); 14090481622SDavid Gibson 1411c5ecae3SMike Kravetz if (spool->max_hpages != -1) { /* maximum size accounting */ 1421c5ecae3SMike Kravetz if ((spool->used_hpages + delta) <= spool->max_hpages) 1431c5ecae3SMike Kravetz spool->used_hpages += delta; 1441c5ecae3SMike Kravetz else { 1451c5ecae3SMike Kravetz ret = -ENOMEM; 1461c5ecae3SMike Kravetz goto unlock_ret; 1471c5ecae3SMike Kravetz } 1481c5ecae3SMike Kravetz } 1491c5ecae3SMike Kravetz 15009a95e29SMike Kravetz /* minimum size accounting */ 15109a95e29SMike Kravetz if (spool->min_hpages != -1 && spool->rsv_hpages) { 1521c5ecae3SMike Kravetz if (delta > spool->rsv_hpages) { 1531c5ecae3SMike Kravetz /* 1541c5ecae3SMike Kravetz * Asking for more reserves than those already taken on 1551c5ecae3SMike Kravetz * behalf of subpool. Return difference. 1561c5ecae3SMike Kravetz */ 1571c5ecae3SMike Kravetz ret = delta - spool->rsv_hpages; 1581c5ecae3SMike Kravetz spool->rsv_hpages = 0; 1591c5ecae3SMike Kravetz } else { 1601c5ecae3SMike Kravetz ret = 0; /* reserves already accounted for */ 1611c5ecae3SMike Kravetz spool->rsv_hpages -= delta; 1621c5ecae3SMike Kravetz } 1631c5ecae3SMike Kravetz } 1641c5ecae3SMike Kravetz 1651c5ecae3SMike Kravetz unlock_ret: 1661c5ecae3SMike Kravetz spin_unlock(&spool->lock); 16790481622SDavid Gibson return ret; 16890481622SDavid Gibson } 16990481622SDavid Gibson 1701c5ecae3SMike Kravetz /* 1711c5ecae3SMike Kravetz * Subpool accounting for freeing and unreserving pages. 1721c5ecae3SMike Kravetz * Return the number of global page reservations that must be dropped. 1731c5ecae3SMike Kravetz * The return value may only be different than the passed value (delta) 1741c5ecae3SMike Kravetz * in the case where a subpool minimum size must be maintained. 1751c5ecae3SMike Kravetz */ 1761c5ecae3SMike Kravetz static long hugepage_subpool_put_pages(struct hugepage_subpool *spool, 17790481622SDavid Gibson long delta) 17890481622SDavid Gibson { 1791c5ecae3SMike Kravetz long ret = delta; 1801c5ecae3SMike Kravetz 18190481622SDavid Gibson if (!spool) 1821c5ecae3SMike Kravetz return delta; 18390481622SDavid Gibson 18490481622SDavid Gibson spin_lock(&spool->lock); 1851c5ecae3SMike Kravetz 1861c5ecae3SMike Kravetz if (spool->max_hpages != -1) /* maximum size accounting */ 18790481622SDavid Gibson spool->used_hpages -= delta; 1881c5ecae3SMike Kravetz 18909a95e29SMike Kravetz /* minimum size accounting */ 19009a95e29SMike Kravetz if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) { 1911c5ecae3SMike Kravetz if (spool->rsv_hpages + delta <= spool->min_hpages) 1921c5ecae3SMike Kravetz ret = 0; 1931c5ecae3SMike Kravetz else 1941c5ecae3SMike Kravetz ret = spool->rsv_hpages + delta - spool->min_hpages; 1951c5ecae3SMike Kravetz 1961c5ecae3SMike Kravetz spool->rsv_hpages += delta; 1971c5ecae3SMike Kravetz if (spool->rsv_hpages > spool->min_hpages) 1981c5ecae3SMike Kravetz spool->rsv_hpages = spool->min_hpages; 1991c5ecae3SMike Kravetz } 2001c5ecae3SMike Kravetz 2011c5ecae3SMike Kravetz /* 2021c5ecae3SMike Kravetz * If hugetlbfs_put_super couldn't free spool due to an outstanding 2031c5ecae3SMike Kravetz * quota reference, free it now. 2041c5ecae3SMike Kravetz */ 20590481622SDavid Gibson unlock_or_release_subpool(spool); 2061c5ecae3SMike Kravetz 2071c5ecae3SMike Kravetz return ret; 20890481622SDavid Gibson } 20990481622SDavid Gibson 21090481622SDavid Gibson static inline struct hugepage_subpool *subpool_inode(struct inode *inode) 21190481622SDavid Gibson { 21290481622SDavid Gibson return HUGETLBFS_SB(inode->i_sb)->spool; 21390481622SDavid Gibson } 21490481622SDavid Gibson 21590481622SDavid Gibson static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma) 21690481622SDavid Gibson { 217496ad9aaSAl Viro return subpool_inode(file_inode(vma->vm_file)); 21890481622SDavid Gibson } 21990481622SDavid Gibson 220e7c4b0bfSAndy Whitcroft /* 22196822904SAndy Whitcroft * Region tracking -- allows tracking of reservations and instantiated pages 22296822904SAndy Whitcroft * across the pages in a mapping. 22384afd99bSAndy Whitcroft * 2241dd308a7SMike Kravetz * The region data structures are embedded into a resv_map and protected 2251dd308a7SMike Kravetz * by a resv_map's lock. The set of regions within the resv_map represent 2261dd308a7SMike Kravetz * reservations for huge pages, or huge pages that have already been 2271dd308a7SMike Kravetz * instantiated within the map. The from and to elements are huge page 2281dd308a7SMike Kravetz * indicies into the associated mapping. from indicates the starting index 2291dd308a7SMike Kravetz * of the region. to represents the first index past the end of the region. 2301dd308a7SMike Kravetz * 2311dd308a7SMike Kravetz * For example, a file region structure with from == 0 and to == 4 represents 2321dd308a7SMike Kravetz * four huge pages in a mapping. It is important to note that the to element 2331dd308a7SMike Kravetz * represents the first element past the end of the region. This is used in 2341dd308a7SMike Kravetz * arithmetic as 4(to) - 0(from) = 4 huge pages in the region. 2351dd308a7SMike Kravetz * 2361dd308a7SMike Kravetz * Interval notation of the form [from, to) will be used to indicate that 2371dd308a7SMike Kravetz * the endpoint from is inclusive and to is exclusive. 23896822904SAndy Whitcroft */ 23996822904SAndy Whitcroft struct file_region { 24096822904SAndy Whitcroft struct list_head link; 24196822904SAndy Whitcroft long from; 24296822904SAndy Whitcroft long to; 24396822904SAndy Whitcroft }; 24496822904SAndy Whitcroft 2451dd308a7SMike Kravetz /* 2461dd308a7SMike Kravetz * Add the huge page range represented by [f, t) to the reserve 2475e911373SMike Kravetz * map. In the normal case, existing regions will be expanded 2485e911373SMike Kravetz * to accommodate the specified range. Sufficient regions should 2495e911373SMike Kravetz * exist for expansion due to the previous call to region_chg 2505e911373SMike Kravetz * with the same range. However, it is possible that region_del 2515e911373SMike Kravetz * could have been called after region_chg and modifed the map 2525e911373SMike Kravetz * in such a way that no region exists to be expanded. In this 2535e911373SMike Kravetz * case, pull a region descriptor from the cache associated with 2545e911373SMike Kravetz * the map and use that for the new range. 255cf3ad20bSMike Kravetz * 256cf3ad20bSMike Kravetz * Return the number of new huge pages added to the map. This 257cf3ad20bSMike Kravetz * number is greater than or equal to zero. 2581dd308a7SMike Kravetz */ 2591406ec9bSJoonsoo Kim static long region_add(struct resv_map *resv, long f, long t) 26096822904SAndy Whitcroft { 2611406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 26296822904SAndy Whitcroft struct file_region *rg, *nrg, *trg; 263cf3ad20bSMike Kravetz long add = 0; 26496822904SAndy Whitcroft 2657b24d861SDavidlohr Bueso spin_lock(&resv->lock); 26696822904SAndy Whitcroft /* Locate the region we are either in or before. */ 26796822904SAndy Whitcroft list_for_each_entry(rg, head, link) 26896822904SAndy Whitcroft if (f <= rg->to) 26996822904SAndy Whitcroft break; 27096822904SAndy Whitcroft 2715e911373SMike Kravetz /* 2725e911373SMike Kravetz * If no region exists which can be expanded to include the 2735e911373SMike Kravetz * specified range, the list must have been modified by an 2745e911373SMike Kravetz * interleving call to region_del(). Pull a region descriptor 2755e911373SMike Kravetz * from the cache and use it for this range. 2765e911373SMike Kravetz */ 2775e911373SMike Kravetz if (&rg->link == head || t < rg->from) { 2785e911373SMike Kravetz VM_BUG_ON(resv->region_cache_count <= 0); 2795e911373SMike Kravetz 2805e911373SMike Kravetz resv->region_cache_count--; 2815e911373SMike Kravetz nrg = list_first_entry(&resv->region_cache, struct file_region, 2825e911373SMike Kravetz link); 2835e911373SMike Kravetz list_del(&nrg->link); 2845e911373SMike Kravetz 2855e911373SMike Kravetz nrg->from = f; 2865e911373SMike Kravetz nrg->to = t; 2875e911373SMike Kravetz list_add(&nrg->link, rg->link.prev); 2885e911373SMike Kravetz 2895e911373SMike Kravetz add += t - f; 2905e911373SMike Kravetz goto out_locked; 2915e911373SMike Kravetz } 2925e911373SMike Kravetz 29396822904SAndy Whitcroft /* Round our left edge to the current segment if it encloses us. */ 29496822904SAndy Whitcroft if (f > rg->from) 29596822904SAndy Whitcroft f = rg->from; 29696822904SAndy Whitcroft 29796822904SAndy Whitcroft /* Check for and consume any regions we now overlap with. */ 29896822904SAndy Whitcroft nrg = rg; 29996822904SAndy Whitcroft list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 30096822904SAndy Whitcroft if (&rg->link == head) 30196822904SAndy Whitcroft break; 30296822904SAndy Whitcroft if (rg->from > t) 30396822904SAndy Whitcroft break; 30496822904SAndy Whitcroft 30596822904SAndy Whitcroft /* If this area reaches higher then extend our area to 30696822904SAndy Whitcroft * include it completely. If this is not the first area 30796822904SAndy Whitcroft * which we intend to reuse, free it. */ 30896822904SAndy Whitcroft if (rg->to > t) 30996822904SAndy Whitcroft t = rg->to; 31096822904SAndy Whitcroft if (rg != nrg) { 311cf3ad20bSMike Kravetz /* Decrement return value by the deleted range. 312cf3ad20bSMike Kravetz * Another range will span this area so that by 313cf3ad20bSMike Kravetz * end of routine add will be >= zero 314cf3ad20bSMike Kravetz */ 315cf3ad20bSMike Kravetz add -= (rg->to - rg->from); 31696822904SAndy Whitcroft list_del(&rg->link); 31796822904SAndy Whitcroft kfree(rg); 31896822904SAndy Whitcroft } 31996822904SAndy Whitcroft } 320cf3ad20bSMike Kravetz 321cf3ad20bSMike Kravetz add += (nrg->from - f); /* Added to beginning of region */ 32296822904SAndy Whitcroft nrg->from = f; 323cf3ad20bSMike Kravetz add += t - nrg->to; /* Added to end of region */ 32496822904SAndy Whitcroft nrg->to = t; 325cf3ad20bSMike Kravetz 3265e911373SMike Kravetz out_locked: 3275e911373SMike Kravetz resv->adds_in_progress--; 3287b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 329cf3ad20bSMike Kravetz VM_BUG_ON(add < 0); 330cf3ad20bSMike Kravetz return add; 33196822904SAndy Whitcroft } 33296822904SAndy Whitcroft 3331dd308a7SMike Kravetz /* 3341dd308a7SMike Kravetz * Examine the existing reserve map and determine how many 3351dd308a7SMike Kravetz * huge pages in the specified range [f, t) are NOT currently 3361dd308a7SMike Kravetz * represented. This routine is called before a subsequent 3371dd308a7SMike Kravetz * call to region_add that will actually modify the reserve 3381dd308a7SMike Kravetz * map to add the specified range [f, t). region_chg does 3391dd308a7SMike Kravetz * not change the number of huge pages represented by the 3401dd308a7SMike Kravetz * map. However, if the existing regions in the map can not 3411dd308a7SMike Kravetz * be expanded to represent the new range, a new file_region 3421dd308a7SMike Kravetz * structure is added to the map as a placeholder. This is 3431dd308a7SMike Kravetz * so that the subsequent region_add call will have all the 3441dd308a7SMike Kravetz * regions it needs and will not fail. 3451dd308a7SMike Kravetz * 3465e911373SMike Kravetz * Upon entry, region_chg will also examine the cache of region descriptors 3475e911373SMike Kravetz * associated with the map. If there are not enough descriptors cached, one 3485e911373SMike Kravetz * will be allocated for the in progress add operation. 3495e911373SMike Kravetz * 3505e911373SMike Kravetz * Returns the number of huge pages that need to be added to the existing 3515e911373SMike Kravetz * reservation map for the range [f, t). This number is greater or equal to 3525e911373SMike Kravetz * zero. -ENOMEM is returned if a new file_region structure or cache entry 3535e911373SMike Kravetz * is needed and can not be allocated. 3541dd308a7SMike Kravetz */ 3551406ec9bSJoonsoo Kim static long region_chg(struct resv_map *resv, long f, long t) 35696822904SAndy Whitcroft { 3571406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 3587b24d861SDavidlohr Bueso struct file_region *rg, *nrg = NULL; 35996822904SAndy Whitcroft long chg = 0; 36096822904SAndy Whitcroft 3617b24d861SDavidlohr Bueso retry: 3627b24d861SDavidlohr Bueso spin_lock(&resv->lock); 3635e911373SMike Kravetz retry_locked: 3645e911373SMike Kravetz resv->adds_in_progress++; 3655e911373SMike Kravetz 3665e911373SMike Kravetz /* 3675e911373SMike Kravetz * Check for sufficient descriptors in the cache to accommodate 3685e911373SMike Kravetz * the number of in progress add operations. 3695e911373SMike Kravetz */ 3705e911373SMike Kravetz if (resv->adds_in_progress > resv->region_cache_count) { 3715e911373SMike Kravetz struct file_region *trg; 3725e911373SMike Kravetz 3735e911373SMike Kravetz VM_BUG_ON(resv->adds_in_progress - resv->region_cache_count > 1); 3745e911373SMike Kravetz /* Must drop lock to allocate a new descriptor. */ 3755e911373SMike Kravetz resv->adds_in_progress--; 3765e911373SMike Kravetz spin_unlock(&resv->lock); 3775e911373SMike Kravetz 3785e911373SMike Kravetz trg = kmalloc(sizeof(*trg), GFP_KERNEL); 379dbe409e4SMike Kravetz if (!trg) { 380dbe409e4SMike Kravetz kfree(nrg); 3815e911373SMike Kravetz return -ENOMEM; 382dbe409e4SMike Kravetz } 3835e911373SMike Kravetz 3845e911373SMike Kravetz spin_lock(&resv->lock); 3855e911373SMike Kravetz list_add(&trg->link, &resv->region_cache); 3865e911373SMike Kravetz resv->region_cache_count++; 3875e911373SMike Kravetz goto retry_locked; 3885e911373SMike Kravetz } 3895e911373SMike Kravetz 39096822904SAndy Whitcroft /* Locate the region we are before or in. */ 39196822904SAndy Whitcroft list_for_each_entry(rg, head, link) 39296822904SAndy Whitcroft if (f <= rg->to) 39396822904SAndy Whitcroft break; 39496822904SAndy Whitcroft 39596822904SAndy Whitcroft /* If we are below the current region then a new region is required. 39696822904SAndy Whitcroft * Subtle, allocate a new region at the position but make it zero 39796822904SAndy Whitcroft * size such that we can guarantee to record the reservation. */ 39896822904SAndy Whitcroft if (&rg->link == head || t < rg->from) { 3997b24d861SDavidlohr Bueso if (!nrg) { 4005e911373SMike Kravetz resv->adds_in_progress--; 4017b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 40296822904SAndy Whitcroft nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 40396822904SAndy Whitcroft if (!nrg) 40496822904SAndy Whitcroft return -ENOMEM; 4057b24d861SDavidlohr Bueso 40696822904SAndy Whitcroft nrg->from = f; 40796822904SAndy Whitcroft nrg->to = f; 40896822904SAndy Whitcroft INIT_LIST_HEAD(&nrg->link); 4097b24d861SDavidlohr Bueso goto retry; 4107b24d861SDavidlohr Bueso } 41196822904SAndy Whitcroft 4127b24d861SDavidlohr Bueso list_add(&nrg->link, rg->link.prev); 4137b24d861SDavidlohr Bueso chg = t - f; 4147b24d861SDavidlohr Bueso goto out_nrg; 41596822904SAndy Whitcroft } 41696822904SAndy Whitcroft 41796822904SAndy Whitcroft /* Round our left edge to the current segment if it encloses us. */ 41896822904SAndy Whitcroft if (f > rg->from) 41996822904SAndy Whitcroft f = rg->from; 42096822904SAndy Whitcroft chg = t - f; 42196822904SAndy Whitcroft 42296822904SAndy Whitcroft /* Check for and consume any regions we now overlap with. */ 42396822904SAndy Whitcroft list_for_each_entry(rg, rg->link.prev, link) { 42496822904SAndy Whitcroft if (&rg->link == head) 42596822904SAndy Whitcroft break; 42696822904SAndy Whitcroft if (rg->from > t) 4277b24d861SDavidlohr Bueso goto out; 42896822904SAndy Whitcroft 42925985edcSLucas De Marchi /* We overlap with this area, if it extends further than 43096822904SAndy Whitcroft * us then we must extend ourselves. Account for its 43196822904SAndy Whitcroft * existing reservation. */ 43296822904SAndy Whitcroft if (rg->to > t) { 43396822904SAndy Whitcroft chg += rg->to - t; 43496822904SAndy Whitcroft t = rg->to; 43596822904SAndy Whitcroft } 43696822904SAndy Whitcroft chg -= rg->to - rg->from; 43796822904SAndy Whitcroft } 4387b24d861SDavidlohr Bueso 4397b24d861SDavidlohr Bueso out: 4407b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 4417b24d861SDavidlohr Bueso /* We already know we raced and no longer need the new region */ 4427b24d861SDavidlohr Bueso kfree(nrg); 4437b24d861SDavidlohr Bueso return chg; 4447b24d861SDavidlohr Bueso out_nrg: 4457b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 44696822904SAndy Whitcroft return chg; 44796822904SAndy Whitcroft } 44896822904SAndy Whitcroft 4491dd308a7SMike Kravetz /* 4505e911373SMike Kravetz * Abort the in progress add operation. The adds_in_progress field 4515e911373SMike Kravetz * of the resv_map keeps track of the operations in progress between 4525e911373SMike Kravetz * calls to region_chg and region_add. Operations are sometimes 4535e911373SMike Kravetz * aborted after the call to region_chg. In such cases, region_abort 4545e911373SMike Kravetz * is called to decrement the adds_in_progress counter. 4555e911373SMike Kravetz * 4565e911373SMike Kravetz * NOTE: The range arguments [f, t) are not needed or used in this 4575e911373SMike Kravetz * routine. They are kept to make reading the calling code easier as 4585e911373SMike Kravetz * arguments will match the associated region_chg call. 4595e911373SMike Kravetz */ 4605e911373SMike Kravetz static void region_abort(struct resv_map *resv, long f, long t) 4615e911373SMike Kravetz { 4625e911373SMike Kravetz spin_lock(&resv->lock); 4635e911373SMike Kravetz VM_BUG_ON(!resv->region_cache_count); 4645e911373SMike Kravetz resv->adds_in_progress--; 4655e911373SMike Kravetz spin_unlock(&resv->lock); 4665e911373SMike Kravetz } 4675e911373SMike Kravetz 4685e911373SMike Kravetz /* 469feba16e2SMike Kravetz * Delete the specified range [f, t) from the reserve map. If the 470feba16e2SMike Kravetz * t parameter is LONG_MAX, this indicates that ALL regions after f 471feba16e2SMike Kravetz * should be deleted. Locate the regions which intersect [f, t) 472feba16e2SMike Kravetz * and either trim, delete or split the existing regions. 473feba16e2SMike Kravetz * 474feba16e2SMike Kravetz * Returns the number of huge pages deleted from the reserve map. 475feba16e2SMike Kravetz * In the normal case, the return value is zero or more. In the 476feba16e2SMike Kravetz * case where a region must be split, a new region descriptor must 477feba16e2SMike Kravetz * be allocated. If the allocation fails, -ENOMEM will be returned. 478feba16e2SMike Kravetz * NOTE: If the parameter t == LONG_MAX, then we will never split 479feba16e2SMike Kravetz * a region and possibly return -ENOMEM. Callers specifying 480feba16e2SMike Kravetz * t == LONG_MAX do not need to check for -ENOMEM error. 4811dd308a7SMike Kravetz */ 482feba16e2SMike Kravetz static long region_del(struct resv_map *resv, long f, long t) 48396822904SAndy Whitcroft { 4841406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 48596822904SAndy Whitcroft struct file_region *rg, *trg; 486feba16e2SMike Kravetz struct file_region *nrg = NULL; 487feba16e2SMike Kravetz long del = 0; 48896822904SAndy Whitcroft 489feba16e2SMike Kravetz retry: 4907b24d861SDavidlohr Bueso spin_lock(&resv->lock); 491feba16e2SMike Kravetz list_for_each_entry_safe(rg, trg, head, link) { 492dbe409e4SMike Kravetz /* 493dbe409e4SMike Kravetz * Skip regions before the range to be deleted. file_region 494dbe409e4SMike Kravetz * ranges are normally of the form [from, to). However, there 495dbe409e4SMike Kravetz * may be a "placeholder" entry in the map which is of the form 496dbe409e4SMike Kravetz * (from, to) with from == to. Check for placeholder entries 497dbe409e4SMike Kravetz * at the beginning of the range to be deleted. 498dbe409e4SMike Kravetz */ 499dbe409e4SMike Kravetz if (rg->to <= f && (rg->to != rg->from || rg->to != f)) 500feba16e2SMike Kravetz continue; 501dbe409e4SMike Kravetz 502feba16e2SMike Kravetz if (rg->from >= t) 50396822904SAndy Whitcroft break; 50496822904SAndy Whitcroft 505feba16e2SMike Kravetz if (f > rg->from && t < rg->to) { /* Must split region */ 506feba16e2SMike Kravetz /* 507feba16e2SMike Kravetz * Check for an entry in the cache before dropping 508feba16e2SMike Kravetz * lock and attempting allocation. 509feba16e2SMike Kravetz */ 510feba16e2SMike Kravetz if (!nrg && 511feba16e2SMike Kravetz resv->region_cache_count > resv->adds_in_progress) { 512feba16e2SMike Kravetz nrg = list_first_entry(&resv->region_cache, 513feba16e2SMike Kravetz struct file_region, 514feba16e2SMike Kravetz link); 515feba16e2SMike Kravetz list_del(&nrg->link); 516feba16e2SMike Kravetz resv->region_cache_count--; 51796822904SAndy Whitcroft } 51896822904SAndy Whitcroft 519feba16e2SMike Kravetz if (!nrg) { 520feba16e2SMike Kravetz spin_unlock(&resv->lock); 521feba16e2SMike Kravetz nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 522feba16e2SMike Kravetz if (!nrg) 523feba16e2SMike Kravetz return -ENOMEM; 524feba16e2SMike Kravetz goto retry; 525feba16e2SMike Kravetz } 526feba16e2SMike Kravetz 527feba16e2SMike Kravetz del += t - f; 528feba16e2SMike Kravetz 529feba16e2SMike Kravetz /* New entry for end of split region */ 530feba16e2SMike Kravetz nrg->from = t; 531feba16e2SMike Kravetz nrg->to = rg->to; 532feba16e2SMike Kravetz INIT_LIST_HEAD(&nrg->link); 533feba16e2SMike Kravetz 534feba16e2SMike Kravetz /* Original entry is trimmed */ 535feba16e2SMike Kravetz rg->to = f; 536feba16e2SMike Kravetz 537feba16e2SMike Kravetz list_add(&nrg->link, &rg->link); 538feba16e2SMike Kravetz nrg = NULL; 53996822904SAndy Whitcroft break; 540feba16e2SMike Kravetz } 541feba16e2SMike Kravetz 542feba16e2SMike Kravetz if (f <= rg->from && t >= rg->to) { /* Remove entire region */ 543feba16e2SMike Kravetz del += rg->to - rg->from; 54496822904SAndy Whitcroft list_del(&rg->link); 54596822904SAndy Whitcroft kfree(rg); 546feba16e2SMike Kravetz continue; 54796822904SAndy Whitcroft } 5487b24d861SDavidlohr Bueso 549feba16e2SMike Kravetz if (f <= rg->from) { /* Trim beginning of region */ 550feba16e2SMike Kravetz del += t - rg->from; 551feba16e2SMike Kravetz rg->from = t; 552feba16e2SMike Kravetz } else { /* Trim end of region */ 553feba16e2SMike Kravetz del += rg->to - f; 554feba16e2SMike Kravetz rg->to = f; 555feba16e2SMike Kravetz } 556feba16e2SMike Kravetz } 557feba16e2SMike Kravetz 5587b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 559feba16e2SMike Kravetz kfree(nrg); 560feba16e2SMike Kravetz return del; 56196822904SAndy Whitcroft } 56296822904SAndy Whitcroft 5631dd308a7SMike Kravetz /* 564b5cec28dSMike Kravetz * A rare out of memory error was encountered which prevented removal of 565b5cec28dSMike Kravetz * the reserve map region for a page. The huge page itself was free'ed 566b5cec28dSMike Kravetz * and removed from the page cache. This routine will adjust the subpool 567b5cec28dSMike Kravetz * usage count, and the global reserve count if needed. By incrementing 568b5cec28dSMike Kravetz * these counts, the reserve map entry which could not be deleted will 569b5cec28dSMike Kravetz * appear as a "reserved" entry instead of simply dangling with incorrect 570b5cec28dSMike Kravetz * counts. 571b5cec28dSMike Kravetz */ 57272e2936cSzhong jiang void hugetlb_fix_reserve_counts(struct inode *inode) 573b5cec28dSMike Kravetz { 574b5cec28dSMike Kravetz struct hugepage_subpool *spool = subpool_inode(inode); 575b5cec28dSMike Kravetz long rsv_adjust; 576b5cec28dSMike Kravetz 577b5cec28dSMike Kravetz rsv_adjust = hugepage_subpool_get_pages(spool, 1); 57872e2936cSzhong jiang if (rsv_adjust) { 579b5cec28dSMike Kravetz struct hstate *h = hstate_inode(inode); 580b5cec28dSMike Kravetz 581b5cec28dSMike Kravetz hugetlb_acct_memory(h, 1); 582b5cec28dSMike Kravetz } 583b5cec28dSMike Kravetz } 584b5cec28dSMike Kravetz 585b5cec28dSMike Kravetz /* 5861dd308a7SMike Kravetz * Count and return the number of huge pages in the reserve map 5871dd308a7SMike Kravetz * that intersect with the range [f, t). 5881dd308a7SMike Kravetz */ 5891406ec9bSJoonsoo Kim static long region_count(struct resv_map *resv, long f, long t) 59084afd99bSAndy Whitcroft { 5911406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 59284afd99bSAndy Whitcroft struct file_region *rg; 59384afd99bSAndy Whitcroft long chg = 0; 59484afd99bSAndy Whitcroft 5957b24d861SDavidlohr Bueso spin_lock(&resv->lock); 59684afd99bSAndy Whitcroft /* Locate each segment we overlap with, and count that overlap. */ 59784afd99bSAndy Whitcroft list_for_each_entry(rg, head, link) { 598f2135a4aSWang Sheng-Hui long seg_from; 599f2135a4aSWang Sheng-Hui long seg_to; 60084afd99bSAndy Whitcroft 60184afd99bSAndy Whitcroft if (rg->to <= f) 60284afd99bSAndy Whitcroft continue; 60384afd99bSAndy Whitcroft if (rg->from >= t) 60484afd99bSAndy Whitcroft break; 60584afd99bSAndy Whitcroft 60684afd99bSAndy Whitcroft seg_from = max(rg->from, f); 60784afd99bSAndy Whitcroft seg_to = min(rg->to, t); 60884afd99bSAndy Whitcroft 60984afd99bSAndy Whitcroft chg += seg_to - seg_from; 61084afd99bSAndy Whitcroft } 6117b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 61284afd99bSAndy Whitcroft 61384afd99bSAndy Whitcroft return chg; 61484afd99bSAndy Whitcroft } 61584afd99bSAndy Whitcroft 61696822904SAndy Whitcroft /* 617e7c4b0bfSAndy Whitcroft * Convert the address within this vma to the page offset within 618e7c4b0bfSAndy Whitcroft * the mapping, in pagecache page units; huge pages here. 619e7c4b0bfSAndy Whitcroft */ 620a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h, 621a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 622e7c4b0bfSAndy Whitcroft { 623a5516438SAndi Kleen return ((address - vma->vm_start) >> huge_page_shift(h)) + 624a5516438SAndi Kleen (vma->vm_pgoff >> huge_page_order(h)); 625e7c4b0bfSAndy Whitcroft } 626e7c4b0bfSAndy Whitcroft 6270fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma, 6280fe6e20bSNaoya Horiguchi unsigned long address) 6290fe6e20bSNaoya Horiguchi { 6300fe6e20bSNaoya Horiguchi return vma_hugecache_offset(hstate_vma(vma), vma, address); 6310fe6e20bSNaoya Horiguchi } 632dee41079SDan Williams EXPORT_SYMBOL_GPL(linear_hugepage_index); 6330fe6e20bSNaoya Horiguchi 63484afd99bSAndy Whitcroft /* 63508fba699SMel Gorman * Return the size of the pages allocated when backing a VMA. In the majority 63608fba699SMel Gorman * cases this will be same size as used by the page table entries. 63708fba699SMel Gorman */ 63808fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) 63908fba699SMel Gorman { 64008fba699SMel Gorman struct hstate *hstate; 64108fba699SMel Gorman 64208fba699SMel Gorman if (!is_vm_hugetlb_page(vma)) 64308fba699SMel Gorman return PAGE_SIZE; 64408fba699SMel Gorman 64508fba699SMel Gorman hstate = hstate_vma(vma); 64608fba699SMel Gorman 6472415cf12SWanpeng Li return 1UL << huge_page_shift(hstate); 64808fba699SMel Gorman } 649f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize); 65008fba699SMel Gorman 65108fba699SMel Gorman /* 6523340289dSMel Gorman * Return the page size being used by the MMU to back a VMA. In the majority 6533340289dSMel Gorman * of cases, the page size used by the kernel matches the MMU size. On 6543340289dSMel Gorman * architectures where it differs, an architecture-specific version of this 6553340289dSMel Gorman * function is required. 6563340289dSMel Gorman */ 6573340289dSMel Gorman #ifndef vma_mmu_pagesize 6583340289dSMel Gorman unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) 6593340289dSMel Gorman { 6603340289dSMel Gorman return vma_kernel_pagesize(vma); 6613340289dSMel Gorman } 6623340289dSMel Gorman #endif 6633340289dSMel Gorman 6643340289dSMel Gorman /* 66584afd99bSAndy Whitcroft * Flags for MAP_PRIVATE reservations. These are stored in the bottom 66684afd99bSAndy Whitcroft * bits of the reservation map pointer, which are always clear due to 66784afd99bSAndy Whitcroft * alignment. 66884afd99bSAndy Whitcroft */ 66984afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER (1UL << 0) 67084afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1) 67104f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) 67284afd99bSAndy Whitcroft 673a1e78772SMel Gorman /* 674a1e78772SMel Gorman * These helpers are used to track how many pages are reserved for 675a1e78772SMel Gorman * faults in a MAP_PRIVATE mapping. Only the process that called mmap() 676a1e78772SMel Gorman * is guaranteed to have their future faults succeed. 677a1e78772SMel Gorman * 678a1e78772SMel Gorman * With the exception of reset_vma_resv_huge_pages() which is called at fork(), 679a1e78772SMel Gorman * the reserve counters are updated with the hugetlb_lock held. It is safe 680a1e78772SMel Gorman * to reset the VMA at fork() time as it is not in use yet and there is no 681a1e78772SMel Gorman * chance of the global counters getting corrupted as a result of the values. 68284afd99bSAndy Whitcroft * 68384afd99bSAndy Whitcroft * The private mapping reservation is represented in a subtly different 68484afd99bSAndy Whitcroft * manner to a shared mapping. A shared mapping has a region map associated 68584afd99bSAndy Whitcroft * with the underlying file, this region map represents the backing file 68684afd99bSAndy Whitcroft * pages which have ever had a reservation assigned which this persists even 68784afd99bSAndy Whitcroft * after the page is instantiated. A private mapping has a region map 68884afd99bSAndy Whitcroft * associated with the original mmap which is attached to all VMAs which 68984afd99bSAndy Whitcroft * reference it, this region map represents those offsets which have consumed 69084afd99bSAndy Whitcroft * reservation ie. where pages have been instantiated. 691a1e78772SMel Gorman */ 692e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma) 693e7c4b0bfSAndy Whitcroft { 694e7c4b0bfSAndy Whitcroft return (unsigned long)vma->vm_private_data; 695e7c4b0bfSAndy Whitcroft } 696e7c4b0bfSAndy Whitcroft 697e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma, 698e7c4b0bfSAndy Whitcroft unsigned long value) 699e7c4b0bfSAndy Whitcroft { 700e7c4b0bfSAndy Whitcroft vma->vm_private_data = (void *)value; 701e7c4b0bfSAndy Whitcroft } 702e7c4b0bfSAndy Whitcroft 7039119a41eSJoonsoo Kim struct resv_map *resv_map_alloc(void) 70484afd99bSAndy Whitcroft { 70584afd99bSAndy Whitcroft struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); 7065e911373SMike Kravetz struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL); 7075e911373SMike Kravetz 7085e911373SMike Kravetz if (!resv_map || !rg) { 7095e911373SMike Kravetz kfree(resv_map); 7105e911373SMike Kravetz kfree(rg); 71184afd99bSAndy Whitcroft return NULL; 7125e911373SMike Kravetz } 71384afd99bSAndy Whitcroft 71484afd99bSAndy Whitcroft kref_init(&resv_map->refs); 7157b24d861SDavidlohr Bueso spin_lock_init(&resv_map->lock); 71684afd99bSAndy Whitcroft INIT_LIST_HEAD(&resv_map->regions); 71784afd99bSAndy Whitcroft 7185e911373SMike Kravetz resv_map->adds_in_progress = 0; 7195e911373SMike Kravetz 7205e911373SMike Kravetz INIT_LIST_HEAD(&resv_map->region_cache); 7215e911373SMike Kravetz list_add(&rg->link, &resv_map->region_cache); 7225e911373SMike Kravetz resv_map->region_cache_count = 1; 7235e911373SMike Kravetz 72484afd99bSAndy Whitcroft return resv_map; 72584afd99bSAndy Whitcroft } 72684afd99bSAndy Whitcroft 7279119a41eSJoonsoo Kim void resv_map_release(struct kref *ref) 72884afd99bSAndy Whitcroft { 72984afd99bSAndy Whitcroft struct resv_map *resv_map = container_of(ref, struct resv_map, refs); 7305e911373SMike Kravetz struct list_head *head = &resv_map->region_cache; 7315e911373SMike Kravetz struct file_region *rg, *trg; 73284afd99bSAndy Whitcroft 73384afd99bSAndy Whitcroft /* Clear out any active regions before we release the map. */ 734feba16e2SMike Kravetz region_del(resv_map, 0, LONG_MAX); 7355e911373SMike Kravetz 7365e911373SMike Kravetz /* ... and any entries left in the cache */ 7375e911373SMike Kravetz list_for_each_entry_safe(rg, trg, head, link) { 7385e911373SMike Kravetz list_del(&rg->link); 7395e911373SMike Kravetz kfree(rg); 7405e911373SMike Kravetz } 7415e911373SMike Kravetz 7425e911373SMike Kravetz VM_BUG_ON(resv_map->adds_in_progress); 7435e911373SMike Kravetz 74484afd99bSAndy Whitcroft kfree(resv_map); 74584afd99bSAndy Whitcroft } 74684afd99bSAndy Whitcroft 7474e35f483SJoonsoo Kim static inline struct resv_map *inode_resv_map(struct inode *inode) 7484e35f483SJoonsoo Kim { 7494e35f483SJoonsoo Kim return inode->i_mapping->private_data; 7504e35f483SJoonsoo Kim } 7514e35f483SJoonsoo Kim 75284afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma) 753a1e78772SMel Gorman { 75481d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 7554e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) { 7564e35f483SJoonsoo Kim struct address_space *mapping = vma->vm_file->f_mapping; 7574e35f483SJoonsoo Kim struct inode *inode = mapping->host; 7584e35f483SJoonsoo Kim 7594e35f483SJoonsoo Kim return inode_resv_map(inode); 7604e35f483SJoonsoo Kim 7614e35f483SJoonsoo Kim } else { 76284afd99bSAndy Whitcroft return (struct resv_map *)(get_vma_private_data(vma) & 76384afd99bSAndy Whitcroft ~HPAGE_RESV_MASK); 7644e35f483SJoonsoo Kim } 765a1e78772SMel Gorman } 766a1e78772SMel Gorman 76784afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) 768a1e78772SMel Gorman { 76981d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 77081d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 771a1e78772SMel Gorman 77284afd99bSAndy Whitcroft set_vma_private_data(vma, (get_vma_private_data(vma) & 77384afd99bSAndy Whitcroft HPAGE_RESV_MASK) | (unsigned long)map); 77404f2cbe3SMel Gorman } 77504f2cbe3SMel Gorman 77604f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) 77704f2cbe3SMel Gorman { 77881d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 77981d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 780e7c4b0bfSAndy Whitcroft 781e7c4b0bfSAndy Whitcroft set_vma_private_data(vma, get_vma_private_data(vma) | flags); 78204f2cbe3SMel Gorman } 78304f2cbe3SMel Gorman 78404f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) 78504f2cbe3SMel Gorman { 78681d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 787e7c4b0bfSAndy Whitcroft 788e7c4b0bfSAndy Whitcroft return (get_vma_private_data(vma) & flag) != 0; 789a1e78772SMel Gorman } 790a1e78772SMel Gorman 79104f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ 792a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma) 793a1e78772SMel Gorman { 79481d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 795f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 796a1e78772SMel Gorman vma->vm_private_data = (void *)0; 797a1e78772SMel Gorman } 798a1e78772SMel Gorman 799a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */ 800559ec2f8SNicholas Krause static bool vma_has_reserves(struct vm_area_struct *vma, long chg) 801a1e78772SMel Gorman { 802af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_NORESERVE) { 803af0ed73eSJoonsoo Kim /* 804af0ed73eSJoonsoo Kim * This address is already reserved by other process(chg == 0), 805af0ed73eSJoonsoo Kim * so, we should decrement reserved count. Without decrementing, 806af0ed73eSJoonsoo Kim * reserve count remains after releasing inode, because this 807af0ed73eSJoonsoo Kim * allocated page will go into page cache and is regarded as 808af0ed73eSJoonsoo Kim * coming from reserved pool in releasing step. Currently, we 809af0ed73eSJoonsoo Kim * don't have any other solution to deal with this situation 810af0ed73eSJoonsoo Kim * properly, so add work-around here. 811af0ed73eSJoonsoo Kim */ 812af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE && chg == 0) 813559ec2f8SNicholas Krause return true; 814af0ed73eSJoonsoo Kim else 815559ec2f8SNicholas Krause return false; 816af0ed73eSJoonsoo Kim } 817a63884e9SJoonsoo Kim 818a63884e9SJoonsoo Kim /* Shared mappings always use reserves */ 8191fb1b0e9SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 8201fb1b0e9SMike Kravetz /* 8211fb1b0e9SMike Kravetz * We know VM_NORESERVE is not set. Therefore, there SHOULD 8221fb1b0e9SMike Kravetz * be a region map for all pages. The only situation where 8231fb1b0e9SMike Kravetz * there is no region map is if a hole was punched via 8241fb1b0e9SMike Kravetz * fallocate. In this case, there really are no reverves to 8251fb1b0e9SMike Kravetz * use. This situation is indicated if chg != 0. 8261fb1b0e9SMike Kravetz */ 8271fb1b0e9SMike Kravetz if (chg) 8281fb1b0e9SMike Kravetz return false; 8291fb1b0e9SMike Kravetz else 830559ec2f8SNicholas Krause return true; 8311fb1b0e9SMike Kravetz } 832a63884e9SJoonsoo Kim 833a63884e9SJoonsoo Kim /* 834a63884e9SJoonsoo Kim * Only the process that called mmap() has reserves for 835a63884e9SJoonsoo Kim * private mappings. 836a63884e9SJoonsoo Kim */ 83767961f9dSMike Kravetz if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { 83867961f9dSMike Kravetz /* 83967961f9dSMike Kravetz * Like the shared case above, a hole punch or truncate 84067961f9dSMike Kravetz * could have been performed on the private mapping. 84167961f9dSMike Kravetz * Examine the value of chg to determine if reserves 84267961f9dSMike Kravetz * actually exist or were previously consumed. 84367961f9dSMike Kravetz * Very Subtle - The value of chg comes from a previous 84467961f9dSMike Kravetz * call to vma_needs_reserves(). The reserve map for 84567961f9dSMike Kravetz * private mappings has different (opposite) semantics 84667961f9dSMike Kravetz * than that of shared mappings. vma_needs_reserves() 84767961f9dSMike Kravetz * has already taken this difference in semantics into 84867961f9dSMike Kravetz * account. Therefore, the meaning of chg is the same 84967961f9dSMike Kravetz * as in the shared case above. Code could easily be 85067961f9dSMike Kravetz * combined, but keeping it separate draws attention to 85167961f9dSMike Kravetz * subtle differences. 85267961f9dSMike Kravetz */ 85367961f9dSMike Kravetz if (chg) 85467961f9dSMike Kravetz return false; 85567961f9dSMike Kravetz else 856559ec2f8SNicholas Krause return true; 85767961f9dSMike Kravetz } 858a63884e9SJoonsoo Kim 859559ec2f8SNicholas Krause return false; 860a1e78772SMel Gorman } 861a1e78772SMel Gorman 862a5516438SAndi Kleen static void enqueue_huge_page(struct hstate *h, struct page *page) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds int nid = page_to_nid(page); 8650edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_freelists[nid]); 866a5516438SAndi Kleen h->free_huge_pages++; 867a5516438SAndi Kleen h->free_huge_pages_node[nid]++; 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds 870bf50bab2SNaoya Horiguchi static struct page *dequeue_huge_page_node(struct hstate *h, int nid) 871bf50bab2SNaoya Horiguchi { 872bf50bab2SNaoya Horiguchi struct page *page; 873bf50bab2SNaoya Horiguchi 874c8721bbbSNaoya Horiguchi list_for_each_entry(page, &h->hugepage_freelists[nid], lru) 875c8721bbbSNaoya Horiguchi if (!is_migrate_isolate_page(page)) 876c8721bbbSNaoya Horiguchi break; 877c8721bbbSNaoya Horiguchi /* 878c8721bbbSNaoya Horiguchi * if 'non-isolated free hugepage' not found on the list, 879c8721bbbSNaoya Horiguchi * the allocation fails. 880c8721bbbSNaoya Horiguchi */ 881c8721bbbSNaoya Horiguchi if (&h->hugepage_freelists[nid] == &page->lru) 882bf50bab2SNaoya Horiguchi return NULL; 8830edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 884a9869b83SNaoya Horiguchi set_page_refcounted(page); 885bf50bab2SNaoya Horiguchi h->free_huge_pages--; 886bf50bab2SNaoya Horiguchi h->free_huge_pages_node[nid]--; 887bf50bab2SNaoya Horiguchi return page; 888bf50bab2SNaoya Horiguchi } 889bf50bab2SNaoya Horiguchi 89086cdb465SNaoya Horiguchi /* Movability of hugepages depends on migration support. */ 89186cdb465SNaoya Horiguchi static inline gfp_t htlb_alloc_mask(struct hstate *h) 89286cdb465SNaoya Horiguchi { 893100873d7SNaoya Horiguchi if (hugepages_treat_as_movable || hugepage_migration_supported(h)) 89486cdb465SNaoya Horiguchi return GFP_HIGHUSER_MOVABLE; 89586cdb465SNaoya Horiguchi else 89686cdb465SNaoya Horiguchi return GFP_HIGHUSER; 89786cdb465SNaoya Horiguchi } 89886cdb465SNaoya Horiguchi 899a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h, 900a5516438SAndi Kleen struct vm_area_struct *vma, 901af0ed73eSJoonsoo Kim unsigned long address, int avoid_reserve, 902af0ed73eSJoonsoo Kim long chg) 9031da177e4SLinus Torvalds { 904b1c12cbcSKonstantin Khlebnikov struct page *page = NULL; 905480eccf9SLee Schermerhorn struct mempolicy *mpol; 90619770b32SMel Gorman nodemask_t *nodemask; 907c0ff7453SMiao Xie struct zonelist *zonelist; 908dd1a239fSMel Gorman struct zone *zone; 909dd1a239fSMel Gorman struct zoneref *z; 910cc9a6c87SMel Gorman unsigned int cpuset_mems_cookie; 9111da177e4SLinus Torvalds 912a1e78772SMel Gorman /* 913a1e78772SMel Gorman * A child process with MAP_PRIVATE mappings created by their parent 914a1e78772SMel Gorman * have no page reserves. This check ensures that reservations are 915a1e78772SMel Gorman * not "stolen". The child may still get SIGKILLed 916a1e78772SMel Gorman */ 917af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg) && 918a5516438SAndi Kleen h->free_huge_pages - h->resv_huge_pages == 0) 919c0ff7453SMiao Xie goto err; 920a1e78772SMel Gorman 92104f2cbe3SMel Gorman /* If reserves cannot be used, ensure enough pages are in the pool */ 922a5516438SAndi Kleen if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0) 9236eab04a8SJustin P. Mattock goto err; 92404f2cbe3SMel Gorman 9259966c4bbSJoonsoo Kim retry_cpuset: 926d26914d1SMel Gorman cpuset_mems_cookie = read_mems_allowed_begin(); 9279966c4bbSJoonsoo Kim zonelist = huge_zonelist(vma, address, 92886cdb465SNaoya Horiguchi htlb_alloc_mask(h), &mpol, &nodemask); 9299966c4bbSJoonsoo Kim 93019770b32SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 93119770b32SMel Gorman MAX_NR_ZONES - 1, nodemask) { 932344736f2SVladimir Davydov if (cpuset_zone_allowed(zone, htlb_alloc_mask(h))) { 933bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, zone_to_nid(zone)); 934bf50bab2SNaoya Horiguchi if (page) { 935af0ed73eSJoonsoo Kim if (avoid_reserve) 936af0ed73eSJoonsoo Kim break; 937af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg)) 938af0ed73eSJoonsoo Kim break; 939af0ed73eSJoonsoo Kim 94007443a85SJoonsoo Kim SetPagePrivate(page); 941a63884e9SJoonsoo Kim h->resv_huge_pages--; 9425ab3ee7bSKen Chen break; 9431da177e4SLinus Torvalds } 9443abf7afdSAndrew Morton } 945bf50bab2SNaoya Horiguchi } 946cc9a6c87SMel Gorman 947cc9a6c87SMel Gorman mpol_cond_put(mpol); 948d26914d1SMel Gorman if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie))) 949cc9a6c87SMel Gorman goto retry_cpuset; 950cc9a6c87SMel Gorman return page; 951cc9a6c87SMel Gorman 952c0ff7453SMiao Xie err: 953cc9a6c87SMel Gorman return NULL; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds 9561cac6f2cSLuiz Capitulino /* 9571cac6f2cSLuiz Capitulino * common helper functions for hstate_next_node_to_{alloc|free}. 9581cac6f2cSLuiz Capitulino * We may have allocated or freed a huge page based on a different 9591cac6f2cSLuiz Capitulino * nodes_allowed previously, so h->next_node_to_{alloc|free} might 9601cac6f2cSLuiz Capitulino * be outside of *nodes_allowed. Ensure that we use an allowed 9611cac6f2cSLuiz Capitulino * node for alloc or free. 9621cac6f2cSLuiz Capitulino */ 9631cac6f2cSLuiz Capitulino static int next_node_allowed(int nid, nodemask_t *nodes_allowed) 9641cac6f2cSLuiz Capitulino { 9650edaf86cSAndrew Morton nid = next_node_in(nid, *nodes_allowed); 9661cac6f2cSLuiz Capitulino VM_BUG_ON(nid >= MAX_NUMNODES); 9671cac6f2cSLuiz Capitulino 9681cac6f2cSLuiz Capitulino return nid; 9691cac6f2cSLuiz Capitulino } 9701cac6f2cSLuiz Capitulino 9711cac6f2cSLuiz Capitulino static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) 9721cac6f2cSLuiz Capitulino { 9731cac6f2cSLuiz Capitulino if (!node_isset(nid, *nodes_allowed)) 9741cac6f2cSLuiz Capitulino nid = next_node_allowed(nid, nodes_allowed); 9751cac6f2cSLuiz Capitulino return nid; 9761cac6f2cSLuiz Capitulino } 9771cac6f2cSLuiz Capitulino 9781cac6f2cSLuiz Capitulino /* 9791cac6f2cSLuiz Capitulino * returns the previously saved node ["this node"] from which to 9801cac6f2cSLuiz Capitulino * allocate a persistent huge page for the pool and advance the 9811cac6f2cSLuiz Capitulino * next node from which to allocate, handling wrap at end of node 9821cac6f2cSLuiz Capitulino * mask. 9831cac6f2cSLuiz Capitulino */ 9841cac6f2cSLuiz Capitulino static int hstate_next_node_to_alloc(struct hstate *h, 9851cac6f2cSLuiz Capitulino nodemask_t *nodes_allowed) 9861cac6f2cSLuiz Capitulino { 9871cac6f2cSLuiz Capitulino int nid; 9881cac6f2cSLuiz Capitulino 9891cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 9901cac6f2cSLuiz Capitulino 9911cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); 9921cac6f2cSLuiz Capitulino h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); 9931cac6f2cSLuiz Capitulino 9941cac6f2cSLuiz Capitulino return nid; 9951cac6f2cSLuiz Capitulino } 9961cac6f2cSLuiz Capitulino 9971cac6f2cSLuiz Capitulino /* 9981cac6f2cSLuiz Capitulino * helper for free_pool_huge_page() - return the previously saved 9991cac6f2cSLuiz Capitulino * node ["this node"] from which to free a huge page. Advance the 10001cac6f2cSLuiz Capitulino * next node id whether or not we find a free huge page to free so 10011cac6f2cSLuiz Capitulino * that the next attempt to free addresses the next node. 10021cac6f2cSLuiz Capitulino */ 10031cac6f2cSLuiz Capitulino static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) 10041cac6f2cSLuiz Capitulino { 10051cac6f2cSLuiz Capitulino int nid; 10061cac6f2cSLuiz Capitulino 10071cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 10081cac6f2cSLuiz Capitulino 10091cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); 10101cac6f2cSLuiz Capitulino h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); 10111cac6f2cSLuiz Capitulino 10121cac6f2cSLuiz Capitulino return nid; 10131cac6f2cSLuiz Capitulino } 10141cac6f2cSLuiz Capitulino 10151cac6f2cSLuiz Capitulino #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \ 10161cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 10171cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 10181cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ 10191cac6f2cSLuiz Capitulino nr_nodes--) 10201cac6f2cSLuiz Capitulino 10211cac6f2cSLuiz Capitulino #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ 10221cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 10231cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 10241cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_free(hs, mask)) || 1); \ 10251cac6f2cSLuiz Capitulino nr_nodes--) 10261cac6f2cSLuiz Capitulino 1027461a7184SYisheng Xie #if defined(CONFIG_ARCH_HAS_GIGANTIC_PAGE) && \ 1028d08de8e2SGerald Schaefer ((defined(CONFIG_MEMORY_ISOLATION) && defined(CONFIG_COMPACTION)) || \ 1029d08de8e2SGerald Schaefer defined(CONFIG_CMA)) 1030944d9fecSLuiz Capitulino static void destroy_compound_gigantic_page(struct page *page, 1031d00181b9SKirill A. Shutemov unsigned int order) 1032944d9fecSLuiz Capitulino { 1033944d9fecSLuiz Capitulino int i; 1034944d9fecSLuiz Capitulino int nr_pages = 1 << order; 1035944d9fecSLuiz Capitulino struct page *p = page + 1; 1036944d9fecSLuiz Capitulino 1037c8cc708aSGerald Schaefer atomic_set(compound_mapcount_ptr(page), 0); 1038944d9fecSLuiz Capitulino for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 10391d798ca3SKirill A. Shutemov clear_compound_head(p); 1040944d9fecSLuiz Capitulino set_page_refcounted(p); 1041944d9fecSLuiz Capitulino } 1042944d9fecSLuiz Capitulino 1043944d9fecSLuiz Capitulino set_compound_order(page, 0); 1044944d9fecSLuiz Capitulino __ClearPageHead(page); 1045944d9fecSLuiz Capitulino } 1046944d9fecSLuiz Capitulino 1047d00181b9SKirill A. Shutemov static void free_gigantic_page(struct page *page, unsigned int order) 1048944d9fecSLuiz Capitulino { 1049944d9fecSLuiz Capitulino free_contig_range(page_to_pfn(page), 1 << order); 1050944d9fecSLuiz Capitulino } 1051944d9fecSLuiz Capitulino 1052944d9fecSLuiz Capitulino static int __alloc_gigantic_page(unsigned long start_pfn, 1053944d9fecSLuiz Capitulino unsigned long nr_pages) 1054944d9fecSLuiz Capitulino { 1055944d9fecSLuiz Capitulino unsigned long end_pfn = start_pfn + nr_pages; 1056ca96b625SLucas Stach return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE, 1057ca96b625SLucas Stach GFP_KERNEL); 1058944d9fecSLuiz Capitulino } 1059944d9fecSLuiz Capitulino 1060f44b2ddaSJoonsoo Kim static bool pfn_range_valid_gigantic(struct zone *z, 1061f44b2ddaSJoonsoo Kim unsigned long start_pfn, unsigned long nr_pages) 1062944d9fecSLuiz Capitulino { 1063944d9fecSLuiz Capitulino unsigned long i, end_pfn = start_pfn + nr_pages; 1064944d9fecSLuiz Capitulino struct page *page; 1065944d9fecSLuiz Capitulino 1066944d9fecSLuiz Capitulino for (i = start_pfn; i < end_pfn; i++) { 1067944d9fecSLuiz Capitulino if (!pfn_valid(i)) 1068944d9fecSLuiz Capitulino return false; 1069944d9fecSLuiz Capitulino 1070944d9fecSLuiz Capitulino page = pfn_to_page(i); 1071944d9fecSLuiz Capitulino 1072f44b2ddaSJoonsoo Kim if (page_zone(page) != z) 1073f44b2ddaSJoonsoo Kim return false; 1074f44b2ddaSJoonsoo Kim 1075944d9fecSLuiz Capitulino if (PageReserved(page)) 1076944d9fecSLuiz Capitulino return false; 1077944d9fecSLuiz Capitulino 1078944d9fecSLuiz Capitulino if (page_count(page) > 0) 1079944d9fecSLuiz Capitulino return false; 1080944d9fecSLuiz Capitulino 1081944d9fecSLuiz Capitulino if (PageHuge(page)) 1082944d9fecSLuiz Capitulino return false; 1083944d9fecSLuiz Capitulino } 1084944d9fecSLuiz Capitulino 1085944d9fecSLuiz Capitulino return true; 1086944d9fecSLuiz Capitulino } 1087944d9fecSLuiz Capitulino 1088944d9fecSLuiz Capitulino static bool zone_spans_last_pfn(const struct zone *zone, 1089944d9fecSLuiz Capitulino unsigned long start_pfn, unsigned long nr_pages) 1090944d9fecSLuiz Capitulino { 1091944d9fecSLuiz Capitulino unsigned long last_pfn = start_pfn + nr_pages - 1; 1092944d9fecSLuiz Capitulino return zone_spans_pfn(zone, last_pfn); 1093944d9fecSLuiz Capitulino } 1094944d9fecSLuiz Capitulino 1095d00181b9SKirill A. Shutemov static struct page *alloc_gigantic_page(int nid, unsigned int order) 1096944d9fecSLuiz Capitulino { 1097944d9fecSLuiz Capitulino unsigned long nr_pages = 1 << order; 1098944d9fecSLuiz Capitulino unsigned long ret, pfn, flags; 1099944d9fecSLuiz Capitulino struct zone *z; 1100944d9fecSLuiz Capitulino 1101944d9fecSLuiz Capitulino z = NODE_DATA(nid)->node_zones; 1102944d9fecSLuiz Capitulino for (; z - NODE_DATA(nid)->node_zones < MAX_NR_ZONES; z++) { 1103944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 1104944d9fecSLuiz Capitulino 1105944d9fecSLuiz Capitulino pfn = ALIGN(z->zone_start_pfn, nr_pages); 1106944d9fecSLuiz Capitulino while (zone_spans_last_pfn(z, pfn, nr_pages)) { 1107f44b2ddaSJoonsoo Kim if (pfn_range_valid_gigantic(z, pfn, nr_pages)) { 1108944d9fecSLuiz Capitulino /* 1109944d9fecSLuiz Capitulino * We release the zone lock here because 1110944d9fecSLuiz Capitulino * alloc_contig_range() will also lock the zone 1111944d9fecSLuiz Capitulino * at some point. If there's an allocation 1112944d9fecSLuiz Capitulino * spinning on this lock, it may win the race 1113944d9fecSLuiz Capitulino * and cause alloc_contig_range() to fail... 1114944d9fecSLuiz Capitulino */ 1115944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 1116944d9fecSLuiz Capitulino ret = __alloc_gigantic_page(pfn, nr_pages); 1117944d9fecSLuiz Capitulino if (!ret) 1118944d9fecSLuiz Capitulino return pfn_to_page(pfn); 1119944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 1120944d9fecSLuiz Capitulino } 1121944d9fecSLuiz Capitulino pfn += nr_pages; 1122944d9fecSLuiz Capitulino } 1123944d9fecSLuiz Capitulino 1124944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 1125944d9fecSLuiz Capitulino } 1126944d9fecSLuiz Capitulino 1127944d9fecSLuiz Capitulino return NULL; 1128944d9fecSLuiz Capitulino } 1129944d9fecSLuiz Capitulino 1130944d9fecSLuiz Capitulino static void prep_new_huge_page(struct hstate *h, struct page *page, int nid); 1131d00181b9SKirill A. Shutemov static void prep_compound_gigantic_page(struct page *page, unsigned int order); 1132944d9fecSLuiz Capitulino 1133944d9fecSLuiz Capitulino static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid) 1134944d9fecSLuiz Capitulino { 1135944d9fecSLuiz Capitulino struct page *page; 1136944d9fecSLuiz Capitulino 1137944d9fecSLuiz Capitulino page = alloc_gigantic_page(nid, huge_page_order(h)); 1138944d9fecSLuiz Capitulino if (page) { 1139944d9fecSLuiz Capitulino prep_compound_gigantic_page(page, huge_page_order(h)); 1140944d9fecSLuiz Capitulino prep_new_huge_page(h, page, nid); 1141944d9fecSLuiz Capitulino } 1142944d9fecSLuiz Capitulino 1143944d9fecSLuiz Capitulino return page; 1144944d9fecSLuiz Capitulino } 1145944d9fecSLuiz Capitulino 1146944d9fecSLuiz Capitulino static int alloc_fresh_gigantic_page(struct hstate *h, 1147944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) 1148944d9fecSLuiz Capitulino { 1149944d9fecSLuiz Capitulino struct page *page = NULL; 1150944d9fecSLuiz Capitulino int nr_nodes, node; 1151944d9fecSLuiz Capitulino 1152944d9fecSLuiz Capitulino for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1153944d9fecSLuiz Capitulino page = alloc_fresh_gigantic_page_node(h, node); 1154944d9fecSLuiz Capitulino if (page) 1155944d9fecSLuiz Capitulino return 1; 1156944d9fecSLuiz Capitulino } 1157944d9fecSLuiz Capitulino 1158944d9fecSLuiz Capitulino return 0; 1159944d9fecSLuiz Capitulino } 1160944d9fecSLuiz Capitulino 1161944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return true; } 1162944d9fecSLuiz Capitulino #else 1163944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return false; } 1164d00181b9SKirill A. Shutemov static inline void free_gigantic_page(struct page *page, unsigned int order) { } 1165944d9fecSLuiz Capitulino static inline void destroy_compound_gigantic_page(struct page *page, 1166d00181b9SKirill A. Shutemov unsigned int order) { } 1167944d9fecSLuiz Capitulino static inline int alloc_fresh_gigantic_page(struct hstate *h, 1168944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) { return 0; } 1169944d9fecSLuiz Capitulino #endif 1170944d9fecSLuiz Capitulino 1171a5516438SAndi Kleen static void update_and_free_page(struct hstate *h, struct page *page) 11726af2acb6SAdam Litke { 11736af2acb6SAdam Litke int i; 1174a5516438SAndi Kleen 1175944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 1176944d9fecSLuiz Capitulino return; 117718229df5SAndy Whitcroft 1178a5516438SAndi Kleen h->nr_huge_pages--; 1179a5516438SAndi Kleen h->nr_huge_pages_node[page_to_nid(page)]--; 1180a5516438SAndi Kleen for (i = 0; i < pages_per_huge_page(h); i++) { 118132f84528SChris Forbes page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 118232f84528SChris Forbes 1 << PG_referenced | 1 << PG_dirty | 1183a7407a27SLuiz Capitulino 1 << PG_active | 1 << PG_private | 1184a7407a27SLuiz Capitulino 1 << PG_writeback); 11856af2acb6SAdam Litke } 1186309381feSSasha Levin VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page); 1187f1e61557SKirill A. Shutemov set_compound_page_dtor(page, NULL_COMPOUND_DTOR); 11886af2acb6SAdam Litke set_page_refcounted(page); 1189944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) { 1190944d9fecSLuiz Capitulino destroy_compound_gigantic_page(page, huge_page_order(h)); 1191944d9fecSLuiz Capitulino free_gigantic_page(page, huge_page_order(h)); 1192944d9fecSLuiz Capitulino } else { 1193a5516438SAndi Kleen __free_pages(page, huge_page_order(h)); 11946af2acb6SAdam Litke } 1195944d9fecSLuiz Capitulino } 11966af2acb6SAdam Litke 1197e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size) 1198e5ff2159SAndi Kleen { 1199e5ff2159SAndi Kleen struct hstate *h; 1200e5ff2159SAndi Kleen 1201e5ff2159SAndi Kleen for_each_hstate(h) { 1202e5ff2159SAndi Kleen if (huge_page_size(h) == size) 1203e5ff2159SAndi Kleen return h; 1204e5ff2159SAndi Kleen } 1205e5ff2159SAndi Kleen return NULL; 1206e5ff2159SAndi Kleen } 1207e5ff2159SAndi Kleen 1208bcc54222SNaoya Horiguchi /* 1209bcc54222SNaoya Horiguchi * Test to determine whether the hugepage is "active/in-use" (i.e. being linked 1210bcc54222SNaoya Horiguchi * to hstate->hugepage_activelist.) 1211bcc54222SNaoya Horiguchi * 1212bcc54222SNaoya Horiguchi * This function can be called for tail pages, but never returns true for them. 1213bcc54222SNaoya Horiguchi */ 1214bcc54222SNaoya Horiguchi bool page_huge_active(struct page *page) 1215bcc54222SNaoya Horiguchi { 1216bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHuge(page), page); 1217bcc54222SNaoya Horiguchi return PageHead(page) && PagePrivate(&page[1]); 1218bcc54222SNaoya Horiguchi } 1219bcc54222SNaoya Horiguchi 1220bcc54222SNaoya Horiguchi /* never called for tail page */ 1221bcc54222SNaoya Horiguchi static void set_page_huge_active(struct page *page) 1222bcc54222SNaoya Horiguchi { 1223bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 1224bcc54222SNaoya Horiguchi SetPagePrivate(&page[1]); 1225bcc54222SNaoya Horiguchi } 1226bcc54222SNaoya Horiguchi 1227bcc54222SNaoya Horiguchi static void clear_page_huge_active(struct page *page) 1228bcc54222SNaoya Horiguchi { 1229bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 1230bcc54222SNaoya Horiguchi ClearPagePrivate(&page[1]); 1231bcc54222SNaoya Horiguchi } 1232bcc54222SNaoya Horiguchi 12338f1d26d0SAtsushi Kumagai void free_huge_page(struct page *page) 123427a85ef1SDavid Gibson { 1235a5516438SAndi Kleen /* 1236a5516438SAndi Kleen * Can't pass hstate in here because it is called from the 1237a5516438SAndi Kleen * compound page destructor. 1238a5516438SAndi Kleen */ 1239e5ff2159SAndi Kleen struct hstate *h = page_hstate(page); 12407893d1d5SAdam Litke int nid = page_to_nid(page); 124190481622SDavid Gibson struct hugepage_subpool *spool = 124290481622SDavid Gibson (struct hugepage_subpool *)page_private(page); 124307443a85SJoonsoo Kim bool restore_reserve; 124427a85ef1SDavid Gibson 1245e5df70abSAndy Whitcroft set_page_private(page, 0); 124623be7468SMel Gorman page->mapping = NULL; 1247b4330afbSMike Kravetz VM_BUG_ON_PAGE(page_count(page), page); 1248b4330afbSMike Kravetz VM_BUG_ON_PAGE(page_mapcount(page), page); 124907443a85SJoonsoo Kim restore_reserve = PagePrivate(page); 125016c794b4SJoonsoo Kim ClearPagePrivate(page); 125127a85ef1SDavid Gibson 12521c5ecae3SMike Kravetz /* 12531c5ecae3SMike Kravetz * A return code of zero implies that the subpool will be under its 12541c5ecae3SMike Kravetz * minimum size if the reservation is not restored after page is free. 12551c5ecae3SMike Kravetz * Therefore, force restore_reserve operation. 12561c5ecae3SMike Kravetz */ 12571c5ecae3SMike Kravetz if (hugepage_subpool_put_pages(spool, 1) == 0) 12581c5ecae3SMike Kravetz restore_reserve = true; 12591c5ecae3SMike Kravetz 126027a85ef1SDavid Gibson spin_lock(&hugetlb_lock); 1261bcc54222SNaoya Horiguchi clear_page_huge_active(page); 12626d76dcf4SAneesh Kumar K.V hugetlb_cgroup_uncharge_page(hstate_index(h), 12636d76dcf4SAneesh Kumar K.V pages_per_huge_page(h), page); 126407443a85SJoonsoo Kim if (restore_reserve) 126507443a85SJoonsoo Kim h->resv_huge_pages++; 126607443a85SJoonsoo Kim 1267944d9fecSLuiz Capitulino if (h->surplus_huge_pages_node[nid]) { 12680edaecfaSAneesh Kumar K.V /* remove the page from active list */ 12690edaecfaSAneesh Kumar K.V list_del(&page->lru); 1270a5516438SAndi Kleen update_and_free_page(h, page); 1271a5516438SAndi Kleen h->surplus_huge_pages--; 1272a5516438SAndi Kleen h->surplus_huge_pages_node[nid]--; 12737893d1d5SAdam Litke } else { 12745d3a551cSWill Deacon arch_clear_hugepage_flags(page); 1275a5516438SAndi Kleen enqueue_huge_page(h, page); 12767893d1d5SAdam Litke } 127727a85ef1SDavid Gibson spin_unlock(&hugetlb_lock); 127827a85ef1SDavid Gibson } 127927a85ef1SDavid Gibson 1280a5516438SAndi Kleen static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) 1281b7ba30c6SAndi Kleen { 12820edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1283f1e61557SKirill A. Shutemov set_compound_page_dtor(page, HUGETLB_PAGE_DTOR); 1284b7ba30c6SAndi Kleen spin_lock(&hugetlb_lock); 12859dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1286a5516438SAndi Kleen h->nr_huge_pages++; 1287a5516438SAndi Kleen h->nr_huge_pages_node[nid]++; 1288b7ba30c6SAndi Kleen spin_unlock(&hugetlb_lock); 1289b7ba30c6SAndi Kleen put_page(page); /* free it into the hugepage allocator */ 1290b7ba30c6SAndi Kleen } 1291b7ba30c6SAndi Kleen 1292d00181b9SKirill A. Shutemov static void prep_compound_gigantic_page(struct page *page, unsigned int order) 129320a0307cSWu Fengguang { 129420a0307cSWu Fengguang int i; 129520a0307cSWu Fengguang int nr_pages = 1 << order; 129620a0307cSWu Fengguang struct page *p = page + 1; 129720a0307cSWu Fengguang 129820a0307cSWu Fengguang /* we rely on prep_new_huge_page to set the destructor */ 129920a0307cSWu Fengguang set_compound_order(page, order); 1300ef5a22beSAndrea Arcangeli __ClearPageReserved(page); 1301de09d31dSKirill A. Shutemov __SetPageHead(page); 130220a0307cSWu Fengguang for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 1303ef5a22beSAndrea Arcangeli /* 1304ef5a22beSAndrea Arcangeli * For gigantic hugepages allocated through bootmem at 1305ef5a22beSAndrea Arcangeli * boot, it's safer to be consistent with the not-gigantic 1306ef5a22beSAndrea Arcangeli * hugepages and clear the PG_reserved bit from all tail pages 1307ef5a22beSAndrea Arcangeli * too. Otherwse drivers using get_user_pages() to access tail 1308ef5a22beSAndrea Arcangeli * pages may get the reference counting wrong if they see 1309ef5a22beSAndrea Arcangeli * PG_reserved set on a tail page (despite the head page not 1310ef5a22beSAndrea Arcangeli * having PG_reserved set). Enforcing this consistency between 1311ef5a22beSAndrea Arcangeli * head and tail pages allows drivers to optimize away a check 1312ef5a22beSAndrea Arcangeli * on the head page when they need know if put_page() is needed 1313ef5a22beSAndrea Arcangeli * after get_user_pages(). 1314ef5a22beSAndrea Arcangeli */ 1315ef5a22beSAndrea Arcangeli __ClearPageReserved(p); 131658a84aa9SYouquan Song set_page_count(p, 0); 13171d798ca3SKirill A. Shutemov set_compound_head(p, page); 131820a0307cSWu Fengguang } 1319b4330afbSMike Kravetz atomic_set(compound_mapcount_ptr(page), -1); 132020a0307cSWu Fengguang } 132120a0307cSWu Fengguang 13227795912cSAndrew Morton /* 13237795912cSAndrew Morton * PageHuge() only returns true for hugetlbfs pages, but not for normal or 13247795912cSAndrew Morton * transparent huge pages. See the PageTransHuge() documentation for more 13257795912cSAndrew Morton * details. 13267795912cSAndrew Morton */ 132720a0307cSWu Fengguang int PageHuge(struct page *page) 132820a0307cSWu Fengguang { 132920a0307cSWu Fengguang if (!PageCompound(page)) 133020a0307cSWu Fengguang return 0; 133120a0307cSWu Fengguang 133220a0307cSWu Fengguang page = compound_head(page); 1333f1e61557SKirill A. Shutemov return page[1].compound_dtor == HUGETLB_PAGE_DTOR; 133420a0307cSWu Fengguang } 133543131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge); 133643131e14SNaoya Horiguchi 133727c73ae7SAndrea Arcangeli /* 133827c73ae7SAndrea Arcangeli * PageHeadHuge() only returns true for hugetlbfs head page, but not for 133927c73ae7SAndrea Arcangeli * normal or transparent huge pages. 134027c73ae7SAndrea Arcangeli */ 134127c73ae7SAndrea Arcangeli int PageHeadHuge(struct page *page_head) 134227c73ae7SAndrea Arcangeli { 134327c73ae7SAndrea Arcangeli if (!PageHead(page_head)) 134427c73ae7SAndrea Arcangeli return 0; 134527c73ae7SAndrea Arcangeli 1346758f66a2SAndrew Morton return get_compound_page_dtor(page_head) == free_huge_page; 134727c73ae7SAndrea Arcangeli } 134827c73ae7SAndrea Arcangeli 134913d60f4bSZhang Yi pgoff_t __basepage_index(struct page *page) 135013d60f4bSZhang Yi { 135113d60f4bSZhang Yi struct page *page_head = compound_head(page); 135213d60f4bSZhang Yi pgoff_t index = page_index(page_head); 135313d60f4bSZhang Yi unsigned long compound_idx; 135413d60f4bSZhang Yi 135513d60f4bSZhang Yi if (!PageHuge(page_head)) 135613d60f4bSZhang Yi return page_index(page); 135713d60f4bSZhang Yi 135813d60f4bSZhang Yi if (compound_order(page_head) >= MAX_ORDER) 135913d60f4bSZhang Yi compound_idx = page_to_pfn(page) - page_to_pfn(page_head); 136013d60f4bSZhang Yi else 136113d60f4bSZhang Yi compound_idx = page - page_head; 136213d60f4bSZhang Yi 136313d60f4bSZhang Yi return (index << compound_order(page_head)) + compound_idx; 136413d60f4bSZhang Yi } 136513d60f4bSZhang Yi 1366a5516438SAndi Kleen static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid) 13671da177e4SLinus Torvalds { 13681da177e4SLinus Torvalds struct page *page; 1369f96efd58SJoe Jin 137096db800fSVlastimil Babka page = __alloc_pages_node(nid, 137186cdb465SNaoya Horiguchi htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| 1372551883aeSNishanth Aravamudan __GFP_REPEAT|__GFP_NOWARN, 1373a5516438SAndi Kleen huge_page_order(h)); 13741da177e4SLinus Torvalds if (page) { 1375a5516438SAndi Kleen prep_new_huge_page(h, page, nid); 13761da177e4SLinus Torvalds } 137763b4613cSNishanth Aravamudan 137863b4613cSNishanth Aravamudan return page; 137963b4613cSNishanth Aravamudan } 138063b4613cSNishanth Aravamudan 1381b2261026SJoonsoo Kim static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed) 1382b2261026SJoonsoo Kim { 1383b2261026SJoonsoo Kim struct page *page; 1384b2261026SJoonsoo Kim int nr_nodes, node; 1385b2261026SJoonsoo Kim int ret = 0; 1386b2261026SJoonsoo Kim 1387b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1388b2261026SJoonsoo Kim page = alloc_fresh_huge_page_node(h, node); 1389b2261026SJoonsoo Kim if (page) { 1390b2261026SJoonsoo Kim ret = 1; 1391b2261026SJoonsoo Kim break; 1392b2261026SJoonsoo Kim } 1393b2261026SJoonsoo Kim } 1394b2261026SJoonsoo Kim 1395b2261026SJoonsoo Kim if (ret) 1396b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC); 1397b2261026SJoonsoo Kim else 1398b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 1399b2261026SJoonsoo Kim 1400b2261026SJoonsoo Kim return ret; 1401b2261026SJoonsoo Kim } 1402b2261026SJoonsoo Kim 1403e8c5c824SLee Schermerhorn /* 1404e8c5c824SLee Schermerhorn * Free huge page from pool from next node to free. 1405e8c5c824SLee Schermerhorn * Attempt to keep persistent huge pages more or less 1406e8c5c824SLee Schermerhorn * balanced over allowed nodes. 1407e8c5c824SLee Schermerhorn * Called with hugetlb_lock locked. 1408e8c5c824SLee Schermerhorn */ 14096ae11b27SLee Schermerhorn static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, 14106ae11b27SLee Schermerhorn bool acct_surplus) 1411e8c5c824SLee Schermerhorn { 1412b2261026SJoonsoo Kim int nr_nodes, node; 1413e8c5c824SLee Schermerhorn int ret = 0; 1414e8c5c824SLee Schermerhorn 1415b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1416685f3457SLee Schermerhorn /* 1417685f3457SLee Schermerhorn * If we're returning unused surplus pages, only examine 1418685f3457SLee Schermerhorn * nodes with surplus pages. 1419685f3457SLee Schermerhorn */ 1420b2261026SJoonsoo Kim if ((!acct_surplus || h->surplus_huge_pages_node[node]) && 1421b2261026SJoonsoo Kim !list_empty(&h->hugepage_freelists[node])) { 1422e8c5c824SLee Schermerhorn struct page *page = 1423b2261026SJoonsoo Kim list_entry(h->hugepage_freelists[node].next, 1424e8c5c824SLee Schermerhorn struct page, lru); 1425e8c5c824SLee Schermerhorn list_del(&page->lru); 1426e8c5c824SLee Schermerhorn h->free_huge_pages--; 1427b2261026SJoonsoo Kim h->free_huge_pages_node[node]--; 1428685f3457SLee Schermerhorn if (acct_surplus) { 1429685f3457SLee Schermerhorn h->surplus_huge_pages--; 1430b2261026SJoonsoo Kim h->surplus_huge_pages_node[node]--; 1431685f3457SLee Schermerhorn } 1432e8c5c824SLee Schermerhorn update_and_free_page(h, page); 1433e8c5c824SLee Schermerhorn ret = 1; 14349a76db09SLee Schermerhorn break; 1435e8c5c824SLee Schermerhorn } 1436b2261026SJoonsoo Kim } 1437e8c5c824SLee Schermerhorn 1438e8c5c824SLee Schermerhorn return ret; 1439e8c5c824SLee Schermerhorn } 1440e8c5c824SLee Schermerhorn 1441c8721bbbSNaoya Horiguchi /* 1442c8721bbbSNaoya Horiguchi * Dissolve a given free hugepage into free buddy pages. This function does 1443082d5b6bSGerald Schaefer * nothing for in-use (including surplus) hugepages. Returns -EBUSY if the 1444082d5b6bSGerald Schaefer * number of free hugepages would be reduced below the number of reserved 1445082d5b6bSGerald Schaefer * hugepages. 1446c8721bbbSNaoya Horiguchi */ 1447082d5b6bSGerald Schaefer static int dissolve_free_huge_page(struct page *page) 1448c8721bbbSNaoya Horiguchi { 1449082d5b6bSGerald Schaefer int rc = 0; 1450082d5b6bSGerald Schaefer 1451c8721bbbSNaoya Horiguchi spin_lock(&hugetlb_lock); 1452c8721bbbSNaoya Horiguchi if (PageHuge(page) && !page_count(page)) { 14532247bb33SGerald Schaefer struct page *head = compound_head(page); 14542247bb33SGerald Schaefer struct hstate *h = page_hstate(head); 14552247bb33SGerald Schaefer int nid = page_to_nid(head); 1456082d5b6bSGerald Schaefer if (h->free_huge_pages - h->resv_huge_pages == 0) { 1457082d5b6bSGerald Schaefer rc = -EBUSY; 1458082d5b6bSGerald Schaefer goto out; 1459082d5b6bSGerald Schaefer } 14602247bb33SGerald Schaefer list_del(&head->lru); 1461c8721bbbSNaoya Horiguchi h->free_huge_pages--; 1462c8721bbbSNaoya Horiguchi h->free_huge_pages_node[nid]--; 1463c1470b33Szhong jiang h->max_huge_pages--; 14642247bb33SGerald Schaefer update_and_free_page(h, head); 1465c8721bbbSNaoya Horiguchi } 1466082d5b6bSGerald Schaefer out: 1467c8721bbbSNaoya Horiguchi spin_unlock(&hugetlb_lock); 1468082d5b6bSGerald Schaefer return rc; 1469c8721bbbSNaoya Horiguchi } 1470c8721bbbSNaoya Horiguchi 1471c8721bbbSNaoya Horiguchi /* 1472c8721bbbSNaoya Horiguchi * Dissolve free hugepages in a given pfn range. Used by memory hotplug to 1473c8721bbbSNaoya Horiguchi * make specified memory blocks removable from the system. 14742247bb33SGerald Schaefer * Note that this will dissolve a free gigantic hugepage completely, if any 14752247bb33SGerald Schaefer * part of it lies within the given range. 1476082d5b6bSGerald Schaefer * Also note that if dissolve_free_huge_page() returns with an error, all 1477082d5b6bSGerald Schaefer * free hugepages that were dissolved before that error are lost. 1478c8721bbbSNaoya Horiguchi */ 1479082d5b6bSGerald Schaefer int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) 1480c8721bbbSNaoya Horiguchi { 1481c8721bbbSNaoya Horiguchi unsigned long pfn; 1482eb03aa00SGerald Schaefer struct page *page; 1483082d5b6bSGerald Schaefer int rc = 0; 1484c8721bbbSNaoya Horiguchi 1485d0177639SLi Zhong if (!hugepages_supported()) 1486082d5b6bSGerald Schaefer return rc; 1487d0177639SLi Zhong 1488eb03aa00SGerald Schaefer for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) { 1489eb03aa00SGerald Schaefer page = pfn_to_page(pfn); 1490eb03aa00SGerald Schaefer if (PageHuge(page) && !page_count(page)) { 1491eb03aa00SGerald Schaefer rc = dissolve_free_huge_page(page); 1492eb03aa00SGerald Schaefer if (rc) 1493082d5b6bSGerald Schaefer break; 1494eb03aa00SGerald Schaefer } 1495eb03aa00SGerald Schaefer } 1496082d5b6bSGerald Schaefer 1497082d5b6bSGerald Schaefer return rc; 1498c8721bbbSNaoya Horiguchi } 1499c8721bbbSNaoya Horiguchi 1500099730d6SDave Hansen /* 1501099730d6SDave Hansen * There are 3 ways this can get called: 1502099730d6SDave Hansen * 1. With vma+addr: we use the VMA's memory policy 1503099730d6SDave Hansen * 2. With !vma, but nid=NUMA_NO_NODE: We try to allocate a huge 1504099730d6SDave Hansen * page from any node, and let the buddy allocator itself figure 1505099730d6SDave Hansen * it out. 1506099730d6SDave Hansen * 3. With !vma, but nid!=NUMA_NO_NODE. We allocate a huge page 1507099730d6SDave Hansen * strictly from 'nid' 1508099730d6SDave Hansen */ 1509099730d6SDave Hansen static struct page *__hugetlb_alloc_buddy_huge_page(struct hstate *h, 1510099730d6SDave Hansen struct vm_area_struct *vma, unsigned long addr, int nid) 1511099730d6SDave Hansen { 1512099730d6SDave Hansen int order = huge_page_order(h); 1513099730d6SDave Hansen gfp_t gfp = htlb_alloc_mask(h)|__GFP_COMP|__GFP_REPEAT|__GFP_NOWARN; 1514099730d6SDave Hansen unsigned int cpuset_mems_cookie; 1515099730d6SDave Hansen 1516099730d6SDave Hansen /* 1517099730d6SDave Hansen * We need a VMA to get a memory policy. If we do not 1518e0ec90eeSDave Hansen * have one, we use the 'nid' argument. 1519e0ec90eeSDave Hansen * 1520e0ec90eeSDave Hansen * The mempolicy stuff below has some non-inlined bits 1521e0ec90eeSDave Hansen * and calls ->vm_ops. That makes it hard to optimize at 1522e0ec90eeSDave Hansen * compile-time, even when NUMA is off and it does 1523e0ec90eeSDave Hansen * nothing. This helps the compiler optimize it out. 1524099730d6SDave Hansen */ 1525e0ec90eeSDave Hansen if (!IS_ENABLED(CONFIG_NUMA) || !vma) { 1526099730d6SDave Hansen /* 1527099730d6SDave Hansen * If a specific node is requested, make sure to 1528099730d6SDave Hansen * get memory from there, but only when a node 1529099730d6SDave Hansen * is explicitly specified. 1530099730d6SDave Hansen */ 1531099730d6SDave Hansen if (nid != NUMA_NO_NODE) 1532099730d6SDave Hansen gfp |= __GFP_THISNODE; 1533099730d6SDave Hansen /* 1534099730d6SDave Hansen * Make sure to call something that can handle 1535099730d6SDave Hansen * nid=NUMA_NO_NODE 1536099730d6SDave Hansen */ 1537099730d6SDave Hansen return alloc_pages_node(nid, gfp, order); 1538099730d6SDave Hansen } 1539099730d6SDave Hansen 1540099730d6SDave Hansen /* 1541099730d6SDave Hansen * OK, so we have a VMA. Fetch the mempolicy and try to 1542e0ec90eeSDave Hansen * allocate a huge page with it. We will only reach this 1543e0ec90eeSDave Hansen * when CONFIG_NUMA=y. 1544099730d6SDave Hansen */ 1545099730d6SDave Hansen do { 1546099730d6SDave Hansen struct page *page; 1547099730d6SDave Hansen struct mempolicy *mpol; 1548099730d6SDave Hansen struct zonelist *zl; 1549099730d6SDave Hansen nodemask_t *nodemask; 1550099730d6SDave Hansen 1551099730d6SDave Hansen cpuset_mems_cookie = read_mems_allowed_begin(); 1552099730d6SDave Hansen zl = huge_zonelist(vma, addr, gfp, &mpol, &nodemask); 1553099730d6SDave Hansen mpol_cond_put(mpol); 1554099730d6SDave Hansen page = __alloc_pages_nodemask(gfp, order, zl, nodemask); 1555099730d6SDave Hansen if (page) 1556099730d6SDave Hansen return page; 1557099730d6SDave Hansen } while (read_mems_allowed_retry(cpuset_mems_cookie)); 1558099730d6SDave Hansen 1559099730d6SDave Hansen return NULL; 1560099730d6SDave Hansen } 1561099730d6SDave Hansen 1562099730d6SDave Hansen /* 1563099730d6SDave Hansen * There are two ways to allocate a huge page: 1564099730d6SDave Hansen * 1. When you have a VMA and an address (like a fault) 1565099730d6SDave Hansen * 2. When you have no VMA (like when setting /proc/.../nr_hugepages) 1566099730d6SDave Hansen * 1567099730d6SDave Hansen * 'vma' and 'addr' are only for (1). 'nid' is always NUMA_NO_NODE in 1568099730d6SDave Hansen * this case which signifies that the allocation should be done with 1569099730d6SDave Hansen * respect for the VMA's memory policy. 1570099730d6SDave Hansen * 1571099730d6SDave Hansen * For (2), we ignore 'vma' and 'addr' and use 'nid' exclusively. This 1572099730d6SDave Hansen * implies that memory policies will not be taken in to account. 1573099730d6SDave Hansen */ 1574099730d6SDave Hansen static struct page *__alloc_buddy_huge_page(struct hstate *h, 1575099730d6SDave Hansen struct vm_area_struct *vma, unsigned long addr, int nid) 15767893d1d5SAdam Litke { 15777893d1d5SAdam Litke struct page *page; 1578bf50bab2SNaoya Horiguchi unsigned int r_nid; 15797893d1d5SAdam Litke 1580bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1581aa888a74SAndi Kleen return NULL; 1582aa888a74SAndi Kleen 1583d1c3fb1fSNishanth Aravamudan /* 1584099730d6SDave Hansen * Make sure that anyone specifying 'nid' is not also specifying a VMA. 1585099730d6SDave Hansen * This makes sure the caller is picking _one_ of the modes with which 1586099730d6SDave Hansen * we can call this function, not both. 1587099730d6SDave Hansen */ 1588099730d6SDave Hansen if (vma || (addr != -1)) { 1589e0ec90eeSDave Hansen VM_WARN_ON_ONCE(addr == -1); 1590e0ec90eeSDave Hansen VM_WARN_ON_ONCE(nid != NUMA_NO_NODE); 1591099730d6SDave Hansen } 1592099730d6SDave Hansen /* 1593d1c3fb1fSNishanth Aravamudan * Assume we will successfully allocate the surplus page to 1594d1c3fb1fSNishanth Aravamudan * prevent racing processes from causing the surplus to exceed 1595d1c3fb1fSNishanth Aravamudan * overcommit 1596d1c3fb1fSNishanth Aravamudan * 1597d1c3fb1fSNishanth Aravamudan * This however introduces a different race, where a process B 1598d1c3fb1fSNishanth Aravamudan * tries to grow the static hugepage pool while alloc_pages() is 1599d1c3fb1fSNishanth Aravamudan * called by process A. B will only examine the per-node 1600d1c3fb1fSNishanth Aravamudan * counters in determining if surplus huge pages can be 1601d1c3fb1fSNishanth Aravamudan * converted to normal huge pages in adjust_pool_surplus(). A 1602d1c3fb1fSNishanth Aravamudan * won't be able to increment the per-node counter, until the 1603d1c3fb1fSNishanth Aravamudan * lock is dropped by B, but B doesn't drop hugetlb_lock until 1604d1c3fb1fSNishanth Aravamudan * no more huge pages can be converted from surplus to normal 1605d1c3fb1fSNishanth Aravamudan * state (and doesn't try to convert again). Thus, we have a 1606d1c3fb1fSNishanth Aravamudan * case where a surplus huge page exists, the pool is grown, and 1607d1c3fb1fSNishanth Aravamudan * the surplus huge page still exists after, even though it 1608d1c3fb1fSNishanth Aravamudan * should just have been converted to a normal huge page. This 1609d1c3fb1fSNishanth Aravamudan * does not leak memory, though, as the hugepage will be freed 1610d1c3fb1fSNishanth Aravamudan * once it is out of use. It also does not allow the counters to 1611d1c3fb1fSNishanth Aravamudan * go out of whack in adjust_pool_surplus() as we don't modify 1612d1c3fb1fSNishanth Aravamudan * the node values until we've gotten the hugepage and only the 1613d1c3fb1fSNishanth Aravamudan * per-node value is checked there. 1614d1c3fb1fSNishanth Aravamudan */ 1615d1c3fb1fSNishanth Aravamudan spin_lock(&hugetlb_lock); 1616a5516438SAndi Kleen if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { 1617d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1618d1c3fb1fSNishanth Aravamudan return NULL; 1619d1c3fb1fSNishanth Aravamudan } else { 1620a5516438SAndi Kleen h->nr_huge_pages++; 1621a5516438SAndi Kleen h->surplus_huge_pages++; 1622d1c3fb1fSNishanth Aravamudan } 1623d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1624d1c3fb1fSNishanth Aravamudan 1625099730d6SDave Hansen page = __hugetlb_alloc_buddy_huge_page(h, vma, addr, nid); 1626d1c3fb1fSNishanth Aravamudan 16277893d1d5SAdam Litke spin_lock(&hugetlb_lock); 1628d1c3fb1fSNishanth Aravamudan if (page) { 16290edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1630bf50bab2SNaoya Horiguchi r_nid = page_to_nid(page); 1631f1e61557SKirill A. Shutemov set_compound_page_dtor(page, HUGETLB_PAGE_DTOR); 16329dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1633d1c3fb1fSNishanth Aravamudan /* 1634d1c3fb1fSNishanth Aravamudan * We incremented the global counters already 1635d1c3fb1fSNishanth Aravamudan */ 1636bf50bab2SNaoya Horiguchi h->nr_huge_pages_node[r_nid]++; 1637bf50bab2SNaoya Horiguchi h->surplus_huge_pages_node[r_nid]++; 16383b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC); 1639d1c3fb1fSNishanth Aravamudan } else { 1640a5516438SAndi Kleen h->nr_huge_pages--; 1641a5516438SAndi Kleen h->surplus_huge_pages--; 16423b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 16437893d1d5SAdam Litke } 1644d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 16457893d1d5SAdam Litke 16467893d1d5SAdam Litke return page; 16477893d1d5SAdam Litke } 16487893d1d5SAdam Litke 1649e4e574b7SAdam Litke /* 1650099730d6SDave Hansen * Allocate a huge page from 'nid'. Note, 'nid' may be 1651099730d6SDave Hansen * NUMA_NO_NODE, which means that it may be allocated 1652099730d6SDave Hansen * anywhere. 1653099730d6SDave Hansen */ 1654e0ec90eeSDave Hansen static 1655099730d6SDave Hansen struct page *__alloc_buddy_huge_page_no_mpol(struct hstate *h, int nid) 1656099730d6SDave Hansen { 1657099730d6SDave Hansen unsigned long addr = -1; 1658099730d6SDave Hansen 1659099730d6SDave Hansen return __alloc_buddy_huge_page(h, NULL, addr, nid); 1660099730d6SDave Hansen } 1661099730d6SDave Hansen 1662099730d6SDave Hansen /* 1663099730d6SDave Hansen * Use the VMA's mpolicy to allocate a huge page from the buddy. 1664099730d6SDave Hansen */ 1665e0ec90eeSDave Hansen static 1666099730d6SDave Hansen struct page *__alloc_buddy_huge_page_with_mpol(struct hstate *h, 1667099730d6SDave Hansen struct vm_area_struct *vma, unsigned long addr) 1668099730d6SDave Hansen { 1669099730d6SDave Hansen return __alloc_buddy_huge_page(h, vma, addr, NUMA_NO_NODE); 1670099730d6SDave Hansen } 1671099730d6SDave Hansen 1672099730d6SDave Hansen /* 1673bf50bab2SNaoya Horiguchi * This allocation function is useful in the context where vma is irrelevant. 1674bf50bab2SNaoya Horiguchi * E.g. soft-offlining uses this function because it only cares physical 1675bf50bab2SNaoya Horiguchi * address of error page. 1676bf50bab2SNaoya Horiguchi */ 1677bf50bab2SNaoya Horiguchi struct page *alloc_huge_page_node(struct hstate *h, int nid) 1678bf50bab2SNaoya Horiguchi { 16794ef91848SJoonsoo Kim struct page *page = NULL; 1680bf50bab2SNaoya Horiguchi 1681bf50bab2SNaoya Horiguchi spin_lock(&hugetlb_lock); 16824ef91848SJoonsoo Kim if (h->free_huge_pages - h->resv_huge_pages > 0) 1683bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, nid); 1684bf50bab2SNaoya Horiguchi spin_unlock(&hugetlb_lock); 1685bf50bab2SNaoya Horiguchi 168694ae8ba7SAneesh Kumar K.V if (!page) 1687099730d6SDave Hansen page = __alloc_buddy_huge_page_no_mpol(h, nid); 1688bf50bab2SNaoya Horiguchi 1689bf50bab2SNaoya Horiguchi return page; 1690bf50bab2SNaoya Horiguchi } 1691bf50bab2SNaoya Horiguchi 1692bf50bab2SNaoya Horiguchi /* 169325985edcSLucas De Marchi * Increase the hugetlb pool such that it can accommodate a reservation 1694e4e574b7SAdam Litke * of size 'delta'. 1695e4e574b7SAdam Litke */ 1696a5516438SAndi Kleen static int gather_surplus_pages(struct hstate *h, int delta) 1697e4e574b7SAdam Litke { 1698e4e574b7SAdam Litke struct list_head surplus_list; 1699e4e574b7SAdam Litke struct page *page, *tmp; 1700e4e574b7SAdam Litke int ret, i; 1701e4e574b7SAdam Litke int needed, allocated; 170228073b02SHillf Danton bool alloc_ok = true; 1703e4e574b7SAdam Litke 1704a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - h->free_huge_pages; 1705ac09b3a1SAdam Litke if (needed <= 0) { 1706a5516438SAndi Kleen h->resv_huge_pages += delta; 1707e4e574b7SAdam Litke return 0; 1708ac09b3a1SAdam Litke } 1709e4e574b7SAdam Litke 1710e4e574b7SAdam Litke allocated = 0; 1711e4e574b7SAdam Litke INIT_LIST_HEAD(&surplus_list); 1712e4e574b7SAdam Litke 1713e4e574b7SAdam Litke ret = -ENOMEM; 1714e4e574b7SAdam Litke retry: 1715e4e574b7SAdam Litke spin_unlock(&hugetlb_lock); 1716e4e574b7SAdam Litke for (i = 0; i < needed; i++) { 1717099730d6SDave Hansen page = __alloc_buddy_huge_page_no_mpol(h, NUMA_NO_NODE); 171828073b02SHillf Danton if (!page) { 171928073b02SHillf Danton alloc_ok = false; 172028073b02SHillf Danton break; 172128073b02SHillf Danton } 1722e4e574b7SAdam Litke list_add(&page->lru, &surplus_list); 1723e4e574b7SAdam Litke } 172428073b02SHillf Danton allocated += i; 1725e4e574b7SAdam Litke 1726e4e574b7SAdam Litke /* 1727e4e574b7SAdam Litke * After retaking hugetlb_lock, we need to recalculate 'needed' 1728e4e574b7SAdam Litke * because either resv_huge_pages or free_huge_pages may have changed. 1729e4e574b7SAdam Litke */ 1730e4e574b7SAdam Litke spin_lock(&hugetlb_lock); 1731a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - 1732a5516438SAndi Kleen (h->free_huge_pages + allocated); 173328073b02SHillf Danton if (needed > 0) { 173428073b02SHillf Danton if (alloc_ok) 1735e4e574b7SAdam Litke goto retry; 173628073b02SHillf Danton /* 173728073b02SHillf Danton * We were not able to allocate enough pages to 173828073b02SHillf Danton * satisfy the entire reservation so we free what 173928073b02SHillf Danton * we've allocated so far. 174028073b02SHillf Danton */ 174128073b02SHillf Danton goto free; 174228073b02SHillf Danton } 1743e4e574b7SAdam Litke /* 1744e4e574b7SAdam Litke * The surplus_list now contains _at_least_ the number of extra pages 174525985edcSLucas De Marchi * needed to accommodate the reservation. Add the appropriate number 1746e4e574b7SAdam Litke * of pages to the hugetlb pool and free the extras back to the buddy 1747ac09b3a1SAdam Litke * allocator. Commit the entire reservation here to prevent another 1748ac09b3a1SAdam Litke * process from stealing the pages as they are added to the pool but 1749ac09b3a1SAdam Litke * before they are reserved. 1750e4e574b7SAdam Litke */ 1751e4e574b7SAdam Litke needed += allocated; 1752a5516438SAndi Kleen h->resv_huge_pages += delta; 1753e4e574b7SAdam Litke ret = 0; 1754a9869b83SNaoya Horiguchi 175519fc3f0aSAdam Litke /* Free the needed pages to the hugetlb pool */ 175619fc3f0aSAdam Litke list_for_each_entry_safe(page, tmp, &surplus_list, lru) { 175719fc3f0aSAdam Litke if ((--needed) < 0) 175819fc3f0aSAdam Litke break; 1759a9869b83SNaoya Horiguchi /* 1760a9869b83SNaoya Horiguchi * This page is now managed by the hugetlb allocator and has 1761a9869b83SNaoya Horiguchi * no users -- drop the buddy allocator's reference. 1762a9869b83SNaoya Horiguchi */ 1763a9869b83SNaoya Horiguchi put_page_testzero(page); 1764309381feSSasha Levin VM_BUG_ON_PAGE(page_count(page), page); 1765a5516438SAndi Kleen enqueue_huge_page(h, page); 176619fc3f0aSAdam Litke } 176728073b02SHillf Danton free: 1768b0365c8dSHillf Danton spin_unlock(&hugetlb_lock); 176919fc3f0aSAdam Litke 177019fc3f0aSAdam Litke /* Free unnecessary surplus pages to the buddy allocator */ 1771c0d934baSJoonsoo Kim list_for_each_entry_safe(page, tmp, &surplus_list, lru) 1772a9869b83SNaoya Horiguchi put_page(page); 177319fc3f0aSAdam Litke spin_lock(&hugetlb_lock); 1774e4e574b7SAdam Litke 1775e4e574b7SAdam Litke return ret; 1776e4e574b7SAdam Litke } 1777e4e574b7SAdam Litke 1778e4e574b7SAdam Litke /* 1779e5bbc8a6SMike Kravetz * This routine has two main purposes: 1780e5bbc8a6SMike Kravetz * 1) Decrement the reservation count (resv_huge_pages) by the value passed 1781e5bbc8a6SMike Kravetz * in unused_resv_pages. This corresponds to the prior adjustments made 1782e5bbc8a6SMike Kravetz * to the associated reservation map. 1783e5bbc8a6SMike Kravetz * 2) Free any unused surplus pages that may have been allocated to satisfy 1784e5bbc8a6SMike Kravetz * the reservation. As many as unused_resv_pages may be freed. 1785e5bbc8a6SMike Kravetz * 1786e5bbc8a6SMike Kravetz * Called with hugetlb_lock held. However, the lock could be dropped (and 1787e5bbc8a6SMike Kravetz * reacquired) during calls to cond_resched_lock. Whenever dropping the lock, 1788e5bbc8a6SMike Kravetz * we must make sure nobody else can claim pages we are in the process of 1789e5bbc8a6SMike Kravetz * freeing. Do this by ensuring resv_huge_page always is greater than the 1790e5bbc8a6SMike Kravetz * number of huge pages we plan to free when dropping the lock. 1791e4e574b7SAdam Litke */ 1792a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h, 1793a5516438SAndi Kleen unsigned long unused_resv_pages) 1794e4e574b7SAdam Litke { 1795e4e574b7SAdam Litke unsigned long nr_pages; 1796e4e574b7SAdam Litke 1797aa888a74SAndi Kleen /* Cannot return gigantic pages currently */ 1798bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1799e5bbc8a6SMike Kravetz goto out; 1800aa888a74SAndi Kleen 1801e5bbc8a6SMike Kravetz /* 1802e5bbc8a6SMike Kravetz * Part (or even all) of the reservation could have been backed 1803e5bbc8a6SMike Kravetz * by pre-allocated pages. Only free surplus pages. 1804e5bbc8a6SMike Kravetz */ 1805a5516438SAndi Kleen nr_pages = min(unused_resv_pages, h->surplus_huge_pages); 1806e4e574b7SAdam Litke 1807685f3457SLee Schermerhorn /* 1808685f3457SLee Schermerhorn * We want to release as many surplus pages as possible, spread 18099b5e5d0fSLee Schermerhorn * evenly across all nodes with memory. Iterate across these nodes 18109b5e5d0fSLee Schermerhorn * until we can no longer free unreserved surplus pages. This occurs 18119b5e5d0fSLee Schermerhorn * when the nodes with surplus pages have no free pages. 18129b5e5d0fSLee Schermerhorn * free_pool_huge_page() will balance the the freed pages across the 18139b5e5d0fSLee Schermerhorn * on-line nodes with memory and will handle the hstate accounting. 1814e5bbc8a6SMike Kravetz * 1815e5bbc8a6SMike Kravetz * Note that we decrement resv_huge_pages as we free the pages. If 1816e5bbc8a6SMike Kravetz * we drop the lock, resv_huge_pages will still be sufficiently large 1817e5bbc8a6SMike Kravetz * to cover subsequent pages we may free. 1818685f3457SLee Schermerhorn */ 1819685f3457SLee Schermerhorn while (nr_pages--) { 1820e5bbc8a6SMike Kravetz h->resv_huge_pages--; 1821e5bbc8a6SMike Kravetz unused_resv_pages--; 18228cebfcd0SLai Jiangshan if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1)) 1823e5bbc8a6SMike Kravetz goto out; 18247848a4bfSMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 1825e4e574b7SAdam Litke } 1826e5bbc8a6SMike Kravetz 1827e5bbc8a6SMike Kravetz out: 1828e5bbc8a6SMike Kravetz /* Fully uncommit the reservation */ 1829e5bbc8a6SMike Kravetz h->resv_huge_pages -= unused_resv_pages; 1830e4e574b7SAdam Litke } 1831e4e574b7SAdam Litke 18325e911373SMike Kravetz 1833c37f9fb1SAndy Whitcroft /* 1834feba16e2SMike Kravetz * vma_needs_reservation, vma_commit_reservation and vma_end_reservation 18355e911373SMike Kravetz * are used by the huge page allocation routines to manage reservations. 1836cf3ad20bSMike Kravetz * 1837cf3ad20bSMike Kravetz * vma_needs_reservation is called to determine if the huge page at addr 1838cf3ad20bSMike Kravetz * within the vma has an associated reservation. If a reservation is 1839cf3ad20bSMike Kravetz * needed, the value 1 is returned. The caller is then responsible for 1840cf3ad20bSMike Kravetz * managing the global reservation and subpool usage counts. After 1841cf3ad20bSMike Kravetz * the huge page has been allocated, vma_commit_reservation is called 1842feba16e2SMike Kravetz * to add the page to the reservation map. If the page allocation fails, 1843feba16e2SMike Kravetz * the reservation must be ended instead of committed. vma_end_reservation 1844feba16e2SMike Kravetz * is called in such cases. 1845cf3ad20bSMike Kravetz * 1846cf3ad20bSMike Kravetz * In the normal case, vma_commit_reservation returns the same value 1847cf3ad20bSMike Kravetz * as the preceding vma_needs_reservation call. The only time this 1848cf3ad20bSMike Kravetz * is not the case is if a reserve map was changed between calls. It 1849cf3ad20bSMike Kravetz * is the responsibility of the caller to notice the difference and 1850cf3ad20bSMike Kravetz * take appropriate action. 185196b96a96SMike Kravetz * 185296b96a96SMike Kravetz * vma_add_reservation is used in error paths where a reservation must 185396b96a96SMike Kravetz * be restored when a newly allocated huge page must be freed. It is 185496b96a96SMike Kravetz * to be called after calling vma_needs_reservation to determine if a 185596b96a96SMike Kravetz * reservation exists. 1856c37f9fb1SAndy Whitcroft */ 18575e911373SMike Kravetz enum vma_resv_mode { 18585e911373SMike Kravetz VMA_NEEDS_RESV, 18595e911373SMike Kravetz VMA_COMMIT_RESV, 1860feba16e2SMike Kravetz VMA_END_RESV, 186196b96a96SMike Kravetz VMA_ADD_RESV, 18625e911373SMike Kravetz }; 1863cf3ad20bSMike Kravetz static long __vma_reservation_common(struct hstate *h, 1864cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr, 18655e911373SMike Kravetz enum vma_resv_mode mode) 1866c37f9fb1SAndy Whitcroft { 18674e35f483SJoonsoo Kim struct resv_map *resv; 18684e35f483SJoonsoo Kim pgoff_t idx; 1869cf3ad20bSMike Kravetz long ret; 1870c37f9fb1SAndy Whitcroft 18714e35f483SJoonsoo Kim resv = vma_resv_map(vma); 18724e35f483SJoonsoo Kim if (!resv) 1873c37f9fb1SAndy Whitcroft return 1; 1874c37f9fb1SAndy Whitcroft 18754e35f483SJoonsoo Kim idx = vma_hugecache_offset(h, vma, addr); 18765e911373SMike Kravetz switch (mode) { 18775e911373SMike Kravetz case VMA_NEEDS_RESV: 1878cf3ad20bSMike Kravetz ret = region_chg(resv, idx, idx + 1); 18795e911373SMike Kravetz break; 18805e911373SMike Kravetz case VMA_COMMIT_RESV: 18815e911373SMike Kravetz ret = region_add(resv, idx, idx + 1); 18825e911373SMike Kravetz break; 1883feba16e2SMike Kravetz case VMA_END_RESV: 18845e911373SMike Kravetz region_abort(resv, idx, idx + 1); 18855e911373SMike Kravetz ret = 0; 18865e911373SMike Kravetz break; 188796b96a96SMike Kravetz case VMA_ADD_RESV: 188896b96a96SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) 188996b96a96SMike Kravetz ret = region_add(resv, idx, idx + 1); 189096b96a96SMike Kravetz else { 189196b96a96SMike Kravetz region_abort(resv, idx, idx + 1); 189296b96a96SMike Kravetz ret = region_del(resv, idx, idx + 1); 189396b96a96SMike Kravetz } 189496b96a96SMike Kravetz break; 18955e911373SMike Kravetz default: 18965e911373SMike Kravetz BUG(); 18975e911373SMike Kravetz } 189884afd99bSAndy Whitcroft 18994e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) 1900cf3ad20bSMike Kravetz return ret; 190167961f9dSMike Kravetz else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && ret >= 0) { 190267961f9dSMike Kravetz /* 190367961f9dSMike Kravetz * In most cases, reserves always exist for private mappings. 190467961f9dSMike Kravetz * However, a file associated with mapping could have been 190567961f9dSMike Kravetz * hole punched or truncated after reserves were consumed. 190667961f9dSMike Kravetz * As subsequent fault on such a range will not use reserves. 190767961f9dSMike Kravetz * Subtle - The reserve map for private mappings has the 190867961f9dSMike Kravetz * opposite meaning than that of shared mappings. If NO 190967961f9dSMike Kravetz * entry is in the reserve map, it means a reservation exists. 191067961f9dSMike Kravetz * If an entry exists in the reserve map, it means the 191167961f9dSMike Kravetz * reservation has already been consumed. As a result, the 191267961f9dSMike Kravetz * return value of this routine is the opposite of the 191367961f9dSMike Kravetz * value returned from reserve map manipulation routines above. 191467961f9dSMike Kravetz */ 191567961f9dSMike Kravetz if (ret) 191667961f9dSMike Kravetz return 0; 191767961f9dSMike Kravetz else 191867961f9dSMike Kravetz return 1; 191967961f9dSMike Kravetz } 19204e35f483SJoonsoo Kim else 1921cf3ad20bSMike Kravetz return ret < 0 ? ret : 0; 192284afd99bSAndy Whitcroft } 1923cf3ad20bSMike Kravetz 1924cf3ad20bSMike Kravetz static long vma_needs_reservation(struct hstate *h, 1925a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long addr) 1926c37f9fb1SAndy Whitcroft { 19275e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV); 1928cf3ad20bSMike Kravetz } 1929c37f9fb1SAndy Whitcroft 1930cf3ad20bSMike Kravetz static long vma_commit_reservation(struct hstate *h, 1931cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr) 1932cf3ad20bSMike Kravetz { 19335e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV); 19345e911373SMike Kravetz } 19355e911373SMike Kravetz 1936feba16e2SMike Kravetz static void vma_end_reservation(struct hstate *h, 19375e911373SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 19385e911373SMike Kravetz { 1939feba16e2SMike Kravetz (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV); 1940c37f9fb1SAndy Whitcroft } 1941c37f9fb1SAndy Whitcroft 194296b96a96SMike Kravetz static long vma_add_reservation(struct hstate *h, 194396b96a96SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 194496b96a96SMike Kravetz { 194596b96a96SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV); 194696b96a96SMike Kravetz } 194796b96a96SMike Kravetz 194896b96a96SMike Kravetz /* 194996b96a96SMike Kravetz * This routine is called to restore a reservation on error paths. In the 195096b96a96SMike Kravetz * specific error paths, a huge page was allocated (via alloc_huge_page) 195196b96a96SMike Kravetz * and is about to be freed. If a reservation for the page existed, 195296b96a96SMike Kravetz * alloc_huge_page would have consumed the reservation and set PagePrivate 195396b96a96SMike Kravetz * in the newly allocated page. When the page is freed via free_huge_page, 195496b96a96SMike Kravetz * the global reservation count will be incremented if PagePrivate is set. 195596b96a96SMike Kravetz * However, free_huge_page can not adjust the reserve map. Adjust the 195696b96a96SMike Kravetz * reserve map here to be consistent with global reserve count adjustments 195796b96a96SMike Kravetz * to be made by free_huge_page. 195896b96a96SMike Kravetz */ 195996b96a96SMike Kravetz static void restore_reserve_on_error(struct hstate *h, 196096b96a96SMike Kravetz struct vm_area_struct *vma, unsigned long address, 196196b96a96SMike Kravetz struct page *page) 196296b96a96SMike Kravetz { 196396b96a96SMike Kravetz if (unlikely(PagePrivate(page))) { 196496b96a96SMike Kravetz long rc = vma_needs_reservation(h, vma, address); 196596b96a96SMike Kravetz 196696b96a96SMike Kravetz if (unlikely(rc < 0)) { 196796b96a96SMike Kravetz /* 196896b96a96SMike Kravetz * Rare out of memory condition in reserve map 196996b96a96SMike Kravetz * manipulation. Clear PagePrivate so that 197096b96a96SMike Kravetz * global reserve count will not be incremented 197196b96a96SMike Kravetz * by free_huge_page. This will make it appear 197296b96a96SMike Kravetz * as though the reservation for this page was 197396b96a96SMike Kravetz * consumed. This may prevent the task from 197496b96a96SMike Kravetz * faulting in the page at a later time. This 197596b96a96SMike Kravetz * is better than inconsistent global huge page 197696b96a96SMike Kravetz * accounting of reserve counts. 197796b96a96SMike Kravetz */ 197896b96a96SMike Kravetz ClearPagePrivate(page); 197996b96a96SMike Kravetz } else if (rc) { 198096b96a96SMike Kravetz rc = vma_add_reservation(h, vma, address); 198196b96a96SMike Kravetz if (unlikely(rc < 0)) 198296b96a96SMike Kravetz /* 198396b96a96SMike Kravetz * See above comment about rare out of 198496b96a96SMike Kravetz * memory condition. 198596b96a96SMike Kravetz */ 198696b96a96SMike Kravetz ClearPagePrivate(page); 198796b96a96SMike Kravetz } else 198896b96a96SMike Kravetz vma_end_reservation(h, vma, address); 198996b96a96SMike Kravetz } 199096b96a96SMike Kravetz } 199196b96a96SMike Kravetz 199270c3547eSMike Kravetz struct page *alloc_huge_page(struct vm_area_struct *vma, 199304f2cbe3SMel Gorman unsigned long addr, int avoid_reserve) 1994348ea204SAdam Litke { 199590481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 1996a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 1997348ea204SAdam Litke struct page *page; 1998d85f69b0SMike Kravetz long map_chg, map_commit; 1999d85f69b0SMike Kravetz long gbl_chg; 20006d76dcf4SAneesh Kumar K.V int ret, idx; 20016d76dcf4SAneesh Kumar K.V struct hugetlb_cgroup *h_cg; 20022fc39cecSAdam Litke 20036d76dcf4SAneesh Kumar K.V idx = hstate_index(h); 2004a1e78772SMel Gorman /* 2005d85f69b0SMike Kravetz * Examine the region/reserve map to determine if the process 2006d85f69b0SMike Kravetz * has a reservation for the page to be allocated. A return 2007d85f69b0SMike Kravetz * code of zero indicates a reservation exists (no change). 2008a1e78772SMel Gorman */ 2009d85f69b0SMike Kravetz map_chg = gbl_chg = vma_needs_reservation(h, vma, addr); 2010d85f69b0SMike Kravetz if (map_chg < 0) 201176dcee75SAneesh Kumar K.V return ERR_PTR(-ENOMEM); 2012d85f69b0SMike Kravetz 2013d85f69b0SMike Kravetz /* 2014d85f69b0SMike Kravetz * Processes that did not create the mapping will have no 2015d85f69b0SMike Kravetz * reserves as indicated by the region/reserve map. Check 2016d85f69b0SMike Kravetz * that the allocation will not exceed the subpool limit. 2017d85f69b0SMike Kravetz * Allocations for MAP_NORESERVE mappings also need to be 2018d85f69b0SMike Kravetz * checked against any subpool limit. 2019d85f69b0SMike Kravetz */ 2020d85f69b0SMike Kravetz if (map_chg || avoid_reserve) { 2021d85f69b0SMike Kravetz gbl_chg = hugepage_subpool_get_pages(spool, 1); 2022d85f69b0SMike Kravetz if (gbl_chg < 0) { 2023feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 202476dcee75SAneesh Kumar K.V return ERR_PTR(-ENOSPC); 20255e911373SMike Kravetz } 202690d8b7e6SAdam Litke 2027d85f69b0SMike Kravetz /* 2028d85f69b0SMike Kravetz * Even though there was no reservation in the region/reserve 2029d85f69b0SMike Kravetz * map, there could be reservations associated with the 2030d85f69b0SMike Kravetz * subpool that can be used. This would be indicated if the 2031d85f69b0SMike Kravetz * return value of hugepage_subpool_get_pages() is zero. 2032d85f69b0SMike Kravetz * However, if avoid_reserve is specified we still avoid even 2033d85f69b0SMike Kravetz * the subpool reservations. 2034d85f69b0SMike Kravetz */ 2035d85f69b0SMike Kravetz if (avoid_reserve) 2036d85f69b0SMike Kravetz gbl_chg = 1; 2037d85f69b0SMike Kravetz } 2038d85f69b0SMike Kravetz 20396d76dcf4SAneesh Kumar K.V ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); 20408f34af6fSJianyu Zhan if (ret) 20418f34af6fSJianyu Zhan goto out_subpool_put; 20428f34af6fSJianyu Zhan 2043a1e78772SMel Gorman spin_lock(&hugetlb_lock); 2044d85f69b0SMike Kravetz /* 2045d85f69b0SMike Kravetz * glb_chg is passed to indicate whether or not a page must be taken 2046d85f69b0SMike Kravetz * from the global free pool (global change). gbl_chg == 0 indicates 2047d85f69b0SMike Kravetz * a reservation exists for the allocation. 2048d85f69b0SMike Kravetz */ 2049d85f69b0SMike Kravetz page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg); 205081a6fcaeSJoonsoo Kim if (!page) { 205194ae8ba7SAneesh Kumar K.V spin_unlock(&hugetlb_lock); 2052099730d6SDave Hansen page = __alloc_buddy_huge_page_with_mpol(h, vma, addr); 20538f34af6fSJianyu Zhan if (!page) 20548f34af6fSJianyu Zhan goto out_uncharge_cgroup; 2055a88c7695SNaoya Horiguchi if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) { 2056a88c7695SNaoya Horiguchi SetPagePrivate(page); 2057a88c7695SNaoya Horiguchi h->resv_huge_pages--; 2058a88c7695SNaoya Horiguchi } 205979dbb236SAneesh Kumar K.V spin_lock(&hugetlb_lock); 206079dbb236SAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 206181a6fcaeSJoonsoo Kim /* Fall through */ 2062a1e78772SMel Gorman } 206381a6fcaeSJoonsoo Kim hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page); 206481a6fcaeSJoonsoo Kim spin_unlock(&hugetlb_lock); 2065a1e78772SMel Gorman 206690481622SDavid Gibson set_page_private(page, (unsigned long)spool); 2067a1e78772SMel Gorman 2068d85f69b0SMike Kravetz map_commit = vma_commit_reservation(h, vma, addr); 2069d85f69b0SMike Kravetz if (unlikely(map_chg > map_commit)) { 207033039678SMike Kravetz /* 207133039678SMike Kravetz * The page was added to the reservation map between 207233039678SMike Kravetz * vma_needs_reservation and vma_commit_reservation. 207333039678SMike Kravetz * This indicates a race with hugetlb_reserve_pages. 207433039678SMike Kravetz * Adjust for the subpool count incremented above AND 207533039678SMike Kravetz * in hugetlb_reserve_pages for the same page. Also, 207633039678SMike Kravetz * the reservation count added in hugetlb_reserve_pages 207733039678SMike Kravetz * no longer applies. 207833039678SMike Kravetz */ 207933039678SMike Kravetz long rsv_adjust; 208033039678SMike Kravetz 208133039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 1); 208233039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 208333039678SMike Kravetz } 20847893d1d5SAdam Litke return page; 20858f34af6fSJianyu Zhan 20868f34af6fSJianyu Zhan out_uncharge_cgroup: 20878f34af6fSJianyu Zhan hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); 20888f34af6fSJianyu Zhan out_subpool_put: 2089d85f69b0SMike Kravetz if (map_chg || avoid_reserve) 20908f34af6fSJianyu Zhan hugepage_subpool_put_pages(spool, 1); 2091feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 20928f34af6fSJianyu Zhan return ERR_PTR(-ENOSPC); 2093b45b5bd6SDavid Gibson } 2094b45b5bd6SDavid Gibson 209574060e4dSNaoya Horiguchi /* 209674060e4dSNaoya Horiguchi * alloc_huge_page()'s wrapper which simply returns the page if allocation 209774060e4dSNaoya Horiguchi * succeeds, otherwise NULL. This function is called from new_vma_page(), 209874060e4dSNaoya Horiguchi * where no ERR_VALUE is expected to be returned. 209974060e4dSNaoya Horiguchi */ 210074060e4dSNaoya Horiguchi struct page *alloc_huge_page_noerr(struct vm_area_struct *vma, 210174060e4dSNaoya Horiguchi unsigned long addr, int avoid_reserve) 210274060e4dSNaoya Horiguchi { 210374060e4dSNaoya Horiguchi struct page *page = alloc_huge_page(vma, addr, avoid_reserve); 210474060e4dSNaoya Horiguchi if (IS_ERR(page)) 210574060e4dSNaoya Horiguchi page = NULL; 210674060e4dSNaoya Horiguchi return page; 210774060e4dSNaoya Horiguchi } 210874060e4dSNaoya Horiguchi 210991f47662SCyrill Gorcunov int __weak alloc_bootmem_huge_page(struct hstate *h) 2110aa888a74SAndi Kleen { 2111aa888a74SAndi Kleen struct huge_bootmem_page *m; 2112b2261026SJoonsoo Kim int nr_nodes, node; 2113aa888a74SAndi Kleen 2114b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) { 2115aa888a74SAndi Kleen void *addr; 2116aa888a74SAndi Kleen 21178b89a116SGrygorii Strashko addr = memblock_virt_alloc_try_nid_nopanic( 21188b89a116SGrygorii Strashko huge_page_size(h), huge_page_size(h), 21198b89a116SGrygorii Strashko 0, BOOTMEM_ALLOC_ACCESSIBLE, node); 2120aa888a74SAndi Kleen if (addr) { 2121aa888a74SAndi Kleen /* 2122aa888a74SAndi Kleen * Use the beginning of the huge page to store the 2123aa888a74SAndi Kleen * huge_bootmem_page struct (until gather_bootmem 2124aa888a74SAndi Kleen * puts them into the mem_map). 2125aa888a74SAndi Kleen */ 2126aa888a74SAndi Kleen m = addr; 2127aa888a74SAndi Kleen goto found; 2128aa888a74SAndi Kleen } 2129aa888a74SAndi Kleen } 2130aa888a74SAndi Kleen return 0; 2131aa888a74SAndi Kleen 2132aa888a74SAndi Kleen found: 2133df994eadSLuiz Capitulino BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h))); 2134aa888a74SAndi Kleen /* Put them into a private list first because mem_map is not up yet */ 2135aa888a74SAndi Kleen list_add(&m->list, &huge_boot_pages); 2136aa888a74SAndi Kleen m->hstate = h; 2137aa888a74SAndi Kleen return 1; 2138aa888a74SAndi Kleen } 2139aa888a74SAndi Kleen 2140d00181b9SKirill A. Shutemov static void __init prep_compound_huge_page(struct page *page, 2141d00181b9SKirill A. Shutemov unsigned int order) 214218229df5SAndy Whitcroft { 214318229df5SAndy Whitcroft if (unlikely(order > (MAX_ORDER - 1))) 214418229df5SAndy Whitcroft prep_compound_gigantic_page(page, order); 214518229df5SAndy Whitcroft else 214618229df5SAndy Whitcroft prep_compound_page(page, order); 214718229df5SAndy Whitcroft } 214818229df5SAndy Whitcroft 2149aa888a74SAndi Kleen /* Put bootmem huge pages into the standard lists after mem_map is up */ 2150aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void) 2151aa888a74SAndi Kleen { 2152aa888a74SAndi Kleen struct huge_bootmem_page *m; 2153aa888a74SAndi Kleen 2154aa888a74SAndi Kleen list_for_each_entry(m, &huge_boot_pages, list) { 2155aa888a74SAndi Kleen struct hstate *h = m->hstate; 2156ee8f248dSBecky Bruce struct page *page; 2157ee8f248dSBecky Bruce 2158ee8f248dSBecky Bruce #ifdef CONFIG_HIGHMEM 2159ee8f248dSBecky Bruce page = pfn_to_page(m->phys >> PAGE_SHIFT); 21608b89a116SGrygorii Strashko memblock_free_late(__pa(m), 2161ee8f248dSBecky Bruce sizeof(struct huge_bootmem_page)); 2162ee8f248dSBecky Bruce #else 2163ee8f248dSBecky Bruce page = virt_to_page(m); 2164ee8f248dSBecky Bruce #endif 2165aa888a74SAndi Kleen WARN_ON(page_count(page) != 1); 216618229df5SAndy Whitcroft prep_compound_huge_page(page, h->order); 2167ef5a22beSAndrea Arcangeli WARN_ON(PageReserved(page)); 2168aa888a74SAndi Kleen prep_new_huge_page(h, page, page_to_nid(page)); 2169b0320c7bSRafael Aquini /* 2170b0320c7bSRafael Aquini * If we had gigantic hugepages allocated at boot time, we need 2171b0320c7bSRafael Aquini * to restore the 'stolen' pages to totalram_pages in order to 2172b0320c7bSRafael Aquini * fix confusing memory reports from free(1) and another 2173b0320c7bSRafael Aquini * side-effects, like CommitLimit going negative. 2174b0320c7bSRafael Aquini */ 2175bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 21763dcc0571SJiang Liu adjust_managed_page_count(page, 1 << h->order); 2177aa888a74SAndi Kleen } 2178aa888a74SAndi Kleen } 2179aa888a74SAndi Kleen 21808faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h) 21811da177e4SLinus Torvalds { 21821da177e4SLinus Torvalds unsigned long i; 21831da177e4SLinus Torvalds 2184e5ff2159SAndi Kleen for (i = 0; i < h->max_huge_pages; ++i) { 2185bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) { 2186aa888a74SAndi Kleen if (!alloc_bootmem_huge_page(h)) 2187aa888a74SAndi Kleen break; 21889b5e5d0fSLee Schermerhorn } else if (!alloc_fresh_huge_page(h, 21898cebfcd0SLai Jiangshan &node_states[N_MEMORY])) 21901da177e4SLinus Torvalds break; 21911da177e4SLinus Torvalds } 21928faa8b07SAndi Kleen h->max_huge_pages = i; 2193e5ff2159SAndi Kleen } 2194e5ff2159SAndi Kleen 2195e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void) 2196e5ff2159SAndi Kleen { 2197e5ff2159SAndi Kleen struct hstate *h; 2198e5ff2159SAndi Kleen 2199e5ff2159SAndi Kleen for_each_hstate(h) { 2200641844f5SNaoya Horiguchi if (minimum_order > huge_page_order(h)) 2201641844f5SNaoya Horiguchi minimum_order = huge_page_order(h); 2202641844f5SNaoya Horiguchi 22038faa8b07SAndi Kleen /* oversize hugepages were init'ed in early boot */ 2204bae7f4aeSLuiz Capitulino if (!hstate_is_gigantic(h)) 22058faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(h); 2206e5ff2159SAndi Kleen } 2207641844f5SNaoya Horiguchi VM_BUG_ON(minimum_order == UINT_MAX); 2208e5ff2159SAndi Kleen } 2209e5ff2159SAndi Kleen 22104abd32dbSAndi Kleen static char * __init memfmt(char *buf, unsigned long n) 22114abd32dbSAndi Kleen { 22124abd32dbSAndi Kleen if (n >= (1UL << 30)) 22134abd32dbSAndi Kleen sprintf(buf, "%lu GB", n >> 30); 22144abd32dbSAndi Kleen else if (n >= (1UL << 20)) 22154abd32dbSAndi Kleen sprintf(buf, "%lu MB", n >> 20); 22164abd32dbSAndi Kleen else 22174abd32dbSAndi Kleen sprintf(buf, "%lu KB", n >> 10); 22184abd32dbSAndi Kleen return buf; 22194abd32dbSAndi Kleen } 22204abd32dbSAndi Kleen 2221e5ff2159SAndi Kleen static void __init report_hugepages(void) 2222e5ff2159SAndi Kleen { 2223e5ff2159SAndi Kleen struct hstate *h; 2224e5ff2159SAndi Kleen 2225e5ff2159SAndi Kleen for_each_hstate(h) { 22264abd32dbSAndi Kleen char buf[32]; 2227ffb22af5SAndrew Morton pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n", 22284abd32dbSAndi Kleen memfmt(buf, huge_page_size(h)), 22294abd32dbSAndi Kleen h->free_huge_pages); 2230e5ff2159SAndi Kleen } 2231e5ff2159SAndi Kleen } 2232e5ff2159SAndi Kleen 22331da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM 22346ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count, 22356ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 22361da177e4SLinus Torvalds { 22374415cc8dSChristoph Lameter int i; 22384415cc8dSChristoph Lameter 2239bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 2240aa888a74SAndi Kleen return; 2241aa888a74SAndi Kleen 22426ae11b27SLee Schermerhorn for_each_node_mask(i, *nodes_allowed) { 22431da177e4SLinus Torvalds struct page *page, *next; 2244a5516438SAndi Kleen struct list_head *freel = &h->hugepage_freelists[i]; 2245a5516438SAndi Kleen list_for_each_entry_safe(page, next, freel, lru) { 2246a5516438SAndi Kleen if (count >= h->nr_huge_pages) 22476b0c880dSAdam Litke return; 22481da177e4SLinus Torvalds if (PageHighMem(page)) 22491da177e4SLinus Torvalds continue; 22501da177e4SLinus Torvalds list_del(&page->lru); 2251e5ff2159SAndi Kleen update_and_free_page(h, page); 2252a5516438SAndi Kleen h->free_huge_pages--; 2253a5516438SAndi Kleen h->free_huge_pages_node[page_to_nid(page)]--; 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds #else 22586ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count, 22596ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 22601da177e4SLinus Torvalds { 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds #endif 22631da177e4SLinus Torvalds 226420a0307cSWu Fengguang /* 226520a0307cSWu Fengguang * Increment or decrement surplus_huge_pages. Keep node-specific counters 226620a0307cSWu Fengguang * balanced by operating on them in a round-robin fashion. 226720a0307cSWu Fengguang * Returns 1 if an adjustment was made. 226820a0307cSWu Fengguang */ 22696ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, 22706ae11b27SLee Schermerhorn int delta) 227120a0307cSWu Fengguang { 2272b2261026SJoonsoo Kim int nr_nodes, node; 227320a0307cSWu Fengguang 227420a0307cSWu Fengguang VM_BUG_ON(delta != -1 && delta != 1); 227520a0307cSWu Fengguang 2276e8c5c824SLee Schermerhorn if (delta < 0) { 2277b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 2278b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node]) 2279b2261026SJoonsoo Kim goto found; 2280b2261026SJoonsoo Kim } 2281b2261026SJoonsoo Kim } else { 2282b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 2283b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node] < 2284b2261026SJoonsoo Kim h->nr_huge_pages_node[node]) 2285b2261026SJoonsoo Kim goto found; 2286e8c5c824SLee Schermerhorn } 22879a76db09SLee Schermerhorn } 2288b2261026SJoonsoo Kim return 0; 228920a0307cSWu Fengguang 2290b2261026SJoonsoo Kim found: 229120a0307cSWu Fengguang h->surplus_huge_pages += delta; 2292b2261026SJoonsoo Kim h->surplus_huge_pages_node[node] += delta; 2293b2261026SJoonsoo Kim return 1; 229420a0307cSWu Fengguang } 229520a0307cSWu Fengguang 2296a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) 22976ae11b27SLee Schermerhorn static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count, 22986ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 22991da177e4SLinus Torvalds { 23007893d1d5SAdam Litke unsigned long min_count, ret; 23011da177e4SLinus Torvalds 2302944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 2303aa888a74SAndi Kleen return h->max_huge_pages; 2304aa888a74SAndi Kleen 23057893d1d5SAdam Litke /* 23067893d1d5SAdam Litke * Increase the pool size 23077893d1d5SAdam Litke * First take pages out of surplus state. Then make up the 23087893d1d5SAdam Litke * remaining difference by allocating fresh huge pages. 2309d1c3fb1fSNishanth Aravamudan * 2310d15c7c09SNaoya Horiguchi * We might race with __alloc_buddy_huge_page() here and be unable 2311d1c3fb1fSNishanth Aravamudan * to convert a surplus huge page to a normal huge page. That is 2312d1c3fb1fSNishanth Aravamudan * not critical, though, it just means the overall size of the 2313d1c3fb1fSNishanth Aravamudan * pool might be one hugepage larger than it needs to be, but 2314d1c3fb1fSNishanth Aravamudan * within all the constraints specified by the sysctls. 23157893d1d5SAdam Litke */ 23161da177e4SLinus Torvalds spin_lock(&hugetlb_lock); 2317a5516438SAndi Kleen while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { 23186ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, -1)) 23197893d1d5SAdam Litke break; 23207893d1d5SAdam Litke } 23217893d1d5SAdam Litke 2322a5516438SAndi Kleen while (count > persistent_huge_pages(h)) { 23237893d1d5SAdam Litke /* 23247893d1d5SAdam Litke * If this allocation races such that we no longer need the 23257893d1d5SAdam Litke * page, free_huge_page will handle it by freeing the page 23267893d1d5SAdam Litke * and reducing the surplus. 23277893d1d5SAdam Litke */ 23287893d1d5SAdam Litke spin_unlock(&hugetlb_lock); 2329649920c6SJia He 2330649920c6SJia He /* yield cpu to avoid soft lockup */ 2331649920c6SJia He cond_resched(); 2332649920c6SJia He 2333944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) 2334944d9fecSLuiz Capitulino ret = alloc_fresh_gigantic_page(h, nodes_allowed); 2335944d9fecSLuiz Capitulino else 23366ae11b27SLee Schermerhorn ret = alloc_fresh_huge_page(h, nodes_allowed); 23377893d1d5SAdam Litke spin_lock(&hugetlb_lock); 23387893d1d5SAdam Litke if (!ret) 23397893d1d5SAdam Litke goto out; 23407893d1d5SAdam Litke 2341536240f2SMel Gorman /* Bail for signals. Probably ctrl-c from user */ 2342536240f2SMel Gorman if (signal_pending(current)) 2343536240f2SMel Gorman goto out; 23447893d1d5SAdam Litke } 23457893d1d5SAdam Litke 23467893d1d5SAdam Litke /* 23477893d1d5SAdam Litke * Decrease the pool size 23487893d1d5SAdam Litke * First return free pages to the buddy allocator (being careful 23497893d1d5SAdam Litke * to keep enough around to satisfy reservations). Then place 23507893d1d5SAdam Litke * pages into surplus state as needed so the pool will shrink 23517893d1d5SAdam Litke * to the desired size as pages become free. 2352d1c3fb1fSNishanth Aravamudan * 2353d1c3fb1fSNishanth Aravamudan * By placing pages into the surplus state independent of the 2354d1c3fb1fSNishanth Aravamudan * overcommit value, we are allowing the surplus pool size to 2355d1c3fb1fSNishanth Aravamudan * exceed overcommit. There are few sane options here. Since 2356d15c7c09SNaoya Horiguchi * __alloc_buddy_huge_page() is checking the global counter, 2357d1c3fb1fSNishanth Aravamudan * though, we'll note that we're not allowed to exceed surplus 2358d1c3fb1fSNishanth Aravamudan * and won't grow the pool anywhere else. Not until one of the 2359d1c3fb1fSNishanth Aravamudan * sysctls are changed, or the surplus pages go out of use. 23607893d1d5SAdam Litke */ 2361a5516438SAndi Kleen min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; 23626b0c880dSAdam Litke min_count = max(count, min_count); 23636ae11b27SLee Schermerhorn try_to_free_low(h, min_count, nodes_allowed); 2364a5516438SAndi Kleen while (min_count < persistent_huge_pages(h)) { 23656ae11b27SLee Schermerhorn if (!free_pool_huge_page(h, nodes_allowed, 0)) 23661da177e4SLinus Torvalds break; 236755f67141SMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 23681da177e4SLinus Torvalds } 2369a5516438SAndi Kleen while (count < persistent_huge_pages(h)) { 23706ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, 1)) 23717893d1d5SAdam Litke break; 23727893d1d5SAdam Litke } 23737893d1d5SAdam Litke out: 2374a5516438SAndi Kleen ret = persistent_huge_pages(h); 23751da177e4SLinus Torvalds spin_unlock(&hugetlb_lock); 23767893d1d5SAdam Litke return ret; 23771da177e4SLinus Torvalds } 23781da177e4SLinus Torvalds 2379a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \ 2380a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 2381a3437870SNishanth Aravamudan 2382a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \ 2383a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = \ 2384a3437870SNishanth Aravamudan __ATTR(_name, 0644, _name##_show, _name##_store) 2385a3437870SNishanth Aravamudan 2386a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj; 2387a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 2388a3437870SNishanth Aravamudan 23899a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp); 23909a305230SLee Schermerhorn 23919a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp) 2392a3437870SNishanth Aravamudan { 2393a3437870SNishanth Aravamudan int i; 23949a305230SLee Schermerhorn 2395a3437870SNishanth Aravamudan for (i = 0; i < HUGE_MAX_HSTATE; i++) 23969a305230SLee Schermerhorn if (hstate_kobjs[i] == kobj) { 23979a305230SLee Schermerhorn if (nidp) 23989a305230SLee Schermerhorn *nidp = NUMA_NO_NODE; 2399a3437870SNishanth Aravamudan return &hstates[i]; 24009a305230SLee Schermerhorn } 24019a305230SLee Schermerhorn 24029a305230SLee Schermerhorn return kobj_to_node_hstate(kobj, nidp); 2403a3437870SNishanth Aravamudan } 2404a3437870SNishanth Aravamudan 240506808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj, 2406a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2407a3437870SNishanth Aravamudan { 24089a305230SLee Schermerhorn struct hstate *h; 24099a305230SLee Schermerhorn unsigned long nr_huge_pages; 24109a305230SLee Schermerhorn int nid; 24119a305230SLee Schermerhorn 24129a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 24139a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 24149a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages; 24159a305230SLee Schermerhorn else 24169a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages_node[nid]; 24179a305230SLee Schermerhorn 24189a305230SLee Schermerhorn return sprintf(buf, "%lu\n", nr_huge_pages); 2419a3437870SNishanth Aravamudan } 2420adbe8726SEric B Munson 2421238d3c13SDavid Rientjes static ssize_t __nr_hugepages_store_common(bool obey_mempolicy, 2422238d3c13SDavid Rientjes struct hstate *h, int nid, 2423238d3c13SDavid Rientjes unsigned long count, size_t len) 2424a3437870SNishanth Aravamudan { 2425a3437870SNishanth Aravamudan int err; 2426bad44b5bSDavid Rientjes NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY); 2427a3437870SNishanth Aravamudan 2428944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) { 2429adbe8726SEric B Munson err = -EINVAL; 2430adbe8726SEric B Munson goto out; 2431adbe8726SEric B Munson } 2432adbe8726SEric B Munson 24339a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) { 24349a305230SLee Schermerhorn /* 24359a305230SLee Schermerhorn * global hstate attribute 24369a305230SLee Schermerhorn */ 24379a305230SLee Schermerhorn if (!(obey_mempolicy && 24389a305230SLee Schermerhorn init_nodemask_of_mempolicy(nodes_allowed))) { 243906808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 24408cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 244106808b08SLee Schermerhorn } 24429a305230SLee Schermerhorn } else if (nodes_allowed) { 24439a305230SLee Schermerhorn /* 24449a305230SLee Schermerhorn * per node hstate attribute: adjust count to global, 24459a305230SLee Schermerhorn * but restrict alloc/free to the specified node. 24469a305230SLee Schermerhorn */ 24479a305230SLee Schermerhorn count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; 24489a305230SLee Schermerhorn init_nodemask_of_node(nodes_allowed, nid); 24499a305230SLee Schermerhorn } else 24508cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 24519a305230SLee Schermerhorn 245206808b08SLee Schermerhorn h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed); 2453a3437870SNishanth Aravamudan 24548cebfcd0SLai Jiangshan if (nodes_allowed != &node_states[N_MEMORY]) 245506808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 245606808b08SLee Schermerhorn 245706808b08SLee Schermerhorn return len; 2458adbe8726SEric B Munson out: 2459adbe8726SEric B Munson NODEMASK_FREE(nodes_allowed); 2460adbe8726SEric B Munson return err; 246106808b08SLee Schermerhorn } 246206808b08SLee Schermerhorn 2463238d3c13SDavid Rientjes static ssize_t nr_hugepages_store_common(bool obey_mempolicy, 2464238d3c13SDavid Rientjes struct kobject *kobj, const char *buf, 2465238d3c13SDavid Rientjes size_t len) 2466238d3c13SDavid Rientjes { 2467238d3c13SDavid Rientjes struct hstate *h; 2468238d3c13SDavid Rientjes unsigned long count; 2469238d3c13SDavid Rientjes int nid; 2470238d3c13SDavid Rientjes int err; 2471238d3c13SDavid Rientjes 2472238d3c13SDavid Rientjes err = kstrtoul(buf, 10, &count); 2473238d3c13SDavid Rientjes if (err) 2474238d3c13SDavid Rientjes return err; 2475238d3c13SDavid Rientjes 2476238d3c13SDavid Rientjes h = kobj_to_hstate(kobj, &nid); 2477238d3c13SDavid Rientjes return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len); 2478238d3c13SDavid Rientjes } 2479238d3c13SDavid Rientjes 248006808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj, 248106808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 248206808b08SLee Schermerhorn { 248306808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 248406808b08SLee Schermerhorn } 248506808b08SLee Schermerhorn 248606808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj, 248706808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 248806808b08SLee Schermerhorn { 2489238d3c13SDavid Rientjes return nr_hugepages_store_common(false, kobj, buf, len); 2490a3437870SNishanth Aravamudan } 2491a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages); 2492a3437870SNishanth Aravamudan 249306808b08SLee Schermerhorn #ifdef CONFIG_NUMA 249406808b08SLee Schermerhorn 249506808b08SLee Schermerhorn /* 249606808b08SLee Schermerhorn * hstate attribute for optionally mempolicy-based constraint on persistent 249706808b08SLee Schermerhorn * huge page alloc/free. 249806808b08SLee Schermerhorn */ 249906808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj, 250006808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 250106808b08SLee Schermerhorn { 250206808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 250306808b08SLee Schermerhorn } 250406808b08SLee Schermerhorn 250506808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj, 250606808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 250706808b08SLee Schermerhorn { 2508238d3c13SDavid Rientjes return nr_hugepages_store_common(true, kobj, buf, len); 250906808b08SLee Schermerhorn } 251006808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy); 251106808b08SLee Schermerhorn #endif 251206808b08SLee Schermerhorn 251306808b08SLee Schermerhorn 2514a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, 2515a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2516a3437870SNishanth Aravamudan { 25179a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2518a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages); 2519a3437870SNishanth Aravamudan } 2520adbe8726SEric B Munson 2521a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, 2522a3437870SNishanth Aravamudan struct kobj_attribute *attr, const char *buf, size_t count) 2523a3437870SNishanth Aravamudan { 2524a3437870SNishanth Aravamudan int err; 2525a3437870SNishanth Aravamudan unsigned long input; 25269a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2527a3437870SNishanth Aravamudan 2528bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 2529adbe8726SEric B Munson return -EINVAL; 2530adbe8726SEric B Munson 25313dbb95f7SJingoo Han err = kstrtoul(buf, 10, &input); 2532a3437870SNishanth Aravamudan if (err) 253373ae31e5SEric B Munson return err; 2534a3437870SNishanth Aravamudan 2535a3437870SNishanth Aravamudan spin_lock(&hugetlb_lock); 2536a3437870SNishanth Aravamudan h->nr_overcommit_huge_pages = input; 2537a3437870SNishanth Aravamudan spin_unlock(&hugetlb_lock); 2538a3437870SNishanth Aravamudan 2539a3437870SNishanth Aravamudan return count; 2540a3437870SNishanth Aravamudan } 2541a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages); 2542a3437870SNishanth Aravamudan 2543a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj, 2544a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2545a3437870SNishanth Aravamudan { 25469a305230SLee Schermerhorn struct hstate *h; 25479a305230SLee Schermerhorn unsigned long free_huge_pages; 25489a305230SLee Schermerhorn int nid; 25499a305230SLee Schermerhorn 25509a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 25519a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 25529a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages; 25539a305230SLee Schermerhorn else 25549a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages_node[nid]; 25559a305230SLee Schermerhorn 25569a305230SLee Schermerhorn return sprintf(buf, "%lu\n", free_huge_pages); 2557a3437870SNishanth Aravamudan } 2558a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages); 2559a3437870SNishanth Aravamudan 2560a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj, 2561a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2562a3437870SNishanth Aravamudan { 25639a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2564a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->resv_huge_pages); 2565a3437870SNishanth Aravamudan } 2566a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages); 2567a3437870SNishanth Aravamudan 2568a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj, 2569a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2570a3437870SNishanth Aravamudan { 25719a305230SLee Schermerhorn struct hstate *h; 25729a305230SLee Schermerhorn unsigned long surplus_huge_pages; 25739a305230SLee Schermerhorn int nid; 25749a305230SLee Schermerhorn 25759a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 25769a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 25779a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages; 25789a305230SLee Schermerhorn else 25799a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages_node[nid]; 25809a305230SLee Schermerhorn 25819a305230SLee Schermerhorn return sprintf(buf, "%lu\n", surplus_huge_pages); 2582a3437870SNishanth Aravamudan } 2583a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages); 2584a3437870SNishanth Aravamudan 2585a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = { 2586a3437870SNishanth Aravamudan &nr_hugepages_attr.attr, 2587a3437870SNishanth Aravamudan &nr_overcommit_hugepages_attr.attr, 2588a3437870SNishanth Aravamudan &free_hugepages_attr.attr, 2589a3437870SNishanth Aravamudan &resv_hugepages_attr.attr, 2590a3437870SNishanth Aravamudan &surplus_hugepages_attr.attr, 259106808b08SLee Schermerhorn #ifdef CONFIG_NUMA 259206808b08SLee Schermerhorn &nr_hugepages_mempolicy_attr.attr, 259306808b08SLee Schermerhorn #endif 2594a3437870SNishanth Aravamudan NULL, 2595a3437870SNishanth Aravamudan }; 2596a3437870SNishanth Aravamudan 2597a3437870SNishanth Aravamudan static struct attribute_group hstate_attr_group = { 2598a3437870SNishanth Aravamudan .attrs = hstate_attrs, 2599a3437870SNishanth Aravamudan }; 2600a3437870SNishanth Aravamudan 2601094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, 26029a305230SLee Schermerhorn struct kobject **hstate_kobjs, 26039a305230SLee Schermerhorn struct attribute_group *hstate_attr_group) 2604a3437870SNishanth Aravamudan { 2605a3437870SNishanth Aravamudan int retval; 2606972dc4deSAneesh Kumar K.V int hi = hstate_index(h); 2607a3437870SNishanth Aravamudan 26089a305230SLee Schermerhorn hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); 26099a305230SLee Schermerhorn if (!hstate_kobjs[hi]) 2610a3437870SNishanth Aravamudan return -ENOMEM; 2611a3437870SNishanth Aravamudan 26129a305230SLee Schermerhorn retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group); 2613a3437870SNishanth Aravamudan if (retval) 26149a305230SLee Schermerhorn kobject_put(hstate_kobjs[hi]); 2615a3437870SNishanth Aravamudan 2616a3437870SNishanth Aravamudan return retval; 2617a3437870SNishanth Aravamudan } 2618a3437870SNishanth Aravamudan 2619a3437870SNishanth Aravamudan static void __init hugetlb_sysfs_init(void) 2620a3437870SNishanth Aravamudan { 2621a3437870SNishanth Aravamudan struct hstate *h; 2622a3437870SNishanth Aravamudan int err; 2623a3437870SNishanth Aravamudan 2624a3437870SNishanth Aravamudan hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); 2625a3437870SNishanth Aravamudan if (!hugepages_kobj) 2626a3437870SNishanth Aravamudan return; 2627a3437870SNishanth Aravamudan 2628a3437870SNishanth Aravamudan for_each_hstate(h) { 26299a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, 26309a305230SLee Schermerhorn hstate_kobjs, &hstate_attr_group); 2631a3437870SNishanth Aravamudan if (err) 2632ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s", h->name); 2633a3437870SNishanth Aravamudan } 2634a3437870SNishanth Aravamudan } 2635a3437870SNishanth Aravamudan 26369a305230SLee Schermerhorn #ifdef CONFIG_NUMA 26379a305230SLee Schermerhorn 26389a305230SLee Schermerhorn /* 26399a305230SLee Schermerhorn * node_hstate/s - associate per node hstate attributes, via their kobjects, 264010fbcf4cSKay Sievers * with node devices in node_devices[] using a parallel array. The array 264110fbcf4cSKay Sievers * index of a node device or _hstate == node id. 264210fbcf4cSKay Sievers * This is here to avoid any static dependency of the node device driver, in 26439a305230SLee Schermerhorn * the base kernel, on the hugetlb module. 26449a305230SLee Schermerhorn */ 26459a305230SLee Schermerhorn struct node_hstate { 26469a305230SLee Schermerhorn struct kobject *hugepages_kobj; 26479a305230SLee Schermerhorn struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 26489a305230SLee Schermerhorn }; 2649b4e289a6SAlexander Kuleshov static struct node_hstate node_hstates[MAX_NUMNODES]; 26509a305230SLee Schermerhorn 26519a305230SLee Schermerhorn /* 265210fbcf4cSKay Sievers * A subset of global hstate attributes for node devices 26539a305230SLee Schermerhorn */ 26549a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = { 26559a305230SLee Schermerhorn &nr_hugepages_attr.attr, 26569a305230SLee Schermerhorn &free_hugepages_attr.attr, 26579a305230SLee Schermerhorn &surplus_hugepages_attr.attr, 26589a305230SLee Schermerhorn NULL, 26599a305230SLee Schermerhorn }; 26609a305230SLee Schermerhorn 26619a305230SLee Schermerhorn static struct attribute_group per_node_hstate_attr_group = { 26629a305230SLee Schermerhorn .attrs = per_node_hstate_attrs, 26639a305230SLee Schermerhorn }; 26649a305230SLee Schermerhorn 26659a305230SLee Schermerhorn /* 266610fbcf4cSKay Sievers * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj. 26679a305230SLee Schermerhorn * Returns node id via non-NULL nidp. 26689a305230SLee Schermerhorn */ 26699a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 26709a305230SLee Schermerhorn { 26719a305230SLee Schermerhorn int nid; 26729a305230SLee Schermerhorn 26739a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) { 26749a305230SLee Schermerhorn struct node_hstate *nhs = &node_hstates[nid]; 26759a305230SLee Schermerhorn int i; 26769a305230SLee Schermerhorn for (i = 0; i < HUGE_MAX_HSTATE; i++) 26779a305230SLee Schermerhorn if (nhs->hstate_kobjs[i] == kobj) { 26789a305230SLee Schermerhorn if (nidp) 26799a305230SLee Schermerhorn *nidp = nid; 26809a305230SLee Schermerhorn return &hstates[i]; 26819a305230SLee Schermerhorn } 26829a305230SLee Schermerhorn } 26839a305230SLee Schermerhorn 26849a305230SLee Schermerhorn BUG(); 26859a305230SLee Schermerhorn return NULL; 26869a305230SLee Schermerhorn } 26879a305230SLee Schermerhorn 26889a305230SLee Schermerhorn /* 268910fbcf4cSKay Sievers * Unregister hstate attributes from a single node device. 26909a305230SLee Schermerhorn * No-op if no hstate attributes attached. 26919a305230SLee Schermerhorn */ 26923cd8b44fSClaudiu Ghioc static void hugetlb_unregister_node(struct node *node) 26939a305230SLee Schermerhorn { 26949a305230SLee Schermerhorn struct hstate *h; 269510fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 26969a305230SLee Schermerhorn 26979a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 26989b5e5d0fSLee Schermerhorn return; /* no hstate attributes */ 26999a305230SLee Schermerhorn 2700972dc4deSAneesh Kumar K.V for_each_hstate(h) { 2701972dc4deSAneesh Kumar K.V int idx = hstate_index(h); 2702972dc4deSAneesh Kumar K.V if (nhs->hstate_kobjs[idx]) { 2703972dc4deSAneesh Kumar K.V kobject_put(nhs->hstate_kobjs[idx]); 2704972dc4deSAneesh Kumar K.V nhs->hstate_kobjs[idx] = NULL; 2705972dc4deSAneesh Kumar K.V } 27069a305230SLee Schermerhorn } 27079a305230SLee Schermerhorn 27089a305230SLee Schermerhorn kobject_put(nhs->hugepages_kobj); 27099a305230SLee Schermerhorn nhs->hugepages_kobj = NULL; 27109a305230SLee Schermerhorn } 27119a305230SLee Schermerhorn 27129a305230SLee Schermerhorn 27139a305230SLee Schermerhorn /* 271410fbcf4cSKay Sievers * Register hstate attributes for a single node device. 27159a305230SLee Schermerhorn * No-op if attributes already registered. 27169a305230SLee Schermerhorn */ 27173cd8b44fSClaudiu Ghioc static void hugetlb_register_node(struct node *node) 27189a305230SLee Schermerhorn { 27199a305230SLee Schermerhorn struct hstate *h; 272010fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 27219a305230SLee Schermerhorn int err; 27229a305230SLee Schermerhorn 27239a305230SLee Schermerhorn if (nhs->hugepages_kobj) 27249a305230SLee Schermerhorn return; /* already allocated */ 27259a305230SLee Schermerhorn 27269a305230SLee Schermerhorn nhs->hugepages_kobj = kobject_create_and_add("hugepages", 272710fbcf4cSKay Sievers &node->dev.kobj); 27289a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 27299a305230SLee Schermerhorn return; 27309a305230SLee Schermerhorn 27319a305230SLee Schermerhorn for_each_hstate(h) { 27329a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, 27339a305230SLee Schermerhorn nhs->hstate_kobjs, 27349a305230SLee Schermerhorn &per_node_hstate_attr_group); 27359a305230SLee Schermerhorn if (err) { 2736ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s for node %d\n", 273710fbcf4cSKay Sievers h->name, node->dev.id); 27389a305230SLee Schermerhorn hugetlb_unregister_node(node); 27399a305230SLee Schermerhorn break; 27409a305230SLee Schermerhorn } 27419a305230SLee Schermerhorn } 27429a305230SLee Schermerhorn } 27439a305230SLee Schermerhorn 27449a305230SLee Schermerhorn /* 27459b5e5d0fSLee Schermerhorn * hugetlb init time: register hstate attributes for all registered node 274610fbcf4cSKay Sievers * devices of nodes that have memory. All on-line nodes should have 274710fbcf4cSKay Sievers * registered their associated device by this time. 27489a305230SLee Schermerhorn */ 27497d9ca000SLuiz Capitulino static void __init hugetlb_register_all_nodes(void) 27509a305230SLee Schermerhorn { 27519a305230SLee Schermerhorn int nid; 27529a305230SLee Schermerhorn 27538cebfcd0SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 27548732794bSWen Congyang struct node *node = node_devices[nid]; 275510fbcf4cSKay Sievers if (node->dev.id == nid) 27569a305230SLee Schermerhorn hugetlb_register_node(node); 27579a305230SLee Schermerhorn } 27589a305230SLee Schermerhorn 27599a305230SLee Schermerhorn /* 276010fbcf4cSKay Sievers * Let the node device driver know we're here so it can 27619a305230SLee Schermerhorn * [un]register hstate attributes on node hotplug. 27629a305230SLee Schermerhorn */ 27639a305230SLee Schermerhorn register_hugetlbfs_with_node(hugetlb_register_node, 27649a305230SLee Schermerhorn hugetlb_unregister_node); 27659a305230SLee Schermerhorn } 27669a305230SLee Schermerhorn #else /* !CONFIG_NUMA */ 27679a305230SLee Schermerhorn 27689a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 27699a305230SLee Schermerhorn { 27709a305230SLee Schermerhorn BUG(); 27719a305230SLee Schermerhorn if (nidp) 27729a305230SLee Schermerhorn *nidp = -1; 27739a305230SLee Schermerhorn return NULL; 27749a305230SLee Schermerhorn } 27759a305230SLee Schermerhorn 27769a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { } 27779a305230SLee Schermerhorn 27789a305230SLee Schermerhorn #endif 27799a305230SLee Schermerhorn 2780a3437870SNishanth Aravamudan static int __init hugetlb_init(void) 2781a3437870SNishanth Aravamudan { 27828382d914SDavidlohr Bueso int i; 27838382d914SDavidlohr Bueso 2784457c1b27SNishanth Aravamudan if (!hugepages_supported()) 27850ef89d25SBenjamin Herrenschmidt return 0; 2786a3437870SNishanth Aravamudan 2787e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) { 2788e11bfbfcSNick Piggin default_hstate_size = HPAGE_SIZE; 2789e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) 2790a3437870SNishanth Aravamudan hugetlb_add_hstate(HUGETLB_PAGE_ORDER); 2791a3437870SNishanth Aravamudan } 2792972dc4deSAneesh Kumar K.V default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size)); 2793f8b74815SVaishali Thakkar if (default_hstate_max_huge_pages) { 2794f8b74815SVaishali Thakkar if (!default_hstate.max_huge_pages) 2795e11bfbfcSNick Piggin default_hstate.max_huge_pages = default_hstate_max_huge_pages; 2796f8b74815SVaishali Thakkar } 2797a3437870SNishanth Aravamudan 2798a3437870SNishanth Aravamudan hugetlb_init_hstates(); 2799aa888a74SAndi Kleen gather_bootmem_prealloc(); 2800a3437870SNishanth Aravamudan report_hugepages(); 2801a3437870SNishanth Aravamudan 2802a3437870SNishanth Aravamudan hugetlb_sysfs_init(); 28039a305230SLee Schermerhorn hugetlb_register_all_nodes(); 28047179e7bfSJianguo Wu hugetlb_cgroup_file_init(); 28059a305230SLee Schermerhorn 28068382d914SDavidlohr Bueso #ifdef CONFIG_SMP 28078382d914SDavidlohr Bueso num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); 28088382d914SDavidlohr Bueso #else 28098382d914SDavidlohr Bueso num_fault_mutexes = 1; 28108382d914SDavidlohr Bueso #endif 2811c672c7f2SMike Kravetz hugetlb_fault_mutex_table = 28128382d914SDavidlohr Bueso kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL); 2813c672c7f2SMike Kravetz BUG_ON(!hugetlb_fault_mutex_table); 28148382d914SDavidlohr Bueso 28158382d914SDavidlohr Bueso for (i = 0; i < num_fault_mutexes; i++) 2816c672c7f2SMike Kravetz mutex_init(&hugetlb_fault_mutex_table[i]); 2817a3437870SNishanth Aravamudan return 0; 2818a3437870SNishanth Aravamudan } 28193e89e1c5SPaul Gortmaker subsys_initcall(hugetlb_init); 2820a3437870SNishanth Aravamudan 2821a3437870SNishanth Aravamudan /* Should be called on processing a hugepagesz=... option */ 28229fee021dSVaishali Thakkar void __init hugetlb_bad_size(void) 28239fee021dSVaishali Thakkar { 28249fee021dSVaishali Thakkar parsed_valid_hugepagesz = false; 28259fee021dSVaishali Thakkar } 28269fee021dSVaishali Thakkar 2827d00181b9SKirill A. Shutemov void __init hugetlb_add_hstate(unsigned int order) 2828a3437870SNishanth Aravamudan { 2829a3437870SNishanth Aravamudan struct hstate *h; 28308faa8b07SAndi Kleen unsigned long i; 28318faa8b07SAndi Kleen 2832a3437870SNishanth Aravamudan if (size_to_hstate(PAGE_SIZE << order)) { 2833598d8091SJoe Perches pr_warn("hugepagesz= specified twice, ignoring\n"); 2834a3437870SNishanth Aravamudan return; 2835a3437870SNishanth Aravamudan } 283647d38344SAneesh Kumar K.V BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); 2837a3437870SNishanth Aravamudan BUG_ON(order == 0); 283847d38344SAneesh Kumar K.V h = &hstates[hugetlb_max_hstate++]; 2839a3437870SNishanth Aravamudan h->order = order; 2840a3437870SNishanth Aravamudan h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); 28418faa8b07SAndi Kleen h->nr_huge_pages = 0; 28428faa8b07SAndi Kleen h->free_huge_pages = 0; 28438faa8b07SAndi Kleen for (i = 0; i < MAX_NUMNODES; ++i) 28448faa8b07SAndi Kleen INIT_LIST_HEAD(&h->hugepage_freelists[i]); 28450edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&h->hugepage_activelist); 284654f18d35SAndrew Morton h->next_nid_to_alloc = first_memory_node; 284754f18d35SAndrew Morton h->next_nid_to_free = first_memory_node; 2848a3437870SNishanth Aravamudan snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", 2849a3437870SNishanth Aravamudan huge_page_size(h)/1024); 28508faa8b07SAndi Kleen 2851a3437870SNishanth Aravamudan parsed_hstate = h; 2852a3437870SNishanth Aravamudan } 2853a3437870SNishanth Aravamudan 2854e11bfbfcSNick Piggin static int __init hugetlb_nrpages_setup(char *s) 2855a3437870SNishanth Aravamudan { 2856a3437870SNishanth Aravamudan unsigned long *mhp; 28578faa8b07SAndi Kleen static unsigned long *last_mhp; 2858a3437870SNishanth Aravamudan 28599fee021dSVaishali Thakkar if (!parsed_valid_hugepagesz) { 28609fee021dSVaishali Thakkar pr_warn("hugepages = %s preceded by " 28619fee021dSVaishali Thakkar "an unsupported hugepagesz, ignoring\n", s); 28629fee021dSVaishali Thakkar parsed_valid_hugepagesz = true; 28639fee021dSVaishali Thakkar return 1; 28649fee021dSVaishali Thakkar } 2865a3437870SNishanth Aravamudan /* 286647d38344SAneesh Kumar K.V * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet, 2867a3437870SNishanth Aravamudan * so this hugepages= parameter goes to the "default hstate". 2868a3437870SNishanth Aravamudan */ 28699fee021dSVaishali Thakkar else if (!hugetlb_max_hstate) 2870a3437870SNishanth Aravamudan mhp = &default_hstate_max_huge_pages; 2871a3437870SNishanth Aravamudan else 2872a3437870SNishanth Aravamudan mhp = &parsed_hstate->max_huge_pages; 2873a3437870SNishanth Aravamudan 28748faa8b07SAndi Kleen if (mhp == last_mhp) { 2875598d8091SJoe Perches pr_warn("hugepages= specified twice without interleaving hugepagesz=, ignoring\n"); 28768faa8b07SAndi Kleen return 1; 28778faa8b07SAndi Kleen } 28788faa8b07SAndi Kleen 2879a3437870SNishanth Aravamudan if (sscanf(s, "%lu", mhp) <= 0) 2880a3437870SNishanth Aravamudan *mhp = 0; 2881a3437870SNishanth Aravamudan 28828faa8b07SAndi Kleen /* 28838faa8b07SAndi Kleen * Global state is always initialized later in hugetlb_init. 28848faa8b07SAndi Kleen * But we need to allocate >= MAX_ORDER hstates here early to still 28858faa8b07SAndi Kleen * use the bootmem allocator. 28868faa8b07SAndi Kleen */ 288747d38344SAneesh Kumar K.V if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER) 28888faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(parsed_hstate); 28898faa8b07SAndi Kleen 28908faa8b07SAndi Kleen last_mhp = mhp; 28918faa8b07SAndi Kleen 2892a3437870SNishanth Aravamudan return 1; 2893a3437870SNishanth Aravamudan } 2894e11bfbfcSNick Piggin __setup("hugepages=", hugetlb_nrpages_setup); 2895e11bfbfcSNick Piggin 2896e11bfbfcSNick Piggin static int __init hugetlb_default_setup(char *s) 2897e11bfbfcSNick Piggin { 2898e11bfbfcSNick Piggin default_hstate_size = memparse(s, &s); 2899e11bfbfcSNick Piggin return 1; 2900e11bfbfcSNick Piggin } 2901e11bfbfcSNick Piggin __setup("default_hugepagesz=", hugetlb_default_setup); 2902a3437870SNishanth Aravamudan 29038a213460SNishanth Aravamudan static unsigned int cpuset_mems_nr(unsigned int *array) 29048a213460SNishanth Aravamudan { 29058a213460SNishanth Aravamudan int node; 29068a213460SNishanth Aravamudan unsigned int nr = 0; 29078a213460SNishanth Aravamudan 29088a213460SNishanth Aravamudan for_each_node_mask(node, cpuset_current_mems_allowed) 29098a213460SNishanth Aravamudan nr += array[node]; 29108a213460SNishanth Aravamudan 29118a213460SNishanth Aravamudan return nr; 29128a213460SNishanth Aravamudan } 29138a213460SNishanth Aravamudan 29148a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL 291506808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy, 291606808b08SLee Schermerhorn struct ctl_table *table, int write, 291706808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 29181da177e4SLinus Torvalds { 2919e5ff2159SAndi Kleen struct hstate *h = &default_hstate; 2920238d3c13SDavid Rientjes unsigned long tmp = h->max_huge_pages; 292108d4a246SMichal Hocko int ret; 2922e5ff2159SAndi Kleen 2923457c1b27SNishanth Aravamudan if (!hugepages_supported()) 292486613628SJan Stancek return -EOPNOTSUPP; 2925457c1b27SNishanth Aravamudan 2926e5ff2159SAndi Kleen table->data = &tmp; 2927e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 292808d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 292908d4a246SMichal Hocko if (ret) 293008d4a246SMichal Hocko goto out; 2931e5ff2159SAndi Kleen 2932238d3c13SDavid Rientjes if (write) 2933238d3c13SDavid Rientjes ret = __nr_hugepages_store_common(obey_mempolicy, h, 2934238d3c13SDavid Rientjes NUMA_NO_NODE, tmp, *length); 293508d4a246SMichal Hocko out: 293608d4a246SMichal Hocko return ret; 29371da177e4SLinus Torvalds } 2938396faf03SMel Gorman 293906808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write, 294006808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 294106808b08SLee Schermerhorn { 294206808b08SLee Schermerhorn 294306808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(false, table, write, 294406808b08SLee Schermerhorn buffer, length, ppos); 294506808b08SLee Schermerhorn } 294606808b08SLee Schermerhorn 294706808b08SLee Schermerhorn #ifdef CONFIG_NUMA 294806808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write, 294906808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 295006808b08SLee Schermerhorn { 295106808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(true, table, write, 295206808b08SLee Schermerhorn buffer, length, ppos); 295306808b08SLee Schermerhorn } 295406808b08SLee Schermerhorn #endif /* CONFIG_NUMA */ 295506808b08SLee Schermerhorn 2956a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write, 29578d65af78SAlexey Dobriyan void __user *buffer, 2958a3d0c6aaSNishanth Aravamudan size_t *length, loff_t *ppos) 2959a3d0c6aaSNishanth Aravamudan { 2960a5516438SAndi Kleen struct hstate *h = &default_hstate; 2961e5ff2159SAndi Kleen unsigned long tmp; 296208d4a246SMichal Hocko int ret; 2963e5ff2159SAndi Kleen 2964457c1b27SNishanth Aravamudan if (!hugepages_supported()) 296586613628SJan Stancek return -EOPNOTSUPP; 2966457c1b27SNishanth Aravamudan 2967e5ff2159SAndi Kleen tmp = h->nr_overcommit_huge_pages; 2968e5ff2159SAndi Kleen 2969bae7f4aeSLuiz Capitulino if (write && hstate_is_gigantic(h)) 2970adbe8726SEric B Munson return -EINVAL; 2971adbe8726SEric B Munson 2972e5ff2159SAndi Kleen table->data = &tmp; 2973e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 297408d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 297508d4a246SMichal Hocko if (ret) 297608d4a246SMichal Hocko goto out; 2977e5ff2159SAndi Kleen 2978e5ff2159SAndi Kleen if (write) { 2979064d9efeSNishanth Aravamudan spin_lock(&hugetlb_lock); 2980e5ff2159SAndi Kleen h->nr_overcommit_huge_pages = tmp; 2981a3d0c6aaSNishanth Aravamudan spin_unlock(&hugetlb_lock); 2982e5ff2159SAndi Kleen } 298308d4a246SMichal Hocko out: 298408d4a246SMichal Hocko return ret; 2985a3d0c6aaSNishanth Aravamudan } 2986a3d0c6aaSNishanth Aravamudan 29871da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 29881da177e4SLinus Torvalds 2989e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m) 29901da177e4SLinus Torvalds { 2991a5516438SAndi Kleen struct hstate *h = &default_hstate; 2992457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2993457c1b27SNishanth Aravamudan return; 2994e1759c21SAlexey Dobriyan seq_printf(m, 29951da177e4SLinus Torvalds "HugePages_Total: %5lu\n" 29961da177e4SLinus Torvalds "HugePages_Free: %5lu\n" 2997b45b5bd6SDavid Gibson "HugePages_Rsvd: %5lu\n" 29987893d1d5SAdam Litke "HugePages_Surp: %5lu\n" 29994f98a2feSRik van Riel "Hugepagesize: %8lu kB\n", 3000a5516438SAndi Kleen h->nr_huge_pages, 3001a5516438SAndi Kleen h->free_huge_pages, 3002a5516438SAndi Kleen h->resv_huge_pages, 3003a5516438SAndi Kleen h->surplus_huge_pages, 3004a5516438SAndi Kleen 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 30051da177e4SLinus Torvalds } 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf) 30081da177e4SLinus Torvalds { 3009a5516438SAndi Kleen struct hstate *h = &default_hstate; 3010457c1b27SNishanth Aravamudan if (!hugepages_supported()) 3011457c1b27SNishanth Aravamudan return 0; 30121da177e4SLinus Torvalds return sprintf(buf, 30131da177e4SLinus Torvalds "Node %d HugePages_Total: %5u\n" 3014a1de0919SNishanth Aravamudan "Node %d HugePages_Free: %5u\n" 3015a1de0919SNishanth Aravamudan "Node %d HugePages_Surp: %5u\n", 3016a5516438SAndi Kleen nid, h->nr_huge_pages_node[nid], 3017a5516438SAndi Kleen nid, h->free_huge_pages_node[nid], 3018a5516438SAndi Kleen nid, h->surplus_huge_pages_node[nid]); 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds 3021949f7ec5SDavid Rientjes void hugetlb_show_meminfo(void) 3022949f7ec5SDavid Rientjes { 3023949f7ec5SDavid Rientjes struct hstate *h; 3024949f7ec5SDavid Rientjes int nid; 3025949f7ec5SDavid Rientjes 3026457c1b27SNishanth Aravamudan if (!hugepages_supported()) 3027457c1b27SNishanth Aravamudan return; 3028457c1b27SNishanth Aravamudan 3029949f7ec5SDavid Rientjes for_each_node_state(nid, N_MEMORY) 3030949f7ec5SDavid Rientjes for_each_hstate(h) 3031949f7ec5SDavid Rientjes pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", 3032949f7ec5SDavid Rientjes nid, 3033949f7ec5SDavid Rientjes h->nr_huge_pages_node[nid], 3034949f7ec5SDavid Rientjes h->free_huge_pages_node[nid], 3035949f7ec5SDavid Rientjes h->surplus_huge_pages_node[nid], 3036949f7ec5SDavid Rientjes 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 3037949f7ec5SDavid Rientjes } 3038949f7ec5SDavid Rientjes 30395d317b2bSNaoya Horiguchi void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm) 30405d317b2bSNaoya Horiguchi { 30415d317b2bSNaoya Horiguchi seq_printf(m, "HugetlbPages:\t%8lu kB\n", 30425d317b2bSNaoya Horiguchi atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10)); 30435d317b2bSNaoya Horiguchi } 30445d317b2bSNaoya Horiguchi 30451da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 30461da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void) 30471da177e4SLinus Torvalds { 3048d0028588SWanpeng Li struct hstate *h; 3049d0028588SWanpeng Li unsigned long nr_total_pages = 0; 3050d0028588SWanpeng Li 3051d0028588SWanpeng Li for_each_hstate(h) 3052d0028588SWanpeng Li nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); 3053d0028588SWanpeng Li return nr_total_pages; 30541da177e4SLinus Torvalds } 30551da177e4SLinus Torvalds 3056a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta) 3057fc1b8a73SMel Gorman { 3058fc1b8a73SMel Gorman int ret = -ENOMEM; 3059fc1b8a73SMel Gorman 3060fc1b8a73SMel Gorman spin_lock(&hugetlb_lock); 3061fc1b8a73SMel Gorman /* 3062fc1b8a73SMel Gorman * When cpuset is configured, it breaks the strict hugetlb page 3063fc1b8a73SMel Gorman * reservation as the accounting is done on a global variable. Such 3064fc1b8a73SMel Gorman * reservation is completely rubbish in the presence of cpuset because 3065fc1b8a73SMel Gorman * the reservation is not checked against page availability for the 3066fc1b8a73SMel Gorman * current cpuset. Application can still potentially OOM'ed by kernel 3067fc1b8a73SMel Gorman * with lack of free htlb page in cpuset that the task is in. 3068fc1b8a73SMel Gorman * Attempt to enforce strict accounting with cpuset is almost 3069fc1b8a73SMel Gorman * impossible (or too ugly) because cpuset is too fluid that 3070fc1b8a73SMel Gorman * task or memory node can be dynamically moved between cpusets. 3071fc1b8a73SMel Gorman * 3072fc1b8a73SMel Gorman * The change of semantics for shared hugetlb mapping with cpuset is 3073fc1b8a73SMel Gorman * undesirable. However, in order to preserve some of the semantics, 3074fc1b8a73SMel Gorman * we fall back to check against current free page availability as 3075fc1b8a73SMel Gorman * a best attempt and hopefully to minimize the impact of changing 3076fc1b8a73SMel Gorman * semantics that cpuset has. 3077fc1b8a73SMel Gorman */ 3078fc1b8a73SMel Gorman if (delta > 0) { 3079a5516438SAndi Kleen if (gather_surplus_pages(h, delta) < 0) 3080fc1b8a73SMel Gorman goto out; 3081fc1b8a73SMel Gorman 3082a5516438SAndi Kleen if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { 3083a5516438SAndi Kleen return_unused_surplus_pages(h, delta); 3084fc1b8a73SMel Gorman goto out; 3085fc1b8a73SMel Gorman } 3086fc1b8a73SMel Gorman } 3087fc1b8a73SMel Gorman 3088fc1b8a73SMel Gorman ret = 0; 3089fc1b8a73SMel Gorman if (delta < 0) 3090a5516438SAndi Kleen return_unused_surplus_pages(h, (unsigned long) -delta); 3091fc1b8a73SMel Gorman 3092fc1b8a73SMel Gorman out: 3093fc1b8a73SMel Gorman spin_unlock(&hugetlb_lock); 3094fc1b8a73SMel Gorman return ret; 3095fc1b8a73SMel Gorman } 3096fc1b8a73SMel Gorman 309784afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma) 309884afd99bSAndy Whitcroft { 3099f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 310084afd99bSAndy Whitcroft 310184afd99bSAndy Whitcroft /* 310284afd99bSAndy Whitcroft * This new VMA should share its siblings reservation map if present. 310384afd99bSAndy Whitcroft * The VMA will only ever have a valid reservation map pointer where 310484afd99bSAndy Whitcroft * it is being copied for another still existing VMA. As that VMA 310525985edcSLucas De Marchi * has a reference to the reservation map it cannot disappear until 310684afd99bSAndy Whitcroft * after this open call completes. It is therefore safe to take a 310784afd99bSAndy Whitcroft * new reference here without additional locking. 310884afd99bSAndy Whitcroft */ 31094e35f483SJoonsoo Kim if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 3110f522c3acSJoonsoo Kim kref_get(&resv->refs); 311184afd99bSAndy Whitcroft } 311284afd99bSAndy Whitcroft 3113a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma) 3114a1e78772SMel Gorman { 3115a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3116f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 311790481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 31184e35f483SJoonsoo Kim unsigned long reserve, start, end; 31191c5ecae3SMike Kravetz long gbl_reserve; 312084afd99bSAndy Whitcroft 31214e35f483SJoonsoo Kim if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 31224e35f483SJoonsoo Kim return; 31234e35f483SJoonsoo Kim 3124a5516438SAndi Kleen start = vma_hugecache_offset(h, vma, vma->vm_start); 3125a5516438SAndi Kleen end = vma_hugecache_offset(h, vma, vma->vm_end); 312684afd99bSAndy Whitcroft 31274e35f483SJoonsoo Kim reserve = (end - start) - region_count(resv, start, end); 312884afd99bSAndy Whitcroft 3129f031dd27SJoonsoo Kim kref_put(&resv->refs, resv_map_release); 313084afd99bSAndy Whitcroft 31317251ff78SAdam Litke if (reserve) { 31321c5ecae3SMike Kravetz /* 31331c5ecae3SMike Kravetz * Decrement reserve counts. The global reserve count may be 31341c5ecae3SMike Kravetz * adjusted if the subpool has a minimum size. 31351c5ecae3SMike Kravetz */ 31361c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, reserve); 31371c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 31387251ff78SAdam Litke } 3139a1e78772SMel Gorman } 3140a1e78772SMel Gorman 31411da177e4SLinus Torvalds /* 31421da177e4SLinus Torvalds * We cannot handle pagefaults against hugetlb pages at all. They cause 31431da177e4SLinus Torvalds * handle_mm_fault() to try to instantiate regular-sized pages in the 31441da177e4SLinus Torvalds * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get 31451da177e4SLinus Torvalds * this far. 31461da177e4SLinus Torvalds */ 314711bac800SDave Jiang static int hugetlb_vm_op_fault(struct vm_fault *vmf) 31481da177e4SLinus Torvalds { 31491da177e4SLinus Torvalds BUG(); 3150d0217ac0SNick Piggin return 0; 31511da177e4SLinus Torvalds } 31521da177e4SLinus Torvalds 3153f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = { 3154d0217ac0SNick Piggin .fault = hugetlb_vm_op_fault, 315584afd99bSAndy Whitcroft .open = hugetlb_vm_op_open, 3156a1e78772SMel Gorman .close = hugetlb_vm_op_close, 31571da177e4SLinus Torvalds }; 31581da177e4SLinus Torvalds 31591e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, 31601e8f889bSDavid Gibson int writable) 316163551ae0SDavid Gibson { 316263551ae0SDavid Gibson pte_t entry; 316363551ae0SDavid Gibson 31641e8f889bSDavid Gibson if (writable) { 3165106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, 3166106c992aSGerald Schaefer vma->vm_page_prot))); 316763551ae0SDavid Gibson } else { 3168106c992aSGerald Schaefer entry = huge_pte_wrprotect(mk_huge_pte(page, 3169106c992aSGerald Schaefer vma->vm_page_prot)); 317063551ae0SDavid Gibson } 317163551ae0SDavid Gibson entry = pte_mkyoung(entry); 317263551ae0SDavid Gibson entry = pte_mkhuge(entry); 3173d9ed9faaSChris Metcalf entry = arch_make_huge_pte(entry, vma, page, writable); 317463551ae0SDavid Gibson 317563551ae0SDavid Gibson return entry; 317663551ae0SDavid Gibson } 317763551ae0SDavid Gibson 31781e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma, 31791e8f889bSDavid Gibson unsigned long address, pte_t *ptep) 31801e8f889bSDavid Gibson { 31811e8f889bSDavid Gibson pte_t entry; 31821e8f889bSDavid Gibson 3183106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); 318432f84528SChris Forbes if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) 31854b3073e1SRussell King update_mmu_cache(vma, address, ptep); 31861e8f889bSDavid Gibson } 31871e8f889bSDavid Gibson 31884a705fefSNaoya Horiguchi static int is_hugetlb_entry_migration(pte_t pte) 31894a705fefSNaoya Horiguchi { 31904a705fefSNaoya Horiguchi swp_entry_t swp; 31914a705fefSNaoya Horiguchi 31924a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 31934a705fefSNaoya Horiguchi return 0; 31944a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 31954a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_migration_entry(swp)) 31964a705fefSNaoya Horiguchi return 1; 31974a705fefSNaoya Horiguchi else 31984a705fefSNaoya Horiguchi return 0; 31994a705fefSNaoya Horiguchi } 32004a705fefSNaoya Horiguchi 32014a705fefSNaoya Horiguchi static int is_hugetlb_entry_hwpoisoned(pte_t pte) 32024a705fefSNaoya Horiguchi { 32034a705fefSNaoya Horiguchi swp_entry_t swp; 32044a705fefSNaoya Horiguchi 32054a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 32064a705fefSNaoya Horiguchi return 0; 32074a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 32084a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_hwpoison_entry(swp)) 32094a705fefSNaoya Horiguchi return 1; 32104a705fefSNaoya Horiguchi else 32114a705fefSNaoya Horiguchi return 0; 32124a705fefSNaoya Horiguchi } 32131e8f889bSDavid Gibson 321463551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 321563551ae0SDavid Gibson struct vm_area_struct *vma) 321663551ae0SDavid Gibson { 321763551ae0SDavid Gibson pte_t *src_pte, *dst_pte, entry; 321863551ae0SDavid Gibson struct page *ptepage; 32191c59827dSHugh Dickins unsigned long addr; 32201e8f889bSDavid Gibson int cow; 3221a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3222a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 3223e8569dd2SAndreas Sandberg unsigned long mmun_start; /* For mmu_notifiers */ 3224e8569dd2SAndreas Sandberg unsigned long mmun_end; /* For mmu_notifiers */ 3225e8569dd2SAndreas Sandberg int ret = 0; 32261e8f889bSDavid Gibson 32271e8f889bSDavid Gibson cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 322863551ae0SDavid Gibson 3229e8569dd2SAndreas Sandberg mmun_start = vma->vm_start; 3230e8569dd2SAndreas Sandberg mmun_end = vma->vm_end; 3231e8569dd2SAndreas Sandberg if (cow) 3232e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end); 3233e8569dd2SAndreas Sandberg 3234a5516438SAndi Kleen for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { 3235cb900f41SKirill A. Shutemov spinlock_t *src_ptl, *dst_ptl; 3236c74df32cSHugh Dickins src_pte = huge_pte_offset(src, addr); 3237c74df32cSHugh Dickins if (!src_pte) 3238c74df32cSHugh Dickins continue; 3239a5516438SAndi Kleen dst_pte = huge_pte_alloc(dst, addr, sz); 3240e8569dd2SAndreas Sandberg if (!dst_pte) { 3241e8569dd2SAndreas Sandberg ret = -ENOMEM; 3242e8569dd2SAndreas Sandberg break; 3243e8569dd2SAndreas Sandberg } 3244c5c99429SLarry Woodman 3245c5c99429SLarry Woodman /* If the pagetables are shared don't copy or take references */ 3246c5c99429SLarry Woodman if (dst_pte == src_pte) 3247c5c99429SLarry Woodman continue; 3248c5c99429SLarry Woodman 3249cb900f41SKirill A. Shutemov dst_ptl = huge_pte_lock(h, dst, dst_pte); 3250cb900f41SKirill A. Shutemov src_ptl = huge_pte_lockptr(h, src, src_pte); 3251cb900f41SKirill A. Shutemov spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 32524a705fefSNaoya Horiguchi entry = huge_ptep_get(src_pte); 32534a705fefSNaoya Horiguchi if (huge_pte_none(entry)) { /* skip none entry */ 32544a705fefSNaoya Horiguchi ; 32554a705fefSNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_migration(entry) || 32564a705fefSNaoya Horiguchi is_hugetlb_entry_hwpoisoned(entry))) { 32574a705fefSNaoya Horiguchi swp_entry_t swp_entry = pte_to_swp_entry(entry); 32584a705fefSNaoya Horiguchi 32594a705fefSNaoya Horiguchi if (is_write_migration_entry(swp_entry) && cow) { 32604a705fefSNaoya Horiguchi /* 32614a705fefSNaoya Horiguchi * COW mappings require pages in both 32624a705fefSNaoya Horiguchi * parent and child to be set to read. 32634a705fefSNaoya Horiguchi */ 32644a705fefSNaoya Horiguchi make_migration_entry_read(&swp_entry); 32654a705fefSNaoya Horiguchi entry = swp_entry_to_pte(swp_entry); 32664a705fefSNaoya Horiguchi set_huge_pte_at(src, addr, src_pte, entry); 32674a705fefSNaoya Horiguchi } 32684a705fefSNaoya Horiguchi set_huge_pte_at(dst, addr, dst_pte, entry); 32694a705fefSNaoya Horiguchi } else { 327034ee645eSJoerg Roedel if (cow) { 32717f2e9525SGerald Schaefer huge_ptep_set_wrprotect(src, addr, src_pte); 327234ee645eSJoerg Roedel mmu_notifier_invalidate_range(src, mmun_start, 327334ee645eSJoerg Roedel mmun_end); 327434ee645eSJoerg Roedel } 32750253d634SNaoya Horiguchi entry = huge_ptep_get(src_pte); 327663551ae0SDavid Gibson ptepage = pte_page(entry); 327763551ae0SDavid Gibson get_page(ptepage); 327853f9263bSKirill A. Shutemov page_dup_rmap(ptepage, true); 327963551ae0SDavid Gibson set_huge_pte_at(dst, addr, dst_pte, entry); 32805d317b2bSNaoya Horiguchi hugetlb_count_add(pages_per_huge_page(h), dst); 32811c59827dSHugh Dickins } 3282cb900f41SKirill A. Shutemov spin_unlock(src_ptl); 3283cb900f41SKirill A. Shutemov spin_unlock(dst_ptl); 328463551ae0SDavid Gibson } 328563551ae0SDavid Gibson 3286e8569dd2SAndreas Sandberg if (cow) 3287e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end); 3288e8569dd2SAndreas Sandberg 3289e8569dd2SAndreas Sandberg return ret; 329063551ae0SDavid Gibson } 329163551ae0SDavid Gibson 329224669e58SAneesh Kumar K.V void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, 329324669e58SAneesh Kumar K.V unsigned long start, unsigned long end, 329424669e58SAneesh Kumar K.V struct page *ref_page) 329563551ae0SDavid Gibson { 329663551ae0SDavid Gibson struct mm_struct *mm = vma->vm_mm; 329763551ae0SDavid Gibson unsigned long address; 3298c7546f8fSDavid Gibson pte_t *ptep; 329963551ae0SDavid Gibson pte_t pte; 3300cb900f41SKirill A. Shutemov spinlock_t *ptl; 330163551ae0SDavid Gibson struct page *page; 3302a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3303a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 33042ec74c3eSSagi Grimberg const unsigned long mmun_start = start; /* For mmu_notifiers */ 33052ec74c3eSSagi Grimberg const unsigned long mmun_end = end; /* For mmu_notifiers */ 3306a5516438SAndi Kleen 330763551ae0SDavid Gibson WARN_ON(!is_vm_hugetlb_page(vma)); 3308a5516438SAndi Kleen BUG_ON(start & ~huge_page_mask(h)); 3309a5516438SAndi Kleen BUG_ON(end & ~huge_page_mask(h)); 331063551ae0SDavid Gibson 331107e32661SAneesh Kumar K.V /* 331207e32661SAneesh Kumar K.V * This is a hugetlb vma, all the pte entries should point 331307e32661SAneesh Kumar K.V * to huge page. 331407e32661SAneesh Kumar K.V */ 331507e32661SAneesh Kumar K.V tlb_remove_check_page_size_change(tlb, sz); 331624669e58SAneesh Kumar K.V tlb_start_vma(tlb, vma); 33172ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 3318569f48b8SHillf Danton address = start; 3319569f48b8SHillf Danton for (; address < end; address += sz) { 3320c7546f8fSDavid Gibson ptep = huge_pte_offset(mm, address); 3321c7546f8fSDavid Gibson if (!ptep) 3322c7546f8fSDavid Gibson continue; 3323c7546f8fSDavid Gibson 3324cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 332531d49da5SAneesh Kumar K.V if (huge_pmd_unshare(mm, &address, ptep)) { 332631d49da5SAneesh Kumar K.V spin_unlock(ptl); 332731d49da5SAneesh Kumar K.V continue; 332831d49da5SAneesh Kumar K.V } 332939dde65cSChen, Kenneth W 33306629326bSHillf Danton pte = huge_ptep_get(ptep); 333131d49da5SAneesh Kumar K.V if (huge_pte_none(pte)) { 333231d49da5SAneesh Kumar K.V spin_unlock(ptl); 333331d49da5SAneesh Kumar K.V continue; 333431d49da5SAneesh Kumar K.V } 33356629326bSHillf Danton 33366629326bSHillf Danton /* 33379fbc1f63SNaoya Horiguchi * Migrating hugepage or HWPoisoned hugepage is already 33389fbc1f63SNaoya Horiguchi * unmapped and its refcount is dropped, so just clear pte here. 33396629326bSHillf Danton */ 33409fbc1f63SNaoya Horiguchi if (unlikely(!pte_present(pte))) { 3341106c992aSGerald Schaefer huge_pte_clear(mm, address, ptep); 334231d49da5SAneesh Kumar K.V spin_unlock(ptl); 334331d49da5SAneesh Kumar K.V continue; 33448c4894c6SNaoya Horiguchi } 33456629326bSHillf Danton 33466629326bSHillf Danton page = pte_page(pte); 334704f2cbe3SMel Gorman /* 334804f2cbe3SMel Gorman * If a reference page is supplied, it is because a specific 334904f2cbe3SMel Gorman * page is being unmapped, not a range. Ensure the page we 335004f2cbe3SMel Gorman * are about to unmap is the actual page of interest. 335104f2cbe3SMel Gorman */ 335204f2cbe3SMel Gorman if (ref_page) { 335331d49da5SAneesh Kumar K.V if (page != ref_page) { 335431d49da5SAneesh Kumar K.V spin_unlock(ptl); 335531d49da5SAneesh Kumar K.V continue; 335631d49da5SAneesh Kumar K.V } 335704f2cbe3SMel Gorman /* 335804f2cbe3SMel Gorman * Mark the VMA as having unmapped its page so that 335904f2cbe3SMel Gorman * future faults in this VMA will fail rather than 336004f2cbe3SMel Gorman * looking like data was lost 336104f2cbe3SMel Gorman */ 336204f2cbe3SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); 336304f2cbe3SMel Gorman } 336404f2cbe3SMel Gorman 3365c7546f8fSDavid Gibson pte = huge_ptep_get_and_clear(mm, address, ptep); 3366b528e4b6SAneesh Kumar K.V tlb_remove_huge_tlb_entry(h, tlb, ptep, address); 3367106c992aSGerald Schaefer if (huge_pte_dirty(pte)) 33686649a386SKen Chen set_page_dirty(page); 33699e81130bSHillf Danton 33705d317b2bSNaoya Horiguchi hugetlb_count_sub(pages_per_huge_page(h), mm); 3371d281ee61SKirill A. Shutemov page_remove_rmap(page, true); 337231d49da5SAneesh Kumar K.V 3373cb900f41SKirill A. Shutemov spin_unlock(ptl); 3374e77b0852SAneesh Kumar K.V tlb_remove_page_size(tlb, page, huge_page_size(h)); 337524669e58SAneesh Kumar K.V /* 337631d49da5SAneesh Kumar K.V * Bail out after unmapping reference page if supplied 337724669e58SAneesh Kumar K.V */ 337831d49da5SAneesh Kumar K.V if (ref_page) 337931d49da5SAneesh Kumar K.V break; 3380fe1668aeSChen, Kenneth W } 33812ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 338224669e58SAneesh Kumar K.V tlb_end_vma(tlb, vma); 33831da177e4SLinus Torvalds } 338463551ae0SDavid Gibson 3385d833352aSMel Gorman void __unmap_hugepage_range_final(struct mmu_gather *tlb, 3386d833352aSMel Gorman struct vm_area_struct *vma, unsigned long start, 3387d833352aSMel Gorman unsigned long end, struct page *ref_page) 3388d833352aSMel Gorman { 3389d833352aSMel Gorman __unmap_hugepage_range(tlb, vma, start, end, ref_page); 3390d833352aSMel Gorman 3391d833352aSMel Gorman /* 3392d833352aSMel Gorman * Clear this flag so that x86's huge_pmd_share page_table_shareable 3393d833352aSMel Gorman * test will fail on a vma being torn down, and not grab a page table 3394d833352aSMel Gorman * on its way out. We're lucky that the flag has such an appropriate 3395d833352aSMel Gorman * name, and can in fact be safely cleared here. We could clear it 3396d833352aSMel Gorman * before the __unmap_hugepage_range above, but all that's necessary 3397c8c06efaSDavidlohr Bueso * is to clear it before releasing the i_mmap_rwsem. This works 3398d833352aSMel Gorman * because in the context this is called, the VMA is about to be 3399c8c06efaSDavidlohr Bueso * destroyed and the i_mmap_rwsem is held. 3400d833352aSMel Gorman */ 3401d833352aSMel Gorman vma->vm_flags &= ~VM_MAYSHARE; 3402d833352aSMel Gorman } 3403d833352aSMel Gorman 3404502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 340504f2cbe3SMel Gorman unsigned long end, struct page *ref_page) 3406502717f4SChen, Kenneth W { 340724669e58SAneesh Kumar K.V struct mm_struct *mm; 340824669e58SAneesh Kumar K.V struct mmu_gather tlb; 340924669e58SAneesh Kumar K.V 341024669e58SAneesh Kumar K.V mm = vma->vm_mm; 341124669e58SAneesh Kumar K.V 34122b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, start, end); 341324669e58SAneesh Kumar K.V __unmap_hugepage_range(&tlb, vma, start, end, ref_page); 341424669e58SAneesh Kumar K.V tlb_finish_mmu(&tlb, start, end); 3415502717f4SChen, Kenneth W } 3416502717f4SChen, Kenneth W 341704f2cbe3SMel Gorman /* 341804f2cbe3SMel Gorman * This is called when the original mapper is failing to COW a MAP_PRIVATE 341904f2cbe3SMel Gorman * mappping it owns the reserve page for. The intention is to unmap the page 342004f2cbe3SMel Gorman * from other VMAs and let the children be SIGKILLed if they are faulting the 342104f2cbe3SMel Gorman * same region. 342204f2cbe3SMel Gorman */ 34232f4612afSDavidlohr Bueso static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, 34242a4b3dedSHarvey Harrison struct page *page, unsigned long address) 342504f2cbe3SMel Gorman { 34267526674dSAdam Litke struct hstate *h = hstate_vma(vma); 342704f2cbe3SMel Gorman struct vm_area_struct *iter_vma; 342804f2cbe3SMel Gorman struct address_space *mapping; 342904f2cbe3SMel Gorman pgoff_t pgoff; 343004f2cbe3SMel Gorman 343104f2cbe3SMel Gorman /* 343204f2cbe3SMel Gorman * vm_pgoff is in PAGE_SIZE units, hence the different calculation 343304f2cbe3SMel Gorman * from page cache lookup which is in HPAGE_SIZE units. 343404f2cbe3SMel Gorman */ 34357526674dSAdam Litke address = address & huge_page_mask(h); 343636e4f20aSMichal Hocko pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + 343736e4f20aSMichal Hocko vma->vm_pgoff; 343893c76a3dSAl Viro mapping = vma->vm_file->f_mapping; 343904f2cbe3SMel Gorman 34404eb2b1dcSMel Gorman /* 34414eb2b1dcSMel Gorman * Take the mapping lock for the duration of the table walk. As 34424eb2b1dcSMel Gorman * this mapping should be shared between all the VMAs, 34434eb2b1dcSMel Gorman * __unmap_hugepage_range() is called as the lock is already held 34444eb2b1dcSMel Gorman */ 344583cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 34466b2dbba8SMichel Lespinasse vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { 344704f2cbe3SMel Gorman /* Do not unmap the current VMA */ 344804f2cbe3SMel Gorman if (iter_vma == vma) 344904f2cbe3SMel Gorman continue; 345004f2cbe3SMel Gorman 345104f2cbe3SMel Gorman /* 34522f84a899SMel Gorman * Shared VMAs have their own reserves and do not affect 34532f84a899SMel Gorman * MAP_PRIVATE accounting but it is possible that a shared 34542f84a899SMel Gorman * VMA is using the same page so check and skip such VMAs. 34552f84a899SMel Gorman */ 34562f84a899SMel Gorman if (iter_vma->vm_flags & VM_MAYSHARE) 34572f84a899SMel Gorman continue; 34582f84a899SMel Gorman 34592f84a899SMel Gorman /* 346004f2cbe3SMel Gorman * Unmap the page from other VMAs without their own reserves. 346104f2cbe3SMel Gorman * They get marked to be SIGKILLed if they fault in these 346204f2cbe3SMel Gorman * areas. This is because a future no-page fault on this VMA 346304f2cbe3SMel Gorman * could insert a zeroed page instead of the data existing 346404f2cbe3SMel Gorman * from the time of fork. This would look like data corruption 346504f2cbe3SMel Gorman */ 346604f2cbe3SMel Gorman if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) 346724669e58SAneesh Kumar K.V unmap_hugepage_range(iter_vma, address, 346824669e58SAneesh Kumar K.V address + huge_page_size(h), page); 346904f2cbe3SMel Gorman } 347083cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 347104f2cbe3SMel Gorman } 347204f2cbe3SMel Gorman 34730fe6e20bSNaoya Horiguchi /* 34740fe6e20bSNaoya Horiguchi * Hugetlb_cow() should be called with page lock of the original hugepage held. 3475ef009b25SMichal Hocko * Called with hugetlb_instantiation_mutex held and pte_page locked so we 3476ef009b25SMichal Hocko * cannot race with other handlers or page migration. 3477ef009b25SMichal Hocko * Keep the pte_same checks anyway to make transition from the mutex easier. 34780fe6e20bSNaoya Horiguchi */ 34791e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, 34803999f52eSAneesh Kumar K.V unsigned long address, pte_t *ptep, 3481cb900f41SKirill A. Shutemov struct page *pagecache_page, spinlock_t *ptl) 34821e8f889bSDavid Gibson { 34833999f52eSAneesh Kumar K.V pte_t pte; 3484a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 34851e8f889bSDavid Gibson struct page *old_page, *new_page; 3486ad4404a2SDavidlohr Bueso int ret = 0, outside_reserve = 0; 34872ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 34882ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 34891e8f889bSDavid Gibson 34903999f52eSAneesh Kumar K.V pte = huge_ptep_get(ptep); 34911e8f889bSDavid Gibson old_page = pte_page(pte); 34921e8f889bSDavid Gibson 349304f2cbe3SMel Gorman retry_avoidcopy: 34941e8f889bSDavid Gibson /* If no-one else is actually using this page, avoid the copy 34951e8f889bSDavid Gibson * and just make the page writable */ 349637a2140dSJoonsoo Kim if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { 34975a49973dSHugh Dickins page_move_anon_rmap(old_page, vma); 34981e8f889bSDavid Gibson set_huge_ptep_writable(vma, address, ptep); 349983c54070SNick Piggin return 0; 35001e8f889bSDavid Gibson } 35011e8f889bSDavid Gibson 350204f2cbe3SMel Gorman /* 350304f2cbe3SMel Gorman * If the process that created a MAP_PRIVATE mapping is about to 350404f2cbe3SMel Gorman * perform a COW due to a shared page count, attempt to satisfy 350504f2cbe3SMel Gorman * the allocation without using the existing reserves. The pagecache 350604f2cbe3SMel Gorman * page is used to determine if the reserve at this address was 350704f2cbe3SMel Gorman * consumed or not. If reserves were used, a partial faulted mapping 350804f2cbe3SMel Gorman * at the time of fork() could consume its reserves on COW instead 350904f2cbe3SMel Gorman * of the full address range. 351004f2cbe3SMel Gorman */ 35115944d011SJoonsoo Kim if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && 351204f2cbe3SMel Gorman old_page != pagecache_page) 351304f2cbe3SMel Gorman outside_reserve = 1; 351404f2cbe3SMel Gorman 351509cbfeafSKirill A. Shutemov get_page(old_page); 3516b76c8cfbSLarry Woodman 3517ad4404a2SDavidlohr Bueso /* 3518ad4404a2SDavidlohr Bueso * Drop page table lock as buddy allocator may be called. It will 3519ad4404a2SDavidlohr Bueso * be acquired again before returning to the caller, as expected. 3520ad4404a2SDavidlohr Bueso */ 3521cb900f41SKirill A. Shutemov spin_unlock(ptl); 352204f2cbe3SMel Gorman new_page = alloc_huge_page(vma, address, outside_reserve); 35231e8f889bSDavid Gibson 35242fc39cecSAdam Litke if (IS_ERR(new_page)) { 352504f2cbe3SMel Gorman /* 352604f2cbe3SMel Gorman * If a process owning a MAP_PRIVATE mapping fails to COW, 352704f2cbe3SMel Gorman * it is due to references held by a child and an insufficient 352804f2cbe3SMel Gorman * huge page pool. To guarantee the original mappers 352904f2cbe3SMel Gorman * reliability, unmap the page from child processes. The child 353004f2cbe3SMel Gorman * may get SIGKILLed if it later faults. 353104f2cbe3SMel Gorman */ 353204f2cbe3SMel Gorman if (outside_reserve) { 353309cbfeafSKirill A. Shutemov put_page(old_page); 353404f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 35352f4612afSDavidlohr Bueso unmap_ref_private(mm, vma, old_page, address); 353604f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 3537cb900f41SKirill A. Shutemov spin_lock(ptl); 3538a734bcc8SHillf Danton ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 3539a9af0c5dSNaoya Horiguchi if (likely(ptep && 3540a9af0c5dSNaoya Horiguchi pte_same(huge_ptep_get(ptep), pte))) 354104f2cbe3SMel Gorman goto retry_avoidcopy; 3542a734bcc8SHillf Danton /* 3543cb900f41SKirill A. Shutemov * race occurs while re-acquiring page table 3544cb900f41SKirill A. Shutemov * lock, and our job is done. 3545a734bcc8SHillf Danton */ 3546a734bcc8SHillf Danton return 0; 354704f2cbe3SMel Gorman } 354804f2cbe3SMel Gorman 3549ad4404a2SDavidlohr Bueso ret = (PTR_ERR(new_page) == -ENOMEM) ? 3550ad4404a2SDavidlohr Bueso VM_FAULT_OOM : VM_FAULT_SIGBUS; 3551ad4404a2SDavidlohr Bueso goto out_release_old; 35521e8f889bSDavid Gibson } 35531e8f889bSDavid Gibson 35540fe6e20bSNaoya Horiguchi /* 35550fe6e20bSNaoya Horiguchi * When the original hugepage is shared one, it does not have 35560fe6e20bSNaoya Horiguchi * anon_vma prepared. 35570fe6e20bSNaoya Horiguchi */ 355844e2aa93SDean Nelson if (unlikely(anon_vma_prepare(vma))) { 3559ad4404a2SDavidlohr Bueso ret = VM_FAULT_OOM; 3560ad4404a2SDavidlohr Bueso goto out_release_all; 356144e2aa93SDean Nelson } 35620fe6e20bSNaoya Horiguchi 356347ad8475SAndrea Arcangeli copy_user_huge_page(new_page, old_page, address, vma, 356447ad8475SAndrea Arcangeli pages_per_huge_page(h)); 35650ed361deSNick Piggin __SetPageUptodate(new_page); 3566bcc54222SNaoya Horiguchi set_page_huge_active(new_page); 35671e8f889bSDavid Gibson 35682ec74c3eSSagi Grimberg mmun_start = address & huge_page_mask(h); 35692ec74c3eSSagi Grimberg mmun_end = mmun_start + huge_page_size(h); 35702ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 3571ad4404a2SDavidlohr Bueso 3572b76c8cfbSLarry Woodman /* 3573cb900f41SKirill A. Shutemov * Retake the page table lock to check for racing updates 3574b76c8cfbSLarry Woodman * before the page tables are altered 3575b76c8cfbSLarry Woodman */ 3576cb900f41SKirill A. Shutemov spin_lock(ptl); 3577a5516438SAndi Kleen ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 3578a9af0c5dSNaoya Horiguchi if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { 357907443a85SJoonsoo Kim ClearPagePrivate(new_page); 358007443a85SJoonsoo Kim 35811e8f889bSDavid Gibson /* Break COW */ 35828fe627ecSGerald Schaefer huge_ptep_clear_flush(vma, address, ptep); 358334ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, mmun_start, mmun_end); 35841e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, 35851e8f889bSDavid Gibson make_huge_pte(vma, new_page, 1)); 3586d281ee61SKirill A. Shutemov page_remove_rmap(old_page, true); 3587cd67f0d2SNaoya Horiguchi hugepage_add_new_anon_rmap(new_page, vma, address); 35881e8f889bSDavid Gibson /* Make the old page be freed below */ 35891e8f889bSDavid Gibson new_page = old_page; 35901e8f889bSDavid Gibson } 3591cb900f41SKirill A. Shutemov spin_unlock(ptl); 35922ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 3593ad4404a2SDavidlohr Bueso out_release_all: 359496b96a96SMike Kravetz restore_reserve_on_error(h, vma, address, new_page); 359509cbfeafSKirill A. Shutemov put_page(new_page); 3596ad4404a2SDavidlohr Bueso out_release_old: 359709cbfeafSKirill A. Shutemov put_page(old_page); 35988312034fSJoonsoo Kim 3599ad4404a2SDavidlohr Bueso spin_lock(ptl); /* Caller expects lock to be held */ 3600ad4404a2SDavidlohr Bueso return ret; 36011e8f889bSDavid Gibson } 36021e8f889bSDavid Gibson 360304f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */ 3604a5516438SAndi Kleen static struct page *hugetlbfs_pagecache_page(struct hstate *h, 3605a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 360604f2cbe3SMel Gorman { 360704f2cbe3SMel Gorman struct address_space *mapping; 3608e7c4b0bfSAndy Whitcroft pgoff_t idx; 360904f2cbe3SMel Gorman 361004f2cbe3SMel Gorman mapping = vma->vm_file->f_mapping; 3611a5516438SAndi Kleen idx = vma_hugecache_offset(h, vma, address); 361204f2cbe3SMel Gorman 361304f2cbe3SMel Gorman return find_lock_page(mapping, idx); 361404f2cbe3SMel Gorman } 361504f2cbe3SMel Gorman 36163ae77f43SHugh Dickins /* 36173ae77f43SHugh Dickins * Return whether there is a pagecache page to back given address within VMA. 36183ae77f43SHugh Dickins * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page. 36193ae77f43SHugh Dickins */ 36203ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h, 36212a15efc9SHugh Dickins struct vm_area_struct *vma, unsigned long address) 36222a15efc9SHugh Dickins { 36232a15efc9SHugh Dickins struct address_space *mapping; 36242a15efc9SHugh Dickins pgoff_t idx; 36252a15efc9SHugh Dickins struct page *page; 36262a15efc9SHugh Dickins 36272a15efc9SHugh Dickins mapping = vma->vm_file->f_mapping; 36282a15efc9SHugh Dickins idx = vma_hugecache_offset(h, vma, address); 36292a15efc9SHugh Dickins 36302a15efc9SHugh Dickins page = find_get_page(mapping, idx); 36312a15efc9SHugh Dickins if (page) 36322a15efc9SHugh Dickins put_page(page); 36332a15efc9SHugh Dickins return page != NULL; 36342a15efc9SHugh Dickins } 36352a15efc9SHugh Dickins 3636ab76ad54SMike Kravetz int huge_add_to_page_cache(struct page *page, struct address_space *mapping, 3637ab76ad54SMike Kravetz pgoff_t idx) 3638ab76ad54SMike Kravetz { 3639ab76ad54SMike Kravetz struct inode *inode = mapping->host; 3640ab76ad54SMike Kravetz struct hstate *h = hstate_inode(inode); 3641ab76ad54SMike Kravetz int err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); 3642ab76ad54SMike Kravetz 3643ab76ad54SMike Kravetz if (err) 3644ab76ad54SMike Kravetz return err; 3645ab76ad54SMike Kravetz ClearPagePrivate(page); 3646ab76ad54SMike Kravetz 3647ab76ad54SMike Kravetz spin_lock(&inode->i_lock); 3648ab76ad54SMike Kravetz inode->i_blocks += blocks_per_huge_page(h); 3649ab76ad54SMike Kravetz spin_unlock(&inode->i_lock); 3650ab76ad54SMike Kravetz return 0; 3651ab76ad54SMike Kravetz } 3652ab76ad54SMike Kravetz 3653a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, 36548382d914SDavidlohr Bueso struct address_space *mapping, pgoff_t idx, 3655788c7df4SHugh Dickins unsigned long address, pte_t *ptep, unsigned int flags) 3656ac9b9c66SHugh Dickins { 3657a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3658ac9b9c66SHugh Dickins int ret = VM_FAULT_SIGBUS; 3659409eb8c2SHillf Danton int anon_rmap = 0; 36604c887265SAdam Litke unsigned long size; 36614c887265SAdam Litke struct page *page; 36621e8f889bSDavid Gibson pte_t new_pte; 3663cb900f41SKirill A. Shutemov spinlock_t *ptl; 36644c887265SAdam Litke 366504f2cbe3SMel Gorman /* 366604f2cbe3SMel Gorman * Currently, we are forced to kill the process in the event the 366704f2cbe3SMel Gorman * original mapper has unmapped pages from the child due to a failed 366825985edcSLucas De Marchi * COW. Warn that such a situation has occurred as it may not be obvious 366904f2cbe3SMel Gorman */ 367004f2cbe3SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { 3671910154d5SGeoffrey Thomas pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n", 367204f2cbe3SMel Gorman current->pid); 367304f2cbe3SMel Gorman return ret; 367404f2cbe3SMel Gorman } 367504f2cbe3SMel Gorman 36764c887265SAdam Litke /* 36774c887265SAdam Litke * Use page lock to guard against racing truncation 36784c887265SAdam Litke * before we get page_table_lock. 36794c887265SAdam Litke */ 36806bda666aSChristoph Lameter retry: 36816bda666aSChristoph Lameter page = find_lock_page(mapping, idx); 36826bda666aSChristoph Lameter if (!page) { 3683a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 3684ebed4bfcSHugh Dickins if (idx >= size) 3685ebed4bfcSHugh Dickins goto out; 36861a1aad8aSMike Kravetz 36871a1aad8aSMike Kravetz /* 36881a1aad8aSMike Kravetz * Check for page in userfault range 36891a1aad8aSMike Kravetz */ 36901a1aad8aSMike Kravetz if (userfaultfd_missing(vma)) { 36911a1aad8aSMike Kravetz u32 hash; 36921a1aad8aSMike Kravetz struct vm_fault vmf = { 36931a1aad8aSMike Kravetz .vma = vma, 36941a1aad8aSMike Kravetz .address = address, 36951a1aad8aSMike Kravetz .flags = flags, 36961a1aad8aSMike Kravetz /* 36971a1aad8aSMike Kravetz * Hard to debug if it ends up being 36981a1aad8aSMike Kravetz * used by a callee that assumes 36991a1aad8aSMike Kravetz * something about the other 37001a1aad8aSMike Kravetz * uninitialized fields... same as in 37011a1aad8aSMike Kravetz * memory.c 37021a1aad8aSMike Kravetz */ 37031a1aad8aSMike Kravetz }; 37041a1aad8aSMike Kravetz 37051a1aad8aSMike Kravetz /* 37061a1aad8aSMike Kravetz * hugetlb_fault_mutex must be dropped before 37071a1aad8aSMike Kravetz * handling userfault. Reacquire after handling 37081a1aad8aSMike Kravetz * fault to make calling code simpler. 37091a1aad8aSMike Kravetz */ 37101a1aad8aSMike Kravetz hash = hugetlb_fault_mutex_hash(h, mm, vma, mapping, 37111a1aad8aSMike Kravetz idx, address); 37121a1aad8aSMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 37131a1aad8aSMike Kravetz ret = handle_userfault(&vmf, VM_UFFD_MISSING); 37141a1aad8aSMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 37151a1aad8aSMike Kravetz goto out; 37161a1aad8aSMike Kravetz } 37171a1aad8aSMike Kravetz 371804f2cbe3SMel Gorman page = alloc_huge_page(vma, address, 0); 37192fc39cecSAdam Litke if (IS_ERR(page)) { 372076dcee75SAneesh Kumar K.V ret = PTR_ERR(page); 372176dcee75SAneesh Kumar K.V if (ret == -ENOMEM) 372276dcee75SAneesh Kumar K.V ret = VM_FAULT_OOM; 372376dcee75SAneesh Kumar K.V else 372476dcee75SAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 37256bda666aSChristoph Lameter goto out; 37266bda666aSChristoph Lameter } 372747ad8475SAndrea Arcangeli clear_huge_page(page, address, pages_per_huge_page(h)); 37280ed361deSNick Piggin __SetPageUptodate(page); 3729bcc54222SNaoya Horiguchi set_page_huge_active(page); 3730ac9b9c66SHugh Dickins 3731f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) { 3732ab76ad54SMike Kravetz int err = huge_add_to_page_cache(page, mapping, idx); 37336bda666aSChristoph Lameter if (err) { 37346bda666aSChristoph Lameter put_page(page); 37356bda666aSChristoph Lameter if (err == -EEXIST) 37366bda666aSChristoph Lameter goto retry; 37376bda666aSChristoph Lameter goto out; 37386bda666aSChristoph Lameter } 373923be7468SMel Gorman } else { 37406bda666aSChristoph Lameter lock_page(page); 37410fe6e20bSNaoya Horiguchi if (unlikely(anon_vma_prepare(vma))) { 37420fe6e20bSNaoya Horiguchi ret = VM_FAULT_OOM; 37430fe6e20bSNaoya Horiguchi goto backout_unlocked; 374423be7468SMel Gorman } 3745409eb8c2SHillf Danton anon_rmap = 1; 37460fe6e20bSNaoya Horiguchi } 37470fe6e20bSNaoya Horiguchi } else { 374857303d80SAndy Whitcroft /* 3749998b4382SNaoya Horiguchi * If memory error occurs between mmap() and fault, some process 3750998b4382SNaoya Horiguchi * don't have hwpoisoned swap entry for errored virtual address. 3751998b4382SNaoya Horiguchi * So we need to block hugepage fault by PG_hwpoison bit check. 3752fd6a03edSNaoya Horiguchi */ 3753fd6a03edSNaoya Horiguchi if (unlikely(PageHWPoison(page))) { 3754aa50d3a7SAndi Kleen ret = VM_FAULT_HWPOISON | 3755972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 3756fd6a03edSNaoya Horiguchi goto backout_unlocked; 37576bda666aSChristoph Lameter } 3758998b4382SNaoya Horiguchi } 37591e8f889bSDavid Gibson 376057303d80SAndy Whitcroft /* 376157303d80SAndy Whitcroft * If we are going to COW a private mapping later, we examine the 376257303d80SAndy Whitcroft * pending reservations for this page now. This will ensure that 376357303d80SAndy Whitcroft * any allocations necessary to record that reservation occur outside 376457303d80SAndy Whitcroft * the spinlock. 376557303d80SAndy Whitcroft */ 37665e911373SMike Kravetz if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 37672b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 37682b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 37692b26736cSAndy Whitcroft goto backout_unlocked; 37702b26736cSAndy Whitcroft } 37715e911373SMike Kravetz /* Just decrements count, does not deallocate */ 3772feba16e2SMike Kravetz vma_end_reservation(h, vma, address); 37735e911373SMike Kravetz } 377457303d80SAndy Whitcroft 37758bea8052SAneesh Kumar K.V ptl = huge_pte_lock(h, mm, ptep); 3776a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 37774c887265SAdam Litke if (idx >= size) 37784c887265SAdam Litke goto backout; 37794c887265SAdam Litke 378083c54070SNick Piggin ret = 0; 37817f2e9525SGerald Schaefer if (!huge_pte_none(huge_ptep_get(ptep))) 37824c887265SAdam Litke goto backout; 37834c887265SAdam Litke 378407443a85SJoonsoo Kim if (anon_rmap) { 378507443a85SJoonsoo Kim ClearPagePrivate(page); 3786409eb8c2SHillf Danton hugepage_add_new_anon_rmap(page, vma, address); 3787ac714904SChoi Gi-yong } else 378853f9263bSKirill A. Shutemov page_dup_rmap(page, true); 37891e8f889bSDavid Gibson new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) 37901e8f889bSDavid Gibson && (vma->vm_flags & VM_SHARED))); 37911e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, new_pte); 37921e8f889bSDavid Gibson 37935d317b2bSNaoya Horiguchi hugetlb_count_add(pages_per_huge_page(h), mm); 3794788c7df4SHugh Dickins if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 37951e8f889bSDavid Gibson /* Optimization, do the COW without a second fault */ 37963999f52eSAneesh Kumar K.V ret = hugetlb_cow(mm, vma, address, ptep, page, ptl); 37971e8f889bSDavid Gibson } 37981e8f889bSDavid Gibson 3799cb900f41SKirill A. Shutemov spin_unlock(ptl); 38004c887265SAdam Litke unlock_page(page); 38014c887265SAdam Litke out: 3802ac9b9c66SHugh Dickins return ret; 38034c887265SAdam Litke 38044c887265SAdam Litke backout: 3805cb900f41SKirill A. Shutemov spin_unlock(ptl); 38062b26736cSAndy Whitcroft backout_unlocked: 38074c887265SAdam Litke unlock_page(page); 380896b96a96SMike Kravetz restore_reserve_on_error(h, vma, address, page); 38094c887265SAdam Litke put_page(page); 38104c887265SAdam Litke goto out; 3811ac9b9c66SHugh Dickins } 3812ac9b9c66SHugh Dickins 38138382d914SDavidlohr Bueso #ifdef CONFIG_SMP 3814c672c7f2SMike Kravetz u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 38158382d914SDavidlohr Bueso struct vm_area_struct *vma, 38168382d914SDavidlohr Bueso struct address_space *mapping, 38178382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 38188382d914SDavidlohr Bueso { 38198382d914SDavidlohr Bueso unsigned long key[2]; 38208382d914SDavidlohr Bueso u32 hash; 38218382d914SDavidlohr Bueso 38228382d914SDavidlohr Bueso if (vma->vm_flags & VM_SHARED) { 38238382d914SDavidlohr Bueso key[0] = (unsigned long) mapping; 38248382d914SDavidlohr Bueso key[1] = idx; 38258382d914SDavidlohr Bueso } else { 38268382d914SDavidlohr Bueso key[0] = (unsigned long) mm; 38278382d914SDavidlohr Bueso key[1] = address >> huge_page_shift(h); 38288382d914SDavidlohr Bueso } 38298382d914SDavidlohr Bueso 38308382d914SDavidlohr Bueso hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0); 38318382d914SDavidlohr Bueso 38328382d914SDavidlohr Bueso return hash & (num_fault_mutexes - 1); 38338382d914SDavidlohr Bueso } 38348382d914SDavidlohr Bueso #else 38358382d914SDavidlohr Bueso /* 38368382d914SDavidlohr Bueso * For uniprocesor systems we always use a single mutex, so just 38378382d914SDavidlohr Bueso * return 0 and avoid the hashing overhead. 38388382d914SDavidlohr Bueso */ 3839c672c7f2SMike Kravetz u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 38408382d914SDavidlohr Bueso struct vm_area_struct *vma, 38418382d914SDavidlohr Bueso struct address_space *mapping, 38428382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 38438382d914SDavidlohr Bueso { 38448382d914SDavidlohr Bueso return 0; 38458382d914SDavidlohr Bueso } 38468382d914SDavidlohr Bueso #endif 38478382d914SDavidlohr Bueso 384886e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3849788c7df4SHugh Dickins unsigned long address, unsigned int flags) 385086e5216fSAdam Litke { 38518382d914SDavidlohr Bueso pte_t *ptep, entry; 3852cb900f41SKirill A. Shutemov spinlock_t *ptl; 38531e8f889bSDavid Gibson int ret; 38548382d914SDavidlohr Bueso u32 hash; 38558382d914SDavidlohr Bueso pgoff_t idx; 38560fe6e20bSNaoya Horiguchi struct page *page = NULL; 385757303d80SAndy Whitcroft struct page *pagecache_page = NULL; 3858a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 38598382d914SDavidlohr Bueso struct address_space *mapping; 38600f792cf9SNaoya Horiguchi int need_wait_lock = 0; 386186e5216fSAdam Litke 38621e16a539SKAMEZAWA Hiroyuki address &= huge_page_mask(h); 38631e16a539SKAMEZAWA Hiroyuki 3864fd6a03edSNaoya Horiguchi ptep = huge_pte_offset(mm, address); 3865fd6a03edSNaoya Horiguchi if (ptep) { 3866fd6a03edSNaoya Horiguchi entry = huge_ptep_get(ptep); 3867290408d4SNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(entry))) { 3868cb900f41SKirill A. Shutemov migration_entry_wait_huge(vma, mm, ptep); 3869290408d4SNaoya Horiguchi return 0; 3870290408d4SNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) 3871aa50d3a7SAndi Kleen return VM_FAULT_HWPOISON_LARGE | 3872972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 38730d777df5SNaoya Horiguchi } else { 3874a5516438SAndi Kleen ptep = huge_pte_alloc(mm, address, huge_page_size(h)); 387586e5216fSAdam Litke if (!ptep) 387686e5216fSAdam Litke return VM_FAULT_OOM; 38770d777df5SNaoya Horiguchi } 387886e5216fSAdam Litke 38798382d914SDavidlohr Bueso mapping = vma->vm_file->f_mapping; 38808382d914SDavidlohr Bueso idx = vma_hugecache_offset(h, vma, address); 38818382d914SDavidlohr Bueso 38823935baa9SDavid Gibson /* 38833935baa9SDavid Gibson * Serialize hugepage allocation and instantiation, so that we don't 38843935baa9SDavid Gibson * get spurious allocation failures if two CPUs race to instantiate 38853935baa9SDavid Gibson * the same page in the page cache. 38863935baa9SDavid Gibson */ 3887c672c7f2SMike Kravetz hash = hugetlb_fault_mutex_hash(h, mm, vma, mapping, idx, address); 3888c672c7f2SMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 38898382d914SDavidlohr Bueso 38907f2e9525SGerald Schaefer entry = huge_ptep_get(ptep); 38917f2e9525SGerald Schaefer if (huge_pte_none(entry)) { 38928382d914SDavidlohr Bueso ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags); 3893b4d1d99fSDavid Gibson goto out_mutex; 38943935baa9SDavid Gibson } 389586e5216fSAdam Litke 389683c54070SNick Piggin ret = 0; 38971e8f889bSDavid Gibson 389857303d80SAndy Whitcroft /* 38990f792cf9SNaoya Horiguchi * entry could be a migration/hwpoison entry at this point, so this 39000f792cf9SNaoya Horiguchi * check prevents the kernel from going below assuming that we have 39010f792cf9SNaoya Horiguchi * a active hugepage in pagecache. This goto expects the 2nd page fault, 39020f792cf9SNaoya Horiguchi * and is_hugetlb_entry_(migration|hwpoisoned) check will properly 39030f792cf9SNaoya Horiguchi * handle it. 39040f792cf9SNaoya Horiguchi */ 39050f792cf9SNaoya Horiguchi if (!pte_present(entry)) 39060f792cf9SNaoya Horiguchi goto out_mutex; 39070f792cf9SNaoya Horiguchi 39080f792cf9SNaoya Horiguchi /* 390957303d80SAndy Whitcroft * If we are going to COW the mapping later, we examine the pending 391057303d80SAndy Whitcroft * reservations for this page now. This will ensure that any 391157303d80SAndy Whitcroft * allocations necessary to record that reservation occur outside the 391257303d80SAndy Whitcroft * spinlock. For private mappings, we also lookup the pagecache 391357303d80SAndy Whitcroft * page now as it is used to determine if a reservation has been 391457303d80SAndy Whitcroft * consumed. 391557303d80SAndy Whitcroft */ 3916106c992aSGerald Schaefer if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { 39172b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 39182b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 3919b4d1d99fSDavid Gibson goto out_mutex; 39202b26736cSAndy Whitcroft } 39215e911373SMike Kravetz /* Just decrements count, does not deallocate */ 3922feba16e2SMike Kravetz vma_end_reservation(h, vma, address); 392357303d80SAndy Whitcroft 3924f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 392557303d80SAndy Whitcroft pagecache_page = hugetlbfs_pagecache_page(h, 392657303d80SAndy Whitcroft vma, address); 392757303d80SAndy Whitcroft } 392857303d80SAndy Whitcroft 39290f792cf9SNaoya Horiguchi ptl = huge_pte_lock(h, mm, ptep); 39300fe6e20bSNaoya Horiguchi 39311e8f889bSDavid Gibson /* Check for a racing update before calling hugetlb_cow */ 3932b4d1d99fSDavid Gibson if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) 3933cb900f41SKirill A. Shutemov goto out_ptl; 3934b4d1d99fSDavid Gibson 39350f792cf9SNaoya Horiguchi /* 39360f792cf9SNaoya Horiguchi * hugetlb_cow() requires page locks of pte_page(entry) and 39370f792cf9SNaoya Horiguchi * pagecache_page, so here we need take the former one 39380f792cf9SNaoya Horiguchi * when page != pagecache_page or !pagecache_page. 39390f792cf9SNaoya Horiguchi */ 39400f792cf9SNaoya Horiguchi page = pte_page(entry); 39410f792cf9SNaoya Horiguchi if (page != pagecache_page) 39420f792cf9SNaoya Horiguchi if (!trylock_page(page)) { 39430f792cf9SNaoya Horiguchi need_wait_lock = 1; 39440f792cf9SNaoya Horiguchi goto out_ptl; 39450f792cf9SNaoya Horiguchi } 39460f792cf9SNaoya Horiguchi 39470f792cf9SNaoya Horiguchi get_page(page); 3948b4d1d99fSDavid Gibson 3949788c7df4SHugh Dickins if (flags & FAULT_FLAG_WRITE) { 3950106c992aSGerald Schaefer if (!huge_pte_write(entry)) { 39513999f52eSAneesh Kumar K.V ret = hugetlb_cow(mm, vma, address, ptep, 3952cb900f41SKirill A. Shutemov pagecache_page, ptl); 39530f792cf9SNaoya Horiguchi goto out_put_page; 3954b4d1d99fSDavid Gibson } 3955106c992aSGerald Schaefer entry = huge_pte_mkdirty(entry); 3956b4d1d99fSDavid Gibson } 3957b4d1d99fSDavid Gibson entry = pte_mkyoung(entry); 3958788c7df4SHugh Dickins if (huge_ptep_set_access_flags(vma, address, ptep, entry, 3959788c7df4SHugh Dickins flags & FAULT_FLAG_WRITE)) 39604b3073e1SRussell King update_mmu_cache(vma, address, ptep); 39610f792cf9SNaoya Horiguchi out_put_page: 39620f792cf9SNaoya Horiguchi if (page != pagecache_page) 39630f792cf9SNaoya Horiguchi unlock_page(page); 39640f792cf9SNaoya Horiguchi put_page(page); 3965cb900f41SKirill A. Shutemov out_ptl: 3966cb900f41SKirill A. Shutemov spin_unlock(ptl); 396757303d80SAndy Whitcroft 396857303d80SAndy Whitcroft if (pagecache_page) { 396957303d80SAndy Whitcroft unlock_page(pagecache_page); 397057303d80SAndy Whitcroft put_page(pagecache_page); 397157303d80SAndy Whitcroft } 3972b4d1d99fSDavid Gibson out_mutex: 3973c672c7f2SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 39740f792cf9SNaoya Horiguchi /* 39750f792cf9SNaoya Horiguchi * Generally it's safe to hold refcount during waiting page lock. But 39760f792cf9SNaoya Horiguchi * here we just wait to defer the next page fault to avoid busy loop and 39770f792cf9SNaoya Horiguchi * the page is not used after unlocked before returning from the current 39780f792cf9SNaoya Horiguchi * page fault. So we are safe from accessing freed page, even if we wait 39790f792cf9SNaoya Horiguchi * here without taking refcount. 39800f792cf9SNaoya Horiguchi */ 39810f792cf9SNaoya Horiguchi if (need_wait_lock) 39820f792cf9SNaoya Horiguchi wait_on_page_locked(page); 39831e8f889bSDavid Gibson return ret; 398486e5216fSAdam Litke } 398586e5216fSAdam Litke 39868fb5debcSMike Kravetz /* 39878fb5debcSMike Kravetz * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with 39888fb5debcSMike Kravetz * modifications for huge pages. 39898fb5debcSMike Kravetz */ 39908fb5debcSMike Kravetz int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, 39918fb5debcSMike Kravetz pte_t *dst_pte, 39928fb5debcSMike Kravetz struct vm_area_struct *dst_vma, 39938fb5debcSMike Kravetz unsigned long dst_addr, 39948fb5debcSMike Kravetz unsigned long src_addr, 39958fb5debcSMike Kravetz struct page **pagep) 39968fb5debcSMike Kravetz { 39971c9e8defSMike Kravetz int vm_shared = dst_vma->vm_flags & VM_SHARED; 39988fb5debcSMike Kravetz struct hstate *h = hstate_vma(dst_vma); 39998fb5debcSMike Kravetz pte_t _dst_pte; 40008fb5debcSMike Kravetz spinlock_t *ptl; 40018fb5debcSMike Kravetz int ret; 40028fb5debcSMike Kravetz struct page *page; 40038fb5debcSMike Kravetz 40048fb5debcSMike Kravetz if (!*pagep) { 40058fb5debcSMike Kravetz ret = -ENOMEM; 40068fb5debcSMike Kravetz page = alloc_huge_page(dst_vma, dst_addr, 0); 40078fb5debcSMike Kravetz if (IS_ERR(page)) 40088fb5debcSMike Kravetz goto out; 40098fb5debcSMike Kravetz 40108fb5debcSMike Kravetz ret = copy_huge_page_from_user(page, 40118fb5debcSMike Kravetz (const void __user *) src_addr, 4012810a56b9SMike Kravetz pages_per_huge_page(h), false); 40138fb5debcSMike Kravetz 40148fb5debcSMike Kravetz /* fallback to copy_from_user outside mmap_sem */ 40158fb5debcSMike Kravetz if (unlikely(ret)) { 40168fb5debcSMike Kravetz ret = -EFAULT; 40178fb5debcSMike Kravetz *pagep = page; 40188fb5debcSMike Kravetz /* don't free the page */ 40198fb5debcSMike Kravetz goto out; 40208fb5debcSMike Kravetz } 40218fb5debcSMike Kravetz } else { 40228fb5debcSMike Kravetz page = *pagep; 40238fb5debcSMike Kravetz *pagep = NULL; 40248fb5debcSMike Kravetz } 40258fb5debcSMike Kravetz 40268fb5debcSMike Kravetz /* 40278fb5debcSMike Kravetz * The memory barrier inside __SetPageUptodate makes sure that 40288fb5debcSMike Kravetz * preceding stores to the page contents become visible before 40298fb5debcSMike Kravetz * the set_pte_at() write. 40308fb5debcSMike Kravetz */ 40318fb5debcSMike Kravetz __SetPageUptodate(page); 40328fb5debcSMike Kravetz set_page_huge_active(page); 40338fb5debcSMike Kravetz 40341c9e8defSMike Kravetz /* 40351c9e8defSMike Kravetz * If shared, add to page cache 40361c9e8defSMike Kravetz */ 40371c9e8defSMike Kravetz if (vm_shared) { 40381c9e8defSMike Kravetz struct address_space *mapping = dst_vma->vm_file->f_mapping; 40391c9e8defSMike Kravetz pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr); 40401c9e8defSMike Kravetz 40411c9e8defSMike Kravetz ret = huge_add_to_page_cache(page, mapping, idx); 40421c9e8defSMike Kravetz if (ret) 40431c9e8defSMike Kravetz goto out_release_nounlock; 40441c9e8defSMike Kravetz } 40451c9e8defSMike Kravetz 40468fb5debcSMike Kravetz ptl = huge_pte_lockptr(h, dst_mm, dst_pte); 40478fb5debcSMike Kravetz spin_lock(ptl); 40488fb5debcSMike Kravetz 40498fb5debcSMike Kravetz ret = -EEXIST; 40508fb5debcSMike Kravetz if (!huge_pte_none(huge_ptep_get(dst_pte))) 40518fb5debcSMike Kravetz goto out_release_unlock; 40528fb5debcSMike Kravetz 40531c9e8defSMike Kravetz if (vm_shared) { 40541c9e8defSMike Kravetz page_dup_rmap(page, true); 40551c9e8defSMike Kravetz } else { 40568fb5debcSMike Kravetz ClearPagePrivate(page); 40578fb5debcSMike Kravetz hugepage_add_new_anon_rmap(page, dst_vma, dst_addr); 40581c9e8defSMike Kravetz } 40598fb5debcSMike Kravetz 40608fb5debcSMike Kravetz _dst_pte = make_huge_pte(dst_vma, page, dst_vma->vm_flags & VM_WRITE); 40618fb5debcSMike Kravetz if (dst_vma->vm_flags & VM_WRITE) 40628fb5debcSMike Kravetz _dst_pte = huge_pte_mkdirty(_dst_pte); 40638fb5debcSMike Kravetz _dst_pte = pte_mkyoung(_dst_pte); 40648fb5debcSMike Kravetz 40658fb5debcSMike Kravetz set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 40668fb5debcSMike Kravetz 40678fb5debcSMike Kravetz (void)huge_ptep_set_access_flags(dst_vma, dst_addr, dst_pte, _dst_pte, 40688fb5debcSMike Kravetz dst_vma->vm_flags & VM_WRITE); 40698fb5debcSMike Kravetz hugetlb_count_add(pages_per_huge_page(h), dst_mm); 40708fb5debcSMike Kravetz 40718fb5debcSMike Kravetz /* No need to invalidate - it was non-present before */ 40728fb5debcSMike Kravetz update_mmu_cache(dst_vma, dst_addr, dst_pte); 40738fb5debcSMike Kravetz 40748fb5debcSMike Kravetz spin_unlock(ptl); 40751c9e8defSMike Kravetz if (vm_shared) 40761c9e8defSMike Kravetz unlock_page(page); 40778fb5debcSMike Kravetz ret = 0; 40788fb5debcSMike Kravetz out: 40798fb5debcSMike Kravetz return ret; 40808fb5debcSMike Kravetz out_release_unlock: 40818fb5debcSMike Kravetz spin_unlock(ptl); 40821c9e8defSMike Kravetz out_release_nounlock: 40831c9e8defSMike Kravetz if (vm_shared) 40841c9e8defSMike Kravetz unlock_page(page); 40858fb5debcSMike Kravetz put_page(page); 40868fb5debcSMike Kravetz goto out; 40878fb5debcSMike Kravetz } 40888fb5debcSMike Kravetz 408928a35716SMichel Lespinasse long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, 409063551ae0SDavid Gibson struct page **pages, struct vm_area_struct **vmas, 409128a35716SMichel Lespinasse unsigned long *position, unsigned long *nr_pages, 409287ffc118SAndrea Arcangeli long i, unsigned int flags, int *nonblocking) 409363551ae0SDavid Gibson { 4094d5d4b0aaSChen, Kenneth W unsigned long pfn_offset; 4095d5d4b0aaSChen, Kenneth W unsigned long vaddr = *position; 409628a35716SMichel Lespinasse unsigned long remainder = *nr_pages; 4097a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 409863551ae0SDavid Gibson 409963551ae0SDavid Gibson while (vaddr < vma->vm_end && remainder) { 410063551ae0SDavid Gibson pte_t *pte; 4101cb900f41SKirill A. Shutemov spinlock_t *ptl = NULL; 41022a15efc9SHugh Dickins int absent; 410363551ae0SDavid Gibson struct page *page; 410463551ae0SDavid Gibson 41054c887265SAdam Litke /* 410602057967SDavid Rientjes * If we have a pending SIGKILL, don't keep faulting pages and 410702057967SDavid Rientjes * potentially allocating memory. 410802057967SDavid Rientjes */ 410902057967SDavid Rientjes if (unlikely(fatal_signal_pending(current))) { 411002057967SDavid Rientjes remainder = 0; 411102057967SDavid Rientjes break; 411202057967SDavid Rientjes } 411302057967SDavid Rientjes 411402057967SDavid Rientjes /* 41154c887265SAdam Litke * Some archs (sparc64, sh*) have multiple pte_ts to 41162a15efc9SHugh Dickins * each hugepage. We have to make sure we get the 41174c887265SAdam Litke * first, for the page indexing below to work. 4118cb900f41SKirill A. Shutemov * 4119cb900f41SKirill A. Shutemov * Note that page table lock is not held when pte is null. 41204c887265SAdam Litke */ 4121a5516438SAndi Kleen pte = huge_pte_offset(mm, vaddr & huge_page_mask(h)); 4122cb900f41SKirill A. Shutemov if (pte) 4123cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, pte); 41242a15efc9SHugh Dickins absent = !pte || huge_pte_none(huge_ptep_get(pte)); 412563551ae0SDavid Gibson 41262a15efc9SHugh Dickins /* 41272a15efc9SHugh Dickins * When coredumping, it suits get_dump_page if we just return 41283ae77f43SHugh Dickins * an error where there's an empty slot with no huge pagecache 41293ae77f43SHugh Dickins * to back it. This way, we avoid allocating a hugepage, and 41303ae77f43SHugh Dickins * the sparse dumpfile avoids allocating disk blocks, but its 41313ae77f43SHugh Dickins * huge holes still show up with zeroes where they need to be. 41322a15efc9SHugh Dickins */ 41333ae77f43SHugh Dickins if (absent && (flags & FOLL_DUMP) && 41343ae77f43SHugh Dickins !hugetlbfs_pagecache_present(h, vma, vaddr)) { 4135cb900f41SKirill A. Shutemov if (pte) 4136cb900f41SKirill A. Shutemov spin_unlock(ptl); 41372a15efc9SHugh Dickins remainder = 0; 41382a15efc9SHugh Dickins break; 41392a15efc9SHugh Dickins } 41402a15efc9SHugh Dickins 41419cc3a5bdSNaoya Horiguchi /* 41429cc3a5bdSNaoya Horiguchi * We need call hugetlb_fault for both hugepages under migration 41439cc3a5bdSNaoya Horiguchi * (in which case hugetlb_fault waits for the migration,) and 41449cc3a5bdSNaoya Horiguchi * hwpoisoned hugepages (in which case we need to prevent the 41459cc3a5bdSNaoya Horiguchi * caller from accessing to them.) In order to do this, we use 41469cc3a5bdSNaoya Horiguchi * here is_swap_pte instead of is_hugetlb_entry_migration and 41479cc3a5bdSNaoya Horiguchi * is_hugetlb_entry_hwpoisoned. This is because it simply covers 41489cc3a5bdSNaoya Horiguchi * both cases, and because we can't follow correct pages 41499cc3a5bdSNaoya Horiguchi * directly from any kind of swap entries. 41509cc3a5bdSNaoya Horiguchi */ 41519cc3a5bdSNaoya Horiguchi if (absent || is_swap_pte(huge_ptep_get(pte)) || 4152106c992aSGerald Schaefer ((flags & FOLL_WRITE) && 4153106c992aSGerald Schaefer !huge_pte_write(huge_ptep_get(pte)))) { 41544c887265SAdam Litke int ret; 415587ffc118SAndrea Arcangeli unsigned int fault_flags = 0; 41564c887265SAdam Litke 4157cb900f41SKirill A. Shutemov if (pte) 4158cb900f41SKirill A. Shutemov spin_unlock(ptl); 415987ffc118SAndrea Arcangeli if (flags & FOLL_WRITE) 416087ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_WRITE; 416187ffc118SAndrea Arcangeli if (nonblocking) 416287ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_ALLOW_RETRY; 416387ffc118SAndrea Arcangeli if (flags & FOLL_NOWAIT) 416487ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_ALLOW_RETRY | 416587ffc118SAndrea Arcangeli FAULT_FLAG_RETRY_NOWAIT; 416687ffc118SAndrea Arcangeli if (flags & FOLL_TRIED) { 416787ffc118SAndrea Arcangeli VM_WARN_ON_ONCE(fault_flags & 416887ffc118SAndrea Arcangeli FAULT_FLAG_ALLOW_RETRY); 416987ffc118SAndrea Arcangeli fault_flags |= FAULT_FLAG_TRIED; 417087ffc118SAndrea Arcangeli } 417187ffc118SAndrea Arcangeli ret = hugetlb_fault(mm, vma, vaddr, fault_flags); 417287ffc118SAndrea Arcangeli if (ret & VM_FAULT_ERROR) { 41731c59827dSHugh Dickins remainder = 0; 41741c59827dSHugh Dickins break; 41751c59827dSHugh Dickins } 417687ffc118SAndrea Arcangeli if (ret & VM_FAULT_RETRY) { 417787ffc118SAndrea Arcangeli if (nonblocking) 417887ffc118SAndrea Arcangeli *nonblocking = 0; 417987ffc118SAndrea Arcangeli *nr_pages = 0; 418087ffc118SAndrea Arcangeli /* 418187ffc118SAndrea Arcangeli * VM_FAULT_RETRY must not return an 418287ffc118SAndrea Arcangeli * error, it will return zero 418387ffc118SAndrea Arcangeli * instead. 418487ffc118SAndrea Arcangeli * 418587ffc118SAndrea Arcangeli * No need to update "position" as the 418687ffc118SAndrea Arcangeli * caller will not check it after 418787ffc118SAndrea Arcangeli * *nr_pages is set to 0. 418887ffc118SAndrea Arcangeli */ 418987ffc118SAndrea Arcangeli return i; 419087ffc118SAndrea Arcangeli } 419187ffc118SAndrea Arcangeli continue; 419287ffc118SAndrea Arcangeli } 419363551ae0SDavid Gibson 4194a5516438SAndi Kleen pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; 41957f2e9525SGerald Schaefer page = pte_page(huge_ptep_get(pte)); 4196d5d4b0aaSChen, Kenneth W same_page: 4197d6692183SChen, Kenneth W if (pages) { 419869d177c2SAndy Whitcroft pages[i] = mem_map_offset(page, pfn_offset); 4199ddc58f27SKirill A. Shutemov get_page(pages[i]); 4200d6692183SChen, Kenneth W } 420163551ae0SDavid Gibson 420263551ae0SDavid Gibson if (vmas) 420363551ae0SDavid Gibson vmas[i] = vma; 420463551ae0SDavid Gibson 420563551ae0SDavid Gibson vaddr += PAGE_SIZE; 4206d5d4b0aaSChen, Kenneth W ++pfn_offset; 420763551ae0SDavid Gibson --remainder; 420863551ae0SDavid Gibson ++i; 4209d5d4b0aaSChen, Kenneth W if (vaddr < vma->vm_end && remainder && 4210a5516438SAndi Kleen pfn_offset < pages_per_huge_page(h)) { 4211d5d4b0aaSChen, Kenneth W /* 4212d5d4b0aaSChen, Kenneth W * We use pfn_offset to avoid touching the pageframes 4213d5d4b0aaSChen, Kenneth W * of this compound page. 4214d5d4b0aaSChen, Kenneth W */ 4215d5d4b0aaSChen, Kenneth W goto same_page; 4216d5d4b0aaSChen, Kenneth W } 4217cb900f41SKirill A. Shutemov spin_unlock(ptl); 421863551ae0SDavid Gibson } 421928a35716SMichel Lespinasse *nr_pages = remainder; 422087ffc118SAndrea Arcangeli /* 422187ffc118SAndrea Arcangeli * setting position is actually required only if remainder is 422287ffc118SAndrea Arcangeli * not zero but it's faster not to add a "if (remainder)" 422387ffc118SAndrea Arcangeli * branch. 422487ffc118SAndrea Arcangeli */ 422563551ae0SDavid Gibson *position = vaddr; 422663551ae0SDavid Gibson 42272a15efc9SHugh Dickins return i ? i : -EFAULT; 422863551ae0SDavid Gibson } 42298f860591SZhang, Yanmin 42305491ae7bSAneesh Kumar K.V #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE 42315491ae7bSAneesh Kumar K.V /* 42325491ae7bSAneesh Kumar K.V * ARCHes with special requirements for evicting HUGETLB backing TLB entries can 42335491ae7bSAneesh Kumar K.V * implement this. 42345491ae7bSAneesh Kumar K.V */ 42355491ae7bSAneesh Kumar K.V #define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end) 42365491ae7bSAneesh Kumar K.V #endif 42375491ae7bSAneesh Kumar K.V 42387da4d641SPeter Zijlstra unsigned long hugetlb_change_protection(struct vm_area_struct *vma, 42398f860591SZhang, Yanmin unsigned long address, unsigned long end, pgprot_t newprot) 42408f860591SZhang, Yanmin { 42418f860591SZhang, Yanmin struct mm_struct *mm = vma->vm_mm; 42428f860591SZhang, Yanmin unsigned long start = address; 42438f860591SZhang, Yanmin pte_t *ptep; 42448f860591SZhang, Yanmin pte_t pte; 4245a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 42467da4d641SPeter Zijlstra unsigned long pages = 0; 42478f860591SZhang, Yanmin 42488f860591SZhang, Yanmin BUG_ON(address >= end); 42498f860591SZhang, Yanmin flush_cache_range(vma, address, end); 42508f860591SZhang, Yanmin 4251a5338093SRik van Riel mmu_notifier_invalidate_range_start(mm, start, end); 425283cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 4253a5516438SAndi Kleen for (; address < end; address += huge_page_size(h)) { 4254cb900f41SKirill A. Shutemov spinlock_t *ptl; 42558f860591SZhang, Yanmin ptep = huge_pte_offset(mm, address); 42568f860591SZhang, Yanmin if (!ptep) 42578f860591SZhang, Yanmin continue; 4258cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 42597da4d641SPeter Zijlstra if (huge_pmd_unshare(mm, &address, ptep)) { 42607da4d641SPeter Zijlstra pages++; 4261cb900f41SKirill A. Shutemov spin_unlock(ptl); 426239dde65cSChen, Kenneth W continue; 42637da4d641SPeter Zijlstra } 4264a8bda28dSNaoya Horiguchi pte = huge_ptep_get(ptep); 4265a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { 4266a8bda28dSNaoya Horiguchi spin_unlock(ptl); 4267a8bda28dSNaoya Horiguchi continue; 4268a8bda28dSNaoya Horiguchi } 4269a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(pte))) { 4270a8bda28dSNaoya Horiguchi swp_entry_t entry = pte_to_swp_entry(pte); 4271a8bda28dSNaoya Horiguchi 4272a8bda28dSNaoya Horiguchi if (is_write_migration_entry(entry)) { 4273a8bda28dSNaoya Horiguchi pte_t newpte; 4274a8bda28dSNaoya Horiguchi 4275a8bda28dSNaoya Horiguchi make_migration_entry_read(&entry); 4276a8bda28dSNaoya Horiguchi newpte = swp_entry_to_pte(entry); 4277a8bda28dSNaoya Horiguchi set_huge_pte_at(mm, address, ptep, newpte); 4278a8bda28dSNaoya Horiguchi pages++; 4279a8bda28dSNaoya Horiguchi } 4280a8bda28dSNaoya Horiguchi spin_unlock(ptl); 4281a8bda28dSNaoya Horiguchi continue; 4282a8bda28dSNaoya Horiguchi } 4283a8bda28dSNaoya Horiguchi if (!huge_pte_none(pte)) { 42848f860591SZhang, Yanmin pte = huge_ptep_get_and_clear(mm, address, ptep); 4285106c992aSGerald Schaefer pte = pte_mkhuge(huge_pte_modify(pte, newprot)); 4286be7517d6STony Lu pte = arch_make_huge_pte(pte, vma, NULL, 0); 42878f860591SZhang, Yanmin set_huge_pte_at(mm, address, ptep, pte); 42887da4d641SPeter Zijlstra pages++; 42898f860591SZhang, Yanmin } 4290cb900f41SKirill A. Shutemov spin_unlock(ptl); 42918f860591SZhang, Yanmin } 4292d833352aSMel Gorman /* 4293c8c06efaSDavidlohr Bueso * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare 4294d833352aSMel Gorman * may have cleared our pud entry and done put_page on the page table: 4295c8c06efaSDavidlohr Bueso * once we release i_mmap_rwsem, another task can do the final put_page 4296d833352aSMel Gorman * and that page table be reused and filled with junk. 4297d833352aSMel Gorman */ 42985491ae7bSAneesh Kumar K.V flush_hugetlb_tlb_range(vma, start, end); 429934ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, start, end); 430083cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 4301a5338093SRik van Riel mmu_notifier_invalidate_range_end(mm, start, end); 43027da4d641SPeter Zijlstra 43037da4d641SPeter Zijlstra return pages << h->order; 43048f860591SZhang, Yanmin } 43058f860591SZhang, Yanmin 4306a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode, 4307a1e78772SMel Gorman long from, long to, 43085a6fe125SMel Gorman struct vm_area_struct *vma, 4309ca16d140SKOSAKI Motohiro vm_flags_t vm_flags) 4310e4e574b7SAdam Litke { 431117c9d12eSMel Gorman long ret, chg; 4312a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 431390481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 43149119a41eSJoonsoo Kim struct resv_map *resv_map; 43151c5ecae3SMike Kravetz long gbl_reserve; 4316e4e574b7SAdam Litke 4317a1e78772SMel Gorman /* 431817c9d12eSMel Gorman * Only apply hugepage reservation if asked. At fault time, an 431917c9d12eSMel Gorman * attempt will be made for VM_NORESERVE to allocate a page 432090481622SDavid Gibson * without using reserves 432117c9d12eSMel Gorman */ 4322ca16d140SKOSAKI Motohiro if (vm_flags & VM_NORESERVE) 432317c9d12eSMel Gorman return 0; 432417c9d12eSMel Gorman 432517c9d12eSMel Gorman /* 4326a1e78772SMel Gorman * Shared mappings base their reservation on the number of pages that 4327a1e78772SMel Gorman * are already allocated on behalf of the file. Private mappings need 4328a1e78772SMel Gorman * to reserve the full area even if read-only as mprotect() may be 4329a1e78772SMel Gorman * called to make the mapping read-write. Assume !vma is a shm mapping 4330a1e78772SMel Gorman */ 43319119a41eSJoonsoo Kim if (!vma || vma->vm_flags & VM_MAYSHARE) { 43324e35f483SJoonsoo Kim resv_map = inode_resv_map(inode); 43339119a41eSJoonsoo Kim 43341406ec9bSJoonsoo Kim chg = region_chg(resv_map, from, to); 43359119a41eSJoonsoo Kim 43369119a41eSJoonsoo Kim } else { 43379119a41eSJoonsoo Kim resv_map = resv_map_alloc(); 43385a6fe125SMel Gorman if (!resv_map) 43395a6fe125SMel Gorman return -ENOMEM; 43405a6fe125SMel Gorman 434117c9d12eSMel Gorman chg = to - from; 434217c9d12eSMel Gorman 43435a6fe125SMel Gorman set_vma_resv_map(vma, resv_map); 43445a6fe125SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_OWNER); 43455a6fe125SMel Gorman } 43465a6fe125SMel Gorman 4347c50ac050SDave Hansen if (chg < 0) { 4348c50ac050SDave Hansen ret = chg; 4349c50ac050SDave Hansen goto out_err; 4350c50ac050SDave Hansen } 435117c9d12eSMel Gorman 43521c5ecae3SMike Kravetz /* 43531c5ecae3SMike Kravetz * There must be enough pages in the subpool for the mapping. If 43541c5ecae3SMike Kravetz * the subpool has a minimum size, there may be some global 43551c5ecae3SMike Kravetz * reservations already in place (gbl_reserve). 43561c5ecae3SMike Kravetz */ 43571c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_get_pages(spool, chg); 43581c5ecae3SMike Kravetz if (gbl_reserve < 0) { 4359c50ac050SDave Hansen ret = -ENOSPC; 4360c50ac050SDave Hansen goto out_err; 4361c50ac050SDave Hansen } 436217c9d12eSMel Gorman 436317c9d12eSMel Gorman /* 436417c9d12eSMel Gorman * Check enough hugepages are available for the reservation. 436590481622SDavid Gibson * Hand the pages back to the subpool if there are not 436617c9d12eSMel Gorman */ 43671c5ecae3SMike Kravetz ret = hugetlb_acct_memory(h, gbl_reserve); 436817c9d12eSMel Gorman if (ret < 0) { 43691c5ecae3SMike Kravetz /* put back original number of pages, chg */ 43701c5ecae3SMike Kravetz (void)hugepage_subpool_put_pages(spool, chg); 4371c50ac050SDave Hansen goto out_err; 437217c9d12eSMel Gorman } 437317c9d12eSMel Gorman 437417c9d12eSMel Gorman /* 437517c9d12eSMel Gorman * Account for the reservations made. Shared mappings record regions 437617c9d12eSMel Gorman * that have reservations as they are shared by multiple VMAs. 437717c9d12eSMel Gorman * When the last VMA disappears, the region map says how much 437817c9d12eSMel Gorman * the reservation was and the page cache tells how much of 437917c9d12eSMel Gorman * the reservation was consumed. Private mappings are per-VMA and 438017c9d12eSMel Gorman * only the consumed reservations are tracked. When the VMA 438117c9d12eSMel Gorman * disappears, the original reservation is the VMA size and the 438217c9d12eSMel Gorman * consumed reservations are stored in the map. Hence, nothing 438317c9d12eSMel Gorman * else has to be done for private mappings here 438417c9d12eSMel Gorman */ 438533039678SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) { 438633039678SMike Kravetz long add = region_add(resv_map, from, to); 438733039678SMike Kravetz 438833039678SMike Kravetz if (unlikely(chg > add)) { 438933039678SMike Kravetz /* 439033039678SMike Kravetz * pages in this range were added to the reserve 439133039678SMike Kravetz * map between region_chg and region_add. This 439233039678SMike Kravetz * indicates a race with alloc_huge_page. Adjust 439333039678SMike Kravetz * the subpool and reserve counts modified above 439433039678SMike Kravetz * based on the difference. 439533039678SMike Kravetz */ 439633039678SMike Kravetz long rsv_adjust; 439733039678SMike Kravetz 439833039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 439933039678SMike Kravetz chg - add); 440033039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 440133039678SMike Kravetz } 440233039678SMike Kravetz } 4403a43a8c39SChen, Kenneth W return 0; 4404c50ac050SDave Hansen out_err: 44055e911373SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) 44065e911373SMike Kravetz region_abort(resv_map, from, to); 4407f031dd27SJoonsoo Kim if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 4408f031dd27SJoonsoo Kim kref_put(&resv_map->refs, resv_map_release); 4409c50ac050SDave Hansen return ret; 4410a43a8c39SChen, Kenneth W } 4411a43a8c39SChen, Kenneth W 4412b5cec28dSMike Kravetz long hugetlb_unreserve_pages(struct inode *inode, long start, long end, 4413b5cec28dSMike Kravetz long freed) 4414a43a8c39SChen, Kenneth W { 4415a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 44164e35f483SJoonsoo Kim struct resv_map *resv_map = inode_resv_map(inode); 44179119a41eSJoonsoo Kim long chg = 0; 441890481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 44191c5ecae3SMike Kravetz long gbl_reserve; 442045c682a6SKen Chen 4421b5cec28dSMike Kravetz if (resv_map) { 4422b5cec28dSMike Kravetz chg = region_del(resv_map, start, end); 4423b5cec28dSMike Kravetz /* 4424b5cec28dSMike Kravetz * region_del() can fail in the rare case where a region 4425b5cec28dSMike Kravetz * must be split and another region descriptor can not be 4426b5cec28dSMike Kravetz * allocated. If end == LONG_MAX, it will not fail. 4427b5cec28dSMike Kravetz */ 4428b5cec28dSMike Kravetz if (chg < 0) 4429b5cec28dSMike Kravetz return chg; 4430b5cec28dSMike Kravetz } 4431b5cec28dSMike Kravetz 443245c682a6SKen Chen spin_lock(&inode->i_lock); 4433e4c6f8beSEric Sandeen inode->i_blocks -= (blocks_per_huge_page(h) * freed); 443445c682a6SKen Chen spin_unlock(&inode->i_lock); 443545c682a6SKen Chen 44361c5ecae3SMike Kravetz /* 44371c5ecae3SMike Kravetz * If the subpool has a minimum size, the number of global 44381c5ecae3SMike Kravetz * reservations to be released may be adjusted. 44391c5ecae3SMike Kravetz */ 44401c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); 44411c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 4442b5cec28dSMike Kravetz 4443b5cec28dSMike Kravetz return 0; 4444a43a8c39SChen, Kenneth W } 444593f70f90SNaoya Horiguchi 44463212b535SSteve Capper #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE 44473212b535SSteve Capper static unsigned long page_table_shareable(struct vm_area_struct *svma, 44483212b535SSteve Capper struct vm_area_struct *vma, 44493212b535SSteve Capper unsigned long addr, pgoff_t idx) 44503212b535SSteve Capper { 44513212b535SSteve Capper unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + 44523212b535SSteve Capper svma->vm_start; 44533212b535SSteve Capper unsigned long sbase = saddr & PUD_MASK; 44543212b535SSteve Capper unsigned long s_end = sbase + PUD_SIZE; 44553212b535SSteve Capper 44563212b535SSteve Capper /* Allow segments to share if only one is marked locked */ 4457de60f5f1SEric B Munson unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK; 4458de60f5f1SEric B Munson unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK; 44593212b535SSteve Capper 44603212b535SSteve Capper /* 44613212b535SSteve Capper * match the virtual addresses, permission and the alignment of the 44623212b535SSteve Capper * page table page. 44633212b535SSteve Capper */ 44643212b535SSteve Capper if (pmd_index(addr) != pmd_index(saddr) || 44653212b535SSteve Capper vm_flags != svm_flags || 44663212b535SSteve Capper sbase < svma->vm_start || svma->vm_end < s_end) 44673212b535SSteve Capper return 0; 44683212b535SSteve Capper 44693212b535SSteve Capper return saddr; 44703212b535SSteve Capper } 44713212b535SSteve Capper 447231aafb45SNicholas Krause static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr) 44733212b535SSteve Capper { 44743212b535SSteve Capper unsigned long base = addr & PUD_MASK; 44753212b535SSteve Capper unsigned long end = base + PUD_SIZE; 44763212b535SSteve Capper 44773212b535SSteve Capper /* 44783212b535SSteve Capper * check on proper vm_flags and page table alignment 44793212b535SSteve Capper */ 44803212b535SSteve Capper if (vma->vm_flags & VM_MAYSHARE && 44813212b535SSteve Capper vma->vm_start <= base && end <= vma->vm_end) 448231aafb45SNicholas Krause return true; 448331aafb45SNicholas Krause return false; 44843212b535SSteve Capper } 44853212b535SSteve Capper 44863212b535SSteve Capper /* 44873212b535SSteve Capper * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() 44883212b535SSteve Capper * and returns the corresponding pte. While this is not necessary for the 44893212b535SSteve Capper * !shared pmd case because we can allocate the pmd later as well, it makes the 44903212b535SSteve Capper * code much cleaner. pmd allocation is essential for the shared case because 4491c8c06efaSDavidlohr Bueso * pud has to be populated inside the same i_mmap_rwsem section - otherwise 44923212b535SSteve Capper * racing tasks could either miss the sharing (see huge_pte_offset) or select a 44933212b535SSteve Capper * bad pmd for sharing. 44943212b535SSteve Capper */ 44953212b535SSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 44963212b535SSteve Capper { 44973212b535SSteve Capper struct vm_area_struct *vma = find_vma(mm, addr); 44983212b535SSteve Capper struct address_space *mapping = vma->vm_file->f_mapping; 44993212b535SSteve Capper pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + 45003212b535SSteve Capper vma->vm_pgoff; 45013212b535SSteve Capper struct vm_area_struct *svma; 45023212b535SSteve Capper unsigned long saddr; 45033212b535SSteve Capper pte_t *spte = NULL; 45043212b535SSteve Capper pte_t *pte; 4505cb900f41SKirill A. Shutemov spinlock_t *ptl; 45063212b535SSteve Capper 45073212b535SSteve Capper if (!vma_shareable(vma, addr)) 45083212b535SSteve Capper return (pte_t *)pmd_alloc(mm, pud, addr); 45093212b535SSteve Capper 451083cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 45113212b535SSteve Capper vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { 45123212b535SSteve Capper if (svma == vma) 45133212b535SSteve Capper continue; 45143212b535SSteve Capper 45153212b535SSteve Capper saddr = page_table_shareable(svma, vma, addr, idx); 45163212b535SSteve Capper if (saddr) { 45173212b535SSteve Capper spte = huge_pte_offset(svma->vm_mm, saddr); 45183212b535SSteve Capper if (spte) { 45193212b535SSteve Capper get_page(virt_to_page(spte)); 45203212b535SSteve Capper break; 45213212b535SSteve Capper } 45223212b535SSteve Capper } 45233212b535SSteve Capper } 45243212b535SSteve Capper 45253212b535SSteve Capper if (!spte) 45263212b535SSteve Capper goto out; 45273212b535SSteve Capper 45288bea8052SAneesh Kumar K.V ptl = huge_pte_lock(hstate_vma(vma), mm, spte); 4529dc6c9a35SKirill A. Shutemov if (pud_none(*pud)) { 45303212b535SSteve Capper pud_populate(mm, pud, 45313212b535SSteve Capper (pmd_t *)((unsigned long)spte & PAGE_MASK)); 4532c17b1f42SKirill A. Shutemov mm_inc_nr_pmds(mm); 4533dc6c9a35SKirill A. Shutemov } else { 45343212b535SSteve Capper put_page(virt_to_page(spte)); 4535dc6c9a35SKirill A. Shutemov } 4536cb900f41SKirill A. Shutemov spin_unlock(ptl); 45373212b535SSteve Capper out: 45383212b535SSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 453983cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 45403212b535SSteve Capper return pte; 45413212b535SSteve Capper } 45423212b535SSteve Capper 45433212b535SSteve Capper /* 45443212b535SSteve Capper * unmap huge page backed by shared pte. 45453212b535SSteve Capper * 45463212b535SSteve Capper * Hugetlb pte page is ref counted at the time of mapping. If pte is shared 45473212b535SSteve Capper * indicated by page_count > 1, unmap is achieved by clearing pud and 45483212b535SSteve Capper * decrementing the ref count. If count == 1, the pte page is not shared. 45493212b535SSteve Capper * 4550cb900f41SKirill A. Shutemov * called with page table lock held. 45513212b535SSteve Capper * 45523212b535SSteve Capper * returns: 1 successfully unmapped a shared pte page 45533212b535SSteve Capper * 0 the underlying pte page is not shared, or it is the last user 45543212b535SSteve Capper */ 45553212b535SSteve Capper int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 45563212b535SSteve Capper { 45573212b535SSteve Capper pgd_t *pgd = pgd_offset(mm, *addr); 45583212b535SSteve Capper pud_t *pud = pud_offset(pgd, *addr); 45593212b535SSteve Capper 45603212b535SSteve Capper BUG_ON(page_count(virt_to_page(ptep)) == 0); 45613212b535SSteve Capper if (page_count(virt_to_page(ptep)) == 1) 45623212b535SSteve Capper return 0; 45633212b535SSteve Capper 45643212b535SSteve Capper pud_clear(pud); 45653212b535SSteve Capper put_page(virt_to_page(ptep)); 4566dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(mm); 45673212b535SSteve Capper *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE; 45683212b535SSteve Capper return 1; 45693212b535SSteve Capper } 45709e5fc74cSSteve Capper #define want_pmd_share() (1) 45719e5fc74cSSteve Capper #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 45729e5fc74cSSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 45739e5fc74cSSteve Capper { 45749e5fc74cSSteve Capper return NULL; 45759e5fc74cSSteve Capper } 4576e81f2d22SZhang Zhen 4577e81f2d22SZhang Zhen int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 4578e81f2d22SZhang Zhen { 4579e81f2d22SZhang Zhen return 0; 4580e81f2d22SZhang Zhen } 45819e5fc74cSSteve Capper #define want_pmd_share() (0) 45823212b535SSteve Capper #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 45833212b535SSteve Capper 45849e5fc74cSSteve Capper #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 45859e5fc74cSSteve Capper pte_t *huge_pte_alloc(struct mm_struct *mm, 45869e5fc74cSSteve Capper unsigned long addr, unsigned long sz) 45879e5fc74cSSteve Capper { 45889e5fc74cSSteve Capper pgd_t *pgd; 45899e5fc74cSSteve Capper pud_t *pud; 45909e5fc74cSSteve Capper pte_t *pte = NULL; 45919e5fc74cSSteve Capper 45929e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 45939e5fc74cSSteve Capper pud = pud_alloc(mm, pgd, addr); 45949e5fc74cSSteve Capper if (pud) { 45959e5fc74cSSteve Capper if (sz == PUD_SIZE) { 45969e5fc74cSSteve Capper pte = (pte_t *)pud; 45979e5fc74cSSteve Capper } else { 45989e5fc74cSSteve Capper BUG_ON(sz != PMD_SIZE); 45999e5fc74cSSteve Capper if (want_pmd_share() && pud_none(*pud)) 46009e5fc74cSSteve Capper pte = huge_pmd_share(mm, addr, pud); 46019e5fc74cSSteve Capper else 46029e5fc74cSSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 46039e5fc74cSSteve Capper } 46049e5fc74cSSteve Capper } 46054e666314SMichal Hocko BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte)); 46069e5fc74cSSteve Capper 46079e5fc74cSSteve Capper return pte; 46089e5fc74cSSteve Capper } 46099e5fc74cSSteve Capper 46109e5fc74cSSteve Capper pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) 46119e5fc74cSSteve Capper { 46129e5fc74cSSteve Capper pgd_t *pgd; 46139e5fc74cSSteve Capper pud_t *pud; 46149e5fc74cSSteve Capper pmd_t *pmd = NULL; 46159e5fc74cSSteve Capper 46169e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 46179e5fc74cSSteve Capper if (pgd_present(*pgd)) { 46189e5fc74cSSteve Capper pud = pud_offset(pgd, addr); 46199e5fc74cSSteve Capper if (pud_present(*pud)) { 46209e5fc74cSSteve Capper if (pud_huge(*pud)) 46219e5fc74cSSteve Capper return (pte_t *)pud; 46229e5fc74cSSteve Capper pmd = pmd_offset(pud, addr); 46239e5fc74cSSteve Capper } 46249e5fc74cSSteve Capper } 46259e5fc74cSSteve Capper return (pte_t *) pmd; 46269e5fc74cSSteve Capper } 46279e5fc74cSSteve Capper 462861f77edaSNaoya Horiguchi #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 462961f77edaSNaoya Horiguchi 463061f77edaSNaoya Horiguchi /* 463161f77edaSNaoya Horiguchi * These functions are overwritable if your architecture needs its own 463261f77edaSNaoya Horiguchi * behavior. 463361f77edaSNaoya Horiguchi */ 463461f77edaSNaoya Horiguchi struct page * __weak 463561f77edaSNaoya Horiguchi follow_huge_addr(struct mm_struct *mm, unsigned long address, 463661f77edaSNaoya Horiguchi int write) 463761f77edaSNaoya Horiguchi { 463861f77edaSNaoya Horiguchi return ERR_PTR(-EINVAL); 463961f77edaSNaoya Horiguchi } 464061f77edaSNaoya Horiguchi 464161f77edaSNaoya Horiguchi struct page * __weak 46429e5fc74cSSteve Capper follow_huge_pmd(struct mm_struct *mm, unsigned long address, 4643e66f17ffSNaoya Horiguchi pmd_t *pmd, int flags) 46449e5fc74cSSteve Capper { 4645e66f17ffSNaoya Horiguchi struct page *page = NULL; 4646e66f17ffSNaoya Horiguchi spinlock_t *ptl; 4647e66f17ffSNaoya Horiguchi retry: 4648e66f17ffSNaoya Horiguchi ptl = pmd_lockptr(mm, pmd); 4649e66f17ffSNaoya Horiguchi spin_lock(ptl); 4650e66f17ffSNaoya Horiguchi /* 4651e66f17ffSNaoya Horiguchi * make sure that the address range covered by this pmd is not 4652e66f17ffSNaoya Horiguchi * unmapped from other threads. 4653e66f17ffSNaoya Horiguchi */ 4654e66f17ffSNaoya Horiguchi if (!pmd_huge(*pmd)) 4655e66f17ffSNaoya Horiguchi goto out; 4656e66f17ffSNaoya Horiguchi if (pmd_present(*pmd)) { 465797534127SGerald Schaefer page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT); 4658e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 4659e66f17ffSNaoya Horiguchi get_page(page); 4660e66f17ffSNaoya Horiguchi } else { 4661e66f17ffSNaoya Horiguchi if (is_hugetlb_entry_migration(huge_ptep_get((pte_t *)pmd))) { 4662e66f17ffSNaoya Horiguchi spin_unlock(ptl); 4663e66f17ffSNaoya Horiguchi __migration_entry_wait(mm, (pte_t *)pmd, ptl); 4664e66f17ffSNaoya Horiguchi goto retry; 4665e66f17ffSNaoya Horiguchi } 4666e66f17ffSNaoya Horiguchi /* 4667e66f17ffSNaoya Horiguchi * hwpoisoned entry is treated as no_page_table in 4668e66f17ffSNaoya Horiguchi * follow_page_mask(). 4669e66f17ffSNaoya Horiguchi */ 4670e66f17ffSNaoya Horiguchi } 4671e66f17ffSNaoya Horiguchi out: 4672e66f17ffSNaoya Horiguchi spin_unlock(ptl); 46739e5fc74cSSteve Capper return page; 46749e5fc74cSSteve Capper } 46759e5fc74cSSteve Capper 467661f77edaSNaoya Horiguchi struct page * __weak 46779e5fc74cSSteve Capper follow_huge_pud(struct mm_struct *mm, unsigned long address, 4678e66f17ffSNaoya Horiguchi pud_t *pud, int flags) 46799e5fc74cSSteve Capper { 4680e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 4681e66f17ffSNaoya Horiguchi return NULL; 46829e5fc74cSSteve Capper 4683e66f17ffSNaoya Horiguchi return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT); 46849e5fc74cSSteve Capper } 46859e5fc74cSSteve Capper 4686d5bd9106SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE 4687d5bd9106SAndi Kleen 468893f70f90SNaoya Horiguchi /* 468993f70f90SNaoya Horiguchi * This function is called from memory failure code. 469093f70f90SNaoya Horiguchi */ 46916de2b1aaSNaoya Horiguchi int dequeue_hwpoisoned_huge_page(struct page *hpage) 469293f70f90SNaoya Horiguchi { 469393f70f90SNaoya Horiguchi struct hstate *h = page_hstate(hpage); 469493f70f90SNaoya Horiguchi int nid = page_to_nid(hpage); 46956de2b1aaSNaoya Horiguchi int ret = -EBUSY; 469693f70f90SNaoya Horiguchi 469793f70f90SNaoya Horiguchi spin_lock(&hugetlb_lock); 46987e1f049eSNaoya Horiguchi /* 46997e1f049eSNaoya Horiguchi * Just checking !page_huge_active is not enough, because that could be 47007e1f049eSNaoya Horiguchi * an isolated/hwpoisoned hugepage (which have >0 refcount). 47017e1f049eSNaoya Horiguchi */ 47027e1f049eSNaoya Horiguchi if (!page_huge_active(hpage) && !page_count(hpage)) { 470356f2fb14SNaoya Horiguchi /* 470456f2fb14SNaoya Horiguchi * Hwpoisoned hugepage isn't linked to activelist or freelist, 470556f2fb14SNaoya Horiguchi * but dangling hpage->lru can trigger list-debug warnings 470656f2fb14SNaoya Horiguchi * (this happens when we call unpoison_memory() on it), 470756f2fb14SNaoya Horiguchi * so let it point to itself with list_del_init(). 470856f2fb14SNaoya Horiguchi */ 470956f2fb14SNaoya Horiguchi list_del_init(&hpage->lru); 47108c6c2ecbSNaoya Horiguchi set_page_refcounted(hpage); 471193f70f90SNaoya Horiguchi h->free_huge_pages--; 471293f70f90SNaoya Horiguchi h->free_huge_pages_node[nid]--; 47136de2b1aaSNaoya Horiguchi ret = 0; 471493f70f90SNaoya Horiguchi } 47156de2b1aaSNaoya Horiguchi spin_unlock(&hugetlb_lock); 47166de2b1aaSNaoya Horiguchi return ret; 47176de2b1aaSNaoya Horiguchi } 47186de2b1aaSNaoya Horiguchi #endif 471931caf665SNaoya Horiguchi 472031caf665SNaoya Horiguchi bool isolate_huge_page(struct page *page, struct list_head *list) 472131caf665SNaoya Horiguchi { 4722bcc54222SNaoya Horiguchi bool ret = true; 4723bcc54222SNaoya Horiguchi 4724309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 472531caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 4726bcc54222SNaoya Horiguchi if (!page_huge_active(page) || !get_page_unless_zero(page)) { 4727bcc54222SNaoya Horiguchi ret = false; 4728bcc54222SNaoya Horiguchi goto unlock; 4729bcc54222SNaoya Horiguchi } 4730bcc54222SNaoya Horiguchi clear_page_huge_active(page); 473131caf665SNaoya Horiguchi list_move_tail(&page->lru, list); 4732bcc54222SNaoya Horiguchi unlock: 473331caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 4734bcc54222SNaoya Horiguchi return ret; 473531caf665SNaoya Horiguchi } 473631caf665SNaoya Horiguchi 473731caf665SNaoya Horiguchi void putback_active_hugepage(struct page *page) 473831caf665SNaoya Horiguchi { 4739309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 474031caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 4741bcc54222SNaoya Horiguchi set_page_huge_active(page); 474231caf665SNaoya Horiguchi list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist); 474331caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 474431caf665SNaoya Horiguchi put_page(page); 474531caf665SNaoya Horiguchi } 4746