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]; 43e5ff2159SAndi Kleen 4453ba51d2SJon Tollefson __initdata LIST_HEAD(huge_boot_pages); 4553ba51d2SJon Tollefson 46e5ff2159SAndi Kleen /* for command line parsing */ 47e5ff2159SAndi Kleen static struct hstate * __initdata parsed_hstate; 48e5ff2159SAndi Kleen static unsigned long __initdata default_hstate_max_huge_pages; 49e11bfbfcSNick Piggin static unsigned long __initdata default_hstate_size; 50e5ff2159SAndi Kleen 513935baa9SDavid Gibson /* 5231caf665SNaoya Horiguchi * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages, 5331caf665SNaoya Horiguchi * free_huge_pages, and surplus_huge_pages. 543935baa9SDavid Gibson */ 55c3f38a38SAneesh Kumar K.V DEFINE_SPINLOCK(hugetlb_lock); 560bd0f9fbSEric Paris 578382d914SDavidlohr Bueso /* 588382d914SDavidlohr Bueso * Serializes faults on the same logical page. This is used to 598382d914SDavidlohr Bueso * prevent spurious OOMs when the hugepage pool is fully utilized. 608382d914SDavidlohr Bueso */ 618382d914SDavidlohr Bueso static int num_fault_mutexes; 628382d914SDavidlohr Bueso static struct mutex *htlb_fault_mutex_table ____cacheline_aligned_in_smp; 638382d914SDavidlohr Bueso 647ca02d0aSMike Kravetz /* Forward declaration */ 657ca02d0aSMike Kravetz static int hugetlb_acct_memory(struct hstate *h, long delta); 667ca02d0aSMike Kravetz 6790481622SDavid Gibson static inline void unlock_or_release_subpool(struct hugepage_subpool *spool) 6890481622SDavid Gibson { 6990481622SDavid Gibson bool free = (spool->count == 0) && (spool->used_hpages == 0); 7090481622SDavid Gibson 7190481622SDavid Gibson spin_unlock(&spool->lock); 7290481622SDavid Gibson 7390481622SDavid Gibson /* If no pages are used, and no other handles to the subpool 747ca02d0aSMike Kravetz * remain, give up any reservations mased on minimum size and 757ca02d0aSMike Kravetz * free the subpool */ 767ca02d0aSMike Kravetz if (free) { 777ca02d0aSMike Kravetz if (spool->min_hpages != -1) 787ca02d0aSMike Kravetz hugetlb_acct_memory(spool->hstate, 797ca02d0aSMike Kravetz -spool->min_hpages); 8090481622SDavid Gibson kfree(spool); 8190481622SDavid Gibson } 827ca02d0aSMike Kravetz } 8390481622SDavid Gibson 847ca02d0aSMike Kravetz struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages, 857ca02d0aSMike Kravetz long min_hpages) 8690481622SDavid Gibson { 8790481622SDavid Gibson struct hugepage_subpool *spool; 8890481622SDavid Gibson 89c6a91820SMike Kravetz spool = kzalloc(sizeof(*spool), GFP_KERNEL); 9090481622SDavid Gibson if (!spool) 9190481622SDavid Gibson return NULL; 9290481622SDavid Gibson 9390481622SDavid Gibson spin_lock_init(&spool->lock); 9490481622SDavid Gibson spool->count = 1; 957ca02d0aSMike Kravetz spool->max_hpages = max_hpages; 967ca02d0aSMike Kravetz spool->hstate = h; 977ca02d0aSMike Kravetz spool->min_hpages = min_hpages; 987ca02d0aSMike Kravetz 997ca02d0aSMike Kravetz if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) { 1007ca02d0aSMike Kravetz kfree(spool); 1017ca02d0aSMike Kravetz return NULL; 1027ca02d0aSMike Kravetz } 1037ca02d0aSMike Kravetz spool->rsv_hpages = min_hpages; 10490481622SDavid Gibson 10590481622SDavid Gibson return spool; 10690481622SDavid Gibson } 10790481622SDavid Gibson 10890481622SDavid Gibson void hugepage_put_subpool(struct hugepage_subpool *spool) 10990481622SDavid Gibson { 11090481622SDavid Gibson spin_lock(&spool->lock); 11190481622SDavid Gibson BUG_ON(!spool->count); 11290481622SDavid Gibson spool->count--; 11390481622SDavid Gibson unlock_or_release_subpool(spool); 11490481622SDavid Gibson } 11590481622SDavid Gibson 1161c5ecae3SMike Kravetz /* 1171c5ecae3SMike Kravetz * Subpool accounting for allocating and reserving pages. 1181c5ecae3SMike Kravetz * Return -ENOMEM if there are not enough resources to satisfy the 1191c5ecae3SMike Kravetz * the request. Otherwise, return the number of pages by which the 1201c5ecae3SMike Kravetz * global pools must be adjusted (upward). The returned value may 1211c5ecae3SMike Kravetz * only be different than the passed value (delta) in the case where 1221c5ecae3SMike Kravetz * a subpool minimum size must be manitained. 1231c5ecae3SMike Kravetz */ 1241c5ecae3SMike Kravetz static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, 12590481622SDavid Gibson long delta) 12690481622SDavid Gibson { 1271c5ecae3SMike Kravetz long ret = delta; 12890481622SDavid Gibson 12990481622SDavid Gibson if (!spool) 1301c5ecae3SMike Kravetz return ret; 13190481622SDavid Gibson 13290481622SDavid Gibson spin_lock(&spool->lock); 13390481622SDavid Gibson 1341c5ecae3SMike Kravetz if (spool->max_hpages != -1) { /* maximum size accounting */ 1351c5ecae3SMike Kravetz if ((spool->used_hpages + delta) <= spool->max_hpages) 1361c5ecae3SMike Kravetz spool->used_hpages += delta; 1371c5ecae3SMike Kravetz else { 1381c5ecae3SMike Kravetz ret = -ENOMEM; 1391c5ecae3SMike Kravetz goto unlock_ret; 1401c5ecae3SMike Kravetz } 1411c5ecae3SMike Kravetz } 1421c5ecae3SMike Kravetz 1431c5ecae3SMike Kravetz if (spool->min_hpages != -1) { /* minimum size accounting */ 1441c5ecae3SMike Kravetz if (delta > spool->rsv_hpages) { 1451c5ecae3SMike Kravetz /* 1461c5ecae3SMike Kravetz * Asking for more reserves than those already taken on 1471c5ecae3SMike Kravetz * behalf of subpool. Return difference. 1481c5ecae3SMike Kravetz */ 1491c5ecae3SMike Kravetz ret = delta - spool->rsv_hpages; 1501c5ecae3SMike Kravetz spool->rsv_hpages = 0; 1511c5ecae3SMike Kravetz } else { 1521c5ecae3SMike Kravetz ret = 0; /* reserves already accounted for */ 1531c5ecae3SMike Kravetz spool->rsv_hpages -= delta; 1541c5ecae3SMike Kravetz } 1551c5ecae3SMike Kravetz } 1561c5ecae3SMike Kravetz 1571c5ecae3SMike Kravetz unlock_ret: 1581c5ecae3SMike Kravetz spin_unlock(&spool->lock); 15990481622SDavid Gibson return ret; 16090481622SDavid Gibson } 16190481622SDavid Gibson 1621c5ecae3SMike Kravetz /* 1631c5ecae3SMike Kravetz * Subpool accounting for freeing and unreserving pages. 1641c5ecae3SMike Kravetz * Return the number of global page reservations that must be dropped. 1651c5ecae3SMike Kravetz * The return value may only be different than the passed value (delta) 1661c5ecae3SMike Kravetz * in the case where a subpool minimum size must be maintained. 1671c5ecae3SMike Kravetz */ 1681c5ecae3SMike Kravetz static long hugepage_subpool_put_pages(struct hugepage_subpool *spool, 16990481622SDavid Gibson long delta) 17090481622SDavid Gibson { 1711c5ecae3SMike Kravetz long ret = delta; 1721c5ecae3SMike Kravetz 17390481622SDavid Gibson if (!spool) 1741c5ecae3SMike Kravetz return delta; 17590481622SDavid Gibson 17690481622SDavid Gibson spin_lock(&spool->lock); 1771c5ecae3SMike Kravetz 1781c5ecae3SMike Kravetz if (spool->max_hpages != -1) /* maximum size accounting */ 17990481622SDavid Gibson spool->used_hpages -= delta; 1801c5ecae3SMike Kravetz 1811c5ecae3SMike Kravetz if (spool->min_hpages != -1) { /* minimum size accounting */ 1821c5ecae3SMike Kravetz if (spool->rsv_hpages + delta <= spool->min_hpages) 1831c5ecae3SMike Kravetz ret = 0; 1841c5ecae3SMike Kravetz else 1851c5ecae3SMike Kravetz ret = spool->rsv_hpages + delta - spool->min_hpages; 1861c5ecae3SMike Kravetz 1871c5ecae3SMike Kravetz spool->rsv_hpages += delta; 1881c5ecae3SMike Kravetz if (spool->rsv_hpages > spool->min_hpages) 1891c5ecae3SMike Kravetz spool->rsv_hpages = spool->min_hpages; 1901c5ecae3SMike Kravetz } 1911c5ecae3SMike Kravetz 1921c5ecae3SMike Kravetz /* 1931c5ecae3SMike Kravetz * If hugetlbfs_put_super couldn't free spool due to an outstanding 1941c5ecae3SMike Kravetz * quota reference, free it now. 1951c5ecae3SMike Kravetz */ 19690481622SDavid Gibson unlock_or_release_subpool(spool); 1971c5ecae3SMike Kravetz 1981c5ecae3SMike Kravetz return ret; 19990481622SDavid Gibson } 20090481622SDavid Gibson 20190481622SDavid Gibson static inline struct hugepage_subpool *subpool_inode(struct inode *inode) 20290481622SDavid Gibson { 20390481622SDavid Gibson return HUGETLBFS_SB(inode->i_sb)->spool; 20490481622SDavid Gibson } 20590481622SDavid Gibson 20690481622SDavid Gibson static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma) 20790481622SDavid Gibson { 208496ad9aaSAl Viro return subpool_inode(file_inode(vma->vm_file)); 20990481622SDavid Gibson } 21090481622SDavid Gibson 211e7c4b0bfSAndy Whitcroft /* 21296822904SAndy Whitcroft * Region tracking -- allows tracking of reservations and instantiated pages 21396822904SAndy Whitcroft * across the pages in a mapping. 21484afd99bSAndy Whitcroft * 2157b24d861SDavidlohr Bueso * The region data structures are embedded into a resv_map and 2167b24d861SDavidlohr Bueso * protected by a resv_map's lock 21796822904SAndy Whitcroft */ 21896822904SAndy Whitcroft struct file_region { 21996822904SAndy Whitcroft struct list_head link; 22096822904SAndy Whitcroft long from; 22196822904SAndy Whitcroft long to; 22296822904SAndy Whitcroft }; 22396822904SAndy Whitcroft 2241406ec9bSJoonsoo Kim static long region_add(struct resv_map *resv, long f, long t) 22596822904SAndy Whitcroft { 2261406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 22796822904SAndy Whitcroft struct file_region *rg, *nrg, *trg; 22896822904SAndy Whitcroft 2297b24d861SDavidlohr Bueso spin_lock(&resv->lock); 23096822904SAndy Whitcroft /* Locate the region we are either in or before. */ 23196822904SAndy Whitcroft list_for_each_entry(rg, head, link) 23296822904SAndy Whitcroft if (f <= rg->to) 23396822904SAndy Whitcroft break; 23496822904SAndy Whitcroft 23596822904SAndy Whitcroft /* Round our left edge to the current segment if it encloses us. */ 23696822904SAndy Whitcroft if (f > rg->from) 23796822904SAndy Whitcroft f = rg->from; 23896822904SAndy Whitcroft 23996822904SAndy Whitcroft /* Check for and consume any regions we now overlap with. */ 24096822904SAndy Whitcroft nrg = rg; 24196822904SAndy Whitcroft list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 24296822904SAndy Whitcroft if (&rg->link == head) 24396822904SAndy Whitcroft break; 24496822904SAndy Whitcroft if (rg->from > t) 24596822904SAndy Whitcroft break; 24696822904SAndy Whitcroft 24796822904SAndy Whitcroft /* If this area reaches higher then extend our area to 24896822904SAndy Whitcroft * include it completely. If this is not the first area 24996822904SAndy Whitcroft * which we intend to reuse, free it. */ 25096822904SAndy Whitcroft if (rg->to > t) 25196822904SAndy Whitcroft t = rg->to; 25296822904SAndy Whitcroft if (rg != nrg) { 25396822904SAndy Whitcroft list_del(&rg->link); 25496822904SAndy Whitcroft kfree(rg); 25596822904SAndy Whitcroft } 25696822904SAndy Whitcroft } 25796822904SAndy Whitcroft nrg->from = f; 25896822904SAndy Whitcroft nrg->to = t; 2597b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 26096822904SAndy Whitcroft return 0; 26196822904SAndy Whitcroft } 26296822904SAndy Whitcroft 2631406ec9bSJoonsoo Kim static long region_chg(struct resv_map *resv, long f, long t) 26496822904SAndy Whitcroft { 2651406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 2667b24d861SDavidlohr Bueso struct file_region *rg, *nrg = NULL; 26796822904SAndy Whitcroft long chg = 0; 26896822904SAndy Whitcroft 2697b24d861SDavidlohr Bueso retry: 2707b24d861SDavidlohr Bueso spin_lock(&resv->lock); 27196822904SAndy Whitcroft /* Locate the region we are before or in. */ 27296822904SAndy Whitcroft list_for_each_entry(rg, head, link) 27396822904SAndy Whitcroft if (f <= rg->to) 27496822904SAndy Whitcroft break; 27596822904SAndy Whitcroft 27696822904SAndy Whitcroft /* If we are below the current region then a new region is required. 27796822904SAndy Whitcroft * Subtle, allocate a new region at the position but make it zero 27896822904SAndy Whitcroft * size such that we can guarantee to record the reservation. */ 27996822904SAndy Whitcroft if (&rg->link == head || t < rg->from) { 2807b24d861SDavidlohr Bueso if (!nrg) { 2817b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 28296822904SAndy Whitcroft nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 28396822904SAndy Whitcroft if (!nrg) 28496822904SAndy Whitcroft return -ENOMEM; 2857b24d861SDavidlohr Bueso 28696822904SAndy Whitcroft nrg->from = f; 28796822904SAndy Whitcroft nrg->to = f; 28896822904SAndy Whitcroft INIT_LIST_HEAD(&nrg->link); 2897b24d861SDavidlohr Bueso goto retry; 2907b24d861SDavidlohr Bueso } 29196822904SAndy Whitcroft 2927b24d861SDavidlohr Bueso list_add(&nrg->link, rg->link.prev); 2937b24d861SDavidlohr Bueso chg = t - f; 2947b24d861SDavidlohr Bueso goto out_nrg; 29596822904SAndy Whitcroft } 29696822904SAndy Whitcroft 29796822904SAndy Whitcroft /* Round our left edge to the current segment if it encloses us. */ 29896822904SAndy Whitcroft if (f > rg->from) 29996822904SAndy Whitcroft f = rg->from; 30096822904SAndy Whitcroft chg = t - f; 30196822904SAndy Whitcroft 30296822904SAndy Whitcroft /* Check for and consume any regions we now overlap with. */ 30396822904SAndy Whitcroft list_for_each_entry(rg, rg->link.prev, link) { 30496822904SAndy Whitcroft if (&rg->link == head) 30596822904SAndy Whitcroft break; 30696822904SAndy Whitcroft if (rg->from > t) 3077b24d861SDavidlohr Bueso goto out; 30896822904SAndy Whitcroft 30925985edcSLucas De Marchi /* We overlap with this area, if it extends further than 31096822904SAndy Whitcroft * us then we must extend ourselves. Account for its 31196822904SAndy Whitcroft * existing reservation. */ 31296822904SAndy Whitcroft if (rg->to > t) { 31396822904SAndy Whitcroft chg += rg->to - t; 31496822904SAndy Whitcroft t = rg->to; 31596822904SAndy Whitcroft } 31696822904SAndy Whitcroft chg -= rg->to - rg->from; 31796822904SAndy Whitcroft } 3187b24d861SDavidlohr Bueso 3197b24d861SDavidlohr Bueso out: 3207b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 3217b24d861SDavidlohr Bueso /* We already know we raced and no longer need the new region */ 3227b24d861SDavidlohr Bueso kfree(nrg); 3237b24d861SDavidlohr Bueso return chg; 3247b24d861SDavidlohr Bueso out_nrg: 3257b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 32696822904SAndy Whitcroft return chg; 32796822904SAndy Whitcroft } 32896822904SAndy Whitcroft 3291406ec9bSJoonsoo Kim static long region_truncate(struct resv_map *resv, long end) 33096822904SAndy Whitcroft { 3311406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 33296822904SAndy Whitcroft struct file_region *rg, *trg; 33396822904SAndy Whitcroft long chg = 0; 33496822904SAndy Whitcroft 3357b24d861SDavidlohr Bueso spin_lock(&resv->lock); 33696822904SAndy Whitcroft /* Locate the region we are either in or before. */ 33796822904SAndy Whitcroft list_for_each_entry(rg, head, link) 33896822904SAndy Whitcroft if (end <= rg->to) 33996822904SAndy Whitcroft break; 34096822904SAndy Whitcroft if (&rg->link == head) 3417b24d861SDavidlohr Bueso goto out; 34296822904SAndy Whitcroft 34396822904SAndy Whitcroft /* If we are in the middle of a region then adjust it. */ 34496822904SAndy Whitcroft if (end > rg->from) { 34596822904SAndy Whitcroft chg = rg->to - end; 34696822904SAndy Whitcroft rg->to = end; 34796822904SAndy Whitcroft rg = list_entry(rg->link.next, typeof(*rg), link); 34896822904SAndy Whitcroft } 34996822904SAndy Whitcroft 35096822904SAndy Whitcroft /* Drop any remaining regions. */ 35196822904SAndy Whitcroft list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 35296822904SAndy Whitcroft if (&rg->link == head) 35396822904SAndy Whitcroft break; 35496822904SAndy Whitcroft chg += rg->to - rg->from; 35596822904SAndy Whitcroft list_del(&rg->link); 35696822904SAndy Whitcroft kfree(rg); 35796822904SAndy Whitcroft } 3587b24d861SDavidlohr Bueso 3597b24d861SDavidlohr Bueso out: 3607b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 36196822904SAndy Whitcroft return chg; 36296822904SAndy Whitcroft } 36396822904SAndy Whitcroft 3641406ec9bSJoonsoo Kim static long region_count(struct resv_map *resv, long f, long t) 36584afd99bSAndy Whitcroft { 3661406ec9bSJoonsoo Kim struct list_head *head = &resv->regions; 36784afd99bSAndy Whitcroft struct file_region *rg; 36884afd99bSAndy Whitcroft long chg = 0; 36984afd99bSAndy Whitcroft 3707b24d861SDavidlohr Bueso spin_lock(&resv->lock); 37184afd99bSAndy Whitcroft /* Locate each segment we overlap with, and count that overlap. */ 37284afd99bSAndy Whitcroft list_for_each_entry(rg, head, link) { 373f2135a4aSWang Sheng-Hui long seg_from; 374f2135a4aSWang Sheng-Hui long seg_to; 37584afd99bSAndy Whitcroft 37684afd99bSAndy Whitcroft if (rg->to <= f) 37784afd99bSAndy Whitcroft continue; 37884afd99bSAndy Whitcroft if (rg->from >= t) 37984afd99bSAndy Whitcroft break; 38084afd99bSAndy Whitcroft 38184afd99bSAndy Whitcroft seg_from = max(rg->from, f); 38284afd99bSAndy Whitcroft seg_to = min(rg->to, t); 38384afd99bSAndy Whitcroft 38484afd99bSAndy Whitcroft chg += seg_to - seg_from; 38584afd99bSAndy Whitcroft } 3867b24d861SDavidlohr Bueso spin_unlock(&resv->lock); 38784afd99bSAndy Whitcroft 38884afd99bSAndy Whitcroft return chg; 38984afd99bSAndy Whitcroft } 39084afd99bSAndy Whitcroft 39196822904SAndy Whitcroft /* 392e7c4b0bfSAndy Whitcroft * Convert the address within this vma to the page offset within 393e7c4b0bfSAndy Whitcroft * the mapping, in pagecache page units; huge pages here. 394e7c4b0bfSAndy Whitcroft */ 395a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h, 396a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 397e7c4b0bfSAndy Whitcroft { 398a5516438SAndi Kleen return ((address - vma->vm_start) >> huge_page_shift(h)) + 399a5516438SAndi Kleen (vma->vm_pgoff >> huge_page_order(h)); 400e7c4b0bfSAndy Whitcroft } 401e7c4b0bfSAndy Whitcroft 4020fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma, 4030fe6e20bSNaoya Horiguchi unsigned long address) 4040fe6e20bSNaoya Horiguchi { 4050fe6e20bSNaoya Horiguchi return vma_hugecache_offset(hstate_vma(vma), vma, address); 4060fe6e20bSNaoya Horiguchi } 4070fe6e20bSNaoya Horiguchi 40884afd99bSAndy Whitcroft /* 40908fba699SMel Gorman * Return the size of the pages allocated when backing a VMA. In the majority 41008fba699SMel Gorman * cases this will be same size as used by the page table entries. 41108fba699SMel Gorman */ 41208fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) 41308fba699SMel Gorman { 41408fba699SMel Gorman struct hstate *hstate; 41508fba699SMel Gorman 41608fba699SMel Gorman if (!is_vm_hugetlb_page(vma)) 41708fba699SMel Gorman return PAGE_SIZE; 41808fba699SMel Gorman 41908fba699SMel Gorman hstate = hstate_vma(vma); 42008fba699SMel Gorman 4212415cf12SWanpeng Li return 1UL << huge_page_shift(hstate); 42208fba699SMel Gorman } 423f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize); 42408fba699SMel Gorman 42508fba699SMel Gorman /* 4263340289dSMel Gorman * Return the page size being used by the MMU to back a VMA. In the majority 4273340289dSMel Gorman * of cases, the page size used by the kernel matches the MMU size. On 4283340289dSMel Gorman * architectures where it differs, an architecture-specific version of this 4293340289dSMel Gorman * function is required. 4303340289dSMel Gorman */ 4313340289dSMel Gorman #ifndef vma_mmu_pagesize 4323340289dSMel Gorman unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) 4333340289dSMel Gorman { 4343340289dSMel Gorman return vma_kernel_pagesize(vma); 4353340289dSMel Gorman } 4363340289dSMel Gorman #endif 4373340289dSMel Gorman 4383340289dSMel Gorman /* 43984afd99bSAndy Whitcroft * Flags for MAP_PRIVATE reservations. These are stored in the bottom 44084afd99bSAndy Whitcroft * bits of the reservation map pointer, which are always clear due to 44184afd99bSAndy Whitcroft * alignment. 44284afd99bSAndy Whitcroft */ 44384afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER (1UL << 0) 44484afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1) 44504f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) 44684afd99bSAndy Whitcroft 447a1e78772SMel Gorman /* 448a1e78772SMel Gorman * These helpers are used to track how many pages are reserved for 449a1e78772SMel Gorman * faults in a MAP_PRIVATE mapping. Only the process that called mmap() 450a1e78772SMel Gorman * is guaranteed to have their future faults succeed. 451a1e78772SMel Gorman * 452a1e78772SMel Gorman * With the exception of reset_vma_resv_huge_pages() which is called at fork(), 453a1e78772SMel Gorman * the reserve counters are updated with the hugetlb_lock held. It is safe 454a1e78772SMel Gorman * to reset the VMA at fork() time as it is not in use yet and there is no 455a1e78772SMel Gorman * chance of the global counters getting corrupted as a result of the values. 45684afd99bSAndy Whitcroft * 45784afd99bSAndy Whitcroft * The private mapping reservation is represented in a subtly different 45884afd99bSAndy Whitcroft * manner to a shared mapping. A shared mapping has a region map associated 45984afd99bSAndy Whitcroft * with the underlying file, this region map represents the backing file 46084afd99bSAndy Whitcroft * pages which have ever had a reservation assigned which this persists even 46184afd99bSAndy Whitcroft * after the page is instantiated. A private mapping has a region map 46284afd99bSAndy Whitcroft * associated with the original mmap which is attached to all VMAs which 46384afd99bSAndy Whitcroft * reference it, this region map represents those offsets which have consumed 46484afd99bSAndy Whitcroft * reservation ie. where pages have been instantiated. 465a1e78772SMel Gorman */ 466e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma) 467e7c4b0bfSAndy Whitcroft { 468e7c4b0bfSAndy Whitcroft return (unsigned long)vma->vm_private_data; 469e7c4b0bfSAndy Whitcroft } 470e7c4b0bfSAndy Whitcroft 471e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma, 472e7c4b0bfSAndy Whitcroft unsigned long value) 473e7c4b0bfSAndy Whitcroft { 474e7c4b0bfSAndy Whitcroft vma->vm_private_data = (void *)value; 475e7c4b0bfSAndy Whitcroft } 476e7c4b0bfSAndy Whitcroft 4779119a41eSJoonsoo Kim struct resv_map *resv_map_alloc(void) 47884afd99bSAndy Whitcroft { 47984afd99bSAndy Whitcroft struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); 48084afd99bSAndy Whitcroft if (!resv_map) 48184afd99bSAndy Whitcroft return NULL; 48284afd99bSAndy Whitcroft 48384afd99bSAndy Whitcroft kref_init(&resv_map->refs); 4847b24d861SDavidlohr Bueso spin_lock_init(&resv_map->lock); 48584afd99bSAndy Whitcroft INIT_LIST_HEAD(&resv_map->regions); 48684afd99bSAndy Whitcroft 48784afd99bSAndy Whitcroft return resv_map; 48884afd99bSAndy Whitcroft } 48984afd99bSAndy Whitcroft 4909119a41eSJoonsoo Kim void resv_map_release(struct kref *ref) 49184afd99bSAndy Whitcroft { 49284afd99bSAndy Whitcroft struct resv_map *resv_map = container_of(ref, struct resv_map, refs); 49384afd99bSAndy Whitcroft 49484afd99bSAndy Whitcroft /* Clear out any active regions before we release the map. */ 4951406ec9bSJoonsoo Kim region_truncate(resv_map, 0); 49684afd99bSAndy Whitcroft kfree(resv_map); 49784afd99bSAndy Whitcroft } 49884afd99bSAndy Whitcroft 4994e35f483SJoonsoo Kim static inline struct resv_map *inode_resv_map(struct inode *inode) 5004e35f483SJoonsoo Kim { 5014e35f483SJoonsoo Kim return inode->i_mapping->private_data; 5024e35f483SJoonsoo Kim } 5034e35f483SJoonsoo Kim 50484afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma) 505a1e78772SMel Gorman { 50681d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 5074e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) { 5084e35f483SJoonsoo Kim struct address_space *mapping = vma->vm_file->f_mapping; 5094e35f483SJoonsoo Kim struct inode *inode = mapping->host; 5104e35f483SJoonsoo Kim 5114e35f483SJoonsoo Kim return inode_resv_map(inode); 5124e35f483SJoonsoo Kim 5134e35f483SJoonsoo Kim } else { 51484afd99bSAndy Whitcroft return (struct resv_map *)(get_vma_private_data(vma) & 51584afd99bSAndy Whitcroft ~HPAGE_RESV_MASK); 5164e35f483SJoonsoo Kim } 517a1e78772SMel Gorman } 518a1e78772SMel Gorman 51984afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) 520a1e78772SMel Gorman { 52181d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 52281d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 523a1e78772SMel Gorman 52484afd99bSAndy Whitcroft set_vma_private_data(vma, (get_vma_private_data(vma) & 52584afd99bSAndy Whitcroft HPAGE_RESV_MASK) | (unsigned long)map); 52604f2cbe3SMel Gorman } 52704f2cbe3SMel Gorman 52804f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) 52904f2cbe3SMel Gorman { 53081d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 53181d1b09cSSasha Levin VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); 532e7c4b0bfSAndy Whitcroft 533e7c4b0bfSAndy Whitcroft set_vma_private_data(vma, get_vma_private_data(vma) | flags); 53404f2cbe3SMel Gorman } 53504f2cbe3SMel Gorman 53604f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) 53704f2cbe3SMel Gorman { 53881d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 539e7c4b0bfSAndy Whitcroft 540e7c4b0bfSAndy Whitcroft return (get_vma_private_data(vma) & flag) != 0; 541a1e78772SMel Gorman } 542a1e78772SMel Gorman 54304f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ 544a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma) 545a1e78772SMel Gorman { 54681d1b09cSSasha Levin VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 547f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 548a1e78772SMel Gorman vma->vm_private_data = (void *)0; 549a1e78772SMel Gorman } 550a1e78772SMel Gorman 551a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */ 552af0ed73eSJoonsoo Kim static int vma_has_reserves(struct vm_area_struct *vma, long chg) 553a1e78772SMel Gorman { 554af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_NORESERVE) { 555af0ed73eSJoonsoo Kim /* 556af0ed73eSJoonsoo Kim * This address is already reserved by other process(chg == 0), 557af0ed73eSJoonsoo Kim * so, we should decrement reserved count. Without decrementing, 558af0ed73eSJoonsoo Kim * reserve count remains after releasing inode, because this 559af0ed73eSJoonsoo Kim * allocated page will go into page cache and is regarded as 560af0ed73eSJoonsoo Kim * coming from reserved pool in releasing step. Currently, we 561af0ed73eSJoonsoo Kim * don't have any other solution to deal with this situation 562af0ed73eSJoonsoo Kim * properly, so add work-around here. 563af0ed73eSJoonsoo Kim */ 564af0ed73eSJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE && chg == 0) 565af0ed73eSJoonsoo Kim return 1; 566af0ed73eSJoonsoo Kim else 56772231b03SJoonsoo Kim return 0; 568af0ed73eSJoonsoo Kim } 569a63884e9SJoonsoo Kim 570a63884e9SJoonsoo Kim /* Shared mappings always use reserves */ 571f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) 572a1e78772SMel Gorman return 1; 573a63884e9SJoonsoo Kim 574a63884e9SJoonsoo Kim /* 575a63884e9SJoonsoo Kim * Only the process that called mmap() has reserves for 576a63884e9SJoonsoo Kim * private mappings. 577a63884e9SJoonsoo Kim */ 5787f09ca51SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 5797f09ca51SMel Gorman return 1; 580a63884e9SJoonsoo Kim 5817f09ca51SMel Gorman return 0; 582a1e78772SMel Gorman } 583a1e78772SMel Gorman 584a5516438SAndi Kleen static void enqueue_huge_page(struct hstate *h, struct page *page) 5851da177e4SLinus Torvalds { 5861da177e4SLinus Torvalds int nid = page_to_nid(page); 5870edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_freelists[nid]); 588a5516438SAndi Kleen h->free_huge_pages++; 589a5516438SAndi Kleen h->free_huge_pages_node[nid]++; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 592bf50bab2SNaoya Horiguchi static struct page *dequeue_huge_page_node(struct hstate *h, int nid) 593bf50bab2SNaoya Horiguchi { 594bf50bab2SNaoya Horiguchi struct page *page; 595bf50bab2SNaoya Horiguchi 596c8721bbbSNaoya Horiguchi list_for_each_entry(page, &h->hugepage_freelists[nid], lru) 597c8721bbbSNaoya Horiguchi if (!is_migrate_isolate_page(page)) 598c8721bbbSNaoya Horiguchi break; 599c8721bbbSNaoya Horiguchi /* 600c8721bbbSNaoya Horiguchi * if 'non-isolated free hugepage' not found on the list, 601c8721bbbSNaoya Horiguchi * the allocation fails. 602c8721bbbSNaoya Horiguchi */ 603c8721bbbSNaoya Horiguchi if (&h->hugepage_freelists[nid] == &page->lru) 604bf50bab2SNaoya Horiguchi return NULL; 6050edaecfaSAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 606a9869b83SNaoya Horiguchi set_page_refcounted(page); 607bf50bab2SNaoya Horiguchi h->free_huge_pages--; 608bf50bab2SNaoya Horiguchi h->free_huge_pages_node[nid]--; 609bf50bab2SNaoya Horiguchi return page; 610bf50bab2SNaoya Horiguchi } 611bf50bab2SNaoya Horiguchi 61286cdb465SNaoya Horiguchi /* Movability of hugepages depends on migration support. */ 61386cdb465SNaoya Horiguchi static inline gfp_t htlb_alloc_mask(struct hstate *h) 61486cdb465SNaoya Horiguchi { 615100873d7SNaoya Horiguchi if (hugepages_treat_as_movable || hugepage_migration_supported(h)) 61686cdb465SNaoya Horiguchi return GFP_HIGHUSER_MOVABLE; 61786cdb465SNaoya Horiguchi else 61886cdb465SNaoya Horiguchi return GFP_HIGHUSER; 61986cdb465SNaoya Horiguchi } 62086cdb465SNaoya Horiguchi 621a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h, 622a5516438SAndi Kleen struct vm_area_struct *vma, 623af0ed73eSJoonsoo Kim unsigned long address, int avoid_reserve, 624af0ed73eSJoonsoo Kim long chg) 6251da177e4SLinus Torvalds { 626b1c12cbcSKonstantin Khlebnikov struct page *page = NULL; 627480eccf9SLee Schermerhorn struct mempolicy *mpol; 62819770b32SMel Gorman nodemask_t *nodemask; 629c0ff7453SMiao Xie struct zonelist *zonelist; 630dd1a239fSMel Gorman struct zone *zone; 631dd1a239fSMel Gorman struct zoneref *z; 632cc9a6c87SMel Gorman unsigned int cpuset_mems_cookie; 6331da177e4SLinus Torvalds 634a1e78772SMel Gorman /* 635a1e78772SMel Gorman * A child process with MAP_PRIVATE mappings created by their parent 636a1e78772SMel Gorman * have no page reserves. This check ensures that reservations are 637a1e78772SMel Gorman * not "stolen". The child may still get SIGKILLed 638a1e78772SMel Gorman */ 639af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg) && 640a5516438SAndi Kleen h->free_huge_pages - h->resv_huge_pages == 0) 641c0ff7453SMiao Xie goto err; 642a1e78772SMel Gorman 64304f2cbe3SMel Gorman /* If reserves cannot be used, ensure enough pages are in the pool */ 644a5516438SAndi Kleen if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0) 6456eab04a8SJustin P. Mattock goto err; 64604f2cbe3SMel Gorman 6479966c4bbSJoonsoo Kim retry_cpuset: 648d26914d1SMel Gorman cpuset_mems_cookie = read_mems_allowed_begin(); 6499966c4bbSJoonsoo Kim zonelist = huge_zonelist(vma, address, 65086cdb465SNaoya Horiguchi htlb_alloc_mask(h), &mpol, &nodemask); 6519966c4bbSJoonsoo Kim 65219770b32SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 65319770b32SMel Gorman MAX_NR_ZONES - 1, nodemask) { 654344736f2SVladimir Davydov if (cpuset_zone_allowed(zone, htlb_alloc_mask(h))) { 655bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, zone_to_nid(zone)); 656bf50bab2SNaoya Horiguchi if (page) { 657af0ed73eSJoonsoo Kim if (avoid_reserve) 658af0ed73eSJoonsoo Kim break; 659af0ed73eSJoonsoo Kim if (!vma_has_reserves(vma, chg)) 660af0ed73eSJoonsoo Kim break; 661af0ed73eSJoonsoo Kim 66207443a85SJoonsoo Kim SetPagePrivate(page); 663a63884e9SJoonsoo Kim h->resv_huge_pages--; 6645ab3ee7bSKen Chen break; 6651da177e4SLinus Torvalds } 6663abf7afdSAndrew Morton } 667bf50bab2SNaoya Horiguchi } 668cc9a6c87SMel Gorman 669cc9a6c87SMel Gorman mpol_cond_put(mpol); 670d26914d1SMel Gorman if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie))) 671cc9a6c87SMel Gorman goto retry_cpuset; 672cc9a6c87SMel Gorman return page; 673cc9a6c87SMel Gorman 674c0ff7453SMiao Xie err: 675cc9a6c87SMel Gorman return NULL; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 6781cac6f2cSLuiz Capitulino /* 6791cac6f2cSLuiz Capitulino * common helper functions for hstate_next_node_to_{alloc|free}. 6801cac6f2cSLuiz Capitulino * We may have allocated or freed a huge page based on a different 6811cac6f2cSLuiz Capitulino * nodes_allowed previously, so h->next_node_to_{alloc|free} might 6821cac6f2cSLuiz Capitulino * be outside of *nodes_allowed. Ensure that we use an allowed 6831cac6f2cSLuiz Capitulino * node for alloc or free. 6841cac6f2cSLuiz Capitulino */ 6851cac6f2cSLuiz Capitulino static int next_node_allowed(int nid, nodemask_t *nodes_allowed) 6861cac6f2cSLuiz Capitulino { 6871cac6f2cSLuiz Capitulino nid = next_node(nid, *nodes_allowed); 6881cac6f2cSLuiz Capitulino if (nid == MAX_NUMNODES) 6891cac6f2cSLuiz Capitulino nid = first_node(*nodes_allowed); 6901cac6f2cSLuiz Capitulino VM_BUG_ON(nid >= MAX_NUMNODES); 6911cac6f2cSLuiz Capitulino 6921cac6f2cSLuiz Capitulino return nid; 6931cac6f2cSLuiz Capitulino } 6941cac6f2cSLuiz Capitulino 6951cac6f2cSLuiz Capitulino static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) 6961cac6f2cSLuiz Capitulino { 6971cac6f2cSLuiz Capitulino if (!node_isset(nid, *nodes_allowed)) 6981cac6f2cSLuiz Capitulino nid = next_node_allowed(nid, nodes_allowed); 6991cac6f2cSLuiz Capitulino return nid; 7001cac6f2cSLuiz Capitulino } 7011cac6f2cSLuiz Capitulino 7021cac6f2cSLuiz Capitulino /* 7031cac6f2cSLuiz Capitulino * returns the previously saved node ["this node"] from which to 7041cac6f2cSLuiz Capitulino * allocate a persistent huge page for the pool and advance the 7051cac6f2cSLuiz Capitulino * next node from which to allocate, handling wrap at end of node 7061cac6f2cSLuiz Capitulino * mask. 7071cac6f2cSLuiz Capitulino */ 7081cac6f2cSLuiz Capitulino static int hstate_next_node_to_alloc(struct hstate *h, 7091cac6f2cSLuiz Capitulino nodemask_t *nodes_allowed) 7101cac6f2cSLuiz Capitulino { 7111cac6f2cSLuiz Capitulino int nid; 7121cac6f2cSLuiz Capitulino 7131cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 7141cac6f2cSLuiz Capitulino 7151cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); 7161cac6f2cSLuiz Capitulino h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); 7171cac6f2cSLuiz Capitulino 7181cac6f2cSLuiz Capitulino return nid; 7191cac6f2cSLuiz Capitulino } 7201cac6f2cSLuiz Capitulino 7211cac6f2cSLuiz Capitulino /* 7221cac6f2cSLuiz Capitulino * helper for free_pool_huge_page() - return the previously saved 7231cac6f2cSLuiz Capitulino * node ["this node"] from which to free a huge page. Advance the 7241cac6f2cSLuiz Capitulino * next node id whether or not we find a free huge page to free so 7251cac6f2cSLuiz Capitulino * that the next attempt to free addresses the next node. 7261cac6f2cSLuiz Capitulino */ 7271cac6f2cSLuiz Capitulino static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) 7281cac6f2cSLuiz Capitulino { 7291cac6f2cSLuiz Capitulino int nid; 7301cac6f2cSLuiz Capitulino 7311cac6f2cSLuiz Capitulino VM_BUG_ON(!nodes_allowed); 7321cac6f2cSLuiz Capitulino 7331cac6f2cSLuiz Capitulino nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); 7341cac6f2cSLuiz Capitulino h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); 7351cac6f2cSLuiz Capitulino 7361cac6f2cSLuiz Capitulino return nid; 7371cac6f2cSLuiz Capitulino } 7381cac6f2cSLuiz Capitulino 7391cac6f2cSLuiz Capitulino #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \ 7401cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 7411cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 7421cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ 7431cac6f2cSLuiz Capitulino nr_nodes--) 7441cac6f2cSLuiz Capitulino 7451cac6f2cSLuiz Capitulino #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ 7461cac6f2cSLuiz Capitulino for (nr_nodes = nodes_weight(*mask); \ 7471cac6f2cSLuiz Capitulino nr_nodes > 0 && \ 7481cac6f2cSLuiz Capitulino ((node = hstate_next_node_to_free(hs, mask)) || 1); \ 7491cac6f2cSLuiz Capitulino nr_nodes--) 7501cac6f2cSLuiz Capitulino 751944d9fecSLuiz Capitulino #if defined(CONFIG_CMA) && defined(CONFIG_X86_64) 752944d9fecSLuiz Capitulino static void destroy_compound_gigantic_page(struct page *page, 753944d9fecSLuiz Capitulino unsigned long order) 754944d9fecSLuiz Capitulino { 755944d9fecSLuiz Capitulino int i; 756944d9fecSLuiz Capitulino int nr_pages = 1 << order; 757944d9fecSLuiz Capitulino struct page *p = page + 1; 758944d9fecSLuiz Capitulino 759944d9fecSLuiz Capitulino for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 760944d9fecSLuiz Capitulino __ClearPageTail(p); 761944d9fecSLuiz Capitulino set_page_refcounted(p); 762944d9fecSLuiz Capitulino p->first_page = NULL; 763944d9fecSLuiz Capitulino } 764944d9fecSLuiz Capitulino 765944d9fecSLuiz Capitulino set_compound_order(page, 0); 766944d9fecSLuiz Capitulino __ClearPageHead(page); 767944d9fecSLuiz Capitulino } 768944d9fecSLuiz Capitulino 769944d9fecSLuiz Capitulino static void free_gigantic_page(struct page *page, unsigned order) 770944d9fecSLuiz Capitulino { 771944d9fecSLuiz Capitulino free_contig_range(page_to_pfn(page), 1 << order); 772944d9fecSLuiz Capitulino } 773944d9fecSLuiz Capitulino 774944d9fecSLuiz Capitulino static int __alloc_gigantic_page(unsigned long start_pfn, 775944d9fecSLuiz Capitulino unsigned long nr_pages) 776944d9fecSLuiz Capitulino { 777944d9fecSLuiz Capitulino unsigned long end_pfn = start_pfn + nr_pages; 778944d9fecSLuiz Capitulino return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 779944d9fecSLuiz Capitulino } 780944d9fecSLuiz Capitulino 781944d9fecSLuiz Capitulino static bool pfn_range_valid_gigantic(unsigned long start_pfn, 782944d9fecSLuiz Capitulino unsigned long nr_pages) 783944d9fecSLuiz Capitulino { 784944d9fecSLuiz Capitulino unsigned long i, end_pfn = start_pfn + nr_pages; 785944d9fecSLuiz Capitulino struct page *page; 786944d9fecSLuiz Capitulino 787944d9fecSLuiz Capitulino for (i = start_pfn; i < end_pfn; i++) { 788944d9fecSLuiz Capitulino if (!pfn_valid(i)) 789944d9fecSLuiz Capitulino return false; 790944d9fecSLuiz Capitulino 791944d9fecSLuiz Capitulino page = pfn_to_page(i); 792944d9fecSLuiz Capitulino 793944d9fecSLuiz Capitulino if (PageReserved(page)) 794944d9fecSLuiz Capitulino return false; 795944d9fecSLuiz Capitulino 796944d9fecSLuiz Capitulino if (page_count(page) > 0) 797944d9fecSLuiz Capitulino return false; 798944d9fecSLuiz Capitulino 799944d9fecSLuiz Capitulino if (PageHuge(page)) 800944d9fecSLuiz Capitulino return false; 801944d9fecSLuiz Capitulino } 802944d9fecSLuiz Capitulino 803944d9fecSLuiz Capitulino return true; 804944d9fecSLuiz Capitulino } 805944d9fecSLuiz Capitulino 806944d9fecSLuiz Capitulino static bool zone_spans_last_pfn(const struct zone *zone, 807944d9fecSLuiz Capitulino unsigned long start_pfn, unsigned long nr_pages) 808944d9fecSLuiz Capitulino { 809944d9fecSLuiz Capitulino unsigned long last_pfn = start_pfn + nr_pages - 1; 810944d9fecSLuiz Capitulino return zone_spans_pfn(zone, last_pfn); 811944d9fecSLuiz Capitulino } 812944d9fecSLuiz Capitulino 813944d9fecSLuiz Capitulino static struct page *alloc_gigantic_page(int nid, unsigned order) 814944d9fecSLuiz Capitulino { 815944d9fecSLuiz Capitulino unsigned long nr_pages = 1 << order; 816944d9fecSLuiz Capitulino unsigned long ret, pfn, flags; 817944d9fecSLuiz Capitulino struct zone *z; 818944d9fecSLuiz Capitulino 819944d9fecSLuiz Capitulino z = NODE_DATA(nid)->node_zones; 820944d9fecSLuiz Capitulino for (; z - NODE_DATA(nid)->node_zones < MAX_NR_ZONES; z++) { 821944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 822944d9fecSLuiz Capitulino 823944d9fecSLuiz Capitulino pfn = ALIGN(z->zone_start_pfn, nr_pages); 824944d9fecSLuiz Capitulino while (zone_spans_last_pfn(z, pfn, nr_pages)) { 825944d9fecSLuiz Capitulino if (pfn_range_valid_gigantic(pfn, nr_pages)) { 826944d9fecSLuiz Capitulino /* 827944d9fecSLuiz Capitulino * We release the zone lock here because 828944d9fecSLuiz Capitulino * alloc_contig_range() will also lock the zone 829944d9fecSLuiz Capitulino * at some point. If there's an allocation 830944d9fecSLuiz Capitulino * spinning on this lock, it may win the race 831944d9fecSLuiz Capitulino * and cause alloc_contig_range() to fail... 832944d9fecSLuiz Capitulino */ 833944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 834944d9fecSLuiz Capitulino ret = __alloc_gigantic_page(pfn, nr_pages); 835944d9fecSLuiz Capitulino if (!ret) 836944d9fecSLuiz Capitulino return pfn_to_page(pfn); 837944d9fecSLuiz Capitulino spin_lock_irqsave(&z->lock, flags); 838944d9fecSLuiz Capitulino } 839944d9fecSLuiz Capitulino pfn += nr_pages; 840944d9fecSLuiz Capitulino } 841944d9fecSLuiz Capitulino 842944d9fecSLuiz Capitulino spin_unlock_irqrestore(&z->lock, flags); 843944d9fecSLuiz Capitulino } 844944d9fecSLuiz Capitulino 845944d9fecSLuiz Capitulino return NULL; 846944d9fecSLuiz Capitulino } 847944d9fecSLuiz Capitulino 848944d9fecSLuiz Capitulino static void prep_new_huge_page(struct hstate *h, struct page *page, int nid); 849944d9fecSLuiz Capitulino static void prep_compound_gigantic_page(struct page *page, unsigned long order); 850944d9fecSLuiz Capitulino 851944d9fecSLuiz Capitulino static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid) 852944d9fecSLuiz Capitulino { 853944d9fecSLuiz Capitulino struct page *page; 854944d9fecSLuiz Capitulino 855944d9fecSLuiz Capitulino page = alloc_gigantic_page(nid, huge_page_order(h)); 856944d9fecSLuiz Capitulino if (page) { 857944d9fecSLuiz Capitulino prep_compound_gigantic_page(page, huge_page_order(h)); 858944d9fecSLuiz Capitulino prep_new_huge_page(h, page, nid); 859944d9fecSLuiz Capitulino } 860944d9fecSLuiz Capitulino 861944d9fecSLuiz Capitulino return page; 862944d9fecSLuiz Capitulino } 863944d9fecSLuiz Capitulino 864944d9fecSLuiz Capitulino static int alloc_fresh_gigantic_page(struct hstate *h, 865944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) 866944d9fecSLuiz Capitulino { 867944d9fecSLuiz Capitulino struct page *page = NULL; 868944d9fecSLuiz Capitulino int nr_nodes, node; 869944d9fecSLuiz Capitulino 870944d9fecSLuiz Capitulino for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 871944d9fecSLuiz Capitulino page = alloc_fresh_gigantic_page_node(h, node); 872944d9fecSLuiz Capitulino if (page) 873944d9fecSLuiz Capitulino return 1; 874944d9fecSLuiz Capitulino } 875944d9fecSLuiz Capitulino 876944d9fecSLuiz Capitulino return 0; 877944d9fecSLuiz Capitulino } 878944d9fecSLuiz Capitulino 879944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return true; } 880944d9fecSLuiz Capitulino #else 881944d9fecSLuiz Capitulino static inline bool gigantic_page_supported(void) { return false; } 882944d9fecSLuiz Capitulino static inline void free_gigantic_page(struct page *page, unsigned order) { } 883944d9fecSLuiz Capitulino static inline void destroy_compound_gigantic_page(struct page *page, 884944d9fecSLuiz Capitulino unsigned long order) { } 885944d9fecSLuiz Capitulino static inline int alloc_fresh_gigantic_page(struct hstate *h, 886944d9fecSLuiz Capitulino nodemask_t *nodes_allowed) { return 0; } 887944d9fecSLuiz Capitulino #endif 888944d9fecSLuiz Capitulino 889a5516438SAndi Kleen static void update_and_free_page(struct hstate *h, struct page *page) 8906af2acb6SAdam Litke { 8916af2acb6SAdam Litke int i; 892a5516438SAndi Kleen 893944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 894944d9fecSLuiz Capitulino return; 89518229df5SAndy Whitcroft 896a5516438SAndi Kleen h->nr_huge_pages--; 897a5516438SAndi Kleen h->nr_huge_pages_node[page_to_nid(page)]--; 898a5516438SAndi Kleen for (i = 0; i < pages_per_huge_page(h); i++) { 89932f84528SChris Forbes page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 90032f84528SChris Forbes 1 << PG_referenced | 1 << PG_dirty | 901a7407a27SLuiz Capitulino 1 << PG_active | 1 << PG_private | 902a7407a27SLuiz Capitulino 1 << PG_writeback); 9036af2acb6SAdam Litke } 904309381feSSasha Levin VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page); 9056af2acb6SAdam Litke set_compound_page_dtor(page, NULL); 9066af2acb6SAdam Litke set_page_refcounted(page); 907944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) { 908944d9fecSLuiz Capitulino destroy_compound_gigantic_page(page, huge_page_order(h)); 909944d9fecSLuiz Capitulino free_gigantic_page(page, huge_page_order(h)); 910944d9fecSLuiz Capitulino } else { 9117f2e9525SGerald Schaefer arch_release_hugepage(page); 912a5516438SAndi Kleen __free_pages(page, huge_page_order(h)); 9136af2acb6SAdam Litke } 914944d9fecSLuiz Capitulino } 9156af2acb6SAdam Litke 916e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size) 917e5ff2159SAndi Kleen { 918e5ff2159SAndi Kleen struct hstate *h; 919e5ff2159SAndi Kleen 920e5ff2159SAndi Kleen for_each_hstate(h) { 921e5ff2159SAndi Kleen if (huge_page_size(h) == size) 922e5ff2159SAndi Kleen return h; 923e5ff2159SAndi Kleen } 924e5ff2159SAndi Kleen return NULL; 925e5ff2159SAndi Kleen } 926e5ff2159SAndi Kleen 927bcc54222SNaoya Horiguchi /* 928bcc54222SNaoya Horiguchi * Test to determine whether the hugepage is "active/in-use" (i.e. being linked 929bcc54222SNaoya Horiguchi * to hstate->hugepage_activelist.) 930bcc54222SNaoya Horiguchi * 931bcc54222SNaoya Horiguchi * This function can be called for tail pages, but never returns true for them. 932bcc54222SNaoya Horiguchi */ 933bcc54222SNaoya Horiguchi bool page_huge_active(struct page *page) 934bcc54222SNaoya Horiguchi { 935bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHuge(page), page); 936bcc54222SNaoya Horiguchi return PageHead(page) && PagePrivate(&page[1]); 937bcc54222SNaoya Horiguchi } 938bcc54222SNaoya Horiguchi 939bcc54222SNaoya Horiguchi /* never called for tail page */ 940bcc54222SNaoya Horiguchi static void set_page_huge_active(struct page *page) 941bcc54222SNaoya Horiguchi { 942bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 943bcc54222SNaoya Horiguchi SetPagePrivate(&page[1]); 944bcc54222SNaoya Horiguchi } 945bcc54222SNaoya Horiguchi 946bcc54222SNaoya Horiguchi static void clear_page_huge_active(struct page *page) 947bcc54222SNaoya Horiguchi { 948bcc54222SNaoya Horiguchi VM_BUG_ON_PAGE(!PageHeadHuge(page), page); 949bcc54222SNaoya Horiguchi ClearPagePrivate(&page[1]); 950bcc54222SNaoya Horiguchi } 951bcc54222SNaoya Horiguchi 9528f1d26d0SAtsushi Kumagai void free_huge_page(struct page *page) 95327a85ef1SDavid Gibson { 954a5516438SAndi Kleen /* 955a5516438SAndi Kleen * Can't pass hstate in here because it is called from the 956a5516438SAndi Kleen * compound page destructor. 957a5516438SAndi Kleen */ 958e5ff2159SAndi Kleen struct hstate *h = page_hstate(page); 9597893d1d5SAdam Litke int nid = page_to_nid(page); 96090481622SDavid Gibson struct hugepage_subpool *spool = 96190481622SDavid Gibson (struct hugepage_subpool *)page_private(page); 96207443a85SJoonsoo Kim bool restore_reserve; 96327a85ef1SDavid Gibson 964e5df70abSAndy Whitcroft set_page_private(page, 0); 96523be7468SMel Gorman page->mapping = NULL; 9667893d1d5SAdam Litke BUG_ON(page_count(page)); 9670fe6e20bSNaoya Horiguchi BUG_ON(page_mapcount(page)); 96807443a85SJoonsoo Kim restore_reserve = PagePrivate(page); 96916c794b4SJoonsoo Kim ClearPagePrivate(page); 97027a85ef1SDavid Gibson 9711c5ecae3SMike Kravetz /* 9721c5ecae3SMike Kravetz * A return code of zero implies that the subpool will be under its 9731c5ecae3SMike Kravetz * minimum size if the reservation is not restored after page is free. 9741c5ecae3SMike Kravetz * Therefore, force restore_reserve operation. 9751c5ecae3SMike Kravetz */ 9761c5ecae3SMike Kravetz if (hugepage_subpool_put_pages(spool, 1) == 0) 9771c5ecae3SMike Kravetz restore_reserve = true; 9781c5ecae3SMike Kravetz 97927a85ef1SDavid Gibson spin_lock(&hugetlb_lock); 980bcc54222SNaoya Horiguchi clear_page_huge_active(page); 9816d76dcf4SAneesh Kumar K.V hugetlb_cgroup_uncharge_page(hstate_index(h), 9826d76dcf4SAneesh Kumar K.V pages_per_huge_page(h), page); 98307443a85SJoonsoo Kim if (restore_reserve) 98407443a85SJoonsoo Kim h->resv_huge_pages++; 98507443a85SJoonsoo Kim 986944d9fecSLuiz Capitulino if (h->surplus_huge_pages_node[nid]) { 9870edaecfaSAneesh Kumar K.V /* remove the page from active list */ 9880edaecfaSAneesh Kumar K.V list_del(&page->lru); 989a5516438SAndi Kleen update_and_free_page(h, page); 990a5516438SAndi Kleen h->surplus_huge_pages--; 991a5516438SAndi Kleen h->surplus_huge_pages_node[nid]--; 9927893d1d5SAdam Litke } else { 9935d3a551cSWill Deacon arch_clear_hugepage_flags(page); 994a5516438SAndi Kleen enqueue_huge_page(h, page); 9957893d1d5SAdam Litke } 99627a85ef1SDavid Gibson spin_unlock(&hugetlb_lock); 99727a85ef1SDavid Gibson } 99827a85ef1SDavid Gibson 999a5516438SAndi Kleen static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) 1000b7ba30c6SAndi Kleen { 10010edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1002b7ba30c6SAndi Kleen set_compound_page_dtor(page, free_huge_page); 1003b7ba30c6SAndi Kleen spin_lock(&hugetlb_lock); 10049dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1005a5516438SAndi Kleen h->nr_huge_pages++; 1006a5516438SAndi Kleen h->nr_huge_pages_node[nid]++; 1007b7ba30c6SAndi Kleen spin_unlock(&hugetlb_lock); 1008b7ba30c6SAndi Kleen put_page(page); /* free it into the hugepage allocator */ 1009b7ba30c6SAndi Kleen } 1010b7ba30c6SAndi Kleen 10112906dd52SLuiz Capitulino static void prep_compound_gigantic_page(struct page *page, unsigned long order) 101220a0307cSWu Fengguang { 101320a0307cSWu Fengguang int i; 101420a0307cSWu Fengguang int nr_pages = 1 << order; 101520a0307cSWu Fengguang struct page *p = page + 1; 101620a0307cSWu Fengguang 101720a0307cSWu Fengguang /* we rely on prep_new_huge_page to set the destructor */ 101820a0307cSWu Fengguang set_compound_order(page, order); 101920a0307cSWu Fengguang __SetPageHead(page); 1020ef5a22beSAndrea Arcangeli __ClearPageReserved(page); 102120a0307cSWu Fengguang for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { 1022ef5a22beSAndrea Arcangeli /* 1023ef5a22beSAndrea Arcangeli * For gigantic hugepages allocated through bootmem at 1024ef5a22beSAndrea Arcangeli * boot, it's safer to be consistent with the not-gigantic 1025ef5a22beSAndrea Arcangeli * hugepages and clear the PG_reserved bit from all tail pages 1026ef5a22beSAndrea Arcangeli * too. Otherwse drivers using get_user_pages() to access tail 1027ef5a22beSAndrea Arcangeli * pages may get the reference counting wrong if they see 1028ef5a22beSAndrea Arcangeli * PG_reserved set on a tail page (despite the head page not 1029ef5a22beSAndrea Arcangeli * having PG_reserved set). Enforcing this consistency between 1030ef5a22beSAndrea Arcangeli * head and tail pages allows drivers to optimize away a check 1031ef5a22beSAndrea Arcangeli * on the head page when they need know if put_page() is needed 1032ef5a22beSAndrea Arcangeli * after get_user_pages(). 1033ef5a22beSAndrea Arcangeli */ 1034ef5a22beSAndrea Arcangeli __ClearPageReserved(p); 103558a84aa9SYouquan Song set_page_count(p, 0); 103620a0307cSWu Fengguang p->first_page = page; 103744fc8057SDavid Rientjes /* Make sure p->first_page is always valid for PageTail() */ 103844fc8057SDavid Rientjes smp_wmb(); 103944fc8057SDavid Rientjes __SetPageTail(p); 104020a0307cSWu Fengguang } 104120a0307cSWu Fengguang } 104220a0307cSWu Fengguang 10437795912cSAndrew Morton /* 10447795912cSAndrew Morton * PageHuge() only returns true for hugetlbfs pages, but not for normal or 10457795912cSAndrew Morton * transparent huge pages. See the PageTransHuge() documentation for more 10467795912cSAndrew Morton * details. 10477795912cSAndrew Morton */ 104820a0307cSWu Fengguang int PageHuge(struct page *page) 104920a0307cSWu Fengguang { 105020a0307cSWu Fengguang if (!PageCompound(page)) 105120a0307cSWu Fengguang return 0; 105220a0307cSWu Fengguang 105320a0307cSWu Fengguang page = compound_head(page); 1054758f66a2SAndrew Morton return get_compound_page_dtor(page) == free_huge_page; 105520a0307cSWu Fengguang } 105643131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge); 105743131e14SNaoya Horiguchi 105827c73ae7SAndrea Arcangeli /* 105927c73ae7SAndrea Arcangeli * PageHeadHuge() only returns true for hugetlbfs head page, but not for 106027c73ae7SAndrea Arcangeli * normal or transparent huge pages. 106127c73ae7SAndrea Arcangeli */ 106227c73ae7SAndrea Arcangeli int PageHeadHuge(struct page *page_head) 106327c73ae7SAndrea Arcangeli { 106427c73ae7SAndrea Arcangeli if (!PageHead(page_head)) 106527c73ae7SAndrea Arcangeli return 0; 106627c73ae7SAndrea Arcangeli 1067758f66a2SAndrew Morton return get_compound_page_dtor(page_head) == free_huge_page; 106827c73ae7SAndrea Arcangeli } 106927c73ae7SAndrea Arcangeli 107013d60f4bSZhang Yi pgoff_t __basepage_index(struct page *page) 107113d60f4bSZhang Yi { 107213d60f4bSZhang Yi struct page *page_head = compound_head(page); 107313d60f4bSZhang Yi pgoff_t index = page_index(page_head); 107413d60f4bSZhang Yi unsigned long compound_idx; 107513d60f4bSZhang Yi 107613d60f4bSZhang Yi if (!PageHuge(page_head)) 107713d60f4bSZhang Yi return page_index(page); 107813d60f4bSZhang Yi 107913d60f4bSZhang Yi if (compound_order(page_head) >= MAX_ORDER) 108013d60f4bSZhang Yi compound_idx = page_to_pfn(page) - page_to_pfn(page_head); 108113d60f4bSZhang Yi else 108213d60f4bSZhang Yi compound_idx = page - page_head; 108313d60f4bSZhang Yi 108413d60f4bSZhang Yi return (index << compound_order(page_head)) + compound_idx; 108513d60f4bSZhang Yi } 108613d60f4bSZhang Yi 1087a5516438SAndi Kleen static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid) 10881da177e4SLinus Torvalds { 10891da177e4SLinus Torvalds struct page *page; 1090f96efd58SJoe Jin 10916484eb3eSMel Gorman page = alloc_pages_exact_node(nid, 109286cdb465SNaoya Horiguchi htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| 1093551883aeSNishanth Aravamudan __GFP_REPEAT|__GFP_NOWARN, 1094a5516438SAndi Kleen huge_page_order(h)); 10951da177e4SLinus Torvalds if (page) { 10967f2e9525SGerald Schaefer if (arch_prepare_hugepage(page)) { 1097caff3a2cSGerald Schaefer __free_pages(page, huge_page_order(h)); 10987b8ee84dSHarvey Harrison return NULL; 10997f2e9525SGerald Schaefer } 1100a5516438SAndi Kleen prep_new_huge_page(h, page, nid); 11011da177e4SLinus Torvalds } 110263b4613cSNishanth Aravamudan 110363b4613cSNishanth Aravamudan return page; 110463b4613cSNishanth Aravamudan } 110563b4613cSNishanth Aravamudan 1106b2261026SJoonsoo Kim static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed) 1107b2261026SJoonsoo Kim { 1108b2261026SJoonsoo Kim struct page *page; 1109b2261026SJoonsoo Kim int nr_nodes, node; 1110b2261026SJoonsoo Kim int ret = 0; 1111b2261026SJoonsoo Kim 1112b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1113b2261026SJoonsoo Kim page = alloc_fresh_huge_page_node(h, node); 1114b2261026SJoonsoo Kim if (page) { 1115b2261026SJoonsoo Kim ret = 1; 1116b2261026SJoonsoo Kim break; 1117b2261026SJoonsoo Kim } 1118b2261026SJoonsoo Kim } 1119b2261026SJoonsoo Kim 1120b2261026SJoonsoo Kim if (ret) 1121b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC); 1122b2261026SJoonsoo Kim else 1123b2261026SJoonsoo Kim count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 1124b2261026SJoonsoo Kim 1125b2261026SJoonsoo Kim return ret; 1126b2261026SJoonsoo Kim } 1127b2261026SJoonsoo Kim 1128e8c5c824SLee Schermerhorn /* 1129e8c5c824SLee Schermerhorn * Free huge page from pool from next node to free. 1130e8c5c824SLee Schermerhorn * Attempt to keep persistent huge pages more or less 1131e8c5c824SLee Schermerhorn * balanced over allowed nodes. 1132e8c5c824SLee Schermerhorn * Called with hugetlb_lock locked. 1133e8c5c824SLee Schermerhorn */ 11346ae11b27SLee Schermerhorn static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, 11356ae11b27SLee Schermerhorn bool acct_surplus) 1136e8c5c824SLee Schermerhorn { 1137b2261026SJoonsoo Kim int nr_nodes, node; 1138e8c5c824SLee Schermerhorn int ret = 0; 1139e8c5c824SLee Schermerhorn 1140b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1141685f3457SLee Schermerhorn /* 1142685f3457SLee Schermerhorn * If we're returning unused surplus pages, only examine 1143685f3457SLee Schermerhorn * nodes with surplus pages. 1144685f3457SLee Schermerhorn */ 1145b2261026SJoonsoo Kim if ((!acct_surplus || h->surplus_huge_pages_node[node]) && 1146b2261026SJoonsoo Kim !list_empty(&h->hugepage_freelists[node])) { 1147e8c5c824SLee Schermerhorn struct page *page = 1148b2261026SJoonsoo Kim list_entry(h->hugepage_freelists[node].next, 1149e8c5c824SLee Schermerhorn struct page, lru); 1150e8c5c824SLee Schermerhorn list_del(&page->lru); 1151e8c5c824SLee Schermerhorn h->free_huge_pages--; 1152b2261026SJoonsoo Kim h->free_huge_pages_node[node]--; 1153685f3457SLee Schermerhorn if (acct_surplus) { 1154685f3457SLee Schermerhorn h->surplus_huge_pages--; 1155b2261026SJoonsoo Kim h->surplus_huge_pages_node[node]--; 1156685f3457SLee Schermerhorn } 1157e8c5c824SLee Schermerhorn update_and_free_page(h, page); 1158e8c5c824SLee Schermerhorn ret = 1; 11599a76db09SLee Schermerhorn break; 1160e8c5c824SLee Schermerhorn } 1161b2261026SJoonsoo Kim } 1162e8c5c824SLee Schermerhorn 1163e8c5c824SLee Schermerhorn return ret; 1164e8c5c824SLee Schermerhorn } 1165e8c5c824SLee Schermerhorn 1166c8721bbbSNaoya Horiguchi /* 1167c8721bbbSNaoya Horiguchi * Dissolve a given free hugepage into free buddy pages. This function does 1168c8721bbbSNaoya Horiguchi * nothing for in-use (including surplus) hugepages. 1169c8721bbbSNaoya Horiguchi */ 1170c8721bbbSNaoya Horiguchi static void dissolve_free_huge_page(struct page *page) 1171c8721bbbSNaoya Horiguchi { 1172c8721bbbSNaoya Horiguchi spin_lock(&hugetlb_lock); 1173c8721bbbSNaoya Horiguchi if (PageHuge(page) && !page_count(page)) { 1174c8721bbbSNaoya Horiguchi struct hstate *h = page_hstate(page); 1175c8721bbbSNaoya Horiguchi int nid = page_to_nid(page); 1176c8721bbbSNaoya Horiguchi list_del(&page->lru); 1177c8721bbbSNaoya Horiguchi h->free_huge_pages--; 1178c8721bbbSNaoya Horiguchi h->free_huge_pages_node[nid]--; 1179c8721bbbSNaoya Horiguchi update_and_free_page(h, page); 1180c8721bbbSNaoya Horiguchi } 1181c8721bbbSNaoya Horiguchi spin_unlock(&hugetlb_lock); 1182c8721bbbSNaoya Horiguchi } 1183c8721bbbSNaoya Horiguchi 1184c8721bbbSNaoya Horiguchi /* 1185c8721bbbSNaoya Horiguchi * Dissolve free hugepages in a given pfn range. Used by memory hotplug to 1186c8721bbbSNaoya Horiguchi * make specified memory blocks removable from the system. 1187c8721bbbSNaoya Horiguchi * Note that start_pfn should aligned with (minimum) hugepage size. 1188c8721bbbSNaoya Horiguchi */ 1189c8721bbbSNaoya Horiguchi void dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) 1190c8721bbbSNaoya Horiguchi { 1191c8721bbbSNaoya Horiguchi unsigned int order = 8 * sizeof(void *); 1192c8721bbbSNaoya Horiguchi unsigned long pfn; 1193c8721bbbSNaoya Horiguchi struct hstate *h; 1194c8721bbbSNaoya Horiguchi 1195d0177639SLi Zhong if (!hugepages_supported()) 1196d0177639SLi Zhong return; 1197d0177639SLi Zhong 1198c8721bbbSNaoya Horiguchi /* Set scan step to minimum hugepage size */ 1199c8721bbbSNaoya Horiguchi for_each_hstate(h) 1200c8721bbbSNaoya Horiguchi if (order > huge_page_order(h)) 1201c8721bbbSNaoya Horiguchi order = huge_page_order(h); 1202c8721bbbSNaoya Horiguchi VM_BUG_ON(!IS_ALIGNED(start_pfn, 1 << order)); 1203c8721bbbSNaoya Horiguchi for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) 1204c8721bbbSNaoya Horiguchi dissolve_free_huge_page(pfn_to_page(pfn)); 1205c8721bbbSNaoya Horiguchi } 1206c8721bbbSNaoya Horiguchi 1207bf50bab2SNaoya Horiguchi static struct page *alloc_buddy_huge_page(struct hstate *h, int nid) 12087893d1d5SAdam Litke { 12097893d1d5SAdam Litke struct page *page; 1210bf50bab2SNaoya Horiguchi unsigned int r_nid; 12117893d1d5SAdam Litke 1212bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1213aa888a74SAndi Kleen return NULL; 1214aa888a74SAndi Kleen 1215d1c3fb1fSNishanth Aravamudan /* 1216d1c3fb1fSNishanth Aravamudan * Assume we will successfully allocate the surplus page to 1217d1c3fb1fSNishanth Aravamudan * prevent racing processes from causing the surplus to exceed 1218d1c3fb1fSNishanth Aravamudan * overcommit 1219d1c3fb1fSNishanth Aravamudan * 1220d1c3fb1fSNishanth Aravamudan * This however introduces a different race, where a process B 1221d1c3fb1fSNishanth Aravamudan * tries to grow the static hugepage pool while alloc_pages() is 1222d1c3fb1fSNishanth Aravamudan * called by process A. B will only examine the per-node 1223d1c3fb1fSNishanth Aravamudan * counters in determining if surplus huge pages can be 1224d1c3fb1fSNishanth Aravamudan * converted to normal huge pages in adjust_pool_surplus(). A 1225d1c3fb1fSNishanth Aravamudan * won't be able to increment the per-node counter, until the 1226d1c3fb1fSNishanth Aravamudan * lock is dropped by B, but B doesn't drop hugetlb_lock until 1227d1c3fb1fSNishanth Aravamudan * no more huge pages can be converted from surplus to normal 1228d1c3fb1fSNishanth Aravamudan * state (and doesn't try to convert again). Thus, we have a 1229d1c3fb1fSNishanth Aravamudan * case where a surplus huge page exists, the pool is grown, and 1230d1c3fb1fSNishanth Aravamudan * the surplus huge page still exists after, even though it 1231d1c3fb1fSNishanth Aravamudan * should just have been converted to a normal huge page. This 1232d1c3fb1fSNishanth Aravamudan * does not leak memory, though, as the hugepage will be freed 1233d1c3fb1fSNishanth Aravamudan * once it is out of use. It also does not allow the counters to 1234d1c3fb1fSNishanth Aravamudan * go out of whack in adjust_pool_surplus() as we don't modify 1235d1c3fb1fSNishanth Aravamudan * the node values until we've gotten the hugepage and only the 1236d1c3fb1fSNishanth Aravamudan * per-node value is checked there. 1237d1c3fb1fSNishanth Aravamudan */ 1238d1c3fb1fSNishanth Aravamudan spin_lock(&hugetlb_lock); 1239a5516438SAndi Kleen if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { 1240d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1241d1c3fb1fSNishanth Aravamudan return NULL; 1242d1c3fb1fSNishanth Aravamudan } else { 1243a5516438SAndi Kleen h->nr_huge_pages++; 1244a5516438SAndi Kleen h->surplus_huge_pages++; 1245d1c3fb1fSNishanth Aravamudan } 1246d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 1247d1c3fb1fSNishanth Aravamudan 1248bf50bab2SNaoya Horiguchi if (nid == NUMA_NO_NODE) 124986cdb465SNaoya Horiguchi page = alloc_pages(htlb_alloc_mask(h)|__GFP_COMP| 1250551883aeSNishanth Aravamudan __GFP_REPEAT|__GFP_NOWARN, 1251a5516438SAndi Kleen huge_page_order(h)); 1252bf50bab2SNaoya Horiguchi else 1253bf50bab2SNaoya Horiguchi page = alloc_pages_exact_node(nid, 125486cdb465SNaoya Horiguchi htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE| 1255bf50bab2SNaoya Horiguchi __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h)); 1256d1c3fb1fSNishanth Aravamudan 1257caff3a2cSGerald Schaefer if (page && arch_prepare_hugepage(page)) { 1258caff3a2cSGerald Schaefer __free_pages(page, huge_page_order(h)); 1259ea5768c7SHillf Danton page = NULL; 1260caff3a2cSGerald Schaefer } 1261caff3a2cSGerald Schaefer 12627893d1d5SAdam Litke spin_lock(&hugetlb_lock); 1263d1c3fb1fSNishanth Aravamudan if (page) { 12640edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&page->lru); 1265bf50bab2SNaoya Horiguchi r_nid = page_to_nid(page); 1266d1c3fb1fSNishanth Aravamudan set_compound_page_dtor(page, free_huge_page); 12679dd540e2SAneesh Kumar K.V set_hugetlb_cgroup(page, NULL); 1268d1c3fb1fSNishanth Aravamudan /* 1269d1c3fb1fSNishanth Aravamudan * We incremented the global counters already 1270d1c3fb1fSNishanth Aravamudan */ 1271bf50bab2SNaoya Horiguchi h->nr_huge_pages_node[r_nid]++; 1272bf50bab2SNaoya Horiguchi h->surplus_huge_pages_node[r_nid]++; 12733b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC); 1274d1c3fb1fSNishanth Aravamudan } else { 1275a5516438SAndi Kleen h->nr_huge_pages--; 1276a5516438SAndi Kleen h->surplus_huge_pages--; 12773b116300SAdam Litke __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 12787893d1d5SAdam Litke } 1279d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 12807893d1d5SAdam Litke 12817893d1d5SAdam Litke return page; 12827893d1d5SAdam Litke } 12837893d1d5SAdam Litke 1284e4e574b7SAdam Litke /* 1285bf50bab2SNaoya Horiguchi * This allocation function is useful in the context where vma is irrelevant. 1286bf50bab2SNaoya Horiguchi * E.g. soft-offlining uses this function because it only cares physical 1287bf50bab2SNaoya Horiguchi * address of error page. 1288bf50bab2SNaoya Horiguchi */ 1289bf50bab2SNaoya Horiguchi struct page *alloc_huge_page_node(struct hstate *h, int nid) 1290bf50bab2SNaoya Horiguchi { 12914ef91848SJoonsoo Kim struct page *page = NULL; 1292bf50bab2SNaoya Horiguchi 1293bf50bab2SNaoya Horiguchi spin_lock(&hugetlb_lock); 12944ef91848SJoonsoo Kim if (h->free_huge_pages - h->resv_huge_pages > 0) 1295bf50bab2SNaoya Horiguchi page = dequeue_huge_page_node(h, nid); 1296bf50bab2SNaoya Horiguchi spin_unlock(&hugetlb_lock); 1297bf50bab2SNaoya Horiguchi 129894ae8ba7SAneesh Kumar K.V if (!page) 1299bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, nid); 1300bf50bab2SNaoya Horiguchi 1301bf50bab2SNaoya Horiguchi return page; 1302bf50bab2SNaoya Horiguchi } 1303bf50bab2SNaoya Horiguchi 1304bf50bab2SNaoya Horiguchi /* 130525985edcSLucas De Marchi * Increase the hugetlb pool such that it can accommodate a reservation 1306e4e574b7SAdam Litke * of size 'delta'. 1307e4e574b7SAdam Litke */ 1308a5516438SAndi Kleen static int gather_surplus_pages(struct hstate *h, int delta) 1309e4e574b7SAdam Litke { 1310e4e574b7SAdam Litke struct list_head surplus_list; 1311e4e574b7SAdam Litke struct page *page, *tmp; 1312e4e574b7SAdam Litke int ret, i; 1313e4e574b7SAdam Litke int needed, allocated; 131428073b02SHillf Danton bool alloc_ok = true; 1315e4e574b7SAdam Litke 1316a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - h->free_huge_pages; 1317ac09b3a1SAdam Litke if (needed <= 0) { 1318a5516438SAndi Kleen h->resv_huge_pages += delta; 1319e4e574b7SAdam Litke return 0; 1320ac09b3a1SAdam Litke } 1321e4e574b7SAdam Litke 1322e4e574b7SAdam Litke allocated = 0; 1323e4e574b7SAdam Litke INIT_LIST_HEAD(&surplus_list); 1324e4e574b7SAdam Litke 1325e4e574b7SAdam Litke ret = -ENOMEM; 1326e4e574b7SAdam Litke retry: 1327e4e574b7SAdam Litke spin_unlock(&hugetlb_lock); 1328e4e574b7SAdam Litke for (i = 0; i < needed; i++) { 1329bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, NUMA_NO_NODE); 133028073b02SHillf Danton if (!page) { 133128073b02SHillf Danton alloc_ok = false; 133228073b02SHillf Danton break; 133328073b02SHillf Danton } 1334e4e574b7SAdam Litke list_add(&page->lru, &surplus_list); 1335e4e574b7SAdam Litke } 133628073b02SHillf Danton allocated += i; 1337e4e574b7SAdam Litke 1338e4e574b7SAdam Litke /* 1339e4e574b7SAdam Litke * After retaking hugetlb_lock, we need to recalculate 'needed' 1340e4e574b7SAdam Litke * because either resv_huge_pages or free_huge_pages may have changed. 1341e4e574b7SAdam Litke */ 1342e4e574b7SAdam Litke spin_lock(&hugetlb_lock); 1343a5516438SAndi Kleen needed = (h->resv_huge_pages + delta) - 1344a5516438SAndi Kleen (h->free_huge_pages + allocated); 134528073b02SHillf Danton if (needed > 0) { 134628073b02SHillf Danton if (alloc_ok) 1347e4e574b7SAdam Litke goto retry; 134828073b02SHillf Danton /* 134928073b02SHillf Danton * We were not able to allocate enough pages to 135028073b02SHillf Danton * satisfy the entire reservation so we free what 135128073b02SHillf Danton * we've allocated so far. 135228073b02SHillf Danton */ 135328073b02SHillf Danton goto free; 135428073b02SHillf Danton } 1355e4e574b7SAdam Litke /* 1356e4e574b7SAdam Litke * The surplus_list now contains _at_least_ the number of extra pages 135725985edcSLucas De Marchi * needed to accommodate the reservation. Add the appropriate number 1358e4e574b7SAdam Litke * of pages to the hugetlb pool and free the extras back to the buddy 1359ac09b3a1SAdam Litke * allocator. Commit the entire reservation here to prevent another 1360ac09b3a1SAdam Litke * process from stealing the pages as they are added to the pool but 1361ac09b3a1SAdam Litke * before they are reserved. 1362e4e574b7SAdam Litke */ 1363e4e574b7SAdam Litke needed += allocated; 1364a5516438SAndi Kleen h->resv_huge_pages += delta; 1365e4e574b7SAdam Litke ret = 0; 1366a9869b83SNaoya Horiguchi 136719fc3f0aSAdam Litke /* Free the needed pages to the hugetlb pool */ 136819fc3f0aSAdam Litke list_for_each_entry_safe(page, tmp, &surplus_list, lru) { 136919fc3f0aSAdam Litke if ((--needed) < 0) 137019fc3f0aSAdam Litke break; 1371a9869b83SNaoya Horiguchi /* 1372a9869b83SNaoya Horiguchi * This page is now managed by the hugetlb allocator and has 1373a9869b83SNaoya Horiguchi * no users -- drop the buddy allocator's reference. 1374a9869b83SNaoya Horiguchi */ 1375a9869b83SNaoya Horiguchi put_page_testzero(page); 1376309381feSSasha Levin VM_BUG_ON_PAGE(page_count(page), page); 1377a5516438SAndi Kleen enqueue_huge_page(h, page); 137819fc3f0aSAdam Litke } 137928073b02SHillf Danton free: 1380b0365c8dSHillf Danton spin_unlock(&hugetlb_lock); 138119fc3f0aSAdam Litke 138219fc3f0aSAdam Litke /* Free unnecessary surplus pages to the buddy allocator */ 1383c0d934baSJoonsoo Kim list_for_each_entry_safe(page, tmp, &surplus_list, lru) 1384a9869b83SNaoya Horiguchi put_page(page); 138519fc3f0aSAdam Litke spin_lock(&hugetlb_lock); 1386e4e574b7SAdam Litke 1387e4e574b7SAdam Litke return ret; 1388e4e574b7SAdam Litke } 1389e4e574b7SAdam Litke 1390e4e574b7SAdam Litke /* 1391e4e574b7SAdam Litke * When releasing a hugetlb pool reservation, any surplus pages that were 1392e4e574b7SAdam Litke * allocated to satisfy the reservation must be explicitly freed if they were 1393e4e574b7SAdam Litke * never used. 1394685f3457SLee Schermerhorn * Called with hugetlb_lock held. 1395e4e574b7SAdam Litke */ 1396a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h, 1397a5516438SAndi Kleen unsigned long unused_resv_pages) 1398e4e574b7SAdam Litke { 1399e4e574b7SAdam Litke unsigned long nr_pages; 1400e4e574b7SAdam Litke 1401ac09b3a1SAdam Litke /* Uncommit the reservation */ 1402a5516438SAndi Kleen h->resv_huge_pages -= unused_resv_pages; 1403ac09b3a1SAdam Litke 1404aa888a74SAndi Kleen /* Cannot return gigantic pages currently */ 1405bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1406aa888a74SAndi Kleen return; 1407aa888a74SAndi Kleen 1408a5516438SAndi Kleen nr_pages = min(unused_resv_pages, h->surplus_huge_pages); 1409e4e574b7SAdam Litke 1410685f3457SLee Schermerhorn /* 1411685f3457SLee Schermerhorn * We want to release as many surplus pages as possible, spread 14129b5e5d0fSLee Schermerhorn * evenly across all nodes with memory. Iterate across these nodes 14139b5e5d0fSLee Schermerhorn * until we can no longer free unreserved surplus pages. This occurs 14149b5e5d0fSLee Schermerhorn * when the nodes with surplus pages have no free pages. 14159b5e5d0fSLee Schermerhorn * free_pool_huge_page() will balance the the freed pages across the 14169b5e5d0fSLee Schermerhorn * on-line nodes with memory and will handle the hstate accounting. 1417685f3457SLee Schermerhorn */ 1418685f3457SLee Schermerhorn while (nr_pages--) { 14198cebfcd0SLai Jiangshan if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1)) 1420685f3457SLee Schermerhorn break; 14217848a4bfSMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 1422e4e574b7SAdam Litke } 1423e4e574b7SAdam Litke } 1424e4e574b7SAdam Litke 1425c37f9fb1SAndy Whitcroft /* 1426c37f9fb1SAndy Whitcroft * Determine if the huge page at addr within the vma has an associated 1427c37f9fb1SAndy Whitcroft * reservation. Where it does not we will need to logically increase 142890481622SDavid Gibson * reservation and actually increase subpool usage before an allocation 142990481622SDavid Gibson * can occur. Where any new reservation would be required the 143090481622SDavid Gibson * reservation change is prepared, but not committed. Once the page 143190481622SDavid Gibson * has been allocated from the subpool and instantiated the change should 143290481622SDavid Gibson * be committed via vma_commit_reservation. No action is required on 143390481622SDavid Gibson * failure. 1434c37f9fb1SAndy Whitcroft */ 1435e2f17d94SRoel Kluin static long vma_needs_reservation(struct hstate *h, 1436a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long addr) 1437c37f9fb1SAndy Whitcroft { 14384e35f483SJoonsoo Kim struct resv_map *resv; 14394e35f483SJoonsoo Kim pgoff_t idx; 14404e35f483SJoonsoo Kim long chg; 1441c37f9fb1SAndy Whitcroft 14424e35f483SJoonsoo Kim resv = vma_resv_map(vma); 14434e35f483SJoonsoo Kim if (!resv) 1444c37f9fb1SAndy Whitcroft return 1; 1445c37f9fb1SAndy Whitcroft 14464e35f483SJoonsoo Kim idx = vma_hugecache_offset(h, vma, addr); 14474e35f483SJoonsoo Kim chg = region_chg(resv, idx, idx + 1); 144884afd99bSAndy Whitcroft 14494e35f483SJoonsoo Kim if (vma->vm_flags & VM_MAYSHARE) 14504e35f483SJoonsoo Kim return chg; 14514e35f483SJoonsoo Kim else 14524e35f483SJoonsoo Kim return chg < 0 ? chg : 0; 145384afd99bSAndy Whitcroft } 1454a5516438SAndi Kleen static void vma_commit_reservation(struct hstate *h, 1455a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long addr) 1456c37f9fb1SAndy Whitcroft { 14574e35f483SJoonsoo Kim struct resv_map *resv; 14584e35f483SJoonsoo Kim pgoff_t idx; 1459c37f9fb1SAndy Whitcroft 14604e35f483SJoonsoo Kim resv = vma_resv_map(vma); 14614e35f483SJoonsoo Kim if (!resv) 14624e35f483SJoonsoo Kim return; 14639119a41eSJoonsoo Kim 14644e35f483SJoonsoo Kim idx = vma_hugecache_offset(h, vma, addr); 14651406ec9bSJoonsoo Kim region_add(resv, idx, idx + 1); 1466c37f9fb1SAndy Whitcroft } 1467c37f9fb1SAndy Whitcroft 1468348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma, 146904f2cbe3SMel Gorman unsigned long addr, int avoid_reserve) 1470348ea204SAdam Litke { 147190481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 1472a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 1473348ea204SAdam Litke struct page *page; 1474e2f17d94SRoel Kluin long chg; 14756d76dcf4SAneesh Kumar K.V int ret, idx; 14766d76dcf4SAneesh Kumar K.V struct hugetlb_cgroup *h_cg; 14772fc39cecSAdam Litke 14786d76dcf4SAneesh Kumar K.V idx = hstate_index(h); 1479a1e78772SMel Gorman /* 148090481622SDavid Gibson * Processes that did not create the mapping will have no 148190481622SDavid Gibson * reserves and will not have accounted against subpool 148290481622SDavid Gibson * limit. Check that the subpool limit can be made before 148390481622SDavid Gibson * satisfying the allocation MAP_NORESERVE mappings may also 148490481622SDavid Gibson * need pages and subpool limit allocated allocated if no reserve 148590481622SDavid Gibson * mapping overlaps. 1486a1e78772SMel Gorman */ 1487a5516438SAndi Kleen chg = vma_needs_reservation(h, vma, addr); 1488c37f9fb1SAndy Whitcroft if (chg < 0) 148976dcee75SAneesh Kumar K.V return ERR_PTR(-ENOMEM); 14908bb3f12eSJoonsoo Kim if (chg || avoid_reserve) 14911c5ecae3SMike Kravetz if (hugepage_subpool_get_pages(spool, 1) < 0) 149276dcee75SAneesh Kumar K.V return ERR_PTR(-ENOSPC); 149390d8b7e6SAdam Litke 14946d76dcf4SAneesh Kumar K.V ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); 14958f34af6fSJianyu Zhan if (ret) 14968f34af6fSJianyu Zhan goto out_subpool_put; 14978f34af6fSJianyu Zhan 1498a1e78772SMel Gorman spin_lock(&hugetlb_lock); 1499af0ed73eSJoonsoo Kim page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg); 150081a6fcaeSJoonsoo Kim if (!page) { 150194ae8ba7SAneesh Kumar K.V spin_unlock(&hugetlb_lock); 1502bf50bab2SNaoya Horiguchi page = alloc_buddy_huge_page(h, NUMA_NO_NODE); 15038f34af6fSJianyu Zhan if (!page) 15048f34af6fSJianyu Zhan goto out_uncharge_cgroup; 15058f34af6fSJianyu Zhan 150679dbb236SAneesh Kumar K.V spin_lock(&hugetlb_lock); 150779dbb236SAneesh Kumar K.V list_move(&page->lru, &h->hugepage_activelist); 150881a6fcaeSJoonsoo Kim /* Fall through */ 1509a1e78772SMel Gorman } 151081a6fcaeSJoonsoo Kim hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page); 151181a6fcaeSJoonsoo Kim spin_unlock(&hugetlb_lock); 1512a1e78772SMel Gorman 151390481622SDavid Gibson set_page_private(page, (unsigned long)spool); 1514a1e78772SMel Gorman 1515a5516438SAndi Kleen vma_commit_reservation(h, vma, addr); 15167893d1d5SAdam Litke return page; 15178f34af6fSJianyu Zhan 15188f34af6fSJianyu Zhan out_uncharge_cgroup: 15198f34af6fSJianyu Zhan hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); 15208f34af6fSJianyu Zhan out_subpool_put: 15218f34af6fSJianyu Zhan if (chg || avoid_reserve) 15228f34af6fSJianyu Zhan hugepage_subpool_put_pages(spool, 1); 15238f34af6fSJianyu Zhan return ERR_PTR(-ENOSPC); 1524b45b5bd6SDavid Gibson } 1525b45b5bd6SDavid Gibson 152674060e4dSNaoya Horiguchi /* 152774060e4dSNaoya Horiguchi * alloc_huge_page()'s wrapper which simply returns the page if allocation 152874060e4dSNaoya Horiguchi * succeeds, otherwise NULL. This function is called from new_vma_page(), 152974060e4dSNaoya Horiguchi * where no ERR_VALUE is expected to be returned. 153074060e4dSNaoya Horiguchi */ 153174060e4dSNaoya Horiguchi struct page *alloc_huge_page_noerr(struct vm_area_struct *vma, 153274060e4dSNaoya Horiguchi unsigned long addr, int avoid_reserve) 153374060e4dSNaoya Horiguchi { 153474060e4dSNaoya Horiguchi struct page *page = alloc_huge_page(vma, addr, avoid_reserve); 153574060e4dSNaoya Horiguchi if (IS_ERR(page)) 153674060e4dSNaoya Horiguchi page = NULL; 153774060e4dSNaoya Horiguchi return page; 153874060e4dSNaoya Horiguchi } 153974060e4dSNaoya Horiguchi 154091f47662SCyrill Gorcunov int __weak alloc_bootmem_huge_page(struct hstate *h) 1541aa888a74SAndi Kleen { 1542aa888a74SAndi Kleen struct huge_bootmem_page *m; 1543b2261026SJoonsoo Kim int nr_nodes, node; 1544aa888a74SAndi Kleen 1545b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) { 1546aa888a74SAndi Kleen void *addr; 1547aa888a74SAndi Kleen 15488b89a116SGrygorii Strashko addr = memblock_virt_alloc_try_nid_nopanic( 15498b89a116SGrygorii Strashko huge_page_size(h), huge_page_size(h), 15508b89a116SGrygorii Strashko 0, BOOTMEM_ALLOC_ACCESSIBLE, node); 1551aa888a74SAndi Kleen if (addr) { 1552aa888a74SAndi Kleen /* 1553aa888a74SAndi Kleen * Use the beginning of the huge page to store the 1554aa888a74SAndi Kleen * huge_bootmem_page struct (until gather_bootmem 1555aa888a74SAndi Kleen * puts them into the mem_map). 1556aa888a74SAndi Kleen */ 1557aa888a74SAndi Kleen m = addr; 1558aa888a74SAndi Kleen goto found; 1559aa888a74SAndi Kleen } 1560aa888a74SAndi Kleen } 1561aa888a74SAndi Kleen return 0; 1562aa888a74SAndi Kleen 1563aa888a74SAndi Kleen found: 1564df994eadSLuiz Capitulino BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h))); 1565aa888a74SAndi Kleen /* Put them into a private list first because mem_map is not up yet */ 1566aa888a74SAndi Kleen list_add(&m->list, &huge_boot_pages); 1567aa888a74SAndi Kleen m->hstate = h; 1568aa888a74SAndi Kleen return 1; 1569aa888a74SAndi Kleen } 1570aa888a74SAndi Kleen 1571f412c97aSDavid Rientjes static void __init prep_compound_huge_page(struct page *page, int order) 157218229df5SAndy Whitcroft { 157318229df5SAndy Whitcroft if (unlikely(order > (MAX_ORDER - 1))) 157418229df5SAndy Whitcroft prep_compound_gigantic_page(page, order); 157518229df5SAndy Whitcroft else 157618229df5SAndy Whitcroft prep_compound_page(page, order); 157718229df5SAndy Whitcroft } 157818229df5SAndy Whitcroft 1579aa888a74SAndi Kleen /* Put bootmem huge pages into the standard lists after mem_map is up */ 1580aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void) 1581aa888a74SAndi Kleen { 1582aa888a74SAndi Kleen struct huge_bootmem_page *m; 1583aa888a74SAndi Kleen 1584aa888a74SAndi Kleen list_for_each_entry(m, &huge_boot_pages, list) { 1585aa888a74SAndi Kleen struct hstate *h = m->hstate; 1586ee8f248dSBecky Bruce struct page *page; 1587ee8f248dSBecky Bruce 1588ee8f248dSBecky Bruce #ifdef CONFIG_HIGHMEM 1589ee8f248dSBecky Bruce page = pfn_to_page(m->phys >> PAGE_SHIFT); 15908b89a116SGrygorii Strashko memblock_free_late(__pa(m), 1591ee8f248dSBecky Bruce sizeof(struct huge_bootmem_page)); 1592ee8f248dSBecky Bruce #else 1593ee8f248dSBecky Bruce page = virt_to_page(m); 1594ee8f248dSBecky Bruce #endif 1595aa888a74SAndi Kleen WARN_ON(page_count(page) != 1); 159618229df5SAndy Whitcroft prep_compound_huge_page(page, h->order); 1597ef5a22beSAndrea Arcangeli WARN_ON(PageReserved(page)); 1598aa888a74SAndi Kleen prep_new_huge_page(h, page, page_to_nid(page)); 1599b0320c7bSRafael Aquini /* 1600b0320c7bSRafael Aquini * If we had gigantic hugepages allocated at boot time, we need 1601b0320c7bSRafael Aquini * to restore the 'stolen' pages to totalram_pages in order to 1602b0320c7bSRafael Aquini * fix confusing memory reports from free(1) and another 1603b0320c7bSRafael Aquini * side-effects, like CommitLimit going negative. 1604b0320c7bSRafael Aquini */ 1605bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 16063dcc0571SJiang Liu adjust_managed_page_count(page, 1 << h->order); 1607aa888a74SAndi Kleen } 1608aa888a74SAndi Kleen } 1609aa888a74SAndi Kleen 16108faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h) 16111da177e4SLinus Torvalds { 16121da177e4SLinus Torvalds unsigned long i; 16131da177e4SLinus Torvalds 1614e5ff2159SAndi Kleen for (i = 0; i < h->max_huge_pages; ++i) { 1615bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) { 1616aa888a74SAndi Kleen if (!alloc_bootmem_huge_page(h)) 1617aa888a74SAndi Kleen break; 16189b5e5d0fSLee Schermerhorn } else if (!alloc_fresh_huge_page(h, 16198cebfcd0SLai Jiangshan &node_states[N_MEMORY])) 16201da177e4SLinus Torvalds break; 16211da177e4SLinus Torvalds } 16228faa8b07SAndi Kleen h->max_huge_pages = i; 1623e5ff2159SAndi Kleen } 1624e5ff2159SAndi Kleen 1625e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void) 1626e5ff2159SAndi Kleen { 1627e5ff2159SAndi Kleen struct hstate *h; 1628e5ff2159SAndi Kleen 1629e5ff2159SAndi Kleen for_each_hstate(h) { 16308faa8b07SAndi Kleen /* oversize hugepages were init'ed in early boot */ 1631bae7f4aeSLuiz Capitulino if (!hstate_is_gigantic(h)) 16328faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(h); 1633e5ff2159SAndi Kleen } 1634e5ff2159SAndi Kleen } 1635e5ff2159SAndi Kleen 16364abd32dbSAndi Kleen static char * __init memfmt(char *buf, unsigned long n) 16374abd32dbSAndi Kleen { 16384abd32dbSAndi Kleen if (n >= (1UL << 30)) 16394abd32dbSAndi Kleen sprintf(buf, "%lu GB", n >> 30); 16404abd32dbSAndi Kleen else if (n >= (1UL << 20)) 16414abd32dbSAndi Kleen sprintf(buf, "%lu MB", n >> 20); 16424abd32dbSAndi Kleen else 16434abd32dbSAndi Kleen sprintf(buf, "%lu KB", n >> 10); 16444abd32dbSAndi Kleen return buf; 16454abd32dbSAndi Kleen } 16464abd32dbSAndi Kleen 1647e5ff2159SAndi Kleen static void __init report_hugepages(void) 1648e5ff2159SAndi Kleen { 1649e5ff2159SAndi Kleen struct hstate *h; 1650e5ff2159SAndi Kleen 1651e5ff2159SAndi Kleen for_each_hstate(h) { 16524abd32dbSAndi Kleen char buf[32]; 1653ffb22af5SAndrew Morton pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n", 16544abd32dbSAndi Kleen memfmt(buf, huge_page_size(h)), 16554abd32dbSAndi Kleen h->free_huge_pages); 1656e5ff2159SAndi Kleen } 1657e5ff2159SAndi Kleen } 1658e5ff2159SAndi Kleen 16591da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM 16606ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count, 16616ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 16621da177e4SLinus Torvalds { 16634415cc8dSChristoph Lameter int i; 16644415cc8dSChristoph Lameter 1665bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1666aa888a74SAndi Kleen return; 1667aa888a74SAndi Kleen 16686ae11b27SLee Schermerhorn for_each_node_mask(i, *nodes_allowed) { 16691da177e4SLinus Torvalds struct page *page, *next; 1670a5516438SAndi Kleen struct list_head *freel = &h->hugepage_freelists[i]; 1671a5516438SAndi Kleen list_for_each_entry_safe(page, next, freel, lru) { 1672a5516438SAndi Kleen if (count >= h->nr_huge_pages) 16736b0c880dSAdam Litke return; 16741da177e4SLinus Torvalds if (PageHighMem(page)) 16751da177e4SLinus Torvalds continue; 16761da177e4SLinus Torvalds list_del(&page->lru); 1677e5ff2159SAndi Kleen update_and_free_page(h, page); 1678a5516438SAndi Kleen h->free_huge_pages--; 1679a5516438SAndi Kleen h->free_huge_pages_node[page_to_nid(page)]--; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds #else 16846ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count, 16856ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 16861da177e4SLinus Torvalds { 16871da177e4SLinus Torvalds } 16881da177e4SLinus Torvalds #endif 16891da177e4SLinus Torvalds 169020a0307cSWu Fengguang /* 169120a0307cSWu Fengguang * Increment or decrement surplus_huge_pages. Keep node-specific counters 169220a0307cSWu Fengguang * balanced by operating on them in a round-robin fashion. 169320a0307cSWu Fengguang * Returns 1 if an adjustment was made. 169420a0307cSWu Fengguang */ 16956ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, 16966ae11b27SLee Schermerhorn int delta) 169720a0307cSWu Fengguang { 1698b2261026SJoonsoo Kim int nr_nodes, node; 169920a0307cSWu Fengguang 170020a0307cSWu Fengguang VM_BUG_ON(delta != -1 && delta != 1); 170120a0307cSWu Fengguang 1702e8c5c824SLee Schermerhorn if (delta < 0) { 1703b2261026SJoonsoo Kim for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { 1704b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node]) 1705b2261026SJoonsoo Kim goto found; 1706b2261026SJoonsoo Kim } 1707b2261026SJoonsoo Kim } else { 1708b2261026SJoonsoo Kim for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1709b2261026SJoonsoo Kim if (h->surplus_huge_pages_node[node] < 1710b2261026SJoonsoo Kim h->nr_huge_pages_node[node]) 1711b2261026SJoonsoo Kim goto found; 1712e8c5c824SLee Schermerhorn } 17139a76db09SLee Schermerhorn } 1714b2261026SJoonsoo Kim return 0; 171520a0307cSWu Fengguang 1716b2261026SJoonsoo Kim found: 171720a0307cSWu Fengguang h->surplus_huge_pages += delta; 1718b2261026SJoonsoo Kim h->surplus_huge_pages_node[node] += delta; 1719b2261026SJoonsoo Kim return 1; 172020a0307cSWu Fengguang } 172120a0307cSWu Fengguang 1722a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) 17236ae11b27SLee Schermerhorn static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count, 17246ae11b27SLee Schermerhorn nodemask_t *nodes_allowed) 17251da177e4SLinus Torvalds { 17267893d1d5SAdam Litke unsigned long min_count, ret; 17271da177e4SLinus Torvalds 1728944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) 1729aa888a74SAndi Kleen return h->max_huge_pages; 1730aa888a74SAndi Kleen 17317893d1d5SAdam Litke /* 17327893d1d5SAdam Litke * Increase the pool size 17337893d1d5SAdam Litke * First take pages out of surplus state. Then make up the 17347893d1d5SAdam Litke * remaining difference by allocating fresh huge pages. 1735d1c3fb1fSNishanth Aravamudan * 1736d1c3fb1fSNishanth Aravamudan * We might race with alloc_buddy_huge_page() here and be unable 1737d1c3fb1fSNishanth Aravamudan * to convert a surplus huge page to a normal huge page. That is 1738d1c3fb1fSNishanth Aravamudan * not critical, though, it just means the overall size of the 1739d1c3fb1fSNishanth Aravamudan * pool might be one hugepage larger than it needs to be, but 1740d1c3fb1fSNishanth Aravamudan * within all the constraints specified by the sysctls. 17417893d1d5SAdam Litke */ 17421da177e4SLinus Torvalds spin_lock(&hugetlb_lock); 1743a5516438SAndi Kleen while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { 17446ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, -1)) 17457893d1d5SAdam Litke break; 17467893d1d5SAdam Litke } 17477893d1d5SAdam Litke 1748a5516438SAndi Kleen while (count > persistent_huge_pages(h)) { 17497893d1d5SAdam Litke /* 17507893d1d5SAdam Litke * If this allocation races such that we no longer need the 17517893d1d5SAdam Litke * page, free_huge_page will handle it by freeing the page 17527893d1d5SAdam Litke * and reducing the surplus. 17537893d1d5SAdam Litke */ 17547893d1d5SAdam Litke spin_unlock(&hugetlb_lock); 1755944d9fecSLuiz Capitulino if (hstate_is_gigantic(h)) 1756944d9fecSLuiz Capitulino ret = alloc_fresh_gigantic_page(h, nodes_allowed); 1757944d9fecSLuiz Capitulino else 17586ae11b27SLee Schermerhorn ret = alloc_fresh_huge_page(h, nodes_allowed); 17597893d1d5SAdam Litke spin_lock(&hugetlb_lock); 17607893d1d5SAdam Litke if (!ret) 17617893d1d5SAdam Litke goto out; 17627893d1d5SAdam Litke 1763536240f2SMel Gorman /* Bail for signals. Probably ctrl-c from user */ 1764536240f2SMel Gorman if (signal_pending(current)) 1765536240f2SMel Gorman goto out; 17667893d1d5SAdam Litke } 17677893d1d5SAdam Litke 17687893d1d5SAdam Litke /* 17697893d1d5SAdam Litke * Decrease the pool size 17707893d1d5SAdam Litke * First return free pages to the buddy allocator (being careful 17717893d1d5SAdam Litke * to keep enough around to satisfy reservations). Then place 17727893d1d5SAdam Litke * pages into surplus state as needed so the pool will shrink 17737893d1d5SAdam Litke * to the desired size as pages become free. 1774d1c3fb1fSNishanth Aravamudan * 1775d1c3fb1fSNishanth Aravamudan * By placing pages into the surplus state independent of the 1776d1c3fb1fSNishanth Aravamudan * overcommit value, we are allowing the surplus pool size to 1777d1c3fb1fSNishanth Aravamudan * exceed overcommit. There are few sane options here. Since 1778d1c3fb1fSNishanth Aravamudan * alloc_buddy_huge_page() is checking the global counter, 1779d1c3fb1fSNishanth Aravamudan * though, we'll note that we're not allowed to exceed surplus 1780d1c3fb1fSNishanth Aravamudan * and won't grow the pool anywhere else. Not until one of the 1781d1c3fb1fSNishanth Aravamudan * sysctls are changed, or the surplus pages go out of use. 17827893d1d5SAdam Litke */ 1783a5516438SAndi Kleen min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; 17846b0c880dSAdam Litke min_count = max(count, min_count); 17856ae11b27SLee Schermerhorn try_to_free_low(h, min_count, nodes_allowed); 1786a5516438SAndi Kleen while (min_count < persistent_huge_pages(h)) { 17876ae11b27SLee Schermerhorn if (!free_pool_huge_page(h, nodes_allowed, 0)) 17881da177e4SLinus Torvalds break; 178955f67141SMizuma, Masayoshi cond_resched_lock(&hugetlb_lock); 17901da177e4SLinus Torvalds } 1791a5516438SAndi Kleen while (count < persistent_huge_pages(h)) { 17926ae11b27SLee Schermerhorn if (!adjust_pool_surplus(h, nodes_allowed, 1)) 17937893d1d5SAdam Litke break; 17947893d1d5SAdam Litke } 17957893d1d5SAdam Litke out: 1796a5516438SAndi Kleen ret = persistent_huge_pages(h); 17971da177e4SLinus Torvalds spin_unlock(&hugetlb_lock); 17987893d1d5SAdam Litke return ret; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 1801a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \ 1802a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 1803a3437870SNishanth Aravamudan 1804a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \ 1805a3437870SNishanth Aravamudan static struct kobj_attribute _name##_attr = \ 1806a3437870SNishanth Aravamudan __ATTR(_name, 0644, _name##_show, _name##_store) 1807a3437870SNishanth Aravamudan 1808a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj; 1809a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 1810a3437870SNishanth Aravamudan 18119a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp); 18129a305230SLee Schermerhorn 18139a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp) 1814a3437870SNishanth Aravamudan { 1815a3437870SNishanth Aravamudan int i; 18169a305230SLee Schermerhorn 1817a3437870SNishanth Aravamudan for (i = 0; i < HUGE_MAX_HSTATE; i++) 18189a305230SLee Schermerhorn if (hstate_kobjs[i] == kobj) { 18199a305230SLee Schermerhorn if (nidp) 18209a305230SLee Schermerhorn *nidp = NUMA_NO_NODE; 1821a3437870SNishanth Aravamudan return &hstates[i]; 18229a305230SLee Schermerhorn } 18239a305230SLee Schermerhorn 18249a305230SLee Schermerhorn return kobj_to_node_hstate(kobj, nidp); 1825a3437870SNishanth Aravamudan } 1826a3437870SNishanth Aravamudan 182706808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj, 1828a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 1829a3437870SNishanth Aravamudan { 18309a305230SLee Schermerhorn struct hstate *h; 18319a305230SLee Schermerhorn unsigned long nr_huge_pages; 18329a305230SLee Schermerhorn int nid; 18339a305230SLee Schermerhorn 18349a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 18359a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 18369a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages; 18379a305230SLee Schermerhorn else 18389a305230SLee Schermerhorn nr_huge_pages = h->nr_huge_pages_node[nid]; 18399a305230SLee Schermerhorn 18409a305230SLee Schermerhorn return sprintf(buf, "%lu\n", nr_huge_pages); 1841a3437870SNishanth Aravamudan } 1842adbe8726SEric B Munson 1843238d3c13SDavid Rientjes static ssize_t __nr_hugepages_store_common(bool obey_mempolicy, 1844238d3c13SDavid Rientjes struct hstate *h, int nid, 1845238d3c13SDavid Rientjes unsigned long count, size_t len) 1846a3437870SNishanth Aravamudan { 1847a3437870SNishanth Aravamudan int err; 1848bad44b5bSDavid Rientjes NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY); 1849a3437870SNishanth Aravamudan 1850944d9fecSLuiz Capitulino if (hstate_is_gigantic(h) && !gigantic_page_supported()) { 1851adbe8726SEric B Munson err = -EINVAL; 1852adbe8726SEric B Munson goto out; 1853adbe8726SEric B Munson } 1854adbe8726SEric B Munson 18559a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) { 18569a305230SLee Schermerhorn /* 18579a305230SLee Schermerhorn * global hstate attribute 18589a305230SLee Schermerhorn */ 18599a305230SLee Schermerhorn if (!(obey_mempolicy && 18609a305230SLee Schermerhorn init_nodemask_of_mempolicy(nodes_allowed))) { 186106808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 18628cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 186306808b08SLee Schermerhorn } 18649a305230SLee Schermerhorn } else if (nodes_allowed) { 18659a305230SLee Schermerhorn /* 18669a305230SLee Schermerhorn * per node hstate attribute: adjust count to global, 18679a305230SLee Schermerhorn * but restrict alloc/free to the specified node. 18689a305230SLee Schermerhorn */ 18699a305230SLee Schermerhorn count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; 18709a305230SLee Schermerhorn init_nodemask_of_node(nodes_allowed, nid); 18719a305230SLee Schermerhorn } else 18728cebfcd0SLai Jiangshan nodes_allowed = &node_states[N_MEMORY]; 18739a305230SLee Schermerhorn 187406808b08SLee Schermerhorn h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed); 1875a3437870SNishanth Aravamudan 18768cebfcd0SLai Jiangshan if (nodes_allowed != &node_states[N_MEMORY]) 187706808b08SLee Schermerhorn NODEMASK_FREE(nodes_allowed); 187806808b08SLee Schermerhorn 187906808b08SLee Schermerhorn return len; 1880adbe8726SEric B Munson out: 1881adbe8726SEric B Munson NODEMASK_FREE(nodes_allowed); 1882adbe8726SEric B Munson return err; 188306808b08SLee Schermerhorn } 188406808b08SLee Schermerhorn 1885238d3c13SDavid Rientjes static ssize_t nr_hugepages_store_common(bool obey_mempolicy, 1886238d3c13SDavid Rientjes struct kobject *kobj, const char *buf, 1887238d3c13SDavid Rientjes size_t len) 1888238d3c13SDavid Rientjes { 1889238d3c13SDavid Rientjes struct hstate *h; 1890238d3c13SDavid Rientjes unsigned long count; 1891238d3c13SDavid Rientjes int nid; 1892238d3c13SDavid Rientjes int err; 1893238d3c13SDavid Rientjes 1894238d3c13SDavid Rientjes err = kstrtoul(buf, 10, &count); 1895238d3c13SDavid Rientjes if (err) 1896238d3c13SDavid Rientjes return err; 1897238d3c13SDavid Rientjes 1898238d3c13SDavid Rientjes h = kobj_to_hstate(kobj, &nid); 1899238d3c13SDavid Rientjes return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len); 1900238d3c13SDavid Rientjes } 1901238d3c13SDavid Rientjes 190206808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj, 190306808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 190406808b08SLee Schermerhorn { 190506808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 190606808b08SLee Schermerhorn } 190706808b08SLee Schermerhorn 190806808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj, 190906808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 191006808b08SLee Schermerhorn { 1911238d3c13SDavid Rientjes return nr_hugepages_store_common(false, kobj, buf, len); 1912a3437870SNishanth Aravamudan } 1913a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages); 1914a3437870SNishanth Aravamudan 191506808b08SLee Schermerhorn #ifdef CONFIG_NUMA 191606808b08SLee Schermerhorn 191706808b08SLee Schermerhorn /* 191806808b08SLee Schermerhorn * hstate attribute for optionally mempolicy-based constraint on persistent 191906808b08SLee Schermerhorn * huge page alloc/free. 192006808b08SLee Schermerhorn */ 192106808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj, 192206808b08SLee Schermerhorn struct kobj_attribute *attr, char *buf) 192306808b08SLee Schermerhorn { 192406808b08SLee Schermerhorn return nr_hugepages_show_common(kobj, attr, buf); 192506808b08SLee Schermerhorn } 192606808b08SLee Schermerhorn 192706808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj, 192806808b08SLee Schermerhorn struct kobj_attribute *attr, const char *buf, size_t len) 192906808b08SLee Schermerhorn { 1930238d3c13SDavid Rientjes return nr_hugepages_store_common(true, kobj, buf, len); 193106808b08SLee Schermerhorn } 193206808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy); 193306808b08SLee Schermerhorn #endif 193406808b08SLee Schermerhorn 193506808b08SLee Schermerhorn 1936a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, 1937a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 1938a3437870SNishanth Aravamudan { 19399a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 1940a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages); 1941a3437870SNishanth Aravamudan } 1942adbe8726SEric B Munson 1943a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, 1944a3437870SNishanth Aravamudan struct kobj_attribute *attr, const char *buf, size_t count) 1945a3437870SNishanth Aravamudan { 1946a3437870SNishanth Aravamudan int err; 1947a3437870SNishanth Aravamudan unsigned long input; 19489a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 1949a3437870SNishanth Aravamudan 1950bae7f4aeSLuiz Capitulino if (hstate_is_gigantic(h)) 1951adbe8726SEric B Munson return -EINVAL; 1952adbe8726SEric B Munson 19533dbb95f7SJingoo Han err = kstrtoul(buf, 10, &input); 1954a3437870SNishanth Aravamudan if (err) 195573ae31e5SEric B Munson return err; 1956a3437870SNishanth Aravamudan 1957a3437870SNishanth Aravamudan spin_lock(&hugetlb_lock); 1958a3437870SNishanth Aravamudan h->nr_overcommit_huge_pages = input; 1959a3437870SNishanth Aravamudan spin_unlock(&hugetlb_lock); 1960a3437870SNishanth Aravamudan 1961a3437870SNishanth Aravamudan return count; 1962a3437870SNishanth Aravamudan } 1963a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages); 1964a3437870SNishanth Aravamudan 1965a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj, 1966a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 1967a3437870SNishanth Aravamudan { 19689a305230SLee Schermerhorn struct hstate *h; 19699a305230SLee Schermerhorn unsigned long free_huge_pages; 19709a305230SLee Schermerhorn int nid; 19719a305230SLee Schermerhorn 19729a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 19739a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 19749a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages; 19759a305230SLee Schermerhorn else 19769a305230SLee Schermerhorn free_huge_pages = h->free_huge_pages_node[nid]; 19779a305230SLee Schermerhorn 19789a305230SLee Schermerhorn return sprintf(buf, "%lu\n", free_huge_pages); 1979a3437870SNishanth Aravamudan } 1980a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages); 1981a3437870SNishanth Aravamudan 1982a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj, 1983a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 1984a3437870SNishanth Aravamudan { 19859a305230SLee Schermerhorn struct hstate *h = kobj_to_hstate(kobj, NULL); 1986a3437870SNishanth Aravamudan return sprintf(buf, "%lu\n", h->resv_huge_pages); 1987a3437870SNishanth Aravamudan } 1988a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages); 1989a3437870SNishanth Aravamudan 1990a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj, 1991a3437870SNishanth Aravamudan struct kobj_attribute *attr, char *buf) 1992a3437870SNishanth Aravamudan { 19939a305230SLee Schermerhorn struct hstate *h; 19949a305230SLee Schermerhorn unsigned long surplus_huge_pages; 19959a305230SLee Schermerhorn int nid; 19969a305230SLee Schermerhorn 19979a305230SLee Schermerhorn h = kobj_to_hstate(kobj, &nid); 19989a305230SLee Schermerhorn if (nid == NUMA_NO_NODE) 19999a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages; 20009a305230SLee Schermerhorn else 20019a305230SLee Schermerhorn surplus_huge_pages = h->surplus_huge_pages_node[nid]; 20029a305230SLee Schermerhorn 20039a305230SLee Schermerhorn return sprintf(buf, "%lu\n", surplus_huge_pages); 2004a3437870SNishanth Aravamudan } 2005a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages); 2006a3437870SNishanth Aravamudan 2007a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = { 2008a3437870SNishanth Aravamudan &nr_hugepages_attr.attr, 2009a3437870SNishanth Aravamudan &nr_overcommit_hugepages_attr.attr, 2010a3437870SNishanth Aravamudan &free_hugepages_attr.attr, 2011a3437870SNishanth Aravamudan &resv_hugepages_attr.attr, 2012a3437870SNishanth Aravamudan &surplus_hugepages_attr.attr, 201306808b08SLee Schermerhorn #ifdef CONFIG_NUMA 201406808b08SLee Schermerhorn &nr_hugepages_mempolicy_attr.attr, 201506808b08SLee Schermerhorn #endif 2016a3437870SNishanth Aravamudan NULL, 2017a3437870SNishanth Aravamudan }; 2018a3437870SNishanth Aravamudan 2019a3437870SNishanth Aravamudan static struct attribute_group hstate_attr_group = { 2020a3437870SNishanth Aravamudan .attrs = hstate_attrs, 2021a3437870SNishanth Aravamudan }; 2022a3437870SNishanth Aravamudan 2023094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, 20249a305230SLee Schermerhorn struct kobject **hstate_kobjs, 20259a305230SLee Schermerhorn struct attribute_group *hstate_attr_group) 2026a3437870SNishanth Aravamudan { 2027a3437870SNishanth Aravamudan int retval; 2028972dc4deSAneesh Kumar K.V int hi = hstate_index(h); 2029a3437870SNishanth Aravamudan 20309a305230SLee Schermerhorn hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); 20319a305230SLee Schermerhorn if (!hstate_kobjs[hi]) 2032a3437870SNishanth Aravamudan return -ENOMEM; 2033a3437870SNishanth Aravamudan 20349a305230SLee Schermerhorn retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group); 2035a3437870SNishanth Aravamudan if (retval) 20369a305230SLee Schermerhorn kobject_put(hstate_kobjs[hi]); 2037a3437870SNishanth Aravamudan 2038a3437870SNishanth Aravamudan return retval; 2039a3437870SNishanth Aravamudan } 2040a3437870SNishanth Aravamudan 2041a3437870SNishanth Aravamudan static void __init hugetlb_sysfs_init(void) 2042a3437870SNishanth Aravamudan { 2043a3437870SNishanth Aravamudan struct hstate *h; 2044a3437870SNishanth Aravamudan int err; 2045a3437870SNishanth Aravamudan 2046a3437870SNishanth Aravamudan hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); 2047a3437870SNishanth Aravamudan if (!hugepages_kobj) 2048a3437870SNishanth Aravamudan return; 2049a3437870SNishanth Aravamudan 2050a3437870SNishanth Aravamudan for_each_hstate(h) { 20519a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, 20529a305230SLee Schermerhorn hstate_kobjs, &hstate_attr_group); 2053a3437870SNishanth Aravamudan if (err) 2054ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s", h->name); 2055a3437870SNishanth Aravamudan } 2056a3437870SNishanth Aravamudan } 2057a3437870SNishanth Aravamudan 20589a305230SLee Schermerhorn #ifdef CONFIG_NUMA 20599a305230SLee Schermerhorn 20609a305230SLee Schermerhorn /* 20619a305230SLee Schermerhorn * node_hstate/s - associate per node hstate attributes, via their kobjects, 206210fbcf4cSKay Sievers * with node devices in node_devices[] using a parallel array. The array 206310fbcf4cSKay Sievers * index of a node device or _hstate == node id. 206410fbcf4cSKay Sievers * This is here to avoid any static dependency of the node device driver, in 20659a305230SLee Schermerhorn * the base kernel, on the hugetlb module. 20669a305230SLee Schermerhorn */ 20679a305230SLee Schermerhorn struct node_hstate { 20689a305230SLee Schermerhorn struct kobject *hugepages_kobj; 20699a305230SLee Schermerhorn struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; 20709a305230SLee Schermerhorn }; 20719a305230SLee Schermerhorn struct node_hstate node_hstates[MAX_NUMNODES]; 20729a305230SLee Schermerhorn 20739a305230SLee Schermerhorn /* 207410fbcf4cSKay Sievers * A subset of global hstate attributes for node devices 20759a305230SLee Schermerhorn */ 20769a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = { 20779a305230SLee Schermerhorn &nr_hugepages_attr.attr, 20789a305230SLee Schermerhorn &free_hugepages_attr.attr, 20799a305230SLee Schermerhorn &surplus_hugepages_attr.attr, 20809a305230SLee Schermerhorn NULL, 20819a305230SLee Schermerhorn }; 20829a305230SLee Schermerhorn 20839a305230SLee Schermerhorn static struct attribute_group per_node_hstate_attr_group = { 20849a305230SLee Schermerhorn .attrs = per_node_hstate_attrs, 20859a305230SLee Schermerhorn }; 20869a305230SLee Schermerhorn 20879a305230SLee Schermerhorn /* 208810fbcf4cSKay Sievers * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj. 20899a305230SLee Schermerhorn * Returns node id via non-NULL nidp. 20909a305230SLee Schermerhorn */ 20919a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 20929a305230SLee Schermerhorn { 20939a305230SLee Schermerhorn int nid; 20949a305230SLee Schermerhorn 20959a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) { 20969a305230SLee Schermerhorn struct node_hstate *nhs = &node_hstates[nid]; 20979a305230SLee Schermerhorn int i; 20989a305230SLee Schermerhorn for (i = 0; i < HUGE_MAX_HSTATE; i++) 20999a305230SLee Schermerhorn if (nhs->hstate_kobjs[i] == kobj) { 21009a305230SLee Schermerhorn if (nidp) 21019a305230SLee Schermerhorn *nidp = nid; 21029a305230SLee Schermerhorn return &hstates[i]; 21039a305230SLee Schermerhorn } 21049a305230SLee Schermerhorn } 21059a305230SLee Schermerhorn 21069a305230SLee Schermerhorn BUG(); 21079a305230SLee Schermerhorn return NULL; 21089a305230SLee Schermerhorn } 21099a305230SLee Schermerhorn 21109a305230SLee Schermerhorn /* 211110fbcf4cSKay Sievers * Unregister hstate attributes from a single node device. 21129a305230SLee Schermerhorn * No-op if no hstate attributes attached. 21139a305230SLee Schermerhorn */ 21143cd8b44fSClaudiu Ghioc static void hugetlb_unregister_node(struct node *node) 21159a305230SLee Schermerhorn { 21169a305230SLee Schermerhorn struct hstate *h; 211710fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 21189a305230SLee Schermerhorn 21199a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 21209b5e5d0fSLee Schermerhorn return; /* no hstate attributes */ 21219a305230SLee Schermerhorn 2122972dc4deSAneesh Kumar K.V for_each_hstate(h) { 2123972dc4deSAneesh Kumar K.V int idx = hstate_index(h); 2124972dc4deSAneesh Kumar K.V if (nhs->hstate_kobjs[idx]) { 2125972dc4deSAneesh Kumar K.V kobject_put(nhs->hstate_kobjs[idx]); 2126972dc4deSAneesh Kumar K.V nhs->hstate_kobjs[idx] = NULL; 2127972dc4deSAneesh Kumar K.V } 21289a305230SLee Schermerhorn } 21299a305230SLee Schermerhorn 21309a305230SLee Schermerhorn kobject_put(nhs->hugepages_kobj); 21319a305230SLee Schermerhorn nhs->hugepages_kobj = NULL; 21329a305230SLee Schermerhorn } 21339a305230SLee Schermerhorn 21349a305230SLee Schermerhorn /* 213510fbcf4cSKay Sievers * hugetlb module exit: unregister hstate attributes from node devices 21369a305230SLee Schermerhorn * that have them. 21379a305230SLee Schermerhorn */ 21389a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) 21399a305230SLee Schermerhorn { 21409a305230SLee Schermerhorn int nid; 21419a305230SLee Schermerhorn 21429a305230SLee Schermerhorn /* 214310fbcf4cSKay Sievers * disable node device registrations. 21449a305230SLee Schermerhorn */ 21459a305230SLee Schermerhorn register_hugetlbfs_with_node(NULL, NULL); 21469a305230SLee Schermerhorn 21479a305230SLee Schermerhorn /* 21489a305230SLee Schermerhorn * remove hstate attributes from any nodes that have them. 21499a305230SLee Schermerhorn */ 21509a305230SLee Schermerhorn for (nid = 0; nid < nr_node_ids; nid++) 21518732794bSWen Congyang hugetlb_unregister_node(node_devices[nid]); 21529a305230SLee Schermerhorn } 21539a305230SLee Schermerhorn 21549a305230SLee Schermerhorn /* 215510fbcf4cSKay Sievers * Register hstate attributes for a single node device. 21569a305230SLee Schermerhorn * No-op if attributes already registered. 21579a305230SLee Schermerhorn */ 21583cd8b44fSClaudiu Ghioc static void hugetlb_register_node(struct node *node) 21599a305230SLee Schermerhorn { 21609a305230SLee Schermerhorn struct hstate *h; 216110fbcf4cSKay Sievers struct node_hstate *nhs = &node_hstates[node->dev.id]; 21629a305230SLee Schermerhorn int err; 21639a305230SLee Schermerhorn 21649a305230SLee Schermerhorn if (nhs->hugepages_kobj) 21659a305230SLee Schermerhorn return; /* already allocated */ 21669a305230SLee Schermerhorn 21679a305230SLee Schermerhorn nhs->hugepages_kobj = kobject_create_and_add("hugepages", 216810fbcf4cSKay Sievers &node->dev.kobj); 21699a305230SLee Schermerhorn if (!nhs->hugepages_kobj) 21709a305230SLee Schermerhorn return; 21719a305230SLee Schermerhorn 21729a305230SLee Schermerhorn for_each_hstate(h) { 21739a305230SLee Schermerhorn err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, 21749a305230SLee Schermerhorn nhs->hstate_kobjs, 21759a305230SLee Schermerhorn &per_node_hstate_attr_group); 21769a305230SLee Schermerhorn if (err) { 2177ffb22af5SAndrew Morton pr_err("Hugetlb: Unable to add hstate %s for node %d\n", 217810fbcf4cSKay Sievers h->name, node->dev.id); 21799a305230SLee Schermerhorn hugetlb_unregister_node(node); 21809a305230SLee Schermerhorn break; 21819a305230SLee Schermerhorn } 21829a305230SLee Schermerhorn } 21839a305230SLee Schermerhorn } 21849a305230SLee Schermerhorn 21859a305230SLee Schermerhorn /* 21869b5e5d0fSLee Schermerhorn * hugetlb init time: register hstate attributes for all registered node 218710fbcf4cSKay Sievers * devices of nodes that have memory. All on-line nodes should have 218810fbcf4cSKay Sievers * registered their associated device by this time. 21899a305230SLee Schermerhorn */ 21907d9ca000SLuiz Capitulino static void __init hugetlb_register_all_nodes(void) 21919a305230SLee Schermerhorn { 21929a305230SLee Schermerhorn int nid; 21939a305230SLee Schermerhorn 21948cebfcd0SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 21958732794bSWen Congyang struct node *node = node_devices[nid]; 219610fbcf4cSKay Sievers if (node->dev.id == nid) 21979a305230SLee Schermerhorn hugetlb_register_node(node); 21989a305230SLee Schermerhorn } 21999a305230SLee Schermerhorn 22009a305230SLee Schermerhorn /* 220110fbcf4cSKay Sievers * Let the node device driver know we're here so it can 22029a305230SLee Schermerhorn * [un]register hstate attributes on node hotplug. 22039a305230SLee Schermerhorn */ 22049a305230SLee Schermerhorn register_hugetlbfs_with_node(hugetlb_register_node, 22059a305230SLee Schermerhorn hugetlb_unregister_node); 22069a305230SLee Schermerhorn } 22079a305230SLee Schermerhorn #else /* !CONFIG_NUMA */ 22089a305230SLee Schermerhorn 22099a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) 22109a305230SLee Schermerhorn { 22119a305230SLee Schermerhorn BUG(); 22129a305230SLee Schermerhorn if (nidp) 22139a305230SLee Schermerhorn *nidp = -1; 22149a305230SLee Schermerhorn return NULL; 22159a305230SLee Schermerhorn } 22169a305230SLee Schermerhorn 22179a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) { } 22189a305230SLee Schermerhorn 22199a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { } 22209a305230SLee Schermerhorn 22219a305230SLee Schermerhorn #endif 22229a305230SLee Schermerhorn 2223a3437870SNishanth Aravamudan static void __exit hugetlb_exit(void) 2224a3437870SNishanth Aravamudan { 2225a3437870SNishanth Aravamudan struct hstate *h; 2226a3437870SNishanth Aravamudan 22279a305230SLee Schermerhorn hugetlb_unregister_all_nodes(); 22289a305230SLee Schermerhorn 2229a3437870SNishanth Aravamudan for_each_hstate(h) { 2230972dc4deSAneesh Kumar K.V kobject_put(hstate_kobjs[hstate_index(h)]); 2231a3437870SNishanth Aravamudan } 2232a3437870SNishanth Aravamudan 2233a3437870SNishanth Aravamudan kobject_put(hugepages_kobj); 22348382d914SDavidlohr Bueso kfree(htlb_fault_mutex_table); 2235a3437870SNishanth Aravamudan } 2236a3437870SNishanth Aravamudan module_exit(hugetlb_exit); 2237a3437870SNishanth Aravamudan 2238a3437870SNishanth Aravamudan static int __init hugetlb_init(void) 2239a3437870SNishanth Aravamudan { 22408382d914SDavidlohr Bueso int i; 22418382d914SDavidlohr Bueso 2242457c1b27SNishanth Aravamudan if (!hugepages_supported()) 22430ef89d25SBenjamin Herrenschmidt return 0; 2244a3437870SNishanth Aravamudan 2245e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) { 2246e11bfbfcSNick Piggin default_hstate_size = HPAGE_SIZE; 2247e11bfbfcSNick Piggin if (!size_to_hstate(default_hstate_size)) 2248a3437870SNishanth Aravamudan hugetlb_add_hstate(HUGETLB_PAGE_ORDER); 2249a3437870SNishanth Aravamudan } 2250972dc4deSAneesh Kumar K.V default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size)); 2251e11bfbfcSNick Piggin if (default_hstate_max_huge_pages) 2252e11bfbfcSNick Piggin default_hstate.max_huge_pages = default_hstate_max_huge_pages; 2253a3437870SNishanth Aravamudan 2254a3437870SNishanth Aravamudan hugetlb_init_hstates(); 2255aa888a74SAndi Kleen gather_bootmem_prealloc(); 2256a3437870SNishanth Aravamudan report_hugepages(); 2257a3437870SNishanth Aravamudan 2258a3437870SNishanth Aravamudan hugetlb_sysfs_init(); 22599a305230SLee Schermerhorn hugetlb_register_all_nodes(); 22607179e7bfSJianguo Wu hugetlb_cgroup_file_init(); 22619a305230SLee Schermerhorn 22628382d914SDavidlohr Bueso #ifdef CONFIG_SMP 22638382d914SDavidlohr Bueso num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); 22648382d914SDavidlohr Bueso #else 22658382d914SDavidlohr Bueso num_fault_mutexes = 1; 22668382d914SDavidlohr Bueso #endif 22678382d914SDavidlohr Bueso htlb_fault_mutex_table = 22688382d914SDavidlohr Bueso kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL); 22698382d914SDavidlohr Bueso BUG_ON(!htlb_fault_mutex_table); 22708382d914SDavidlohr Bueso 22718382d914SDavidlohr Bueso for (i = 0; i < num_fault_mutexes; i++) 22728382d914SDavidlohr Bueso mutex_init(&htlb_fault_mutex_table[i]); 2273a3437870SNishanth Aravamudan return 0; 2274a3437870SNishanth Aravamudan } 2275a3437870SNishanth Aravamudan module_init(hugetlb_init); 2276a3437870SNishanth Aravamudan 2277a3437870SNishanth Aravamudan /* Should be called on processing a hugepagesz=... option */ 2278a3437870SNishanth Aravamudan void __init hugetlb_add_hstate(unsigned order) 2279a3437870SNishanth Aravamudan { 2280a3437870SNishanth Aravamudan struct hstate *h; 22818faa8b07SAndi Kleen unsigned long i; 22828faa8b07SAndi Kleen 2283a3437870SNishanth Aravamudan if (size_to_hstate(PAGE_SIZE << order)) { 2284ffb22af5SAndrew Morton pr_warning("hugepagesz= specified twice, ignoring\n"); 2285a3437870SNishanth Aravamudan return; 2286a3437870SNishanth Aravamudan } 228747d38344SAneesh Kumar K.V BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); 2288a3437870SNishanth Aravamudan BUG_ON(order == 0); 228947d38344SAneesh Kumar K.V h = &hstates[hugetlb_max_hstate++]; 2290a3437870SNishanth Aravamudan h->order = order; 2291a3437870SNishanth Aravamudan h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); 22928faa8b07SAndi Kleen h->nr_huge_pages = 0; 22938faa8b07SAndi Kleen h->free_huge_pages = 0; 22948faa8b07SAndi Kleen for (i = 0; i < MAX_NUMNODES; ++i) 22958faa8b07SAndi Kleen INIT_LIST_HEAD(&h->hugepage_freelists[i]); 22960edaecfaSAneesh Kumar K.V INIT_LIST_HEAD(&h->hugepage_activelist); 22978cebfcd0SLai Jiangshan h->next_nid_to_alloc = first_node(node_states[N_MEMORY]); 22988cebfcd0SLai Jiangshan h->next_nid_to_free = first_node(node_states[N_MEMORY]); 2299a3437870SNishanth Aravamudan snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", 2300a3437870SNishanth Aravamudan huge_page_size(h)/1024); 23018faa8b07SAndi Kleen 2302a3437870SNishanth Aravamudan parsed_hstate = h; 2303a3437870SNishanth Aravamudan } 2304a3437870SNishanth Aravamudan 2305e11bfbfcSNick Piggin static int __init hugetlb_nrpages_setup(char *s) 2306a3437870SNishanth Aravamudan { 2307a3437870SNishanth Aravamudan unsigned long *mhp; 23088faa8b07SAndi Kleen static unsigned long *last_mhp; 2309a3437870SNishanth Aravamudan 2310a3437870SNishanth Aravamudan /* 231147d38344SAneesh Kumar K.V * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet, 2312a3437870SNishanth Aravamudan * so this hugepages= parameter goes to the "default hstate". 2313a3437870SNishanth Aravamudan */ 231447d38344SAneesh Kumar K.V if (!hugetlb_max_hstate) 2315a3437870SNishanth Aravamudan mhp = &default_hstate_max_huge_pages; 2316a3437870SNishanth Aravamudan else 2317a3437870SNishanth Aravamudan mhp = &parsed_hstate->max_huge_pages; 2318a3437870SNishanth Aravamudan 23198faa8b07SAndi Kleen if (mhp == last_mhp) { 2320ffb22af5SAndrew Morton pr_warning("hugepages= specified twice without " 23218faa8b07SAndi Kleen "interleaving hugepagesz=, ignoring\n"); 23228faa8b07SAndi Kleen return 1; 23238faa8b07SAndi Kleen } 23248faa8b07SAndi Kleen 2325a3437870SNishanth Aravamudan if (sscanf(s, "%lu", mhp) <= 0) 2326a3437870SNishanth Aravamudan *mhp = 0; 2327a3437870SNishanth Aravamudan 23288faa8b07SAndi Kleen /* 23298faa8b07SAndi Kleen * Global state is always initialized later in hugetlb_init. 23308faa8b07SAndi Kleen * But we need to allocate >= MAX_ORDER hstates here early to still 23318faa8b07SAndi Kleen * use the bootmem allocator. 23328faa8b07SAndi Kleen */ 233347d38344SAneesh Kumar K.V if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER) 23348faa8b07SAndi Kleen hugetlb_hstate_alloc_pages(parsed_hstate); 23358faa8b07SAndi Kleen 23368faa8b07SAndi Kleen last_mhp = mhp; 23378faa8b07SAndi Kleen 2338a3437870SNishanth Aravamudan return 1; 2339a3437870SNishanth Aravamudan } 2340e11bfbfcSNick Piggin __setup("hugepages=", hugetlb_nrpages_setup); 2341e11bfbfcSNick Piggin 2342e11bfbfcSNick Piggin static int __init hugetlb_default_setup(char *s) 2343e11bfbfcSNick Piggin { 2344e11bfbfcSNick Piggin default_hstate_size = memparse(s, &s); 2345e11bfbfcSNick Piggin return 1; 2346e11bfbfcSNick Piggin } 2347e11bfbfcSNick Piggin __setup("default_hugepagesz=", hugetlb_default_setup); 2348a3437870SNishanth Aravamudan 23498a213460SNishanth Aravamudan static unsigned int cpuset_mems_nr(unsigned int *array) 23508a213460SNishanth Aravamudan { 23518a213460SNishanth Aravamudan int node; 23528a213460SNishanth Aravamudan unsigned int nr = 0; 23538a213460SNishanth Aravamudan 23548a213460SNishanth Aravamudan for_each_node_mask(node, cpuset_current_mems_allowed) 23558a213460SNishanth Aravamudan nr += array[node]; 23568a213460SNishanth Aravamudan 23578a213460SNishanth Aravamudan return nr; 23588a213460SNishanth Aravamudan } 23598a213460SNishanth Aravamudan 23608a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL 236106808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy, 236206808b08SLee Schermerhorn struct ctl_table *table, int write, 236306808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 23641da177e4SLinus Torvalds { 2365e5ff2159SAndi Kleen struct hstate *h = &default_hstate; 2366238d3c13SDavid Rientjes unsigned long tmp = h->max_huge_pages; 236708d4a246SMichal Hocko int ret; 2368e5ff2159SAndi Kleen 2369457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2370457c1b27SNishanth Aravamudan return -ENOTSUPP; 2371457c1b27SNishanth Aravamudan 2372e5ff2159SAndi Kleen table->data = &tmp; 2373e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 237408d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 237508d4a246SMichal Hocko if (ret) 237608d4a246SMichal Hocko goto out; 2377e5ff2159SAndi Kleen 2378238d3c13SDavid Rientjes if (write) 2379238d3c13SDavid Rientjes ret = __nr_hugepages_store_common(obey_mempolicy, h, 2380238d3c13SDavid Rientjes NUMA_NO_NODE, tmp, *length); 238108d4a246SMichal Hocko out: 238208d4a246SMichal Hocko return ret; 23831da177e4SLinus Torvalds } 2384396faf03SMel Gorman 238506808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write, 238606808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 238706808b08SLee Schermerhorn { 238806808b08SLee Schermerhorn 238906808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(false, table, write, 239006808b08SLee Schermerhorn buffer, length, ppos); 239106808b08SLee Schermerhorn } 239206808b08SLee Schermerhorn 239306808b08SLee Schermerhorn #ifdef CONFIG_NUMA 239406808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write, 239506808b08SLee Schermerhorn void __user *buffer, size_t *length, loff_t *ppos) 239606808b08SLee Schermerhorn { 239706808b08SLee Schermerhorn return hugetlb_sysctl_handler_common(true, table, write, 239806808b08SLee Schermerhorn buffer, length, ppos); 239906808b08SLee Schermerhorn } 240006808b08SLee Schermerhorn #endif /* CONFIG_NUMA */ 240106808b08SLee Schermerhorn 2402a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write, 24038d65af78SAlexey Dobriyan void __user *buffer, 2404a3d0c6aaSNishanth Aravamudan size_t *length, loff_t *ppos) 2405a3d0c6aaSNishanth Aravamudan { 2406a5516438SAndi Kleen struct hstate *h = &default_hstate; 2407e5ff2159SAndi Kleen unsigned long tmp; 240808d4a246SMichal Hocko int ret; 2409e5ff2159SAndi Kleen 2410457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2411457c1b27SNishanth Aravamudan return -ENOTSUPP; 2412457c1b27SNishanth Aravamudan 2413e5ff2159SAndi Kleen tmp = h->nr_overcommit_huge_pages; 2414e5ff2159SAndi Kleen 2415bae7f4aeSLuiz Capitulino if (write && hstate_is_gigantic(h)) 2416adbe8726SEric B Munson return -EINVAL; 2417adbe8726SEric B Munson 2418e5ff2159SAndi Kleen table->data = &tmp; 2419e5ff2159SAndi Kleen table->maxlen = sizeof(unsigned long); 242008d4a246SMichal Hocko ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); 242108d4a246SMichal Hocko if (ret) 242208d4a246SMichal Hocko goto out; 2423e5ff2159SAndi Kleen 2424e5ff2159SAndi Kleen if (write) { 2425064d9efeSNishanth Aravamudan spin_lock(&hugetlb_lock); 2426e5ff2159SAndi Kleen h->nr_overcommit_huge_pages = tmp; 2427a3d0c6aaSNishanth Aravamudan spin_unlock(&hugetlb_lock); 2428e5ff2159SAndi Kleen } 242908d4a246SMichal Hocko out: 243008d4a246SMichal Hocko return ret; 2431a3d0c6aaSNishanth Aravamudan } 2432a3d0c6aaSNishanth Aravamudan 24331da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 24341da177e4SLinus Torvalds 2435e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m) 24361da177e4SLinus Torvalds { 2437a5516438SAndi Kleen struct hstate *h = &default_hstate; 2438457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2439457c1b27SNishanth Aravamudan return; 2440e1759c21SAlexey Dobriyan seq_printf(m, 24411da177e4SLinus Torvalds "HugePages_Total: %5lu\n" 24421da177e4SLinus Torvalds "HugePages_Free: %5lu\n" 2443b45b5bd6SDavid Gibson "HugePages_Rsvd: %5lu\n" 24447893d1d5SAdam Litke "HugePages_Surp: %5lu\n" 24454f98a2feSRik van Riel "Hugepagesize: %8lu kB\n", 2446a5516438SAndi Kleen h->nr_huge_pages, 2447a5516438SAndi Kleen h->free_huge_pages, 2448a5516438SAndi Kleen h->resv_huge_pages, 2449a5516438SAndi Kleen h->surplus_huge_pages, 2450a5516438SAndi Kleen 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 24511da177e4SLinus Torvalds } 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf) 24541da177e4SLinus Torvalds { 2455a5516438SAndi Kleen struct hstate *h = &default_hstate; 2456457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2457457c1b27SNishanth Aravamudan return 0; 24581da177e4SLinus Torvalds return sprintf(buf, 24591da177e4SLinus Torvalds "Node %d HugePages_Total: %5u\n" 2460a1de0919SNishanth Aravamudan "Node %d HugePages_Free: %5u\n" 2461a1de0919SNishanth Aravamudan "Node %d HugePages_Surp: %5u\n", 2462a5516438SAndi Kleen nid, h->nr_huge_pages_node[nid], 2463a5516438SAndi Kleen nid, h->free_huge_pages_node[nid], 2464a5516438SAndi Kleen nid, h->surplus_huge_pages_node[nid]); 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 2467949f7ec5SDavid Rientjes void hugetlb_show_meminfo(void) 2468949f7ec5SDavid Rientjes { 2469949f7ec5SDavid Rientjes struct hstate *h; 2470949f7ec5SDavid Rientjes int nid; 2471949f7ec5SDavid Rientjes 2472457c1b27SNishanth Aravamudan if (!hugepages_supported()) 2473457c1b27SNishanth Aravamudan return; 2474457c1b27SNishanth Aravamudan 2475949f7ec5SDavid Rientjes for_each_node_state(nid, N_MEMORY) 2476949f7ec5SDavid Rientjes for_each_hstate(h) 2477949f7ec5SDavid Rientjes pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", 2478949f7ec5SDavid Rientjes nid, 2479949f7ec5SDavid Rientjes h->nr_huge_pages_node[nid], 2480949f7ec5SDavid Rientjes h->free_huge_pages_node[nid], 2481949f7ec5SDavid Rientjes h->surplus_huge_pages_node[nid], 2482949f7ec5SDavid Rientjes 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); 2483949f7ec5SDavid Rientjes } 2484949f7ec5SDavid Rientjes 24851da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 24861da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void) 24871da177e4SLinus Torvalds { 2488d0028588SWanpeng Li struct hstate *h; 2489d0028588SWanpeng Li unsigned long nr_total_pages = 0; 2490d0028588SWanpeng Li 2491d0028588SWanpeng Li for_each_hstate(h) 2492d0028588SWanpeng Li nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); 2493d0028588SWanpeng Li return nr_total_pages; 24941da177e4SLinus Torvalds } 24951da177e4SLinus Torvalds 2496a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta) 2497fc1b8a73SMel Gorman { 2498fc1b8a73SMel Gorman int ret = -ENOMEM; 2499fc1b8a73SMel Gorman 2500fc1b8a73SMel Gorman spin_lock(&hugetlb_lock); 2501fc1b8a73SMel Gorman /* 2502fc1b8a73SMel Gorman * When cpuset is configured, it breaks the strict hugetlb page 2503fc1b8a73SMel Gorman * reservation as the accounting is done on a global variable. Such 2504fc1b8a73SMel Gorman * reservation is completely rubbish in the presence of cpuset because 2505fc1b8a73SMel Gorman * the reservation is not checked against page availability for the 2506fc1b8a73SMel Gorman * current cpuset. Application can still potentially OOM'ed by kernel 2507fc1b8a73SMel Gorman * with lack of free htlb page in cpuset that the task is in. 2508fc1b8a73SMel Gorman * Attempt to enforce strict accounting with cpuset is almost 2509fc1b8a73SMel Gorman * impossible (or too ugly) because cpuset is too fluid that 2510fc1b8a73SMel Gorman * task or memory node can be dynamically moved between cpusets. 2511fc1b8a73SMel Gorman * 2512fc1b8a73SMel Gorman * The change of semantics for shared hugetlb mapping with cpuset is 2513fc1b8a73SMel Gorman * undesirable. However, in order to preserve some of the semantics, 2514fc1b8a73SMel Gorman * we fall back to check against current free page availability as 2515fc1b8a73SMel Gorman * a best attempt and hopefully to minimize the impact of changing 2516fc1b8a73SMel Gorman * semantics that cpuset has. 2517fc1b8a73SMel Gorman */ 2518fc1b8a73SMel Gorman if (delta > 0) { 2519a5516438SAndi Kleen if (gather_surplus_pages(h, delta) < 0) 2520fc1b8a73SMel Gorman goto out; 2521fc1b8a73SMel Gorman 2522a5516438SAndi Kleen if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { 2523a5516438SAndi Kleen return_unused_surplus_pages(h, delta); 2524fc1b8a73SMel Gorman goto out; 2525fc1b8a73SMel Gorman } 2526fc1b8a73SMel Gorman } 2527fc1b8a73SMel Gorman 2528fc1b8a73SMel Gorman ret = 0; 2529fc1b8a73SMel Gorman if (delta < 0) 2530a5516438SAndi Kleen return_unused_surplus_pages(h, (unsigned long) -delta); 2531fc1b8a73SMel Gorman 2532fc1b8a73SMel Gorman out: 2533fc1b8a73SMel Gorman spin_unlock(&hugetlb_lock); 2534fc1b8a73SMel Gorman return ret; 2535fc1b8a73SMel Gorman } 2536fc1b8a73SMel Gorman 253784afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma) 253884afd99bSAndy Whitcroft { 2539f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 254084afd99bSAndy Whitcroft 254184afd99bSAndy Whitcroft /* 254284afd99bSAndy Whitcroft * This new VMA should share its siblings reservation map if present. 254384afd99bSAndy Whitcroft * The VMA will only ever have a valid reservation map pointer where 254484afd99bSAndy Whitcroft * it is being copied for another still existing VMA. As that VMA 254525985edcSLucas De Marchi * has a reference to the reservation map it cannot disappear until 254684afd99bSAndy Whitcroft * after this open call completes. It is therefore safe to take a 254784afd99bSAndy Whitcroft * new reference here without additional locking. 254884afd99bSAndy Whitcroft */ 25494e35f483SJoonsoo Kim if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 2550f522c3acSJoonsoo Kim kref_get(&resv->refs); 255184afd99bSAndy Whitcroft } 255284afd99bSAndy Whitcroft 2553a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma) 2554a1e78772SMel Gorman { 2555a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2556f522c3acSJoonsoo Kim struct resv_map *resv = vma_resv_map(vma); 255790481622SDavid Gibson struct hugepage_subpool *spool = subpool_vma(vma); 25584e35f483SJoonsoo Kim unsigned long reserve, start, end; 25591c5ecae3SMike Kravetz long gbl_reserve; 256084afd99bSAndy Whitcroft 25614e35f483SJoonsoo Kim if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 25624e35f483SJoonsoo Kim return; 25634e35f483SJoonsoo Kim 2564a5516438SAndi Kleen start = vma_hugecache_offset(h, vma, vma->vm_start); 2565a5516438SAndi Kleen end = vma_hugecache_offset(h, vma, vma->vm_end); 256684afd99bSAndy Whitcroft 25674e35f483SJoonsoo Kim reserve = (end - start) - region_count(resv, start, end); 256884afd99bSAndy Whitcroft 2569f031dd27SJoonsoo Kim kref_put(&resv->refs, resv_map_release); 257084afd99bSAndy Whitcroft 25717251ff78SAdam Litke if (reserve) { 25721c5ecae3SMike Kravetz /* 25731c5ecae3SMike Kravetz * Decrement reserve counts. The global reserve count may be 25741c5ecae3SMike Kravetz * adjusted if the subpool has a minimum size. 25751c5ecae3SMike Kravetz */ 25761c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, reserve); 25771c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 25787251ff78SAdam Litke } 2579a1e78772SMel Gorman } 2580a1e78772SMel Gorman 25811da177e4SLinus Torvalds /* 25821da177e4SLinus Torvalds * We cannot handle pagefaults against hugetlb pages at all. They cause 25831da177e4SLinus Torvalds * handle_mm_fault() to try to instantiate regular-sized pages in the 25841da177e4SLinus Torvalds * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get 25851da177e4SLinus Torvalds * this far. 25861da177e4SLinus Torvalds */ 2587d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 25881da177e4SLinus Torvalds { 25891da177e4SLinus Torvalds BUG(); 2590d0217ac0SNick Piggin return 0; 25911da177e4SLinus Torvalds } 25921da177e4SLinus Torvalds 2593f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = { 2594d0217ac0SNick Piggin .fault = hugetlb_vm_op_fault, 259584afd99bSAndy Whitcroft .open = hugetlb_vm_op_open, 2596a1e78772SMel Gorman .close = hugetlb_vm_op_close, 25971da177e4SLinus Torvalds }; 25981da177e4SLinus Torvalds 25991e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, 26001e8f889bSDavid Gibson int writable) 260163551ae0SDavid Gibson { 260263551ae0SDavid Gibson pte_t entry; 260363551ae0SDavid Gibson 26041e8f889bSDavid Gibson if (writable) { 2605106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, 2606106c992aSGerald Schaefer vma->vm_page_prot))); 260763551ae0SDavid Gibson } else { 2608106c992aSGerald Schaefer entry = huge_pte_wrprotect(mk_huge_pte(page, 2609106c992aSGerald Schaefer vma->vm_page_prot)); 261063551ae0SDavid Gibson } 261163551ae0SDavid Gibson entry = pte_mkyoung(entry); 261263551ae0SDavid Gibson entry = pte_mkhuge(entry); 2613d9ed9faaSChris Metcalf entry = arch_make_huge_pte(entry, vma, page, writable); 261463551ae0SDavid Gibson 261563551ae0SDavid Gibson return entry; 261663551ae0SDavid Gibson } 261763551ae0SDavid Gibson 26181e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma, 26191e8f889bSDavid Gibson unsigned long address, pte_t *ptep) 26201e8f889bSDavid Gibson { 26211e8f889bSDavid Gibson pte_t entry; 26221e8f889bSDavid Gibson 2623106c992aSGerald Schaefer entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); 262432f84528SChris Forbes if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) 26254b3073e1SRussell King update_mmu_cache(vma, address, ptep); 26261e8f889bSDavid Gibson } 26271e8f889bSDavid Gibson 26284a705fefSNaoya Horiguchi static int is_hugetlb_entry_migration(pte_t pte) 26294a705fefSNaoya Horiguchi { 26304a705fefSNaoya Horiguchi swp_entry_t swp; 26314a705fefSNaoya Horiguchi 26324a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 26334a705fefSNaoya Horiguchi return 0; 26344a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 26354a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_migration_entry(swp)) 26364a705fefSNaoya Horiguchi return 1; 26374a705fefSNaoya Horiguchi else 26384a705fefSNaoya Horiguchi return 0; 26394a705fefSNaoya Horiguchi } 26404a705fefSNaoya Horiguchi 26414a705fefSNaoya Horiguchi static int is_hugetlb_entry_hwpoisoned(pte_t pte) 26424a705fefSNaoya Horiguchi { 26434a705fefSNaoya Horiguchi swp_entry_t swp; 26444a705fefSNaoya Horiguchi 26454a705fefSNaoya Horiguchi if (huge_pte_none(pte) || pte_present(pte)) 26464a705fefSNaoya Horiguchi return 0; 26474a705fefSNaoya Horiguchi swp = pte_to_swp_entry(pte); 26484a705fefSNaoya Horiguchi if (non_swap_entry(swp) && is_hwpoison_entry(swp)) 26494a705fefSNaoya Horiguchi return 1; 26504a705fefSNaoya Horiguchi else 26514a705fefSNaoya Horiguchi return 0; 26524a705fefSNaoya Horiguchi } 26531e8f889bSDavid Gibson 265463551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 265563551ae0SDavid Gibson struct vm_area_struct *vma) 265663551ae0SDavid Gibson { 265763551ae0SDavid Gibson pte_t *src_pte, *dst_pte, entry; 265863551ae0SDavid Gibson struct page *ptepage; 26591c59827dSHugh Dickins unsigned long addr; 26601e8f889bSDavid Gibson int cow; 2661a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2662a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 2663e8569dd2SAndreas Sandberg unsigned long mmun_start; /* For mmu_notifiers */ 2664e8569dd2SAndreas Sandberg unsigned long mmun_end; /* For mmu_notifiers */ 2665e8569dd2SAndreas Sandberg int ret = 0; 26661e8f889bSDavid Gibson 26671e8f889bSDavid Gibson cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 266863551ae0SDavid Gibson 2669e8569dd2SAndreas Sandberg mmun_start = vma->vm_start; 2670e8569dd2SAndreas Sandberg mmun_end = vma->vm_end; 2671e8569dd2SAndreas Sandberg if (cow) 2672e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end); 2673e8569dd2SAndreas Sandberg 2674a5516438SAndi Kleen for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { 2675cb900f41SKirill A. Shutemov spinlock_t *src_ptl, *dst_ptl; 2676c74df32cSHugh Dickins src_pte = huge_pte_offset(src, addr); 2677c74df32cSHugh Dickins if (!src_pte) 2678c74df32cSHugh Dickins continue; 2679a5516438SAndi Kleen dst_pte = huge_pte_alloc(dst, addr, sz); 2680e8569dd2SAndreas Sandberg if (!dst_pte) { 2681e8569dd2SAndreas Sandberg ret = -ENOMEM; 2682e8569dd2SAndreas Sandberg break; 2683e8569dd2SAndreas Sandberg } 2684c5c99429SLarry Woodman 2685c5c99429SLarry Woodman /* If the pagetables are shared don't copy or take references */ 2686c5c99429SLarry Woodman if (dst_pte == src_pte) 2687c5c99429SLarry Woodman continue; 2688c5c99429SLarry Woodman 2689cb900f41SKirill A. Shutemov dst_ptl = huge_pte_lock(h, dst, dst_pte); 2690cb900f41SKirill A. Shutemov src_ptl = huge_pte_lockptr(h, src, src_pte); 2691cb900f41SKirill A. Shutemov spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 26924a705fefSNaoya Horiguchi entry = huge_ptep_get(src_pte); 26934a705fefSNaoya Horiguchi if (huge_pte_none(entry)) { /* skip none entry */ 26944a705fefSNaoya Horiguchi ; 26954a705fefSNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_migration(entry) || 26964a705fefSNaoya Horiguchi is_hugetlb_entry_hwpoisoned(entry))) { 26974a705fefSNaoya Horiguchi swp_entry_t swp_entry = pte_to_swp_entry(entry); 26984a705fefSNaoya Horiguchi 26994a705fefSNaoya Horiguchi if (is_write_migration_entry(swp_entry) && cow) { 27004a705fefSNaoya Horiguchi /* 27014a705fefSNaoya Horiguchi * COW mappings require pages in both 27024a705fefSNaoya Horiguchi * parent and child to be set to read. 27034a705fefSNaoya Horiguchi */ 27044a705fefSNaoya Horiguchi make_migration_entry_read(&swp_entry); 27054a705fefSNaoya Horiguchi entry = swp_entry_to_pte(swp_entry); 27064a705fefSNaoya Horiguchi set_huge_pte_at(src, addr, src_pte, entry); 27074a705fefSNaoya Horiguchi } 27084a705fefSNaoya Horiguchi set_huge_pte_at(dst, addr, dst_pte, entry); 27094a705fefSNaoya Horiguchi } else { 271034ee645eSJoerg Roedel if (cow) { 27117f2e9525SGerald Schaefer huge_ptep_set_wrprotect(src, addr, src_pte); 271234ee645eSJoerg Roedel mmu_notifier_invalidate_range(src, mmun_start, 271334ee645eSJoerg Roedel mmun_end); 271434ee645eSJoerg Roedel } 27150253d634SNaoya Horiguchi entry = huge_ptep_get(src_pte); 271663551ae0SDavid Gibson ptepage = pte_page(entry); 271763551ae0SDavid Gibson get_page(ptepage); 27180fe6e20bSNaoya Horiguchi page_dup_rmap(ptepage); 271963551ae0SDavid Gibson set_huge_pte_at(dst, addr, dst_pte, entry); 27201c59827dSHugh Dickins } 2721cb900f41SKirill A. Shutemov spin_unlock(src_ptl); 2722cb900f41SKirill A. Shutemov spin_unlock(dst_ptl); 272363551ae0SDavid Gibson } 272463551ae0SDavid Gibson 2725e8569dd2SAndreas Sandberg if (cow) 2726e8569dd2SAndreas Sandberg mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end); 2727e8569dd2SAndreas Sandberg 2728e8569dd2SAndreas Sandberg return ret; 272963551ae0SDavid Gibson } 273063551ae0SDavid Gibson 273124669e58SAneesh Kumar K.V void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, 273224669e58SAneesh Kumar K.V unsigned long start, unsigned long end, 273324669e58SAneesh Kumar K.V struct page *ref_page) 273463551ae0SDavid Gibson { 273524669e58SAneesh Kumar K.V int force_flush = 0; 273663551ae0SDavid Gibson struct mm_struct *mm = vma->vm_mm; 273763551ae0SDavid Gibson unsigned long address; 2738c7546f8fSDavid Gibson pte_t *ptep; 273963551ae0SDavid Gibson pte_t pte; 2740cb900f41SKirill A. Shutemov spinlock_t *ptl; 274163551ae0SDavid Gibson struct page *page; 2742a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 2743a5516438SAndi Kleen unsigned long sz = huge_page_size(h); 27442ec74c3eSSagi Grimberg const unsigned long mmun_start = start; /* For mmu_notifiers */ 27452ec74c3eSSagi Grimberg const unsigned long mmun_end = end; /* For mmu_notifiers */ 2746a5516438SAndi Kleen 274763551ae0SDavid Gibson WARN_ON(!is_vm_hugetlb_page(vma)); 2748a5516438SAndi Kleen BUG_ON(start & ~huge_page_mask(h)); 2749a5516438SAndi Kleen BUG_ON(end & ~huge_page_mask(h)); 275063551ae0SDavid Gibson 275124669e58SAneesh Kumar K.V tlb_start_vma(tlb, vma); 27522ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 2753569f48b8SHillf Danton address = start; 275424669e58SAneesh Kumar K.V again: 2755569f48b8SHillf Danton for (; address < end; address += sz) { 2756c7546f8fSDavid Gibson ptep = huge_pte_offset(mm, address); 2757c7546f8fSDavid Gibson if (!ptep) 2758c7546f8fSDavid Gibson continue; 2759c7546f8fSDavid Gibson 2760cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 276139dde65cSChen, Kenneth W if (huge_pmd_unshare(mm, &address, ptep)) 2762cb900f41SKirill A. Shutemov goto unlock; 276339dde65cSChen, Kenneth W 27646629326bSHillf Danton pte = huge_ptep_get(ptep); 27656629326bSHillf Danton if (huge_pte_none(pte)) 2766cb900f41SKirill A. Shutemov goto unlock; 27676629326bSHillf Danton 27686629326bSHillf Danton /* 27699fbc1f63SNaoya Horiguchi * Migrating hugepage or HWPoisoned hugepage is already 27709fbc1f63SNaoya Horiguchi * unmapped and its refcount is dropped, so just clear pte here. 27716629326bSHillf Danton */ 27729fbc1f63SNaoya Horiguchi if (unlikely(!pte_present(pte))) { 2773106c992aSGerald Schaefer huge_pte_clear(mm, address, ptep); 2774cb900f41SKirill A. Shutemov goto unlock; 27758c4894c6SNaoya Horiguchi } 27766629326bSHillf Danton 27776629326bSHillf Danton page = pte_page(pte); 277804f2cbe3SMel Gorman /* 277904f2cbe3SMel Gorman * If a reference page is supplied, it is because a specific 278004f2cbe3SMel Gorman * page is being unmapped, not a range. Ensure the page we 278104f2cbe3SMel Gorman * are about to unmap is the actual page of interest. 278204f2cbe3SMel Gorman */ 278304f2cbe3SMel Gorman if (ref_page) { 278404f2cbe3SMel Gorman if (page != ref_page) 2785cb900f41SKirill A. Shutemov goto unlock; 278604f2cbe3SMel Gorman 278704f2cbe3SMel Gorman /* 278804f2cbe3SMel Gorman * Mark the VMA as having unmapped its page so that 278904f2cbe3SMel Gorman * future faults in this VMA will fail rather than 279004f2cbe3SMel Gorman * looking like data was lost 279104f2cbe3SMel Gorman */ 279204f2cbe3SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); 279304f2cbe3SMel Gorman } 279404f2cbe3SMel Gorman 2795c7546f8fSDavid Gibson pte = huge_ptep_get_and_clear(mm, address, ptep); 279624669e58SAneesh Kumar K.V tlb_remove_tlb_entry(tlb, ptep, address); 2797106c992aSGerald Schaefer if (huge_pte_dirty(pte)) 27986649a386SKen Chen set_page_dirty(page); 27999e81130bSHillf Danton 280024669e58SAneesh Kumar K.V page_remove_rmap(page); 280124669e58SAneesh Kumar K.V force_flush = !__tlb_remove_page(tlb, page); 2802cb900f41SKirill A. Shutemov if (force_flush) { 2803569f48b8SHillf Danton address += sz; 2804cb900f41SKirill A. Shutemov spin_unlock(ptl); 28059e81130bSHillf Danton break; 280663551ae0SDavid Gibson } 2807cb900f41SKirill A. Shutemov /* Bail out after unmapping reference page if supplied */ 2808cb900f41SKirill A. Shutemov if (ref_page) { 2809cb900f41SKirill A. Shutemov spin_unlock(ptl); 2810cb900f41SKirill A. Shutemov break; 2811cb900f41SKirill A. Shutemov } 2812cb900f41SKirill A. Shutemov unlock: 2813cb900f41SKirill A. Shutemov spin_unlock(ptl); 2814cb900f41SKirill A. Shutemov } 281524669e58SAneesh Kumar K.V /* 281624669e58SAneesh Kumar K.V * mmu_gather ran out of room to batch pages, we break out of 281724669e58SAneesh Kumar K.V * the PTE lock to avoid doing the potential expensive TLB invalidate 281824669e58SAneesh Kumar K.V * and page-free while holding it. 281924669e58SAneesh Kumar K.V */ 282024669e58SAneesh Kumar K.V if (force_flush) { 282124669e58SAneesh Kumar K.V force_flush = 0; 282224669e58SAneesh Kumar K.V tlb_flush_mmu(tlb); 282324669e58SAneesh Kumar K.V if (address < end && !ref_page) 282424669e58SAneesh Kumar K.V goto again; 2825fe1668aeSChen, Kenneth W } 28262ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 282724669e58SAneesh Kumar K.V tlb_end_vma(tlb, vma); 28281da177e4SLinus Torvalds } 282963551ae0SDavid Gibson 2830d833352aSMel Gorman void __unmap_hugepage_range_final(struct mmu_gather *tlb, 2831d833352aSMel Gorman struct vm_area_struct *vma, unsigned long start, 2832d833352aSMel Gorman unsigned long end, struct page *ref_page) 2833d833352aSMel Gorman { 2834d833352aSMel Gorman __unmap_hugepage_range(tlb, vma, start, end, ref_page); 2835d833352aSMel Gorman 2836d833352aSMel Gorman /* 2837d833352aSMel Gorman * Clear this flag so that x86's huge_pmd_share page_table_shareable 2838d833352aSMel Gorman * test will fail on a vma being torn down, and not grab a page table 2839d833352aSMel Gorman * on its way out. We're lucky that the flag has such an appropriate 2840d833352aSMel Gorman * name, and can in fact be safely cleared here. We could clear it 2841d833352aSMel Gorman * before the __unmap_hugepage_range above, but all that's necessary 2842c8c06efaSDavidlohr Bueso * is to clear it before releasing the i_mmap_rwsem. This works 2843d833352aSMel Gorman * because in the context this is called, the VMA is about to be 2844c8c06efaSDavidlohr Bueso * destroyed and the i_mmap_rwsem is held. 2845d833352aSMel Gorman */ 2846d833352aSMel Gorman vma->vm_flags &= ~VM_MAYSHARE; 2847d833352aSMel Gorman } 2848d833352aSMel Gorman 2849502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 285004f2cbe3SMel Gorman unsigned long end, struct page *ref_page) 2851502717f4SChen, Kenneth W { 285224669e58SAneesh Kumar K.V struct mm_struct *mm; 285324669e58SAneesh Kumar K.V struct mmu_gather tlb; 285424669e58SAneesh Kumar K.V 285524669e58SAneesh Kumar K.V mm = vma->vm_mm; 285624669e58SAneesh Kumar K.V 28572b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, start, end); 285824669e58SAneesh Kumar K.V __unmap_hugepage_range(&tlb, vma, start, end, ref_page); 285924669e58SAneesh Kumar K.V tlb_finish_mmu(&tlb, start, end); 2860502717f4SChen, Kenneth W } 2861502717f4SChen, Kenneth W 286204f2cbe3SMel Gorman /* 286304f2cbe3SMel Gorman * This is called when the original mapper is failing to COW a MAP_PRIVATE 286404f2cbe3SMel Gorman * mappping it owns the reserve page for. The intention is to unmap the page 286504f2cbe3SMel Gorman * from other VMAs and let the children be SIGKILLed if they are faulting the 286604f2cbe3SMel Gorman * same region. 286704f2cbe3SMel Gorman */ 28682f4612afSDavidlohr Bueso static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, 28692a4b3dedSHarvey Harrison struct page *page, unsigned long address) 287004f2cbe3SMel Gorman { 28717526674dSAdam Litke struct hstate *h = hstate_vma(vma); 287204f2cbe3SMel Gorman struct vm_area_struct *iter_vma; 287304f2cbe3SMel Gorman struct address_space *mapping; 287404f2cbe3SMel Gorman pgoff_t pgoff; 287504f2cbe3SMel Gorman 287604f2cbe3SMel Gorman /* 287704f2cbe3SMel Gorman * vm_pgoff is in PAGE_SIZE units, hence the different calculation 287804f2cbe3SMel Gorman * from page cache lookup which is in HPAGE_SIZE units. 287904f2cbe3SMel Gorman */ 28807526674dSAdam Litke address = address & huge_page_mask(h); 288136e4f20aSMichal Hocko pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + 288236e4f20aSMichal Hocko vma->vm_pgoff; 2883496ad9aaSAl Viro mapping = file_inode(vma->vm_file)->i_mapping; 288404f2cbe3SMel Gorman 28854eb2b1dcSMel Gorman /* 28864eb2b1dcSMel Gorman * Take the mapping lock for the duration of the table walk. As 28874eb2b1dcSMel Gorman * this mapping should be shared between all the VMAs, 28884eb2b1dcSMel Gorman * __unmap_hugepage_range() is called as the lock is already held 28894eb2b1dcSMel Gorman */ 289083cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 28916b2dbba8SMichel Lespinasse vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { 289204f2cbe3SMel Gorman /* Do not unmap the current VMA */ 289304f2cbe3SMel Gorman if (iter_vma == vma) 289404f2cbe3SMel Gorman continue; 289504f2cbe3SMel Gorman 289604f2cbe3SMel Gorman /* 289704f2cbe3SMel Gorman * Unmap the page from other VMAs without their own reserves. 289804f2cbe3SMel Gorman * They get marked to be SIGKILLed if they fault in these 289904f2cbe3SMel Gorman * areas. This is because a future no-page fault on this VMA 290004f2cbe3SMel Gorman * could insert a zeroed page instead of the data existing 290104f2cbe3SMel Gorman * from the time of fork. This would look like data corruption 290204f2cbe3SMel Gorman */ 290304f2cbe3SMel Gorman if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) 290424669e58SAneesh Kumar K.V unmap_hugepage_range(iter_vma, address, 290524669e58SAneesh Kumar K.V address + huge_page_size(h), page); 290604f2cbe3SMel Gorman } 290783cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 290804f2cbe3SMel Gorman } 290904f2cbe3SMel Gorman 29100fe6e20bSNaoya Horiguchi /* 29110fe6e20bSNaoya Horiguchi * Hugetlb_cow() should be called with page lock of the original hugepage held. 2912ef009b25SMichal Hocko * Called with hugetlb_instantiation_mutex held and pte_page locked so we 2913ef009b25SMichal Hocko * cannot race with other handlers or page migration. 2914ef009b25SMichal Hocko * Keep the pte_same checks anyway to make transition from the mutex easier. 29150fe6e20bSNaoya Horiguchi */ 29161e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, 291704f2cbe3SMel Gorman unsigned long address, pte_t *ptep, pte_t pte, 2918cb900f41SKirill A. Shutemov struct page *pagecache_page, spinlock_t *ptl) 29191e8f889bSDavid Gibson { 2920a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 29211e8f889bSDavid Gibson struct page *old_page, *new_page; 2922ad4404a2SDavidlohr Bueso int ret = 0, outside_reserve = 0; 29232ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 29242ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 29251e8f889bSDavid Gibson 29261e8f889bSDavid Gibson old_page = pte_page(pte); 29271e8f889bSDavid Gibson 292804f2cbe3SMel Gorman retry_avoidcopy: 29291e8f889bSDavid Gibson /* If no-one else is actually using this page, avoid the copy 29301e8f889bSDavid Gibson * and just make the page writable */ 293137a2140dSJoonsoo Kim if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { 29320fe6e20bSNaoya Horiguchi page_move_anon_rmap(old_page, vma, address); 29331e8f889bSDavid Gibson set_huge_ptep_writable(vma, address, ptep); 293483c54070SNick Piggin return 0; 29351e8f889bSDavid Gibson } 29361e8f889bSDavid Gibson 293704f2cbe3SMel Gorman /* 293804f2cbe3SMel Gorman * If the process that created a MAP_PRIVATE mapping is about to 293904f2cbe3SMel Gorman * perform a COW due to a shared page count, attempt to satisfy 294004f2cbe3SMel Gorman * the allocation without using the existing reserves. The pagecache 294104f2cbe3SMel Gorman * page is used to determine if the reserve at this address was 294204f2cbe3SMel Gorman * consumed or not. If reserves were used, a partial faulted mapping 294304f2cbe3SMel Gorman * at the time of fork() could consume its reserves on COW instead 294404f2cbe3SMel Gorman * of the full address range. 294504f2cbe3SMel Gorman */ 29465944d011SJoonsoo Kim if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && 294704f2cbe3SMel Gorman old_page != pagecache_page) 294804f2cbe3SMel Gorman outside_reserve = 1; 294904f2cbe3SMel Gorman 29501e8f889bSDavid Gibson page_cache_get(old_page); 2951b76c8cfbSLarry Woodman 2952ad4404a2SDavidlohr Bueso /* 2953ad4404a2SDavidlohr Bueso * Drop page table lock as buddy allocator may be called. It will 2954ad4404a2SDavidlohr Bueso * be acquired again before returning to the caller, as expected. 2955ad4404a2SDavidlohr Bueso */ 2956cb900f41SKirill A. Shutemov spin_unlock(ptl); 295704f2cbe3SMel Gorman new_page = alloc_huge_page(vma, address, outside_reserve); 29581e8f889bSDavid Gibson 29592fc39cecSAdam Litke if (IS_ERR(new_page)) { 296004f2cbe3SMel Gorman /* 296104f2cbe3SMel Gorman * If a process owning a MAP_PRIVATE mapping fails to COW, 296204f2cbe3SMel Gorman * it is due to references held by a child and an insufficient 296304f2cbe3SMel Gorman * huge page pool. To guarantee the original mappers 296404f2cbe3SMel Gorman * reliability, unmap the page from child processes. The child 296504f2cbe3SMel Gorman * may get SIGKILLed if it later faults. 296604f2cbe3SMel Gorman */ 296704f2cbe3SMel Gorman if (outside_reserve) { 2968ad4404a2SDavidlohr Bueso page_cache_release(old_page); 296904f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 29702f4612afSDavidlohr Bueso unmap_ref_private(mm, vma, old_page, address); 297104f2cbe3SMel Gorman BUG_ON(huge_pte_none(pte)); 2972cb900f41SKirill A. Shutemov spin_lock(ptl); 2973a734bcc8SHillf Danton ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 2974a9af0c5dSNaoya Horiguchi if (likely(ptep && 2975a9af0c5dSNaoya Horiguchi pte_same(huge_ptep_get(ptep), pte))) 297604f2cbe3SMel Gorman goto retry_avoidcopy; 2977a734bcc8SHillf Danton /* 2978cb900f41SKirill A. Shutemov * race occurs while re-acquiring page table 2979cb900f41SKirill A. Shutemov * lock, and our job is done. 2980a734bcc8SHillf Danton */ 2981a734bcc8SHillf Danton return 0; 298204f2cbe3SMel Gorman } 298304f2cbe3SMel Gorman 2984ad4404a2SDavidlohr Bueso ret = (PTR_ERR(new_page) == -ENOMEM) ? 2985ad4404a2SDavidlohr Bueso VM_FAULT_OOM : VM_FAULT_SIGBUS; 2986ad4404a2SDavidlohr Bueso goto out_release_old; 29871e8f889bSDavid Gibson } 29881e8f889bSDavid Gibson 29890fe6e20bSNaoya Horiguchi /* 29900fe6e20bSNaoya Horiguchi * When the original hugepage is shared one, it does not have 29910fe6e20bSNaoya Horiguchi * anon_vma prepared. 29920fe6e20bSNaoya Horiguchi */ 299344e2aa93SDean Nelson if (unlikely(anon_vma_prepare(vma))) { 2994ad4404a2SDavidlohr Bueso ret = VM_FAULT_OOM; 2995ad4404a2SDavidlohr Bueso goto out_release_all; 299644e2aa93SDean Nelson } 29970fe6e20bSNaoya Horiguchi 299847ad8475SAndrea Arcangeli copy_user_huge_page(new_page, old_page, address, vma, 299947ad8475SAndrea Arcangeli pages_per_huge_page(h)); 30000ed361deSNick Piggin __SetPageUptodate(new_page); 3001bcc54222SNaoya Horiguchi set_page_huge_active(new_page); 30021e8f889bSDavid Gibson 30032ec74c3eSSagi Grimberg mmun_start = address & huge_page_mask(h); 30042ec74c3eSSagi Grimberg mmun_end = mmun_start + huge_page_size(h); 30052ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 3006ad4404a2SDavidlohr Bueso 3007b76c8cfbSLarry Woodman /* 3008cb900f41SKirill A. Shutemov * Retake the page table lock to check for racing updates 3009b76c8cfbSLarry Woodman * before the page tables are altered 3010b76c8cfbSLarry Woodman */ 3011cb900f41SKirill A. Shutemov spin_lock(ptl); 3012a5516438SAndi Kleen ptep = huge_pte_offset(mm, address & huge_page_mask(h)); 3013a9af0c5dSNaoya Horiguchi if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { 301407443a85SJoonsoo Kim ClearPagePrivate(new_page); 301507443a85SJoonsoo Kim 30161e8f889bSDavid Gibson /* Break COW */ 30178fe627ecSGerald Schaefer huge_ptep_clear_flush(vma, address, ptep); 301834ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, mmun_start, mmun_end); 30191e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, 30201e8f889bSDavid Gibson make_huge_pte(vma, new_page, 1)); 30210fe6e20bSNaoya Horiguchi page_remove_rmap(old_page); 3022cd67f0d2SNaoya Horiguchi hugepage_add_new_anon_rmap(new_page, vma, address); 30231e8f889bSDavid Gibson /* Make the old page be freed below */ 30241e8f889bSDavid Gibson new_page = old_page; 30251e8f889bSDavid Gibson } 3026cb900f41SKirill A. Shutemov spin_unlock(ptl); 30272ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 3028ad4404a2SDavidlohr Bueso out_release_all: 30291e8f889bSDavid Gibson page_cache_release(new_page); 3030ad4404a2SDavidlohr Bueso out_release_old: 30311e8f889bSDavid Gibson page_cache_release(old_page); 30328312034fSJoonsoo Kim 3033ad4404a2SDavidlohr Bueso spin_lock(ptl); /* Caller expects lock to be held */ 3034ad4404a2SDavidlohr Bueso return ret; 30351e8f889bSDavid Gibson } 30361e8f889bSDavid Gibson 303704f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */ 3038a5516438SAndi Kleen static struct page *hugetlbfs_pagecache_page(struct hstate *h, 3039a5516438SAndi Kleen struct vm_area_struct *vma, unsigned long address) 304004f2cbe3SMel Gorman { 304104f2cbe3SMel Gorman struct address_space *mapping; 3042e7c4b0bfSAndy Whitcroft pgoff_t idx; 304304f2cbe3SMel Gorman 304404f2cbe3SMel Gorman mapping = vma->vm_file->f_mapping; 3045a5516438SAndi Kleen idx = vma_hugecache_offset(h, vma, address); 304604f2cbe3SMel Gorman 304704f2cbe3SMel Gorman return find_lock_page(mapping, idx); 304804f2cbe3SMel Gorman } 304904f2cbe3SMel Gorman 30503ae77f43SHugh Dickins /* 30513ae77f43SHugh Dickins * Return whether there is a pagecache page to back given address within VMA. 30523ae77f43SHugh Dickins * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page. 30533ae77f43SHugh Dickins */ 30543ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h, 30552a15efc9SHugh Dickins struct vm_area_struct *vma, unsigned long address) 30562a15efc9SHugh Dickins { 30572a15efc9SHugh Dickins struct address_space *mapping; 30582a15efc9SHugh Dickins pgoff_t idx; 30592a15efc9SHugh Dickins struct page *page; 30602a15efc9SHugh Dickins 30612a15efc9SHugh Dickins mapping = vma->vm_file->f_mapping; 30622a15efc9SHugh Dickins idx = vma_hugecache_offset(h, vma, address); 30632a15efc9SHugh Dickins 30642a15efc9SHugh Dickins page = find_get_page(mapping, idx); 30652a15efc9SHugh Dickins if (page) 30662a15efc9SHugh Dickins put_page(page); 30672a15efc9SHugh Dickins return page != NULL; 30682a15efc9SHugh Dickins } 30692a15efc9SHugh Dickins 3070a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, 30718382d914SDavidlohr Bueso struct address_space *mapping, pgoff_t idx, 3072788c7df4SHugh Dickins unsigned long address, pte_t *ptep, unsigned int flags) 3073ac9b9c66SHugh Dickins { 3074a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 3075ac9b9c66SHugh Dickins int ret = VM_FAULT_SIGBUS; 3076409eb8c2SHillf Danton int anon_rmap = 0; 30774c887265SAdam Litke unsigned long size; 30784c887265SAdam Litke struct page *page; 30791e8f889bSDavid Gibson pte_t new_pte; 3080cb900f41SKirill A. Shutemov spinlock_t *ptl; 30814c887265SAdam Litke 308204f2cbe3SMel Gorman /* 308304f2cbe3SMel Gorman * Currently, we are forced to kill the process in the event the 308404f2cbe3SMel Gorman * original mapper has unmapped pages from the child due to a failed 308525985edcSLucas De Marchi * COW. Warn that such a situation has occurred as it may not be obvious 308604f2cbe3SMel Gorman */ 308704f2cbe3SMel Gorman if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { 3088ffb22af5SAndrew Morton pr_warning("PID %d killed due to inadequate hugepage pool\n", 308904f2cbe3SMel Gorman current->pid); 309004f2cbe3SMel Gorman return ret; 309104f2cbe3SMel Gorman } 309204f2cbe3SMel Gorman 30934c887265SAdam Litke /* 30944c887265SAdam Litke * Use page lock to guard against racing truncation 30954c887265SAdam Litke * before we get page_table_lock. 30964c887265SAdam Litke */ 30976bda666aSChristoph Lameter retry: 30986bda666aSChristoph Lameter page = find_lock_page(mapping, idx); 30996bda666aSChristoph Lameter if (!page) { 3100a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 3101ebed4bfcSHugh Dickins if (idx >= size) 3102ebed4bfcSHugh Dickins goto out; 310304f2cbe3SMel Gorman page = alloc_huge_page(vma, address, 0); 31042fc39cecSAdam Litke if (IS_ERR(page)) { 310576dcee75SAneesh Kumar K.V ret = PTR_ERR(page); 310676dcee75SAneesh Kumar K.V if (ret == -ENOMEM) 310776dcee75SAneesh Kumar K.V ret = VM_FAULT_OOM; 310876dcee75SAneesh Kumar K.V else 310976dcee75SAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 31106bda666aSChristoph Lameter goto out; 31116bda666aSChristoph Lameter } 311247ad8475SAndrea Arcangeli clear_huge_page(page, address, pages_per_huge_page(h)); 31130ed361deSNick Piggin __SetPageUptodate(page); 3114bcc54222SNaoya Horiguchi set_page_huge_active(page); 3115ac9b9c66SHugh Dickins 3116f83a275dSMel Gorman if (vma->vm_flags & VM_MAYSHARE) { 31176bda666aSChristoph Lameter int err; 311845c682a6SKen Chen struct inode *inode = mapping->host; 31196bda666aSChristoph Lameter 31206bda666aSChristoph Lameter err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); 31216bda666aSChristoph Lameter if (err) { 31226bda666aSChristoph Lameter put_page(page); 31236bda666aSChristoph Lameter if (err == -EEXIST) 31246bda666aSChristoph Lameter goto retry; 31256bda666aSChristoph Lameter goto out; 31266bda666aSChristoph Lameter } 312707443a85SJoonsoo Kim ClearPagePrivate(page); 312845c682a6SKen Chen 312945c682a6SKen Chen spin_lock(&inode->i_lock); 3130a5516438SAndi Kleen inode->i_blocks += blocks_per_huge_page(h); 313145c682a6SKen Chen spin_unlock(&inode->i_lock); 313223be7468SMel Gorman } else { 31336bda666aSChristoph Lameter lock_page(page); 31340fe6e20bSNaoya Horiguchi if (unlikely(anon_vma_prepare(vma))) { 31350fe6e20bSNaoya Horiguchi ret = VM_FAULT_OOM; 31360fe6e20bSNaoya Horiguchi goto backout_unlocked; 313723be7468SMel Gorman } 3138409eb8c2SHillf Danton anon_rmap = 1; 31390fe6e20bSNaoya Horiguchi } 31400fe6e20bSNaoya Horiguchi } else { 314157303d80SAndy Whitcroft /* 3142998b4382SNaoya Horiguchi * If memory error occurs between mmap() and fault, some process 3143998b4382SNaoya Horiguchi * don't have hwpoisoned swap entry for errored virtual address. 3144998b4382SNaoya Horiguchi * So we need to block hugepage fault by PG_hwpoison bit check. 3145fd6a03edSNaoya Horiguchi */ 3146fd6a03edSNaoya Horiguchi if (unlikely(PageHWPoison(page))) { 3147aa50d3a7SAndi Kleen ret = VM_FAULT_HWPOISON | 3148972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 3149fd6a03edSNaoya Horiguchi goto backout_unlocked; 31506bda666aSChristoph Lameter } 3151998b4382SNaoya Horiguchi } 31521e8f889bSDavid Gibson 315357303d80SAndy Whitcroft /* 315457303d80SAndy Whitcroft * If we are going to COW a private mapping later, we examine the 315557303d80SAndy Whitcroft * pending reservations for this page now. This will ensure that 315657303d80SAndy Whitcroft * any allocations necessary to record that reservation occur outside 315757303d80SAndy Whitcroft * the spinlock. 315857303d80SAndy Whitcroft */ 3159788c7df4SHugh Dickins if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) 31602b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 31612b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 31622b26736cSAndy Whitcroft goto backout_unlocked; 31632b26736cSAndy Whitcroft } 316457303d80SAndy Whitcroft 3165cb900f41SKirill A. Shutemov ptl = huge_pte_lockptr(h, mm, ptep); 3166cb900f41SKirill A. Shutemov spin_lock(ptl); 3167a5516438SAndi Kleen size = i_size_read(mapping->host) >> huge_page_shift(h); 31684c887265SAdam Litke if (idx >= size) 31694c887265SAdam Litke goto backout; 31704c887265SAdam Litke 317183c54070SNick Piggin ret = 0; 31727f2e9525SGerald Schaefer if (!huge_pte_none(huge_ptep_get(ptep))) 31734c887265SAdam Litke goto backout; 31744c887265SAdam Litke 317507443a85SJoonsoo Kim if (anon_rmap) { 317607443a85SJoonsoo Kim ClearPagePrivate(page); 3177409eb8c2SHillf Danton hugepage_add_new_anon_rmap(page, vma, address); 3178ac714904SChoi Gi-yong } else 3179409eb8c2SHillf Danton page_dup_rmap(page); 31801e8f889bSDavid Gibson new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) 31811e8f889bSDavid Gibson && (vma->vm_flags & VM_SHARED))); 31821e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, new_pte); 31831e8f889bSDavid Gibson 3184788c7df4SHugh Dickins if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 31851e8f889bSDavid Gibson /* Optimization, do the COW without a second fault */ 3186cb900f41SKirill A. Shutemov ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page, ptl); 31871e8f889bSDavid Gibson } 31881e8f889bSDavid Gibson 3189cb900f41SKirill A. Shutemov spin_unlock(ptl); 31904c887265SAdam Litke unlock_page(page); 31914c887265SAdam Litke out: 3192ac9b9c66SHugh Dickins return ret; 31934c887265SAdam Litke 31944c887265SAdam Litke backout: 3195cb900f41SKirill A. Shutemov spin_unlock(ptl); 31962b26736cSAndy Whitcroft backout_unlocked: 31974c887265SAdam Litke unlock_page(page); 31984c887265SAdam Litke put_page(page); 31994c887265SAdam Litke goto out; 3200ac9b9c66SHugh Dickins } 3201ac9b9c66SHugh Dickins 32028382d914SDavidlohr Bueso #ifdef CONFIG_SMP 32038382d914SDavidlohr Bueso static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 32048382d914SDavidlohr Bueso struct vm_area_struct *vma, 32058382d914SDavidlohr Bueso struct address_space *mapping, 32068382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 32078382d914SDavidlohr Bueso { 32088382d914SDavidlohr Bueso unsigned long key[2]; 32098382d914SDavidlohr Bueso u32 hash; 32108382d914SDavidlohr Bueso 32118382d914SDavidlohr Bueso if (vma->vm_flags & VM_SHARED) { 32128382d914SDavidlohr Bueso key[0] = (unsigned long) mapping; 32138382d914SDavidlohr Bueso key[1] = idx; 32148382d914SDavidlohr Bueso } else { 32158382d914SDavidlohr Bueso key[0] = (unsigned long) mm; 32168382d914SDavidlohr Bueso key[1] = address >> huge_page_shift(h); 32178382d914SDavidlohr Bueso } 32188382d914SDavidlohr Bueso 32198382d914SDavidlohr Bueso hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0); 32208382d914SDavidlohr Bueso 32218382d914SDavidlohr Bueso return hash & (num_fault_mutexes - 1); 32228382d914SDavidlohr Bueso } 32238382d914SDavidlohr Bueso #else 32248382d914SDavidlohr Bueso /* 32258382d914SDavidlohr Bueso * For uniprocesor systems we always use a single mutex, so just 32268382d914SDavidlohr Bueso * return 0 and avoid the hashing overhead. 32278382d914SDavidlohr Bueso */ 32288382d914SDavidlohr Bueso static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm, 32298382d914SDavidlohr Bueso struct vm_area_struct *vma, 32308382d914SDavidlohr Bueso struct address_space *mapping, 32318382d914SDavidlohr Bueso pgoff_t idx, unsigned long address) 32328382d914SDavidlohr Bueso { 32338382d914SDavidlohr Bueso return 0; 32348382d914SDavidlohr Bueso } 32358382d914SDavidlohr Bueso #endif 32368382d914SDavidlohr Bueso 323786e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3238788c7df4SHugh Dickins unsigned long address, unsigned int flags) 323986e5216fSAdam Litke { 32408382d914SDavidlohr Bueso pte_t *ptep, entry; 3241cb900f41SKirill A. Shutemov spinlock_t *ptl; 32421e8f889bSDavid Gibson int ret; 32438382d914SDavidlohr Bueso u32 hash; 32448382d914SDavidlohr Bueso pgoff_t idx; 32450fe6e20bSNaoya Horiguchi struct page *page = NULL; 324657303d80SAndy Whitcroft struct page *pagecache_page = NULL; 3247a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 32488382d914SDavidlohr Bueso struct address_space *mapping; 32490f792cf9SNaoya Horiguchi int need_wait_lock = 0; 325086e5216fSAdam Litke 32511e16a539SKAMEZAWA Hiroyuki address &= huge_page_mask(h); 32521e16a539SKAMEZAWA Hiroyuki 3253fd6a03edSNaoya Horiguchi ptep = huge_pte_offset(mm, address); 3254fd6a03edSNaoya Horiguchi if (ptep) { 3255fd6a03edSNaoya Horiguchi entry = huge_ptep_get(ptep); 3256290408d4SNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(entry))) { 3257cb900f41SKirill A. Shutemov migration_entry_wait_huge(vma, mm, ptep); 3258290408d4SNaoya Horiguchi return 0; 3259290408d4SNaoya Horiguchi } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) 3260aa50d3a7SAndi Kleen return VM_FAULT_HWPOISON_LARGE | 3261972dc4deSAneesh Kumar K.V VM_FAULT_SET_HINDEX(hstate_index(h)); 3262fd6a03edSNaoya Horiguchi } 3263fd6a03edSNaoya Horiguchi 3264a5516438SAndi Kleen ptep = huge_pte_alloc(mm, address, huge_page_size(h)); 326586e5216fSAdam Litke if (!ptep) 326686e5216fSAdam Litke return VM_FAULT_OOM; 326786e5216fSAdam Litke 32688382d914SDavidlohr Bueso mapping = vma->vm_file->f_mapping; 32698382d914SDavidlohr Bueso idx = vma_hugecache_offset(h, vma, address); 32708382d914SDavidlohr Bueso 32713935baa9SDavid Gibson /* 32723935baa9SDavid Gibson * Serialize hugepage allocation and instantiation, so that we don't 32733935baa9SDavid Gibson * get spurious allocation failures if two CPUs race to instantiate 32743935baa9SDavid Gibson * the same page in the page cache. 32753935baa9SDavid Gibson */ 32768382d914SDavidlohr Bueso hash = fault_mutex_hash(h, mm, vma, mapping, idx, address); 32778382d914SDavidlohr Bueso mutex_lock(&htlb_fault_mutex_table[hash]); 32788382d914SDavidlohr Bueso 32797f2e9525SGerald Schaefer entry = huge_ptep_get(ptep); 32807f2e9525SGerald Schaefer if (huge_pte_none(entry)) { 32818382d914SDavidlohr Bueso ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags); 3282b4d1d99fSDavid Gibson goto out_mutex; 32833935baa9SDavid Gibson } 328486e5216fSAdam Litke 328583c54070SNick Piggin ret = 0; 32861e8f889bSDavid Gibson 328757303d80SAndy Whitcroft /* 32880f792cf9SNaoya Horiguchi * entry could be a migration/hwpoison entry at this point, so this 32890f792cf9SNaoya Horiguchi * check prevents the kernel from going below assuming that we have 32900f792cf9SNaoya Horiguchi * a active hugepage in pagecache. This goto expects the 2nd page fault, 32910f792cf9SNaoya Horiguchi * and is_hugetlb_entry_(migration|hwpoisoned) check will properly 32920f792cf9SNaoya Horiguchi * handle it. 32930f792cf9SNaoya Horiguchi */ 32940f792cf9SNaoya Horiguchi if (!pte_present(entry)) 32950f792cf9SNaoya Horiguchi goto out_mutex; 32960f792cf9SNaoya Horiguchi 32970f792cf9SNaoya Horiguchi /* 329857303d80SAndy Whitcroft * If we are going to COW the mapping later, we examine the pending 329957303d80SAndy Whitcroft * reservations for this page now. This will ensure that any 330057303d80SAndy Whitcroft * allocations necessary to record that reservation occur outside the 330157303d80SAndy Whitcroft * spinlock. For private mappings, we also lookup the pagecache 330257303d80SAndy Whitcroft * page now as it is used to determine if a reservation has been 330357303d80SAndy Whitcroft * consumed. 330457303d80SAndy Whitcroft */ 3305106c992aSGerald Schaefer if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { 33062b26736cSAndy Whitcroft if (vma_needs_reservation(h, vma, address) < 0) { 33072b26736cSAndy Whitcroft ret = VM_FAULT_OOM; 3308b4d1d99fSDavid Gibson goto out_mutex; 33092b26736cSAndy Whitcroft } 331057303d80SAndy Whitcroft 3311f83a275dSMel Gorman if (!(vma->vm_flags & VM_MAYSHARE)) 331257303d80SAndy Whitcroft pagecache_page = hugetlbfs_pagecache_page(h, 331357303d80SAndy Whitcroft vma, address); 331457303d80SAndy Whitcroft } 331557303d80SAndy Whitcroft 33160f792cf9SNaoya Horiguchi ptl = huge_pte_lock(h, mm, ptep); 33170fe6e20bSNaoya Horiguchi 33181e8f889bSDavid Gibson /* Check for a racing update before calling hugetlb_cow */ 3319b4d1d99fSDavid Gibson if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) 3320cb900f41SKirill A. Shutemov goto out_ptl; 3321b4d1d99fSDavid Gibson 33220f792cf9SNaoya Horiguchi /* 33230f792cf9SNaoya Horiguchi * hugetlb_cow() requires page locks of pte_page(entry) and 33240f792cf9SNaoya Horiguchi * pagecache_page, so here we need take the former one 33250f792cf9SNaoya Horiguchi * when page != pagecache_page or !pagecache_page. 33260f792cf9SNaoya Horiguchi */ 33270f792cf9SNaoya Horiguchi page = pte_page(entry); 33280f792cf9SNaoya Horiguchi if (page != pagecache_page) 33290f792cf9SNaoya Horiguchi if (!trylock_page(page)) { 33300f792cf9SNaoya Horiguchi need_wait_lock = 1; 33310f792cf9SNaoya Horiguchi goto out_ptl; 33320f792cf9SNaoya Horiguchi } 33330f792cf9SNaoya Horiguchi 33340f792cf9SNaoya Horiguchi get_page(page); 3335b4d1d99fSDavid Gibson 3336788c7df4SHugh Dickins if (flags & FAULT_FLAG_WRITE) { 3337106c992aSGerald Schaefer if (!huge_pte_write(entry)) { 333857303d80SAndy Whitcroft ret = hugetlb_cow(mm, vma, address, ptep, entry, 3339cb900f41SKirill A. Shutemov pagecache_page, ptl); 33400f792cf9SNaoya Horiguchi goto out_put_page; 3341b4d1d99fSDavid Gibson } 3342106c992aSGerald Schaefer entry = huge_pte_mkdirty(entry); 3343b4d1d99fSDavid Gibson } 3344b4d1d99fSDavid Gibson entry = pte_mkyoung(entry); 3345788c7df4SHugh Dickins if (huge_ptep_set_access_flags(vma, address, ptep, entry, 3346788c7df4SHugh Dickins flags & FAULT_FLAG_WRITE)) 33474b3073e1SRussell King update_mmu_cache(vma, address, ptep); 33480f792cf9SNaoya Horiguchi out_put_page: 33490f792cf9SNaoya Horiguchi if (page != pagecache_page) 33500f792cf9SNaoya Horiguchi unlock_page(page); 33510f792cf9SNaoya Horiguchi put_page(page); 3352cb900f41SKirill A. Shutemov out_ptl: 3353cb900f41SKirill A. Shutemov spin_unlock(ptl); 335457303d80SAndy Whitcroft 335557303d80SAndy Whitcroft if (pagecache_page) { 335657303d80SAndy Whitcroft unlock_page(pagecache_page); 335757303d80SAndy Whitcroft put_page(pagecache_page); 335857303d80SAndy Whitcroft } 3359b4d1d99fSDavid Gibson out_mutex: 33608382d914SDavidlohr Bueso mutex_unlock(&htlb_fault_mutex_table[hash]); 33610f792cf9SNaoya Horiguchi /* 33620f792cf9SNaoya Horiguchi * Generally it's safe to hold refcount during waiting page lock. But 33630f792cf9SNaoya Horiguchi * here we just wait to defer the next page fault to avoid busy loop and 33640f792cf9SNaoya Horiguchi * the page is not used after unlocked before returning from the current 33650f792cf9SNaoya Horiguchi * page fault. So we are safe from accessing freed page, even if we wait 33660f792cf9SNaoya Horiguchi * here without taking refcount. 33670f792cf9SNaoya Horiguchi */ 33680f792cf9SNaoya Horiguchi if (need_wait_lock) 33690f792cf9SNaoya Horiguchi wait_on_page_locked(page); 33701e8f889bSDavid Gibson return ret; 337186e5216fSAdam Litke } 337286e5216fSAdam Litke 337328a35716SMichel Lespinasse long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, 337463551ae0SDavid Gibson struct page **pages, struct vm_area_struct **vmas, 337528a35716SMichel Lespinasse unsigned long *position, unsigned long *nr_pages, 337628a35716SMichel Lespinasse long i, unsigned int flags) 337763551ae0SDavid Gibson { 3378d5d4b0aaSChen, Kenneth W unsigned long pfn_offset; 3379d5d4b0aaSChen, Kenneth W unsigned long vaddr = *position; 338028a35716SMichel Lespinasse unsigned long remainder = *nr_pages; 3381a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 338263551ae0SDavid Gibson 338363551ae0SDavid Gibson while (vaddr < vma->vm_end && remainder) { 338463551ae0SDavid Gibson pte_t *pte; 3385cb900f41SKirill A. Shutemov spinlock_t *ptl = NULL; 33862a15efc9SHugh Dickins int absent; 338763551ae0SDavid Gibson struct page *page; 338863551ae0SDavid Gibson 33894c887265SAdam Litke /* 339002057967SDavid Rientjes * If we have a pending SIGKILL, don't keep faulting pages and 339102057967SDavid Rientjes * potentially allocating memory. 339202057967SDavid Rientjes */ 339302057967SDavid Rientjes if (unlikely(fatal_signal_pending(current))) { 339402057967SDavid Rientjes remainder = 0; 339502057967SDavid Rientjes break; 339602057967SDavid Rientjes } 339702057967SDavid Rientjes 339802057967SDavid Rientjes /* 33994c887265SAdam Litke * Some archs (sparc64, sh*) have multiple pte_ts to 34002a15efc9SHugh Dickins * each hugepage. We have to make sure we get the 34014c887265SAdam Litke * first, for the page indexing below to work. 3402cb900f41SKirill A. Shutemov * 3403cb900f41SKirill A. Shutemov * Note that page table lock is not held when pte is null. 34044c887265SAdam Litke */ 3405a5516438SAndi Kleen pte = huge_pte_offset(mm, vaddr & huge_page_mask(h)); 3406cb900f41SKirill A. Shutemov if (pte) 3407cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, pte); 34082a15efc9SHugh Dickins absent = !pte || huge_pte_none(huge_ptep_get(pte)); 340963551ae0SDavid Gibson 34102a15efc9SHugh Dickins /* 34112a15efc9SHugh Dickins * When coredumping, it suits get_dump_page if we just return 34123ae77f43SHugh Dickins * an error where there's an empty slot with no huge pagecache 34133ae77f43SHugh Dickins * to back it. This way, we avoid allocating a hugepage, and 34143ae77f43SHugh Dickins * the sparse dumpfile avoids allocating disk blocks, but its 34153ae77f43SHugh Dickins * huge holes still show up with zeroes where they need to be. 34162a15efc9SHugh Dickins */ 34173ae77f43SHugh Dickins if (absent && (flags & FOLL_DUMP) && 34183ae77f43SHugh Dickins !hugetlbfs_pagecache_present(h, vma, vaddr)) { 3419cb900f41SKirill A. Shutemov if (pte) 3420cb900f41SKirill A. Shutemov spin_unlock(ptl); 34212a15efc9SHugh Dickins remainder = 0; 34222a15efc9SHugh Dickins break; 34232a15efc9SHugh Dickins } 34242a15efc9SHugh Dickins 34259cc3a5bdSNaoya Horiguchi /* 34269cc3a5bdSNaoya Horiguchi * We need call hugetlb_fault for both hugepages under migration 34279cc3a5bdSNaoya Horiguchi * (in which case hugetlb_fault waits for the migration,) and 34289cc3a5bdSNaoya Horiguchi * hwpoisoned hugepages (in which case we need to prevent the 34299cc3a5bdSNaoya Horiguchi * caller from accessing to them.) In order to do this, we use 34309cc3a5bdSNaoya Horiguchi * here is_swap_pte instead of is_hugetlb_entry_migration and 34319cc3a5bdSNaoya Horiguchi * is_hugetlb_entry_hwpoisoned. This is because it simply covers 34329cc3a5bdSNaoya Horiguchi * both cases, and because we can't follow correct pages 34339cc3a5bdSNaoya Horiguchi * directly from any kind of swap entries. 34349cc3a5bdSNaoya Horiguchi */ 34359cc3a5bdSNaoya Horiguchi if (absent || is_swap_pte(huge_ptep_get(pte)) || 3436106c992aSGerald Schaefer ((flags & FOLL_WRITE) && 3437106c992aSGerald Schaefer !huge_pte_write(huge_ptep_get(pte)))) { 34384c887265SAdam Litke int ret; 34394c887265SAdam Litke 3440cb900f41SKirill A. Shutemov if (pte) 3441cb900f41SKirill A. Shutemov spin_unlock(ptl); 34422a15efc9SHugh Dickins ret = hugetlb_fault(mm, vma, vaddr, 34432a15efc9SHugh Dickins (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0); 3444a89182c7SAdam Litke if (!(ret & VM_FAULT_ERROR)) 34454c887265SAdam Litke continue; 34464c887265SAdam Litke 34471c59827dSHugh Dickins remainder = 0; 34481c59827dSHugh Dickins break; 34491c59827dSHugh Dickins } 345063551ae0SDavid Gibson 3451a5516438SAndi Kleen pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; 34527f2e9525SGerald Schaefer page = pte_page(huge_ptep_get(pte)); 3453d5d4b0aaSChen, Kenneth W same_page: 3454d6692183SChen, Kenneth W if (pages) { 345569d177c2SAndy Whitcroft pages[i] = mem_map_offset(page, pfn_offset); 3456a0368d4eSAndrea Arcangeli get_page_foll(pages[i]); 3457d6692183SChen, Kenneth W } 345863551ae0SDavid Gibson 345963551ae0SDavid Gibson if (vmas) 346063551ae0SDavid Gibson vmas[i] = vma; 346163551ae0SDavid Gibson 346263551ae0SDavid Gibson vaddr += PAGE_SIZE; 3463d5d4b0aaSChen, Kenneth W ++pfn_offset; 346463551ae0SDavid Gibson --remainder; 346563551ae0SDavid Gibson ++i; 3466d5d4b0aaSChen, Kenneth W if (vaddr < vma->vm_end && remainder && 3467a5516438SAndi Kleen pfn_offset < pages_per_huge_page(h)) { 3468d5d4b0aaSChen, Kenneth W /* 3469d5d4b0aaSChen, Kenneth W * We use pfn_offset to avoid touching the pageframes 3470d5d4b0aaSChen, Kenneth W * of this compound page. 3471d5d4b0aaSChen, Kenneth W */ 3472d5d4b0aaSChen, Kenneth W goto same_page; 3473d5d4b0aaSChen, Kenneth W } 3474cb900f41SKirill A. Shutemov spin_unlock(ptl); 347563551ae0SDavid Gibson } 347628a35716SMichel Lespinasse *nr_pages = remainder; 347763551ae0SDavid Gibson *position = vaddr; 347863551ae0SDavid Gibson 34792a15efc9SHugh Dickins return i ? i : -EFAULT; 348063551ae0SDavid Gibson } 34818f860591SZhang, Yanmin 34827da4d641SPeter Zijlstra unsigned long hugetlb_change_protection(struct vm_area_struct *vma, 34838f860591SZhang, Yanmin unsigned long address, unsigned long end, pgprot_t newprot) 34848f860591SZhang, Yanmin { 34858f860591SZhang, Yanmin struct mm_struct *mm = vma->vm_mm; 34868f860591SZhang, Yanmin unsigned long start = address; 34878f860591SZhang, Yanmin pte_t *ptep; 34888f860591SZhang, Yanmin pte_t pte; 3489a5516438SAndi Kleen struct hstate *h = hstate_vma(vma); 34907da4d641SPeter Zijlstra unsigned long pages = 0; 34918f860591SZhang, Yanmin 34928f860591SZhang, Yanmin BUG_ON(address >= end); 34938f860591SZhang, Yanmin flush_cache_range(vma, address, end); 34948f860591SZhang, Yanmin 3495a5338093SRik van Riel mmu_notifier_invalidate_range_start(mm, start, end); 349683cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 3497a5516438SAndi Kleen for (; address < end; address += huge_page_size(h)) { 3498cb900f41SKirill A. Shutemov spinlock_t *ptl; 34998f860591SZhang, Yanmin ptep = huge_pte_offset(mm, address); 35008f860591SZhang, Yanmin if (!ptep) 35018f860591SZhang, Yanmin continue; 3502cb900f41SKirill A. Shutemov ptl = huge_pte_lock(h, mm, ptep); 35037da4d641SPeter Zijlstra if (huge_pmd_unshare(mm, &address, ptep)) { 35047da4d641SPeter Zijlstra pages++; 3505cb900f41SKirill A. Shutemov spin_unlock(ptl); 350639dde65cSChen, Kenneth W continue; 35077da4d641SPeter Zijlstra } 3508a8bda28dSNaoya Horiguchi pte = huge_ptep_get(ptep); 3509a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { 3510a8bda28dSNaoya Horiguchi spin_unlock(ptl); 3511a8bda28dSNaoya Horiguchi continue; 3512a8bda28dSNaoya Horiguchi } 3513a8bda28dSNaoya Horiguchi if (unlikely(is_hugetlb_entry_migration(pte))) { 3514a8bda28dSNaoya Horiguchi swp_entry_t entry = pte_to_swp_entry(pte); 3515a8bda28dSNaoya Horiguchi 3516a8bda28dSNaoya Horiguchi if (is_write_migration_entry(entry)) { 3517a8bda28dSNaoya Horiguchi pte_t newpte; 3518a8bda28dSNaoya Horiguchi 3519a8bda28dSNaoya Horiguchi make_migration_entry_read(&entry); 3520a8bda28dSNaoya Horiguchi newpte = swp_entry_to_pte(entry); 3521a8bda28dSNaoya Horiguchi set_huge_pte_at(mm, address, ptep, newpte); 3522a8bda28dSNaoya Horiguchi pages++; 3523a8bda28dSNaoya Horiguchi } 3524a8bda28dSNaoya Horiguchi spin_unlock(ptl); 3525a8bda28dSNaoya Horiguchi continue; 3526a8bda28dSNaoya Horiguchi } 3527a8bda28dSNaoya Horiguchi if (!huge_pte_none(pte)) { 35288f860591SZhang, Yanmin pte = huge_ptep_get_and_clear(mm, address, ptep); 3529106c992aSGerald Schaefer pte = pte_mkhuge(huge_pte_modify(pte, newprot)); 3530be7517d6STony Lu pte = arch_make_huge_pte(pte, vma, NULL, 0); 35318f860591SZhang, Yanmin set_huge_pte_at(mm, address, ptep, pte); 35327da4d641SPeter Zijlstra pages++; 35338f860591SZhang, Yanmin } 3534cb900f41SKirill A. Shutemov spin_unlock(ptl); 35358f860591SZhang, Yanmin } 3536d833352aSMel Gorman /* 3537c8c06efaSDavidlohr Bueso * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare 3538d833352aSMel Gorman * may have cleared our pud entry and done put_page on the page table: 3539c8c06efaSDavidlohr Bueso * once we release i_mmap_rwsem, another task can do the final put_page 3540d833352aSMel Gorman * and that page table be reused and filled with junk. 3541d833352aSMel Gorman */ 35428f860591SZhang, Yanmin flush_tlb_range(vma, start, end); 354334ee645eSJoerg Roedel mmu_notifier_invalidate_range(mm, start, end); 354483cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 3545a5338093SRik van Riel mmu_notifier_invalidate_range_end(mm, start, end); 35467da4d641SPeter Zijlstra 35477da4d641SPeter Zijlstra return pages << h->order; 35488f860591SZhang, Yanmin } 35498f860591SZhang, Yanmin 3550a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode, 3551a1e78772SMel Gorman long from, long to, 35525a6fe125SMel Gorman struct vm_area_struct *vma, 3553ca16d140SKOSAKI Motohiro vm_flags_t vm_flags) 3554e4e574b7SAdam Litke { 355517c9d12eSMel Gorman long ret, chg; 3556a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 355790481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 35589119a41eSJoonsoo Kim struct resv_map *resv_map; 35591c5ecae3SMike Kravetz long gbl_reserve; 3560e4e574b7SAdam Litke 3561a1e78772SMel Gorman /* 356217c9d12eSMel Gorman * Only apply hugepage reservation if asked. At fault time, an 356317c9d12eSMel Gorman * attempt will be made for VM_NORESERVE to allocate a page 356490481622SDavid Gibson * without using reserves 356517c9d12eSMel Gorman */ 3566ca16d140SKOSAKI Motohiro if (vm_flags & VM_NORESERVE) 356717c9d12eSMel Gorman return 0; 356817c9d12eSMel Gorman 356917c9d12eSMel Gorman /* 3570a1e78772SMel Gorman * Shared mappings base their reservation on the number of pages that 3571a1e78772SMel Gorman * are already allocated on behalf of the file. Private mappings need 3572a1e78772SMel Gorman * to reserve the full area even if read-only as mprotect() may be 3573a1e78772SMel Gorman * called to make the mapping read-write. Assume !vma is a shm mapping 3574a1e78772SMel Gorman */ 35759119a41eSJoonsoo Kim if (!vma || vma->vm_flags & VM_MAYSHARE) { 35764e35f483SJoonsoo Kim resv_map = inode_resv_map(inode); 35779119a41eSJoonsoo Kim 35781406ec9bSJoonsoo Kim chg = region_chg(resv_map, from, to); 35799119a41eSJoonsoo Kim 35809119a41eSJoonsoo Kim } else { 35819119a41eSJoonsoo Kim resv_map = resv_map_alloc(); 35825a6fe125SMel Gorman if (!resv_map) 35835a6fe125SMel Gorman return -ENOMEM; 35845a6fe125SMel Gorman 358517c9d12eSMel Gorman chg = to - from; 358617c9d12eSMel Gorman 35875a6fe125SMel Gorman set_vma_resv_map(vma, resv_map); 35885a6fe125SMel Gorman set_vma_resv_flags(vma, HPAGE_RESV_OWNER); 35895a6fe125SMel Gorman } 35905a6fe125SMel Gorman 3591c50ac050SDave Hansen if (chg < 0) { 3592c50ac050SDave Hansen ret = chg; 3593c50ac050SDave Hansen goto out_err; 3594c50ac050SDave Hansen } 359517c9d12eSMel Gorman 35961c5ecae3SMike Kravetz /* 35971c5ecae3SMike Kravetz * There must be enough pages in the subpool for the mapping. If 35981c5ecae3SMike Kravetz * the subpool has a minimum size, there may be some global 35991c5ecae3SMike Kravetz * reservations already in place (gbl_reserve). 36001c5ecae3SMike Kravetz */ 36011c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_get_pages(spool, chg); 36021c5ecae3SMike Kravetz if (gbl_reserve < 0) { 3603c50ac050SDave Hansen ret = -ENOSPC; 3604c50ac050SDave Hansen goto out_err; 3605c50ac050SDave Hansen } 360617c9d12eSMel Gorman 360717c9d12eSMel Gorman /* 360817c9d12eSMel Gorman * Check enough hugepages are available for the reservation. 360990481622SDavid Gibson * Hand the pages back to the subpool if there are not 361017c9d12eSMel Gorman */ 36111c5ecae3SMike Kravetz ret = hugetlb_acct_memory(h, gbl_reserve); 361217c9d12eSMel Gorman if (ret < 0) { 36131c5ecae3SMike Kravetz /* put back original number of pages, chg */ 36141c5ecae3SMike Kravetz (void)hugepage_subpool_put_pages(spool, chg); 3615c50ac050SDave Hansen goto out_err; 361617c9d12eSMel Gorman } 361717c9d12eSMel Gorman 361817c9d12eSMel Gorman /* 361917c9d12eSMel Gorman * Account for the reservations made. Shared mappings record regions 362017c9d12eSMel Gorman * that have reservations as they are shared by multiple VMAs. 362117c9d12eSMel Gorman * When the last VMA disappears, the region map says how much 362217c9d12eSMel Gorman * the reservation was and the page cache tells how much of 362317c9d12eSMel Gorman * the reservation was consumed. Private mappings are per-VMA and 362417c9d12eSMel Gorman * only the consumed reservations are tracked. When the VMA 362517c9d12eSMel Gorman * disappears, the original reservation is the VMA size and the 362617c9d12eSMel Gorman * consumed reservations are stored in the map. Hence, nothing 362717c9d12eSMel Gorman * else has to be done for private mappings here 362817c9d12eSMel Gorman */ 3629f83a275dSMel Gorman if (!vma || vma->vm_flags & VM_MAYSHARE) 36301406ec9bSJoonsoo Kim region_add(resv_map, from, to); 3631a43a8c39SChen, Kenneth W return 0; 3632c50ac050SDave Hansen out_err: 3633f031dd27SJoonsoo Kim if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 3634f031dd27SJoonsoo Kim kref_put(&resv_map->refs, resv_map_release); 3635c50ac050SDave Hansen return ret; 3636a43a8c39SChen, Kenneth W } 3637a43a8c39SChen, Kenneth W 3638a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) 3639a43a8c39SChen, Kenneth W { 3640a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 36414e35f483SJoonsoo Kim struct resv_map *resv_map = inode_resv_map(inode); 36429119a41eSJoonsoo Kim long chg = 0; 364390481622SDavid Gibson struct hugepage_subpool *spool = subpool_inode(inode); 36441c5ecae3SMike Kravetz long gbl_reserve; 364545c682a6SKen Chen 36469119a41eSJoonsoo Kim if (resv_map) 36471406ec9bSJoonsoo Kim chg = region_truncate(resv_map, offset); 364845c682a6SKen Chen spin_lock(&inode->i_lock); 3649e4c6f8beSEric Sandeen inode->i_blocks -= (blocks_per_huge_page(h) * freed); 365045c682a6SKen Chen spin_unlock(&inode->i_lock); 365145c682a6SKen Chen 36521c5ecae3SMike Kravetz /* 36531c5ecae3SMike Kravetz * If the subpool has a minimum size, the number of global 36541c5ecae3SMike Kravetz * reservations to be released may be adjusted. 36551c5ecae3SMike Kravetz */ 36561c5ecae3SMike Kravetz gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); 36571c5ecae3SMike Kravetz hugetlb_acct_memory(h, -gbl_reserve); 3658a43a8c39SChen, Kenneth W } 365993f70f90SNaoya Horiguchi 36603212b535SSteve Capper #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE 36613212b535SSteve Capper static unsigned long page_table_shareable(struct vm_area_struct *svma, 36623212b535SSteve Capper struct vm_area_struct *vma, 36633212b535SSteve Capper unsigned long addr, pgoff_t idx) 36643212b535SSteve Capper { 36653212b535SSteve Capper unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + 36663212b535SSteve Capper svma->vm_start; 36673212b535SSteve Capper unsigned long sbase = saddr & PUD_MASK; 36683212b535SSteve Capper unsigned long s_end = sbase + PUD_SIZE; 36693212b535SSteve Capper 36703212b535SSteve Capper /* Allow segments to share if only one is marked locked */ 36713212b535SSteve Capper unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED; 36723212b535SSteve Capper unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED; 36733212b535SSteve Capper 36743212b535SSteve Capper /* 36753212b535SSteve Capper * match the virtual addresses, permission and the alignment of the 36763212b535SSteve Capper * page table page. 36773212b535SSteve Capper */ 36783212b535SSteve Capper if (pmd_index(addr) != pmd_index(saddr) || 36793212b535SSteve Capper vm_flags != svm_flags || 36803212b535SSteve Capper sbase < svma->vm_start || svma->vm_end < s_end) 36813212b535SSteve Capper return 0; 36823212b535SSteve Capper 36833212b535SSteve Capper return saddr; 36843212b535SSteve Capper } 36853212b535SSteve Capper 36863212b535SSteve Capper static int vma_shareable(struct vm_area_struct *vma, unsigned long addr) 36873212b535SSteve Capper { 36883212b535SSteve Capper unsigned long base = addr & PUD_MASK; 36893212b535SSteve Capper unsigned long end = base + PUD_SIZE; 36903212b535SSteve Capper 36913212b535SSteve Capper /* 36923212b535SSteve Capper * check on proper vm_flags and page table alignment 36933212b535SSteve Capper */ 36943212b535SSteve Capper if (vma->vm_flags & VM_MAYSHARE && 36953212b535SSteve Capper vma->vm_start <= base && end <= vma->vm_end) 36963212b535SSteve Capper return 1; 36973212b535SSteve Capper return 0; 36983212b535SSteve Capper } 36993212b535SSteve Capper 37003212b535SSteve Capper /* 37013212b535SSteve Capper * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() 37023212b535SSteve Capper * and returns the corresponding pte. While this is not necessary for the 37033212b535SSteve Capper * !shared pmd case because we can allocate the pmd later as well, it makes the 37043212b535SSteve Capper * code much cleaner. pmd allocation is essential for the shared case because 3705c8c06efaSDavidlohr Bueso * pud has to be populated inside the same i_mmap_rwsem section - otherwise 37063212b535SSteve Capper * racing tasks could either miss the sharing (see huge_pte_offset) or select a 37073212b535SSteve Capper * bad pmd for sharing. 37083212b535SSteve Capper */ 37093212b535SSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 37103212b535SSteve Capper { 37113212b535SSteve Capper struct vm_area_struct *vma = find_vma(mm, addr); 37123212b535SSteve Capper struct address_space *mapping = vma->vm_file->f_mapping; 37133212b535SSteve Capper pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + 37143212b535SSteve Capper vma->vm_pgoff; 37153212b535SSteve Capper struct vm_area_struct *svma; 37163212b535SSteve Capper unsigned long saddr; 37173212b535SSteve Capper pte_t *spte = NULL; 37183212b535SSteve Capper pte_t *pte; 3719cb900f41SKirill A. Shutemov spinlock_t *ptl; 37203212b535SSteve Capper 37213212b535SSteve Capper if (!vma_shareable(vma, addr)) 37223212b535SSteve Capper return (pte_t *)pmd_alloc(mm, pud, addr); 37233212b535SSteve Capper 372483cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 37253212b535SSteve Capper vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { 37263212b535SSteve Capper if (svma == vma) 37273212b535SSteve Capper continue; 37283212b535SSteve Capper 37293212b535SSteve Capper saddr = page_table_shareable(svma, vma, addr, idx); 37303212b535SSteve Capper if (saddr) { 37313212b535SSteve Capper spte = huge_pte_offset(svma->vm_mm, saddr); 37323212b535SSteve Capper if (spte) { 3733dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 37343212b535SSteve Capper get_page(virt_to_page(spte)); 37353212b535SSteve Capper break; 37363212b535SSteve Capper } 37373212b535SSteve Capper } 37383212b535SSteve Capper } 37393212b535SSteve Capper 37403212b535SSteve Capper if (!spte) 37413212b535SSteve Capper goto out; 37423212b535SSteve Capper 3743cb900f41SKirill A. Shutemov ptl = huge_pte_lockptr(hstate_vma(vma), mm, spte); 3744cb900f41SKirill A. Shutemov spin_lock(ptl); 3745dc6c9a35SKirill A. Shutemov if (pud_none(*pud)) { 37463212b535SSteve Capper pud_populate(mm, pud, 37473212b535SSteve Capper (pmd_t *)((unsigned long)spte & PAGE_MASK)); 3748dc6c9a35SKirill A. Shutemov } else { 37493212b535SSteve Capper put_page(virt_to_page(spte)); 3750dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 3751dc6c9a35SKirill A. Shutemov } 3752cb900f41SKirill A. Shutemov spin_unlock(ptl); 37533212b535SSteve Capper out: 37543212b535SSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 375583cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 37563212b535SSteve Capper return pte; 37573212b535SSteve Capper } 37583212b535SSteve Capper 37593212b535SSteve Capper /* 37603212b535SSteve Capper * unmap huge page backed by shared pte. 37613212b535SSteve Capper * 37623212b535SSteve Capper * Hugetlb pte page is ref counted at the time of mapping. If pte is shared 37633212b535SSteve Capper * indicated by page_count > 1, unmap is achieved by clearing pud and 37643212b535SSteve Capper * decrementing the ref count. If count == 1, the pte page is not shared. 37653212b535SSteve Capper * 3766cb900f41SKirill A. Shutemov * called with page table lock held. 37673212b535SSteve Capper * 37683212b535SSteve Capper * returns: 1 successfully unmapped a shared pte page 37693212b535SSteve Capper * 0 the underlying pte page is not shared, or it is the last user 37703212b535SSteve Capper */ 37713212b535SSteve Capper int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 37723212b535SSteve Capper { 37733212b535SSteve Capper pgd_t *pgd = pgd_offset(mm, *addr); 37743212b535SSteve Capper pud_t *pud = pud_offset(pgd, *addr); 37753212b535SSteve Capper 37763212b535SSteve Capper BUG_ON(page_count(virt_to_page(ptep)) == 0); 37773212b535SSteve Capper if (page_count(virt_to_page(ptep)) == 1) 37783212b535SSteve Capper return 0; 37793212b535SSteve Capper 37803212b535SSteve Capper pud_clear(pud); 37813212b535SSteve Capper put_page(virt_to_page(ptep)); 3782dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(mm); 37833212b535SSteve Capper *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE; 37843212b535SSteve Capper return 1; 37853212b535SSteve Capper } 37869e5fc74cSSteve Capper #define want_pmd_share() (1) 37879e5fc74cSSteve Capper #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 37889e5fc74cSSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) 37899e5fc74cSSteve Capper { 37909e5fc74cSSteve Capper return NULL; 37919e5fc74cSSteve Capper } 3792e81f2d22SZhang Zhen 3793e81f2d22SZhang Zhen int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) 3794e81f2d22SZhang Zhen { 3795e81f2d22SZhang Zhen return 0; 3796e81f2d22SZhang Zhen } 37979e5fc74cSSteve Capper #define want_pmd_share() (0) 37983212b535SSteve Capper #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */ 37993212b535SSteve Capper 38009e5fc74cSSteve Capper #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 38019e5fc74cSSteve Capper pte_t *huge_pte_alloc(struct mm_struct *mm, 38029e5fc74cSSteve Capper unsigned long addr, unsigned long sz) 38039e5fc74cSSteve Capper { 38049e5fc74cSSteve Capper pgd_t *pgd; 38059e5fc74cSSteve Capper pud_t *pud; 38069e5fc74cSSteve Capper pte_t *pte = NULL; 38079e5fc74cSSteve Capper 38089e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 38099e5fc74cSSteve Capper pud = pud_alloc(mm, pgd, addr); 38109e5fc74cSSteve Capper if (pud) { 38119e5fc74cSSteve Capper if (sz == PUD_SIZE) { 38129e5fc74cSSteve Capper pte = (pte_t *)pud; 38139e5fc74cSSteve Capper } else { 38149e5fc74cSSteve Capper BUG_ON(sz != PMD_SIZE); 38159e5fc74cSSteve Capper if (want_pmd_share() && pud_none(*pud)) 38169e5fc74cSSteve Capper pte = huge_pmd_share(mm, addr, pud); 38179e5fc74cSSteve Capper else 38189e5fc74cSSteve Capper pte = (pte_t *)pmd_alloc(mm, pud, addr); 38199e5fc74cSSteve Capper } 38209e5fc74cSSteve Capper } 38219e5fc74cSSteve Capper BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte)); 38229e5fc74cSSteve Capper 38239e5fc74cSSteve Capper return pte; 38249e5fc74cSSteve Capper } 38259e5fc74cSSteve Capper 38269e5fc74cSSteve Capper pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) 38279e5fc74cSSteve Capper { 38289e5fc74cSSteve Capper pgd_t *pgd; 38299e5fc74cSSteve Capper pud_t *pud; 38309e5fc74cSSteve Capper pmd_t *pmd = NULL; 38319e5fc74cSSteve Capper 38329e5fc74cSSteve Capper pgd = pgd_offset(mm, addr); 38339e5fc74cSSteve Capper if (pgd_present(*pgd)) { 38349e5fc74cSSteve Capper pud = pud_offset(pgd, addr); 38359e5fc74cSSteve Capper if (pud_present(*pud)) { 38369e5fc74cSSteve Capper if (pud_huge(*pud)) 38379e5fc74cSSteve Capper return (pte_t *)pud; 38389e5fc74cSSteve Capper pmd = pmd_offset(pud, addr); 38399e5fc74cSSteve Capper } 38409e5fc74cSSteve Capper } 38419e5fc74cSSteve Capper return (pte_t *) pmd; 38429e5fc74cSSteve Capper } 38439e5fc74cSSteve Capper 384461f77edaSNaoya Horiguchi #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 384561f77edaSNaoya Horiguchi 384661f77edaSNaoya Horiguchi /* 384761f77edaSNaoya Horiguchi * These functions are overwritable if your architecture needs its own 384861f77edaSNaoya Horiguchi * behavior. 384961f77edaSNaoya Horiguchi */ 385061f77edaSNaoya Horiguchi struct page * __weak 385161f77edaSNaoya Horiguchi follow_huge_addr(struct mm_struct *mm, unsigned long address, 385261f77edaSNaoya Horiguchi int write) 385361f77edaSNaoya Horiguchi { 385461f77edaSNaoya Horiguchi return ERR_PTR(-EINVAL); 385561f77edaSNaoya Horiguchi } 385661f77edaSNaoya Horiguchi 385761f77edaSNaoya Horiguchi struct page * __weak 38589e5fc74cSSteve Capper follow_huge_pmd(struct mm_struct *mm, unsigned long address, 3859e66f17ffSNaoya Horiguchi pmd_t *pmd, int flags) 38609e5fc74cSSteve Capper { 3861e66f17ffSNaoya Horiguchi struct page *page = NULL; 3862e66f17ffSNaoya Horiguchi spinlock_t *ptl; 3863e66f17ffSNaoya Horiguchi retry: 3864e66f17ffSNaoya Horiguchi ptl = pmd_lockptr(mm, pmd); 3865e66f17ffSNaoya Horiguchi spin_lock(ptl); 3866e66f17ffSNaoya Horiguchi /* 3867e66f17ffSNaoya Horiguchi * make sure that the address range covered by this pmd is not 3868e66f17ffSNaoya Horiguchi * unmapped from other threads. 3869e66f17ffSNaoya Horiguchi */ 3870e66f17ffSNaoya Horiguchi if (!pmd_huge(*pmd)) 3871e66f17ffSNaoya Horiguchi goto out; 3872e66f17ffSNaoya Horiguchi if (pmd_present(*pmd)) { 387397534127SGerald Schaefer page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT); 3874e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 3875e66f17ffSNaoya Horiguchi get_page(page); 3876e66f17ffSNaoya Horiguchi } else { 3877e66f17ffSNaoya Horiguchi if (is_hugetlb_entry_migration(huge_ptep_get((pte_t *)pmd))) { 3878e66f17ffSNaoya Horiguchi spin_unlock(ptl); 3879e66f17ffSNaoya Horiguchi __migration_entry_wait(mm, (pte_t *)pmd, ptl); 3880e66f17ffSNaoya Horiguchi goto retry; 3881e66f17ffSNaoya Horiguchi } 3882e66f17ffSNaoya Horiguchi /* 3883e66f17ffSNaoya Horiguchi * hwpoisoned entry is treated as no_page_table in 3884e66f17ffSNaoya Horiguchi * follow_page_mask(). 3885e66f17ffSNaoya Horiguchi */ 3886e66f17ffSNaoya Horiguchi } 3887e66f17ffSNaoya Horiguchi out: 3888e66f17ffSNaoya Horiguchi spin_unlock(ptl); 38899e5fc74cSSteve Capper return page; 38909e5fc74cSSteve Capper } 38919e5fc74cSSteve Capper 389261f77edaSNaoya Horiguchi struct page * __weak 38939e5fc74cSSteve Capper follow_huge_pud(struct mm_struct *mm, unsigned long address, 3894e66f17ffSNaoya Horiguchi pud_t *pud, int flags) 38959e5fc74cSSteve Capper { 3896e66f17ffSNaoya Horiguchi if (flags & FOLL_GET) 3897e66f17ffSNaoya Horiguchi return NULL; 38989e5fc74cSSteve Capper 3899e66f17ffSNaoya Horiguchi return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT); 39009e5fc74cSSteve Capper } 39019e5fc74cSSteve Capper 3902d5bd9106SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE 3903d5bd9106SAndi Kleen 390493f70f90SNaoya Horiguchi /* 390593f70f90SNaoya Horiguchi * This function is called from memory failure code. 390693f70f90SNaoya Horiguchi * Assume the caller holds page lock of the head page. 390793f70f90SNaoya Horiguchi */ 39086de2b1aaSNaoya Horiguchi int dequeue_hwpoisoned_huge_page(struct page *hpage) 390993f70f90SNaoya Horiguchi { 391093f70f90SNaoya Horiguchi struct hstate *h = page_hstate(hpage); 391193f70f90SNaoya Horiguchi int nid = page_to_nid(hpage); 39126de2b1aaSNaoya Horiguchi int ret = -EBUSY; 391393f70f90SNaoya Horiguchi 391493f70f90SNaoya Horiguchi spin_lock(&hugetlb_lock); 39157e1f049eSNaoya Horiguchi /* 39167e1f049eSNaoya Horiguchi * Just checking !page_huge_active is not enough, because that could be 39177e1f049eSNaoya Horiguchi * an isolated/hwpoisoned hugepage (which have >0 refcount). 39187e1f049eSNaoya Horiguchi */ 39197e1f049eSNaoya Horiguchi if (!page_huge_active(hpage) && !page_count(hpage)) { 392056f2fb14SNaoya Horiguchi /* 392156f2fb14SNaoya Horiguchi * Hwpoisoned hugepage isn't linked to activelist or freelist, 392256f2fb14SNaoya Horiguchi * but dangling hpage->lru can trigger list-debug warnings 392356f2fb14SNaoya Horiguchi * (this happens when we call unpoison_memory() on it), 392456f2fb14SNaoya Horiguchi * so let it point to itself with list_del_init(). 392556f2fb14SNaoya Horiguchi */ 392656f2fb14SNaoya Horiguchi list_del_init(&hpage->lru); 39278c6c2ecbSNaoya Horiguchi set_page_refcounted(hpage); 392893f70f90SNaoya Horiguchi h->free_huge_pages--; 392993f70f90SNaoya Horiguchi h->free_huge_pages_node[nid]--; 39306de2b1aaSNaoya Horiguchi ret = 0; 393193f70f90SNaoya Horiguchi } 39326de2b1aaSNaoya Horiguchi spin_unlock(&hugetlb_lock); 39336de2b1aaSNaoya Horiguchi return ret; 39346de2b1aaSNaoya Horiguchi } 39356de2b1aaSNaoya Horiguchi #endif 393631caf665SNaoya Horiguchi 393731caf665SNaoya Horiguchi bool isolate_huge_page(struct page *page, struct list_head *list) 393831caf665SNaoya Horiguchi { 3939bcc54222SNaoya Horiguchi bool ret = true; 3940bcc54222SNaoya Horiguchi 3941309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 394231caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 3943bcc54222SNaoya Horiguchi if (!page_huge_active(page) || !get_page_unless_zero(page)) { 3944bcc54222SNaoya Horiguchi ret = false; 3945bcc54222SNaoya Horiguchi goto unlock; 3946bcc54222SNaoya Horiguchi } 3947bcc54222SNaoya Horiguchi clear_page_huge_active(page); 394831caf665SNaoya Horiguchi list_move_tail(&page->lru, list); 3949bcc54222SNaoya Horiguchi unlock: 395031caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 3951bcc54222SNaoya Horiguchi return ret; 395231caf665SNaoya Horiguchi } 395331caf665SNaoya Horiguchi 395431caf665SNaoya Horiguchi void putback_active_hugepage(struct page *page) 395531caf665SNaoya Horiguchi { 3956309381feSSasha Levin VM_BUG_ON_PAGE(!PageHead(page), page); 395731caf665SNaoya Horiguchi spin_lock(&hugetlb_lock); 3958bcc54222SNaoya Horiguchi set_page_huge_active(page); 395931caf665SNaoya Horiguchi list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist); 396031caf665SNaoya Horiguchi spin_unlock(&hugetlb_lock); 396131caf665SNaoya Horiguchi put_page(page); 396231caf665SNaoya Horiguchi } 3963