xref: /openbmc/linux/mm/hugetlb.c (revision 90d8b7e6)
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;
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;
2307893d1d5SAdam Litke 
23154f9f80dSAdam Litke 	/* Check if the dynamic pool is enabled */
23254f9f80dSAdam Litke 	if (!hugetlb_dynamic_pool)
23354f9f80dSAdam Litke 		return NULL;
23454f9f80dSAdam Litke 
2357893d1d5SAdam Litke 	page = alloc_pages(htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN,
2367893d1d5SAdam Litke 					HUGETLB_PAGE_ORDER);
2377893d1d5SAdam Litke 	if (page) {
2387893d1d5SAdam Litke 		set_compound_page_dtor(page, free_huge_page);
2397893d1d5SAdam Litke 		spin_lock(&hugetlb_lock);
2407893d1d5SAdam Litke 		nr_huge_pages++;
2417893d1d5SAdam Litke 		nr_huge_pages_node[page_to_nid(page)]++;
2427893d1d5SAdam Litke 		surplus_huge_pages++;
2437893d1d5SAdam Litke 		surplus_huge_pages_node[page_to_nid(page)]++;
2447893d1d5SAdam Litke 		spin_unlock(&hugetlb_lock);
2457893d1d5SAdam Litke 	}
2467893d1d5SAdam Litke 
2477893d1d5SAdam Litke 	return page;
2487893d1d5SAdam Litke }
2497893d1d5SAdam Litke 
250e4e574b7SAdam Litke /*
251e4e574b7SAdam Litke  * Increase the hugetlb pool such that it can accomodate a reservation
252e4e574b7SAdam Litke  * of size 'delta'.
253e4e574b7SAdam Litke  */
254e4e574b7SAdam Litke static int gather_surplus_pages(int delta)
255e4e574b7SAdam Litke {
256e4e574b7SAdam Litke 	struct list_head surplus_list;
257e4e574b7SAdam Litke 	struct page *page, *tmp;
258e4e574b7SAdam Litke 	int ret, i;
259e4e574b7SAdam Litke 	int needed, allocated;
260e4e574b7SAdam Litke 
261e4e574b7SAdam Litke 	needed = (resv_huge_pages + delta) - free_huge_pages;
262e4e574b7SAdam Litke 	if (needed <= 0)
263e4e574b7SAdam Litke 		return 0;
264e4e574b7SAdam Litke 
265e4e574b7SAdam Litke 	allocated = 0;
266e4e574b7SAdam Litke 	INIT_LIST_HEAD(&surplus_list);
267e4e574b7SAdam Litke 
268e4e574b7SAdam Litke 	ret = -ENOMEM;
269e4e574b7SAdam Litke retry:
270e4e574b7SAdam Litke 	spin_unlock(&hugetlb_lock);
271e4e574b7SAdam Litke 	for (i = 0; i < needed; i++) {
272e4e574b7SAdam Litke 		page = alloc_buddy_huge_page(NULL, 0);
273e4e574b7SAdam Litke 		if (!page) {
274e4e574b7SAdam Litke 			/*
275e4e574b7SAdam Litke 			 * We were not able to allocate enough pages to
276e4e574b7SAdam Litke 			 * satisfy the entire reservation so we free what
277e4e574b7SAdam Litke 			 * we've allocated so far.
278e4e574b7SAdam Litke 			 */
279e4e574b7SAdam Litke 			spin_lock(&hugetlb_lock);
280e4e574b7SAdam Litke 			needed = 0;
281e4e574b7SAdam Litke 			goto free;
282e4e574b7SAdam Litke 		}
283e4e574b7SAdam Litke 
284e4e574b7SAdam Litke 		list_add(&page->lru, &surplus_list);
285e4e574b7SAdam Litke 	}
286e4e574b7SAdam Litke 	allocated += needed;
287e4e574b7SAdam Litke 
288e4e574b7SAdam Litke 	/*
289e4e574b7SAdam Litke 	 * After retaking hugetlb_lock, we need to recalculate 'needed'
290e4e574b7SAdam Litke 	 * because either resv_huge_pages or free_huge_pages may have changed.
291e4e574b7SAdam Litke 	 */
292e4e574b7SAdam Litke 	spin_lock(&hugetlb_lock);
293e4e574b7SAdam Litke 	needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
294e4e574b7SAdam Litke 	if (needed > 0)
295e4e574b7SAdam Litke 		goto retry;
296e4e574b7SAdam Litke 
297e4e574b7SAdam Litke 	/*
298e4e574b7SAdam Litke 	 * The surplus_list now contains _at_least_ the number of extra pages
299e4e574b7SAdam Litke 	 * needed to accomodate the reservation.  Add the appropriate number
300e4e574b7SAdam Litke 	 * of pages to the hugetlb pool and free the extras back to the buddy
301e4e574b7SAdam Litke 	 * allocator.
302e4e574b7SAdam Litke 	 */
303e4e574b7SAdam Litke 	needed += allocated;
304e4e574b7SAdam Litke 	ret = 0;
305e4e574b7SAdam Litke free:
306e4e574b7SAdam Litke 	list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
307e4e574b7SAdam Litke 		list_del(&page->lru);
308e4e574b7SAdam Litke 		if ((--needed) >= 0)
309e4e574b7SAdam Litke 			enqueue_huge_page(page);
310af767cbdSAdam Litke 		else {
311af767cbdSAdam Litke 			/*
312af767cbdSAdam Litke 			 * Decrement the refcount and free the page using its
313af767cbdSAdam Litke 			 * destructor.  This must be done with hugetlb_lock
314af767cbdSAdam Litke 			 * unlocked which is safe because free_huge_page takes
315af767cbdSAdam Litke 			 * hugetlb_lock before deciding how to free the page.
316af767cbdSAdam Litke 			 */
317af767cbdSAdam Litke 			spin_unlock(&hugetlb_lock);
318af767cbdSAdam Litke 			put_page(page);
319af767cbdSAdam Litke 			spin_lock(&hugetlb_lock);
320af767cbdSAdam Litke 		}
321e4e574b7SAdam Litke 	}
322e4e574b7SAdam Litke 
323e4e574b7SAdam Litke 	return ret;
324e4e574b7SAdam Litke }
325e4e574b7SAdam Litke 
326e4e574b7SAdam Litke /*
327e4e574b7SAdam Litke  * When releasing a hugetlb pool reservation, any surplus pages that were
328e4e574b7SAdam Litke  * allocated to satisfy the reservation must be explicitly freed if they were
329e4e574b7SAdam Litke  * never used.
330e4e574b7SAdam Litke  */
331e4e574b7SAdam Litke void return_unused_surplus_pages(unsigned long unused_resv_pages)
332e4e574b7SAdam Litke {
333e4e574b7SAdam Litke 	static int nid = -1;
334e4e574b7SAdam Litke 	struct page *page;
335e4e574b7SAdam Litke 	unsigned long nr_pages;
336e4e574b7SAdam Litke 
337e4e574b7SAdam Litke 	nr_pages = min(unused_resv_pages, surplus_huge_pages);
338e4e574b7SAdam Litke 
339e4e574b7SAdam Litke 	while (nr_pages) {
340e4e574b7SAdam Litke 		nid = next_node(nid, node_online_map);
341e4e574b7SAdam Litke 		if (nid == MAX_NUMNODES)
342e4e574b7SAdam Litke 			nid = first_node(node_online_map);
343e4e574b7SAdam Litke 
344e4e574b7SAdam Litke 		if (!surplus_huge_pages_node[nid])
345e4e574b7SAdam Litke 			continue;
346e4e574b7SAdam Litke 
347e4e574b7SAdam Litke 		if (!list_empty(&hugepage_freelists[nid])) {
348e4e574b7SAdam Litke 			page = list_entry(hugepage_freelists[nid].next,
349e4e574b7SAdam Litke 					  struct page, lru);
350e4e574b7SAdam Litke 			list_del(&page->lru);
351e4e574b7SAdam Litke 			update_and_free_page(page);
352e4e574b7SAdam Litke 			free_huge_pages--;
353e4e574b7SAdam Litke 			free_huge_pages_node[nid]--;
354e4e574b7SAdam Litke 			surplus_huge_pages--;
355e4e574b7SAdam Litke 			surplus_huge_pages_node[nid]--;
356e4e574b7SAdam Litke 			nr_pages--;
357e4e574b7SAdam Litke 		}
358e4e574b7SAdam Litke 	}
359e4e574b7SAdam Litke }
360e4e574b7SAdam Litke 
361348ea204SAdam Litke 
362348ea204SAdam Litke static struct page *alloc_huge_page_shared(struct vm_area_struct *vma,
363348ea204SAdam Litke 						unsigned long addr)
364348ea204SAdam Litke {
365348ea204SAdam Litke 	struct page *page;
366348ea204SAdam Litke 
367348ea204SAdam Litke 	spin_lock(&hugetlb_lock);
368348ea204SAdam Litke 	page = dequeue_huge_page(vma, addr);
369348ea204SAdam Litke 	spin_unlock(&hugetlb_lock);
37090d8b7e6SAdam Litke 	return page ? page : ERR_PTR(-VM_FAULT_OOM);
371348ea204SAdam Litke }
372348ea204SAdam Litke 
373348ea204SAdam Litke static struct page *alloc_huge_page_private(struct vm_area_struct *vma,
37427a85ef1SDavid Gibson 						unsigned long addr)
3751da177e4SLinus Torvalds {
3767893d1d5SAdam Litke 	struct page *page = NULL;
3771da177e4SLinus Torvalds 
37890d8b7e6SAdam Litke 	if (hugetlb_get_quota(vma->vm_file->f_mapping, 1))
37990d8b7e6SAdam Litke 		return ERR_PTR(-VM_FAULT_SIGBUS);
38090d8b7e6SAdam Litke 
3811da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
382348ea204SAdam Litke 	if (free_huge_pages > resv_huge_pages)
383b45b5bd6SDavid Gibson 		page = dequeue_huge_page(vma, addr);
384348ea204SAdam Litke 	spin_unlock(&hugetlb_lock);
385b45b5bd6SDavid Gibson 	if (!page)
3867893d1d5SAdam Litke 		page = alloc_buddy_huge_page(vma, addr);
38790d8b7e6SAdam Litke 	return page ? page : ERR_PTR(-VM_FAULT_OOM);
388348ea204SAdam Litke }
3897893d1d5SAdam Litke 
390348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma,
391348ea204SAdam Litke 				    unsigned long addr)
392348ea204SAdam Litke {
393348ea204SAdam Litke 	struct page *page;
3942fc39cecSAdam Litke 	struct address_space *mapping = vma->vm_file->f_mapping;
3952fc39cecSAdam Litke 
396348ea204SAdam Litke 	if (vma->vm_flags & VM_MAYSHARE)
397348ea204SAdam Litke 		page = alloc_huge_page_shared(vma, addr);
398348ea204SAdam Litke 	else
399348ea204SAdam Litke 		page = alloc_huge_page_private(vma, addr);
40090d8b7e6SAdam Litke 
40190d8b7e6SAdam Litke 	if (!IS_ERR(page)) {
402348ea204SAdam Litke 		set_page_refcounted(page);
4032fc39cecSAdam Litke 		set_page_private(page, (unsigned long) mapping);
40490d8b7e6SAdam Litke 	}
4057893d1d5SAdam Litke 	return page;
406b45b5bd6SDavid Gibson }
407b45b5bd6SDavid Gibson 
4081da177e4SLinus Torvalds static int __init hugetlb_init(void)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	unsigned long i;
4111da177e4SLinus Torvalds 
4123c726f8dSBenjamin Herrenschmidt 	if (HPAGE_SHIFT == 0)
4133c726f8dSBenjamin Herrenschmidt 		return 0;
4143c726f8dSBenjamin Herrenschmidt 
4151da177e4SLinus Torvalds 	for (i = 0; i < MAX_NUMNODES; ++i)
4161da177e4SLinus Torvalds 		INIT_LIST_HEAD(&hugepage_freelists[i]);
4171da177e4SLinus Torvalds 
41863b4613cSNishanth Aravamudan 	hugetlb_next_nid = first_node(node_online_map);
41963b4613cSNishanth Aravamudan 
4201da177e4SLinus Torvalds 	for (i = 0; i < max_huge_pages; ++i) {
421a482289dSNick Piggin 		if (!alloc_fresh_huge_page())
4221da177e4SLinus Torvalds 			break;
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 	max_huge_pages = free_huge_pages = nr_huge_pages = i;
4251da177e4SLinus Torvalds 	printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
4261da177e4SLinus Torvalds 	return 0;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds module_init(hugetlb_init);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds static int __init hugetlb_setup(char *s)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	if (sscanf(s, "%lu", &max_huge_pages) <= 0)
4331da177e4SLinus Torvalds 		max_huge_pages = 0;
4341da177e4SLinus Torvalds 	return 1;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds __setup("hugepages=", hugetlb_setup);
4371da177e4SLinus Torvalds 
4388a630112SKen Chen static unsigned int cpuset_mems_nr(unsigned int *array)
4398a630112SKen Chen {
4408a630112SKen Chen 	int node;
4418a630112SKen Chen 	unsigned int nr = 0;
4428a630112SKen Chen 
4438a630112SKen Chen 	for_each_node_mask(node, cpuset_current_mems_allowed)
4448a630112SKen Chen 		nr += array[node];
4458a630112SKen Chen 
4468a630112SKen Chen 	return nr;
4478a630112SKen Chen }
4488a630112SKen Chen 
4491da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
4501da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM
4511da177e4SLinus Torvalds static void try_to_free_low(unsigned long count)
4521da177e4SLinus Torvalds {
4534415cc8dSChristoph Lameter 	int i;
4544415cc8dSChristoph Lameter 
4551da177e4SLinus Torvalds 	for (i = 0; i < MAX_NUMNODES; ++i) {
4561da177e4SLinus Torvalds 		struct page *page, *next;
4571da177e4SLinus Torvalds 		list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
4586b0c880dSAdam Litke 			if (count >= nr_huge_pages)
4596b0c880dSAdam Litke 				return;
4601da177e4SLinus Torvalds 			if (PageHighMem(page))
4611da177e4SLinus Torvalds 				continue;
4621da177e4SLinus Torvalds 			list_del(&page->lru);
4631da177e4SLinus Torvalds 			update_and_free_page(page);
4641da177e4SLinus Torvalds 			free_huge_pages--;
4654415cc8dSChristoph Lameter 			free_huge_pages_node[page_to_nid(page)]--;
4661da177e4SLinus Torvalds 		}
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds #else
4701da177e4SLinus Torvalds static inline void try_to_free_low(unsigned long count)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds #endif
4741da177e4SLinus Torvalds 
4757893d1d5SAdam Litke #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
4761da177e4SLinus Torvalds static unsigned long set_max_huge_pages(unsigned long count)
4771da177e4SLinus Torvalds {
4787893d1d5SAdam Litke 	unsigned long min_count, ret;
4791da177e4SLinus Torvalds 
4807893d1d5SAdam Litke 	/*
4817893d1d5SAdam Litke 	 * Increase the pool size
4827893d1d5SAdam Litke 	 * First take pages out of surplus state.  Then make up the
4837893d1d5SAdam Litke 	 * remaining difference by allocating fresh huge pages.
4847893d1d5SAdam Litke 	 */
4851da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
4867893d1d5SAdam Litke 	while (surplus_huge_pages && count > persistent_huge_pages) {
4877893d1d5SAdam Litke 		if (!adjust_pool_surplus(-1))
4887893d1d5SAdam Litke 			break;
4897893d1d5SAdam Litke 	}
4907893d1d5SAdam Litke 
4917893d1d5SAdam Litke 	while (count > persistent_huge_pages) {
4927893d1d5SAdam Litke 		int ret;
4937893d1d5SAdam Litke 		/*
4947893d1d5SAdam Litke 		 * If this allocation races such that we no longer need the
4957893d1d5SAdam Litke 		 * page, free_huge_page will handle it by freeing the page
4967893d1d5SAdam Litke 		 * and reducing the surplus.
4977893d1d5SAdam Litke 		 */
4987893d1d5SAdam Litke 		spin_unlock(&hugetlb_lock);
4997893d1d5SAdam Litke 		ret = alloc_fresh_huge_page();
5007893d1d5SAdam Litke 		spin_lock(&hugetlb_lock);
5017893d1d5SAdam Litke 		if (!ret)
5027893d1d5SAdam Litke 			goto out;
5037893d1d5SAdam Litke 
5047893d1d5SAdam Litke 	}
5057893d1d5SAdam Litke 
5067893d1d5SAdam Litke 	/*
5077893d1d5SAdam Litke 	 * Decrease the pool size
5087893d1d5SAdam Litke 	 * First return free pages to the buddy allocator (being careful
5097893d1d5SAdam Litke 	 * to keep enough around to satisfy reservations).  Then place
5107893d1d5SAdam Litke 	 * pages into surplus state as needed so the pool will shrink
5117893d1d5SAdam Litke 	 * to the desired size as pages become free.
5127893d1d5SAdam Litke 	 */
5136b0c880dSAdam Litke 	min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
5146b0c880dSAdam Litke 	min_count = max(count, min_count);
5157893d1d5SAdam Litke 	try_to_free_low(min_count);
5167893d1d5SAdam Litke 	while (min_count < persistent_huge_pages) {
5175da7ca86SChristoph Lameter 		struct page *page = dequeue_huge_page(NULL, 0);
5181da177e4SLinus Torvalds 		if (!page)
5191da177e4SLinus Torvalds 			break;
5201da177e4SLinus Torvalds 		update_and_free_page(page);
5211da177e4SLinus Torvalds 	}
5227893d1d5SAdam Litke 	while (count < persistent_huge_pages) {
5237893d1d5SAdam Litke 		if (!adjust_pool_surplus(1))
5247893d1d5SAdam Litke 			break;
5257893d1d5SAdam Litke 	}
5267893d1d5SAdam Litke out:
5277893d1d5SAdam Litke 	ret = persistent_huge_pages;
5281da177e4SLinus Torvalds 	spin_unlock(&hugetlb_lock);
5297893d1d5SAdam Litke 	return ret;
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds int hugetlb_sysctl_handler(struct ctl_table *table, int write,
5331da177e4SLinus Torvalds 			   struct file *file, void __user *buffer,
5341da177e4SLinus Torvalds 			   size_t *length, loff_t *ppos)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
5371da177e4SLinus Torvalds 	max_huge_pages = set_max_huge_pages(max_huge_pages);
5381da177e4SLinus Torvalds 	return 0;
5391da177e4SLinus Torvalds }
540396faf03SMel Gorman 
541396faf03SMel Gorman int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
542396faf03SMel Gorman 			struct file *file, void __user *buffer,
543396faf03SMel Gorman 			size_t *length, loff_t *ppos)
544396faf03SMel Gorman {
545396faf03SMel Gorman 	proc_dointvec(table, write, file, buffer, length, ppos);
546396faf03SMel Gorman 	if (hugepages_treat_as_movable)
547396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
548396faf03SMel Gorman 	else
549396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER;
550396faf03SMel Gorman 	return 0;
551396faf03SMel Gorman }
552396faf03SMel Gorman 
5531da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds int hugetlb_report_meminfo(char *buf)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	return sprintf(buf,
5581da177e4SLinus Torvalds 			"HugePages_Total: %5lu\n"
5591da177e4SLinus Torvalds 			"HugePages_Free:  %5lu\n"
560b45b5bd6SDavid Gibson 			"HugePages_Rsvd:  %5lu\n"
5617893d1d5SAdam Litke 			"HugePages_Surp:  %5lu\n"
5621da177e4SLinus Torvalds 			"Hugepagesize:    %5lu kB\n",
5631da177e4SLinus Torvalds 			nr_huge_pages,
5641da177e4SLinus Torvalds 			free_huge_pages,
565a43a8c39SChen, Kenneth W 			resv_huge_pages,
5667893d1d5SAdam Litke 			surplus_huge_pages,
5671da177e4SLinus Torvalds 			HPAGE_SIZE/1024);
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf)
5711da177e4SLinus Torvalds {
5721da177e4SLinus Torvalds 	return sprintf(buf,
5731da177e4SLinus Torvalds 		"Node %d HugePages_Total: %5u\n"
5741da177e4SLinus Torvalds 		"Node %d HugePages_Free:  %5u\n",
5751da177e4SLinus Torvalds 		nid, nr_huge_pages_node[nid],
5761da177e4SLinus Torvalds 		nid, free_huge_pages_node[nid]);
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
5801da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds /*
5861da177e4SLinus Torvalds  * We cannot handle pagefaults against hugetlb pages at all.  They cause
5871da177e4SLinus Torvalds  * handle_mm_fault() to try to instantiate regular-sized pages in the
5881da177e4SLinus Torvalds  * hugegpage VMA.  do_page_fault() is supposed to trap this, so BUG is we get
5891da177e4SLinus Torvalds  * this far.
5901da177e4SLinus Torvalds  */
591d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	BUG();
594d0217ac0SNick Piggin 	return 0;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds struct vm_operations_struct hugetlb_vm_ops = {
598d0217ac0SNick Piggin 	.fault = hugetlb_vm_op_fault,
5991da177e4SLinus Torvalds };
6001da177e4SLinus Torvalds 
6011e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
6021e8f889bSDavid Gibson 				int writable)
60363551ae0SDavid Gibson {
60463551ae0SDavid Gibson 	pte_t entry;
60563551ae0SDavid Gibson 
6061e8f889bSDavid Gibson 	if (writable) {
60763551ae0SDavid Gibson 		entry =
60863551ae0SDavid Gibson 		    pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
60963551ae0SDavid Gibson 	} else {
61063551ae0SDavid Gibson 		entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
61163551ae0SDavid Gibson 	}
61263551ae0SDavid Gibson 	entry = pte_mkyoung(entry);
61363551ae0SDavid Gibson 	entry = pte_mkhuge(entry);
61463551ae0SDavid Gibson 
61563551ae0SDavid Gibson 	return entry;
61663551ae0SDavid Gibson }
61763551ae0SDavid Gibson 
6181e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma,
6191e8f889bSDavid Gibson 				   unsigned long address, pte_t *ptep)
6201e8f889bSDavid Gibson {
6211e8f889bSDavid Gibson 	pte_t entry;
6221e8f889bSDavid Gibson 
6231e8f889bSDavid Gibson 	entry = pte_mkwrite(pte_mkdirty(*ptep));
6248dab5241SBenjamin Herrenschmidt 	if (ptep_set_access_flags(vma, address, ptep, entry, 1)) {
6251e8f889bSDavid Gibson 		update_mmu_cache(vma, address, entry);
6261e8f889bSDavid Gibson 	}
6278dab5241SBenjamin Herrenschmidt }
6281e8f889bSDavid Gibson 
6291e8f889bSDavid Gibson 
63063551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
63163551ae0SDavid Gibson 			    struct vm_area_struct *vma)
63263551ae0SDavid Gibson {
63363551ae0SDavid Gibson 	pte_t *src_pte, *dst_pte, entry;
63463551ae0SDavid Gibson 	struct page *ptepage;
6351c59827dSHugh Dickins 	unsigned long addr;
6361e8f889bSDavid Gibson 	int cow;
6371e8f889bSDavid Gibson 
6381e8f889bSDavid Gibson 	cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
63963551ae0SDavid Gibson 
6401c59827dSHugh Dickins 	for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
641c74df32cSHugh Dickins 		src_pte = huge_pte_offset(src, addr);
642c74df32cSHugh Dickins 		if (!src_pte)
643c74df32cSHugh Dickins 			continue;
64463551ae0SDavid Gibson 		dst_pte = huge_pte_alloc(dst, addr);
64563551ae0SDavid Gibson 		if (!dst_pte)
64663551ae0SDavid Gibson 			goto nomem;
647c74df32cSHugh Dickins 		spin_lock(&dst->page_table_lock);
6481c59827dSHugh Dickins 		spin_lock(&src->page_table_lock);
649c74df32cSHugh Dickins 		if (!pte_none(*src_pte)) {
6501e8f889bSDavid Gibson 			if (cow)
6511e8f889bSDavid Gibson 				ptep_set_wrprotect(src, addr, src_pte);
65263551ae0SDavid Gibson 			entry = *src_pte;
65363551ae0SDavid Gibson 			ptepage = pte_page(entry);
65463551ae0SDavid Gibson 			get_page(ptepage);
65563551ae0SDavid Gibson 			set_huge_pte_at(dst, addr, dst_pte, entry);
6561c59827dSHugh Dickins 		}
6571c59827dSHugh Dickins 		spin_unlock(&src->page_table_lock);
658c74df32cSHugh Dickins 		spin_unlock(&dst->page_table_lock);
65963551ae0SDavid Gibson 	}
66063551ae0SDavid Gibson 	return 0;
66163551ae0SDavid Gibson 
66263551ae0SDavid Gibson nomem:
66363551ae0SDavid Gibson 	return -ENOMEM;
66463551ae0SDavid Gibson }
66563551ae0SDavid Gibson 
666502717f4SChen, Kenneth W void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
66763551ae0SDavid Gibson 			    unsigned long end)
66863551ae0SDavid Gibson {
66963551ae0SDavid Gibson 	struct mm_struct *mm = vma->vm_mm;
67063551ae0SDavid Gibson 	unsigned long address;
671c7546f8fSDavid Gibson 	pte_t *ptep;
67263551ae0SDavid Gibson 	pte_t pte;
67363551ae0SDavid Gibson 	struct page *page;
674fe1668aeSChen, Kenneth W 	struct page *tmp;
675c0a499c2SChen, Kenneth W 	/*
676c0a499c2SChen, Kenneth W 	 * A page gathering list, protected by per file i_mmap_lock. The
677c0a499c2SChen, Kenneth W 	 * lock is used to avoid list corruption from multiple unmapping
678c0a499c2SChen, Kenneth W 	 * of the same page since we are using page->lru.
679c0a499c2SChen, Kenneth W 	 */
680fe1668aeSChen, Kenneth W 	LIST_HEAD(page_list);
68163551ae0SDavid Gibson 
68263551ae0SDavid Gibson 	WARN_ON(!is_vm_hugetlb_page(vma));
68363551ae0SDavid Gibson 	BUG_ON(start & ~HPAGE_MASK);
68463551ae0SDavid Gibson 	BUG_ON(end & ~HPAGE_MASK);
68563551ae0SDavid Gibson 
686508034a3SHugh Dickins 	spin_lock(&mm->page_table_lock);
68763551ae0SDavid Gibson 	for (address = start; address < end; address += HPAGE_SIZE) {
688c7546f8fSDavid Gibson 		ptep = huge_pte_offset(mm, address);
689c7546f8fSDavid Gibson 		if (!ptep)
690c7546f8fSDavid Gibson 			continue;
691c7546f8fSDavid Gibson 
69239dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
69339dde65cSChen, Kenneth W 			continue;
69439dde65cSChen, Kenneth W 
695c7546f8fSDavid Gibson 		pte = huge_ptep_get_and_clear(mm, address, ptep);
69663551ae0SDavid Gibson 		if (pte_none(pte))
69763551ae0SDavid Gibson 			continue;
698c7546f8fSDavid Gibson 
69963551ae0SDavid Gibson 		page = pte_page(pte);
7006649a386SKen Chen 		if (pte_dirty(pte))
7016649a386SKen Chen 			set_page_dirty(page);
702fe1668aeSChen, Kenneth W 		list_add(&page->lru, &page_list);
70363551ae0SDavid Gibson 	}
7041da177e4SLinus Torvalds 	spin_unlock(&mm->page_table_lock);
705508034a3SHugh Dickins 	flush_tlb_range(vma, start, end);
706fe1668aeSChen, Kenneth W 	list_for_each_entry_safe(page, tmp, &page_list, lru) {
707fe1668aeSChen, Kenneth W 		list_del(&page->lru);
708fe1668aeSChen, Kenneth W 		put_page(page);
709fe1668aeSChen, Kenneth W 	}
7101da177e4SLinus Torvalds }
71163551ae0SDavid Gibson 
712502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
713502717f4SChen, Kenneth W 			  unsigned long end)
714502717f4SChen, Kenneth W {
715502717f4SChen, Kenneth W 	/*
716502717f4SChen, Kenneth W 	 * It is undesirable to test vma->vm_file as it should be non-null
717502717f4SChen, Kenneth W 	 * for valid hugetlb area. However, vm_file will be NULL in the error
718502717f4SChen, Kenneth W 	 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
719502717f4SChen, Kenneth W 	 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
720502717f4SChen, Kenneth W 	 * to clean up. Since no pte has actually been setup, it is safe to
721502717f4SChen, Kenneth W 	 * do nothing in this case.
722502717f4SChen, Kenneth W 	 */
723502717f4SChen, Kenneth W 	if (vma->vm_file) {
724502717f4SChen, Kenneth W 		spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
725502717f4SChen, Kenneth W 		__unmap_hugepage_range(vma, start, end);
726502717f4SChen, Kenneth W 		spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
727502717f4SChen, Kenneth W 	}
728502717f4SChen, Kenneth W }
729502717f4SChen, Kenneth W 
7301e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
7311e8f889bSDavid Gibson 			unsigned long address, pte_t *ptep, pte_t pte)
7321e8f889bSDavid Gibson {
7331e8f889bSDavid Gibson 	struct page *old_page, *new_page;
73479ac6ba4SDavid Gibson 	int avoidcopy;
7351e8f889bSDavid Gibson 
7361e8f889bSDavid Gibson 	old_page = pte_page(pte);
7371e8f889bSDavid Gibson 
7381e8f889bSDavid Gibson 	/* If no-one else is actually using this page, avoid the copy
7391e8f889bSDavid Gibson 	 * and just make the page writable */
7401e8f889bSDavid Gibson 	avoidcopy = (page_count(old_page) == 1);
7411e8f889bSDavid Gibson 	if (avoidcopy) {
7421e8f889bSDavid Gibson 		set_huge_ptep_writable(vma, address, ptep);
74383c54070SNick Piggin 		return 0;
7441e8f889bSDavid Gibson 	}
7451e8f889bSDavid Gibson 
7461e8f889bSDavid Gibson 	page_cache_get(old_page);
7475da7ca86SChristoph Lameter 	new_page = alloc_huge_page(vma, address);
7481e8f889bSDavid Gibson 
7492fc39cecSAdam Litke 	if (IS_ERR(new_page)) {
7501e8f889bSDavid Gibson 		page_cache_release(old_page);
7512fc39cecSAdam Litke 		return -PTR_ERR(new_page);
7521e8f889bSDavid Gibson 	}
7531e8f889bSDavid Gibson 
7541e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
7559de455b2SAtsushi Nemoto 	copy_huge_page(new_page, old_page, address, vma);
7561e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
7571e8f889bSDavid Gibson 
7581e8f889bSDavid Gibson 	ptep = huge_pte_offset(mm, address & HPAGE_MASK);
7591e8f889bSDavid Gibson 	if (likely(pte_same(*ptep, pte))) {
7601e8f889bSDavid Gibson 		/* Break COW */
7611e8f889bSDavid Gibson 		set_huge_pte_at(mm, address, ptep,
7621e8f889bSDavid Gibson 				make_huge_pte(vma, new_page, 1));
7631e8f889bSDavid Gibson 		/* Make the old page be freed below */
7641e8f889bSDavid Gibson 		new_page = old_page;
7651e8f889bSDavid Gibson 	}
7661e8f889bSDavid Gibson 	page_cache_release(new_page);
7671e8f889bSDavid Gibson 	page_cache_release(old_page);
76883c54070SNick Piggin 	return 0;
7691e8f889bSDavid Gibson }
7701e8f889bSDavid Gibson 
771a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
7721e8f889bSDavid Gibson 			unsigned long address, pte_t *ptep, int write_access)
773ac9b9c66SHugh Dickins {
774ac9b9c66SHugh Dickins 	int ret = VM_FAULT_SIGBUS;
7754c887265SAdam Litke 	unsigned long idx;
7764c887265SAdam Litke 	unsigned long size;
7774c887265SAdam Litke 	struct page *page;
7784c887265SAdam Litke 	struct address_space *mapping;
7791e8f889bSDavid Gibson 	pte_t new_pte;
7804c887265SAdam Litke 
7814c887265SAdam Litke 	mapping = vma->vm_file->f_mapping;
7824c887265SAdam Litke 	idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
7834c887265SAdam Litke 		+ (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
7844c887265SAdam Litke 
7854c887265SAdam Litke 	/*
7864c887265SAdam Litke 	 * Use page lock to guard against racing truncation
7874c887265SAdam Litke 	 * before we get page_table_lock.
7884c887265SAdam Litke 	 */
7896bda666aSChristoph Lameter retry:
7906bda666aSChristoph Lameter 	page = find_lock_page(mapping, idx);
7916bda666aSChristoph Lameter 	if (!page) {
792ebed4bfcSHugh Dickins 		size = i_size_read(mapping->host) >> HPAGE_SHIFT;
793ebed4bfcSHugh Dickins 		if (idx >= size)
794ebed4bfcSHugh Dickins 			goto out;
7956bda666aSChristoph Lameter 		page = alloc_huge_page(vma, address);
7962fc39cecSAdam Litke 		if (IS_ERR(page)) {
7972fc39cecSAdam Litke 			ret = -PTR_ERR(page);
7986bda666aSChristoph Lameter 			goto out;
7996bda666aSChristoph Lameter 		}
80079ac6ba4SDavid Gibson 		clear_huge_page(page, address);
801ac9b9c66SHugh Dickins 
8026bda666aSChristoph Lameter 		if (vma->vm_flags & VM_SHARED) {
8036bda666aSChristoph Lameter 			int err;
8046bda666aSChristoph Lameter 
8056bda666aSChristoph Lameter 			err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
8066bda666aSChristoph Lameter 			if (err) {
8076bda666aSChristoph Lameter 				put_page(page);
8086bda666aSChristoph Lameter 				if (err == -EEXIST)
8096bda666aSChristoph Lameter 					goto retry;
8106bda666aSChristoph Lameter 				goto out;
8116bda666aSChristoph Lameter 			}
8126bda666aSChristoph Lameter 		} else
8136bda666aSChristoph Lameter 			lock_page(page);
8146bda666aSChristoph Lameter 	}
8151e8f889bSDavid Gibson 
816ac9b9c66SHugh Dickins 	spin_lock(&mm->page_table_lock);
8174c887265SAdam Litke 	size = i_size_read(mapping->host) >> HPAGE_SHIFT;
8184c887265SAdam Litke 	if (idx >= size)
8194c887265SAdam Litke 		goto backout;
8204c887265SAdam Litke 
82183c54070SNick Piggin 	ret = 0;
82286e5216fSAdam Litke 	if (!pte_none(*ptep))
8234c887265SAdam Litke 		goto backout;
8244c887265SAdam Litke 
8251e8f889bSDavid Gibson 	new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
8261e8f889bSDavid Gibson 				&& (vma->vm_flags & VM_SHARED)));
8271e8f889bSDavid Gibson 	set_huge_pte_at(mm, address, ptep, new_pte);
8281e8f889bSDavid Gibson 
8291e8f889bSDavid Gibson 	if (write_access && !(vma->vm_flags & VM_SHARED)) {
8301e8f889bSDavid Gibson 		/* Optimization, do the COW without a second fault */
8311e8f889bSDavid Gibson 		ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
8321e8f889bSDavid Gibson 	}
8331e8f889bSDavid Gibson 
834ac9b9c66SHugh Dickins 	spin_unlock(&mm->page_table_lock);
8354c887265SAdam Litke 	unlock_page(page);
8364c887265SAdam Litke out:
837ac9b9c66SHugh Dickins 	return ret;
8384c887265SAdam Litke 
8394c887265SAdam Litke backout:
8404c887265SAdam Litke 	spin_unlock(&mm->page_table_lock);
8414c887265SAdam Litke 	unlock_page(page);
8424c887265SAdam Litke 	put_page(page);
8434c887265SAdam Litke 	goto out;
844ac9b9c66SHugh Dickins }
845ac9b9c66SHugh Dickins 
84686e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
84786e5216fSAdam Litke 			unsigned long address, int write_access)
84886e5216fSAdam Litke {
84986e5216fSAdam Litke 	pte_t *ptep;
85086e5216fSAdam Litke 	pte_t entry;
8511e8f889bSDavid Gibson 	int ret;
8523935baa9SDavid Gibson 	static DEFINE_MUTEX(hugetlb_instantiation_mutex);
85386e5216fSAdam Litke 
85486e5216fSAdam Litke 	ptep = huge_pte_alloc(mm, address);
85586e5216fSAdam Litke 	if (!ptep)
85686e5216fSAdam Litke 		return VM_FAULT_OOM;
85786e5216fSAdam Litke 
8583935baa9SDavid Gibson 	/*
8593935baa9SDavid Gibson 	 * Serialize hugepage allocation and instantiation, so that we don't
8603935baa9SDavid Gibson 	 * get spurious allocation failures if two CPUs race to instantiate
8613935baa9SDavid Gibson 	 * the same page in the page cache.
8623935baa9SDavid Gibson 	 */
8633935baa9SDavid Gibson 	mutex_lock(&hugetlb_instantiation_mutex);
86486e5216fSAdam Litke 	entry = *ptep;
8653935baa9SDavid Gibson 	if (pte_none(entry)) {
8663935baa9SDavid Gibson 		ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
8673935baa9SDavid Gibson 		mutex_unlock(&hugetlb_instantiation_mutex);
8683935baa9SDavid Gibson 		return ret;
8693935baa9SDavid Gibson 	}
87086e5216fSAdam Litke 
87183c54070SNick Piggin 	ret = 0;
8721e8f889bSDavid Gibson 
8731e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
8741e8f889bSDavid Gibson 	/* Check for a racing update before calling hugetlb_cow */
8751e8f889bSDavid Gibson 	if (likely(pte_same(entry, *ptep)))
8761e8f889bSDavid Gibson 		if (write_access && !pte_write(entry))
8771e8f889bSDavid Gibson 			ret = hugetlb_cow(mm, vma, address, ptep, entry);
8781e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
8793935baa9SDavid Gibson 	mutex_unlock(&hugetlb_instantiation_mutex);
8801e8f889bSDavid Gibson 
8811e8f889bSDavid Gibson 	return ret;
88286e5216fSAdam Litke }
88386e5216fSAdam Litke 
88463551ae0SDavid Gibson int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
88563551ae0SDavid Gibson 			struct page **pages, struct vm_area_struct **vmas,
8865b23dbe8SAdam Litke 			unsigned long *position, int *length, int i,
8875b23dbe8SAdam Litke 			int write)
88863551ae0SDavid Gibson {
889d5d4b0aaSChen, Kenneth W 	unsigned long pfn_offset;
890d5d4b0aaSChen, Kenneth W 	unsigned long vaddr = *position;
89163551ae0SDavid Gibson 	int remainder = *length;
89263551ae0SDavid Gibson 
8931c59827dSHugh Dickins 	spin_lock(&mm->page_table_lock);
89463551ae0SDavid Gibson 	while (vaddr < vma->vm_end && remainder) {
89563551ae0SDavid Gibson 		pte_t *pte;
89663551ae0SDavid Gibson 		struct page *page;
89763551ae0SDavid Gibson 
8984c887265SAdam Litke 		/*
8994c887265SAdam Litke 		 * Some archs (sparc64, sh*) have multiple pte_ts to
9004c887265SAdam Litke 		 * each hugepage.  We have to make * sure we get the
9014c887265SAdam Litke 		 * first, for the page indexing below to work.
9024c887265SAdam Litke 		 */
90363551ae0SDavid Gibson 		pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
90463551ae0SDavid Gibson 
9051c59827dSHugh Dickins 		if (!pte || pte_none(*pte)) {
9064c887265SAdam Litke 			int ret;
9074c887265SAdam Litke 
9084c887265SAdam Litke 			spin_unlock(&mm->page_table_lock);
9095b23dbe8SAdam Litke 			ret = hugetlb_fault(mm, vma, vaddr, write);
9104c887265SAdam Litke 			spin_lock(&mm->page_table_lock);
911a89182c7SAdam Litke 			if (!(ret & VM_FAULT_ERROR))
9124c887265SAdam Litke 				continue;
9134c887265SAdam Litke 
9141c59827dSHugh Dickins 			remainder = 0;
9151c59827dSHugh Dickins 			if (!i)
9161c59827dSHugh Dickins 				i = -EFAULT;
9171c59827dSHugh Dickins 			break;
9181c59827dSHugh Dickins 		}
91963551ae0SDavid Gibson 
920d5d4b0aaSChen, Kenneth W 		pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
921d5d4b0aaSChen, Kenneth W 		page = pte_page(*pte);
922d5d4b0aaSChen, Kenneth W same_page:
923d6692183SChen, Kenneth W 		if (pages) {
92463551ae0SDavid Gibson 			get_page(page);
925d5d4b0aaSChen, Kenneth W 			pages[i] = page + pfn_offset;
926d6692183SChen, Kenneth W 		}
92763551ae0SDavid Gibson 
92863551ae0SDavid Gibson 		if (vmas)
92963551ae0SDavid Gibson 			vmas[i] = vma;
93063551ae0SDavid Gibson 
93163551ae0SDavid Gibson 		vaddr += PAGE_SIZE;
932d5d4b0aaSChen, Kenneth W 		++pfn_offset;
93363551ae0SDavid Gibson 		--remainder;
93463551ae0SDavid Gibson 		++i;
935d5d4b0aaSChen, Kenneth W 		if (vaddr < vma->vm_end && remainder &&
936d5d4b0aaSChen, Kenneth W 				pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
937d5d4b0aaSChen, Kenneth W 			/*
938d5d4b0aaSChen, Kenneth W 			 * We use pfn_offset to avoid touching the pageframes
939d5d4b0aaSChen, Kenneth W 			 * of this compound page.
940d5d4b0aaSChen, Kenneth W 			 */
941d5d4b0aaSChen, Kenneth W 			goto same_page;
942d5d4b0aaSChen, Kenneth W 		}
94363551ae0SDavid Gibson 	}
9441c59827dSHugh Dickins 	spin_unlock(&mm->page_table_lock);
94563551ae0SDavid Gibson 	*length = remainder;
94663551ae0SDavid Gibson 	*position = vaddr;
94763551ae0SDavid Gibson 
94863551ae0SDavid Gibson 	return i;
94963551ae0SDavid Gibson }
9508f860591SZhang, Yanmin 
9518f860591SZhang, Yanmin void hugetlb_change_protection(struct vm_area_struct *vma,
9528f860591SZhang, Yanmin 		unsigned long address, unsigned long end, pgprot_t newprot)
9538f860591SZhang, Yanmin {
9548f860591SZhang, Yanmin 	struct mm_struct *mm = vma->vm_mm;
9558f860591SZhang, Yanmin 	unsigned long start = address;
9568f860591SZhang, Yanmin 	pte_t *ptep;
9578f860591SZhang, Yanmin 	pte_t pte;
9588f860591SZhang, Yanmin 
9598f860591SZhang, Yanmin 	BUG_ON(address >= end);
9608f860591SZhang, Yanmin 	flush_cache_range(vma, address, end);
9618f860591SZhang, Yanmin 
96239dde65cSChen, Kenneth W 	spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
9638f860591SZhang, Yanmin 	spin_lock(&mm->page_table_lock);
9648f860591SZhang, Yanmin 	for (; address < end; address += HPAGE_SIZE) {
9658f860591SZhang, Yanmin 		ptep = huge_pte_offset(mm, address);
9668f860591SZhang, Yanmin 		if (!ptep)
9678f860591SZhang, Yanmin 			continue;
96839dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
96939dde65cSChen, Kenneth W 			continue;
9708f860591SZhang, Yanmin 		if (!pte_none(*ptep)) {
9718f860591SZhang, Yanmin 			pte = huge_ptep_get_and_clear(mm, address, ptep);
9728f860591SZhang, Yanmin 			pte = pte_mkhuge(pte_modify(pte, newprot));
9738f860591SZhang, Yanmin 			set_huge_pte_at(mm, address, ptep, pte);
9748f860591SZhang, Yanmin 		}
9758f860591SZhang, Yanmin 	}
9768f860591SZhang, Yanmin 	spin_unlock(&mm->page_table_lock);
97739dde65cSChen, Kenneth W 	spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
9788f860591SZhang, Yanmin 
9798f860591SZhang, Yanmin 	flush_tlb_range(vma, start, end);
9808f860591SZhang, Yanmin }
9818f860591SZhang, Yanmin 
982a43a8c39SChen, Kenneth W struct file_region {
983a43a8c39SChen, Kenneth W 	struct list_head link;
984a43a8c39SChen, Kenneth W 	long from;
985a43a8c39SChen, Kenneth W 	long to;
986a43a8c39SChen, Kenneth W };
987a43a8c39SChen, Kenneth W 
988a43a8c39SChen, Kenneth W static long region_add(struct list_head *head, long f, long t)
989a43a8c39SChen, Kenneth W {
990a43a8c39SChen, Kenneth W 	struct file_region *rg, *nrg, *trg;
991a43a8c39SChen, Kenneth W 
992a43a8c39SChen, Kenneth W 	/* Locate the region we are either in or before. */
993a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, head, link)
994a43a8c39SChen, Kenneth W 		if (f <= rg->to)
995a43a8c39SChen, Kenneth W 			break;
996a43a8c39SChen, Kenneth W 
997a43a8c39SChen, Kenneth W 	/* Round our left edge to the current segment if it encloses us. */
998a43a8c39SChen, Kenneth W 	if (f > rg->from)
999a43a8c39SChen, Kenneth W 		f = rg->from;
1000a43a8c39SChen, Kenneth W 
1001a43a8c39SChen, Kenneth W 	/* Check for and consume any regions we now overlap with. */
1002a43a8c39SChen, Kenneth W 	nrg = rg;
1003a43a8c39SChen, Kenneth W 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
1004a43a8c39SChen, Kenneth W 		if (&rg->link == head)
1005a43a8c39SChen, Kenneth W 			break;
1006a43a8c39SChen, Kenneth W 		if (rg->from > t)
1007a43a8c39SChen, Kenneth W 			break;
1008a43a8c39SChen, Kenneth W 
1009a43a8c39SChen, Kenneth W 		/* If this area reaches higher then extend our area to
1010a43a8c39SChen, Kenneth W 		 * include it completely.  If this is not the first area
1011a43a8c39SChen, Kenneth W 		 * which we intend to reuse, free it. */
1012a43a8c39SChen, Kenneth W 		if (rg->to > t)
1013a43a8c39SChen, Kenneth W 			t = rg->to;
1014a43a8c39SChen, Kenneth W 		if (rg != nrg) {
1015a43a8c39SChen, Kenneth W 			list_del(&rg->link);
1016a43a8c39SChen, Kenneth W 			kfree(rg);
1017a43a8c39SChen, Kenneth W 		}
1018a43a8c39SChen, Kenneth W 	}
1019a43a8c39SChen, Kenneth W 	nrg->from = f;
1020a43a8c39SChen, Kenneth W 	nrg->to = t;
1021a43a8c39SChen, Kenneth W 	return 0;
1022a43a8c39SChen, Kenneth W }
1023a43a8c39SChen, Kenneth W 
1024a43a8c39SChen, Kenneth W static long region_chg(struct list_head *head, long f, long t)
1025a43a8c39SChen, Kenneth W {
1026a43a8c39SChen, Kenneth W 	struct file_region *rg, *nrg;
1027a43a8c39SChen, Kenneth W 	long chg = 0;
1028a43a8c39SChen, Kenneth W 
1029a43a8c39SChen, Kenneth W 	/* Locate the region we are before or in. */
1030a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, head, link)
1031a43a8c39SChen, Kenneth W 		if (f <= rg->to)
1032a43a8c39SChen, Kenneth W 			break;
1033a43a8c39SChen, Kenneth W 
1034a43a8c39SChen, Kenneth W 	/* If we are below the current region then a new region is required.
1035a43a8c39SChen, Kenneth W 	 * Subtle, allocate a new region at the position but make it zero
1036183ff22bSSimon Arlott 	 * size such that we can guarantee to record the reservation. */
1037a43a8c39SChen, Kenneth W 	if (&rg->link == head || t < rg->from) {
1038a43a8c39SChen, Kenneth W 		nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
1039c80544dcSStephen Hemminger 		if (!nrg)
1040a43a8c39SChen, Kenneth W 			return -ENOMEM;
1041a43a8c39SChen, Kenneth W 		nrg->from = f;
1042a43a8c39SChen, Kenneth W 		nrg->to   = f;
1043a43a8c39SChen, Kenneth W 		INIT_LIST_HEAD(&nrg->link);
1044a43a8c39SChen, Kenneth W 		list_add(&nrg->link, rg->link.prev);
1045a43a8c39SChen, Kenneth W 
1046a43a8c39SChen, Kenneth W 		return t - f;
1047a43a8c39SChen, Kenneth W 	}
1048a43a8c39SChen, Kenneth W 
1049a43a8c39SChen, Kenneth W 	/* Round our left edge to the current segment if it encloses us. */
1050a43a8c39SChen, Kenneth W 	if (f > rg->from)
1051a43a8c39SChen, Kenneth W 		f = rg->from;
1052a43a8c39SChen, Kenneth W 	chg = t - f;
1053a43a8c39SChen, Kenneth W 
1054a43a8c39SChen, Kenneth W 	/* Check for and consume any regions we now overlap with. */
1055a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, rg->link.prev, link) {
1056a43a8c39SChen, Kenneth W 		if (&rg->link == head)
1057a43a8c39SChen, Kenneth W 			break;
1058a43a8c39SChen, Kenneth W 		if (rg->from > t)
1059a43a8c39SChen, Kenneth W 			return chg;
1060a43a8c39SChen, Kenneth W 
1061a43a8c39SChen, Kenneth W 		/* We overlap with this area, if it extends futher than
1062a43a8c39SChen, Kenneth W 		 * us then we must extend ourselves.  Account for its
1063a43a8c39SChen, Kenneth W 		 * existing reservation. */
1064a43a8c39SChen, Kenneth W 		if (rg->to > t) {
1065a43a8c39SChen, Kenneth W 			chg += rg->to - t;
1066a43a8c39SChen, Kenneth W 			t = rg->to;
1067a43a8c39SChen, Kenneth W 		}
1068a43a8c39SChen, Kenneth W 		chg -= rg->to - rg->from;
1069a43a8c39SChen, Kenneth W 	}
1070a43a8c39SChen, Kenneth W 	return chg;
1071a43a8c39SChen, Kenneth W }
1072a43a8c39SChen, Kenneth W 
1073a43a8c39SChen, Kenneth W static long region_truncate(struct list_head *head, long end)
1074a43a8c39SChen, Kenneth W {
1075a43a8c39SChen, Kenneth W 	struct file_region *rg, *trg;
1076a43a8c39SChen, Kenneth W 	long chg = 0;
1077a43a8c39SChen, Kenneth W 
1078a43a8c39SChen, Kenneth W 	/* Locate the region we are either in or before. */
1079a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, head, link)
1080a43a8c39SChen, Kenneth W 		if (end <= rg->to)
1081a43a8c39SChen, Kenneth W 			break;
1082a43a8c39SChen, Kenneth W 	if (&rg->link == head)
1083a43a8c39SChen, Kenneth W 		return 0;
1084a43a8c39SChen, Kenneth W 
1085a43a8c39SChen, Kenneth W 	/* If we are in the middle of a region then adjust it. */
1086a43a8c39SChen, Kenneth W 	if (end > rg->from) {
1087a43a8c39SChen, Kenneth W 		chg = rg->to - end;
1088a43a8c39SChen, Kenneth W 		rg->to = end;
1089a43a8c39SChen, Kenneth W 		rg = list_entry(rg->link.next, typeof(*rg), link);
1090a43a8c39SChen, Kenneth W 	}
1091a43a8c39SChen, Kenneth W 
1092a43a8c39SChen, Kenneth W 	/* Drop any remaining regions. */
1093a43a8c39SChen, Kenneth W 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
1094a43a8c39SChen, Kenneth W 		if (&rg->link == head)
1095a43a8c39SChen, Kenneth W 			break;
1096a43a8c39SChen, Kenneth W 		chg += rg->to - rg->from;
1097a43a8c39SChen, Kenneth W 		list_del(&rg->link);
1098a43a8c39SChen, Kenneth W 		kfree(rg);
1099a43a8c39SChen, Kenneth W 	}
1100a43a8c39SChen, Kenneth W 	return chg;
1101a43a8c39SChen, Kenneth W }
1102a43a8c39SChen, Kenneth W 
1103a43a8c39SChen, Kenneth W static int hugetlb_acct_memory(long delta)
1104a43a8c39SChen, Kenneth W {
1105a43a8c39SChen, Kenneth W 	int ret = -ENOMEM;
1106a43a8c39SChen, Kenneth W 
1107a43a8c39SChen, Kenneth W 	spin_lock(&hugetlb_lock);
11088a630112SKen Chen 	/*
11098a630112SKen Chen 	 * When cpuset is configured, it breaks the strict hugetlb page
11108a630112SKen Chen 	 * reservation as the accounting is done on a global variable. Such
11118a630112SKen Chen 	 * reservation is completely rubbish in the presence of cpuset because
11128a630112SKen Chen 	 * the reservation is not checked against page availability for the
11138a630112SKen Chen 	 * current cpuset. Application can still potentially OOM'ed by kernel
11148a630112SKen Chen 	 * with lack of free htlb page in cpuset that the task is in.
11158a630112SKen Chen 	 * Attempt to enforce strict accounting with cpuset is almost
11168a630112SKen Chen 	 * impossible (or too ugly) because cpuset is too fluid that
11178a630112SKen Chen 	 * task or memory node can be dynamically moved between cpusets.
11188a630112SKen Chen 	 *
11198a630112SKen Chen 	 * The change of semantics for shared hugetlb mapping with cpuset is
11208a630112SKen Chen 	 * undesirable. However, in order to preserve some of the semantics,
11218a630112SKen Chen 	 * we fall back to check against current free page availability as
11228a630112SKen Chen 	 * a best attempt and hopefully to minimize the impact of changing
11238a630112SKen Chen 	 * semantics that cpuset has.
11248a630112SKen Chen 	 */
1125e4e574b7SAdam Litke 	if (delta > 0) {
1126e4e574b7SAdam Litke 		if (gather_surplus_pages(delta) < 0)
1127e4e574b7SAdam Litke 			goto out;
1128e4e574b7SAdam Litke 
1129e4e574b7SAdam Litke 		if (delta > cpuset_mems_nr(free_huge_pages_node))
1130e4e574b7SAdam Litke 			goto out;
1131e4e574b7SAdam Litke 	}
1132e4e574b7SAdam Litke 
1133e4e574b7SAdam Litke 	ret = 0;
1134e4e574b7SAdam Litke 	resv_huge_pages += delta;
1135e4e574b7SAdam Litke 	if (delta < 0)
1136e4e574b7SAdam Litke 		return_unused_surplus_pages((unsigned long) -delta);
1137e4e574b7SAdam Litke 
1138e4e574b7SAdam Litke out:
1139e4e574b7SAdam Litke 	spin_unlock(&hugetlb_lock);
1140e4e574b7SAdam Litke 	return ret;
1141e4e574b7SAdam Litke }
1142e4e574b7SAdam Litke 
1143e4e574b7SAdam Litke int hugetlb_reserve_pages(struct inode *inode, long from, long to)
1144e4e574b7SAdam Litke {
1145e4e574b7SAdam Litke 	long ret, chg;
1146e4e574b7SAdam Litke 
1147e4e574b7SAdam Litke 	chg = region_chg(&inode->i_mapping->private_list, from, to);
1148e4e574b7SAdam Litke 	if (chg < 0)
1149e4e574b7SAdam Litke 		return chg;
11508a630112SKen Chen 
115190d8b7e6SAdam Litke 	if (hugetlb_get_quota(inode->i_mapping, chg))
115290d8b7e6SAdam Litke 		return -ENOSPC;
1153a43a8c39SChen, Kenneth W 	ret = hugetlb_acct_memory(chg);
1154a43a8c39SChen, Kenneth W 	if (ret < 0)
1155a43a8c39SChen, Kenneth W 		return ret;
1156a43a8c39SChen, Kenneth W 	region_add(&inode->i_mapping->private_list, from, to);
1157a43a8c39SChen, Kenneth W 	return 0;
1158a43a8c39SChen, Kenneth W }
1159a43a8c39SChen, Kenneth W 
1160a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1161a43a8c39SChen, Kenneth W {
1162a43a8c39SChen, Kenneth W 	long chg = region_truncate(&inode->i_mapping->private_list, offset);
116390d8b7e6SAdam Litke 	hugetlb_put_quota(inode->i_mapping, (chg - freed));
116490d8b7e6SAdam Litke 	hugetlb_acct_memory(-(chg - freed));
1165a43a8c39SChen, Kenneth W }
1166