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 /* 550b5cec28dSMike Kravetz * A rare out of memory error was encountered which prevented removal of 551b5cec28dSMike Kravetz * the reserve map region for a page. The huge page itself was free'ed 552b5cec28dSMike Kravetz * and removed from the page cache. This routine will adjust the subpool 553b5cec28dSMike Kravetz * usage count, and the global reserve count if needed. By incrementing 554b5cec28dSMike Kravetz * these counts, the reserve map entry which could not be deleted will 555b5cec28dSMike Kravetz * appear as a "reserved" entry instead of simply dangling with incorrect 556b5cec28dSMike Kravetz * counts. 557b5cec28dSMike Kravetz */ 558b5cec28dSMike Kravetz void hugetlb_fix_reserve_counts(struct inode *inode, bool restore_reserve) 559b5cec28dSMike Kravetz { 560b5cec28dSMike Kravetz struct hugepage_subpool *spool = subpool_inode(inode); 561b5cec28dSMike Kravetz long rsv_adjust; 562b5cec28dSMike Kravetz 563b5cec28dSMike Kravetz rsv_adjust = hugepage_subpool_get_pages(spool, 1); 564b5cec28dSMike Kravetz if (restore_reserve && rsv_adjust) { 565b5cec28dSMike Kravetz struct hstate *h = hstate_inode(inode); 566b5cec28dSMike Kravetz 567b5cec28dSMike Kravetz hugetlb_acct_memory(h, 1); 568b5cec28dSMike Kravetz } 569b5cec28dSMike Kravetz } 570b5cec28dSMike Kravetz 571b5cec28dSMike Kravetz /* 5721dd308a7SMike Kravetz * Count and return the number of huge pages in the reserve map 5731dd308a7SMike Kravetz * that intersect with the range [f, t). 5741dd308a7SMike Kravetz */ 5751406ec9bSJoonsoo Kim static long region_count(struct resv_map *resv, long f, long t) 57684afd99bSAndy Whitcroft { 5771406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 57884afd99bSAndy Whitcroft struct file_region *rg; 57984afd99bSAndy Whitcroft long chg = 0; 58084afd99bSAndy Whitcroft 5817b24d861SDavidlohr Bueso spin_lock(&resv->lock); 58284afd99bSAndy Whitcroft /* Locate each segment we overlap with, and count that overlap. */ 58384afd99bSAndy Whitcroft list_for_each_entry(rg, head, link) { 584f2135a4aSWang Sheng-Hui long seg_from; 585f2135a4aSWang Sheng-Hui long seg_to; 58684afd99bSAndy Whitcroft 58784afd99bSAndy Whitcroft if (rg->to <= f) 58884afd99bSAndy Whitcroft continue; 58984afd99bSAndy Whitcroft if (rg->from >= t) 59084afd99bSAndy Whitcroft break; 59184afd99bSAndy Whitcroft 59284afd99bSAndy Whitcroft seg_from = max(rg->from, f); 59384afd99bSAndy Whitcroft seg_to = min(rg->to, t); 59484afd99bSAndy Whitcroft 59584afd99bSAndy Whitcroft chg += seg_to - seg_from; 59684afd99bSAndy Whitcroft } 5977b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 59884afd99bSAndy Whitcroft 59984afd99bSAndy Whitcroft return chg; 60084afd99bSAndy Whitcroft } 60184afd99bSAndy Whitcroft 60296822904SAndy Whitcroft /* 603e7c4b0bfSAndy Whitcroft * Convert the address within this vma to the page offset within 604e7c4b0bfSAndy Whitcroft * the mapping, in pagecache page units; huge pages here. 605e7c4b0bfSAndy Whitcroft */ 606a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h, 607a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 608e7c4b0bfSAndy Whitcroft { 609a5516438SAndi Kleen return ((address - vma->vm_start) >> huge_page_shift(h)) + 610a5516438SAndi Kleen (vma->vm_pgoff >> huge_page_order(h)); 611e7c4b0bfSAndy Whitcroft } 612e7c4b0bfSAndy Whitcroft 6130fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma, 6140fe6e20bSNaoya Horiguchi unsigned long address) 6150fe6e20bSNaoya Horiguchi { 6160fe6e20bSNaoya Horiguchi return vma_hugecache_offset(hstate_vma(vma), vma, address); 6170fe6e20bSNaoya Horiguchi } 6180fe6e20bSNaoya Horiguchi 61984afd99bSAndy Whitcroft /* 62008fba699SMel Gorman * Return the size of the pages allocated when backing a VMA. In the majority 62108fba699SMel Gorman * cases this will be same size as used by the page table entries. 62208fba699SMel Gorman */ 62308fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) 62408fba699SMel Gorman { 62508fba699SMel Gorman struct hstate *hstate; 62608fba699SMel Gorman 62708fba699SMel Gorman if (!is_vm_hugetlb_page(vma)) 62808fba699SMel Gorman return PAGE_SIZE; 62908fba699SMel Gorman 63008fba699SMel Gorman hstate = hstate_vma(vma); 63108fba699SMel Gorman 6322415cf12SWanpeng Li return 1UL << huge_page_shift(hstate); 63308fba699SMel Gorman } 634f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize); 63508fba699SMel Gorman 63608fba699SMel Gorman /* 6373340289dSMel Gorman * Return the page size being used by the MMU to back a VMA. In the majority 6383340289dSMel Gorman * of cases, the page size used by the kernel matches the MMU size. On 6393340289dSMel Gorman * architectures where it differs, an architecture-specific version of this 6403340289dSMel Gorman * function is required. 6413340289dSMel Gorman */ 6423340289dSMel Gorman #ifndef vma_mmu_pagesize 6433340289dSMel Gorman unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) 6443340289dSMel Gorman { 6453340289dSMel Gorman return vma_kernel_pagesize(vma); 6463340289dSMel Gorman } 6473340289dSMel Gorman #endif 6483340289dSMel Gorman 6493340289dSMel Gorman /* 65084afd99bSAndy Whitcroft * Flags for MAP_PRIVATE reservations. These are stored in the bottom 65184afd99bSAndy Whitcroft * bits of the reservation map pointer, which are always clear due to 65284afd99bSAndy Whitcroft * alignment. 65384afd99bSAndy Whitcroft */ 65484afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER (1UL << 0) 65584afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1) 65604f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) 65784afd99bSAndy Whitcroft 658a1e78772SMel Gorman /* 659a1e78772SMel Gorman * These helpers are used to track how many pages are reserved for 660a1e78772SMel Gorman * faults in a MAP_PRIVATE mapping. Only the process that called mmap() 661a1e78772SMel Gorman * is guaranteed to have their future faults succeed. 662a1e78772SMel Gorman * 663a1e78772SMel Gorman * With the exception of reset_vma_resv_huge_pages() which is called at fork(), 664a1e78772SMel Gorman * the reserve counters are updated with the hugetlb_lock held. It is safe 665a1e78772SMel Gorman * to reset the VMA at fork() time as it is not in use yet and there is no 666a1e78772SMel Gorman * chance of the global counters getting corrupted as a result of the values. 66784afd99bSAndy Whitcroft * 66884afd99bSAndy Whitcroft * The private mapping reservation is represented in a subtly different 66984afd99bSAndy Whitcroft * manner to a shared mapping. A shared mapping has a region map associated 67084afd99bSAndy Whitcroft * with the underlying file, this region map represents the backing file 67184afd99bSAndy Whitcroft * pages which have ever had a reservation assigned which this persists even 67284afd99bSAndy Whitcroft * after the page is instantiated. A private mapping has a region map 67384afd99bSAndy Whitcroft * associated with the original mmap which is attached to all VMAs which 67484afd99bSAndy Whitcroft * reference it, this region map represents those offsets which have consumed 67584afd99bSAndy Whitcroft * reservation ie. where pages have been instantiated. 676a1e78772SMel Gorman */ 677e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma) 678e7c4b0bfSAndy Whitcroft { 679e7c4b0bfSAndy Whitcroft return (unsigned long)vma->vm_private_data; 680e7c4b0bfSAndy Whitcroft } 681e7c4b0bfSAndy Whitcroft 682e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma, 683e7c4b0bfSAndy Whitcroft unsigned long value) 684e7c4b0bfSAndy Whitcroft { 685e7c4b0bfSAndy Whitcroft vma->vm_private_data = (void *)value; 686e7c4b0bfSAndy Whitcroft } 687e7c4b0bfSAndy Whitcroft 6889119a41eSJoonsoo Kim struct resv_map *resv_map_alloc(void) 68984afd99bSAndy Whitcroft { 69084afd99bSAndy Whitcroft struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); 6915e911373SMike Kravetz struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL); 6925e911373SMike Kravetz 6935e911373SMike Kravetz if (!resv_map || !rg) { 6945e911373SMike Kravetz kfree(resv_map); 6955e911373SMike Kravetz kfree(rg); 69684afd99bSAndy Whitcroft return NULL; 6975e911373SMike Kravetz } 69884afd99bSAndy Whitcroft 69984afd99bSAndy Whitcroft kref_init(&resv_map->refs); 7007b24d861SDavidlohr Bueso spin_lock_init(&resv_map->lock); 70184afd99bSAndy Whitcroft INIT_LIST_HEAD(&resv_map->regions); 70284afd99bSAndy Whitcroft 7035e911373SMike Kravetz resv_map->adds_in_progress = 0; 7045e911373SMike Kravetz 7055e911373SMike Kravetz INIT_LIST_HEAD(&resv_map->region_cache); 7065e911373SMike Kravetz list_add(&rg->link, &resv_map->region_cache); 7075e911373SMike Kravetz resv_map->region_cache_count = 1; 7085e911373SMike Kravetz 70984afd99bSAndy Whitcroft return resv_map; 71084afd99bSAndy Whitcroft } 71184afd99bSAndy Whitcroft 7129119a41eSJoonsoo Kim void resv_map_release(struct kref *ref) 71384afd99bSAndy Whitcroft { 71484afd99bSAndy Whitcroft struct resv_map *resv_map = container_of(ref, struct resv_map, refs); 7155e911373SMike Kravetz struct list_head *head = &resv_map->region_cache; 7165e911373SMike Kravetz struct file_region *rg, *trg; 71784afd99bSAndy Whitcroft 71884afd99bSAndy Whitcroft /* Clear out any active regions before we release the map. */ 719feba16e2SMike Kravetz region_del(resv_map, 0, LONG_MAX); 7205e911373SMike Kravetz 7215e911373SMike Kravetz /* ... and any entries left in the cache */ 7225e911373SMike Kravetz list_for_each_entry_safe(rg, trg, head, link) { 7235e911373SMike Kravetz list_del(&rg->link); 7245e911373SMike Kravetz kfree(rg); 7255e911373SMike Kravetz } 7265e911373SMike Kravetz 7275e911373SMike Kravetz VM_BUG_ON(resv_map->adds_in_progress); 7285e911373SMike Kravetz 72984afd99bSAndy Whitcroft kfree(resv_map); 73084afd99bSAndy Whitcroft } 73184afd99bSAndy Whitcroft 7324e35f483SJoonsoo Kim static inline struct resv_map *inode_resv_map(struct inode *inode) 7334e35f483SJoonsoo Kim { 7344e35f483SJoonsoo Kim return inode->i_mapping->private_data; 7354e35f483SJoonsoo Kim } 7364e35f483SJoonsoo Kim 73784afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma) 738a1e78772SMel Gorman { 73981d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 7404e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) { 7414e35f483SJoonsoo Kim struct address_space *mapping = vma->vm_file->f_mapping; 7424e35f483SJoonsoo Kim struct inode *inode = mapping->host; 7434e35f483SJoonsoo Kim 7444e35f483SJoonsoo Kim return inode_resv_map(inode); 7454e35f483SJoonsoo Kim 7464e35f483SJoonsoo Kim } else { 74784afd99bSAndy Whitcroft return (struct resv_map *)(get_vma_private_data(vma) & 74884afd99bSAndy Whitcroft ~HPAGE_RESV_MASK); 7494e35f483SJoonsoo Kim } 750a1e78772SMel Gorman } 751a1e78772SMel Gorman 75284afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) 753a1e78772SMel Gorman { 75481d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 75581d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 756a1e78772SMel Gorman 75784afd99bSAndy Whitcroft set_vma_private_data(vma, (get_vma_private_data(vma) & 75884afd99bSAndy Whitcroft HPAGE_RESV_MASK) | (unsigned long)map); 75904f2cbe3SMel Gorman } 76004f2cbe3SMel Gorman 76104f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) 76204f2cbe3SMel Gorman { 76381d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 76481d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 765e7c4b0bfSAndy Whitcroft 766e7c4b0bfSAndy Whitcroft set_vma_private_data(vma, get_vma_private_data(vma) | flags); 76704f2cbe3SMel Gorman } 76804f2cbe3SMel Gorman 76904f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) 77004f2cbe3SMel Gorman { 77181d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 772e7c4b0bfSAndy Whitcroft 773e7c4b0bfSAndy Whitcroft return (get_vma_private_data(vma) & flag) != 0; 774a1e78772SMel Gorman } 775a1e78772SMel Gorman 77604f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ 777a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma) 778a1e78772SMel Gorman { 77981d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 780f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 781a1e78772SMel Gorman vma->vm_private_data = (void *)0; 782a1e78772SMel Gorman } 783a1e78772SMel Gorman 784a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */ 785559ec2f8SNicholas Krause static bool vma_has_reserves(struct vm_area_struct *vma, long chg) 786a1e78772SMel Gorman { 787af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_NORESERVE) { 788af0ed73eSJoonsoo Kim /* 789af0ed73eSJoonsoo Kim * This address is already reserved by other process(chg == 0), 790af0ed73eSJoonsoo Kim * so, we should decrement reserved count. Without decrementing, 791af0ed73eSJoonsoo Kim * reserve count remains after releasing inode, because this 792af0ed73eSJoonsoo Kim * allocated page will go into page cache and is regarded as 793af0ed73eSJoonsoo Kim * coming from reserved pool in releasing step. Currently, we 794af0ed73eSJoonsoo Kim * don't have any other solution to deal with this situation 795af0ed73eSJoonsoo Kim * properly, so add work-around here. 796af0ed73eSJoonsoo Kim */ 797af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE && chg == 0) 798559ec2f8SNicholas Krause return true; 799af0ed73eSJoonsoo Kim else 800559ec2f8SNicholas Krause return false; 801af0ed73eSJoonsoo Kim } 802a63884e9SJoonsoo Kim 803a63884e9SJoonsoo Kim /* Shared mappings always use reserves */ 8041fb1b0e9SMike Kravetz if (vma->vm_flags & VM_MAYSHARE) { 8051fb1b0e9SMike Kravetz /* 8061fb1b0e9SMike Kravetz * We know VM_NORESERVE is not set. Therefore, there SHOULD 8071fb1b0e9SMike Kravetz * be a region map for all pages. The only situation where 8081fb1b0e9SMike Kravetz * there is no region map is if a hole was punched via 8091fb1b0e9SMike Kravetz * fallocate. In this case, there really are no reverves to 8101fb1b0e9SMike Kravetz * use. This situation is indicated if chg != 0. 8111fb1b0e9SMike Kravetz */ 8121fb1b0e9SMike Kravetz if (chg) 8131fb1b0e9SMike Kravetz return false; 8141fb1b0e9SMike Kravetz else 815559ec2f8SNicholas Krause return true; 8161fb1b0e9SMike Kravetz } 817a63884e9SJoonsoo Kim 818a63884e9SJoonsoo Kim /* 819a63884e9SJoonsoo Kim * Only the process that called mmap() has reserves for 820a63884e9SJoonsoo Kim * private mappings. 821a63884e9SJoonsoo Kim */ 8227f09ca51SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 823559ec2f8SNicholas Krause return true; 824a63884e9SJoonsoo Kim 825559ec2f8SNicholas Krause return false; 826a1e78772SMel Gorman } 827a1e78772SMel Gorman 828a5516438SAndi Kleen static void enqueue_huge_page(struct hstate *h, struct page *page) 8291da177e4SLinus Torvalds { 8301da177e4SLinus Torvalds int nid = page_to_nid(page); 8310edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_freelists[nid]); 832a5516438SAndi Kleen h->free_huge_pages++; 833a5516438SAndi Kleen h->free_huge_pages_node[nid]++; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 836bf50bab2SNaoya Horiguchi static struct page *dequeue_huge_page_node(struct hstate *h, int nid) 837bf50bab2SNaoya Horiguchi { 838bf50bab2SNaoya Horiguchi struct page *page; 839bf50bab2SNaoya Horiguchi 840c8721bbbSNaoya Horiguchi list_for_each_entry(page, &h->hugepage_freelists[nid], lru) 841c8721bbbSNaoya Horiguchi if (!is_migrate_isolate_page(page)) 842c8721bbbSNaoya Horiguchi break; 843c8721bbbSNaoya Horiguchi /* 844c8721bbbSNaoya Horiguchi * if 'non-isolated free hugepage' not found on the list, 845c8721bbbSNaoya Horiguchi * the allocation fails. 846c8721bbbSNaoya Horiguchi */ 847c8721bbbSNaoya Horiguchi if (&h->hugepage_freelists[nid] == &page->lru) 848bf50bab2SNaoya Horiguchi return NULL; 8490edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 850a9869b83SNaoya Horiguchi set_page_refcounted(page); 851bf50bab2SNaoya Horiguchi h->free_huge_pages--; 852bf50bab2SNaoya Horiguchi h->free_huge_pages_node[nid]--; 853bf50bab2SNaoya Horiguchi return page; 854bf50bab2SNaoya Horiguchi } 855bf50bab2SNaoya Horiguchi 85686cdb465SNaoya Horiguchi /* Movability of hugepages depends on migration support. */ 85786cdb465SNaoya Horiguchi static inline gfp_t htlb_alloc_mask(struct hstate *h) 85886cdb465SNaoya Horiguchi { 859100873d7SNaoya Horiguchi if (hugepages_treat_as_movable || hugepage_migration_supported(h)) 86086cdb465SNaoya Horiguchi return GFP_HIGHUSER_MOVABLE; 86186cdb465SNaoya Horiguchi else 86286cdb465SNaoya Horiguchi return GFP_HIGHUSER; 86386cdb465SNaoya Horiguchi } 86486cdb465SNaoya Horiguchi 865a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h, 866a5516438SAndi Kleen struct vm_area_struct *vma, 867af0ed73eSJoonsoo Kim unsigned long address, int avoid_reserve, 868af0ed73eSJoonsoo Kim long chg) 8691da177e4SLinus Torvalds { 870b1c12cbcSKonstantin Khlebnikov struct page *page = NULL; 871480eccf9SLee Schermerhorn struct mempolicy *mpol; 87219770b32SMel Gorman nodemask_t *nodemask; 873c0ff7453SMiao Xie struct zonelist *zonelist; 874dd1a239fSMel Gorman struct zone *zone; 875dd1a239fSMel Gorman struct zoneref *z; 876cc9a6c87SMel Gorman unsigned int cpuset_mems_cookie; 8771da177e4SLinus Torvalds 878a1e78772SMel Gorman /* 879a1e78772SMel Gorman * A child process with MAP_PRIVATE mappings created by their parent 880a1e78772SMel Gorman * have no page reserves. This check ensures that reservations are 881a1e78772SMel Gorman * not "stolen". The child may still get SIGKILLed 882a1e78772SMel Gorman */ 883af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg) && 884a5516438SAndi Kleen h->free_huge_pages - h->resv_huge_pages == 0) 885c0ff7453SMiao Xie goto err; 886a1e78772SMel Gorman 88704f2cbe3SMel Gorman /* If reserves cannot be used, ensure enough pages are in the pool */ 888a5516438SAndi Kleen if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0) 8896eab04a8SJustin P. Mattock goto err; 89004f2cbe3SMel Gorman 8919966c4bbSJoonsoo Kim retry_cpuset: 892d26914d1SMel Gorman cpuset_mems_cookie = read_mems_allowed_begin(); 8939966c4bbSJoonsoo Kim zonelist = huge_zonelist(vma, address, 89486cdb465SNaoya Horiguchi htlb_alloc_mask(h), &mpol, &nodemask); 8959966c4bbSJoonsoo Kim 89619770b32SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 89719770b32SMel Gorman MAX_NR_ZONES - 1, nodemask) { 898344736f2SVladimir Davydov if (cpuset_zone_allowed(zone, htlb_alloc_mask(h))) { 899bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, zone_to_nid(zone)); 900bf50bab2SNaoya Horiguchi if (page) { 901af0ed73eSJoonsoo Kim if (avoid_reserve) 902af0ed73eSJoonsoo Kim break; 903af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg)) 904af0ed73eSJoonsoo Kim break; 905af0ed73eSJoonsoo Kim 90607443a85SJoonsoo Kim SetPagePrivate(page); 907a63884e9SJoonsoo Kim h->resv_huge_pages--; 9085ab3ee7bSKen Chen break; 9091da177e4SLinus Torvalds } 9103abf7afdSAndrew Morton } 911bf50bab2SNaoya Horiguchi } 912cc9a6c87SMel Gorman 913cc9a6c87SMel Gorman mpol_cond_put(mpol); 914d26914d1SMel Gorman if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie))) 915cc9a6c87SMel Gorman goto retry_cpuset; 916cc9a6c87SMel Gorman return page; 917cc9a6c87SMel Gorman 918c0ff7453SMiao Xie err: 919cc9a6c87SMel Gorman return NULL; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds 9221cac6f2cSLuiz Capitulino /* 9231cac6f2cSLuiz Capitulino * common helper functions for hstate_next_node_to_{alloc|free}. 9241cac6f2cSLuiz Capitulino * We may have allocated or freed a huge page based on a different 9251cac6f2cSLuiz Capitulino * nodes_allowed previously, so h->next_node_to_{alloc|free} might 9261cac6f2cSLuiz Capitulino * be outside of *nodes_allowed. Ensure that we use an allowed 9271cac6f2cSLuiz Capitulino * node for alloc or free. 9281cac6f2cSLuiz Capitulino */ 9291cac6f2cSLuiz Capitulino static int next_node_allowed(int nid, nodemask_t *nodes_allowed) 9301cac6f2cSLuiz Capitulino { 9311cac6f2cSLuiz Capitulino nid = next_node(nid, *nodes_allowed); 9321cac6f2cSLuiz Capitulino if (nid == MAX_NUMNODES) 9331cac6f2cSLuiz Capitulino nid = first_node(*nodes_allowed); 9341cac6f2cSLuiz Capitulino VM_BUG_ON(nid >= MAX_NUMNODES); 9351cac6f2cSLuiz Capitulino 9361cac6f2cSLuiz Capitulino return nid; 9371cac6f2cSLuiz Capitulino } 9381cac6f2cSLuiz Capitulino 9391cac6f2cSLuiz Capitulino static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) 9401cac6f2cSLuiz Capitulino { 9411cac6f2cSLuiz Capitulino if (!node_isset(nid, *nodes_allowed)) 9421cac6f2cSLuiz Capitulino nid = next_node_allowed(nid, nodes_allowed); 9431cac6f2cSLuiz Capitulino return nid; 9441cac6f2cSLuiz Capitulino } 9451cac6f2cSLuiz Capitulino 9461cac6f2cSLuiz Capitulino /* 9471cac6f2cSLuiz Capitulino * returns the previously saved node ["this node"] from which to 9481cac6f2cSLuiz Capitulino * allocate a persistent huge page for the pool and advance the 9491cac6f2cSLuiz Capitulino * next node from which to allocate, handling wrap at end of node 9501cac6f2cSLuiz Capitulino * mask. 9511cac6f2cSLuiz Capitulino */ 9521cac6f2cSLuiz Capitulino static int hstate_next_node_to_alloc(struct hstate *h, 9531cac6f2cSLuiz Capitulino nodemask_t *nodes_allowed) 9541cac6f2cSLuiz Capitulino { 9551cac6f2cSLuiz Capitulino int nid; 9561cac6f2cSLuiz Capitulino 9571cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 9581cac6f2cSLuiz Capitulino 9591cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); 9601cac6f2cSLuiz Capitulino h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); 9611cac6f2cSLuiz Capitulino 9621cac6f2cSLuiz Capitulino return nid; 9631cac6f2cSLuiz Capitulino } 9641cac6f2cSLuiz Capitulino 9651cac6f2cSLuiz Capitulino /* 9661cac6f2cSLuiz Capitulino * helper for free_pool_huge_page() - return the previously saved 9671cac6f2cSLuiz Capitulino * node ["this node"] from which to free a huge page. Advance the 9681cac6f2cSLuiz Capitulino * next node id whether or not we find a free huge page to free so 9691cac6f2cSLuiz Capitulino * that the next attempt to free addresses the next node. 9701cac6f2cSLuiz Capitulino */ 9711cac6f2cSLuiz Capitulino static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) 9721cac6f2cSLuiz Capitulino { 9731cac6f2cSLuiz Capitulino int nid; 9741cac6f2cSLuiz Capitulino 9751cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 9761cac6f2cSLuiz Capitulino 9771cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); 9781cac6f2cSLuiz Capitulino h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); 9791cac6f2cSLuiz Capitulino 9801cac6f2cSLuiz Capitulino return nid; 9811cac6f2cSLuiz Capitulino } 9821cac6f2cSLuiz Capitulino 9831cac6f2cSLuiz Capitulino #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \ 9841cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 9851cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 9861cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ 9871cac6f2cSLuiz Capitulino nr_nodes--) 9881cac6f2cSLuiz Capitulino 9891cac6f2cSLuiz Capitulino #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ 9901cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 9911cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 9921cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_free(hs, mask)) || 1); \ 9931cac6f2cSLuiz Capitulino nr_nodes--) 9941cac6f2cSLuiz Capitulino 995944d9fecSLuiz Capitulino #if defined(CONFIG_CMA) && defined(CONFIG_X86_64) 996944d9fecSLuiz Capitulino static void destroy_compound_gigantic_page(struct page *page, 997944d9fecSLuiz Capitulino unsigned long order) 998944d9fecSLuiz Capitulino { 999944d9fecSLuiz Capitulino int i; 1000944d9fecSLuiz Capitulino int nr_pages = 1 << order; 1001944d9fecSLuiz Capitulino struct page *p = page + 1; 1002944d9fecSLuiz Capitulino 1003944d9fecSLuiz Capitulino for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 1004944d9fecSLuiz Capitulino __ClearPageTail(p); 1005944d9fecSLuiz Capitulino set_page_refcounted(p); 1006944d9fecSLuiz Capitulino p->first_page = NULL; 1007944d9fecSLuiz Capitulino } 1008944d9fecSLuiz Capitulino 1009944d9fecSLuiz Capitulino set_compound_order(page, 0); 1010944d9fecSLuiz Capitulino __ClearPageHead(page); 1011944d9fecSLuiz Capitulino } 1012944d9fecSLuiz Capitulino 1013944d9fecSLuiz Capitulino static void free_gigantic_page(struct page *page, unsigned order) 1014944d9fecSLuiz Capitulino { 1015944d9fecSLuiz Capitulino free_contig_range(page_to_pfn(page), 1 << order); 1016944d9fecSLuiz Capitulino } 1017944d9fecSLuiz Capitulino 1018944d9fecSLuiz Capitulino static int __alloc_gigantic_page(unsigned long start_pfn, 1019944d9fecSLuiz Capitulino unsigned long nr_pages) 1020944d9fecSLuiz Capitulino { 1021944d9fecSLuiz Capitulino unsigned long end_pfn = start_pfn + nr_pages; 1022944d9fecSLuiz Capitulino return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 1023944d9fecSLuiz Capitulino } 1024944d9fecSLuiz Capitulino 1025944d9fecSLuiz Capitulino static bool pfn_range_valid_gigantic(unsigned long start_pfn, 1026944d9fecSLuiz Capitulino unsigned long nr_pages) 1027944d9fecSLuiz Capitulino { 1028944d9fecSLuiz Capitulino unsigned long i, end_pfn = start_pfn + nr_pages; 1029944d9fecSLuiz Capitulino struct page *page; 1030944d9fecSLuiz Capitulino 1031944d9fecSLuiz Capitulino for (i = start_pfn; i < end_pfn; i++) { 1032944d9fecSLuiz Capitulino if (!pfn_valid(i)) 1033944d9fecSLuiz Capitulino return false; 1034944d9fecSLuiz Capitulino 1035944d9fecSLuiz Capitulino page = pfn_to_page(i); 1036944d9fecSLuiz Capitulino 1037944d9fecSLuiz Capitulino if (PageReserved(page)) 1038944d9fecSLuiz Capitulino return false; 1039944d9fecSLuiz Capitulino 1040944d9fecSLuiz Capitulino if (page_count(page) > 0) 1041944d9fecSLuiz Capitulino return false; 1042944d9fecSLuiz Capitulino 1043944d9fecSLuiz Capitulino if (PageHuge(page)) 1044944d9fecSLuiz Capitulino return false; 1045944d9fecSLuiz Capitulino } 1046944d9fecSLuiz Capitulino 1047944d9fecSLuiz Capitulino return true; 1048944d9fecSLuiz Capitulino } 1049944d9fecSLuiz Capitulino 1050944d9fecSLuiz Capitulino static bool zone_spans_last_pfn(const struct zone *zone, 1051944d9fecSLuiz Capitulino unsigned long start_pfn, unsigned long nr_pages) 1052944d9fecSLuiz Capitulino { 1053944d9fecSLuiz Capitulino unsigned long last_pfn = start_pfn + nr_pages - 1; 1054944d9fecSLuiz Capitulino return zone_spans_pfn(zone, last_pfn); 1055944d9fecSLuiz Capitulino } 1056944d9fecSLuiz Capitulino 1057944d9fecSLuiz Capitulino static struct page *alloc_gigantic_page(int nid, unsigned order) 1058944d9fecSLuiz Capitulino { 1059944d9fecSLuiz Capitulino unsigned long nr_pages = 1 << order; 1060944d9fecSLuiz Capitulino unsigned long ret, pfn, flags; 1061944d9fecSLuiz Capitulino struct zone *z; 1062944d9fecSLuiz Capitulino 1063944d9fecSLuiz Capitulino z = NODE_DATA(nid)->node_zones; 1064944d9fecSLuiz Capitulino for (; z - NODE_DATA(nid)->node_zones < MAX_NR_ZONES; z++) { 1065944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 1066944d9fecSLuiz Capitulino 1067944d9fecSLuiz Capitulino pfn = ALIGN(z->zone_start_pfn, nr_pages); 1068944d9fecSLuiz Capitulino while (zone_spans_last_pfn(z, pfn, nr_pages)) { 1069944d9fecSLuiz Capitulino if (pfn_range_valid_gigantic(pfn, nr_pages)) { 1070944d9fecSLuiz Capitulino /* 1071944d9fecSLuiz Capitulino * We release the zone lock here because 1072944d9fecSLuiz Capitulino * alloc_contig_range() will also lock the zone 1073944d9fecSLuiz Capitulino * at some point. If there's an allocation 1074944d9fecSLuiz Capitulino * spinning on this lock, it may win the race 1075944d9fecSLuiz Capitulino * and cause alloc_contig_range() to fail... 1076944d9fecSLuiz Capitulino */ 1077944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 1078944d9fecSLuiz Capitulino ret = __alloc_gigantic_page(pfn, nr_pages); 1079944d9fecSLuiz Capitulino if (!ret) 1080944d9fecSLuiz Capitulino return pfn_to_page(pfn); 1081944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 1082944d9fecSLuiz Capitulino } 1083944d9fecSLuiz Capitulino pfn += nr_pages; 1084944d9fecSLuiz Capitulino } 1085944d9fecSLuiz Capitulino 1086944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 1087944d9fecSLuiz Capitulino } 1088944d9fecSLuiz Capitulino 1089944d9fecSLuiz Capitulino return NULL; 1090944d9fecSLuiz Capitulino } 1091944d9fecSLuiz Capitulino 1092944d9fecSLuiz Capitulino static void prep_new_huge_page(struct hstate *h, struct page *page, int nid); 1093944d9fecSLuiz Capitulino static void prep_compound_gigantic_page(struct page *page, unsigned long order); 1094944d9fecSLuiz Capitulino 1095944d9fecSLuiz Capitulino static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid) 1096944d9fecSLuiz Capitulino { 1097944d9fecSLuiz Capitulino struct page *page; 1098944d9fecSLuiz Capitulino 1099944d9fecSLuiz Capitulino page = alloc_gigantic_page(nid, huge_page_order(h)); 1100944d9fecSLuiz Capitulino if (page) { 1101944d9fecSLuiz Capitulino prep_compound_gigantic_page(page, huge_page_order(h)); 1102944d9fecSLuiz Capitulino prep_new_huge_page(h, page, nid); 1103944d9fecSLuiz Capitulino } 1104944d9fecSLuiz Capitulino 1105944d9fecSLuiz Capitulino return page; 1106944d9fecSLuiz Capitulino } 1107944d9fecSLuiz Capitulino 1108944d9fecSLuiz Capitulino static int alloc_fresh_gigantic_page(struct hstate *h, 1109944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) 1110944d9fecSLuiz Capitulino { 1111944d9fecSLuiz Capitulino struct page *page = NULL; 1112944d9fecSLuiz Capitulino int nr_nodes, node; 1113944d9fecSLuiz Capitulino 1114944d9fecSLuiz Capitulino for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1115944d9fecSLuiz Capitulino page = alloc_fresh_gigantic_page_node(h, node); 1116944d9fecSLuiz Capitulino if (page) 1117944d9fecSLuiz Capitulino return 1; 1118944d9fecSLuiz Capitulino } 1119944d9fecSLuiz Capitulino 1120944d9fecSLuiz Capitulino return 0; 1121944d9fecSLuiz Capitulino } 1122944d9fecSLuiz Capitulino 1123944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return true; } 1124944d9fecSLuiz Capitulino #else 1125944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return false; } 1126944d9fecSLuiz Capitulino static inline void free_gigantic_page(struct page *page, unsigned order) { } 1127944d9fecSLuiz Capitulino static inline void destroy_compound_gigantic_page(struct page *page, 1128944d9fecSLuiz Capitulino unsigned long order) { } 1129944d9fecSLuiz Capitulino static inline int alloc_fresh_gigantic_page(struct hstate *h, 1130944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) { return 0; } 1131944d9fecSLuiz Capitulino #endif 1132944d9fecSLuiz Capitulino 1133a5516438SAndi Kleen static void update_and_free_page(struct hstate *h, struct page *page) 11346af2acb6SAdam Litke { 11356af2acb6SAdam Litke int i; 1136a5516438SAndi Kleen 1137944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 1138944d9fecSLuiz Capitulino return; 113918229df5SAndy Whitcroft 1140a5516438SAndi Kleen h->nr_huge_pages--; 1141a5516438SAndi Kleen h->nr_huge_pages_node[page_to_nid(page)]--; 1142a5516438SAndi Kleen for (i = 0; i < pages_per_huge_page(h); i++) { 114332f84528SChris Forbes page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 114432f84528SChris Forbes 1 << PG_referenced | 1 << PG_dirty | 1145a7407a27SLuiz Capitulino 1 << PG_active | 1 << PG_private | 1146a7407a27SLuiz Capitulino 1 << PG_writeback); 11476af2acb6SAdam Litke } 1148309381feSSasha Levin VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page); 11496af2acb6SAdam Litke set_compound_page_dtor(page, NULL); 11506af2acb6SAdam Litke set_page_refcounted(page); 1151944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) { 1152944d9fecSLuiz Capitulino destroy_compound_gigantic_page(page, huge_page_order(h)); 1153944d9fecSLuiz Capitulino free_gigantic_page(page, huge_page_order(h)); 1154944d9fecSLuiz Capitulino } else { 1155a5516438SAndi Kleen __free_pages(page, huge_page_order(h)); 11566af2acb6SAdam Litke } 1157944d9fecSLuiz Capitulino } 11586af2acb6SAdam Litke 1159e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size) 1160e5ff2159SAndi Kleen { 1161e5ff2159SAndi Kleen struct hstate *h; 1162e5ff2159SAndi Kleen 1163e5ff2159SAndi Kleen for_each_hstate(h) { 1164e5ff2159SAndi Kleen if (huge_page_size(h) == size) 1165e5ff2159SAndi Kleen return h; 1166e5ff2159SAndi Kleen } 1167e5ff2159SAndi Kleen return NULL; 1168e5ff2159SAndi Kleen } 1169e5ff2159SAndi Kleen 1170bcc54222SNaoya Horiguchi /* 1171bcc54222SNaoya Horiguchi * Test to determine whether the hugepage is "active/in-use" (i.e. being linked 1172bcc54222SNaoya Horiguchi * to hstate->hugepage_activelist.) 1173bcc54222SNaoya Horiguchi * 1174bcc54222SNaoya Horiguchi * This function can be called for tail pages, but never returns true for them. 1175bcc54222SNaoya Horiguchi */ 1176bcc54222SNaoya Horiguchi bool page_huge_active(struct page *page) 1177bcc54222SNaoya Horiguchi { 1178bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHuge(page), page); 1179bcc54222SNaoya Horiguchi return PageHead(page) && PagePrivate(&page[1]); 1180bcc54222SNaoya Horiguchi } 1181bcc54222SNaoya Horiguchi 1182bcc54222SNaoya Horiguchi /* never called for tail page */ 1183bcc54222SNaoya Horiguchi static void set_page_huge_active(struct page *page) 1184bcc54222SNaoya Horiguchi { 1185bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 1186bcc54222SNaoya Horiguchi SetPagePrivate(&page[1]); 1187bcc54222SNaoya Horiguchi } 1188bcc54222SNaoya Horiguchi 1189bcc54222SNaoya Horiguchi static void clear_page_huge_active(struct page *page) 1190bcc54222SNaoya Horiguchi { 1191bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 1192bcc54222SNaoya Horiguchi ClearPagePrivate(&page[1]); 1193bcc54222SNaoya Horiguchi } 1194bcc54222SNaoya Horiguchi 11958f1d26d0SAtsushi Kumagai void free_huge_page(struct page *page) 119627a85ef1SDavid Gibson { 1197a5516438SAndi Kleen /* 1198a5516438SAndi Kleen * Can't pass hstate in here because it is called from the 1199a5516438SAndi Kleen * compound page destructor. 1200a5516438SAndi Kleen */ 1201e5ff2159SAndi Kleen struct hstate *h = page_hstate(page); 12027893d1d5SAdam Litke int nid = page_to_nid(page); 120390481622SDavid Gibson struct hugepage_subpool *spool = 120490481622SDavid Gibson (struct hugepage_subpool *)page_private(page); 120507443a85SJoonsoo Kim bool restore_reserve; 120627a85ef1SDavid Gibson 1207e5df70abSAndy Whitcroft set_page_private(page, 0); 120823be7468SMel Gorman page->mapping = NULL; 12097893d1d5SAdam Litke BUG_ON(page_count(page)); 12100fe6e20bSNaoya Horiguchi BUG_ON(page_mapcount(page)); 121107443a85SJoonsoo Kim restore_reserve = PagePrivate(page); 121216c794b4SJoonsoo Kim ClearPagePrivate(page); 121327a85ef1SDavid Gibson 12141c5ecae3SMike Kravetz /* 12151c5ecae3SMike Kravetz * A return code of zero implies that the subpool will be under its 12161c5ecae3SMike Kravetz * minimum size if the reservation is not restored after page is free. 12171c5ecae3SMike Kravetz * Therefore, force restore_reserve operation. 12181c5ecae3SMike Kravetz */ 12191c5ecae3SMike Kravetz if (hugepage_subpool_put_pages(spool, 1) == 0) 12201c5ecae3SMike Kravetz restore_reserve = true; 12211c5ecae3SMike Kravetz 122227a85ef1SDavid Gibson spin_lock(&hugetlb_lock); 1223bcc54222SNaoya Horiguchi clear_page_huge_active(page); 12246d76dcf4SAneesh Kumar K.V hugetlb_cgroup_uncharge_page(hstate_index(h), 12256d76dcf4SAneesh Kumar K.V pages_per_huge_page(h), page); 122607443a85SJoonsoo Kim if (restore_reserve) 122707443a85SJoonsoo Kim h->resv_huge_pages++; 122807443a85SJoonsoo Kim 1229944d9fecSLuiz Capitulino if (h->surplus_huge_pages_node[nid]) { 12300edaecfaSAneesh Kumar K.V /* remove the page from active list */ 12310edaecfaSAneesh Kumar K.V list_del(&page->lru); 1232a5516438SAndi Kleen update_and_free_page(h, page); 1233a5516438SAndi Kleen h->surplus_huge_pages--; 1234a5516438SAndi Kleen h->surplus_huge_pages_node[nid]--; 12357893d1d5SAdam Litke } else { 12365d3a551cSWill Deacon arch_clear_hugepage_flags(page); 1237a5516438SAndi Kleen enqueue_huge_page(h, page); 12387893d1d5SAdam Litke } 123927a85ef1SDavid Gibson spin_unlock(&hugetlb_lock); 124027a85ef1SDavid Gibson } 124127a85ef1SDavid Gibson 1242a5516438SAndi Kleen static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) 1243b7ba30c6SAndi Kleen { 12440edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1245b7ba30c6SAndi Kleen set_compound_page_dtor(page, free_huge_page); 1246b7ba30c6SAndi Kleen spin_lock(&hugetlb_lock); 12479dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1248a5516438SAndi Kleen h->nr_huge_pages++; 1249a5516438SAndi Kleen h->nr_huge_pages_node[nid]++; 1250b7ba30c6SAndi Kleen spin_unlock(&hugetlb_lock); 1251b7ba30c6SAndi Kleen put_page(page); /* free it into the hugepage allocator */ 1252b7ba30c6SAndi Kleen } 1253b7ba30c6SAndi Kleen 12542906dd52SLuiz Capitulino static void prep_compound_gigantic_page(struct page *page, unsigned long order) 125520a0307cSWu Fengguang { 125620a0307cSWu Fengguang int i; 125720a0307cSWu Fengguang int nr_pages = 1 << order; 125820a0307cSWu Fengguang struct page *p = page + 1; 125920a0307cSWu Fengguang 126020a0307cSWu Fengguang /* we rely on prep_new_huge_page to set the destructor */ 126120a0307cSWu Fengguang set_compound_order(page, order); 126220a0307cSWu Fengguang __SetPageHead(page); 1263ef5a22beSAndrea Arcangeli __ClearPageReserved(page); 126420a0307cSWu Fengguang for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 1265ef5a22beSAndrea Arcangeli /* 1266ef5a22beSAndrea Arcangeli * For gigantic hugepages allocated through bootmem at 1267ef5a22beSAndrea Arcangeli * boot, it's safer to be consistent with the not-gigantic 1268ef5a22beSAndrea Arcangeli * hugepages and clear the PG_reserved bit from all tail pages 1269ef5a22beSAndrea Arcangeli * too. Otherwse drivers using get_user_pages() to access tail 1270ef5a22beSAndrea Arcangeli * pages may get the reference counting wrong if they see 1271ef5a22beSAndrea Arcangeli * PG_reserved set on a tail page (despite the head page not 1272ef5a22beSAndrea Arcangeli * having PG_reserved set). Enforcing this consistency between 1273ef5a22beSAndrea Arcangeli * head and tail pages allows drivers to optimize away a check 1274ef5a22beSAndrea Arcangeli * on the head page when they need know if put_page() is needed 1275ef5a22beSAndrea Arcangeli * after get_user_pages(). 1276ef5a22beSAndrea Arcangeli */ 1277ef5a22beSAndrea Arcangeli __ClearPageReserved(p); 127858a84aa9SYouquan Song set_page_count(p, 0); 127920a0307cSWu Fengguang p->first_page = page; 128044fc8057SDavid Rientjes /* Make sure p->first_page is always valid for PageTail() */ 128144fc8057SDavid Rientjes smp_wmb(); 128244fc8057SDavid Rientjes __SetPageTail(p); 128320a0307cSWu Fengguang } 128420a0307cSWu Fengguang } 128520a0307cSWu Fengguang 12867795912cSAndrew Morton /* 12877795912cSAndrew Morton * PageHuge() only returns true for hugetlbfs pages, but not for normal or 12887795912cSAndrew Morton * transparent huge pages. See the PageTransHuge() documentation for more 12897795912cSAndrew Morton * details. 12907795912cSAndrew Morton */ 129120a0307cSWu Fengguang int PageHuge(struct page *page) 129220a0307cSWu Fengguang { 129320a0307cSWu Fengguang if (!PageCompound(page)) 129420a0307cSWu Fengguang return 0; 129520a0307cSWu Fengguang 129620a0307cSWu Fengguang page = compound_head(page); 1297758f66a2SAndrew Morton return get_compound_page_dtor(page) == free_huge_page; 129820a0307cSWu Fengguang } 129943131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge); 130043131e14SNaoya Horiguchi 130127c73ae7SAndrea Arcangeli /* 130227c73ae7SAndrea Arcangeli * PageHeadHuge() only returns true for hugetlbfs head page, but not for 130327c73ae7SAndrea Arcangeli * normal or transparent huge pages. 130427c73ae7SAndrea Arcangeli */ 130527c73ae7SAndrea Arcangeli int PageHeadHuge(struct page *page_head) 130627c73ae7SAndrea Arcangeli { 130727c73ae7SAndrea Arcangeli if (!PageHead(page_head)) 130827c73ae7SAndrea Arcangeli return 0; 130927c73ae7SAndrea Arcangeli 1310758f66a2SAndrew Morton return get_compound_page_dtor(page_head) == free_huge_page; 131127c73ae7SAndrea Arcangeli } 131227c73ae7SAndrea Arcangeli 131313d60f4bSZhang Yi pgoff_t __basepage_index(struct page *page) 131413d60f4bSZhang Yi { 131513d60f4bSZhang Yi struct page *page_head = compound_head(page); 131613d60f4bSZhang Yi pgoff_t index = page_index(page_head); 131713d60f4bSZhang Yi unsigned long compound_idx; 131813d60f4bSZhang Yi 131913d60f4bSZhang Yi if (!PageHuge(page_head)) 132013d60f4bSZhang Yi return page_index(page); 132113d60f4bSZhang Yi 132213d60f4bSZhang Yi if (compound_order(page_head) >= MAX_ORDER) 132313d60f4bSZhang Yi compound_idx = page_to_pfn(page) - page_to_pfn(page_head); 132413d60f4bSZhang Yi else 132513d60f4bSZhang Yi compound_idx = page - page_head; 132613d60f4bSZhang Yi 132713d60f4bSZhang Yi return (index << compound_order(page_head)) + compound_idx; 132813d60f4bSZhang Yi } 132913d60f4bSZhang Yi 1330a5516438SAndi Kleen static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid) 13311da177e4SLinus Torvalds { 13321da177e4SLinus Torvalds struct page *page; 1333f96efd58SJoe Jin 13346484eb3eSMel Gorman page = alloc_pages_exact_node(nid, 133586cdb465SNaoya Horiguchi htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| 1336551883aeSNishanth Aravamudan __GFP_REPEAT|__GFP_NOWARN, 1337a5516438SAndi Kleen huge_page_order(h)); 13381da177e4SLinus Torvalds if (page) { 1339a5516438SAndi Kleen prep_new_huge_page(h, page, nid); 13401da177e4SLinus Torvalds } 134163b4613cSNishanth Aravamudan 134263b4613cSNishanth Aravamudan return page; 134363b4613cSNishanth Aravamudan } 134463b4613cSNishanth Aravamudan 1345b2261026SJoonsoo Kim static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed) 1346b2261026SJoonsoo Kim { 1347b2261026SJoonsoo Kim struct page *page; 1348b2261026SJoonsoo Kim int nr_nodes, node; 1349b2261026SJoonsoo Kim int ret = 0; 1350b2261026SJoonsoo Kim 1351b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1352b2261026SJoonsoo Kim page = alloc_fresh_huge_page_node(h, node); 1353b2261026SJoonsoo Kim if (page) { 1354b2261026SJoonsoo Kim ret = 1; 1355b2261026SJoonsoo Kim break; 1356b2261026SJoonsoo Kim } 1357b2261026SJoonsoo Kim } 1358b2261026SJoonsoo Kim 1359b2261026SJoonsoo Kim if (ret) 1360b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC); 1361b2261026SJoonsoo Kim else 1362b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 1363b2261026SJoonsoo Kim 1364b2261026SJoonsoo Kim return ret; 1365b2261026SJoonsoo Kim } 1366b2261026SJoonsoo Kim 1367e8c5c824SLee Schermerhorn /* 1368e8c5c824SLee Schermerhorn * Free huge page from pool from next node to free. 1369e8c5c824SLee Schermerhorn * Attempt to keep persistent huge pages more or less 1370e8c5c824SLee Schermerhorn * balanced over allowed nodes. 1371e8c5c824SLee Schermerhorn * Called with hugetlb_lock locked. 1372e8c5c824SLee Schermerhorn */ 13736ae11b27SLee Schermerhorn static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, 13746ae11b27SLee Schermerhorn bool acct_surplus) 1375e8c5c824SLee Schermerhorn { 1376b2261026SJoonsoo Kim int nr_nodes, node; 1377e8c5c824SLee Schermerhorn int ret = 0; 1378e8c5c824SLee Schermerhorn 1379b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1380685f3457SLee Schermerhorn /* 1381685f3457SLee Schermerhorn * If we're returning unused surplus pages, only examine 1382685f3457SLee Schermerhorn * nodes with surplus pages. 1383685f3457SLee Schermerhorn */ 1384b2261026SJoonsoo Kim if ((!acct_surplus || h->surplus_huge_pages_node[node]) && 1385b2261026SJoonsoo Kim !list_empty(&h->hugepage_freelists[node])) { 1386e8c5c824SLee Schermerhorn struct page *page = 1387b2261026SJoonsoo Kim list_entry(h->hugepage_freelists[node].next, 1388e8c5c824SLee Schermerhorn struct page, lru); 1389e8c5c824SLee Schermerhorn list_del(&page->lru); 1390e8c5c824SLee Schermerhorn h->free_huge_pages--; 1391b2261026SJoonsoo Kim h->free_huge_pages_node[node]--; 1392685f3457SLee Schermerhorn if (acct_surplus) { 1393685f3457SLee Schermerhorn h->surplus_huge_pages--; 1394b2261026SJoonsoo Kim h->surplus_huge_pages_node[node]--; 1395685f3457SLee Schermerhorn } 1396e8c5c824SLee Schermerhorn update_and_free_page(h, page); 1397e8c5c824SLee Schermerhorn ret = 1; 13989a76db09SLee Schermerhorn break; 1399e8c5c824SLee Schermerhorn } 1400b2261026SJoonsoo Kim } 1401e8c5c824SLee Schermerhorn 1402e8c5c824SLee Schermerhorn return ret; 1403e8c5c824SLee Schermerhorn } 1404e8c5c824SLee Schermerhorn 1405c8721bbbSNaoya Horiguchi /* 1406c8721bbbSNaoya Horiguchi * Dissolve a given free hugepage into free buddy pages. This function does 1407c8721bbbSNaoya Horiguchi * nothing for in-use (including surplus) hugepages. 1408c8721bbbSNaoya Horiguchi */ 1409c8721bbbSNaoya Horiguchi static void dissolve_free_huge_page(struct page *page) 1410c8721bbbSNaoya Horiguchi { 1411c8721bbbSNaoya Horiguchi spin_lock(&hugetlb_lock); 1412c8721bbbSNaoya Horiguchi if (PageHuge(page) && !page_count(page)) { 1413c8721bbbSNaoya Horiguchi struct hstate *h = page_hstate(page); 1414c8721bbbSNaoya Horiguchi int nid = page_to_nid(page); 1415c8721bbbSNaoya Horiguchi list_del(&page->lru); 1416c8721bbbSNaoya Horiguchi h->free_huge_pages--; 1417c8721bbbSNaoya Horiguchi h->free_huge_pages_node[nid]--; 1418c8721bbbSNaoya Horiguchi update_and_free_page(h, page); 1419c8721bbbSNaoya Horiguchi } 1420c8721bbbSNaoya Horiguchi spin_unlock(&hugetlb_lock); 1421c8721bbbSNaoya Horiguchi } 1422c8721bbbSNaoya Horiguchi 1423c8721bbbSNaoya Horiguchi /* 1424c8721bbbSNaoya Horiguchi * Dissolve free hugepages in a given pfn range. Used by memory hotplug to 1425c8721bbbSNaoya Horiguchi * make specified memory blocks removable from the system. 1426c8721bbbSNaoya Horiguchi * Note that start_pfn should aligned with (minimum) hugepage size. 1427c8721bbbSNaoya Horiguchi */ 1428c8721bbbSNaoya Horiguchi void dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) 1429c8721bbbSNaoya Horiguchi { 1430c8721bbbSNaoya Horiguchi unsigned long pfn; 1431c8721bbbSNaoya Horiguchi 1432d0177639SLi Zhong if (!hugepages_supported()) 1433d0177639SLi Zhong return; 1434d0177639SLi Zhong 1435641844f5SNaoya Horiguchi VM_BUG_ON(!IS_ALIGNED(start_pfn, 1 << minimum_order)); 1436641844f5SNaoya Horiguchi for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) 1437c8721bbbSNaoya Horiguchi dissolve_free_huge_page(pfn_to_page(pfn)); 1438c8721bbbSNaoya Horiguchi } 1439c8721bbbSNaoya Horiguchi 1440bf50bab2SNaoya Horiguchi static struct page *alloc_buddy_huge_page(struct hstate *h, int nid) 14417893d1d5SAdam Litke { 14427893d1d5SAdam Litke struct page *page; 1443bf50bab2SNaoya Horiguchi unsigned int r_nid; 14447893d1d5SAdam Litke 1445bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1446aa888a74SAndi Kleen return NULL; 1447aa888a74SAndi Kleen 1448d1c3fb1fSNishanth Aravamudan /* 1449d1c3fb1fSNishanth Aravamudan * Assume we will successfully allocate the surplus page to 1450d1c3fb1fSNishanth Aravamudan * prevent racing processes from causing the surplus to exceed 1451d1c3fb1fSNishanth Aravamudan * overcommit 1452d1c3fb1fSNishanth Aravamudan * 1453d1c3fb1fSNishanth Aravamudan * This however introduces a different race, where a process B 1454d1c3fb1fSNishanth Aravamudan * tries to grow the static hugepage pool while alloc_pages() is 1455d1c3fb1fSNishanth Aravamudan * called by process A. B will only examine the per-node 1456d1c3fb1fSNishanth Aravamudan * counters in determining if surplus huge pages can be 1457d1c3fb1fSNishanth Aravamudan * converted to normal huge pages in adjust_pool_surplus(). A 1458d1c3fb1fSNishanth Aravamudan * won't be able to increment the per-node counter, until the 1459d1c3fb1fSNishanth Aravamudan * lock is dropped by B, but B doesn't drop hugetlb_lock until 1460d1c3fb1fSNishanth Aravamudan * no more huge pages can be converted from surplus to normal 1461d1c3fb1fSNishanth Aravamudan * state (and doesn't try to convert again). Thus, we have a 1462d1c3fb1fSNishanth Aravamudan * case where a surplus huge page exists, the pool is grown, and 1463d1c3fb1fSNishanth Aravamudan * the surplus huge page still exists after, even though it 1464d1c3fb1fSNishanth Aravamudan * should just have been converted to a normal huge page. This 1465d1c3fb1fSNishanth Aravamudan * does not leak memory, though, as the hugepage will be freed 1466d1c3fb1fSNishanth Aravamudan * once it is out of use. It also does not allow the counters to 1467d1c3fb1fSNishanth Aravamudan * go out of whack in adjust_pool_surplus() as we don't modify 1468d1c3fb1fSNishanth Aravamudan * the node values until we've gotten the hugepage and only the 1469d1c3fb1fSNishanth Aravamudan * per-node value is checked there. 1470d1c3fb1fSNishanth Aravamudan */ 1471d1c3fb1fSNishanth Aravamudan spin_lock(&hugetlb_lock); 1472a5516438SAndi Kleen if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { 1473d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1474d1c3fb1fSNishanth Aravamudan return NULL; 1475d1c3fb1fSNishanth Aravamudan } else { 1476a5516438SAndi Kleen h->nr_huge_pages++; 1477a5516438SAndi Kleen h->surplus_huge_pages++; 1478d1c3fb1fSNishanth Aravamudan } 1479d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1480d1c3fb1fSNishanth Aravamudan 1481bf50bab2SNaoya Horiguchi if (nid == NUMA_NO_NODE) 148286cdb465SNaoya Horiguchi page = alloc_pages(htlb_alloc_mask(h)|__GFP_COMP| 1483551883aeSNishanth Aravamudan __GFP_REPEAT|__GFP_NOWARN, 1484a5516438SAndi Kleen huge_page_order(h)); 1485bf50bab2SNaoya Horiguchi else 1486bf50bab2SNaoya Horiguchi page = alloc_pages_exact_node(nid, 148786cdb465SNaoya Horiguchi htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| 1488bf50bab2SNaoya Horiguchi __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h)); 1489d1c3fb1fSNishanth Aravamudan 14907893d1d5SAdam Litke spin_lock(&hugetlb_lock); 1491d1c3fb1fSNishanth Aravamudan if (page) { 14920edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1493bf50bab2SNaoya Horiguchi r_nid = page_to_nid(page); 1494d1c3fb1fSNishanth Aravamudan set_compound_page_dtor(page, free_huge_page); 14959dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1496d1c3fb1fSNishanth Aravamudan /* 1497d1c3fb1fSNishanth Aravamudan * We incremented the global counters already 1498d1c3fb1fSNishanth Aravamudan */ 1499bf50bab2SNaoya Horiguchi h->nr_huge_pages_node[r_nid]++; 1500bf50bab2SNaoya Horiguchi h->surplus_huge_pages_node[r_nid]++; 15013b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC); 1502d1c3fb1fSNishanth Aravamudan } else { 1503a5516438SAndi Kleen h->nr_huge_pages--; 1504a5516438SAndi Kleen h->surplus_huge_pages--; 15053b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 15067893d1d5SAdam Litke } 1507d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 15087893d1d5SAdam Litke 15097893d1d5SAdam Litke return page; 15107893d1d5SAdam Litke } 15117893d1d5SAdam Litke 1512e4e574b7SAdam Litke /* 1513bf50bab2SNaoya Horiguchi * This allocation function is useful in the context where vma is irrelevant. 1514bf50bab2SNaoya Horiguchi * E.g. soft-offlining uses this function because it only cares physical 1515bf50bab2SNaoya Horiguchi * address of error page. 1516bf50bab2SNaoya Horiguchi */ 1517bf50bab2SNaoya Horiguchi struct page *alloc_huge_page_node(struct hstate *h, int nid) 1518bf50bab2SNaoya Horiguchi { 15194ef91848SJoonsoo Kim struct page *page = NULL; 1520bf50bab2SNaoya Horiguchi 1521bf50bab2SNaoya Horiguchi spin_lock(&hugetlb_lock); 15224ef91848SJoonsoo Kim if (h->free_huge_pages - h->resv_huge_pages > 0) 1523bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, nid); 1524bf50bab2SNaoya Horiguchi spin_unlock(&hugetlb_lock); 1525bf50bab2SNaoya Horiguchi 152694ae8ba7SAneesh Kumar K.V if (!page) 1527bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, nid); 1528bf50bab2SNaoya Horiguchi 1529bf50bab2SNaoya Horiguchi return page; 1530bf50bab2SNaoya Horiguchi } 1531bf50bab2SNaoya Horiguchi 1532bf50bab2SNaoya Horiguchi /* 153325985edcSLucas De Marchi * Increase the hugetlb pool such that it can accommodate a reservation 1534e4e574b7SAdam Litke * of size 'delta'. 1535e4e574b7SAdam Litke */ 1536a5516438SAndi Kleen static int gather_surplus_pages(struct hstate *h, int delta) 1537e4e574b7SAdam Litke { 1538e4e574b7SAdam Litke struct list_head surplus_list; 1539e4e574b7SAdam Litke struct page *page, *tmp; 1540e4e574b7SAdam Litke int ret, i; 1541e4e574b7SAdam Litke int needed, allocated; 154228073b02SHillf Danton bool alloc_ok = true; 1543e4e574b7SAdam Litke 1544a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - h->free_huge_pages; 1545ac09b3a1SAdam Litke if (needed <= 0) { 1546a5516438SAndi Kleen h->resv_huge_pages += delta; 1547e4e574b7SAdam Litke return 0; 1548ac09b3a1SAdam Litke } 1549e4e574b7SAdam Litke 1550e4e574b7SAdam Litke allocated = 0; 1551e4e574b7SAdam Litke INIT_LIST_HEAD(&surplus_list); 1552e4e574b7SAdam Litke 1553e4e574b7SAdam Litke ret = -ENOMEM; 1554e4e574b7SAdam Litke retry: 1555e4e574b7SAdam Litke spin_unlock(&hugetlb_lock); 1556e4e574b7SAdam Litke for (i = 0; i < needed; i++) { 1557bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, NUMA_NO_NODE); 155828073b02SHillf Danton if (!page) { 155928073b02SHillf Danton alloc_ok = false; 156028073b02SHillf Danton break; 156128073b02SHillf Danton } 1562e4e574b7SAdam Litke list_add(&page->lru, &surplus_list); 1563e4e574b7SAdam Litke } 156428073b02SHillf Danton allocated += i; 1565e4e574b7SAdam Litke 1566e4e574b7SAdam Litke /* 1567e4e574b7SAdam Litke * After retaking hugetlb_lock, we need to recalculate 'needed' 1568e4e574b7SAdam Litke * because either resv_huge_pages or free_huge_pages may have changed. 1569e4e574b7SAdam Litke */ 1570e4e574b7SAdam Litke spin_lock(&hugetlb_lock); 1571a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - 1572a5516438SAndi Kleen (h->free_huge_pages + allocated); 157328073b02SHillf Danton if (needed > 0) { 157428073b02SHillf Danton if (alloc_ok) 1575e4e574b7SAdam Litke goto retry; 157628073b02SHillf Danton /* 157728073b02SHillf Danton * We were not able to allocate enough pages to 157828073b02SHillf Danton * satisfy the entire reservation so we free what 157928073b02SHillf Danton * we've allocated so far. 158028073b02SHillf Danton */ 158128073b02SHillf Danton goto free; 158228073b02SHillf Danton } 1583e4e574b7SAdam Litke /* 1584e4e574b7SAdam Litke * The surplus_list now contains _at_least_ the number of extra pages 158525985edcSLucas De Marchi * needed to accommodate the reservation. Add the appropriate number 1586e4e574b7SAdam Litke * of pages to the hugetlb pool and free the extras back to the buddy 1587ac09b3a1SAdam Litke * allocator. Commit the entire reservation here to prevent another 1588ac09b3a1SAdam Litke * process from stealing the pages as they are added to the pool but 1589ac09b3a1SAdam Litke * before they are reserved. 1590e4e574b7SAdam Litke */ 1591e4e574b7SAdam Litke needed += allocated; 1592a5516438SAndi Kleen h->resv_huge_pages += delta; 1593e4e574b7SAdam Litke ret = 0; 1594a9869b83SNaoya Horiguchi 159519fc3f0aSAdam Litke /* Free the needed pages to the hugetlb pool */ 159619fc3f0aSAdam Litke list_for_each_entry_safe(page, tmp, &surplus_list, lru) { 159719fc3f0aSAdam Litke if ((--needed) < 0) 159819fc3f0aSAdam Litke break; 1599a9869b83SNaoya Horiguchi /* 1600a9869b83SNaoya Horiguchi * This page is now managed by the hugetlb allocator and has 1601a9869b83SNaoya Horiguchi * no users -- drop the buddy allocator's reference. 1602a9869b83SNaoya Horiguchi */ 1603a9869b83SNaoya Horiguchi put_page_testzero(page); 1604309381feSSasha Levin VM_BUG_ON_PAGE(page_count(page), page); 1605a5516438SAndi Kleen enqueue_huge_page(h, page); 160619fc3f0aSAdam Litke } 160728073b02SHillf Danton free: 1608b0365c8dSHillf Danton spin_unlock(&hugetlb_lock); 160919fc3f0aSAdam Litke 161019fc3f0aSAdam Litke /* Free unnecessary surplus pages to the buddy allocator */ 1611c0d934baSJoonsoo Kim list_for_each_entry_safe(page, tmp, &surplus_list, lru) 1612a9869b83SNaoya Horiguchi put_page(page); 161319fc3f0aSAdam Litke spin_lock(&hugetlb_lock); 1614e4e574b7SAdam Litke 1615e4e574b7SAdam Litke return ret; 1616e4e574b7SAdam Litke } 1617e4e574b7SAdam Litke 1618e4e574b7SAdam Litke /* 1619e4e574b7SAdam Litke * When releasing a hugetlb pool reservation, any surplus pages that were 1620e4e574b7SAdam Litke * allocated to satisfy the reservation must be explicitly freed if they were 1621e4e574b7SAdam Litke * never used. 1622685f3457SLee Schermerhorn * Called with hugetlb_lock held. 1623e4e574b7SAdam Litke */ 1624a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h, 1625a5516438SAndi Kleen unsigned long unused_resv_pages) 1626e4e574b7SAdam Litke { 1627e4e574b7SAdam Litke unsigned long nr_pages; 1628e4e574b7SAdam Litke 1629ac09b3a1SAdam Litke /* Uncommit the reservation */ 1630a5516438SAndi Kleen h->resv_huge_pages -= unused_resv_pages; 1631ac09b3a1SAdam Litke 1632aa888a74SAndi Kleen /* Cannot return gigantic pages currently */ 1633bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1634aa888a74SAndi Kleen return; 1635aa888a74SAndi Kleen 1636a5516438SAndi Kleen nr_pages = min(unused_resv_pages, h->surplus_huge_pages); 1637e4e574b7SAdam Litke 1638685f3457SLee Schermerhorn /* 1639685f3457SLee Schermerhorn * We want to release as many surplus pages as possible, spread 16409b5e5d0fSLee Schermerhorn * evenly across all nodes with memory. Iterate across these nodes 16419b5e5d0fSLee Schermerhorn * until we can no longer free unreserved surplus pages. This occurs 16429b5e5d0fSLee Schermerhorn * when the nodes with surplus pages have no free pages. 16439b5e5d0fSLee Schermerhorn * free_pool_huge_page() will balance the the freed pages across the 16449b5e5d0fSLee Schermerhorn * on-line nodes with memory and will handle the hstate accounting. 1645685f3457SLee Schermerhorn */ 1646685f3457SLee Schermerhorn while (nr_pages--) { 16478cebfcd0SLai Jiangshan if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1)) 1648685f3457SLee Schermerhorn break; 16497848a4bfSMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 1650e4e574b7SAdam Litke } 1651e4e574b7SAdam Litke } 1652e4e574b7SAdam Litke 16535e911373SMike Kravetz 1654c37f9fb1SAndy Whitcroft /* 1655feba16e2SMike Kravetz * vma_needs_reservation, vma_commit_reservation and vma_end_reservation 16565e911373SMike Kravetz * are used by the huge page allocation routines to manage reservations. 1657cf3ad20bSMike Kravetz * 1658cf3ad20bSMike Kravetz * vma_needs_reservation is called to determine if the huge page at addr 1659cf3ad20bSMike Kravetz * within the vma has an associated reservation. If a reservation is 1660cf3ad20bSMike Kravetz * needed, the value 1 is returned. The caller is then responsible for 1661cf3ad20bSMike Kravetz * managing the global reservation and subpool usage counts. After 1662cf3ad20bSMike Kravetz * the huge page has been allocated, vma_commit_reservation is called 1663feba16e2SMike Kravetz * to add the page to the reservation map. If the page allocation fails, 1664feba16e2SMike Kravetz * the reservation must be ended instead of committed. vma_end_reservation 1665feba16e2SMike Kravetz * is called in such cases. 1666cf3ad20bSMike Kravetz * 1667cf3ad20bSMike Kravetz * In the normal case, vma_commit_reservation returns the same value 1668cf3ad20bSMike Kravetz * as the preceding vma_needs_reservation call. The only time this 1669cf3ad20bSMike Kravetz * is not the case is if a reserve map was changed between calls. It 1670cf3ad20bSMike Kravetz * is the responsibility of the caller to notice the difference and 1671cf3ad20bSMike Kravetz * take appropriate action. 1672c37f9fb1SAndy Whitcroft */ 16735e911373SMike Kravetz enum vma_resv_mode { 16745e911373SMike Kravetz VMA_NEEDS_RESV, 16755e911373SMike Kravetz VMA_COMMIT_RESV, 1676feba16e2SMike Kravetz VMA_END_RESV, 16775e911373SMike Kravetz }; 1678cf3ad20bSMike Kravetz static long __vma_reservation_common(struct hstate *h, 1679cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr, 16805e911373SMike Kravetz enum vma_resv_mode mode) 1681c37f9fb1SAndy Whitcroft { 16824e35f483SJoonsoo Kim struct resv_map *resv; 16834e35f483SJoonsoo Kim pgoff_t idx; 1684cf3ad20bSMike Kravetz long ret; 1685c37f9fb1SAndy Whitcroft 16864e35f483SJoonsoo Kim resv = vma_resv_map(vma); 16874e35f483SJoonsoo Kim if (!resv) 1688c37f9fb1SAndy Whitcroft return 1; 1689c37f9fb1SAndy Whitcroft 16904e35f483SJoonsoo Kim idx = vma_hugecache_offset(h, vma, addr); 16915e911373SMike Kravetz switch (mode) { 16925e911373SMike Kravetz case VMA_NEEDS_RESV: 1693cf3ad20bSMike Kravetz ret = region_chg(resv, idx, idx + 1); 16945e911373SMike Kravetz break; 16955e911373SMike Kravetz case VMA_COMMIT_RESV: 16965e911373SMike Kravetz ret = region_add(resv, idx, idx + 1); 16975e911373SMike Kravetz break; 1698feba16e2SMike Kravetz case VMA_END_RESV: 16995e911373SMike Kravetz region_abort(resv, idx, idx + 1); 17005e911373SMike Kravetz ret = 0; 17015e911373SMike Kravetz break; 17025e911373SMike Kravetz default: 17035e911373SMike Kravetz BUG(); 17045e911373SMike Kravetz } 170584afd99bSAndy Whitcroft 17064e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) 1707cf3ad20bSMike Kravetz return ret; 17084e35f483SJoonsoo Kim else 1709cf3ad20bSMike Kravetz return ret < 0 ? ret : 0; 171084afd99bSAndy Whitcroft } 1711cf3ad20bSMike Kravetz 1712cf3ad20bSMike Kravetz static long vma_needs_reservation(struct hstate *h, 1713a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long addr) 1714c37f9fb1SAndy Whitcroft { 17155e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV); 1716cf3ad20bSMike Kravetz } 1717c37f9fb1SAndy Whitcroft 1718cf3ad20bSMike Kravetz static long vma_commit_reservation(struct hstate *h, 1719cf3ad20bSMike Kravetz struct vm_area_struct *vma, unsigned long addr) 1720cf3ad20bSMike Kravetz { 17215e911373SMike Kravetz return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV); 17225e911373SMike Kravetz } 17235e911373SMike Kravetz 1724feba16e2SMike Kravetz static void vma_end_reservation(struct hstate *h, 17255e911373SMike Kravetz struct vm_area_struct *vma, unsigned long addr) 17265e911373SMike Kravetz { 1727feba16e2SMike Kravetz (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV); 1728c37f9fb1SAndy Whitcroft } 1729c37f9fb1SAndy Whitcroft 1730348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma, 173104f2cbe3SMel Gorman unsigned long addr, int avoid_reserve) 1732348ea204SAdam Litke { 173390481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 1734a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 1735348ea204SAdam Litke struct page *page; 173633039678SMike Kravetz long chg, commit; 17376d76dcf4SAneesh Kumar K.V int ret, idx; 17386d76dcf4SAneesh Kumar K.V struct hugetlb_cgroup *h_cg; 17392fc39cecSAdam Litke 17406d76dcf4SAneesh Kumar K.V idx = hstate_index(h); 1741a1e78772SMel Gorman /* 174290481622SDavid Gibson * Processes that did not create the mapping will have no 174390481622SDavid Gibson * reserves and will not have accounted against subpool 174490481622SDavid Gibson * limit. Check that the subpool limit can be made before 174590481622SDavid Gibson * satisfying the allocation MAP_NORESERVE mappings may also 174690481622SDavid Gibson * need pages and subpool limit allocated allocated if no reserve 174790481622SDavid Gibson * mapping overlaps. 1748a1e78772SMel Gorman */ 1749a5516438SAndi Kleen chg = vma_needs_reservation(h, vma, addr); 1750c37f9fb1SAndy Whitcroft if (chg < 0) 175176dcee75SAneesh Kumar K.V return ERR_PTR(-ENOMEM); 17528bb3f12eSJoonsoo Kim if (chg || avoid_reserve) 17535e911373SMike Kravetz if (hugepage_subpool_get_pages(spool, 1) < 0) { 1754feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 175576dcee75SAneesh Kumar K.V return ERR_PTR(-ENOSPC); 17565e911373SMike Kravetz } 175790d8b7e6SAdam Litke 17586d76dcf4SAneesh Kumar K.V ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); 17598f34af6fSJianyu Zhan if (ret) 17608f34af6fSJianyu Zhan goto out_subpool_put; 17618f34af6fSJianyu Zhan 1762a1e78772SMel Gorman spin_lock(&hugetlb_lock); 1763af0ed73eSJoonsoo Kim page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg); 176481a6fcaeSJoonsoo Kim if (!page) { 176594ae8ba7SAneesh Kumar K.V spin_unlock(&hugetlb_lock); 1766bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, NUMA_NO_NODE); 17678f34af6fSJianyu Zhan if (!page) 17688f34af6fSJianyu Zhan goto out_uncharge_cgroup; 17698f34af6fSJianyu Zhan 177079dbb236SAneesh Kumar K.V spin_lock(&hugetlb_lock); 177179dbb236SAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 177281a6fcaeSJoonsoo Kim /* Fall through */ 1773a1e78772SMel Gorman } 177481a6fcaeSJoonsoo Kim hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page); 177581a6fcaeSJoonsoo Kim spin_unlock(&hugetlb_lock); 1776a1e78772SMel Gorman 177790481622SDavid Gibson set_page_private(page, (unsigned long)spool); 1778a1e78772SMel Gorman 177933039678SMike Kravetz commit = vma_commit_reservation(h, vma, addr); 178033039678SMike Kravetz if (unlikely(chg > commit)) { 178133039678SMike Kravetz /* 178233039678SMike Kravetz * The page was added to the reservation map between 178333039678SMike Kravetz * vma_needs_reservation and vma_commit_reservation. 178433039678SMike Kravetz * This indicates a race with hugetlb_reserve_pages. 178533039678SMike Kravetz * Adjust for the subpool count incremented above AND 178633039678SMike Kravetz * in hugetlb_reserve_pages for the same page. Also, 178733039678SMike Kravetz * the reservation count added in hugetlb_reserve_pages 178833039678SMike Kravetz * no longer applies. 178933039678SMike Kravetz */ 179033039678SMike Kravetz long rsv_adjust; 179133039678SMike Kravetz 179233039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 1); 179333039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 179433039678SMike Kravetz } 17957893d1d5SAdam Litke return page; 17968f34af6fSJianyu Zhan 17978f34af6fSJianyu Zhan out_uncharge_cgroup: 17988f34af6fSJianyu Zhan hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); 17998f34af6fSJianyu Zhan out_subpool_put: 18008f34af6fSJianyu Zhan if (chg || avoid_reserve) 18018f34af6fSJianyu Zhan hugepage_subpool_put_pages(spool, 1); 1802feba16e2SMike Kravetz vma_end_reservation(h, vma, addr); 18038f34af6fSJianyu Zhan return ERR_PTR(-ENOSPC); 1804b45b5bd6SDavid Gibson } 1805b45b5bd6SDavid Gibson 180674060e4dSNaoya Horiguchi /* 180774060e4dSNaoya Horiguchi * alloc_huge_page()'s wrapper which simply returns the page if allocation 180874060e4dSNaoya Horiguchi * succeeds, otherwise NULL. This function is called from new_vma_page(), 180974060e4dSNaoya Horiguchi * where no ERR_VALUE is expected to be returned. 181074060e4dSNaoya Horiguchi */ 181174060e4dSNaoya Horiguchi struct page *alloc_huge_page_noerr(struct vm_area_struct *vma, 181274060e4dSNaoya Horiguchi unsigned long addr, int avoid_reserve) 181374060e4dSNaoya Horiguchi { 181474060e4dSNaoya Horiguchi struct page *page = alloc_huge_page(vma, addr, avoid_reserve); 181574060e4dSNaoya Horiguchi if (IS_ERR(page)) 181674060e4dSNaoya Horiguchi page = NULL; 181774060e4dSNaoya Horiguchi return page; 181874060e4dSNaoya Horiguchi } 181974060e4dSNaoya Horiguchi 182091f47662SCyrill Gorcunov int __weak alloc_bootmem_huge_page(struct hstate *h) 1821aa888a74SAndi Kleen { 1822aa888a74SAndi Kleen struct huge_bootmem_page *m; 1823b2261026SJoonsoo Kim int nr_nodes, node; 1824aa888a74SAndi Kleen 1825b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) { 1826aa888a74SAndi Kleen void *addr; 1827aa888a74SAndi Kleen 18288b89a116SGrygorii Strashko addr = memblock_virt_alloc_try_nid_nopanic( 18298b89a116SGrygorii Strashko huge_page_size(h), huge_page_size(h), 18308b89a116SGrygorii Strashko 0, BOOTMEM_ALLOC_ACCESSIBLE, node); 1831aa888a74SAndi Kleen if (addr) { 1832aa888a74SAndi Kleen /* 1833aa888a74SAndi Kleen * Use the beginning of the huge page to store the 1834aa888a74SAndi Kleen * huge_bootmem_page struct (until gather_bootmem 1835aa888a74SAndi Kleen * puts them into the mem_map). 1836aa888a74SAndi Kleen */ 1837aa888a74SAndi Kleen m = addr; 1838aa888a74SAndi Kleen goto found; 1839aa888a74SAndi Kleen } 1840aa888a74SAndi Kleen } 1841aa888a74SAndi Kleen return 0; 1842aa888a74SAndi Kleen 1843aa888a74SAndi Kleen found: 1844df994eadSLuiz Capitulino BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h))); 1845aa888a74SAndi Kleen /* Put them into a private list first because mem_map is not up yet */ 1846aa888a74SAndi Kleen list_add(&m->list, &huge_boot_pages); 1847aa888a74SAndi Kleen m->hstate = h; 1848aa888a74SAndi Kleen return 1; 1849aa888a74SAndi Kleen } 1850aa888a74SAndi Kleen 1851f412c97aSDavid Rientjes static void __init prep_compound_huge_page(struct page *page, int order) 185218229df5SAndy Whitcroft { 185318229df5SAndy Whitcroft if (unlikely(order > (MAX_ORDER - 1))) 185418229df5SAndy Whitcroft prep_compound_gigantic_page(page, order); 185518229df5SAndy Whitcroft else 185618229df5SAndy Whitcroft prep_compound_page(page, order); 185718229df5SAndy Whitcroft } 185818229df5SAndy Whitcroft 1859aa888a74SAndi Kleen /* Put bootmem huge pages into the standard lists after mem_map is up */ 1860aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void) 1861aa888a74SAndi Kleen { 1862aa888a74SAndi Kleen struct huge_bootmem_page *m; 1863aa888a74SAndi Kleen 1864aa888a74SAndi Kleen list_for_each_entry(m, &huge_boot_pages, list) { 1865aa888a74SAndi Kleen struct hstate *h = m->hstate; 1866ee8f248dSBecky Bruce struct page *page; 1867ee8f248dSBecky Bruce 1868ee8f248dSBecky Bruce #ifdef CONFIG_HIGHMEM 1869ee8f248dSBecky Bruce page = pfn_to_page(m->phys >> PAGE_SHIFT); 18708b89a116SGrygorii Strashko memblock_free_late(__pa(m), 1871ee8f248dSBecky Bruce sizeof(struct huge_bootmem_page)); 1872ee8f248dSBecky Bruce #else 1873ee8f248dSBecky Bruce page = virt_to_page(m); 1874ee8f248dSBecky Bruce #endif 1875aa888a74SAndi Kleen WARN_ON(page_count(page) != 1); 187618229df5SAndy Whitcroft prep_compound_huge_page(page, h->order); 1877ef5a22beSAndrea Arcangeli WARN_ON(PageReserved(page)); 1878aa888a74SAndi Kleen prep_new_huge_page(h, page, page_to_nid(page)); 1879b0320c7bSRafael Aquini /* 1880b0320c7bSRafael Aquini * If we had gigantic hugepages allocated at boot time, we need 1881b0320c7bSRafael Aquini * to restore the 'stolen' pages to totalram_pages in order to 1882b0320c7bSRafael Aquini * fix confusing memory reports from free(1) and another 1883b0320c7bSRafael Aquini * side-effects, like CommitLimit going negative. 1884b0320c7bSRafael Aquini */ 1885bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 18863dcc0571SJiang Liu adjust_managed_page_count(page, 1 << h->order); 1887aa888a74SAndi Kleen } 1888aa888a74SAndi Kleen } 1889aa888a74SAndi Kleen 18908faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h) 18911da177e4SLinus Torvalds { 18921da177e4SLinus Torvalds unsigned long i; 18931da177e4SLinus Torvalds 1894e5ff2159SAndi Kleen for (i = 0; i < h->max_huge_pages; ++i) { 1895bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) { 1896aa888a74SAndi Kleen if (!alloc_bootmem_huge_page(h)) 1897aa888a74SAndi Kleen break; 18989b5e5d0fSLee Schermerhorn } else if (!alloc_fresh_huge_page(h, 18998cebfcd0SLai Jiangshan &node_states[N_MEMORY])) 19001da177e4SLinus Torvalds break; 19011da177e4SLinus Torvalds } 19028faa8b07SAndi Kleen h->max_huge_pages = i; 1903e5ff2159SAndi Kleen } 1904e5ff2159SAndi Kleen 1905e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void) 1906e5ff2159SAndi Kleen { 1907e5ff2159SAndi Kleen struct hstate *h; 1908e5ff2159SAndi Kleen 1909e5ff2159SAndi Kleen for_each_hstate(h) { 1910641844f5SNaoya Horiguchi if (minimum_order > huge_page_order(h)) 1911641844f5SNaoya Horiguchi minimum_order = huge_page_order(h); 1912641844f5SNaoya Horiguchi 19138faa8b07SAndi Kleen /* oversize hugepages were init'ed in early boot */ 1914bae7f4aeSLuiz Capitulino if (!hstate_is_gigantic(h)) 19158faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(h); 1916e5ff2159SAndi Kleen } 1917641844f5SNaoya Horiguchi VM_BUG_ON(minimum_order == UINT_MAX); 1918e5ff2159SAndi Kleen } 1919e5ff2159SAndi Kleen 19204abd32dbSAndi Kleen static char * __init memfmt(char *buf, unsigned long n) 19214abd32dbSAndi Kleen { 19224abd32dbSAndi Kleen if (n >= (1UL << 30)) 19234abd32dbSAndi Kleen sprintf(buf, "%lu GB", n >> 30); 19244abd32dbSAndi Kleen else if (n >= (1UL << 20)) 19254abd32dbSAndi Kleen sprintf(buf, "%lu MB", n >> 20); 19264abd32dbSAndi Kleen else 19274abd32dbSAndi Kleen sprintf(buf, "%lu KB", n >> 10); 19284abd32dbSAndi Kleen return buf; 19294abd32dbSAndi Kleen } 19304abd32dbSAndi Kleen 1931e5ff2159SAndi Kleen static void __init report_hugepages(void) 1932e5ff2159SAndi Kleen { 1933e5ff2159SAndi Kleen struct hstate *h; 1934e5ff2159SAndi Kleen 1935e5ff2159SAndi Kleen for_each_hstate(h) { 19364abd32dbSAndi Kleen char buf[32]; 1937ffb22af5SAndrew Morton pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n", 19384abd32dbSAndi Kleen memfmt(buf, huge_page_size(h)), 19394abd32dbSAndi Kleen h->free_huge_pages); 1940e5ff2159SAndi Kleen } 1941e5ff2159SAndi Kleen } 1942e5ff2159SAndi Kleen 19431da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM 19446ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count, 19456ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 19461da177e4SLinus Torvalds { 19474415cc8dSChristoph Lameter int i; 19484415cc8dSChristoph Lameter 1949bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1950aa888a74SAndi Kleen return; 1951aa888a74SAndi Kleen 19526ae11b27SLee Schermerhorn for_each_node_mask(i, *nodes_allowed) { 19531da177e4SLinus Torvalds struct page *page, *next; 1954a5516438SAndi Kleen struct list_head *freel = &h->hugepage_freelists[i]; 1955a5516438SAndi Kleen list_for_each_entry_safe(page, next, freel, lru) { 1956a5516438SAndi Kleen if (count >= h->nr_huge_pages) 19576b0c880dSAdam Litke return; 19581da177e4SLinus Torvalds if (PageHighMem(page)) 19591da177e4SLinus Torvalds continue; 19601da177e4SLinus Torvalds list_del(&page->lru); 1961e5ff2159SAndi Kleen update_and_free_page(h, page); 1962a5516438SAndi Kleen h->free_huge_pages--; 1963a5516438SAndi Kleen h->free_huge_pages_node[page_to_nid(page)]--; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds #else 19686ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count, 19696ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 19701da177e4SLinus Torvalds { 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds #endif 19731da177e4SLinus Torvalds 197420a0307cSWu Fengguang /* 197520a0307cSWu Fengguang * Increment or decrement surplus_huge_pages. Keep node-specific counters 197620a0307cSWu Fengguang * balanced by operating on them in a round-robin fashion. 197720a0307cSWu Fengguang * Returns 1 if an adjustment was made. 197820a0307cSWu Fengguang */ 19796ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, 19806ae11b27SLee Schermerhorn int delta) 198120a0307cSWu Fengguang { 1982b2261026SJoonsoo Kim int nr_nodes, node; 198320a0307cSWu Fengguang 198420a0307cSWu Fengguang VM_BUG_ON(delta != -1 && delta != 1); 198520a0307cSWu Fengguang 1986e8c5c824SLee Schermerhorn if (delta < 0) { 1987b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1988b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node]) 1989b2261026SJoonsoo Kim goto found; 1990b2261026SJoonsoo Kim } 1991b2261026SJoonsoo Kim } else { 1992b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1993b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node] < 1994b2261026SJoonsoo Kim h->nr_huge_pages_node[node]) 1995b2261026SJoonsoo Kim goto found; 1996e8c5c824SLee Schermerhorn } 19979a76db09SLee Schermerhorn } 1998b2261026SJoonsoo Kim return 0; 199920a0307cSWu Fengguang 2000b2261026SJoonsoo Kim found: 200120a0307cSWu Fengguang h->surplus_huge_pages += delta; 2002b2261026SJoonsoo Kim h->surplus_huge_pages_node[node] += delta; 2003b2261026SJoonsoo Kim return 1; 200420a0307cSWu Fengguang } 200520a0307cSWu Fengguang 2006a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) 20076ae11b27SLee Schermerhorn static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count, 20086ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 20091da177e4SLinus Torvalds { 20107893d1d5SAdam Litke unsigned long min_count, ret; 20111da177e4SLinus Torvalds 2012944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 2013aa888a74SAndi Kleen return h->max_huge_pages; 2014aa888a74SAndi Kleen 20157893d1d5SAdam Litke /* 20167893d1d5SAdam Litke * Increase the pool size 20177893d1d5SAdam Litke * First take pages out of surplus state. Then make up the 20187893d1d5SAdam Litke * remaining difference by allocating fresh huge pages. 2019d1c3fb1fSNishanth Aravamudan * 2020d1c3fb1fSNishanth Aravamudan * We might race with alloc_buddy_huge_page() here and be unable 2021d1c3fb1fSNishanth Aravamudan * to convert a surplus huge page to a normal huge page. That is 2022d1c3fb1fSNishanth Aravamudan * not critical, though, it just means the overall size of the 2023d1c3fb1fSNishanth Aravamudan * pool might be one hugepage larger than it needs to be, but 2024d1c3fb1fSNishanth Aravamudan * within all the constraints specified by the sysctls. 20257893d1d5SAdam Litke */ 20261da177e4SLinus Torvalds spin_lock(&hugetlb_lock); 2027a5516438SAndi Kleen while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { 20286ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, -1)) 20297893d1d5SAdam Litke break; 20307893d1d5SAdam Litke } 20317893d1d5SAdam Litke 2032a5516438SAndi Kleen while (count > persistent_huge_pages(h)) { 20337893d1d5SAdam Litke /* 20347893d1d5SAdam Litke * If this allocation races such that we no longer need the 20357893d1d5SAdam Litke * page, free_huge_page will handle it by freeing the page 20367893d1d5SAdam Litke * and reducing the surplus. 20377893d1d5SAdam Litke */ 20387893d1d5SAdam Litke spin_unlock(&hugetlb_lock); 2039944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) 2040944d9fecSLuiz Capitulino ret = alloc_fresh_gigantic_page(h, nodes_allowed); 2041944d9fecSLuiz Capitulino else 20426ae11b27SLee Schermerhorn ret = alloc_fresh_huge_page(h, nodes_allowed); 20437893d1d5SAdam Litke spin_lock(&hugetlb_lock); 20447893d1d5SAdam Litke if (!ret) 20457893d1d5SAdam Litke goto out; 20467893d1d5SAdam Litke 2047536240f2SMel Gorman /* Bail for signals. Probably ctrl-c from user */ 2048536240f2SMel Gorman if (signal_pending(current)) 2049536240f2SMel Gorman goto out; 20507893d1d5SAdam Litke } 20517893d1d5SAdam Litke 20527893d1d5SAdam Litke /* 20537893d1d5SAdam Litke * Decrease the pool size 20547893d1d5SAdam Litke * First return free pages to the buddy allocator (being careful 20557893d1d5SAdam Litke * to keep enough around to satisfy reservations). Then place 20567893d1d5SAdam Litke * pages into surplus state as needed so the pool will shrink 20577893d1d5SAdam Litke * to the desired size as pages become free. 2058d1c3fb1fSNishanth Aravamudan * 2059d1c3fb1fSNishanth Aravamudan * By placing pages into the surplus state independent of the 2060d1c3fb1fSNishanth Aravamudan * overcommit value, we are allowing the surplus pool size to 2061d1c3fb1fSNishanth Aravamudan * exceed overcommit. There are few sane options here. Since 2062d1c3fb1fSNishanth Aravamudan * alloc_buddy_huge_page() is checking the global counter, 2063d1c3fb1fSNishanth Aravamudan * though, we'll note that we're not allowed to exceed surplus 2064d1c3fb1fSNishanth Aravamudan * and won't grow the pool anywhere else. Not until one of the 2065d1c3fb1fSNishanth Aravamudan * sysctls are changed, or the surplus pages go out of use. 20667893d1d5SAdam Litke */ 2067a5516438SAndi Kleen min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; 20686b0c880dSAdam Litke min_count = max(count, min_count); 20696ae11b27SLee Schermerhorn try_to_free_low(h, min_count, nodes_allowed); 2070a5516438SAndi Kleen while (min_count < persistent_huge_pages(h)) { 20716ae11b27SLee Schermerhorn if (!free_pool_huge_page(h, nodes_allowed, 0)) 20721da177e4SLinus Torvalds break; 207355f67141SMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 20741da177e4SLinus Torvalds } 2075a5516438SAndi Kleen while (count < persistent_huge_pages(h)) { 20766ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, 1)) 20777893d1d5SAdam Litke break; 20787893d1d5SAdam Litke } 20797893d1d5SAdam Litke out: 2080a5516438SAndi Kleen ret = persistent_huge_pages(h); 20811da177e4SLinus Torvalds spin_unlock(&hugetlb_lock); 20827893d1d5SAdam Litke return ret; 20831da177e4SLinus Torvalds } 20841da177e4SLinus Torvalds 2085a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \ 2086a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 2087a3437870SNishanth Aravamudan 2088a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \ 2089a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = \ 2090a3437870SNishanth Aravamudan __ATTR(_name, 0644, _name##_show, _name##_store) 2091a3437870SNishanth Aravamudan 2092a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj; 2093a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 2094a3437870SNishanth Aravamudan 20959a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp); 20969a305230SLee Schermerhorn 20979a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp) 2098a3437870SNishanth Aravamudan { 2099a3437870SNishanth Aravamudan int i; 21009a305230SLee Schermerhorn 2101a3437870SNishanth Aravamudan for (i = 0; i < HUGE_MAX_HSTATE; i++) 21029a305230SLee Schermerhorn if (hstate_kobjs[i] == kobj) { 21039a305230SLee Schermerhorn if (nidp) 21049a305230SLee Schermerhorn *nidp = NUMA_NO_NODE; 2105a3437870SNishanth Aravamudan return &hstates[i]; 21069a305230SLee Schermerhorn } 21079a305230SLee Schermerhorn 21089a305230SLee Schermerhorn return kobj_to_node_hstate(kobj, nidp); 2109a3437870SNishanth Aravamudan } 2110a3437870SNishanth Aravamudan 211106808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj, 2112a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2113a3437870SNishanth Aravamudan { 21149a305230SLee Schermerhorn struct hstate *h; 21159a305230SLee Schermerhorn unsigned long nr_huge_pages; 21169a305230SLee Schermerhorn int nid; 21179a305230SLee Schermerhorn 21189a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 21199a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 21209a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages; 21219a305230SLee Schermerhorn else 21229a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages_node[nid]; 21239a305230SLee Schermerhorn 21249a305230SLee Schermerhorn return sprintf(buf, "%lu\n", nr_huge_pages); 2125a3437870SNishanth Aravamudan } 2126adbe8726SEric B Munson 2127238d3c13SDavid Rientjes static ssize_t __nr_hugepages_store_common(bool obey_mempolicy, 2128238d3c13SDavid Rientjes struct hstate *h, int nid, 2129238d3c13SDavid Rientjes unsigned long count, size_t len) 2130a3437870SNishanth Aravamudan { 2131a3437870SNishanth Aravamudan int err; 2132bad44b5bSDavid Rientjes NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY); 2133a3437870SNishanth Aravamudan 2134944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) { 2135adbe8726SEric B Munson err = -EINVAL; 2136adbe8726SEric B Munson goto out; 2137adbe8726SEric B Munson } 2138adbe8726SEric B Munson 21399a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) { 21409a305230SLee Schermerhorn /* 21419a305230SLee Schermerhorn * global hstate attribute 21429a305230SLee Schermerhorn */ 21439a305230SLee Schermerhorn if (!(obey_mempolicy && 21449a305230SLee Schermerhorn init_nodemask_of_mempolicy(nodes_allowed))) { 214506808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 21468cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 214706808b08SLee Schermerhorn } 21489a305230SLee Schermerhorn } else if (nodes_allowed) { 21499a305230SLee Schermerhorn /* 21509a305230SLee Schermerhorn * per node hstate attribute: adjust count to global, 21519a305230SLee Schermerhorn * but restrict alloc/free to the specified node. 21529a305230SLee Schermerhorn */ 21539a305230SLee Schermerhorn count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; 21549a305230SLee Schermerhorn init_nodemask_of_node(nodes_allowed, nid); 21559a305230SLee Schermerhorn } else 21568cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 21579a305230SLee Schermerhorn 215806808b08SLee Schermerhorn h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed); 2159a3437870SNishanth Aravamudan 21608cebfcd0SLai Jiangshan if (nodes_allowed != &node_states[N_MEMORY]) 216106808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 216206808b08SLee Schermerhorn 216306808b08SLee Schermerhorn return len; 2164adbe8726SEric B Munson out: 2165adbe8726SEric B Munson NODEMASK_FREE(nodes_allowed); 2166adbe8726SEric B Munson return err; 216706808b08SLee Schermerhorn } 216806808b08SLee Schermerhorn 2169238d3c13SDavid Rientjes static ssize_t nr_hugepages_store_common(bool obey_mempolicy, 2170238d3c13SDavid Rientjes struct kobject *kobj, const char *buf, 2171238d3c13SDavid Rientjes size_t len) 2172238d3c13SDavid Rientjes { 2173238d3c13SDavid Rientjes struct hstate *h; 2174238d3c13SDavid Rientjes unsigned long count; 2175238d3c13SDavid Rientjes int nid; 2176238d3c13SDavid Rientjes int err; 2177238d3c13SDavid Rientjes 2178238d3c13SDavid Rientjes err = kstrtoul(buf, 10, &count); 2179238d3c13SDavid Rientjes if (err) 2180238d3c13SDavid Rientjes return err; 2181238d3c13SDavid Rientjes 2182238d3c13SDavid Rientjes h = kobj_to_hstate(kobj, &nid); 2183238d3c13SDavid Rientjes return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len); 2184238d3c13SDavid Rientjes } 2185238d3c13SDavid Rientjes 218606808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj, 218706808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 218806808b08SLee Schermerhorn { 218906808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 219006808b08SLee Schermerhorn } 219106808b08SLee Schermerhorn 219206808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj, 219306808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 219406808b08SLee Schermerhorn { 2195238d3c13SDavid Rientjes return nr_hugepages_store_common(false, kobj, buf, len); 2196a3437870SNishanth Aravamudan } 2197a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages); 2198a3437870SNishanth Aravamudan 219906808b08SLee Schermerhorn #ifdef CONFIG_NUMA 220006808b08SLee Schermerhorn 220106808b08SLee Schermerhorn /* 220206808b08SLee Schermerhorn * hstate attribute for optionally mempolicy-based constraint on persistent 220306808b08SLee Schermerhorn * huge page alloc/free. 220406808b08SLee Schermerhorn */ 220506808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj, 220606808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 220706808b08SLee Schermerhorn { 220806808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 220906808b08SLee Schermerhorn } 221006808b08SLee Schermerhorn 221106808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj, 221206808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 221306808b08SLee Schermerhorn { 2214238d3c13SDavid Rientjes return nr_hugepages_store_common(true, kobj, buf, len); 221506808b08SLee Schermerhorn } 221606808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy); 221706808b08SLee Schermerhorn #endif 221806808b08SLee Schermerhorn 221906808b08SLee Schermerhorn 2220a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, 2221a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2222a3437870SNishanth Aravamudan { 22239a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2224a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages); 2225a3437870SNishanth Aravamudan } 2226adbe8726SEric B Munson 2227a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, 2228a3437870SNishanth Aravamudan struct kobj_attribute *attr, const char *buf, size_t count) 2229a3437870SNishanth Aravamudan { 2230a3437870SNishanth Aravamudan int err; 2231a3437870SNishanth Aravamudan unsigned long input; 22329a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2233a3437870SNishanth Aravamudan 2234bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 2235adbe8726SEric B Munson return -EINVAL; 2236adbe8726SEric B Munson 22373dbb95f7SJingoo Han err = kstrtoul(buf, 10, &input); 2238a3437870SNishanth Aravamudan if (err) 223973ae31e5SEric B Munson return err; 2240a3437870SNishanth Aravamudan 2241a3437870SNishanth Aravamudan spin_lock(&hugetlb_lock); 2242a3437870SNishanth Aravamudan h->nr_overcommit_huge_pages = input; 2243a3437870SNishanth Aravamudan spin_unlock(&hugetlb_lock); 2244a3437870SNishanth Aravamudan 2245a3437870SNishanth Aravamudan return count; 2246a3437870SNishanth Aravamudan } 2247a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages); 2248a3437870SNishanth Aravamudan 2249a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj, 2250a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2251a3437870SNishanth Aravamudan { 22529a305230SLee Schermerhorn struct hstate *h; 22539a305230SLee Schermerhorn unsigned long free_huge_pages; 22549a305230SLee Schermerhorn int nid; 22559a305230SLee Schermerhorn 22569a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 22579a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 22589a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages; 22599a305230SLee Schermerhorn else 22609a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages_node[nid]; 22619a305230SLee Schermerhorn 22629a305230SLee Schermerhorn return sprintf(buf, "%lu\n", free_huge_pages); 2263a3437870SNishanth Aravamudan } 2264a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages); 2265a3437870SNishanth Aravamudan 2266a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj, 2267a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2268a3437870SNishanth Aravamudan { 22699a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 2270a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->resv_huge_pages); 2271a3437870SNishanth Aravamudan } 2272a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages); 2273a3437870SNishanth Aravamudan 2274a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj, 2275a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 2276a3437870SNishanth Aravamudan { 22779a305230SLee Schermerhorn struct hstate *h; 22789a305230SLee Schermerhorn unsigned long surplus_huge_pages; 22799a305230SLee Schermerhorn int nid; 22809a305230SLee Schermerhorn 22819a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 22829a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 22839a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages; 22849a305230SLee Schermerhorn else 22859a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages_node[nid]; 22869a305230SLee Schermerhorn 22879a305230SLee Schermerhorn return sprintf(buf, "%lu\n", surplus_huge_pages); 2288a3437870SNishanth Aravamudan } 2289a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages); 2290a3437870SNishanth Aravamudan 2291a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = { 2292a3437870SNishanth Aravamudan &nr_hugepages_attr.attr, 2293a3437870SNishanth Aravamudan &nr_overcommit_hugepages_attr.attr, 2294a3437870SNishanth Aravamudan &free_hugepages_attr.attr, 2295a3437870SNishanth Aravamudan &resv_hugepages_attr.attr, 2296a3437870SNishanth Aravamudan &surplus_hugepages_attr.attr, 229706808b08SLee Schermerhorn #ifdef CONFIG_NUMA 229806808b08SLee Schermerhorn &nr_hugepages_mempolicy_attr.attr, 229906808b08SLee Schermerhorn #endif 2300a3437870SNishanth Aravamudan NULL, 2301a3437870SNishanth Aravamudan }; 2302a3437870SNishanth Aravamudan 2303a3437870SNishanth Aravamudan static struct attribute_group hstate_attr_group = { 2304a3437870SNishanth Aravamudan .attrs = hstate_attrs, 2305a3437870SNishanth Aravamudan }; 2306a3437870SNishanth Aravamudan 2307094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, 23089a305230SLee Schermerhorn struct kobject **hstate_kobjs, 23099a305230SLee Schermerhorn struct attribute_group *hstate_attr_group) 2310a3437870SNishanth Aravamudan { 2311a3437870SNishanth Aravamudan int retval; 2312972dc4deSAneesh Kumar K.V int hi = hstate_index(h); 2313a3437870SNishanth Aravamudan 23149a305230SLee Schermerhorn hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); 23159a305230SLee Schermerhorn if (!hstate_kobjs[hi]) 2316a3437870SNishanth Aravamudan return -ENOMEM; 2317a3437870SNishanth Aravamudan 23189a305230SLee Schermerhorn retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group); 2319a3437870SNishanth Aravamudan if (retval) 23209a305230SLee Schermerhorn kobject_put(hstate_kobjs[hi]); 2321a3437870SNishanth Aravamudan 2322a3437870SNishanth Aravamudan return retval; 2323a3437870SNishanth Aravamudan } 2324a3437870SNishanth Aravamudan 2325a3437870SNishanth Aravamudan static void __init hugetlb_sysfs_init(void) 2326a3437870SNishanth Aravamudan { 2327a3437870SNishanth Aravamudan struct hstate *h; 2328a3437870SNishanth Aravamudan int err; 2329a3437870SNishanth Aravamudan 2330a3437870SNishanth Aravamudan hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); 2331a3437870SNishanth Aravamudan if (!hugepages_kobj) 2332a3437870SNishanth Aravamudan return; 2333a3437870SNishanth Aravamudan 2334a3437870SNishanth Aravamudan for_each_hstate(h) { 23359a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, 23369a305230SLee Schermerhorn hstate_kobjs, &hstate_attr_group); 2337a3437870SNishanth Aravamudan if (err) 2338ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s", h->name); 2339a3437870SNishanth Aravamudan } 2340a3437870SNishanth Aravamudan } 2341a3437870SNishanth Aravamudan 23429a305230SLee Schermerhorn #ifdef CONFIG_NUMA 23439a305230SLee Schermerhorn 23449a305230SLee Schermerhorn /* 23459a305230SLee Schermerhorn * node_hstate/s - associate per node hstate attributes, via their kobjects, 234610fbcf4cSKay Sievers * with node devices in node_devices[] using a parallel array. The array 234710fbcf4cSKay Sievers * index of a node device or _hstate == node id. 234810fbcf4cSKay Sievers * This is here to avoid any static dependency of the node device driver, in 23499a305230SLee Schermerhorn * the base kernel, on the hugetlb module. 23509a305230SLee Schermerhorn */ 23519a305230SLee Schermerhorn struct node_hstate { 23529a305230SLee Schermerhorn struct kobject *hugepages_kobj; 23539a305230SLee Schermerhorn struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 23549a305230SLee Schermerhorn }; 23559a305230SLee Schermerhorn struct node_hstate node_hstates[MAX_NUMNODES]; 23569a305230SLee Schermerhorn 23579a305230SLee Schermerhorn /* 235810fbcf4cSKay Sievers * A subset of global hstate attributes for node devices 23599a305230SLee Schermerhorn */ 23609a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = { 23619a305230SLee Schermerhorn &nr_hugepages_attr.attr, 23629a305230SLee Schermerhorn &free_hugepages_attr.attr, 23639a305230SLee Schermerhorn &surplus_hugepages_attr.attr, 23649a305230SLee Schermerhorn NULL, 23659a305230SLee Schermerhorn }; 23669a305230SLee Schermerhorn 23679a305230SLee Schermerhorn static struct attribute_group per_node_hstate_attr_group = { 23689a305230SLee Schermerhorn .attrs = per_node_hstate_attrs, 23699a305230SLee Schermerhorn }; 23709a305230SLee Schermerhorn 23719a305230SLee Schermerhorn /* 237210fbcf4cSKay Sievers * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj. 23739a305230SLee Schermerhorn * Returns node id via non-NULL nidp. 23749a305230SLee Schermerhorn */ 23759a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 23769a305230SLee Schermerhorn { 23779a305230SLee Schermerhorn int nid; 23789a305230SLee Schermerhorn 23799a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) { 23809a305230SLee Schermerhorn struct node_hstate *nhs = &node_hstates[nid]; 23819a305230SLee Schermerhorn int i; 23829a305230SLee Schermerhorn for (i = 0; i < HUGE_MAX_HSTATE; i++) 23839a305230SLee Schermerhorn if (nhs->hstate_kobjs[i] == kobj) { 23849a305230SLee Schermerhorn if (nidp) 23859a305230SLee Schermerhorn *nidp = nid; 23869a305230SLee Schermerhorn return &hstates[i]; 23879a305230SLee Schermerhorn } 23889a305230SLee Schermerhorn } 23899a305230SLee Schermerhorn 23909a305230SLee Schermerhorn BUG(); 23919a305230SLee Schermerhorn return NULL; 23929a305230SLee Schermerhorn } 23939a305230SLee Schermerhorn 23949a305230SLee Schermerhorn /* 239510fbcf4cSKay Sievers * Unregister hstate attributes from a single node device. 23969a305230SLee Schermerhorn * No-op if no hstate attributes attached. 23979a305230SLee Schermerhorn */ 23983cd8b44fSClaudiu Ghioc static void hugetlb_unregister_node(struct node *node) 23999a305230SLee Schermerhorn { 24009a305230SLee Schermerhorn struct hstate *h; 240110fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 24029a305230SLee Schermerhorn 24039a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 24049b5e5d0fSLee Schermerhorn return; /* no hstate attributes */ 24059a305230SLee Schermerhorn 2406972dc4deSAneesh Kumar K.V for_each_hstate(h) { 2407972dc4deSAneesh Kumar K.V int idx = hstate_index(h); 2408972dc4deSAneesh Kumar K.V if (nhs->hstate_kobjs[idx]) { 2409972dc4deSAneesh Kumar K.V kobject_put(nhs->hstate_kobjs[idx]); 2410972dc4deSAneesh Kumar K.V nhs->hstate_kobjs[idx] = NULL; 2411972dc4deSAneesh Kumar K.V } 24129a305230SLee Schermerhorn } 24139a305230SLee Schermerhorn 24149a305230SLee Schermerhorn kobject_put(nhs->hugepages_kobj); 24159a305230SLee Schermerhorn nhs->hugepages_kobj = NULL; 24169a305230SLee Schermerhorn } 24179a305230SLee Schermerhorn 24189a305230SLee Schermerhorn /* 241910fbcf4cSKay Sievers * hugetlb module exit: unregister hstate attributes from node devices 24209a305230SLee Schermerhorn * that have them. 24219a305230SLee Schermerhorn */ 24229a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) 24239a305230SLee Schermerhorn { 24249a305230SLee Schermerhorn int nid; 24259a305230SLee Schermerhorn 24269a305230SLee Schermerhorn /* 242710fbcf4cSKay Sievers * disable node device registrations. 24289a305230SLee Schermerhorn */ 24299a305230SLee Schermerhorn register_hugetlbfs_with_node(NULL, NULL); 24309a305230SLee Schermerhorn 24319a305230SLee Schermerhorn /* 24329a305230SLee Schermerhorn * remove hstate attributes from any nodes that have them. 24339a305230SLee Schermerhorn */ 24349a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) 24358732794bSWen Congyang hugetlb_unregister_node(node_devices[nid]); 24369a305230SLee Schermerhorn } 24379a305230SLee Schermerhorn 24389a305230SLee Schermerhorn /* 243910fbcf4cSKay Sievers * Register hstate attributes for a single node device. 24409a305230SLee Schermerhorn * No-op if attributes already registered. 24419a305230SLee Schermerhorn */ 24423cd8b44fSClaudiu Ghioc static void hugetlb_register_node(struct node *node) 24439a305230SLee Schermerhorn { 24449a305230SLee Schermerhorn struct hstate *h; 244510fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 24469a305230SLee Schermerhorn int err; 24479a305230SLee Schermerhorn 24489a305230SLee Schermerhorn if (nhs->hugepages_kobj) 24499a305230SLee Schermerhorn return; /* already allocated */ 24509a305230SLee Schermerhorn 24519a305230SLee Schermerhorn nhs->hugepages_kobj = kobject_create_and_add("hugepages", 245210fbcf4cSKay Sievers &node->dev.kobj); 24539a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 24549a305230SLee Schermerhorn return; 24559a305230SLee Schermerhorn 24569a305230SLee Schermerhorn for_each_hstate(h) { 24579a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, 24589a305230SLee Schermerhorn nhs->hstate_kobjs, 24599a305230SLee Schermerhorn &per_node_hstate_attr_group); 24609a305230SLee Schermerhorn if (err) { 2461ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s for node %d\n", 246210fbcf4cSKay Sievers h->name, node->dev.id); 24639a305230SLee Schermerhorn hugetlb_unregister_node(node); 24649a305230SLee Schermerhorn break; 24659a305230SLee Schermerhorn } 24669a305230SLee Schermerhorn } 24679a305230SLee Schermerhorn } 24689a305230SLee Schermerhorn 24699a305230SLee Schermerhorn /* 24709b5e5d0fSLee Schermerhorn * hugetlb init time: register hstate attributes for all registered node 247110fbcf4cSKay Sievers * devices of nodes that have memory. All on-line nodes should have 247210fbcf4cSKay Sievers * registered their associated device by this time. 24739a305230SLee Schermerhorn */ 24747d9ca000SLuiz Capitulino static void __init hugetlb_register_all_nodes(void) 24759a305230SLee Schermerhorn { 24769a305230SLee Schermerhorn int nid; 24779a305230SLee Schermerhorn 24788cebfcd0SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 24798732794bSWen Congyang struct node *node = node_devices[nid]; 248010fbcf4cSKay Sievers if (node->dev.id == nid) 24819a305230SLee Schermerhorn hugetlb_register_node(node); 24829a305230SLee Schermerhorn } 24839a305230SLee Schermerhorn 24849a305230SLee Schermerhorn /* 248510fbcf4cSKay Sievers * Let the node device driver know we're here so it can 24869a305230SLee Schermerhorn * [un]register hstate attributes on node hotplug. 24879a305230SLee Schermerhorn */ 24889a305230SLee Schermerhorn register_hugetlbfs_with_node(hugetlb_register_node, 24899a305230SLee Schermerhorn hugetlb_unregister_node); 24909a305230SLee Schermerhorn } 24919a305230SLee Schermerhorn #else /* !CONFIG_NUMA */ 24929a305230SLee Schermerhorn 24939a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 24949a305230SLee Schermerhorn { 24959a305230SLee Schermerhorn BUG(); 24969a305230SLee Schermerhorn if (nidp) 24979a305230SLee Schermerhorn *nidp = -1; 24989a305230SLee Schermerhorn return NULL; 24999a305230SLee Schermerhorn } 25009a305230SLee Schermerhorn 25019a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) { } 25029a305230SLee Schermerhorn 25039a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { } 25049a305230SLee Schermerhorn 25059a305230SLee Schermerhorn #endif 25069a305230SLee Schermerhorn 2507a3437870SNishanth Aravamudan static void __exit hugetlb_exit(void) 2508a3437870SNishanth Aravamudan { 2509a3437870SNishanth Aravamudan struct hstate *h; 2510a3437870SNishanth Aravamudan 25119a305230SLee Schermerhorn hugetlb_unregister_all_nodes(); 25129a305230SLee Schermerhorn 2513a3437870SNishanth Aravamudan for_each_hstate(h) { 2514972dc4deSAneesh Kumar K.V kobject_put(hstate_kobjs[hstate_index(h)]); 2515a3437870SNishanth Aravamudan } 2516a3437870SNishanth Aravamudan 2517a3437870SNishanth Aravamudan kobject_put(hugepages_kobj); 2518c672c7f2SMike Kravetz kfree(hugetlb_fault_mutex_table); 2519a3437870SNishanth Aravamudan } 2520a3437870SNishanth Aravamudan module_exit(hugetlb_exit); 2521a3437870SNishanth Aravamudan 2522a3437870SNishanth Aravamudan static int __init hugetlb_init(void) 2523a3437870SNishanth Aravamudan { 25248382d914SDavidlohr Bueso int i; 25258382d914SDavidlohr Bueso 2526457c1b27SNishanth Aravamudan if (!hugepages_supported()) 25270ef89d25SBenjamin Herrenschmidt return 0; 2528a3437870SNishanth Aravamudan 2529e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) { 2530e11bfbfcSNick Piggin default_hstate_size = HPAGE_SIZE; 2531e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) 2532a3437870SNishanth Aravamudan hugetlb_add_hstate(HUGETLB_PAGE_ORDER); 2533a3437870SNishanth Aravamudan } 2534972dc4deSAneesh Kumar K.V default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size)); 2535e11bfbfcSNick Piggin if (default_hstate_max_huge_pages) 2536e11bfbfcSNick Piggin default_hstate.max_huge_pages = default_hstate_max_huge_pages; 2537a3437870SNishanth Aravamudan 2538a3437870SNishanth Aravamudan hugetlb_init_hstates(); 2539aa888a74SAndi Kleen gather_bootmem_prealloc(); 2540a3437870SNishanth Aravamudan report_hugepages(); 2541a3437870SNishanth Aravamudan 2542a3437870SNishanth Aravamudan hugetlb_sysfs_init(); 25439a305230SLee Schermerhorn hugetlb_register_all_nodes(); 25447179e7bfSJianguo Wu hugetlb_cgroup_file_init(); 25459a305230SLee Schermerhorn 25468382d914SDavidlohr Bueso #ifdef CONFIG_SMP 25478382d914SDavidlohr Bueso num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); 25488382d914SDavidlohr Bueso #else 25498382d914SDavidlohr Bueso num_fault_mutexes = 1; 25508382d914SDavidlohr Bueso #endif 2551c672c7f2SMike Kravetz hugetlb_fault_mutex_table = 25528382d914SDavidlohr Bueso kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL); 2553c672c7f2SMike Kravetz BUG_ON(!hugetlb_fault_mutex_table); 25548382d914SDavidlohr Bueso 25558382d914SDavidlohr Bueso for (i = 0; i < num_fault_mutexes; i++) 2556c672c7f2SMike Kravetz mutex_init(&hugetlb_fault_mutex_table[i]); 2557a3437870SNishanth Aravamudan return 0; 2558a3437870SNishanth Aravamudan } 2559a3437870SNishanth Aravamudan module_init(hugetlb_init); 2560a3437870SNishanth Aravamudan 2561a3437870SNishanth Aravamudan /* Should be called on processing a hugepagesz=... option */ 2562a3437870SNishanth Aravamudan void __init hugetlb_add_hstate(unsigned order) 2563a3437870SNishanth Aravamudan { 2564a3437870SNishanth Aravamudan struct hstate *h; 25658faa8b07SAndi Kleen unsigned long i; 25668faa8b07SAndi Kleen 2567a3437870SNishanth Aravamudan if (size_to_hstate(PAGE_SIZE << order)) { 2568ffb22af5SAndrew Morton pr_warning("hugepagesz= specified twice, ignoring\n"); 2569a3437870SNishanth Aravamudan return; 2570a3437870SNishanth Aravamudan } 257147d38344SAneesh Kumar K.V BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); 2572a3437870SNishanth Aravamudan BUG_ON(order == 0); 257347d38344SAneesh Kumar K.V h = &hstates[hugetlb_max_hstate++]; 2574a3437870SNishanth Aravamudan h->order = order; 2575a3437870SNishanth Aravamudan h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); 25768faa8b07SAndi Kleen h->nr_huge_pages = 0; 25778faa8b07SAndi Kleen h->free_huge_pages = 0; 25788faa8b07SAndi Kleen for (i = 0; i < MAX_NUMNODES; ++i) 25798faa8b07SAndi Kleen INIT_LIST_HEAD(&h->hugepage_freelists[i]); 25800edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&h->hugepage_activelist); 25818cebfcd0SLai Jiangshan h->next_nid_to_alloc = first_node(node_states[N_MEMORY]); 25828cebfcd0SLai Jiangshan h->next_nid_to_free = first_node(node_states[N_MEMORY]); 2583a3437870SNishanth Aravamudan snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", 2584a3437870SNishanth Aravamudan huge_page_size(h)/1024); 25858faa8b07SAndi Kleen 2586a3437870SNishanth Aravamudan parsed_hstate = h; 2587a3437870SNishanth Aravamudan } 2588a3437870SNishanth Aravamudan 2589e11bfbfcSNick Piggin static int __init hugetlb_nrpages_setup(char *s) 2590a3437870SNishanth Aravamudan { 2591a3437870SNishanth Aravamudan unsigned long *mhp; 25928faa8b07SAndi Kleen static unsigned long *last_mhp; 2593a3437870SNishanth Aravamudan 2594a3437870SNishanth Aravamudan /* 259547d38344SAneesh Kumar K.V * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet, 2596a3437870SNishanth Aravamudan * so this hugepages= parameter goes to the "default hstate". 2597a3437870SNishanth Aravamudan */ 259847d38344SAneesh Kumar K.V if (!hugetlb_max_hstate) 2599a3437870SNishanth Aravamudan mhp = &default_hstate_max_huge_pages; 2600a3437870SNishanth Aravamudan else 2601a3437870SNishanth Aravamudan mhp = &parsed_hstate->max_huge_pages; 2602a3437870SNishanth Aravamudan 26038faa8b07SAndi Kleen if (mhp == last_mhp) { 2604ffb22af5SAndrew Morton pr_warning("hugepages= specified twice without " 26058faa8b07SAndi Kleen "interleaving hugepagesz=, ignoring\n"); 26068faa8b07SAndi Kleen return 1; 26078faa8b07SAndi Kleen } 26088faa8b07SAndi Kleen 2609a3437870SNishanth Aravamudan if (sscanf(s, "%lu", mhp) <= 0) 2610a3437870SNishanth Aravamudan *mhp = 0; 2611a3437870SNishanth Aravamudan 26128faa8b07SAndi Kleen /* 26138faa8b07SAndi Kleen * Global state is always initialized later in hugetlb_init. 26148faa8b07SAndi Kleen * But we need to allocate >= MAX_ORDER hstates here early to still 26158faa8b07SAndi Kleen * use the bootmem allocator. 26168faa8b07SAndi Kleen */ 261747d38344SAneesh Kumar K.V if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER) 26188faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(parsed_hstate); 26198faa8b07SAndi Kleen 26208faa8b07SAndi Kleen last_mhp = mhp; 26218faa8b07SAndi Kleen 2622a3437870SNishanth Aravamudan return 1; 2623a3437870SNishanth Aravamudan } 2624e11bfbfcSNick Piggin __setup("hugepages=", hugetlb_nrpages_setup); 2625e11bfbfcSNick Piggin 2626e11bfbfcSNick Piggin static int __init hugetlb_default_setup(char *s) 2627e11bfbfcSNick Piggin { 2628e11bfbfcSNick Piggin default_hstate_size = memparse(s, &s); 2629e11bfbfcSNick Piggin return 1; 2630e11bfbfcSNick Piggin } 2631e11bfbfcSNick Piggin __setup("default_hugepagesz=", hugetlb_default_setup); 2632a3437870SNishanth Aravamudan 26338a213460SNishanth Aravamudan static unsigned int cpuset_mems_nr(unsigned int *array) 26348a213460SNishanth Aravamudan { 26358a213460SNishanth Aravamudan int node; 26368a213460SNishanth Aravamudan unsigned int nr = 0; 26378a213460SNishanth Aravamudan 26388a213460SNishanth Aravamudan for_each_node_mask(node, cpuset_current_mems_allowed) 26398a213460SNishanth Aravamudan nr += array[node]; 26408a213460SNishanth Aravamudan 26418a213460SNishanth Aravamudan return nr; 26428a213460SNishanth Aravamudan } 26438a213460SNishanth Aravamudan 26448a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL 264506808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy, 264606808b08SLee Schermerhorn struct ctl_table *table, int write, 264706808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 26481da177e4SLinus Torvalds { 2649e5ff2159SAndi Kleen struct hstate *h = &default_hstate; 2650238d3c13SDavid Rientjes unsigned long tmp = h->max_huge_pages; 265108d4a246SMichal Hocko int ret; 2652e5ff2159SAndi Kleen 2653457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2654457c1b27SNishanth Aravamudan return -ENOTSUPP; 2655457c1b27SNishanth Aravamudan 2656e5ff2159SAndi Kleen table->data = &tmp; 2657e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 265808d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 265908d4a246SMichal Hocko if (ret) 266008d4a246SMichal Hocko goto out; 2661e5ff2159SAndi Kleen 2662238d3c13SDavid Rientjes if (write) 2663238d3c13SDavid Rientjes ret = __nr_hugepages_store_common(obey_mempolicy, h, 2664238d3c13SDavid Rientjes NUMA_NO_NODE, tmp, *length); 266508d4a246SMichal Hocko out: 266608d4a246SMichal Hocko return ret; 26671da177e4SLinus Torvalds } 2668396faf03SMel Gorman 266906808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write, 267006808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 267106808b08SLee Schermerhorn { 267206808b08SLee Schermerhorn 267306808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(false, table, write, 267406808b08SLee Schermerhorn buffer, length, ppos); 267506808b08SLee Schermerhorn } 267606808b08SLee Schermerhorn 267706808b08SLee Schermerhorn #ifdef CONFIG_NUMA 267806808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write, 267906808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 268006808b08SLee Schermerhorn { 268106808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(true, table, write, 268206808b08SLee Schermerhorn buffer, length, ppos); 268306808b08SLee Schermerhorn } 268406808b08SLee Schermerhorn #endif /* CONFIG_NUMA */ 268506808b08SLee Schermerhorn 2686a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write, 26878d65af78SAlexey Dobriyan void __user *buffer, 2688a3d0c6aaSNishanth Aravamudan size_t *length, loff_t *ppos) 2689a3d0c6aaSNishanth Aravamudan { 2690a5516438SAndi Kleen struct hstate *h = &default_hstate; 2691e5ff2159SAndi Kleen unsigned long tmp; 269208d4a246SMichal Hocko int ret; 2693e5ff2159SAndi Kleen 2694457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2695457c1b27SNishanth Aravamudan return -ENOTSUPP; 2696457c1b27SNishanth Aravamudan 2697e5ff2159SAndi Kleen tmp = h->nr_overcommit_huge_pages; 2698e5ff2159SAndi Kleen 2699bae7f4aeSLuiz Capitulino if (write && hstate_is_gigantic(h)) 2700adbe8726SEric B Munson return -EINVAL; 2701adbe8726SEric B Munson 2702e5ff2159SAndi Kleen table->data = &tmp; 2703e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 270408d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 270508d4a246SMichal Hocko if (ret) 270608d4a246SMichal Hocko goto out; 2707e5ff2159SAndi Kleen 2708e5ff2159SAndi Kleen if (write) { 2709064d9efeSNishanth Aravamudan spin_lock(&hugetlb_lock); 2710e5ff2159SAndi Kleen h->nr_overcommit_huge_pages = tmp; 2711a3d0c6aaSNishanth Aravamudan spin_unlock(&hugetlb_lock); 2712e5ff2159SAndi Kleen } 271308d4a246SMichal Hocko out: 271408d4a246SMichal Hocko return ret; 2715a3d0c6aaSNishanth Aravamudan } 2716a3d0c6aaSNishanth Aravamudan 27171da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 27181da177e4SLinus Torvalds 2719e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m) 27201da177e4SLinus Torvalds { 2721a5516438SAndi Kleen struct hstate *h = &default_hstate; 2722457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2723457c1b27SNishanth Aravamudan return; 2724e1759c21SAlexey Dobriyan seq_printf(m, 27251da177e4SLinus Torvalds "HugePages_Total: %5lu\n" 27261da177e4SLinus Torvalds "HugePages_Free: %5lu\n" 2727b45b5bd6SDavid Gibson "HugePages_Rsvd: %5lu\n" 27287893d1d5SAdam Litke "HugePages_Surp: %5lu\n" 27294f98a2feSRik van Riel "Hugepagesize: %8lu kB\n", 2730a5516438SAndi Kleen h->nr_huge_pages, 2731a5516438SAndi Kleen h->free_huge_pages, 2732a5516438SAndi Kleen h->resv_huge_pages, 2733a5516438SAndi Kleen h->surplus_huge_pages, 2734a5516438SAndi Kleen 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf) 27381da177e4SLinus Torvalds { 2739a5516438SAndi Kleen struct hstate *h = &default_hstate; 2740457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2741457c1b27SNishanth Aravamudan return 0; 27421da177e4SLinus Torvalds return sprintf(buf, 27431da177e4SLinus Torvalds "Node %d HugePages_Total: %5u\n" 2744a1de0919SNishanth Aravamudan "Node %d HugePages_Free: %5u\n" 2745a1de0919SNishanth Aravamudan "Node %d HugePages_Surp: %5u\n", 2746a5516438SAndi Kleen nid, h->nr_huge_pages_node[nid], 2747a5516438SAndi Kleen nid, h->free_huge_pages_node[nid], 2748a5516438SAndi Kleen nid, h->surplus_huge_pages_node[nid]); 27491da177e4SLinus Torvalds } 27501da177e4SLinus Torvalds 2751949f7ec5SDavid Rientjes void hugetlb_show_meminfo(void) 2752949f7ec5SDavid Rientjes { 2753949f7ec5SDavid Rientjes struct hstate *h; 2754949f7ec5SDavid Rientjes int nid; 2755949f7ec5SDavid Rientjes 2756457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2757457c1b27SNishanth Aravamudan return; 2758457c1b27SNishanth Aravamudan 2759949f7ec5SDavid Rientjes for_each_node_state(nid, N_MEMORY) 2760949f7ec5SDavid Rientjes for_each_hstate(h) 2761949f7ec5SDavid Rientjes pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", 2762949f7ec5SDavid Rientjes nid, 2763949f7ec5SDavid Rientjes h->nr_huge_pages_node[nid], 2764949f7ec5SDavid Rientjes h->free_huge_pages_node[nid], 2765949f7ec5SDavid Rientjes h->surplus_huge_pages_node[nid], 2766949f7ec5SDavid Rientjes 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 2767949f7ec5SDavid Rientjes } 2768949f7ec5SDavid Rientjes 27691da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 27701da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void) 27711da177e4SLinus Torvalds { 2772d0028588SWanpeng Li struct hstate *h; 2773d0028588SWanpeng Li unsigned long nr_total_pages = 0; 2774d0028588SWanpeng Li 2775d0028588SWanpeng Li for_each_hstate(h) 2776d0028588SWanpeng Li nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); 2777d0028588SWanpeng Li return nr_total_pages; 27781da177e4SLinus Torvalds } 27791da177e4SLinus Torvalds 2780a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta) 2781fc1b8a73SMel Gorman { 2782fc1b8a73SMel Gorman int ret = -ENOMEM; 2783fc1b8a73SMel Gorman 2784fc1b8a73SMel Gorman spin_lock(&hugetlb_lock); 2785fc1b8a73SMel Gorman /* 2786fc1b8a73SMel Gorman * When cpuset is configured, it breaks the strict hugetlb page 2787fc1b8a73SMel Gorman * reservation as the accounting is done on a global variable. Such 2788fc1b8a73SMel Gorman * reservation is completely rubbish in the presence of cpuset because 2789fc1b8a73SMel Gorman * the reservation is not checked against page availability for the 2790fc1b8a73SMel Gorman * current cpuset. Application can still potentially OOM'ed by kernel 2791fc1b8a73SMel Gorman * with lack of free htlb page in cpuset that the task is in. 2792fc1b8a73SMel Gorman * Attempt to enforce strict accounting with cpuset is almost 2793fc1b8a73SMel Gorman * impossible (or too ugly) because cpuset is too fluid that 2794fc1b8a73SMel Gorman * task or memory node can be dynamically moved between cpusets. 2795fc1b8a73SMel Gorman * 2796fc1b8a73SMel Gorman * The change of semantics for shared hugetlb mapping with cpuset is 2797fc1b8a73SMel Gorman * undesirable. However, in order to preserve some of the semantics, 2798fc1b8a73SMel Gorman * we fall back to check against current free page availability as 2799fc1b8a73SMel Gorman * a best attempt and hopefully to minimize the impact of changing 2800fc1b8a73SMel Gorman * semantics that cpuset has. 2801fc1b8a73SMel Gorman */ 2802fc1b8a73SMel Gorman if (delta > 0) { 2803a5516438SAndi Kleen if (gather_surplus_pages(h, delta) < 0) 2804fc1b8a73SMel Gorman goto out; 2805fc1b8a73SMel Gorman 2806a5516438SAndi Kleen if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { 2807a5516438SAndi Kleen return_unused_surplus_pages(h, delta); 2808fc1b8a73SMel Gorman goto out; 2809fc1b8a73SMel Gorman } 2810fc1b8a73SMel Gorman } 2811fc1b8a73SMel Gorman 2812fc1b8a73SMel Gorman ret = 0; 2813fc1b8a73SMel Gorman if (delta < 0) 2814a5516438SAndi Kleen return_unused_surplus_pages(h, (unsigned long) -delta); 2815fc1b8a73SMel Gorman 2816fc1b8a73SMel Gorman out: 2817fc1b8a73SMel Gorman spin_unlock(&hugetlb_lock); 2818fc1b8a73SMel Gorman return ret; 2819fc1b8a73SMel Gorman } 2820fc1b8a73SMel Gorman 282184afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma) 282284afd99bSAndy Whitcroft { 2823f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 282484afd99bSAndy Whitcroft 282584afd99bSAndy Whitcroft /* 282684afd99bSAndy Whitcroft * This new VMA should share its siblings reservation map if present. 282784afd99bSAndy Whitcroft * The VMA will only ever have a valid reservation map pointer where 282884afd99bSAndy Whitcroft * it is being copied for another still existing VMA. As that VMA 282925985edcSLucas De Marchi * has a reference to the reservation map it cannot disappear until 283084afd99bSAndy Whitcroft * after this open call completes. It is therefore safe to take a 283184afd99bSAndy Whitcroft * new reference here without additional locking. 283284afd99bSAndy Whitcroft */ 28334e35f483SJoonsoo Kim if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 2834f522c3acSJoonsoo Kim kref_get(&resv->refs); 283584afd99bSAndy Whitcroft } 283684afd99bSAndy Whitcroft 2837a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma) 2838a1e78772SMel Gorman { 2839a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2840f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 284190481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 28424e35f483SJoonsoo Kim unsigned long reserve, start, end; 28431c5ecae3SMike Kravetz long gbl_reserve; 284484afd99bSAndy Whitcroft 28454e35f483SJoonsoo Kim if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 28464e35f483SJoonsoo Kim return; 28474e35f483SJoonsoo Kim 2848a5516438SAndi Kleen start = vma_hugecache_offset(h, vma, vma->vm_start); 2849a5516438SAndi Kleen end = vma_hugecache_offset(h, vma, vma->vm_end); 285084afd99bSAndy Whitcroft 28514e35f483SJoonsoo Kim reserve = (end - start) - region_count(resv, start, end); 285284afd99bSAndy Whitcroft 2853f031dd27SJoonsoo Kim kref_put(&resv->refs, resv_map_release); 285484afd99bSAndy Whitcroft 28557251ff78SAdam Litke if (reserve) { 28561c5ecae3SMike Kravetz /* 28571c5ecae3SMike Kravetz * Decrement reserve counts. The global reserve count may be 28581c5ecae3SMike Kravetz * adjusted if the subpool has a minimum size. 28591c5ecae3SMike Kravetz */ 28601c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, reserve); 28611c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 28627251ff78SAdam Litke } 2863a1e78772SMel Gorman } 2864a1e78772SMel Gorman 28651da177e4SLinus Torvalds /* 28661da177e4SLinus Torvalds * We cannot handle pagefaults against hugetlb pages at all. They cause 28671da177e4SLinus Torvalds * handle_mm_fault() to try to instantiate regular-sized pages in the 28681da177e4SLinus Torvalds * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get 28691da177e4SLinus Torvalds * this far. 28701da177e4SLinus Torvalds */ 2871d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 28721da177e4SLinus Torvalds { 28731da177e4SLinus Torvalds BUG(); 2874d0217ac0SNick Piggin return 0; 28751da177e4SLinus Torvalds } 28761da177e4SLinus Torvalds 2877f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = { 2878d0217ac0SNick Piggin .fault = hugetlb_vm_op_fault, 287984afd99bSAndy Whitcroft .open = hugetlb_vm_op_open, 2880a1e78772SMel Gorman .close = hugetlb_vm_op_close, 28811da177e4SLinus Torvalds }; 28821da177e4SLinus Torvalds 28831e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, 28841e8f889bSDavid Gibson int writable) 288563551ae0SDavid Gibson { 288663551ae0SDavid Gibson pte_t entry; 288763551ae0SDavid Gibson 28881e8f889bSDavid Gibson if (writable) { 2889106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, 2890106c992aSGerald Schaefer vma->vm_page_prot))); 289163551ae0SDavid Gibson } else { 2892106c992aSGerald Schaefer entry = huge_pte_wrprotect(mk_huge_pte(page, 2893106c992aSGerald Schaefer vma->vm_page_prot)); 289463551ae0SDavid Gibson } 289563551ae0SDavid Gibson entry = pte_mkyoung(entry); 289663551ae0SDavid Gibson entry = pte_mkhuge(entry); 2897d9ed9faaSChris Metcalf entry = arch_make_huge_pte(entry, vma, page, writable); 289863551ae0SDavid Gibson 289963551ae0SDavid Gibson return entry; 290063551ae0SDavid Gibson } 290163551ae0SDavid Gibson 29021e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma, 29031e8f889bSDavid Gibson unsigned long address, pte_t *ptep) 29041e8f889bSDavid Gibson { 29051e8f889bSDavid Gibson pte_t entry; 29061e8f889bSDavid Gibson 2907106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); 290832f84528SChris Forbes if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) 29094b3073e1SRussell King update_mmu_cache(vma, address, ptep); 29101e8f889bSDavid Gibson } 29111e8f889bSDavid Gibson 29124a705fefSNaoya Horiguchi static int is_hugetlb_entry_migration(pte_t pte) 29134a705fefSNaoya Horiguchi { 29144a705fefSNaoya Horiguchi swp_entry_t swp; 29154a705fefSNaoya Horiguchi 29164a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 29174a705fefSNaoya Horiguchi return 0; 29184a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 29194a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_migration_entry(swp)) 29204a705fefSNaoya Horiguchi return 1; 29214a705fefSNaoya Horiguchi else 29224a705fefSNaoya Horiguchi return 0; 29234a705fefSNaoya Horiguchi } 29244a705fefSNaoya Horiguchi 29254a705fefSNaoya Horiguchi static int is_hugetlb_entry_hwpoisoned(pte_t pte) 29264a705fefSNaoya Horiguchi { 29274a705fefSNaoya Horiguchi swp_entry_t swp; 29284a705fefSNaoya Horiguchi 29294a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 29304a705fefSNaoya Horiguchi return 0; 29314a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 29324a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_hwpoison_entry(swp)) 29334a705fefSNaoya Horiguchi return 1; 29344a705fefSNaoya Horiguchi else 29354a705fefSNaoya Horiguchi return 0; 29364a705fefSNaoya Horiguchi } 29371e8f889bSDavid Gibson 293863551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 293963551ae0SDavid Gibson struct vm_area_struct *vma) 294063551ae0SDavid Gibson { 294163551ae0SDavid Gibson pte_t *src_pte, *dst_pte, entry; 294263551ae0SDavid Gibson struct page *ptepage; 29431c59827dSHugh Dickins unsigned long addr; 29441e8f889bSDavid Gibson int cow; 2945a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2946a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 2947e8569dd2SAndreas Sandberg unsigned long mmun_start; /* For mmu_notifiers */ 2948e8569dd2SAndreas Sandberg unsigned long mmun_end; /* For mmu_notifiers */ 2949e8569dd2SAndreas Sandberg int ret = 0; 29501e8f889bSDavid Gibson 29511e8f889bSDavid Gibson cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 295263551ae0SDavid Gibson 2953e8569dd2SAndreas Sandberg mmun_start = vma->vm_start; 2954e8569dd2SAndreas Sandberg mmun_end = vma->vm_end; 2955e8569dd2SAndreas Sandberg if (cow) 2956e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end); 2957e8569dd2SAndreas Sandberg 2958a5516438SAndi Kleen for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { 2959cb900f41SKirill A. Shutemov spinlock_t *src_ptl, *dst_ptl; 2960c74df32cSHugh Dickins src_pte = huge_pte_offset(src, addr); 2961c74df32cSHugh Dickins if (!src_pte) 2962c74df32cSHugh Dickins continue; 2963a5516438SAndi Kleen dst_pte = huge_pte_alloc(dst, addr, sz); 2964e8569dd2SAndreas Sandberg if (!dst_pte) { 2965e8569dd2SAndreas Sandberg ret = -ENOMEM; 2966e8569dd2SAndreas Sandberg break; 2967e8569dd2SAndreas Sandberg } 2968c5c99429SLarry Woodman 2969c5c99429SLarry Woodman /* If the pagetables are shared don't copy or take references */ 2970c5c99429SLarry Woodman if (dst_pte == src_pte) 2971c5c99429SLarry Woodman continue; 2972c5c99429SLarry Woodman 2973cb900f41SKirill A. Shutemov dst_ptl = huge_pte_lock(h, dst, dst_pte); 2974cb900f41SKirill A. Shutemov src_ptl = huge_pte_lockptr(h, src, src_pte); 2975cb900f41SKirill A. Shutemov spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 29764a705fefSNaoya Horiguchi entry = huge_ptep_get(src_pte); 29774a705fefSNaoya Horiguchi if (huge_pte_none(entry)) { /* skip none entry */ 29784a705fefSNaoya Horiguchi ; 29794a705fefSNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_migration(entry) || 29804a705fefSNaoya Horiguchi is_hugetlb_entry_hwpoisoned(entry))) { 29814a705fefSNaoya Horiguchi swp_entry_t swp_entry = pte_to_swp_entry(entry); 29824a705fefSNaoya Horiguchi 29834a705fefSNaoya Horiguchi if (is_write_migration_entry(swp_entry) && cow) { 29844a705fefSNaoya Horiguchi /* 29854a705fefSNaoya Horiguchi * COW mappings require pages in both 29864a705fefSNaoya Horiguchi * parent and child to be set to read. 29874a705fefSNaoya Horiguchi */ 29884a705fefSNaoya Horiguchi make_migration_entry_read(&swp_entry); 29894a705fefSNaoya Horiguchi entry = swp_entry_to_pte(swp_entry); 29904a705fefSNaoya Horiguchi set_huge_pte_at(src, addr, src_pte, entry); 29914a705fefSNaoya Horiguchi } 29924a705fefSNaoya Horiguchi set_huge_pte_at(dst, addr, dst_pte, entry); 29934a705fefSNaoya Horiguchi } else { 299434ee645eSJoerg Roedel if (cow) { 29957f2e9525SGerald Schaefer huge_ptep_set_wrprotect(src, addr, src_pte); 299634ee645eSJoerg Roedel mmu_notifier_invalidate_range(src, mmun_start, 299734ee645eSJoerg Roedel mmun_end); 299834ee645eSJoerg Roedel } 29990253d634SNaoya Horiguchi entry = huge_ptep_get(src_pte); 300063551ae0SDavid Gibson ptepage = pte_page(entry); 300163551ae0SDavid Gibson get_page(ptepage); 30020fe6e20bSNaoya Horiguchi page_dup_rmap(ptepage); 300363551ae0SDavid Gibson set_huge_pte_at(dst, addr, dst_pte, entry); 30041c59827dSHugh Dickins } 3005cb900f41SKirill A. Shutemov spin_unlock(src_ptl); 3006cb900f41SKirill A. Shutemov spin_unlock(dst_ptl); 300763551ae0SDavid Gibson } 300863551ae0SDavid Gibson 3009e8569dd2SAndreas Sandberg if (cow) 3010e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end); 3011e8569dd2SAndreas Sandberg 3012e8569dd2SAndreas Sandberg return ret; 301363551ae0SDavid Gibson } 301463551ae0SDavid Gibson 301524669e58SAneesh Kumar K.V void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, 301624669e58SAneesh Kumar K.V unsigned long start, unsigned long end, 301724669e58SAneesh Kumar K.V struct page *ref_page) 301863551ae0SDavid Gibson { 301924669e58SAneesh Kumar K.V int force_flush = 0; 302063551ae0SDavid Gibson struct mm_struct *mm = vma->vm_mm; 302163551ae0SDavid Gibson unsigned long address; 3022c7546f8fSDavid Gibson pte_t *ptep; 302363551ae0SDavid Gibson pte_t pte; 3024cb900f41SKirill A. Shutemov spinlock_t *ptl; 302563551ae0SDavid Gibson struct page *page; 3026a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3027a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 30282ec74c3eSSagi Grimberg const unsigned long mmun_start = start; /* For mmu_notifiers */ 30292ec74c3eSSagi Grimberg const unsigned long mmun_end = end; /* For mmu_notifiers */ 3030a5516438SAndi Kleen 303163551ae0SDavid Gibson WARN_ON(!is_vm_hugetlb_page(vma)); 3032a5516438SAndi Kleen BUG_ON(start & ~huge_page_mask(h)); 3033a5516438SAndi Kleen BUG_ON(end & ~huge_page_mask(h)); 303463551ae0SDavid Gibson 303524669e58SAneesh Kumar K.V tlb_start_vma(tlb, vma); 30362ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 3037569f48b8SHillf Danton address = start; 303824669e58SAneesh Kumar K.V again: 3039569f48b8SHillf Danton for (; address < end; address += sz) { 3040c7546f8fSDavid Gibson ptep = huge_pte_offset(mm, address); 3041c7546f8fSDavid Gibson if (!ptep) 3042c7546f8fSDavid Gibson continue; 3043c7546f8fSDavid Gibson 3044cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 304539dde65cSChen, Kenneth W if (huge_pmd_unshare(mm, &address, ptep)) 3046cb900f41SKirill A. Shutemov goto unlock; 304739dde65cSChen, Kenneth W 30486629326bSHillf Danton pte = huge_ptep_get(ptep); 30496629326bSHillf Danton if (huge_pte_none(pte)) 3050cb900f41SKirill A. Shutemov goto unlock; 30516629326bSHillf Danton 30526629326bSHillf Danton /* 30539fbc1f63SNaoya Horiguchi * Migrating hugepage or HWPoisoned hugepage is already 30549fbc1f63SNaoya Horiguchi * unmapped and its refcount is dropped, so just clear pte here. 30556629326bSHillf Danton */ 30569fbc1f63SNaoya Horiguchi if (unlikely(!pte_present(pte))) { 3057106c992aSGerald Schaefer huge_pte_clear(mm, address, ptep); 3058cb900f41SKirill A. Shutemov goto unlock; 30598c4894c6SNaoya Horiguchi } 30606629326bSHillf Danton 30616629326bSHillf Danton page = pte_page(pte); 306204f2cbe3SMel Gorman /* 306304f2cbe3SMel Gorman * If a reference page is supplied, it is because a specific 306404f2cbe3SMel Gorman * page is being unmapped, not a range. Ensure the page we 306504f2cbe3SMel Gorman * are about to unmap is the actual page of interest. 306604f2cbe3SMel Gorman */ 306704f2cbe3SMel Gorman if (ref_page) { 306804f2cbe3SMel Gorman if (page != ref_page) 3069cb900f41SKirill A. Shutemov goto unlock; 307004f2cbe3SMel Gorman 307104f2cbe3SMel Gorman /* 307204f2cbe3SMel Gorman * Mark the VMA as having unmapped its page so that 307304f2cbe3SMel Gorman * future faults in this VMA will fail rather than 307404f2cbe3SMel Gorman * looking like data was lost 307504f2cbe3SMel Gorman */ 307604f2cbe3SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); 307704f2cbe3SMel Gorman } 307804f2cbe3SMel Gorman 3079c7546f8fSDavid Gibson pte = huge_ptep_get_and_clear(mm, address, ptep); 308024669e58SAneesh Kumar K.V tlb_remove_tlb_entry(tlb, ptep, address); 3081106c992aSGerald Schaefer if (huge_pte_dirty(pte)) 30826649a386SKen Chen set_page_dirty(page); 30839e81130bSHillf Danton 308424669e58SAneesh Kumar K.V page_remove_rmap(page); 308524669e58SAneesh Kumar K.V force_flush = !__tlb_remove_page(tlb, page); 3086cb900f41SKirill A. Shutemov if (force_flush) { 3087569f48b8SHillf Danton address += sz; 3088cb900f41SKirill A. Shutemov spin_unlock(ptl); 30899e81130bSHillf Danton break; 309063551ae0SDavid Gibson } 3091cb900f41SKirill A. Shutemov /* Bail out after unmapping reference page if supplied */ 3092cb900f41SKirill A. Shutemov if (ref_page) { 3093cb900f41SKirill A. Shutemov spin_unlock(ptl); 3094cb900f41SKirill A. Shutemov break; 3095cb900f41SKirill A. Shutemov } 3096cb900f41SKirill A. Shutemov unlock: 3097cb900f41SKirill A. Shutemov spin_unlock(ptl); 3098cb900f41SKirill A. Shutemov } 309924669e58SAneesh Kumar K.V /* 310024669e58SAneesh Kumar K.V * mmu_gather ran out of room to batch pages, we break out of 310124669e58SAneesh Kumar K.V * the PTE lock to avoid doing the potential expensive TLB invalidate 310224669e58SAneesh Kumar K.V * and page-free while holding it. 310324669e58SAneesh Kumar K.V */ 310424669e58SAneesh Kumar K.V if (force_flush) { 310524669e58SAneesh Kumar K.V force_flush = 0; 310624669e58SAneesh Kumar K.V tlb_flush_mmu(tlb); 310724669e58SAneesh Kumar K.V if (address < end && !ref_page) 310824669e58SAneesh Kumar K.V goto again; 3109fe1668aeSChen, Kenneth W } 31102ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 311124669e58SAneesh Kumar K.V tlb_end_vma(tlb, vma); 31121da177e4SLinus Torvalds } 311363551ae0SDavid Gibson 3114d833352aSMel Gorman void __unmap_hugepage_range_final(struct mmu_gather *tlb, 3115d833352aSMel Gorman struct vm_area_struct *vma, unsigned long start, 3116d833352aSMel Gorman unsigned long end, struct page *ref_page) 3117d833352aSMel Gorman { 3118d833352aSMel Gorman __unmap_hugepage_range(tlb, vma, start, end, ref_page); 3119d833352aSMel Gorman 3120d833352aSMel Gorman /* 3121d833352aSMel Gorman * Clear this flag so that x86's huge_pmd_share page_table_shareable 3122d833352aSMel Gorman * test will fail on a vma being torn down, and not grab a page table 3123d833352aSMel Gorman * on its way out. We're lucky that the flag has such an appropriate 3124d833352aSMel Gorman * name, and can in fact be safely cleared here. We could clear it 3125d833352aSMel Gorman * before the __unmap_hugepage_range above, but all that's necessary 3126c8c06efaSDavidlohr Bueso * is to clear it before releasing the i_mmap_rwsem. This works 3127d833352aSMel Gorman * because in the context this is called, the VMA is about to be 3128c8c06efaSDavidlohr Bueso * destroyed and the i_mmap_rwsem is held. 3129d833352aSMel Gorman */ 3130d833352aSMel Gorman vma->vm_flags &= ~VM_MAYSHARE; 3131d833352aSMel Gorman } 3132d833352aSMel Gorman 3133502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 313404f2cbe3SMel Gorman unsigned long end, struct page *ref_page) 3135502717f4SChen, Kenneth W { 313624669e58SAneesh Kumar K.V struct mm_struct *mm; 313724669e58SAneesh Kumar K.V struct mmu_gather tlb; 313824669e58SAneesh Kumar K.V 313924669e58SAneesh Kumar K.V mm = vma->vm_mm; 314024669e58SAneesh Kumar K.V 31412b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, start, end); 314224669e58SAneesh Kumar K.V __unmap_hugepage_range(&tlb, vma, start, end, ref_page); 314324669e58SAneesh Kumar K.V tlb_finish_mmu(&tlb, start, end); 3144502717f4SChen, Kenneth W } 3145502717f4SChen, Kenneth W 314604f2cbe3SMel Gorman /* 314704f2cbe3SMel Gorman * This is called when the original mapper is failing to COW a MAP_PRIVATE 314804f2cbe3SMel Gorman * mappping it owns the reserve page for. The intention is to unmap the page 314904f2cbe3SMel Gorman * from other VMAs and let the children be SIGKILLed if they are faulting the 315004f2cbe3SMel Gorman * same region. 315104f2cbe3SMel Gorman */ 31522f4612afSDavidlohr Bueso static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, 31532a4b3dedSHarvey Harrison struct page *page, unsigned long address) 315404f2cbe3SMel Gorman { 31557526674dSAdam Litke struct hstate *h = hstate_vma(vma); 315604f2cbe3SMel Gorman struct vm_area_struct *iter_vma; 315704f2cbe3SMel Gorman struct address_space *mapping; 315804f2cbe3SMel Gorman pgoff_t pgoff; 315904f2cbe3SMel Gorman 316004f2cbe3SMel Gorman /* 316104f2cbe3SMel Gorman * vm_pgoff is in PAGE_SIZE units, hence the different calculation 316204f2cbe3SMel Gorman * from page cache lookup which is in HPAGE_SIZE units. 316304f2cbe3SMel Gorman */ 31647526674dSAdam Litke address = address & huge_page_mask(h); 316536e4f20aSMichal Hocko pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + 316636e4f20aSMichal Hocko vma->vm_pgoff; 3167496ad9aaSAl Viro mapping = file_inode(vma->vm_file)->i_mapping; 316804f2cbe3SMel Gorman 31694eb2b1dcSMel Gorman /* 31704eb2b1dcSMel Gorman * Take the mapping lock for the duration of the table walk. As 31714eb2b1dcSMel Gorman * this mapping should be shared between all the VMAs, 31724eb2b1dcSMel Gorman * __unmap_hugepage_range() is called as the lock is already held 31734eb2b1dcSMel Gorman */ 317483cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 31756b2dbba8SMichel Lespinasse vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { 317604f2cbe3SMel Gorman /* Do not unmap the current VMA */ 317704f2cbe3SMel Gorman if (iter_vma == vma) 317804f2cbe3SMel Gorman continue; 317904f2cbe3SMel Gorman 318004f2cbe3SMel Gorman /* 318104f2cbe3SMel Gorman * Unmap the page from other VMAs without their own reserves. 318204f2cbe3SMel Gorman * They get marked to be SIGKILLed if they fault in these 318304f2cbe3SMel Gorman * areas. This is because a future no-page fault on this VMA 318404f2cbe3SMel Gorman * could insert a zeroed page instead of the data existing 318504f2cbe3SMel Gorman * from the time of fork. This would look like data corruption 318604f2cbe3SMel Gorman */ 318704f2cbe3SMel Gorman if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) 318824669e58SAneesh Kumar K.V unmap_hugepage_range(iter_vma, address, 318924669e58SAneesh Kumar K.V address + huge_page_size(h), page); 319004f2cbe3SMel Gorman } 319183cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 319204f2cbe3SMel Gorman } 319304f2cbe3SMel Gorman 31940fe6e20bSNaoya Horiguchi /* 31950fe6e20bSNaoya Horiguchi * Hugetlb_cow() should be called with page lock of the original hugepage held. 3196ef009b25SMichal Hocko * Called with hugetlb_instantiation_mutex held and pte_page locked so we 3197ef009b25SMichal Hocko * cannot race with other handlers or page migration. 3198ef009b25SMichal Hocko * Keep the pte_same checks anyway to make transition from the mutex easier. 31990fe6e20bSNaoya Horiguchi */ 32001e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, 320104f2cbe3SMel Gorman unsigned long address, pte_t *ptep, pte_t pte, 3202cb900f41SKirill A. Shutemov struct page *pagecache_page, spinlock_t *ptl) 32031e8f889bSDavid Gibson { 3204a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 32051e8f889bSDavid Gibson struct page *old_page, *new_page; 3206ad4404a2SDavidlohr Bueso int ret = 0, outside_reserve = 0; 32072ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 32082ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 32091e8f889bSDavid Gibson 32101e8f889bSDavid Gibson old_page = pte_page(pte); 32111e8f889bSDavid Gibson 321204f2cbe3SMel Gorman retry_avoidcopy: 32131e8f889bSDavid Gibson /* If no-one else is actually using this page, avoid the copy 32141e8f889bSDavid Gibson * and just make the page writable */ 321537a2140dSJoonsoo Kim if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { 32160fe6e20bSNaoya Horiguchi page_move_anon_rmap(old_page, vma, address); 32171e8f889bSDavid Gibson set_huge_ptep_writable(vma, address, ptep); 321883c54070SNick Piggin return 0; 32191e8f889bSDavid Gibson } 32201e8f889bSDavid Gibson 322104f2cbe3SMel Gorman /* 322204f2cbe3SMel Gorman * If the process that created a MAP_PRIVATE mapping is about to 322304f2cbe3SMel Gorman * perform a COW due to a shared page count, attempt to satisfy 322404f2cbe3SMel Gorman * the allocation without using the existing reserves. The pagecache 322504f2cbe3SMel Gorman * page is used to determine if the reserve at this address was 322604f2cbe3SMel Gorman * consumed or not. If reserves were used, a partial faulted mapping 322704f2cbe3SMel Gorman * at the time of fork() could consume its reserves on COW instead 322804f2cbe3SMel Gorman * of the full address range. 322904f2cbe3SMel Gorman */ 32305944d011SJoonsoo Kim if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && 323104f2cbe3SMel Gorman old_page != pagecache_page) 323204f2cbe3SMel Gorman outside_reserve = 1; 323304f2cbe3SMel Gorman 32341e8f889bSDavid Gibson page_cache_get(old_page); 3235b76c8cfbSLarry Woodman 3236ad4404a2SDavidlohr Bueso /* 3237ad4404a2SDavidlohr Bueso * Drop page table lock as buddy allocator may be called. It will 3238ad4404a2SDavidlohr Bueso * be acquired again before returning to the caller, as expected. 3239ad4404a2SDavidlohr Bueso */ 3240cb900f41SKirill A. Shutemov spin_unlock(ptl); 324104f2cbe3SMel Gorman new_page = alloc_huge_page(vma, address, outside_reserve); 32421e8f889bSDavid Gibson 32432fc39cecSAdam Litke if (IS_ERR(new_page)) { 324404f2cbe3SMel Gorman /* 324504f2cbe3SMel Gorman * If a process owning a MAP_PRIVATE mapping fails to COW, 324604f2cbe3SMel Gorman * it is due to references held by a child and an insufficient 324704f2cbe3SMel Gorman * huge page pool. To guarantee the original mappers 324804f2cbe3SMel Gorman * reliability, unmap the page from child processes. The child 324904f2cbe3SMel Gorman * may get SIGKILLed if it later faults. 325004f2cbe3SMel Gorman */ 325104f2cbe3SMel Gorman if (outside_reserve) { 3252ad4404a2SDavidlohr Bueso page_cache_release(old_page); 325304f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 32542f4612afSDavidlohr Bueso unmap_ref_private(mm, vma, old_page, address); 325504f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 3256cb900f41SKirill A. Shutemov spin_lock(ptl); 3257a734bcc8SHillf Danton ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 3258a9af0c5dSNaoya Horiguchi if (likely(ptep && 3259a9af0c5dSNaoya Horiguchi pte_same(huge_ptep_get(ptep), pte))) 326004f2cbe3SMel Gorman goto retry_avoidcopy; 3261a734bcc8SHillf Danton /* 3262cb900f41SKirill A. Shutemov * race occurs while re-acquiring page table 3263cb900f41SKirill A. Shutemov * lock, and our job is done. 3264a734bcc8SHillf Danton */ 3265a734bcc8SHillf Danton return 0; 326604f2cbe3SMel Gorman } 326704f2cbe3SMel Gorman 3268ad4404a2SDavidlohr Bueso ret = (PTR_ERR(new_page) == -ENOMEM) ? 3269ad4404a2SDavidlohr Bueso VM_FAULT_OOM : VM_FAULT_SIGBUS; 3270ad4404a2SDavidlohr Bueso goto out_release_old; 32711e8f889bSDavid Gibson } 32721e8f889bSDavid Gibson 32730fe6e20bSNaoya Horiguchi /* 32740fe6e20bSNaoya Horiguchi * When the original hugepage is shared one, it does not have 32750fe6e20bSNaoya Horiguchi * anon_vma prepared. 32760fe6e20bSNaoya Horiguchi */ 327744e2aa93SDean Nelson if (unlikely(anon_vma_prepare(vma))) { 3278ad4404a2SDavidlohr Bueso ret = VM_FAULT_OOM; 3279ad4404a2SDavidlohr Bueso goto out_release_all; 328044e2aa93SDean Nelson } 32810fe6e20bSNaoya Horiguchi 328247ad8475SAndrea Arcangeli copy_user_huge_page(new_page, old_page, address, vma, 328347ad8475SAndrea Arcangeli pages_per_huge_page(h)); 32840ed361deSNick Piggin __SetPageUptodate(new_page); 3285bcc54222SNaoya Horiguchi set_page_huge_active(new_page); 32861e8f889bSDavid Gibson 32872ec74c3eSSagi Grimberg mmun_start = address & huge_page_mask(h); 32882ec74c3eSSagi Grimberg mmun_end = mmun_start + huge_page_size(h); 32892ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 3290ad4404a2SDavidlohr Bueso 3291b76c8cfbSLarry Woodman /* 3292cb900f41SKirill A. Shutemov * Retake the page table lock to check for racing updates 3293b76c8cfbSLarry Woodman * before the page tables are altered 3294b76c8cfbSLarry Woodman */ 3295cb900f41SKirill A. Shutemov spin_lock(ptl); 3296a5516438SAndi Kleen ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 3297a9af0c5dSNaoya Horiguchi if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { 329807443a85SJoonsoo Kim ClearPagePrivate(new_page); 329907443a85SJoonsoo Kim 33001e8f889bSDavid Gibson /* Break COW */ 33018fe627ecSGerald Schaefer huge_ptep_clear_flush(vma, address, ptep); 330234ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, mmun_start, mmun_end); 33031e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, 33041e8f889bSDavid Gibson make_huge_pte(vma, new_page, 1)); 33050fe6e20bSNaoya Horiguchi page_remove_rmap(old_page); 3306cd67f0d2SNaoya Horiguchi hugepage_add_new_anon_rmap(new_page, vma, address); 33071e8f889bSDavid Gibson /* Make the old page be freed below */ 33081e8f889bSDavid Gibson new_page = old_page; 33091e8f889bSDavid Gibson } 3310cb900f41SKirill A. Shutemov spin_unlock(ptl); 33112ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 3312ad4404a2SDavidlohr Bueso out_release_all: 33131e8f889bSDavid Gibson page_cache_release(new_page); 3314ad4404a2SDavidlohr Bueso out_release_old: 33151e8f889bSDavid Gibson page_cache_release(old_page); 33168312034fSJoonsoo Kim 3317ad4404a2SDavidlohr Bueso spin_lock(ptl); /* Caller expects lock to be held */ 3318ad4404a2SDavidlohr Bueso return ret; 33191e8f889bSDavid Gibson } 33201e8f889bSDavid Gibson 332104f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */ 3322a5516438SAndi Kleen static struct page *hugetlbfs_pagecache_page(struct hstate *h, 3323a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 332404f2cbe3SMel Gorman { 332504f2cbe3SMel Gorman struct address_space *mapping; 3326e7c4b0bfSAndy Whitcroft pgoff_t idx; 332704f2cbe3SMel Gorman 332804f2cbe3SMel Gorman mapping = vma->vm_file->f_mapping; 3329a5516438SAndi Kleen idx = vma_hugecache_offset(h, vma, address); 333004f2cbe3SMel Gorman 333104f2cbe3SMel Gorman return find_lock_page(mapping, idx); 333204f2cbe3SMel Gorman } 333304f2cbe3SMel Gorman 33343ae77f43SHugh Dickins /* 33353ae77f43SHugh Dickins * Return whether there is a pagecache page to back given address within VMA. 33363ae77f43SHugh Dickins * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page. 33373ae77f43SHugh Dickins */ 33383ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h, 33392a15efc9SHugh Dickins struct vm_area_struct *vma, unsigned long address) 33402a15efc9SHugh Dickins { 33412a15efc9SHugh Dickins struct address_space *mapping; 33422a15efc9SHugh Dickins pgoff_t idx; 33432a15efc9SHugh Dickins struct page *page; 33442a15efc9SHugh Dickins 33452a15efc9SHugh Dickins mapping = vma->vm_file->f_mapping; 33462a15efc9SHugh Dickins idx = vma_hugecache_offset(h, vma, address); 33472a15efc9SHugh Dickins 33482a15efc9SHugh Dickins page = find_get_page(mapping, idx); 33492a15efc9SHugh Dickins if (page) 33502a15efc9SHugh Dickins put_page(page); 33512a15efc9SHugh Dickins return page != NULL; 33522a15efc9SHugh Dickins } 33532a15efc9SHugh Dickins 3354a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, 33558382d914SDavidlohr Bueso struct address_space *mapping, pgoff_t idx, 3356788c7df4SHugh Dickins unsigned long address, pte_t *ptep, unsigned int flags) 3357ac9b9c66SHugh Dickins { 3358a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3359ac9b9c66SHugh Dickins int ret = VM_FAULT_SIGBUS; 3360409eb8c2SHillf Danton int anon_rmap = 0; 33614c887265SAdam Litke unsigned long size; 33624c887265SAdam Litke struct page *page; 33631e8f889bSDavid Gibson pte_t new_pte; 3364cb900f41SKirill A. Shutemov spinlock_t *ptl; 33654c887265SAdam Litke 336604f2cbe3SMel Gorman /* 336704f2cbe3SMel Gorman * Currently, we are forced to kill the process in the event the 336804f2cbe3SMel Gorman * original mapper has unmapped pages from the child due to a failed 336925985edcSLucas De Marchi * COW. Warn that such a situation has occurred as it may not be obvious 337004f2cbe3SMel Gorman */ 337104f2cbe3SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { 3372ffb22af5SAndrew Morton pr_warning("PID %d killed due to inadequate hugepage pool\n", 337304f2cbe3SMel Gorman current->pid); 337404f2cbe3SMel Gorman return ret; 337504f2cbe3SMel Gorman } 337604f2cbe3SMel Gorman 33774c887265SAdam Litke /* 33784c887265SAdam Litke * Use page lock to guard against racing truncation 33794c887265SAdam Litke * before we get page_table_lock. 33804c887265SAdam Litke */ 33816bda666aSChristoph Lameter retry: 33826bda666aSChristoph Lameter page = find_lock_page(mapping, idx); 33836bda666aSChristoph Lameter if (!page) { 3384a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 3385ebed4bfcSHugh Dickins if (idx >= size) 3386ebed4bfcSHugh Dickins goto out; 338704f2cbe3SMel Gorman page = alloc_huge_page(vma, address, 0); 33882fc39cecSAdam Litke if (IS_ERR(page)) { 338976dcee75SAneesh Kumar K.V ret = PTR_ERR(page); 339076dcee75SAneesh Kumar K.V if (ret == -ENOMEM) 339176dcee75SAneesh Kumar K.V ret = VM_FAULT_OOM; 339276dcee75SAneesh Kumar K.V else 339376dcee75SAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 33946bda666aSChristoph Lameter goto out; 33956bda666aSChristoph Lameter } 339647ad8475SAndrea Arcangeli clear_huge_page(page, address, pages_per_huge_page(h)); 33970ed361deSNick Piggin __SetPageUptodate(page); 3398bcc54222SNaoya Horiguchi set_page_huge_active(page); 3399ac9b9c66SHugh Dickins 3400f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) { 34016bda666aSChristoph Lameter int err; 340245c682a6SKen Chen struct inode *inode = mapping->host; 34036bda666aSChristoph Lameter 34046bda666aSChristoph Lameter err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); 34056bda666aSChristoph Lameter if (err) { 34066bda666aSChristoph Lameter put_page(page); 34076bda666aSChristoph Lameter if (err == -EEXIST) 34086bda666aSChristoph Lameter goto retry; 34096bda666aSChristoph Lameter goto out; 34106bda666aSChristoph Lameter } 341107443a85SJoonsoo Kim ClearPagePrivate(page); 341245c682a6SKen Chen 341345c682a6SKen Chen spin_lock(&inode->i_lock); 3414a5516438SAndi Kleen inode->i_blocks += blocks_per_huge_page(h); 341545c682a6SKen Chen spin_unlock(&inode->i_lock); 341623be7468SMel Gorman } else { 34176bda666aSChristoph Lameter lock_page(page); 34180fe6e20bSNaoya Horiguchi if (unlikely(anon_vma_prepare(vma))) { 34190fe6e20bSNaoya Horiguchi ret = VM_FAULT_OOM; 34200fe6e20bSNaoya Horiguchi goto backout_unlocked; 342123be7468SMel Gorman } 3422409eb8c2SHillf Danton anon_rmap = 1; 34230fe6e20bSNaoya Horiguchi } 34240fe6e20bSNaoya Horiguchi } else { 342557303d80SAndy Whitcroft /* 3426998b4382SNaoya Horiguchi * If memory error occurs between mmap() and fault, some process 3427998b4382SNaoya Horiguchi * don't have hwpoisoned swap entry for errored virtual address. 3428998b4382SNaoya Horiguchi * So we need to block hugepage fault by PG_hwpoison bit check. 3429fd6a03edSNaoya Horiguchi */ 3430fd6a03edSNaoya Horiguchi if (unlikely(PageHWPoison(page))) { 3431aa50d3a7SAndi Kleen ret = VM_FAULT_HWPOISON | 3432972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 3433fd6a03edSNaoya Horiguchi goto backout_unlocked; 34346bda666aSChristoph Lameter } 3435998b4382SNaoya Horiguchi } 34361e8f889bSDavid Gibson 343757303d80SAndy Whitcroft /* 343857303d80SAndy Whitcroft * If we are going to COW a private mapping later, we examine the 343957303d80SAndy Whitcroft * pending reservations for this page now. This will ensure that 344057303d80SAndy Whitcroft * any allocations necessary to record that reservation occur outside 344157303d80SAndy Whitcroft * the spinlock. 344257303d80SAndy Whitcroft */ 34435e911373SMike Kravetz if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 34442b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 34452b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 34462b26736cSAndy Whitcroft goto backout_unlocked; 34472b26736cSAndy Whitcroft } 34485e911373SMike Kravetz /* Just decrements count, does not deallocate */ 3449feba16e2SMike Kravetz vma_end_reservation(h, vma, address); 34505e911373SMike Kravetz } 345157303d80SAndy Whitcroft 3452cb900f41SKirill A. Shutemov ptl = huge_pte_lockptr(h, mm, ptep); 3453cb900f41SKirill A. Shutemov spin_lock(ptl); 3454a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 34554c887265SAdam Litke if (idx >= size) 34564c887265SAdam Litke goto backout; 34574c887265SAdam Litke 345883c54070SNick Piggin ret = 0; 34597f2e9525SGerald Schaefer if (!huge_pte_none(huge_ptep_get(ptep))) 34604c887265SAdam Litke goto backout; 34614c887265SAdam Litke 346207443a85SJoonsoo Kim if (anon_rmap) { 346307443a85SJoonsoo Kim ClearPagePrivate(page); 3464409eb8c2SHillf Danton hugepage_add_new_anon_rmap(page, vma, address); 3465ac714904SChoi Gi-yong } else 3466409eb8c2SHillf Danton page_dup_rmap(page); 34671e8f889bSDavid Gibson new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) 34681e8f889bSDavid Gibson && (vma->vm_flags & VM_SHARED))); 34691e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, new_pte); 34701e8f889bSDavid Gibson 3471788c7df4SHugh Dickins if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 34721e8f889bSDavid Gibson /* Optimization, do the COW without a second fault */ 3473cb900f41SKirill A. Shutemov ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page, ptl); 34741e8f889bSDavid Gibson } 34751e8f889bSDavid Gibson 3476cb900f41SKirill A. Shutemov spin_unlock(ptl); 34774c887265SAdam Litke unlock_page(page); 34784c887265SAdam Litke out: 3479ac9b9c66SHugh Dickins return ret; 34804c887265SAdam Litke 34814c887265SAdam Litke backout: 3482cb900f41SKirill A. Shutemov spin_unlock(ptl); 34832b26736cSAndy Whitcroft backout_unlocked: 34844c887265SAdam Litke unlock_page(page); 34854c887265SAdam Litke put_page(page); 34864c887265SAdam Litke goto out; 3487ac9b9c66SHugh Dickins } 3488ac9b9c66SHugh Dickins 34898382d914SDavidlohr Bueso #ifdef CONFIG_SMP 3490c672c7f2SMike Kravetz u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 34918382d914SDavidlohr Bueso struct vm_area_struct *vma, 34928382d914SDavidlohr Bueso struct address_space *mapping, 34938382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 34948382d914SDavidlohr Bueso { 34958382d914SDavidlohr Bueso unsigned long key[2]; 34968382d914SDavidlohr Bueso u32 hash; 34978382d914SDavidlohr Bueso 34988382d914SDavidlohr Bueso if (vma->vm_flags & VM_SHARED) { 34998382d914SDavidlohr Bueso key[0] = (unsigned long) mapping; 35008382d914SDavidlohr Bueso key[1] = idx; 35018382d914SDavidlohr Bueso } else { 35028382d914SDavidlohr Bueso key[0] = (unsigned long) mm; 35038382d914SDavidlohr Bueso key[1] = address >> huge_page_shift(h); 35048382d914SDavidlohr Bueso } 35058382d914SDavidlohr Bueso 35068382d914SDavidlohr Bueso hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0); 35078382d914SDavidlohr Bueso 35088382d914SDavidlohr Bueso return hash & (num_fault_mutexes - 1); 35098382d914SDavidlohr Bueso } 35108382d914SDavidlohr Bueso #else 35118382d914SDavidlohr Bueso /* 35128382d914SDavidlohr Bueso * For uniprocesor systems we always use a single mutex, so just 35138382d914SDavidlohr Bueso * return 0 and avoid the hashing overhead. 35148382d914SDavidlohr Bueso */ 3515c672c7f2SMike Kravetz u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 35168382d914SDavidlohr Bueso struct vm_area_struct *vma, 35178382d914SDavidlohr Bueso struct address_space *mapping, 35188382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 35198382d914SDavidlohr Bueso { 35208382d914SDavidlohr Bueso return 0; 35218382d914SDavidlohr Bueso } 35228382d914SDavidlohr Bueso #endif 35238382d914SDavidlohr Bueso 352486e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3525788c7df4SHugh Dickins unsigned long address, unsigned int flags) 352686e5216fSAdam Litke { 35278382d914SDavidlohr Bueso pte_t *ptep, entry; 3528cb900f41SKirill A. Shutemov spinlock_t *ptl; 35291e8f889bSDavid Gibson int ret; 35308382d914SDavidlohr Bueso u32 hash; 35318382d914SDavidlohr Bueso pgoff_t idx; 35320fe6e20bSNaoya Horiguchi struct page *page = NULL; 353357303d80SAndy Whitcroft struct page *pagecache_page = NULL; 3534a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 35358382d914SDavidlohr Bueso struct address_space *mapping; 35360f792cf9SNaoya Horiguchi int need_wait_lock = 0; 353786e5216fSAdam Litke 35381e16a539SKAMEZAWA Hiroyuki address &= huge_page_mask(h); 35391e16a539SKAMEZAWA Hiroyuki 3540fd6a03edSNaoya Horiguchi ptep = huge_pte_offset(mm, address); 3541fd6a03edSNaoya Horiguchi if (ptep) { 3542fd6a03edSNaoya Horiguchi entry = huge_ptep_get(ptep); 3543290408d4SNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(entry))) { 3544cb900f41SKirill A. Shutemov migration_entry_wait_huge(vma, mm, ptep); 3545290408d4SNaoya Horiguchi return 0; 3546290408d4SNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) 3547aa50d3a7SAndi Kleen return VM_FAULT_HWPOISON_LARGE | 3548972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 3549fd6a03edSNaoya Horiguchi } 3550fd6a03edSNaoya Horiguchi 3551a5516438SAndi Kleen ptep = huge_pte_alloc(mm, address, huge_page_size(h)); 355286e5216fSAdam Litke if (!ptep) 355386e5216fSAdam Litke return VM_FAULT_OOM; 355486e5216fSAdam Litke 35558382d914SDavidlohr Bueso mapping = vma->vm_file->f_mapping; 35568382d914SDavidlohr Bueso idx = vma_hugecache_offset(h, vma, address); 35578382d914SDavidlohr Bueso 35583935baa9SDavid Gibson /* 35593935baa9SDavid Gibson * Serialize hugepage allocation and instantiation, so that we don't 35603935baa9SDavid Gibson * get spurious allocation failures if two CPUs race to instantiate 35613935baa9SDavid Gibson * the same page in the page cache. 35623935baa9SDavid Gibson */ 3563c672c7f2SMike Kravetz hash = hugetlb_fault_mutex_hash(h, mm, vma, mapping, idx, address); 3564c672c7f2SMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 35658382d914SDavidlohr Bueso 35667f2e9525SGerald Schaefer entry = huge_ptep_get(ptep); 35677f2e9525SGerald Schaefer if (huge_pte_none(entry)) { 35688382d914SDavidlohr Bueso ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags); 3569b4d1d99fSDavid Gibson goto out_mutex; 35703935baa9SDavid Gibson } 357186e5216fSAdam Litke 357283c54070SNick Piggin ret = 0; 35731e8f889bSDavid Gibson 357457303d80SAndy Whitcroft /* 35750f792cf9SNaoya Horiguchi * entry could be a migration/hwpoison entry at this point, so this 35760f792cf9SNaoya Horiguchi * check prevents the kernel from going below assuming that we have 35770f792cf9SNaoya Horiguchi * a active hugepage in pagecache. This goto expects the 2nd page fault, 35780f792cf9SNaoya Horiguchi * and is_hugetlb_entry_(migration|hwpoisoned) check will properly 35790f792cf9SNaoya Horiguchi * handle it. 35800f792cf9SNaoya Horiguchi */ 35810f792cf9SNaoya Horiguchi if (!pte_present(entry)) 35820f792cf9SNaoya Horiguchi goto out_mutex; 35830f792cf9SNaoya Horiguchi 35840f792cf9SNaoya Horiguchi /* 358557303d80SAndy Whitcroft * If we are going to COW the mapping later, we examine the pending 358657303d80SAndy Whitcroft * reservations for this page now. This will ensure that any 358757303d80SAndy Whitcroft * allocations necessary to record that reservation occur outside the 358857303d80SAndy Whitcroft * spinlock. For private mappings, we also lookup the pagecache 358957303d80SAndy Whitcroft * page now as it is used to determine if a reservation has been 359057303d80SAndy Whitcroft * consumed. 359157303d80SAndy Whitcroft */ 3592106c992aSGerald Schaefer if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { 35932b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 35942b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 3595b4d1d99fSDavid Gibson goto out_mutex; 35962b26736cSAndy Whitcroft } 35975e911373SMike Kravetz /* Just decrements count, does not deallocate */ 3598feba16e2SMike Kravetz vma_end_reservation(h, vma, address); 359957303d80SAndy Whitcroft 3600f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 360157303d80SAndy Whitcroft pagecache_page = hugetlbfs_pagecache_page(h, 360257303d80SAndy Whitcroft vma, address); 360357303d80SAndy Whitcroft } 360457303d80SAndy Whitcroft 36050f792cf9SNaoya Horiguchi ptl = huge_pte_lock(h, mm, ptep); 36060fe6e20bSNaoya Horiguchi 36071e8f889bSDavid Gibson /* Check for a racing update before calling hugetlb_cow */ 3608b4d1d99fSDavid Gibson if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) 3609cb900f41SKirill A. Shutemov goto out_ptl; 3610b4d1d99fSDavid Gibson 36110f792cf9SNaoya Horiguchi /* 36120f792cf9SNaoya Horiguchi * hugetlb_cow() requires page locks of pte_page(entry) and 36130f792cf9SNaoya Horiguchi * pagecache_page, so here we need take the former one 36140f792cf9SNaoya Horiguchi * when page != pagecache_page or !pagecache_page. 36150f792cf9SNaoya Horiguchi */ 36160f792cf9SNaoya Horiguchi page = pte_page(entry); 36170f792cf9SNaoya Horiguchi if (page != pagecache_page) 36180f792cf9SNaoya Horiguchi if (!trylock_page(page)) { 36190f792cf9SNaoya Horiguchi need_wait_lock = 1; 36200f792cf9SNaoya Horiguchi goto out_ptl; 36210f792cf9SNaoya Horiguchi } 36220f792cf9SNaoya Horiguchi 36230f792cf9SNaoya Horiguchi get_page(page); 3624b4d1d99fSDavid Gibson 3625788c7df4SHugh Dickins if (flags & FAULT_FLAG_WRITE) { 3626106c992aSGerald Schaefer if (!huge_pte_write(entry)) { 362757303d80SAndy Whitcroft ret = hugetlb_cow(mm, vma, address, ptep, entry, 3628cb900f41SKirill A. Shutemov pagecache_page, ptl); 36290f792cf9SNaoya Horiguchi goto out_put_page; 3630b4d1d99fSDavid Gibson } 3631106c992aSGerald Schaefer entry = huge_pte_mkdirty(entry); 3632b4d1d99fSDavid Gibson } 3633b4d1d99fSDavid Gibson entry = pte_mkyoung(entry); 3634788c7df4SHugh Dickins if (huge_ptep_set_access_flags(vma, address, ptep, entry, 3635788c7df4SHugh Dickins flags & FAULT_FLAG_WRITE)) 36364b3073e1SRussell King update_mmu_cache(vma, address, ptep); 36370f792cf9SNaoya Horiguchi out_put_page: 36380f792cf9SNaoya Horiguchi if (page != pagecache_page) 36390f792cf9SNaoya Horiguchi unlock_page(page); 36400f792cf9SNaoya Horiguchi put_page(page); 3641cb900f41SKirill A. Shutemov out_ptl: 3642cb900f41SKirill A. Shutemov spin_unlock(ptl); 364357303d80SAndy Whitcroft 364457303d80SAndy Whitcroft if (pagecache_page) { 364557303d80SAndy Whitcroft unlock_page(pagecache_page); 364657303d80SAndy Whitcroft put_page(pagecache_page); 364757303d80SAndy Whitcroft } 3648b4d1d99fSDavid Gibson out_mutex: 3649c672c7f2SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 36500f792cf9SNaoya Horiguchi /* 36510f792cf9SNaoya Horiguchi * Generally it's safe to hold refcount during waiting page lock. But 36520f792cf9SNaoya Horiguchi * here we just wait to defer the next page fault to avoid busy loop and 36530f792cf9SNaoya Horiguchi * the page is not used after unlocked before returning from the current 36540f792cf9SNaoya Horiguchi * page fault. So we are safe from accessing freed page, even if we wait 36550f792cf9SNaoya Horiguchi * here without taking refcount. 36560f792cf9SNaoya Horiguchi */ 36570f792cf9SNaoya Horiguchi if (need_wait_lock) 36580f792cf9SNaoya Horiguchi wait_on_page_locked(page); 36591e8f889bSDavid Gibson return ret; 366086e5216fSAdam Litke } 366186e5216fSAdam Litke 366228a35716SMichel Lespinasse long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, 366363551ae0SDavid Gibson struct page **pages, struct vm_area_struct **vmas, 366428a35716SMichel Lespinasse unsigned long *position, unsigned long *nr_pages, 366528a35716SMichel Lespinasse long i, unsigned int flags) 366663551ae0SDavid Gibson { 3667d5d4b0aaSChen, Kenneth W unsigned long pfn_offset; 3668d5d4b0aaSChen, Kenneth W unsigned long vaddr = *position; 366928a35716SMichel Lespinasse unsigned long remainder = *nr_pages; 3670a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 367163551ae0SDavid Gibson 367263551ae0SDavid Gibson while (vaddr < vma->vm_end && remainder) { 367363551ae0SDavid Gibson pte_t *pte; 3674cb900f41SKirill A. Shutemov spinlock_t *ptl = NULL; 36752a15efc9SHugh Dickins int absent; 367663551ae0SDavid Gibson struct page *page; 367763551ae0SDavid Gibson 36784c887265SAdam Litke /* 367902057967SDavid Rientjes * If we have a pending SIGKILL, don't keep faulting pages and 368002057967SDavid Rientjes * potentially allocating memory. 368102057967SDavid Rientjes */ 368202057967SDavid Rientjes if (unlikely(fatal_signal_pending(current))) { 368302057967SDavid Rientjes remainder = 0; 368402057967SDavid Rientjes break; 368502057967SDavid Rientjes } 368602057967SDavid Rientjes 368702057967SDavid Rientjes /* 36884c887265SAdam Litke * Some archs (sparc64, sh*) have multiple pte_ts to 36892a15efc9SHugh Dickins * each hugepage. We have to make sure we get the 36904c887265SAdam Litke * first, for the page indexing below to work. 3691cb900f41SKirill A. Shutemov * 3692cb900f41SKirill A. Shutemov * Note that page table lock is not held when pte is null. 36934c887265SAdam Litke */ 3694a5516438SAndi Kleen pte = huge_pte_offset(mm, vaddr & huge_page_mask(h)); 3695cb900f41SKirill A. Shutemov if (pte) 3696cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, pte); 36972a15efc9SHugh Dickins absent = !pte || huge_pte_none(huge_ptep_get(pte)); 369863551ae0SDavid Gibson 36992a15efc9SHugh Dickins /* 37002a15efc9SHugh Dickins * When coredumping, it suits get_dump_page if we just return 37013ae77f43SHugh Dickins * an error where there's an empty slot with no huge pagecache 37023ae77f43SHugh Dickins * to back it. This way, we avoid allocating a hugepage, and 37033ae77f43SHugh Dickins * the sparse dumpfile avoids allocating disk blocks, but its 37043ae77f43SHugh Dickins * huge holes still show up with zeroes where they need to be. 37052a15efc9SHugh Dickins */ 37063ae77f43SHugh Dickins if (absent && (flags & FOLL_DUMP) && 37073ae77f43SHugh Dickins !hugetlbfs_pagecache_present(h, vma, vaddr)) { 3708cb900f41SKirill A. Shutemov if (pte) 3709cb900f41SKirill A. Shutemov spin_unlock(ptl); 37102a15efc9SHugh Dickins remainder = 0; 37112a15efc9SHugh Dickins break; 37122a15efc9SHugh Dickins } 37132a15efc9SHugh Dickins 37149cc3a5bdSNaoya Horiguchi /* 37159cc3a5bdSNaoya Horiguchi * We need call hugetlb_fault for both hugepages under migration 37169cc3a5bdSNaoya Horiguchi * (in which case hugetlb_fault waits for the migration,) and 37179cc3a5bdSNaoya Horiguchi * hwpoisoned hugepages (in which case we need to prevent the 37189cc3a5bdSNaoya Horiguchi * caller from accessing to them.) In order to do this, we use 37199cc3a5bdSNaoya Horiguchi * here is_swap_pte instead of is_hugetlb_entry_migration and 37209cc3a5bdSNaoya Horiguchi * is_hugetlb_entry_hwpoisoned. This is because it simply covers 37219cc3a5bdSNaoya Horiguchi * both cases, and because we can't follow correct pages 37229cc3a5bdSNaoya Horiguchi * directly from any kind of swap entries. 37239cc3a5bdSNaoya Horiguchi */ 37249cc3a5bdSNaoya Horiguchi if (absent || is_swap_pte(huge_ptep_get(pte)) || 3725106c992aSGerald Schaefer ((flags & FOLL_WRITE) && 3726106c992aSGerald Schaefer !huge_pte_write(huge_ptep_get(pte)))) { 37274c887265SAdam Litke int ret; 37284c887265SAdam Litke 3729cb900f41SKirill A. Shutemov if (pte) 3730cb900f41SKirill A. Shutemov spin_unlock(ptl); 37312a15efc9SHugh Dickins ret = hugetlb_fault(mm, vma, vaddr, 37322a15efc9SHugh Dickins (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0); 3733a89182c7SAdam Litke if (!(ret & VM_FAULT_ERROR)) 37344c887265SAdam Litke continue; 37354c887265SAdam Litke 37361c59827dSHugh Dickins remainder = 0; 37371c59827dSHugh Dickins break; 37381c59827dSHugh Dickins } 373963551ae0SDavid Gibson 3740a5516438SAndi Kleen pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; 37417f2e9525SGerald Schaefer page = pte_page(huge_ptep_get(pte)); 3742d5d4b0aaSChen, Kenneth W same_page: 3743d6692183SChen, Kenneth W if (pages) { 374469d177c2SAndy Whitcroft pages[i] = mem_map_offset(page, pfn_offset); 3745a0368d4eSAndrea Arcangeli get_page_foll(pages[i]); 3746d6692183SChen, Kenneth W } 374763551ae0SDavid Gibson 374863551ae0SDavid Gibson if (vmas) 374963551ae0SDavid Gibson vmas[i] = vma; 375063551ae0SDavid Gibson 375163551ae0SDavid Gibson vaddr += PAGE_SIZE; 3752d5d4b0aaSChen, Kenneth W ++pfn_offset; 375363551ae0SDavid Gibson --remainder; 375463551ae0SDavid Gibson ++i; 3755d5d4b0aaSChen, Kenneth W if (vaddr < vma->vm_end && remainder && 3756a5516438SAndi Kleen pfn_offset < pages_per_huge_page(h)) { 3757d5d4b0aaSChen, Kenneth W /* 3758d5d4b0aaSChen, Kenneth W * We use pfn_offset to avoid touching the pageframes 3759d5d4b0aaSChen, Kenneth W * of this compound page. 3760d5d4b0aaSChen, Kenneth W */ 3761d5d4b0aaSChen, Kenneth W goto same_page; 3762d5d4b0aaSChen, Kenneth W } 3763cb900f41SKirill A. Shutemov spin_unlock(ptl); 376463551ae0SDavid Gibson } 376528a35716SMichel Lespinasse *nr_pages = remainder; 376663551ae0SDavid Gibson *position = vaddr; 376763551ae0SDavid Gibson 37682a15efc9SHugh Dickins return i ? i : -EFAULT; 376963551ae0SDavid Gibson } 37708f860591SZhang, Yanmin 37717da4d641SPeter Zijlstra unsigned long hugetlb_change_protection(struct vm_area_struct *vma, 37728f860591SZhang, Yanmin unsigned long address, unsigned long end, pgprot_t newprot) 37738f860591SZhang, Yanmin { 37748f860591SZhang, Yanmin struct mm_struct *mm = vma->vm_mm; 37758f860591SZhang, Yanmin unsigned long start = address; 37768f860591SZhang, Yanmin pte_t *ptep; 37778f860591SZhang, Yanmin pte_t pte; 3778a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 37797da4d641SPeter Zijlstra unsigned long pages = 0; 37808f860591SZhang, Yanmin 37818f860591SZhang, Yanmin BUG_ON(address >= end); 37828f860591SZhang, Yanmin flush_cache_range(vma, address, end); 37838f860591SZhang, Yanmin 3784a5338093SRik van Riel mmu_notifier_invalidate_range_start(mm, start, end); 378583cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 3786a5516438SAndi Kleen for (; address < end; address += huge_page_size(h)) { 3787cb900f41SKirill A. Shutemov spinlock_t *ptl; 37888f860591SZhang, Yanmin ptep = huge_pte_offset(mm, address); 37898f860591SZhang, Yanmin if (!ptep) 37908f860591SZhang, Yanmin continue; 3791cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 37927da4d641SPeter Zijlstra if (huge_pmd_unshare(mm, &address, ptep)) { 37937da4d641SPeter Zijlstra pages++; 3794cb900f41SKirill A. Shutemov spin_unlock(ptl); 379539dde65cSChen, Kenneth W continue; 37967da4d641SPeter Zijlstra } 3797a8bda28dSNaoya Horiguchi pte = huge_ptep_get(ptep); 3798a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { 3799a8bda28dSNaoya Horiguchi spin_unlock(ptl); 3800a8bda28dSNaoya Horiguchi continue; 3801a8bda28dSNaoya Horiguchi } 3802a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(pte))) { 3803a8bda28dSNaoya Horiguchi swp_entry_t entry = pte_to_swp_entry(pte); 3804a8bda28dSNaoya Horiguchi 3805a8bda28dSNaoya Horiguchi if (is_write_migration_entry(entry)) { 3806a8bda28dSNaoya Horiguchi pte_t newpte; 3807a8bda28dSNaoya Horiguchi 3808a8bda28dSNaoya Horiguchi make_migration_entry_read(&entry); 3809a8bda28dSNaoya Horiguchi newpte = swp_entry_to_pte(entry); 3810a8bda28dSNaoya Horiguchi set_huge_pte_at(mm, address, ptep, newpte); 3811a8bda28dSNaoya Horiguchi pages++; 3812a8bda28dSNaoya Horiguchi } 3813a8bda28dSNaoya Horiguchi spin_unlock(ptl); 3814a8bda28dSNaoya Horiguchi continue; 3815a8bda28dSNaoya Horiguchi } 3816a8bda28dSNaoya Horiguchi if (!huge_pte_none(pte)) { 38178f860591SZhang, Yanmin pte = huge_ptep_get_and_clear(mm, address, ptep); 3818106c992aSGerald Schaefer pte = pte_mkhuge(huge_pte_modify(pte, newprot)); 3819be7517d6STony Lu pte = arch_make_huge_pte(pte, vma, NULL, 0); 38208f860591SZhang, Yanmin set_huge_pte_at(mm, address, ptep, pte); 38217da4d641SPeter Zijlstra pages++; 38228f860591SZhang, Yanmin } 3823cb900f41SKirill A. Shutemov spin_unlock(ptl); 38248f860591SZhang, Yanmin } 3825d833352aSMel Gorman /* 3826c8c06efaSDavidlohr Bueso * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare 3827d833352aSMel Gorman * may have cleared our pud entry and done put_page on the page table: 3828c8c06efaSDavidlohr Bueso * once we release i_mmap_rwsem, another task can do the final put_page 3829d833352aSMel Gorman * and that page table be reused and filled with junk. 3830d833352aSMel Gorman */ 38318f860591SZhang, Yanmin flush_tlb_range(vma, start, end); 383234ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, start, end); 383383cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 3834a5338093SRik van Riel mmu_notifier_invalidate_range_end(mm, start, end); 38357da4d641SPeter Zijlstra 38367da4d641SPeter Zijlstra return pages << h->order; 38378f860591SZhang, Yanmin } 38388f860591SZhang, Yanmin 3839a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode, 3840a1e78772SMel Gorman long from, long to, 38415a6fe125SMel Gorman struct vm_area_struct *vma, 3842ca16d140SKOSAKI Motohiro vm_flags_t vm_flags) 3843e4e574b7SAdam Litke { 384417c9d12eSMel Gorman long ret, chg; 3845a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 384690481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 38479119a41eSJoonsoo Kim struct resv_map *resv_map; 38481c5ecae3SMike Kravetz long gbl_reserve; 3849e4e574b7SAdam Litke 3850a1e78772SMel Gorman /* 385117c9d12eSMel Gorman * Only apply hugepage reservation if asked. At fault time, an 385217c9d12eSMel Gorman * attempt will be made for VM_NORESERVE to allocate a page 385390481622SDavid Gibson * without using reserves 385417c9d12eSMel Gorman */ 3855ca16d140SKOSAKI Motohiro if (vm_flags & VM_NORESERVE) 385617c9d12eSMel Gorman return 0; 385717c9d12eSMel Gorman 385817c9d12eSMel Gorman /* 3859a1e78772SMel Gorman * Shared mappings base their reservation on the number of pages that 3860a1e78772SMel Gorman * are already allocated on behalf of the file. Private mappings need 3861a1e78772SMel Gorman * to reserve the full area even if read-only as mprotect() may be 3862a1e78772SMel Gorman * called to make the mapping read-write. Assume !vma is a shm mapping 3863a1e78772SMel Gorman */ 38649119a41eSJoonsoo Kim if (!vma || vma->vm_flags & VM_MAYSHARE) { 38654e35f483SJoonsoo Kim resv_map = inode_resv_map(inode); 38669119a41eSJoonsoo Kim 38671406ec9bSJoonsoo Kim chg = region_chg(resv_map, from, to); 38689119a41eSJoonsoo Kim 38699119a41eSJoonsoo Kim } else { 38709119a41eSJoonsoo Kim resv_map = resv_map_alloc(); 38715a6fe125SMel Gorman if (!resv_map) 38725a6fe125SMel Gorman return -ENOMEM; 38735a6fe125SMel Gorman 387417c9d12eSMel Gorman chg = to - from; 387517c9d12eSMel Gorman 38765a6fe125SMel Gorman set_vma_resv_map(vma, resv_map); 38775a6fe125SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_OWNER); 38785a6fe125SMel Gorman } 38795a6fe125SMel Gorman 3880c50ac050SDave Hansen if (chg < 0) { 3881c50ac050SDave Hansen ret = chg; 3882c50ac050SDave Hansen goto out_err; 3883c50ac050SDave Hansen } 388417c9d12eSMel Gorman 38851c5ecae3SMike Kravetz /* 38861c5ecae3SMike Kravetz * There must be enough pages in the subpool for the mapping. If 38871c5ecae3SMike Kravetz * the subpool has a minimum size, there may be some global 38881c5ecae3SMike Kravetz * reservations already in place (gbl_reserve). 38891c5ecae3SMike Kravetz */ 38901c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_get_pages(spool, chg); 38911c5ecae3SMike Kravetz if (gbl_reserve < 0) { 3892c50ac050SDave Hansen ret = -ENOSPC; 3893c50ac050SDave Hansen goto out_err; 3894c50ac050SDave Hansen } 389517c9d12eSMel Gorman 389617c9d12eSMel Gorman /* 389717c9d12eSMel Gorman * Check enough hugepages are available for the reservation. 389890481622SDavid Gibson * Hand the pages back to the subpool if there are not 389917c9d12eSMel Gorman */ 39001c5ecae3SMike Kravetz ret = hugetlb_acct_memory(h, gbl_reserve); 390117c9d12eSMel Gorman if (ret < 0) { 39021c5ecae3SMike Kravetz /* put back original number of pages, chg */ 39031c5ecae3SMike Kravetz (void)hugepage_subpool_put_pages(spool, chg); 3904c50ac050SDave Hansen goto out_err; 390517c9d12eSMel Gorman } 390617c9d12eSMel Gorman 390717c9d12eSMel Gorman /* 390817c9d12eSMel Gorman * Account for the reservations made. Shared mappings record regions 390917c9d12eSMel Gorman * that have reservations as they are shared by multiple VMAs. 391017c9d12eSMel Gorman * When the last VMA disappears, the region map says how much 391117c9d12eSMel Gorman * the reservation was and the page cache tells how much of 391217c9d12eSMel Gorman * the reservation was consumed. Private mappings are per-VMA and 391317c9d12eSMel Gorman * only the consumed reservations are tracked. When the VMA 391417c9d12eSMel Gorman * disappears, the original reservation is the VMA size and the 391517c9d12eSMel Gorman * consumed reservations are stored in the map. Hence, nothing 391617c9d12eSMel Gorman * else has to be done for private mappings here 391717c9d12eSMel Gorman */ 391833039678SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) { 391933039678SMike Kravetz long add = region_add(resv_map, from, to); 392033039678SMike Kravetz 392133039678SMike Kravetz if (unlikely(chg > add)) { 392233039678SMike Kravetz /* 392333039678SMike Kravetz * pages in this range were added to the reserve 392433039678SMike Kravetz * map between region_chg and region_add. This 392533039678SMike Kravetz * indicates a race with alloc_huge_page. Adjust 392633039678SMike Kravetz * the subpool and reserve counts modified above 392733039678SMike Kravetz * based on the difference. 392833039678SMike Kravetz */ 392933039678SMike Kravetz long rsv_adjust; 393033039678SMike Kravetz 393133039678SMike Kravetz rsv_adjust = hugepage_subpool_put_pages(spool, 393233039678SMike Kravetz chg - add); 393333039678SMike Kravetz hugetlb_acct_memory(h, -rsv_adjust); 393433039678SMike Kravetz } 393533039678SMike Kravetz } 3936a43a8c39SChen, Kenneth W return 0; 3937c50ac050SDave Hansen out_err: 39385e911373SMike Kravetz if (!vma || vma->vm_flags & VM_MAYSHARE) 39395e911373SMike Kravetz region_abort(resv_map, from, to); 3940f031dd27SJoonsoo Kim if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 3941f031dd27SJoonsoo Kim kref_put(&resv_map->refs, resv_map_release); 3942c50ac050SDave Hansen return ret; 3943a43a8c39SChen, Kenneth W } 3944a43a8c39SChen, Kenneth W 3945b5cec28dSMike Kravetz long hugetlb_unreserve_pages(struct inode *inode, long start, long end, 3946b5cec28dSMike Kravetz long freed) 3947a43a8c39SChen, Kenneth W { 3948a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 39494e35f483SJoonsoo Kim struct resv_map *resv_map = inode_resv_map(inode); 39509119a41eSJoonsoo Kim long chg = 0; 395190481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 39521c5ecae3SMike Kravetz long gbl_reserve; 395345c682a6SKen Chen 3954b5cec28dSMike Kravetz if (resv_map) { 3955b5cec28dSMike Kravetz chg = region_del(resv_map, start, end); 3956b5cec28dSMike Kravetz /* 3957b5cec28dSMike Kravetz * region_del() can fail in the rare case where a region 3958b5cec28dSMike Kravetz * must be split and another region descriptor can not be 3959b5cec28dSMike Kravetz * allocated. If end == LONG_MAX, it will not fail. 3960b5cec28dSMike Kravetz */ 3961b5cec28dSMike Kravetz if (chg < 0) 3962b5cec28dSMike Kravetz return chg; 3963b5cec28dSMike Kravetz } 3964b5cec28dSMike Kravetz 396545c682a6SKen Chen spin_lock(&inode->i_lock); 3966e4c6f8beSEric Sandeen inode->i_blocks -= (blocks_per_huge_page(h) * freed); 396745c682a6SKen Chen spin_unlock(&inode->i_lock); 396845c682a6SKen Chen 39691c5ecae3SMike Kravetz /* 39701c5ecae3SMike Kravetz * If the subpool has a minimum size, the number of global 39711c5ecae3SMike Kravetz * reservations to be released may be adjusted. 39721c5ecae3SMike Kravetz */ 39731c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); 39741c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 3975b5cec28dSMike Kravetz 3976b5cec28dSMike Kravetz return 0; 3977a43a8c39SChen, Kenneth W } 397893f70f90SNaoya Horiguchi 39793212b535SSteve Capper #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE 39803212b535SSteve Capper static unsigned long page_table_shareable(struct vm_area_struct *svma, 39813212b535SSteve Capper struct vm_area_struct *vma, 39823212b535SSteve Capper unsigned long addr, pgoff_t idx) 39833212b535SSteve Capper { 39843212b535SSteve Capper unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + 39853212b535SSteve Capper svma->vm_start; 39863212b535SSteve Capper unsigned long sbase = saddr & PUD_MASK; 39873212b535SSteve Capper unsigned long s_end = sbase + PUD_SIZE; 39883212b535SSteve Capper 39893212b535SSteve Capper /* Allow segments to share if only one is marked locked */ 39903212b535SSteve Capper unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED; 39913212b535SSteve Capper unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED; 39923212b535SSteve Capper 39933212b535SSteve Capper /* 39943212b535SSteve Capper * match the virtual addresses, permission and the alignment of the 39953212b535SSteve Capper * page table page. 39963212b535SSteve Capper */ 39973212b535SSteve Capper if (pmd_index(addr) != pmd_index(saddr) || 39983212b535SSteve Capper vm_flags != svm_flags || 39993212b535SSteve Capper sbase < svma->vm_start || svma->vm_end < s_end) 40003212b535SSteve Capper return 0; 40013212b535SSteve Capper 40023212b535SSteve Capper return saddr; 40033212b535SSteve Capper } 40043212b535SSteve Capper 400531aafb45SNicholas Krause static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr) 40063212b535SSteve Capper { 40073212b535SSteve Capper unsigned long base = addr & PUD_MASK; 40083212b535SSteve Capper unsigned long end = base + PUD_SIZE; 40093212b535SSteve Capper 40103212b535SSteve Capper /* 40113212b535SSteve Capper * check on proper vm_flags and page table alignment 40123212b535SSteve Capper */ 40133212b535SSteve Capper if (vma->vm_flags & VM_MAYSHARE && 40143212b535SSteve Capper vma->vm_start <= base && end <= vma->vm_end) 401531aafb45SNicholas Krause return true; 401631aafb45SNicholas Krause return false; 40173212b535SSteve Capper } 40183212b535SSteve Capper 40193212b535SSteve Capper /* 40203212b535SSteve Capper * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() 40213212b535SSteve Capper * and returns the corresponding pte. While this is not necessary for the 40223212b535SSteve Capper * !shared pmd case because we can allocate the pmd later as well, it makes the 40233212b535SSteve Capper * code much cleaner. pmd allocation is essential for the shared case because 4024c8c06efaSDavidlohr Bueso * pud has to be populated inside the same i_mmap_rwsem section - otherwise 40253212b535SSteve Capper * racing tasks could either miss the sharing (see huge_pte_offset) or select a 40263212b535SSteve Capper * bad pmd for sharing. 40273212b535SSteve Capper */ 40283212b535SSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 40293212b535SSteve Capper { 40303212b535SSteve Capper struct vm_area_struct *vma = find_vma(mm, addr); 40313212b535SSteve Capper struct address_space *mapping = vma->vm_file->f_mapping; 40323212b535SSteve Capper pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + 40333212b535SSteve Capper vma->vm_pgoff; 40343212b535SSteve Capper struct vm_area_struct *svma; 40353212b535SSteve Capper unsigned long saddr; 40363212b535SSteve Capper pte_t *spte = NULL; 40373212b535SSteve Capper pte_t *pte; 4038cb900f41SKirill A. Shutemov spinlock_t *ptl; 40393212b535SSteve Capper 40403212b535SSteve Capper if (!vma_shareable(vma, addr)) 40413212b535SSteve Capper return (pte_t *)pmd_alloc(mm, pud, addr); 40423212b535SSteve Capper 404383cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 40443212b535SSteve Capper vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { 40453212b535SSteve Capper if (svma == vma) 40463212b535SSteve Capper continue; 40473212b535SSteve Capper 40483212b535SSteve Capper saddr = page_table_shareable(svma, vma, addr, idx); 40493212b535SSteve Capper if (saddr) { 40503212b535SSteve Capper spte = huge_pte_offset(svma->vm_mm, saddr); 40513212b535SSteve Capper if (spte) { 4052dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 40533212b535SSteve Capper get_page(virt_to_page(spte)); 40543212b535SSteve Capper break; 40553212b535SSteve Capper } 40563212b535SSteve Capper } 40573212b535SSteve Capper } 40583212b535SSteve Capper 40593212b535SSteve Capper if (!spte) 40603212b535SSteve Capper goto out; 40613212b535SSteve Capper 4062cb900f41SKirill A. Shutemov ptl = huge_pte_lockptr(hstate_vma(vma), mm, spte); 4063cb900f41SKirill A. Shutemov spin_lock(ptl); 4064dc6c9a35SKirill A. Shutemov if (pud_none(*pud)) { 40653212b535SSteve Capper pud_populate(mm, pud, 40663212b535SSteve Capper (pmd_t *)((unsigned long)spte & PAGE_MASK)); 4067dc6c9a35SKirill A. Shutemov } else { 40683212b535SSteve Capper put_page(virt_to_page(spte)); 4069dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 4070dc6c9a35SKirill A. Shutemov } 4071cb900f41SKirill A. Shutemov spin_unlock(ptl); 40723212b535SSteve Capper out: 40733212b535SSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 407483cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 40753212b535SSteve Capper return pte; 40763212b535SSteve Capper } 40773212b535SSteve Capper 40783212b535SSteve Capper /* 40793212b535SSteve Capper * unmap huge page backed by shared pte. 40803212b535SSteve Capper * 40813212b535SSteve Capper * Hugetlb pte page is ref counted at the time of mapping. If pte is shared 40823212b535SSteve Capper * indicated by page_count > 1, unmap is achieved by clearing pud and 40833212b535SSteve Capper * decrementing the ref count. If count == 1, the pte page is not shared. 40843212b535SSteve Capper * 4085cb900f41SKirill A. Shutemov * called with page table lock held. 40863212b535SSteve Capper * 40873212b535SSteve Capper * returns: 1 successfully unmapped a shared pte page 40883212b535SSteve Capper * 0 the underlying pte page is not shared, or it is the last user 40893212b535SSteve Capper */ 40903212b535SSteve Capper int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 40913212b535SSteve Capper { 40923212b535SSteve Capper pgd_t *pgd = pgd_offset(mm, *addr); 40933212b535SSteve Capper pud_t *pud = pud_offset(pgd, *addr); 40943212b535SSteve Capper 40953212b535SSteve Capper BUG_ON(page_count(virt_to_page(ptep)) == 0); 40963212b535SSteve Capper if (page_count(virt_to_page(ptep)) == 1) 40973212b535SSteve Capper return 0; 40983212b535SSteve Capper 40993212b535SSteve Capper pud_clear(pud); 41003212b535SSteve Capper put_page(virt_to_page(ptep)); 4101dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(mm); 41023212b535SSteve Capper *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE; 41033212b535SSteve Capper return 1; 41043212b535SSteve Capper } 41059e5fc74cSSteve Capper #define want_pmd_share() (1) 41069e5fc74cSSteve Capper #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 41079e5fc74cSSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 41089e5fc74cSSteve Capper { 41099e5fc74cSSteve Capper return NULL; 41109e5fc74cSSteve Capper } 4111e81f2d22SZhang Zhen 4112e81f2d22SZhang Zhen int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 4113e81f2d22SZhang Zhen { 4114e81f2d22SZhang Zhen return 0; 4115e81f2d22SZhang Zhen } 41169e5fc74cSSteve Capper #define want_pmd_share() (0) 41173212b535SSteve Capper #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 41183212b535SSteve Capper 41199e5fc74cSSteve Capper #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 41209e5fc74cSSteve Capper pte_t *huge_pte_alloc(struct mm_struct *mm, 41219e5fc74cSSteve Capper unsigned long addr, unsigned long sz) 41229e5fc74cSSteve Capper { 41239e5fc74cSSteve Capper pgd_t *pgd; 41249e5fc74cSSteve Capper pud_t *pud; 41259e5fc74cSSteve Capper pte_t *pte = NULL; 41269e5fc74cSSteve Capper 41279e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 41289e5fc74cSSteve Capper pud = pud_alloc(mm, pgd, addr); 41299e5fc74cSSteve Capper if (pud) { 41309e5fc74cSSteve Capper if (sz == PUD_SIZE) { 41319e5fc74cSSteve Capper pte = (pte_t *)pud; 41329e5fc74cSSteve Capper } else { 41339e5fc74cSSteve Capper BUG_ON(sz != PMD_SIZE); 41349e5fc74cSSteve Capper if (want_pmd_share() && pud_none(*pud)) 41359e5fc74cSSteve Capper pte = huge_pmd_share(mm, addr, pud); 41369e5fc74cSSteve Capper else 41379e5fc74cSSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 41389e5fc74cSSteve Capper } 41399e5fc74cSSteve Capper } 41409e5fc74cSSteve Capper BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte)); 41419e5fc74cSSteve Capper 41429e5fc74cSSteve Capper return pte; 41439e5fc74cSSteve Capper } 41449e5fc74cSSteve Capper 41459e5fc74cSSteve Capper pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) 41469e5fc74cSSteve Capper { 41479e5fc74cSSteve Capper pgd_t *pgd; 41489e5fc74cSSteve Capper pud_t *pud; 41499e5fc74cSSteve Capper pmd_t *pmd = NULL; 41509e5fc74cSSteve Capper 41519e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 41529e5fc74cSSteve Capper if (pgd_present(*pgd)) { 41539e5fc74cSSteve Capper pud = pud_offset(pgd, addr); 41549e5fc74cSSteve Capper if (pud_present(*pud)) { 41559e5fc74cSSteve Capper if (pud_huge(*pud)) 41569e5fc74cSSteve Capper return (pte_t *)pud; 41579e5fc74cSSteve Capper pmd = pmd_offset(pud, addr); 41589e5fc74cSSteve Capper } 41599e5fc74cSSteve Capper } 41609e5fc74cSSteve Capper return (pte_t *) pmd; 41619e5fc74cSSteve Capper } 41629e5fc74cSSteve Capper 416361f77edaSNaoya Horiguchi #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 416461f77edaSNaoya Horiguchi 416561f77edaSNaoya Horiguchi /* 416661f77edaSNaoya Horiguchi * These functions are overwritable if your architecture needs its own 416761f77edaSNaoya Horiguchi * behavior. 416861f77edaSNaoya Horiguchi */ 416961f77edaSNaoya Horiguchi struct page * __weak 417061f77edaSNaoya Horiguchi follow_huge_addr(struct mm_struct *mm, unsigned long address, 417161f77edaSNaoya Horiguchi int write) 417261f77edaSNaoya Horiguchi { 417361f77edaSNaoya Horiguchi return ERR_PTR(-EINVAL); 417461f77edaSNaoya Horiguchi } 417561f77edaSNaoya Horiguchi 417661f77edaSNaoya Horiguchi struct page * __weak 41779e5fc74cSSteve Capper follow_huge_pmd(struct mm_struct *mm, unsigned long address, 4178e66f17ffSNaoya Horiguchi pmd_t *pmd, int flags) 41799e5fc74cSSteve Capper { 4180e66f17ffSNaoya Horiguchi struct page *page = NULL; 4181e66f17ffSNaoya Horiguchi spinlock_t *ptl; 4182e66f17ffSNaoya Horiguchi retry: 4183e66f17ffSNaoya Horiguchi ptl = pmd_lockptr(mm, pmd); 4184e66f17ffSNaoya Horiguchi spin_lock(ptl); 4185e66f17ffSNaoya Horiguchi /* 4186e66f17ffSNaoya Horiguchi * make sure that the address range covered by this pmd is not 4187e66f17ffSNaoya Horiguchi * unmapped from other threads. 4188e66f17ffSNaoya Horiguchi */ 4189e66f17ffSNaoya Horiguchi if (!pmd_huge(*pmd)) 4190e66f17ffSNaoya Horiguchi goto out; 4191e66f17ffSNaoya Horiguchi if (pmd_present(*pmd)) { 419297534127SGerald Schaefer page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT); 4193e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 4194e66f17ffSNaoya Horiguchi get_page(page); 4195e66f17ffSNaoya Horiguchi } else { 4196e66f17ffSNaoya Horiguchi if (is_hugetlb_entry_migration(huge_ptep_get((pte_t *)pmd))) { 4197e66f17ffSNaoya Horiguchi spin_unlock(ptl); 4198e66f17ffSNaoya Horiguchi __migration_entry_wait(mm, (pte_t *)pmd, ptl); 4199e66f17ffSNaoya Horiguchi goto retry; 4200e66f17ffSNaoya Horiguchi } 4201e66f17ffSNaoya Horiguchi /* 4202e66f17ffSNaoya Horiguchi * hwpoisoned entry is treated as no_page_table in 4203e66f17ffSNaoya Horiguchi * follow_page_mask(). 4204e66f17ffSNaoya Horiguchi */ 4205e66f17ffSNaoya Horiguchi } 4206e66f17ffSNaoya Horiguchi out: 4207e66f17ffSNaoya Horiguchi spin_unlock(ptl); 42089e5fc74cSSteve Capper return page; 42099e5fc74cSSteve Capper } 42109e5fc74cSSteve Capper 421161f77edaSNaoya Horiguchi struct page * __weak 42129e5fc74cSSteve Capper follow_huge_pud(struct mm_struct *mm, unsigned long address, 4213e66f17ffSNaoya Horiguchi pud_t *pud, int flags) 42149e5fc74cSSteve Capper { 4215e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 4216e66f17ffSNaoya Horiguchi return NULL; 42179e5fc74cSSteve Capper 4218e66f17ffSNaoya Horiguchi return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT); 42199e5fc74cSSteve Capper } 42209e5fc74cSSteve Capper 4221d5bd9106SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE 4222d5bd9106SAndi Kleen 422393f70f90SNaoya Horiguchi /* 422493f70f90SNaoya Horiguchi * This function is called from memory failure code. 422593f70f90SNaoya Horiguchi * Assume the caller holds page lock of the head page. 422693f70f90SNaoya Horiguchi */ 42276de2b1aaSNaoya Horiguchi int dequeue_hwpoisoned_huge_page(struct page *hpage) 422893f70f90SNaoya Horiguchi { 422993f70f90SNaoya Horiguchi struct hstate *h = page_hstate(hpage); 423093f70f90SNaoya Horiguchi int nid = page_to_nid(hpage); 42316de2b1aaSNaoya Horiguchi int ret = -EBUSY; 423293f70f90SNaoya Horiguchi 423393f70f90SNaoya Horiguchi spin_lock(&hugetlb_lock); 42347e1f049eSNaoya Horiguchi /* 42357e1f049eSNaoya Horiguchi * Just checking !page_huge_active is not enough, because that could be 42367e1f049eSNaoya Horiguchi * an isolated/hwpoisoned hugepage (which have >0 refcount). 42377e1f049eSNaoya Horiguchi */ 42387e1f049eSNaoya Horiguchi if (!page_huge_active(hpage) && !page_count(hpage)) { 423956f2fb14SNaoya Horiguchi /* 424056f2fb14SNaoya Horiguchi * Hwpoisoned hugepage isn't linked to activelist or freelist, 424156f2fb14SNaoya Horiguchi * but dangling hpage->lru can trigger list-debug warnings 424256f2fb14SNaoya Horiguchi * (this happens when we call unpoison_memory() on it), 424356f2fb14SNaoya Horiguchi * so let it point to itself with list_del_init(). 424456f2fb14SNaoya Horiguchi */ 424556f2fb14SNaoya Horiguchi list_del_init(&hpage->lru); 42468c6c2ecbSNaoya Horiguchi set_page_refcounted(hpage); 424793f70f90SNaoya Horiguchi h->free_huge_pages--; 424893f70f90SNaoya Horiguchi h->free_huge_pages_node[nid]--; 42496de2b1aaSNaoya Horiguchi ret = 0; 425093f70f90SNaoya Horiguchi } 42516de2b1aaSNaoya Horiguchi spin_unlock(&hugetlb_lock); 42526de2b1aaSNaoya Horiguchi return ret; 42536de2b1aaSNaoya Horiguchi } 42546de2b1aaSNaoya Horiguchi #endif 425531caf665SNaoya Horiguchi 425631caf665SNaoya Horiguchi bool isolate_huge_page(struct page *page, struct list_head *list) 425731caf665SNaoya Horiguchi { 4258bcc54222SNaoya Horiguchi bool ret = true; 4259bcc54222SNaoya Horiguchi 4260309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 426131caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 4262bcc54222SNaoya Horiguchi if (!page_huge_active(page) || !get_page_unless_zero(page)) { 4263bcc54222SNaoya Horiguchi ret = false; 4264bcc54222SNaoya Horiguchi goto unlock; 4265bcc54222SNaoya Horiguchi } 4266bcc54222SNaoya Horiguchi clear_page_huge_active(page); 426731caf665SNaoya Horiguchi list_move_tail(&page->lru, list); 4268bcc54222SNaoya Horiguchi unlock: 426931caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 4270bcc54222SNaoya Horiguchi return ret; 427131caf665SNaoya Horiguchi } 427231caf665SNaoya Horiguchi 427331caf665SNaoya Horiguchi void putback_active_hugepage(struct page *page) 427431caf665SNaoya Horiguchi { 4275309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 427631caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 4277bcc54222SNaoya Horiguchi set_page_huge_active(page); 427831caf665SNaoya Horiguchi list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist); 427931caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 428031caf665SNaoya Horiguchi put_page(page); 428131caf665SNaoya Horiguchi } 4282