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; 271da177e4SLinus Torvalds unsigned long max_huge_pages; 281da177e4SLinus Torvalds static struct list_head hugepage_freelists[MAX_NUMNODES]; 291da177e4SLinus Torvalds static unsigned int nr_huge_pages_node[MAX_NUMNODES]; 301da177e4SLinus Torvalds static unsigned int free_huge_pages_node[MAX_NUMNODES]; 317893d1d5SAdam Litke static unsigned int surplus_huge_pages_node[MAX_NUMNODES]; 32396faf03SMel Gorman static gfp_t htlb_alloc_mask = GFP_HIGHUSER; 33396faf03SMel Gorman unsigned long hugepages_treat_as_movable; 34d1c3fb1fSNishanth Aravamudan unsigned long nr_overcommit_huge_pages; 3563b4613cSNishanth Aravamudan static int hugetlb_next_nid; 36396faf03SMel Gorman 373935baa9SDavid Gibson /* 383935baa9SDavid Gibson * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages 393935baa9SDavid Gibson */ 403935baa9SDavid Gibson static DEFINE_SPINLOCK(hugetlb_lock); 410bd0f9fbSEric Paris 4279ac6ba4SDavid Gibson static void clear_huge_page(struct page *page, unsigned long addr) 4379ac6ba4SDavid Gibson { 4479ac6ba4SDavid Gibson int i; 4579ac6ba4SDavid Gibson 4679ac6ba4SDavid Gibson might_sleep(); 4779ac6ba4SDavid Gibson for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) { 4879ac6ba4SDavid Gibson cond_resched(); 49281e0e3bSRalf Baechle clear_user_highpage(page + i, addr + i * PAGE_SIZE); 5079ac6ba4SDavid Gibson } 5179ac6ba4SDavid Gibson } 5279ac6ba4SDavid Gibson 5379ac6ba4SDavid Gibson static void copy_huge_page(struct page *dst, struct page *src, 549de455b2SAtsushi Nemoto unsigned long addr, struct vm_area_struct *vma) 5579ac6ba4SDavid Gibson { 5679ac6ba4SDavid Gibson int i; 5779ac6ba4SDavid Gibson 5879ac6ba4SDavid Gibson might_sleep(); 5979ac6ba4SDavid Gibson for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) { 6079ac6ba4SDavid Gibson cond_resched(); 619de455b2SAtsushi Nemoto copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); 6279ac6ba4SDavid Gibson } 6379ac6ba4SDavid Gibson } 6479ac6ba4SDavid Gibson 651da177e4SLinus Torvalds static void enqueue_huge_page(struct page *page) 661da177e4SLinus Torvalds { 671da177e4SLinus Torvalds int nid = page_to_nid(page); 681da177e4SLinus Torvalds list_add(&page->lru, &hugepage_freelists[nid]); 691da177e4SLinus Torvalds free_huge_pages++; 701da177e4SLinus Torvalds free_huge_pages_node[nid]++; 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds 735da7ca86SChristoph Lameter static struct page *dequeue_huge_page(struct vm_area_struct *vma, 745da7ca86SChristoph Lameter unsigned long address) 751da177e4SLinus Torvalds { 7631a5c6e4SNishanth Aravamudan int nid; 771da177e4SLinus Torvalds struct page *page = NULL; 78480eccf9SLee Schermerhorn struct mempolicy *mpol; 79396faf03SMel Gorman struct zonelist *zonelist = huge_zonelist(vma, address, 80480eccf9SLee Schermerhorn htlb_alloc_mask, &mpol); 8196df9333SChristoph Lameter struct zone **z; 821da177e4SLinus Torvalds 8396df9333SChristoph Lameter for (z = zonelist->zones; *z; z++) { 8489fa3024SChristoph Lameter nid = zone_to_nid(*z); 85396faf03SMel Gorman if (cpuset_zone_allowed_softwall(*z, htlb_alloc_mask) && 863abf7afdSAndrew Morton !list_empty(&hugepage_freelists[nid])) { 871da177e4SLinus Torvalds page = list_entry(hugepage_freelists[nid].next, 881da177e4SLinus Torvalds struct page, lru); 891da177e4SLinus Torvalds list_del(&page->lru); 901da177e4SLinus Torvalds free_huge_pages--; 911da177e4SLinus Torvalds free_huge_pages_node[nid]--; 92e4e574b7SAdam Litke if (vma && vma->vm_flags & VM_MAYSHARE) 93e4e574b7SAdam Litke resv_huge_pages--; 945ab3ee7bSKen Chen break; 951da177e4SLinus Torvalds } 963abf7afdSAndrew Morton } 97480eccf9SLee Schermerhorn mpol_free(mpol); /* unref if mpol !NULL */ 981da177e4SLinus Torvalds return page; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds 1016af2acb6SAdam Litke static void update_and_free_page(struct page *page) 1026af2acb6SAdam Litke { 1036af2acb6SAdam Litke int i; 1046af2acb6SAdam Litke nr_huge_pages--; 1056af2acb6SAdam Litke nr_huge_pages_node[page_to_nid(page)]--; 1066af2acb6SAdam Litke for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) { 1076af2acb6SAdam Litke page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | 1086af2acb6SAdam Litke 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved | 1096af2acb6SAdam Litke 1 << PG_private | 1<< PG_writeback); 1106af2acb6SAdam Litke } 1116af2acb6SAdam Litke set_compound_page_dtor(page, NULL); 1126af2acb6SAdam Litke set_page_refcounted(page); 1136af2acb6SAdam Litke __free_pages(page, HUGETLB_PAGE_ORDER); 1146af2acb6SAdam Litke } 1156af2acb6SAdam Litke 11627a85ef1SDavid Gibson static void free_huge_page(struct page *page) 11727a85ef1SDavid Gibson { 1187893d1d5SAdam Litke int nid = page_to_nid(page); 119c79fb75eSAdam Litke struct address_space *mapping; 12027a85ef1SDavid Gibson 121c79fb75eSAdam Litke mapping = (struct address_space *) page_private(page); 1227893d1d5SAdam Litke BUG_ON(page_count(page)); 12327a85ef1SDavid Gibson INIT_LIST_HEAD(&page->lru); 12427a85ef1SDavid Gibson 12527a85ef1SDavid Gibson spin_lock(&hugetlb_lock); 1267893d1d5SAdam Litke if (surplus_huge_pages_node[nid]) { 1277893d1d5SAdam Litke update_and_free_page(page); 1287893d1d5SAdam Litke surplus_huge_pages--; 1297893d1d5SAdam Litke surplus_huge_pages_node[nid]--; 1307893d1d5SAdam Litke } else { 13127a85ef1SDavid Gibson enqueue_huge_page(page); 1327893d1d5SAdam Litke } 13327a85ef1SDavid Gibson spin_unlock(&hugetlb_lock); 134c79fb75eSAdam Litke if (mapping) 1359a119c05SAdam Litke hugetlb_put_quota(mapping, 1); 136c79fb75eSAdam Litke set_page_private(page, 0); 13727a85ef1SDavid Gibson } 13827a85ef1SDavid Gibson 1397893d1d5SAdam Litke /* 1407893d1d5SAdam Litke * Increment or decrement surplus_huge_pages. Keep node-specific counters 1417893d1d5SAdam Litke * balanced by operating on them in a round-robin fashion. 1427893d1d5SAdam Litke * Returns 1 if an adjustment was made. 1437893d1d5SAdam Litke */ 1447893d1d5SAdam Litke static int adjust_pool_surplus(int delta) 1457893d1d5SAdam Litke { 1467893d1d5SAdam Litke static int prev_nid; 1477893d1d5SAdam Litke int nid = prev_nid; 1487893d1d5SAdam Litke int ret = 0; 1497893d1d5SAdam Litke 1507893d1d5SAdam Litke VM_BUG_ON(delta != -1 && delta != 1); 1517893d1d5SAdam Litke do { 1527893d1d5SAdam Litke nid = next_node(nid, node_online_map); 1537893d1d5SAdam Litke if (nid == MAX_NUMNODES) 1547893d1d5SAdam Litke nid = first_node(node_online_map); 1557893d1d5SAdam Litke 1567893d1d5SAdam Litke /* To shrink on this node, there must be a surplus page */ 1577893d1d5SAdam Litke if (delta < 0 && !surplus_huge_pages_node[nid]) 1587893d1d5SAdam Litke continue; 1597893d1d5SAdam Litke /* Surplus cannot exceed the total number of pages */ 1607893d1d5SAdam Litke if (delta > 0 && surplus_huge_pages_node[nid] >= 1617893d1d5SAdam Litke nr_huge_pages_node[nid]) 1627893d1d5SAdam Litke continue; 1637893d1d5SAdam Litke 1647893d1d5SAdam Litke surplus_huge_pages += delta; 1657893d1d5SAdam Litke surplus_huge_pages_node[nid] += delta; 1667893d1d5SAdam Litke ret = 1; 1677893d1d5SAdam Litke break; 1687893d1d5SAdam Litke } while (nid != prev_nid); 1697893d1d5SAdam Litke 1707893d1d5SAdam Litke prev_nid = nid; 1717893d1d5SAdam Litke return ret; 1727893d1d5SAdam Litke } 1737893d1d5SAdam Litke 17463b4613cSNishanth Aravamudan static struct page *alloc_fresh_huge_page_node(int nid) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds struct page *page; 177f96efd58SJoe Jin 17863b4613cSNishanth Aravamudan page = alloc_pages_node(nid, 17963b4613cSNishanth Aravamudan htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|__GFP_NOWARN, 180f96efd58SJoe Jin HUGETLB_PAGE_ORDER); 1811da177e4SLinus Torvalds if (page) { 18233f2ef89SAndy Whitcroft set_compound_page_dtor(page, free_huge_page); 1830bd0f9fbSEric Paris spin_lock(&hugetlb_lock); 1841da177e4SLinus Torvalds nr_huge_pages++; 18563b4613cSNishanth Aravamudan nr_huge_pages_node[nid]++; 1860bd0f9fbSEric Paris spin_unlock(&hugetlb_lock); 187a482289dSNick Piggin put_page(page); /* free it into the hugepage allocator */ 1881da177e4SLinus Torvalds } 18963b4613cSNishanth Aravamudan 19063b4613cSNishanth Aravamudan return page; 19163b4613cSNishanth Aravamudan } 19263b4613cSNishanth Aravamudan 19363b4613cSNishanth Aravamudan static int alloc_fresh_huge_page(void) 19463b4613cSNishanth Aravamudan { 19563b4613cSNishanth Aravamudan struct page *page; 19663b4613cSNishanth Aravamudan int start_nid; 19763b4613cSNishanth Aravamudan int next_nid; 19863b4613cSNishanth Aravamudan int ret = 0; 19963b4613cSNishanth Aravamudan 20063b4613cSNishanth Aravamudan start_nid = hugetlb_next_nid; 20163b4613cSNishanth Aravamudan 20263b4613cSNishanth Aravamudan do { 20363b4613cSNishanth Aravamudan page = alloc_fresh_huge_page_node(hugetlb_next_nid); 20463b4613cSNishanth Aravamudan if (page) 20563b4613cSNishanth Aravamudan ret = 1; 20663b4613cSNishanth Aravamudan /* 20763b4613cSNishanth Aravamudan * Use a helper variable to find the next node and then 20863b4613cSNishanth Aravamudan * copy it back to hugetlb_next_nid afterwards: 20963b4613cSNishanth Aravamudan * otherwise there's a window in which a racer might 21063b4613cSNishanth Aravamudan * pass invalid nid MAX_NUMNODES to alloc_pages_node. 21163b4613cSNishanth Aravamudan * But we don't need to use a spin_lock here: it really 21263b4613cSNishanth Aravamudan * doesn't matter if occasionally a racer chooses the 21363b4613cSNishanth Aravamudan * same nid as we do. Move nid forward in the mask even 21463b4613cSNishanth Aravamudan * if we just successfully allocated a hugepage so that 21563b4613cSNishanth Aravamudan * the next caller gets hugepages on the next node. 21663b4613cSNishanth Aravamudan */ 21763b4613cSNishanth Aravamudan next_nid = next_node(hugetlb_next_nid, node_online_map); 21863b4613cSNishanth Aravamudan if (next_nid == MAX_NUMNODES) 21963b4613cSNishanth Aravamudan next_nid = first_node(node_online_map); 22063b4613cSNishanth Aravamudan hugetlb_next_nid = next_nid; 22163b4613cSNishanth Aravamudan } while (!page && hugetlb_next_nid != start_nid); 22263b4613cSNishanth Aravamudan 22363b4613cSNishanth Aravamudan return ret; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2267893d1d5SAdam Litke static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma, 2277893d1d5SAdam Litke unsigned long address) 2287893d1d5SAdam Litke { 2297893d1d5SAdam Litke struct page *page; 230d1c3fb1fSNishanth Aravamudan unsigned int nid; 2317893d1d5SAdam Litke 232d1c3fb1fSNishanth Aravamudan /* 233d1c3fb1fSNishanth Aravamudan * Assume we will successfully allocate the surplus page to 234d1c3fb1fSNishanth Aravamudan * prevent racing processes from causing the surplus to exceed 235d1c3fb1fSNishanth Aravamudan * overcommit 236d1c3fb1fSNishanth Aravamudan * 237d1c3fb1fSNishanth Aravamudan * This however introduces a different race, where a process B 238d1c3fb1fSNishanth Aravamudan * tries to grow the static hugepage pool while alloc_pages() is 239d1c3fb1fSNishanth Aravamudan * called by process A. B will only examine the per-node 240d1c3fb1fSNishanth Aravamudan * counters in determining if surplus huge pages can be 241d1c3fb1fSNishanth Aravamudan * converted to normal huge pages in adjust_pool_surplus(). A 242d1c3fb1fSNishanth Aravamudan * won't be able to increment the per-node counter, until the 243d1c3fb1fSNishanth Aravamudan * lock is dropped by B, but B doesn't drop hugetlb_lock until 244d1c3fb1fSNishanth Aravamudan * no more huge pages can be converted from surplus to normal 245d1c3fb1fSNishanth Aravamudan * state (and doesn't try to convert again). Thus, we have a 246d1c3fb1fSNishanth Aravamudan * case where a surplus huge page exists, the pool is grown, and 247d1c3fb1fSNishanth Aravamudan * the surplus huge page still exists after, even though it 248d1c3fb1fSNishanth Aravamudan * should just have been converted to a normal huge page. This 249d1c3fb1fSNishanth Aravamudan * does not leak memory, though, as the hugepage will be freed 250d1c3fb1fSNishanth Aravamudan * once it is out of use. It also does not allow the counters to 251d1c3fb1fSNishanth Aravamudan * go out of whack in adjust_pool_surplus() as we don't modify 252d1c3fb1fSNishanth Aravamudan * the node values until we've gotten the hugepage and only the 253d1c3fb1fSNishanth Aravamudan * per-node value is checked there. 254d1c3fb1fSNishanth Aravamudan */ 255d1c3fb1fSNishanth Aravamudan spin_lock(&hugetlb_lock); 256d1c3fb1fSNishanth Aravamudan if (surplus_huge_pages >= nr_overcommit_huge_pages) { 257d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 258d1c3fb1fSNishanth Aravamudan return NULL; 259d1c3fb1fSNishanth Aravamudan } else { 260d1c3fb1fSNishanth Aravamudan nr_huge_pages++; 261d1c3fb1fSNishanth Aravamudan surplus_huge_pages++; 262d1c3fb1fSNishanth Aravamudan } 263d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 264d1c3fb1fSNishanth Aravamudan 2657893d1d5SAdam Litke page = alloc_pages(htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN, 2667893d1d5SAdam Litke HUGETLB_PAGE_ORDER); 267d1c3fb1fSNishanth Aravamudan 2687893d1d5SAdam Litke spin_lock(&hugetlb_lock); 269d1c3fb1fSNishanth Aravamudan if (page) { 270d1c3fb1fSNishanth Aravamudan nid = page_to_nid(page); 271d1c3fb1fSNishanth Aravamudan set_compound_page_dtor(page, free_huge_page); 272d1c3fb1fSNishanth Aravamudan /* 273d1c3fb1fSNishanth Aravamudan * We incremented the global counters already 274d1c3fb1fSNishanth Aravamudan */ 275d1c3fb1fSNishanth Aravamudan nr_huge_pages_node[nid]++; 276d1c3fb1fSNishanth Aravamudan surplus_huge_pages_node[nid]++; 277d1c3fb1fSNishanth Aravamudan } else { 278d1c3fb1fSNishanth Aravamudan nr_huge_pages--; 279d1c3fb1fSNishanth Aravamudan surplus_huge_pages--; 2807893d1d5SAdam Litke } 281d1c3fb1fSNishanth Aravamudan spin_unlock(&hugetlb_lock); 2827893d1d5SAdam Litke 2837893d1d5SAdam Litke return page; 2847893d1d5SAdam Litke } 2857893d1d5SAdam Litke 286e4e574b7SAdam Litke /* 287e4e574b7SAdam Litke * Increase the hugetlb pool such that it can accomodate a reservation 288e4e574b7SAdam Litke * of size 'delta'. 289e4e574b7SAdam Litke */ 290e4e574b7SAdam Litke static int gather_surplus_pages(int delta) 291e4e574b7SAdam Litke { 292e4e574b7SAdam Litke struct list_head surplus_list; 293e4e574b7SAdam Litke struct page *page, *tmp; 294e4e574b7SAdam Litke int ret, i; 295e4e574b7SAdam Litke int needed, allocated; 296e4e574b7SAdam Litke 297e4e574b7SAdam Litke needed = (resv_huge_pages + delta) - free_huge_pages; 298e4e574b7SAdam Litke if (needed <= 0) 299e4e574b7SAdam Litke return 0; 300e4e574b7SAdam Litke 301e4e574b7SAdam Litke allocated = 0; 302e4e574b7SAdam Litke INIT_LIST_HEAD(&surplus_list); 303e4e574b7SAdam Litke 304e4e574b7SAdam Litke ret = -ENOMEM; 305e4e574b7SAdam Litke retry: 306e4e574b7SAdam Litke spin_unlock(&hugetlb_lock); 307e4e574b7SAdam Litke for (i = 0; i < needed; i++) { 308e4e574b7SAdam Litke page = alloc_buddy_huge_page(NULL, 0); 309e4e574b7SAdam Litke if (!page) { 310e4e574b7SAdam Litke /* 311e4e574b7SAdam Litke * We were not able to allocate enough pages to 312e4e574b7SAdam Litke * satisfy the entire reservation so we free what 313e4e574b7SAdam Litke * we've allocated so far. 314e4e574b7SAdam Litke */ 315e4e574b7SAdam Litke spin_lock(&hugetlb_lock); 316e4e574b7SAdam Litke needed = 0; 317e4e574b7SAdam Litke goto free; 318e4e574b7SAdam Litke } 319e4e574b7SAdam Litke 320e4e574b7SAdam Litke list_add(&page->lru, &surplus_list); 321e4e574b7SAdam Litke } 322e4e574b7SAdam Litke allocated += needed; 323e4e574b7SAdam Litke 324e4e574b7SAdam Litke /* 325e4e574b7SAdam Litke * After retaking hugetlb_lock, we need to recalculate 'needed' 326e4e574b7SAdam Litke * because either resv_huge_pages or free_huge_pages may have changed. 327e4e574b7SAdam Litke */ 328e4e574b7SAdam Litke spin_lock(&hugetlb_lock); 329e4e574b7SAdam Litke needed = (resv_huge_pages + delta) - (free_huge_pages + allocated); 330e4e574b7SAdam Litke if (needed > 0) 331e4e574b7SAdam Litke goto retry; 332e4e574b7SAdam Litke 333e4e574b7SAdam Litke /* 334e4e574b7SAdam Litke * The surplus_list now contains _at_least_ the number of extra pages 335e4e574b7SAdam Litke * needed to accomodate the reservation. Add the appropriate number 336e4e574b7SAdam Litke * of pages to the hugetlb pool and free the extras back to the buddy 337e4e574b7SAdam Litke * allocator. 338e4e574b7SAdam Litke */ 339e4e574b7SAdam Litke needed += allocated; 340e4e574b7SAdam Litke ret = 0; 341e4e574b7SAdam Litke free: 342e4e574b7SAdam Litke list_for_each_entry_safe(page, tmp, &surplus_list, lru) { 343e4e574b7SAdam Litke list_del(&page->lru); 344e4e574b7SAdam Litke if ((--needed) >= 0) 345e4e574b7SAdam Litke enqueue_huge_page(page); 346af767cbdSAdam Litke else { 347af767cbdSAdam Litke /* 348af767cbdSAdam Litke * Decrement the refcount and free the page using its 349af767cbdSAdam Litke * destructor. This must be done with hugetlb_lock 350af767cbdSAdam Litke * unlocked which is safe because free_huge_page takes 351af767cbdSAdam Litke * hugetlb_lock before deciding how to free the page. 352af767cbdSAdam Litke */ 353af767cbdSAdam Litke spin_unlock(&hugetlb_lock); 354af767cbdSAdam Litke put_page(page); 355af767cbdSAdam Litke spin_lock(&hugetlb_lock); 356af767cbdSAdam Litke } 357e4e574b7SAdam Litke } 358e4e574b7SAdam Litke 359e4e574b7SAdam Litke return ret; 360e4e574b7SAdam Litke } 361e4e574b7SAdam Litke 362e4e574b7SAdam Litke /* 363e4e574b7SAdam Litke * When releasing a hugetlb pool reservation, any surplus pages that were 364e4e574b7SAdam Litke * allocated to satisfy the reservation must be explicitly freed if they were 365e4e574b7SAdam Litke * never used. 366e4e574b7SAdam Litke */ 3678cde045cSAdrian Bunk static void return_unused_surplus_pages(unsigned long unused_resv_pages) 368e4e574b7SAdam Litke { 369e4e574b7SAdam Litke static int nid = -1; 370e4e574b7SAdam Litke struct page *page; 371e4e574b7SAdam Litke unsigned long nr_pages; 372e4e574b7SAdam Litke 373e4e574b7SAdam Litke nr_pages = min(unused_resv_pages, surplus_huge_pages); 374e4e574b7SAdam Litke 375e4e574b7SAdam Litke while (nr_pages) { 376e4e574b7SAdam Litke nid = next_node(nid, node_online_map); 377e4e574b7SAdam Litke if (nid == MAX_NUMNODES) 378e4e574b7SAdam Litke nid = first_node(node_online_map); 379e4e574b7SAdam Litke 380e4e574b7SAdam Litke if (!surplus_huge_pages_node[nid]) 381e4e574b7SAdam Litke continue; 382e4e574b7SAdam Litke 383e4e574b7SAdam Litke if (!list_empty(&hugepage_freelists[nid])) { 384e4e574b7SAdam Litke page = list_entry(hugepage_freelists[nid].next, 385e4e574b7SAdam Litke struct page, lru); 386e4e574b7SAdam Litke list_del(&page->lru); 387e4e574b7SAdam Litke update_and_free_page(page); 388e4e574b7SAdam Litke free_huge_pages--; 389e4e574b7SAdam Litke free_huge_pages_node[nid]--; 390e4e574b7SAdam Litke surplus_huge_pages--; 391e4e574b7SAdam Litke surplus_huge_pages_node[nid]--; 392e4e574b7SAdam Litke nr_pages--; 393e4e574b7SAdam Litke } 394e4e574b7SAdam Litke } 395e4e574b7SAdam Litke } 396e4e574b7SAdam Litke 397348ea204SAdam Litke 398348ea204SAdam Litke static struct page *alloc_huge_page_shared(struct vm_area_struct *vma, 399348ea204SAdam Litke unsigned long addr) 400348ea204SAdam Litke { 401348ea204SAdam Litke struct page *page; 402348ea204SAdam Litke 403348ea204SAdam Litke spin_lock(&hugetlb_lock); 404348ea204SAdam Litke page = dequeue_huge_page(vma, addr); 405348ea204SAdam Litke spin_unlock(&hugetlb_lock); 40690d8b7e6SAdam Litke return page ? page : ERR_PTR(-VM_FAULT_OOM); 407348ea204SAdam Litke } 408348ea204SAdam Litke 409348ea204SAdam Litke static struct page *alloc_huge_page_private(struct vm_area_struct *vma, 41027a85ef1SDavid Gibson unsigned long addr) 4111da177e4SLinus Torvalds { 4127893d1d5SAdam Litke struct page *page = NULL; 4131da177e4SLinus Torvalds 41490d8b7e6SAdam Litke if (hugetlb_get_quota(vma->vm_file->f_mapping, 1)) 41590d8b7e6SAdam Litke return ERR_PTR(-VM_FAULT_SIGBUS); 41690d8b7e6SAdam Litke 4171da177e4SLinus Torvalds spin_lock(&hugetlb_lock); 418348ea204SAdam Litke if (free_huge_pages > resv_huge_pages) 419b45b5bd6SDavid Gibson page = dequeue_huge_page(vma, addr); 420348ea204SAdam Litke spin_unlock(&hugetlb_lock); 42168842c9bSKen Chen if (!page) { 4227893d1d5SAdam Litke page = alloc_buddy_huge_page(vma, addr); 42368842c9bSKen Chen if (!page) { 42468842c9bSKen Chen hugetlb_put_quota(vma->vm_file->f_mapping, 1); 42568842c9bSKen Chen return ERR_PTR(-VM_FAULT_OOM); 42668842c9bSKen Chen } 42768842c9bSKen Chen } 42868842c9bSKen Chen return page; 429348ea204SAdam Litke } 4307893d1d5SAdam Litke 431348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma, 432348ea204SAdam Litke unsigned long addr) 433348ea204SAdam Litke { 434348ea204SAdam Litke struct page *page; 4352fc39cecSAdam Litke struct address_space *mapping = vma->vm_file->f_mapping; 4362fc39cecSAdam Litke 437348ea204SAdam Litke if (vma->vm_flags & VM_MAYSHARE) 438348ea204SAdam Litke page = alloc_huge_page_shared(vma, addr); 439348ea204SAdam Litke else 440348ea204SAdam Litke page = alloc_huge_page_private(vma, addr); 44190d8b7e6SAdam Litke 44290d8b7e6SAdam Litke if (!IS_ERR(page)) { 443348ea204SAdam Litke set_page_refcounted(page); 4442fc39cecSAdam Litke set_page_private(page, (unsigned long) mapping); 44590d8b7e6SAdam Litke } 4467893d1d5SAdam Litke return page; 447b45b5bd6SDavid Gibson } 448b45b5bd6SDavid Gibson 4491da177e4SLinus Torvalds static int __init hugetlb_init(void) 4501da177e4SLinus Torvalds { 4511da177e4SLinus Torvalds unsigned long i; 4521da177e4SLinus Torvalds 4533c726f8dSBenjamin Herrenschmidt if (HPAGE_SHIFT == 0) 4543c726f8dSBenjamin Herrenschmidt return 0; 4553c726f8dSBenjamin Herrenschmidt 4561da177e4SLinus Torvalds for (i = 0; i < MAX_NUMNODES; ++i) 4571da177e4SLinus Torvalds INIT_LIST_HEAD(&hugepage_freelists[i]); 4581da177e4SLinus Torvalds 45963b4613cSNishanth Aravamudan hugetlb_next_nid = first_node(node_online_map); 46063b4613cSNishanth Aravamudan 4611da177e4SLinus Torvalds for (i = 0; i < max_huge_pages; ++i) { 462a482289dSNick Piggin if (!alloc_fresh_huge_page()) 4631da177e4SLinus Torvalds break; 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds max_huge_pages = free_huge_pages = nr_huge_pages = i; 4661da177e4SLinus Torvalds printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages); 4671da177e4SLinus Torvalds return 0; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds module_init(hugetlb_init); 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds static int __init hugetlb_setup(char *s) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds if (sscanf(s, "%lu", &max_huge_pages) <= 0) 4741da177e4SLinus Torvalds max_huge_pages = 0; 4751da177e4SLinus Torvalds return 1; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds __setup("hugepages=", hugetlb_setup); 4781da177e4SLinus Torvalds 4798a630112SKen Chen static unsigned int cpuset_mems_nr(unsigned int *array) 4808a630112SKen Chen { 4818a630112SKen Chen int node; 4828a630112SKen Chen unsigned int nr = 0; 4838a630112SKen Chen 4848a630112SKen Chen for_each_node_mask(node, cpuset_current_mems_allowed) 4858a630112SKen Chen nr += array[node]; 4868a630112SKen Chen 4878a630112SKen Chen return nr; 4888a630112SKen Chen } 4898a630112SKen Chen 4901da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 4911da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM 4921da177e4SLinus Torvalds static void try_to_free_low(unsigned long count) 4931da177e4SLinus Torvalds { 4944415cc8dSChristoph Lameter int i; 4954415cc8dSChristoph Lameter 4961da177e4SLinus Torvalds for (i = 0; i < MAX_NUMNODES; ++i) { 4971da177e4SLinus Torvalds struct page *page, *next; 4981da177e4SLinus Torvalds list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) { 4996b0c880dSAdam Litke if (count >= nr_huge_pages) 5006b0c880dSAdam Litke return; 5011da177e4SLinus Torvalds if (PageHighMem(page)) 5021da177e4SLinus Torvalds continue; 5031da177e4SLinus Torvalds list_del(&page->lru); 5041da177e4SLinus Torvalds update_and_free_page(page); 5051da177e4SLinus Torvalds free_huge_pages--; 5064415cc8dSChristoph Lameter free_huge_pages_node[page_to_nid(page)]--; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds #else 5111da177e4SLinus Torvalds static inline void try_to_free_low(unsigned long count) 5121da177e4SLinus Torvalds { 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds #endif 5151da177e4SLinus Torvalds 5167893d1d5SAdam Litke #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages) 5171da177e4SLinus Torvalds static unsigned long set_max_huge_pages(unsigned long count) 5181da177e4SLinus Torvalds { 5197893d1d5SAdam Litke unsigned long min_count, ret; 5201da177e4SLinus Torvalds 5217893d1d5SAdam Litke /* 5227893d1d5SAdam Litke * Increase the pool size 5237893d1d5SAdam Litke * First take pages out of surplus state. Then make up the 5247893d1d5SAdam Litke * remaining difference by allocating fresh huge pages. 525d1c3fb1fSNishanth Aravamudan * 526d1c3fb1fSNishanth Aravamudan * We might race with alloc_buddy_huge_page() here and be unable 527d1c3fb1fSNishanth Aravamudan * to convert a surplus huge page to a normal huge page. That is 528d1c3fb1fSNishanth Aravamudan * not critical, though, it just means the overall size of the 529d1c3fb1fSNishanth Aravamudan * pool might be one hugepage larger than it needs to be, but 530d1c3fb1fSNishanth Aravamudan * within all the constraints specified by the sysctls. 5317893d1d5SAdam Litke */ 5321da177e4SLinus Torvalds spin_lock(&hugetlb_lock); 5337893d1d5SAdam Litke while (surplus_huge_pages && count > persistent_huge_pages) { 5347893d1d5SAdam Litke if (!adjust_pool_surplus(-1)) 5357893d1d5SAdam Litke break; 5367893d1d5SAdam Litke } 5377893d1d5SAdam Litke 5387893d1d5SAdam Litke while (count > persistent_huge_pages) { 5397893d1d5SAdam Litke int ret; 5407893d1d5SAdam Litke /* 5417893d1d5SAdam Litke * If this allocation races such that we no longer need the 5427893d1d5SAdam Litke * page, free_huge_page will handle it by freeing the page 5437893d1d5SAdam Litke * and reducing the surplus. 5447893d1d5SAdam Litke */ 5457893d1d5SAdam Litke spin_unlock(&hugetlb_lock); 5467893d1d5SAdam Litke ret = alloc_fresh_huge_page(); 5477893d1d5SAdam Litke spin_lock(&hugetlb_lock); 5487893d1d5SAdam Litke if (!ret) 5497893d1d5SAdam Litke goto out; 5507893d1d5SAdam Litke 5517893d1d5SAdam Litke } 5527893d1d5SAdam Litke 5537893d1d5SAdam Litke /* 5547893d1d5SAdam Litke * Decrease the pool size 5557893d1d5SAdam Litke * First return free pages to the buddy allocator (being careful 5567893d1d5SAdam Litke * to keep enough around to satisfy reservations). Then place 5577893d1d5SAdam Litke * pages into surplus state as needed so the pool will shrink 5587893d1d5SAdam Litke * to the desired size as pages become free. 559d1c3fb1fSNishanth Aravamudan * 560d1c3fb1fSNishanth Aravamudan * By placing pages into the surplus state independent of the 561d1c3fb1fSNishanth Aravamudan * overcommit value, we are allowing the surplus pool size to 562d1c3fb1fSNishanth Aravamudan * exceed overcommit. There are few sane options here. Since 563d1c3fb1fSNishanth Aravamudan * alloc_buddy_huge_page() is checking the global counter, 564d1c3fb1fSNishanth Aravamudan * though, we'll note that we're not allowed to exceed surplus 565d1c3fb1fSNishanth Aravamudan * and won't grow the pool anywhere else. Not until one of the 566d1c3fb1fSNishanth Aravamudan * sysctls are changed, or the surplus pages go out of use. 5677893d1d5SAdam Litke */ 5686b0c880dSAdam Litke min_count = resv_huge_pages + nr_huge_pages - free_huge_pages; 5696b0c880dSAdam Litke min_count = max(count, min_count); 5707893d1d5SAdam Litke try_to_free_low(min_count); 5717893d1d5SAdam Litke while (min_count < persistent_huge_pages) { 5725da7ca86SChristoph Lameter struct page *page = dequeue_huge_page(NULL, 0); 5731da177e4SLinus Torvalds if (!page) 5741da177e4SLinus Torvalds break; 5751da177e4SLinus Torvalds update_and_free_page(page); 5761da177e4SLinus Torvalds } 5777893d1d5SAdam Litke while (count < persistent_huge_pages) { 5787893d1d5SAdam Litke if (!adjust_pool_surplus(1)) 5797893d1d5SAdam Litke break; 5807893d1d5SAdam Litke } 5817893d1d5SAdam Litke out: 5827893d1d5SAdam Litke ret = persistent_huge_pages; 5831da177e4SLinus Torvalds spin_unlock(&hugetlb_lock); 5847893d1d5SAdam Litke return ret; 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds int hugetlb_sysctl_handler(struct ctl_table *table, int write, 5881da177e4SLinus Torvalds struct file *file, void __user *buffer, 5891da177e4SLinus Torvalds size_t *length, loff_t *ppos) 5901da177e4SLinus Torvalds { 5911da177e4SLinus Torvalds proc_doulongvec_minmax(table, write, file, buffer, length, ppos); 5921da177e4SLinus Torvalds max_huge_pages = set_max_huge_pages(max_huge_pages); 5931da177e4SLinus Torvalds return 0; 5941da177e4SLinus Torvalds } 595396faf03SMel Gorman 596396faf03SMel Gorman int hugetlb_treat_movable_handler(struct ctl_table *table, int write, 597396faf03SMel Gorman struct file *file, void __user *buffer, 598396faf03SMel Gorman size_t *length, loff_t *ppos) 599396faf03SMel Gorman { 600396faf03SMel Gorman proc_dointvec(table, write, file, buffer, length, ppos); 601396faf03SMel Gorman if (hugepages_treat_as_movable) 602396faf03SMel Gorman htlb_alloc_mask = GFP_HIGHUSER_MOVABLE; 603396faf03SMel Gorman else 604396faf03SMel Gorman htlb_alloc_mask = GFP_HIGHUSER; 605396faf03SMel Gorman return 0; 606396faf03SMel Gorman } 607396faf03SMel Gorman 6081da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds int hugetlb_report_meminfo(char *buf) 6111da177e4SLinus Torvalds { 6121da177e4SLinus Torvalds return sprintf(buf, 6131da177e4SLinus Torvalds "HugePages_Total: %5lu\n" 6141da177e4SLinus Torvalds "HugePages_Free: %5lu\n" 615b45b5bd6SDavid Gibson "HugePages_Rsvd: %5lu\n" 6167893d1d5SAdam Litke "HugePages_Surp: %5lu\n" 6171da177e4SLinus Torvalds "Hugepagesize: %5lu kB\n", 6181da177e4SLinus Torvalds nr_huge_pages, 6191da177e4SLinus Torvalds free_huge_pages, 620a43a8c39SChen, Kenneth W resv_huge_pages, 6217893d1d5SAdam Litke surplus_huge_pages, 6221da177e4SLinus Torvalds HPAGE_SIZE/1024); 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf) 6261da177e4SLinus Torvalds { 6271da177e4SLinus Torvalds return sprintf(buf, 6281da177e4SLinus Torvalds "Node %d HugePages_Total: %5u\n" 6291da177e4SLinus Torvalds "Node %d HugePages_Free: %5u\n", 6301da177e4SLinus Torvalds nid, nr_huge_pages_node[nid], 6311da177e4SLinus Torvalds nid, free_huge_pages_node[nid]); 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 6351da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE); 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds /* 6411da177e4SLinus Torvalds * We cannot handle pagefaults against hugetlb pages at all. They cause 6421da177e4SLinus Torvalds * handle_mm_fault() to try to instantiate regular-sized pages in the 6431da177e4SLinus Torvalds * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get 6441da177e4SLinus Torvalds * this far. 6451da177e4SLinus Torvalds */ 646d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 6471da177e4SLinus Torvalds { 6481da177e4SLinus Torvalds BUG(); 649d0217ac0SNick Piggin return 0; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds struct vm_operations_struct hugetlb_vm_ops = { 653d0217ac0SNick Piggin .fault = hugetlb_vm_op_fault, 6541da177e4SLinus Torvalds }; 6551da177e4SLinus Torvalds 6561e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, 6571e8f889bSDavid Gibson int writable) 65863551ae0SDavid Gibson { 65963551ae0SDavid Gibson pte_t entry; 66063551ae0SDavid Gibson 6611e8f889bSDavid Gibson if (writable) { 66263551ae0SDavid Gibson entry = 66363551ae0SDavid Gibson pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); 66463551ae0SDavid Gibson } else { 66563551ae0SDavid Gibson entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot)); 66663551ae0SDavid Gibson } 66763551ae0SDavid Gibson entry = pte_mkyoung(entry); 66863551ae0SDavid Gibson entry = pte_mkhuge(entry); 66963551ae0SDavid Gibson 67063551ae0SDavid Gibson return entry; 67163551ae0SDavid Gibson } 67263551ae0SDavid Gibson 6731e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma, 6741e8f889bSDavid Gibson unsigned long address, pte_t *ptep) 6751e8f889bSDavid Gibson { 6761e8f889bSDavid Gibson pte_t entry; 6771e8f889bSDavid Gibson 6781e8f889bSDavid Gibson entry = pte_mkwrite(pte_mkdirty(*ptep)); 6798dab5241SBenjamin Herrenschmidt if (ptep_set_access_flags(vma, address, ptep, entry, 1)) { 6801e8f889bSDavid Gibson update_mmu_cache(vma, address, entry); 6811e8f889bSDavid Gibson } 6828dab5241SBenjamin Herrenschmidt } 6831e8f889bSDavid Gibson 6841e8f889bSDavid Gibson 68563551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 68663551ae0SDavid Gibson struct vm_area_struct *vma) 68763551ae0SDavid Gibson { 68863551ae0SDavid Gibson pte_t *src_pte, *dst_pte, entry; 68963551ae0SDavid Gibson struct page *ptepage; 6901c59827dSHugh Dickins unsigned long addr; 6911e8f889bSDavid Gibson int cow; 6921e8f889bSDavid Gibson 6931e8f889bSDavid Gibson cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 69463551ae0SDavid Gibson 6951c59827dSHugh Dickins for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) { 696c74df32cSHugh Dickins src_pte = huge_pte_offset(src, addr); 697c74df32cSHugh Dickins if (!src_pte) 698c74df32cSHugh Dickins continue; 69963551ae0SDavid Gibson dst_pte = huge_pte_alloc(dst, addr); 70063551ae0SDavid Gibson if (!dst_pte) 70163551ae0SDavid Gibson goto nomem; 702c5c99429SLarry Woodman 703c5c99429SLarry Woodman /* If the pagetables are shared don't copy or take references */ 704c5c99429SLarry Woodman if (dst_pte == src_pte) 705c5c99429SLarry Woodman continue; 706c5c99429SLarry Woodman 707c74df32cSHugh Dickins spin_lock(&dst->page_table_lock); 7081c59827dSHugh Dickins spin_lock(&src->page_table_lock); 709c74df32cSHugh Dickins if (!pte_none(*src_pte)) { 7101e8f889bSDavid Gibson if (cow) 7111e8f889bSDavid Gibson ptep_set_wrprotect(src, addr, src_pte); 71263551ae0SDavid Gibson entry = *src_pte; 71363551ae0SDavid Gibson ptepage = pte_page(entry); 71463551ae0SDavid Gibson get_page(ptepage); 71563551ae0SDavid Gibson set_huge_pte_at(dst, addr, dst_pte, entry); 7161c59827dSHugh Dickins } 7171c59827dSHugh Dickins spin_unlock(&src->page_table_lock); 718c74df32cSHugh Dickins spin_unlock(&dst->page_table_lock); 71963551ae0SDavid Gibson } 72063551ae0SDavid Gibson return 0; 72163551ae0SDavid Gibson 72263551ae0SDavid Gibson nomem: 72363551ae0SDavid Gibson return -ENOMEM; 72463551ae0SDavid Gibson } 72563551ae0SDavid Gibson 726502717f4SChen, Kenneth W void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 72763551ae0SDavid Gibson unsigned long end) 72863551ae0SDavid Gibson { 72963551ae0SDavid Gibson struct mm_struct *mm = vma->vm_mm; 73063551ae0SDavid Gibson unsigned long address; 731c7546f8fSDavid Gibson pte_t *ptep; 73263551ae0SDavid Gibson pte_t pte; 73363551ae0SDavid Gibson struct page *page; 734fe1668aeSChen, Kenneth W struct page *tmp; 735c0a499c2SChen, Kenneth W /* 736c0a499c2SChen, Kenneth W * A page gathering list, protected by per file i_mmap_lock. The 737c0a499c2SChen, Kenneth W * lock is used to avoid list corruption from multiple unmapping 738c0a499c2SChen, Kenneth W * of the same page since we are using page->lru. 739c0a499c2SChen, Kenneth W */ 740fe1668aeSChen, Kenneth W LIST_HEAD(page_list); 74163551ae0SDavid Gibson 74263551ae0SDavid Gibson WARN_ON(!is_vm_hugetlb_page(vma)); 74363551ae0SDavid Gibson BUG_ON(start & ~HPAGE_MASK); 74463551ae0SDavid Gibson BUG_ON(end & ~HPAGE_MASK); 74563551ae0SDavid Gibson 746508034a3SHugh Dickins spin_lock(&mm->page_table_lock); 74763551ae0SDavid Gibson for (address = start; address < end; address += HPAGE_SIZE) { 748c7546f8fSDavid Gibson ptep = huge_pte_offset(mm, address); 749c7546f8fSDavid Gibson if (!ptep) 750c7546f8fSDavid Gibson continue; 751c7546f8fSDavid Gibson 75239dde65cSChen, Kenneth W if (huge_pmd_unshare(mm, &address, ptep)) 75339dde65cSChen, Kenneth W continue; 75439dde65cSChen, Kenneth W 755c7546f8fSDavid Gibson pte = huge_ptep_get_and_clear(mm, address, ptep); 75663551ae0SDavid Gibson if (pte_none(pte)) 75763551ae0SDavid Gibson continue; 758c7546f8fSDavid Gibson 75963551ae0SDavid Gibson page = pte_page(pte); 7606649a386SKen Chen if (pte_dirty(pte)) 7616649a386SKen Chen set_page_dirty(page); 762fe1668aeSChen, Kenneth W list_add(&page->lru, &page_list); 76363551ae0SDavid Gibson } 7641da177e4SLinus Torvalds spin_unlock(&mm->page_table_lock); 765508034a3SHugh Dickins flush_tlb_range(vma, start, end); 766fe1668aeSChen, Kenneth W list_for_each_entry_safe(page, tmp, &page_list, lru) { 767fe1668aeSChen, Kenneth W list_del(&page->lru); 768fe1668aeSChen, Kenneth W put_page(page); 769fe1668aeSChen, Kenneth W } 7701da177e4SLinus Torvalds } 77163551ae0SDavid Gibson 772502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 773502717f4SChen, Kenneth W unsigned long end) 774502717f4SChen, Kenneth W { 775502717f4SChen, Kenneth W /* 776502717f4SChen, Kenneth W * It is undesirable to test vma->vm_file as it should be non-null 777502717f4SChen, Kenneth W * for valid hugetlb area. However, vm_file will be NULL in the error 778502717f4SChen, Kenneth W * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails, 779502717f4SChen, Kenneth W * do_mmap_pgoff() nullifies vma->vm_file before calling this function 780502717f4SChen, Kenneth W * to clean up. Since no pte has actually been setup, it is safe to 781502717f4SChen, Kenneth W * do nothing in this case. 782502717f4SChen, Kenneth W */ 783502717f4SChen, Kenneth W if (vma->vm_file) { 784502717f4SChen, Kenneth W spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); 785502717f4SChen, Kenneth W __unmap_hugepage_range(vma, start, end); 786502717f4SChen, Kenneth W spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); 787502717f4SChen, Kenneth W } 788502717f4SChen, Kenneth W } 789502717f4SChen, Kenneth W 7901e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, 7911e8f889bSDavid Gibson unsigned long address, pte_t *ptep, pte_t pte) 7921e8f889bSDavid Gibson { 7931e8f889bSDavid Gibson struct page *old_page, *new_page; 79479ac6ba4SDavid Gibson int avoidcopy; 7951e8f889bSDavid Gibson 7961e8f889bSDavid Gibson old_page = pte_page(pte); 7971e8f889bSDavid Gibson 7981e8f889bSDavid Gibson /* If no-one else is actually using this page, avoid the copy 7991e8f889bSDavid Gibson * and just make the page writable */ 8001e8f889bSDavid Gibson avoidcopy = (page_count(old_page) == 1); 8011e8f889bSDavid Gibson if (avoidcopy) { 8021e8f889bSDavid Gibson set_huge_ptep_writable(vma, address, ptep); 80383c54070SNick Piggin return 0; 8041e8f889bSDavid Gibson } 8051e8f889bSDavid Gibson 8061e8f889bSDavid Gibson page_cache_get(old_page); 8075da7ca86SChristoph Lameter new_page = alloc_huge_page(vma, address); 8081e8f889bSDavid Gibson 8092fc39cecSAdam Litke if (IS_ERR(new_page)) { 8101e8f889bSDavid Gibson page_cache_release(old_page); 8112fc39cecSAdam Litke return -PTR_ERR(new_page); 8121e8f889bSDavid Gibson } 8131e8f889bSDavid Gibson 8141e8f889bSDavid Gibson spin_unlock(&mm->page_table_lock); 8159de455b2SAtsushi Nemoto copy_huge_page(new_page, old_page, address, vma); 8161e8f889bSDavid Gibson spin_lock(&mm->page_table_lock); 8171e8f889bSDavid Gibson 8181e8f889bSDavid Gibson ptep = huge_pte_offset(mm, address & HPAGE_MASK); 8191e8f889bSDavid Gibson if (likely(pte_same(*ptep, pte))) { 8201e8f889bSDavid Gibson /* Break COW */ 8211e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, 8221e8f889bSDavid Gibson make_huge_pte(vma, new_page, 1)); 8231e8f889bSDavid Gibson /* Make the old page be freed below */ 8241e8f889bSDavid Gibson new_page = old_page; 8251e8f889bSDavid Gibson } 8261e8f889bSDavid Gibson page_cache_release(new_page); 8271e8f889bSDavid Gibson page_cache_release(old_page); 82883c54070SNick Piggin return 0; 8291e8f889bSDavid Gibson } 8301e8f889bSDavid Gibson 831a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, 8321e8f889bSDavid Gibson unsigned long address, pte_t *ptep, int write_access) 833ac9b9c66SHugh Dickins { 834ac9b9c66SHugh Dickins int ret = VM_FAULT_SIGBUS; 8354c887265SAdam Litke unsigned long idx; 8364c887265SAdam Litke unsigned long size; 8374c887265SAdam Litke struct page *page; 8384c887265SAdam Litke struct address_space *mapping; 8391e8f889bSDavid Gibson pte_t new_pte; 8404c887265SAdam Litke 8414c887265SAdam Litke mapping = vma->vm_file->f_mapping; 8424c887265SAdam Litke idx = ((address - vma->vm_start) >> HPAGE_SHIFT) 8434c887265SAdam Litke + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT)); 8444c887265SAdam Litke 8454c887265SAdam Litke /* 8464c887265SAdam Litke * Use page lock to guard against racing truncation 8474c887265SAdam Litke * before we get page_table_lock. 8484c887265SAdam Litke */ 8496bda666aSChristoph Lameter retry: 8506bda666aSChristoph Lameter page = find_lock_page(mapping, idx); 8516bda666aSChristoph Lameter if (!page) { 852ebed4bfcSHugh Dickins size = i_size_read(mapping->host) >> HPAGE_SHIFT; 853ebed4bfcSHugh Dickins if (idx >= size) 854ebed4bfcSHugh Dickins goto out; 8556bda666aSChristoph Lameter page = alloc_huge_page(vma, address); 8562fc39cecSAdam Litke if (IS_ERR(page)) { 8572fc39cecSAdam Litke ret = -PTR_ERR(page); 8586bda666aSChristoph Lameter goto out; 8596bda666aSChristoph Lameter } 86079ac6ba4SDavid Gibson clear_huge_page(page, address); 861ac9b9c66SHugh Dickins 8626bda666aSChristoph Lameter if (vma->vm_flags & VM_SHARED) { 8636bda666aSChristoph Lameter int err; 86445c682a6SKen Chen struct inode *inode = mapping->host; 8656bda666aSChristoph Lameter 8666bda666aSChristoph Lameter err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); 8676bda666aSChristoph Lameter if (err) { 8686bda666aSChristoph Lameter put_page(page); 8696bda666aSChristoph Lameter if (err == -EEXIST) 8706bda666aSChristoph Lameter goto retry; 8716bda666aSChristoph Lameter goto out; 8726bda666aSChristoph Lameter } 87345c682a6SKen Chen 87445c682a6SKen Chen spin_lock(&inode->i_lock); 87545c682a6SKen Chen inode->i_blocks += BLOCKS_PER_HUGEPAGE; 87645c682a6SKen Chen spin_unlock(&inode->i_lock); 8776bda666aSChristoph Lameter } else 8786bda666aSChristoph Lameter lock_page(page); 8796bda666aSChristoph Lameter } 8801e8f889bSDavid Gibson 881ac9b9c66SHugh Dickins spin_lock(&mm->page_table_lock); 8824c887265SAdam Litke size = i_size_read(mapping->host) >> HPAGE_SHIFT; 8834c887265SAdam Litke if (idx >= size) 8844c887265SAdam Litke goto backout; 8854c887265SAdam Litke 88683c54070SNick Piggin ret = 0; 88786e5216fSAdam Litke if (!pte_none(*ptep)) 8884c887265SAdam Litke goto backout; 8894c887265SAdam Litke 8901e8f889bSDavid Gibson new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) 8911e8f889bSDavid Gibson && (vma->vm_flags & VM_SHARED))); 8921e8f889bSDavid Gibson set_huge_pte_at(mm, address, ptep, new_pte); 8931e8f889bSDavid Gibson 8941e8f889bSDavid Gibson if (write_access && !(vma->vm_flags & VM_SHARED)) { 8951e8f889bSDavid Gibson /* Optimization, do the COW without a second fault */ 8961e8f889bSDavid Gibson ret = hugetlb_cow(mm, vma, address, ptep, new_pte); 8971e8f889bSDavid Gibson } 8981e8f889bSDavid Gibson 899ac9b9c66SHugh Dickins spin_unlock(&mm->page_table_lock); 9004c887265SAdam Litke unlock_page(page); 9014c887265SAdam Litke out: 902ac9b9c66SHugh Dickins return ret; 9034c887265SAdam Litke 9044c887265SAdam Litke backout: 9054c887265SAdam Litke spin_unlock(&mm->page_table_lock); 9064c887265SAdam Litke unlock_page(page); 9074c887265SAdam Litke put_page(page); 9084c887265SAdam Litke goto out; 909ac9b9c66SHugh Dickins } 910ac9b9c66SHugh Dickins 91186e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 91286e5216fSAdam Litke unsigned long address, int write_access) 91386e5216fSAdam Litke { 91486e5216fSAdam Litke pte_t *ptep; 91586e5216fSAdam Litke pte_t entry; 9161e8f889bSDavid Gibson int ret; 9173935baa9SDavid Gibson static DEFINE_MUTEX(hugetlb_instantiation_mutex); 91886e5216fSAdam Litke 91986e5216fSAdam Litke ptep = huge_pte_alloc(mm, address); 92086e5216fSAdam Litke if (!ptep) 92186e5216fSAdam Litke return VM_FAULT_OOM; 92286e5216fSAdam Litke 9233935baa9SDavid Gibson /* 9243935baa9SDavid Gibson * Serialize hugepage allocation and instantiation, so that we don't 9253935baa9SDavid Gibson * get spurious allocation failures if two CPUs race to instantiate 9263935baa9SDavid Gibson * the same page in the page cache. 9273935baa9SDavid Gibson */ 9283935baa9SDavid Gibson mutex_lock(&hugetlb_instantiation_mutex); 92986e5216fSAdam Litke entry = *ptep; 9303935baa9SDavid Gibson if (pte_none(entry)) { 9313935baa9SDavid Gibson ret = hugetlb_no_page(mm, vma, address, ptep, write_access); 9323935baa9SDavid Gibson mutex_unlock(&hugetlb_instantiation_mutex); 9333935baa9SDavid Gibson return ret; 9343935baa9SDavid Gibson } 93586e5216fSAdam Litke 93683c54070SNick Piggin ret = 0; 9371e8f889bSDavid Gibson 9381e8f889bSDavid Gibson spin_lock(&mm->page_table_lock); 9391e8f889bSDavid Gibson /* Check for a racing update before calling hugetlb_cow */ 9401e8f889bSDavid Gibson if (likely(pte_same(entry, *ptep))) 9411e8f889bSDavid Gibson if (write_access && !pte_write(entry)) 9421e8f889bSDavid Gibson ret = hugetlb_cow(mm, vma, address, ptep, entry); 9431e8f889bSDavid Gibson spin_unlock(&mm->page_table_lock); 9443935baa9SDavid Gibson mutex_unlock(&hugetlb_instantiation_mutex); 9451e8f889bSDavid Gibson 9461e8f889bSDavid Gibson return ret; 94786e5216fSAdam Litke } 94886e5216fSAdam Litke 94963551ae0SDavid Gibson int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, 95063551ae0SDavid Gibson struct page **pages, struct vm_area_struct **vmas, 9515b23dbe8SAdam Litke unsigned long *position, int *length, int i, 9525b23dbe8SAdam Litke int write) 95363551ae0SDavid Gibson { 954d5d4b0aaSChen, Kenneth W unsigned long pfn_offset; 955d5d4b0aaSChen, Kenneth W unsigned long vaddr = *position; 95663551ae0SDavid Gibson int remainder = *length; 95763551ae0SDavid Gibson 9581c59827dSHugh Dickins spin_lock(&mm->page_table_lock); 95963551ae0SDavid Gibson while (vaddr < vma->vm_end && remainder) { 96063551ae0SDavid Gibson pte_t *pte; 96163551ae0SDavid Gibson struct page *page; 96263551ae0SDavid Gibson 9634c887265SAdam Litke /* 9644c887265SAdam Litke * Some archs (sparc64, sh*) have multiple pte_ts to 9654c887265SAdam Litke * each hugepage. We have to make * sure we get the 9664c887265SAdam Litke * first, for the page indexing below to work. 9674c887265SAdam Litke */ 96863551ae0SDavid Gibson pte = huge_pte_offset(mm, vaddr & HPAGE_MASK); 96963551ae0SDavid Gibson 97072fad713SAdam Litke if (!pte || pte_none(*pte) || (write && !pte_write(*pte))) { 9714c887265SAdam Litke int ret; 9724c887265SAdam Litke 9734c887265SAdam Litke spin_unlock(&mm->page_table_lock); 9745b23dbe8SAdam Litke ret = hugetlb_fault(mm, vma, vaddr, write); 9754c887265SAdam Litke spin_lock(&mm->page_table_lock); 976a89182c7SAdam Litke if (!(ret & VM_FAULT_ERROR)) 9774c887265SAdam Litke continue; 9784c887265SAdam Litke 9791c59827dSHugh Dickins remainder = 0; 9801c59827dSHugh Dickins if (!i) 9811c59827dSHugh Dickins i = -EFAULT; 9821c59827dSHugh Dickins break; 9831c59827dSHugh Dickins } 98463551ae0SDavid Gibson 985d5d4b0aaSChen, Kenneth W pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT; 986d5d4b0aaSChen, Kenneth W page = pte_page(*pte); 987d5d4b0aaSChen, Kenneth W same_page: 988d6692183SChen, Kenneth W if (pages) { 98963551ae0SDavid Gibson get_page(page); 990d5d4b0aaSChen, Kenneth W pages[i] = page + pfn_offset; 991d6692183SChen, Kenneth W } 99263551ae0SDavid Gibson 99363551ae0SDavid Gibson if (vmas) 99463551ae0SDavid Gibson vmas[i] = vma; 99563551ae0SDavid Gibson 99663551ae0SDavid Gibson vaddr += PAGE_SIZE; 997d5d4b0aaSChen, Kenneth W ++pfn_offset; 99863551ae0SDavid Gibson --remainder; 99963551ae0SDavid Gibson ++i; 1000d5d4b0aaSChen, Kenneth W if (vaddr < vma->vm_end && remainder && 1001d5d4b0aaSChen, Kenneth W pfn_offset < HPAGE_SIZE/PAGE_SIZE) { 1002d5d4b0aaSChen, Kenneth W /* 1003d5d4b0aaSChen, Kenneth W * We use pfn_offset to avoid touching the pageframes 1004d5d4b0aaSChen, Kenneth W * of this compound page. 1005d5d4b0aaSChen, Kenneth W */ 1006d5d4b0aaSChen, Kenneth W goto same_page; 1007d5d4b0aaSChen, Kenneth W } 100863551ae0SDavid Gibson } 10091c59827dSHugh Dickins spin_unlock(&mm->page_table_lock); 101063551ae0SDavid Gibson *length = remainder; 101163551ae0SDavid Gibson *position = vaddr; 101263551ae0SDavid Gibson 101363551ae0SDavid Gibson return i; 101463551ae0SDavid Gibson } 10158f860591SZhang, Yanmin 10168f860591SZhang, Yanmin void hugetlb_change_protection(struct vm_area_struct *vma, 10178f860591SZhang, Yanmin unsigned long address, unsigned long end, pgprot_t newprot) 10188f860591SZhang, Yanmin { 10198f860591SZhang, Yanmin struct mm_struct *mm = vma->vm_mm; 10208f860591SZhang, Yanmin unsigned long start = address; 10218f860591SZhang, Yanmin pte_t *ptep; 10228f860591SZhang, Yanmin pte_t pte; 10238f860591SZhang, Yanmin 10248f860591SZhang, Yanmin BUG_ON(address >= end); 10258f860591SZhang, Yanmin flush_cache_range(vma, address, end); 10268f860591SZhang, Yanmin 102739dde65cSChen, Kenneth W spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); 10288f860591SZhang, Yanmin spin_lock(&mm->page_table_lock); 10298f860591SZhang, Yanmin for (; address < end; address += HPAGE_SIZE) { 10308f860591SZhang, Yanmin ptep = huge_pte_offset(mm, address); 10318f860591SZhang, Yanmin if (!ptep) 10328f860591SZhang, Yanmin continue; 103339dde65cSChen, Kenneth W if (huge_pmd_unshare(mm, &address, ptep)) 103439dde65cSChen, Kenneth W continue; 10358f860591SZhang, Yanmin if (!pte_none(*ptep)) { 10368f860591SZhang, Yanmin pte = huge_ptep_get_and_clear(mm, address, ptep); 10378f860591SZhang, Yanmin pte = pte_mkhuge(pte_modify(pte, newprot)); 10388f860591SZhang, Yanmin set_huge_pte_at(mm, address, ptep, pte); 10398f860591SZhang, Yanmin } 10408f860591SZhang, Yanmin } 10418f860591SZhang, Yanmin spin_unlock(&mm->page_table_lock); 104239dde65cSChen, Kenneth W spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); 10438f860591SZhang, Yanmin 10448f860591SZhang, Yanmin flush_tlb_range(vma, start, end); 10458f860591SZhang, Yanmin } 10468f860591SZhang, Yanmin 1047a43a8c39SChen, Kenneth W struct file_region { 1048a43a8c39SChen, Kenneth W struct list_head link; 1049a43a8c39SChen, Kenneth W long from; 1050a43a8c39SChen, Kenneth W long to; 1051a43a8c39SChen, Kenneth W }; 1052a43a8c39SChen, Kenneth W 1053a43a8c39SChen, Kenneth W static long region_add(struct list_head *head, long f, long t) 1054a43a8c39SChen, Kenneth W { 1055a43a8c39SChen, Kenneth W struct file_region *rg, *nrg, *trg; 1056a43a8c39SChen, Kenneth W 1057a43a8c39SChen, Kenneth W /* Locate the region we are either in or before. */ 1058a43a8c39SChen, Kenneth W list_for_each_entry(rg, head, link) 1059a43a8c39SChen, Kenneth W if (f <= rg->to) 1060a43a8c39SChen, Kenneth W break; 1061a43a8c39SChen, Kenneth W 1062a43a8c39SChen, Kenneth W /* Round our left edge to the current segment if it encloses us. */ 1063a43a8c39SChen, Kenneth W if (f > rg->from) 1064a43a8c39SChen, Kenneth W f = rg->from; 1065a43a8c39SChen, Kenneth W 1066a43a8c39SChen, Kenneth W /* Check for and consume any regions we now overlap with. */ 1067a43a8c39SChen, Kenneth W nrg = rg; 1068a43a8c39SChen, Kenneth W list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 1069a43a8c39SChen, Kenneth W if (&rg->link == head) 1070a43a8c39SChen, Kenneth W break; 1071a43a8c39SChen, Kenneth W if (rg->from > t) 1072a43a8c39SChen, Kenneth W break; 1073a43a8c39SChen, Kenneth W 1074a43a8c39SChen, Kenneth W /* If this area reaches higher then extend our area to 1075a43a8c39SChen, Kenneth W * include it completely. If this is not the first area 1076a43a8c39SChen, Kenneth W * which we intend to reuse, free it. */ 1077a43a8c39SChen, Kenneth W if (rg->to > t) 1078a43a8c39SChen, Kenneth W t = rg->to; 1079a43a8c39SChen, Kenneth W if (rg != nrg) { 1080a43a8c39SChen, Kenneth W list_del(&rg->link); 1081a43a8c39SChen, Kenneth W kfree(rg); 1082a43a8c39SChen, Kenneth W } 1083a43a8c39SChen, Kenneth W } 1084a43a8c39SChen, Kenneth W nrg->from = f; 1085a43a8c39SChen, Kenneth W nrg->to = t; 1086a43a8c39SChen, Kenneth W return 0; 1087a43a8c39SChen, Kenneth W } 1088a43a8c39SChen, Kenneth W 1089a43a8c39SChen, Kenneth W static long region_chg(struct list_head *head, long f, long t) 1090a43a8c39SChen, Kenneth W { 1091a43a8c39SChen, Kenneth W struct file_region *rg, *nrg; 1092a43a8c39SChen, Kenneth W long chg = 0; 1093a43a8c39SChen, Kenneth W 1094a43a8c39SChen, Kenneth W /* Locate the region we are before or in. */ 1095a43a8c39SChen, Kenneth W list_for_each_entry(rg, head, link) 1096a43a8c39SChen, Kenneth W if (f <= rg->to) 1097a43a8c39SChen, Kenneth W break; 1098a43a8c39SChen, Kenneth W 1099a43a8c39SChen, Kenneth W /* If we are below the current region then a new region is required. 1100a43a8c39SChen, Kenneth W * Subtle, allocate a new region at the position but make it zero 1101183ff22bSSimon Arlott * size such that we can guarantee to record the reservation. */ 1102a43a8c39SChen, Kenneth W if (&rg->link == head || t < rg->from) { 1103a43a8c39SChen, Kenneth W nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); 1104c80544dcSStephen Hemminger if (!nrg) 1105a43a8c39SChen, Kenneth W return -ENOMEM; 1106a43a8c39SChen, Kenneth W nrg->from = f; 1107a43a8c39SChen, Kenneth W nrg->to = f; 1108a43a8c39SChen, Kenneth W INIT_LIST_HEAD(&nrg->link); 1109a43a8c39SChen, Kenneth W list_add(&nrg->link, rg->link.prev); 1110a43a8c39SChen, Kenneth W 1111a43a8c39SChen, Kenneth W return t - f; 1112a43a8c39SChen, Kenneth W } 1113a43a8c39SChen, Kenneth W 1114a43a8c39SChen, Kenneth W /* Round our left edge to the current segment if it encloses us. */ 1115a43a8c39SChen, Kenneth W if (f > rg->from) 1116a43a8c39SChen, Kenneth W f = rg->from; 1117a43a8c39SChen, Kenneth W chg = t - f; 1118a43a8c39SChen, Kenneth W 1119a43a8c39SChen, Kenneth W /* Check for and consume any regions we now overlap with. */ 1120a43a8c39SChen, Kenneth W list_for_each_entry(rg, rg->link.prev, link) { 1121a43a8c39SChen, Kenneth W if (&rg->link == head) 1122a43a8c39SChen, Kenneth W break; 1123a43a8c39SChen, Kenneth W if (rg->from > t) 1124a43a8c39SChen, Kenneth W return chg; 1125a43a8c39SChen, Kenneth W 1126a43a8c39SChen, Kenneth W /* We overlap with this area, if it extends futher than 1127a43a8c39SChen, Kenneth W * us then we must extend ourselves. Account for its 1128a43a8c39SChen, Kenneth W * existing reservation. */ 1129a43a8c39SChen, Kenneth W if (rg->to > t) { 1130a43a8c39SChen, Kenneth W chg += rg->to - t; 1131a43a8c39SChen, Kenneth W t = rg->to; 1132a43a8c39SChen, Kenneth W } 1133a43a8c39SChen, Kenneth W chg -= rg->to - rg->from; 1134a43a8c39SChen, Kenneth W } 1135a43a8c39SChen, Kenneth W return chg; 1136a43a8c39SChen, Kenneth W } 1137a43a8c39SChen, Kenneth W 1138a43a8c39SChen, Kenneth W static long region_truncate(struct list_head *head, long end) 1139a43a8c39SChen, Kenneth W { 1140a43a8c39SChen, Kenneth W struct file_region *rg, *trg; 1141a43a8c39SChen, Kenneth W long chg = 0; 1142a43a8c39SChen, Kenneth W 1143a43a8c39SChen, Kenneth W /* Locate the region we are either in or before. */ 1144a43a8c39SChen, Kenneth W list_for_each_entry(rg, head, link) 1145a43a8c39SChen, Kenneth W if (end <= rg->to) 1146a43a8c39SChen, Kenneth W break; 1147a43a8c39SChen, Kenneth W if (&rg->link == head) 1148a43a8c39SChen, Kenneth W return 0; 1149a43a8c39SChen, Kenneth W 1150a43a8c39SChen, Kenneth W /* If we are in the middle of a region then adjust it. */ 1151a43a8c39SChen, Kenneth W if (end > rg->from) { 1152a43a8c39SChen, Kenneth W chg = rg->to - end; 1153a43a8c39SChen, Kenneth W rg->to = end; 1154a43a8c39SChen, Kenneth W rg = list_entry(rg->link.next, typeof(*rg), link); 1155a43a8c39SChen, Kenneth W } 1156a43a8c39SChen, Kenneth W 1157a43a8c39SChen, Kenneth W /* Drop any remaining regions. */ 1158a43a8c39SChen, Kenneth W list_for_each_entry_safe(rg, trg, rg->link.prev, link) { 1159a43a8c39SChen, Kenneth W if (&rg->link == head) 1160a43a8c39SChen, Kenneth W break; 1161a43a8c39SChen, Kenneth W chg += rg->to - rg->from; 1162a43a8c39SChen, Kenneth W list_del(&rg->link); 1163a43a8c39SChen, Kenneth W kfree(rg); 1164a43a8c39SChen, Kenneth W } 1165a43a8c39SChen, Kenneth W return chg; 1166a43a8c39SChen, Kenneth W } 1167a43a8c39SChen, Kenneth W 1168a43a8c39SChen, Kenneth W static int hugetlb_acct_memory(long delta) 1169a43a8c39SChen, Kenneth W { 1170a43a8c39SChen, Kenneth W int ret = -ENOMEM; 1171a43a8c39SChen, Kenneth W 1172a43a8c39SChen, Kenneth W spin_lock(&hugetlb_lock); 11738a630112SKen Chen /* 11748a630112SKen Chen * When cpuset is configured, it breaks the strict hugetlb page 11758a630112SKen Chen * reservation as the accounting is done on a global variable. Such 11768a630112SKen Chen * reservation is completely rubbish in the presence of cpuset because 11778a630112SKen Chen * the reservation is not checked against page availability for the 11788a630112SKen Chen * current cpuset. Application can still potentially OOM'ed by kernel 11798a630112SKen Chen * with lack of free htlb page in cpuset that the task is in. 11808a630112SKen Chen * Attempt to enforce strict accounting with cpuset is almost 11818a630112SKen Chen * impossible (or too ugly) because cpuset is too fluid that 11828a630112SKen Chen * task or memory node can be dynamically moved between cpusets. 11838a630112SKen Chen * 11848a630112SKen Chen * The change of semantics for shared hugetlb mapping with cpuset is 11858a630112SKen Chen * undesirable. However, in order to preserve some of the semantics, 11868a630112SKen Chen * we fall back to check against current free page availability as 11878a630112SKen Chen * a best attempt and hopefully to minimize the impact of changing 11888a630112SKen Chen * semantics that cpuset has. 11898a630112SKen Chen */ 1190e4e574b7SAdam Litke if (delta > 0) { 1191e4e574b7SAdam Litke if (gather_surplus_pages(delta) < 0) 1192e4e574b7SAdam Litke goto out; 1193e4e574b7SAdam Litke 1194e4e574b7SAdam Litke if (delta > cpuset_mems_nr(free_huge_pages_node)) 1195e4e574b7SAdam Litke goto out; 1196e4e574b7SAdam Litke } 1197e4e574b7SAdam Litke 1198e4e574b7SAdam Litke ret = 0; 1199e4e574b7SAdam Litke resv_huge_pages += delta; 1200e4e574b7SAdam Litke if (delta < 0) 1201e4e574b7SAdam Litke return_unused_surplus_pages((unsigned long) -delta); 1202e4e574b7SAdam Litke 1203e4e574b7SAdam Litke out: 1204e4e574b7SAdam Litke spin_unlock(&hugetlb_lock); 1205e4e574b7SAdam Litke return ret; 1206e4e574b7SAdam Litke } 1207e4e574b7SAdam Litke 1208e4e574b7SAdam Litke int hugetlb_reserve_pages(struct inode *inode, long from, long to) 1209e4e574b7SAdam Litke { 1210e4e574b7SAdam Litke long ret, chg; 1211e4e574b7SAdam Litke 1212e4e574b7SAdam Litke chg = region_chg(&inode->i_mapping->private_list, from, to); 1213e4e574b7SAdam Litke if (chg < 0) 1214e4e574b7SAdam Litke return chg; 12158a630112SKen Chen 121690d8b7e6SAdam Litke if (hugetlb_get_quota(inode->i_mapping, chg)) 121790d8b7e6SAdam Litke return -ENOSPC; 1218a43a8c39SChen, Kenneth W ret = hugetlb_acct_memory(chg); 121968842c9bSKen Chen if (ret < 0) { 122068842c9bSKen Chen hugetlb_put_quota(inode->i_mapping, chg); 1221a43a8c39SChen, Kenneth W return ret; 122268842c9bSKen Chen } 1223a43a8c39SChen, Kenneth W region_add(&inode->i_mapping->private_list, from, to); 1224a43a8c39SChen, Kenneth W return 0; 1225a43a8c39SChen, Kenneth W } 1226a43a8c39SChen, Kenneth W 1227a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) 1228a43a8c39SChen, Kenneth W { 1229a43a8c39SChen, Kenneth W long chg = region_truncate(&inode->i_mapping->private_list, offset); 123045c682a6SKen Chen 123145c682a6SKen Chen spin_lock(&inode->i_lock); 123245c682a6SKen Chen inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed; 123345c682a6SKen Chen spin_unlock(&inode->i_lock); 123445c682a6SKen Chen 123590d8b7e6SAdam Litke hugetlb_put_quota(inode->i_mapping, (chg - freed)); 123690d8b7e6SAdam Litke hugetlb_acct_memory(-(chg - freed)); 1237a43a8c39SChen, Kenneth W } 1238