11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Generic hugetlb support. 31da177e4SLinus Torvalds * (C) William Irwin, April 2004 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds #include <linux/gfp.h> 61da177e4SLinus Torvalds #include <linux/list.h> 71da177e4SLinus Torvalds #include <linux/init.h> 81da177e4SLinus Torvalds #include <linux/module.h> 91da177e4SLinus Torvalds #include <linux/mm.h> 101da177e4SLinus Torvalds #include <linux/sysctl.h> 111da177e4SLinus Torvalds #include <linux/highmem.h> 121da177e4SLinus Torvalds #include <linux/nodemask.h> 1363551ae0SDavid Gibson #include <linux/pagemap.h> 145da7ca86SChristoph Lameter #include <linux/mempolicy.h> 15aea47ff3SChristoph Lameter #include <linux/cpuset.h> 163935baa9SDavid Gibson #include <linux/mutex.h> 175da7ca86SChristoph Lameter 1863551ae0SDavid Gibson #include <asm/page.h> 1963551ae0SDavid Gibson #include <asm/pgtable.h> 2063551ae0SDavid Gibson 2163551ae0SDavid Gibson #include <linux/hugetlb.h> 227835e98bSNick Piggin #include "internal.h" 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL; 25a43a8c39SChen, Kenneth W static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages; 267893d1d5SAdam Litke static unsigned long surplus_huge_pages; 27064d9efeSNishanth Aravamudan static unsigned long nr_overcommit_huge_pages; 281da177e4SLinus Torvalds unsigned long max_huge_pages; 29064d9efeSNishanth Aravamudan unsigned long sysctl_overcommit_huge_pages; 301da177e4SLinus Torvalds static struct list_head hugepage_freelists[MAX_NUMNODES]; 311da177e4SLinus Torvalds static unsigned int nr_huge_pages_node[MAX_NUMNODES]; 321da177e4SLinus Torvalds static unsigned int free_huge_pages_node[MAX_NUMNODES]; 337893d1d5SAdam Litke static unsigned int surplus_huge_pages_node[MAX_NUMNODES]; 34396faf03SMel Gorman static gfp_t htlb_alloc_mask = GFP_HIGHUSER; 35396faf03SMel Gorman unsigned long hugepages_treat_as_movable; 3663b4613cSNishanth Aravamudan static int hugetlb_next_nid; 37396faf03SMel Gorman 383935baa9SDavid Gibson /* 393935baa9SDavid Gibson * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages 403935baa9SDavid Gibson */ 413935baa9SDavid Gibson static DEFINE_SPINLOCK(hugetlb_lock); 420bd0f9fbSEric Paris 4379ac6ba4SDavid Gibson static void clear_huge_page(struct page *page, unsigned long addr) 4479ac6ba4SDavid Gibson { 4579ac6ba4SDavid Gibson int i; 4679ac6ba4SDavid Gibson 4779ac6ba4SDavid Gibson might_sleep(); 4879ac6ba4SDavid Gibson for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) { 4979ac6ba4SDavid Gibson cond_resched(); 50281e0e3bSRalf Baechle clear_user_highpage(page + i, addr + i * PAGE_SIZE); 5179ac6ba4SDavid Gibson } 5279ac6ba4SDavid Gibson } 5379ac6ba4SDavid Gibson 5479ac6ba4SDavid Gibson static void copy_huge_page(struct page *dst, struct page *src, 559de455b2SAtsushi Nemoto unsigned long addr, struct vm_area_struct *vma) 5679ac6ba4SDavid Gibson { 5779ac6ba4SDavid Gibson int i; 5879ac6ba4SDavid Gibson 5979ac6ba4SDavid Gibson might_sleep(); 6079ac6ba4SDavid Gibson for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) { 6179ac6ba4SDavid Gibson cond_resched(); 629de455b2SAtsushi Nemoto copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); 6379ac6ba4SDavid Gibson } 6479ac6ba4SDavid Gibson } 6579ac6ba4SDavid Gibson 661da177e4SLinus Torvalds static void enqueue_huge_page(struct page *page) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds int nid = page_to_nid(page); 691da177e4SLinus Torvalds list_add(&page->lru, &hugepage_freelists[nid]); 701da177e4SLinus Torvalds free_huge_pages++; 711da177e4SLinus Torvalds free_huge_pages_node[nid]++; 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 74348e1e04SNishanth Aravamudan static struct page *dequeue_huge_page(void) 75348e1e04SNishanth Aravamudan { 76348e1e04SNishanth Aravamudan int nid; 77348e1e04SNishanth Aravamudan struct page *page = NULL; 78348e1e04SNishanth Aravamudan 79348e1e04SNishanth Aravamudan for (nid = 0; nid < MAX_NUMNODES; ++nid) { 80348e1e04SNishanth Aravamudan if (!list_empty(&hugepage_freelists[nid])) { 81348e1e04SNishanth Aravamudan page = list_entry(hugepage_freelists[nid].next, 82348e1e04SNishanth Aravamudan struct page, lru); 83348e1e04SNishanth Aravamudan list_del(&page->lru); 84348e1e04SNishanth Aravamudan free_huge_pages--; 85348e1e04SNishanth Aravamudan free_huge_pages_node[nid]--; 86348e1e04SNishanth Aravamudan break; 87348e1e04SNishanth Aravamudan } 88348e1e04SNishanth Aravamudan } 89348e1e04SNishanth Aravamudan return page; 90348e1e04SNishanth Aravamudan } 91348e1e04SNishanth Aravamudan 92348e1e04SNishanth Aravamudan static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma, 935da7ca86SChristoph Lameter unsigned long address) 941da177e4SLinus Torvalds { 9531a5c6e4SNishanth Aravamudan int nid; 961da177e4SLinus Torvalds struct page *page = NULL; 97480eccf9SLee Schermerhorn struct mempolicy *mpol; 98396faf03SMel Gorman struct zonelist *zonelist = huge_zonelist(vma, address, 99480eccf9SLee Schermerhorn htlb_alloc_mask, &mpol); 10096df9333SChristoph Lameter struct zone **z; 1011da177e4SLinus Torvalds 10296df9333SChristoph Lameter for (z = zonelist->zones; *z; z++) { 10389fa3024SChristoph Lameter nid = zone_to_nid(*z); 104396faf03SMel Gorman if (cpuset_zone_allowed_softwall(*z, htlb_alloc_mask) && 1053abf7afdSAndrew Morton !list_empty(&hugepage_freelists[nid])) { 1061da177e4SLinus Torvalds page = list_entry(hugepage_freelists[nid].next, 1071da177e4SLinus Torvalds struct page, lru); 1081da177e4SLinus Torvalds list_del(&page->lru); 1091da177e4SLinus Torvalds free_huge_pages--; 1101da177e4SLinus Torvalds free_huge_pages_node[nid]--; 111e4e574b7SAdam Litke if (vma && vma->vm_flags & VM_MAYSHARE) 112e4e574b7SAdam Litke resv_huge_pages--; 1135ab3ee7bSKen Chen break; 1141da177e4SLinus Torvalds } 1153abf7afdSAndrew Morton } 116480eccf9SLee Schermerhorn mpol_free(mpol); /* unref if mpol !NULL */ 1171da177e4SLinus Torvalds return page; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1206af2acb6SAdam Litke static void update_and_free_page(struct page *page) 1216af2acb6SAdam Litke { 1226af2acb6SAdam Litke int i; 1236af2acb6SAdam Litke nr_huge_pages--; 1246af2acb6SAdam Litke nr_huge_pages_node[page_to_nid(page)]--; 1256af2acb6SAdam Litke for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) { 1266af2acb6SAdam Litke page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | 1276af2acb6SAdam Litke 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved | 1286af2acb6SAdam Litke 1 << PG_private | 1<< PG_writeback); 1296af2acb6SAdam Litke } 1306af2acb6SAdam Litke set_compound_page_dtor(page, NULL); 1316af2acb6SAdam Litke set_page_refcounted(page); 1326af2acb6SAdam Litke __free_pages(page, HUGETLB_PAGE_ORDER); 1336af2acb6SAdam Litke } 1346af2acb6SAdam Litke 13527a85ef1SDavid Gibson static void free_huge_page(struct page *page) 13627a85ef1SDavid Gibson { 1377893d1d5SAdam Litke int nid = page_to_nid(page); 138c79fb75eSAdam Litke struct address_space *mapping; 13927a85ef1SDavid Gibson 140c79fb75eSAdam Litke mapping = (struct address_space *) page_private(page); 141e5df70abSAndy Whitcroft set_page_private(page, 0); 1427893d1d5SAdam Litke BUG_ON(page_count(page)); 14327a85ef1SDavid Gibson INIT_LIST_HEAD(&page->lru); 14427a85ef1SDavid Gibson 14527a85ef1SDavid Gibson spin_lock(&hugetlb_lock); 1467893d1d5SAdam Litke if (surplus_huge_pages_node[nid]) { 1477893d1d5SAdam Litke update_and_free_page(page); 1487893d1d5SAdam Litke surplus_huge_pages--; 1497893d1d5SAdam Litke surplus_huge_pages_node[nid]--; 1507893d1d5SAdam Litke } else { 15127a85ef1SDavid Gibson enqueue_huge_page(page); 1527893d1d5SAdam Litke } 15327a85ef1SDavid Gibson spin_unlock(&hugetlb_lock); 154c79fb75eSAdam Litke if (mapping) 1559a119c05SAdam Litke hugetlb_put_quota(mapping, 1); 15627a85ef1SDavid Gibson } 15727a85ef1SDavid Gibson 1587893d1d5SAdam Litke /* 1597893d1d5SAdam Litke * Increment or decrement surplus_huge_pages. Keep node-specific counters 1607893d1d5SAdam Litke * balanced by operating on them in a round-robin fashion. 1617893d1d5SAdam Litke * Returns 1 if an adjustment was made. 1627893d1d5SAdam Litke */ 1637893d1d5SAdam Litke static int adjust_pool_surplus(int delta) 1647893d1d5SAdam Litke { 1657893d1d5SAdam Litke static int prev_nid; 1667893d1d5SAdam Litke int nid = prev_nid; 1677893d1d5SAdam Litke int ret = 0; 1687893d1d5SAdam Litke 1697893d1d5SAdam Litke VM_BUG_ON(delta != -1 && delta != 1); 1707893d1d5SAdam Litke do { 1717893d1d5SAdam Litke nid = next_node(nid, node_online_map); 1727893d1d5SAdam Litke if (nid == MAX_NUMNODES) 1737893d1d5SAdam Litke nid = first_node(node_online_map); 1747893d1d5SAdam Litke 1757893d1d5SAdam Litke /* To shrink on this node, there must be a surplus page */ 1767893d1d5SAdam Litke if (delta < 0 && !surplus_huge_pages_node[nid]) 1777893d1d5SAdam Litke continue; 1787893d1d5SAdam Litke /* Surplus cannot exceed the total number of pages */ 1797893d1d5SAdam Litke if (delta > 0 && surplus_huge_pages_node[nid] >= 1807893d1d5SAdam Litke nr_huge_pages_node[nid]) 1817893d1d5SAdam Litke continue; 1827893d1d5SAdam Litke 1837893d1d5SAdam Litke surplus_huge_pages += delta; 1847893d1d5SAdam Litke surplus_huge_pages_node[nid] += delta; 1857893d1d5SAdam Litke ret = 1; 1867893d1d5SAdam Litke break; 1877893d1d5SAdam Litke } while (nid != prev_nid); 1887893d1d5SAdam Litke 1897893d1d5SAdam Litke prev_nid = nid; 1907893d1d5SAdam Litke return ret; 1917893d1d5SAdam Litke } 1927893d1d5SAdam Litke 19363b4613cSNishanth Aravamudan static struct page *alloc_fresh_huge_page_node(int nid) 1941da177e4SLinus Torvalds { 1951da177e4SLinus Torvalds struct page *page; 196f96efd58SJoe Jin 19763b4613cSNishanth Aravamudan page = alloc_pages_node(nid, 19863b4613cSNishanth Aravamudan htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|__GFP_NOWARN, 199f96efd58SJoe Jin HUGETLB_PAGE_ORDER); 2001da177e4SLinus Torvalds if (page) { 20133f2ef89SAndy Whitcroft set_compound_page_dtor(page, free_huge_page); 2020bd0f9fbSEric Paris spin_lock(&hugetlb_lock); 2031da177e4SLinus Torvalds nr_huge_pages++; 20463b4613cSNishanth Aravamudan nr_huge_pages_node[nid]++; 2050bd0f9fbSEric Paris spin_unlock(&hugetlb_lock); 206a482289dSNick Piggin put_page(page); /* free it into the hugepage allocator */ 2071da177e4SLinus Torvalds } 20863b4613cSNishanth Aravamudan 20963b4613cSNishanth Aravamudan return page; 21063b4613cSNishanth Aravamudan } 21163b4613cSNishanth Aravamudan 21263b4613cSNishanth Aravamudan static int alloc_fresh_huge_page(void) 21363b4613cSNishanth Aravamudan { 21463b4613cSNishanth Aravamudan struct page *page; 21563b4613cSNishanth Aravamudan int start_nid; 21663b4613cSNishanth Aravamudan int next_nid; 21763b4613cSNishanth Aravamudan int ret = 0; 21863b4613cSNishanth Aravamudan 21963b4613cSNishanth Aravamudan start_nid = hugetlb_next_nid; 22063b4613cSNishanth Aravamudan 22163b4613cSNishanth Aravamudan do { 22263b4613cSNishanth Aravamudan page = alloc_fresh_huge_page_node(hugetlb_next_nid); 22363b4613cSNishanth Aravamudan if (page) 22463b4613cSNishanth Aravamudan ret = 1; 22563b4613cSNishanth Aravamudan /* 22663b4613cSNishanth Aravamudan * Use a helper variable to find the next node and then 22763b4613cSNishanth Aravamudan * copy it back to hugetlb_next_nid afterwards: 22863b4613cSNishanth Aravamudan * otherwise there's a window in which a racer might 22963b4613cSNishanth Aravamudan * pass invalid nid MAX_NUMNODES to alloc_pages_node. 23063b4613cSNishanth Aravamudan * But we don't need to use a spin_lock here: it really 23163b4613cSNishanth Aravamudan * doesn't matter if occasionally a racer chooses the 23263b4613cSNishanth Aravamudan * same nid as we do. Move nid forward in the mask even 23363b4613cSNishanth Aravamudan * if we just successfully allocated a hugepage so that 23463b4613cSNishanth Aravamudan * the next caller gets hugepages on the next node. 23563b4613cSNishanth Aravamudan */ 23663b4613cSNishanth Aravamudan next_nid = next_node(hugetlb_next_nid, node_online_map); 23763b4613cSNishanth Aravamudan if (next_nid == MAX_NUMNODES) 23863b4613cSNishanth Aravamudan next_nid = first_node(node_online_map); 23963b4613cSNishanth Aravamudan hugetlb_next_nid = next_nid; 24063b4613cSNishanth Aravamudan } while (!page && hugetlb_next_nid != start_nid); 24163b4613cSNishanth Aravamudan 24263b4613cSNishanth Aravamudan return ret; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2457893d1d5SAdam Litke static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma, 2467893d1d5SAdam Litke unsigned long address) 2477893d1d5SAdam Litke { 2487893d1d5SAdam Litke struct page *page; 249d1c3fb1fSNishanth Aravamudan unsigned int nid; 2507893d1d5SAdam Litke 251d1c3fb1fSNishanth Aravamudan /* 252d1c3fb1fSNishanth Aravamudan * Assume we will successfully allocate the surplus page to 253d1c3fb1fSNishanth Aravamudan * prevent racing processes from causing the surplus to exceed 254d1c3fb1fSNishanth Aravamudan * overcommit 255d1c3fb1fSNishanth Aravamudan * 256d1c3fb1fSNishanth Aravamudan * This however introduces a different race, where a process B 257d1c3fb1fSNishanth Aravamudan * tries to grow the static hugepage pool while alloc_pages() is 258d1c3fb1fSNishanth Aravamudan * called by process A. B will only examine the per-node 259d1c3fb1fSNishanth Aravamudan * counters in determining if surplus huge pages can be 260d1c3fb1fSNishanth Aravamudan * converted to normal huge pages in adjust_pool_surplus(). A 261d1c3fb1fSNishanth Aravamudan * won't be able to increment the per-node counter, until the 262d1c3fb1fSNishanth Aravamudan * lock is dropped by B, but B doesn't drop hugetlb_lock until 263d1c3fb1fSNishanth Aravamudan * no more huge pages can be converted from surplus to normal 264d1c3fb1fSNishanth Aravamudan * state (and doesn't try to convert again). Thus, we have a 265d1c3fb1fSNishanth Aravamudan * case where a surplus huge page exists, the pool is grown, and 266d1c3fb1fSNishanth Aravamudan * the surplus huge page still exists after, even though it 267d1c3fb1fSNishanth Aravamudan * should just have been converted to a normal huge page. This 268d1c3fb1fSNishanth Aravamudan * does not leak memory, though, as the hugepage will be freed 269d1c3fb1fSNishanth Aravamudan * once it is out of use. It also does not allow the counters to 270d1c3fb1fSNishanth Aravamudan * go out of whack in adjust_pool_surplus() as we don't modify 271d1c3fb1fSNishanth Aravamudan * the node values until we've gotten the hugepage and only the 272d1c3fb1fSNishanth Aravamudan * per-node value is checked there. 273d1c3fb1fSNishanth Aravamudan */ 274d1c3fb1fSNishanth Aravamudan spin_lock(&hugetlb_lock); 275d1c3fb1fSNishanth Aravamudan if (surplus_huge_pages >= nr_overcommit_huge_pages) { 276d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 277d1c3fb1fSNishanth Aravamudan return NULL; 278d1c3fb1fSNishanth Aravamudan } else { 279d1c3fb1fSNishanth Aravamudan nr_huge_pages++; 280d1c3fb1fSNishanth Aravamudan surplus_huge_pages++; 281d1c3fb1fSNishanth Aravamudan } 282d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 283d1c3fb1fSNishanth Aravamudan 2847893d1d5SAdam Litke page = alloc_pages(htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN, 2857893d1d5SAdam Litke HUGETLB_PAGE_ORDER); 286d1c3fb1fSNishanth Aravamudan 2877893d1d5SAdam Litke spin_lock(&hugetlb_lock); 288d1c3fb1fSNishanth Aravamudan if (page) { 2892668db91SAdam Litke /* 2902668db91SAdam Litke * This page is now managed by the hugetlb allocator and has 2912668db91SAdam Litke * no users -- drop the buddy allocator's reference. 2922668db91SAdam Litke */ 2932668db91SAdam Litke put_page_testzero(page); 2942668db91SAdam Litke VM_BUG_ON(page_count(page)); 295d1c3fb1fSNishanth Aravamudan nid = page_to_nid(page); 296d1c3fb1fSNishanth Aravamudan set_compound_page_dtor(page, free_huge_page); 297d1c3fb1fSNishanth Aravamudan /* 298d1c3fb1fSNishanth Aravamudan * We incremented the global counters already 299d1c3fb1fSNishanth Aravamudan */ 300d1c3fb1fSNishanth Aravamudan nr_huge_pages_node[nid]++; 301d1c3fb1fSNishanth Aravamudan surplus_huge_pages_node[nid]++; 302d1c3fb1fSNishanth Aravamudan } else { 303d1c3fb1fSNishanth Aravamudan nr_huge_pages--; 304d1c3fb1fSNishanth Aravamudan surplus_huge_pages--; 3057893d1d5SAdam Litke } 306d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 3077893d1d5SAdam Litke 3087893d1d5SAdam Litke return page; 3097893d1d5SAdam Litke } 3107893d1d5SAdam Litke 311e4e574b7SAdam Litke /* 312e4e574b7SAdam Litke * Increase the hugetlb pool such that it can accomodate a reservation 313e4e574b7SAdam Litke * of size 'delta'. 314e4e574b7SAdam Litke */ 315e4e574b7SAdam Litke static int gather_surplus_pages(int delta) 316e4e574b7SAdam Litke { 317e4e574b7SAdam Litke struct list_head surplus_list; 318e4e574b7SAdam Litke struct page *page, *tmp; 319e4e574b7SAdam Litke int ret, i; 320e4e574b7SAdam Litke int needed, allocated; 321e4e574b7SAdam Litke 322e4e574b7SAdam Litke needed = (resv_huge_pages + delta) - free_huge_pages; 323ac09b3a1SAdam Litke if (needed <= 0) { 324ac09b3a1SAdam Litke resv_huge_pages += delta; 325e4e574b7SAdam Litke return 0; 326ac09b3a1SAdam Litke } 327e4e574b7SAdam Litke 328e4e574b7SAdam Litke allocated = 0; 329e4e574b7SAdam Litke INIT_LIST_HEAD(&surplus_list); 330e4e574b7SAdam Litke 331e4e574b7SAdam Litke ret = -ENOMEM; 332e4e574b7SAdam Litke retry: 333e4e574b7SAdam Litke spin_unlock(&hugetlb_lock); 334e4e574b7SAdam Litke for (i = 0; i < needed; i++) { 335e4e574b7SAdam Litke page = alloc_buddy_huge_page(NULL, 0); 336e4e574b7SAdam Litke if (!page) { 337e4e574b7SAdam Litke /* 338e4e574b7SAdam Litke * We were not able to allocate enough pages to 339e4e574b7SAdam Litke * satisfy the entire reservation so we free what 340e4e574b7SAdam Litke * we've allocated so far. 341e4e574b7SAdam Litke */ 342e4e574b7SAdam Litke spin_lock(&hugetlb_lock); 343e4e574b7SAdam Litke needed = 0; 344e4e574b7SAdam Litke goto free; 345e4e574b7SAdam Litke } 346e4e574b7SAdam Litke 347e4e574b7SAdam Litke list_add(&page->lru, &surplus_list); 348e4e574b7SAdam Litke } 349e4e574b7SAdam Litke allocated += needed; 350e4e574b7SAdam Litke 351e4e574b7SAdam Litke /* 352e4e574b7SAdam Litke * After retaking hugetlb_lock, we need to recalculate 'needed' 353e4e574b7SAdam Litke * because either resv_huge_pages or free_huge_pages may have changed. 354e4e574b7SAdam Litke */ 355e4e574b7SAdam Litke spin_lock(&hugetlb_lock); 356e4e574b7SAdam Litke needed = (resv_huge_pages + delta) - (free_huge_pages + allocated); 357e4e574b7SAdam Litke if (needed > 0) 358e4e574b7SAdam Litke goto retry; 359e4e574b7SAdam Litke 360e4e574b7SAdam Litke /* 361e4e574b7SAdam Litke * The surplus_list now contains _at_least_ the number of extra pages 362e4e574b7SAdam Litke * needed to accomodate the reservation. Add the appropriate number 363e4e574b7SAdam Litke * of pages to the hugetlb pool and free the extras back to the buddy 364ac09b3a1SAdam Litke * allocator. Commit the entire reservation here to prevent another 365ac09b3a1SAdam Litke * process from stealing the pages as they are added to the pool but 366ac09b3a1SAdam Litke * before they are reserved. 367e4e574b7SAdam Litke */ 368e4e574b7SAdam Litke needed += allocated; 369ac09b3a1SAdam Litke resv_huge_pages += delta; 370e4e574b7SAdam Litke ret = 0; 371e4e574b7SAdam Litke free: 372e4e574b7SAdam Litke list_for_each_entry_safe(page, tmp, &surplus_list, lru) { 373e4e574b7SAdam Litke list_del(&page->lru); 374e4e574b7SAdam Litke if ((--needed) >= 0) 375e4e574b7SAdam Litke enqueue_huge_page(page); 376af767cbdSAdam Litke else { 377af767cbdSAdam Litke /* 3782668db91SAdam Litke * The page has a reference count of zero already, so 3792668db91SAdam Litke * call free_huge_page directly instead of using 3802668db91SAdam Litke * put_page. This must be done with hugetlb_lock 381af767cbdSAdam Litke * unlocked which is safe because free_huge_page takes 382af767cbdSAdam Litke * hugetlb_lock before deciding how to free the page. 383af767cbdSAdam Litke */ 384af767cbdSAdam Litke spin_unlock(&hugetlb_lock); 3852668db91SAdam Litke free_huge_page(page); 386af767cbdSAdam Litke spin_lock(&hugetlb_lock); 387af767cbdSAdam Litke } 388e4e574b7SAdam Litke } 389e4e574b7SAdam Litke 390e4e574b7SAdam Litke return ret; 391e4e574b7SAdam Litke } 392e4e574b7SAdam Litke 393e4e574b7SAdam Litke /* 394e4e574b7SAdam Litke * When releasing a hugetlb pool reservation, any surplus pages that were 395e4e574b7SAdam Litke * allocated to satisfy the reservation must be explicitly freed if they were 396e4e574b7SAdam Litke * never used. 397e4e574b7SAdam Litke */ 3988cde045cSAdrian Bunk static void return_unused_surplus_pages(unsigned long unused_resv_pages) 399e4e574b7SAdam Litke { 400e4e574b7SAdam Litke static int nid = -1; 401e4e574b7SAdam Litke struct page *page; 402e4e574b7SAdam Litke unsigned long nr_pages; 403e4e574b7SAdam Litke 404ac09b3a1SAdam Litke /* Uncommit the reservation */ 405ac09b3a1SAdam Litke resv_huge_pages -= unused_resv_pages; 406ac09b3a1SAdam Litke 407e4e574b7SAdam Litke nr_pages = min(unused_resv_pages, surplus_huge_pages); 408e4e574b7SAdam Litke 409e4e574b7SAdam Litke while (nr_pages) { 410e4e574b7SAdam Litke nid = next_node(nid, node_online_map); 411e4e574b7SAdam Litke if (nid == MAX_NUMNODES) 412e4e574b7SAdam Litke nid = first_node(node_online_map); 413e4e574b7SAdam Litke 414e4e574b7SAdam Litke if (!surplus_huge_pages_node[nid]) 415e4e574b7SAdam Litke continue; 416e4e574b7SAdam Litke 417e4e574b7SAdam Litke if (!list_empty(&hugepage_freelists[nid])) { 418e4e574b7SAdam Litke page = list_entry(hugepage_freelists[nid].next, 419e4e574b7SAdam Litke struct page, lru); 420e4e574b7SAdam Litke list_del(&page->lru); 421e4e574b7SAdam Litke update_and_free_page(page); 422e4e574b7SAdam Litke free_huge_pages--; 423e4e574b7SAdam Litke free_huge_pages_node[nid]--; 424e4e574b7SAdam Litke surplus_huge_pages--; 425e4e574b7SAdam Litke surplus_huge_pages_node[nid]--; 426e4e574b7SAdam Litke nr_pages--; 427e4e574b7SAdam Litke } 428e4e574b7SAdam Litke } 429e4e574b7SAdam Litke } 430e4e574b7SAdam Litke 431348ea204SAdam Litke 432348ea204SAdam Litke static struct page *alloc_huge_page_shared(struct vm_area_struct *vma, 433348ea204SAdam Litke unsigned long addr) 434348ea204SAdam Litke { 435348ea204SAdam Litke struct page *page; 436348ea204SAdam Litke 437348ea204SAdam Litke spin_lock(&hugetlb_lock); 438348e1e04SNishanth Aravamudan page = dequeue_huge_page_vma(vma, addr); 439348ea204SAdam Litke spin_unlock(&hugetlb_lock); 44090d8b7e6SAdam Litke return page ? page : ERR_PTR(-VM_FAULT_OOM); 441348ea204SAdam Litke } 442348ea204SAdam Litke 443348ea204SAdam Litke static struct page *alloc_huge_page_private(struct vm_area_struct *vma, 44427a85ef1SDavid Gibson unsigned long addr) 4451da177e4SLinus Torvalds { 4467893d1d5SAdam Litke struct page *page = NULL; 4471da177e4SLinus Torvalds 44890d8b7e6SAdam Litke if (hugetlb_get_quota(vma->vm_file->f_mapping, 1)) 44990d8b7e6SAdam Litke return ERR_PTR(-VM_FAULT_SIGBUS); 45090d8b7e6SAdam Litke 4511da177e4SLinus Torvalds spin_lock(&hugetlb_lock); 452348ea204SAdam Litke if (free_huge_pages > resv_huge_pages) 453348e1e04SNishanth Aravamudan page = dequeue_huge_page_vma(vma, addr); 454348ea204SAdam Litke spin_unlock(&hugetlb_lock); 45568842c9bSKen Chen if (!page) { 4567893d1d5SAdam Litke page = alloc_buddy_huge_page(vma, addr); 45768842c9bSKen Chen if (!page) { 45868842c9bSKen Chen hugetlb_put_quota(vma->vm_file->f_mapping, 1); 45968842c9bSKen Chen return ERR_PTR(-VM_FAULT_OOM); 46068842c9bSKen Chen } 46168842c9bSKen Chen } 46268842c9bSKen Chen return page; 463348ea204SAdam Litke } 4647893d1d5SAdam Litke 465348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma, 466348ea204SAdam Litke unsigned long addr) 467348ea204SAdam Litke { 468348ea204SAdam Litke struct page *page; 4692fc39cecSAdam Litke struct address_space *mapping = vma->vm_file->f_mapping; 4702fc39cecSAdam Litke 471348ea204SAdam Litke if (vma->vm_flags & VM_MAYSHARE) 472348ea204SAdam Litke page = alloc_huge_page_shared(vma, addr); 473348ea204SAdam Litke else 474348ea204SAdam Litke page = alloc_huge_page_private(vma, addr); 47590d8b7e6SAdam Litke 47690d8b7e6SAdam Litke if (!IS_ERR(page)) { 477348ea204SAdam Litke set_page_refcounted(page); 4782fc39cecSAdam Litke set_page_private(page, (unsigned long) mapping); 47990d8b7e6SAdam Litke } 4807893d1d5SAdam Litke return page; 481b45b5bd6SDavid Gibson } 482b45b5bd6SDavid Gibson 4831da177e4SLinus Torvalds static int __init hugetlb_init(void) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds unsigned long i; 4861da177e4SLinus Torvalds 4873c726f8dSBenjamin Herrenschmidt if (HPAGE_SHIFT == 0) 4883c726f8dSBenjamin Herrenschmidt return 0; 4893c726f8dSBenjamin Herrenschmidt 4901da177e4SLinus Torvalds for (i = 0; i < MAX_NUMNODES; ++i) 4911da177e4SLinus Torvalds INIT_LIST_HEAD(&hugepage_freelists[i]); 4921da177e4SLinus Torvalds 49363b4613cSNishanth Aravamudan hugetlb_next_nid = first_node(node_online_map); 49463b4613cSNishanth Aravamudan 4951da177e4SLinus Torvalds for (i = 0; i < max_huge_pages; ++i) { 496a482289dSNick Piggin if (!alloc_fresh_huge_page()) 4971da177e4SLinus Torvalds break; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds max_huge_pages = free_huge_pages = nr_huge_pages = i; 5001da177e4SLinus Torvalds printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages); 5011da177e4SLinus Torvalds return 0; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds module_init(hugetlb_init); 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds static int __init hugetlb_setup(char *s) 5061da177e4SLinus Torvalds { 5071da177e4SLinus Torvalds if (sscanf(s, "%lu", &max_huge_pages) <= 0) 5081da177e4SLinus Torvalds max_huge_pages = 0; 5091da177e4SLinus Torvalds return 1; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds __setup("hugepages=", hugetlb_setup); 5121da177e4SLinus Torvalds 5138a630112SKen Chen static unsigned int cpuset_mems_nr(unsigned int *array) 5148a630112SKen Chen { 5158a630112SKen Chen int node; 5168a630112SKen Chen unsigned int nr = 0; 5178a630112SKen Chen 5188a630112SKen Chen for_each_node_mask(node, cpuset_current_mems_allowed) 5198a630112SKen Chen nr += array[node]; 5208a630112SKen Chen 5218a630112SKen Chen return nr; 5228a630112SKen Chen } 5238a630112SKen Chen 5241da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 5251da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM 5261da177e4SLinus Torvalds static void try_to_free_low(unsigned long count) 5271da177e4SLinus Torvalds { 5284415cc8dSChristoph Lameter int i; 5294415cc8dSChristoph Lameter 5301da177e4SLinus Torvalds for (i = 0; i < MAX_NUMNODES; ++i) { 5311da177e4SLinus Torvalds struct page *page, *next; 5321da177e4SLinus Torvalds list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) { 5336b0c880dSAdam Litke if (count >= nr_huge_pages) 5346b0c880dSAdam Litke return; 5351da177e4SLinus Torvalds if (PageHighMem(page)) 5361da177e4SLinus Torvalds continue; 5371da177e4SLinus Torvalds list_del(&page->lru); 5381da177e4SLinus Torvalds update_and_free_page(page); 5391da177e4SLinus Torvalds free_huge_pages--; 5404415cc8dSChristoph Lameter free_huge_pages_node[page_to_nid(page)]--; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds #else 5451da177e4SLinus Torvalds static inline void try_to_free_low(unsigned long count) 5461da177e4SLinus Torvalds { 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds #endif 5491da177e4SLinus Torvalds 5507893d1d5SAdam Litke #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages) 5511da177e4SLinus Torvalds static unsigned long set_max_huge_pages(unsigned long count) 5521da177e4SLinus Torvalds { 5537893d1d5SAdam Litke unsigned long min_count, ret; 5541da177e4SLinus Torvalds 5557893d1d5SAdam Litke /* 5567893d1d5SAdam Litke * Increase the pool size 5577893d1d5SAdam Litke * First take pages out of surplus state. Then make up the 5587893d1d5SAdam Litke * remaining difference by allocating fresh huge pages. 559d1c3fb1fSNishanth Aravamudan * 560d1c3fb1fSNishanth Aravamudan * We might race with alloc_buddy_huge_page() here and be unable 561d1c3fb1fSNishanth Aravamudan * to convert a surplus huge page to a normal huge page. That is 562d1c3fb1fSNishanth Aravamudan * not critical, though, it just means the overall size of the 563d1c3fb1fSNishanth Aravamudan * pool might be one hugepage larger than it needs to be, but 564d1c3fb1fSNishanth Aravamudan * within all the constraints specified by the sysctls. 5657893d1d5SAdam Litke */ 5661da177e4SLinus Torvalds spin_lock(&hugetlb_lock); 5677893d1d5SAdam Litke while (surplus_huge_pages && count > persistent_huge_pages) { 5687893d1d5SAdam Litke if (!adjust_pool_surplus(-1)) 5697893d1d5SAdam Litke break; 5707893d1d5SAdam Litke } 5717893d1d5SAdam Litke 5727893d1d5SAdam Litke while (count > persistent_huge_pages) { 5737893d1d5SAdam Litke int ret; 5747893d1d5SAdam Litke /* 5757893d1d5SAdam Litke * If this allocation races such that we no longer need the 5767893d1d5SAdam Litke * page, free_huge_page will handle it by freeing the page 5777893d1d5SAdam Litke * and reducing the surplus. 5787893d1d5SAdam Litke */ 5797893d1d5SAdam Litke spin_unlock(&hugetlb_lock); 5807893d1d5SAdam Litke ret = alloc_fresh_huge_page(); 5817893d1d5SAdam Litke spin_lock(&hugetlb_lock); 5827893d1d5SAdam Litke if (!ret) 5837893d1d5SAdam Litke goto out; 5847893d1d5SAdam Litke 5857893d1d5SAdam Litke } 5867893d1d5SAdam Litke 5877893d1d5SAdam Litke /* 5887893d1d5SAdam Litke * Decrease the pool size 5897893d1d5SAdam Litke * First return free pages to the buddy allocator (being careful 5907893d1d5SAdam Litke * to keep enough around to satisfy reservations). Then place 5917893d1d5SAdam Litke * pages into surplus state as needed so the pool will shrink 5927893d1d5SAdam Litke * to the desired size as pages become free. 593d1c3fb1fSNishanth Aravamudan * 594d1c3fb1fSNishanth Aravamudan * By placing pages into the surplus state independent of the 595d1c3fb1fSNishanth Aravamudan * overcommit value, we are allowing the surplus pool size to 596d1c3fb1fSNishanth Aravamudan * exceed overcommit. There are few sane options here. Since 597d1c3fb1fSNishanth Aravamudan * alloc_buddy_huge_page() is checking the global counter, 598d1c3fb1fSNishanth Aravamudan * though, we'll note that we're not allowed to exceed surplus 599d1c3fb1fSNishanth Aravamudan * and won't grow the pool anywhere else. Not until one of the 600d1c3fb1fSNishanth Aravamudan * sysctls are changed, or the surplus pages go out of use. 6017893d1d5SAdam Litke */ 6026b0c880dSAdam Litke min_count = resv_huge_pages + nr_huge_pages - free_huge_pages; 6036b0c880dSAdam Litke min_count = max(count, min_count); 6047893d1d5SAdam Litke try_to_free_low(min_count); 6057893d1d5SAdam Litke while (min_count < persistent_huge_pages) { 606348e1e04SNishanth Aravamudan struct page *page = dequeue_huge_page(); 6071da177e4SLinus Torvalds if (!page) 6081da177e4SLinus Torvalds break; 6091da177e4SLinus Torvalds update_and_free_page(page); 6101da177e4SLinus Torvalds } 6117893d1d5SAdam Litke while (count < persistent_huge_pages) { 6127893d1d5SAdam Litke if (!adjust_pool_surplus(1)) 6137893d1d5SAdam Litke break; 6147893d1d5SAdam Litke } 6157893d1d5SAdam Litke out: 6167893d1d5SAdam Litke ret = persistent_huge_pages; 6171da177e4SLinus Torvalds spin_unlock(&hugetlb_lock); 6187893d1d5SAdam Litke return ret; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds int hugetlb_sysctl_handler(struct ctl_table *table, int write, 6221da177e4SLinus Torvalds struct file *file, void __user *buffer, 6231da177e4SLinus Torvalds size_t *length, loff_t *ppos) 6241da177e4SLinus Torvalds { 6251da177e4SLinus Torvalds proc_doulongvec_minmax(table, write, file, buffer, length, ppos); 6261da177e4SLinus Torvalds max_huge_pages = set_max_huge_pages(max_huge_pages); 6271da177e4SLinus Torvalds return 0; 6281da177e4SLinus Torvalds } 629396faf03SMel Gorman 630396faf03SMel Gorman int hugetlb_treat_movable_handler(struct ctl_table *table, int write, 631396faf03SMel Gorman struct file *file, void __user *buffer, 632396faf03SMel Gorman size_t *length, loff_t *ppos) 633396faf03SMel Gorman { 634396faf03SMel Gorman proc_dointvec(table, write, file, buffer, length, ppos); 635396faf03SMel Gorman if (hugepages_treat_as_movable) 636396faf03SMel Gorman htlb_alloc_mask = GFP_HIGHUSER_MOVABLE; 637396faf03SMel Gorman else 638396faf03SMel Gorman htlb_alloc_mask = GFP_HIGHUSER; 639396faf03SMel Gorman return 0; 640396faf03SMel Gorman } 641396faf03SMel Gorman 642a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write, 643a3d0c6aaSNishanth Aravamudan struct file *file, void __user *buffer, 644a3d0c6aaSNishanth Aravamudan size_t *length, loff_t *ppos) 645a3d0c6aaSNishanth Aravamudan { 646a3d0c6aaSNishanth Aravamudan proc_doulongvec_minmax(table, write, file, buffer, length, ppos); 647064d9efeSNishanth Aravamudan spin_lock(&hugetlb_lock); 648064d9efeSNishanth Aravamudan nr_overcommit_huge_pages = sysctl_overcommit_huge_pages; 649a3d0c6aaSNishanth Aravamudan spin_unlock(&hugetlb_lock); 650a3d0c6aaSNishanth Aravamudan return 0; 651a3d0c6aaSNishanth Aravamudan } 652a3d0c6aaSNishanth Aravamudan 6531da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds int hugetlb_report_meminfo(char *buf) 6561da177e4SLinus Torvalds { 6571da177e4SLinus Torvalds return sprintf(buf, 6581da177e4SLinus Torvalds "HugePages_Total: %5lu\n" 6591da177e4SLinus Torvalds "HugePages_Free: %5lu\n" 660b45b5bd6SDavid Gibson "HugePages_Rsvd: %5lu\n" 6617893d1d5SAdam Litke "HugePages_Surp: %5lu\n" 6621da177e4SLinus Torvalds "Hugepagesize: %5lu kB\n", 6631da177e4SLinus Torvalds nr_huge_pages, 6641da177e4SLinus Torvalds free_huge_pages, 665a43a8c39SChen, Kenneth W resv_huge_pages, 6667893d1d5SAdam Litke surplus_huge_pages, 6671da177e4SLinus Torvalds HPAGE_SIZE/1024); 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds return sprintf(buf, 6731da177e4SLinus Torvalds "Node %d HugePages_Total: %5u\n" 674a1de0919SNishanth Aravamudan "Node %d HugePages_Free: %5u\n" 675a1de0919SNishanth Aravamudan "Node %d HugePages_Surp: %5u\n", 6761da177e4SLinus Torvalds nid, nr_huge_pages_node[nid], 677a1de0919SNishanth Aravamudan nid, free_huge_pages_node[nid], 678a1de0919SNishanth Aravamudan nid, surplus_huge_pages_node[nid]); 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 6821da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void) 6831da177e4SLinus Torvalds { 6841da177e4SLinus Torvalds return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE); 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds /* 6881da177e4SLinus Torvalds * We cannot handle pagefaults against hugetlb pages at all. They cause 6891da177e4SLinus Torvalds * handle_mm_fault() to try to instantiate regular-sized pages in the 6901da177e4SLinus Torvalds * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get 6911da177e4SLinus Torvalds * this far. 6921da177e4SLinus Torvalds */ 693d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 6941da177e4SLinus Torvalds { 6951da177e4SLinus Torvalds BUG(); 696d0217ac0SNick Piggin return 0; 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds struct vm_operations_struct hugetlb_vm_ops = { 700d0217ac0SNick Piggin .fault = hugetlb_vm_op_fault, 7011da177e4SLinus Torvalds }; 7021da177e4SLinus Torvalds 7031e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, 7041e8f889bSDavid Gibson int writable) 70563551ae0SDavid Gibson { 70663551ae0SDavid Gibson pte_t entry; 70763551ae0SDavid Gibson 7081e8f889bSDavid Gibson if (writable) { 70963551ae0SDavid Gibson entry = 71063551ae0SDavid Gibson pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); 71163551ae0SDavid Gibson } else { 71263551ae0SDavid Gibson entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot)); 71363551ae0SDavid Gibson } 71463551ae0SDavid Gibson entry = pte_mkyoung(entry); 71563551ae0SDavid Gibson entry = pte_mkhuge(entry); 71663551ae0SDavid Gibson 71763551ae0SDavid Gibson return entry; 71863551ae0SDavid Gibson } 71963551ae0SDavid Gibson 7201e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma, 7211e8f889bSDavid Gibson unsigned long address, pte_t *ptep) 7221e8f889bSDavid Gibson { 7231e8f889bSDavid Gibson pte_t entry; 7241e8f889bSDavid Gibson 7251e8f889bSDavid Gibson entry = pte_mkwrite(pte_mkdirty(*ptep)); 7268dab5241SBenjamin Herrenschmidt if (ptep_set_access_flags(vma, address, ptep, entry, 1)) { 7271e8f889bSDavid Gibson update_mmu_cache(vma, address, entry); 7281e8f889bSDavid Gibson } 7298dab5241SBenjamin Herrenschmidt } 7301e8f889bSDavid Gibson 7311e8f889bSDavid Gibson 73263551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 73363551ae0SDavid Gibson struct vm_area_struct *vma) 73463551ae0SDavid Gibson { 73563551ae0SDavid Gibson pte_t *src_pte, *dst_pte, entry; 73663551ae0SDavid Gibson struct page *ptepage; 7371c59827dSHugh Dickins unsigned long addr; 7381e8f889bSDavid Gibson int cow; 7391e8f889bSDavid Gibson 7401e8f889bSDavid Gibson cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 74163551ae0SDavid Gibson 7421c59827dSHugh Dickins for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) { 743c74df32cSHugh Dickins src_pte = huge_pte_offset(src, addr); 744c74df32cSHugh Dickins if (!src_pte) 745c74df32cSHugh Dickins continue; 74663551ae0SDavid Gibson dst_pte = huge_pte_alloc(dst, addr); 74763551ae0SDavid Gibson if (!dst_pte) 74863551ae0SDavid Gibson goto nomem; 749c5c99429SLarry Woodman 750c5c99429SLarry Woodman /* If the pagetables are shared don't copy or take references */ 751c5c99429SLarry Woodman if (dst_pte == src_pte) 752c5c99429SLarry Woodman continue; 753c5c99429SLarry Woodman 754c74df32cSHugh Dickins spin_lock(&dst->page_table_lock); 7551c59827dSHugh Dickins spin_lock(&src->page_table_lock); 756c74df32cSHugh Dickins if (!pte_none(*src_pte)) { 7571e8f889bSDavid Gibson if (cow) 7581e8f889bSDavid Gibson ptep_set_wrprotect(src, addr, src_pte); 75963551ae0SDavid Gibson entry = *src_pte; 76063551ae0SDavid Gibson ptepage = pte_page(entry); 76163551ae0SDavid Gibson get_page(ptepage); 76263551ae0SDavid Gibson set_huge_pte_at(dst, addr, dst_pte, entry); 7631c59827dSHugh Dickins } 7641c59827dSHugh Dickins spin_unlock(&src->page_table_lock); 765c74df32cSHugh Dickins spin_unlock(&dst->page_table_lock); 76663551ae0SDavid Gibson } 76763551ae0SDavid Gibson return 0; 76863551ae0SDavid Gibson 76963551ae0SDavid Gibson nomem: 77063551ae0SDavid Gibson return -ENOMEM; 77163551ae0SDavid Gibson } 77263551ae0SDavid Gibson 773502717f4SChen, Kenneth W void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 77463551ae0SDavid Gibson unsigned long end) 77563551ae0SDavid Gibson { 77663551ae0SDavid Gibson struct mm_struct *mm = vma->vm_mm; 77763551ae0SDavid Gibson unsigned long address; 778c7546f8fSDavid Gibson pte_t *ptep; 77963551ae0SDavid Gibson pte_t pte; 78063551ae0SDavid Gibson struct page *page; 781fe1668aeSChen, Kenneth W struct page *tmp; 782c0a499c2SChen, Kenneth W /* 783c0a499c2SChen, Kenneth W * A page gathering list, protected by per file i_mmap_lock. The 784c0a499c2SChen, Kenneth W * lock is used to avoid list corruption from multiple unmapping 785c0a499c2SChen, Kenneth W * of the same page since we are using page->lru. 786c0a499c2SChen, Kenneth W */ 787fe1668aeSChen, Kenneth W LIST_HEAD(page_list); 78863551ae0SDavid Gibson 78963551ae0SDavid Gibson WARN_ON(!is_vm_hugetlb_page(vma)); 79063551ae0SDavid Gibson BUG_ON(start & ~HPAGE_MASK); 79163551ae0SDavid Gibson BUG_ON(end & ~HPAGE_MASK); 79263551ae0SDavid Gibson 793508034a3SHugh Dickins spin_lock(&mm->page_table_lock); 79463551ae0SDavid Gibson for (address = start; address < end; address += HPAGE_SIZE) { 795c7546f8fSDavid Gibson ptep = huge_pte_offset(mm, address); 796c7546f8fSDavid Gibson if (!ptep) 797c7546f8fSDavid Gibson continue; 798c7546f8fSDavid Gibson 79939dde65cSChen, Kenneth W if (huge_pmd_unshare(mm, &address, ptep)) 80039dde65cSChen, Kenneth W continue; 80139dde65cSChen, Kenneth W 802c7546f8fSDavid Gibson pte = huge_ptep_get_and_clear(mm, address, ptep); 80363551ae0SDavid Gibson if (pte_none(pte)) 80463551ae0SDavid Gibson continue; 805c7546f8fSDavid Gibson 80663551ae0SDavid Gibson page = pte_page(pte); 8076649a386SKen Chen if (pte_dirty(pte)) 8086649a386SKen Chen set_page_dirty(page); 809fe1668aeSChen, Kenneth W list_add(&page->lru, &page_list); 81063551ae0SDavid Gibson } 8111da177e4SLinus Torvalds spin_unlock(&mm->page_table_lock); 812508034a3SHugh Dickins flush_tlb_range(vma, start, end); 813fe1668aeSChen, Kenneth W list_for_each_entry_safe(page, tmp, &page_list, lru) { 814fe1668aeSChen, Kenneth W list_del(&page->lru); 815fe1668aeSChen, Kenneth W put_page(page); 816fe1668aeSChen, Kenneth W } 8171da177e4SLinus Torvalds } 81863551ae0SDavid Gibson 819502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 820502717f4SChen, Kenneth W unsigned long end) 821502717f4SChen, Kenneth W { 822502717f4SChen, Kenneth W /* 823502717f4SChen, Kenneth W * It is undesirable to test vma->vm_file as it should be non-null 824502717f4SChen, Kenneth W * for valid hugetlb area. However, vm_file will be NULL in the error 825502717f4SChen, Kenneth W * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails, 826502717f4SChen, Kenneth W * do_mmap_pgoff() nullifies vma->vm_file before calling this function 827502717f4SChen, Kenneth W * to clean up. Since no pte has actually been setup, it is safe to 828502717f4SChen, Kenneth W * do nothing in this case. 829502717f4SChen, Kenneth W */ 830502717f4SChen, Kenneth W if (vma->vm_file) { 831502717f4SChen, Kenneth W spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); 832502717f4SChen, Kenneth W __unmap_hugepage_range(vma, start, end); 833502717f4SChen, Kenneth W spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); 834502717f4SChen, Kenneth W } 835502717f4SChen, Kenneth W } 836502717f4SChen, Kenneth W 8371e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, 8381e8f889bSDavid Gibson unsigned long address, pte_t *ptep, pte_t pte) 8391e8f889bSDavid Gibson { 8401e8f889bSDavid Gibson struct page *old_page, *new_page; 84179ac6ba4SDavid Gibson int avoidcopy; 8421e8f889bSDavid Gibson 8431e8f889bSDavid Gibson old_page = pte_page(pte); 8441e8f889bSDavid Gibson 8451e8f889bSDavid Gibson /* If no-one else is actually using this page, avoid the copy 8461e8f889bSDavid Gibson * and just make the page writable */ 8471e8f889bSDavid Gibson avoidcopy = (page_count(old_page) == 1); 8481e8f889bSDavid Gibson if (avoidcopy) { 8491e8f889bSDavid Gibson set_huge_ptep_writable(vma, address, ptep); 85083c54070SNick Piggin return 0; 8511e8f889bSDavid Gibson } 8521e8f889bSDavid Gibson 8531e8f889bSDavid Gibson page_cache_get(old_page); 8545da7ca86SChristoph Lameter new_page = alloc_huge_page(vma, address); 8551e8f889bSDavid Gibson 8562fc39cecSAdam Litke if (IS_ERR(new_page)) { 8571e8f889bSDavid Gibson page_cache_release(old_page); 8582fc39cecSAdam Litke return -PTR_ERR(new_page); 8591e8f889bSDavid Gibson } 8601e8f889bSDavid Gibson 8611e8f889bSDavid Gibson spin_unlock(&mm->page_table_lock); 8629de455b2SAtsushi Nemoto copy_huge_page(new_page, old_page, address, vma); 8630ed361deSNick Piggin __SetPageUptodate(new_page); 8641e8f889bSDavid Gibson spin_lock(&mm->page_table_lock); 8651e8f889bSDavid Gibson 8661e8f889bSDavid Gibson ptep = huge_pte_offset(mm, address & HPAGE_MASK); 8671e8f889bSDavid Gibson if (likely(pte_same(*ptep, pte))) { 8681e8f889bSDavid Gibson /* Break COW */ 8691e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, 8701e8f889bSDavid Gibson make_huge_pte(vma, new_page, 1)); 8711e8f889bSDavid Gibson /* Make the old page be freed below */ 8721e8f889bSDavid Gibson new_page = old_page; 8731e8f889bSDavid Gibson } 8741e8f889bSDavid Gibson page_cache_release(new_page); 8751e8f889bSDavid Gibson page_cache_release(old_page); 87683c54070SNick Piggin return 0; 8771e8f889bSDavid Gibson } 8781e8f889bSDavid Gibson 879a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, 8801e8f889bSDavid Gibson unsigned long address, pte_t *ptep, int write_access) 881ac9b9c66SHugh Dickins { 882ac9b9c66SHugh Dickins int ret = VM_FAULT_SIGBUS; 8834c887265SAdam Litke unsigned long idx; 8844c887265SAdam Litke unsigned long size; 8854c887265SAdam Litke struct page *page; 8864c887265SAdam Litke struct address_space *mapping; 8871e8f889bSDavid Gibson pte_t new_pte; 8884c887265SAdam Litke 8894c887265SAdam Litke mapping = vma->vm_file->f_mapping; 8904c887265SAdam Litke idx = ((address - vma->vm_start) >> HPAGE_SHIFT) 8914c887265SAdam Litke + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT)); 8924c887265SAdam Litke 8934c887265SAdam Litke /* 8944c887265SAdam Litke * Use page lock to guard against racing truncation 8954c887265SAdam Litke * before we get page_table_lock. 8964c887265SAdam Litke */ 8976bda666aSChristoph Lameter retry: 8986bda666aSChristoph Lameter page = find_lock_page(mapping, idx); 8996bda666aSChristoph Lameter if (!page) { 900ebed4bfcSHugh Dickins size = i_size_read(mapping->host) >> HPAGE_SHIFT; 901ebed4bfcSHugh Dickins if (idx >= size) 902ebed4bfcSHugh Dickins goto out; 9036bda666aSChristoph Lameter page = alloc_huge_page(vma, address); 9042fc39cecSAdam Litke if (IS_ERR(page)) { 9052fc39cecSAdam Litke ret = -PTR_ERR(page); 9066bda666aSChristoph Lameter goto out; 9076bda666aSChristoph Lameter } 90879ac6ba4SDavid Gibson clear_huge_page(page, address); 9090ed361deSNick Piggin __SetPageUptodate(page); 910ac9b9c66SHugh Dickins 9116bda666aSChristoph Lameter if (vma->vm_flags & VM_SHARED) { 9126bda666aSChristoph Lameter int err; 91345c682a6SKen Chen struct inode *inode = mapping->host; 9146bda666aSChristoph Lameter 9156bda666aSChristoph Lameter err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); 9166bda666aSChristoph Lameter if (err) { 9176bda666aSChristoph Lameter put_page(page); 9186bda666aSChristoph Lameter if (err == -EEXIST) 9196bda666aSChristoph Lameter goto retry; 9206bda666aSChristoph Lameter goto out; 9216bda666aSChristoph Lameter } 92245c682a6SKen Chen 92345c682a6SKen Chen spin_lock(&inode->i_lock); 92445c682a6SKen Chen inode->i_blocks += BLOCKS_PER_HUGEPAGE; 92545c682a6SKen Chen spin_unlock(&inode->i_lock); 9266bda666aSChristoph Lameter } else 9276bda666aSChristoph Lameter lock_page(page); 9286bda666aSChristoph Lameter } 9291e8f889bSDavid Gibson 930ac9b9c66SHugh Dickins spin_lock(&mm->page_table_lock); 9314c887265SAdam Litke size = i_size_read(mapping->host) >> HPAGE_SHIFT; 9324c887265SAdam Litke if (idx >= size) 9334c887265SAdam Litke goto backout; 9344c887265SAdam Litke 93583c54070SNick Piggin ret = 0; 93686e5216fSAdam Litke if (!pte_none(*ptep)) 9374c887265SAdam Litke goto backout; 9384c887265SAdam Litke 9391e8f889bSDavid Gibson new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) 9401e8f889bSDavid Gibson && (vma->vm_flags & VM_SHARED))); 9411e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, new_pte); 9421e8f889bSDavid Gibson 9431e8f889bSDavid Gibson if (write_access && !(vma->vm_flags & VM_SHARED)) { 9441e8f889bSDavid Gibson /* Optimization, do the COW without a second fault */ 9451e8f889bSDavid Gibson ret = hugetlb_cow(mm, vma, address, ptep, new_pte); 9461e8f889bSDavid Gibson } 9471e8f889bSDavid Gibson 948ac9b9c66SHugh Dickins spin_unlock(&mm->page_table_lock); 9494c887265SAdam Litke unlock_page(page); 9504c887265SAdam Litke out: 951ac9b9c66SHugh Dickins return ret; 9524c887265SAdam Litke 9534c887265SAdam Litke backout: 9544c887265SAdam Litke spin_unlock(&mm->page_table_lock); 9554c887265SAdam Litke unlock_page(page); 9564c887265SAdam Litke put_page(page); 9574c887265SAdam Litke goto out; 958ac9b9c66SHugh Dickins } 959ac9b9c66SHugh Dickins 96086e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 96186e5216fSAdam Litke unsigned long address, int write_access) 96286e5216fSAdam Litke { 96386e5216fSAdam Litke pte_t *ptep; 96486e5216fSAdam Litke pte_t entry; 9651e8f889bSDavid Gibson int ret; 9663935baa9SDavid Gibson static DEFINE_MUTEX(hugetlb_instantiation_mutex); 96786e5216fSAdam Litke 96886e5216fSAdam Litke ptep = huge_pte_alloc(mm, address); 96986e5216fSAdam Litke if (!ptep) 97086e5216fSAdam Litke return VM_FAULT_OOM; 97186e5216fSAdam Litke 9723935baa9SDavid Gibson /* 9733935baa9SDavid Gibson * Serialize hugepage allocation and instantiation, so that we don't 9743935baa9SDavid Gibson * get spurious allocation failures if two CPUs race to instantiate 9753935baa9SDavid Gibson * the same page in the page cache. 9763935baa9SDavid Gibson */ 9773935baa9SDavid Gibson mutex_lock(&hugetlb_instantiation_mutex); 97886e5216fSAdam Litke entry = *ptep; 9793935baa9SDavid Gibson if (pte_none(entry)) { 9803935baa9SDavid Gibson ret = hugetlb_no_page(mm, vma, address, ptep, write_access); 9813935baa9SDavid Gibson mutex_unlock(&hugetlb_instantiation_mutex); 9823935baa9SDavid Gibson return ret; 9833935baa9SDavid Gibson } 98486e5216fSAdam Litke 98583c54070SNick Piggin ret = 0; 9861e8f889bSDavid Gibson 9871e8f889bSDavid Gibson spin_lock(&mm->page_table_lock); 9881e8f889bSDavid Gibson /* Check for a racing update before calling hugetlb_cow */ 9891e8f889bSDavid Gibson if (likely(pte_same(entry, *ptep))) 9901e8f889bSDavid Gibson if (write_access && !pte_write(entry)) 9911e8f889bSDavid Gibson ret = hugetlb_cow(mm, vma, address, ptep, entry); 9921e8f889bSDavid Gibson spin_unlock(&mm->page_table_lock); 9933935baa9SDavid Gibson mutex_unlock(&hugetlb_instantiation_mutex); 9941e8f889bSDavid Gibson 9951e8f889bSDavid Gibson return ret; 99686e5216fSAdam Litke } 99786e5216fSAdam Litke 99863551ae0SDavid Gibson int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, 99963551ae0SDavid Gibson struct page **pages, struct vm_area_struct **vmas, 10005b23dbe8SAdam Litke unsigned long *position, int *length, int i, 10015b23dbe8SAdam Litke int write) 100263551ae0SDavid Gibson { 1003d5d4b0aaSChen, Kenneth W unsigned long pfn_offset; 1004d5d4b0aaSChen, Kenneth W unsigned long vaddr = *position; 100563551ae0SDavid Gibson int remainder = *length; 100663551ae0SDavid Gibson 10071c59827dSHugh Dickins spin_lock(&mm->page_table_lock); 100863551ae0SDavid Gibson while (vaddr < vma->vm_end && remainder) { 100963551ae0SDavid Gibson pte_t *pte; 101063551ae0SDavid Gibson struct page *page; 101163551ae0SDavid Gibson 10124c887265SAdam Litke /* 10134c887265SAdam Litke * Some archs (sparc64, sh*) have multiple pte_ts to 10144c887265SAdam Litke * each hugepage. We have to make * sure we get the 10154c887265SAdam Litke * first, for the page indexing below to work. 10164c887265SAdam Litke */ 101763551ae0SDavid Gibson pte = huge_pte_offset(mm, vaddr & HPAGE_MASK); 101863551ae0SDavid Gibson 101972fad713SAdam Litke if (!pte || pte_none(*pte) || (write && !pte_write(*pte))) { 10204c887265SAdam Litke int ret; 10214c887265SAdam Litke 10224c887265SAdam Litke spin_unlock(&mm->page_table_lock); 10235b23dbe8SAdam Litke ret = hugetlb_fault(mm, vma, vaddr, write); 10244c887265SAdam Litke spin_lock(&mm->page_table_lock); 1025a89182c7SAdam Litke if (!(ret & VM_FAULT_ERROR)) 10264c887265SAdam Litke continue; 10274c887265SAdam Litke 10281c59827dSHugh Dickins remainder = 0; 10291c59827dSHugh Dickins if (!i) 10301c59827dSHugh Dickins i = -EFAULT; 10311c59827dSHugh Dickins break; 10321c59827dSHugh Dickins } 103363551ae0SDavid Gibson 1034d5d4b0aaSChen, Kenneth W pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT; 1035d5d4b0aaSChen, Kenneth W page = pte_page(*pte); 1036d5d4b0aaSChen, Kenneth W same_page: 1037d6692183SChen, Kenneth W if (pages) { 103863551ae0SDavid Gibson get_page(page); 1039d5d4b0aaSChen, Kenneth W pages[i] = page + pfn_offset; 1040d6692183SChen, Kenneth W } 104163551ae0SDavid Gibson 104263551ae0SDavid Gibson if (vmas) 104363551ae0SDavid Gibson vmas[i] = vma; 104463551ae0SDavid Gibson 104563551ae0SDavid Gibson vaddr += PAGE_SIZE; 1046d5d4b0aaSChen, Kenneth W ++pfn_offset; 104763551ae0SDavid Gibson --remainder; 104863551ae0SDavid Gibson ++i; 1049d5d4b0aaSChen, Kenneth W if (vaddr < vma->vm_end && remainder && 1050d5d4b0aaSChen, Kenneth W pfn_offset < HPAGE_SIZE/PAGE_SIZE) { 1051d5d4b0aaSChen, Kenneth W /* 1052d5d4b0aaSChen, Kenneth W * We use pfn_offset to avoid touching the pageframes 1053d5d4b0aaSChen, Kenneth W * of this compound page. 1054d5d4b0aaSChen, Kenneth W */ 1055d5d4b0aaSChen, Kenneth W goto same_page; 1056d5d4b0aaSChen, Kenneth W } 105763551ae0SDavid Gibson } 10581c59827dSHugh Dickins spin_unlock(&mm->page_table_lock); 105963551ae0SDavid Gibson *length = remainder; 106063551ae0SDavid Gibson *position = vaddr; 106163551ae0SDavid Gibson 106263551ae0SDavid Gibson return i; 106363551ae0SDavid Gibson } 10648f860591SZhang, Yanmin 10658f860591SZhang, Yanmin void hugetlb_change_protection(struct vm_area_struct *vma, 10668f860591SZhang, Yanmin unsigned long address, unsigned long end, pgprot_t newprot) 10678f860591SZhang, Yanmin { 10688f860591SZhang, Yanmin struct mm_struct *mm = vma->vm_mm; 10698f860591SZhang, Yanmin unsigned long start = address; 10708f860591SZhang, Yanmin pte_t *ptep; 10718f860591SZhang, Yanmin pte_t pte; 10728f860591SZhang, Yanmin 10738f860591SZhang, Yanmin BUG_ON(address >= end); 10748f860591SZhang, Yanmin flush_cache_range(vma, address, end); 10758f860591SZhang, Yanmin 107639dde65cSChen, Kenneth W spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); 10778f860591SZhang, Yanmin spin_lock(&mm->page_table_lock); 10788f860591SZhang, Yanmin for (; address < end; address += HPAGE_SIZE) { 10798f860591SZhang, Yanmin ptep = huge_pte_offset(mm, address); 10808f860591SZhang, Yanmin if (!ptep) 10818f860591SZhang, Yanmin continue; 108239dde65cSChen, Kenneth W if (huge_pmd_unshare(mm, &address, ptep)) 108339dde65cSChen, Kenneth W continue; 10848f860591SZhang, Yanmin if (!pte_none(*ptep)) { 10858f860591SZhang, Yanmin pte = huge_ptep_get_and_clear(mm, address, ptep); 10868f860591SZhang, Yanmin pte = pte_mkhuge(pte_modify(pte, newprot)); 10878f860591SZhang, Yanmin set_huge_pte_at(mm, address, ptep, pte); 10888f860591SZhang, Yanmin } 10898f860591SZhang, Yanmin } 10908f860591SZhang, Yanmin spin_unlock(&mm->page_table_lock); 109139dde65cSChen, Kenneth W spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); 10928f860591SZhang, Yanmin 10938f860591SZhang, Yanmin flush_tlb_range(vma, start, end); 10948f860591SZhang, Yanmin } 10958f860591SZhang, Yanmin 1096a43a8c39SChen, Kenneth W struct file_region { 1097a43a8c39SChen, Kenneth W struct list_head link; 1098a43a8c39SChen, Kenneth W long from; 1099a43a8c39SChen, Kenneth W long to; 1100a43a8c39SChen, Kenneth W }; 1101a43a8c39SChen, Kenneth W 1102a43a8c39SChen, Kenneth W static long region_add(struct list_head *head, long f, long t) 1103a43a8c39SChen, Kenneth W { 1104a43a8c39SChen, Kenneth W struct file_region *rg, *nrg, *trg; 1105a43a8c39SChen, Kenneth W 1106a43a8c39SChen, Kenneth W /* Locate the region we are either in or before. */ 1107a43a8c39SChen, Kenneth W list_for_each_entry(rg, head, link) 1108a43a8c39SChen, Kenneth W if (f <= rg->to) 1109a43a8c39SChen, Kenneth W break; 1110a43a8c39SChen, Kenneth W 1111a43a8c39SChen, Kenneth W /* Round our left edge to the current segment if it encloses us. */ 1112a43a8c39SChen, Kenneth W if (f > rg->from) 1113a43a8c39SChen, Kenneth W f = rg->from; 1114a43a8c39SChen, Kenneth W 1115a43a8c39SChen, Kenneth W /* Check for and consume any regions we now overlap with. */ 1116a43a8c39SChen, Kenneth W nrg = rg; 1117a43a8c39SChen, Kenneth W list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 1118a43a8c39SChen, Kenneth W if (&rg->link == head) 1119a43a8c39SChen, Kenneth W break; 1120a43a8c39SChen, Kenneth W if (rg->from > t) 1121a43a8c39SChen, Kenneth W break; 1122a43a8c39SChen, Kenneth W 1123a43a8c39SChen, Kenneth W /* If this area reaches higher then extend our area to 1124a43a8c39SChen, Kenneth W * include it completely. If this is not the first area 1125a43a8c39SChen, Kenneth W * which we intend to reuse, free it. */ 1126a43a8c39SChen, Kenneth W if (rg->to > t) 1127a43a8c39SChen, Kenneth W t = rg->to; 1128a43a8c39SChen, Kenneth W if (rg != nrg) { 1129a43a8c39SChen, Kenneth W list_del(&rg->link); 1130a43a8c39SChen, Kenneth W kfree(rg); 1131a43a8c39SChen, Kenneth W } 1132a43a8c39SChen, Kenneth W } 1133a43a8c39SChen, Kenneth W nrg->from = f; 1134a43a8c39SChen, Kenneth W nrg->to = t; 1135a43a8c39SChen, Kenneth W return 0; 1136a43a8c39SChen, Kenneth W } 1137a43a8c39SChen, Kenneth W 1138a43a8c39SChen, Kenneth W static long region_chg(struct list_head *head, long f, long t) 1139a43a8c39SChen, Kenneth W { 1140a43a8c39SChen, Kenneth W struct file_region *rg, *nrg; 1141a43a8c39SChen, Kenneth W long chg = 0; 1142a43a8c39SChen, Kenneth W 1143a43a8c39SChen, Kenneth W /* Locate the region we are before or in. */ 1144a43a8c39SChen, Kenneth W list_for_each_entry(rg, head, link) 1145a43a8c39SChen, Kenneth W if (f <= rg->to) 1146a43a8c39SChen, Kenneth W break; 1147a43a8c39SChen, Kenneth W 1148a43a8c39SChen, Kenneth W /* If we are below the current region then a new region is required. 1149a43a8c39SChen, Kenneth W * Subtle, allocate a new region at the position but make it zero 1150183ff22bSSimon Arlott * size such that we can guarantee to record the reservation. */ 1151a43a8c39SChen, Kenneth W if (&rg->link == head || t < rg->from) { 1152a43a8c39SChen, Kenneth W nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 1153c80544dcSStephen Hemminger if (!nrg) 1154a43a8c39SChen, Kenneth W return -ENOMEM; 1155a43a8c39SChen, Kenneth W nrg->from = f; 1156a43a8c39SChen, Kenneth W nrg->to = f; 1157a43a8c39SChen, Kenneth W INIT_LIST_HEAD(&nrg->link); 1158a43a8c39SChen, Kenneth W list_add(&nrg->link, rg->link.prev); 1159a43a8c39SChen, Kenneth W 1160a43a8c39SChen, Kenneth W return t - f; 1161a43a8c39SChen, Kenneth W } 1162a43a8c39SChen, Kenneth W 1163a43a8c39SChen, Kenneth W /* Round our left edge to the current segment if it encloses us. */ 1164a43a8c39SChen, Kenneth W if (f > rg->from) 1165a43a8c39SChen, Kenneth W f = rg->from; 1166a43a8c39SChen, Kenneth W chg = t - f; 1167a43a8c39SChen, Kenneth W 1168a43a8c39SChen, Kenneth W /* Check for and consume any regions we now overlap with. */ 1169a43a8c39SChen, Kenneth W list_for_each_entry(rg, rg->link.prev, link) { 1170a43a8c39SChen, Kenneth W if (&rg->link == head) 1171a43a8c39SChen, Kenneth W break; 1172a43a8c39SChen, Kenneth W if (rg->from > t) 1173a43a8c39SChen, Kenneth W return chg; 1174a43a8c39SChen, Kenneth W 1175a43a8c39SChen, Kenneth W /* We overlap with this area, if it extends futher than 1176a43a8c39SChen, Kenneth W * us then we must extend ourselves. Account for its 1177a43a8c39SChen, Kenneth W * existing reservation. */ 1178a43a8c39SChen, Kenneth W if (rg->to > t) { 1179a43a8c39SChen, Kenneth W chg += rg->to - t; 1180a43a8c39SChen, Kenneth W t = rg->to; 1181a43a8c39SChen, Kenneth W } 1182a43a8c39SChen, Kenneth W chg -= rg->to - rg->from; 1183a43a8c39SChen, Kenneth W } 1184a43a8c39SChen, Kenneth W return chg; 1185a43a8c39SChen, Kenneth W } 1186a43a8c39SChen, Kenneth W 1187a43a8c39SChen, Kenneth W static long region_truncate(struct list_head *head, long end) 1188a43a8c39SChen, Kenneth W { 1189a43a8c39SChen, Kenneth W struct file_region *rg, *trg; 1190a43a8c39SChen, Kenneth W long chg = 0; 1191a43a8c39SChen, Kenneth W 1192a43a8c39SChen, Kenneth W /* Locate the region we are either in or before. */ 1193a43a8c39SChen, Kenneth W list_for_each_entry(rg, head, link) 1194a43a8c39SChen, Kenneth W if (end <= rg->to) 1195a43a8c39SChen, Kenneth W break; 1196a43a8c39SChen, Kenneth W if (&rg->link == head) 1197a43a8c39SChen, Kenneth W return 0; 1198a43a8c39SChen, Kenneth W 1199a43a8c39SChen, Kenneth W /* If we are in the middle of a region then adjust it. */ 1200a43a8c39SChen, Kenneth W if (end > rg->from) { 1201a43a8c39SChen, Kenneth W chg = rg->to - end; 1202a43a8c39SChen, Kenneth W rg->to = end; 1203a43a8c39SChen, Kenneth W rg = list_entry(rg->link.next, typeof(*rg), link); 1204a43a8c39SChen, Kenneth W } 1205a43a8c39SChen, Kenneth W 1206a43a8c39SChen, Kenneth W /* Drop any remaining regions. */ 1207a43a8c39SChen, Kenneth W list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 1208a43a8c39SChen, Kenneth W if (&rg->link == head) 1209a43a8c39SChen, Kenneth W break; 1210a43a8c39SChen, Kenneth W chg += rg->to - rg->from; 1211a43a8c39SChen, Kenneth W list_del(&rg->link); 1212a43a8c39SChen, Kenneth W kfree(rg); 1213a43a8c39SChen, Kenneth W } 1214a43a8c39SChen, Kenneth W return chg; 1215a43a8c39SChen, Kenneth W } 1216a43a8c39SChen, Kenneth W 1217a43a8c39SChen, Kenneth W static int hugetlb_acct_memory(long delta) 1218a43a8c39SChen, Kenneth W { 1219a43a8c39SChen, Kenneth W int ret = -ENOMEM; 1220a43a8c39SChen, Kenneth W 1221a43a8c39SChen, Kenneth W spin_lock(&hugetlb_lock); 12228a630112SKen Chen /* 12238a630112SKen Chen * When cpuset is configured, it breaks the strict hugetlb page 12248a630112SKen Chen * reservation as the accounting is done on a global variable. Such 12258a630112SKen Chen * reservation is completely rubbish in the presence of cpuset because 12268a630112SKen Chen * the reservation is not checked against page availability for the 12278a630112SKen Chen * current cpuset. Application can still potentially OOM'ed by kernel 12288a630112SKen Chen * with lack of free htlb page in cpuset that the task is in. 12298a630112SKen Chen * Attempt to enforce strict accounting with cpuset is almost 12308a630112SKen Chen * impossible (or too ugly) because cpuset is too fluid that 12318a630112SKen Chen * task or memory node can be dynamically moved between cpusets. 12328a630112SKen Chen * 12338a630112SKen Chen * The change of semantics for shared hugetlb mapping with cpuset is 12348a630112SKen Chen * undesirable. However, in order to preserve some of the semantics, 12358a630112SKen Chen * we fall back to check against current free page availability as 12368a630112SKen Chen * a best attempt and hopefully to minimize the impact of changing 12378a630112SKen Chen * semantics that cpuset has. 12388a630112SKen Chen */ 1239e4e574b7SAdam Litke if (delta > 0) { 1240e4e574b7SAdam Litke if (gather_surplus_pages(delta) < 0) 1241e4e574b7SAdam Litke goto out; 1242e4e574b7SAdam Litke 1243ac09b3a1SAdam Litke if (delta > cpuset_mems_nr(free_huge_pages_node)) { 1244ac09b3a1SAdam Litke return_unused_surplus_pages(delta); 1245e4e574b7SAdam Litke goto out; 1246e4e574b7SAdam Litke } 1247ac09b3a1SAdam Litke } 1248e4e574b7SAdam Litke 1249e4e574b7SAdam Litke ret = 0; 1250e4e574b7SAdam Litke if (delta < 0) 1251e4e574b7SAdam Litke return_unused_surplus_pages((unsigned long) -delta); 1252e4e574b7SAdam Litke 1253e4e574b7SAdam Litke out: 1254e4e574b7SAdam Litke spin_unlock(&hugetlb_lock); 1255e4e574b7SAdam Litke return ret; 1256e4e574b7SAdam Litke } 1257e4e574b7SAdam Litke 1258e4e574b7SAdam Litke int hugetlb_reserve_pages(struct inode *inode, long from, long to) 1259e4e574b7SAdam Litke { 1260e4e574b7SAdam Litke long ret, chg; 1261e4e574b7SAdam Litke 1262e4e574b7SAdam Litke chg = region_chg(&inode->i_mapping->private_list, from, to); 1263e4e574b7SAdam Litke if (chg < 0) 1264e4e574b7SAdam Litke return chg; 12658a630112SKen Chen 126690d8b7e6SAdam Litke if (hugetlb_get_quota(inode->i_mapping, chg)) 126790d8b7e6SAdam Litke return -ENOSPC; 1268a43a8c39SChen, Kenneth W ret = hugetlb_acct_memory(chg); 126968842c9bSKen Chen if (ret < 0) { 127068842c9bSKen Chen hugetlb_put_quota(inode->i_mapping, chg); 1271a43a8c39SChen, Kenneth W return ret; 127268842c9bSKen Chen } 1273a43a8c39SChen, Kenneth W region_add(&inode->i_mapping->private_list, from, to); 1274a43a8c39SChen, Kenneth W return 0; 1275a43a8c39SChen, Kenneth W } 1276a43a8c39SChen, Kenneth W 1277a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) 1278a43a8c39SChen, Kenneth W { 1279a43a8c39SChen, Kenneth W long chg = region_truncate(&inode->i_mapping->private_list, offset); 128045c682a6SKen Chen 128145c682a6SKen Chen spin_lock(&inode->i_lock); 128245c682a6SKen Chen inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed; 128345c682a6SKen Chen spin_unlock(&inode->i_lock); 128445c682a6SKen Chen 128590d8b7e6SAdam Litke hugetlb_put_quota(inode->i_mapping, (chg - freed)); 128690d8b7e6SAdam Litke hugetlb_acct_memory(-(chg - freed)); 1287a43a8c39SChen, Kenneth W } 1288