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; 67c672c7f2SMike Kravetz struct mutex *hugetlb_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 2435e911373SMike Kravetz * map. In the normal case, existing regions will be expanded 2445e911373SMike Kravetz * to accommodate the specified range. Sufficient regions should 2455e911373SMike Kravetz * exist for expansion due to the previous call to region_chg 2465e911373SMike Kravetz * with the same range. However, it is possible that region_del 2475e911373SMike Kravetz * could have been called after region_chg and modifed the map 2485e911373SMike Kravetz * in such a way that no region exists to be expanded. In this 2495e911373SMike Kravetz * case, pull a region descriptor from the cache associated with 2505e911373SMike Kravetz * the map and use that for the new range. 251cf3ad20bSMike Kravetz * 252cf3ad20bSMike Kravetz * Return the number of new huge pages added to the map. This 253cf3ad20bSMike Kravetz * number is greater than or equal to zero. 2541dd308a7SMike Kravetz */ 2551406ec9bSJoonsoo Kim static long region_add(struct resv_map *resv, long f, long t) 25696822904SAndy Whitcroft { 2571406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 25896822904SAndy Whitcroft struct file_region *rg, *nrg, *trg; 259cf3ad20bSMike Kravetz long add = 0; 26096822904SAndy Whitcroft 2617b24d861SDavidlohr Bueso spin_lock(&resv->lock); 26296822904SAndy Whitcroft /* Locate the region we are either in or before. */ 26396822904SAndy Whitcroft list_for_each_entry(rg, head, link) 26496822904SAndy Whitcroft if (f <= rg->to) 26596822904SAndy Whitcroft break; 26696822904SAndy Whitcroft 2675e911373SMike Kravetz /* 2685e911373SMike Kravetz * If no region exists which can be expanded to include the 2695e911373SMike Kravetz * specified range, the list must have been modified by an 2705e911373SMike Kravetz * interleving call to region_del(). Pull a region descriptor 2715e911373SMike Kravetz * from the cache and use it for this range. 2725e911373SMike Kravetz */ 2735e911373SMike Kravetz if (&rg->link == head || t < rg->from) { 2745e911373SMike Kravetz VM_BUG_ON(resv->region_cache_count <= 0); 2755e911373SMike Kravetz 2765e911373SMike Kravetz resv->region_cache_count--; 2775e911373SMike Kravetz nrg = list_first_entry(&resv->region_cache, struct file_region, 2785e911373SMike Kravetz link); 2795e911373SMike Kravetz list_del(&nrg->link); 2805e911373SMike Kravetz 2815e911373SMike Kravetz nrg->from = f; 2825e911373SMike Kravetz nrg->to = t; 2835e911373SMike Kravetz list_add(&nrg->link, rg->link.prev); 2845e911373SMike Kravetz 2855e911373SMike Kravetz add += t - f; 2865e911373SMike Kravetz goto out_locked; 2875e911373SMike Kravetz } 2885e911373SMike Kravetz 28996822904SAndy Whitcroft /* Round our left edge to the current segment if it encloses us. */ 29096822904SAndy Whitcroft if (f > rg->from) 29196822904SAndy Whitcroft f = rg->from; 29296822904SAndy Whitcroft 29396822904SAndy Whitcroft /* Check for and consume any regions we now overlap with. */ 29496822904SAndy Whitcroft nrg = rg; 29596822904SAndy Whitcroft list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 29696822904SAndy Whitcroft if (&rg->link == head) 29796822904SAndy Whitcroft break; 29896822904SAndy Whitcroft if (rg->from > t) 29996822904SAndy Whitcroft break; 30096822904SAndy Whitcroft 30196822904SAndy Whitcroft /* If this area reaches higher then extend our area to 30296822904SAndy Whitcroft * include it completely. If this is not the first area 30396822904SAndy Whitcroft * which we intend to reuse, free it. */ 30496822904SAndy Whitcroft if (rg->to > t) 30596822904SAndy Whitcroft t = rg->to; 30696822904SAndy Whitcroft if (rg != nrg) { 307cf3ad20bSMike Kravetz /* Decrement return value by the deleted range. 308cf3ad20bSMike Kravetz * Another range will span this area so that by 309cf3ad20bSMike Kravetz * end of routine add will be >= zero 310cf3ad20bSMike Kravetz */ 311cf3ad20bSMike Kravetz add -= (rg->to - rg->from); 31296822904SAndy Whitcroft list_del(&rg->link); 31396822904SAndy Whitcroft kfree(rg); 31496822904SAndy Whitcroft } 31596822904SAndy Whitcroft } 316cf3ad20bSMike Kravetz 317cf3ad20bSMike Kravetz add += (nrg->from - f); /* Added to beginning of region */ 31896822904SAndy Whitcroft nrg->from = f; 319cf3ad20bSMike Kravetz add += t - nrg->to; /* Added to end of region */ 32096822904SAndy Whitcroft nrg->to = t; 321cf3ad20bSMike Kravetz 3225e911373SMike Kravetz out_locked: 3235e911373SMike Kravetz resv->adds_in_progress--; 3247b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 325cf3ad20bSMike Kravetz VM_BUG_ON(add < 0); 326cf3ad20bSMike Kravetz return add; 32796822904SAndy Whitcroft } 32896822904SAndy Whitcroft 3291dd308a7SMike Kravetz /* 3301dd308a7SMike Kravetz * Examine the existing reserve map and determine how many 3311dd308a7SMike Kravetz * huge pages in the specified range [f, t) are NOT currently 3321dd308a7SMike Kravetz * represented. This routine is called before a subsequent 3331dd308a7SMike Kravetz * call to region_add that will actually modify the reserve 3341dd308a7SMike Kravetz * map to add the specified range [f, t). region_chg does 3351dd308a7SMike Kravetz * not change the number of huge pages represented by the 3361dd308a7SMike Kravetz * map. However, if the existing regions in the map can not 3371dd308a7SMike Kravetz * be expanded to represent the new range, a new file_region 3381dd308a7SMike Kravetz * structure is added to the map as a placeholder. This is 3391dd308a7SMike Kravetz * so that the subsequent region_add call will have all the 3401dd308a7SMike Kravetz * regions it needs and will not fail. 3411dd308a7SMike Kravetz * 3425e911373SMike Kravetz * Upon entry, region_chg will also examine the cache of region descriptors 3435e911373SMike Kravetz * associated with the map. If there are not enough descriptors cached, one 3445e911373SMike Kravetz * will be allocated for the in progress add operation. 3455e911373SMike Kravetz * 3465e911373SMike Kravetz * Returns the number of huge pages that need to be added to the existing 3475e911373SMike Kravetz * reservation map for the range [f, t). This number is greater or equal to 3485e911373SMike Kravetz * zero. -ENOMEM is returned if a new file_region structure or cache entry 3495e911373SMike Kravetz * is needed and can not be allocated. 3501dd308a7SMike Kravetz */ 3511406ec9bSJoonsoo Kim static long region_chg(struct resv_map *resv, long f, long t) 35296822904SAndy Whitcroft { 3531406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 3547b24d861SDavidlohr Bueso struct file_region *rg, *nrg = NULL; 35596822904SAndy Whitcroft long chg = 0; 35696822904SAndy Whitcroft 3577b24d861SDavidlohr Bueso retry: 3587b24d861SDavidlohr Bueso spin_lock(&resv->lock); 3595e911373SMike Kravetz retry_locked: 3605e911373SMike Kravetz resv->adds_in_progress++; 3615e911373SMike Kravetz 3625e911373SMike Kravetz /* 3635e911373SMike Kravetz * Check for sufficient descriptors in the cache to accommodate 3645e911373SMike Kravetz * the number of in progress add operations. 3655e911373SMike Kravetz */ 3665e911373SMike Kravetz if (resv->adds_in_progress > resv->region_cache_count) { 3675e911373SMike Kravetz struct file_region *trg; 3685e911373SMike Kravetz 3695e911373SMike Kravetz VM_BUG_ON(resv->adds_in_progress - resv->region_cache_count > 1); 3705e911373SMike Kravetz /* Must drop lock to allocate a new descriptor. */ 3715e911373SMike Kravetz resv->adds_in_progress--; 3725e911373SMike Kravetz spin_unlock(&resv->lock); 3735e911373SMike Kravetz 3745e911373SMike Kravetz trg = kmalloc(sizeof(*trg), GFP_KERNEL); 3755e911373SMike Kravetz if (!trg) 3765e911373SMike Kravetz return -ENOMEM; 3775e911373SMike Kravetz 3785e911373SMike Kravetz spin_lock(&resv->lock); 3795e911373SMike Kravetz list_add(&trg->link, &resv->region_cache); 3805e911373SMike Kravetz resv->region_cache_count++; 3815e911373SMike Kravetz goto retry_locked; 3825e911373SMike Kravetz } 3835e911373SMike Kravetz 38496822904SAndy Whitcroft /* Locate the region we are before or in. */ 38596822904SAndy Whitcroft list_for_each_entry(rg, head, link) 38696822904SAndy Whitcroft if (f <= rg->to) 38796822904SAndy Whitcroft break; 38896822904SAndy Whitcroft 38996822904SAndy Whitcroft /* If we are below the current region then a new region is required. 39096822904SAndy Whitcroft * Subtle, allocate a new region at the position but make it zero 39196822904SAndy Whitcroft * size such that we can guarantee to record the reservation. */ 39296822904SAndy Whitcroft if (&rg->link == head || t < rg->from) { 3937b24d861SDavidlohr Bueso if (!nrg) { 3945e911373SMike Kravetz resv->adds_in_progress--; 3957b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 39696822904SAndy Whitcroft nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 39796822904SAndy Whitcroft if (!nrg) 39896822904SAndy Whitcroft return -ENOMEM; 3997b24d861SDavidlohr Bueso 40096822904SAndy Whitcroft nrg->from = f; 40196822904SAndy Whitcroft nrg->to = f; 40296822904SAndy Whitcroft INIT_LIST_HEAD(&nrg->link); 4037b24d861SDavidlohr Bueso goto retry; 4047b24d861SDavidlohr Bueso } 40596822904SAndy Whitcroft 4067b24d861SDavidlohr Bueso list_add(&nrg->link, rg->link.prev); 4077b24d861SDavidlohr Bueso chg = t - f; 4087b24d861SDavidlohr Bueso goto out_nrg; 40996822904SAndy Whitcroft } 41096822904SAndy Whitcroft 41196822904SAndy Whitcroft /* Round our left edge to the current segment if it encloses us. */ 41296822904SAndy Whitcroft if (f > rg->from) 41396822904SAndy Whitcroft f = rg->from; 41496822904SAndy Whitcroft chg = t - f; 41596822904SAndy Whitcroft 41696822904SAndy Whitcroft /* Check for and consume any regions we now overlap with. */ 41796822904SAndy Whitcroft list_for_each_entry(rg, rg->link.prev, link) { 41896822904SAndy Whitcroft if (&rg->link == head) 41996822904SAndy Whitcroft break; 42096822904SAndy Whitcroft if (rg->from > t) 4217b24d861SDavidlohr Bueso goto out; 42296822904SAndy Whitcroft 42325985edcSLucas De Marchi /* We overlap with this area, if it extends further than 42496822904SAndy Whitcroft * us then we must extend ourselves. Account for its 42596822904SAndy Whitcroft * existing reservation. */ 42696822904SAndy Whitcroft if (rg->to > t) { 42796822904SAndy Whitcroft chg += rg->to - t; 42896822904SAndy Whitcroft t = rg->to; 42996822904SAndy Whitcroft } 43096822904SAndy Whitcroft chg -= rg->to - rg->from; 43196822904SAndy Whitcroft } 4327b24d861SDavidlohr Bueso 4337b24d861SDavidlohr Bueso out: 4347b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 4357b24d861SDavidlohr Bueso /* We already know we raced and no longer need the new region */ 4367b24d861SDavidlohr Bueso kfree(nrg); 4377b24d861SDavidlohr Bueso return chg; 4387b24d861SDavidlohr Bueso out_nrg: 4397b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 44096822904SAndy Whitcroft return chg; 44196822904SAndy Whitcroft } 44296822904SAndy Whitcroft 4431dd308a7SMike Kravetz /* 4445e911373SMike Kravetz * Abort the in progress add operation. The adds_in_progress field 4455e911373SMike Kravetz * of the resv_map keeps track of the operations in progress between 4465e911373SMike Kravetz * calls to region_chg and region_add. Operations are sometimes 4475e911373SMike Kravetz * aborted after the call to region_chg. In such cases, region_abort 4485e911373SMike Kravetz * is called to decrement the adds_in_progress counter. 4495e911373SMike Kravetz * 4505e911373SMike Kravetz * NOTE: The range arguments [f, t) are not needed or used in this 4515e911373SMike Kravetz * routine. They are kept to make reading the calling code easier as 4525e911373SMike Kravetz * arguments will match the associated region_chg call. 4535e911373SMike Kravetz */ 4545e911373SMike Kravetz static void region_abort(struct resv_map *resv, long f, long t) 4555e911373SMike Kravetz { 4565e911373SMike Kravetz spin_lock(&resv->lock); 4575e911373SMike Kravetz VM_BUG_ON(!resv->region_cache_count); 4585e911373SMike Kravetz resv->adds_in_progress--; 4595e911373SMike Kravetz spin_unlock(&resv->lock); 4605e911373SMike Kravetz } 4615e911373SMike Kravetz 4625e911373SMike Kravetz /* 463feba16e2SMike Kravetz * Delete the specified range [f, t) from the reserve map. If the 464feba16e2SMike Kravetz * t parameter is LONG_MAX, this indicates that ALL regions after f 465feba16e2SMike Kravetz * should be deleted. Locate the regions which intersect [f, t) 466feba16e2SMike Kravetz * and either trim, delete or split the existing regions. 467feba16e2SMike Kravetz * 468feba16e2SMike Kravetz * Returns the number of huge pages deleted from the reserve map. 469feba16e2SMike Kravetz * In the normal case, the return value is zero or more. In the 470feba16e2SMike Kravetz * case where a region must be split, a new region descriptor must 471feba16e2SMike Kravetz * be allocated. If the allocation fails, -ENOMEM will be returned. 472feba16e2SMike Kravetz * NOTE: If the parameter t == LONG_MAX, then we will never split 473feba16e2SMike Kravetz * a region and possibly return -ENOMEM. Callers specifying 474feba16e2SMike Kravetz * t == LONG_MAX do not need to check for -ENOMEM error. 4751dd308a7SMike Kravetz */ 476feba16e2SMike Kravetz static long region_del(struct resv_map *resv, long f, long t) 47796822904SAndy Whitcroft { 4781406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 47996822904SAndy Whitcroft struct file_region *rg, *trg; 480feba16e2SMike Kravetz struct file_region *nrg = NULL; 481feba16e2SMike Kravetz long del = 0; 48296822904SAndy Whitcroft 483feba16e2SMike Kravetz retry: 4847b24d861SDavidlohr Bueso spin_lock(&resv->lock); 485feba16e2SMike Kravetz list_for_each_entry_safe(rg, trg, head, link) { 486feba16e2SMike Kravetz if (rg->to <= f) 487feba16e2SMike Kravetz continue; 488feba16e2SMike Kravetz if (rg->from >= t) 48996822904SAndy Whitcroft break; 49096822904SAndy Whitcroft 491feba16e2SMike Kravetz if (f > rg->from && t < rg->to) { /* Must split region */ 492feba16e2SMike Kravetz /* 493feba16e2SMike Kravetz * Check for an entry in the cache before dropping 494feba16e2SMike Kravetz * lock and attempting allocation. 495feba16e2SMike Kravetz */ 496feba16e2SMike Kravetz if (!nrg && 497feba16e2SMike Kravetz resv->region_cache_count > resv->adds_in_progress) { 498feba16e2SMike Kravetz nrg = list_first_entry(&resv->region_cache, 499feba16e2SMike Kravetz struct file_region, 500feba16e2SMike Kravetz link); 501feba16e2SMike Kravetz list_del(&nrg->link); 502feba16e2SMike Kravetz resv->region_cache_count--; 50396822904SAndy Whitcroft } 50496822904SAndy Whitcroft 505feba16e2SMike Kravetz if (!nrg) { 506feba16e2SMike Kravetz spin_unlock(&resv->lock); 507feba16e2SMike Kravetz nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 508feba16e2SMike Kravetz if (!nrg) 509feba16e2SMike Kravetz return -ENOMEM; 510feba16e2SMike Kravetz goto retry; 511feba16e2SMike Kravetz } 512feba16e2SMike Kravetz 513feba16e2SMike Kravetz del += t - f; 514feba16e2SMike Kravetz 515feba16e2SMike Kravetz /* New entry for end of split region */ 516feba16e2SMike Kravetz nrg->from = t; 517feba16e2SMike Kravetz nrg->to = rg->to; 518feba16e2SMike Kravetz INIT_LIST_HEAD(&nrg->link); 519feba16e2SMike Kravetz 520feba16e2SMike Kravetz /* Original entry is trimmed */ 521feba16e2SMike Kravetz rg->to = f; 522feba16e2SMike Kravetz 523feba16e2SMike Kravetz list_add(&nrg->link, &rg->link); 524feba16e2SMike Kravetz nrg = NULL; 52596822904SAndy Whitcroft break; 526feba16e2SMike Kravetz } 527feba16e2SMike Kravetz 528feba16e2SMike Kravetz if (f <= rg->from && t >= rg->to) { /* Remove entire region */ 529feba16e2SMike Kravetz del += rg->to - rg->from; 53096822904SAndy Whitcroft list_del(&rg->link); 53196822904SAndy Whitcroft kfree(rg); 532feba16e2SMike Kravetz continue; 53396822904SAndy Whitcroft } 5347b24d861SDavidlohr Bueso 535feba16e2SMike Kravetz if (f <= rg->from) { /* Trim beginning of region */ 536feba16e2SMike Kravetz del += t - rg->from; 537feba16e2SMike Kravetz rg->from = t; 538feba16e2SMike Kravetz } else { /* Trim end of region */ 539feba16e2SMike Kravetz del += rg->to - f; 540feba16e2SMike Kravetz rg->to = f; 541feba16e2SMike Kravetz } 542feba16e2SMike Kravetz } 543feba16e2SMike Kravetz 5447b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 545feba16e2SMike Kravetz kfree(nrg); 546feba16e2SMike Kravetz return del; 54796822904SAndy Whitcroft } 54896822904SAndy Whitcroft 5491dd308a7SMike Kravetz /* 5501dd308a7SMike Kravetz * Count and return the number of huge pages in the reserve map 5511dd308a7SMike Kravetz * that intersect with the range [f, t). 5521dd308a7SMike Kravetz */ 5531406ec9bSJoonsoo Kim static long region_count(struct resv_map *resv, long f, long t) 55484afd99bSAndy Whitcroft { 5551406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 55684afd99bSAndy Whitcroft struct file_region *rg; 55784afd99bSAndy Whitcroft long chg = 0; 55884afd99bSAndy Whitcroft 5597b24d861SDavidlohr Bueso spin_lock(&resv->lock); 56084afd99bSAndy Whitcroft /* Locate each segment we overlap with, and count that overlap. */ 56184afd99bSAndy Whitcroft list_for_each_entry(rg, head, link) { 562f2135a4aSWang Sheng-Hui long seg_from; 563f2135a4aSWang Sheng-Hui long seg_to; 56484afd99bSAndy Whitcroft 56584afd99bSAndy Whitcroft if (rg->to <= f) 56684afd99bSAndy Whitcroft continue; 56784afd99bSAndy Whitcroft if (rg->from >= t) 56884afd99bSAndy Whitcroft break; 56984afd99bSAndy Whitcroft 57084afd99bSAndy Whitcroft seg_from = max(rg->from, f); 57184afd99bSAndy Whitcroft seg_to = min(rg->to, t); 57284afd99bSAndy Whitcroft 57384afd99bSAndy Whitcroft chg += seg_to - seg_from; 57484afd99bSAndy Whitcroft } 5757b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 57684afd99bSAndy Whitcroft 57784afd99bSAndy Whitcroft return chg; 57884afd99bSAndy Whitcroft } 57984afd99bSAndy Whitcroft 58096822904SAndy Whitcroft /* 581e7c4b0bfSAndy Whitcroft * Convert the address within this vma to the page offset within 582e7c4b0bfSAndy Whitcroft * the mapping, in pagecache page units; huge pages here. 583e7c4b0bfSAndy Whitcroft */ 584a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h, 585a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 586e7c4b0bfSAndy Whitcroft { 587a5516438SAndi Kleen return ((address - vma->vm_start) >> huge_page_shift(h)) + 588a5516438SAndi Kleen (vma->vm_pgoff >> huge_page_order(h)); 589e7c4b0bfSAndy Whitcroft } 590e7c4b0bfSAndy Whitcroft 5910fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma, 5920fe6e20bSNaoya Horiguchi unsigned long address) 5930fe6e20bSNaoya Horiguchi { 5940fe6e20bSNaoya Horiguchi return vma_hugecache_offset(hstate_vma(vma), vma, address); 5950fe6e20bSNaoya Horiguchi } 5960fe6e20bSNaoya Horiguchi 59784afd99bSAndy Whitcroft /* 59808fba699SMel Gorman * Return the size of the pages allocated when backing a VMA. In the majority 59908fba699SMel Gorman * cases this will be same size as used by the page table entries. 60008fba699SMel Gorman */ 60108fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) 60208fba699SMel Gorman { 60308fba699SMel Gorman struct hstate *hstate; 60408fba699SMel Gorman 60508fba699SMel Gorman if (!is_vm_hugetlb_page(vma)) 60608fba699SMel Gorman return PAGE_SIZE; 60708fba699SMel Gorman 60808fba699SMel Gorman hstate = hstate_vma(vma); 60908fba699SMel Gorman 6102415cf12SWanpeng Li return 1UL << huge_page_shift(hstate); 61108fba699SMel Gorman } 612f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize); 61308fba699SMel Gorman 61408fba699SMel Gorman /* 6153340289dSMel Gorman * Return the page size being used by the MMU to back a VMA. In the majority 6163340289dSMel Gorman * of cases, the page size used by the kernel matches the MMU size. On 6173340289dSMel Gorman * architectures where it differs, an architecture-specific version of this 6183340289dSMel Gorman * function is required. 6193340289dSMel Gorman */ 6203340289dSMel Gorman #ifndef vma_mmu_pagesize 6213340289dSMel Gorman unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) 6223340289dSMel Gorman { 6233340289dSMel Gorman return vma_kernel_pagesize(vma); 6243340289dSMel Gorman } 6253340289dSMel Gorman #endif 6263340289dSMel Gorman 6273340289dSMel Gorman /* 62884afd99bSAndy Whitcroft * Flags for MAP_PRIVATE reservations. These are stored in the bottom 62984afd99bSAndy Whitcroft * bits of the reservation map pointer, which are always clear due to 63084afd99bSAndy Whitcroft * alignment. 63184afd99bSAndy Whitcroft */ 63284afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER (1UL << 0) 63384afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1) 63404f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) 63584afd99bSAndy Whitcroft 636a1e78772SMel Gorman /* 637a1e78772SMel Gorman * These helpers are used to track how many pages are reserved for 638a1e78772SMel Gorman * faults in a MAP_PRIVATE mapping. Only the process that called mmap() 639a1e78772SMel Gorman * is guaranteed to have their future faults succeed. 640a1e78772SMel Gorman * 641a1e78772SMel Gorman * With the exception of reset_vma_resv_huge_pages() which is called at fork(), 642a1e78772SMel Gorman * the reserve counters are updated with the hugetlb_lock held. It is safe 643a1e78772SMel Gorman * to reset the VMA at fork() time as it is not in use yet and there is no 644a1e78772SMel Gorman * chance of the global counters getting corrupted as a result of the values. 64584afd99bSAndy Whitcroft * 64684afd99bSAndy Whitcroft * The private mapping reservation is represented in a subtly different 64784afd99bSAndy Whitcroft * manner to a shared mapping. A shared mapping has a region map associated 64884afd99bSAndy Whitcroft * with the underlying file, this region map represents the backing file 64984afd99bSAndy Whitcroft * pages which have ever had a reservation assigned which this persists even 65084afd99bSAndy Whitcroft * after the page is instantiated. A private mapping has a region map 65184afd99bSAndy Whitcroft * associated with the original mmap which is attached to all VMAs which 65284afd99bSAndy Whitcroft * reference it, this region map represents those offsets which have consumed 65384afd99bSAndy Whitcroft * reservation ie. where pages have been instantiated. 654a1e78772SMel Gorman */ 655e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma) 656e7c4b0bfSAndy Whitcroft { 657e7c4b0bfSAndy Whitcroft return (unsigned long)vma->vm_private_data; 658e7c4b0bfSAndy Whitcroft } 659e7c4b0bfSAndy Whitcroft 660e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma, 661e7c4b0bfSAndy Whitcroft unsigned long value) 662e7c4b0bfSAndy Whitcroft { 663e7c4b0bfSAndy Whitcroft vma->vm_private_data = (void *)value; 664e7c4b0bfSAndy Whitcroft } 665e7c4b0bfSAndy Whitcroft 6669119a41eSJoonsoo Kim struct resv_map *resv_map_alloc(void) 66784afd99bSAndy Whitcroft { 66884afd99bSAndy Whitcroft struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); 6695e911373SMike Kravetz struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL); 6705e911373SMike Kravetz 6715e911373SMike Kravetz if (!resv_map || !rg) { 6725e911373SMike Kravetz kfree(resv_map); 6735e911373SMike Kravetz kfree(rg); 67484afd99bSAndy Whitcroft return NULL; 6755e911373SMike Kravetz } 67684afd99bSAndy Whitcroft 67784afd99bSAndy Whitcroft kref_init(&resv_map->refs); 6787b24d861SDavidlohr Bueso spin_lock_init(&resv_map->lock); 67984afd99bSAndy Whitcroft INIT_LIST_HEAD(&resv_map->regions); 68084afd99bSAndy Whitcroft 6815e911373SMike Kravetz resv_map->adds_in_progress = 0; 6825e911373SMike Kravetz 6835e911373SMike Kravetz INIT_LIST_HEAD(&resv_map->region_cache); 6845e911373SMike Kravetz list_add(&rg->link, &resv_map->region_cache); 6855e911373SMike Kravetz resv_map->region_cache_count = 1; 6865e911373SMike Kravetz 68784afd99bSAndy Whitcroft return resv_map; 68884afd99bSAndy Whitcroft } 68984afd99bSAndy Whitcroft 6909119a41eSJoonsoo Kim void resv_map_release(struct kref *ref) 69184afd99bSAndy Whitcroft { 69284afd99bSAndy Whitcroft struct resv_map *resv_map = container_of(ref, struct resv_map, refs); 6935e911373SMike Kravetz struct list_head *head = &resv_map->region_cache; 6945e911373SMike Kravetz struct file_region *rg, *trg; 69584afd99bSAndy Whitcroft 69684afd99bSAndy Whitcroft /* Clear out any active regions before we release the map. */ 697feba16e2SMike Kravetz region_del(resv_map, 0, LONG_MAX); 6985e911373SMike Kravetz 6995e911373SMike Kravetz /* ... and any entries left in the cache */ 7005e911373SMike Kravetz list_for_each_entry_safe(rg, trg, head, link) { 7015e911373SMike Kravetz list_del(&rg->link); 7025e911373SMike Kravetz kfree(rg); 7035e911373SMike Kravetz } 7045e911373SMike Kravetz 7055e911373SMike Kravetz VM_BUG_ON(resv_map->adds_in_progress); 7065e911373SMike Kravetz 70784afd99bSAndy Whitcroft kfree(resv_map); 70884afd99bSAndy Whitcroft } 70984afd99bSAndy Whitcroft 7104e35f483SJoonsoo Kim static inline struct resv_map *inode_resv_map(struct inode *inode) 7114e35f483SJoonsoo Kim { 7124e35f483SJoonsoo Kim return inode->i_mapping->private_data; 7134e35f483SJoonsoo Kim } 7144e35f483SJoonsoo Kim 71584afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma) 716a1e78772SMel Gorman { 71781d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 7184e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) { 7194e35f483SJoonsoo Kim struct address_space *mapping = vma->vm_file->f_mapping; 7204e35f483SJoonsoo Kim struct inode *inode = mapping->host; 7214e35f483SJoonsoo Kim 7224e35f483SJoonsoo Kim return inode_resv_map(inode); 7234e35f483SJoonsoo Kim 7244e35f483SJoonsoo Kim } else { 72584afd99bSAndy Whitcroft return (struct resv_map *)(get_vma_private_data(vma) & 72684afd99bSAndy Whitcroft ~HPAGE_RESV_MASK); 7274e35f483SJoonsoo Kim } 728a1e78772SMel Gorman } 729a1e78772SMel Gorman 73084afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) 731a1e78772SMel Gorman { 73281d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 73381d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 734a1e78772SMel Gorman 73584afd99bSAndy Whitcroft set_vma_private_data(vma, (get_vma_private_data(vma) & 73684afd99bSAndy Whitcroft HPAGE_RESV_MASK) | (unsigned long)map); 73704f2cbe3SMel Gorman } 73804f2cbe3SMel Gorman 73904f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) 74004f2cbe3SMel Gorman { 74181d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 74281d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 743e7c4b0bfSAndy Whitcroft 744e7c4b0bfSAndy Whitcroft set_vma_private_data(vma, get_vma_private_data(vma) | flags); 74504f2cbe3SMel Gorman } 74604f2cbe3SMel Gorman 74704f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) 74804f2cbe3SMel Gorman { 74981d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 750e7c4b0bfSAndy Whitcroft 751e7c4b0bfSAndy Whitcroft return (get_vma_private_data(vma) & flag) != 0; 752a1e78772SMel Gorman } 753a1e78772SMel Gorman 75404f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ 755a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma) 756a1e78772SMel Gorman { 75781d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 758f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 759a1e78772SMel Gorman vma->vm_private_data = (void *)0; 760a1e78772SMel Gorman } 761a1e78772SMel Gorman 762a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */ 763559ec2f8SNicholas Krause static bool vma_has_reserves(struct vm_area_struct *vma, long chg) 764a1e78772SMel Gorman { 765af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_NORESERVE) { 766af0ed73eSJoonsoo Kim /* 767af0ed73eSJoonsoo Kim * This address is already reserved by other process(chg == 0), 768af0ed73eSJoonsoo Kim * so, we should decrement reserved count. Without decrementing, 769af0ed73eSJoonsoo Kim * reserve count remains after releasing inode, because this 770af0ed73eSJoonsoo Kim * allocated page will go into page cache and is regarded as 771af0ed73eSJoonsoo Kim * coming from reserved pool in releasing step. Currently, we 772af0ed73eSJoonsoo Kim * don't have any other solution to deal with this situation 773af0ed73eSJoonsoo Kim * properly, so add work-around here. 774af0ed73eSJoonsoo Kim */ 775af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE && chg == 0) 776559ec2f8SNicholas Krause return true; 777af0ed73eSJoonsoo Kim else 778559ec2f8SNicholas Krause return false; 779af0ed73eSJoonsoo Kim } 780a63884e9SJoonsoo Kim 781a63884e9SJoonsoo Kim /* Shared mappings always use reserves */ 782f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) 783559ec2f8SNicholas Krause return true; 784a63884e9SJoonsoo Kim 785a63884e9SJoonsoo Kim /* 786a63884e9SJoonsoo Kim * Only the process that called mmap() has reserves for 787a63884e9SJoonsoo Kim * private mappings. 788a63884e9SJoonsoo Kim */ 7897f09ca51SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 790559ec2f8SNicholas Krause return true; 791a63884e9SJoonsoo Kim 792559ec2f8SNicholas Krause return false; 793a1e78772SMel Gorman } 794a1e78772SMel Gorman 795a5516438SAndi Kleen static void enqueue_huge_page(struct hstate *h, struct page *page) 7961da177e4SLinus Torvalds { 7971da177e4SLinus Torvalds int nid = page_to_nid(page); 7980edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_freelists[nid]); 799a5516438SAndi Kleen h->free_huge_pages++; 800a5516438SAndi Kleen h->free_huge_pages_node[nid]++; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 803bf50bab2SNaoya Horiguchi static struct page *dequeue_huge_page_node(struct hstate *h, int nid) 804bf50bab2SNaoya Horiguchi { 805bf50bab2SNaoya Horiguchi struct page *page; 806bf50bab2SNaoya Horiguchi 807c8721bbbSNaoya Horiguchi list_for_each_entry(page, &h->hugepage_freelists[nid], lru) 808c8721bbbSNaoya Horiguchi if (!is_migrate_isolate_page(page)) 809c8721bbbSNaoya Horiguchi break; 810c8721bbbSNaoya Horiguchi /* 811c8721bbbSNaoya Horiguchi * if 'non-isolated free hugepage' not found on the list, 812c8721bbbSNaoya Horiguchi * the allocation fails. 813c8721bbbSNaoya Horiguchi */ 814c8721bbbSNaoya Horiguchi if (&h->hugepage_freelists[nid] == &page->lru) 815bf50bab2SNaoya Horiguchi return NULL; 8160edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 817a9869b83SNaoya Horiguchi set_page_refcounted(page); 818bf50bab2SNaoya Horiguchi h->free_huge_pages--; 819bf50bab2SNaoya Horiguchi h->free_huge_pages_node[nid]--; 820bf50bab2SNaoya Horiguchi return page; 821bf50bab2SNaoya Horiguchi } 822bf50bab2SNaoya Horiguchi 82386cdb465SNaoya Horiguchi /* Movability of hugepages depends on migration support. */ 82486cdb465SNaoya Horiguchi static inline gfp_t htlb_alloc_mask(struct hstate *h) 82586cdb465SNaoya Horiguchi { 826100873d7SNaoya Horiguchi if (hugepages_treat_as_movable || hugepage_migration_supported(h)) 82786cdb465SNaoya Horiguchi return GFP_HIGHUSER_MOVABLE; 82886cdb465SNaoya Horiguchi else 82986cdb465SNaoya Horiguchi return GFP_HIGHUSER; 83086cdb465SNaoya Horiguchi } 83186cdb465SNaoya Horiguchi 832a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h, 833a5516438SAndi Kleen struct vm_area_struct *vma, 834af0ed73eSJoonsoo Kim unsigned long address, int avoid_reserve, 835af0ed73eSJoonsoo Kim long chg) 8361da177e4SLinus Torvalds { 837b1c12cbcSKonstantin Khlebnikov struct page *page = NULL; 838480eccf9SLee Schermerhorn struct mempolicy *mpol; 83919770b32SMel Gorman nodemask_t *nodemask; 840c0ff7453SMiao Xie struct zonelist *zonelist; 841dd1a239fSMel Gorman struct zone *zone; 842dd1a239fSMel Gorman struct zoneref *z; 843cc9a6c87SMel Gorman unsigned int cpuset_mems_cookie; 8441da177e4SLinus Torvalds 845a1e78772SMel Gorman /* 846a1e78772SMel Gorman * A child process with MAP_PRIVATE mappings created by their parent 847a1e78772SMel Gorman * have no page reserves. This check ensures that reservations are 848a1e78772SMel Gorman * not "stolen". The child may still get SIGKILLed 849a1e78772SMel Gorman */ 850af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg) && 851a5516438SAndi Kleen h->free_huge_pages - h->resv_huge_pages == 0) 852c0ff7453SMiao Xie goto err; 853a1e78772SMel Gorman 85404f2cbe3SMel Gorman /* If reserves cannot be used, ensure enough pages are in the pool */ 855a5516438SAndi Kleen if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0) 8566eab04a8SJustin P. Mattock goto err; 85704f2cbe3SMel Gorman 8589966c4bbSJoonsoo Kim retry_cpuset: 859d26914d1SMel Gorman cpuset_mems_cookie = read_mems_allowed_begin(); 8609966c4bbSJoonsoo Kim zonelist = huge_zonelist(vma, address, 86186cdb465SNaoya Horiguchi htlb_alloc_mask(h), &mpol, &nodemask); 8629966c4bbSJoonsoo Kim 86319770b32SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 86419770b32SMel Gorman MAX_NR_ZONES - 1, nodemask) { 865344736f2SVladimir Davydov if (cpuset_zone_allowed(zone, htlb_alloc_mask(h))) { 866bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, zone_to_nid(zone)); 867bf50bab2SNaoya Horiguchi if (page) { 868af0ed73eSJoonsoo Kim if (avoid_reserve) 869af0ed73eSJoonsoo Kim break; 870af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg)) 871af0ed73eSJoonsoo Kim break; 872af0ed73eSJoonsoo Kim 87307443a85SJoonsoo Kim SetPagePrivate(page); 874a63884e9SJoonsoo Kim h->resv_huge_pages--; 8755ab3ee7bSKen Chen break; 8761da177e4SLinus Torvalds } 8773abf7afdSAndrew Morton } 878bf50bab2SNaoya Horiguchi } 879cc9a6c87SMel Gorman 880cc9a6c87SMel Gorman mpol_cond_put(mpol); 881d26914d1SMel Gorman if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie))) 882cc9a6c87SMel Gorman goto retry_cpuset; 883cc9a6c87SMel Gorman return page; 884cc9a6c87SMel Gorman 885c0ff7453SMiao Xie err: 886cc9a6c87SMel Gorman return NULL; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8891cac6f2cSLuiz Capitulino /* 8901cac6f2cSLuiz Capitulino * common helper functions for hstate_next_node_to_{alloc|free}. 8911cac6f2cSLuiz Capitulino * We may have allocated or freed a huge page based on a different 8921cac6f2cSLuiz Capitulino * nodes_allowed previously, so h->next_node_to_{alloc|free} might 8931cac6f2cSLuiz Capitulino * be outside of *nodes_allowed. Ensure that we use an allowed 8941cac6f2cSLuiz Capitulino * node for alloc or free. 8951cac6f2cSLuiz Capitulino */ 8961cac6f2cSLuiz Capitulino static int next_node_allowed(int nid, nodemask_t *nodes_allowed) 8971cac6f2cSLuiz Capitulino { 8981cac6f2cSLuiz Capitulino nid = next_node(nid, *nodes_allowed); 8991cac6f2cSLuiz Capitulino if (nid == MAX_NUMNODES) 9001cac6f2cSLuiz Capitulino nid = first_node(*nodes_allowed); 9011cac6f2cSLuiz Capitulino VM_BUG_ON(nid >= MAX_NUMNODES); 9021cac6f2cSLuiz Capitulino 9031cac6f2cSLuiz Capitulino return nid; 9041cac6f2cSLuiz Capitulino } 9051cac6f2cSLuiz Capitulino 9061cac6f2cSLuiz Capitulino static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) 9071cac6f2cSLuiz Capitulino { 9081cac6f2cSLuiz Capitulino if (!node_isset(nid, *nodes_allowed)) 9091cac6f2cSLuiz Capitulino nid = next_node_allowed(nid, nodes_allowed); 9101cac6f2cSLuiz Capitulino return nid; 9111cac6f2cSLuiz Capitulino } 9121cac6f2cSLuiz Capitulino 9131cac6f2cSLuiz Capitulino /* 9141cac6f2cSLuiz Capitulino * returns the previously saved node ["this node"] from which to 9151cac6f2cSLuiz Capitulino * allocate a persistent huge page for the pool and advance the 9161cac6f2cSLuiz Capitulino * next node from which to allocate, handling wrap at end of node 9171cac6f2cSLuiz Capitulino * mask. 9181cac6f2cSLuiz Capitulino */ 9191cac6f2cSLuiz Capitulino static int hstate_next_node_to_alloc(struct hstate *h, 9201cac6f2cSLuiz Capitulino nodemask_t *nodes_allowed) 9211cac6f2cSLuiz Capitulino { 9221cac6f2cSLuiz Capitulino int nid; 9231cac6f2cSLuiz Capitulino 9241cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 9251cac6f2cSLuiz Capitulino 9261cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); 9271cac6f2cSLuiz Capitulino h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); 9281cac6f2cSLuiz Capitulino 9291cac6f2cSLuiz Capitulino return nid; 9301cac6f2cSLuiz Capitulino } 9311cac6f2cSLuiz Capitulino 9321cac6f2cSLuiz Capitulino /* 9331cac6f2cSLuiz Capitulino * helper for free_pool_huge_page() - return the previously saved 9341cac6f2cSLuiz Capitulino * node ["this node"] from which to free a huge page. Advance the 9351cac6f2cSLuiz Capitulino * next node id whether or not we find a free huge page to free so 9361cac6f2cSLuiz Capitulino * that the next attempt to free addresses the next node. 9371cac6f2cSLuiz Capitulino */ 9381cac6f2cSLuiz Capitulino static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) 9391cac6f2cSLuiz Capitulino { 9401cac6f2cSLuiz Capitulino int nid; 9411cac6f2cSLuiz Capitulino 9421cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 9431cac6f2cSLuiz Capitulino 9441cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); 9451cac6f2cSLuiz Capitulino h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); 9461cac6f2cSLuiz Capitulino 9471cac6f2cSLuiz Capitulino return nid; 9481cac6f2cSLuiz Capitulino } 9491cac6f2cSLuiz Capitulino 9501cac6f2cSLuiz Capitulino #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \ 9511cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 9521cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 9531cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ 9541cac6f2cSLuiz Capitulino nr_nodes--) 9551cac6f2cSLuiz Capitulino 9561cac6f2cSLuiz Capitulino #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ 9571cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 9581cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 9591cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_free(hs, mask)) || 1); \ 9601cac6f2cSLuiz Capitulino nr_nodes--) 9611cac6f2cSLuiz Capitulino 962944d9fecSLuiz Capitulino #if defined(CONFIG_CMA) && defined(CONFIG_X86_64) 963944d9fecSLuiz Capitulino static void destroy_compound_gigantic_page(struct page *page, 964944d9fecSLuiz Capitulino unsigned long order) 965944d9fecSLuiz Capitulino { 966944d9fecSLuiz Capitulino int i; 967944d9fecSLuiz Capitulino int nr_pages = 1 << order; 968944d9fecSLuiz Capitulino struct page *p = page + 1; 969944d9fecSLuiz Capitulino 970944d9fecSLuiz Capitulino for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 971944d9fecSLuiz Capitulino __ClearPageTail(p); 972944d9fecSLuiz Capitulino set_page_refcounted(p); 973944d9fecSLuiz Capitulino p->first_page = NULL; 974944d9fecSLuiz Capitulino } 975944d9fecSLuiz Capitulino 976944d9fecSLuiz Capitulino set_compound_order(page, 0); 977944d9fecSLuiz Capitulino __ClearPageHead(page); 978944d9fecSLuiz Capitulino } 979944d9fecSLuiz Capitulino 980944d9fecSLuiz Capitulino static void free_gigantic_page(struct page *page, unsigned order) 981944d9fecSLuiz Capitulino { 982944d9fecSLuiz Capitulino free_contig_range(page_to_pfn(page), 1 << order); 983944d9fecSLuiz Capitulino } 984944d9fecSLuiz Capitulino 985944d9fecSLuiz Capitulino static int __alloc_gigantic_page(unsigned long start_pfn, 986944d9fecSLuiz Capitulino unsigned long nr_pages) 987944d9fecSLuiz Capitulino { 988944d9fecSLuiz Capitulino unsigned long end_pfn = start_pfn + nr_pages; 989944d9fecSLuiz Capitulino return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 990944d9fecSLuiz Capitulino } 991944d9fecSLuiz Capitulino 992944d9fecSLuiz Capitulino static bool pfn_range_valid_gigantic(unsigned long start_pfn, 993944d9fecSLuiz Capitulino unsigned long nr_pages) 994944d9fecSLuiz Capitulino { 995944d9fecSLuiz Capitulino unsigned long i, end_pfn = start_pfn + nr_pages; 996944d9fecSLuiz Capitulino struct page *page; 997944d9fecSLuiz Capitulino 998944d9fecSLuiz Capitulino for (i = start_pfn; i < end_pfn; i++) { 999944d9fecSLuiz Capitulino if (!pfn_valid(i)) 1000944d9fecSLuiz Capitulino return false; 1001944d9fecSLuiz Capitulino 1002944d9fecSLuiz Capitulino page = pfn_to_page(i); 1003944d9fecSLuiz Capitulino 1004944d9fecSLuiz Capitulino if (PageReserved(page)) 1005944d9fecSLuiz Capitulino return false; 1006944d9fecSLuiz Capitulino 1007944d9fecSLuiz Capitulino if (page_count(page) > 0) 1008944d9fecSLuiz Capitulino return false; 1009944d9fecSLuiz Capitulino 1010944d9fecSLuiz Capitulino if (PageHuge(page)) 1011944d9fecSLuiz Capitulino return false; 1012944d9fecSLuiz Capitulino } 1013944d9fecSLuiz Capitulino 1014944d9fecSLuiz Capitulino return true; 1015944d9fecSLuiz Capitulino } 1016944d9fecSLuiz Capitulino 1017944d9fecSLuiz Capitulino static bool zone_spans_last_pfn(const struct zone *zone, 1018944d9fecSLuiz Capitulino unsigned long start_pfn, unsigned long nr_pages) 1019944d9fecSLuiz Capitulino { 1020944d9fecSLuiz Capitulino unsigned long last_pfn = start_pfn + nr_pages - 1; 1021944d9fecSLuiz Capitulino return zone_spans_pfn(zone, last_pfn); 1022944d9fecSLuiz Capitulino } 1023944d9fecSLuiz Capitulino 1024944d9fecSLuiz Capitulino static struct page *alloc_gigantic_page(int nid, unsigned order) 1025944d9fecSLuiz Capitulino { 1026944d9fecSLuiz Capitulino unsigned long nr_pages = 1 << order; 1027944d9fecSLuiz Capitulino unsigned long ret, pfn, flags; 1028944d9fecSLuiz Capitulino struct zone *z; 1029944d9fecSLuiz Capitulino 1030944d9fecSLuiz Capitulino z = NODE_DATA(nid)->node_zones; 1031944d9fecSLuiz Capitulino for (; z - NODE_DATA(nid)->node_zones < MAX_NR_ZONES; z++) { 1032944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 1033944d9fecSLuiz Capitulino 1034944d9fecSLuiz Capitulino pfn = ALIGN(z->zone_start_pfn, nr_pages); 1035944d9fecSLuiz Capitulino while (zone_spans_last_pfn(z, pfn, nr_pages)) { 1036944d9fecSLuiz Capitulino if (pfn_range_valid_gigantic(pfn, nr_pages)) { 1037944d9fecSLuiz Capitulino /* 1038944d9fecSLuiz Capitulino * We release the zone lock here because 1039944d9fecSLuiz Capitulino * alloc_contig_range() will also lock the zone 1040944d9fecSLuiz Capitulino * at some point. If there's an allocation 1041944d9fecSLuiz Capitulino * spinning on this lock, it may win the race 1042944d9fecSLuiz Capitulino * and cause alloc_contig_range() to fail... 1043944d9fecSLuiz Capitulino */ 1044944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 1045944d9fecSLuiz Capitulino ret = __alloc_gigantic_page(pfn, nr_pages); 1046944d9fecSLuiz Capitulino if (!ret) 1047944d9fecSLuiz Capitulino return pfn_to_page(pfn); 1048944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 1049944d9fecSLuiz Capitulino } 1050944d9fecSLuiz Capitulino pfn += nr_pages; 1051944d9fecSLuiz Capitulino } 1052944d9fecSLuiz Capitulino 1053944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 1054944d9fecSLuiz Capitulino } 1055944d9fecSLuiz Capitulino 1056944d9fecSLuiz Capitulino return NULL; 1057944d9fecSLuiz Capitulino } 1058944d9fecSLuiz Capitulino 1059944d9fecSLuiz Capitulino static void prep_new_huge_page(struct hstate *h, struct page *page, int nid); 1060944d9fecSLuiz Capitulino static void prep_compound_gigantic_page(struct page *page, unsigned long order); 1061944d9fecSLuiz Capitulino 1062944d9fecSLuiz Capitulino static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid) 1063944d9fecSLuiz Capitulino { 1064944d9fecSLuiz Capitulino struct page *page; 1065944d9fecSLuiz Capitulino 1066944d9fecSLuiz Capitulino page = alloc_gigantic_page(nid, huge_page_order(h)); 1067944d9fecSLuiz Capitulino if (page) { 1068944d9fecSLuiz Capitulino prep_compound_gigantic_page(page, huge_page_order(h)); 1069944d9fecSLuiz Capitulino prep_new_huge_page(h, page, nid); 1070944d9fecSLuiz Capitulino } 1071944d9fecSLuiz Capitulino 1072944d9fecSLuiz Capitulino return page; 1073944d9fecSLuiz Capitulino } 1074944d9fecSLuiz Capitulino 1075944d9fecSLuiz Capitulino static int alloc_fresh_gigantic_page(struct hstate *h, 1076944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) 1077944d9fecSLuiz Capitulino { 1078944d9fecSLuiz Capitulino struct page *page = NULL; 1079944d9fecSLuiz Capitulino int nr_nodes, node; 1080944d9fecSLuiz Capitulino 1081944d9fecSLuiz Capitulino for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1082944d9fecSLuiz Capitulino page = alloc_fresh_gigantic_page_node(h, node); 1083944d9fecSLuiz Capitulino if (page) 1084944d9fecSLuiz Capitulino return 1; 1085944d9fecSLuiz Capitulino } 1086944d9fecSLuiz Capitulino 1087944d9fecSLuiz Capitulino return 0; 1088944d9fecSLuiz Capitulino } 1089944d9fecSLuiz Capitulino 1090944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return true; } 1091944d9fecSLuiz Capitulino #else 1092944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return false; } 1093944d9fecSLuiz Capitulino static inline void free_gigantic_page(struct page *page, unsigned order) { } 1094944d9fecSLuiz Capitulino static inline void destroy_compound_gigantic_page(struct page *page, 1095944d9fecSLuiz Capitulino unsigned long order) { } 1096944d9fecSLuiz Capitulino static inline int alloc_fresh_gigantic_page(struct hstate *h, 1097944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) { return 0; } 1098944d9fecSLuiz Capitulino #endif 1099944d9fecSLuiz Capitulino 1100a5516438SAndi Kleen static void update_and_free_page(struct hstate *h, struct page *page) 11016af2acb6SAdam Litke { 11026af2acb6SAdam Litke int i; 1103a5516438SAndi Kleen 1104944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 1105944d9fecSLuiz Capitulino return; 110618229df5SAndy Whitcroft 1107a5516438SAndi Kleen h->nr_huge_pages--; 1108a5516438SAndi Kleen h->nr_huge_pages_node[page_to_nid(page)]--; 1109a5516438SAndi Kleen for (i = 0; i < pages_per_huge_page(h); i++) { 111032f84528SChris Forbes page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 111132f84528SChris Forbes 1 << PG_referenced | 1 << PG_dirty | 1112a7407a27SLuiz Capitulino 1 << PG_active | 1 << PG_private | 1113a7407a27SLuiz Capitulino 1 << PG_writeback); 11146af2acb6SAdam Litke } 1115309381feSSasha Levin VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page); 11166af2acb6SAdam Litke set_compound_page_dtor(page, NULL); 11176af2acb6SAdam Litke set_page_refcounted(page); 1118944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) { 1119944d9fecSLuiz Capitulino destroy_compound_gigantic_page(page, huge_page_order(h)); 1120944d9fecSLuiz Capitulino free_gigantic_page(page, huge_page_order(h)); 1121944d9fecSLuiz Capitulino } else { 1122a5516438SAndi Kleen __free_pages(page, huge_page_order(h)); 11236af2acb6SAdam Litke } 1124944d9fecSLuiz Capitulino } 11256af2acb6SAdam Litke 1126e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size) 1127e5ff2159SAndi Kleen { 1128e5ff2159SAndi Kleen struct hstate *h; 1129e5ff2159SAndi Kleen 1130e5ff2159SAndi Kleen for_each_hstate(h) { 1131e5ff2159SAndi Kleen if (huge_page_size(h) == size) 1132e5ff2159SAndi Kleen return h; 1133e5ff2159SAndi Kleen } 1134e5ff2159SAndi Kleen return NULL; 1135e5ff2159SAndi Kleen } 1136e5ff2159SAndi Kleen 1137bcc54222SNaoya Horiguchi /* 1138bcc54222SNaoya Horiguchi * Test to determine whether the hugepage is "active/in-use" (i.e. being linked 1139bcc54222SNaoya Horiguchi * to hstate->hugepage_activelist.) 1140bcc54222SNaoya Horiguchi * 1141bcc54222SNaoya Horiguchi * This function can be called for tail pages, but never returns true for them. 1142bcc54222SNaoya Horiguchi */ 1143bcc54222SNaoya Horiguchi bool page_huge_active(struct page *page) 1144bcc54222SNaoya Horiguchi { 1145bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHuge(page), page); 1146bcc54222SNaoya Horiguchi return PageHead(page) && PagePrivate(&page[1]); 1147bcc54222SNaoya Horiguchi } 1148bcc54222SNaoya Horiguchi 1149bcc54222SNaoya Horiguchi /* never called for tail page */ 1150bcc54222SNaoya Horiguchi static void set_page_huge_active(struct page *page) 1151bcc54222SNaoya Horiguchi { 1152bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 1153bcc54222SNaoya Horiguchi SetPagePrivate(&page[1]); 1154bcc54222SNaoya Horiguchi } 1155bcc54222SNaoya Horiguchi 1156bcc54222SNaoya Horiguchi static void clear_page_huge_active(struct page *page) 1157bcc54222SNaoya Horiguchi { 1158bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 1159bcc54222SNaoya Horiguchi ClearPagePrivate(&page[1]); 1160bcc54222SNaoya Horiguchi } 1161bcc54222SNaoya Horiguchi 11628f1d26d0SAtsushi Kumagai void free_huge_page(struct page *page) 116327a85ef1SDavid Gibson { 1164a5516438SAndi Kleen /* 1165a5516438SAndi Kleen * Can't pass hstate in here because it is called from the 1166a5516438SAndi Kleen * compound page destructor. 1167a5516438SAndi Kleen */ 1168e5ff2159SAndi Kleen struct hstate *h = page_hstate(page); 11697893d1d5SAdam Litke int nid = page_to_nid(page); 117090481622SDavid Gibson struct hugepage_subpool *spool = 117190481622SDavid Gibson (struct hugepage_subpool *)page_private(page); 117207443a85SJoonsoo Kim bool restore_reserve; 117327a85ef1SDavid Gibson 1174e5df70abSAndy Whitcroft set_page_private(page, 0); 117523be7468SMel Gorman page->mapping = NULL; 11767893d1d5SAdam Litke BUG_ON(page_count(page)); 11770fe6e20bSNaoya Horiguchi BUG_ON(page_mapcount(page)); 117807443a85SJoonsoo Kim restore_reserve = PagePrivate(page); 117916c794b4SJoonsoo Kim ClearPagePrivate(page); 118027a85ef1SDavid Gibson 11811c5ecae3SMike Kravetz /* 11821c5ecae3SMike Kravetz * A return code of zero implies that the subpool will be under its 11831c5ecae3SMike Kravetz * minimum size if the reservation is not restored after page is free. 11841c5ecae3SMike Kravetz * Therefore, force restore_reserve operation. 11851c5ecae3SMike Kravetz */ 11861c5ecae3SMike Kravetz if (hugepage_subpool_put_pages(spool, 1) == 0) 11871c5ecae3SMike Kravetz restore_reserve = true; 11881c5ecae3SMike Kravetz 118927a85ef1SDavid Gibson spin_lock(&hugetlb_lock); 1190bcc54222SNaoya Horiguchi clear_page_huge_active(page); 11916d76dcf4SAneesh Kumar K.V hugetlb_cgroup_uncharge_page(hstate_index(h), 11926d76dcf4SAneesh Kumar K.V pages_per_huge_page(h), page); 119307443a85SJoonsoo Kim if (restore_reserve) 119407443a85SJoonsoo Kim h->resv_huge_pages++; 119507443a85SJoonsoo Kim 1196944d9fecSLuiz Capitulino if (h->surplus_huge_pages_node[nid]) { 11970edaecfaSAneesh Kumar K.V /* remove the page from active list */ 11980edaecfaSAneesh Kumar K.V list_del(&page->lru); 1199a5516438SAndi Kleen update_and_free_page(h, page); 1200a5516438SAndi Kleen h->surplus_huge_pages--; 1201a5516438SAndi Kleen h->surplus_huge_pages_node[nid]--; 12027893d1d5SAdam Litke } else { 12035d3a551cSWill Deacon arch_clear_hugepage_flags(page); 1204a5516438SAndi Kleen enqueue_huge_page(h, page); 12057893d1d5SAdam Litke } 120627a85ef1SDavid Gibson spin_unlock(&hugetlb_lock); 120727a85ef1SDavid Gibson } 120827a85ef1SDavid Gibson 1209a5516438SAndi Kleen static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) 1210b7ba30c6SAndi Kleen { 12110edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1212b7ba30c6SAndi Kleen set_compound_page_dtor(page, free_huge_page); 1213b7ba30c6SAndi Kleen spin_lock(&hugetlb_lock); 12149dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1215a5516438SAndi Kleen h->nr_huge_pages++; 1216a5516438SAndi Kleen h->nr_huge_pages_node[nid]++; 1217b7ba30c6SAndi Kleen spin_unlock(&hugetlb_lock); 1218b7ba30c6SAndi Kleen put_page(page); /* free it into the hugepage allocator */ 1219b7ba30c6SAndi Kleen } 1220b7ba30c6SAndi Kleen 12212906dd52SLuiz Capitulino static void prep_compound_gigantic_page(struct page *page, unsigned long order) 122220a0307cSWu Fengguang { 122320a0307cSWu Fengguang int i; 122420a0307cSWu Fengguang int nr_pages = 1 << order; 122520a0307cSWu Fengguang struct page *p = page + 1; 122620a0307cSWu Fengguang 122720a0307cSWu Fengguang /* we rely on prep_new_huge_page to set the destructor */ 122820a0307cSWu Fengguang set_compound_order(page, order); 122920a0307cSWu Fengguang __SetPageHead(page); 1230ef5a22beSAndrea Arcangeli __ClearPageReserved(page); 123120a0307cSWu Fengguang for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 1232ef5a22beSAndrea Arcangeli /* 1233ef5a22beSAndrea Arcangeli * For gigantic hugepages allocated through bootmem at 1234ef5a22beSAndrea Arcangeli * boot, it's safer to be consistent with the not-gigantic 1235ef5a22beSAndrea Arcangeli * hugepages and clear the PG_reserved bit from all tail pages 1236ef5a22beSAndrea Arcangeli * too. Otherwse drivers using get_user_pages() to access tail 1237ef5a22beSAndrea Arcangeli * pages may get the reference counting wrong if they see 1238ef5a22beSAndrea Arcangeli * PG_reserved set on a tail page (despite the head page not 1239ef5a22beSAndrea Arcangeli * having PG_reserved set). Enforcing this consistency between 1240ef5a22beSAndrea Arcangeli * head and tail pages allows drivers to optimize away a check 1241ef5a22beSAndrea Arcangeli * on the head page when they need know if put_page() is needed 1242ef5a22beSAndrea Arcangeli * after get_user_pages(). 1243ef5a22beSAndrea Arcangeli */ 1244ef5a22beSAndrea Arcangeli __ClearPageReserved(p); 124558a84aa9SYouquan Song set_page_count(p, 0); 124620a0307cSWu Fengguang p->first_page = page; 124744fc8057SDavid Rientjes /* Make sure p->first_page is always valid for PageTail() */ 124844fc8057SDavid Rientjes smp_wmb(); 124944fc8057SDavid Rientjes __SetPageTail(p); 125020a0307cSWu Fengguang } 125120a0307cSWu Fengguang } 125220a0307cSWu Fengguang 12537795912cSAndrew Morton /* 12547795912cSAndrew Morton * PageHuge() only returns true for hugetlbfs pages, but not for normal or 12557795912cSAndrew Morton * transparent huge pages. See the PageTransHuge() documentation for more 12567795912cSAndrew Morton * details. 12577795912cSAndrew Morton */ 125820a0307cSWu Fengguang int PageHuge(struct page *page) 125920a0307cSWu Fengguang { 126020a0307cSWu Fengguang if (!PageCompound(page)) 126120a0307cSWu Fengguang return 0; 126220a0307cSWu Fengguang 126320a0307cSWu Fengguang page = compound_head(page); 1264758f66a2SAndrew Morton return get_compound_page_dtor(page) == free_huge_page; 126520a0307cSWu Fengguang } 126643131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge); 126743131e14SNaoya Horiguchi 126827c73ae7SAndrea Arcangeli /* 126927c73ae7SAndrea Arcangeli * PageHeadHuge() only returns true for hugetlbfs head page, but not for 127027c73ae7SAndrea Arcangeli * normal or transparent huge pages. 127127c73ae7SAndrea Arcangeli */ 127227c73ae7SAndrea Arcangeli int PageHeadHuge(struct page *page_head) 127327c73ae7SAndrea Arcangeli { 127427c73ae7SAndrea Arcangeli if (!PageHead(page_head)) 127527c73ae7SAndrea Arcangeli return 0; 127627c73ae7SAndrea Arcangeli 1277758f66a2SAndrew Morton return get_compound_page_dtor(page_head) == free_huge_page; 127827c73ae7SAndrea Arcangeli } 127927c73ae7SAndrea Arcangeli 128013d60f4bSZhang Yi pgoff_t __basepage_index(struct page *page) 128113d60f4bSZhang Yi { 128213d60f4bSZhang Yi struct page *page_head = compound_head(page); 128313d60f4bSZhang Yi pgoff_t index = page_index(page_head); 128413d60f4bSZhang Yi unsigned long compound_idx; 128513d60f4bSZhang Yi 128613d60f4bSZhang Yi if (!PageHuge(page_head)) 128713d60f4bSZhang Yi return page_index(page); 128813d60f4bSZhang Yi 128913d60f4bSZhang Yi if (compound_order(page_head) >= MAX_ORDER) 129013d60f4bSZhang Yi compound_idx = page_to_pfn(page) - page_to_pfn(page_head); 129113d60f4bSZhang Yi else 129213d60f4bSZhang Yi compound_idx = page - page_head; 129313d60f4bSZhang Yi 129413d60f4bSZhang Yi return (index << compound_order(page_head)) + compound_idx; 129513d60f4bSZhang Yi } 129613d60f4bSZhang Yi 1297a5516438SAndi Kleen static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid) 12981da177e4SLinus Torvalds { 12991da177e4SLinus Torvalds struct page *page; 1300f96efd58SJoe Jin 13016484eb3eSMel Gorman page = alloc_pages_exact_node(nid, 130286cdb465SNaoya Horiguchi htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| 1303551883aeSNishanth Aravamudan __GFP_REPEAT|__GFP_NOWARN, 1304a5516438SAndi Kleen huge_page_order(h)); 13051da177e4SLinus Torvalds if (page) { 1306a5516438SAndi Kleen prep_new_huge_page(h, page, nid); 13071da177e4SLinus Torvalds } 130863b4613cSNishanth Aravamudan 130963b4613cSNishanth Aravamudan return page; 131063b4613cSNishanth Aravamudan } 131163b4613cSNishanth Aravamudan 1312b2261026SJoonsoo Kim static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed) 1313b2261026SJoonsoo Kim { 1314b2261026SJoonsoo Kim struct page *page; 1315b2261026SJoonsoo Kim int nr_nodes, node; 1316b2261026SJoonsoo Kim int ret = 0; 1317b2261026SJoonsoo Kim 1318b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1319b2261026SJoonsoo Kim page = alloc_fresh_huge_page_node(h, node); 1320b2261026SJoonsoo Kim if (page) { 1321b2261026SJoonsoo Kim ret = 1; 1322b2261026SJoonsoo Kim break; 1323b2261026SJoonsoo Kim } 1324b2261026SJoonsoo Kim } 1325b2261026SJoonsoo Kim 1326b2261026SJoonsoo Kim if (ret) 1327b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC); 1328b2261026SJoonsoo Kim else 1329b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 1330b2261026SJoonsoo Kim 1331b2261026SJoonsoo Kim return ret; 1332b2261026SJoonsoo Kim } 1333b2261026SJoonsoo Kim 1334e8c5c824SLee Schermerhorn /* 1335e8c5c824SLee Schermerhorn * Free huge page from pool from next node to free. 1336e8c5c824SLee Schermerhorn * Attempt to keep persistent huge pages more or less 1337e8c5c824SLee Schermerhorn * balanced over allowed nodes. 1338e8c5c824SLee Schermerhorn * Called with hugetlb_lock locked. 1339e8c5c824SLee Schermerhorn */ 13406ae11b27SLee Schermerhorn static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, 13416ae11b27SLee Schermerhorn bool acct_surplus) 1342e8c5c824SLee Schermerhorn { 1343b2261026SJoonsoo Kim int nr_nodes, node; 1344e8c5c824SLee Schermerhorn int ret = 0; 1345e8c5c824SLee Schermerhorn 1346b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1347685f3457SLee Schermerhorn /* 1348685f3457SLee Schermerhorn * If we're returning unused surplus pages, only examine 1349685f3457SLee Schermerhorn * nodes with surplus pages. 1350685f3457SLee Schermerhorn */ 1351b2261026SJoonsoo Kim if ((!acct_surplus || h->surplus_huge_pages_node[node]) && 1352b2261026SJoonsoo Kim !list_empty(&h->hugepage_freelists[node])) { 1353e8c5c824SLee Schermerhorn struct page *page = 1354b2261026SJoonsoo Kim list_entry(h->hugepage_freelists[node].next, 1355e8c5c824SLee Schermerhorn struct page, lru); 1356e8c5c824SLee Schermerhorn list_del(&page->lru); 1357e8c5c824SLee Schermerhorn h->free_huge_pages--; 1358b2261026SJoonsoo Kim h->free_huge_pages_node[node]--; 1359685f3457SLee Schermerhorn if (acct_surplus) { 1360685f3457SLee Schermerhorn h->surplus_huge_pages--; 1361b2261026SJoonsoo Kim h->surplus_huge_pages_node[node]--; 1362685f3457SLee Schermerhorn } 1363e8c5c824SLee Schermerhorn update_and_free_page(h, page); 1364e8c5c824SLee Schermerhorn ret = 1; 13659a76db09SLee Schermerhorn break; 1366e8c5c824SLee Schermerhorn } 1367b2261026SJoonsoo Kim } 1368e8c5c824SLee Schermerhorn 1369e8c5c824SLee Schermerhorn return ret; 1370e8c5c824SLee Schermerhorn } 1371e8c5c824SLee Schermerhorn 1372c8721bbbSNaoya Horiguchi /* 1373c8721bbbSNaoya Horiguchi * Dissolve a given free hugepage into free buddy pages. This function does 1374c8721bbbSNaoya Horiguchi * nothing for in-use (including surplus) hugepages. 1375c8721bbbSNaoya Horiguchi */ 1376c8721bbbSNaoya Horiguchi static void dissolve_free_huge_page(struct page *page) 1377c8721bbbSNaoya Horiguchi { 1378c8721bbbSNaoya Horiguchi spin_lock(&hugetlb_lock); 1379c8721bbbSNaoya Horiguchi if (PageHuge(page) && !page_count(page)) { 1380c8721bbbSNaoya Horiguchi struct hstate *h = page_hstate(page); 1381c8721bbbSNaoya Horiguchi int nid = page_to_nid(page); 1382c8721bbbSNaoya Horiguchi list_del(&page->lru); 1383c8721bbbSNaoya Horiguchi h->free_huge_pages--; 1384c8721bbbSNaoya Horiguchi h->free_huge_pages_node[nid]--; 1385c8721bbbSNaoya Horiguchi update_and_free_page(h, page); 1386c8721bbbSNaoya Horiguchi } 1387c8721bbbSNaoya Horiguchi spin_unlock(&hugetlb_lock); 1388c8721bbbSNaoya Horiguchi } 1389c8721bbbSNaoya Horiguchi 1390c8721bbbSNaoya Horiguchi /* 1391c8721bbbSNaoya Horiguchi * Dissolve free hugepages in a given pfn range. Used by memory hotplug to 1392c8721bbbSNaoya Horiguchi * make specified memory blocks removable from the system. 1393c8721bbbSNaoya Horiguchi * Note that start_pfn should aligned with (minimum) hugepage size. 1394c8721bbbSNaoya Horiguchi */ 1395c8721bbbSNaoya Horiguchi void dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) 1396c8721bbbSNaoya Horiguchi { 1397c8721bbbSNaoya Horiguchi unsigned long pfn; 1398c8721bbbSNaoya Horiguchi 1399d0177639SLi Zhong if (!hugepages_supported()) 1400d0177639SLi Zhong return; 1401d0177639SLi Zhong 1402641844f5SNaoya Horiguchi VM_BUG_ON(!IS_ALIGNED(start_pfn, 1 << minimum_order)); 1403641844f5SNaoya Horiguchi for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) 1404c8721bbbSNaoya Horiguchi dissolve_free_huge_page(pfn_to_page(pfn)); 1405c8721bbbSNaoya Horiguchi } 1406c8721bbbSNaoya Horiguchi 1407bf50bab2SNaoya Horiguchi static struct page *alloc_buddy_huge_page(struct hstate *h, int nid) 14087893d1d5SAdam Litke { 14097893d1d5SAdam Litke struct page *page; 1410bf50bab2SNaoya Horiguchi unsigned int r_nid; 14117893d1d5SAdam Litke 1412bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1413aa888a74SAndi Kleen return NULL; 1414aa888a74SAndi Kleen 1415d1c3fb1fSNishanth Aravamudan /* 1416d1c3fb1fSNishanth Aravamudan * Assume we will successfully allocate the surplus page to 1417d1c3fb1fSNishanth Aravamudan * prevent racing processes from causing the surplus to exceed 1418d1c3fb1fSNishanth Aravamudan * overcommit 1419d1c3fb1fSNishanth Aravamudan * 1420d1c3fb1fSNishanth Aravamudan * This however introduces a different race, where a process B 1421d1c3fb1fSNishanth Aravamudan * tries to grow the static hugepage pool while alloc_pages() is 1422d1c3fb1fSNishanth Aravamudan * called by process A. B will only examine the per-node 1423d1c3fb1fSNishanth Aravamudan * counters in determining if surplus huge pages can be 1424d1c3fb1fSNishanth Aravamudan * converted to normal huge pages in adjust_pool_surplus(). A 1425d1c3fb1fSNishanth Aravamudan * won't be able to increment the per-node counter, until the 1426d1c3fb1fSNishanth Aravamudan * lock is dropped by B, but B doesn't drop hugetlb_lock until 1427d1c3fb1fSNishanth Aravamudan * no more huge pages can be converted from surplus to normal 1428d1c3fb1fSNishanth Aravamudan * state (and doesn't try to convert again). Thus, we have a 1429d1c3fb1fSNishanth Aravamudan * case where a surplus huge page exists, the pool is grown, and 1430d1c3fb1fSNishanth Aravamudan * the surplus huge page still exists after, even though it 1431d1c3fb1fSNishanth Aravamudan * should just have been converted to a normal huge page. This 1432d1c3fb1fSNishanth Aravamudan * does not leak memory, though, as the hugepage will be freed 1433d1c3fb1fSNishanth Aravamudan * once it is out of use. It also does not allow the counters to 1434d1c3fb1fSNishanth Aravamudan * go out of whack in adjust_pool_surplus() as we don't modify 1435d1c3fb1fSNishanth Aravamudan * the node values until we've gotten the hugepage and only the 1436d1c3fb1fSNishanth Aravamudan * per-node value is checked there. 1437d1c3fb1fSNishanth Aravamudan */ 1438d1c3fb1fSNishanth Aravamudan spin_lock(&hugetlb_lock); 1439a5516438SAndi Kleen if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { 1440d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1441d1c3fb1fSNishanth Aravamudan return NULL; 1442d1c3fb1fSNishanth Aravamudan } else { 1443a5516438SAndi Kleen h->nr_huge_pages++; 1444a5516438SAndi Kleen h->surplus_huge_pages++; 1445d1c3fb1fSNishanth Aravamudan } 1446d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1447d1c3fb1fSNishanth Aravamudan 1448bf50bab2SNaoya Horiguchi if (nid == NUMA_NO_NODE) 144986cdb465SNaoya Horiguchi page = alloc_pages(htlb_alloc_mask(h)|__GFP_COMP| 1450551883aeSNishanth Aravamudan __GFP_REPEAT|__GFP_NOWARN, 1451a5516438SAndi Kleen huge_page_order(h)); 1452bf50bab2SNaoya Horiguchi else 1453bf50bab2SNaoya Horiguchi page = alloc_pages_exact_node(nid, 145486cdb465SNaoya Horiguchi htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| 1455bf50bab2SNaoya Horiguchi __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h)); 1456d1c3fb1fSNishanth Aravamudan 14577893d1d5SAdam Litke spin_lock(&hugetlb_lock); 1458d1c3fb1fSNishanth Aravamudan if (page) { 14590edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1460bf50bab2SNaoya Horiguchi r_nid = page_to_nid(page); 1461d1c3fb1fSNishanth Aravamudan set_compound_page_dtor(page, free_huge_page); 14629dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1463d1c3fb1fSNishanth Aravamudan /* 1464d1c3fb1fSNishanth Aravamudan * We incremented the global counters already 1465d1c3fb1fSNishanth Aravamudan */ 1466bf50bab2SNaoya Horiguchi h->nr_huge_pages_node[r_nid]++; 1467bf50bab2SNaoya Horiguchi h->surplus_huge_pages_node[r_nid]++; 14683b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC); 1469d1c3fb1fSNishanth Aravamudan } else { 1470a5516438SAndi Kleen h->nr_huge_pages--; 1471a5516438SAndi Kleen h->surplus_huge_pages--; 14723b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 14737893d1d5SAdam Litke } 1474d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 14757893d1d5SAdam Litke 14767893d1d5SAdam Litke return page; 14777893d1d5SAdam Litke } 14787893d1d5SAdam Litke 1479e4e574b7SAdam Litke /* 1480bf50bab2SNaoya Horiguchi * This allocation function is useful in the context where vma is irrelevant. 1481bf50bab2SNaoya Horiguchi * E.g. soft-offlining uses this function because it only cares physical 1482bf50bab2SNaoya Horiguchi * address of error page. 1483bf50bab2SNaoya Horiguchi */ 1484bf50bab2SNaoya Horiguchi struct page *alloc_huge_page_node(struct hstate *h, int nid) 1485bf50bab2SNaoya Horiguchi { 14864ef91848SJoonsoo Kim struct page *page = NULL; 1487bf50bab2SNaoya Horiguchi 1488bf50bab2SNaoya Horiguchi spin_lock(&hugetlb_lock); 14894ef91848SJoonsoo Kim if (h->free_huge_pages - h->resv_huge_pages > 0) 1490bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, nid); 1491bf50bab2SNaoya Horiguchi spin_unlock(&hugetlb_lock); 1492bf50bab2SNaoya Horiguchi 149394ae8ba7SAneesh Kumar K.V if (!page) 1494bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, nid); 1495bf50bab2SNaoya Horiguchi 1496bf50bab2SNaoya Horiguchi return page; 1497bf50bab2SNaoya Horiguchi } 1498bf50bab2SNaoya Horiguchi 1499bf50bab2SNaoya Horiguchi /* 150025985edcSLucas De Marchi * Increase the hugetlb pool such that it can accommodate a reservation 1501e4e574b7SAdam Litke * of size 'delta'. 1502e4e574b7SAdam Litke */ 1503a5516438SAndi Kleen static int gather_surplus_pages(struct hstate *h, int delta) 1504e4e574b7SAdam Litke { 1505e4e574b7SAdam Litke struct list_head surplus_list; 1506e4e574b7SAdam Litke struct page *page, *tmp; 1507e4e574b7SAdam Litke int ret, i; 1508e4e574b7SAdam Litke int needed, allocated; 150928073b02SHillf Danton bool alloc_ok = true; 1510e4e574b7SAdam Litke 1511a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - h->free_huge_pages; 1512ac09b3a1SAdam Litke if (needed <= 0) { 1513a5516438SAndi Kleen h->resv_huge_pages += delta; 1514e4e574b7SAdam Litke return 0; 1515ac09b3a1SAdam Litke } 1516e4e574b7SAdam Litke 1517e4e574b7SAdam Litke allocated = 0; 1518e4e574b7SAdam Litke INIT_LIST_HEAD(&surplus_list); 1519e4e574b7SAdam Litke 1520e4e574b7SAdam Litke ret = -ENOMEM; 1521e4e574b7SAdam Litke retry: 1522e4e574b7SAdam Litke spin_unlock(&hugetlb_lock); 1523e4e574b7SAdam Litke for (i = 0; i < needed; i++) { 1524bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, NUMA_NO_NODE); 152528073b02SHillf Danton if (!page) { 152628073b02SHillf Danton alloc_ok = false; 152728073b02SHillf Danton break; 152828073b02SHillf Danton } 1529e4e574b7SAdam Litke list_add(&page->lru, &surplus_list); 1530e4e574b7SAdam Litke } 153128073b02SHillf Danton allocated += i; 1532e4e574b7SAdam Litke 1533e4e574b7SAdam Litke /* 1534e4e574b7SAdam Litke * After retaking hugetlb_lock, we need to recalculate 'needed' 1535e4e574b7SAdam Litke * because either resv_huge_pages or free_huge_pages may have changed. 1536e4e574b7SAdam Litke */ 1537e4e574b7SAdam Litke spin_lock(&hugetlb_lock); 1538a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - 1539a5516438SAndi Kleen (h->free_huge_pages + allocated); 154028073b02SHillf Danton if (needed > 0) { 154128073b02SHillf Danton if (alloc_ok) 1542e4e574b7SAdam Litke goto retry; 154328073b02SHillf Danton /* 154428073b02SHillf Danton * We were not able to allocate enough pages to 154528073b02SHillf Danton * satisfy the entire reservation so we free what 154628073b02SHillf Danton * we've allocated so far. 154728073b02SHillf Danton */ 154828073b02SHillf Danton goto free; 154928073b02SHillf Danton } 1550e4e574b7SAdam Litke /* 1551e4e574b7SAdam Litke * The surplus_list now contains _at_least_ the number of extra pages 155225985edcSLucas De Marchi * needed to accommodate the reservation. Add the appropriate number 1553e4e574b7SAdam Litke * of pages to the hugetlb pool and free the extras back to the buddy 1554ac09b3a1SAdam Litke * allocator. Commit the entire reservation here to prevent another 1555ac09b3a1SAdam Litke * process from stealing the pages as they are added to the pool but 1556ac09b3a1SAdam Litke * before they are reserved. 1557e4e574b7SAdam Litke */ 1558e4e574b7SAdam Litke needed += allocated; 1559a5516438SAndi Kleen h->resv_huge_pages += delta; 1560e4e574b7SAdam Litke ret = 0; 1561a9869b83SNaoya Horiguchi 156219fc3f0aSAdam Litke /* Free the needed pages to the hugetlb pool */ 156319fc3f0aSAdam Litke list_for_each_entry_safe(page, tmp, &surplus_list, lru) { 156419fc3f0aSAdam Litke if ((--needed) < 0) 156519fc3f0aSAdam Litke break; 1566a9869b83SNaoya Horiguchi /* 1567a9869b83SNaoya Horiguchi * This page is now managed by the hugetlb allocator and has 1568a9869b83SNaoya Horiguchi * no users -- drop the buddy allocator's reference. 1569a9869b83SNaoya Horiguchi */ 1570a9869b83SNaoya Horiguchi put_page_testzero(page); 1571309381feSSasha Levin VM_BUG_ON_PAGE(page_count(page), page); 1572a5516438SAndi Kleen enqueue_huge_page(h, page); 157319fc3f0aSAdam Litke } 157428073b02SHillf Danton free: 1575b0365c8dSHillf Danton spin_unlock(&hugetlb_lock); 157619fc3f0aSAdam Litke 157719fc3f0aSAdam Litke /* Free unnecessary surplus pages to the buddy allocator */ 1578c0d934baSJoonsoo Kim list_for_each_entry_safe(page, tmp, &surplus_list, lru) 1579a9869b83SNaoya Horiguchi put_page(page); 158019fc3f0aSAdam Litke spin_lock(&hugetlb_lock); 1581e4e574b7SAdam Litke 1582e4e574b7SAdam Litke return ret; 1583e4e574b7SAdam Litke } 1584e4e574b7SAdam Litke 1585e4e574b7SAdam Litke /* 1586e4e574b7SAdam Litke * When releasing a hugetlb pool reservation, any surplus pages that were 1587e4e574b7SAdam Litke * allocated to satisfy the reservation must be explicitly freed if they were 1588e4e574b7SAdam Litke * never used. 1589685f3457SLee Schermerhorn * Called with hugetlb_lock held. 1590e4e574b7SAdam Litke */ 1591a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h, 1592a5516438SAndi Kleen unsigned long unused_resv_pages) 1593e4e574b7SAdam Litke { 1594e4e574b7SAdam Litke unsigned long nr_pages; 1595e4e574b7SAdam Litke 1596ac09b3a1SAdam Litke /* Uncommit the reservation */ 1597a5516438SAndi Kleen h->resv_huge_pages -= unused_resv_pages; 1598ac09b3a1SAdam Litke 1599aa888a74SAndi Kleen /* Cannot return gigantic pages currently */ 1600bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1601aa888a74SAndi Kleen return; 1602aa888a74SAndi Kleen 1603a5516438SAndi Kleen nr_pages = min(unused_resv_pages, h->surplus_huge_pages); 1604e4e574b7SAdam Litke 1605685f3457SLee Schermerhorn /* 1606685f3457SLee Schermerhorn * We want to release as many surplus pages as possible, spread 16079b5e5d0fSLee Schermerhorn * evenly across all nodes with memory. Iterate across these nodes 16089b5e5d0fSLee Schermerhorn * until we can no longer free unreserved surplus pages. This occurs 16099b5e5d0fSLee Schermerhorn * when the nodes with surplus pages have no free pages. 16109b5e5d0fSLee Schermerhorn * free_pool_huge_page() will balance the the freed pages across the 16119b5e5d0fSLee Schermerhorn * on-line nodes with memory and will handle the hstate accounting. 1612685f3457SLee Schermerhorn */ 1613685f3457SLee Schermerhorn while (nr_pages--) { 16148cebfcd0SLai Jiangshan if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1)) 1615685f3457SLee Schermerhorn break; 16167848a4bfSMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 1617e4e574b7SAdam Litke } 1618e4e574b7SAdam Litke } 1619e4e574b7SAdam Litke 16205e911373SMike Kravetz 1621c37f9fb1SAndy Whitcroft /* 1622feba16e2SMike Kravetz * vma_needs_reservation, vma_commit_reservation and vma_end_reservation 16235e911373SMike Kravetz * are used by the huge page allocation routines to manage reservations. 1624cf3ad20bSMike Kravetz * 1625cf3ad20bSMike Kravetz * vma_needs_reservation is called to determine if the huge page at addr 1626cf3ad20bSMike Kravetz * within the vma has an associated reservation. If a reservation is 1627cf3ad20bSMike Kravetz * needed, the value 1 is returned. The caller is then responsible for 1628cf3ad20bSMike Kravetz * managing the global reservation and subpool usage counts. After 1629cf3ad20bSMike Kravetz * the huge page has been allocated, vma_commit_reservation is called 1630feba16e2SMike Kravetz * to add the page to the reservation map. If the page allocation fails, 1631feba16e2SMike Kravetz * the reservation must be ended instead of committed. vma_end_reservation 1632feba16e2SMike Kravetz * is called in such cases. 1633cf3ad20bSMike Kravetz * 1634cf3ad20bSMike Kravetz * In the normal case, vma_commit_reservation returns the same value 1635cf3ad20bSMike Kravetz * as the preceding vma_needs_reservation call. The only time this 1636cf3ad20bSMike Kravetz * is not the case is if a reserve map was changed between calls. It 1637cf3ad20bSMike Kravetz * is the responsibility of the caller to notice the difference and 1638cf3ad20bSMike Kravetz * take appropriate action. 1639c37f9fb1SAndy Whitcroft */ 16405e911373SMike Kravetz enum vma_resv_mode { 16415e911373SMike Kravetz VMA_NEEDS_RESV, 16425e911373SMike Kravetz VMA_COMMIT_RESV, 1643feba16e2SMike Kravetz VMA_END_RESV, 16445e911373SMike Kravetz }; 1645cf3ad20bSMike Kravetz static long __vma_reservation_common(struct hstate *h, 1646cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr, 16475e911373SMike Kravetz enum vma_resv_mode mode) 1648c37f9fb1SAndy Whitcroft { 16494e35f483SJoonsoo Kim struct resv_map *resv; 16504e35f483SJoonsoo Kim pgoff_t idx; 1651cf3ad20bSMike Kravetz long ret; 1652c37f9fb1SAndy Whitcroft 16534e35f483SJoonsoo Kim resv = vma_resv_map(vma); 16544e35f483SJoonsoo Kim if (!resv) 1655c37f9fb1SAndy Whitcroft return 1; 1656c37f9fb1SAndy Whitcroft 16574e35f483SJoonsoo Kim idx = vma_hugecache_offset(h, vma, addr); 16585e911373SMike Kravetz switch (mode) { 16595e911373SMike Kravetz case VMA_NEEDS_RESV: 1660cf3ad20bSMike Kravetz ret = region_chg(resv, idx, idx + 1); 16615e911373SMike Kravetz break; 16625e911373SMike Kravetz case VMA_COMMIT_RESV: 16635e911373SMike Kravetz ret = region_add(resv, idx, idx + 1); 16645e911373SMike Kravetz break; 1665feba16e2SMike Kravetz case VMA_END_RESV: 16665e911373SMike Kravetz region_abort(resv, idx, idx + 1); 16675e911373SMike Kravetz ret = 0; 16685e911373SMike Kravetz break; 16695e911373SMike Kravetz default: 16705e911373SMike Kravetz BUG(); 16715e911373SMike Kravetz } 167284afd99bSAndy Whitcroft 16734e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) 1674cf3ad20bSMike Kravetz return ret; 16754e35f483SJoonsoo Kim else 1676cf3ad20bSMike Kravetz return ret < 0 ? ret : 0; 167784afd99bSAndy Whitcroft } 1678cf3ad20bSMike Kravetz 1679cf3ad20bSMike Kravetz static long vma_needs_reservation(struct hstate *h, 1680a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long addr) 1681c37f9fb1SAndy Whitcroft { 16825e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV); 1683cf3ad20bSMike Kravetz } 1684c37f9fb1SAndy Whitcroft 1685cf3ad20bSMike Kravetz static long vma_commit_reservation(struct hstate *h, 1686cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr) 1687cf3ad20bSMike Kravetz { 16885e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV); 16895e911373SMike Kravetz } 16905e911373SMike Kravetz 1691feba16e2SMike Kravetz static void vma_end_reservation(struct hstate *h, 16925e911373SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 16935e911373SMike Kravetz { 1694feba16e2SMike Kravetz (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV); 1695c37f9fb1SAndy Whitcroft } 1696c37f9fb1SAndy Whitcroft 1697348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma, 169804f2cbe3SMel Gorman unsigned long addr, int avoid_reserve) 1699348ea204SAdam Litke { 170090481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 1701a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 1702348ea204SAdam Litke struct page *page; 170333039678SMike Kravetz long chg, commit; 17046d76dcf4SAneesh Kumar K.V int ret, idx; 17056d76dcf4SAneesh Kumar K.V struct hugetlb_cgroup *h_cg; 17062fc39cecSAdam Litke 17076d76dcf4SAneesh Kumar K.V idx = hstate_index(h); 1708a1e78772SMel Gorman /* 170990481622SDavid Gibson * Processes that did not create the mapping will have no 171090481622SDavid Gibson * reserves and will not have accounted against subpool 171190481622SDavid Gibson * limit. Check that the subpool limit can be made before 171290481622SDavid Gibson * satisfying the allocation MAP_NORESERVE mappings may also 171390481622SDavid Gibson * need pages and subpool limit allocated allocated if no reserve 171490481622SDavid Gibson * mapping overlaps. 1715a1e78772SMel Gorman */ 1716a5516438SAndi Kleen chg = vma_needs_reservation(h, vma, addr); 1717c37f9fb1SAndy Whitcroft if (chg < 0) 171876dcee75SAneesh Kumar K.V return ERR_PTR(-ENOMEM); 17198bb3f12eSJoonsoo Kim if (chg || avoid_reserve) 17205e911373SMike Kravetz if (hugepage_subpool_get_pages(spool, 1) < 0) { 1721feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 172276dcee75SAneesh Kumar K.V return ERR_PTR(-ENOSPC); 17235e911373SMike Kravetz } 172490d8b7e6SAdam Litke 17256d76dcf4SAneesh Kumar K.V ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); 17268f34af6fSJianyu Zhan if (ret) 17278f34af6fSJianyu Zhan goto out_subpool_put; 17288f34af6fSJianyu Zhan 1729a1e78772SMel Gorman spin_lock(&hugetlb_lock); 1730af0ed73eSJoonsoo Kim page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg); 173181a6fcaeSJoonsoo Kim if (!page) { 173294ae8ba7SAneesh Kumar K.V spin_unlock(&hugetlb_lock); 1733bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, NUMA_NO_NODE); 17348f34af6fSJianyu Zhan if (!page) 17358f34af6fSJianyu Zhan goto out_uncharge_cgroup; 17368f34af6fSJianyu Zhan 173779dbb236SAneesh Kumar K.V spin_lock(&hugetlb_lock); 173879dbb236SAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 173981a6fcaeSJoonsoo Kim /* Fall through */ 1740a1e78772SMel Gorman } 174181a6fcaeSJoonsoo Kim hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page); 174281a6fcaeSJoonsoo Kim spin_unlock(&hugetlb_lock); 1743a1e78772SMel Gorman 174490481622SDavid Gibson set_page_private(page, (unsigned long)spool); 1745a1e78772SMel Gorman 174633039678SMike Kravetz commit = vma_commit_reservation(h, vma, addr); 174733039678SMike Kravetz if (unlikely(chg > commit)) { 174833039678SMike Kravetz /* 174933039678SMike Kravetz * The page was added to the reservation map between 175033039678SMike Kravetz * vma_needs_reservation and vma_commit_reservation. 175133039678SMike Kravetz * This indicates a race with hugetlb_reserve_pages. 175233039678SMike Kravetz * Adjust for the subpool count incremented above AND 175333039678SMike Kravetz * in hugetlb_reserve_pages for the same page. Also, 175433039678SMike Kravetz * the reservation count added in hugetlb_reserve_pages 175533039678SMike Kravetz * no longer applies. 175633039678SMike Kravetz */ 175733039678SMike Kravetz long rsv_adjust; 175833039678SMike Kravetz 175933039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 1); 176033039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 176133039678SMike Kravetz } 17627893d1d5SAdam Litke return page; 17638f34af6fSJianyu Zhan 17648f34af6fSJianyu Zhan out_uncharge_cgroup: 17658f34af6fSJianyu Zhan hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); 17668f34af6fSJianyu Zhan out_subpool_put: 17678f34af6fSJianyu Zhan if (chg || avoid_reserve) 17688f34af6fSJianyu Zhan hugepage_subpool_put_pages(spool, 1); 1769feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 17708f34af6fSJianyu Zhan return ERR_PTR(-ENOSPC); 1771b45b5bd6SDavid Gibson } 1772b45b5bd6SDavid Gibson 177374060e4dSNaoya Horiguchi /* 177474060e4dSNaoya Horiguchi * alloc_huge_page()'s wrapper which simply returns the page if allocation 177574060e4dSNaoya Horiguchi * succeeds, otherwise NULL. This function is called from new_vma_page(), 177674060e4dSNaoya Horiguchi * where no ERR_VALUE is expected to be returned. 177774060e4dSNaoya Horiguchi */ 177874060e4dSNaoya Horiguchi struct page *alloc_huge_page_noerr(struct vm_area_struct *vma, 177974060e4dSNaoya Horiguchi unsigned long addr, int avoid_reserve) 178074060e4dSNaoya Horiguchi { 178174060e4dSNaoya Horiguchi struct page *page = alloc_huge_page(vma, addr, avoid_reserve); 178274060e4dSNaoya Horiguchi if (IS_ERR(page)) 178374060e4dSNaoya Horiguchi page = NULL; 178474060e4dSNaoya Horiguchi return page; 178574060e4dSNaoya Horiguchi } 178674060e4dSNaoya Horiguchi 178791f47662SCyrill Gorcunov int __weak alloc_bootmem_huge_page(struct hstate *h) 1788aa888a74SAndi Kleen { 1789aa888a74SAndi Kleen struct huge_bootmem_page *m; 1790b2261026SJoonsoo Kim int nr_nodes, node; 1791aa888a74SAndi Kleen 1792b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) { 1793aa888a74SAndi Kleen void *addr; 1794aa888a74SAndi Kleen 17958b89a116SGrygorii Strashko addr = memblock_virt_alloc_try_nid_nopanic( 17968b89a116SGrygorii Strashko huge_page_size(h), huge_page_size(h), 17978b89a116SGrygorii Strashko 0, BOOTMEM_ALLOC_ACCESSIBLE, node); 1798aa888a74SAndi Kleen if (addr) { 1799aa888a74SAndi Kleen /* 1800aa888a74SAndi Kleen * Use the beginning of the huge page to store the 1801aa888a74SAndi Kleen * huge_bootmem_page struct (until gather_bootmem 1802aa888a74SAndi Kleen * puts them into the mem_map). 1803aa888a74SAndi Kleen */ 1804aa888a74SAndi Kleen m = addr; 1805aa888a74SAndi Kleen goto found; 1806aa888a74SAndi Kleen } 1807aa888a74SAndi Kleen } 1808aa888a74SAndi Kleen return 0; 1809aa888a74SAndi Kleen 1810aa888a74SAndi Kleen found: 1811df994eadSLuiz Capitulino BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h))); 1812aa888a74SAndi Kleen /* Put them into a private list first because mem_map is not up yet */ 1813aa888a74SAndi Kleen list_add(&m->list, &huge_boot_pages); 1814aa888a74SAndi Kleen m->hstate = h; 1815aa888a74SAndi Kleen return 1; 1816aa888a74SAndi Kleen } 1817aa888a74SAndi Kleen 1818f412c97aSDavid Rientjes static void __init prep_compound_huge_page(struct page *page, int order) 181918229df5SAndy Whitcroft { 182018229df5SAndy Whitcroft if (unlikely(order > (MAX_ORDER - 1))) 182118229df5SAndy Whitcroft prep_compound_gigantic_page(page, order); 182218229df5SAndy Whitcroft else 182318229df5SAndy Whitcroft prep_compound_page(page, order); 182418229df5SAndy Whitcroft } 182518229df5SAndy Whitcroft 1826aa888a74SAndi Kleen /* Put bootmem huge pages into the standard lists after mem_map is up */ 1827aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void) 1828aa888a74SAndi Kleen { 1829aa888a74SAndi Kleen struct huge_bootmem_page *m; 1830aa888a74SAndi Kleen 1831aa888a74SAndi Kleen list_for_each_entry(m, &huge_boot_pages, list) { 1832aa888a74SAndi Kleen struct hstate *h = m->hstate; 1833ee8f248dSBecky Bruce struct page *page; 1834ee8f248dSBecky Bruce 1835ee8f248dSBecky Bruce #ifdef CONFIG_HIGHMEM 1836ee8f248dSBecky Bruce page = pfn_to_page(m->phys >> PAGE_SHIFT); 18378b89a116SGrygorii Strashko memblock_free_late(__pa(m), 1838ee8f248dSBecky Bruce sizeof(struct huge_bootmem_page)); 1839ee8f248dSBecky Bruce #else 1840ee8f248dSBecky Bruce page = virt_to_page(m); 1841ee8f248dSBecky Bruce #endif 1842aa888a74SAndi Kleen WARN_ON(page_count(page) != 1); 184318229df5SAndy Whitcroft prep_compound_huge_page(page, h->order); 1844ef5a22beSAndrea Arcangeli WARN_ON(PageReserved(page)); 1845aa888a74SAndi Kleen prep_new_huge_page(h, page, page_to_nid(page)); 1846b0320c7bSRafael Aquini /* 1847b0320c7bSRafael Aquini * If we had gigantic hugepages allocated at boot time, we need 1848b0320c7bSRafael Aquini * to restore the 'stolen' pages to totalram_pages in order to 1849b0320c7bSRafael Aquini * fix confusing memory reports from free(1) and another 1850b0320c7bSRafael Aquini * side-effects, like CommitLimit going negative. 1851b0320c7bSRafael Aquini */ 1852bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 18533dcc0571SJiang Liu adjust_managed_page_count(page, 1 << h->order); 1854aa888a74SAndi Kleen } 1855aa888a74SAndi Kleen } 1856aa888a74SAndi Kleen 18578faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h) 18581da177e4SLinus Torvalds { 18591da177e4SLinus Torvalds unsigned long i; 18601da177e4SLinus Torvalds 1861e5ff2159SAndi Kleen for (i = 0; i < h->max_huge_pages; ++i) { 1862bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) { 1863aa888a74SAndi Kleen if (!alloc_bootmem_huge_page(h)) 1864aa888a74SAndi Kleen break; 18659b5e5d0fSLee Schermerhorn } else if (!alloc_fresh_huge_page(h, 18668cebfcd0SLai Jiangshan &node_states[N_MEMORY])) 18671da177e4SLinus Torvalds break; 18681da177e4SLinus Torvalds } 18698faa8b07SAndi Kleen h->max_huge_pages = i; 1870e5ff2159SAndi Kleen } 1871e5ff2159SAndi Kleen 1872e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void) 1873e5ff2159SAndi Kleen { 1874e5ff2159SAndi Kleen struct hstate *h; 1875e5ff2159SAndi Kleen 1876e5ff2159SAndi Kleen for_each_hstate(h) { 1877641844f5SNaoya Horiguchi if (minimum_order > huge_page_order(h)) 1878641844f5SNaoya Horiguchi minimum_order = huge_page_order(h); 1879641844f5SNaoya Horiguchi 18808faa8b07SAndi Kleen /* oversize hugepages were init'ed in early boot */ 1881bae7f4aeSLuiz Capitulino if (!hstate_is_gigantic(h)) 18828faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(h); 1883e5ff2159SAndi Kleen } 1884641844f5SNaoya Horiguchi VM_BUG_ON(minimum_order == UINT_MAX); 1885e5ff2159SAndi Kleen } 1886e5ff2159SAndi Kleen 18874abd32dbSAndi Kleen static char * __init memfmt(char *buf, unsigned long n) 18884abd32dbSAndi Kleen { 18894abd32dbSAndi Kleen if (n >= (1UL << 30)) 18904abd32dbSAndi Kleen sprintf(buf, "%lu GB", n >> 30); 18914abd32dbSAndi Kleen else if (n >= (1UL << 20)) 18924abd32dbSAndi Kleen sprintf(buf, "%lu MB", n >> 20); 18934abd32dbSAndi Kleen else 18944abd32dbSAndi Kleen sprintf(buf, "%lu KB", n >> 10); 18954abd32dbSAndi Kleen return buf; 18964abd32dbSAndi Kleen } 18974abd32dbSAndi Kleen 1898e5ff2159SAndi Kleen static void __init report_hugepages(void) 1899e5ff2159SAndi Kleen { 1900e5ff2159SAndi Kleen struct hstate *h; 1901e5ff2159SAndi Kleen 1902e5ff2159SAndi Kleen for_each_hstate(h) { 19034abd32dbSAndi Kleen char buf[32]; 1904ffb22af5SAndrew Morton pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n", 19054abd32dbSAndi Kleen memfmt(buf, huge_page_size(h)), 19064abd32dbSAndi Kleen h->free_huge_pages); 1907e5ff2159SAndi Kleen } 1908e5ff2159SAndi Kleen } 1909e5ff2159SAndi Kleen 19101da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM 19116ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count, 19126ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 19131da177e4SLinus Torvalds { 19144415cc8dSChristoph Lameter int i; 19154415cc8dSChristoph Lameter 1916bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1917aa888a74SAndi Kleen return; 1918aa888a74SAndi Kleen 19196ae11b27SLee Schermerhorn for_each_node_mask(i, *nodes_allowed) { 19201da177e4SLinus Torvalds struct page *page, *next; 1921a5516438SAndi Kleen struct list_head *freel = &h->hugepage_freelists[i]; 1922a5516438SAndi Kleen list_for_each_entry_safe(page, next, freel, lru) { 1923a5516438SAndi Kleen if (count >= h->nr_huge_pages) 19246b0c880dSAdam Litke return; 19251da177e4SLinus Torvalds if (PageHighMem(page)) 19261da177e4SLinus Torvalds continue; 19271da177e4SLinus Torvalds list_del(&page->lru); 1928e5ff2159SAndi Kleen update_and_free_page(h, page); 1929a5516438SAndi Kleen h->free_huge_pages--; 1930a5516438SAndi Kleen h->free_huge_pages_node[page_to_nid(page)]--; 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds #else 19356ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count, 19366ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 19371da177e4SLinus Torvalds { 19381da177e4SLinus Torvalds } 19391da177e4SLinus Torvalds #endif 19401da177e4SLinus Torvalds 194120a0307cSWu Fengguang /* 194220a0307cSWu Fengguang * Increment or decrement surplus_huge_pages. Keep node-specific counters 194320a0307cSWu Fengguang * balanced by operating on them in a round-robin fashion. 194420a0307cSWu Fengguang * Returns 1 if an adjustment was made. 194520a0307cSWu Fengguang */ 19466ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, 19476ae11b27SLee Schermerhorn int delta) 194820a0307cSWu Fengguang { 1949b2261026SJoonsoo Kim int nr_nodes, node; 195020a0307cSWu Fengguang 195120a0307cSWu Fengguang VM_BUG_ON(delta != -1 && delta != 1); 195220a0307cSWu Fengguang 1953e8c5c824SLee Schermerhorn if (delta < 0) { 1954b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1955b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node]) 1956b2261026SJoonsoo Kim goto found; 1957b2261026SJoonsoo Kim } 1958b2261026SJoonsoo Kim } else { 1959b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1960b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node] < 1961b2261026SJoonsoo Kim h->nr_huge_pages_node[node]) 1962b2261026SJoonsoo Kim goto found; 1963e8c5c824SLee Schermerhorn } 19649a76db09SLee Schermerhorn } 1965b2261026SJoonsoo Kim return 0; 196620a0307cSWu Fengguang 1967b2261026SJoonsoo Kim found: 196820a0307cSWu Fengguang h->surplus_huge_pages += delta; 1969b2261026SJoonsoo Kim h->surplus_huge_pages_node[node] += delta; 1970b2261026SJoonsoo Kim return 1; 197120a0307cSWu Fengguang } 197220a0307cSWu Fengguang 1973a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) 19746ae11b27SLee Schermerhorn static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count, 19756ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 19761da177e4SLinus Torvalds { 19777893d1d5SAdam Litke unsigned long min_count, ret; 19781da177e4SLinus Torvalds 1979944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 1980aa888a74SAndi Kleen return h->max_huge_pages; 1981aa888a74SAndi Kleen 19827893d1d5SAdam Litke /* 19837893d1d5SAdam Litke * Increase the pool size 19847893d1d5SAdam Litke * First take pages out of surplus state. Then make up the 19857893d1d5SAdam Litke * remaining difference by allocating fresh huge pages. 1986d1c3fb1fSNishanth Aravamudan * 1987d1c3fb1fSNishanth Aravamudan * We might race with alloc_buddy_huge_page() here and be unable 1988d1c3fb1fSNishanth Aravamudan * to convert a surplus huge page to a normal huge page. That is 1989d1c3fb1fSNishanth Aravamudan * not critical, though, it just means the overall size of the 1990d1c3fb1fSNishanth Aravamudan * pool might be one hugepage larger than it needs to be, but 1991d1c3fb1fSNishanth Aravamudan * within all the constraints specified by the sysctls. 19927893d1d5SAdam Litke */ 19931da177e4SLinus Torvalds spin_lock(&hugetlb_lock); 1994a5516438SAndi Kleen while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { 19956ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, -1)) 19967893d1d5SAdam Litke break; 19977893d1d5SAdam Litke } 19987893d1d5SAdam Litke 1999a5516438SAndi Kleen while (count > persistent_huge_pages(h)) { 20007893d1d5SAdam Litke /* 20017893d1d5SAdam Litke * If this allocation races such that we no longer need the 20027893d1d5SAdam Litke * page, free_huge_page will handle it by freeing the page 20037893d1d5SAdam Litke * and reducing the surplus. 20047893d1d5SAdam Litke */ 20057893d1d5SAdam Litke spin_unlock(&hugetlb_lock); 2006944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) 2007944d9fecSLuiz Capitulino ret = alloc_fresh_gigantic_page(h, nodes_allowed); 2008944d9fecSLuiz Capitulino else 20096ae11b27SLee Schermerhorn ret = alloc_fresh_huge_page(h, nodes_allowed); 20107893d1d5SAdam Litke spin_lock(&hugetlb_lock); 20117893d1d5SAdam Litke if (!ret) 20127893d1d5SAdam Litke goto out; 20137893d1d5SAdam Litke 2014536240f2SMel Gorman /* Bail for signals. Probably ctrl-c from user */ 2015536240f2SMel Gorman if (signal_pending(current)) 2016536240f2SMel Gorman goto out; 20177893d1d5SAdam Litke } 20187893d1d5SAdam Litke 20197893d1d5SAdam Litke /* 20207893d1d5SAdam Litke * Decrease the pool size 20217893d1d5SAdam Litke * First return free pages to the buddy allocator (being careful 20227893d1d5SAdam Litke * to keep enough around to satisfy reservations). Then place 20237893d1d5SAdam Litke * pages into surplus state as needed so the pool will shrink 20247893d1d5SAdam Litke * to the desired size as pages become free. 2025d1c3fb1fSNishanth Aravamudan * 2026d1c3fb1fSNishanth Aravamudan * By placing pages into the surplus state independent of the 2027d1c3fb1fSNishanth Aravamudan * overcommit value, we are allowing the surplus pool size to 2028d1c3fb1fSNishanth Aravamudan * exceed overcommit. There are few sane options here. Since 2029d1c3fb1fSNishanth Aravamudan * alloc_buddy_huge_page() is checking the global counter, 2030d1c3fb1fSNishanth Aravamudan * though, we'll note that we're not allowed to exceed surplus 2031d1c3fb1fSNishanth Aravamudan * and won't grow the pool anywhere else. Not until one of the 2032d1c3fb1fSNishanth Aravamudan * sysctls are changed, or the surplus pages go out of use. 20337893d1d5SAdam Litke */ 2034a5516438SAndi Kleen min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; 20356b0c880dSAdam Litke min_count = max(count, min_count); 20366ae11b27SLee Schermerhorn try_to_free_low(h, min_count, nodes_allowed); 2037a5516438SAndi Kleen while (min_count < persistent_huge_pages(h)) { 20386ae11b27SLee Schermerhorn if (!free_pool_huge_page(h, nodes_allowed, 0)) 20391da177e4SLinus Torvalds break; 204055f67141SMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 20411da177e4SLinus Torvalds } 2042a5516438SAndi Kleen while (count < persistent_huge_pages(h)) { 20436ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, 1)) 20447893d1d5SAdam Litke break; 20457893d1d5SAdam Litke } 20467893d1d5SAdam Litke out: 2047a5516438SAndi Kleen ret = persistent_huge_pages(h); 20481da177e4SLinus Torvalds spin_unlock(&hugetlb_lock); 20497893d1d5SAdam Litke return ret; 20501da177e4SLinus Torvalds } 20511da177e4SLinus Torvalds 2052a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \ 2053a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 2054a3437870SNishanth Aravamudan 2055a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \ 2056a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = \ 2057a3437870SNishanth Aravamudan __ATTR(_name, 0644, _name##_show, _name##_store) 2058a3437870SNishanth Aravamudan 2059a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj; 2060a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 2061a3437870SNishanth Aravamudan 20629a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp); 20639a305230SLee Schermerhorn 20649a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp) 2065a3437870SNishanth Aravamudan { 2066a3437870SNishanth Aravamudan int i; 20679a305230SLee Schermerhorn 2068a3437870SNishanth Aravamudan for (i = 0; i < HUGE_MAX_HSTATE; i++) 20699a305230SLee Schermerhorn if (hstate_kobjs[i] == kobj) { 20709a305230SLee Schermerhorn if (nidp) 20719a305230SLee Schermerhorn *nidp = NUMA_NO_NODE; 2072a3437870SNishanth Aravamudan return &hstates[i]; 20739a305230SLee Schermerhorn } 20749a305230SLee Schermerhorn 20759a305230SLee Schermerhorn return kobj_to_node_hstate(kobj, nidp); 2076a3437870SNishanth Aravamudan } 2077a3437870SNishanth Aravamudan 207806808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj, 2079a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2080a3437870SNishanth Aravamudan { 20819a305230SLee Schermerhorn struct hstate *h; 20829a305230SLee Schermerhorn unsigned long nr_huge_pages; 20839a305230SLee Schermerhorn int nid; 20849a305230SLee Schermerhorn 20859a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 20869a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 20879a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages; 20889a305230SLee Schermerhorn else 20899a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages_node[nid]; 20909a305230SLee Schermerhorn 20919a305230SLee Schermerhorn return sprintf(buf, "%lu\n", nr_huge_pages); 2092a3437870SNishanth Aravamudan } 2093adbe8726SEric B Munson 2094238d3c13SDavid Rientjes static ssize_t __nr_hugepages_store_common(bool obey_mempolicy, 2095238d3c13SDavid Rientjes struct hstate *h, int nid, 2096238d3c13SDavid Rientjes unsigned long count, size_t len) 2097a3437870SNishanth Aravamudan { 2098a3437870SNishanth Aravamudan int err; 2099bad44b5bSDavid Rientjes NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY); 2100a3437870SNishanth Aravamudan 2101944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) { 2102adbe8726SEric B Munson err = -EINVAL; 2103adbe8726SEric B Munson goto out; 2104adbe8726SEric B Munson } 2105adbe8726SEric B Munson 21069a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) { 21079a305230SLee Schermerhorn /* 21089a305230SLee Schermerhorn * global hstate attribute 21099a305230SLee Schermerhorn */ 21109a305230SLee Schermerhorn if (!(obey_mempolicy && 21119a305230SLee Schermerhorn init_nodemask_of_mempolicy(nodes_allowed))) { 211206808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 21138cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 211406808b08SLee Schermerhorn } 21159a305230SLee Schermerhorn } else if (nodes_allowed) { 21169a305230SLee Schermerhorn /* 21179a305230SLee Schermerhorn * per node hstate attribute: adjust count to global, 21189a305230SLee Schermerhorn * but restrict alloc/free to the specified node. 21199a305230SLee Schermerhorn */ 21209a305230SLee Schermerhorn count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; 21219a305230SLee Schermerhorn init_nodemask_of_node(nodes_allowed, nid); 21229a305230SLee Schermerhorn } else 21238cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 21249a305230SLee Schermerhorn 212506808b08SLee Schermerhorn h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed); 2126a3437870SNishanth Aravamudan 21278cebfcd0SLai Jiangshan if (nodes_allowed != &node_states[N_MEMORY]) 212806808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 212906808b08SLee Schermerhorn 213006808b08SLee Schermerhorn return len; 2131adbe8726SEric B Munson out: 2132adbe8726SEric B Munson NODEMASK_FREE(nodes_allowed); 2133adbe8726SEric B Munson return err; 213406808b08SLee Schermerhorn } 213506808b08SLee Schermerhorn 2136238d3c13SDavid Rientjes static ssize_t nr_hugepages_store_common(bool obey_mempolicy, 2137238d3c13SDavid Rientjes struct kobject *kobj, const char *buf, 2138238d3c13SDavid Rientjes size_t len) 2139238d3c13SDavid Rientjes { 2140238d3c13SDavid Rientjes struct hstate *h; 2141238d3c13SDavid Rientjes unsigned long count; 2142238d3c13SDavid Rientjes int nid; 2143238d3c13SDavid Rientjes int err; 2144238d3c13SDavid Rientjes 2145238d3c13SDavid Rientjes err = kstrtoul(buf, 10, &count); 2146238d3c13SDavid Rientjes if (err) 2147238d3c13SDavid Rientjes return err; 2148238d3c13SDavid Rientjes 2149238d3c13SDavid Rientjes h = kobj_to_hstate(kobj, &nid); 2150238d3c13SDavid Rientjes return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len); 2151238d3c13SDavid Rientjes } 2152238d3c13SDavid Rientjes 215306808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj, 215406808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 215506808b08SLee Schermerhorn { 215606808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 215706808b08SLee Schermerhorn } 215806808b08SLee Schermerhorn 215906808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj, 216006808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 216106808b08SLee Schermerhorn { 2162238d3c13SDavid Rientjes return nr_hugepages_store_common(false, kobj, buf, len); 2163a3437870SNishanth Aravamudan } 2164a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages); 2165a3437870SNishanth Aravamudan 216606808b08SLee Schermerhorn #ifdef CONFIG_NUMA 216706808b08SLee Schermerhorn 216806808b08SLee Schermerhorn /* 216906808b08SLee Schermerhorn * hstate attribute for optionally mempolicy-based constraint on persistent 217006808b08SLee Schermerhorn * huge page alloc/free. 217106808b08SLee Schermerhorn */ 217206808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj, 217306808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 217406808b08SLee Schermerhorn { 217506808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 217606808b08SLee Schermerhorn } 217706808b08SLee Schermerhorn 217806808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj, 217906808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 218006808b08SLee Schermerhorn { 2181238d3c13SDavid Rientjes return nr_hugepages_store_common(true, kobj, buf, len); 218206808b08SLee Schermerhorn } 218306808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy); 218406808b08SLee Schermerhorn #endif 218506808b08SLee Schermerhorn 218606808b08SLee Schermerhorn 2187a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, 2188a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2189a3437870SNishanth Aravamudan { 21909a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2191a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages); 2192a3437870SNishanth Aravamudan } 2193adbe8726SEric B Munson 2194a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, 2195a3437870SNishanth Aravamudan struct kobj_attribute *attr, const char *buf, size_t count) 2196a3437870SNishanth Aravamudan { 2197a3437870SNishanth Aravamudan int err; 2198a3437870SNishanth Aravamudan unsigned long input; 21999a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2200a3437870SNishanth Aravamudan 2201bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 2202adbe8726SEric B Munson return -EINVAL; 2203adbe8726SEric B Munson 22043dbb95f7SJingoo Han err = kstrtoul(buf, 10, &input); 2205a3437870SNishanth Aravamudan if (err) 220673ae31e5SEric B Munson return err; 2207a3437870SNishanth Aravamudan 2208a3437870SNishanth Aravamudan spin_lock(&hugetlb_lock); 2209a3437870SNishanth Aravamudan h->nr_overcommit_huge_pages = input; 2210a3437870SNishanth Aravamudan spin_unlock(&hugetlb_lock); 2211a3437870SNishanth Aravamudan 2212a3437870SNishanth Aravamudan return count; 2213a3437870SNishanth Aravamudan } 2214a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages); 2215a3437870SNishanth Aravamudan 2216a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj, 2217a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2218a3437870SNishanth Aravamudan { 22199a305230SLee Schermerhorn struct hstate *h; 22209a305230SLee Schermerhorn unsigned long free_huge_pages; 22219a305230SLee Schermerhorn int nid; 22229a305230SLee Schermerhorn 22239a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 22249a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 22259a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages; 22269a305230SLee Schermerhorn else 22279a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages_node[nid]; 22289a305230SLee Schermerhorn 22299a305230SLee Schermerhorn return sprintf(buf, "%lu\n", free_huge_pages); 2230a3437870SNishanth Aravamudan } 2231a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages); 2232a3437870SNishanth Aravamudan 2233a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj, 2234a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2235a3437870SNishanth Aravamudan { 22369a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2237a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->resv_huge_pages); 2238a3437870SNishanth Aravamudan } 2239a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages); 2240a3437870SNishanth Aravamudan 2241a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj, 2242a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2243a3437870SNishanth Aravamudan { 22449a305230SLee Schermerhorn struct hstate *h; 22459a305230SLee Schermerhorn unsigned long surplus_huge_pages; 22469a305230SLee Schermerhorn int nid; 22479a305230SLee Schermerhorn 22489a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 22499a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 22509a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages; 22519a305230SLee Schermerhorn else 22529a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages_node[nid]; 22539a305230SLee Schermerhorn 22549a305230SLee Schermerhorn return sprintf(buf, "%lu\n", surplus_huge_pages); 2255a3437870SNishanth Aravamudan } 2256a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages); 2257a3437870SNishanth Aravamudan 2258a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = { 2259a3437870SNishanth Aravamudan &nr_hugepages_attr.attr, 2260a3437870SNishanth Aravamudan &nr_overcommit_hugepages_attr.attr, 2261a3437870SNishanth Aravamudan &free_hugepages_attr.attr, 2262a3437870SNishanth Aravamudan &resv_hugepages_attr.attr, 2263a3437870SNishanth Aravamudan &surplus_hugepages_attr.attr, 226406808b08SLee Schermerhorn #ifdef CONFIG_NUMA 226506808b08SLee Schermerhorn &nr_hugepages_mempolicy_attr.attr, 226606808b08SLee Schermerhorn #endif 2267a3437870SNishanth Aravamudan NULL, 2268a3437870SNishanth Aravamudan }; 2269a3437870SNishanth Aravamudan 2270a3437870SNishanth Aravamudan static struct attribute_group hstate_attr_group = { 2271a3437870SNishanth Aravamudan .attrs = hstate_attrs, 2272a3437870SNishanth Aravamudan }; 2273a3437870SNishanth Aravamudan 2274094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, 22759a305230SLee Schermerhorn struct kobject **hstate_kobjs, 22769a305230SLee Schermerhorn struct attribute_group *hstate_attr_group) 2277a3437870SNishanth Aravamudan { 2278a3437870SNishanth Aravamudan int retval; 2279972dc4deSAneesh Kumar K.V int hi = hstate_index(h); 2280a3437870SNishanth Aravamudan 22819a305230SLee Schermerhorn hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); 22829a305230SLee Schermerhorn if (!hstate_kobjs[hi]) 2283a3437870SNishanth Aravamudan return -ENOMEM; 2284a3437870SNishanth Aravamudan 22859a305230SLee Schermerhorn retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group); 2286a3437870SNishanth Aravamudan if (retval) 22879a305230SLee Schermerhorn kobject_put(hstate_kobjs[hi]); 2288a3437870SNishanth Aravamudan 2289a3437870SNishanth Aravamudan return retval; 2290a3437870SNishanth Aravamudan } 2291a3437870SNishanth Aravamudan 2292a3437870SNishanth Aravamudan static void __init hugetlb_sysfs_init(void) 2293a3437870SNishanth Aravamudan { 2294a3437870SNishanth Aravamudan struct hstate *h; 2295a3437870SNishanth Aravamudan int err; 2296a3437870SNishanth Aravamudan 2297a3437870SNishanth Aravamudan hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); 2298a3437870SNishanth Aravamudan if (!hugepages_kobj) 2299a3437870SNishanth Aravamudan return; 2300a3437870SNishanth Aravamudan 2301a3437870SNishanth Aravamudan for_each_hstate(h) { 23029a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, 23039a305230SLee Schermerhorn hstate_kobjs, &hstate_attr_group); 2304a3437870SNishanth Aravamudan if (err) 2305ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s", h->name); 2306a3437870SNishanth Aravamudan } 2307a3437870SNishanth Aravamudan } 2308a3437870SNishanth Aravamudan 23099a305230SLee Schermerhorn #ifdef CONFIG_NUMA 23109a305230SLee Schermerhorn 23119a305230SLee Schermerhorn /* 23129a305230SLee Schermerhorn * node_hstate/s - associate per node hstate attributes, via their kobjects, 231310fbcf4cSKay Sievers * with node devices in node_devices[] using a parallel array. The array 231410fbcf4cSKay Sievers * index of a node device or _hstate == node id. 231510fbcf4cSKay Sievers * This is here to avoid any static dependency of the node device driver, in 23169a305230SLee Schermerhorn * the base kernel, on the hugetlb module. 23179a305230SLee Schermerhorn */ 23189a305230SLee Schermerhorn struct node_hstate { 23199a305230SLee Schermerhorn struct kobject *hugepages_kobj; 23209a305230SLee Schermerhorn struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 23219a305230SLee Schermerhorn }; 23229a305230SLee Schermerhorn struct node_hstate node_hstates[MAX_NUMNODES]; 23239a305230SLee Schermerhorn 23249a305230SLee Schermerhorn /* 232510fbcf4cSKay Sievers * A subset of global hstate attributes for node devices 23269a305230SLee Schermerhorn */ 23279a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = { 23289a305230SLee Schermerhorn &nr_hugepages_attr.attr, 23299a305230SLee Schermerhorn &free_hugepages_attr.attr, 23309a305230SLee Schermerhorn &surplus_hugepages_attr.attr, 23319a305230SLee Schermerhorn NULL, 23329a305230SLee Schermerhorn }; 23339a305230SLee Schermerhorn 23349a305230SLee Schermerhorn static struct attribute_group per_node_hstate_attr_group = { 23359a305230SLee Schermerhorn .attrs = per_node_hstate_attrs, 23369a305230SLee Schermerhorn }; 23379a305230SLee Schermerhorn 23389a305230SLee Schermerhorn /* 233910fbcf4cSKay Sievers * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj. 23409a305230SLee Schermerhorn * Returns node id via non-NULL nidp. 23419a305230SLee Schermerhorn */ 23429a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 23439a305230SLee Schermerhorn { 23449a305230SLee Schermerhorn int nid; 23459a305230SLee Schermerhorn 23469a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) { 23479a305230SLee Schermerhorn struct node_hstate *nhs = &node_hstates[nid]; 23489a305230SLee Schermerhorn int i; 23499a305230SLee Schermerhorn for (i = 0; i < HUGE_MAX_HSTATE; i++) 23509a305230SLee Schermerhorn if (nhs->hstate_kobjs[i] == kobj) { 23519a305230SLee Schermerhorn if (nidp) 23529a305230SLee Schermerhorn *nidp = nid; 23539a305230SLee Schermerhorn return &hstates[i]; 23549a305230SLee Schermerhorn } 23559a305230SLee Schermerhorn } 23569a305230SLee Schermerhorn 23579a305230SLee Schermerhorn BUG(); 23589a305230SLee Schermerhorn return NULL; 23599a305230SLee Schermerhorn } 23609a305230SLee Schermerhorn 23619a305230SLee Schermerhorn /* 236210fbcf4cSKay Sievers * Unregister hstate attributes from a single node device. 23639a305230SLee Schermerhorn * No-op if no hstate attributes attached. 23649a305230SLee Schermerhorn */ 23653cd8b44fSClaudiu Ghioc static void hugetlb_unregister_node(struct node *node) 23669a305230SLee Schermerhorn { 23679a305230SLee Schermerhorn struct hstate *h; 236810fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 23699a305230SLee Schermerhorn 23709a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 23719b5e5d0fSLee Schermerhorn return; /* no hstate attributes */ 23729a305230SLee Schermerhorn 2373972dc4deSAneesh Kumar K.V for_each_hstate(h) { 2374972dc4deSAneesh Kumar K.V int idx = hstate_index(h); 2375972dc4deSAneesh Kumar K.V if (nhs->hstate_kobjs[idx]) { 2376972dc4deSAneesh Kumar K.V kobject_put(nhs->hstate_kobjs[idx]); 2377972dc4deSAneesh Kumar K.V nhs->hstate_kobjs[idx] = NULL; 2378972dc4deSAneesh Kumar K.V } 23799a305230SLee Schermerhorn } 23809a305230SLee Schermerhorn 23819a305230SLee Schermerhorn kobject_put(nhs->hugepages_kobj); 23829a305230SLee Schermerhorn nhs->hugepages_kobj = NULL; 23839a305230SLee Schermerhorn } 23849a305230SLee Schermerhorn 23859a305230SLee Schermerhorn /* 238610fbcf4cSKay Sievers * hugetlb module exit: unregister hstate attributes from node devices 23879a305230SLee Schermerhorn * that have them. 23889a305230SLee Schermerhorn */ 23899a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) 23909a305230SLee Schermerhorn { 23919a305230SLee Schermerhorn int nid; 23929a305230SLee Schermerhorn 23939a305230SLee Schermerhorn /* 239410fbcf4cSKay Sievers * disable node device registrations. 23959a305230SLee Schermerhorn */ 23969a305230SLee Schermerhorn register_hugetlbfs_with_node(NULL, NULL); 23979a305230SLee Schermerhorn 23989a305230SLee Schermerhorn /* 23999a305230SLee Schermerhorn * remove hstate attributes from any nodes that have them. 24009a305230SLee Schermerhorn */ 24019a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) 24028732794bSWen Congyang hugetlb_unregister_node(node_devices[nid]); 24039a305230SLee Schermerhorn } 24049a305230SLee Schermerhorn 24059a305230SLee Schermerhorn /* 240610fbcf4cSKay Sievers * Register hstate attributes for a single node device. 24079a305230SLee Schermerhorn * No-op if attributes already registered. 24089a305230SLee Schermerhorn */ 24093cd8b44fSClaudiu Ghioc static void hugetlb_register_node(struct node *node) 24109a305230SLee Schermerhorn { 24119a305230SLee Schermerhorn struct hstate *h; 241210fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 24139a305230SLee Schermerhorn int err; 24149a305230SLee Schermerhorn 24159a305230SLee Schermerhorn if (nhs->hugepages_kobj) 24169a305230SLee Schermerhorn return; /* already allocated */ 24179a305230SLee Schermerhorn 24189a305230SLee Schermerhorn nhs->hugepages_kobj = kobject_create_and_add("hugepages", 241910fbcf4cSKay Sievers &node->dev.kobj); 24209a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 24219a305230SLee Schermerhorn return; 24229a305230SLee Schermerhorn 24239a305230SLee Schermerhorn for_each_hstate(h) { 24249a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, 24259a305230SLee Schermerhorn nhs->hstate_kobjs, 24269a305230SLee Schermerhorn &per_node_hstate_attr_group); 24279a305230SLee Schermerhorn if (err) { 2428ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s for node %d\n", 242910fbcf4cSKay Sievers h->name, node->dev.id); 24309a305230SLee Schermerhorn hugetlb_unregister_node(node); 24319a305230SLee Schermerhorn break; 24329a305230SLee Schermerhorn } 24339a305230SLee Schermerhorn } 24349a305230SLee Schermerhorn } 24359a305230SLee Schermerhorn 24369a305230SLee Schermerhorn /* 24379b5e5d0fSLee Schermerhorn * hugetlb init time: register hstate attributes for all registered node 243810fbcf4cSKay Sievers * devices of nodes that have memory. All on-line nodes should have 243910fbcf4cSKay Sievers * registered their associated device by this time. 24409a305230SLee Schermerhorn */ 24417d9ca000SLuiz Capitulino static void __init hugetlb_register_all_nodes(void) 24429a305230SLee Schermerhorn { 24439a305230SLee Schermerhorn int nid; 24449a305230SLee Schermerhorn 24458cebfcd0SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 24468732794bSWen Congyang struct node *node = node_devices[nid]; 244710fbcf4cSKay Sievers if (node->dev.id == nid) 24489a305230SLee Schermerhorn hugetlb_register_node(node); 24499a305230SLee Schermerhorn } 24509a305230SLee Schermerhorn 24519a305230SLee Schermerhorn /* 245210fbcf4cSKay Sievers * Let the node device driver know we're here so it can 24539a305230SLee Schermerhorn * [un]register hstate attributes on node hotplug. 24549a305230SLee Schermerhorn */ 24559a305230SLee Schermerhorn register_hugetlbfs_with_node(hugetlb_register_node, 24569a305230SLee Schermerhorn hugetlb_unregister_node); 24579a305230SLee Schermerhorn } 24589a305230SLee Schermerhorn #else /* !CONFIG_NUMA */ 24599a305230SLee Schermerhorn 24609a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 24619a305230SLee Schermerhorn { 24629a305230SLee Schermerhorn BUG(); 24639a305230SLee Schermerhorn if (nidp) 24649a305230SLee Schermerhorn *nidp = -1; 24659a305230SLee Schermerhorn return NULL; 24669a305230SLee Schermerhorn } 24679a305230SLee Schermerhorn 24689a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) { } 24699a305230SLee Schermerhorn 24709a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { } 24719a305230SLee Schermerhorn 24729a305230SLee Schermerhorn #endif 24739a305230SLee Schermerhorn 2474a3437870SNishanth Aravamudan static void __exit hugetlb_exit(void) 2475a3437870SNishanth Aravamudan { 2476a3437870SNishanth Aravamudan struct hstate *h; 2477a3437870SNishanth Aravamudan 24789a305230SLee Schermerhorn hugetlb_unregister_all_nodes(); 24799a305230SLee Schermerhorn 2480a3437870SNishanth Aravamudan for_each_hstate(h) { 2481972dc4deSAneesh Kumar K.V kobject_put(hstate_kobjs[hstate_index(h)]); 2482a3437870SNishanth Aravamudan } 2483a3437870SNishanth Aravamudan 2484a3437870SNishanth Aravamudan kobject_put(hugepages_kobj); 2485c672c7f2SMike Kravetz kfree(hugetlb_fault_mutex_table); 2486a3437870SNishanth Aravamudan } 2487a3437870SNishanth Aravamudan module_exit(hugetlb_exit); 2488a3437870SNishanth Aravamudan 2489a3437870SNishanth Aravamudan static int __init hugetlb_init(void) 2490a3437870SNishanth Aravamudan { 24918382d914SDavidlohr Bueso int i; 24928382d914SDavidlohr Bueso 2493457c1b27SNishanth Aravamudan if (!hugepages_supported()) 24940ef89d25SBenjamin Herrenschmidt return 0; 2495a3437870SNishanth Aravamudan 2496e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) { 2497e11bfbfcSNick Piggin default_hstate_size = HPAGE_SIZE; 2498e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) 2499a3437870SNishanth Aravamudan hugetlb_add_hstate(HUGETLB_PAGE_ORDER); 2500a3437870SNishanth Aravamudan } 2501972dc4deSAneesh Kumar K.V default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size)); 2502e11bfbfcSNick Piggin if (default_hstate_max_huge_pages) 2503e11bfbfcSNick Piggin default_hstate.max_huge_pages = default_hstate_max_huge_pages; 2504a3437870SNishanth Aravamudan 2505a3437870SNishanth Aravamudan hugetlb_init_hstates(); 2506aa888a74SAndi Kleen gather_bootmem_prealloc(); 2507a3437870SNishanth Aravamudan report_hugepages(); 2508a3437870SNishanth Aravamudan 2509a3437870SNishanth Aravamudan hugetlb_sysfs_init(); 25109a305230SLee Schermerhorn hugetlb_register_all_nodes(); 25117179e7bfSJianguo Wu hugetlb_cgroup_file_init(); 25129a305230SLee Schermerhorn 25138382d914SDavidlohr Bueso #ifdef CONFIG_SMP 25148382d914SDavidlohr Bueso num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); 25158382d914SDavidlohr Bueso #else 25168382d914SDavidlohr Bueso num_fault_mutexes = 1; 25178382d914SDavidlohr Bueso #endif 2518c672c7f2SMike Kravetz hugetlb_fault_mutex_table = 25198382d914SDavidlohr Bueso kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL); 2520c672c7f2SMike Kravetz BUG_ON(!hugetlb_fault_mutex_table); 25218382d914SDavidlohr Bueso 25228382d914SDavidlohr Bueso for (i = 0; i < num_fault_mutexes; i++) 2523c672c7f2SMike Kravetz mutex_init(&hugetlb_fault_mutex_table[i]); 2524a3437870SNishanth Aravamudan return 0; 2525a3437870SNishanth Aravamudan } 2526a3437870SNishanth Aravamudan module_init(hugetlb_init); 2527a3437870SNishanth Aravamudan 2528a3437870SNishanth Aravamudan /* Should be called on processing a hugepagesz=... option */ 2529a3437870SNishanth Aravamudan void __init hugetlb_add_hstate(unsigned order) 2530a3437870SNishanth Aravamudan { 2531a3437870SNishanth Aravamudan struct hstate *h; 25328faa8b07SAndi Kleen unsigned long i; 25338faa8b07SAndi Kleen 2534a3437870SNishanth Aravamudan if (size_to_hstate(PAGE_SIZE << order)) { 2535ffb22af5SAndrew Morton pr_warning("hugepagesz= specified twice, ignoring\n"); 2536a3437870SNishanth Aravamudan return; 2537a3437870SNishanth Aravamudan } 253847d38344SAneesh Kumar K.V BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); 2539a3437870SNishanth Aravamudan BUG_ON(order == 0); 254047d38344SAneesh Kumar K.V h = &hstates[hugetlb_max_hstate++]; 2541a3437870SNishanth Aravamudan h->order = order; 2542a3437870SNishanth Aravamudan h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); 25438faa8b07SAndi Kleen h->nr_huge_pages = 0; 25448faa8b07SAndi Kleen h->free_huge_pages = 0; 25458faa8b07SAndi Kleen for (i = 0; i < MAX_NUMNODES; ++i) 25468faa8b07SAndi Kleen INIT_LIST_HEAD(&h->hugepage_freelists[i]); 25470edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&h->hugepage_activelist); 25488cebfcd0SLai Jiangshan h->next_nid_to_alloc = first_node(node_states[N_MEMORY]); 25498cebfcd0SLai Jiangshan h->next_nid_to_free = first_node(node_states[N_MEMORY]); 2550a3437870SNishanth Aravamudan snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", 2551a3437870SNishanth Aravamudan huge_page_size(h)/1024); 25528faa8b07SAndi Kleen 2553a3437870SNishanth Aravamudan parsed_hstate = h; 2554a3437870SNishanth Aravamudan } 2555a3437870SNishanth Aravamudan 2556e11bfbfcSNick Piggin static int __init hugetlb_nrpages_setup(char *s) 2557a3437870SNishanth Aravamudan { 2558a3437870SNishanth Aravamudan unsigned long *mhp; 25598faa8b07SAndi Kleen static unsigned long *last_mhp; 2560a3437870SNishanth Aravamudan 2561a3437870SNishanth Aravamudan /* 256247d38344SAneesh Kumar K.V * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet, 2563a3437870SNishanth Aravamudan * so this hugepages= parameter goes to the "default hstate". 2564a3437870SNishanth Aravamudan */ 256547d38344SAneesh Kumar K.V if (!hugetlb_max_hstate) 2566a3437870SNishanth Aravamudan mhp = &default_hstate_max_huge_pages; 2567a3437870SNishanth Aravamudan else 2568a3437870SNishanth Aravamudan mhp = &parsed_hstate->max_huge_pages; 2569a3437870SNishanth Aravamudan 25708faa8b07SAndi Kleen if (mhp == last_mhp) { 2571ffb22af5SAndrew Morton pr_warning("hugepages= specified twice without " 25728faa8b07SAndi Kleen "interleaving hugepagesz=, ignoring\n"); 25738faa8b07SAndi Kleen return 1; 25748faa8b07SAndi Kleen } 25758faa8b07SAndi Kleen 2576a3437870SNishanth Aravamudan if (sscanf(s, "%lu", mhp) <= 0) 2577a3437870SNishanth Aravamudan *mhp = 0; 2578a3437870SNishanth Aravamudan 25798faa8b07SAndi Kleen /* 25808faa8b07SAndi Kleen * Global state is always initialized later in hugetlb_init. 25818faa8b07SAndi Kleen * But we need to allocate >= MAX_ORDER hstates here early to still 25828faa8b07SAndi Kleen * use the bootmem allocator. 25838faa8b07SAndi Kleen */ 258447d38344SAneesh Kumar K.V if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER) 25858faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(parsed_hstate); 25868faa8b07SAndi Kleen 25878faa8b07SAndi Kleen last_mhp = mhp; 25888faa8b07SAndi Kleen 2589a3437870SNishanth Aravamudan return 1; 2590a3437870SNishanth Aravamudan } 2591e11bfbfcSNick Piggin __setup("hugepages=", hugetlb_nrpages_setup); 2592e11bfbfcSNick Piggin 2593e11bfbfcSNick Piggin static int __init hugetlb_default_setup(char *s) 2594e11bfbfcSNick Piggin { 2595e11bfbfcSNick Piggin default_hstate_size = memparse(s, &s); 2596e11bfbfcSNick Piggin return 1; 2597e11bfbfcSNick Piggin } 2598e11bfbfcSNick Piggin __setup("default_hugepagesz=", hugetlb_default_setup); 2599a3437870SNishanth Aravamudan 26008a213460SNishanth Aravamudan static unsigned int cpuset_mems_nr(unsigned int *array) 26018a213460SNishanth Aravamudan { 26028a213460SNishanth Aravamudan int node; 26038a213460SNishanth Aravamudan unsigned int nr = 0; 26048a213460SNishanth Aravamudan 26058a213460SNishanth Aravamudan for_each_node_mask(node, cpuset_current_mems_allowed) 26068a213460SNishanth Aravamudan nr += array[node]; 26078a213460SNishanth Aravamudan 26088a213460SNishanth Aravamudan return nr; 26098a213460SNishanth Aravamudan } 26108a213460SNishanth Aravamudan 26118a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL 261206808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy, 261306808b08SLee Schermerhorn struct ctl_table *table, int write, 261406808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 26151da177e4SLinus Torvalds { 2616e5ff2159SAndi Kleen struct hstate *h = &default_hstate; 2617238d3c13SDavid Rientjes unsigned long tmp = h->max_huge_pages; 261808d4a246SMichal Hocko int ret; 2619e5ff2159SAndi Kleen 2620457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2621457c1b27SNishanth Aravamudan return -ENOTSUPP; 2622457c1b27SNishanth Aravamudan 2623e5ff2159SAndi Kleen table->data = &tmp; 2624e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 262508d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 262608d4a246SMichal Hocko if (ret) 262708d4a246SMichal Hocko goto out; 2628e5ff2159SAndi Kleen 2629238d3c13SDavid Rientjes if (write) 2630238d3c13SDavid Rientjes ret = __nr_hugepages_store_common(obey_mempolicy, h, 2631238d3c13SDavid Rientjes NUMA_NO_NODE, tmp, *length); 263208d4a246SMichal Hocko out: 263308d4a246SMichal Hocko return ret; 26341da177e4SLinus Torvalds } 2635396faf03SMel Gorman 263606808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write, 263706808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 263806808b08SLee Schermerhorn { 263906808b08SLee Schermerhorn 264006808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(false, table, write, 264106808b08SLee Schermerhorn buffer, length, ppos); 264206808b08SLee Schermerhorn } 264306808b08SLee Schermerhorn 264406808b08SLee Schermerhorn #ifdef CONFIG_NUMA 264506808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write, 264606808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 264706808b08SLee Schermerhorn { 264806808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(true, table, write, 264906808b08SLee Schermerhorn buffer, length, ppos); 265006808b08SLee Schermerhorn } 265106808b08SLee Schermerhorn #endif /* CONFIG_NUMA */ 265206808b08SLee Schermerhorn 2653a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write, 26548d65af78SAlexey Dobriyan void __user *buffer, 2655a3d0c6aaSNishanth Aravamudan size_t *length, loff_t *ppos) 2656a3d0c6aaSNishanth Aravamudan { 2657a5516438SAndi Kleen struct hstate *h = &default_hstate; 2658e5ff2159SAndi Kleen unsigned long tmp; 265908d4a246SMichal Hocko int ret; 2660e5ff2159SAndi Kleen 2661457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2662457c1b27SNishanth Aravamudan return -ENOTSUPP; 2663457c1b27SNishanth Aravamudan 2664e5ff2159SAndi Kleen tmp = h->nr_overcommit_huge_pages; 2665e5ff2159SAndi Kleen 2666bae7f4aeSLuiz Capitulino if (write && hstate_is_gigantic(h)) 2667adbe8726SEric B Munson return -EINVAL; 2668adbe8726SEric B Munson 2669e5ff2159SAndi Kleen table->data = &tmp; 2670e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 267108d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 267208d4a246SMichal Hocko if (ret) 267308d4a246SMichal Hocko goto out; 2674e5ff2159SAndi Kleen 2675e5ff2159SAndi Kleen if (write) { 2676064d9efeSNishanth Aravamudan spin_lock(&hugetlb_lock); 2677e5ff2159SAndi Kleen h->nr_overcommit_huge_pages = tmp; 2678a3d0c6aaSNishanth Aravamudan spin_unlock(&hugetlb_lock); 2679e5ff2159SAndi Kleen } 268008d4a246SMichal Hocko out: 268108d4a246SMichal Hocko return ret; 2682a3d0c6aaSNishanth Aravamudan } 2683a3d0c6aaSNishanth Aravamudan 26841da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 26851da177e4SLinus Torvalds 2686e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m) 26871da177e4SLinus Torvalds { 2688a5516438SAndi Kleen struct hstate *h = &default_hstate; 2689457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2690457c1b27SNishanth Aravamudan return; 2691e1759c21SAlexey Dobriyan seq_printf(m, 26921da177e4SLinus Torvalds "HugePages_Total: %5lu\n" 26931da177e4SLinus Torvalds "HugePages_Free: %5lu\n" 2694b45b5bd6SDavid Gibson "HugePages_Rsvd: %5lu\n" 26957893d1d5SAdam Litke "HugePages_Surp: %5lu\n" 26964f98a2feSRik van Riel "Hugepagesize: %8lu kB\n", 2697a5516438SAndi Kleen h->nr_huge_pages, 2698a5516438SAndi Kleen h->free_huge_pages, 2699a5516438SAndi Kleen h->resv_huge_pages, 2700a5516438SAndi Kleen h->surplus_huge_pages, 2701a5516438SAndi Kleen 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 27021da177e4SLinus Torvalds } 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf) 27051da177e4SLinus Torvalds { 2706a5516438SAndi Kleen struct hstate *h = &default_hstate; 2707457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2708457c1b27SNishanth Aravamudan return 0; 27091da177e4SLinus Torvalds return sprintf(buf, 27101da177e4SLinus Torvalds "Node %d HugePages_Total: %5u\n" 2711a1de0919SNishanth Aravamudan "Node %d HugePages_Free: %5u\n" 2712a1de0919SNishanth Aravamudan "Node %d HugePages_Surp: %5u\n", 2713a5516438SAndi Kleen nid, h->nr_huge_pages_node[nid], 2714a5516438SAndi Kleen nid, h->free_huge_pages_node[nid], 2715a5516438SAndi Kleen nid, h->surplus_huge_pages_node[nid]); 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds 2718949f7ec5SDavid Rientjes void hugetlb_show_meminfo(void) 2719949f7ec5SDavid Rientjes { 2720949f7ec5SDavid Rientjes struct hstate *h; 2721949f7ec5SDavid Rientjes int nid; 2722949f7ec5SDavid Rientjes 2723457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2724457c1b27SNishanth Aravamudan return; 2725457c1b27SNishanth Aravamudan 2726949f7ec5SDavid Rientjes for_each_node_state(nid, N_MEMORY) 2727949f7ec5SDavid Rientjes for_each_hstate(h) 2728949f7ec5SDavid Rientjes pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", 2729949f7ec5SDavid Rientjes nid, 2730949f7ec5SDavid Rientjes h->nr_huge_pages_node[nid], 2731949f7ec5SDavid Rientjes h->free_huge_pages_node[nid], 2732949f7ec5SDavid Rientjes h->surplus_huge_pages_node[nid], 2733949f7ec5SDavid Rientjes 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 2734949f7ec5SDavid Rientjes } 2735949f7ec5SDavid Rientjes 27361da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 27371da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void) 27381da177e4SLinus Torvalds { 2739d0028588SWanpeng Li struct hstate *h; 2740d0028588SWanpeng Li unsigned long nr_total_pages = 0; 2741d0028588SWanpeng Li 2742d0028588SWanpeng Li for_each_hstate(h) 2743d0028588SWanpeng Li nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); 2744d0028588SWanpeng Li return nr_total_pages; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds 2747a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta) 2748fc1b8a73SMel Gorman { 2749fc1b8a73SMel Gorman int ret = -ENOMEM; 2750fc1b8a73SMel Gorman 2751fc1b8a73SMel Gorman spin_lock(&hugetlb_lock); 2752fc1b8a73SMel Gorman /* 2753fc1b8a73SMel Gorman * When cpuset is configured, it breaks the strict hugetlb page 2754fc1b8a73SMel Gorman * reservation as the accounting is done on a global variable. Such 2755fc1b8a73SMel Gorman * reservation is completely rubbish in the presence of cpuset because 2756fc1b8a73SMel Gorman * the reservation is not checked against page availability for the 2757fc1b8a73SMel Gorman * current cpuset. Application can still potentially OOM'ed by kernel 2758fc1b8a73SMel Gorman * with lack of free htlb page in cpuset that the task is in. 2759fc1b8a73SMel Gorman * Attempt to enforce strict accounting with cpuset is almost 2760fc1b8a73SMel Gorman * impossible (or too ugly) because cpuset is too fluid that 2761fc1b8a73SMel Gorman * task or memory node can be dynamically moved between cpusets. 2762fc1b8a73SMel Gorman * 2763fc1b8a73SMel Gorman * The change of semantics for shared hugetlb mapping with cpuset is 2764fc1b8a73SMel Gorman * undesirable. However, in order to preserve some of the semantics, 2765fc1b8a73SMel Gorman * we fall back to check against current free page availability as 2766fc1b8a73SMel Gorman * a best attempt and hopefully to minimize the impact of changing 2767fc1b8a73SMel Gorman * semantics that cpuset has. 2768fc1b8a73SMel Gorman */ 2769fc1b8a73SMel Gorman if (delta > 0) { 2770a5516438SAndi Kleen if (gather_surplus_pages(h, delta) < 0) 2771fc1b8a73SMel Gorman goto out; 2772fc1b8a73SMel Gorman 2773a5516438SAndi Kleen if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { 2774a5516438SAndi Kleen return_unused_surplus_pages(h, delta); 2775fc1b8a73SMel Gorman goto out; 2776fc1b8a73SMel Gorman } 2777fc1b8a73SMel Gorman } 2778fc1b8a73SMel Gorman 2779fc1b8a73SMel Gorman ret = 0; 2780fc1b8a73SMel Gorman if (delta < 0) 2781a5516438SAndi Kleen return_unused_surplus_pages(h, (unsigned long) -delta); 2782fc1b8a73SMel Gorman 2783fc1b8a73SMel Gorman out: 2784fc1b8a73SMel Gorman spin_unlock(&hugetlb_lock); 2785fc1b8a73SMel Gorman return ret; 2786fc1b8a73SMel Gorman } 2787fc1b8a73SMel Gorman 278884afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma) 278984afd99bSAndy Whitcroft { 2790f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 279184afd99bSAndy Whitcroft 279284afd99bSAndy Whitcroft /* 279384afd99bSAndy Whitcroft * This new VMA should share its siblings reservation map if present. 279484afd99bSAndy Whitcroft * The VMA will only ever have a valid reservation map pointer where 279584afd99bSAndy Whitcroft * it is being copied for another still existing VMA. As that VMA 279625985edcSLucas De Marchi * has a reference to the reservation map it cannot disappear until 279784afd99bSAndy Whitcroft * after this open call completes. It is therefore safe to take a 279884afd99bSAndy Whitcroft * new reference here without additional locking. 279984afd99bSAndy Whitcroft */ 28004e35f483SJoonsoo Kim if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 2801f522c3acSJoonsoo Kim kref_get(&resv->refs); 280284afd99bSAndy Whitcroft } 280384afd99bSAndy Whitcroft 2804a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma) 2805a1e78772SMel Gorman { 2806a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2807f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 280890481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 28094e35f483SJoonsoo Kim unsigned long reserve, start, end; 28101c5ecae3SMike Kravetz long gbl_reserve; 281184afd99bSAndy Whitcroft 28124e35f483SJoonsoo Kim if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 28134e35f483SJoonsoo Kim return; 28144e35f483SJoonsoo Kim 2815a5516438SAndi Kleen start = vma_hugecache_offset(h, vma, vma->vm_start); 2816a5516438SAndi Kleen end = vma_hugecache_offset(h, vma, vma->vm_end); 281784afd99bSAndy Whitcroft 28184e35f483SJoonsoo Kim reserve = (end - start) - region_count(resv, start, end); 281984afd99bSAndy Whitcroft 2820f031dd27SJoonsoo Kim kref_put(&resv->refs, resv_map_release); 282184afd99bSAndy Whitcroft 28227251ff78SAdam Litke if (reserve) { 28231c5ecae3SMike Kravetz /* 28241c5ecae3SMike Kravetz * Decrement reserve counts. The global reserve count may be 28251c5ecae3SMike Kravetz * adjusted if the subpool has a minimum size. 28261c5ecae3SMike Kravetz */ 28271c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, reserve); 28281c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 28297251ff78SAdam Litke } 2830a1e78772SMel Gorman } 2831a1e78772SMel Gorman 28321da177e4SLinus Torvalds /* 28331da177e4SLinus Torvalds * We cannot handle pagefaults against hugetlb pages at all. They cause 28341da177e4SLinus Torvalds * handle_mm_fault() to try to instantiate regular-sized pages in the 28351da177e4SLinus Torvalds * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get 28361da177e4SLinus Torvalds * this far. 28371da177e4SLinus Torvalds */ 2838d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 28391da177e4SLinus Torvalds { 28401da177e4SLinus Torvalds BUG(); 2841d0217ac0SNick Piggin return 0; 28421da177e4SLinus Torvalds } 28431da177e4SLinus Torvalds 2844f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = { 2845d0217ac0SNick Piggin .fault = hugetlb_vm_op_fault, 284684afd99bSAndy Whitcroft .open = hugetlb_vm_op_open, 2847a1e78772SMel Gorman .close = hugetlb_vm_op_close, 28481da177e4SLinus Torvalds }; 28491da177e4SLinus Torvalds 28501e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, 28511e8f889bSDavid Gibson int writable) 285263551ae0SDavid Gibson { 285363551ae0SDavid Gibson pte_t entry; 285463551ae0SDavid Gibson 28551e8f889bSDavid Gibson if (writable) { 2856106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, 2857106c992aSGerald Schaefer vma->vm_page_prot))); 285863551ae0SDavid Gibson } else { 2859106c992aSGerald Schaefer entry = huge_pte_wrprotect(mk_huge_pte(page, 2860106c992aSGerald Schaefer vma->vm_page_prot)); 286163551ae0SDavid Gibson } 286263551ae0SDavid Gibson entry = pte_mkyoung(entry); 286363551ae0SDavid Gibson entry = pte_mkhuge(entry); 2864d9ed9faaSChris Metcalf entry = arch_make_huge_pte(entry, vma, page, writable); 286563551ae0SDavid Gibson 286663551ae0SDavid Gibson return entry; 286763551ae0SDavid Gibson } 286863551ae0SDavid Gibson 28691e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma, 28701e8f889bSDavid Gibson unsigned long address, pte_t *ptep) 28711e8f889bSDavid Gibson { 28721e8f889bSDavid Gibson pte_t entry; 28731e8f889bSDavid Gibson 2874106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); 287532f84528SChris Forbes if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) 28764b3073e1SRussell King update_mmu_cache(vma, address, ptep); 28771e8f889bSDavid Gibson } 28781e8f889bSDavid Gibson 28794a705fefSNaoya Horiguchi static int is_hugetlb_entry_migration(pte_t pte) 28804a705fefSNaoya Horiguchi { 28814a705fefSNaoya Horiguchi swp_entry_t swp; 28824a705fefSNaoya Horiguchi 28834a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 28844a705fefSNaoya Horiguchi return 0; 28854a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 28864a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_migration_entry(swp)) 28874a705fefSNaoya Horiguchi return 1; 28884a705fefSNaoya Horiguchi else 28894a705fefSNaoya Horiguchi return 0; 28904a705fefSNaoya Horiguchi } 28914a705fefSNaoya Horiguchi 28924a705fefSNaoya Horiguchi static int is_hugetlb_entry_hwpoisoned(pte_t pte) 28934a705fefSNaoya Horiguchi { 28944a705fefSNaoya Horiguchi swp_entry_t swp; 28954a705fefSNaoya Horiguchi 28964a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 28974a705fefSNaoya Horiguchi return 0; 28984a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 28994a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_hwpoison_entry(swp)) 29004a705fefSNaoya Horiguchi return 1; 29014a705fefSNaoya Horiguchi else 29024a705fefSNaoya Horiguchi return 0; 29034a705fefSNaoya Horiguchi } 29041e8f889bSDavid Gibson 290563551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 290663551ae0SDavid Gibson struct vm_area_struct *vma) 290763551ae0SDavid Gibson { 290863551ae0SDavid Gibson pte_t *src_pte, *dst_pte, entry; 290963551ae0SDavid Gibson struct page *ptepage; 29101c59827dSHugh Dickins unsigned long addr; 29111e8f889bSDavid Gibson int cow; 2912a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2913a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 2914e8569dd2SAndreas Sandberg unsigned long mmun_start; /* For mmu_notifiers */ 2915e8569dd2SAndreas Sandberg unsigned long mmun_end; /* For mmu_notifiers */ 2916e8569dd2SAndreas Sandberg int ret = 0; 29171e8f889bSDavid Gibson 29181e8f889bSDavid Gibson cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 291963551ae0SDavid Gibson 2920e8569dd2SAndreas Sandberg mmun_start = vma->vm_start; 2921e8569dd2SAndreas Sandberg mmun_end = vma->vm_end; 2922e8569dd2SAndreas Sandberg if (cow) 2923e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end); 2924e8569dd2SAndreas Sandberg 2925a5516438SAndi Kleen for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { 2926cb900f41SKirill A. Shutemov spinlock_t *src_ptl, *dst_ptl; 2927c74df32cSHugh Dickins src_pte = huge_pte_offset(src, addr); 2928c74df32cSHugh Dickins if (!src_pte) 2929c74df32cSHugh Dickins continue; 2930a5516438SAndi Kleen dst_pte = huge_pte_alloc(dst, addr, sz); 2931e8569dd2SAndreas Sandberg if (!dst_pte) { 2932e8569dd2SAndreas Sandberg ret = -ENOMEM; 2933e8569dd2SAndreas Sandberg break; 2934e8569dd2SAndreas Sandberg } 2935c5c99429SLarry Woodman 2936c5c99429SLarry Woodman /* If the pagetables are shared don't copy or take references */ 2937c5c99429SLarry Woodman if (dst_pte == src_pte) 2938c5c99429SLarry Woodman continue; 2939c5c99429SLarry Woodman 2940cb900f41SKirill A. Shutemov dst_ptl = huge_pte_lock(h, dst, dst_pte); 2941cb900f41SKirill A. Shutemov src_ptl = huge_pte_lockptr(h, src, src_pte); 2942cb900f41SKirill A. Shutemov spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 29434a705fefSNaoya Horiguchi entry = huge_ptep_get(src_pte); 29444a705fefSNaoya Horiguchi if (huge_pte_none(entry)) { /* skip none entry */ 29454a705fefSNaoya Horiguchi ; 29464a705fefSNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_migration(entry) || 29474a705fefSNaoya Horiguchi is_hugetlb_entry_hwpoisoned(entry))) { 29484a705fefSNaoya Horiguchi swp_entry_t swp_entry = pte_to_swp_entry(entry); 29494a705fefSNaoya Horiguchi 29504a705fefSNaoya Horiguchi if (is_write_migration_entry(swp_entry) && cow) { 29514a705fefSNaoya Horiguchi /* 29524a705fefSNaoya Horiguchi * COW mappings require pages in both 29534a705fefSNaoya Horiguchi * parent and child to be set to read. 29544a705fefSNaoya Horiguchi */ 29554a705fefSNaoya Horiguchi make_migration_entry_read(&swp_entry); 29564a705fefSNaoya Horiguchi entry = swp_entry_to_pte(swp_entry); 29574a705fefSNaoya Horiguchi set_huge_pte_at(src, addr, src_pte, entry); 29584a705fefSNaoya Horiguchi } 29594a705fefSNaoya Horiguchi set_huge_pte_at(dst, addr, dst_pte, entry); 29604a705fefSNaoya Horiguchi } else { 296134ee645eSJoerg Roedel if (cow) { 29627f2e9525SGerald Schaefer huge_ptep_set_wrprotect(src, addr, src_pte); 296334ee645eSJoerg Roedel mmu_notifier_invalidate_range(src, mmun_start, 296434ee645eSJoerg Roedel mmun_end); 296534ee645eSJoerg Roedel } 29660253d634SNaoya Horiguchi entry = huge_ptep_get(src_pte); 296763551ae0SDavid Gibson ptepage = pte_page(entry); 296863551ae0SDavid Gibson get_page(ptepage); 29690fe6e20bSNaoya Horiguchi page_dup_rmap(ptepage); 297063551ae0SDavid Gibson set_huge_pte_at(dst, addr, dst_pte, entry); 29711c59827dSHugh Dickins } 2972cb900f41SKirill A. Shutemov spin_unlock(src_ptl); 2973cb900f41SKirill A. Shutemov spin_unlock(dst_ptl); 297463551ae0SDavid Gibson } 297563551ae0SDavid Gibson 2976e8569dd2SAndreas Sandberg if (cow) 2977e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end); 2978e8569dd2SAndreas Sandberg 2979e8569dd2SAndreas Sandberg return ret; 298063551ae0SDavid Gibson } 298163551ae0SDavid Gibson 298224669e58SAneesh Kumar K.V void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, 298324669e58SAneesh Kumar K.V unsigned long start, unsigned long end, 298424669e58SAneesh Kumar K.V struct page *ref_page) 298563551ae0SDavid Gibson { 298624669e58SAneesh Kumar K.V int force_flush = 0; 298763551ae0SDavid Gibson struct mm_struct *mm = vma->vm_mm; 298863551ae0SDavid Gibson unsigned long address; 2989c7546f8fSDavid Gibson pte_t *ptep; 299063551ae0SDavid Gibson pte_t pte; 2991cb900f41SKirill A. Shutemov spinlock_t *ptl; 299263551ae0SDavid Gibson struct page *page; 2993a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2994a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 29952ec74c3eSSagi Grimberg const unsigned long mmun_start = start; /* For mmu_notifiers */ 29962ec74c3eSSagi Grimberg const unsigned long mmun_end = end; /* For mmu_notifiers */ 2997a5516438SAndi Kleen 299863551ae0SDavid Gibson WARN_ON(!is_vm_hugetlb_page(vma)); 2999a5516438SAndi Kleen BUG_ON(start & ~huge_page_mask(h)); 3000a5516438SAndi Kleen BUG_ON(end & ~huge_page_mask(h)); 300163551ae0SDavid Gibson 300224669e58SAneesh Kumar K.V tlb_start_vma(tlb, vma); 30032ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 3004569f48b8SHillf Danton address = start; 300524669e58SAneesh Kumar K.V again: 3006569f48b8SHillf Danton for (; address < end; address += sz) { 3007c7546f8fSDavid Gibson ptep = huge_pte_offset(mm, address); 3008c7546f8fSDavid Gibson if (!ptep) 3009c7546f8fSDavid Gibson continue; 3010c7546f8fSDavid Gibson 3011cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 301239dde65cSChen, Kenneth W if (huge_pmd_unshare(mm, &address, ptep)) 3013cb900f41SKirill A. Shutemov goto unlock; 301439dde65cSChen, Kenneth W 30156629326bSHillf Danton pte = huge_ptep_get(ptep); 30166629326bSHillf Danton if (huge_pte_none(pte)) 3017cb900f41SKirill A. Shutemov goto unlock; 30186629326bSHillf Danton 30196629326bSHillf Danton /* 30209fbc1f63SNaoya Horiguchi * Migrating hugepage or HWPoisoned hugepage is already 30219fbc1f63SNaoya Horiguchi * unmapped and its refcount is dropped, so just clear pte here. 30226629326bSHillf Danton */ 30239fbc1f63SNaoya Horiguchi if (unlikely(!pte_present(pte))) { 3024106c992aSGerald Schaefer huge_pte_clear(mm, address, ptep); 3025cb900f41SKirill A. Shutemov goto unlock; 30268c4894c6SNaoya Horiguchi } 30276629326bSHillf Danton 30286629326bSHillf Danton page = pte_page(pte); 302904f2cbe3SMel Gorman /* 303004f2cbe3SMel Gorman * If a reference page is supplied, it is because a specific 303104f2cbe3SMel Gorman * page is being unmapped, not a range. Ensure the page we 303204f2cbe3SMel Gorman * are about to unmap is the actual page of interest. 303304f2cbe3SMel Gorman */ 303404f2cbe3SMel Gorman if (ref_page) { 303504f2cbe3SMel Gorman if (page != ref_page) 3036cb900f41SKirill A. Shutemov goto unlock; 303704f2cbe3SMel Gorman 303804f2cbe3SMel Gorman /* 303904f2cbe3SMel Gorman * Mark the VMA as having unmapped its page so that 304004f2cbe3SMel Gorman * future faults in this VMA will fail rather than 304104f2cbe3SMel Gorman * looking like data was lost 304204f2cbe3SMel Gorman */ 304304f2cbe3SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); 304404f2cbe3SMel Gorman } 304504f2cbe3SMel Gorman 3046c7546f8fSDavid Gibson pte = huge_ptep_get_and_clear(mm, address, ptep); 304724669e58SAneesh Kumar K.V tlb_remove_tlb_entry(tlb, ptep, address); 3048106c992aSGerald Schaefer if (huge_pte_dirty(pte)) 30496649a386SKen Chen set_page_dirty(page); 30509e81130bSHillf Danton 305124669e58SAneesh Kumar K.V page_remove_rmap(page); 305224669e58SAneesh Kumar K.V force_flush = !__tlb_remove_page(tlb, page); 3053cb900f41SKirill A. Shutemov if (force_flush) { 3054569f48b8SHillf Danton address += sz; 3055cb900f41SKirill A. Shutemov spin_unlock(ptl); 30569e81130bSHillf Danton break; 305763551ae0SDavid Gibson } 3058cb900f41SKirill A. Shutemov /* Bail out after unmapping reference page if supplied */ 3059cb900f41SKirill A. Shutemov if (ref_page) { 3060cb900f41SKirill A. Shutemov spin_unlock(ptl); 3061cb900f41SKirill A. Shutemov break; 3062cb900f41SKirill A. Shutemov } 3063cb900f41SKirill A. Shutemov unlock: 3064cb900f41SKirill A. Shutemov spin_unlock(ptl); 3065cb900f41SKirill A. Shutemov } 306624669e58SAneesh Kumar K.V /* 306724669e58SAneesh Kumar K.V * mmu_gather ran out of room to batch pages, we break out of 306824669e58SAneesh Kumar K.V * the PTE lock to avoid doing the potential expensive TLB invalidate 306924669e58SAneesh Kumar K.V * and page-free while holding it. 307024669e58SAneesh Kumar K.V */ 307124669e58SAneesh Kumar K.V if (force_flush) { 307224669e58SAneesh Kumar K.V force_flush = 0; 307324669e58SAneesh Kumar K.V tlb_flush_mmu(tlb); 307424669e58SAneesh Kumar K.V if (address < end && !ref_page) 307524669e58SAneesh Kumar K.V goto again; 3076fe1668aeSChen, Kenneth W } 30772ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 307824669e58SAneesh Kumar K.V tlb_end_vma(tlb, vma); 30791da177e4SLinus Torvalds } 308063551ae0SDavid Gibson 3081d833352aSMel Gorman void __unmap_hugepage_range_final(struct mmu_gather *tlb, 3082d833352aSMel Gorman struct vm_area_struct *vma, unsigned long start, 3083d833352aSMel Gorman unsigned long end, struct page *ref_page) 3084d833352aSMel Gorman { 3085d833352aSMel Gorman __unmap_hugepage_range(tlb, vma, start, end, ref_page); 3086d833352aSMel Gorman 3087d833352aSMel Gorman /* 3088d833352aSMel Gorman * Clear this flag so that x86's huge_pmd_share page_table_shareable 3089d833352aSMel Gorman * test will fail on a vma being torn down, and not grab a page table 3090d833352aSMel Gorman * on its way out. We're lucky that the flag has such an appropriate 3091d833352aSMel Gorman * name, and can in fact be safely cleared here. We could clear it 3092d833352aSMel Gorman * before the __unmap_hugepage_range above, but all that's necessary 3093c8c06efaSDavidlohr Bueso * is to clear it before releasing the i_mmap_rwsem. This works 3094d833352aSMel Gorman * because in the context this is called, the VMA is about to be 3095c8c06efaSDavidlohr Bueso * destroyed and the i_mmap_rwsem is held. 3096d833352aSMel Gorman */ 3097d833352aSMel Gorman vma->vm_flags &= ~VM_MAYSHARE; 3098d833352aSMel Gorman } 3099d833352aSMel Gorman 3100502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 310104f2cbe3SMel Gorman unsigned long end, struct page *ref_page) 3102502717f4SChen, Kenneth W { 310324669e58SAneesh Kumar K.V struct mm_struct *mm; 310424669e58SAneesh Kumar K.V struct mmu_gather tlb; 310524669e58SAneesh Kumar K.V 310624669e58SAneesh Kumar K.V mm = vma->vm_mm; 310724669e58SAneesh Kumar K.V 31082b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, start, end); 310924669e58SAneesh Kumar K.V __unmap_hugepage_range(&tlb, vma, start, end, ref_page); 311024669e58SAneesh Kumar K.V tlb_finish_mmu(&tlb, start, end); 3111502717f4SChen, Kenneth W } 3112502717f4SChen, Kenneth W 311304f2cbe3SMel Gorman /* 311404f2cbe3SMel Gorman * This is called when the original mapper is failing to COW a MAP_PRIVATE 311504f2cbe3SMel Gorman * mappping it owns the reserve page for. The intention is to unmap the page 311604f2cbe3SMel Gorman * from other VMAs and let the children be SIGKILLed if they are faulting the 311704f2cbe3SMel Gorman * same region. 311804f2cbe3SMel Gorman */ 31192f4612afSDavidlohr Bueso static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, 31202a4b3dedSHarvey Harrison struct page *page, unsigned long address) 312104f2cbe3SMel Gorman { 31227526674dSAdam Litke struct hstate *h = hstate_vma(vma); 312304f2cbe3SMel Gorman struct vm_area_struct *iter_vma; 312404f2cbe3SMel Gorman struct address_space *mapping; 312504f2cbe3SMel Gorman pgoff_t pgoff; 312604f2cbe3SMel Gorman 312704f2cbe3SMel Gorman /* 312804f2cbe3SMel Gorman * vm_pgoff is in PAGE_SIZE units, hence the different calculation 312904f2cbe3SMel Gorman * from page cache lookup which is in HPAGE_SIZE units. 313004f2cbe3SMel Gorman */ 31317526674dSAdam Litke address = address & huge_page_mask(h); 313236e4f20aSMichal Hocko pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + 313336e4f20aSMichal Hocko vma->vm_pgoff; 3134496ad9aaSAl Viro mapping = file_inode(vma->vm_file)->i_mapping; 313504f2cbe3SMel Gorman 31364eb2b1dcSMel Gorman /* 31374eb2b1dcSMel Gorman * Take the mapping lock for the duration of the table walk. As 31384eb2b1dcSMel Gorman * this mapping should be shared between all the VMAs, 31394eb2b1dcSMel Gorman * __unmap_hugepage_range() is called as the lock is already held 31404eb2b1dcSMel Gorman */ 314183cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 31426b2dbba8SMichel Lespinasse vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { 314304f2cbe3SMel Gorman /* Do not unmap the current VMA */ 314404f2cbe3SMel Gorman if (iter_vma == vma) 314504f2cbe3SMel Gorman continue; 314604f2cbe3SMel Gorman 314704f2cbe3SMel Gorman /* 314804f2cbe3SMel Gorman * Unmap the page from other VMAs without their own reserves. 314904f2cbe3SMel Gorman * They get marked to be SIGKILLed if they fault in these 315004f2cbe3SMel Gorman * areas. This is because a future no-page fault on this VMA 315104f2cbe3SMel Gorman * could insert a zeroed page instead of the data existing 315204f2cbe3SMel Gorman * from the time of fork. This would look like data corruption 315304f2cbe3SMel Gorman */ 315404f2cbe3SMel Gorman if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) 315524669e58SAneesh Kumar K.V unmap_hugepage_range(iter_vma, address, 315624669e58SAneesh Kumar K.V address + huge_page_size(h), page); 315704f2cbe3SMel Gorman } 315883cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 315904f2cbe3SMel Gorman } 316004f2cbe3SMel Gorman 31610fe6e20bSNaoya Horiguchi /* 31620fe6e20bSNaoya Horiguchi * Hugetlb_cow() should be called with page lock of the original hugepage held. 3163ef009b25SMichal Hocko * Called with hugetlb_instantiation_mutex held and pte_page locked so we 3164ef009b25SMichal Hocko * cannot race with other handlers or page migration. 3165ef009b25SMichal Hocko * Keep the pte_same checks anyway to make transition from the mutex easier. 31660fe6e20bSNaoya Horiguchi */ 31671e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, 316804f2cbe3SMel Gorman unsigned long address, pte_t *ptep, pte_t pte, 3169cb900f41SKirill A. Shutemov struct page *pagecache_page, spinlock_t *ptl) 31701e8f889bSDavid Gibson { 3171a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 31721e8f889bSDavid Gibson struct page *old_page, *new_page; 3173ad4404a2SDavidlohr Bueso int ret = 0, outside_reserve = 0; 31742ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 31752ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 31761e8f889bSDavid Gibson 31771e8f889bSDavid Gibson old_page = pte_page(pte); 31781e8f889bSDavid Gibson 317904f2cbe3SMel Gorman retry_avoidcopy: 31801e8f889bSDavid Gibson /* If no-one else is actually using this page, avoid the copy 31811e8f889bSDavid Gibson * and just make the page writable */ 318237a2140dSJoonsoo Kim if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { 31830fe6e20bSNaoya Horiguchi page_move_anon_rmap(old_page, vma, address); 31841e8f889bSDavid Gibson set_huge_ptep_writable(vma, address, ptep); 318583c54070SNick Piggin return 0; 31861e8f889bSDavid Gibson } 31871e8f889bSDavid Gibson 318804f2cbe3SMel Gorman /* 318904f2cbe3SMel Gorman * If the process that created a MAP_PRIVATE mapping is about to 319004f2cbe3SMel Gorman * perform a COW due to a shared page count, attempt to satisfy 319104f2cbe3SMel Gorman * the allocation without using the existing reserves. The pagecache 319204f2cbe3SMel Gorman * page is used to determine if the reserve at this address was 319304f2cbe3SMel Gorman * consumed or not. If reserves were used, a partial faulted mapping 319404f2cbe3SMel Gorman * at the time of fork() could consume its reserves on COW instead 319504f2cbe3SMel Gorman * of the full address range. 319604f2cbe3SMel Gorman */ 31975944d011SJoonsoo Kim if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && 319804f2cbe3SMel Gorman old_page != pagecache_page) 319904f2cbe3SMel Gorman outside_reserve = 1; 320004f2cbe3SMel Gorman 32011e8f889bSDavid Gibson page_cache_get(old_page); 3202b76c8cfbSLarry Woodman 3203ad4404a2SDavidlohr Bueso /* 3204ad4404a2SDavidlohr Bueso * Drop page table lock as buddy allocator may be called. It will 3205ad4404a2SDavidlohr Bueso * be acquired again before returning to the caller, as expected. 3206ad4404a2SDavidlohr Bueso */ 3207cb900f41SKirill A. Shutemov spin_unlock(ptl); 320804f2cbe3SMel Gorman new_page = alloc_huge_page(vma, address, outside_reserve); 32091e8f889bSDavid Gibson 32102fc39cecSAdam Litke if (IS_ERR(new_page)) { 321104f2cbe3SMel Gorman /* 321204f2cbe3SMel Gorman * If a process owning a MAP_PRIVATE mapping fails to COW, 321304f2cbe3SMel Gorman * it is due to references held by a child and an insufficient 321404f2cbe3SMel Gorman * huge page pool. To guarantee the original mappers 321504f2cbe3SMel Gorman * reliability, unmap the page from child processes. The child 321604f2cbe3SMel Gorman * may get SIGKILLed if it later faults. 321704f2cbe3SMel Gorman */ 321804f2cbe3SMel Gorman if (outside_reserve) { 3219ad4404a2SDavidlohr Bueso page_cache_release(old_page); 322004f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 32212f4612afSDavidlohr Bueso unmap_ref_private(mm, vma, old_page, address); 322204f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 3223cb900f41SKirill A. Shutemov spin_lock(ptl); 3224a734bcc8SHillf Danton ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 3225a9af0c5dSNaoya Horiguchi if (likely(ptep && 3226a9af0c5dSNaoya Horiguchi pte_same(huge_ptep_get(ptep), pte))) 322704f2cbe3SMel Gorman goto retry_avoidcopy; 3228a734bcc8SHillf Danton /* 3229cb900f41SKirill A. Shutemov * race occurs while re-acquiring page table 3230cb900f41SKirill A. Shutemov * lock, and our job is done. 3231a734bcc8SHillf Danton */ 3232a734bcc8SHillf Danton return 0; 323304f2cbe3SMel Gorman } 323404f2cbe3SMel Gorman 3235ad4404a2SDavidlohr Bueso ret = (PTR_ERR(new_page) == -ENOMEM) ? 3236ad4404a2SDavidlohr Bueso VM_FAULT_OOM : VM_FAULT_SIGBUS; 3237ad4404a2SDavidlohr Bueso goto out_release_old; 32381e8f889bSDavid Gibson } 32391e8f889bSDavid Gibson 32400fe6e20bSNaoya Horiguchi /* 32410fe6e20bSNaoya Horiguchi * When the original hugepage is shared one, it does not have 32420fe6e20bSNaoya Horiguchi * anon_vma prepared. 32430fe6e20bSNaoya Horiguchi */ 324444e2aa93SDean Nelson if (unlikely(anon_vma_prepare(vma))) { 3245ad4404a2SDavidlohr Bueso ret = VM_FAULT_OOM; 3246ad4404a2SDavidlohr Bueso goto out_release_all; 324744e2aa93SDean Nelson } 32480fe6e20bSNaoya Horiguchi 324947ad8475SAndrea Arcangeli copy_user_huge_page(new_page, old_page, address, vma, 325047ad8475SAndrea Arcangeli pages_per_huge_page(h)); 32510ed361deSNick Piggin __SetPageUptodate(new_page); 3252bcc54222SNaoya Horiguchi set_page_huge_active(new_page); 32531e8f889bSDavid Gibson 32542ec74c3eSSagi Grimberg mmun_start = address & huge_page_mask(h); 32552ec74c3eSSagi Grimberg mmun_end = mmun_start + huge_page_size(h); 32562ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 3257ad4404a2SDavidlohr Bueso 3258b76c8cfbSLarry Woodman /* 3259cb900f41SKirill A. Shutemov * Retake the page table lock to check for racing updates 3260b76c8cfbSLarry Woodman * before the page tables are altered 3261b76c8cfbSLarry Woodman */ 3262cb900f41SKirill A. Shutemov spin_lock(ptl); 3263a5516438SAndi Kleen ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 3264a9af0c5dSNaoya Horiguchi if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { 326507443a85SJoonsoo Kim ClearPagePrivate(new_page); 326607443a85SJoonsoo Kim 32671e8f889bSDavid Gibson /* Break COW */ 32688fe627ecSGerald Schaefer huge_ptep_clear_flush(vma, address, ptep); 326934ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, mmun_start, mmun_end); 32701e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, 32711e8f889bSDavid Gibson make_huge_pte(vma, new_page, 1)); 32720fe6e20bSNaoya Horiguchi page_remove_rmap(old_page); 3273cd67f0d2SNaoya Horiguchi hugepage_add_new_anon_rmap(new_page, vma, address); 32741e8f889bSDavid Gibson /* Make the old page be freed below */ 32751e8f889bSDavid Gibson new_page = old_page; 32761e8f889bSDavid Gibson } 3277cb900f41SKirill A. Shutemov spin_unlock(ptl); 32782ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 3279ad4404a2SDavidlohr Bueso out_release_all: 32801e8f889bSDavid Gibson page_cache_release(new_page); 3281ad4404a2SDavidlohr Bueso out_release_old: 32821e8f889bSDavid Gibson page_cache_release(old_page); 32838312034fSJoonsoo Kim 3284ad4404a2SDavidlohr Bueso spin_lock(ptl); /* Caller expects lock to be held */ 3285ad4404a2SDavidlohr Bueso return ret; 32861e8f889bSDavid Gibson } 32871e8f889bSDavid Gibson 328804f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */ 3289a5516438SAndi Kleen static struct page *hugetlbfs_pagecache_page(struct hstate *h, 3290a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 329104f2cbe3SMel Gorman { 329204f2cbe3SMel Gorman struct address_space *mapping; 3293e7c4b0bfSAndy Whitcroft pgoff_t idx; 329404f2cbe3SMel Gorman 329504f2cbe3SMel Gorman mapping = vma->vm_file->f_mapping; 3296a5516438SAndi Kleen idx = vma_hugecache_offset(h, vma, address); 329704f2cbe3SMel Gorman 329804f2cbe3SMel Gorman return find_lock_page(mapping, idx); 329904f2cbe3SMel Gorman } 330004f2cbe3SMel Gorman 33013ae77f43SHugh Dickins /* 33023ae77f43SHugh Dickins * Return whether there is a pagecache page to back given address within VMA. 33033ae77f43SHugh Dickins * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page. 33043ae77f43SHugh Dickins */ 33053ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h, 33062a15efc9SHugh Dickins struct vm_area_struct *vma, unsigned long address) 33072a15efc9SHugh Dickins { 33082a15efc9SHugh Dickins struct address_space *mapping; 33092a15efc9SHugh Dickins pgoff_t idx; 33102a15efc9SHugh Dickins struct page *page; 33112a15efc9SHugh Dickins 33122a15efc9SHugh Dickins mapping = vma->vm_file->f_mapping; 33132a15efc9SHugh Dickins idx = vma_hugecache_offset(h, vma, address); 33142a15efc9SHugh Dickins 33152a15efc9SHugh Dickins page = find_get_page(mapping, idx); 33162a15efc9SHugh Dickins if (page) 33172a15efc9SHugh Dickins put_page(page); 33182a15efc9SHugh Dickins return page != NULL; 33192a15efc9SHugh Dickins } 33202a15efc9SHugh Dickins 3321a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, 33228382d914SDavidlohr Bueso struct address_space *mapping, pgoff_t idx, 3323788c7df4SHugh Dickins unsigned long address, pte_t *ptep, unsigned int flags) 3324ac9b9c66SHugh Dickins { 3325a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3326ac9b9c66SHugh Dickins int ret = VM_FAULT_SIGBUS; 3327409eb8c2SHillf Danton int anon_rmap = 0; 33284c887265SAdam Litke unsigned long size; 33294c887265SAdam Litke struct page *page; 33301e8f889bSDavid Gibson pte_t new_pte; 3331cb900f41SKirill A. Shutemov spinlock_t *ptl; 33324c887265SAdam Litke 333304f2cbe3SMel Gorman /* 333404f2cbe3SMel Gorman * Currently, we are forced to kill the process in the event the 333504f2cbe3SMel Gorman * original mapper has unmapped pages from the child due to a failed 333625985edcSLucas De Marchi * COW. Warn that such a situation has occurred as it may not be obvious 333704f2cbe3SMel Gorman */ 333804f2cbe3SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { 3339ffb22af5SAndrew Morton pr_warning("PID %d killed due to inadequate hugepage pool\n", 334004f2cbe3SMel Gorman current->pid); 334104f2cbe3SMel Gorman return ret; 334204f2cbe3SMel Gorman } 334304f2cbe3SMel Gorman 33444c887265SAdam Litke /* 33454c887265SAdam Litke * Use page lock to guard against racing truncation 33464c887265SAdam Litke * before we get page_table_lock. 33474c887265SAdam Litke */ 33486bda666aSChristoph Lameter retry: 33496bda666aSChristoph Lameter page = find_lock_page(mapping, idx); 33506bda666aSChristoph Lameter if (!page) { 3351a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 3352ebed4bfcSHugh Dickins if (idx >= size) 3353ebed4bfcSHugh Dickins goto out; 335404f2cbe3SMel Gorman page = alloc_huge_page(vma, address, 0); 33552fc39cecSAdam Litke if (IS_ERR(page)) { 335676dcee75SAneesh Kumar K.V ret = PTR_ERR(page); 335776dcee75SAneesh Kumar K.V if (ret == -ENOMEM) 335876dcee75SAneesh Kumar K.V ret = VM_FAULT_OOM; 335976dcee75SAneesh Kumar K.V else 336076dcee75SAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 33616bda666aSChristoph Lameter goto out; 33626bda666aSChristoph Lameter } 336347ad8475SAndrea Arcangeli clear_huge_page(page, address, pages_per_huge_page(h)); 33640ed361deSNick Piggin __SetPageUptodate(page); 3365bcc54222SNaoya Horiguchi set_page_huge_active(page); 3366ac9b9c66SHugh Dickins 3367f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) { 33686bda666aSChristoph Lameter int err; 336945c682a6SKen Chen struct inode *inode = mapping->host; 33706bda666aSChristoph Lameter 33716bda666aSChristoph Lameter err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); 33726bda666aSChristoph Lameter if (err) { 33736bda666aSChristoph Lameter put_page(page); 33746bda666aSChristoph Lameter if (err == -EEXIST) 33756bda666aSChristoph Lameter goto retry; 33766bda666aSChristoph Lameter goto out; 33776bda666aSChristoph Lameter } 337807443a85SJoonsoo Kim ClearPagePrivate(page); 337945c682a6SKen Chen 338045c682a6SKen Chen spin_lock(&inode->i_lock); 3381a5516438SAndi Kleen inode->i_blocks += blocks_per_huge_page(h); 338245c682a6SKen Chen spin_unlock(&inode->i_lock); 338323be7468SMel Gorman } else { 33846bda666aSChristoph Lameter lock_page(page); 33850fe6e20bSNaoya Horiguchi if (unlikely(anon_vma_prepare(vma))) { 33860fe6e20bSNaoya Horiguchi ret = VM_FAULT_OOM; 33870fe6e20bSNaoya Horiguchi goto backout_unlocked; 338823be7468SMel Gorman } 3389409eb8c2SHillf Danton anon_rmap = 1; 33900fe6e20bSNaoya Horiguchi } 33910fe6e20bSNaoya Horiguchi } else { 339257303d80SAndy Whitcroft /* 3393998b4382SNaoya Horiguchi * If memory error occurs between mmap() and fault, some process 3394998b4382SNaoya Horiguchi * don't have hwpoisoned swap entry for errored virtual address. 3395998b4382SNaoya Horiguchi * So we need to block hugepage fault by PG_hwpoison bit check. 3396fd6a03edSNaoya Horiguchi */ 3397fd6a03edSNaoya Horiguchi if (unlikely(PageHWPoison(page))) { 3398aa50d3a7SAndi Kleen ret = VM_FAULT_HWPOISON | 3399972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 3400fd6a03edSNaoya Horiguchi goto backout_unlocked; 34016bda666aSChristoph Lameter } 3402998b4382SNaoya Horiguchi } 34031e8f889bSDavid Gibson 340457303d80SAndy Whitcroft /* 340557303d80SAndy Whitcroft * If we are going to COW a private mapping later, we examine the 340657303d80SAndy Whitcroft * pending reservations for this page now. This will ensure that 340757303d80SAndy Whitcroft * any allocations necessary to record that reservation occur outside 340857303d80SAndy Whitcroft * the spinlock. 340957303d80SAndy Whitcroft */ 34105e911373SMike Kravetz if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 34112b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 34122b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 34132b26736cSAndy Whitcroft goto backout_unlocked; 34142b26736cSAndy Whitcroft } 34155e911373SMike Kravetz /* Just decrements count, does not deallocate */ 3416feba16e2SMike Kravetz vma_end_reservation(h, vma, address); 34175e911373SMike Kravetz } 341857303d80SAndy Whitcroft 3419cb900f41SKirill A. Shutemov ptl = huge_pte_lockptr(h, mm, ptep); 3420cb900f41SKirill A. Shutemov spin_lock(ptl); 3421a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 34224c887265SAdam Litke if (idx >= size) 34234c887265SAdam Litke goto backout; 34244c887265SAdam Litke 342583c54070SNick Piggin ret = 0; 34267f2e9525SGerald Schaefer if (!huge_pte_none(huge_ptep_get(ptep))) 34274c887265SAdam Litke goto backout; 34284c887265SAdam Litke 342907443a85SJoonsoo Kim if (anon_rmap) { 343007443a85SJoonsoo Kim ClearPagePrivate(page); 3431409eb8c2SHillf Danton hugepage_add_new_anon_rmap(page, vma, address); 3432ac714904SChoi Gi-yong } else 3433409eb8c2SHillf Danton page_dup_rmap(page); 34341e8f889bSDavid Gibson new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) 34351e8f889bSDavid Gibson && (vma->vm_flags & VM_SHARED))); 34361e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, new_pte); 34371e8f889bSDavid Gibson 3438788c7df4SHugh Dickins if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 34391e8f889bSDavid Gibson /* Optimization, do the COW without a second fault */ 3440cb900f41SKirill A. Shutemov ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page, ptl); 34411e8f889bSDavid Gibson } 34421e8f889bSDavid Gibson 3443cb900f41SKirill A. Shutemov spin_unlock(ptl); 34444c887265SAdam Litke unlock_page(page); 34454c887265SAdam Litke out: 3446ac9b9c66SHugh Dickins return ret; 34474c887265SAdam Litke 34484c887265SAdam Litke backout: 3449cb900f41SKirill A. Shutemov spin_unlock(ptl); 34502b26736cSAndy Whitcroft backout_unlocked: 34514c887265SAdam Litke unlock_page(page); 34524c887265SAdam Litke put_page(page); 34534c887265SAdam Litke goto out; 3454ac9b9c66SHugh Dickins } 3455ac9b9c66SHugh Dickins 34568382d914SDavidlohr Bueso #ifdef CONFIG_SMP 3457c672c7f2SMike Kravetz u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 34588382d914SDavidlohr Bueso struct vm_area_struct *vma, 34598382d914SDavidlohr Bueso struct address_space *mapping, 34608382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 34618382d914SDavidlohr Bueso { 34628382d914SDavidlohr Bueso unsigned long key[2]; 34638382d914SDavidlohr Bueso u32 hash; 34648382d914SDavidlohr Bueso 34658382d914SDavidlohr Bueso if (vma->vm_flags & VM_SHARED) { 34668382d914SDavidlohr Bueso key[0] = (unsigned long) mapping; 34678382d914SDavidlohr Bueso key[1] = idx; 34688382d914SDavidlohr Bueso } else { 34698382d914SDavidlohr Bueso key[0] = (unsigned long) mm; 34708382d914SDavidlohr Bueso key[1] = address >> huge_page_shift(h); 34718382d914SDavidlohr Bueso } 34728382d914SDavidlohr Bueso 34738382d914SDavidlohr Bueso hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0); 34748382d914SDavidlohr Bueso 34758382d914SDavidlohr Bueso return hash & (num_fault_mutexes - 1); 34768382d914SDavidlohr Bueso } 34778382d914SDavidlohr Bueso #else 34788382d914SDavidlohr Bueso /* 34798382d914SDavidlohr Bueso * For uniprocesor systems we always use a single mutex, so just 34808382d914SDavidlohr Bueso * return 0 and avoid the hashing overhead. 34818382d914SDavidlohr Bueso */ 3482c672c7f2SMike Kravetz u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 34838382d914SDavidlohr Bueso struct vm_area_struct *vma, 34848382d914SDavidlohr Bueso struct address_space *mapping, 34858382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 34868382d914SDavidlohr Bueso { 34878382d914SDavidlohr Bueso return 0; 34888382d914SDavidlohr Bueso } 34898382d914SDavidlohr Bueso #endif 34908382d914SDavidlohr Bueso 349186e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3492788c7df4SHugh Dickins unsigned long address, unsigned int flags) 349386e5216fSAdam Litke { 34948382d914SDavidlohr Bueso pte_t *ptep, entry; 3495cb900f41SKirill A. Shutemov spinlock_t *ptl; 34961e8f889bSDavid Gibson int ret; 34978382d914SDavidlohr Bueso u32 hash; 34988382d914SDavidlohr Bueso pgoff_t idx; 34990fe6e20bSNaoya Horiguchi struct page *page = NULL; 350057303d80SAndy Whitcroft struct page *pagecache_page = NULL; 3501a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 35028382d914SDavidlohr Bueso struct address_space *mapping; 35030f792cf9SNaoya Horiguchi int need_wait_lock = 0; 350486e5216fSAdam Litke 35051e16a539SKAMEZAWA Hiroyuki address &= huge_page_mask(h); 35061e16a539SKAMEZAWA Hiroyuki 3507fd6a03edSNaoya Horiguchi ptep = huge_pte_offset(mm, address); 3508fd6a03edSNaoya Horiguchi if (ptep) { 3509fd6a03edSNaoya Horiguchi entry = huge_ptep_get(ptep); 3510290408d4SNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(entry))) { 3511cb900f41SKirill A. Shutemov migration_entry_wait_huge(vma, mm, ptep); 3512290408d4SNaoya Horiguchi return 0; 3513290408d4SNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) 3514aa50d3a7SAndi Kleen return VM_FAULT_HWPOISON_LARGE | 3515972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 3516fd6a03edSNaoya Horiguchi } 3517fd6a03edSNaoya Horiguchi 3518a5516438SAndi Kleen ptep = huge_pte_alloc(mm, address, huge_page_size(h)); 351986e5216fSAdam Litke if (!ptep) 352086e5216fSAdam Litke return VM_FAULT_OOM; 352186e5216fSAdam Litke 35228382d914SDavidlohr Bueso mapping = vma->vm_file->f_mapping; 35238382d914SDavidlohr Bueso idx = vma_hugecache_offset(h, vma, address); 35248382d914SDavidlohr Bueso 35253935baa9SDavid Gibson /* 35263935baa9SDavid Gibson * Serialize hugepage allocation and instantiation, so that we don't 35273935baa9SDavid Gibson * get spurious allocation failures if two CPUs race to instantiate 35283935baa9SDavid Gibson * the same page in the page cache. 35293935baa9SDavid Gibson */ 3530c672c7f2SMike Kravetz hash = hugetlb_fault_mutex_hash(h, mm, vma, mapping, idx, address); 3531c672c7f2SMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 35328382d914SDavidlohr Bueso 35337f2e9525SGerald Schaefer entry = huge_ptep_get(ptep); 35347f2e9525SGerald Schaefer if (huge_pte_none(entry)) { 35358382d914SDavidlohr Bueso ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags); 3536b4d1d99fSDavid Gibson goto out_mutex; 35373935baa9SDavid Gibson } 353886e5216fSAdam Litke 353983c54070SNick Piggin ret = 0; 35401e8f889bSDavid Gibson 354157303d80SAndy Whitcroft /* 35420f792cf9SNaoya Horiguchi * entry could be a migration/hwpoison entry at this point, so this 35430f792cf9SNaoya Horiguchi * check prevents the kernel from going below assuming that we have 35440f792cf9SNaoya Horiguchi * a active hugepage in pagecache. This goto expects the 2nd page fault, 35450f792cf9SNaoya Horiguchi * and is_hugetlb_entry_(migration|hwpoisoned) check will properly 35460f792cf9SNaoya Horiguchi * handle it. 35470f792cf9SNaoya Horiguchi */ 35480f792cf9SNaoya Horiguchi if (!pte_present(entry)) 35490f792cf9SNaoya Horiguchi goto out_mutex; 35500f792cf9SNaoya Horiguchi 35510f792cf9SNaoya Horiguchi /* 355257303d80SAndy Whitcroft * If we are going to COW the mapping later, we examine the pending 355357303d80SAndy Whitcroft * reservations for this page now. This will ensure that any 355457303d80SAndy Whitcroft * allocations necessary to record that reservation occur outside the 355557303d80SAndy Whitcroft * spinlock. For private mappings, we also lookup the pagecache 355657303d80SAndy Whitcroft * page now as it is used to determine if a reservation has been 355757303d80SAndy Whitcroft * consumed. 355857303d80SAndy Whitcroft */ 3559106c992aSGerald Schaefer if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { 35602b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 35612b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 3562b4d1d99fSDavid Gibson goto out_mutex; 35632b26736cSAndy Whitcroft } 35645e911373SMike Kravetz /* Just decrements count, does not deallocate */ 3565feba16e2SMike Kravetz vma_end_reservation(h, vma, address); 356657303d80SAndy Whitcroft 3567f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 356857303d80SAndy Whitcroft pagecache_page = hugetlbfs_pagecache_page(h, 356957303d80SAndy Whitcroft vma, address); 357057303d80SAndy Whitcroft } 357157303d80SAndy Whitcroft 35720f792cf9SNaoya Horiguchi ptl = huge_pte_lock(h, mm, ptep); 35730fe6e20bSNaoya Horiguchi 35741e8f889bSDavid Gibson /* Check for a racing update before calling hugetlb_cow */ 3575b4d1d99fSDavid Gibson if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) 3576cb900f41SKirill A. Shutemov goto out_ptl; 3577b4d1d99fSDavid Gibson 35780f792cf9SNaoya Horiguchi /* 35790f792cf9SNaoya Horiguchi * hugetlb_cow() requires page locks of pte_page(entry) and 35800f792cf9SNaoya Horiguchi * pagecache_page, so here we need take the former one 35810f792cf9SNaoya Horiguchi * when page != pagecache_page or !pagecache_page. 35820f792cf9SNaoya Horiguchi */ 35830f792cf9SNaoya Horiguchi page = pte_page(entry); 35840f792cf9SNaoya Horiguchi if (page != pagecache_page) 35850f792cf9SNaoya Horiguchi if (!trylock_page(page)) { 35860f792cf9SNaoya Horiguchi need_wait_lock = 1; 35870f792cf9SNaoya Horiguchi goto out_ptl; 35880f792cf9SNaoya Horiguchi } 35890f792cf9SNaoya Horiguchi 35900f792cf9SNaoya Horiguchi get_page(page); 3591b4d1d99fSDavid Gibson 3592788c7df4SHugh Dickins if (flags & FAULT_FLAG_WRITE) { 3593106c992aSGerald Schaefer if (!huge_pte_write(entry)) { 359457303d80SAndy Whitcroft ret = hugetlb_cow(mm, vma, address, ptep, entry, 3595cb900f41SKirill A. Shutemov pagecache_page, ptl); 35960f792cf9SNaoya Horiguchi goto out_put_page; 3597b4d1d99fSDavid Gibson } 3598106c992aSGerald Schaefer entry = huge_pte_mkdirty(entry); 3599b4d1d99fSDavid Gibson } 3600b4d1d99fSDavid Gibson entry = pte_mkyoung(entry); 3601788c7df4SHugh Dickins if (huge_ptep_set_access_flags(vma, address, ptep, entry, 3602788c7df4SHugh Dickins flags & FAULT_FLAG_WRITE)) 36034b3073e1SRussell King update_mmu_cache(vma, address, ptep); 36040f792cf9SNaoya Horiguchi out_put_page: 36050f792cf9SNaoya Horiguchi if (page != pagecache_page) 36060f792cf9SNaoya Horiguchi unlock_page(page); 36070f792cf9SNaoya Horiguchi put_page(page); 3608cb900f41SKirill A. Shutemov out_ptl: 3609cb900f41SKirill A. Shutemov spin_unlock(ptl); 361057303d80SAndy Whitcroft 361157303d80SAndy Whitcroft if (pagecache_page) { 361257303d80SAndy Whitcroft unlock_page(pagecache_page); 361357303d80SAndy Whitcroft put_page(pagecache_page); 361457303d80SAndy Whitcroft } 3615b4d1d99fSDavid Gibson out_mutex: 3616c672c7f2SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 36170f792cf9SNaoya Horiguchi /* 36180f792cf9SNaoya Horiguchi * Generally it's safe to hold refcount during waiting page lock. But 36190f792cf9SNaoya Horiguchi * here we just wait to defer the next page fault to avoid busy loop and 36200f792cf9SNaoya Horiguchi * the page is not used after unlocked before returning from the current 36210f792cf9SNaoya Horiguchi * page fault. So we are safe from accessing freed page, even if we wait 36220f792cf9SNaoya Horiguchi * here without taking refcount. 36230f792cf9SNaoya Horiguchi */ 36240f792cf9SNaoya Horiguchi if (need_wait_lock) 36250f792cf9SNaoya Horiguchi wait_on_page_locked(page); 36261e8f889bSDavid Gibson return ret; 362786e5216fSAdam Litke } 362886e5216fSAdam Litke 362928a35716SMichel Lespinasse long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, 363063551ae0SDavid Gibson struct page **pages, struct vm_area_struct **vmas, 363128a35716SMichel Lespinasse unsigned long *position, unsigned long *nr_pages, 363228a35716SMichel Lespinasse long i, unsigned int flags) 363363551ae0SDavid Gibson { 3634d5d4b0aaSChen, Kenneth W unsigned long pfn_offset; 3635d5d4b0aaSChen, Kenneth W unsigned long vaddr = *position; 363628a35716SMichel Lespinasse unsigned long remainder = *nr_pages; 3637a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 363863551ae0SDavid Gibson 363963551ae0SDavid Gibson while (vaddr < vma->vm_end && remainder) { 364063551ae0SDavid Gibson pte_t *pte; 3641cb900f41SKirill A. Shutemov spinlock_t *ptl = NULL; 36422a15efc9SHugh Dickins int absent; 364363551ae0SDavid Gibson struct page *page; 364463551ae0SDavid Gibson 36454c887265SAdam Litke /* 364602057967SDavid Rientjes * If we have a pending SIGKILL, don't keep faulting pages and 364702057967SDavid Rientjes * potentially allocating memory. 364802057967SDavid Rientjes */ 364902057967SDavid Rientjes if (unlikely(fatal_signal_pending(current))) { 365002057967SDavid Rientjes remainder = 0; 365102057967SDavid Rientjes break; 365202057967SDavid Rientjes } 365302057967SDavid Rientjes 365402057967SDavid Rientjes /* 36554c887265SAdam Litke * Some archs (sparc64, sh*) have multiple pte_ts to 36562a15efc9SHugh Dickins * each hugepage. We have to make sure we get the 36574c887265SAdam Litke * first, for the page indexing below to work. 3658cb900f41SKirill A. Shutemov * 3659cb900f41SKirill A. Shutemov * Note that page table lock is not held when pte is null. 36604c887265SAdam Litke */ 3661a5516438SAndi Kleen pte = huge_pte_offset(mm, vaddr & huge_page_mask(h)); 3662cb900f41SKirill A. Shutemov if (pte) 3663cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, pte); 36642a15efc9SHugh Dickins absent = !pte || huge_pte_none(huge_ptep_get(pte)); 366563551ae0SDavid Gibson 36662a15efc9SHugh Dickins /* 36672a15efc9SHugh Dickins * When coredumping, it suits get_dump_page if we just return 36683ae77f43SHugh Dickins * an error where there's an empty slot with no huge pagecache 36693ae77f43SHugh Dickins * to back it. This way, we avoid allocating a hugepage, and 36703ae77f43SHugh Dickins * the sparse dumpfile avoids allocating disk blocks, but its 36713ae77f43SHugh Dickins * huge holes still show up with zeroes where they need to be. 36722a15efc9SHugh Dickins */ 36733ae77f43SHugh Dickins if (absent && (flags & FOLL_DUMP) && 36743ae77f43SHugh Dickins !hugetlbfs_pagecache_present(h, vma, vaddr)) { 3675cb900f41SKirill A. Shutemov if (pte) 3676cb900f41SKirill A. Shutemov spin_unlock(ptl); 36772a15efc9SHugh Dickins remainder = 0; 36782a15efc9SHugh Dickins break; 36792a15efc9SHugh Dickins } 36802a15efc9SHugh Dickins 36819cc3a5bdSNaoya Horiguchi /* 36829cc3a5bdSNaoya Horiguchi * We need call hugetlb_fault for both hugepages under migration 36839cc3a5bdSNaoya Horiguchi * (in which case hugetlb_fault waits for the migration,) and 36849cc3a5bdSNaoya Horiguchi * hwpoisoned hugepages (in which case we need to prevent the 36859cc3a5bdSNaoya Horiguchi * caller from accessing to them.) In order to do this, we use 36869cc3a5bdSNaoya Horiguchi * here is_swap_pte instead of is_hugetlb_entry_migration and 36879cc3a5bdSNaoya Horiguchi * is_hugetlb_entry_hwpoisoned. This is because it simply covers 36889cc3a5bdSNaoya Horiguchi * both cases, and because we can't follow correct pages 36899cc3a5bdSNaoya Horiguchi * directly from any kind of swap entries. 36909cc3a5bdSNaoya Horiguchi */ 36919cc3a5bdSNaoya Horiguchi if (absent || is_swap_pte(huge_ptep_get(pte)) || 3692106c992aSGerald Schaefer ((flags & FOLL_WRITE) && 3693106c992aSGerald Schaefer !huge_pte_write(huge_ptep_get(pte)))) { 36944c887265SAdam Litke int ret; 36954c887265SAdam Litke 3696cb900f41SKirill A. Shutemov if (pte) 3697cb900f41SKirill A. Shutemov spin_unlock(ptl); 36982a15efc9SHugh Dickins ret = hugetlb_fault(mm, vma, vaddr, 36992a15efc9SHugh Dickins (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0); 3700a89182c7SAdam Litke if (!(ret & VM_FAULT_ERROR)) 37014c887265SAdam Litke continue; 37024c887265SAdam Litke 37031c59827dSHugh Dickins remainder = 0; 37041c59827dSHugh Dickins break; 37051c59827dSHugh Dickins } 370663551ae0SDavid Gibson 3707a5516438SAndi Kleen pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; 37087f2e9525SGerald Schaefer page = pte_page(huge_ptep_get(pte)); 3709d5d4b0aaSChen, Kenneth W same_page: 3710d6692183SChen, Kenneth W if (pages) { 371169d177c2SAndy Whitcroft pages[i] = mem_map_offset(page, pfn_offset); 3712a0368d4eSAndrea Arcangeli get_page_foll(pages[i]); 3713d6692183SChen, Kenneth W } 371463551ae0SDavid Gibson 371563551ae0SDavid Gibson if (vmas) 371663551ae0SDavid Gibson vmas[i] = vma; 371763551ae0SDavid Gibson 371863551ae0SDavid Gibson vaddr += PAGE_SIZE; 3719d5d4b0aaSChen, Kenneth W ++pfn_offset; 372063551ae0SDavid Gibson --remainder; 372163551ae0SDavid Gibson ++i; 3722d5d4b0aaSChen, Kenneth W if (vaddr < vma->vm_end && remainder && 3723a5516438SAndi Kleen pfn_offset < pages_per_huge_page(h)) { 3724d5d4b0aaSChen, Kenneth W /* 3725d5d4b0aaSChen, Kenneth W * We use pfn_offset to avoid touching the pageframes 3726d5d4b0aaSChen, Kenneth W * of this compound page. 3727d5d4b0aaSChen, Kenneth W */ 3728d5d4b0aaSChen, Kenneth W goto same_page; 3729d5d4b0aaSChen, Kenneth W } 3730cb900f41SKirill A. Shutemov spin_unlock(ptl); 373163551ae0SDavid Gibson } 373228a35716SMichel Lespinasse *nr_pages = remainder; 373363551ae0SDavid Gibson *position = vaddr; 373463551ae0SDavid Gibson 37352a15efc9SHugh Dickins return i ? i : -EFAULT; 373663551ae0SDavid Gibson } 37378f860591SZhang, Yanmin 37387da4d641SPeter Zijlstra unsigned long hugetlb_change_protection(struct vm_area_struct *vma, 37398f860591SZhang, Yanmin unsigned long address, unsigned long end, pgprot_t newprot) 37408f860591SZhang, Yanmin { 37418f860591SZhang, Yanmin struct mm_struct *mm = vma->vm_mm; 37428f860591SZhang, Yanmin unsigned long start = address; 37438f860591SZhang, Yanmin pte_t *ptep; 37448f860591SZhang, Yanmin pte_t pte; 3745a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 37467da4d641SPeter Zijlstra unsigned long pages = 0; 37478f860591SZhang, Yanmin 37488f860591SZhang, Yanmin BUG_ON(address >= end); 37498f860591SZhang, Yanmin flush_cache_range(vma, address, end); 37508f860591SZhang, Yanmin 3751a5338093SRik van Riel mmu_notifier_invalidate_range_start(mm, start, end); 375283cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 3753a5516438SAndi Kleen for (; address < end; address += huge_page_size(h)) { 3754cb900f41SKirill A. Shutemov spinlock_t *ptl; 37558f860591SZhang, Yanmin ptep = huge_pte_offset(mm, address); 37568f860591SZhang, Yanmin if (!ptep) 37578f860591SZhang, Yanmin continue; 3758cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 37597da4d641SPeter Zijlstra if (huge_pmd_unshare(mm, &address, ptep)) { 37607da4d641SPeter Zijlstra pages++; 3761cb900f41SKirill A. Shutemov spin_unlock(ptl); 376239dde65cSChen, Kenneth W continue; 37637da4d641SPeter Zijlstra } 3764a8bda28dSNaoya Horiguchi pte = huge_ptep_get(ptep); 3765a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { 3766a8bda28dSNaoya Horiguchi spin_unlock(ptl); 3767a8bda28dSNaoya Horiguchi continue; 3768a8bda28dSNaoya Horiguchi } 3769a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(pte))) { 3770a8bda28dSNaoya Horiguchi swp_entry_t entry = pte_to_swp_entry(pte); 3771a8bda28dSNaoya Horiguchi 3772a8bda28dSNaoya Horiguchi if (is_write_migration_entry(entry)) { 3773a8bda28dSNaoya Horiguchi pte_t newpte; 3774a8bda28dSNaoya Horiguchi 3775a8bda28dSNaoya Horiguchi make_migration_entry_read(&entry); 3776a8bda28dSNaoya Horiguchi newpte = swp_entry_to_pte(entry); 3777a8bda28dSNaoya Horiguchi set_huge_pte_at(mm, address, ptep, newpte); 3778a8bda28dSNaoya Horiguchi pages++; 3779a8bda28dSNaoya Horiguchi } 3780a8bda28dSNaoya Horiguchi spin_unlock(ptl); 3781a8bda28dSNaoya Horiguchi continue; 3782a8bda28dSNaoya Horiguchi } 3783a8bda28dSNaoya Horiguchi if (!huge_pte_none(pte)) { 37848f860591SZhang, Yanmin pte = huge_ptep_get_and_clear(mm, address, ptep); 3785106c992aSGerald Schaefer pte = pte_mkhuge(huge_pte_modify(pte, newprot)); 3786be7517d6STony Lu pte = arch_make_huge_pte(pte, vma, NULL, 0); 37878f860591SZhang, Yanmin set_huge_pte_at(mm, address, ptep, pte); 37887da4d641SPeter Zijlstra pages++; 37898f860591SZhang, Yanmin } 3790cb900f41SKirill A. Shutemov spin_unlock(ptl); 37918f860591SZhang, Yanmin } 3792d833352aSMel Gorman /* 3793c8c06efaSDavidlohr Bueso * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare 3794d833352aSMel Gorman * may have cleared our pud entry and done put_page on the page table: 3795c8c06efaSDavidlohr Bueso * once we release i_mmap_rwsem, another task can do the final put_page 3796d833352aSMel Gorman * and that page table be reused and filled with junk. 3797d833352aSMel Gorman */ 37988f860591SZhang, Yanmin flush_tlb_range(vma, start, end); 379934ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, start, end); 380083cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 3801a5338093SRik van Riel mmu_notifier_invalidate_range_end(mm, start, end); 38027da4d641SPeter Zijlstra 38037da4d641SPeter Zijlstra return pages << h->order; 38048f860591SZhang, Yanmin } 38058f860591SZhang, Yanmin 3806a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode, 3807a1e78772SMel Gorman long from, long to, 38085a6fe125SMel Gorman struct vm_area_struct *vma, 3809ca16d140SKOSAKI Motohiro vm_flags_t vm_flags) 3810e4e574b7SAdam Litke { 381117c9d12eSMel Gorman long ret, chg; 3812a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 381390481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 38149119a41eSJoonsoo Kim struct resv_map *resv_map; 38151c5ecae3SMike Kravetz long gbl_reserve; 3816e4e574b7SAdam Litke 3817a1e78772SMel Gorman /* 381817c9d12eSMel Gorman * Only apply hugepage reservation if asked. At fault time, an 381917c9d12eSMel Gorman * attempt will be made for VM_NORESERVE to allocate a page 382090481622SDavid Gibson * without using reserves 382117c9d12eSMel Gorman */ 3822ca16d140SKOSAKI Motohiro if (vm_flags & VM_NORESERVE) 382317c9d12eSMel Gorman return 0; 382417c9d12eSMel Gorman 382517c9d12eSMel Gorman /* 3826a1e78772SMel Gorman * Shared mappings base their reservation on the number of pages that 3827a1e78772SMel Gorman * are already allocated on behalf of the file. Private mappings need 3828a1e78772SMel Gorman * to reserve the full area even if read-only as mprotect() may be 3829a1e78772SMel Gorman * called to make the mapping read-write. Assume !vma is a shm mapping 3830a1e78772SMel Gorman */ 38319119a41eSJoonsoo Kim if (!vma || vma->vm_flags & VM_MAYSHARE) { 38324e35f483SJoonsoo Kim resv_map = inode_resv_map(inode); 38339119a41eSJoonsoo Kim 38341406ec9bSJoonsoo Kim chg = region_chg(resv_map, from, to); 38359119a41eSJoonsoo Kim 38369119a41eSJoonsoo Kim } else { 38379119a41eSJoonsoo Kim resv_map = resv_map_alloc(); 38385a6fe125SMel Gorman if (!resv_map) 38395a6fe125SMel Gorman return -ENOMEM; 38405a6fe125SMel Gorman 384117c9d12eSMel Gorman chg = to - from; 384217c9d12eSMel Gorman 38435a6fe125SMel Gorman set_vma_resv_map(vma, resv_map); 38445a6fe125SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_OWNER); 38455a6fe125SMel Gorman } 38465a6fe125SMel Gorman 3847c50ac050SDave Hansen if (chg < 0) { 3848c50ac050SDave Hansen ret = chg; 3849c50ac050SDave Hansen goto out_err; 3850c50ac050SDave Hansen } 385117c9d12eSMel Gorman 38521c5ecae3SMike Kravetz /* 38531c5ecae3SMike Kravetz * There must be enough pages in the subpool for the mapping. If 38541c5ecae3SMike Kravetz * the subpool has a minimum size, there may be some global 38551c5ecae3SMike Kravetz * reservations already in place (gbl_reserve). 38561c5ecae3SMike Kravetz */ 38571c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_get_pages(spool, chg); 38581c5ecae3SMike Kravetz if (gbl_reserve < 0) { 3859c50ac050SDave Hansen ret = -ENOSPC; 3860c50ac050SDave Hansen goto out_err; 3861c50ac050SDave Hansen } 386217c9d12eSMel Gorman 386317c9d12eSMel Gorman /* 386417c9d12eSMel Gorman * Check enough hugepages are available for the reservation. 386590481622SDavid Gibson * Hand the pages back to the subpool if there are not 386617c9d12eSMel Gorman */ 38671c5ecae3SMike Kravetz ret = hugetlb_acct_memory(h, gbl_reserve); 386817c9d12eSMel Gorman if (ret < 0) { 38691c5ecae3SMike Kravetz /* put back original number of pages, chg */ 38701c5ecae3SMike Kravetz (void)hugepage_subpool_put_pages(spool, chg); 3871c50ac050SDave Hansen goto out_err; 387217c9d12eSMel Gorman } 387317c9d12eSMel Gorman 387417c9d12eSMel Gorman /* 387517c9d12eSMel Gorman * Account for the reservations made. Shared mappings record regions 387617c9d12eSMel Gorman * that have reservations as they are shared by multiple VMAs. 387717c9d12eSMel Gorman * When the last VMA disappears, the region map says how much 387817c9d12eSMel Gorman * the reservation was and the page cache tells how much of 387917c9d12eSMel Gorman * the reservation was consumed. Private mappings are per-VMA and 388017c9d12eSMel Gorman * only the consumed reservations are tracked. When the VMA 388117c9d12eSMel Gorman * disappears, the original reservation is the VMA size and the 388217c9d12eSMel Gorman * consumed reservations are stored in the map. Hence, nothing 388317c9d12eSMel Gorman * else has to be done for private mappings here 388417c9d12eSMel Gorman */ 388533039678SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) { 388633039678SMike Kravetz long add = region_add(resv_map, from, to); 388733039678SMike Kravetz 388833039678SMike Kravetz if (unlikely(chg > add)) { 388933039678SMike Kravetz /* 389033039678SMike Kravetz * pages in this range were added to the reserve 389133039678SMike Kravetz * map between region_chg and region_add. This 389233039678SMike Kravetz * indicates a race with alloc_huge_page. Adjust 389333039678SMike Kravetz * the subpool and reserve counts modified above 389433039678SMike Kravetz * based on the difference. 389533039678SMike Kravetz */ 389633039678SMike Kravetz long rsv_adjust; 389733039678SMike Kravetz 389833039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 389933039678SMike Kravetz chg - add); 390033039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 390133039678SMike Kravetz } 390233039678SMike Kravetz } 3903a43a8c39SChen, Kenneth W return 0; 3904c50ac050SDave Hansen out_err: 39055e911373SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) 39065e911373SMike Kravetz region_abort(resv_map, from, to); 3907f031dd27SJoonsoo Kim if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 3908f031dd27SJoonsoo Kim kref_put(&resv_map->refs, resv_map_release); 3909c50ac050SDave Hansen return ret; 3910a43a8c39SChen, Kenneth W } 3911a43a8c39SChen, Kenneth W 3912a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) 3913a43a8c39SChen, Kenneth W { 3914a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 39154e35f483SJoonsoo Kim struct resv_map *resv_map = inode_resv_map(inode); 39169119a41eSJoonsoo Kim long chg = 0; 391790481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 39181c5ecae3SMike Kravetz long gbl_reserve; 391945c682a6SKen Chen 39209119a41eSJoonsoo Kim if (resv_map) 3921feba16e2SMike Kravetz chg = region_del(resv_map, offset, LONG_MAX); 392245c682a6SKen Chen spin_lock(&inode->i_lock); 3923e4c6f8beSEric Sandeen inode->i_blocks -= (blocks_per_huge_page(h) * freed); 392445c682a6SKen Chen spin_unlock(&inode->i_lock); 392545c682a6SKen Chen 39261c5ecae3SMike Kravetz /* 39271c5ecae3SMike Kravetz * If the subpool has a minimum size, the number of global 39281c5ecae3SMike Kravetz * reservations to be released may be adjusted. 39291c5ecae3SMike Kravetz */ 39301c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); 39311c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 3932a43a8c39SChen, Kenneth W } 393393f70f90SNaoya Horiguchi 39343212b535SSteve Capper #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE 39353212b535SSteve Capper static unsigned long page_table_shareable(struct vm_area_struct *svma, 39363212b535SSteve Capper struct vm_area_struct *vma, 39373212b535SSteve Capper unsigned long addr, pgoff_t idx) 39383212b535SSteve Capper { 39393212b535SSteve Capper unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + 39403212b535SSteve Capper svma->vm_start; 39413212b535SSteve Capper unsigned long sbase = saddr & PUD_MASK; 39423212b535SSteve Capper unsigned long s_end = sbase + PUD_SIZE; 39433212b535SSteve Capper 39443212b535SSteve Capper /* Allow segments to share if only one is marked locked */ 39453212b535SSteve Capper unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED; 39463212b535SSteve Capper unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED; 39473212b535SSteve Capper 39483212b535SSteve Capper /* 39493212b535SSteve Capper * match the virtual addresses, permission and the alignment of the 39503212b535SSteve Capper * page table page. 39513212b535SSteve Capper */ 39523212b535SSteve Capper if (pmd_index(addr) != pmd_index(saddr) || 39533212b535SSteve Capper vm_flags != svm_flags || 39543212b535SSteve Capper sbase < svma->vm_start || svma->vm_end < s_end) 39553212b535SSteve Capper return 0; 39563212b535SSteve Capper 39573212b535SSteve Capper return saddr; 39583212b535SSteve Capper } 39593212b535SSteve Capper 396031aafb45SNicholas Krause static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr) 39613212b535SSteve Capper { 39623212b535SSteve Capper unsigned long base = addr & PUD_MASK; 39633212b535SSteve Capper unsigned long end = base + PUD_SIZE; 39643212b535SSteve Capper 39653212b535SSteve Capper /* 39663212b535SSteve Capper * check on proper vm_flags and page table alignment 39673212b535SSteve Capper */ 39683212b535SSteve Capper if (vma->vm_flags & VM_MAYSHARE && 39693212b535SSteve Capper vma->vm_start <= base && end <= vma->vm_end) 397031aafb45SNicholas Krause return true; 397131aafb45SNicholas Krause return false; 39723212b535SSteve Capper } 39733212b535SSteve Capper 39743212b535SSteve Capper /* 39753212b535SSteve Capper * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() 39763212b535SSteve Capper * and returns the corresponding pte. While this is not necessary for the 39773212b535SSteve Capper * !shared pmd case because we can allocate the pmd later as well, it makes the 39783212b535SSteve Capper * code much cleaner. pmd allocation is essential for the shared case because 3979c8c06efaSDavidlohr Bueso * pud has to be populated inside the same i_mmap_rwsem section - otherwise 39803212b535SSteve Capper * racing tasks could either miss the sharing (see huge_pte_offset) or select a 39813212b535SSteve Capper * bad pmd for sharing. 39823212b535SSteve Capper */ 39833212b535SSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 39843212b535SSteve Capper { 39853212b535SSteve Capper struct vm_area_struct *vma = find_vma(mm, addr); 39863212b535SSteve Capper struct address_space *mapping = vma->vm_file->f_mapping; 39873212b535SSteve Capper pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + 39883212b535SSteve Capper vma->vm_pgoff; 39893212b535SSteve Capper struct vm_area_struct *svma; 39903212b535SSteve Capper unsigned long saddr; 39913212b535SSteve Capper pte_t *spte = NULL; 39923212b535SSteve Capper pte_t *pte; 3993cb900f41SKirill A. Shutemov spinlock_t *ptl; 39943212b535SSteve Capper 39953212b535SSteve Capper if (!vma_shareable(vma, addr)) 39963212b535SSteve Capper return (pte_t *)pmd_alloc(mm, pud, addr); 39973212b535SSteve Capper 399883cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 39993212b535SSteve Capper vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { 40003212b535SSteve Capper if (svma == vma) 40013212b535SSteve Capper continue; 40023212b535SSteve Capper 40033212b535SSteve Capper saddr = page_table_shareable(svma, vma, addr, idx); 40043212b535SSteve Capper if (saddr) { 40053212b535SSteve Capper spte = huge_pte_offset(svma->vm_mm, saddr); 40063212b535SSteve Capper if (spte) { 4007dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 40083212b535SSteve Capper get_page(virt_to_page(spte)); 40093212b535SSteve Capper break; 40103212b535SSteve Capper } 40113212b535SSteve Capper } 40123212b535SSteve Capper } 40133212b535SSteve Capper 40143212b535SSteve Capper if (!spte) 40153212b535SSteve Capper goto out; 40163212b535SSteve Capper 4017cb900f41SKirill A. Shutemov ptl = huge_pte_lockptr(hstate_vma(vma), mm, spte); 4018cb900f41SKirill A. Shutemov spin_lock(ptl); 4019dc6c9a35SKirill A. Shutemov if (pud_none(*pud)) { 40203212b535SSteve Capper pud_populate(mm, pud, 40213212b535SSteve Capper (pmd_t *)((unsigned long)spte & PAGE_MASK)); 4022dc6c9a35SKirill A. Shutemov } else { 40233212b535SSteve Capper put_page(virt_to_page(spte)); 4024dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 4025dc6c9a35SKirill A. Shutemov } 4026cb900f41SKirill A. Shutemov spin_unlock(ptl); 40273212b535SSteve Capper out: 40283212b535SSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 402983cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 40303212b535SSteve Capper return pte; 40313212b535SSteve Capper } 40323212b535SSteve Capper 40333212b535SSteve Capper /* 40343212b535SSteve Capper * unmap huge page backed by shared pte. 40353212b535SSteve Capper * 40363212b535SSteve Capper * Hugetlb pte page is ref counted at the time of mapping. If pte is shared 40373212b535SSteve Capper * indicated by page_count > 1, unmap is achieved by clearing pud and 40383212b535SSteve Capper * decrementing the ref count. If count == 1, the pte page is not shared. 40393212b535SSteve Capper * 4040cb900f41SKirill A. Shutemov * called with page table lock held. 40413212b535SSteve Capper * 40423212b535SSteve Capper * returns: 1 successfully unmapped a shared pte page 40433212b535SSteve Capper * 0 the underlying pte page is not shared, or it is the last user 40443212b535SSteve Capper */ 40453212b535SSteve Capper int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 40463212b535SSteve Capper { 40473212b535SSteve Capper pgd_t *pgd = pgd_offset(mm, *addr); 40483212b535SSteve Capper pud_t *pud = pud_offset(pgd, *addr); 40493212b535SSteve Capper 40503212b535SSteve Capper BUG_ON(page_count(virt_to_page(ptep)) == 0); 40513212b535SSteve Capper if (page_count(virt_to_page(ptep)) == 1) 40523212b535SSteve Capper return 0; 40533212b535SSteve Capper 40543212b535SSteve Capper pud_clear(pud); 40553212b535SSteve Capper put_page(virt_to_page(ptep)); 4056dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(mm); 40573212b535SSteve Capper *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE; 40583212b535SSteve Capper return 1; 40593212b535SSteve Capper } 40609e5fc74cSSteve Capper #define want_pmd_share() (1) 40619e5fc74cSSteve Capper #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 40629e5fc74cSSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 40639e5fc74cSSteve Capper { 40649e5fc74cSSteve Capper return NULL; 40659e5fc74cSSteve Capper } 4066e81f2d22SZhang Zhen 4067e81f2d22SZhang Zhen int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 4068e81f2d22SZhang Zhen { 4069e81f2d22SZhang Zhen return 0; 4070e81f2d22SZhang Zhen } 40719e5fc74cSSteve Capper #define want_pmd_share() (0) 40723212b535SSteve Capper #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 40733212b535SSteve Capper 40749e5fc74cSSteve Capper #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 40759e5fc74cSSteve Capper pte_t *huge_pte_alloc(struct mm_struct *mm, 40769e5fc74cSSteve Capper unsigned long addr, unsigned long sz) 40779e5fc74cSSteve Capper { 40789e5fc74cSSteve Capper pgd_t *pgd; 40799e5fc74cSSteve Capper pud_t *pud; 40809e5fc74cSSteve Capper pte_t *pte = NULL; 40819e5fc74cSSteve Capper 40829e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 40839e5fc74cSSteve Capper pud = pud_alloc(mm, pgd, addr); 40849e5fc74cSSteve Capper if (pud) { 40859e5fc74cSSteve Capper if (sz == PUD_SIZE) { 40869e5fc74cSSteve Capper pte = (pte_t *)pud; 40879e5fc74cSSteve Capper } else { 40889e5fc74cSSteve Capper BUG_ON(sz != PMD_SIZE); 40899e5fc74cSSteve Capper if (want_pmd_share() && pud_none(*pud)) 40909e5fc74cSSteve Capper pte = huge_pmd_share(mm, addr, pud); 40919e5fc74cSSteve Capper else 40929e5fc74cSSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 40939e5fc74cSSteve Capper } 40949e5fc74cSSteve Capper } 40959e5fc74cSSteve Capper BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte)); 40969e5fc74cSSteve Capper 40979e5fc74cSSteve Capper return pte; 40989e5fc74cSSteve Capper } 40999e5fc74cSSteve Capper 41009e5fc74cSSteve Capper pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) 41019e5fc74cSSteve Capper { 41029e5fc74cSSteve Capper pgd_t *pgd; 41039e5fc74cSSteve Capper pud_t *pud; 41049e5fc74cSSteve Capper pmd_t *pmd = NULL; 41059e5fc74cSSteve Capper 41069e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 41079e5fc74cSSteve Capper if (pgd_present(*pgd)) { 41089e5fc74cSSteve Capper pud = pud_offset(pgd, addr); 41099e5fc74cSSteve Capper if (pud_present(*pud)) { 41109e5fc74cSSteve Capper if (pud_huge(*pud)) 41119e5fc74cSSteve Capper return (pte_t *)pud; 41129e5fc74cSSteve Capper pmd = pmd_offset(pud, addr); 41139e5fc74cSSteve Capper } 41149e5fc74cSSteve Capper } 41159e5fc74cSSteve Capper return (pte_t *) pmd; 41169e5fc74cSSteve Capper } 41179e5fc74cSSteve Capper 411861f77edaSNaoya Horiguchi #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 411961f77edaSNaoya Horiguchi 412061f77edaSNaoya Horiguchi /* 412161f77edaSNaoya Horiguchi * These functions are overwritable if your architecture needs its own 412261f77edaSNaoya Horiguchi * behavior. 412361f77edaSNaoya Horiguchi */ 412461f77edaSNaoya Horiguchi struct page * __weak 412561f77edaSNaoya Horiguchi follow_huge_addr(struct mm_struct *mm, unsigned long address, 412661f77edaSNaoya Horiguchi int write) 412761f77edaSNaoya Horiguchi { 412861f77edaSNaoya Horiguchi return ERR_PTR(-EINVAL); 412961f77edaSNaoya Horiguchi } 413061f77edaSNaoya Horiguchi 413161f77edaSNaoya Horiguchi struct page * __weak 41329e5fc74cSSteve Capper follow_huge_pmd(struct mm_struct *mm, unsigned long address, 4133e66f17ffSNaoya Horiguchi pmd_t *pmd, int flags) 41349e5fc74cSSteve Capper { 4135e66f17ffSNaoya Horiguchi struct page *page = NULL; 4136e66f17ffSNaoya Horiguchi spinlock_t *ptl; 4137e66f17ffSNaoya Horiguchi retry: 4138e66f17ffSNaoya Horiguchi ptl = pmd_lockptr(mm, pmd); 4139e66f17ffSNaoya Horiguchi spin_lock(ptl); 4140e66f17ffSNaoya Horiguchi /* 4141e66f17ffSNaoya Horiguchi * make sure that the address range covered by this pmd is not 4142e66f17ffSNaoya Horiguchi * unmapped from other threads. 4143e66f17ffSNaoya Horiguchi */ 4144e66f17ffSNaoya Horiguchi if (!pmd_huge(*pmd)) 4145e66f17ffSNaoya Horiguchi goto out; 4146e66f17ffSNaoya Horiguchi if (pmd_present(*pmd)) { 414797534127SGerald Schaefer page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT); 4148e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 4149e66f17ffSNaoya Horiguchi get_page(page); 4150e66f17ffSNaoya Horiguchi } else { 4151e66f17ffSNaoya Horiguchi if (is_hugetlb_entry_migration(huge_ptep_get((pte_t *)pmd))) { 4152e66f17ffSNaoya Horiguchi spin_unlock(ptl); 4153e66f17ffSNaoya Horiguchi __migration_entry_wait(mm, (pte_t *)pmd, ptl); 4154e66f17ffSNaoya Horiguchi goto retry; 4155e66f17ffSNaoya Horiguchi } 4156e66f17ffSNaoya Horiguchi /* 4157e66f17ffSNaoya Horiguchi * hwpoisoned entry is treated as no_page_table in 4158e66f17ffSNaoya Horiguchi * follow_page_mask(). 4159e66f17ffSNaoya Horiguchi */ 4160e66f17ffSNaoya Horiguchi } 4161e66f17ffSNaoya Horiguchi out: 4162e66f17ffSNaoya Horiguchi spin_unlock(ptl); 41639e5fc74cSSteve Capper return page; 41649e5fc74cSSteve Capper } 41659e5fc74cSSteve Capper 416661f77edaSNaoya Horiguchi struct page * __weak 41679e5fc74cSSteve Capper follow_huge_pud(struct mm_struct *mm, unsigned long address, 4168e66f17ffSNaoya Horiguchi pud_t *pud, int flags) 41699e5fc74cSSteve Capper { 4170e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 4171e66f17ffSNaoya Horiguchi return NULL; 41729e5fc74cSSteve Capper 4173e66f17ffSNaoya Horiguchi return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT); 41749e5fc74cSSteve Capper } 41759e5fc74cSSteve Capper 4176d5bd9106SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE 4177d5bd9106SAndi Kleen 417893f70f90SNaoya Horiguchi /* 417993f70f90SNaoya Horiguchi * This function is called from memory failure code. 418093f70f90SNaoya Horiguchi * Assume the caller holds page lock of the head page. 418193f70f90SNaoya Horiguchi */ 41826de2b1aaSNaoya Horiguchi int dequeue_hwpoisoned_huge_page(struct page *hpage) 418393f70f90SNaoya Horiguchi { 418493f70f90SNaoya Horiguchi struct hstate *h = page_hstate(hpage); 418593f70f90SNaoya Horiguchi int nid = page_to_nid(hpage); 41866de2b1aaSNaoya Horiguchi int ret = -EBUSY; 418793f70f90SNaoya Horiguchi 418893f70f90SNaoya Horiguchi spin_lock(&hugetlb_lock); 41897e1f049eSNaoya Horiguchi /* 41907e1f049eSNaoya Horiguchi * Just checking !page_huge_active is not enough, because that could be 41917e1f049eSNaoya Horiguchi * an isolated/hwpoisoned hugepage (which have >0 refcount). 41927e1f049eSNaoya Horiguchi */ 41937e1f049eSNaoya Horiguchi if (!page_huge_active(hpage) && !page_count(hpage)) { 419456f2fb14SNaoya Horiguchi /* 419556f2fb14SNaoya Horiguchi * Hwpoisoned hugepage isn't linked to activelist or freelist, 419656f2fb14SNaoya Horiguchi * but dangling hpage->lru can trigger list-debug warnings 419756f2fb14SNaoya Horiguchi * (this happens when we call unpoison_memory() on it), 419856f2fb14SNaoya Horiguchi * so let it point to itself with list_del_init(). 419956f2fb14SNaoya Horiguchi */ 420056f2fb14SNaoya Horiguchi list_del_init(&hpage->lru); 42018c6c2ecbSNaoya Horiguchi set_page_refcounted(hpage); 420293f70f90SNaoya Horiguchi h->free_huge_pages--; 420393f70f90SNaoya Horiguchi h->free_huge_pages_node[nid]--; 42046de2b1aaSNaoya Horiguchi ret = 0; 420593f70f90SNaoya Horiguchi } 42066de2b1aaSNaoya Horiguchi spin_unlock(&hugetlb_lock); 42076de2b1aaSNaoya Horiguchi return ret; 42086de2b1aaSNaoya Horiguchi } 42096de2b1aaSNaoya Horiguchi #endif 421031caf665SNaoya Horiguchi 421131caf665SNaoya Horiguchi bool isolate_huge_page(struct page *page, struct list_head *list) 421231caf665SNaoya Horiguchi { 4213bcc54222SNaoya Horiguchi bool ret = true; 4214bcc54222SNaoya Horiguchi 4215309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 421631caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 4217bcc54222SNaoya Horiguchi if (!page_huge_active(page) || !get_page_unless_zero(page)) { 4218bcc54222SNaoya Horiguchi ret = false; 4219bcc54222SNaoya Horiguchi goto unlock; 4220bcc54222SNaoya Horiguchi } 4221bcc54222SNaoya Horiguchi clear_page_huge_active(page); 422231caf665SNaoya Horiguchi list_move_tail(&page->lru, list); 4223bcc54222SNaoya Horiguchi unlock: 422431caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 4225bcc54222SNaoya Horiguchi return ret; 422631caf665SNaoya Horiguchi } 422731caf665SNaoya Horiguchi 422831caf665SNaoya Horiguchi void putback_active_hugepage(struct page *page) 422931caf665SNaoya Horiguchi { 4230309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 423131caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 4232bcc54222SNaoya Horiguchi set_page_huge_active(page); 423331caf665SNaoya Horiguchi list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist); 423431caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 423531caf665SNaoya Horiguchi put_page(page); 423631caf665SNaoya Horiguchi } 4237