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/module.h> 81da177e4SLinus Torvalds #include <linux/mm.h> 9e1759c21SAlexey Dobriyan #include <linux/seq_file.h> 101da177e4SLinus Torvalds #include <linux/sysctl.h> 111da177e4SLinus Torvalds #include <linux/highmem.h> 12cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 131da177e4SLinus Torvalds #include <linux/nodemask.h> 1463551ae0SDavid Gibson #include <linux/pagemap.h> 155da7ca86SChristoph Lameter #include <linux/mempolicy.h> 163b32123dSGideon Israel Dsouza #include <linux/compiler.h> 17aea47ff3SChristoph Lameter #include <linux/cpuset.h> 183935baa9SDavid Gibson #include <linux/mutex.h> 19aa888a74SAndi Kleen #include <linux/bootmem.h> 20a3437870SNishanth Aravamudan #include <linux/sysfs.h> 215a0e3ad6STejun Heo #include <linux/slab.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> 367835e98bSNick Piggin #include "internal.h" 371da177e4SLinus Torvalds 38753162cdSAndrey Ryabinin int hugepages_treat_as_movable; 39a5516438SAndi Kleen 40c3f38a38SAneesh Kumar K.V int hugetlb_max_hstate __read_mostly; 41e5ff2159SAndi Kleen unsigned int default_hstate_idx; 42e5ff2159SAndi Kleen struct hstate hstates[HUGE_MAX_HSTATE]; 43641844f5SNaoya Horiguchi /* 44641844f5SNaoya Horiguchi * Minimum page order among possible hugepage sizes, set to a proper value 45641844f5SNaoya Horiguchi * at boot time. 46641844f5SNaoya Horiguchi */ 47641844f5SNaoya Horiguchi static unsigned int minimum_order __read_mostly = UINT_MAX; 48e5ff2159SAndi Kleen 4953ba51d2SJon Tollefson __initdata LIST_HEAD(huge_boot_pages); 5053ba51d2SJon Tollefson 51e5ff2159SAndi Kleen /* for command line parsing */ 52e5ff2159SAndi Kleen static struct hstate * __initdata parsed_hstate; 53e5ff2159SAndi Kleen static unsigned long __initdata default_hstate_max_huge_pages; 54e11bfbfcSNick Piggin static unsigned long __initdata default_hstate_size; 55e5ff2159SAndi Kleen 563935baa9SDavid Gibson /* 5731caf665SNaoya Horiguchi * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages, 5831caf665SNaoya Horiguchi * free_huge_pages, and surplus_huge_pages. 593935baa9SDavid Gibson */ 60c3f38a38SAneesh Kumar K.V DEFINE_SPINLOCK(hugetlb_lock); 610bd0f9fbSEric Paris 628382d914SDavidlohr Bueso /* 638382d914SDavidlohr Bueso * Serializes faults on the same logical page. This is used to 648382d914SDavidlohr Bueso * prevent spurious OOMs when the hugepage pool is fully utilized. 658382d914SDavidlohr Bueso */ 668382d914SDavidlohr Bueso static int num_fault_mutexes; 678382d914SDavidlohr Bueso static struct mutex *htlb_fault_mutex_table ____cacheline_aligned_in_smp; 688382d914SDavidlohr Bueso 697ca02d0aSMike Kravetz /* Forward declaration */ 707ca02d0aSMike Kravetz static int hugetlb_acct_memory(struct hstate *h, long delta); 717ca02d0aSMike Kravetz 7290481622SDavid Gibson static inline void unlock_or_release_subpool(struct hugepage_subpool *spool) 7390481622SDavid Gibson { 7490481622SDavid Gibson bool free = (spool->count == 0) && (spool->used_hpages == 0); 7590481622SDavid Gibson 7690481622SDavid Gibson spin_unlock(&spool->lock); 7790481622SDavid Gibson 7890481622SDavid Gibson /* If no pages are used, and no other handles to the subpool 797ca02d0aSMike Kravetz * remain, give up any reservations mased on minimum size and 807ca02d0aSMike Kravetz * free the subpool */ 817ca02d0aSMike Kravetz if (free) { 827ca02d0aSMike Kravetz if (spool->min_hpages != -1) 837ca02d0aSMike Kravetz hugetlb_acct_memory(spool->hstate, 847ca02d0aSMike Kravetz -spool->min_hpages); 8590481622SDavid Gibson kfree(spool); 8690481622SDavid Gibson } 877ca02d0aSMike Kravetz } 8890481622SDavid Gibson 897ca02d0aSMike Kravetz struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages, 907ca02d0aSMike Kravetz long min_hpages) 9190481622SDavid Gibson { 9290481622SDavid Gibson struct hugepage_subpool *spool; 9390481622SDavid Gibson 94c6a91820SMike Kravetz spool = kzalloc(sizeof(*spool), GFP_KERNEL); 9590481622SDavid Gibson if (!spool) 9690481622SDavid Gibson return NULL; 9790481622SDavid Gibson 9890481622SDavid Gibson spin_lock_init(&spool->lock); 9990481622SDavid Gibson spool->count = 1; 1007ca02d0aSMike Kravetz spool->max_hpages = max_hpages; 1017ca02d0aSMike Kravetz spool->hstate = h; 1027ca02d0aSMike Kravetz spool->min_hpages = min_hpages; 1037ca02d0aSMike Kravetz 1047ca02d0aSMike Kravetz if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) { 1057ca02d0aSMike Kravetz kfree(spool); 1067ca02d0aSMike Kravetz return NULL; 1077ca02d0aSMike Kravetz } 1087ca02d0aSMike Kravetz spool->rsv_hpages = min_hpages; 10990481622SDavid Gibson 11090481622SDavid Gibson return spool; 11190481622SDavid Gibson } 11290481622SDavid Gibson 11390481622SDavid Gibson void hugepage_put_subpool(struct hugepage_subpool *spool) 11490481622SDavid Gibson { 11590481622SDavid Gibson spin_lock(&spool->lock); 11690481622SDavid Gibson BUG_ON(!spool->count); 11790481622SDavid Gibson spool->count--; 11890481622SDavid Gibson unlock_or_release_subpool(spool); 11990481622SDavid Gibson } 12090481622SDavid Gibson 1211c5ecae3SMike Kravetz /* 1221c5ecae3SMike Kravetz * Subpool accounting for allocating and reserving pages. 1231c5ecae3SMike Kravetz * Return -ENOMEM if there are not enough resources to satisfy the 1241c5ecae3SMike Kravetz * the request. Otherwise, return the number of pages by which the 1251c5ecae3SMike Kravetz * global pools must be adjusted (upward). The returned value may 1261c5ecae3SMike Kravetz * only be different than the passed value (delta) in the case where 1271c5ecae3SMike Kravetz * a subpool minimum size must be manitained. 1281c5ecae3SMike Kravetz */ 1291c5ecae3SMike Kravetz static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, 13090481622SDavid Gibson long delta) 13190481622SDavid Gibson { 1321c5ecae3SMike Kravetz long ret = delta; 13390481622SDavid Gibson 13490481622SDavid Gibson if (!spool) 1351c5ecae3SMike Kravetz return ret; 13690481622SDavid Gibson 13790481622SDavid Gibson spin_lock(&spool->lock); 13890481622SDavid Gibson 1391c5ecae3SMike Kravetz if (spool->max_hpages != -1) { /* maximum size accounting */ 1401c5ecae3SMike Kravetz if ((spool->used_hpages + delta) <= spool->max_hpages) 1411c5ecae3SMike Kravetz spool->used_hpages += delta; 1421c5ecae3SMike Kravetz else { 1431c5ecae3SMike Kravetz ret = -ENOMEM; 1441c5ecae3SMike Kravetz goto unlock_ret; 1451c5ecae3SMike Kravetz } 1461c5ecae3SMike Kravetz } 1471c5ecae3SMike Kravetz 1481c5ecae3SMike Kravetz if (spool->min_hpages != -1) { /* minimum size accounting */ 1491c5ecae3SMike Kravetz if (delta > spool->rsv_hpages) { 1501c5ecae3SMike Kravetz /* 1511c5ecae3SMike Kravetz * Asking for more reserves than those already taken on 1521c5ecae3SMike Kravetz * behalf of subpool. Return difference. 1531c5ecae3SMike Kravetz */ 1541c5ecae3SMike Kravetz ret = delta - spool->rsv_hpages; 1551c5ecae3SMike Kravetz spool->rsv_hpages = 0; 1561c5ecae3SMike Kravetz } else { 1571c5ecae3SMike Kravetz ret = 0; /* reserves already accounted for */ 1581c5ecae3SMike Kravetz spool->rsv_hpages -= delta; 1591c5ecae3SMike Kravetz } 1601c5ecae3SMike Kravetz } 1611c5ecae3SMike Kravetz 1621c5ecae3SMike Kravetz unlock_ret: 1631c5ecae3SMike Kravetz spin_unlock(&spool->lock); 16490481622SDavid Gibson return ret; 16590481622SDavid Gibson } 16690481622SDavid Gibson 1671c5ecae3SMike Kravetz /* 1681c5ecae3SMike Kravetz * Subpool accounting for freeing and unreserving pages. 1691c5ecae3SMike Kravetz * Return the number of global page reservations that must be dropped. 1701c5ecae3SMike Kravetz * The return value may only be different than the passed value (delta) 1711c5ecae3SMike Kravetz * in the case where a subpool minimum size must be maintained. 1721c5ecae3SMike Kravetz */ 1731c5ecae3SMike Kravetz static long hugepage_subpool_put_pages(struct hugepage_subpool *spool, 17490481622SDavid Gibson long delta) 17590481622SDavid Gibson { 1761c5ecae3SMike Kravetz long ret = delta; 1771c5ecae3SMike Kravetz 17890481622SDavid Gibson if (!spool) 1791c5ecae3SMike Kravetz return delta; 18090481622SDavid Gibson 18190481622SDavid Gibson spin_lock(&spool->lock); 1821c5ecae3SMike Kravetz 1831c5ecae3SMike Kravetz if (spool->max_hpages != -1) /* maximum size accounting */ 18490481622SDavid Gibson spool->used_hpages -= delta; 1851c5ecae3SMike Kravetz 1861c5ecae3SMike Kravetz if (spool->min_hpages != -1) { /* minimum size accounting */ 1871c5ecae3SMike Kravetz if (spool->rsv_hpages + delta <= spool->min_hpages) 1881c5ecae3SMike Kravetz ret = 0; 1891c5ecae3SMike Kravetz else 1901c5ecae3SMike Kravetz ret = spool->rsv_hpages + delta - spool->min_hpages; 1911c5ecae3SMike Kravetz 1921c5ecae3SMike Kravetz spool->rsv_hpages += delta; 1931c5ecae3SMike Kravetz if (spool->rsv_hpages > spool->min_hpages) 1941c5ecae3SMike Kravetz spool->rsv_hpages = spool->min_hpages; 1951c5ecae3SMike Kravetz } 1961c5ecae3SMike Kravetz 1971c5ecae3SMike Kravetz /* 1981c5ecae3SMike Kravetz * If hugetlbfs_put_super couldn't free spool due to an outstanding 1991c5ecae3SMike Kravetz * quota reference, free it now. 2001c5ecae3SMike Kravetz */ 20190481622SDavid Gibson unlock_or_release_subpool(spool); 2021c5ecae3SMike Kravetz 2031c5ecae3SMike Kravetz return ret; 20490481622SDavid Gibson } 20590481622SDavid Gibson 20690481622SDavid Gibson static inline struct hugepage_subpool *subpool_inode(struct inode *inode) 20790481622SDavid Gibson { 20890481622SDavid Gibson return HUGETLBFS_SB(inode->i_sb)->spool; 20990481622SDavid Gibson } 21090481622SDavid Gibson 21190481622SDavid Gibson static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma) 21290481622SDavid Gibson { 213496ad9aaSAl Viro return subpool_inode(file_inode(vma->vm_file)); 21490481622SDavid Gibson } 21590481622SDavid Gibson 216e7c4b0bfSAndy Whitcroft /* 21796822904SAndy Whitcroft * Region tracking -- allows tracking of reservations and instantiated pages 21896822904SAndy Whitcroft * across the pages in a mapping. 21984afd99bSAndy Whitcroft * 2201dd308a7SMike Kravetz * The region data structures are embedded into a resv_map and protected 2211dd308a7SMike Kravetz * by a resv_map's lock. The set of regions within the resv_map represent 2221dd308a7SMike Kravetz * reservations for huge pages, or huge pages that have already been 2231dd308a7SMike Kravetz * instantiated within the map. The from and to elements are huge page 2241dd308a7SMike Kravetz * indicies into the associated mapping. from indicates the starting index 2251dd308a7SMike Kravetz * of the region. to represents the first index past the end of the region. 2261dd308a7SMike Kravetz * 2271dd308a7SMike Kravetz * For example, a file region structure with from == 0 and to == 4 represents 2281dd308a7SMike Kravetz * four huge pages in a mapping. It is important to note that the to element 2291dd308a7SMike Kravetz * represents the first element past the end of the region. This is used in 2301dd308a7SMike Kravetz * arithmetic as 4(to) - 0(from) = 4 huge pages in the region. 2311dd308a7SMike Kravetz * 2321dd308a7SMike Kravetz * Interval notation of the form [from, to) will be used to indicate that 2331dd308a7SMike Kravetz * the endpoint from is inclusive and to is exclusive. 23496822904SAndy Whitcroft */ 23596822904SAndy Whitcroft struct file_region { 23696822904SAndy Whitcroft struct list_head link; 23796822904SAndy Whitcroft long from; 23896822904SAndy Whitcroft long to; 23996822904SAndy Whitcroft }; 24096822904SAndy Whitcroft 2411dd308a7SMike Kravetz /* 2421dd308a7SMike Kravetz * Add the huge page range represented by [f, t) to the reserve 2431dd308a7SMike Kravetz * map. Existing regions will be expanded to accommodate the 2441dd308a7SMike Kravetz * specified range. We know only existing regions need to be 2451dd308a7SMike Kravetz * expanded, because region_add is only called after region_chg 2461dd308a7SMike Kravetz * with the same range. If a new file_region structure must 2471dd308a7SMike Kravetz * be allocated, it is done in region_chg. 248cf3ad20bSMike Kravetz * 249cf3ad20bSMike Kravetz * Return the number of new huge pages added to the map. This 250cf3ad20bSMike Kravetz * number is greater than or equal to zero. 2511dd308a7SMike Kravetz */ 2521406ec9bSJoonsoo Kim static long region_add(struct resv_map *resv, long f, long t) 25396822904SAndy Whitcroft { 2541406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 25596822904SAndy Whitcroft struct file_region *rg, *nrg, *trg; 256cf3ad20bSMike Kravetz long add = 0; 25796822904SAndy Whitcroft 2587b24d861SDavidlohr Bueso spin_lock(&resv->lock); 25996822904SAndy Whitcroft /* Locate the region we are either in or before. */ 26096822904SAndy Whitcroft list_for_each_entry(rg, head, link) 26196822904SAndy Whitcroft if (f <= rg->to) 26296822904SAndy Whitcroft break; 26396822904SAndy Whitcroft 26496822904SAndy Whitcroft /* Round our left edge to the current segment if it encloses us. */ 26596822904SAndy Whitcroft if (f > rg->from) 26696822904SAndy Whitcroft f = rg->from; 26796822904SAndy Whitcroft 26896822904SAndy Whitcroft /* Check for and consume any regions we now overlap with. */ 26996822904SAndy Whitcroft nrg = rg; 27096822904SAndy Whitcroft list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 27196822904SAndy Whitcroft if (&rg->link == head) 27296822904SAndy Whitcroft break; 27396822904SAndy Whitcroft if (rg->from > t) 27496822904SAndy Whitcroft break; 27596822904SAndy Whitcroft 27696822904SAndy Whitcroft /* If this area reaches higher then extend our area to 27796822904SAndy Whitcroft * include it completely. If this is not the first area 27896822904SAndy Whitcroft * which we intend to reuse, free it. */ 27996822904SAndy Whitcroft if (rg->to > t) 28096822904SAndy Whitcroft t = rg->to; 28196822904SAndy Whitcroft if (rg != nrg) { 282cf3ad20bSMike Kravetz /* Decrement return value by the deleted range. 283cf3ad20bSMike Kravetz * Another range will span this area so that by 284cf3ad20bSMike Kravetz * end of routine add will be >= zero 285cf3ad20bSMike Kravetz */ 286cf3ad20bSMike Kravetz add -= (rg->to - rg->from); 28796822904SAndy Whitcroft list_del(&rg->link); 28896822904SAndy Whitcroft kfree(rg); 28996822904SAndy Whitcroft } 29096822904SAndy Whitcroft } 291cf3ad20bSMike Kravetz 292cf3ad20bSMike Kravetz add += (nrg->from - f); /* Added to beginning of region */ 29396822904SAndy Whitcroft nrg->from = f; 294cf3ad20bSMike Kravetz add += t - nrg->to; /* Added to end of region */ 29596822904SAndy Whitcroft nrg->to = t; 296cf3ad20bSMike Kravetz 2977b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 298cf3ad20bSMike Kravetz VM_BUG_ON(add < 0); 299cf3ad20bSMike Kravetz return add; 30096822904SAndy Whitcroft } 30196822904SAndy Whitcroft 3021dd308a7SMike Kravetz /* 3031dd308a7SMike Kravetz * Examine the existing reserve map and determine how many 3041dd308a7SMike Kravetz * huge pages in the specified range [f, t) are NOT currently 3051dd308a7SMike Kravetz * represented. This routine is called before a subsequent 3061dd308a7SMike Kravetz * call to region_add that will actually modify the reserve 3071dd308a7SMike Kravetz * map to add the specified range [f, t). region_chg does 3081dd308a7SMike Kravetz * not change the number of huge pages represented by the 3091dd308a7SMike Kravetz * map. However, if the existing regions in the map can not 3101dd308a7SMike Kravetz * be expanded to represent the new range, a new file_region 3111dd308a7SMike Kravetz * structure is added to the map as a placeholder. This is 3121dd308a7SMike Kravetz * so that the subsequent region_add call will have all the 3131dd308a7SMike Kravetz * regions it needs and will not fail. 3141dd308a7SMike Kravetz * 3151dd308a7SMike Kravetz * Returns the number of huge pages that need to be added 3161dd308a7SMike Kravetz * to the existing reservation map for the range [f, t). 3171dd308a7SMike Kravetz * This number is greater or equal to zero. -ENOMEM is 3181dd308a7SMike Kravetz * returned if a new file_region structure is needed and can 3191dd308a7SMike Kravetz * not be allocated. 3201dd308a7SMike Kravetz */ 3211406ec9bSJoonsoo Kim static long region_chg(struct resv_map *resv, long f, long t) 32296822904SAndy Whitcroft { 3231406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 3247b24d861SDavidlohr Bueso struct file_region *rg, *nrg = NULL; 32596822904SAndy Whitcroft long chg = 0; 32696822904SAndy Whitcroft 3277b24d861SDavidlohr Bueso retry: 3287b24d861SDavidlohr Bueso spin_lock(&resv->lock); 32996822904SAndy Whitcroft /* Locate the region we are before or in. */ 33096822904SAndy Whitcroft list_for_each_entry(rg, head, link) 33196822904SAndy Whitcroft if (f <= rg->to) 33296822904SAndy Whitcroft break; 33396822904SAndy Whitcroft 33496822904SAndy Whitcroft /* If we are below the current region then a new region is required. 33596822904SAndy Whitcroft * Subtle, allocate a new region at the position but make it zero 33696822904SAndy Whitcroft * size such that we can guarantee to record the reservation. */ 33796822904SAndy Whitcroft if (&rg->link == head || t < rg->from) { 3387b24d861SDavidlohr Bueso if (!nrg) { 3397b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 34096822904SAndy Whitcroft nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 34196822904SAndy Whitcroft if (!nrg) 34296822904SAndy Whitcroft return -ENOMEM; 3437b24d861SDavidlohr Bueso 34496822904SAndy Whitcroft nrg->from = f; 34596822904SAndy Whitcroft nrg->to = f; 34696822904SAndy Whitcroft INIT_LIST_HEAD(&nrg->link); 3477b24d861SDavidlohr Bueso goto retry; 3487b24d861SDavidlohr Bueso } 34996822904SAndy Whitcroft 3507b24d861SDavidlohr Bueso list_add(&nrg->link, rg->link.prev); 3517b24d861SDavidlohr Bueso chg = t - f; 3527b24d861SDavidlohr Bueso goto out_nrg; 35396822904SAndy Whitcroft } 35496822904SAndy Whitcroft 35596822904SAndy Whitcroft /* Round our left edge to the current segment if it encloses us. */ 35696822904SAndy Whitcroft if (f > rg->from) 35796822904SAndy Whitcroft f = rg->from; 35896822904SAndy Whitcroft chg = t - f; 35996822904SAndy Whitcroft 36096822904SAndy Whitcroft /* Check for and consume any regions we now overlap with. */ 36196822904SAndy Whitcroft list_for_each_entry(rg, rg->link.prev, link) { 36296822904SAndy Whitcroft if (&rg->link == head) 36396822904SAndy Whitcroft break; 36496822904SAndy Whitcroft if (rg->from > t) 3657b24d861SDavidlohr Bueso goto out; 36696822904SAndy Whitcroft 36725985edcSLucas De Marchi /* We overlap with this area, if it extends further than 36896822904SAndy Whitcroft * us then we must extend ourselves. Account for its 36996822904SAndy Whitcroft * existing reservation. */ 37096822904SAndy Whitcroft if (rg->to > t) { 37196822904SAndy Whitcroft chg += rg->to - t; 37296822904SAndy Whitcroft t = rg->to; 37396822904SAndy Whitcroft } 37496822904SAndy Whitcroft chg -= rg->to - rg->from; 37596822904SAndy Whitcroft } 3767b24d861SDavidlohr Bueso 3777b24d861SDavidlohr Bueso out: 3787b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 3797b24d861SDavidlohr Bueso /* We already know we raced and no longer need the new region */ 3807b24d861SDavidlohr Bueso kfree(nrg); 3817b24d861SDavidlohr Bueso return chg; 3827b24d861SDavidlohr Bueso out_nrg: 3837b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 38496822904SAndy Whitcroft return chg; 38596822904SAndy Whitcroft } 38696822904SAndy Whitcroft 3871dd308a7SMike Kravetz /* 3881dd308a7SMike Kravetz * Truncate the reserve map at index 'end'. Modify/truncate any 3891dd308a7SMike Kravetz * region which contains end. Delete any regions past end. 3901dd308a7SMike Kravetz * Return the number of huge pages removed from the map. 3911dd308a7SMike Kravetz */ 3921406ec9bSJoonsoo Kim static long region_truncate(struct resv_map *resv, long end) 39396822904SAndy Whitcroft { 3941406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 39596822904SAndy Whitcroft struct file_region *rg, *trg; 39696822904SAndy Whitcroft long chg = 0; 39796822904SAndy Whitcroft 3987b24d861SDavidlohr Bueso spin_lock(&resv->lock); 39996822904SAndy Whitcroft /* Locate the region we are either in or before. */ 40096822904SAndy Whitcroft list_for_each_entry(rg, head, link) 40196822904SAndy Whitcroft if (end <= rg->to) 40296822904SAndy Whitcroft break; 40396822904SAndy Whitcroft if (&rg->link == head) 4047b24d861SDavidlohr Bueso goto out; 40596822904SAndy Whitcroft 40696822904SAndy Whitcroft /* If we are in the middle of a region then adjust it. */ 40796822904SAndy Whitcroft if (end > rg->from) { 40896822904SAndy Whitcroft chg = rg->to - end; 40996822904SAndy Whitcroft rg->to = end; 41096822904SAndy Whitcroft rg = list_entry(rg->link.next, typeof(*rg), link); 41196822904SAndy Whitcroft } 41296822904SAndy Whitcroft 41396822904SAndy Whitcroft /* Drop any remaining regions. */ 41496822904SAndy Whitcroft list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 41596822904SAndy Whitcroft if (&rg->link == head) 41696822904SAndy Whitcroft break; 41796822904SAndy Whitcroft chg += rg->to - rg->from; 41896822904SAndy Whitcroft list_del(&rg->link); 41996822904SAndy Whitcroft kfree(rg); 42096822904SAndy Whitcroft } 4217b24d861SDavidlohr Bueso 4227b24d861SDavidlohr Bueso out: 4237b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 42496822904SAndy Whitcroft return chg; 42596822904SAndy Whitcroft } 42696822904SAndy Whitcroft 4271dd308a7SMike Kravetz /* 4281dd308a7SMike Kravetz * Count and return the number of huge pages in the reserve map 4291dd308a7SMike Kravetz * that intersect with the range [f, t). 4301dd308a7SMike Kravetz */ 4311406ec9bSJoonsoo Kim static long region_count(struct resv_map *resv, long f, long t) 43284afd99bSAndy Whitcroft { 4331406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 43484afd99bSAndy Whitcroft struct file_region *rg; 43584afd99bSAndy Whitcroft long chg = 0; 43684afd99bSAndy Whitcroft 4377b24d861SDavidlohr Bueso spin_lock(&resv->lock); 43884afd99bSAndy Whitcroft /* Locate each segment we overlap with, and count that overlap. */ 43984afd99bSAndy Whitcroft list_for_each_entry(rg, head, link) { 440f2135a4aSWang Sheng-Hui long seg_from; 441f2135a4aSWang Sheng-Hui long seg_to; 44284afd99bSAndy Whitcroft 44384afd99bSAndy Whitcroft if (rg->to <= f) 44484afd99bSAndy Whitcroft continue; 44584afd99bSAndy Whitcroft if (rg->from >= t) 44684afd99bSAndy Whitcroft break; 44784afd99bSAndy Whitcroft 44884afd99bSAndy Whitcroft seg_from = max(rg->from, f); 44984afd99bSAndy Whitcroft seg_to = min(rg->to, t); 45084afd99bSAndy Whitcroft 45184afd99bSAndy Whitcroft chg += seg_to - seg_from; 45284afd99bSAndy Whitcroft } 4537b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 45484afd99bSAndy Whitcroft 45584afd99bSAndy Whitcroft return chg; 45684afd99bSAndy Whitcroft } 45784afd99bSAndy Whitcroft 45896822904SAndy Whitcroft /* 459e7c4b0bfSAndy Whitcroft * Convert the address within this vma to the page offset within 460e7c4b0bfSAndy Whitcroft * the mapping, in pagecache page units; huge pages here. 461e7c4b0bfSAndy Whitcroft */ 462a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h, 463a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 464e7c4b0bfSAndy Whitcroft { 465a5516438SAndi Kleen return ((address - vma->vm_start) >> huge_page_shift(h)) + 466a5516438SAndi Kleen (vma->vm_pgoff >> huge_page_order(h)); 467e7c4b0bfSAndy Whitcroft } 468e7c4b0bfSAndy Whitcroft 4690fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma, 4700fe6e20bSNaoya Horiguchi unsigned long address) 4710fe6e20bSNaoya Horiguchi { 4720fe6e20bSNaoya Horiguchi return vma_hugecache_offset(hstate_vma(vma), vma, address); 4730fe6e20bSNaoya Horiguchi } 4740fe6e20bSNaoya Horiguchi 47584afd99bSAndy Whitcroft /* 47608fba699SMel Gorman * Return the size of the pages allocated when backing a VMA. In the majority 47708fba699SMel Gorman * cases this will be same size as used by the page table entries. 47808fba699SMel Gorman */ 47908fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) 48008fba699SMel Gorman { 48108fba699SMel Gorman struct hstate *hstate; 48208fba699SMel Gorman 48308fba699SMel Gorman if (!is_vm_hugetlb_page(vma)) 48408fba699SMel Gorman return PAGE_SIZE; 48508fba699SMel Gorman 48608fba699SMel Gorman hstate = hstate_vma(vma); 48708fba699SMel Gorman 4882415cf12SWanpeng Li return 1UL << huge_page_shift(hstate); 48908fba699SMel Gorman } 490f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize); 49108fba699SMel Gorman 49208fba699SMel Gorman /* 4933340289dSMel Gorman * Return the page size being used by the MMU to back a VMA. In the majority 4943340289dSMel Gorman * of cases, the page size used by the kernel matches the MMU size. On 4953340289dSMel Gorman * architectures where it differs, an architecture-specific version of this 4963340289dSMel Gorman * function is required. 4973340289dSMel Gorman */ 4983340289dSMel Gorman #ifndef vma_mmu_pagesize 4993340289dSMel Gorman unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) 5003340289dSMel Gorman { 5013340289dSMel Gorman return vma_kernel_pagesize(vma); 5023340289dSMel Gorman } 5033340289dSMel Gorman #endif 5043340289dSMel Gorman 5053340289dSMel Gorman /* 50684afd99bSAndy Whitcroft * Flags for MAP_PRIVATE reservations. These are stored in the bottom 50784afd99bSAndy Whitcroft * bits of the reservation map pointer, which are always clear due to 50884afd99bSAndy Whitcroft * alignment. 50984afd99bSAndy Whitcroft */ 51084afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER (1UL << 0) 51184afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1) 51204f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) 51384afd99bSAndy Whitcroft 514a1e78772SMel Gorman /* 515a1e78772SMel Gorman * These helpers are used to track how many pages are reserved for 516a1e78772SMel Gorman * faults in a MAP_PRIVATE mapping. Only the process that called mmap() 517a1e78772SMel Gorman * is guaranteed to have their future faults succeed. 518a1e78772SMel Gorman * 519a1e78772SMel Gorman * With the exception of reset_vma_resv_huge_pages() which is called at fork(), 520a1e78772SMel Gorman * the reserve counters are updated with the hugetlb_lock held. It is safe 521a1e78772SMel Gorman * to reset the VMA at fork() time as it is not in use yet and there is no 522a1e78772SMel Gorman * chance of the global counters getting corrupted as a result of the values. 52384afd99bSAndy Whitcroft * 52484afd99bSAndy Whitcroft * The private mapping reservation is represented in a subtly different 52584afd99bSAndy Whitcroft * manner to a shared mapping. A shared mapping has a region map associated 52684afd99bSAndy Whitcroft * with the underlying file, this region map represents the backing file 52784afd99bSAndy Whitcroft * pages which have ever had a reservation assigned which this persists even 52884afd99bSAndy Whitcroft * after the page is instantiated. A private mapping has a region map 52984afd99bSAndy Whitcroft * associated with the original mmap which is attached to all VMAs which 53084afd99bSAndy Whitcroft * reference it, this region map represents those offsets which have consumed 53184afd99bSAndy Whitcroft * reservation ie. where pages have been instantiated. 532a1e78772SMel Gorman */ 533e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma) 534e7c4b0bfSAndy Whitcroft { 535e7c4b0bfSAndy Whitcroft return (unsigned long)vma->vm_private_data; 536e7c4b0bfSAndy Whitcroft } 537e7c4b0bfSAndy Whitcroft 538e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma, 539e7c4b0bfSAndy Whitcroft unsigned long value) 540e7c4b0bfSAndy Whitcroft { 541e7c4b0bfSAndy Whitcroft vma->vm_private_data = (void *)value; 542e7c4b0bfSAndy Whitcroft } 543e7c4b0bfSAndy Whitcroft 5449119a41eSJoonsoo Kim struct resv_map *resv_map_alloc(void) 54584afd99bSAndy Whitcroft { 54684afd99bSAndy Whitcroft struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); 54784afd99bSAndy Whitcroft if (!resv_map) 54884afd99bSAndy Whitcroft return NULL; 54984afd99bSAndy Whitcroft 55084afd99bSAndy Whitcroft kref_init(&resv_map->refs); 5517b24d861SDavidlohr Bueso spin_lock_init(&resv_map->lock); 55284afd99bSAndy Whitcroft INIT_LIST_HEAD(&resv_map->regions); 55384afd99bSAndy Whitcroft 55484afd99bSAndy Whitcroft return resv_map; 55584afd99bSAndy Whitcroft } 55684afd99bSAndy Whitcroft 5579119a41eSJoonsoo Kim void resv_map_release(struct kref *ref) 55884afd99bSAndy Whitcroft { 55984afd99bSAndy Whitcroft struct resv_map *resv_map = container_of(ref, struct resv_map, refs); 56084afd99bSAndy Whitcroft 56184afd99bSAndy Whitcroft /* Clear out any active regions before we release the map. */ 5621406ec9bSJoonsoo Kim region_truncate(resv_map, 0); 56384afd99bSAndy Whitcroft kfree(resv_map); 56484afd99bSAndy Whitcroft } 56584afd99bSAndy Whitcroft 5664e35f483SJoonsoo Kim static inline struct resv_map *inode_resv_map(struct inode *inode) 5674e35f483SJoonsoo Kim { 5684e35f483SJoonsoo Kim return inode->i_mapping->private_data; 5694e35f483SJoonsoo Kim } 5704e35f483SJoonsoo Kim 57184afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma) 572a1e78772SMel Gorman { 57381d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 5744e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) { 5754e35f483SJoonsoo Kim struct address_space *mapping = vma->vm_file->f_mapping; 5764e35f483SJoonsoo Kim struct inode *inode = mapping->host; 5774e35f483SJoonsoo Kim 5784e35f483SJoonsoo Kim return inode_resv_map(inode); 5794e35f483SJoonsoo Kim 5804e35f483SJoonsoo Kim } else { 58184afd99bSAndy Whitcroft return (struct resv_map *)(get_vma_private_data(vma) & 58284afd99bSAndy Whitcroft ~HPAGE_RESV_MASK); 5834e35f483SJoonsoo Kim } 584a1e78772SMel Gorman } 585a1e78772SMel Gorman 58684afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) 587a1e78772SMel Gorman { 58881d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 58981d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 590a1e78772SMel Gorman 59184afd99bSAndy Whitcroft set_vma_private_data(vma, (get_vma_private_data(vma) & 59284afd99bSAndy Whitcroft HPAGE_RESV_MASK) | (unsigned long)map); 59304f2cbe3SMel Gorman } 59404f2cbe3SMel Gorman 59504f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) 59604f2cbe3SMel Gorman { 59781d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 59881d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 599e7c4b0bfSAndy Whitcroft 600e7c4b0bfSAndy Whitcroft set_vma_private_data(vma, get_vma_private_data(vma) | flags); 60104f2cbe3SMel Gorman } 60204f2cbe3SMel Gorman 60304f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) 60404f2cbe3SMel Gorman { 60581d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 606e7c4b0bfSAndy Whitcroft 607e7c4b0bfSAndy Whitcroft return (get_vma_private_data(vma) & flag) != 0; 608a1e78772SMel Gorman } 609a1e78772SMel Gorman 61004f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ 611a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma) 612a1e78772SMel Gorman { 61381d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 614f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 615a1e78772SMel Gorman vma->vm_private_data = (void *)0; 616a1e78772SMel Gorman } 617a1e78772SMel Gorman 618a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */ 619af0ed73eSJoonsoo Kim static int vma_has_reserves(struct vm_area_struct *vma, long chg) 620a1e78772SMel Gorman { 621af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_NORESERVE) { 622af0ed73eSJoonsoo Kim /* 623af0ed73eSJoonsoo Kim * This address is already reserved by other process(chg == 0), 624af0ed73eSJoonsoo Kim * so, we should decrement reserved count. Without decrementing, 625af0ed73eSJoonsoo Kim * reserve count remains after releasing inode, because this 626af0ed73eSJoonsoo Kim * allocated page will go into page cache and is regarded as 627af0ed73eSJoonsoo Kim * coming from reserved pool in releasing step. Currently, we 628af0ed73eSJoonsoo Kim * don't have any other solution to deal with this situation 629af0ed73eSJoonsoo Kim * properly, so add work-around here. 630af0ed73eSJoonsoo Kim */ 631af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE && chg == 0) 632af0ed73eSJoonsoo Kim return 1; 633af0ed73eSJoonsoo Kim else 63472231b03SJoonsoo Kim return 0; 635af0ed73eSJoonsoo Kim } 636a63884e9SJoonsoo Kim 637a63884e9SJoonsoo Kim /* Shared mappings always use reserves */ 638f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) 639a1e78772SMel Gorman return 1; 640a63884e9SJoonsoo Kim 641a63884e9SJoonsoo Kim /* 642a63884e9SJoonsoo Kim * Only the process that called mmap() has reserves for 643a63884e9SJoonsoo Kim * private mappings. 644a63884e9SJoonsoo Kim */ 6457f09ca51SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 6467f09ca51SMel Gorman return 1; 647a63884e9SJoonsoo Kim 6487f09ca51SMel Gorman return 0; 649a1e78772SMel Gorman } 650a1e78772SMel Gorman 651a5516438SAndi Kleen static void enqueue_huge_page(struct hstate *h, struct page *page) 6521da177e4SLinus Torvalds { 6531da177e4SLinus Torvalds int nid = page_to_nid(page); 6540edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_freelists[nid]); 655a5516438SAndi Kleen h->free_huge_pages++; 656a5516438SAndi Kleen h->free_huge_pages_node[nid]++; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds 659bf50bab2SNaoya Horiguchi static struct page *dequeue_huge_page_node(struct hstate *h, int nid) 660bf50bab2SNaoya Horiguchi { 661bf50bab2SNaoya Horiguchi struct page *page; 662bf50bab2SNaoya Horiguchi 663c8721bbbSNaoya Horiguchi list_for_each_entry(page, &h->hugepage_freelists[nid], lru) 664c8721bbbSNaoya Horiguchi if (!is_migrate_isolate_page(page)) 665c8721bbbSNaoya Horiguchi break; 666c8721bbbSNaoya Horiguchi /* 667c8721bbbSNaoya Horiguchi * if 'non-isolated free hugepage' not found on the list, 668c8721bbbSNaoya Horiguchi * the allocation fails. 669c8721bbbSNaoya Horiguchi */ 670c8721bbbSNaoya Horiguchi if (&h->hugepage_freelists[nid] == &page->lru) 671bf50bab2SNaoya Horiguchi return NULL; 6720edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 673a9869b83SNaoya Horiguchi set_page_refcounted(page); 674bf50bab2SNaoya Horiguchi h->free_huge_pages--; 675bf50bab2SNaoya Horiguchi h->free_huge_pages_node[nid]--; 676bf50bab2SNaoya Horiguchi return page; 677bf50bab2SNaoya Horiguchi } 678bf50bab2SNaoya Horiguchi 67986cdb465SNaoya Horiguchi /* Movability of hugepages depends on migration support. */ 68086cdb465SNaoya Horiguchi static inline gfp_t htlb_alloc_mask(struct hstate *h) 68186cdb465SNaoya Horiguchi { 682100873d7SNaoya Horiguchi if (hugepages_treat_as_movable || hugepage_migration_supported(h)) 68386cdb465SNaoya Horiguchi return GFP_HIGHUSER_MOVABLE; 68486cdb465SNaoya Horiguchi else 68586cdb465SNaoya Horiguchi return GFP_HIGHUSER; 68686cdb465SNaoya Horiguchi } 68786cdb465SNaoya Horiguchi 688a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h, 689a5516438SAndi Kleen struct vm_area_struct *vma, 690af0ed73eSJoonsoo Kim unsigned long address, int avoid_reserve, 691af0ed73eSJoonsoo Kim long chg) 6921da177e4SLinus Torvalds { 693b1c12cbcSKonstantin Khlebnikov struct page *page = NULL; 694480eccf9SLee Schermerhorn struct mempolicy *mpol; 69519770b32SMel Gorman nodemask_t *nodemask; 696c0ff7453SMiao Xie struct zonelist *zonelist; 697dd1a239fSMel Gorman struct zone *zone; 698dd1a239fSMel Gorman struct zoneref *z; 699cc9a6c87SMel Gorman unsigned int cpuset_mems_cookie; 7001da177e4SLinus Torvalds 701a1e78772SMel Gorman /* 702a1e78772SMel Gorman * A child process with MAP_PRIVATE mappings created by their parent 703a1e78772SMel Gorman * have no page reserves. This check ensures that reservations are 704a1e78772SMel Gorman * not "stolen". The child may still get SIGKILLed 705a1e78772SMel Gorman */ 706af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg) && 707a5516438SAndi Kleen h->free_huge_pages - h->resv_huge_pages == 0) 708c0ff7453SMiao Xie goto err; 709a1e78772SMel Gorman 71004f2cbe3SMel Gorman /* If reserves cannot be used, ensure enough pages are in the pool */ 711a5516438SAndi Kleen if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0) 7126eab04a8SJustin P. Mattock goto err; 71304f2cbe3SMel Gorman 7149966c4bbSJoonsoo Kim retry_cpuset: 715d26914d1SMel Gorman cpuset_mems_cookie = read_mems_allowed_begin(); 7169966c4bbSJoonsoo Kim zonelist = huge_zonelist(vma, address, 71786cdb465SNaoya Horiguchi htlb_alloc_mask(h), &mpol, &nodemask); 7189966c4bbSJoonsoo Kim 71919770b32SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 72019770b32SMel Gorman MAX_NR_ZONES - 1, nodemask) { 721344736f2SVladimir Davydov if (cpuset_zone_allowed(zone, htlb_alloc_mask(h))) { 722bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, zone_to_nid(zone)); 723bf50bab2SNaoya Horiguchi if (page) { 724af0ed73eSJoonsoo Kim if (avoid_reserve) 725af0ed73eSJoonsoo Kim break; 726af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg)) 727af0ed73eSJoonsoo Kim break; 728af0ed73eSJoonsoo Kim 72907443a85SJoonsoo Kim SetPagePrivate(page); 730a63884e9SJoonsoo Kim h->resv_huge_pages--; 7315ab3ee7bSKen Chen break; 7321da177e4SLinus Torvalds } 7333abf7afdSAndrew Morton } 734bf50bab2SNaoya Horiguchi } 735cc9a6c87SMel Gorman 736cc9a6c87SMel Gorman mpol_cond_put(mpol); 737d26914d1SMel Gorman if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie))) 738cc9a6c87SMel Gorman goto retry_cpuset; 739cc9a6c87SMel Gorman return page; 740cc9a6c87SMel Gorman 741c0ff7453SMiao Xie err: 742cc9a6c87SMel Gorman return NULL; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7451cac6f2cSLuiz Capitulino /* 7461cac6f2cSLuiz Capitulino * common helper functions for hstate_next_node_to_{alloc|free}. 7471cac6f2cSLuiz Capitulino * We may have allocated or freed a huge page based on a different 7481cac6f2cSLuiz Capitulino * nodes_allowed previously, so h->next_node_to_{alloc|free} might 7491cac6f2cSLuiz Capitulino * be outside of *nodes_allowed. Ensure that we use an allowed 7501cac6f2cSLuiz Capitulino * node for alloc or free. 7511cac6f2cSLuiz Capitulino */ 7521cac6f2cSLuiz Capitulino static int next_node_allowed(int nid, nodemask_t *nodes_allowed) 7531cac6f2cSLuiz Capitulino { 7541cac6f2cSLuiz Capitulino nid = next_node(nid, *nodes_allowed); 7551cac6f2cSLuiz Capitulino if (nid == MAX_NUMNODES) 7561cac6f2cSLuiz Capitulino nid = first_node(*nodes_allowed); 7571cac6f2cSLuiz Capitulino VM_BUG_ON(nid >= MAX_NUMNODES); 7581cac6f2cSLuiz Capitulino 7591cac6f2cSLuiz Capitulino return nid; 7601cac6f2cSLuiz Capitulino } 7611cac6f2cSLuiz Capitulino 7621cac6f2cSLuiz Capitulino static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) 7631cac6f2cSLuiz Capitulino { 7641cac6f2cSLuiz Capitulino if (!node_isset(nid, *nodes_allowed)) 7651cac6f2cSLuiz Capitulino nid = next_node_allowed(nid, nodes_allowed); 7661cac6f2cSLuiz Capitulino return nid; 7671cac6f2cSLuiz Capitulino } 7681cac6f2cSLuiz Capitulino 7691cac6f2cSLuiz Capitulino /* 7701cac6f2cSLuiz Capitulino * returns the previously saved node ["this node"] from which to 7711cac6f2cSLuiz Capitulino * allocate a persistent huge page for the pool and advance the 7721cac6f2cSLuiz Capitulino * next node from which to allocate, handling wrap at end of node 7731cac6f2cSLuiz Capitulino * mask. 7741cac6f2cSLuiz Capitulino */ 7751cac6f2cSLuiz Capitulino static int hstate_next_node_to_alloc(struct hstate *h, 7761cac6f2cSLuiz Capitulino nodemask_t *nodes_allowed) 7771cac6f2cSLuiz Capitulino { 7781cac6f2cSLuiz Capitulino int nid; 7791cac6f2cSLuiz Capitulino 7801cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 7811cac6f2cSLuiz Capitulino 7821cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); 7831cac6f2cSLuiz Capitulino h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); 7841cac6f2cSLuiz Capitulino 7851cac6f2cSLuiz Capitulino return nid; 7861cac6f2cSLuiz Capitulino } 7871cac6f2cSLuiz Capitulino 7881cac6f2cSLuiz Capitulino /* 7891cac6f2cSLuiz Capitulino * helper for free_pool_huge_page() - return the previously saved 7901cac6f2cSLuiz Capitulino * node ["this node"] from which to free a huge page. Advance the 7911cac6f2cSLuiz Capitulino * next node id whether or not we find a free huge page to free so 7921cac6f2cSLuiz Capitulino * that the next attempt to free addresses the next node. 7931cac6f2cSLuiz Capitulino */ 7941cac6f2cSLuiz Capitulino static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) 7951cac6f2cSLuiz Capitulino { 7961cac6f2cSLuiz Capitulino int nid; 7971cac6f2cSLuiz Capitulino 7981cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 7991cac6f2cSLuiz Capitulino 8001cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); 8011cac6f2cSLuiz Capitulino h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); 8021cac6f2cSLuiz Capitulino 8031cac6f2cSLuiz Capitulino return nid; 8041cac6f2cSLuiz Capitulino } 8051cac6f2cSLuiz Capitulino 8061cac6f2cSLuiz Capitulino #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \ 8071cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 8081cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 8091cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ 8101cac6f2cSLuiz Capitulino nr_nodes--) 8111cac6f2cSLuiz Capitulino 8121cac6f2cSLuiz Capitulino #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ 8131cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 8141cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 8151cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_free(hs, mask)) || 1); \ 8161cac6f2cSLuiz Capitulino nr_nodes--) 8171cac6f2cSLuiz Capitulino 818944d9fecSLuiz Capitulino #if defined(CONFIG_CMA) && defined(CONFIG_X86_64) 819944d9fecSLuiz Capitulino static void destroy_compound_gigantic_page(struct page *page, 820944d9fecSLuiz Capitulino unsigned long order) 821944d9fecSLuiz Capitulino { 822944d9fecSLuiz Capitulino int i; 823944d9fecSLuiz Capitulino int nr_pages = 1 << order; 824944d9fecSLuiz Capitulino struct page *p = page + 1; 825944d9fecSLuiz Capitulino 826944d9fecSLuiz Capitulino for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 827944d9fecSLuiz Capitulino __ClearPageTail(p); 828944d9fecSLuiz Capitulino set_page_refcounted(p); 829944d9fecSLuiz Capitulino p->first_page = NULL; 830944d9fecSLuiz Capitulino } 831944d9fecSLuiz Capitulino 832944d9fecSLuiz Capitulino set_compound_order(page, 0); 833944d9fecSLuiz Capitulino __ClearPageHead(page); 834944d9fecSLuiz Capitulino } 835944d9fecSLuiz Capitulino 836944d9fecSLuiz Capitulino static void free_gigantic_page(struct page *page, unsigned order) 837944d9fecSLuiz Capitulino { 838944d9fecSLuiz Capitulino free_contig_range(page_to_pfn(page), 1 << order); 839944d9fecSLuiz Capitulino } 840944d9fecSLuiz Capitulino 841944d9fecSLuiz Capitulino static int __alloc_gigantic_page(unsigned long start_pfn, 842944d9fecSLuiz Capitulino unsigned long nr_pages) 843944d9fecSLuiz Capitulino { 844944d9fecSLuiz Capitulino unsigned long end_pfn = start_pfn + nr_pages; 845944d9fecSLuiz Capitulino return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 846944d9fecSLuiz Capitulino } 847944d9fecSLuiz Capitulino 848944d9fecSLuiz Capitulino static bool pfn_range_valid_gigantic(unsigned long start_pfn, 849944d9fecSLuiz Capitulino unsigned long nr_pages) 850944d9fecSLuiz Capitulino { 851944d9fecSLuiz Capitulino unsigned long i, end_pfn = start_pfn + nr_pages; 852944d9fecSLuiz Capitulino struct page *page; 853944d9fecSLuiz Capitulino 854944d9fecSLuiz Capitulino for (i = start_pfn; i < end_pfn; i++) { 855944d9fecSLuiz Capitulino if (!pfn_valid(i)) 856944d9fecSLuiz Capitulino return false; 857944d9fecSLuiz Capitulino 858944d9fecSLuiz Capitulino page = pfn_to_page(i); 859944d9fecSLuiz Capitulino 860944d9fecSLuiz Capitulino if (PageReserved(page)) 861944d9fecSLuiz Capitulino return false; 862944d9fecSLuiz Capitulino 863944d9fecSLuiz Capitulino if (page_count(page) > 0) 864944d9fecSLuiz Capitulino return false; 865944d9fecSLuiz Capitulino 866944d9fecSLuiz Capitulino if (PageHuge(page)) 867944d9fecSLuiz Capitulino return false; 868944d9fecSLuiz Capitulino } 869944d9fecSLuiz Capitulino 870944d9fecSLuiz Capitulino return true; 871944d9fecSLuiz Capitulino } 872944d9fecSLuiz Capitulino 873944d9fecSLuiz Capitulino static bool zone_spans_last_pfn(const struct zone *zone, 874944d9fecSLuiz Capitulino unsigned long start_pfn, unsigned long nr_pages) 875944d9fecSLuiz Capitulino { 876944d9fecSLuiz Capitulino unsigned long last_pfn = start_pfn + nr_pages - 1; 877944d9fecSLuiz Capitulino return zone_spans_pfn(zone, last_pfn); 878944d9fecSLuiz Capitulino } 879944d9fecSLuiz Capitulino 880944d9fecSLuiz Capitulino static struct page *alloc_gigantic_page(int nid, unsigned order) 881944d9fecSLuiz Capitulino { 882944d9fecSLuiz Capitulino unsigned long nr_pages = 1 << order; 883944d9fecSLuiz Capitulino unsigned long ret, pfn, flags; 884944d9fecSLuiz Capitulino struct zone *z; 885944d9fecSLuiz Capitulino 886944d9fecSLuiz Capitulino z = NODE_DATA(nid)->node_zones; 887944d9fecSLuiz Capitulino for (; z - NODE_DATA(nid)->node_zones < MAX_NR_ZONES; z++) { 888944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 889944d9fecSLuiz Capitulino 890944d9fecSLuiz Capitulino pfn = ALIGN(z->zone_start_pfn, nr_pages); 891944d9fecSLuiz Capitulino while (zone_spans_last_pfn(z, pfn, nr_pages)) { 892944d9fecSLuiz Capitulino if (pfn_range_valid_gigantic(pfn, nr_pages)) { 893944d9fecSLuiz Capitulino /* 894944d9fecSLuiz Capitulino * We release the zone lock here because 895944d9fecSLuiz Capitulino * alloc_contig_range() will also lock the zone 896944d9fecSLuiz Capitulino * at some point. If there's an allocation 897944d9fecSLuiz Capitulino * spinning on this lock, it may win the race 898944d9fecSLuiz Capitulino * and cause alloc_contig_range() to fail... 899944d9fecSLuiz Capitulino */ 900944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 901944d9fecSLuiz Capitulino ret = __alloc_gigantic_page(pfn, nr_pages); 902944d9fecSLuiz Capitulino if (!ret) 903944d9fecSLuiz Capitulino return pfn_to_page(pfn); 904944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 905944d9fecSLuiz Capitulino } 906944d9fecSLuiz Capitulino pfn += nr_pages; 907944d9fecSLuiz Capitulino } 908944d9fecSLuiz Capitulino 909944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 910944d9fecSLuiz Capitulino } 911944d9fecSLuiz Capitulino 912944d9fecSLuiz Capitulino return NULL; 913944d9fecSLuiz Capitulino } 914944d9fecSLuiz Capitulino 915944d9fecSLuiz Capitulino static void prep_new_huge_page(struct hstate *h, struct page *page, int nid); 916944d9fecSLuiz Capitulino static void prep_compound_gigantic_page(struct page *page, unsigned long order); 917944d9fecSLuiz Capitulino 918944d9fecSLuiz Capitulino static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid) 919944d9fecSLuiz Capitulino { 920944d9fecSLuiz Capitulino struct page *page; 921944d9fecSLuiz Capitulino 922944d9fecSLuiz Capitulino page = alloc_gigantic_page(nid, huge_page_order(h)); 923944d9fecSLuiz Capitulino if (page) { 924944d9fecSLuiz Capitulino prep_compound_gigantic_page(page, huge_page_order(h)); 925944d9fecSLuiz Capitulino prep_new_huge_page(h, page, nid); 926944d9fecSLuiz Capitulino } 927944d9fecSLuiz Capitulino 928944d9fecSLuiz Capitulino return page; 929944d9fecSLuiz Capitulino } 930944d9fecSLuiz Capitulino 931944d9fecSLuiz Capitulino static int alloc_fresh_gigantic_page(struct hstate *h, 932944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) 933944d9fecSLuiz Capitulino { 934944d9fecSLuiz Capitulino struct page *page = NULL; 935944d9fecSLuiz Capitulino int nr_nodes, node; 936944d9fecSLuiz Capitulino 937944d9fecSLuiz Capitulino for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 938944d9fecSLuiz Capitulino page = alloc_fresh_gigantic_page_node(h, node); 939944d9fecSLuiz Capitulino if (page) 940944d9fecSLuiz Capitulino return 1; 941944d9fecSLuiz Capitulino } 942944d9fecSLuiz Capitulino 943944d9fecSLuiz Capitulino return 0; 944944d9fecSLuiz Capitulino } 945944d9fecSLuiz Capitulino 946944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return true; } 947944d9fecSLuiz Capitulino #else 948944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return false; } 949944d9fecSLuiz Capitulino static inline void free_gigantic_page(struct page *page, unsigned order) { } 950944d9fecSLuiz Capitulino static inline void destroy_compound_gigantic_page(struct page *page, 951944d9fecSLuiz Capitulino unsigned long order) { } 952944d9fecSLuiz Capitulino static inline int alloc_fresh_gigantic_page(struct hstate *h, 953944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) { return 0; } 954944d9fecSLuiz Capitulino #endif 955944d9fecSLuiz Capitulino 956a5516438SAndi Kleen static void update_and_free_page(struct hstate *h, struct page *page) 9576af2acb6SAdam Litke { 9586af2acb6SAdam Litke int i; 959a5516438SAndi Kleen 960944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 961944d9fecSLuiz Capitulino return; 96218229df5SAndy Whitcroft 963a5516438SAndi Kleen h->nr_huge_pages--; 964a5516438SAndi Kleen h->nr_huge_pages_node[page_to_nid(page)]--; 965a5516438SAndi Kleen for (i = 0; i < pages_per_huge_page(h); i++) { 96632f84528SChris Forbes page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 96732f84528SChris Forbes 1 << PG_referenced | 1 << PG_dirty | 968a7407a27SLuiz Capitulino 1 << PG_active | 1 << PG_private | 969a7407a27SLuiz Capitulino 1 << PG_writeback); 9706af2acb6SAdam Litke } 971309381feSSasha Levin VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page); 9726af2acb6SAdam Litke set_compound_page_dtor(page, NULL); 9736af2acb6SAdam Litke set_page_refcounted(page); 974944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) { 975944d9fecSLuiz Capitulino destroy_compound_gigantic_page(page, huge_page_order(h)); 976944d9fecSLuiz Capitulino free_gigantic_page(page, huge_page_order(h)); 977944d9fecSLuiz Capitulino } else { 9787f2e9525SGerald Schaefer arch_release_hugepage(page); 979a5516438SAndi Kleen __free_pages(page, huge_page_order(h)); 9806af2acb6SAdam Litke } 981944d9fecSLuiz Capitulino } 9826af2acb6SAdam Litke 983e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size) 984e5ff2159SAndi Kleen { 985e5ff2159SAndi Kleen struct hstate *h; 986e5ff2159SAndi Kleen 987e5ff2159SAndi Kleen for_each_hstate(h) { 988e5ff2159SAndi Kleen if (huge_page_size(h) == size) 989e5ff2159SAndi Kleen return h; 990e5ff2159SAndi Kleen } 991e5ff2159SAndi Kleen return NULL; 992e5ff2159SAndi Kleen } 993e5ff2159SAndi Kleen 994bcc54222SNaoya Horiguchi /* 995bcc54222SNaoya Horiguchi * Test to determine whether the hugepage is "active/in-use" (i.e. being linked 996bcc54222SNaoya Horiguchi * to hstate->hugepage_activelist.) 997bcc54222SNaoya Horiguchi * 998bcc54222SNaoya Horiguchi * This function can be called for tail pages, but never returns true for them. 999bcc54222SNaoya Horiguchi */ 1000bcc54222SNaoya Horiguchi bool page_huge_active(struct page *page) 1001bcc54222SNaoya Horiguchi { 1002bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHuge(page), page); 1003bcc54222SNaoya Horiguchi return PageHead(page) && PagePrivate(&page[1]); 1004bcc54222SNaoya Horiguchi } 1005bcc54222SNaoya Horiguchi 1006bcc54222SNaoya Horiguchi /* never called for tail page */ 1007bcc54222SNaoya Horiguchi static void set_page_huge_active(struct page *page) 1008bcc54222SNaoya Horiguchi { 1009bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 1010bcc54222SNaoya Horiguchi SetPagePrivate(&page[1]); 1011bcc54222SNaoya Horiguchi } 1012bcc54222SNaoya Horiguchi 1013bcc54222SNaoya Horiguchi static void clear_page_huge_active(struct page *page) 1014bcc54222SNaoya Horiguchi { 1015bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 1016bcc54222SNaoya Horiguchi ClearPagePrivate(&page[1]); 1017bcc54222SNaoya Horiguchi } 1018bcc54222SNaoya Horiguchi 10198f1d26d0SAtsushi Kumagai void free_huge_page(struct page *page) 102027a85ef1SDavid Gibson { 1021a5516438SAndi Kleen /* 1022a5516438SAndi Kleen * Can't pass hstate in here because it is called from the 1023a5516438SAndi Kleen * compound page destructor. 1024a5516438SAndi Kleen */ 1025e5ff2159SAndi Kleen struct hstate *h = page_hstate(page); 10267893d1d5SAdam Litke int nid = page_to_nid(page); 102790481622SDavid Gibson struct hugepage_subpool *spool = 102890481622SDavid Gibson (struct hugepage_subpool *)page_private(page); 102907443a85SJoonsoo Kim bool restore_reserve; 103027a85ef1SDavid Gibson 1031e5df70abSAndy Whitcroft set_page_private(page, 0); 103223be7468SMel Gorman page->mapping = NULL; 10337893d1d5SAdam Litke BUG_ON(page_count(page)); 10340fe6e20bSNaoya Horiguchi BUG_ON(page_mapcount(page)); 103507443a85SJoonsoo Kim restore_reserve = PagePrivate(page); 103616c794b4SJoonsoo Kim ClearPagePrivate(page); 103727a85ef1SDavid Gibson 10381c5ecae3SMike Kravetz /* 10391c5ecae3SMike Kravetz * A return code of zero implies that the subpool will be under its 10401c5ecae3SMike Kravetz * minimum size if the reservation is not restored after page is free. 10411c5ecae3SMike Kravetz * Therefore, force restore_reserve operation. 10421c5ecae3SMike Kravetz */ 10431c5ecae3SMike Kravetz if (hugepage_subpool_put_pages(spool, 1) == 0) 10441c5ecae3SMike Kravetz restore_reserve = true; 10451c5ecae3SMike Kravetz 104627a85ef1SDavid Gibson spin_lock(&hugetlb_lock); 1047bcc54222SNaoya Horiguchi clear_page_huge_active(page); 10486d76dcf4SAneesh Kumar K.V hugetlb_cgroup_uncharge_page(hstate_index(h), 10496d76dcf4SAneesh Kumar K.V pages_per_huge_page(h), page); 105007443a85SJoonsoo Kim if (restore_reserve) 105107443a85SJoonsoo Kim h->resv_huge_pages++; 105207443a85SJoonsoo Kim 1053944d9fecSLuiz Capitulino if (h->surplus_huge_pages_node[nid]) { 10540edaecfaSAneesh Kumar K.V /* remove the page from active list */ 10550edaecfaSAneesh Kumar K.V list_del(&page->lru); 1056a5516438SAndi Kleen update_and_free_page(h, page); 1057a5516438SAndi Kleen h->surplus_huge_pages--; 1058a5516438SAndi Kleen h->surplus_huge_pages_node[nid]--; 10597893d1d5SAdam Litke } else { 10605d3a551cSWill Deacon arch_clear_hugepage_flags(page); 1061a5516438SAndi Kleen enqueue_huge_page(h, page); 10627893d1d5SAdam Litke } 106327a85ef1SDavid Gibson spin_unlock(&hugetlb_lock); 106427a85ef1SDavid Gibson } 106527a85ef1SDavid Gibson 1066a5516438SAndi Kleen static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) 1067b7ba30c6SAndi Kleen { 10680edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1069b7ba30c6SAndi Kleen set_compound_page_dtor(page, free_huge_page); 1070b7ba30c6SAndi Kleen spin_lock(&hugetlb_lock); 10719dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1072a5516438SAndi Kleen h->nr_huge_pages++; 1073a5516438SAndi Kleen h->nr_huge_pages_node[nid]++; 1074b7ba30c6SAndi Kleen spin_unlock(&hugetlb_lock); 1075b7ba30c6SAndi Kleen put_page(page); /* free it into the hugepage allocator */ 1076b7ba30c6SAndi Kleen } 1077b7ba30c6SAndi Kleen 10782906dd52SLuiz Capitulino static void prep_compound_gigantic_page(struct page *page, unsigned long order) 107920a0307cSWu Fengguang { 108020a0307cSWu Fengguang int i; 108120a0307cSWu Fengguang int nr_pages = 1 << order; 108220a0307cSWu Fengguang struct page *p = page + 1; 108320a0307cSWu Fengguang 108420a0307cSWu Fengguang /* we rely on prep_new_huge_page to set the destructor */ 108520a0307cSWu Fengguang set_compound_order(page, order); 108620a0307cSWu Fengguang __SetPageHead(page); 1087ef5a22beSAndrea Arcangeli __ClearPageReserved(page); 108820a0307cSWu Fengguang for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 1089ef5a22beSAndrea Arcangeli /* 1090ef5a22beSAndrea Arcangeli * For gigantic hugepages allocated through bootmem at 1091ef5a22beSAndrea Arcangeli * boot, it's safer to be consistent with the not-gigantic 1092ef5a22beSAndrea Arcangeli * hugepages and clear the PG_reserved bit from all tail pages 1093ef5a22beSAndrea Arcangeli * too. Otherwse drivers using get_user_pages() to access tail 1094ef5a22beSAndrea Arcangeli * pages may get the reference counting wrong if they see 1095ef5a22beSAndrea Arcangeli * PG_reserved set on a tail page (despite the head page not 1096ef5a22beSAndrea Arcangeli * having PG_reserved set). Enforcing this consistency between 1097ef5a22beSAndrea Arcangeli * head and tail pages allows drivers to optimize away a check 1098ef5a22beSAndrea Arcangeli * on the head page when they need know if put_page() is needed 1099ef5a22beSAndrea Arcangeli * after get_user_pages(). 1100ef5a22beSAndrea Arcangeli */ 1101ef5a22beSAndrea Arcangeli __ClearPageReserved(p); 110258a84aa9SYouquan Song set_page_count(p, 0); 110320a0307cSWu Fengguang p->first_page = page; 110444fc8057SDavid Rientjes /* Make sure p->first_page is always valid for PageTail() */ 110544fc8057SDavid Rientjes smp_wmb(); 110644fc8057SDavid Rientjes __SetPageTail(p); 110720a0307cSWu Fengguang } 110820a0307cSWu Fengguang } 110920a0307cSWu Fengguang 11107795912cSAndrew Morton /* 11117795912cSAndrew Morton * PageHuge() only returns true for hugetlbfs pages, but not for normal or 11127795912cSAndrew Morton * transparent huge pages. See the PageTransHuge() documentation for more 11137795912cSAndrew Morton * details. 11147795912cSAndrew Morton */ 111520a0307cSWu Fengguang int PageHuge(struct page *page) 111620a0307cSWu Fengguang { 111720a0307cSWu Fengguang if (!PageCompound(page)) 111820a0307cSWu Fengguang return 0; 111920a0307cSWu Fengguang 112020a0307cSWu Fengguang page = compound_head(page); 1121758f66a2SAndrew Morton return get_compound_page_dtor(page) == free_huge_page; 112220a0307cSWu Fengguang } 112343131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge); 112443131e14SNaoya Horiguchi 112527c73ae7SAndrea Arcangeli /* 112627c73ae7SAndrea Arcangeli * PageHeadHuge() only returns true for hugetlbfs head page, but not for 112727c73ae7SAndrea Arcangeli * normal or transparent huge pages. 112827c73ae7SAndrea Arcangeli */ 112927c73ae7SAndrea Arcangeli int PageHeadHuge(struct page *page_head) 113027c73ae7SAndrea Arcangeli { 113127c73ae7SAndrea Arcangeli if (!PageHead(page_head)) 113227c73ae7SAndrea Arcangeli return 0; 113327c73ae7SAndrea Arcangeli 1134758f66a2SAndrew Morton return get_compound_page_dtor(page_head) == free_huge_page; 113527c73ae7SAndrea Arcangeli } 113627c73ae7SAndrea Arcangeli 113713d60f4bSZhang Yi pgoff_t __basepage_index(struct page *page) 113813d60f4bSZhang Yi { 113913d60f4bSZhang Yi struct page *page_head = compound_head(page); 114013d60f4bSZhang Yi pgoff_t index = page_index(page_head); 114113d60f4bSZhang Yi unsigned long compound_idx; 114213d60f4bSZhang Yi 114313d60f4bSZhang Yi if (!PageHuge(page_head)) 114413d60f4bSZhang Yi return page_index(page); 114513d60f4bSZhang Yi 114613d60f4bSZhang Yi if (compound_order(page_head) >= MAX_ORDER) 114713d60f4bSZhang Yi compound_idx = page_to_pfn(page) - page_to_pfn(page_head); 114813d60f4bSZhang Yi else 114913d60f4bSZhang Yi compound_idx = page - page_head; 115013d60f4bSZhang Yi 115113d60f4bSZhang Yi return (index << compound_order(page_head)) + compound_idx; 115213d60f4bSZhang Yi } 115313d60f4bSZhang Yi 1154a5516438SAndi Kleen static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid) 11551da177e4SLinus Torvalds { 11561da177e4SLinus Torvalds struct page *page; 1157f96efd58SJoe Jin 11586484eb3eSMel Gorman page = alloc_pages_exact_node(nid, 115986cdb465SNaoya Horiguchi htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| 1160551883aeSNishanth Aravamudan __GFP_REPEAT|__GFP_NOWARN, 1161a5516438SAndi Kleen huge_page_order(h)); 11621da177e4SLinus Torvalds if (page) { 11637f2e9525SGerald Schaefer if (arch_prepare_hugepage(page)) { 1164caff3a2cSGerald Schaefer __free_pages(page, huge_page_order(h)); 11657b8ee84dSHarvey Harrison return NULL; 11667f2e9525SGerald Schaefer } 1167a5516438SAndi Kleen prep_new_huge_page(h, page, nid); 11681da177e4SLinus Torvalds } 116963b4613cSNishanth Aravamudan 117063b4613cSNishanth Aravamudan return page; 117163b4613cSNishanth Aravamudan } 117263b4613cSNishanth Aravamudan 1173b2261026SJoonsoo Kim static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed) 1174b2261026SJoonsoo Kim { 1175b2261026SJoonsoo Kim struct page *page; 1176b2261026SJoonsoo Kim int nr_nodes, node; 1177b2261026SJoonsoo Kim int ret = 0; 1178b2261026SJoonsoo Kim 1179b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1180b2261026SJoonsoo Kim page = alloc_fresh_huge_page_node(h, node); 1181b2261026SJoonsoo Kim if (page) { 1182b2261026SJoonsoo Kim ret = 1; 1183b2261026SJoonsoo Kim break; 1184b2261026SJoonsoo Kim } 1185b2261026SJoonsoo Kim } 1186b2261026SJoonsoo Kim 1187b2261026SJoonsoo Kim if (ret) 1188b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC); 1189b2261026SJoonsoo Kim else 1190b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 1191b2261026SJoonsoo Kim 1192b2261026SJoonsoo Kim return ret; 1193b2261026SJoonsoo Kim } 1194b2261026SJoonsoo Kim 1195e8c5c824SLee Schermerhorn /* 1196e8c5c824SLee Schermerhorn * Free huge page from pool from next node to free. 1197e8c5c824SLee Schermerhorn * Attempt to keep persistent huge pages more or less 1198e8c5c824SLee Schermerhorn * balanced over allowed nodes. 1199e8c5c824SLee Schermerhorn * Called with hugetlb_lock locked. 1200e8c5c824SLee Schermerhorn */ 12016ae11b27SLee Schermerhorn static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, 12026ae11b27SLee Schermerhorn bool acct_surplus) 1203e8c5c824SLee Schermerhorn { 1204b2261026SJoonsoo Kim int nr_nodes, node; 1205e8c5c824SLee Schermerhorn int ret = 0; 1206e8c5c824SLee Schermerhorn 1207b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1208685f3457SLee Schermerhorn /* 1209685f3457SLee Schermerhorn * If we're returning unused surplus pages, only examine 1210685f3457SLee Schermerhorn * nodes with surplus pages. 1211685f3457SLee Schermerhorn */ 1212b2261026SJoonsoo Kim if ((!acct_surplus || h->surplus_huge_pages_node[node]) && 1213b2261026SJoonsoo Kim !list_empty(&h->hugepage_freelists[node])) { 1214e8c5c824SLee Schermerhorn struct page *page = 1215b2261026SJoonsoo Kim list_entry(h->hugepage_freelists[node].next, 1216e8c5c824SLee Schermerhorn struct page, lru); 1217e8c5c824SLee Schermerhorn list_del(&page->lru); 1218e8c5c824SLee Schermerhorn h->free_huge_pages--; 1219b2261026SJoonsoo Kim h->free_huge_pages_node[node]--; 1220685f3457SLee Schermerhorn if (acct_surplus) { 1221685f3457SLee Schermerhorn h->surplus_huge_pages--; 1222b2261026SJoonsoo Kim h->surplus_huge_pages_node[node]--; 1223685f3457SLee Schermerhorn } 1224e8c5c824SLee Schermerhorn update_and_free_page(h, page); 1225e8c5c824SLee Schermerhorn ret = 1; 12269a76db09SLee Schermerhorn break; 1227e8c5c824SLee Schermerhorn } 1228b2261026SJoonsoo Kim } 1229e8c5c824SLee Schermerhorn 1230e8c5c824SLee Schermerhorn return ret; 1231e8c5c824SLee Schermerhorn } 1232e8c5c824SLee Schermerhorn 1233c8721bbbSNaoya Horiguchi /* 1234c8721bbbSNaoya Horiguchi * Dissolve a given free hugepage into free buddy pages. This function does 1235c8721bbbSNaoya Horiguchi * nothing for in-use (including surplus) hugepages. 1236c8721bbbSNaoya Horiguchi */ 1237c8721bbbSNaoya Horiguchi static void dissolve_free_huge_page(struct page *page) 1238c8721bbbSNaoya Horiguchi { 1239c8721bbbSNaoya Horiguchi spin_lock(&hugetlb_lock); 1240c8721bbbSNaoya Horiguchi if (PageHuge(page) && !page_count(page)) { 1241c8721bbbSNaoya Horiguchi struct hstate *h = page_hstate(page); 1242c8721bbbSNaoya Horiguchi int nid = page_to_nid(page); 1243c8721bbbSNaoya Horiguchi list_del(&page->lru); 1244c8721bbbSNaoya Horiguchi h->free_huge_pages--; 1245c8721bbbSNaoya Horiguchi h->free_huge_pages_node[nid]--; 1246c8721bbbSNaoya Horiguchi update_and_free_page(h, page); 1247c8721bbbSNaoya Horiguchi } 1248c8721bbbSNaoya Horiguchi spin_unlock(&hugetlb_lock); 1249c8721bbbSNaoya Horiguchi } 1250c8721bbbSNaoya Horiguchi 1251c8721bbbSNaoya Horiguchi /* 1252c8721bbbSNaoya Horiguchi * Dissolve free hugepages in a given pfn range. Used by memory hotplug to 1253c8721bbbSNaoya Horiguchi * make specified memory blocks removable from the system. 1254c8721bbbSNaoya Horiguchi * Note that start_pfn should aligned with (minimum) hugepage size. 1255c8721bbbSNaoya Horiguchi */ 1256c8721bbbSNaoya Horiguchi void dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) 1257c8721bbbSNaoya Horiguchi { 1258c8721bbbSNaoya Horiguchi unsigned long pfn; 1259c8721bbbSNaoya Horiguchi 1260d0177639SLi Zhong if (!hugepages_supported()) 1261d0177639SLi Zhong return; 1262d0177639SLi Zhong 1263641844f5SNaoya Horiguchi VM_BUG_ON(!IS_ALIGNED(start_pfn, 1 << minimum_order)); 1264641844f5SNaoya Horiguchi for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) 1265c8721bbbSNaoya Horiguchi dissolve_free_huge_page(pfn_to_page(pfn)); 1266c8721bbbSNaoya Horiguchi } 1267c8721bbbSNaoya Horiguchi 1268bf50bab2SNaoya Horiguchi static struct page *alloc_buddy_huge_page(struct hstate *h, int nid) 12697893d1d5SAdam Litke { 12707893d1d5SAdam Litke struct page *page; 1271bf50bab2SNaoya Horiguchi unsigned int r_nid; 12727893d1d5SAdam Litke 1273bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1274aa888a74SAndi Kleen return NULL; 1275aa888a74SAndi Kleen 1276d1c3fb1fSNishanth Aravamudan /* 1277d1c3fb1fSNishanth Aravamudan * Assume we will successfully allocate the surplus page to 1278d1c3fb1fSNishanth Aravamudan * prevent racing processes from causing the surplus to exceed 1279d1c3fb1fSNishanth Aravamudan * overcommit 1280d1c3fb1fSNishanth Aravamudan * 1281d1c3fb1fSNishanth Aravamudan * This however introduces a different race, where a process B 1282d1c3fb1fSNishanth Aravamudan * tries to grow the static hugepage pool while alloc_pages() is 1283d1c3fb1fSNishanth Aravamudan * called by process A. B will only examine the per-node 1284d1c3fb1fSNishanth Aravamudan * counters in determining if surplus huge pages can be 1285d1c3fb1fSNishanth Aravamudan * converted to normal huge pages in adjust_pool_surplus(). A 1286d1c3fb1fSNishanth Aravamudan * won't be able to increment the per-node counter, until the 1287d1c3fb1fSNishanth Aravamudan * lock is dropped by B, but B doesn't drop hugetlb_lock until 1288d1c3fb1fSNishanth Aravamudan * no more huge pages can be converted from surplus to normal 1289d1c3fb1fSNishanth Aravamudan * state (and doesn't try to convert again). Thus, we have a 1290d1c3fb1fSNishanth Aravamudan * case where a surplus huge page exists, the pool is grown, and 1291d1c3fb1fSNishanth Aravamudan * the surplus huge page still exists after, even though it 1292d1c3fb1fSNishanth Aravamudan * should just have been converted to a normal huge page. This 1293d1c3fb1fSNishanth Aravamudan * does not leak memory, though, as the hugepage will be freed 1294d1c3fb1fSNishanth Aravamudan * once it is out of use. It also does not allow the counters to 1295d1c3fb1fSNishanth Aravamudan * go out of whack in adjust_pool_surplus() as we don't modify 1296d1c3fb1fSNishanth Aravamudan * the node values until we've gotten the hugepage and only the 1297d1c3fb1fSNishanth Aravamudan * per-node value is checked there. 1298d1c3fb1fSNishanth Aravamudan */ 1299d1c3fb1fSNishanth Aravamudan spin_lock(&hugetlb_lock); 1300a5516438SAndi Kleen if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { 1301d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1302d1c3fb1fSNishanth Aravamudan return NULL; 1303d1c3fb1fSNishanth Aravamudan } else { 1304a5516438SAndi Kleen h->nr_huge_pages++; 1305a5516438SAndi Kleen h->surplus_huge_pages++; 1306d1c3fb1fSNishanth Aravamudan } 1307d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1308d1c3fb1fSNishanth Aravamudan 1309bf50bab2SNaoya Horiguchi if (nid == NUMA_NO_NODE) 131086cdb465SNaoya Horiguchi page = alloc_pages(htlb_alloc_mask(h)|__GFP_COMP| 1311551883aeSNishanth Aravamudan __GFP_REPEAT|__GFP_NOWARN, 1312a5516438SAndi Kleen huge_page_order(h)); 1313bf50bab2SNaoya Horiguchi else 1314bf50bab2SNaoya Horiguchi page = alloc_pages_exact_node(nid, 131586cdb465SNaoya Horiguchi htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| 1316bf50bab2SNaoya Horiguchi __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h)); 1317d1c3fb1fSNishanth Aravamudan 1318caff3a2cSGerald Schaefer if (page && arch_prepare_hugepage(page)) { 1319caff3a2cSGerald Schaefer __free_pages(page, huge_page_order(h)); 1320ea5768c7SHillf Danton page = NULL; 1321caff3a2cSGerald Schaefer } 1322caff3a2cSGerald Schaefer 13237893d1d5SAdam Litke spin_lock(&hugetlb_lock); 1324d1c3fb1fSNishanth Aravamudan if (page) { 13250edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1326bf50bab2SNaoya Horiguchi r_nid = page_to_nid(page); 1327d1c3fb1fSNishanth Aravamudan set_compound_page_dtor(page, free_huge_page); 13289dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1329d1c3fb1fSNishanth Aravamudan /* 1330d1c3fb1fSNishanth Aravamudan * We incremented the global counters already 1331d1c3fb1fSNishanth Aravamudan */ 1332bf50bab2SNaoya Horiguchi h->nr_huge_pages_node[r_nid]++; 1333bf50bab2SNaoya Horiguchi h->surplus_huge_pages_node[r_nid]++; 13343b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC); 1335d1c3fb1fSNishanth Aravamudan } else { 1336a5516438SAndi Kleen h->nr_huge_pages--; 1337a5516438SAndi Kleen h->surplus_huge_pages--; 13383b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 13397893d1d5SAdam Litke } 1340d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 13417893d1d5SAdam Litke 13427893d1d5SAdam Litke return page; 13437893d1d5SAdam Litke } 13447893d1d5SAdam Litke 1345e4e574b7SAdam Litke /* 1346bf50bab2SNaoya Horiguchi * This allocation function is useful in the context where vma is irrelevant. 1347bf50bab2SNaoya Horiguchi * E.g. soft-offlining uses this function because it only cares physical 1348bf50bab2SNaoya Horiguchi * address of error page. 1349bf50bab2SNaoya Horiguchi */ 1350bf50bab2SNaoya Horiguchi struct page *alloc_huge_page_node(struct hstate *h, int nid) 1351bf50bab2SNaoya Horiguchi { 13524ef91848SJoonsoo Kim struct page *page = NULL; 1353bf50bab2SNaoya Horiguchi 1354bf50bab2SNaoya Horiguchi spin_lock(&hugetlb_lock); 13554ef91848SJoonsoo Kim if (h->free_huge_pages - h->resv_huge_pages > 0) 1356bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, nid); 1357bf50bab2SNaoya Horiguchi spin_unlock(&hugetlb_lock); 1358bf50bab2SNaoya Horiguchi 135994ae8ba7SAneesh Kumar K.V if (!page) 1360bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, nid); 1361bf50bab2SNaoya Horiguchi 1362bf50bab2SNaoya Horiguchi return page; 1363bf50bab2SNaoya Horiguchi } 1364bf50bab2SNaoya Horiguchi 1365bf50bab2SNaoya Horiguchi /* 136625985edcSLucas De Marchi * Increase the hugetlb pool such that it can accommodate a reservation 1367e4e574b7SAdam Litke * of size 'delta'. 1368e4e574b7SAdam Litke */ 1369a5516438SAndi Kleen static int gather_surplus_pages(struct hstate *h, int delta) 1370e4e574b7SAdam Litke { 1371e4e574b7SAdam Litke struct list_head surplus_list; 1372e4e574b7SAdam Litke struct page *page, *tmp; 1373e4e574b7SAdam Litke int ret, i; 1374e4e574b7SAdam Litke int needed, allocated; 137528073b02SHillf Danton bool alloc_ok = true; 1376e4e574b7SAdam Litke 1377a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - h->free_huge_pages; 1378ac09b3a1SAdam Litke if (needed <= 0) { 1379a5516438SAndi Kleen h->resv_huge_pages += delta; 1380e4e574b7SAdam Litke return 0; 1381ac09b3a1SAdam Litke } 1382e4e574b7SAdam Litke 1383e4e574b7SAdam Litke allocated = 0; 1384e4e574b7SAdam Litke INIT_LIST_HEAD(&surplus_list); 1385e4e574b7SAdam Litke 1386e4e574b7SAdam Litke ret = -ENOMEM; 1387e4e574b7SAdam Litke retry: 1388e4e574b7SAdam Litke spin_unlock(&hugetlb_lock); 1389e4e574b7SAdam Litke for (i = 0; i < needed; i++) { 1390bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, NUMA_NO_NODE); 139128073b02SHillf Danton if (!page) { 139228073b02SHillf Danton alloc_ok = false; 139328073b02SHillf Danton break; 139428073b02SHillf Danton } 1395e4e574b7SAdam Litke list_add(&page->lru, &surplus_list); 1396e4e574b7SAdam Litke } 139728073b02SHillf Danton allocated += i; 1398e4e574b7SAdam Litke 1399e4e574b7SAdam Litke /* 1400e4e574b7SAdam Litke * After retaking hugetlb_lock, we need to recalculate 'needed' 1401e4e574b7SAdam Litke * because either resv_huge_pages or free_huge_pages may have changed. 1402e4e574b7SAdam Litke */ 1403e4e574b7SAdam Litke spin_lock(&hugetlb_lock); 1404a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - 1405a5516438SAndi Kleen (h->free_huge_pages + allocated); 140628073b02SHillf Danton if (needed > 0) { 140728073b02SHillf Danton if (alloc_ok) 1408e4e574b7SAdam Litke goto retry; 140928073b02SHillf Danton /* 141028073b02SHillf Danton * We were not able to allocate enough pages to 141128073b02SHillf Danton * satisfy the entire reservation so we free what 141228073b02SHillf Danton * we've allocated so far. 141328073b02SHillf Danton */ 141428073b02SHillf Danton goto free; 141528073b02SHillf Danton } 1416e4e574b7SAdam Litke /* 1417e4e574b7SAdam Litke * The surplus_list now contains _at_least_ the number of extra pages 141825985edcSLucas De Marchi * needed to accommodate the reservation. Add the appropriate number 1419e4e574b7SAdam Litke * of pages to the hugetlb pool and free the extras back to the buddy 1420ac09b3a1SAdam Litke * allocator. Commit the entire reservation here to prevent another 1421ac09b3a1SAdam Litke * process from stealing the pages as they are added to the pool but 1422ac09b3a1SAdam Litke * before they are reserved. 1423e4e574b7SAdam Litke */ 1424e4e574b7SAdam Litke needed += allocated; 1425a5516438SAndi Kleen h->resv_huge_pages += delta; 1426e4e574b7SAdam Litke ret = 0; 1427a9869b83SNaoya Horiguchi 142819fc3f0aSAdam Litke /* Free the needed pages to the hugetlb pool */ 142919fc3f0aSAdam Litke list_for_each_entry_safe(page, tmp, &surplus_list, lru) { 143019fc3f0aSAdam Litke if ((--needed) < 0) 143119fc3f0aSAdam Litke break; 1432a9869b83SNaoya Horiguchi /* 1433a9869b83SNaoya Horiguchi * This page is now managed by the hugetlb allocator and has 1434a9869b83SNaoya Horiguchi * no users -- drop the buddy allocator's reference. 1435a9869b83SNaoya Horiguchi */ 1436a9869b83SNaoya Horiguchi put_page_testzero(page); 1437309381feSSasha Levin VM_BUG_ON_PAGE(page_count(page), page); 1438a5516438SAndi Kleen enqueue_huge_page(h, page); 143919fc3f0aSAdam Litke } 144028073b02SHillf Danton free: 1441b0365c8dSHillf Danton spin_unlock(&hugetlb_lock); 144219fc3f0aSAdam Litke 144319fc3f0aSAdam Litke /* Free unnecessary surplus pages to the buddy allocator */ 1444c0d934baSJoonsoo Kim list_for_each_entry_safe(page, tmp, &surplus_list, lru) 1445a9869b83SNaoya Horiguchi put_page(page); 144619fc3f0aSAdam Litke spin_lock(&hugetlb_lock); 1447e4e574b7SAdam Litke 1448e4e574b7SAdam Litke return ret; 1449e4e574b7SAdam Litke } 1450e4e574b7SAdam Litke 1451e4e574b7SAdam Litke /* 1452e4e574b7SAdam Litke * When releasing a hugetlb pool reservation, any surplus pages that were 1453e4e574b7SAdam Litke * allocated to satisfy the reservation must be explicitly freed if they were 1454e4e574b7SAdam Litke * never used. 1455685f3457SLee Schermerhorn * Called with hugetlb_lock held. 1456e4e574b7SAdam Litke */ 1457a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h, 1458a5516438SAndi Kleen unsigned long unused_resv_pages) 1459e4e574b7SAdam Litke { 1460e4e574b7SAdam Litke unsigned long nr_pages; 1461e4e574b7SAdam Litke 1462ac09b3a1SAdam Litke /* Uncommit the reservation */ 1463a5516438SAndi Kleen h->resv_huge_pages -= unused_resv_pages; 1464ac09b3a1SAdam Litke 1465aa888a74SAndi Kleen /* Cannot return gigantic pages currently */ 1466bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1467aa888a74SAndi Kleen return; 1468aa888a74SAndi Kleen 1469a5516438SAndi Kleen nr_pages = min(unused_resv_pages, h->surplus_huge_pages); 1470e4e574b7SAdam Litke 1471685f3457SLee Schermerhorn /* 1472685f3457SLee Schermerhorn * We want to release as many surplus pages as possible, spread 14739b5e5d0fSLee Schermerhorn * evenly across all nodes with memory. Iterate across these nodes 14749b5e5d0fSLee Schermerhorn * until we can no longer free unreserved surplus pages. This occurs 14759b5e5d0fSLee Schermerhorn * when the nodes with surplus pages have no free pages. 14769b5e5d0fSLee Schermerhorn * free_pool_huge_page() will balance the the freed pages across the 14779b5e5d0fSLee Schermerhorn * on-line nodes with memory and will handle the hstate accounting. 1478685f3457SLee Schermerhorn */ 1479685f3457SLee Schermerhorn while (nr_pages--) { 14808cebfcd0SLai Jiangshan if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1)) 1481685f3457SLee Schermerhorn break; 14827848a4bfSMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 1483e4e574b7SAdam Litke } 1484e4e574b7SAdam Litke } 1485e4e574b7SAdam Litke 1486c37f9fb1SAndy Whitcroft /* 1487cf3ad20bSMike Kravetz * vma_needs_reservation and vma_commit_reservation are used by the huge 1488cf3ad20bSMike Kravetz * page allocation routines to manage reservations. 1489cf3ad20bSMike Kravetz * 1490cf3ad20bSMike Kravetz * vma_needs_reservation is called to determine if the huge page at addr 1491cf3ad20bSMike Kravetz * within the vma has an associated reservation. If a reservation is 1492cf3ad20bSMike Kravetz * needed, the value 1 is returned. The caller is then responsible for 1493cf3ad20bSMike Kravetz * managing the global reservation and subpool usage counts. After 1494cf3ad20bSMike Kravetz * the huge page has been allocated, vma_commit_reservation is called 1495cf3ad20bSMike Kravetz * to add the page to the reservation map. 1496cf3ad20bSMike Kravetz * 1497cf3ad20bSMike Kravetz * In the normal case, vma_commit_reservation returns the same value 1498cf3ad20bSMike Kravetz * as the preceding vma_needs_reservation call. The only time this 1499cf3ad20bSMike Kravetz * is not the case is if a reserve map was changed between calls. It 1500cf3ad20bSMike Kravetz * is the responsibility of the caller to notice the difference and 1501cf3ad20bSMike Kravetz * take appropriate action. 1502c37f9fb1SAndy Whitcroft */ 1503cf3ad20bSMike Kravetz static long __vma_reservation_common(struct hstate *h, 1504cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr, 1505cf3ad20bSMike Kravetz bool commit) 1506c37f9fb1SAndy Whitcroft { 15074e35f483SJoonsoo Kim struct resv_map *resv; 15084e35f483SJoonsoo Kim pgoff_t idx; 1509cf3ad20bSMike Kravetz long ret; 1510c37f9fb1SAndy Whitcroft 15114e35f483SJoonsoo Kim resv = vma_resv_map(vma); 15124e35f483SJoonsoo Kim if (!resv) 1513c37f9fb1SAndy Whitcroft return 1; 1514c37f9fb1SAndy Whitcroft 15154e35f483SJoonsoo Kim idx = vma_hugecache_offset(h, vma, addr); 1516cf3ad20bSMike Kravetz if (commit) 1517cf3ad20bSMike Kravetz ret = region_add(resv, idx, idx + 1); 1518cf3ad20bSMike Kravetz else 1519cf3ad20bSMike Kravetz ret = region_chg(resv, idx, idx + 1); 152084afd99bSAndy Whitcroft 15214e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) 1522cf3ad20bSMike Kravetz return ret; 15234e35f483SJoonsoo Kim else 1524cf3ad20bSMike Kravetz return ret < 0 ? ret : 0; 152584afd99bSAndy Whitcroft } 1526cf3ad20bSMike Kravetz 1527cf3ad20bSMike Kravetz static long vma_needs_reservation(struct hstate *h, 1528a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long addr) 1529c37f9fb1SAndy Whitcroft { 1530cf3ad20bSMike Kravetz return __vma_reservation_common(h, vma, addr, false); 1531cf3ad20bSMike Kravetz } 1532c37f9fb1SAndy Whitcroft 1533cf3ad20bSMike Kravetz static long vma_commit_reservation(struct hstate *h, 1534cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr) 1535cf3ad20bSMike Kravetz { 1536cf3ad20bSMike Kravetz return __vma_reservation_common(h, vma, addr, true); 1537c37f9fb1SAndy Whitcroft } 1538c37f9fb1SAndy Whitcroft 1539348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma, 154004f2cbe3SMel Gorman unsigned long addr, int avoid_reserve) 1541348ea204SAdam Litke { 154290481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 1543a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 1544348ea204SAdam Litke struct page *page; 154533039678SMike Kravetz long chg, commit; 15466d76dcf4SAneesh Kumar K.V int ret, idx; 15476d76dcf4SAneesh Kumar K.V struct hugetlb_cgroup *h_cg; 15482fc39cecSAdam Litke 15496d76dcf4SAneesh Kumar K.V idx = hstate_index(h); 1550a1e78772SMel Gorman /* 155190481622SDavid Gibson * Processes that did not create the mapping will have no 155290481622SDavid Gibson * reserves and will not have accounted against subpool 155390481622SDavid Gibson * limit. Check that the subpool limit can be made before 155490481622SDavid Gibson * satisfying the allocation MAP_NORESERVE mappings may also 155590481622SDavid Gibson * need pages and subpool limit allocated allocated if no reserve 155690481622SDavid Gibson * mapping overlaps. 1557a1e78772SMel Gorman */ 1558a5516438SAndi Kleen chg = vma_needs_reservation(h, vma, addr); 1559c37f9fb1SAndy Whitcroft if (chg < 0) 156076dcee75SAneesh Kumar K.V return ERR_PTR(-ENOMEM); 15618bb3f12eSJoonsoo Kim if (chg || avoid_reserve) 15621c5ecae3SMike Kravetz if (hugepage_subpool_get_pages(spool, 1) < 0) 156376dcee75SAneesh Kumar K.V return ERR_PTR(-ENOSPC); 156490d8b7e6SAdam Litke 15656d76dcf4SAneesh Kumar K.V ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); 15668f34af6fSJianyu Zhan if (ret) 15678f34af6fSJianyu Zhan goto out_subpool_put; 15688f34af6fSJianyu Zhan 1569a1e78772SMel Gorman spin_lock(&hugetlb_lock); 1570af0ed73eSJoonsoo Kim page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg); 157181a6fcaeSJoonsoo Kim if (!page) { 157294ae8ba7SAneesh Kumar K.V spin_unlock(&hugetlb_lock); 1573bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, NUMA_NO_NODE); 15748f34af6fSJianyu Zhan if (!page) 15758f34af6fSJianyu Zhan goto out_uncharge_cgroup; 15768f34af6fSJianyu Zhan 157779dbb236SAneesh Kumar K.V spin_lock(&hugetlb_lock); 157879dbb236SAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 157981a6fcaeSJoonsoo Kim /* Fall through */ 1580a1e78772SMel Gorman } 158181a6fcaeSJoonsoo Kim hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page); 158281a6fcaeSJoonsoo Kim spin_unlock(&hugetlb_lock); 1583a1e78772SMel Gorman 158490481622SDavid Gibson set_page_private(page, (unsigned long)spool); 1585a1e78772SMel Gorman 158633039678SMike Kravetz commit = vma_commit_reservation(h, vma, addr); 158733039678SMike Kravetz if (unlikely(chg > commit)) { 158833039678SMike Kravetz /* 158933039678SMike Kravetz * The page was added to the reservation map between 159033039678SMike Kravetz * vma_needs_reservation and vma_commit_reservation. 159133039678SMike Kravetz * This indicates a race with hugetlb_reserve_pages. 159233039678SMike Kravetz * Adjust for the subpool count incremented above AND 159333039678SMike Kravetz * in hugetlb_reserve_pages for the same page. Also, 159433039678SMike Kravetz * the reservation count added in hugetlb_reserve_pages 159533039678SMike Kravetz * no longer applies. 159633039678SMike Kravetz */ 159733039678SMike Kravetz long rsv_adjust; 159833039678SMike Kravetz 159933039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 1); 160033039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 160133039678SMike Kravetz } 16027893d1d5SAdam Litke return page; 16038f34af6fSJianyu Zhan 16048f34af6fSJianyu Zhan out_uncharge_cgroup: 16058f34af6fSJianyu Zhan hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); 16068f34af6fSJianyu Zhan out_subpool_put: 16078f34af6fSJianyu Zhan if (chg || avoid_reserve) 16088f34af6fSJianyu Zhan hugepage_subpool_put_pages(spool, 1); 16098f34af6fSJianyu Zhan return ERR_PTR(-ENOSPC); 1610b45b5bd6SDavid Gibson } 1611b45b5bd6SDavid Gibson 161274060e4dSNaoya Horiguchi /* 161374060e4dSNaoya Horiguchi * alloc_huge_page()'s wrapper which simply returns the page if allocation 161474060e4dSNaoya Horiguchi * succeeds, otherwise NULL. This function is called from new_vma_page(), 161574060e4dSNaoya Horiguchi * where no ERR_VALUE is expected to be returned. 161674060e4dSNaoya Horiguchi */ 161774060e4dSNaoya Horiguchi struct page *alloc_huge_page_noerr(struct vm_area_struct *vma, 161874060e4dSNaoya Horiguchi unsigned long addr, int avoid_reserve) 161974060e4dSNaoya Horiguchi { 162074060e4dSNaoya Horiguchi struct page *page = alloc_huge_page(vma, addr, avoid_reserve); 162174060e4dSNaoya Horiguchi if (IS_ERR(page)) 162274060e4dSNaoya Horiguchi page = NULL; 162374060e4dSNaoya Horiguchi return page; 162474060e4dSNaoya Horiguchi } 162574060e4dSNaoya Horiguchi 162691f47662SCyrill Gorcunov int __weak alloc_bootmem_huge_page(struct hstate *h) 1627aa888a74SAndi Kleen { 1628aa888a74SAndi Kleen struct huge_bootmem_page *m; 1629b2261026SJoonsoo Kim int nr_nodes, node; 1630aa888a74SAndi Kleen 1631b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) { 1632aa888a74SAndi Kleen void *addr; 1633aa888a74SAndi Kleen 16348b89a116SGrygorii Strashko addr = memblock_virt_alloc_try_nid_nopanic( 16358b89a116SGrygorii Strashko huge_page_size(h), huge_page_size(h), 16368b89a116SGrygorii Strashko 0, BOOTMEM_ALLOC_ACCESSIBLE, node); 1637aa888a74SAndi Kleen if (addr) { 1638aa888a74SAndi Kleen /* 1639aa888a74SAndi Kleen * Use the beginning of the huge page to store the 1640aa888a74SAndi Kleen * huge_bootmem_page struct (until gather_bootmem 1641aa888a74SAndi Kleen * puts them into the mem_map). 1642aa888a74SAndi Kleen */ 1643aa888a74SAndi Kleen m = addr; 1644aa888a74SAndi Kleen goto found; 1645aa888a74SAndi Kleen } 1646aa888a74SAndi Kleen } 1647aa888a74SAndi Kleen return 0; 1648aa888a74SAndi Kleen 1649aa888a74SAndi Kleen found: 1650df994eadSLuiz Capitulino BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h))); 1651aa888a74SAndi Kleen /* Put them into a private list first because mem_map is not up yet */ 1652aa888a74SAndi Kleen list_add(&m->list, &huge_boot_pages); 1653aa888a74SAndi Kleen m->hstate = h; 1654aa888a74SAndi Kleen return 1; 1655aa888a74SAndi Kleen } 1656aa888a74SAndi Kleen 1657f412c97aSDavid Rientjes static void __init prep_compound_huge_page(struct page *page, int order) 165818229df5SAndy Whitcroft { 165918229df5SAndy Whitcroft if (unlikely(order > (MAX_ORDER - 1))) 166018229df5SAndy Whitcroft prep_compound_gigantic_page(page, order); 166118229df5SAndy Whitcroft else 166218229df5SAndy Whitcroft prep_compound_page(page, order); 166318229df5SAndy Whitcroft } 166418229df5SAndy Whitcroft 1665aa888a74SAndi Kleen /* Put bootmem huge pages into the standard lists after mem_map is up */ 1666aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void) 1667aa888a74SAndi Kleen { 1668aa888a74SAndi Kleen struct huge_bootmem_page *m; 1669aa888a74SAndi Kleen 1670aa888a74SAndi Kleen list_for_each_entry(m, &huge_boot_pages, list) { 1671aa888a74SAndi Kleen struct hstate *h = m->hstate; 1672ee8f248dSBecky Bruce struct page *page; 1673ee8f248dSBecky Bruce 1674ee8f248dSBecky Bruce #ifdef CONFIG_HIGHMEM 1675ee8f248dSBecky Bruce page = pfn_to_page(m->phys >> PAGE_SHIFT); 16768b89a116SGrygorii Strashko memblock_free_late(__pa(m), 1677ee8f248dSBecky Bruce sizeof(struct huge_bootmem_page)); 1678ee8f248dSBecky Bruce #else 1679ee8f248dSBecky Bruce page = virt_to_page(m); 1680ee8f248dSBecky Bruce #endif 1681aa888a74SAndi Kleen WARN_ON(page_count(page) != 1); 168218229df5SAndy Whitcroft prep_compound_huge_page(page, h->order); 1683ef5a22beSAndrea Arcangeli WARN_ON(PageReserved(page)); 1684aa888a74SAndi Kleen prep_new_huge_page(h, page, page_to_nid(page)); 1685b0320c7bSRafael Aquini /* 1686b0320c7bSRafael Aquini * If we had gigantic hugepages allocated at boot time, we need 1687b0320c7bSRafael Aquini * to restore the 'stolen' pages to totalram_pages in order to 1688b0320c7bSRafael Aquini * fix confusing memory reports from free(1) and another 1689b0320c7bSRafael Aquini * side-effects, like CommitLimit going negative. 1690b0320c7bSRafael Aquini */ 1691bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 16923dcc0571SJiang Liu adjust_managed_page_count(page, 1 << h->order); 1693aa888a74SAndi Kleen } 1694aa888a74SAndi Kleen } 1695aa888a74SAndi Kleen 16968faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h) 16971da177e4SLinus Torvalds { 16981da177e4SLinus Torvalds unsigned long i; 16991da177e4SLinus Torvalds 1700e5ff2159SAndi Kleen for (i = 0; i < h->max_huge_pages; ++i) { 1701bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) { 1702aa888a74SAndi Kleen if (!alloc_bootmem_huge_page(h)) 1703aa888a74SAndi Kleen break; 17049b5e5d0fSLee Schermerhorn } else if (!alloc_fresh_huge_page(h, 17058cebfcd0SLai Jiangshan &node_states[N_MEMORY])) 17061da177e4SLinus Torvalds break; 17071da177e4SLinus Torvalds } 17088faa8b07SAndi Kleen h->max_huge_pages = i; 1709e5ff2159SAndi Kleen } 1710e5ff2159SAndi Kleen 1711e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void) 1712e5ff2159SAndi Kleen { 1713e5ff2159SAndi Kleen struct hstate *h; 1714e5ff2159SAndi Kleen 1715e5ff2159SAndi Kleen for_each_hstate(h) { 1716641844f5SNaoya Horiguchi if (minimum_order > huge_page_order(h)) 1717641844f5SNaoya Horiguchi minimum_order = huge_page_order(h); 1718641844f5SNaoya Horiguchi 17198faa8b07SAndi Kleen /* oversize hugepages were init'ed in early boot */ 1720bae7f4aeSLuiz Capitulino if (!hstate_is_gigantic(h)) 17218faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(h); 1722e5ff2159SAndi Kleen } 1723641844f5SNaoya Horiguchi VM_BUG_ON(minimum_order == UINT_MAX); 1724e5ff2159SAndi Kleen } 1725e5ff2159SAndi Kleen 17264abd32dbSAndi Kleen static char * __init memfmt(char *buf, unsigned long n) 17274abd32dbSAndi Kleen { 17284abd32dbSAndi Kleen if (n >= (1UL << 30)) 17294abd32dbSAndi Kleen sprintf(buf, "%lu GB", n >> 30); 17304abd32dbSAndi Kleen else if (n >= (1UL << 20)) 17314abd32dbSAndi Kleen sprintf(buf, "%lu MB", n >> 20); 17324abd32dbSAndi Kleen else 17334abd32dbSAndi Kleen sprintf(buf, "%lu KB", n >> 10); 17344abd32dbSAndi Kleen return buf; 17354abd32dbSAndi Kleen } 17364abd32dbSAndi Kleen 1737e5ff2159SAndi Kleen static void __init report_hugepages(void) 1738e5ff2159SAndi Kleen { 1739e5ff2159SAndi Kleen struct hstate *h; 1740e5ff2159SAndi Kleen 1741e5ff2159SAndi Kleen for_each_hstate(h) { 17424abd32dbSAndi Kleen char buf[32]; 1743ffb22af5SAndrew Morton pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n", 17444abd32dbSAndi Kleen memfmt(buf, huge_page_size(h)), 17454abd32dbSAndi Kleen h->free_huge_pages); 1746e5ff2159SAndi Kleen } 1747e5ff2159SAndi Kleen } 1748e5ff2159SAndi Kleen 17491da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM 17506ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count, 17516ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 17521da177e4SLinus Torvalds { 17534415cc8dSChristoph Lameter int i; 17544415cc8dSChristoph Lameter 1755bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1756aa888a74SAndi Kleen return; 1757aa888a74SAndi Kleen 17586ae11b27SLee Schermerhorn for_each_node_mask(i, *nodes_allowed) { 17591da177e4SLinus Torvalds struct page *page, *next; 1760a5516438SAndi Kleen struct list_head *freel = &h->hugepage_freelists[i]; 1761a5516438SAndi Kleen list_for_each_entry_safe(page, next, freel, lru) { 1762a5516438SAndi Kleen if (count >= h->nr_huge_pages) 17636b0c880dSAdam Litke return; 17641da177e4SLinus Torvalds if (PageHighMem(page)) 17651da177e4SLinus Torvalds continue; 17661da177e4SLinus Torvalds list_del(&page->lru); 1767e5ff2159SAndi Kleen update_and_free_page(h, page); 1768a5516438SAndi Kleen h->free_huge_pages--; 1769a5516438SAndi Kleen h->free_huge_pages_node[page_to_nid(page)]--; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds #else 17746ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count, 17756ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 17761da177e4SLinus Torvalds { 17771da177e4SLinus Torvalds } 17781da177e4SLinus Torvalds #endif 17791da177e4SLinus Torvalds 178020a0307cSWu Fengguang /* 178120a0307cSWu Fengguang * Increment or decrement surplus_huge_pages. Keep node-specific counters 178220a0307cSWu Fengguang * balanced by operating on them in a round-robin fashion. 178320a0307cSWu Fengguang * Returns 1 if an adjustment was made. 178420a0307cSWu Fengguang */ 17856ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, 17866ae11b27SLee Schermerhorn int delta) 178720a0307cSWu Fengguang { 1788b2261026SJoonsoo Kim int nr_nodes, node; 178920a0307cSWu Fengguang 179020a0307cSWu Fengguang VM_BUG_ON(delta != -1 && delta != 1); 179120a0307cSWu Fengguang 1792e8c5c824SLee Schermerhorn if (delta < 0) { 1793b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1794b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node]) 1795b2261026SJoonsoo Kim goto found; 1796b2261026SJoonsoo Kim } 1797b2261026SJoonsoo Kim } else { 1798b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1799b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node] < 1800b2261026SJoonsoo Kim h->nr_huge_pages_node[node]) 1801b2261026SJoonsoo Kim goto found; 1802e8c5c824SLee Schermerhorn } 18039a76db09SLee Schermerhorn } 1804b2261026SJoonsoo Kim return 0; 180520a0307cSWu Fengguang 1806b2261026SJoonsoo Kim found: 180720a0307cSWu Fengguang h->surplus_huge_pages += delta; 1808b2261026SJoonsoo Kim h->surplus_huge_pages_node[node] += delta; 1809b2261026SJoonsoo Kim return 1; 181020a0307cSWu Fengguang } 181120a0307cSWu Fengguang 1812a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) 18136ae11b27SLee Schermerhorn static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count, 18146ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 18151da177e4SLinus Torvalds { 18167893d1d5SAdam Litke unsigned long min_count, ret; 18171da177e4SLinus Torvalds 1818944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 1819aa888a74SAndi Kleen return h->max_huge_pages; 1820aa888a74SAndi Kleen 18217893d1d5SAdam Litke /* 18227893d1d5SAdam Litke * Increase the pool size 18237893d1d5SAdam Litke * First take pages out of surplus state. Then make up the 18247893d1d5SAdam Litke * remaining difference by allocating fresh huge pages. 1825d1c3fb1fSNishanth Aravamudan * 1826d1c3fb1fSNishanth Aravamudan * We might race with alloc_buddy_huge_page() here and be unable 1827d1c3fb1fSNishanth Aravamudan * to convert a surplus huge page to a normal huge page. That is 1828d1c3fb1fSNishanth Aravamudan * not critical, though, it just means the overall size of the 1829d1c3fb1fSNishanth Aravamudan * pool might be one hugepage larger than it needs to be, but 1830d1c3fb1fSNishanth Aravamudan * within all the constraints specified by the sysctls. 18317893d1d5SAdam Litke */ 18321da177e4SLinus Torvalds spin_lock(&hugetlb_lock); 1833a5516438SAndi Kleen while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { 18346ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, -1)) 18357893d1d5SAdam Litke break; 18367893d1d5SAdam Litke } 18377893d1d5SAdam Litke 1838a5516438SAndi Kleen while (count > persistent_huge_pages(h)) { 18397893d1d5SAdam Litke /* 18407893d1d5SAdam Litke * If this allocation races such that we no longer need the 18417893d1d5SAdam Litke * page, free_huge_page will handle it by freeing the page 18427893d1d5SAdam Litke * and reducing the surplus. 18437893d1d5SAdam Litke */ 18447893d1d5SAdam Litke spin_unlock(&hugetlb_lock); 1845944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) 1846944d9fecSLuiz Capitulino ret = alloc_fresh_gigantic_page(h, nodes_allowed); 1847944d9fecSLuiz Capitulino else 18486ae11b27SLee Schermerhorn ret = alloc_fresh_huge_page(h, nodes_allowed); 18497893d1d5SAdam Litke spin_lock(&hugetlb_lock); 18507893d1d5SAdam Litke if (!ret) 18517893d1d5SAdam Litke goto out; 18527893d1d5SAdam Litke 1853536240f2SMel Gorman /* Bail for signals. Probably ctrl-c from user */ 1854536240f2SMel Gorman if (signal_pending(current)) 1855536240f2SMel Gorman goto out; 18567893d1d5SAdam Litke } 18577893d1d5SAdam Litke 18587893d1d5SAdam Litke /* 18597893d1d5SAdam Litke * Decrease the pool size 18607893d1d5SAdam Litke * First return free pages to the buddy allocator (being careful 18617893d1d5SAdam Litke * to keep enough around to satisfy reservations). Then place 18627893d1d5SAdam Litke * pages into surplus state as needed so the pool will shrink 18637893d1d5SAdam Litke * to the desired size as pages become free. 1864d1c3fb1fSNishanth Aravamudan * 1865d1c3fb1fSNishanth Aravamudan * By placing pages into the surplus state independent of the 1866d1c3fb1fSNishanth Aravamudan * overcommit value, we are allowing the surplus pool size to 1867d1c3fb1fSNishanth Aravamudan * exceed overcommit. There are few sane options here. Since 1868d1c3fb1fSNishanth Aravamudan * alloc_buddy_huge_page() is checking the global counter, 1869d1c3fb1fSNishanth Aravamudan * though, we'll note that we're not allowed to exceed surplus 1870d1c3fb1fSNishanth Aravamudan * and won't grow the pool anywhere else. Not until one of the 1871d1c3fb1fSNishanth Aravamudan * sysctls are changed, or the surplus pages go out of use. 18727893d1d5SAdam Litke */ 1873a5516438SAndi Kleen min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; 18746b0c880dSAdam Litke min_count = max(count, min_count); 18756ae11b27SLee Schermerhorn try_to_free_low(h, min_count, nodes_allowed); 1876a5516438SAndi Kleen while (min_count < persistent_huge_pages(h)) { 18776ae11b27SLee Schermerhorn if (!free_pool_huge_page(h, nodes_allowed, 0)) 18781da177e4SLinus Torvalds break; 187955f67141SMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 18801da177e4SLinus Torvalds } 1881a5516438SAndi Kleen while (count < persistent_huge_pages(h)) { 18826ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, 1)) 18837893d1d5SAdam Litke break; 18847893d1d5SAdam Litke } 18857893d1d5SAdam Litke out: 1886a5516438SAndi Kleen ret = persistent_huge_pages(h); 18871da177e4SLinus Torvalds spin_unlock(&hugetlb_lock); 18887893d1d5SAdam Litke return ret; 18891da177e4SLinus Torvalds } 18901da177e4SLinus Torvalds 1891a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \ 1892a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 1893a3437870SNishanth Aravamudan 1894a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \ 1895a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = \ 1896a3437870SNishanth Aravamudan __ATTR(_name, 0644, _name##_show, _name##_store) 1897a3437870SNishanth Aravamudan 1898a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj; 1899a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 1900a3437870SNishanth Aravamudan 19019a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp); 19029a305230SLee Schermerhorn 19039a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp) 1904a3437870SNishanth Aravamudan { 1905a3437870SNishanth Aravamudan int i; 19069a305230SLee Schermerhorn 1907a3437870SNishanth Aravamudan for (i = 0; i < HUGE_MAX_HSTATE; i++) 19089a305230SLee Schermerhorn if (hstate_kobjs[i] == kobj) { 19099a305230SLee Schermerhorn if (nidp) 19109a305230SLee Schermerhorn *nidp = NUMA_NO_NODE; 1911a3437870SNishanth Aravamudan return &hstates[i]; 19129a305230SLee Schermerhorn } 19139a305230SLee Schermerhorn 19149a305230SLee Schermerhorn return kobj_to_node_hstate(kobj, nidp); 1915a3437870SNishanth Aravamudan } 1916a3437870SNishanth Aravamudan 191706808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj, 1918a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 1919a3437870SNishanth Aravamudan { 19209a305230SLee Schermerhorn struct hstate *h; 19219a305230SLee Schermerhorn unsigned long nr_huge_pages; 19229a305230SLee Schermerhorn int nid; 19239a305230SLee Schermerhorn 19249a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 19259a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 19269a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages; 19279a305230SLee Schermerhorn else 19289a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages_node[nid]; 19299a305230SLee Schermerhorn 19309a305230SLee Schermerhorn return sprintf(buf, "%lu\n", nr_huge_pages); 1931a3437870SNishanth Aravamudan } 1932adbe8726SEric B Munson 1933238d3c13SDavid Rientjes static ssize_t __nr_hugepages_store_common(bool obey_mempolicy, 1934238d3c13SDavid Rientjes struct hstate *h, int nid, 1935238d3c13SDavid Rientjes unsigned long count, size_t len) 1936a3437870SNishanth Aravamudan { 1937a3437870SNishanth Aravamudan int err; 1938bad44b5bSDavid Rientjes NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY); 1939a3437870SNishanth Aravamudan 1940944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) { 1941adbe8726SEric B Munson err = -EINVAL; 1942adbe8726SEric B Munson goto out; 1943adbe8726SEric B Munson } 1944adbe8726SEric B Munson 19459a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) { 19469a305230SLee Schermerhorn /* 19479a305230SLee Schermerhorn * global hstate attribute 19489a305230SLee Schermerhorn */ 19499a305230SLee Schermerhorn if (!(obey_mempolicy && 19509a305230SLee Schermerhorn init_nodemask_of_mempolicy(nodes_allowed))) { 195106808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 19528cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 195306808b08SLee Schermerhorn } 19549a305230SLee Schermerhorn } else if (nodes_allowed) { 19559a305230SLee Schermerhorn /* 19569a305230SLee Schermerhorn * per node hstate attribute: adjust count to global, 19579a305230SLee Schermerhorn * but restrict alloc/free to the specified node. 19589a305230SLee Schermerhorn */ 19599a305230SLee Schermerhorn count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; 19609a305230SLee Schermerhorn init_nodemask_of_node(nodes_allowed, nid); 19619a305230SLee Schermerhorn } else 19628cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 19639a305230SLee Schermerhorn 196406808b08SLee Schermerhorn h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed); 1965a3437870SNishanth Aravamudan 19668cebfcd0SLai Jiangshan if (nodes_allowed != &node_states[N_MEMORY]) 196706808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 196806808b08SLee Schermerhorn 196906808b08SLee Schermerhorn return len; 1970adbe8726SEric B Munson out: 1971adbe8726SEric B Munson NODEMASK_FREE(nodes_allowed); 1972adbe8726SEric B Munson return err; 197306808b08SLee Schermerhorn } 197406808b08SLee Schermerhorn 1975238d3c13SDavid Rientjes static ssize_t nr_hugepages_store_common(bool obey_mempolicy, 1976238d3c13SDavid Rientjes struct kobject *kobj, const char *buf, 1977238d3c13SDavid Rientjes size_t len) 1978238d3c13SDavid Rientjes { 1979238d3c13SDavid Rientjes struct hstate *h; 1980238d3c13SDavid Rientjes unsigned long count; 1981238d3c13SDavid Rientjes int nid; 1982238d3c13SDavid Rientjes int err; 1983238d3c13SDavid Rientjes 1984238d3c13SDavid Rientjes err = kstrtoul(buf, 10, &count); 1985238d3c13SDavid Rientjes if (err) 1986238d3c13SDavid Rientjes return err; 1987238d3c13SDavid Rientjes 1988238d3c13SDavid Rientjes h = kobj_to_hstate(kobj, &nid); 1989238d3c13SDavid Rientjes return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len); 1990238d3c13SDavid Rientjes } 1991238d3c13SDavid Rientjes 199206808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj, 199306808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 199406808b08SLee Schermerhorn { 199506808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 199606808b08SLee Schermerhorn } 199706808b08SLee Schermerhorn 199806808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj, 199906808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 200006808b08SLee Schermerhorn { 2001238d3c13SDavid Rientjes return nr_hugepages_store_common(false, kobj, buf, len); 2002a3437870SNishanth Aravamudan } 2003a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages); 2004a3437870SNishanth Aravamudan 200506808b08SLee Schermerhorn #ifdef CONFIG_NUMA 200606808b08SLee Schermerhorn 200706808b08SLee Schermerhorn /* 200806808b08SLee Schermerhorn * hstate attribute for optionally mempolicy-based constraint on persistent 200906808b08SLee Schermerhorn * huge page alloc/free. 201006808b08SLee Schermerhorn */ 201106808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj, 201206808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 201306808b08SLee Schermerhorn { 201406808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 201506808b08SLee Schermerhorn } 201606808b08SLee Schermerhorn 201706808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj, 201806808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 201906808b08SLee Schermerhorn { 2020238d3c13SDavid Rientjes return nr_hugepages_store_common(true, kobj, buf, len); 202106808b08SLee Schermerhorn } 202206808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy); 202306808b08SLee Schermerhorn #endif 202406808b08SLee Schermerhorn 202506808b08SLee Schermerhorn 2026a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, 2027a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2028a3437870SNishanth Aravamudan { 20299a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2030a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages); 2031a3437870SNishanth Aravamudan } 2032adbe8726SEric B Munson 2033a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, 2034a3437870SNishanth Aravamudan struct kobj_attribute *attr, const char *buf, size_t count) 2035a3437870SNishanth Aravamudan { 2036a3437870SNishanth Aravamudan int err; 2037a3437870SNishanth Aravamudan unsigned long input; 20389a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2039a3437870SNishanth Aravamudan 2040bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 2041adbe8726SEric B Munson return -EINVAL; 2042adbe8726SEric B Munson 20433dbb95f7SJingoo Han err = kstrtoul(buf, 10, &input); 2044a3437870SNishanth Aravamudan if (err) 204573ae31e5SEric B Munson return err; 2046a3437870SNishanth Aravamudan 2047a3437870SNishanth Aravamudan spin_lock(&hugetlb_lock); 2048a3437870SNishanth Aravamudan h->nr_overcommit_huge_pages = input; 2049a3437870SNishanth Aravamudan spin_unlock(&hugetlb_lock); 2050a3437870SNishanth Aravamudan 2051a3437870SNishanth Aravamudan return count; 2052a3437870SNishanth Aravamudan } 2053a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages); 2054a3437870SNishanth Aravamudan 2055a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj, 2056a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2057a3437870SNishanth Aravamudan { 20589a305230SLee Schermerhorn struct hstate *h; 20599a305230SLee Schermerhorn unsigned long free_huge_pages; 20609a305230SLee Schermerhorn int nid; 20619a305230SLee Schermerhorn 20629a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 20639a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 20649a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages; 20659a305230SLee Schermerhorn else 20669a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages_node[nid]; 20679a305230SLee Schermerhorn 20689a305230SLee Schermerhorn return sprintf(buf, "%lu\n", free_huge_pages); 2069a3437870SNishanth Aravamudan } 2070a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages); 2071a3437870SNishanth Aravamudan 2072a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj, 2073a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2074a3437870SNishanth Aravamudan { 20759a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2076a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->resv_huge_pages); 2077a3437870SNishanth Aravamudan } 2078a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages); 2079a3437870SNishanth Aravamudan 2080a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj, 2081a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2082a3437870SNishanth Aravamudan { 20839a305230SLee Schermerhorn struct hstate *h; 20849a305230SLee Schermerhorn unsigned long surplus_huge_pages; 20859a305230SLee Schermerhorn int nid; 20869a305230SLee Schermerhorn 20879a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 20889a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 20899a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages; 20909a305230SLee Schermerhorn else 20919a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages_node[nid]; 20929a305230SLee Schermerhorn 20939a305230SLee Schermerhorn return sprintf(buf, "%lu\n", surplus_huge_pages); 2094a3437870SNishanth Aravamudan } 2095a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages); 2096a3437870SNishanth Aravamudan 2097a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = { 2098a3437870SNishanth Aravamudan &nr_hugepages_attr.attr, 2099a3437870SNishanth Aravamudan &nr_overcommit_hugepages_attr.attr, 2100a3437870SNishanth Aravamudan &free_hugepages_attr.attr, 2101a3437870SNishanth Aravamudan &resv_hugepages_attr.attr, 2102a3437870SNishanth Aravamudan &surplus_hugepages_attr.attr, 210306808b08SLee Schermerhorn #ifdef CONFIG_NUMA 210406808b08SLee Schermerhorn &nr_hugepages_mempolicy_attr.attr, 210506808b08SLee Schermerhorn #endif 2106a3437870SNishanth Aravamudan NULL, 2107a3437870SNishanth Aravamudan }; 2108a3437870SNishanth Aravamudan 2109a3437870SNishanth Aravamudan static struct attribute_group hstate_attr_group = { 2110a3437870SNishanth Aravamudan .attrs = hstate_attrs, 2111a3437870SNishanth Aravamudan }; 2112a3437870SNishanth Aravamudan 2113094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, 21149a305230SLee Schermerhorn struct kobject **hstate_kobjs, 21159a305230SLee Schermerhorn struct attribute_group *hstate_attr_group) 2116a3437870SNishanth Aravamudan { 2117a3437870SNishanth Aravamudan int retval; 2118972dc4deSAneesh Kumar K.V int hi = hstate_index(h); 2119a3437870SNishanth Aravamudan 21209a305230SLee Schermerhorn hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); 21219a305230SLee Schermerhorn if (!hstate_kobjs[hi]) 2122a3437870SNishanth Aravamudan return -ENOMEM; 2123a3437870SNishanth Aravamudan 21249a305230SLee Schermerhorn retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group); 2125a3437870SNishanth Aravamudan if (retval) 21269a305230SLee Schermerhorn kobject_put(hstate_kobjs[hi]); 2127a3437870SNishanth Aravamudan 2128a3437870SNishanth Aravamudan return retval; 2129a3437870SNishanth Aravamudan } 2130a3437870SNishanth Aravamudan 2131a3437870SNishanth Aravamudan static void __init hugetlb_sysfs_init(void) 2132a3437870SNishanth Aravamudan { 2133a3437870SNishanth Aravamudan struct hstate *h; 2134a3437870SNishanth Aravamudan int err; 2135a3437870SNishanth Aravamudan 2136a3437870SNishanth Aravamudan hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); 2137a3437870SNishanth Aravamudan if (!hugepages_kobj) 2138a3437870SNishanth Aravamudan return; 2139a3437870SNishanth Aravamudan 2140a3437870SNishanth Aravamudan for_each_hstate(h) { 21419a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, 21429a305230SLee Schermerhorn hstate_kobjs, &hstate_attr_group); 2143a3437870SNishanth Aravamudan if (err) 2144ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s", h->name); 2145a3437870SNishanth Aravamudan } 2146a3437870SNishanth Aravamudan } 2147a3437870SNishanth Aravamudan 21489a305230SLee Schermerhorn #ifdef CONFIG_NUMA 21499a305230SLee Schermerhorn 21509a305230SLee Schermerhorn /* 21519a305230SLee Schermerhorn * node_hstate/s - associate per node hstate attributes, via their kobjects, 215210fbcf4cSKay Sievers * with node devices in node_devices[] using a parallel array. The array 215310fbcf4cSKay Sievers * index of a node device or _hstate == node id. 215410fbcf4cSKay Sievers * This is here to avoid any static dependency of the node device driver, in 21559a305230SLee Schermerhorn * the base kernel, on the hugetlb module. 21569a305230SLee Schermerhorn */ 21579a305230SLee Schermerhorn struct node_hstate { 21589a305230SLee Schermerhorn struct kobject *hugepages_kobj; 21599a305230SLee Schermerhorn struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 21609a305230SLee Schermerhorn }; 21619a305230SLee Schermerhorn struct node_hstate node_hstates[MAX_NUMNODES]; 21629a305230SLee Schermerhorn 21639a305230SLee Schermerhorn /* 216410fbcf4cSKay Sievers * A subset of global hstate attributes for node devices 21659a305230SLee Schermerhorn */ 21669a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = { 21679a305230SLee Schermerhorn &nr_hugepages_attr.attr, 21689a305230SLee Schermerhorn &free_hugepages_attr.attr, 21699a305230SLee Schermerhorn &surplus_hugepages_attr.attr, 21709a305230SLee Schermerhorn NULL, 21719a305230SLee Schermerhorn }; 21729a305230SLee Schermerhorn 21739a305230SLee Schermerhorn static struct attribute_group per_node_hstate_attr_group = { 21749a305230SLee Schermerhorn .attrs = per_node_hstate_attrs, 21759a305230SLee Schermerhorn }; 21769a305230SLee Schermerhorn 21779a305230SLee Schermerhorn /* 217810fbcf4cSKay Sievers * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj. 21799a305230SLee Schermerhorn * Returns node id via non-NULL nidp. 21809a305230SLee Schermerhorn */ 21819a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 21829a305230SLee Schermerhorn { 21839a305230SLee Schermerhorn int nid; 21849a305230SLee Schermerhorn 21859a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) { 21869a305230SLee Schermerhorn struct node_hstate *nhs = &node_hstates[nid]; 21879a305230SLee Schermerhorn int i; 21889a305230SLee Schermerhorn for (i = 0; i < HUGE_MAX_HSTATE; i++) 21899a305230SLee Schermerhorn if (nhs->hstate_kobjs[i] == kobj) { 21909a305230SLee Schermerhorn if (nidp) 21919a305230SLee Schermerhorn *nidp = nid; 21929a305230SLee Schermerhorn return &hstates[i]; 21939a305230SLee Schermerhorn } 21949a305230SLee Schermerhorn } 21959a305230SLee Schermerhorn 21969a305230SLee Schermerhorn BUG(); 21979a305230SLee Schermerhorn return NULL; 21989a305230SLee Schermerhorn } 21999a305230SLee Schermerhorn 22009a305230SLee Schermerhorn /* 220110fbcf4cSKay Sievers * Unregister hstate attributes from a single node device. 22029a305230SLee Schermerhorn * No-op if no hstate attributes attached. 22039a305230SLee Schermerhorn */ 22043cd8b44fSClaudiu Ghioc static void hugetlb_unregister_node(struct node *node) 22059a305230SLee Schermerhorn { 22069a305230SLee Schermerhorn struct hstate *h; 220710fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 22089a305230SLee Schermerhorn 22099a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 22109b5e5d0fSLee Schermerhorn return; /* no hstate attributes */ 22119a305230SLee Schermerhorn 2212972dc4deSAneesh Kumar K.V for_each_hstate(h) { 2213972dc4deSAneesh Kumar K.V int idx = hstate_index(h); 2214972dc4deSAneesh Kumar K.V if (nhs->hstate_kobjs[idx]) { 2215972dc4deSAneesh Kumar K.V kobject_put(nhs->hstate_kobjs[idx]); 2216972dc4deSAneesh Kumar K.V nhs->hstate_kobjs[idx] = NULL; 2217972dc4deSAneesh Kumar K.V } 22189a305230SLee Schermerhorn } 22199a305230SLee Schermerhorn 22209a305230SLee Schermerhorn kobject_put(nhs->hugepages_kobj); 22219a305230SLee Schermerhorn nhs->hugepages_kobj = NULL; 22229a305230SLee Schermerhorn } 22239a305230SLee Schermerhorn 22249a305230SLee Schermerhorn /* 222510fbcf4cSKay Sievers * hugetlb module exit: unregister hstate attributes from node devices 22269a305230SLee Schermerhorn * that have them. 22279a305230SLee Schermerhorn */ 22289a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) 22299a305230SLee Schermerhorn { 22309a305230SLee Schermerhorn int nid; 22319a305230SLee Schermerhorn 22329a305230SLee Schermerhorn /* 223310fbcf4cSKay Sievers * disable node device registrations. 22349a305230SLee Schermerhorn */ 22359a305230SLee Schermerhorn register_hugetlbfs_with_node(NULL, NULL); 22369a305230SLee Schermerhorn 22379a305230SLee Schermerhorn /* 22389a305230SLee Schermerhorn * remove hstate attributes from any nodes that have them. 22399a305230SLee Schermerhorn */ 22409a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) 22418732794bSWen Congyang hugetlb_unregister_node(node_devices[nid]); 22429a305230SLee Schermerhorn } 22439a305230SLee Schermerhorn 22449a305230SLee Schermerhorn /* 224510fbcf4cSKay Sievers * Register hstate attributes for a single node device. 22469a305230SLee Schermerhorn * No-op if attributes already registered. 22479a305230SLee Schermerhorn */ 22483cd8b44fSClaudiu Ghioc static void hugetlb_register_node(struct node *node) 22499a305230SLee Schermerhorn { 22509a305230SLee Schermerhorn struct hstate *h; 225110fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 22529a305230SLee Schermerhorn int err; 22539a305230SLee Schermerhorn 22549a305230SLee Schermerhorn if (nhs->hugepages_kobj) 22559a305230SLee Schermerhorn return; /* already allocated */ 22569a305230SLee Schermerhorn 22579a305230SLee Schermerhorn nhs->hugepages_kobj = kobject_create_and_add("hugepages", 225810fbcf4cSKay Sievers &node->dev.kobj); 22599a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 22609a305230SLee Schermerhorn return; 22619a305230SLee Schermerhorn 22629a305230SLee Schermerhorn for_each_hstate(h) { 22639a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, 22649a305230SLee Schermerhorn nhs->hstate_kobjs, 22659a305230SLee Schermerhorn &per_node_hstate_attr_group); 22669a305230SLee Schermerhorn if (err) { 2267ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s for node %d\n", 226810fbcf4cSKay Sievers h->name, node->dev.id); 22699a305230SLee Schermerhorn hugetlb_unregister_node(node); 22709a305230SLee Schermerhorn break; 22719a305230SLee Schermerhorn } 22729a305230SLee Schermerhorn } 22739a305230SLee Schermerhorn } 22749a305230SLee Schermerhorn 22759a305230SLee Schermerhorn /* 22769b5e5d0fSLee Schermerhorn * hugetlb init time: register hstate attributes for all registered node 227710fbcf4cSKay Sievers * devices of nodes that have memory. All on-line nodes should have 227810fbcf4cSKay Sievers * registered their associated device by this time. 22799a305230SLee Schermerhorn */ 22807d9ca000SLuiz Capitulino static void __init hugetlb_register_all_nodes(void) 22819a305230SLee Schermerhorn { 22829a305230SLee Schermerhorn int nid; 22839a305230SLee Schermerhorn 22848cebfcd0SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 22858732794bSWen Congyang struct node *node = node_devices[nid]; 228610fbcf4cSKay Sievers if (node->dev.id == nid) 22879a305230SLee Schermerhorn hugetlb_register_node(node); 22889a305230SLee Schermerhorn } 22899a305230SLee Schermerhorn 22909a305230SLee Schermerhorn /* 229110fbcf4cSKay Sievers * Let the node device driver know we're here so it can 22929a305230SLee Schermerhorn * [un]register hstate attributes on node hotplug. 22939a305230SLee Schermerhorn */ 22949a305230SLee Schermerhorn register_hugetlbfs_with_node(hugetlb_register_node, 22959a305230SLee Schermerhorn hugetlb_unregister_node); 22969a305230SLee Schermerhorn } 22979a305230SLee Schermerhorn #else /* !CONFIG_NUMA */ 22989a305230SLee Schermerhorn 22999a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 23009a305230SLee Schermerhorn { 23019a305230SLee Schermerhorn BUG(); 23029a305230SLee Schermerhorn if (nidp) 23039a305230SLee Schermerhorn *nidp = -1; 23049a305230SLee Schermerhorn return NULL; 23059a305230SLee Schermerhorn } 23069a305230SLee Schermerhorn 23079a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) { } 23089a305230SLee Schermerhorn 23099a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { } 23109a305230SLee Schermerhorn 23119a305230SLee Schermerhorn #endif 23129a305230SLee Schermerhorn 2313a3437870SNishanth Aravamudan static void __exit hugetlb_exit(void) 2314a3437870SNishanth Aravamudan { 2315a3437870SNishanth Aravamudan struct hstate *h; 2316a3437870SNishanth Aravamudan 23179a305230SLee Schermerhorn hugetlb_unregister_all_nodes(); 23189a305230SLee Schermerhorn 2319a3437870SNishanth Aravamudan for_each_hstate(h) { 2320972dc4deSAneesh Kumar K.V kobject_put(hstate_kobjs[hstate_index(h)]); 2321a3437870SNishanth Aravamudan } 2322a3437870SNishanth Aravamudan 2323a3437870SNishanth Aravamudan kobject_put(hugepages_kobj); 23248382d914SDavidlohr Bueso kfree(htlb_fault_mutex_table); 2325a3437870SNishanth Aravamudan } 2326a3437870SNishanth Aravamudan module_exit(hugetlb_exit); 2327a3437870SNishanth Aravamudan 2328a3437870SNishanth Aravamudan static int __init hugetlb_init(void) 2329a3437870SNishanth Aravamudan { 23308382d914SDavidlohr Bueso int i; 23318382d914SDavidlohr Bueso 2332457c1b27SNishanth Aravamudan if (!hugepages_supported()) 23330ef89d25SBenjamin Herrenschmidt return 0; 2334a3437870SNishanth Aravamudan 2335e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) { 2336e11bfbfcSNick Piggin default_hstate_size = HPAGE_SIZE; 2337e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) 2338a3437870SNishanth Aravamudan hugetlb_add_hstate(HUGETLB_PAGE_ORDER); 2339a3437870SNishanth Aravamudan } 2340972dc4deSAneesh Kumar K.V default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size)); 2341e11bfbfcSNick Piggin if (default_hstate_max_huge_pages) 2342e11bfbfcSNick Piggin default_hstate.max_huge_pages = default_hstate_max_huge_pages; 2343a3437870SNishanth Aravamudan 2344a3437870SNishanth Aravamudan hugetlb_init_hstates(); 2345aa888a74SAndi Kleen gather_bootmem_prealloc(); 2346a3437870SNishanth Aravamudan report_hugepages(); 2347a3437870SNishanth Aravamudan 2348a3437870SNishanth Aravamudan hugetlb_sysfs_init(); 23499a305230SLee Schermerhorn hugetlb_register_all_nodes(); 23507179e7bfSJianguo Wu hugetlb_cgroup_file_init(); 23519a305230SLee Schermerhorn 23528382d914SDavidlohr Bueso #ifdef CONFIG_SMP 23538382d914SDavidlohr Bueso num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); 23548382d914SDavidlohr Bueso #else 23558382d914SDavidlohr Bueso num_fault_mutexes = 1; 23568382d914SDavidlohr Bueso #endif 23578382d914SDavidlohr Bueso htlb_fault_mutex_table = 23588382d914SDavidlohr Bueso kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL); 23598382d914SDavidlohr Bueso BUG_ON(!htlb_fault_mutex_table); 23608382d914SDavidlohr Bueso 23618382d914SDavidlohr Bueso for (i = 0; i < num_fault_mutexes; i++) 23628382d914SDavidlohr Bueso mutex_init(&htlb_fault_mutex_table[i]); 2363a3437870SNishanth Aravamudan return 0; 2364a3437870SNishanth Aravamudan } 2365a3437870SNishanth Aravamudan module_init(hugetlb_init); 2366a3437870SNishanth Aravamudan 2367a3437870SNishanth Aravamudan /* Should be called on processing a hugepagesz=... option */ 2368a3437870SNishanth Aravamudan void __init hugetlb_add_hstate(unsigned order) 2369a3437870SNishanth Aravamudan { 2370a3437870SNishanth Aravamudan struct hstate *h; 23718faa8b07SAndi Kleen unsigned long i; 23728faa8b07SAndi Kleen 2373a3437870SNishanth Aravamudan if (size_to_hstate(PAGE_SIZE << order)) { 2374ffb22af5SAndrew Morton pr_warning("hugepagesz= specified twice, ignoring\n"); 2375a3437870SNishanth Aravamudan return; 2376a3437870SNishanth Aravamudan } 237747d38344SAneesh Kumar K.V BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); 2378a3437870SNishanth Aravamudan BUG_ON(order == 0); 237947d38344SAneesh Kumar K.V h = &hstates[hugetlb_max_hstate++]; 2380a3437870SNishanth Aravamudan h->order = order; 2381a3437870SNishanth Aravamudan h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); 23828faa8b07SAndi Kleen h->nr_huge_pages = 0; 23838faa8b07SAndi Kleen h->free_huge_pages = 0; 23848faa8b07SAndi Kleen for (i = 0; i < MAX_NUMNODES; ++i) 23858faa8b07SAndi Kleen INIT_LIST_HEAD(&h->hugepage_freelists[i]); 23860edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&h->hugepage_activelist); 23878cebfcd0SLai Jiangshan h->next_nid_to_alloc = first_node(node_states[N_MEMORY]); 23888cebfcd0SLai Jiangshan h->next_nid_to_free = first_node(node_states[N_MEMORY]); 2389a3437870SNishanth Aravamudan snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", 2390a3437870SNishanth Aravamudan huge_page_size(h)/1024); 23918faa8b07SAndi Kleen 2392a3437870SNishanth Aravamudan parsed_hstate = h; 2393a3437870SNishanth Aravamudan } 2394a3437870SNishanth Aravamudan 2395e11bfbfcSNick Piggin static int __init hugetlb_nrpages_setup(char *s) 2396a3437870SNishanth Aravamudan { 2397a3437870SNishanth Aravamudan unsigned long *mhp; 23988faa8b07SAndi Kleen static unsigned long *last_mhp; 2399a3437870SNishanth Aravamudan 2400a3437870SNishanth Aravamudan /* 240147d38344SAneesh Kumar K.V * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet, 2402a3437870SNishanth Aravamudan * so this hugepages= parameter goes to the "default hstate". 2403a3437870SNishanth Aravamudan */ 240447d38344SAneesh Kumar K.V if (!hugetlb_max_hstate) 2405a3437870SNishanth Aravamudan mhp = &default_hstate_max_huge_pages; 2406a3437870SNishanth Aravamudan else 2407a3437870SNishanth Aravamudan mhp = &parsed_hstate->max_huge_pages; 2408a3437870SNishanth Aravamudan 24098faa8b07SAndi Kleen if (mhp == last_mhp) { 2410ffb22af5SAndrew Morton pr_warning("hugepages= specified twice without " 24118faa8b07SAndi Kleen "interleaving hugepagesz=, ignoring\n"); 24128faa8b07SAndi Kleen return 1; 24138faa8b07SAndi Kleen } 24148faa8b07SAndi Kleen 2415a3437870SNishanth Aravamudan if (sscanf(s, "%lu", mhp) <= 0) 2416a3437870SNishanth Aravamudan *mhp = 0; 2417a3437870SNishanth Aravamudan 24188faa8b07SAndi Kleen /* 24198faa8b07SAndi Kleen * Global state is always initialized later in hugetlb_init. 24208faa8b07SAndi Kleen * But we need to allocate >= MAX_ORDER hstates here early to still 24218faa8b07SAndi Kleen * use the bootmem allocator. 24228faa8b07SAndi Kleen */ 242347d38344SAneesh Kumar K.V if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER) 24248faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(parsed_hstate); 24258faa8b07SAndi Kleen 24268faa8b07SAndi Kleen last_mhp = mhp; 24278faa8b07SAndi Kleen 2428a3437870SNishanth Aravamudan return 1; 2429a3437870SNishanth Aravamudan } 2430e11bfbfcSNick Piggin __setup("hugepages=", hugetlb_nrpages_setup); 2431e11bfbfcSNick Piggin 2432e11bfbfcSNick Piggin static int __init hugetlb_default_setup(char *s) 2433e11bfbfcSNick Piggin { 2434e11bfbfcSNick Piggin default_hstate_size = memparse(s, &s); 2435e11bfbfcSNick Piggin return 1; 2436e11bfbfcSNick Piggin } 2437e11bfbfcSNick Piggin __setup("default_hugepagesz=", hugetlb_default_setup); 2438a3437870SNishanth Aravamudan 24398a213460SNishanth Aravamudan static unsigned int cpuset_mems_nr(unsigned int *array) 24408a213460SNishanth Aravamudan { 24418a213460SNishanth Aravamudan int node; 24428a213460SNishanth Aravamudan unsigned int nr = 0; 24438a213460SNishanth Aravamudan 24448a213460SNishanth Aravamudan for_each_node_mask(node, cpuset_current_mems_allowed) 24458a213460SNishanth Aravamudan nr += array[node]; 24468a213460SNishanth Aravamudan 24478a213460SNishanth Aravamudan return nr; 24488a213460SNishanth Aravamudan } 24498a213460SNishanth Aravamudan 24508a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL 245106808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy, 245206808b08SLee Schermerhorn struct ctl_table *table, int write, 245306808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 24541da177e4SLinus Torvalds { 2455e5ff2159SAndi Kleen struct hstate *h = &default_hstate; 2456238d3c13SDavid Rientjes unsigned long tmp = h->max_huge_pages; 245708d4a246SMichal Hocko int ret; 2458e5ff2159SAndi Kleen 2459457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2460457c1b27SNishanth Aravamudan return -ENOTSUPP; 2461457c1b27SNishanth Aravamudan 2462e5ff2159SAndi Kleen table->data = &tmp; 2463e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 246408d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 246508d4a246SMichal Hocko if (ret) 246608d4a246SMichal Hocko goto out; 2467e5ff2159SAndi Kleen 2468238d3c13SDavid Rientjes if (write) 2469238d3c13SDavid Rientjes ret = __nr_hugepages_store_common(obey_mempolicy, h, 2470238d3c13SDavid Rientjes NUMA_NO_NODE, tmp, *length); 247108d4a246SMichal Hocko out: 247208d4a246SMichal Hocko return ret; 24731da177e4SLinus Torvalds } 2474396faf03SMel Gorman 247506808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write, 247606808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 247706808b08SLee Schermerhorn { 247806808b08SLee Schermerhorn 247906808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(false, table, write, 248006808b08SLee Schermerhorn buffer, length, ppos); 248106808b08SLee Schermerhorn } 248206808b08SLee Schermerhorn 248306808b08SLee Schermerhorn #ifdef CONFIG_NUMA 248406808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write, 248506808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 248606808b08SLee Schermerhorn { 248706808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(true, table, write, 248806808b08SLee Schermerhorn buffer, length, ppos); 248906808b08SLee Schermerhorn } 249006808b08SLee Schermerhorn #endif /* CONFIG_NUMA */ 249106808b08SLee Schermerhorn 2492a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write, 24938d65af78SAlexey Dobriyan void __user *buffer, 2494a3d0c6aaSNishanth Aravamudan size_t *length, loff_t *ppos) 2495a3d0c6aaSNishanth Aravamudan { 2496a5516438SAndi Kleen struct hstate *h = &default_hstate; 2497e5ff2159SAndi Kleen unsigned long tmp; 249808d4a246SMichal Hocko int ret; 2499e5ff2159SAndi Kleen 2500457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2501457c1b27SNishanth Aravamudan return -ENOTSUPP; 2502457c1b27SNishanth Aravamudan 2503e5ff2159SAndi Kleen tmp = h->nr_overcommit_huge_pages; 2504e5ff2159SAndi Kleen 2505bae7f4aeSLuiz Capitulino if (write && hstate_is_gigantic(h)) 2506adbe8726SEric B Munson return -EINVAL; 2507adbe8726SEric B Munson 2508e5ff2159SAndi Kleen table->data = &tmp; 2509e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 251008d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 251108d4a246SMichal Hocko if (ret) 251208d4a246SMichal Hocko goto out; 2513e5ff2159SAndi Kleen 2514e5ff2159SAndi Kleen if (write) { 2515064d9efeSNishanth Aravamudan spin_lock(&hugetlb_lock); 2516e5ff2159SAndi Kleen h->nr_overcommit_huge_pages = tmp; 2517a3d0c6aaSNishanth Aravamudan spin_unlock(&hugetlb_lock); 2518e5ff2159SAndi Kleen } 251908d4a246SMichal Hocko out: 252008d4a246SMichal Hocko return ret; 2521a3d0c6aaSNishanth Aravamudan } 2522a3d0c6aaSNishanth Aravamudan 25231da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 25241da177e4SLinus Torvalds 2525e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m) 25261da177e4SLinus Torvalds { 2527a5516438SAndi Kleen struct hstate *h = &default_hstate; 2528457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2529457c1b27SNishanth Aravamudan return; 2530e1759c21SAlexey Dobriyan seq_printf(m, 25311da177e4SLinus Torvalds "HugePages_Total: %5lu\n" 25321da177e4SLinus Torvalds "HugePages_Free: %5lu\n" 2533b45b5bd6SDavid Gibson "HugePages_Rsvd: %5lu\n" 25347893d1d5SAdam Litke "HugePages_Surp: %5lu\n" 25354f98a2feSRik van Riel "Hugepagesize: %8lu kB\n", 2536a5516438SAndi Kleen h->nr_huge_pages, 2537a5516438SAndi Kleen h->free_huge_pages, 2538a5516438SAndi Kleen h->resv_huge_pages, 2539a5516438SAndi Kleen h->surplus_huge_pages, 2540a5516438SAndi Kleen 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds 25431da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf) 25441da177e4SLinus Torvalds { 2545a5516438SAndi Kleen struct hstate *h = &default_hstate; 2546457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2547457c1b27SNishanth Aravamudan return 0; 25481da177e4SLinus Torvalds return sprintf(buf, 25491da177e4SLinus Torvalds "Node %d HugePages_Total: %5u\n" 2550a1de0919SNishanth Aravamudan "Node %d HugePages_Free: %5u\n" 2551a1de0919SNishanth Aravamudan "Node %d HugePages_Surp: %5u\n", 2552a5516438SAndi Kleen nid, h->nr_huge_pages_node[nid], 2553a5516438SAndi Kleen nid, h->free_huge_pages_node[nid], 2554a5516438SAndi Kleen nid, h->surplus_huge_pages_node[nid]); 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds 2557949f7ec5SDavid Rientjes void hugetlb_show_meminfo(void) 2558949f7ec5SDavid Rientjes { 2559949f7ec5SDavid Rientjes struct hstate *h; 2560949f7ec5SDavid Rientjes int nid; 2561949f7ec5SDavid Rientjes 2562457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2563457c1b27SNishanth Aravamudan return; 2564457c1b27SNishanth Aravamudan 2565949f7ec5SDavid Rientjes for_each_node_state(nid, N_MEMORY) 2566949f7ec5SDavid Rientjes for_each_hstate(h) 2567949f7ec5SDavid Rientjes pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", 2568949f7ec5SDavid Rientjes nid, 2569949f7ec5SDavid Rientjes h->nr_huge_pages_node[nid], 2570949f7ec5SDavid Rientjes h->free_huge_pages_node[nid], 2571949f7ec5SDavid Rientjes h->surplus_huge_pages_node[nid], 2572949f7ec5SDavid Rientjes 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 2573949f7ec5SDavid Rientjes } 2574949f7ec5SDavid Rientjes 25751da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 25761da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void) 25771da177e4SLinus Torvalds { 2578d0028588SWanpeng Li struct hstate *h; 2579d0028588SWanpeng Li unsigned long nr_total_pages = 0; 2580d0028588SWanpeng Li 2581d0028588SWanpeng Li for_each_hstate(h) 2582d0028588SWanpeng Li nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); 2583d0028588SWanpeng Li return nr_total_pages; 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds 2586a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta) 2587fc1b8a73SMel Gorman { 2588fc1b8a73SMel Gorman int ret = -ENOMEM; 2589fc1b8a73SMel Gorman 2590fc1b8a73SMel Gorman spin_lock(&hugetlb_lock); 2591fc1b8a73SMel Gorman /* 2592fc1b8a73SMel Gorman * When cpuset is configured, it breaks the strict hugetlb page 2593fc1b8a73SMel Gorman * reservation as the accounting is done on a global variable. Such 2594fc1b8a73SMel Gorman * reservation is completely rubbish in the presence of cpuset because 2595fc1b8a73SMel Gorman * the reservation is not checked against page availability for the 2596fc1b8a73SMel Gorman * current cpuset. Application can still potentially OOM'ed by kernel 2597fc1b8a73SMel Gorman * with lack of free htlb page in cpuset that the task is in. 2598fc1b8a73SMel Gorman * Attempt to enforce strict accounting with cpuset is almost 2599fc1b8a73SMel Gorman * impossible (or too ugly) because cpuset is too fluid that 2600fc1b8a73SMel Gorman * task or memory node can be dynamically moved between cpusets. 2601fc1b8a73SMel Gorman * 2602fc1b8a73SMel Gorman * The change of semantics for shared hugetlb mapping with cpuset is 2603fc1b8a73SMel Gorman * undesirable. However, in order to preserve some of the semantics, 2604fc1b8a73SMel Gorman * we fall back to check against current free page availability as 2605fc1b8a73SMel Gorman * a best attempt and hopefully to minimize the impact of changing 2606fc1b8a73SMel Gorman * semantics that cpuset has. 2607fc1b8a73SMel Gorman */ 2608fc1b8a73SMel Gorman if (delta > 0) { 2609a5516438SAndi Kleen if (gather_surplus_pages(h, delta) < 0) 2610fc1b8a73SMel Gorman goto out; 2611fc1b8a73SMel Gorman 2612a5516438SAndi Kleen if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { 2613a5516438SAndi Kleen return_unused_surplus_pages(h, delta); 2614fc1b8a73SMel Gorman goto out; 2615fc1b8a73SMel Gorman } 2616fc1b8a73SMel Gorman } 2617fc1b8a73SMel Gorman 2618fc1b8a73SMel Gorman ret = 0; 2619fc1b8a73SMel Gorman if (delta < 0) 2620a5516438SAndi Kleen return_unused_surplus_pages(h, (unsigned long) -delta); 2621fc1b8a73SMel Gorman 2622fc1b8a73SMel Gorman out: 2623fc1b8a73SMel Gorman spin_unlock(&hugetlb_lock); 2624fc1b8a73SMel Gorman return ret; 2625fc1b8a73SMel Gorman } 2626fc1b8a73SMel Gorman 262784afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma) 262884afd99bSAndy Whitcroft { 2629f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 263084afd99bSAndy Whitcroft 263184afd99bSAndy Whitcroft /* 263284afd99bSAndy Whitcroft * This new VMA should share its siblings reservation map if present. 263384afd99bSAndy Whitcroft * The VMA will only ever have a valid reservation map pointer where 263484afd99bSAndy Whitcroft * it is being copied for another still existing VMA. As that VMA 263525985edcSLucas De Marchi * has a reference to the reservation map it cannot disappear until 263684afd99bSAndy Whitcroft * after this open call completes. It is therefore safe to take a 263784afd99bSAndy Whitcroft * new reference here without additional locking. 263884afd99bSAndy Whitcroft */ 26394e35f483SJoonsoo Kim if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 2640f522c3acSJoonsoo Kim kref_get(&resv->refs); 264184afd99bSAndy Whitcroft } 264284afd99bSAndy Whitcroft 2643a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma) 2644a1e78772SMel Gorman { 2645a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2646f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 264790481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 26484e35f483SJoonsoo Kim unsigned long reserve, start, end; 26491c5ecae3SMike Kravetz long gbl_reserve; 265084afd99bSAndy Whitcroft 26514e35f483SJoonsoo Kim if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 26524e35f483SJoonsoo Kim return; 26534e35f483SJoonsoo Kim 2654a5516438SAndi Kleen start = vma_hugecache_offset(h, vma, vma->vm_start); 2655a5516438SAndi Kleen end = vma_hugecache_offset(h, vma, vma->vm_end); 265684afd99bSAndy Whitcroft 26574e35f483SJoonsoo Kim reserve = (end - start) - region_count(resv, start, end); 265884afd99bSAndy Whitcroft 2659f031dd27SJoonsoo Kim kref_put(&resv->refs, resv_map_release); 266084afd99bSAndy Whitcroft 26617251ff78SAdam Litke if (reserve) { 26621c5ecae3SMike Kravetz /* 26631c5ecae3SMike Kravetz * Decrement reserve counts. The global reserve count may be 26641c5ecae3SMike Kravetz * adjusted if the subpool has a minimum size. 26651c5ecae3SMike Kravetz */ 26661c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, reserve); 26671c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 26687251ff78SAdam Litke } 2669a1e78772SMel Gorman } 2670a1e78772SMel Gorman 26711da177e4SLinus Torvalds /* 26721da177e4SLinus Torvalds * We cannot handle pagefaults against hugetlb pages at all. They cause 26731da177e4SLinus Torvalds * handle_mm_fault() to try to instantiate regular-sized pages in the 26741da177e4SLinus Torvalds * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get 26751da177e4SLinus Torvalds * this far. 26761da177e4SLinus Torvalds */ 2677d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 26781da177e4SLinus Torvalds { 26791da177e4SLinus Torvalds BUG(); 2680d0217ac0SNick Piggin return 0; 26811da177e4SLinus Torvalds } 26821da177e4SLinus Torvalds 2683f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = { 2684d0217ac0SNick Piggin .fault = hugetlb_vm_op_fault, 268584afd99bSAndy Whitcroft .open = hugetlb_vm_op_open, 2686a1e78772SMel Gorman .close = hugetlb_vm_op_close, 26871da177e4SLinus Torvalds }; 26881da177e4SLinus Torvalds 26891e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, 26901e8f889bSDavid Gibson int writable) 269163551ae0SDavid Gibson { 269263551ae0SDavid Gibson pte_t entry; 269363551ae0SDavid Gibson 26941e8f889bSDavid Gibson if (writable) { 2695106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, 2696106c992aSGerald Schaefer vma->vm_page_prot))); 269763551ae0SDavid Gibson } else { 2698106c992aSGerald Schaefer entry = huge_pte_wrprotect(mk_huge_pte(page, 2699106c992aSGerald Schaefer vma->vm_page_prot)); 270063551ae0SDavid Gibson } 270163551ae0SDavid Gibson entry = pte_mkyoung(entry); 270263551ae0SDavid Gibson entry = pte_mkhuge(entry); 2703d9ed9faaSChris Metcalf entry = arch_make_huge_pte(entry, vma, page, writable); 270463551ae0SDavid Gibson 270563551ae0SDavid Gibson return entry; 270663551ae0SDavid Gibson } 270763551ae0SDavid Gibson 27081e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma, 27091e8f889bSDavid Gibson unsigned long address, pte_t *ptep) 27101e8f889bSDavid Gibson { 27111e8f889bSDavid Gibson pte_t entry; 27121e8f889bSDavid Gibson 2713106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); 271432f84528SChris Forbes if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) 27154b3073e1SRussell King update_mmu_cache(vma, address, ptep); 27161e8f889bSDavid Gibson } 27171e8f889bSDavid Gibson 27184a705fefSNaoya Horiguchi static int is_hugetlb_entry_migration(pte_t pte) 27194a705fefSNaoya Horiguchi { 27204a705fefSNaoya Horiguchi swp_entry_t swp; 27214a705fefSNaoya Horiguchi 27224a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 27234a705fefSNaoya Horiguchi return 0; 27244a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 27254a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_migration_entry(swp)) 27264a705fefSNaoya Horiguchi return 1; 27274a705fefSNaoya Horiguchi else 27284a705fefSNaoya Horiguchi return 0; 27294a705fefSNaoya Horiguchi } 27304a705fefSNaoya Horiguchi 27314a705fefSNaoya Horiguchi static int is_hugetlb_entry_hwpoisoned(pte_t pte) 27324a705fefSNaoya Horiguchi { 27334a705fefSNaoya Horiguchi swp_entry_t swp; 27344a705fefSNaoya Horiguchi 27354a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 27364a705fefSNaoya Horiguchi return 0; 27374a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 27384a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_hwpoison_entry(swp)) 27394a705fefSNaoya Horiguchi return 1; 27404a705fefSNaoya Horiguchi else 27414a705fefSNaoya Horiguchi return 0; 27424a705fefSNaoya Horiguchi } 27431e8f889bSDavid Gibson 274463551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 274563551ae0SDavid Gibson struct vm_area_struct *vma) 274663551ae0SDavid Gibson { 274763551ae0SDavid Gibson pte_t *src_pte, *dst_pte, entry; 274863551ae0SDavid Gibson struct page *ptepage; 27491c59827dSHugh Dickins unsigned long addr; 27501e8f889bSDavid Gibson int cow; 2751a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2752a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 2753e8569dd2SAndreas Sandberg unsigned long mmun_start; /* For mmu_notifiers */ 2754e8569dd2SAndreas Sandberg unsigned long mmun_end; /* For mmu_notifiers */ 2755e8569dd2SAndreas Sandberg int ret = 0; 27561e8f889bSDavid Gibson 27571e8f889bSDavid Gibson cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 275863551ae0SDavid Gibson 2759e8569dd2SAndreas Sandberg mmun_start = vma->vm_start; 2760e8569dd2SAndreas Sandberg mmun_end = vma->vm_end; 2761e8569dd2SAndreas Sandberg if (cow) 2762e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end); 2763e8569dd2SAndreas Sandberg 2764a5516438SAndi Kleen for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { 2765cb900f41SKirill A. Shutemov spinlock_t *src_ptl, *dst_ptl; 2766c74df32cSHugh Dickins src_pte = huge_pte_offset(src, addr); 2767c74df32cSHugh Dickins if (!src_pte) 2768c74df32cSHugh Dickins continue; 2769a5516438SAndi Kleen dst_pte = huge_pte_alloc(dst, addr, sz); 2770e8569dd2SAndreas Sandberg if (!dst_pte) { 2771e8569dd2SAndreas Sandberg ret = -ENOMEM; 2772e8569dd2SAndreas Sandberg break; 2773e8569dd2SAndreas Sandberg } 2774c5c99429SLarry Woodman 2775c5c99429SLarry Woodman /* If the pagetables are shared don't copy or take references */ 2776c5c99429SLarry Woodman if (dst_pte == src_pte) 2777c5c99429SLarry Woodman continue; 2778c5c99429SLarry Woodman 2779cb900f41SKirill A. Shutemov dst_ptl = huge_pte_lock(h, dst, dst_pte); 2780cb900f41SKirill A. Shutemov src_ptl = huge_pte_lockptr(h, src, src_pte); 2781cb900f41SKirill A. Shutemov spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 27824a705fefSNaoya Horiguchi entry = huge_ptep_get(src_pte); 27834a705fefSNaoya Horiguchi if (huge_pte_none(entry)) { /* skip none entry */ 27844a705fefSNaoya Horiguchi ; 27854a705fefSNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_migration(entry) || 27864a705fefSNaoya Horiguchi is_hugetlb_entry_hwpoisoned(entry))) { 27874a705fefSNaoya Horiguchi swp_entry_t swp_entry = pte_to_swp_entry(entry); 27884a705fefSNaoya Horiguchi 27894a705fefSNaoya Horiguchi if (is_write_migration_entry(swp_entry) && cow) { 27904a705fefSNaoya Horiguchi /* 27914a705fefSNaoya Horiguchi * COW mappings require pages in both 27924a705fefSNaoya Horiguchi * parent and child to be set to read. 27934a705fefSNaoya Horiguchi */ 27944a705fefSNaoya Horiguchi make_migration_entry_read(&swp_entry); 27954a705fefSNaoya Horiguchi entry = swp_entry_to_pte(swp_entry); 27964a705fefSNaoya Horiguchi set_huge_pte_at(src, addr, src_pte, entry); 27974a705fefSNaoya Horiguchi } 27984a705fefSNaoya Horiguchi set_huge_pte_at(dst, addr, dst_pte, entry); 27994a705fefSNaoya Horiguchi } else { 280034ee645eSJoerg Roedel if (cow) { 28017f2e9525SGerald Schaefer huge_ptep_set_wrprotect(src, addr, src_pte); 280234ee645eSJoerg Roedel mmu_notifier_invalidate_range(src, mmun_start, 280334ee645eSJoerg Roedel mmun_end); 280434ee645eSJoerg Roedel } 28050253d634SNaoya Horiguchi entry = huge_ptep_get(src_pte); 280663551ae0SDavid Gibson ptepage = pte_page(entry); 280763551ae0SDavid Gibson get_page(ptepage); 28080fe6e20bSNaoya Horiguchi page_dup_rmap(ptepage); 280963551ae0SDavid Gibson set_huge_pte_at(dst, addr, dst_pte, entry); 28101c59827dSHugh Dickins } 2811cb900f41SKirill A. Shutemov spin_unlock(src_ptl); 2812cb900f41SKirill A. Shutemov spin_unlock(dst_ptl); 281363551ae0SDavid Gibson } 281463551ae0SDavid Gibson 2815e8569dd2SAndreas Sandberg if (cow) 2816e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end); 2817e8569dd2SAndreas Sandberg 2818e8569dd2SAndreas Sandberg return ret; 281963551ae0SDavid Gibson } 282063551ae0SDavid Gibson 282124669e58SAneesh Kumar K.V void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, 282224669e58SAneesh Kumar K.V unsigned long start, unsigned long end, 282324669e58SAneesh Kumar K.V struct page *ref_page) 282463551ae0SDavid Gibson { 282524669e58SAneesh Kumar K.V int force_flush = 0; 282663551ae0SDavid Gibson struct mm_struct *mm = vma->vm_mm; 282763551ae0SDavid Gibson unsigned long address; 2828c7546f8fSDavid Gibson pte_t *ptep; 282963551ae0SDavid Gibson pte_t pte; 2830cb900f41SKirill A. Shutemov spinlock_t *ptl; 283163551ae0SDavid Gibson struct page *page; 2832a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2833a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 28342ec74c3eSSagi Grimberg const unsigned long mmun_start = start; /* For mmu_notifiers */ 28352ec74c3eSSagi Grimberg const unsigned long mmun_end = end; /* For mmu_notifiers */ 2836a5516438SAndi Kleen 283763551ae0SDavid Gibson WARN_ON(!is_vm_hugetlb_page(vma)); 2838a5516438SAndi Kleen BUG_ON(start & ~huge_page_mask(h)); 2839a5516438SAndi Kleen BUG_ON(end & ~huge_page_mask(h)); 284063551ae0SDavid Gibson 284124669e58SAneesh Kumar K.V tlb_start_vma(tlb, vma); 28422ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 2843569f48b8SHillf Danton address = start; 284424669e58SAneesh Kumar K.V again: 2845569f48b8SHillf Danton for (; address < end; address += sz) { 2846c7546f8fSDavid Gibson ptep = huge_pte_offset(mm, address); 2847c7546f8fSDavid Gibson if (!ptep) 2848c7546f8fSDavid Gibson continue; 2849c7546f8fSDavid Gibson 2850cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 285139dde65cSChen, Kenneth W if (huge_pmd_unshare(mm, &address, ptep)) 2852cb900f41SKirill A. Shutemov goto unlock; 285339dde65cSChen, Kenneth W 28546629326bSHillf Danton pte = huge_ptep_get(ptep); 28556629326bSHillf Danton if (huge_pte_none(pte)) 2856cb900f41SKirill A. Shutemov goto unlock; 28576629326bSHillf Danton 28586629326bSHillf Danton /* 28599fbc1f63SNaoya Horiguchi * Migrating hugepage or HWPoisoned hugepage is already 28609fbc1f63SNaoya Horiguchi * unmapped and its refcount is dropped, so just clear pte here. 28616629326bSHillf Danton */ 28629fbc1f63SNaoya Horiguchi if (unlikely(!pte_present(pte))) { 2863106c992aSGerald Schaefer huge_pte_clear(mm, address, ptep); 2864cb900f41SKirill A. Shutemov goto unlock; 28658c4894c6SNaoya Horiguchi } 28666629326bSHillf Danton 28676629326bSHillf Danton page = pte_page(pte); 286804f2cbe3SMel Gorman /* 286904f2cbe3SMel Gorman * If a reference page is supplied, it is because a specific 287004f2cbe3SMel Gorman * page is being unmapped, not a range. Ensure the page we 287104f2cbe3SMel Gorman * are about to unmap is the actual page of interest. 287204f2cbe3SMel Gorman */ 287304f2cbe3SMel Gorman if (ref_page) { 287404f2cbe3SMel Gorman if (page != ref_page) 2875cb900f41SKirill A. Shutemov goto unlock; 287604f2cbe3SMel Gorman 287704f2cbe3SMel Gorman /* 287804f2cbe3SMel Gorman * Mark the VMA as having unmapped its page so that 287904f2cbe3SMel Gorman * future faults in this VMA will fail rather than 288004f2cbe3SMel Gorman * looking like data was lost 288104f2cbe3SMel Gorman */ 288204f2cbe3SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); 288304f2cbe3SMel Gorman } 288404f2cbe3SMel Gorman 2885c7546f8fSDavid Gibson pte = huge_ptep_get_and_clear(mm, address, ptep); 288624669e58SAneesh Kumar K.V tlb_remove_tlb_entry(tlb, ptep, address); 2887106c992aSGerald Schaefer if (huge_pte_dirty(pte)) 28886649a386SKen Chen set_page_dirty(page); 28899e81130bSHillf Danton 289024669e58SAneesh Kumar K.V page_remove_rmap(page); 289124669e58SAneesh Kumar K.V force_flush = !__tlb_remove_page(tlb, page); 2892cb900f41SKirill A. Shutemov if (force_flush) { 2893569f48b8SHillf Danton address += sz; 2894cb900f41SKirill A. Shutemov spin_unlock(ptl); 28959e81130bSHillf Danton break; 289663551ae0SDavid Gibson } 2897cb900f41SKirill A. Shutemov /* Bail out after unmapping reference page if supplied */ 2898cb900f41SKirill A. Shutemov if (ref_page) { 2899cb900f41SKirill A. Shutemov spin_unlock(ptl); 2900cb900f41SKirill A. Shutemov break; 2901cb900f41SKirill A. Shutemov } 2902cb900f41SKirill A. Shutemov unlock: 2903cb900f41SKirill A. Shutemov spin_unlock(ptl); 2904cb900f41SKirill A. Shutemov } 290524669e58SAneesh Kumar K.V /* 290624669e58SAneesh Kumar K.V * mmu_gather ran out of room to batch pages, we break out of 290724669e58SAneesh Kumar K.V * the PTE lock to avoid doing the potential expensive TLB invalidate 290824669e58SAneesh Kumar K.V * and page-free while holding it. 290924669e58SAneesh Kumar K.V */ 291024669e58SAneesh Kumar K.V if (force_flush) { 291124669e58SAneesh Kumar K.V force_flush = 0; 291224669e58SAneesh Kumar K.V tlb_flush_mmu(tlb); 291324669e58SAneesh Kumar K.V if (address < end && !ref_page) 291424669e58SAneesh Kumar K.V goto again; 2915fe1668aeSChen, Kenneth W } 29162ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 291724669e58SAneesh Kumar K.V tlb_end_vma(tlb, vma); 29181da177e4SLinus Torvalds } 291963551ae0SDavid Gibson 2920d833352aSMel Gorman void __unmap_hugepage_range_final(struct mmu_gather *tlb, 2921d833352aSMel Gorman struct vm_area_struct *vma, unsigned long start, 2922d833352aSMel Gorman unsigned long end, struct page *ref_page) 2923d833352aSMel Gorman { 2924d833352aSMel Gorman __unmap_hugepage_range(tlb, vma, start, end, ref_page); 2925d833352aSMel Gorman 2926d833352aSMel Gorman /* 2927d833352aSMel Gorman * Clear this flag so that x86's huge_pmd_share page_table_shareable 2928d833352aSMel Gorman * test will fail on a vma being torn down, and not grab a page table 2929d833352aSMel Gorman * on its way out. We're lucky that the flag has such an appropriate 2930d833352aSMel Gorman * name, and can in fact be safely cleared here. We could clear it 2931d833352aSMel Gorman * before the __unmap_hugepage_range above, but all that's necessary 2932c8c06efaSDavidlohr Bueso * is to clear it before releasing the i_mmap_rwsem. This works 2933d833352aSMel Gorman * because in the context this is called, the VMA is about to be 2934c8c06efaSDavidlohr Bueso * destroyed and the i_mmap_rwsem is held. 2935d833352aSMel Gorman */ 2936d833352aSMel Gorman vma->vm_flags &= ~VM_MAYSHARE; 2937d833352aSMel Gorman } 2938d833352aSMel Gorman 2939502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 294004f2cbe3SMel Gorman unsigned long end, struct page *ref_page) 2941502717f4SChen, Kenneth W { 294224669e58SAneesh Kumar K.V struct mm_struct *mm; 294324669e58SAneesh Kumar K.V struct mmu_gather tlb; 294424669e58SAneesh Kumar K.V 294524669e58SAneesh Kumar K.V mm = vma->vm_mm; 294624669e58SAneesh Kumar K.V 29472b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, start, end); 294824669e58SAneesh Kumar K.V __unmap_hugepage_range(&tlb, vma, start, end, ref_page); 294924669e58SAneesh Kumar K.V tlb_finish_mmu(&tlb, start, end); 2950502717f4SChen, Kenneth W } 2951502717f4SChen, Kenneth W 295204f2cbe3SMel Gorman /* 295304f2cbe3SMel Gorman * This is called when the original mapper is failing to COW a MAP_PRIVATE 295404f2cbe3SMel Gorman * mappping it owns the reserve page for. The intention is to unmap the page 295504f2cbe3SMel Gorman * from other VMAs and let the children be SIGKILLed if they are faulting the 295604f2cbe3SMel Gorman * same region. 295704f2cbe3SMel Gorman */ 29582f4612afSDavidlohr Bueso static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, 29592a4b3dedSHarvey Harrison struct page *page, unsigned long address) 296004f2cbe3SMel Gorman { 29617526674dSAdam Litke struct hstate *h = hstate_vma(vma); 296204f2cbe3SMel Gorman struct vm_area_struct *iter_vma; 296304f2cbe3SMel Gorman struct address_space *mapping; 296404f2cbe3SMel Gorman pgoff_t pgoff; 296504f2cbe3SMel Gorman 296604f2cbe3SMel Gorman /* 296704f2cbe3SMel Gorman * vm_pgoff is in PAGE_SIZE units, hence the different calculation 296804f2cbe3SMel Gorman * from page cache lookup which is in HPAGE_SIZE units. 296904f2cbe3SMel Gorman */ 29707526674dSAdam Litke address = address & huge_page_mask(h); 297136e4f20aSMichal Hocko pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + 297236e4f20aSMichal Hocko vma->vm_pgoff; 2973496ad9aaSAl Viro mapping = file_inode(vma->vm_file)->i_mapping; 297404f2cbe3SMel Gorman 29754eb2b1dcSMel Gorman /* 29764eb2b1dcSMel Gorman * Take the mapping lock for the duration of the table walk. As 29774eb2b1dcSMel Gorman * this mapping should be shared between all the VMAs, 29784eb2b1dcSMel Gorman * __unmap_hugepage_range() is called as the lock is already held 29794eb2b1dcSMel Gorman */ 298083cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 29816b2dbba8SMichel Lespinasse vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { 298204f2cbe3SMel Gorman /* Do not unmap the current VMA */ 298304f2cbe3SMel Gorman if (iter_vma == vma) 298404f2cbe3SMel Gorman continue; 298504f2cbe3SMel Gorman 298604f2cbe3SMel Gorman /* 298704f2cbe3SMel Gorman * Unmap the page from other VMAs without their own reserves. 298804f2cbe3SMel Gorman * They get marked to be SIGKILLed if they fault in these 298904f2cbe3SMel Gorman * areas. This is because a future no-page fault on this VMA 299004f2cbe3SMel Gorman * could insert a zeroed page instead of the data existing 299104f2cbe3SMel Gorman * from the time of fork. This would look like data corruption 299204f2cbe3SMel Gorman */ 299304f2cbe3SMel Gorman if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) 299424669e58SAneesh Kumar K.V unmap_hugepage_range(iter_vma, address, 299524669e58SAneesh Kumar K.V address + huge_page_size(h), page); 299604f2cbe3SMel Gorman } 299783cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 299804f2cbe3SMel Gorman } 299904f2cbe3SMel Gorman 30000fe6e20bSNaoya Horiguchi /* 30010fe6e20bSNaoya Horiguchi * Hugetlb_cow() should be called with page lock of the original hugepage held. 3002ef009b25SMichal Hocko * Called with hugetlb_instantiation_mutex held and pte_page locked so we 3003ef009b25SMichal Hocko * cannot race with other handlers or page migration. 3004ef009b25SMichal Hocko * Keep the pte_same checks anyway to make transition from the mutex easier. 30050fe6e20bSNaoya Horiguchi */ 30061e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, 300704f2cbe3SMel Gorman unsigned long address, pte_t *ptep, pte_t pte, 3008cb900f41SKirill A. Shutemov struct page *pagecache_page, spinlock_t *ptl) 30091e8f889bSDavid Gibson { 3010a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 30111e8f889bSDavid Gibson struct page *old_page, *new_page; 3012ad4404a2SDavidlohr Bueso int ret = 0, outside_reserve = 0; 30132ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 30142ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 30151e8f889bSDavid Gibson 30161e8f889bSDavid Gibson old_page = pte_page(pte); 30171e8f889bSDavid Gibson 301804f2cbe3SMel Gorman retry_avoidcopy: 30191e8f889bSDavid Gibson /* If no-one else is actually using this page, avoid the copy 30201e8f889bSDavid Gibson * and just make the page writable */ 302137a2140dSJoonsoo Kim if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { 30220fe6e20bSNaoya Horiguchi page_move_anon_rmap(old_page, vma, address); 30231e8f889bSDavid Gibson set_huge_ptep_writable(vma, address, ptep); 302483c54070SNick Piggin return 0; 30251e8f889bSDavid Gibson } 30261e8f889bSDavid Gibson 302704f2cbe3SMel Gorman /* 302804f2cbe3SMel Gorman * If the process that created a MAP_PRIVATE mapping is about to 302904f2cbe3SMel Gorman * perform a COW due to a shared page count, attempt to satisfy 303004f2cbe3SMel Gorman * the allocation without using the existing reserves. The pagecache 303104f2cbe3SMel Gorman * page is used to determine if the reserve at this address was 303204f2cbe3SMel Gorman * consumed or not. If reserves were used, a partial faulted mapping 303304f2cbe3SMel Gorman * at the time of fork() could consume its reserves on COW instead 303404f2cbe3SMel Gorman * of the full address range. 303504f2cbe3SMel Gorman */ 30365944d011SJoonsoo Kim if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && 303704f2cbe3SMel Gorman old_page != pagecache_page) 303804f2cbe3SMel Gorman outside_reserve = 1; 303904f2cbe3SMel Gorman 30401e8f889bSDavid Gibson page_cache_get(old_page); 3041b76c8cfbSLarry Woodman 3042ad4404a2SDavidlohr Bueso /* 3043ad4404a2SDavidlohr Bueso * Drop page table lock as buddy allocator may be called. It will 3044ad4404a2SDavidlohr Bueso * be acquired again before returning to the caller, as expected. 3045ad4404a2SDavidlohr Bueso */ 3046cb900f41SKirill A. Shutemov spin_unlock(ptl); 304704f2cbe3SMel Gorman new_page = alloc_huge_page(vma, address, outside_reserve); 30481e8f889bSDavid Gibson 30492fc39cecSAdam Litke if (IS_ERR(new_page)) { 305004f2cbe3SMel Gorman /* 305104f2cbe3SMel Gorman * If a process owning a MAP_PRIVATE mapping fails to COW, 305204f2cbe3SMel Gorman * it is due to references held by a child and an insufficient 305304f2cbe3SMel Gorman * huge page pool. To guarantee the original mappers 305404f2cbe3SMel Gorman * reliability, unmap the page from child processes. The child 305504f2cbe3SMel Gorman * may get SIGKILLed if it later faults. 305604f2cbe3SMel Gorman */ 305704f2cbe3SMel Gorman if (outside_reserve) { 3058ad4404a2SDavidlohr Bueso page_cache_release(old_page); 305904f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 30602f4612afSDavidlohr Bueso unmap_ref_private(mm, vma, old_page, address); 306104f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 3062cb900f41SKirill A. Shutemov spin_lock(ptl); 3063a734bcc8SHillf Danton ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 3064a9af0c5dSNaoya Horiguchi if (likely(ptep && 3065a9af0c5dSNaoya Horiguchi pte_same(huge_ptep_get(ptep), pte))) 306604f2cbe3SMel Gorman goto retry_avoidcopy; 3067a734bcc8SHillf Danton /* 3068cb900f41SKirill A. Shutemov * race occurs while re-acquiring page table 3069cb900f41SKirill A. Shutemov * lock, and our job is done. 3070a734bcc8SHillf Danton */ 3071a734bcc8SHillf Danton return 0; 307204f2cbe3SMel Gorman } 307304f2cbe3SMel Gorman 3074ad4404a2SDavidlohr Bueso ret = (PTR_ERR(new_page) == -ENOMEM) ? 3075ad4404a2SDavidlohr Bueso VM_FAULT_OOM : VM_FAULT_SIGBUS; 3076ad4404a2SDavidlohr Bueso goto out_release_old; 30771e8f889bSDavid Gibson } 30781e8f889bSDavid Gibson 30790fe6e20bSNaoya Horiguchi /* 30800fe6e20bSNaoya Horiguchi * When the original hugepage is shared one, it does not have 30810fe6e20bSNaoya Horiguchi * anon_vma prepared. 30820fe6e20bSNaoya Horiguchi */ 308344e2aa93SDean Nelson if (unlikely(anon_vma_prepare(vma))) { 3084ad4404a2SDavidlohr Bueso ret = VM_FAULT_OOM; 3085ad4404a2SDavidlohr Bueso goto out_release_all; 308644e2aa93SDean Nelson } 30870fe6e20bSNaoya Horiguchi 308847ad8475SAndrea Arcangeli copy_user_huge_page(new_page, old_page, address, vma, 308947ad8475SAndrea Arcangeli pages_per_huge_page(h)); 30900ed361deSNick Piggin __SetPageUptodate(new_page); 3091bcc54222SNaoya Horiguchi set_page_huge_active(new_page); 30921e8f889bSDavid Gibson 30932ec74c3eSSagi Grimberg mmun_start = address & huge_page_mask(h); 30942ec74c3eSSagi Grimberg mmun_end = mmun_start + huge_page_size(h); 30952ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 3096ad4404a2SDavidlohr Bueso 3097b76c8cfbSLarry Woodman /* 3098cb900f41SKirill A. Shutemov * Retake the page table lock to check for racing updates 3099b76c8cfbSLarry Woodman * before the page tables are altered 3100b76c8cfbSLarry Woodman */ 3101cb900f41SKirill A. Shutemov spin_lock(ptl); 3102a5516438SAndi Kleen ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 3103a9af0c5dSNaoya Horiguchi if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { 310407443a85SJoonsoo Kim ClearPagePrivate(new_page); 310507443a85SJoonsoo Kim 31061e8f889bSDavid Gibson /* Break COW */ 31078fe627ecSGerald Schaefer huge_ptep_clear_flush(vma, address, ptep); 310834ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, mmun_start, mmun_end); 31091e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, 31101e8f889bSDavid Gibson make_huge_pte(vma, new_page, 1)); 31110fe6e20bSNaoya Horiguchi page_remove_rmap(old_page); 3112cd67f0d2SNaoya Horiguchi hugepage_add_new_anon_rmap(new_page, vma, address); 31131e8f889bSDavid Gibson /* Make the old page be freed below */ 31141e8f889bSDavid Gibson new_page = old_page; 31151e8f889bSDavid Gibson } 3116cb900f41SKirill A. Shutemov spin_unlock(ptl); 31172ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 3118ad4404a2SDavidlohr Bueso out_release_all: 31191e8f889bSDavid Gibson page_cache_release(new_page); 3120ad4404a2SDavidlohr Bueso out_release_old: 31211e8f889bSDavid Gibson page_cache_release(old_page); 31228312034fSJoonsoo Kim 3123ad4404a2SDavidlohr Bueso spin_lock(ptl); /* Caller expects lock to be held */ 3124ad4404a2SDavidlohr Bueso return ret; 31251e8f889bSDavid Gibson } 31261e8f889bSDavid Gibson 312704f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */ 3128a5516438SAndi Kleen static struct page *hugetlbfs_pagecache_page(struct hstate *h, 3129a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 313004f2cbe3SMel Gorman { 313104f2cbe3SMel Gorman struct address_space *mapping; 3132e7c4b0bfSAndy Whitcroft pgoff_t idx; 313304f2cbe3SMel Gorman 313404f2cbe3SMel Gorman mapping = vma->vm_file->f_mapping; 3135a5516438SAndi Kleen idx = vma_hugecache_offset(h, vma, address); 313604f2cbe3SMel Gorman 313704f2cbe3SMel Gorman return find_lock_page(mapping, idx); 313804f2cbe3SMel Gorman } 313904f2cbe3SMel Gorman 31403ae77f43SHugh Dickins /* 31413ae77f43SHugh Dickins * Return whether there is a pagecache page to back given address within VMA. 31423ae77f43SHugh Dickins * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page. 31433ae77f43SHugh Dickins */ 31443ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h, 31452a15efc9SHugh Dickins struct vm_area_struct *vma, unsigned long address) 31462a15efc9SHugh Dickins { 31472a15efc9SHugh Dickins struct address_space *mapping; 31482a15efc9SHugh Dickins pgoff_t idx; 31492a15efc9SHugh Dickins struct page *page; 31502a15efc9SHugh Dickins 31512a15efc9SHugh Dickins mapping = vma->vm_file->f_mapping; 31522a15efc9SHugh Dickins idx = vma_hugecache_offset(h, vma, address); 31532a15efc9SHugh Dickins 31542a15efc9SHugh Dickins page = find_get_page(mapping, idx); 31552a15efc9SHugh Dickins if (page) 31562a15efc9SHugh Dickins put_page(page); 31572a15efc9SHugh Dickins return page != NULL; 31582a15efc9SHugh Dickins } 31592a15efc9SHugh Dickins 3160a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, 31618382d914SDavidlohr Bueso struct address_space *mapping, pgoff_t idx, 3162788c7df4SHugh Dickins unsigned long address, pte_t *ptep, unsigned int flags) 3163ac9b9c66SHugh Dickins { 3164a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3165ac9b9c66SHugh Dickins int ret = VM_FAULT_SIGBUS; 3166409eb8c2SHillf Danton int anon_rmap = 0; 31674c887265SAdam Litke unsigned long size; 31684c887265SAdam Litke struct page *page; 31691e8f889bSDavid Gibson pte_t new_pte; 3170cb900f41SKirill A. Shutemov spinlock_t *ptl; 31714c887265SAdam Litke 317204f2cbe3SMel Gorman /* 317304f2cbe3SMel Gorman * Currently, we are forced to kill the process in the event the 317404f2cbe3SMel Gorman * original mapper has unmapped pages from the child due to a failed 317525985edcSLucas De Marchi * COW. Warn that such a situation has occurred as it may not be obvious 317604f2cbe3SMel Gorman */ 317704f2cbe3SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { 3178ffb22af5SAndrew Morton pr_warning("PID %d killed due to inadequate hugepage pool\n", 317904f2cbe3SMel Gorman current->pid); 318004f2cbe3SMel Gorman return ret; 318104f2cbe3SMel Gorman } 318204f2cbe3SMel Gorman 31834c887265SAdam Litke /* 31844c887265SAdam Litke * Use page lock to guard against racing truncation 31854c887265SAdam Litke * before we get page_table_lock. 31864c887265SAdam Litke */ 31876bda666aSChristoph Lameter retry: 31886bda666aSChristoph Lameter page = find_lock_page(mapping, idx); 31896bda666aSChristoph Lameter if (!page) { 3190a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 3191ebed4bfcSHugh Dickins if (idx >= size) 3192ebed4bfcSHugh Dickins goto out; 319304f2cbe3SMel Gorman page = alloc_huge_page(vma, address, 0); 31942fc39cecSAdam Litke if (IS_ERR(page)) { 319576dcee75SAneesh Kumar K.V ret = PTR_ERR(page); 319676dcee75SAneesh Kumar K.V if (ret == -ENOMEM) 319776dcee75SAneesh Kumar K.V ret = VM_FAULT_OOM; 319876dcee75SAneesh Kumar K.V else 319976dcee75SAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 32006bda666aSChristoph Lameter goto out; 32016bda666aSChristoph Lameter } 320247ad8475SAndrea Arcangeli clear_huge_page(page, address, pages_per_huge_page(h)); 32030ed361deSNick Piggin __SetPageUptodate(page); 3204bcc54222SNaoya Horiguchi set_page_huge_active(page); 3205ac9b9c66SHugh Dickins 3206f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) { 32076bda666aSChristoph Lameter int err; 320845c682a6SKen Chen struct inode *inode = mapping->host; 32096bda666aSChristoph Lameter 32106bda666aSChristoph Lameter err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); 32116bda666aSChristoph Lameter if (err) { 32126bda666aSChristoph Lameter put_page(page); 32136bda666aSChristoph Lameter if (err == -EEXIST) 32146bda666aSChristoph Lameter goto retry; 32156bda666aSChristoph Lameter goto out; 32166bda666aSChristoph Lameter } 321707443a85SJoonsoo Kim ClearPagePrivate(page); 321845c682a6SKen Chen 321945c682a6SKen Chen spin_lock(&inode->i_lock); 3220a5516438SAndi Kleen inode->i_blocks += blocks_per_huge_page(h); 322145c682a6SKen Chen spin_unlock(&inode->i_lock); 322223be7468SMel Gorman } else { 32236bda666aSChristoph Lameter lock_page(page); 32240fe6e20bSNaoya Horiguchi if (unlikely(anon_vma_prepare(vma))) { 32250fe6e20bSNaoya Horiguchi ret = VM_FAULT_OOM; 32260fe6e20bSNaoya Horiguchi goto backout_unlocked; 322723be7468SMel Gorman } 3228409eb8c2SHillf Danton anon_rmap = 1; 32290fe6e20bSNaoya Horiguchi } 32300fe6e20bSNaoya Horiguchi } else { 323157303d80SAndy Whitcroft /* 3232998b4382SNaoya Horiguchi * If memory error occurs between mmap() and fault, some process 3233998b4382SNaoya Horiguchi * don't have hwpoisoned swap entry for errored virtual address. 3234998b4382SNaoya Horiguchi * So we need to block hugepage fault by PG_hwpoison bit check. 3235fd6a03edSNaoya Horiguchi */ 3236fd6a03edSNaoya Horiguchi if (unlikely(PageHWPoison(page))) { 3237aa50d3a7SAndi Kleen ret = VM_FAULT_HWPOISON | 3238972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 3239fd6a03edSNaoya Horiguchi goto backout_unlocked; 32406bda666aSChristoph Lameter } 3241998b4382SNaoya Horiguchi } 32421e8f889bSDavid Gibson 324357303d80SAndy Whitcroft /* 324457303d80SAndy Whitcroft * If we are going to COW a private mapping later, we examine the 324557303d80SAndy Whitcroft * pending reservations for this page now. This will ensure that 324657303d80SAndy Whitcroft * any allocations necessary to record that reservation occur outside 324757303d80SAndy Whitcroft * the spinlock. 324857303d80SAndy Whitcroft */ 3249788c7df4SHugh Dickins if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) 32502b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 32512b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 32522b26736cSAndy Whitcroft goto backout_unlocked; 32532b26736cSAndy Whitcroft } 325457303d80SAndy Whitcroft 3255cb900f41SKirill A. Shutemov ptl = huge_pte_lockptr(h, mm, ptep); 3256cb900f41SKirill A. Shutemov spin_lock(ptl); 3257a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 32584c887265SAdam Litke if (idx >= size) 32594c887265SAdam Litke goto backout; 32604c887265SAdam Litke 326183c54070SNick Piggin ret = 0; 32627f2e9525SGerald Schaefer if (!huge_pte_none(huge_ptep_get(ptep))) 32634c887265SAdam Litke goto backout; 32644c887265SAdam Litke 326507443a85SJoonsoo Kim if (anon_rmap) { 326607443a85SJoonsoo Kim ClearPagePrivate(page); 3267409eb8c2SHillf Danton hugepage_add_new_anon_rmap(page, vma, address); 3268ac714904SChoi Gi-yong } else 3269409eb8c2SHillf Danton page_dup_rmap(page); 32701e8f889bSDavid Gibson new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) 32711e8f889bSDavid Gibson && (vma->vm_flags & VM_SHARED))); 32721e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, new_pte); 32731e8f889bSDavid Gibson 3274788c7df4SHugh Dickins if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 32751e8f889bSDavid Gibson /* Optimization, do the COW without a second fault */ 3276cb900f41SKirill A. Shutemov ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page, ptl); 32771e8f889bSDavid Gibson } 32781e8f889bSDavid Gibson 3279cb900f41SKirill A. Shutemov spin_unlock(ptl); 32804c887265SAdam Litke unlock_page(page); 32814c887265SAdam Litke out: 3282ac9b9c66SHugh Dickins return ret; 32834c887265SAdam Litke 32844c887265SAdam Litke backout: 3285cb900f41SKirill A. Shutemov spin_unlock(ptl); 32862b26736cSAndy Whitcroft backout_unlocked: 32874c887265SAdam Litke unlock_page(page); 32884c887265SAdam Litke put_page(page); 32894c887265SAdam Litke goto out; 3290ac9b9c66SHugh Dickins } 3291ac9b9c66SHugh Dickins 32928382d914SDavidlohr Bueso #ifdef CONFIG_SMP 32938382d914SDavidlohr Bueso static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 32948382d914SDavidlohr Bueso struct vm_area_struct *vma, 32958382d914SDavidlohr Bueso struct address_space *mapping, 32968382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 32978382d914SDavidlohr Bueso { 32988382d914SDavidlohr Bueso unsigned long key[2]; 32998382d914SDavidlohr Bueso u32 hash; 33008382d914SDavidlohr Bueso 33018382d914SDavidlohr Bueso if (vma->vm_flags & VM_SHARED) { 33028382d914SDavidlohr Bueso key[0] = (unsigned long) mapping; 33038382d914SDavidlohr Bueso key[1] = idx; 33048382d914SDavidlohr Bueso } else { 33058382d914SDavidlohr Bueso key[0] = (unsigned long) mm; 33068382d914SDavidlohr Bueso key[1] = address >> huge_page_shift(h); 33078382d914SDavidlohr Bueso } 33088382d914SDavidlohr Bueso 33098382d914SDavidlohr Bueso hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0); 33108382d914SDavidlohr Bueso 33118382d914SDavidlohr Bueso return hash & (num_fault_mutexes - 1); 33128382d914SDavidlohr Bueso } 33138382d914SDavidlohr Bueso #else 33148382d914SDavidlohr Bueso /* 33158382d914SDavidlohr Bueso * For uniprocesor systems we always use a single mutex, so just 33168382d914SDavidlohr Bueso * return 0 and avoid the hashing overhead. 33178382d914SDavidlohr Bueso */ 33188382d914SDavidlohr Bueso static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 33198382d914SDavidlohr Bueso struct vm_area_struct *vma, 33208382d914SDavidlohr Bueso struct address_space *mapping, 33218382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 33228382d914SDavidlohr Bueso { 33238382d914SDavidlohr Bueso return 0; 33248382d914SDavidlohr Bueso } 33258382d914SDavidlohr Bueso #endif 33268382d914SDavidlohr Bueso 332786e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3328788c7df4SHugh Dickins unsigned long address, unsigned int flags) 332986e5216fSAdam Litke { 33308382d914SDavidlohr Bueso pte_t *ptep, entry; 3331cb900f41SKirill A. Shutemov spinlock_t *ptl; 33321e8f889bSDavid Gibson int ret; 33338382d914SDavidlohr Bueso u32 hash; 33348382d914SDavidlohr Bueso pgoff_t idx; 33350fe6e20bSNaoya Horiguchi struct page *page = NULL; 333657303d80SAndy Whitcroft struct page *pagecache_page = NULL; 3337a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 33388382d914SDavidlohr Bueso struct address_space *mapping; 33390f792cf9SNaoya Horiguchi int need_wait_lock = 0; 334086e5216fSAdam Litke 33411e16a539SKAMEZAWA Hiroyuki address &= huge_page_mask(h); 33421e16a539SKAMEZAWA Hiroyuki 3343fd6a03edSNaoya Horiguchi ptep = huge_pte_offset(mm, address); 3344fd6a03edSNaoya Horiguchi if (ptep) { 3345fd6a03edSNaoya Horiguchi entry = huge_ptep_get(ptep); 3346290408d4SNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(entry))) { 3347cb900f41SKirill A. Shutemov migration_entry_wait_huge(vma, mm, ptep); 3348290408d4SNaoya Horiguchi return 0; 3349290408d4SNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) 3350aa50d3a7SAndi Kleen return VM_FAULT_HWPOISON_LARGE | 3351972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 3352fd6a03edSNaoya Horiguchi } 3353fd6a03edSNaoya Horiguchi 3354a5516438SAndi Kleen ptep = huge_pte_alloc(mm, address, huge_page_size(h)); 335586e5216fSAdam Litke if (!ptep) 335686e5216fSAdam Litke return VM_FAULT_OOM; 335786e5216fSAdam Litke 33588382d914SDavidlohr Bueso mapping = vma->vm_file->f_mapping; 33598382d914SDavidlohr Bueso idx = vma_hugecache_offset(h, vma, address); 33608382d914SDavidlohr Bueso 33613935baa9SDavid Gibson /* 33623935baa9SDavid Gibson * Serialize hugepage allocation and instantiation, so that we don't 33633935baa9SDavid Gibson * get spurious allocation failures if two CPUs race to instantiate 33643935baa9SDavid Gibson * the same page in the page cache. 33653935baa9SDavid Gibson */ 33668382d914SDavidlohr Bueso hash = fault_mutex_hash(h, mm, vma, mapping, idx, address); 33678382d914SDavidlohr Bueso mutex_lock(&htlb_fault_mutex_table[hash]); 33688382d914SDavidlohr Bueso 33697f2e9525SGerald Schaefer entry = huge_ptep_get(ptep); 33707f2e9525SGerald Schaefer if (huge_pte_none(entry)) { 33718382d914SDavidlohr Bueso ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags); 3372b4d1d99fSDavid Gibson goto out_mutex; 33733935baa9SDavid Gibson } 337486e5216fSAdam Litke 337583c54070SNick Piggin ret = 0; 33761e8f889bSDavid Gibson 337757303d80SAndy Whitcroft /* 33780f792cf9SNaoya Horiguchi * entry could be a migration/hwpoison entry at this point, so this 33790f792cf9SNaoya Horiguchi * check prevents the kernel from going below assuming that we have 33800f792cf9SNaoya Horiguchi * a active hugepage in pagecache. This goto expects the 2nd page fault, 33810f792cf9SNaoya Horiguchi * and is_hugetlb_entry_(migration|hwpoisoned) check will properly 33820f792cf9SNaoya Horiguchi * handle it. 33830f792cf9SNaoya Horiguchi */ 33840f792cf9SNaoya Horiguchi if (!pte_present(entry)) 33850f792cf9SNaoya Horiguchi goto out_mutex; 33860f792cf9SNaoya Horiguchi 33870f792cf9SNaoya Horiguchi /* 338857303d80SAndy Whitcroft * If we are going to COW the mapping later, we examine the pending 338957303d80SAndy Whitcroft * reservations for this page now. This will ensure that any 339057303d80SAndy Whitcroft * allocations necessary to record that reservation occur outside the 339157303d80SAndy Whitcroft * spinlock. For private mappings, we also lookup the pagecache 339257303d80SAndy Whitcroft * page now as it is used to determine if a reservation has been 339357303d80SAndy Whitcroft * consumed. 339457303d80SAndy Whitcroft */ 3395106c992aSGerald Schaefer if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { 33962b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 33972b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 3398b4d1d99fSDavid Gibson goto out_mutex; 33992b26736cSAndy Whitcroft } 340057303d80SAndy Whitcroft 3401f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 340257303d80SAndy Whitcroft pagecache_page = hugetlbfs_pagecache_page(h, 340357303d80SAndy Whitcroft vma, address); 340457303d80SAndy Whitcroft } 340557303d80SAndy Whitcroft 34060f792cf9SNaoya Horiguchi ptl = huge_pte_lock(h, mm, ptep); 34070fe6e20bSNaoya Horiguchi 34081e8f889bSDavid Gibson /* Check for a racing update before calling hugetlb_cow */ 3409b4d1d99fSDavid Gibson if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) 3410cb900f41SKirill A. Shutemov goto out_ptl; 3411b4d1d99fSDavid Gibson 34120f792cf9SNaoya Horiguchi /* 34130f792cf9SNaoya Horiguchi * hugetlb_cow() requires page locks of pte_page(entry) and 34140f792cf9SNaoya Horiguchi * pagecache_page, so here we need take the former one 34150f792cf9SNaoya Horiguchi * when page != pagecache_page or !pagecache_page. 34160f792cf9SNaoya Horiguchi */ 34170f792cf9SNaoya Horiguchi page = pte_page(entry); 34180f792cf9SNaoya Horiguchi if (page != pagecache_page) 34190f792cf9SNaoya Horiguchi if (!trylock_page(page)) { 34200f792cf9SNaoya Horiguchi need_wait_lock = 1; 34210f792cf9SNaoya Horiguchi goto out_ptl; 34220f792cf9SNaoya Horiguchi } 34230f792cf9SNaoya Horiguchi 34240f792cf9SNaoya Horiguchi get_page(page); 3425b4d1d99fSDavid Gibson 3426788c7df4SHugh Dickins if (flags & FAULT_FLAG_WRITE) { 3427106c992aSGerald Schaefer if (!huge_pte_write(entry)) { 342857303d80SAndy Whitcroft ret = hugetlb_cow(mm, vma, address, ptep, entry, 3429cb900f41SKirill A. Shutemov pagecache_page, ptl); 34300f792cf9SNaoya Horiguchi goto out_put_page; 3431b4d1d99fSDavid Gibson } 3432106c992aSGerald Schaefer entry = huge_pte_mkdirty(entry); 3433b4d1d99fSDavid Gibson } 3434b4d1d99fSDavid Gibson entry = pte_mkyoung(entry); 3435788c7df4SHugh Dickins if (huge_ptep_set_access_flags(vma, address, ptep, entry, 3436788c7df4SHugh Dickins flags & FAULT_FLAG_WRITE)) 34374b3073e1SRussell King update_mmu_cache(vma, address, ptep); 34380f792cf9SNaoya Horiguchi out_put_page: 34390f792cf9SNaoya Horiguchi if (page != pagecache_page) 34400f792cf9SNaoya Horiguchi unlock_page(page); 34410f792cf9SNaoya Horiguchi put_page(page); 3442cb900f41SKirill A. Shutemov out_ptl: 3443cb900f41SKirill A. Shutemov spin_unlock(ptl); 344457303d80SAndy Whitcroft 344557303d80SAndy Whitcroft if (pagecache_page) { 344657303d80SAndy Whitcroft unlock_page(pagecache_page); 344757303d80SAndy Whitcroft put_page(pagecache_page); 344857303d80SAndy Whitcroft } 3449b4d1d99fSDavid Gibson out_mutex: 34508382d914SDavidlohr Bueso mutex_unlock(&htlb_fault_mutex_table[hash]); 34510f792cf9SNaoya Horiguchi /* 34520f792cf9SNaoya Horiguchi * Generally it's safe to hold refcount during waiting page lock. But 34530f792cf9SNaoya Horiguchi * here we just wait to defer the next page fault to avoid busy loop and 34540f792cf9SNaoya Horiguchi * the page is not used after unlocked before returning from the current 34550f792cf9SNaoya Horiguchi * page fault. So we are safe from accessing freed page, even if we wait 34560f792cf9SNaoya Horiguchi * here without taking refcount. 34570f792cf9SNaoya Horiguchi */ 34580f792cf9SNaoya Horiguchi if (need_wait_lock) 34590f792cf9SNaoya Horiguchi wait_on_page_locked(page); 34601e8f889bSDavid Gibson return ret; 346186e5216fSAdam Litke } 346286e5216fSAdam Litke 346328a35716SMichel Lespinasse long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, 346463551ae0SDavid Gibson struct page **pages, struct vm_area_struct **vmas, 346528a35716SMichel Lespinasse unsigned long *position, unsigned long *nr_pages, 346628a35716SMichel Lespinasse long i, unsigned int flags) 346763551ae0SDavid Gibson { 3468d5d4b0aaSChen, Kenneth W unsigned long pfn_offset; 3469d5d4b0aaSChen, Kenneth W unsigned long vaddr = *position; 347028a35716SMichel Lespinasse unsigned long remainder = *nr_pages; 3471a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 347263551ae0SDavid Gibson 347363551ae0SDavid Gibson while (vaddr < vma->vm_end && remainder) { 347463551ae0SDavid Gibson pte_t *pte; 3475cb900f41SKirill A. Shutemov spinlock_t *ptl = NULL; 34762a15efc9SHugh Dickins int absent; 347763551ae0SDavid Gibson struct page *page; 347863551ae0SDavid Gibson 34794c887265SAdam Litke /* 348002057967SDavid Rientjes * If we have a pending SIGKILL, don't keep faulting pages and 348102057967SDavid Rientjes * potentially allocating memory. 348202057967SDavid Rientjes */ 348302057967SDavid Rientjes if (unlikely(fatal_signal_pending(current))) { 348402057967SDavid Rientjes remainder = 0; 348502057967SDavid Rientjes break; 348602057967SDavid Rientjes } 348702057967SDavid Rientjes 348802057967SDavid Rientjes /* 34894c887265SAdam Litke * Some archs (sparc64, sh*) have multiple pte_ts to 34902a15efc9SHugh Dickins * each hugepage. We have to make sure we get the 34914c887265SAdam Litke * first, for the page indexing below to work. 3492cb900f41SKirill A. Shutemov * 3493cb900f41SKirill A. Shutemov * Note that page table lock is not held when pte is null. 34944c887265SAdam Litke */ 3495a5516438SAndi Kleen pte = huge_pte_offset(mm, vaddr & huge_page_mask(h)); 3496cb900f41SKirill A. Shutemov if (pte) 3497cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, pte); 34982a15efc9SHugh Dickins absent = !pte || huge_pte_none(huge_ptep_get(pte)); 349963551ae0SDavid Gibson 35002a15efc9SHugh Dickins /* 35012a15efc9SHugh Dickins * When coredumping, it suits get_dump_page if we just return 35023ae77f43SHugh Dickins * an error where there's an empty slot with no huge pagecache 35033ae77f43SHugh Dickins * to back it. This way, we avoid allocating a hugepage, and 35043ae77f43SHugh Dickins * the sparse dumpfile avoids allocating disk blocks, but its 35053ae77f43SHugh Dickins * huge holes still show up with zeroes where they need to be. 35062a15efc9SHugh Dickins */ 35073ae77f43SHugh Dickins if (absent && (flags & FOLL_DUMP) && 35083ae77f43SHugh Dickins !hugetlbfs_pagecache_present(h, vma, vaddr)) { 3509cb900f41SKirill A. Shutemov if (pte) 3510cb900f41SKirill A. Shutemov spin_unlock(ptl); 35112a15efc9SHugh Dickins remainder = 0; 35122a15efc9SHugh Dickins break; 35132a15efc9SHugh Dickins } 35142a15efc9SHugh Dickins 35159cc3a5bdSNaoya Horiguchi /* 35169cc3a5bdSNaoya Horiguchi * We need call hugetlb_fault for both hugepages under migration 35179cc3a5bdSNaoya Horiguchi * (in which case hugetlb_fault waits for the migration,) and 35189cc3a5bdSNaoya Horiguchi * hwpoisoned hugepages (in which case we need to prevent the 35199cc3a5bdSNaoya Horiguchi * caller from accessing to them.) In order to do this, we use 35209cc3a5bdSNaoya Horiguchi * here is_swap_pte instead of is_hugetlb_entry_migration and 35219cc3a5bdSNaoya Horiguchi * is_hugetlb_entry_hwpoisoned. This is because it simply covers 35229cc3a5bdSNaoya Horiguchi * both cases, and because we can't follow correct pages 35239cc3a5bdSNaoya Horiguchi * directly from any kind of swap entries. 35249cc3a5bdSNaoya Horiguchi */ 35259cc3a5bdSNaoya Horiguchi if (absent || is_swap_pte(huge_ptep_get(pte)) || 3526106c992aSGerald Schaefer ((flags & FOLL_WRITE) && 3527106c992aSGerald Schaefer !huge_pte_write(huge_ptep_get(pte)))) { 35284c887265SAdam Litke int ret; 35294c887265SAdam Litke 3530cb900f41SKirill A. Shutemov if (pte) 3531cb900f41SKirill A. Shutemov spin_unlock(ptl); 35322a15efc9SHugh Dickins ret = hugetlb_fault(mm, vma, vaddr, 35332a15efc9SHugh Dickins (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0); 3534a89182c7SAdam Litke if (!(ret & VM_FAULT_ERROR)) 35354c887265SAdam Litke continue; 35364c887265SAdam Litke 35371c59827dSHugh Dickins remainder = 0; 35381c59827dSHugh Dickins break; 35391c59827dSHugh Dickins } 354063551ae0SDavid Gibson 3541a5516438SAndi Kleen pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; 35427f2e9525SGerald Schaefer page = pte_page(huge_ptep_get(pte)); 3543d5d4b0aaSChen, Kenneth W same_page: 3544d6692183SChen, Kenneth W if (pages) { 354569d177c2SAndy Whitcroft pages[i] = mem_map_offset(page, pfn_offset); 3546a0368d4eSAndrea Arcangeli get_page_foll(pages[i]); 3547d6692183SChen, Kenneth W } 354863551ae0SDavid Gibson 354963551ae0SDavid Gibson if (vmas) 355063551ae0SDavid Gibson vmas[i] = vma; 355163551ae0SDavid Gibson 355263551ae0SDavid Gibson vaddr += PAGE_SIZE; 3553d5d4b0aaSChen, Kenneth W ++pfn_offset; 355463551ae0SDavid Gibson --remainder; 355563551ae0SDavid Gibson ++i; 3556d5d4b0aaSChen, Kenneth W if (vaddr < vma->vm_end && remainder && 3557a5516438SAndi Kleen pfn_offset < pages_per_huge_page(h)) { 3558d5d4b0aaSChen, Kenneth W /* 3559d5d4b0aaSChen, Kenneth W * We use pfn_offset to avoid touching the pageframes 3560d5d4b0aaSChen, Kenneth W * of this compound page. 3561d5d4b0aaSChen, Kenneth W */ 3562d5d4b0aaSChen, Kenneth W goto same_page; 3563d5d4b0aaSChen, Kenneth W } 3564cb900f41SKirill A. Shutemov spin_unlock(ptl); 356563551ae0SDavid Gibson } 356628a35716SMichel Lespinasse *nr_pages = remainder; 356763551ae0SDavid Gibson *position = vaddr; 356863551ae0SDavid Gibson 35692a15efc9SHugh Dickins return i ? i : -EFAULT; 357063551ae0SDavid Gibson } 35718f860591SZhang, Yanmin 35727da4d641SPeter Zijlstra unsigned long hugetlb_change_protection(struct vm_area_struct *vma, 35738f860591SZhang, Yanmin unsigned long address, unsigned long end, pgprot_t newprot) 35748f860591SZhang, Yanmin { 35758f860591SZhang, Yanmin struct mm_struct *mm = vma->vm_mm; 35768f860591SZhang, Yanmin unsigned long start = address; 35778f860591SZhang, Yanmin pte_t *ptep; 35788f860591SZhang, Yanmin pte_t pte; 3579a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 35807da4d641SPeter Zijlstra unsigned long pages = 0; 35818f860591SZhang, Yanmin 35828f860591SZhang, Yanmin BUG_ON(address >= end); 35838f860591SZhang, Yanmin flush_cache_range(vma, address, end); 35848f860591SZhang, Yanmin 3585a5338093SRik van Riel mmu_notifier_invalidate_range_start(mm, start, end); 358683cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 3587a5516438SAndi Kleen for (; address < end; address += huge_page_size(h)) { 3588cb900f41SKirill A. Shutemov spinlock_t *ptl; 35898f860591SZhang, Yanmin ptep = huge_pte_offset(mm, address); 35908f860591SZhang, Yanmin if (!ptep) 35918f860591SZhang, Yanmin continue; 3592cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 35937da4d641SPeter Zijlstra if (huge_pmd_unshare(mm, &address, ptep)) { 35947da4d641SPeter Zijlstra pages++; 3595cb900f41SKirill A. Shutemov spin_unlock(ptl); 359639dde65cSChen, Kenneth W continue; 35977da4d641SPeter Zijlstra } 3598a8bda28dSNaoya Horiguchi pte = huge_ptep_get(ptep); 3599a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { 3600a8bda28dSNaoya Horiguchi spin_unlock(ptl); 3601a8bda28dSNaoya Horiguchi continue; 3602a8bda28dSNaoya Horiguchi } 3603a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(pte))) { 3604a8bda28dSNaoya Horiguchi swp_entry_t entry = pte_to_swp_entry(pte); 3605a8bda28dSNaoya Horiguchi 3606a8bda28dSNaoya Horiguchi if (is_write_migration_entry(entry)) { 3607a8bda28dSNaoya Horiguchi pte_t newpte; 3608a8bda28dSNaoya Horiguchi 3609a8bda28dSNaoya Horiguchi make_migration_entry_read(&entry); 3610a8bda28dSNaoya Horiguchi newpte = swp_entry_to_pte(entry); 3611a8bda28dSNaoya Horiguchi set_huge_pte_at(mm, address, ptep, newpte); 3612a8bda28dSNaoya Horiguchi pages++; 3613a8bda28dSNaoya Horiguchi } 3614a8bda28dSNaoya Horiguchi spin_unlock(ptl); 3615a8bda28dSNaoya Horiguchi continue; 3616a8bda28dSNaoya Horiguchi } 3617a8bda28dSNaoya Horiguchi if (!huge_pte_none(pte)) { 36188f860591SZhang, Yanmin pte = huge_ptep_get_and_clear(mm, address, ptep); 3619106c992aSGerald Schaefer pte = pte_mkhuge(huge_pte_modify(pte, newprot)); 3620be7517d6STony Lu pte = arch_make_huge_pte(pte, vma, NULL, 0); 36218f860591SZhang, Yanmin set_huge_pte_at(mm, address, ptep, pte); 36227da4d641SPeter Zijlstra pages++; 36238f860591SZhang, Yanmin } 3624cb900f41SKirill A. Shutemov spin_unlock(ptl); 36258f860591SZhang, Yanmin } 3626d833352aSMel Gorman /* 3627c8c06efaSDavidlohr Bueso * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare 3628d833352aSMel Gorman * may have cleared our pud entry and done put_page on the page table: 3629c8c06efaSDavidlohr Bueso * once we release i_mmap_rwsem, another task can do the final put_page 3630d833352aSMel Gorman * and that page table be reused and filled with junk. 3631d833352aSMel Gorman */ 36328f860591SZhang, Yanmin flush_tlb_range(vma, start, end); 363334ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, start, end); 363483cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 3635a5338093SRik van Riel mmu_notifier_invalidate_range_end(mm, start, end); 36367da4d641SPeter Zijlstra 36377da4d641SPeter Zijlstra return pages << h->order; 36388f860591SZhang, Yanmin } 36398f860591SZhang, Yanmin 3640a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode, 3641a1e78772SMel Gorman long from, long to, 36425a6fe125SMel Gorman struct vm_area_struct *vma, 3643ca16d140SKOSAKI Motohiro vm_flags_t vm_flags) 3644e4e574b7SAdam Litke { 364517c9d12eSMel Gorman long ret, chg; 3646a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 364790481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 36489119a41eSJoonsoo Kim struct resv_map *resv_map; 36491c5ecae3SMike Kravetz long gbl_reserve; 3650e4e574b7SAdam Litke 3651a1e78772SMel Gorman /* 365217c9d12eSMel Gorman * Only apply hugepage reservation if asked. At fault time, an 365317c9d12eSMel Gorman * attempt will be made for VM_NORESERVE to allocate a page 365490481622SDavid Gibson * without using reserves 365517c9d12eSMel Gorman */ 3656ca16d140SKOSAKI Motohiro if (vm_flags & VM_NORESERVE) 365717c9d12eSMel Gorman return 0; 365817c9d12eSMel Gorman 365917c9d12eSMel Gorman /* 3660a1e78772SMel Gorman * Shared mappings base their reservation on the number of pages that 3661a1e78772SMel Gorman * are already allocated on behalf of the file. Private mappings need 3662a1e78772SMel Gorman * to reserve the full area even if read-only as mprotect() may be 3663a1e78772SMel Gorman * called to make the mapping read-write. Assume !vma is a shm mapping 3664a1e78772SMel Gorman */ 36659119a41eSJoonsoo Kim if (!vma || vma->vm_flags & VM_MAYSHARE) { 36664e35f483SJoonsoo Kim resv_map = inode_resv_map(inode); 36679119a41eSJoonsoo Kim 36681406ec9bSJoonsoo Kim chg = region_chg(resv_map, from, to); 36699119a41eSJoonsoo Kim 36709119a41eSJoonsoo Kim } else { 36719119a41eSJoonsoo Kim resv_map = resv_map_alloc(); 36725a6fe125SMel Gorman if (!resv_map) 36735a6fe125SMel Gorman return -ENOMEM; 36745a6fe125SMel Gorman 367517c9d12eSMel Gorman chg = to - from; 367617c9d12eSMel Gorman 36775a6fe125SMel Gorman set_vma_resv_map(vma, resv_map); 36785a6fe125SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_OWNER); 36795a6fe125SMel Gorman } 36805a6fe125SMel Gorman 3681c50ac050SDave Hansen if (chg < 0) { 3682c50ac050SDave Hansen ret = chg; 3683c50ac050SDave Hansen goto out_err; 3684c50ac050SDave Hansen } 368517c9d12eSMel Gorman 36861c5ecae3SMike Kravetz /* 36871c5ecae3SMike Kravetz * There must be enough pages in the subpool for the mapping. If 36881c5ecae3SMike Kravetz * the subpool has a minimum size, there may be some global 36891c5ecae3SMike Kravetz * reservations already in place (gbl_reserve). 36901c5ecae3SMike Kravetz */ 36911c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_get_pages(spool, chg); 36921c5ecae3SMike Kravetz if (gbl_reserve < 0) { 3693c50ac050SDave Hansen ret = -ENOSPC; 3694c50ac050SDave Hansen goto out_err; 3695c50ac050SDave Hansen } 369617c9d12eSMel Gorman 369717c9d12eSMel Gorman /* 369817c9d12eSMel Gorman * Check enough hugepages are available for the reservation. 369990481622SDavid Gibson * Hand the pages back to the subpool if there are not 370017c9d12eSMel Gorman */ 37011c5ecae3SMike Kravetz ret = hugetlb_acct_memory(h, gbl_reserve); 370217c9d12eSMel Gorman if (ret < 0) { 37031c5ecae3SMike Kravetz /* put back original number of pages, chg */ 37041c5ecae3SMike Kravetz (void)hugepage_subpool_put_pages(spool, chg); 3705c50ac050SDave Hansen goto out_err; 370617c9d12eSMel Gorman } 370717c9d12eSMel Gorman 370817c9d12eSMel Gorman /* 370917c9d12eSMel Gorman * Account for the reservations made. Shared mappings record regions 371017c9d12eSMel Gorman * that have reservations as they are shared by multiple VMAs. 371117c9d12eSMel Gorman * When the last VMA disappears, the region map says how much 371217c9d12eSMel Gorman * the reservation was and the page cache tells how much of 371317c9d12eSMel Gorman * the reservation was consumed. Private mappings are per-VMA and 371417c9d12eSMel Gorman * only the consumed reservations are tracked. When the VMA 371517c9d12eSMel Gorman * disappears, the original reservation is the VMA size and the 371617c9d12eSMel Gorman * consumed reservations are stored in the map. Hence, nothing 371717c9d12eSMel Gorman * else has to be done for private mappings here 371817c9d12eSMel Gorman */ 371933039678SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) { 372033039678SMike Kravetz long add = region_add(resv_map, from, to); 372133039678SMike Kravetz 372233039678SMike Kravetz if (unlikely(chg > add)) { 372333039678SMike Kravetz /* 372433039678SMike Kravetz * pages in this range were added to the reserve 372533039678SMike Kravetz * map between region_chg and region_add. This 372633039678SMike Kravetz * indicates a race with alloc_huge_page. Adjust 372733039678SMike Kravetz * the subpool and reserve counts modified above 372833039678SMike Kravetz * based on the difference. 372933039678SMike Kravetz */ 373033039678SMike Kravetz long rsv_adjust; 373133039678SMike Kravetz 373233039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 373333039678SMike Kravetz chg - add); 373433039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 373533039678SMike Kravetz } 373633039678SMike Kravetz } 3737a43a8c39SChen, Kenneth W return 0; 3738c50ac050SDave Hansen out_err: 3739f031dd27SJoonsoo Kim if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 3740f031dd27SJoonsoo Kim kref_put(&resv_map->refs, resv_map_release); 3741c50ac050SDave Hansen return ret; 3742a43a8c39SChen, Kenneth W } 3743a43a8c39SChen, Kenneth W 3744a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) 3745a43a8c39SChen, Kenneth W { 3746a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 37474e35f483SJoonsoo Kim struct resv_map *resv_map = inode_resv_map(inode); 37489119a41eSJoonsoo Kim long chg = 0; 374990481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 37501c5ecae3SMike Kravetz long gbl_reserve; 375145c682a6SKen Chen 37529119a41eSJoonsoo Kim if (resv_map) 37531406ec9bSJoonsoo Kim chg = region_truncate(resv_map, offset); 375445c682a6SKen Chen spin_lock(&inode->i_lock); 3755e4c6f8beSEric Sandeen inode->i_blocks -= (blocks_per_huge_page(h) * freed); 375645c682a6SKen Chen spin_unlock(&inode->i_lock); 375745c682a6SKen Chen 37581c5ecae3SMike Kravetz /* 37591c5ecae3SMike Kravetz * If the subpool has a minimum size, the number of global 37601c5ecae3SMike Kravetz * reservations to be released may be adjusted. 37611c5ecae3SMike Kravetz */ 37621c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); 37631c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 3764a43a8c39SChen, Kenneth W } 376593f70f90SNaoya Horiguchi 37663212b535SSteve Capper #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE 37673212b535SSteve Capper static unsigned long page_table_shareable(struct vm_area_struct *svma, 37683212b535SSteve Capper struct vm_area_struct *vma, 37693212b535SSteve Capper unsigned long addr, pgoff_t idx) 37703212b535SSteve Capper { 37713212b535SSteve Capper unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + 37723212b535SSteve Capper svma->vm_start; 37733212b535SSteve Capper unsigned long sbase = saddr & PUD_MASK; 37743212b535SSteve Capper unsigned long s_end = sbase + PUD_SIZE; 37753212b535SSteve Capper 37763212b535SSteve Capper /* Allow segments to share if only one is marked locked */ 37773212b535SSteve Capper unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED; 37783212b535SSteve Capper unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED; 37793212b535SSteve Capper 37803212b535SSteve Capper /* 37813212b535SSteve Capper * match the virtual addresses, permission and the alignment of the 37823212b535SSteve Capper * page table page. 37833212b535SSteve Capper */ 37843212b535SSteve Capper if (pmd_index(addr) != pmd_index(saddr) || 37853212b535SSteve Capper vm_flags != svm_flags || 37863212b535SSteve Capper sbase < svma->vm_start || svma->vm_end < s_end) 37873212b535SSteve Capper return 0; 37883212b535SSteve Capper 37893212b535SSteve Capper return saddr; 37903212b535SSteve Capper } 37913212b535SSteve Capper 37923212b535SSteve Capper static int vma_shareable(struct vm_area_struct *vma, unsigned long addr) 37933212b535SSteve Capper { 37943212b535SSteve Capper unsigned long base = addr & PUD_MASK; 37953212b535SSteve Capper unsigned long end = base + PUD_SIZE; 37963212b535SSteve Capper 37973212b535SSteve Capper /* 37983212b535SSteve Capper * check on proper vm_flags and page table alignment 37993212b535SSteve Capper */ 38003212b535SSteve Capper if (vma->vm_flags & VM_MAYSHARE && 38013212b535SSteve Capper vma->vm_start <= base && end <= vma->vm_end) 38023212b535SSteve Capper return 1; 38033212b535SSteve Capper return 0; 38043212b535SSteve Capper } 38053212b535SSteve Capper 38063212b535SSteve Capper /* 38073212b535SSteve Capper * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() 38083212b535SSteve Capper * and returns the corresponding pte. While this is not necessary for the 38093212b535SSteve Capper * !shared pmd case because we can allocate the pmd later as well, it makes the 38103212b535SSteve Capper * code much cleaner. pmd allocation is essential for the shared case because 3811c8c06efaSDavidlohr Bueso * pud has to be populated inside the same i_mmap_rwsem section - otherwise 38123212b535SSteve Capper * racing tasks could either miss the sharing (see huge_pte_offset) or select a 38133212b535SSteve Capper * bad pmd for sharing. 38143212b535SSteve Capper */ 38153212b535SSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 38163212b535SSteve Capper { 38173212b535SSteve Capper struct vm_area_struct *vma = find_vma(mm, addr); 38183212b535SSteve Capper struct address_space *mapping = vma->vm_file->f_mapping; 38193212b535SSteve Capper pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + 38203212b535SSteve Capper vma->vm_pgoff; 38213212b535SSteve Capper struct vm_area_struct *svma; 38223212b535SSteve Capper unsigned long saddr; 38233212b535SSteve Capper pte_t *spte = NULL; 38243212b535SSteve Capper pte_t *pte; 3825cb900f41SKirill A. Shutemov spinlock_t *ptl; 38263212b535SSteve Capper 38273212b535SSteve Capper if (!vma_shareable(vma, addr)) 38283212b535SSteve Capper return (pte_t *)pmd_alloc(mm, pud, addr); 38293212b535SSteve Capper 383083cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 38313212b535SSteve Capper vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { 38323212b535SSteve Capper if (svma == vma) 38333212b535SSteve Capper continue; 38343212b535SSteve Capper 38353212b535SSteve Capper saddr = page_table_shareable(svma, vma, addr, idx); 38363212b535SSteve Capper if (saddr) { 38373212b535SSteve Capper spte = huge_pte_offset(svma->vm_mm, saddr); 38383212b535SSteve Capper if (spte) { 3839dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 38403212b535SSteve Capper get_page(virt_to_page(spte)); 38413212b535SSteve Capper break; 38423212b535SSteve Capper } 38433212b535SSteve Capper } 38443212b535SSteve Capper } 38453212b535SSteve Capper 38463212b535SSteve Capper if (!spte) 38473212b535SSteve Capper goto out; 38483212b535SSteve Capper 3849cb900f41SKirill A. Shutemov ptl = huge_pte_lockptr(hstate_vma(vma), mm, spte); 3850cb900f41SKirill A. Shutemov spin_lock(ptl); 3851dc6c9a35SKirill A. Shutemov if (pud_none(*pud)) { 38523212b535SSteve Capper pud_populate(mm, pud, 38533212b535SSteve Capper (pmd_t *)((unsigned long)spte & PAGE_MASK)); 3854dc6c9a35SKirill A. Shutemov } else { 38553212b535SSteve Capper put_page(virt_to_page(spte)); 3856dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 3857dc6c9a35SKirill A. Shutemov } 3858cb900f41SKirill A. Shutemov spin_unlock(ptl); 38593212b535SSteve Capper out: 38603212b535SSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 386183cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 38623212b535SSteve Capper return pte; 38633212b535SSteve Capper } 38643212b535SSteve Capper 38653212b535SSteve Capper /* 38663212b535SSteve Capper * unmap huge page backed by shared pte. 38673212b535SSteve Capper * 38683212b535SSteve Capper * Hugetlb pte page is ref counted at the time of mapping. If pte is shared 38693212b535SSteve Capper * indicated by page_count > 1, unmap is achieved by clearing pud and 38703212b535SSteve Capper * decrementing the ref count. If count == 1, the pte page is not shared. 38713212b535SSteve Capper * 3872cb900f41SKirill A. Shutemov * called with page table lock held. 38733212b535SSteve Capper * 38743212b535SSteve Capper * returns: 1 successfully unmapped a shared pte page 38753212b535SSteve Capper * 0 the underlying pte page is not shared, or it is the last user 38763212b535SSteve Capper */ 38773212b535SSteve Capper int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 38783212b535SSteve Capper { 38793212b535SSteve Capper pgd_t *pgd = pgd_offset(mm, *addr); 38803212b535SSteve Capper pud_t *pud = pud_offset(pgd, *addr); 38813212b535SSteve Capper 38823212b535SSteve Capper BUG_ON(page_count(virt_to_page(ptep)) == 0); 38833212b535SSteve Capper if (page_count(virt_to_page(ptep)) == 1) 38843212b535SSteve Capper return 0; 38853212b535SSteve Capper 38863212b535SSteve Capper pud_clear(pud); 38873212b535SSteve Capper put_page(virt_to_page(ptep)); 3888dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(mm); 38893212b535SSteve Capper *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE; 38903212b535SSteve Capper return 1; 38913212b535SSteve Capper } 38929e5fc74cSSteve Capper #define want_pmd_share() (1) 38939e5fc74cSSteve Capper #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 38949e5fc74cSSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 38959e5fc74cSSteve Capper { 38969e5fc74cSSteve Capper return NULL; 38979e5fc74cSSteve Capper } 3898e81f2d22SZhang Zhen 3899e81f2d22SZhang Zhen int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 3900e81f2d22SZhang Zhen { 3901e81f2d22SZhang Zhen return 0; 3902e81f2d22SZhang Zhen } 39039e5fc74cSSteve Capper #define want_pmd_share() (0) 39043212b535SSteve Capper #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 39053212b535SSteve Capper 39069e5fc74cSSteve Capper #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 39079e5fc74cSSteve Capper pte_t *huge_pte_alloc(struct mm_struct *mm, 39089e5fc74cSSteve Capper unsigned long addr, unsigned long sz) 39099e5fc74cSSteve Capper { 39109e5fc74cSSteve Capper pgd_t *pgd; 39119e5fc74cSSteve Capper pud_t *pud; 39129e5fc74cSSteve Capper pte_t *pte = NULL; 39139e5fc74cSSteve Capper 39149e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 39159e5fc74cSSteve Capper pud = pud_alloc(mm, pgd, addr); 39169e5fc74cSSteve Capper if (pud) { 39179e5fc74cSSteve Capper if (sz == PUD_SIZE) { 39189e5fc74cSSteve Capper pte = (pte_t *)pud; 39199e5fc74cSSteve Capper } else { 39209e5fc74cSSteve Capper BUG_ON(sz != PMD_SIZE); 39219e5fc74cSSteve Capper if (want_pmd_share() && pud_none(*pud)) 39229e5fc74cSSteve Capper pte = huge_pmd_share(mm, addr, pud); 39239e5fc74cSSteve Capper else 39249e5fc74cSSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 39259e5fc74cSSteve Capper } 39269e5fc74cSSteve Capper } 39279e5fc74cSSteve Capper BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte)); 39289e5fc74cSSteve Capper 39299e5fc74cSSteve Capper return pte; 39309e5fc74cSSteve Capper } 39319e5fc74cSSteve Capper 39329e5fc74cSSteve Capper pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) 39339e5fc74cSSteve Capper { 39349e5fc74cSSteve Capper pgd_t *pgd; 39359e5fc74cSSteve Capper pud_t *pud; 39369e5fc74cSSteve Capper pmd_t *pmd = NULL; 39379e5fc74cSSteve Capper 39389e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 39399e5fc74cSSteve Capper if (pgd_present(*pgd)) { 39409e5fc74cSSteve Capper pud = pud_offset(pgd, addr); 39419e5fc74cSSteve Capper if (pud_present(*pud)) { 39429e5fc74cSSteve Capper if (pud_huge(*pud)) 39439e5fc74cSSteve Capper return (pte_t *)pud; 39449e5fc74cSSteve Capper pmd = pmd_offset(pud, addr); 39459e5fc74cSSteve Capper } 39469e5fc74cSSteve Capper } 39479e5fc74cSSteve Capper return (pte_t *) pmd; 39489e5fc74cSSteve Capper } 39499e5fc74cSSteve Capper 395061f77edaSNaoya Horiguchi #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 395161f77edaSNaoya Horiguchi 395261f77edaSNaoya Horiguchi /* 395361f77edaSNaoya Horiguchi * These functions are overwritable if your architecture needs its own 395461f77edaSNaoya Horiguchi * behavior. 395561f77edaSNaoya Horiguchi */ 395661f77edaSNaoya Horiguchi struct page * __weak 395761f77edaSNaoya Horiguchi follow_huge_addr(struct mm_struct *mm, unsigned long address, 395861f77edaSNaoya Horiguchi int write) 395961f77edaSNaoya Horiguchi { 396061f77edaSNaoya Horiguchi return ERR_PTR(-EINVAL); 396161f77edaSNaoya Horiguchi } 396261f77edaSNaoya Horiguchi 396361f77edaSNaoya Horiguchi struct page * __weak 39649e5fc74cSSteve Capper follow_huge_pmd(struct mm_struct *mm, unsigned long address, 3965e66f17ffSNaoya Horiguchi pmd_t *pmd, int flags) 39669e5fc74cSSteve Capper { 3967e66f17ffSNaoya Horiguchi struct page *page = NULL; 3968e66f17ffSNaoya Horiguchi spinlock_t *ptl; 3969e66f17ffSNaoya Horiguchi retry: 3970e66f17ffSNaoya Horiguchi ptl = pmd_lockptr(mm, pmd); 3971e66f17ffSNaoya Horiguchi spin_lock(ptl); 3972e66f17ffSNaoya Horiguchi /* 3973e66f17ffSNaoya Horiguchi * make sure that the address range covered by this pmd is not 3974e66f17ffSNaoya Horiguchi * unmapped from other threads. 3975e66f17ffSNaoya Horiguchi */ 3976e66f17ffSNaoya Horiguchi if (!pmd_huge(*pmd)) 3977e66f17ffSNaoya Horiguchi goto out; 3978e66f17ffSNaoya Horiguchi if (pmd_present(*pmd)) { 397997534127SGerald Schaefer page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT); 3980e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 3981e66f17ffSNaoya Horiguchi get_page(page); 3982e66f17ffSNaoya Horiguchi } else { 3983e66f17ffSNaoya Horiguchi if (is_hugetlb_entry_migration(huge_ptep_get((pte_t *)pmd))) { 3984e66f17ffSNaoya Horiguchi spin_unlock(ptl); 3985e66f17ffSNaoya Horiguchi __migration_entry_wait(mm, (pte_t *)pmd, ptl); 3986e66f17ffSNaoya Horiguchi goto retry; 3987e66f17ffSNaoya Horiguchi } 3988e66f17ffSNaoya Horiguchi /* 3989e66f17ffSNaoya Horiguchi * hwpoisoned entry is treated as no_page_table in 3990e66f17ffSNaoya Horiguchi * follow_page_mask(). 3991e66f17ffSNaoya Horiguchi */ 3992e66f17ffSNaoya Horiguchi } 3993e66f17ffSNaoya Horiguchi out: 3994e66f17ffSNaoya Horiguchi spin_unlock(ptl); 39959e5fc74cSSteve Capper return page; 39969e5fc74cSSteve Capper } 39979e5fc74cSSteve Capper 399861f77edaSNaoya Horiguchi struct page * __weak 39999e5fc74cSSteve Capper follow_huge_pud(struct mm_struct *mm, unsigned long address, 4000e66f17ffSNaoya Horiguchi pud_t *pud, int flags) 40019e5fc74cSSteve Capper { 4002e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 4003e66f17ffSNaoya Horiguchi return NULL; 40049e5fc74cSSteve Capper 4005e66f17ffSNaoya Horiguchi return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT); 40069e5fc74cSSteve Capper } 40079e5fc74cSSteve Capper 4008d5bd9106SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE 4009d5bd9106SAndi Kleen 401093f70f90SNaoya Horiguchi /* 401193f70f90SNaoya Horiguchi * This function is called from memory failure code. 401293f70f90SNaoya Horiguchi * Assume the caller holds page lock of the head page. 401393f70f90SNaoya Horiguchi */ 40146de2b1aaSNaoya Horiguchi int dequeue_hwpoisoned_huge_page(struct page *hpage) 401593f70f90SNaoya Horiguchi { 401693f70f90SNaoya Horiguchi struct hstate *h = page_hstate(hpage); 401793f70f90SNaoya Horiguchi int nid = page_to_nid(hpage); 40186de2b1aaSNaoya Horiguchi int ret = -EBUSY; 401993f70f90SNaoya Horiguchi 402093f70f90SNaoya Horiguchi spin_lock(&hugetlb_lock); 40217e1f049eSNaoya Horiguchi /* 40227e1f049eSNaoya Horiguchi * Just checking !page_huge_active is not enough, because that could be 40237e1f049eSNaoya Horiguchi * an isolated/hwpoisoned hugepage (which have >0 refcount). 40247e1f049eSNaoya Horiguchi */ 40257e1f049eSNaoya Horiguchi if (!page_huge_active(hpage) && !page_count(hpage)) { 402656f2fb14SNaoya Horiguchi /* 402756f2fb14SNaoya Horiguchi * Hwpoisoned hugepage isn't linked to activelist or freelist, 402856f2fb14SNaoya Horiguchi * but dangling hpage->lru can trigger list-debug warnings 402956f2fb14SNaoya Horiguchi * (this happens when we call unpoison_memory() on it), 403056f2fb14SNaoya Horiguchi * so let it point to itself with list_del_init(). 403156f2fb14SNaoya Horiguchi */ 403256f2fb14SNaoya Horiguchi list_del_init(&hpage->lru); 40338c6c2ecbSNaoya Horiguchi set_page_refcounted(hpage); 403493f70f90SNaoya Horiguchi h->free_huge_pages--; 403593f70f90SNaoya Horiguchi h->free_huge_pages_node[nid]--; 40366de2b1aaSNaoya Horiguchi ret = 0; 403793f70f90SNaoya Horiguchi } 40386de2b1aaSNaoya Horiguchi spin_unlock(&hugetlb_lock); 40396de2b1aaSNaoya Horiguchi return ret; 40406de2b1aaSNaoya Horiguchi } 40416de2b1aaSNaoya Horiguchi #endif 404231caf665SNaoya Horiguchi 404331caf665SNaoya Horiguchi bool isolate_huge_page(struct page *page, struct list_head *list) 404431caf665SNaoya Horiguchi { 4045bcc54222SNaoya Horiguchi bool ret = true; 4046bcc54222SNaoya Horiguchi 4047309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 404831caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 4049bcc54222SNaoya Horiguchi if (!page_huge_active(page) || !get_page_unless_zero(page)) { 4050bcc54222SNaoya Horiguchi ret = false; 4051bcc54222SNaoya Horiguchi goto unlock; 4052bcc54222SNaoya Horiguchi } 4053bcc54222SNaoya Horiguchi clear_page_huge_active(page); 405431caf665SNaoya Horiguchi list_move_tail(&page->lru, list); 4055bcc54222SNaoya Horiguchi unlock: 405631caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 4057bcc54222SNaoya Horiguchi return ret; 405831caf665SNaoya Horiguchi } 405931caf665SNaoya Horiguchi 406031caf665SNaoya Horiguchi void putback_active_hugepage(struct page *page) 406131caf665SNaoya Horiguchi { 4062309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 406331caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 4064bcc54222SNaoya Horiguchi set_page_huge_active(page); 406531caf665SNaoya Horiguchi list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist); 406631caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 406731caf665SNaoya Horiguchi put_page(page); 406831caf665SNaoya Horiguchi } 4069