xref: /openbmc/linux/mm/hugetlb.c (revision d1c3fb1f)
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;
3454f9f80dSAdam Litke int hugetlb_dynamic_pool;
35d1c3fb1fSNishanth Aravamudan unsigned long nr_overcommit_huge_pages;
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 
745da7ca86SChristoph Lameter static struct page *dequeue_huge_page(struct vm_area_struct *vma,
755da7ca86SChristoph Lameter 				unsigned long address)
761da177e4SLinus Torvalds {
7731a5c6e4SNishanth Aravamudan 	int nid;
781da177e4SLinus Torvalds 	struct page *page = NULL;
79480eccf9SLee Schermerhorn 	struct mempolicy *mpol;
80396faf03SMel Gorman 	struct zonelist *zonelist = huge_zonelist(vma, address,
81480eccf9SLee Schermerhorn 					htlb_alloc_mask, &mpol);
8296df9333SChristoph Lameter 	struct zone **z;
831da177e4SLinus Torvalds 
8496df9333SChristoph Lameter 	for (z = zonelist->zones; *z; z++) {
8589fa3024SChristoph Lameter 		nid = zone_to_nid(*z);
86396faf03SMel Gorman 		if (cpuset_zone_allowed_softwall(*z, htlb_alloc_mask) &&
873abf7afdSAndrew Morton 		    !list_empty(&hugepage_freelists[nid])) {
881da177e4SLinus Torvalds 			page = list_entry(hugepage_freelists[nid].next,
891da177e4SLinus Torvalds 					  struct page, lru);
901da177e4SLinus Torvalds 			list_del(&page->lru);
911da177e4SLinus Torvalds 			free_huge_pages--;
921da177e4SLinus Torvalds 			free_huge_pages_node[nid]--;
93e4e574b7SAdam Litke 			if (vma && vma->vm_flags & VM_MAYSHARE)
94e4e574b7SAdam Litke 				resv_huge_pages--;
955ab3ee7bSKen Chen 			break;
961da177e4SLinus Torvalds 		}
973abf7afdSAndrew Morton 	}
98480eccf9SLee Schermerhorn 	mpol_free(mpol);	/* unref if mpol !NULL */
991da177e4SLinus Torvalds 	return page;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1026af2acb6SAdam Litke static void update_and_free_page(struct page *page)
1036af2acb6SAdam Litke {
1046af2acb6SAdam Litke 	int i;
1056af2acb6SAdam Litke 	nr_huge_pages--;
1066af2acb6SAdam Litke 	nr_huge_pages_node[page_to_nid(page)]--;
1076af2acb6SAdam Litke 	for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
1086af2acb6SAdam Litke 		page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
1096af2acb6SAdam Litke 				1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
1106af2acb6SAdam Litke 				1 << PG_private | 1<< PG_writeback);
1116af2acb6SAdam Litke 	}
1126af2acb6SAdam Litke 	set_compound_page_dtor(page, NULL);
1136af2acb6SAdam Litke 	set_page_refcounted(page);
1146af2acb6SAdam Litke 	__free_pages(page, HUGETLB_PAGE_ORDER);
1156af2acb6SAdam Litke }
1166af2acb6SAdam Litke 
11727a85ef1SDavid Gibson static void free_huge_page(struct page *page)
11827a85ef1SDavid Gibson {
1197893d1d5SAdam Litke 	int nid = page_to_nid(page);
120c79fb75eSAdam Litke 	struct address_space *mapping;
12127a85ef1SDavid Gibson 
122c79fb75eSAdam Litke 	mapping = (struct address_space *) page_private(page);
1237893d1d5SAdam Litke 	BUG_ON(page_count(page));
12427a85ef1SDavid Gibson 	INIT_LIST_HEAD(&page->lru);
12527a85ef1SDavid Gibson 
12627a85ef1SDavid Gibson 	spin_lock(&hugetlb_lock);
1277893d1d5SAdam Litke 	if (surplus_huge_pages_node[nid]) {
1287893d1d5SAdam Litke 		update_and_free_page(page);
1297893d1d5SAdam Litke 		surplus_huge_pages--;
1307893d1d5SAdam Litke 		surplus_huge_pages_node[nid]--;
1317893d1d5SAdam Litke 	} else {
13227a85ef1SDavid Gibson 		enqueue_huge_page(page);
1337893d1d5SAdam Litke 	}
13427a85ef1SDavid Gibson 	spin_unlock(&hugetlb_lock);
135c79fb75eSAdam Litke 	if (mapping)
1369a119c05SAdam Litke 		hugetlb_put_quota(mapping, 1);
137c79fb75eSAdam Litke 	set_page_private(page, 0);
13827a85ef1SDavid Gibson }
13927a85ef1SDavid Gibson 
1407893d1d5SAdam Litke /*
1417893d1d5SAdam Litke  * Increment or decrement surplus_huge_pages.  Keep node-specific counters
1427893d1d5SAdam Litke  * balanced by operating on them in a round-robin fashion.
1437893d1d5SAdam Litke  * Returns 1 if an adjustment was made.
1447893d1d5SAdam Litke  */
1457893d1d5SAdam Litke static int adjust_pool_surplus(int delta)
1467893d1d5SAdam Litke {
1477893d1d5SAdam Litke 	static int prev_nid;
1487893d1d5SAdam Litke 	int nid = prev_nid;
1497893d1d5SAdam Litke 	int ret = 0;
1507893d1d5SAdam Litke 
1517893d1d5SAdam Litke 	VM_BUG_ON(delta != -1 && delta != 1);
1527893d1d5SAdam Litke 	do {
1537893d1d5SAdam Litke 		nid = next_node(nid, node_online_map);
1547893d1d5SAdam Litke 		if (nid == MAX_NUMNODES)
1557893d1d5SAdam Litke 			nid = first_node(node_online_map);
1567893d1d5SAdam Litke 
1577893d1d5SAdam Litke 		/* To shrink on this node, there must be a surplus page */
1587893d1d5SAdam Litke 		if (delta < 0 && !surplus_huge_pages_node[nid])
1597893d1d5SAdam Litke 			continue;
1607893d1d5SAdam Litke 		/* Surplus cannot exceed the total number of pages */
1617893d1d5SAdam Litke 		if (delta > 0 && surplus_huge_pages_node[nid] >=
1627893d1d5SAdam Litke 						nr_huge_pages_node[nid])
1637893d1d5SAdam Litke 			continue;
1647893d1d5SAdam Litke 
1657893d1d5SAdam Litke 		surplus_huge_pages += delta;
1667893d1d5SAdam Litke 		surplus_huge_pages_node[nid] += delta;
1677893d1d5SAdam Litke 		ret = 1;
1687893d1d5SAdam Litke 		break;
1697893d1d5SAdam Litke 	} while (nid != prev_nid);
1707893d1d5SAdam Litke 
1717893d1d5SAdam Litke 	prev_nid = nid;
1727893d1d5SAdam Litke 	return ret;
1737893d1d5SAdam Litke }
1747893d1d5SAdam Litke 
17563b4613cSNishanth Aravamudan static struct page *alloc_fresh_huge_page_node(int nid)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	struct page *page;
178f96efd58SJoe Jin 
17963b4613cSNishanth Aravamudan 	page = alloc_pages_node(nid,
18063b4613cSNishanth Aravamudan 		htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|__GFP_NOWARN,
181f96efd58SJoe Jin 		HUGETLB_PAGE_ORDER);
1821da177e4SLinus Torvalds 	if (page) {
18333f2ef89SAndy Whitcroft 		set_compound_page_dtor(page, free_huge_page);
1840bd0f9fbSEric Paris 		spin_lock(&hugetlb_lock);
1851da177e4SLinus Torvalds 		nr_huge_pages++;
18663b4613cSNishanth Aravamudan 		nr_huge_pages_node[nid]++;
1870bd0f9fbSEric Paris 		spin_unlock(&hugetlb_lock);
188a482289dSNick Piggin 		put_page(page); /* free it into the hugepage allocator */
1891da177e4SLinus Torvalds 	}
19063b4613cSNishanth Aravamudan 
19163b4613cSNishanth Aravamudan 	return page;
19263b4613cSNishanth Aravamudan }
19363b4613cSNishanth Aravamudan 
19463b4613cSNishanth Aravamudan static int alloc_fresh_huge_page(void)
19563b4613cSNishanth Aravamudan {
19663b4613cSNishanth Aravamudan 	struct page *page;
19763b4613cSNishanth Aravamudan 	int start_nid;
19863b4613cSNishanth Aravamudan 	int next_nid;
19963b4613cSNishanth Aravamudan 	int ret = 0;
20063b4613cSNishanth Aravamudan 
20163b4613cSNishanth Aravamudan 	start_nid = hugetlb_next_nid;
20263b4613cSNishanth Aravamudan 
20363b4613cSNishanth Aravamudan 	do {
20463b4613cSNishanth Aravamudan 		page = alloc_fresh_huge_page_node(hugetlb_next_nid);
20563b4613cSNishanth Aravamudan 		if (page)
20663b4613cSNishanth Aravamudan 			ret = 1;
20763b4613cSNishanth Aravamudan 		/*
20863b4613cSNishanth Aravamudan 		 * Use a helper variable to find the next node and then
20963b4613cSNishanth Aravamudan 		 * copy it back to hugetlb_next_nid afterwards:
21063b4613cSNishanth Aravamudan 		 * otherwise there's a window in which a racer might
21163b4613cSNishanth Aravamudan 		 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
21263b4613cSNishanth Aravamudan 		 * But we don't need to use a spin_lock here: it really
21363b4613cSNishanth Aravamudan 		 * doesn't matter if occasionally a racer chooses the
21463b4613cSNishanth Aravamudan 		 * same nid as we do.  Move nid forward in the mask even
21563b4613cSNishanth Aravamudan 		 * if we just successfully allocated a hugepage so that
21663b4613cSNishanth Aravamudan 		 * the next caller gets hugepages on the next node.
21763b4613cSNishanth Aravamudan 		 */
21863b4613cSNishanth Aravamudan 		next_nid = next_node(hugetlb_next_nid, node_online_map);
21963b4613cSNishanth Aravamudan 		if (next_nid == MAX_NUMNODES)
22063b4613cSNishanth Aravamudan 			next_nid = first_node(node_online_map);
22163b4613cSNishanth Aravamudan 		hugetlb_next_nid = next_nid;
22263b4613cSNishanth Aravamudan 	} while (!page && hugetlb_next_nid != start_nid);
22363b4613cSNishanth Aravamudan 
22463b4613cSNishanth Aravamudan 	return ret;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2277893d1d5SAdam Litke static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
2287893d1d5SAdam Litke 						unsigned long address)
2297893d1d5SAdam Litke {
2307893d1d5SAdam Litke 	struct page *page;
231d1c3fb1fSNishanth Aravamudan 	unsigned int nid;
2327893d1d5SAdam Litke 
23354f9f80dSAdam Litke 	/* Check if the dynamic pool is enabled */
23454f9f80dSAdam Litke 	if (!hugetlb_dynamic_pool)
23554f9f80dSAdam Litke 		return NULL;
23654f9f80dSAdam Litke 
237d1c3fb1fSNishanth Aravamudan 	/*
238d1c3fb1fSNishanth Aravamudan 	 * Assume we will successfully allocate the surplus page to
239d1c3fb1fSNishanth Aravamudan 	 * prevent racing processes from causing the surplus to exceed
240d1c3fb1fSNishanth Aravamudan 	 * overcommit
241d1c3fb1fSNishanth Aravamudan 	 *
242d1c3fb1fSNishanth Aravamudan 	 * This however introduces a different race, where a process B
243d1c3fb1fSNishanth Aravamudan 	 * tries to grow the static hugepage pool while alloc_pages() is
244d1c3fb1fSNishanth Aravamudan 	 * called by process A. B will only examine the per-node
245d1c3fb1fSNishanth Aravamudan 	 * counters in determining if surplus huge pages can be
246d1c3fb1fSNishanth Aravamudan 	 * converted to normal huge pages in adjust_pool_surplus(). A
247d1c3fb1fSNishanth Aravamudan 	 * won't be able to increment the per-node counter, until the
248d1c3fb1fSNishanth Aravamudan 	 * lock is dropped by B, but B doesn't drop hugetlb_lock until
249d1c3fb1fSNishanth Aravamudan 	 * no more huge pages can be converted from surplus to normal
250d1c3fb1fSNishanth Aravamudan 	 * state (and doesn't try to convert again). Thus, we have a
251d1c3fb1fSNishanth Aravamudan 	 * case where a surplus huge page exists, the pool is grown, and
252d1c3fb1fSNishanth Aravamudan 	 * the surplus huge page still exists after, even though it
253d1c3fb1fSNishanth Aravamudan 	 * should just have been converted to a normal huge page. This
254d1c3fb1fSNishanth Aravamudan 	 * does not leak memory, though, as the hugepage will be freed
255d1c3fb1fSNishanth Aravamudan 	 * once it is out of use. It also does not allow the counters to
256d1c3fb1fSNishanth Aravamudan 	 * go out of whack in adjust_pool_surplus() as we don't modify
257d1c3fb1fSNishanth Aravamudan 	 * the node values until we've gotten the hugepage and only the
258d1c3fb1fSNishanth Aravamudan 	 * per-node value is checked there.
259d1c3fb1fSNishanth Aravamudan 	 */
260d1c3fb1fSNishanth Aravamudan 	spin_lock(&hugetlb_lock);
261d1c3fb1fSNishanth Aravamudan 	if (surplus_huge_pages >= nr_overcommit_huge_pages) {
262d1c3fb1fSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
263d1c3fb1fSNishanth Aravamudan 		return NULL;
264d1c3fb1fSNishanth Aravamudan 	} else {
265d1c3fb1fSNishanth Aravamudan 		nr_huge_pages++;
266d1c3fb1fSNishanth Aravamudan 		surplus_huge_pages++;
267d1c3fb1fSNishanth Aravamudan 	}
268d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
269d1c3fb1fSNishanth Aravamudan 
2707893d1d5SAdam Litke 	page = alloc_pages(htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN,
2717893d1d5SAdam Litke 					HUGETLB_PAGE_ORDER);
272d1c3fb1fSNishanth Aravamudan 
2737893d1d5SAdam Litke 	spin_lock(&hugetlb_lock);
274d1c3fb1fSNishanth Aravamudan 	if (page) {
275d1c3fb1fSNishanth Aravamudan 		nid = page_to_nid(page);
276d1c3fb1fSNishanth Aravamudan 		set_compound_page_dtor(page, free_huge_page);
277d1c3fb1fSNishanth Aravamudan 		/*
278d1c3fb1fSNishanth Aravamudan 		 * We incremented the global counters already
279d1c3fb1fSNishanth Aravamudan 		 */
280d1c3fb1fSNishanth Aravamudan 		nr_huge_pages_node[nid]++;
281d1c3fb1fSNishanth Aravamudan 		surplus_huge_pages_node[nid]++;
282d1c3fb1fSNishanth Aravamudan 	} else {
283d1c3fb1fSNishanth Aravamudan 		nr_huge_pages--;
284d1c3fb1fSNishanth Aravamudan 		surplus_huge_pages--;
2857893d1d5SAdam Litke 	}
286d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
2877893d1d5SAdam Litke 
2887893d1d5SAdam Litke 	return page;
2897893d1d5SAdam Litke }
2907893d1d5SAdam Litke 
291e4e574b7SAdam Litke /*
292e4e574b7SAdam Litke  * Increase the hugetlb pool such that it can accomodate a reservation
293e4e574b7SAdam Litke  * of size 'delta'.
294e4e574b7SAdam Litke  */
295e4e574b7SAdam Litke static int gather_surplus_pages(int delta)
296e4e574b7SAdam Litke {
297e4e574b7SAdam Litke 	struct list_head surplus_list;
298e4e574b7SAdam Litke 	struct page *page, *tmp;
299e4e574b7SAdam Litke 	int ret, i;
300e4e574b7SAdam Litke 	int needed, allocated;
301e4e574b7SAdam Litke 
302e4e574b7SAdam Litke 	needed = (resv_huge_pages + delta) - free_huge_pages;
303e4e574b7SAdam Litke 	if (needed <= 0)
304e4e574b7SAdam Litke 		return 0;
305e4e574b7SAdam Litke 
306e4e574b7SAdam Litke 	allocated = 0;
307e4e574b7SAdam Litke 	INIT_LIST_HEAD(&surplus_list);
308e4e574b7SAdam Litke 
309e4e574b7SAdam Litke 	ret = -ENOMEM;
310e4e574b7SAdam Litke retry:
311e4e574b7SAdam Litke 	spin_unlock(&hugetlb_lock);
312e4e574b7SAdam Litke 	for (i = 0; i < needed; i++) {
313e4e574b7SAdam Litke 		page = alloc_buddy_huge_page(NULL, 0);
314e4e574b7SAdam Litke 		if (!page) {
315e4e574b7SAdam Litke 			/*
316e4e574b7SAdam Litke 			 * We were not able to allocate enough pages to
317e4e574b7SAdam Litke 			 * satisfy the entire reservation so we free what
318e4e574b7SAdam Litke 			 * we've allocated so far.
319e4e574b7SAdam Litke 			 */
320e4e574b7SAdam Litke 			spin_lock(&hugetlb_lock);
321e4e574b7SAdam Litke 			needed = 0;
322e4e574b7SAdam Litke 			goto free;
323e4e574b7SAdam Litke 		}
324e4e574b7SAdam Litke 
325e4e574b7SAdam Litke 		list_add(&page->lru, &surplus_list);
326e4e574b7SAdam Litke 	}
327e4e574b7SAdam Litke 	allocated += needed;
328e4e574b7SAdam Litke 
329e4e574b7SAdam Litke 	/*
330e4e574b7SAdam Litke 	 * After retaking hugetlb_lock, we need to recalculate 'needed'
331e4e574b7SAdam Litke 	 * because either resv_huge_pages or free_huge_pages may have changed.
332e4e574b7SAdam Litke 	 */
333e4e574b7SAdam Litke 	spin_lock(&hugetlb_lock);
334e4e574b7SAdam Litke 	needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
335e4e574b7SAdam Litke 	if (needed > 0)
336e4e574b7SAdam Litke 		goto retry;
337e4e574b7SAdam Litke 
338e4e574b7SAdam Litke 	/*
339e4e574b7SAdam Litke 	 * The surplus_list now contains _at_least_ the number of extra pages
340e4e574b7SAdam Litke 	 * needed to accomodate the reservation.  Add the appropriate number
341e4e574b7SAdam Litke 	 * of pages to the hugetlb pool and free the extras back to the buddy
342e4e574b7SAdam Litke 	 * allocator.
343e4e574b7SAdam Litke 	 */
344e4e574b7SAdam Litke 	needed += allocated;
345e4e574b7SAdam Litke 	ret = 0;
346e4e574b7SAdam Litke free:
347e4e574b7SAdam Litke 	list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
348e4e574b7SAdam Litke 		list_del(&page->lru);
349e4e574b7SAdam Litke 		if ((--needed) >= 0)
350e4e574b7SAdam Litke 			enqueue_huge_page(page);
351af767cbdSAdam Litke 		else {
352af767cbdSAdam Litke 			/*
353af767cbdSAdam Litke 			 * Decrement the refcount and free the page using its
354af767cbdSAdam Litke 			 * destructor.  This must be done with hugetlb_lock
355af767cbdSAdam Litke 			 * unlocked which is safe because free_huge_page takes
356af767cbdSAdam Litke 			 * hugetlb_lock before deciding how to free the page.
357af767cbdSAdam Litke 			 */
358af767cbdSAdam Litke 			spin_unlock(&hugetlb_lock);
359af767cbdSAdam Litke 			put_page(page);
360af767cbdSAdam Litke 			spin_lock(&hugetlb_lock);
361af767cbdSAdam Litke 		}
362e4e574b7SAdam Litke 	}
363e4e574b7SAdam Litke 
364e4e574b7SAdam Litke 	return ret;
365e4e574b7SAdam Litke }
366e4e574b7SAdam Litke 
367e4e574b7SAdam Litke /*
368e4e574b7SAdam Litke  * When releasing a hugetlb pool reservation, any surplus pages that were
369e4e574b7SAdam Litke  * allocated to satisfy the reservation must be explicitly freed if they were
370e4e574b7SAdam Litke  * never used.
371e4e574b7SAdam Litke  */
3728cde045cSAdrian Bunk static void return_unused_surplus_pages(unsigned long unused_resv_pages)
373e4e574b7SAdam Litke {
374e4e574b7SAdam Litke 	static int nid = -1;
375e4e574b7SAdam Litke 	struct page *page;
376e4e574b7SAdam Litke 	unsigned long nr_pages;
377e4e574b7SAdam Litke 
378e4e574b7SAdam Litke 	nr_pages = min(unused_resv_pages, surplus_huge_pages);
379e4e574b7SAdam Litke 
380e4e574b7SAdam Litke 	while (nr_pages) {
381e4e574b7SAdam Litke 		nid = next_node(nid, node_online_map);
382e4e574b7SAdam Litke 		if (nid == MAX_NUMNODES)
383e4e574b7SAdam Litke 			nid = first_node(node_online_map);
384e4e574b7SAdam Litke 
385e4e574b7SAdam Litke 		if (!surplus_huge_pages_node[nid])
386e4e574b7SAdam Litke 			continue;
387e4e574b7SAdam Litke 
388e4e574b7SAdam Litke 		if (!list_empty(&hugepage_freelists[nid])) {
389e4e574b7SAdam Litke 			page = list_entry(hugepage_freelists[nid].next,
390e4e574b7SAdam Litke 					  struct page, lru);
391e4e574b7SAdam Litke 			list_del(&page->lru);
392e4e574b7SAdam Litke 			update_and_free_page(page);
393e4e574b7SAdam Litke 			free_huge_pages--;
394e4e574b7SAdam Litke 			free_huge_pages_node[nid]--;
395e4e574b7SAdam Litke 			surplus_huge_pages--;
396e4e574b7SAdam Litke 			surplus_huge_pages_node[nid]--;
397e4e574b7SAdam Litke 			nr_pages--;
398e4e574b7SAdam Litke 		}
399e4e574b7SAdam Litke 	}
400e4e574b7SAdam Litke }
401e4e574b7SAdam Litke 
402348ea204SAdam Litke 
403348ea204SAdam Litke static struct page *alloc_huge_page_shared(struct vm_area_struct *vma,
404348ea204SAdam Litke 						unsigned long addr)
405348ea204SAdam Litke {
406348ea204SAdam Litke 	struct page *page;
407348ea204SAdam Litke 
408348ea204SAdam Litke 	spin_lock(&hugetlb_lock);
409348ea204SAdam Litke 	page = dequeue_huge_page(vma, addr);
410348ea204SAdam Litke 	spin_unlock(&hugetlb_lock);
41190d8b7e6SAdam Litke 	return page ? page : ERR_PTR(-VM_FAULT_OOM);
412348ea204SAdam Litke }
413348ea204SAdam Litke 
414348ea204SAdam Litke static struct page *alloc_huge_page_private(struct vm_area_struct *vma,
41527a85ef1SDavid Gibson 						unsigned long addr)
4161da177e4SLinus Torvalds {
4177893d1d5SAdam Litke 	struct page *page = NULL;
4181da177e4SLinus Torvalds 
41990d8b7e6SAdam Litke 	if (hugetlb_get_quota(vma->vm_file->f_mapping, 1))
42090d8b7e6SAdam Litke 		return ERR_PTR(-VM_FAULT_SIGBUS);
42190d8b7e6SAdam Litke 
4221da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
423348ea204SAdam Litke 	if (free_huge_pages > resv_huge_pages)
424b45b5bd6SDavid Gibson 		page = dequeue_huge_page(vma, addr);
425348ea204SAdam Litke 	spin_unlock(&hugetlb_lock);
426b45b5bd6SDavid Gibson 	if (!page)
4277893d1d5SAdam Litke 		page = alloc_buddy_huge_page(vma, addr);
42890d8b7e6SAdam Litke 	return page ? page : ERR_PTR(-VM_FAULT_OOM);
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;
702c74df32cSHugh Dickins 		spin_lock(&dst->page_table_lock);
7031c59827dSHugh Dickins 		spin_lock(&src->page_table_lock);
704c74df32cSHugh Dickins 		if (!pte_none(*src_pte)) {
7051e8f889bSDavid Gibson 			if (cow)
7061e8f889bSDavid Gibson 				ptep_set_wrprotect(src, addr, src_pte);
70763551ae0SDavid Gibson 			entry = *src_pte;
70863551ae0SDavid Gibson 			ptepage = pte_page(entry);
70963551ae0SDavid Gibson 			get_page(ptepage);
71063551ae0SDavid Gibson 			set_huge_pte_at(dst, addr, dst_pte, entry);
7111c59827dSHugh Dickins 		}
7121c59827dSHugh Dickins 		spin_unlock(&src->page_table_lock);
713c74df32cSHugh Dickins 		spin_unlock(&dst->page_table_lock);
71463551ae0SDavid Gibson 	}
71563551ae0SDavid Gibson 	return 0;
71663551ae0SDavid Gibson 
71763551ae0SDavid Gibson nomem:
71863551ae0SDavid Gibson 	return -ENOMEM;
71963551ae0SDavid Gibson }
72063551ae0SDavid Gibson 
721502717f4SChen, Kenneth W void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
72263551ae0SDavid Gibson 			    unsigned long end)
72363551ae0SDavid Gibson {
72463551ae0SDavid Gibson 	struct mm_struct *mm = vma->vm_mm;
72563551ae0SDavid Gibson 	unsigned long address;
726c7546f8fSDavid Gibson 	pte_t *ptep;
72763551ae0SDavid Gibson 	pte_t pte;
72863551ae0SDavid Gibson 	struct page *page;
729fe1668aeSChen, Kenneth W 	struct page *tmp;
730c0a499c2SChen, Kenneth W 	/*
731c0a499c2SChen, Kenneth W 	 * A page gathering list, protected by per file i_mmap_lock. The
732c0a499c2SChen, Kenneth W 	 * lock is used to avoid list corruption from multiple unmapping
733c0a499c2SChen, Kenneth W 	 * of the same page since we are using page->lru.
734c0a499c2SChen, Kenneth W 	 */
735fe1668aeSChen, Kenneth W 	LIST_HEAD(page_list);
73663551ae0SDavid Gibson 
73763551ae0SDavid Gibson 	WARN_ON(!is_vm_hugetlb_page(vma));
73863551ae0SDavid Gibson 	BUG_ON(start & ~HPAGE_MASK);
73963551ae0SDavid Gibson 	BUG_ON(end & ~HPAGE_MASK);
74063551ae0SDavid Gibson 
741508034a3SHugh Dickins 	spin_lock(&mm->page_table_lock);
74263551ae0SDavid Gibson 	for (address = start; address < end; address += HPAGE_SIZE) {
743c7546f8fSDavid Gibson 		ptep = huge_pte_offset(mm, address);
744c7546f8fSDavid Gibson 		if (!ptep)
745c7546f8fSDavid Gibson 			continue;
746c7546f8fSDavid Gibson 
74739dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
74839dde65cSChen, Kenneth W 			continue;
74939dde65cSChen, Kenneth W 
750c7546f8fSDavid Gibson 		pte = huge_ptep_get_and_clear(mm, address, ptep);
75163551ae0SDavid Gibson 		if (pte_none(pte))
75263551ae0SDavid Gibson 			continue;
753c7546f8fSDavid Gibson 
75463551ae0SDavid Gibson 		page = pte_page(pte);
7556649a386SKen Chen 		if (pte_dirty(pte))
7566649a386SKen Chen 			set_page_dirty(page);
757fe1668aeSChen, Kenneth W 		list_add(&page->lru, &page_list);
75863551ae0SDavid Gibson 	}
7591da177e4SLinus Torvalds 	spin_unlock(&mm->page_table_lock);
760508034a3SHugh Dickins 	flush_tlb_range(vma, start, end);
761fe1668aeSChen, Kenneth W 	list_for_each_entry_safe(page, tmp, &page_list, lru) {
762fe1668aeSChen, Kenneth W 		list_del(&page->lru);
763fe1668aeSChen, Kenneth W 		put_page(page);
764fe1668aeSChen, Kenneth W 	}
7651da177e4SLinus Torvalds }
76663551ae0SDavid Gibson 
767502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
768502717f4SChen, Kenneth W 			  unsigned long end)
769502717f4SChen, Kenneth W {
770502717f4SChen, Kenneth W 	/*
771502717f4SChen, Kenneth W 	 * It is undesirable to test vma->vm_file as it should be non-null
772502717f4SChen, Kenneth W 	 * for valid hugetlb area. However, vm_file will be NULL in the error
773502717f4SChen, Kenneth W 	 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
774502717f4SChen, Kenneth W 	 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
775502717f4SChen, Kenneth W 	 * to clean up. Since no pte has actually been setup, it is safe to
776502717f4SChen, Kenneth W 	 * do nothing in this case.
777502717f4SChen, Kenneth W 	 */
778502717f4SChen, Kenneth W 	if (vma->vm_file) {
779502717f4SChen, Kenneth W 		spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
780502717f4SChen, Kenneth W 		__unmap_hugepage_range(vma, start, end);
781502717f4SChen, Kenneth W 		spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
782502717f4SChen, Kenneth W 	}
783502717f4SChen, Kenneth W }
784502717f4SChen, Kenneth W 
7851e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
7861e8f889bSDavid Gibson 			unsigned long address, pte_t *ptep, pte_t pte)
7871e8f889bSDavid Gibson {
7881e8f889bSDavid Gibson 	struct page *old_page, *new_page;
78979ac6ba4SDavid Gibson 	int avoidcopy;
7901e8f889bSDavid Gibson 
7911e8f889bSDavid Gibson 	old_page = pte_page(pte);
7921e8f889bSDavid Gibson 
7931e8f889bSDavid Gibson 	/* If no-one else is actually using this page, avoid the copy
7941e8f889bSDavid Gibson 	 * and just make the page writable */
7951e8f889bSDavid Gibson 	avoidcopy = (page_count(old_page) == 1);
7961e8f889bSDavid Gibson 	if (avoidcopy) {
7971e8f889bSDavid Gibson 		set_huge_ptep_writable(vma, address, ptep);
79883c54070SNick Piggin 		return 0;
7991e8f889bSDavid Gibson 	}
8001e8f889bSDavid Gibson 
8011e8f889bSDavid Gibson 	page_cache_get(old_page);
8025da7ca86SChristoph Lameter 	new_page = alloc_huge_page(vma, address);
8031e8f889bSDavid Gibson 
8042fc39cecSAdam Litke 	if (IS_ERR(new_page)) {
8051e8f889bSDavid Gibson 		page_cache_release(old_page);
8062fc39cecSAdam Litke 		return -PTR_ERR(new_page);
8071e8f889bSDavid Gibson 	}
8081e8f889bSDavid Gibson 
8091e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
8109de455b2SAtsushi Nemoto 	copy_huge_page(new_page, old_page, address, vma);
8111e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
8121e8f889bSDavid Gibson 
8131e8f889bSDavid Gibson 	ptep = huge_pte_offset(mm, address & HPAGE_MASK);
8141e8f889bSDavid Gibson 	if (likely(pte_same(*ptep, pte))) {
8151e8f889bSDavid Gibson 		/* Break COW */
8161e8f889bSDavid Gibson 		set_huge_pte_at(mm, address, ptep,
8171e8f889bSDavid Gibson 				make_huge_pte(vma, new_page, 1));
8181e8f889bSDavid Gibson 		/* Make the old page be freed below */
8191e8f889bSDavid Gibson 		new_page = old_page;
8201e8f889bSDavid Gibson 	}
8211e8f889bSDavid Gibson 	page_cache_release(new_page);
8221e8f889bSDavid Gibson 	page_cache_release(old_page);
82383c54070SNick Piggin 	return 0;
8241e8f889bSDavid Gibson }
8251e8f889bSDavid Gibson 
826a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
8271e8f889bSDavid Gibson 			unsigned long address, pte_t *ptep, int write_access)
828ac9b9c66SHugh Dickins {
829ac9b9c66SHugh Dickins 	int ret = VM_FAULT_SIGBUS;
8304c887265SAdam Litke 	unsigned long idx;
8314c887265SAdam Litke 	unsigned long size;
8324c887265SAdam Litke 	struct page *page;
8334c887265SAdam Litke 	struct address_space *mapping;
8341e8f889bSDavid Gibson 	pte_t new_pte;
8354c887265SAdam Litke 
8364c887265SAdam Litke 	mapping = vma->vm_file->f_mapping;
8374c887265SAdam Litke 	idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
8384c887265SAdam Litke 		+ (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
8394c887265SAdam Litke 
8404c887265SAdam Litke 	/*
8414c887265SAdam Litke 	 * Use page lock to guard against racing truncation
8424c887265SAdam Litke 	 * before we get page_table_lock.
8434c887265SAdam Litke 	 */
8446bda666aSChristoph Lameter retry:
8456bda666aSChristoph Lameter 	page = find_lock_page(mapping, idx);
8466bda666aSChristoph Lameter 	if (!page) {
847ebed4bfcSHugh Dickins 		size = i_size_read(mapping->host) >> HPAGE_SHIFT;
848ebed4bfcSHugh Dickins 		if (idx >= size)
849ebed4bfcSHugh Dickins 			goto out;
8506bda666aSChristoph Lameter 		page = alloc_huge_page(vma, address);
8512fc39cecSAdam Litke 		if (IS_ERR(page)) {
8522fc39cecSAdam Litke 			ret = -PTR_ERR(page);
8536bda666aSChristoph Lameter 			goto out;
8546bda666aSChristoph Lameter 		}
85579ac6ba4SDavid Gibson 		clear_huge_page(page, address);
856ac9b9c66SHugh Dickins 
8576bda666aSChristoph Lameter 		if (vma->vm_flags & VM_SHARED) {
8586bda666aSChristoph Lameter 			int err;
85945c682a6SKen Chen 			struct inode *inode = mapping->host;
8606bda666aSChristoph Lameter 
8616bda666aSChristoph Lameter 			err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
8626bda666aSChristoph Lameter 			if (err) {
8636bda666aSChristoph Lameter 				put_page(page);
8646bda666aSChristoph Lameter 				if (err == -EEXIST)
8656bda666aSChristoph Lameter 					goto retry;
8666bda666aSChristoph Lameter 				goto out;
8676bda666aSChristoph Lameter 			}
86845c682a6SKen Chen 
86945c682a6SKen Chen 			spin_lock(&inode->i_lock);
87045c682a6SKen Chen 			inode->i_blocks += BLOCKS_PER_HUGEPAGE;
87145c682a6SKen Chen 			spin_unlock(&inode->i_lock);
8726bda666aSChristoph Lameter 		} else
8736bda666aSChristoph Lameter 			lock_page(page);
8746bda666aSChristoph Lameter 	}
8751e8f889bSDavid Gibson 
876ac9b9c66SHugh Dickins 	spin_lock(&mm->page_table_lock);
8774c887265SAdam Litke 	size = i_size_read(mapping->host) >> HPAGE_SHIFT;
8784c887265SAdam Litke 	if (idx >= size)
8794c887265SAdam Litke 		goto backout;
8804c887265SAdam Litke 
88183c54070SNick Piggin 	ret = 0;
88286e5216fSAdam Litke 	if (!pte_none(*ptep))
8834c887265SAdam Litke 		goto backout;
8844c887265SAdam Litke 
8851e8f889bSDavid Gibson 	new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
8861e8f889bSDavid Gibson 				&& (vma->vm_flags & VM_SHARED)));
8871e8f889bSDavid Gibson 	set_huge_pte_at(mm, address, ptep, new_pte);
8881e8f889bSDavid Gibson 
8891e8f889bSDavid Gibson 	if (write_access && !(vma->vm_flags & VM_SHARED)) {
8901e8f889bSDavid Gibson 		/* Optimization, do the COW without a second fault */
8911e8f889bSDavid Gibson 		ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
8921e8f889bSDavid Gibson 	}
8931e8f889bSDavid Gibson 
894ac9b9c66SHugh Dickins 	spin_unlock(&mm->page_table_lock);
8954c887265SAdam Litke 	unlock_page(page);
8964c887265SAdam Litke out:
897ac9b9c66SHugh Dickins 	return ret;
8984c887265SAdam Litke 
8994c887265SAdam Litke backout:
9004c887265SAdam Litke 	spin_unlock(&mm->page_table_lock);
9014c887265SAdam Litke 	unlock_page(page);
9024c887265SAdam Litke 	put_page(page);
9034c887265SAdam Litke 	goto out;
904ac9b9c66SHugh Dickins }
905ac9b9c66SHugh Dickins 
90686e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
90786e5216fSAdam Litke 			unsigned long address, int write_access)
90886e5216fSAdam Litke {
90986e5216fSAdam Litke 	pte_t *ptep;
91086e5216fSAdam Litke 	pte_t entry;
9111e8f889bSDavid Gibson 	int ret;
9123935baa9SDavid Gibson 	static DEFINE_MUTEX(hugetlb_instantiation_mutex);
91386e5216fSAdam Litke 
91486e5216fSAdam Litke 	ptep = huge_pte_alloc(mm, address);
91586e5216fSAdam Litke 	if (!ptep)
91686e5216fSAdam Litke 		return VM_FAULT_OOM;
91786e5216fSAdam Litke 
9183935baa9SDavid Gibson 	/*
9193935baa9SDavid Gibson 	 * Serialize hugepage allocation and instantiation, so that we don't
9203935baa9SDavid Gibson 	 * get spurious allocation failures if two CPUs race to instantiate
9213935baa9SDavid Gibson 	 * the same page in the page cache.
9223935baa9SDavid Gibson 	 */
9233935baa9SDavid Gibson 	mutex_lock(&hugetlb_instantiation_mutex);
92486e5216fSAdam Litke 	entry = *ptep;
9253935baa9SDavid Gibson 	if (pte_none(entry)) {
9263935baa9SDavid Gibson 		ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
9273935baa9SDavid Gibson 		mutex_unlock(&hugetlb_instantiation_mutex);
9283935baa9SDavid Gibson 		return ret;
9293935baa9SDavid Gibson 	}
93086e5216fSAdam Litke 
93183c54070SNick Piggin 	ret = 0;
9321e8f889bSDavid Gibson 
9331e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
9341e8f889bSDavid Gibson 	/* Check for a racing update before calling hugetlb_cow */
9351e8f889bSDavid Gibson 	if (likely(pte_same(entry, *ptep)))
9361e8f889bSDavid Gibson 		if (write_access && !pte_write(entry))
9371e8f889bSDavid Gibson 			ret = hugetlb_cow(mm, vma, address, ptep, entry);
9381e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
9393935baa9SDavid Gibson 	mutex_unlock(&hugetlb_instantiation_mutex);
9401e8f889bSDavid Gibson 
9411e8f889bSDavid Gibson 	return ret;
94286e5216fSAdam Litke }
94386e5216fSAdam Litke 
94463551ae0SDavid Gibson int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
94563551ae0SDavid Gibson 			struct page **pages, struct vm_area_struct **vmas,
9465b23dbe8SAdam Litke 			unsigned long *position, int *length, int i,
9475b23dbe8SAdam Litke 			int write)
94863551ae0SDavid Gibson {
949d5d4b0aaSChen, Kenneth W 	unsigned long pfn_offset;
950d5d4b0aaSChen, Kenneth W 	unsigned long vaddr = *position;
95163551ae0SDavid Gibson 	int remainder = *length;
95263551ae0SDavid Gibson 
9531c59827dSHugh Dickins 	spin_lock(&mm->page_table_lock);
95463551ae0SDavid Gibson 	while (vaddr < vma->vm_end && remainder) {
95563551ae0SDavid Gibson 		pte_t *pte;
95663551ae0SDavid Gibson 		struct page *page;
95763551ae0SDavid Gibson 
9584c887265SAdam Litke 		/*
9594c887265SAdam Litke 		 * Some archs (sparc64, sh*) have multiple pte_ts to
9604c887265SAdam Litke 		 * each hugepage.  We have to make * sure we get the
9614c887265SAdam Litke 		 * first, for the page indexing below to work.
9624c887265SAdam Litke 		 */
96363551ae0SDavid Gibson 		pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
96463551ae0SDavid Gibson 
96572fad713SAdam Litke 		if (!pte || pte_none(*pte) || (write && !pte_write(*pte))) {
9664c887265SAdam Litke 			int ret;
9674c887265SAdam Litke 
9684c887265SAdam Litke 			spin_unlock(&mm->page_table_lock);
9695b23dbe8SAdam Litke 			ret = hugetlb_fault(mm, vma, vaddr, write);
9704c887265SAdam Litke 			spin_lock(&mm->page_table_lock);
971a89182c7SAdam Litke 			if (!(ret & VM_FAULT_ERROR))
9724c887265SAdam Litke 				continue;
9734c887265SAdam Litke 
9741c59827dSHugh Dickins 			remainder = 0;
9751c59827dSHugh Dickins 			if (!i)
9761c59827dSHugh Dickins 				i = -EFAULT;
9771c59827dSHugh Dickins 			break;
9781c59827dSHugh Dickins 		}
97963551ae0SDavid Gibson 
980d5d4b0aaSChen, Kenneth W 		pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
981d5d4b0aaSChen, Kenneth W 		page = pte_page(*pte);
982d5d4b0aaSChen, Kenneth W same_page:
983d6692183SChen, Kenneth W 		if (pages) {
98463551ae0SDavid Gibson 			get_page(page);
985d5d4b0aaSChen, Kenneth W 			pages[i] = page + pfn_offset;
986d6692183SChen, Kenneth W 		}
98763551ae0SDavid Gibson 
98863551ae0SDavid Gibson 		if (vmas)
98963551ae0SDavid Gibson 			vmas[i] = vma;
99063551ae0SDavid Gibson 
99163551ae0SDavid Gibson 		vaddr += PAGE_SIZE;
992d5d4b0aaSChen, Kenneth W 		++pfn_offset;
99363551ae0SDavid Gibson 		--remainder;
99463551ae0SDavid Gibson 		++i;
995d5d4b0aaSChen, Kenneth W 		if (vaddr < vma->vm_end && remainder &&
996d5d4b0aaSChen, Kenneth W 				pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
997d5d4b0aaSChen, Kenneth W 			/*
998d5d4b0aaSChen, Kenneth W 			 * We use pfn_offset to avoid touching the pageframes
999d5d4b0aaSChen, Kenneth W 			 * of this compound page.
1000d5d4b0aaSChen, Kenneth W 			 */
1001d5d4b0aaSChen, Kenneth W 			goto same_page;
1002d5d4b0aaSChen, Kenneth W 		}
100363551ae0SDavid Gibson 	}
10041c59827dSHugh Dickins 	spin_unlock(&mm->page_table_lock);
100563551ae0SDavid Gibson 	*length = remainder;
100663551ae0SDavid Gibson 	*position = vaddr;
100763551ae0SDavid Gibson 
100863551ae0SDavid Gibson 	return i;
100963551ae0SDavid Gibson }
10108f860591SZhang, Yanmin 
10118f860591SZhang, Yanmin void hugetlb_change_protection(struct vm_area_struct *vma,
10128f860591SZhang, Yanmin 		unsigned long address, unsigned long end, pgprot_t newprot)
10138f860591SZhang, Yanmin {
10148f860591SZhang, Yanmin 	struct mm_struct *mm = vma->vm_mm;
10158f860591SZhang, Yanmin 	unsigned long start = address;
10168f860591SZhang, Yanmin 	pte_t *ptep;
10178f860591SZhang, Yanmin 	pte_t pte;
10188f860591SZhang, Yanmin 
10198f860591SZhang, Yanmin 	BUG_ON(address >= end);
10208f860591SZhang, Yanmin 	flush_cache_range(vma, address, end);
10218f860591SZhang, Yanmin 
102239dde65cSChen, Kenneth W 	spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
10238f860591SZhang, Yanmin 	spin_lock(&mm->page_table_lock);
10248f860591SZhang, Yanmin 	for (; address < end; address += HPAGE_SIZE) {
10258f860591SZhang, Yanmin 		ptep = huge_pte_offset(mm, address);
10268f860591SZhang, Yanmin 		if (!ptep)
10278f860591SZhang, Yanmin 			continue;
102839dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
102939dde65cSChen, Kenneth W 			continue;
10308f860591SZhang, Yanmin 		if (!pte_none(*ptep)) {
10318f860591SZhang, Yanmin 			pte = huge_ptep_get_and_clear(mm, address, ptep);
10328f860591SZhang, Yanmin 			pte = pte_mkhuge(pte_modify(pte, newprot));
10338f860591SZhang, Yanmin 			set_huge_pte_at(mm, address, ptep, pte);
10348f860591SZhang, Yanmin 		}
10358f860591SZhang, Yanmin 	}
10368f860591SZhang, Yanmin 	spin_unlock(&mm->page_table_lock);
103739dde65cSChen, Kenneth W 	spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
10388f860591SZhang, Yanmin 
10398f860591SZhang, Yanmin 	flush_tlb_range(vma, start, end);
10408f860591SZhang, Yanmin }
10418f860591SZhang, Yanmin 
1042a43a8c39SChen, Kenneth W struct file_region {
1043a43a8c39SChen, Kenneth W 	struct list_head link;
1044a43a8c39SChen, Kenneth W 	long from;
1045a43a8c39SChen, Kenneth W 	long to;
1046a43a8c39SChen, Kenneth W };
1047a43a8c39SChen, Kenneth W 
1048a43a8c39SChen, Kenneth W static long region_add(struct list_head *head, long f, long t)
1049a43a8c39SChen, Kenneth W {
1050a43a8c39SChen, Kenneth W 	struct file_region *rg, *nrg, *trg;
1051a43a8c39SChen, Kenneth W 
1052a43a8c39SChen, Kenneth W 	/* Locate the region we are either in or before. */
1053a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, head, link)
1054a43a8c39SChen, Kenneth W 		if (f <= rg->to)
1055a43a8c39SChen, Kenneth W 			break;
1056a43a8c39SChen, Kenneth W 
1057a43a8c39SChen, Kenneth W 	/* Round our left edge to the current segment if it encloses us. */
1058a43a8c39SChen, Kenneth W 	if (f > rg->from)
1059a43a8c39SChen, Kenneth W 		f = rg->from;
1060a43a8c39SChen, Kenneth W 
1061a43a8c39SChen, Kenneth W 	/* Check for and consume any regions we now overlap with. */
1062a43a8c39SChen, Kenneth W 	nrg = rg;
1063a43a8c39SChen, Kenneth W 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
1064a43a8c39SChen, Kenneth W 		if (&rg->link == head)
1065a43a8c39SChen, Kenneth W 			break;
1066a43a8c39SChen, Kenneth W 		if (rg->from > t)
1067a43a8c39SChen, Kenneth W 			break;
1068a43a8c39SChen, Kenneth W 
1069a43a8c39SChen, Kenneth W 		/* If this area reaches higher then extend our area to
1070a43a8c39SChen, Kenneth W 		 * include it completely.  If this is not the first area
1071a43a8c39SChen, Kenneth W 		 * which we intend to reuse, free it. */
1072a43a8c39SChen, Kenneth W 		if (rg->to > t)
1073a43a8c39SChen, Kenneth W 			t = rg->to;
1074a43a8c39SChen, Kenneth W 		if (rg != nrg) {
1075a43a8c39SChen, Kenneth W 			list_del(&rg->link);
1076a43a8c39SChen, Kenneth W 			kfree(rg);
1077a43a8c39SChen, Kenneth W 		}
1078a43a8c39SChen, Kenneth W 	}
1079a43a8c39SChen, Kenneth W 	nrg->from = f;
1080a43a8c39SChen, Kenneth W 	nrg->to = t;
1081a43a8c39SChen, Kenneth W 	return 0;
1082a43a8c39SChen, Kenneth W }
1083a43a8c39SChen, Kenneth W 
1084a43a8c39SChen, Kenneth W static long region_chg(struct list_head *head, long f, long t)
1085a43a8c39SChen, Kenneth W {
1086a43a8c39SChen, Kenneth W 	struct file_region *rg, *nrg;
1087a43a8c39SChen, Kenneth W 	long chg = 0;
1088a43a8c39SChen, Kenneth W 
1089a43a8c39SChen, Kenneth W 	/* Locate the region we are before or in. */
1090a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, head, link)
1091a43a8c39SChen, Kenneth W 		if (f <= rg->to)
1092a43a8c39SChen, Kenneth W 			break;
1093a43a8c39SChen, Kenneth W 
1094a43a8c39SChen, Kenneth W 	/* If we are below the current region then a new region is required.
1095a43a8c39SChen, Kenneth W 	 * Subtle, allocate a new region at the position but make it zero
1096183ff22bSSimon Arlott 	 * size such that we can guarantee to record the reservation. */
1097a43a8c39SChen, Kenneth W 	if (&rg->link == head || t < rg->from) {
1098a43a8c39SChen, Kenneth W 		nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
1099c80544dcSStephen Hemminger 		if (!nrg)
1100a43a8c39SChen, Kenneth W 			return -ENOMEM;
1101a43a8c39SChen, Kenneth W 		nrg->from = f;
1102a43a8c39SChen, Kenneth W 		nrg->to   = f;
1103a43a8c39SChen, Kenneth W 		INIT_LIST_HEAD(&nrg->link);
1104a43a8c39SChen, Kenneth W 		list_add(&nrg->link, rg->link.prev);
1105a43a8c39SChen, Kenneth W 
1106a43a8c39SChen, Kenneth W 		return t - f;
1107a43a8c39SChen, Kenneth W 	}
1108a43a8c39SChen, Kenneth W 
1109a43a8c39SChen, Kenneth W 	/* Round our left edge to the current segment if it encloses us. */
1110a43a8c39SChen, Kenneth W 	if (f > rg->from)
1111a43a8c39SChen, Kenneth W 		f = rg->from;
1112a43a8c39SChen, Kenneth W 	chg = t - f;
1113a43a8c39SChen, Kenneth W 
1114a43a8c39SChen, Kenneth W 	/* Check for and consume any regions we now overlap with. */
1115a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, rg->link.prev, link) {
1116a43a8c39SChen, Kenneth W 		if (&rg->link == head)
1117a43a8c39SChen, Kenneth W 			break;
1118a43a8c39SChen, Kenneth W 		if (rg->from > t)
1119a43a8c39SChen, Kenneth W 			return chg;
1120a43a8c39SChen, Kenneth W 
1121a43a8c39SChen, Kenneth W 		/* We overlap with this area, if it extends futher than
1122a43a8c39SChen, Kenneth W 		 * us then we must extend ourselves.  Account for its
1123a43a8c39SChen, Kenneth W 		 * existing reservation. */
1124a43a8c39SChen, Kenneth W 		if (rg->to > t) {
1125a43a8c39SChen, Kenneth W 			chg += rg->to - t;
1126a43a8c39SChen, Kenneth W 			t = rg->to;
1127a43a8c39SChen, Kenneth W 		}
1128a43a8c39SChen, Kenneth W 		chg -= rg->to - rg->from;
1129a43a8c39SChen, Kenneth W 	}
1130a43a8c39SChen, Kenneth W 	return chg;
1131a43a8c39SChen, Kenneth W }
1132a43a8c39SChen, Kenneth W 
1133a43a8c39SChen, Kenneth W static long region_truncate(struct list_head *head, long end)
1134a43a8c39SChen, Kenneth W {
1135a43a8c39SChen, Kenneth W 	struct file_region *rg, *trg;
1136a43a8c39SChen, Kenneth W 	long chg = 0;
1137a43a8c39SChen, Kenneth W 
1138a43a8c39SChen, Kenneth W 	/* Locate the region we are either in or before. */
1139a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, head, link)
1140a43a8c39SChen, Kenneth W 		if (end <= rg->to)
1141a43a8c39SChen, Kenneth W 			break;
1142a43a8c39SChen, Kenneth W 	if (&rg->link == head)
1143a43a8c39SChen, Kenneth W 		return 0;
1144a43a8c39SChen, Kenneth W 
1145a43a8c39SChen, Kenneth W 	/* If we are in the middle of a region then adjust it. */
1146a43a8c39SChen, Kenneth W 	if (end > rg->from) {
1147a43a8c39SChen, Kenneth W 		chg = rg->to - end;
1148a43a8c39SChen, Kenneth W 		rg->to = end;
1149a43a8c39SChen, Kenneth W 		rg = list_entry(rg->link.next, typeof(*rg), link);
1150a43a8c39SChen, Kenneth W 	}
1151a43a8c39SChen, Kenneth W 
1152a43a8c39SChen, Kenneth W 	/* Drop any remaining regions. */
1153a43a8c39SChen, Kenneth W 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
1154a43a8c39SChen, Kenneth W 		if (&rg->link == head)
1155a43a8c39SChen, Kenneth W 			break;
1156a43a8c39SChen, Kenneth W 		chg += rg->to - rg->from;
1157a43a8c39SChen, Kenneth W 		list_del(&rg->link);
1158a43a8c39SChen, Kenneth W 		kfree(rg);
1159a43a8c39SChen, Kenneth W 	}
1160a43a8c39SChen, Kenneth W 	return chg;
1161a43a8c39SChen, Kenneth W }
1162a43a8c39SChen, Kenneth W 
1163a43a8c39SChen, Kenneth W static int hugetlb_acct_memory(long delta)
1164a43a8c39SChen, Kenneth W {
1165a43a8c39SChen, Kenneth W 	int ret = -ENOMEM;
1166a43a8c39SChen, Kenneth W 
1167a43a8c39SChen, Kenneth W 	spin_lock(&hugetlb_lock);
11688a630112SKen Chen 	/*
11698a630112SKen Chen 	 * When cpuset is configured, it breaks the strict hugetlb page
11708a630112SKen Chen 	 * reservation as the accounting is done on a global variable. Such
11718a630112SKen Chen 	 * reservation is completely rubbish in the presence of cpuset because
11728a630112SKen Chen 	 * the reservation is not checked against page availability for the
11738a630112SKen Chen 	 * current cpuset. Application can still potentially OOM'ed by kernel
11748a630112SKen Chen 	 * with lack of free htlb page in cpuset that the task is in.
11758a630112SKen Chen 	 * Attempt to enforce strict accounting with cpuset is almost
11768a630112SKen Chen 	 * impossible (or too ugly) because cpuset is too fluid that
11778a630112SKen Chen 	 * task or memory node can be dynamically moved between cpusets.
11788a630112SKen Chen 	 *
11798a630112SKen Chen 	 * The change of semantics for shared hugetlb mapping with cpuset is
11808a630112SKen Chen 	 * undesirable. However, in order to preserve some of the semantics,
11818a630112SKen Chen 	 * we fall back to check against current free page availability as
11828a630112SKen Chen 	 * a best attempt and hopefully to minimize the impact of changing
11838a630112SKen Chen 	 * semantics that cpuset has.
11848a630112SKen Chen 	 */
1185e4e574b7SAdam Litke 	if (delta > 0) {
1186e4e574b7SAdam Litke 		if (gather_surplus_pages(delta) < 0)
1187e4e574b7SAdam Litke 			goto out;
1188e4e574b7SAdam Litke 
1189e4e574b7SAdam Litke 		if (delta > cpuset_mems_nr(free_huge_pages_node))
1190e4e574b7SAdam Litke 			goto out;
1191e4e574b7SAdam Litke 	}
1192e4e574b7SAdam Litke 
1193e4e574b7SAdam Litke 	ret = 0;
1194e4e574b7SAdam Litke 	resv_huge_pages += delta;
1195e4e574b7SAdam Litke 	if (delta < 0)
1196e4e574b7SAdam Litke 		return_unused_surplus_pages((unsigned long) -delta);
1197e4e574b7SAdam Litke 
1198e4e574b7SAdam Litke out:
1199e4e574b7SAdam Litke 	spin_unlock(&hugetlb_lock);
1200e4e574b7SAdam Litke 	return ret;
1201e4e574b7SAdam Litke }
1202e4e574b7SAdam Litke 
1203e4e574b7SAdam Litke int hugetlb_reserve_pages(struct inode *inode, long from, long to)
1204e4e574b7SAdam Litke {
1205e4e574b7SAdam Litke 	long ret, chg;
1206e4e574b7SAdam Litke 
1207e4e574b7SAdam Litke 	chg = region_chg(&inode->i_mapping->private_list, from, to);
1208e4e574b7SAdam Litke 	if (chg < 0)
1209e4e574b7SAdam Litke 		return chg;
12108a630112SKen Chen 
121190d8b7e6SAdam Litke 	if (hugetlb_get_quota(inode->i_mapping, chg))
121290d8b7e6SAdam Litke 		return -ENOSPC;
1213a43a8c39SChen, Kenneth W 	ret = hugetlb_acct_memory(chg);
1214a43a8c39SChen, Kenneth W 	if (ret < 0)
1215a43a8c39SChen, Kenneth W 		return ret;
1216a43a8c39SChen, Kenneth W 	region_add(&inode->i_mapping->private_list, from, to);
1217a43a8c39SChen, Kenneth W 	return 0;
1218a43a8c39SChen, Kenneth W }
1219a43a8c39SChen, Kenneth W 
1220a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1221a43a8c39SChen, Kenneth W {
1222a43a8c39SChen, Kenneth W 	long chg = region_truncate(&inode->i_mapping->private_list, offset);
122345c682a6SKen Chen 
122445c682a6SKen Chen 	spin_lock(&inode->i_lock);
122545c682a6SKen Chen 	inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
122645c682a6SKen Chen 	spin_unlock(&inode->i_lock);
122745c682a6SKen Chen 
122890d8b7e6SAdam Litke 	hugetlb_put_quota(inode->i_mapping, (chg - freed));
122990d8b7e6SAdam Litke 	hugetlb_acct_memory(-(chg - freed));
1230a43a8c39SChen, Kenneth W }
1231