xref: /openbmc/linux/mm/hugetlb.c (revision 96822904)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Generic hugetlb support.
31da177e4SLinus Torvalds  * (C) William Irwin, April 2004
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds #include <linux/gfp.h>
61da177e4SLinus Torvalds #include <linux/list.h>
71da177e4SLinus Torvalds #include <linux/init.h>
81da177e4SLinus Torvalds #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/mm.h>
101da177e4SLinus Torvalds #include <linux/sysctl.h>
111da177e4SLinus Torvalds #include <linux/highmem.h>
121da177e4SLinus Torvalds #include <linux/nodemask.h>
1363551ae0SDavid Gibson #include <linux/pagemap.h>
145da7ca86SChristoph Lameter #include <linux/mempolicy.h>
15aea47ff3SChristoph Lameter #include <linux/cpuset.h>
163935baa9SDavid Gibson #include <linux/mutex.h>
175da7ca86SChristoph Lameter 
1863551ae0SDavid Gibson #include <asm/page.h>
1963551ae0SDavid Gibson #include <asm/pgtable.h>
2063551ae0SDavid Gibson 
2163551ae0SDavid Gibson #include <linux/hugetlb.h>
227835e98bSNick Piggin #include "internal.h"
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
25a43a8c39SChen, Kenneth W static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages;
267893d1d5SAdam Litke static unsigned long surplus_huge_pages;
27064d9efeSNishanth Aravamudan static unsigned long nr_overcommit_huge_pages;
281da177e4SLinus Torvalds unsigned long max_huge_pages;
29064d9efeSNishanth Aravamudan unsigned long sysctl_overcommit_huge_pages;
301da177e4SLinus Torvalds static struct list_head hugepage_freelists[MAX_NUMNODES];
311da177e4SLinus Torvalds static unsigned int nr_huge_pages_node[MAX_NUMNODES];
321da177e4SLinus Torvalds static unsigned int free_huge_pages_node[MAX_NUMNODES];
337893d1d5SAdam Litke static unsigned int surplus_huge_pages_node[MAX_NUMNODES];
34396faf03SMel Gorman static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
35396faf03SMel Gorman unsigned long hugepages_treat_as_movable;
3663b4613cSNishanth Aravamudan static int hugetlb_next_nid;
37396faf03SMel Gorman 
383935baa9SDavid Gibson /*
393935baa9SDavid Gibson  * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
403935baa9SDavid Gibson  */
413935baa9SDavid Gibson static DEFINE_SPINLOCK(hugetlb_lock);
420bd0f9fbSEric Paris 
43e7c4b0bfSAndy Whitcroft /*
4496822904SAndy Whitcroft  * Region tracking -- allows tracking of reservations and instantiated pages
4596822904SAndy Whitcroft  *                    across the pages in a mapping.
4696822904SAndy Whitcroft  */
4796822904SAndy Whitcroft struct file_region {
4896822904SAndy Whitcroft 	struct list_head link;
4996822904SAndy Whitcroft 	long from;
5096822904SAndy Whitcroft 	long to;
5196822904SAndy Whitcroft };
5296822904SAndy Whitcroft 
5396822904SAndy Whitcroft static long region_add(struct list_head *head, long f, long t)
5496822904SAndy Whitcroft {
5596822904SAndy Whitcroft 	struct file_region *rg, *nrg, *trg;
5696822904SAndy Whitcroft 
5796822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
5896822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
5996822904SAndy Whitcroft 		if (f <= rg->to)
6096822904SAndy Whitcroft 			break;
6196822904SAndy Whitcroft 
6296822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
6396822904SAndy Whitcroft 	if (f > rg->from)
6496822904SAndy Whitcroft 		f = rg->from;
6596822904SAndy Whitcroft 
6696822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
6796822904SAndy Whitcroft 	nrg = rg;
6896822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
6996822904SAndy Whitcroft 		if (&rg->link == head)
7096822904SAndy Whitcroft 			break;
7196822904SAndy Whitcroft 		if (rg->from > t)
7296822904SAndy Whitcroft 			break;
7396822904SAndy Whitcroft 
7496822904SAndy Whitcroft 		/* If this area reaches higher then extend our area to
7596822904SAndy Whitcroft 		 * include it completely.  If this is not the first area
7696822904SAndy Whitcroft 		 * which we intend to reuse, free it. */
7796822904SAndy Whitcroft 		if (rg->to > t)
7896822904SAndy Whitcroft 			t = rg->to;
7996822904SAndy Whitcroft 		if (rg != nrg) {
8096822904SAndy Whitcroft 			list_del(&rg->link);
8196822904SAndy Whitcroft 			kfree(rg);
8296822904SAndy Whitcroft 		}
8396822904SAndy Whitcroft 	}
8496822904SAndy Whitcroft 	nrg->from = f;
8596822904SAndy Whitcroft 	nrg->to = t;
8696822904SAndy Whitcroft 	return 0;
8796822904SAndy Whitcroft }
8896822904SAndy Whitcroft 
8996822904SAndy Whitcroft static long region_chg(struct list_head *head, long f, long t)
9096822904SAndy Whitcroft {
9196822904SAndy Whitcroft 	struct file_region *rg, *nrg;
9296822904SAndy Whitcroft 	long chg = 0;
9396822904SAndy Whitcroft 
9496822904SAndy Whitcroft 	/* Locate the region we are before or in. */
9596822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
9696822904SAndy Whitcroft 		if (f <= rg->to)
9796822904SAndy Whitcroft 			break;
9896822904SAndy Whitcroft 
9996822904SAndy Whitcroft 	/* If we are below the current region then a new region is required.
10096822904SAndy Whitcroft 	 * Subtle, allocate a new region at the position but make it zero
10196822904SAndy Whitcroft 	 * size such that we can guarantee to record the reservation. */
10296822904SAndy Whitcroft 	if (&rg->link == head || t < rg->from) {
10396822904SAndy Whitcroft 		nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
10496822904SAndy Whitcroft 		if (!nrg)
10596822904SAndy Whitcroft 			return -ENOMEM;
10696822904SAndy Whitcroft 		nrg->from = f;
10796822904SAndy Whitcroft 		nrg->to   = f;
10896822904SAndy Whitcroft 		INIT_LIST_HEAD(&nrg->link);
10996822904SAndy Whitcroft 		list_add(&nrg->link, rg->link.prev);
11096822904SAndy Whitcroft 
11196822904SAndy Whitcroft 		return t - f;
11296822904SAndy Whitcroft 	}
11396822904SAndy Whitcroft 
11496822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
11596822904SAndy Whitcroft 	if (f > rg->from)
11696822904SAndy Whitcroft 		f = rg->from;
11796822904SAndy Whitcroft 	chg = t - f;
11896822904SAndy Whitcroft 
11996822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
12096822904SAndy Whitcroft 	list_for_each_entry(rg, rg->link.prev, link) {
12196822904SAndy Whitcroft 		if (&rg->link == head)
12296822904SAndy Whitcroft 			break;
12396822904SAndy Whitcroft 		if (rg->from > t)
12496822904SAndy Whitcroft 			return chg;
12596822904SAndy Whitcroft 
12696822904SAndy Whitcroft 		/* We overlap with this area, if it extends futher than
12796822904SAndy Whitcroft 		 * us then we must extend ourselves.  Account for its
12896822904SAndy Whitcroft 		 * existing reservation. */
12996822904SAndy Whitcroft 		if (rg->to > t) {
13096822904SAndy Whitcroft 			chg += rg->to - t;
13196822904SAndy Whitcroft 			t = rg->to;
13296822904SAndy Whitcroft 		}
13396822904SAndy Whitcroft 		chg -= rg->to - rg->from;
13496822904SAndy Whitcroft 	}
13596822904SAndy Whitcroft 	return chg;
13696822904SAndy Whitcroft }
13796822904SAndy Whitcroft 
13896822904SAndy Whitcroft static long region_truncate(struct list_head *head, long end)
13996822904SAndy Whitcroft {
14096822904SAndy Whitcroft 	struct file_region *rg, *trg;
14196822904SAndy Whitcroft 	long chg = 0;
14296822904SAndy Whitcroft 
14396822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
14496822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
14596822904SAndy Whitcroft 		if (end <= rg->to)
14696822904SAndy Whitcroft 			break;
14796822904SAndy Whitcroft 	if (&rg->link == head)
14896822904SAndy Whitcroft 		return 0;
14996822904SAndy Whitcroft 
15096822904SAndy Whitcroft 	/* If we are in the middle of a region then adjust it. */
15196822904SAndy Whitcroft 	if (end > rg->from) {
15296822904SAndy Whitcroft 		chg = rg->to - end;
15396822904SAndy Whitcroft 		rg->to = end;
15496822904SAndy Whitcroft 		rg = list_entry(rg->link.next, typeof(*rg), link);
15596822904SAndy Whitcroft 	}
15696822904SAndy Whitcroft 
15796822904SAndy Whitcroft 	/* Drop any remaining regions. */
15896822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
15996822904SAndy Whitcroft 		if (&rg->link == head)
16096822904SAndy Whitcroft 			break;
16196822904SAndy Whitcroft 		chg += rg->to - rg->from;
16296822904SAndy Whitcroft 		list_del(&rg->link);
16396822904SAndy Whitcroft 		kfree(rg);
16496822904SAndy Whitcroft 	}
16596822904SAndy Whitcroft 	return chg;
16696822904SAndy Whitcroft }
16796822904SAndy Whitcroft 
16896822904SAndy Whitcroft /*
169e7c4b0bfSAndy Whitcroft  * Convert the address within this vma to the page offset within
170e7c4b0bfSAndy Whitcroft  * the mapping, in base page units.
171e7c4b0bfSAndy Whitcroft  */
172e7c4b0bfSAndy Whitcroft static pgoff_t vma_page_offset(struct vm_area_struct *vma,
173e7c4b0bfSAndy Whitcroft 				unsigned long address)
174e7c4b0bfSAndy Whitcroft {
175e7c4b0bfSAndy Whitcroft 	return ((address - vma->vm_start) >> PAGE_SHIFT) +
176e7c4b0bfSAndy Whitcroft 					(vma->vm_pgoff >> PAGE_SHIFT);
177e7c4b0bfSAndy Whitcroft }
178e7c4b0bfSAndy Whitcroft 
179e7c4b0bfSAndy Whitcroft /*
180e7c4b0bfSAndy Whitcroft  * Convert the address within this vma to the page offset within
181e7c4b0bfSAndy Whitcroft  * the mapping, in pagecache page units; huge pages here.
182e7c4b0bfSAndy Whitcroft  */
183e7c4b0bfSAndy Whitcroft static pgoff_t vma_pagecache_offset(struct vm_area_struct *vma,
184e7c4b0bfSAndy Whitcroft 					unsigned long address)
185e7c4b0bfSAndy Whitcroft {
186e7c4b0bfSAndy Whitcroft 	return ((address - vma->vm_start) >> HPAGE_SHIFT) +
187e7c4b0bfSAndy Whitcroft 			(vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
188e7c4b0bfSAndy Whitcroft }
189e7c4b0bfSAndy Whitcroft 
19004f2cbe3SMel Gorman #define HPAGE_RESV_OWNER    (1UL << (BITS_PER_LONG - 1))
19104f2cbe3SMel Gorman #define HPAGE_RESV_UNMAPPED (1UL << (BITS_PER_LONG - 2))
19204f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
193a1e78772SMel Gorman /*
194a1e78772SMel Gorman  * These helpers are used to track how many pages are reserved for
195a1e78772SMel Gorman  * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
196a1e78772SMel Gorman  * is guaranteed to have their future faults succeed.
197a1e78772SMel Gorman  *
198a1e78772SMel Gorman  * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
199a1e78772SMel Gorman  * the reserve counters are updated with the hugetlb_lock held. It is safe
200a1e78772SMel Gorman  * to reset the VMA at fork() time as it is not in use yet and there is no
201a1e78772SMel Gorman  * chance of the global counters getting corrupted as a result of the values.
202a1e78772SMel Gorman  */
203e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma)
204e7c4b0bfSAndy Whitcroft {
205e7c4b0bfSAndy Whitcroft 	return (unsigned long)vma->vm_private_data;
206e7c4b0bfSAndy Whitcroft }
207e7c4b0bfSAndy Whitcroft 
208e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma,
209e7c4b0bfSAndy Whitcroft 							unsigned long value)
210e7c4b0bfSAndy Whitcroft {
211e7c4b0bfSAndy Whitcroft 	vma->vm_private_data = (void *)value;
212e7c4b0bfSAndy Whitcroft }
213e7c4b0bfSAndy Whitcroft 
214a1e78772SMel Gorman static unsigned long vma_resv_huge_pages(struct vm_area_struct *vma)
215a1e78772SMel Gorman {
216a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
217a1e78772SMel Gorman 	if (!(vma->vm_flags & VM_SHARED))
218e7c4b0bfSAndy Whitcroft 		return get_vma_private_data(vma) & ~HPAGE_RESV_MASK;
219a1e78772SMel Gorman 	return 0;
220a1e78772SMel Gorman }
221a1e78772SMel Gorman 
222a1e78772SMel Gorman static void set_vma_resv_huge_pages(struct vm_area_struct *vma,
223a1e78772SMel Gorman 							unsigned long reserve)
224a1e78772SMel Gorman {
225a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
226a1e78772SMel Gorman 	VM_BUG_ON(vma->vm_flags & VM_SHARED);
227a1e78772SMel Gorman 
228e7c4b0bfSAndy Whitcroft 	set_vma_private_data(vma,
229e7c4b0bfSAndy Whitcroft 		(get_vma_private_data(vma) & HPAGE_RESV_MASK) | reserve);
23004f2cbe3SMel Gorman }
23104f2cbe3SMel Gorman 
23204f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
23304f2cbe3SMel Gorman {
23404f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
235e7c4b0bfSAndy Whitcroft 	VM_BUG_ON(vma->vm_flags & VM_SHARED);
236e7c4b0bfSAndy Whitcroft 
237e7c4b0bfSAndy Whitcroft 	set_vma_private_data(vma, get_vma_private_data(vma) | flags);
23804f2cbe3SMel Gorman }
23904f2cbe3SMel Gorman 
24004f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
24104f2cbe3SMel Gorman {
24204f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
243e7c4b0bfSAndy Whitcroft 
244e7c4b0bfSAndy Whitcroft 	return (get_vma_private_data(vma) & flag) != 0;
245a1e78772SMel Gorman }
246a1e78772SMel Gorman 
247a1e78772SMel Gorman /* Decrement the reserved pages in the hugepage pool by one */
248a1e78772SMel Gorman static void decrement_hugepage_resv_vma(struct vm_area_struct *vma)
249a1e78772SMel Gorman {
250a1e78772SMel Gorman 	if (vma->vm_flags & VM_SHARED) {
251a1e78772SMel Gorman 		/* Shared mappings always use reserves */
252a1e78772SMel Gorman 		resv_huge_pages--;
253a1e78772SMel Gorman 	} else {
254a1e78772SMel Gorman 		/*
255a1e78772SMel Gorman 		 * Only the process that called mmap() has reserves for
256a1e78772SMel Gorman 		 * private mappings.
257a1e78772SMel Gorman 		 */
25804f2cbe3SMel Gorman 		if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
25904f2cbe3SMel Gorman 			unsigned long flags, reserve;
260a1e78772SMel Gorman 			resv_huge_pages--;
26104f2cbe3SMel Gorman 			flags = (unsigned long)vma->vm_private_data &
26204f2cbe3SMel Gorman 							HPAGE_RESV_MASK;
263a1e78772SMel Gorman 			reserve = (unsigned long)vma->vm_private_data - 1;
26404f2cbe3SMel Gorman 			vma->vm_private_data = (void *)(reserve | flags);
265a1e78772SMel Gorman 		}
266a1e78772SMel Gorman 	}
267a1e78772SMel Gorman }
268a1e78772SMel Gorman 
26904f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
270a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
271a1e78772SMel Gorman {
272a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
273a1e78772SMel Gorman 	if (!(vma->vm_flags & VM_SHARED))
274a1e78772SMel Gorman 		vma->vm_private_data = (void *)0;
275a1e78772SMel Gorman }
276a1e78772SMel Gorman 
277a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */
278a1e78772SMel Gorman static int vma_has_private_reserves(struct vm_area_struct *vma)
279a1e78772SMel Gorman {
280a1e78772SMel Gorman 	if (vma->vm_flags & VM_SHARED)
281a1e78772SMel Gorman 		return 0;
282a1e78772SMel Gorman 	if (!vma_resv_huge_pages(vma))
283a1e78772SMel Gorman 		return 0;
284a1e78772SMel Gorman 	return 1;
285a1e78772SMel Gorman }
286a1e78772SMel Gorman 
28779ac6ba4SDavid Gibson static void clear_huge_page(struct page *page, unsigned long addr)
28879ac6ba4SDavid Gibson {
28979ac6ba4SDavid Gibson 	int i;
29079ac6ba4SDavid Gibson 
29179ac6ba4SDavid Gibson 	might_sleep();
29279ac6ba4SDavid Gibson 	for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
29379ac6ba4SDavid Gibson 		cond_resched();
294281e0e3bSRalf Baechle 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
29579ac6ba4SDavid Gibson 	}
29679ac6ba4SDavid Gibson }
29779ac6ba4SDavid Gibson 
29879ac6ba4SDavid Gibson static void copy_huge_page(struct page *dst, struct page *src,
2999de455b2SAtsushi Nemoto 			   unsigned long addr, struct vm_area_struct *vma)
30079ac6ba4SDavid Gibson {
30179ac6ba4SDavid Gibson 	int i;
30279ac6ba4SDavid Gibson 
30379ac6ba4SDavid Gibson 	might_sleep();
30479ac6ba4SDavid Gibson 	for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
30579ac6ba4SDavid Gibson 		cond_resched();
3069de455b2SAtsushi Nemoto 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
30779ac6ba4SDavid Gibson 	}
30879ac6ba4SDavid Gibson }
30979ac6ba4SDavid Gibson 
3101da177e4SLinus Torvalds static void enqueue_huge_page(struct page *page)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	int nid = page_to_nid(page);
3131da177e4SLinus Torvalds 	list_add(&page->lru, &hugepage_freelists[nid]);
3141da177e4SLinus Torvalds 	free_huge_pages++;
3151da177e4SLinus Torvalds 	free_huge_pages_node[nid]++;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
318348e1e04SNishanth Aravamudan static struct page *dequeue_huge_page(void)
319348e1e04SNishanth Aravamudan {
320348e1e04SNishanth Aravamudan 	int nid;
321348e1e04SNishanth Aravamudan 	struct page *page = NULL;
322348e1e04SNishanth Aravamudan 
323348e1e04SNishanth Aravamudan 	for (nid = 0; nid < MAX_NUMNODES; ++nid) {
324348e1e04SNishanth Aravamudan 		if (!list_empty(&hugepage_freelists[nid])) {
325348e1e04SNishanth Aravamudan 			page = list_entry(hugepage_freelists[nid].next,
326348e1e04SNishanth Aravamudan 					  struct page, lru);
327348e1e04SNishanth Aravamudan 			list_del(&page->lru);
328348e1e04SNishanth Aravamudan 			free_huge_pages--;
329348e1e04SNishanth Aravamudan 			free_huge_pages_node[nid]--;
330348e1e04SNishanth Aravamudan 			break;
331348e1e04SNishanth Aravamudan 		}
332348e1e04SNishanth Aravamudan 	}
333348e1e04SNishanth Aravamudan 	return page;
334348e1e04SNishanth Aravamudan }
335348e1e04SNishanth Aravamudan 
336348e1e04SNishanth Aravamudan static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
33704f2cbe3SMel Gorman 				unsigned long address, int avoid_reserve)
3381da177e4SLinus Torvalds {
33931a5c6e4SNishanth Aravamudan 	int nid;
3401da177e4SLinus Torvalds 	struct page *page = NULL;
341480eccf9SLee Schermerhorn 	struct mempolicy *mpol;
34219770b32SMel Gorman 	nodemask_t *nodemask;
343396faf03SMel Gorman 	struct zonelist *zonelist = huge_zonelist(vma, address,
34419770b32SMel Gorman 					htlb_alloc_mask, &mpol, &nodemask);
345dd1a239fSMel Gorman 	struct zone *zone;
346dd1a239fSMel Gorman 	struct zoneref *z;
3471da177e4SLinus Torvalds 
348a1e78772SMel Gorman 	/*
349a1e78772SMel Gorman 	 * A child process with MAP_PRIVATE mappings created by their parent
350a1e78772SMel Gorman 	 * have no page reserves. This check ensures that reservations are
351a1e78772SMel Gorman 	 * not "stolen". The child may still get SIGKILLed
352a1e78772SMel Gorman 	 */
353a1e78772SMel Gorman 	if (!vma_has_private_reserves(vma) &&
354a1e78772SMel Gorman 			free_huge_pages - resv_huge_pages == 0)
355a1e78772SMel Gorman 		return NULL;
356a1e78772SMel Gorman 
35704f2cbe3SMel Gorman 	/* If reserves cannot be used, ensure enough pages are in the pool */
35804f2cbe3SMel Gorman 	if (avoid_reserve && free_huge_pages - resv_huge_pages == 0)
35904f2cbe3SMel Gorman 		return NULL;
36004f2cbe3SMel Gorman 
36119770b32SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
36219770b32SMel Gorman 						MAX_NR_ZONES - 1, nodemask) {
36354a6eb5cSMel Gorman 		nid = zone_to_nid(zone);
36454a6eb5cSMel Gorman 		if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
3653abf7afdSAndrew Morton 		    !list_empty(&hugepage_freelists[nid])) {
3661da177e4SLinus Torvalds 			page = list_entry(hugepage_freelists[nid].next,
3671da177e4SLinus Torvalds 					  struct page, lru);
3681da177e4SLinus Torvalds 			list_del(&page->lru);
3691da177e4SLinus Torvalds 			free_huge_pages--;
3701da177e4SLinus Torvalds 			free_huge_pages_node[nid]--;
37104f2cbe3SMel Gorman 
37204f2cbe3SMel Gorman 			if (!avoid_reserve)
373a1e78772SMel Gorman 				decrement_hugepage_resv_vma(vma);
374a1e78772SMel Gorman 
3755ab3ee7bSKen Chen 			break;
3761da177e4SLinus Torvalds 		}
3773abf7afdSAndrew Morton 	}
37852cd3b07SLee Schermerhorn 	mpol_cond_put(mpol);
3791da177e4SLinus Torvalds 	return page;
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3826af2acb6SAdam Litke static void update_and_free_page(struct page *page)
3836af2acb6SAdam Litke {
3846af2acb6SAdam Litke 	int i;
3856af2acb6SAdam Litke 	nr_huge_pages--;
3866af2acb6SAdam Litke 	nr_huge_pages_node[page_to_nid(page)]--;
3876af2acb6SAdam Litke 	for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
3886af2acb6SAdam Litke 		page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
3896af2acb6SAdam Litke 				1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
3906af2acb6SAdam Litke 				1 << PG_private | 1<< PG_writeback);
3916af2acb6SAdam Litke 	}
3926af2acb6SAdam Litke 	set_compound_page_dtor(page, NULL);
3936af2acb6SAdam Litke 	set_page_refcounted(page);
3947f2e9525SGerald Schaefer 	arch_release_hugepage(page);
3956af2acb6SAdam Litke 	__free_pages(page, HUGETLB_PAGE_ORDER);
3966af2acb6SAdam Litke }
3976af2acb6SAdam Litke 
39827a85ef1SDavid Gibson static void free_huge_page(struct page *page)
39927a85ef1SDavid Gibson {
4007893d1d5SAdam Litke 	int nid = page_to_nid(page);
401c79fb75eSAdam Litke 	struct address_space *mapping;
40227a85ef1SDavid Gibson 
403c79fb75eSAdam Litke 	mapping = (struct address_space *) page_private(page);
404e5df70abSAndy Whitcroft 	set_page_private(page, 0);
4057893d1d5SAdam Litke 	BUG_ON(page_count(page));
40627a85ef1SDavid Gibson 	INIT_LIST_HEAD(&page->lru);
40727a85ef1SDavid Gibson 
40827a85ef1SDavid Gibson 	spin_lock(&hugetlb_lock);
4097893d1d5SAdam Litke 	if (surplus_huge_pages_node[nid]) {
4107893d1d5SAdam Litke 		update_and_free_page(page);
4117893d1d5SAdam Litke 		surplus_huge_pages--;
4127893d1d5SAdam Litke 		surplus_huge_pages_node[nid]--;
4137893d1d5SAdam Litke 	} else {
41427a85ef1SDavid Gibson 		enqueue_huge_page(page);
4157893d1d5SAdam Litke 	}
41627a85ef1SDavid Gibson 	spin_unlock(&hugetlb_lock);
417c79fb75eSAdam Litke 	if (mapping)
4189a119c05SAdam Litke 		hugetlb_put_quota(mapping, 1);
41927a85ef1SDavid Gibson }
42027a85ef1SDavid Gibson 
4217893d1d5SAdam Litke /*
4227893d1d5SAdam Litke  * Increment or decrement surplus_huge_pages.  Keep node-specific counters
4237893d1d5SAdam Litke  * balanced by operating on them in a round-robin fashion.
4247893d1d5SAdam Litke  * Returns 1 if an adjustment was made.
4257893d1d5SAdam Litke  */
4267893d1d5SAdam Litke static int adjust_pool_surplus(int delta)
4277893d1d5SAdam Litke {
4287893d1d5SAdam Litke 	static int prev_nid;
4297893d1d5SAdam Litke 	int nid = prev_nid;
4307893d1d5SAdam Litke 	int ret = 0;
4317893d1d5SAdam Litke 
4327893d1d5SAdam Litke 	VM_BUG_ON(delta != -1 && delta != 1);
4337893d1d5SAdam Litke 	do {
4347893d1d5SAdam Litke 		nid = next_node(nid, node_online_map);
4357893d1d5SAdam Litke 		if (nid == MAX_NUMNODES)
4367893d1d5SAdam Litke 			nid = first_node(node_online_map);
4377893d1d5SAdam Litke 
4387893d1d5SAdam Litke 		/* To shrink on this node, there must be a surplus page */
4397893d1d5SAdam Litke 		if (delta < 0 && !surplus_huge_pages_node[nid])
4407893d1d5SAdam Litke 			continue;
4417893d1d5SAdam Litke 		/* Surplus cannot exceed the total number of pages */
4427893d1d5SAdam Litke 		if (delta > 0 && surplus_huge_pages_node[nid] >=
4437893d1d5SAdam Litke 						nr_huge_pages_node[nid])
4447893d1d5SAdam Litke 			continue;
4457893d1d5SAdam Litke 
4467893d1d5SAdam Litke 		surplus_huge_pages += delta;
4477893d1d5SAdam Litke 		surplus_huge_pages_node[nid] += delta;
4487893d1d5SAdam Litke 		ret = 1;
4497893d1d5SAdam Litke 		break;
4507893d1d5SAdam Litke 	} while (nid != prev_nid);
4517893d1d5SAdam Litke 
4527893d1d5SAdam Litke 	prev_nid = nid;
4537893d1d5SAdam Litke 	return ret;
4547893d1d5SAdam Litke }
4557893d1d5SAdam Litke 
45663b4613cSNishanth Aravamudan static struct page *alloc_fresh_huge_page_node(int nid)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	struct page *page;
459f96efd58SJoe Jin 
46063b4613cSNishanth Aravamudan 	page = alloc_pages_node(nid,
461551883aeSNishanth Aravamudan 		htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
462551883aeSNishanth Aravamudan 						__GFP_REPEAT|__GFP_NOWARN,
463f96efd58SJoe Jin 		HUGETLB_PAGE_ORDER);
4641da177e4SLinus Torvalds 	if (page) {
4657f2e9525SGerald Schaefer 		if (arch_prepare_hugepage(page)) {
4667f2e9525SGerald Schaefer 			__free_pages(page, HUGETLB_PAGE_ORDER);
4677b8ee84dSHarvey Harrison 			return NULL;
4687f2e9525SGerald Schaefer 		}
46933f2ef89SAndy Whitcroft 		set_compound_page_dtor(page, free_huge_page);
4700bd0f9fbSEric Paris 		spin_lock(&hugetlb_lock);
4711da177e4SLinus Torvalds 		nr_huge_pages++;
47263b4613cSNishanth Aravamudan 		nr_huge_pages_node[nid]++;
4730bd0f9fbSEric Paris 		spin_unlock(&hugetlb_lock);
474a482289dSNick Piggin 		put_page(page); /* free it into the hugepage allocator */
4751da177e4SLinus Torvalds 	}
47663b4613cSNishanth Aravamudan 
47763b4613cSNishanth Aravamudan 	return page;
47863b4613cSNishanth Aravamudan }
47963b4613cSNishanth Aravamudan 
48063b4613cSNishanth Aravamudan static int alloc_fresh_huge_page(void)
48163b4613cSNishanth Aravamudan {
48263b4613cSNishanth Aravamudan 	struct page *page;
48363b4613cSNishanth Aravamudan 	int start_nid;
48463b4613cSNishanth Aravamudan 	int next_nid;
48563b4613cSNishanth Aravamudan 	int ret = 0;
48663b4613cSNishanth Aravamudan 
48763b4613cSNishanth Aravamudan 	start_nid = hugetlb_next_nid;
48863b4613cSNishanth Aravamudan 
48963b4613cSNishanth Aravamudan 	do {
49063b4613cSNishanth Aravamudan 		page = alloc_fresh_huge_page_node(hugetlb_next_nid);
49163b4613cSNishanth Aravamudan 		if (page)
49263b4613cSNishanth Aravamudan 			ret = 1;
49363b4613cSNishanth Aravamudan 		/*
49463b4613cSNishanth Aravamudan 		 * Use a helper variable to find the next node and then
49563b4613cSNishanth Aravamudan 		 * copy it back to hugetlb_next_nid afterwards:
49663b4613cSNishanth Aravamudan 		 * otherwise there's a window in which a racer might
49763b4613cSNishanth Aravamudan 		 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
49863b4613cSNishanth Aravamudan 		 * But we don't need to use a spin_lock here: it really
49963b4613cSNishanth Aravamudan 		 * doesn't matter if occasionally a racer chooses the
50063b4613cSNishanth Aravamudan 		 * same nid as we do.  Move nid forward in the mask even
50163b4613cSNishanth Aravamudan 		 * if we just successfully allocated a hugepage so that
50263b4613cSNishanth Aravamudan 		 * the next caller gets hugepages on the next node.
50363b4613cSNishanth Aravamudan 		 */
50463b4613cSNishanth Aravamudan 		next_nid = next_node(hugetlb_next_nid, node_online_map);
50563b4613cSNishanth Aravamudan 		if (next_nid == MAX_NUMNODES)
50663b4613cSNishanth Aravamudan 			next_nid = first_node(node_online_map);
50763b4613cSNishanth Aravamudan 		hugetlb_next_nid = next_nid;
50863b4613cSNishanth Aravamudan 	} while (!page && hugetlb_next_nid != start_nid);
50963b4613cSNishanth Aravamudan 
5103b116300SAdam Litke 	if (ret)
5113b116300SAdam Litke 		count_vm_event(HTLB_BUDDY_PGALLOC);
5123b116300SAdam Litke 	else
5133b116300SAdam Litke 		count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
5143b116300SAdam Litke 
51563b4613cSNishanth Aravamudan 	return ret;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5187893d1d5SAdam Litke static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
5197893d1d5SAdam Litke 						unsigned long address)
5207893d1d5SAdam Litke {
5217893d1d5SAdam Litke 	struct page *page;
522d1c3fb1fSNishanth Aravamudan 	unsigned int nid;
5237893d1d5SAdam Litke 
524d1c3fb1fSNishanth Aravamudan 	/*
525d1c3fb1fSNishanth Aravamudan 	 * Assume we will successfully allocate the surplus page to
526d1c3fb1fSNishanth Aravamudan 	 * prevent racing processes from causing the surplus to exceed
527d1c3fb1fSNishanth Aravamudan 	 * overcommit
528d1c3fb1fSNishanth Aravamudan 	 *
529d1c3fb1fSNishanth Aravamudan 	 * This however introduces a different race, where a process B
530d1c3fb1fSNishanth Aravamudan 	 * tries to grow the static hugepage pool while alloc_pages() is
531d1c3fb1fSNishanth Aravamudan 	 * called by process A. B will only examine the per-node
532d1c3fb1fSNishanth Aravamudan 	 * counters in determining if surplus huge pages can be
533d1c3fb1fSNishanth Aravamudan 	 * converted to normal huge pages in adjust_pool_surplus(). A
534d1c3fb1fSNishanth Aravamudan 	 * won't be able to increment the per-node counter, until the
535d1c3fb1fSNishanth Aravamudan 	 * lock is dropped by B, but B doesn't drop hugetlb_lock until
536d1c3fb1fSNishanth Aravamudan 	 * no more huge pages can be converted from surplus to normal
537d1c3fb1fSNishanth Aravamudan 	 * state (and doesn't try to convert again). Thus, we have a
538d1c3fb1fSNishanth Aravamudan 	 * case where a surplus huge page exists, the pool is grown, and
539d1c3fb1fSNishanth Aravamudan 	 * the surplus huge page still exists after, even though it
540d1c3fb1fSNishanth Aravamudan 	 * should just have been converted to a normal huge page. This
541d1c3fb1fSNishanth Aravamudan 	 * does not leak memory, though, as the hugepage will be freed
542d1c3fb1fSNishanth Aravamudan 	 * once it is out of use. It also does not allow the counters to
543d1c3fb1fSNishanth Aravamudan 	 * go out of whack in adjust_pool_surplus() as we don't modify
544d1c3fb1fSNishanth Aravamudan 	 * the node values until we've gotten the hugepage and only the
545d1c3fb1fSNishanth Aravamudan 	 * per-node value is checked there.
546d1c3fb1fSNishanth Aravamudan 	 */
547d1c3fb1fSNishanth Aravamudan 	spin_lock(&hugetlb_lock);
548d1c3fb1fSNishanth Aravamudan 	if (surplus_huge_pages >= nr_overcommit_huge_pages) {
549d1c3fb1fSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
550d1c3fb1fSNishanth Aravamudan 		return NULL;
551d1c3fb1fSNishanth Aravamudan 	} else {
552d1c3fb1fSNishanth Aravamudan 		nr_huge_pages++;
553d1c3fb1fSNishanth Aravamudan 		surplus_huge_pages++;
554d1c3fb1fSNishanth Aravamudan 	}
555d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
556d1c3fb1fSNishanth Aravamudan 
557551883aeSNishanth Aravamudan 	page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
558551883aeSNishanth Aravamudan 					__GFP_REPEAT|__GFP_NOWARN,
5597893d1d5SAdam Litke 					HUGETLB_PAGE_ORDER);
560d1c3fb1fSNishanth Aravamudan 
5617893d1d5SAdam Litke 	spin_lock(&hugetlb_lock);
562d1c3fb1fSNishanth Aravamudan 	if (page) {
5632668db91SAdam Litke 		/*
5642668db91SAdam Litke 		 * This page is now managed by the hugetlb allocator and has
5652668db91SAdam Litke 		 * no users -- drop the buddy allocator's reference.
5662668db91SAdam Litke 		 */
5672668db91SAdam Litke 		put_page_testzero(page);
5682668db91SAdam Litke 		VM_BUG_ON(page_count(page));
569d1c3fb1fSNishanth Aravamudan 		nid = page_to_nid(page);
570d1c3fb1fSNishanth Aravamudan 		set_compound_page_dtor(page, free_huge_page);
571d1c3fb1fSNishanth Aravamudan 		/*
572d1c3fb1fSNishanth Aravamudan 		 * We incremented the global counters already
573d1c3fb1fSNishanth Aravamudan 		 */
574d1c3fb1fSNishanth Aravamudan 		nr_huge_pages_node[nid]++;
575d1c3fb1fSNishanth Aravamudan 		surplus_huge_pages_node[nid]++;
5763b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC);
577d1c3fb1fSNishanth Aravamudan 	} else {
578d1c3fb1fSNishanth Aravamudan 		nr_huge_pages--;
579d1c3fb1fSNishanth Aravamudan 		surplus_huge_pages--;
5803b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
5817893d1d5SAdam Litke 	}
582d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
5837893d1d5SAdam Litke 
5847893d1d5SAdam Litke 	return page;
5857893d1d5SAdam Litke }
5867893d1d5SAdam Litke 
587e4e574b7SAdam Litke /*
588e4e574b7SAdam Litke  * Increase the hugetlb pool such that it can accomodate a reservation
589e4e574b7SAdam Litke  * of size 'delta'.
590e4e574b7SAdam Litke  */
591e4e574b7SAdam Litke static int gather_surplus_pages(int delta)
592e4e574b7SAdam Litke {
593e4e574b7SAdam Litke 	struct list_head surplus_list;
594e4e574b7SAdam Litke 	struct page *page, *tmp;
595e4e574b7SAdam Litke 	int ret, i;
596e4e574b7SAdam Litke 	int needed, allocated;
597e4e574b7SAdam Litke 
598e4e574b7SAdam Litke 	needed = (resv_huge_pages + delta) - free_huge_pages;
599ac09b3a1SAdam Litke 	if (needed <= 0) {
600ac09b3a1SAdam Litke 		resv_huge_pages += delta;
601e4e574b7SAdam Litke 		return 0;
602ac09b3a1SAdam Litke 	}
603e4e574b7SAdam Litke 
604e4e574b7SAdam Litke 	allocated = 0;
605e4e574b7SAdam Litke 	INIT_LIST_HEAD(&surplus_list);
606e4e574b7SAdam Litke 
607e4e574b7SAdam Litke 	ret = -ENOMEM;
608e4e574b7SAdam Litke retry:
609e4e574b7SAdam Litke 	spin_unlock(&hugetlb_lock);
610e4e574b7SAdam Litke 	for (i = 0; i < needed; i++) {
611e4e574b7SAdam Litke 		page = alloc_buddy_huge_page(NULL, 0);
612e4e574b7SAdam Litke 		if (!page) {
613e4e574b7SAdam Litke 			/*
614e4e574b7SAdam Litke 			 * We were not able to allocate enough pages to
615e4e574b7SAdam Litke 			 * satisfy the entire reservation so we free what
616e4e574b7SAdam Litke 			 * we've allocated so far.
617e4e574b7SAdam Litke 			 */
618e4e574b7SAdam Litke 			spin_lock(&hugetlb_lock);
619e4e574b7SAdam Litke 			needed = 0;
620e4e574b7SAdam Litke 			goto free;
621e4e574b7SAdam Litke 		}
622e4e574b7SAdam Litke 
623e4e574b7SAdam Litke 		list_add(&page->lru, &surplus_list);
624e4e574b7SAdam Litke 	}
625e4e574b7SAdam Litke 	allocated += needed;
626e4e574b7SAdam Litke 
627e4e574b7SAdam Litke 	/*
628e4e574b7SAdam Litke 	 * After retaking hugetlb_lock, we need to recalculate 'needed'
629e4e574b7SAdam Litke 	 * because either resv_huge_pages or free_huge_pages may have changed.
630e4e574b7SAdam Litke 	 */
631e4e574b7SAdam Litke 	spin_lock(&hugetlb_lock);
632e4e574b7SAdam Litke 	needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
633e4e574b7SAdam Litke 	if (needed > 0)
634e4e574b7SAdam Litke 		goto retry;
635e4e574b7SAdam Litke 
636e4e574b7SAdam Litke 	/*
637e4e574b7SAdam Litke 	 * The surplus_list now contains _at_least_ the number of extra pages
638e4e574b7SAdam Litke 	 * needed to accomodate the reservation.  Add the appropriate number
639e4e574b7SAdam Litke 	 * of pages to the hugetlb pool and free the extras back to the buddy
640ac09b3a1SAdam Litke 	 * allocator.  Commit the entire reservation here to prevent another
641ac09b3a1SAdam Litke 	 * process from stealing the pages as they are added to the pool but
642ac09b3a1SAdam Litke 	 * before they are reserved.
643e4e574b7SAdam Litke 	 */
644e4e574b7SAdam Litke 	needed += allocated;
645ac09b3a1SAdam Litke 	resv_huge_pages += delta;
646e4e574b7SAdam Litke 	ret = 0;
647e4e574b7SAdam Litke free:
64819fc3f0aSAdam Litke 	/* Free the needed pages to the hugetlb pool */
64919fc3f0aSAdam Litke 	list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
65019fc3f0aSAdam Litke 		if ((--needed) < 0)
65119fc3f0aSAdam Litke 			break;
65219fc3f0aSAdam Litke 		list_del(&page->lru);
65319fc3f0aSAdam Litke 		enqueue_huge_page(page);
65419fc3f0aSAdam Litke 	}
65519fc3f0aSAdam Litke 
65619fc3f0aSAdam Litke 	/* Free unnecessary surplus pages to the buddy allocator */
65719fc3f0aSAdam Litke 	if (!list_empty(&surplus_list)) {
65819fc3f0aSAdam Litke 		spin_unlock(&hugetlb_lock);
659e4e574b7SAdam Litke 		list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
660e4e574b7SAdam Litke 			list_del(&page->lru);
661af767cbdSAdam Litke 			/*
6622668db91SAdam Litke 			 * The page has a reference count of zero already, so
6632668db91SAdam Litke 			 * call free_huge_page directly instead of using
6642668db91SAdam Litke 			 * put_page.  This must be done with hugetlb_lock
665af767cbdSAdam Litke 			 * unlocked which is safe because free_huge_page takes
666af767cbdSAdam Litke 			 * hugetlb_lock before deciding how to free the page.
667af767cbdSAdam Litke 			 */
6682668db91SAdam Litke 			free_huge_page(page);
669af767cbdSAdam Litke 		}
67019fc3f0aSAdam Litke 		spin_lock(&hugetlb_lock);
671e4e574b7SAdam Litke 	}
672e4e574b7SAdam Litke 
673e4e574b7SAdam Litke 	return ret;
674e4e574b7SAdam Litke }
675e4e574b7SAdam Litke 
676e4e574b7SAdam Litke /*
677e4e574b7SAdam Litke  * When releasing a hugetlb pool reservation, any surplus pages that were
678e4e574b7SAdam Litke  * allocated to satisfy the reservation must be explicitly freed if they were
679e4e574b7SAdam Litke  * never used.
680e4e574b7SAdam Litke  */
6818cde045cSAdrian Bunk static void return_unused_surplus_pages(unsigned long unused_resv_pages)
682e4e574b7SAdam Litke {
683e4e574b7SAdam Litke 	static int nid = -1;
684e4e574b7SAdam Litke 	struct page *page;
685e4e574b7SAdam Litke 	unsigned long nr_pages;
686e4e574b7SAdam Litke 
68711320d17SNishanth Aravamudan 	/*
68811320d17SNishanth Aravamudan 	 * We want to release as many surplus pages as possible, spread
68911320d17SNishanth Aravamudan 	 * evenly across all nodes. Iterate across all nodes until we
69011320d17SNishanth Aravamudan 	 * can no longer free unreserved surplus pages. This occurs when
69111320d17SNishanth Aravamudan 	 * the nodes with surplus pages have no free pages.
69211320d17SNishanth Aravamudan 	 */
69311320d17SNishanth Aravamudan 	unsigned long remaining_iterations = num_online_nodes();
69411320d17SNishanth Aravamudan 
695ac09b3a1SAdam Litke 	/* Uncommit the reservation */
696ac09b3a1SAdam Litke 	resv_huge_pages -= unused_resv_pages;
697ac09b3a1SAdam Litke 
698e4e574b7SAdam Litke 	nr_pages = min(unused_resv_pages, surplus_huge_pages);
699e4e574b7SAdam Litke 
70011320d17SNishanth Aravamudan 	while (remaining_iterations-- && nr_pages) {
701e4e574b7SAdam Litke 		nid = next_node(nid, node_online_map);
702e4e574b7SAdam Litke 		if (nid == MAX_NUMNODES)
703e4e574b7SAdam Litke 			nid = first_node(node_online_map);
704e4e574b7SAdam Litke 
705e4e574b7SAdam Litke 		if (!surplus_huge_pages_node[nid])
706e4e574b7SAdam Litke 			continue;
707e4e574b7SAdam Litke 
708e4e574b7SAdam Litke 		if (!list_empty(&hugepage_freelists[nid])) {
709e4e574b7SAdam Litke 			page = list_entry(hugepage_freelists[nid].next,
710e4e574b7SAdam Litke 					  struct page, lru);
711e4e574b7SAdam Litke 			list_del(&page->lru);
712e4e574b7SAdam Litke 			update_and_free_page(page);
713e4e574b7SAdam Litke 			free_huge_pages--;
714e4e574b7SAdam Litke 			free_huge_pages_node[nid]--;
715e4e574b7SAdam Litke 			surplus_huge_pages--;
716e4e574b7SAdam Litke 			surplus_huge_pages_node[nid]--;
717e4e574b7SAdam Litke 			nr_pages--;
71811320d17SNishanth Aravamudan 			remaining_iterations = num_online_nodes();
719e4e574b7SAdam Litke 		}
720e4e574b7SAdam Litke 	}
721e4e574b7SAdam Litke }
722e4e574b7SAdam Litke 
723348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma,
72404f2cbe3SMel Gorman 				    unsigned long addr, int avoid_reserve)
725348ea204SAdam Litke {
726348ea204SAdam Litke 	struct page *page;
7272fc39cecSAdam Litke 	struct address_space *mapping = vma->vm_file->f_mapping;
728a1e78772SMel Gorman 	struct inode *inode = mapping->host;
729a1e78772SMel Gorman 	unsigned int chg = 0;
7302fc39cecSAdam Litke 
731a1e78772SMel Gorman 	/*
732a1e78772SMel Gorman 	 * Processes that did not create the mapping will have no reserves and
733a1e78772SMel Gorman 	 * will not have accounted against quota. Check that the quota can be
734a1e78772SMel Gorman 	 * made before satisfying the allocation
735a1e78772SMel Gorman 	 */
73604f2cbe3SMel Gorman 	if (!(vma->vm_flags & VM_SHARED) &&
73704f2cbe3SMel Gorman 			!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
738a1e78772SMel Gorman 		chg = 1;
739a1e78772SMel Gorman 		if (hugetlb_get_quota(inode->i_mapping, chg))
740a1e78772SMel Gorman 			return ERR_PTR(-ENOSPC);
741a1e78772SMel Gorman 	}
74290d8b7e6SAdam Litke 
743a1e78772SMel Gorman 	spin_lock(&hugetlb_lock);
74404f2cbe3SMel Gorman 	page = dequeue_huge_page_vma(vma, addr, avoid_reserve);
745a1e78772SMel Gorman 	spin_unlock(&hugetlb_lock);
746a1e78772SMel Gorman 
747a1e78772SMel Gorman 	if (!page) {
748a1e78772SMel Gorman 		page = alloc_buddy_huge_page(vma, addr);
749a1e78772SMel Gorman 		if (!page) {
750a1e78772SMel Gorman 			hugetlb_put_quota(inode->i_mapping, chg);
751a1e78772SMel Gorman 			return ERR_PTR(-VM_FAULT_OOM);
752a1e78772SMel Gorman 		}
753a1e78772SMel Gorman 	}
754a1e78772SMel Gorman 
755348ea204SAdam Litke 	set_page_refcounted(page);
7562fc39cecSAdam Litke 	set_page_private(page, (unsigned long) mapping);
757a1e78772SMel Gorman 
7587893d1d5SAdam Litke 	return page;
759b45b5bd6SDavid Gibson }
760b45b5bd6SDavid Gibson 
7611da177e4SLinus Torvalds static int __init hugetlb_init(void)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	unsigned long i;
7641da177e4SLinus Torvalds 
7653c726f8dSBenjamin Herrenschmidt 	if (HPAGE_SHIFT == 0)
7663c726f8dSBenjamin Herrenschmidt 		return 0;
7673c726f8dSBenjamin Herrenschmidt 
7681da177e4SLinus Torvalds 	for (i = 0; i < MAX_NUMNODES; ++i)
7691da177e4SLinus Torvalds 		INIT_LIST_HEAD(&hugepage_freelists[i]);
7701da177e4SLinus Torvalds 
77163b4613cSNishanth Aravamudan 	hugetlb_next_nid = first_node(node_online_map);
77263b4613cSNishanth Aravamudan 
7731da177e4SLinus Torvalds 	for (i = 0; i < max_huge_pages; ++i) {
774a482289dSNick Piggin 		if (!alloc_fresh_huge_page())
7751da177e4SLinus Torvalds 			break;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 	max_huge_pages = free_huge_pages = nr_huge_pages = i;
7781da177e4SLinus Torvalds 	printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
7791da177e4SLinus Torvalds 	return 0;
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds module_init(hugetlb_init);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds static int __init hugetlb_setup(char *s)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	if (sscanf(s, "%lu", &max_huge_pages) <= 0)
7861da177e4SLinus Torvalds 		max_huge_pages = 0;
7871da177e4SLinus Torvalds 	return 1;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds __setup("hugepages=", hugetlb_setup);
7901da177e4SLinus Torvalds 
7918a630112SKen Chen static unsigned int cpuset_mems_nr(unsigned int *array)
7928a630112SKen Chen {
7938a630112SKen Chen 	int node;
7948a630112SKen Chen 	unsigned int nr = 0;
7958a630112SKen Chen 
7968a630112SKen Chen 	for_each_node_mask(node, cpuset_current_mems_allowed)
7978a630112SKen Chen 		nr += array[node];
7988a630112SKen Chen 
7998a630112SKen Chen 	return nr;
8008a630112SKen Chen }
8018a630112SKen Chen 
8021da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
8031da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM
8041da177e4SLinus Torvalds static void try_to_free_low(unsigned long count)
8051da177e4SLinus Torvalds {
8064415cc8dSChristoph Lameter 	int i;
8074415cc8dSChristoph Lameter 
8081da177e4SLinus Torvalds 	for (i = 0; i < MAX_NUMNODES; ++i) {
8091da177e4SLinus Torvalds 		struct page *page, *next;
8101da177e4SLinus Torvalds 		list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
8116b0c880dSAdam Litke 			if (count >= nr_huge_pages)
8126b0c880dSAdam Litke 				return;
8131da177e4SLinus Torvalds 			if (PageHighMem(page))
8141da177e4SLinus Torvalds 				continue;
8151da177e4SLinus Torvalds 			list_del(&page->lru);
8161da177e4SLinus Torvalds 			update_and_free_page(page);
8171da177e4SLinus Torvalds 			free_huge_pages--;
8184415cc8dSChristoph Lameter 			free_huge_pages_node[page_to_nid(page)]--;
8191da177e4SLinus Torvalds 		}
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds #else
8231da177e4SLinus Torvalds static inline void try_to_free_low(unsigned long count)
8241da177e4SLinus Torvalds {
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds #endif
8271da177e4SLinus Torvalds 
8287893d1d5SAdam Litke #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
8291da177e4SLinus Torvalds static unsigned long set_max_huge_pages(unsigned long count)
8301da177e4SLinus Torvalds {
8317893d1d5SAdam Litke 	unsigned long min_count, ret;
8321da177e4SLinus Torvalds 
8337893d1d5SAdam Litke 	/*
8347893d1d5SAdam Litke 	 * Increase the pool size
8357893d1d5SAdam Litke 	 * First take pages out of surplus state.  Then make up the
8367893d1d5SAdam Litke 	 * remaining difference by allocating fresh huge pages.
837d1c3fb1fSNishanth Aravamudan 	 *
838d1c3fb1fSNishanth Aravamudan 	 * We might race with alloc_buddy_huge_page() here and be unable
839d1c3fb1fSNishanth Aravamudan 	 * to convert a surplus huge page to a normal huge page. That is
840d1c3fb1fSNishanth Aravamudan 	 * not critical, though, it just means the overall size of the
841d1c3fb1fSNishanth Aravamudan 	 * pool might be one hugepage larger than it needs to be, but
842d1c3fb1fSNishanth Aravamudan 	 * within all the constraints specified by the sysctls.
8437893d1d5SAdam Litke 	 */
8441da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
8457893d1d5SAdam Litke 	while (surplus_huge_pages && count > persistent_huge_pages) {
8467893d1d5SAdam Litke 		if (!adjust_pool_surplus(-1))
8477893d1d5SAdam Litke 			break;
8487893d1d5SAdam Litke 	}
8497893d1d5SAdam Litke 
8507893d1d5SAdam Litke 	while (count > persistent_huge_pages) {
8517893d1d5SAdam Litke 		/*
8527893d1d5SAdam Litke 		 * If this allocation races such that we no longer need the
8537893d1d5SAdam Litke 		 * page, free_huge_page will handle it by freeing the page
8547893d1d5SAdam Litke 		 * and reducing the surplus.
8557893d1d5SAdam Litke 		 */
8567893d1d5SAdam Litke 		spin_unlock(&hugetlb_lock);
8577893d1d5SAdam Litke 		ret = alloc_fresh_huge_page();
8587893d1d5SAdam Litke 		spin_lock(&hugetlb_lock);
8597893d1d5SAdam Litke 		if (!ret)
8607893d1d5SAdam Litke 			goto out;
8617893d1d5SAdam Litke 
8627893d1d5SAdam Litke 	}
8637893d1d5SAdam Litke 
8647893d1d5SAdam Litke 	/*
8657893d1d5SAdam Litke 	 * Decrease the pool size
8667893d1d5SAdam Litke 	 * First return free pages to the buddy allocator (being careful
8677893d1d5SAdam Litke 	 * to keep enough around to satisfy reservations).  Then place
8687893d1d5SAdam Litke 	 * pages into surplus state as needed so the pool will shrink
8697893d1d5SAdam Litke 	 * to the desired size as pages become free.
870d1c3fb1fSNishanth Aravamudan 	 *
871d1c3fb1fSNishanth Aravamudan 	 * By placing pages into the surplus state independent of the
872d1c3fb1fSNishanth Aravamudan 	 * overcommit value, we are allowing the surplus pool size to
873d1c3fb1fSNishanth Aravamudan 	 * exceed overcommit. There are few sane options here. Since
874d1c3fb1fSNishanth Aravamudan 	 * alloc_buddy_huge_page() is checking the global counter,
875d1c3fb1fSNishanth Aravamudan 	 * though, we'll note that we're not allowed to exceed surplus
876d1c3fb1fSNishanth Aravamudan 	 * and won't grow the pool anywhere else. Not until one of the
877d1c3fb1fSNishanth Aravamudan 	 * sysctls are changed, or the surplus pages go out of use.
8787893d1d5SAdam Litke 	 */
8796b0c880dSAdam Litke 	min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
8806b0c880dSAdam Litke 	min_count = max(count, min_count);
8817893d1d5SAdam Litke 	try_to_free_low(min_count);
8827893d1d5SAdam Litke 	while (min_count < persistent_huge_pages) {
883348e1e04SNishanth Aravamudan 		struct page *page = dequeue_huge_page();
8841da177e4SLinus Torvalds 		if (!page)
8851da177e4SLinus Torvalds 			break;
8861da177e4SLinus Torvalds 		update_and_free_page(page);
8871da177e4SLinus Torvalds 	}
8887893d1d5SAdam Litke 	while (count < persistent_huge_pages) {
8897893d1d5SAdam Litke 		if (!adjust_pool_surplus(1))
8907893d1d5SAdam Litke 			break;
8917893d1d5SAdam Litke 	}
8927893d1d5SAdam Litke out:
8937893d1d5SAdam Litke 	ret = persistent_huge_pages;
8941da177e4SLinus Torvalds 	spin_unlock(&hugetlb_lock);
8957893d1d5SAdam Litke 	return ret;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds int hugetlb_sysctl_handler(struct ctl_table *table, int write,
8991da177e4SLinus Torvalds 			   struct file *file, void __user *buffer,
9001da177e4SLinus Torvalds 			   size_t *length, loff_t *ppos)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
9031da177e4SLinus Torvalds 	max_huge_pages = set_max_huge_pages(max_huge_pages);
9041da177e4SLinus Torvalds 	return 0;
9051da177e4SLinus Torvalds }
906396faf03SMel Gorman 
907396faf03SMel Gorman int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
908396faf03SMel Gorman 			struct file *file, void __user *buffer,
909396faf03SMel Gorman 			size_t *length, loff_t *ppos)
910396faf03SMel Gorman {
911396faf03SMel Gorman 	proc_dointvec(table, write, file, buffer, length, ppos);
912396faf03SMel Gorman 	if (hugepages_treat_as_movable)
913396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
914396faf03SMel Gorman 	else
915396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER;
916396faf03SMel Gorman 	return 0;
917396faf03SMel Gorman }
918396faf03SMel Gorman 
919a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write,
920a3d0c6aaSNishanth Aravamudan 			struct file *file, void __user *buffer,
921a3d0c6aaSNishanth Aravamudan 			size_t *length, loff_t *ppos)
922a3d0c6aaSNishanth Aravamudan {
923a3d0c6aaSNishanth Aravamudan 	proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
924064d9efeSNishanth Aravamudan 	spin_lock(&hugetlb_lock);
925064d9efeSNishanth Aravamudan 	nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
926a3d0c6aaSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
927a3d0c6aaSNishanth Aravamudan 	return 0;
928a3d0c6aaSNishanth Aravamudan }
929a3d0c6aaSNishanth Aravamudan 
9301da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds int hugetlb_report_meminfo(char *buf)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	return sprintf(buf,
9351da177e4SLinus Torvalds 			"HugePages_Total: %5lu\n"
9361da177e4SLinus Torvalds 			"HugePages_Free:  %5lu\n"
937b45b5bd6SDavid Gibson 			"HugePages_Rsvd:  %5lu\n"
9387893d1d5SAdam Litke 			"HugePages_Surp:  %5lu\n"
9391da177e4SLinus Torvalds 			"Hugepagesize:    %5lu kB\n",
9401da177e4SLinus Torvalds 			nr_huge_pages,
9411da177e4SLinus Torvalds 			free_huge_pages,
942a43a8c39SChen, Kenneth W 			resv_huge_pages,
9437893d1d5SAdam Litke 			surplus_huge_pages,
9441da177e4SLinus Torvalds 			HPAGE_SIZE/1024);
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf)
9481da177e4SLinus Torvalds {
9491da177e4SLinus Torvalds 	return sprintf(buf,
9501da177e4SLinus Torvalds 		"Node %d HugePages_Total: %5u\n"
951a1de0919SNishanth Aravamudan 		"Node %d HugePages_Free:  %5u\n"
952a1de0919SNishanth Aravamudan 		"Node %d HugePages_Surp:  %5u\n",
9531da177e4SLinus Torvalds 		nid, nr_huge_pages_node[nid],
954a1de0919SNishanth Aravamudan 		nid, free_huge_pages_node[nid],
955a1de0919SNishanth Aravamudan 		nid, surplus_huge_pages_node[nid]);
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
9591da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
964fc1b8a73SMel Gorman static int hugetlb_acct_memory(long delta)
965fc1b8a73SMel Gorman {
966fc1b8a73SMel Gorman 	int ret = -ENOMEM;
967fc1b8a73SMel Gorman 
968fc1b8a73SMel Gorman 	spin_lock(&hugetlb_lock);
969fc1b8a73SMel Gorman 	/*
970fc1b8a73SMel Gorman 	 * When cpuset is configured, it breaks the strict hugetlb page
971fc1b8a73SMel Gorman 	 * reservation as the accounting is done on a global variable. Such
972fc1b8a73SMel Gorman 	 * reservation is completely rubbish in the presence of cpuset because
973fc1b8a73SMel Gorman 	 * the reservation is not checked against page availability for the
974fc1b8a73SMel Gorman 	 * current cpuset. Application can still potentially OOM'ed by kernel
975fc1b8a73SMel Gorman 	 * with lack of free htlb page in cpuset that the task is in.
976fc1b8a73SMel Gorman 	 * Attempt to enforce strict accounting with cpuset is almost
977fc1b8a73SMel Gorman 	 * impossible (or too ugly) because cpuset is too fluid that
978fc1b8a73SMel Gorman 	 * task or memory node can be dynamically moved between cpusets.
979fc1b8a73SMel Gorman 	 *
980fc1b8a73SMel Gorman 	 * The change of semantics for shared hugetlb mapping with cpuset is
981fc1b8a73SMel Gorman 	 * undesirable. However, in order to preserve some of the semantics,
982fc1b8a73SMel Gorman 	 * we fall back to check against current free page availability as
983fc1b8a73SMel Gorman 	 * a best attempt and hopefully to minimize the impact of changing
984fc1b8a73SMel Gorman 	 * semantics that cpuset has.
985fc1b8a73SMel Gorman 	 */
986fc1b8a73SMel Gorman 	if (delta > 0) {
987fc1b8a73SMel Gorman 		if (gather_surplus_pages(delta) < 0)
988fc1b8a73SMel Gorman 			goto out;
989fc1b8a73SMel Gorman 
990fc1b8a73SMel Gorman 		if (delta > cpuset_mems_nr(free_huge_pages_node)) {
991fc1b8a73SMel Gorman 			return_unused_surplus_pages(delta);
992fc1b8a73SMel Gorman 			goto out;
993fc1b8a73SMel Gorman 		}
994fc1b8a73SMel Gorman 	}
995fc1b8a73SMel Gorman 
996fc1b8a73SMel Gorman 	ret = 0;
997fc1b8a73SMel Gorman 	if (delta < 0)
998fc1b8a73SMel Gorman 		return_unused_surplus_pages((unsigned long) -delta);
999fc1b8a73SMel Gorman 
1000fc1b8a73SMel Gorman out:
1001fc1b8a73SMel Gorman 	spin_unlock(&hugetlb_lock);
1002fc1b8a73SMel Gorman 	return ret;
1003fc1b8a73SMel Gorman }
1004fc1b8a73SMel Gorman 
1005a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma)
1006a1e78772SMel Gorman {
1007a1e78772SMel Gorman 	unsigned long reserve = vma_resv_huge_pages(vma);
1008a1e78772SMel Gorman 	if (reserve)
1009a1e78772SMel Gorman 		hugetlb_acct_memory(-reserve);
1010a1e78772SMel Gorman }
1011a1e78772SMel Gorman 
10121da177e4SLinus Torvalds /*
10131da177e4SLinus Torvalds  * We cannot handle pagefaults against hugetlb pages at all.  They cause
10141da177e4SLinus Torvalds  * handle_mm_fault() to try to instantiate regular-sized pages in the
10151da177e4SLinus Torvalds  * hugegpage VMA.  do_page_fault() is supposed to trap this, so BUG is we get
10161da177e4SLinus Torvalds  * this far.
10171da177e4SLinus Torvalds  */
1018d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
10191da177e4SLinus Torvalds {
10201da177e4SLinus Torvalds 	BUG();
1021d0217ac0SNick Piggin 	return 0;
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds struct vm_operations_struct hugetlb_vm_ops = {
1025d0217ac0SNick Piggin 	.fault = hugetlb_vm_op_fault,
1026a1e78772SMel Gorman 	.close = hugetlb_vm_op_close,
10271da177e4SLinus Torvalds };
10281da177e4SLinus Torvalds 
10291e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
10301e8f889bSDavid Gibson 				int writable)
103163551ae0SDavid Gibson {
103263551ae0SDavid Gibson 	pte_t entry;
103363551ae0SDavid Gibson 
10341e8f889bSDavid Gibson 	if (writable) {
103563551ae0SDavid Gibson 		entry =
103663551ae0SDavid Gibson 		    pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
103763551ae0SDavid Gibson 	} else {
10387f2e9525SGerald Schaefer 		entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
103963551ae0SDavid Gibson 	}
104063551ae0SDavid Gibson 	entry = pte_mkyoung(entry);
104163551ae0SDavid Gibson 	entry = pte_mkhuge(entry);
104263551ae0SDavid Gibson 
104363551ae0SDavid Gibson 	return entry;
104463551ae0SDavid Gibson }
104563551ae0SDavid Gibson 
10461e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma,
10471e8f889bSDavid Gibson 				   unsigned long address, pte_t *ptep)
10481e8f889bSDavid Gibson {
10491e8f889bSDavid Gibson 	pte_t entry;
10501e8f889bSDavid Gibson 
10517f2e9525SGerald Schaefer 	entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
10527f2e9525SGerald Schaefer 	if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
10531e8f889bSDavid Gibson 		update_mmu_cache(vma, address, entry);
10541e8f889bSDavid Gibson 	}
10558dab5241SBenjamin Herrenschmidt }
10561e8f889bSDavid Gibson 
10571e8f889bSDavid Gibson 
105863551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
105963551ae0SDavid Gibson 			    struct vm_area_struct *vma)
106063551ae0SDavid Gibson {
106163551ae0SDavid Gibson 	pte_t *src_pte, *dst_pte, entry;
106263551ae0SDavid Gibson 	struct page *ptepage;
10631c59827dSHugh Dickins 	unsigned long addr;
10641e8f889bSDavid Gibson 	int cow;
10651e8f889bSDavid Gibson 
10661e8f889bSDavid Gibson 	cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
106763551ae0SDavid Gibson 
10681c59827dSHugh Dickins 	for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
1069c74df32cSHugh Dickins 		src_pte = huge_pte_offset(src, addr);
1070c74df32cSHugh Dickins 		if (!src_pte)
1071c74df32cSHugh Dickins 			continue;
107263551ae0SDavid Gibson 		dst_pte = huge_pte_alloc(dst, addr);
107363551ae0SDavid Gibson 		if (!dst_pte)
107463551ae0SDavid Gibson 			goto nomem;
1075c5c99429SLarry Woodman 
1076c5c99429SLarry Woodman 		/* If the pagetables are shared don't copy or take references */
1077c5c99429SLarry Woodman 		if (dst_pte == src_pte)
1078c5c99429SLarry Woodman 			continue;
1079c5c99429SLarry Woodman 
1080c74df32cSHugh Dickins 		spin_lock(&dst->page_table_lock);
108146478758SNick Piggin 		spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
10827f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(src_pte))) {
10831e8f889bSDavid Gibson 			if (cow)
10847f2e9525SGerald Schaefer 				huge_ptep_set_wrprotect(src, addr, src_pte);
10857f2e9525SGerald Schaefer 			entry = huge_ptep_get(src_pte);
108663551ae0SDavid Gibson 			ptepage = pte_page(entry);
108763551ae0SDavid Gibson 			get_page(ptepage);
108863551ae0SDavid Gibson 			set_huge_pte_at(dst, addr, dst_pte, entry);
10891c59827dSHugh Dickins 		}
10901c59827dSHugh Dickins 		spin_unlock(&src->page_table_lock);
1091c74df32cSHugh Dickins 		spin_unlock(&dst->page_table_lock);
109263551ae0SDavid Gibson 	}
109363551ae0SDavid Gibson 	return 0;
109463551ae0SDavid Gibson 
109563551ae0SDavid Gibson nomem:
109663551ae0SDavid Gibson 	return -ENOMEM;
109763551ae0SDavid Gibson }
109863551ae0SDavid Gibson 
1099502717f4SChen, Kenneth W void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
110004f2cbe3SMel Gorman 			    unsigned long end, struct page *ref_page)
110163551ae0SDavid Gibson {
110263551ae0SDavid Gibson 	struct mm_struct *mm = vma->vm_mm;
110363551ae0SDavid Gibson 	unsigned long address;
1104c7546f8fSDavid Gibson 	pte_t *ptep;
110563551ae0SDavid Gibson 	pte_t pte;
110663551ae0SDavid Gibson 	struct page *page;
1107fe1668aeSChen, Kenneth W 	struct page *tmp;
1108c0a499c2SChen, Kenneth W 	/*
1109c0a499c2SChen, Kenneth W 	 * A page gathering list, protected by per file i_mmap_lock. The
1110c0a499c2SChen, Kenneth W 	 * lock is used to avoid list corruption from multiple unmapping
1111c0a499c2SChen, Kenneth W 	 * of the same page since we are using page->lru.
1112c0a499c2SChen, Kenneth W 	 */
1113fe1668aeSChen, Kenneth W 	LIST_HEAD(page_list);
111463551ae0SDavid Gibson 
111563551ae0SDavid Gibson 	WARN_ON(!is_vm_hugetlb_page(vma));
111663551ae0SDavid Gibson 	BUG_ON(start & ~HPAGE_MASK);
111763551ae0SDavid Gibson 	BUG_ON(end & ~HPAGE_MASK);
111863551ae0SDavid Gibson 
1119508034a3SHugh Dickins 	spin_lock(&mm->page_table_lock);
112063551ae0SDavid Gibson 	for (address = start; address < end; address += HPAGE_SIZE) {
1121c7546f8fSDavid Gibson 		ptep = huge_pte_offset(mm, address);
1122c7546f8fSDavid Gibson 		if (!ptep)
1123c7546f8fSDavid Gibson 			continue;
1124c7546f8fSDavid Gibson 
112539dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
112639dde65cSChen, Kenneth W 			continue;
112739dde65cSChen, Kenneth W 
112804f2cbe3SMel Gorman 		/*
112904f2cbe3SMel Gorman 		 * If a reference page is supplied, it is because a specific
113004f2cbe3SMel Gorman 		 * page is being unmapped, not a range. Ensure the page we
113104f2cbe3SMel Gorman 		 * are about to unmap is the actual page of interest.
113204f2cbe3SMel Gorman 		 */
113304f2cbe3SMel Gorman 		if (ref_page) {
113404f2cbe3SMel Gorman 			pte = huge_ptep_get(ptep);
113504f2cbe3SMel Gorman 			if (huge_pte_none(pte))
113604f2cbe3SMel Gorman 				continue;
113704f2cbe3SMel Gorman 			page = pte_page(pte);
113804f2cbe3SMel Gorman 			if (page != ref_page)
113904f2cbe3SMel Gorman 				continue;
114004f2cbe3SMel Gorman 
114104f2cbe3SMel Gorman 			/*
114204f2cbe3SMel Gorman 			 * Mark the VMA as having unmapped its page so that
114304f2cbe3SMel Gorman 			 * future faults in this VMA will fail rather than
114404f2cbe3SMel Gorman 			 * looking like data was lost
114504f2cbe3SMel Gorman 			 */
114604f2cbe3SMel Gorman 			set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
114704f2cbe3SMel Gorman 		}
114804f2cbe3SMel Gorman 
1149c7546f8fSDavid Gibson 		pte = huge_ptep_get_and_clear(mm, address, ptep);
11507f2e9525SGerald Schaefer 		if (huge_pte_none(pte))
115163551ae0SDavid Gibson 			continue;
1152c7546f8fSDavid Gibson 
115363551ae0SDavid Gibson 		page = pte_page(pte);
11546649a386SKen Chen 		if (pte_dirty(pte))
11556649a386SKen Chen 			set_page_dirty(page);
1156fe1668aeSChen, Kenneth W 		list_add(&page->lru, &page_list);
115763551ae0SDavid Gibson 	}
11581da177e4SLinus Torvalds 	spin_unlock(&mm->page_table_lock);
1159508034a3SHugh Dickins 	flush_tlb_range(vma, start, end);
1160fe1668aeSChen, Kenneth W 	list_for_each_entry_safe(page, tmp, &page_list, lru) {
1161fe1668aeSChen, Kenneth W 		list_del(&page->lru);
1162fe1668aeSChen, Kenneth W 		put_page(page);
1163fe1668aeSChen, Kenneth W 	}
11641da177e4SLinus Torvalds }
116563551ae0SDavid Gibson 
1166502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
116704f2cbe3SMel Gorman 			  unsigned long end, struct page *ref_page)
1168502717f4SChen, Kenneth W {
1169502717f4SChen, Kenneth W 	/*
1170502717f4SChen, Kenneth W 	 * It is undesirable to test vma->vm_file as it should be non-null
1171502717f4SChen, Kenneth W 	 * for valid hugetlb area. However, vm_file will be NULL in the error
1172502717f4SChen, Kenneth W 	 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
1173502717f4SChen, Kenneth W 	 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
1174502717f4SChen, Kenneth W 	 * to clean up. Since no pte has actually been setup, it is safe to
1175502717f4SChen, Kenneth W 	 * do nothing in this case.
1176502717f4SChen, Kenneth W 	 */
1177502717f4SChen, Kenneth W 	if (vma->vm_file) {
1178502717f4SChen, Kenneth W 		spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
117904f2cbe3SMel Gorman 		__unmap_hugepage_range(vma, start, end, ref_page);
1180502717f4SChen, Kenneth W 		spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
1181502717f4SChen, Kenneth W 	}
1182502717f4SChen, Kenneth W }
1183502717f4SChen, Kenneth W 
118404f2cbe3SMel Gorman /*
118504f2cbe3SMel Gorman  * This is called when the original mapper is failing to COW a MAP_PRIVATE
118604f2cbe3SMel Gorman  * mappping it owns the reserve page for. The intention is to unmap the page
118704f2cbe3SMel Gorman  * from other VMAs and let the children be SIGKILLed if they are faulting the
118804f2cbe3SMel Gorman  * same region.
118904f2cbe3SMel Gorman  */
119004f2cbe3SMel Gorman int unmap_ref_private(struct mm_struct *mm,
119104f2cbe3SMel Gorman 					struct vm_area_struct *vma,
119204f2cbe3SMel Gorman 					struct page *page,
119304f2cbe3SMel Gorman 					unsigned long address)
119404f2cbe3SMel Gorman {
119504f2cbe3SMel Gorman 	struct vm_area_struct *iter_vma;
119604f2cbe3SMel Gorman 	struct address_space *mapping;
119704f2cbe3SMel Gorman 	struct prio_tree_iter iter;
119804f2cbe3SMel Gorman 	pgoff_t pgoff;
119904f2cbe3SMel Gorman 
120004f2cbe3SMel Gorman 	/*
120104f2cbe3SMel Gorman 	 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
120204f2cbe3SMel Gorman 	 * from page cache lookup which is in HPAGE_SIZE units.
120304f2cbe3SMel Gorman 	 */
120404f2cbe3SMel Gorman 	address = address & huge_page_mask(hstate_vma(vma));
120504f2cbe3SMel Gorman 	pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
120604f2cbe3SMel Gorman 		+ (vma->vm_pgoff >> PAGE_SHIFT);
120704f2cbe3SMel Gorman 	mapping = (struct address_space *)page_private(page);
120804f2cbe3SMel Gorman 
120904f2cbe3SMel Gorman 	vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
121004f2cbe3SMel Gorman 		/* Do not unmap the current VMA */
121104f2cbe3SMel Gorman 		if (iter_vma == vma)
121204f2cbe3SMel Gorman 			continue;
121304f2cbe3SMel Gorman 
121404f2cbe3SMel Gorman 		/*
121504f2cbe3SMel Gorman 		 * Unmap the page from other VMAs without their own reserves.
121604f2cbe3SMel Gorman 		 * They get marked to be SIGKILLed if they fault in these
121704f2cbe3SMel Gorman 		 * areas. This is because a future no-page fault on this VMA
121804f2cbe3SMel Gorman 		 * could insert a zeroed page instead of the data existing
121904f2cbe3SMel Gorman 		 * from the time of fork. This would look like data corruption
122004f2cbe3SMel Gorman 		 */
122104f2cbe3SMel Gorman 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
122204f2cbe3SMel Gorman 			unmap_hugepage_range(iter_vma,
122304f2cbe3SMel Gorman 				address, address + HPAGE_SIZE,
122404f2cbe3SMel Gorman 				page);
122504f2cbe3SMel Gorman 	}
122604f2cbe3SMel Gorman 
122704f2cbe3SMel Gorman 	return 1;
122804f2cbe3SMel Gorman }
122904f2cbe3SMel Gorman 
12301e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
123104f2cbe3SMel Gorman 			unsigned long address, pte_t *ptep, pte_t pte,
123204f2cbe3SMel Gorman 			struct page *pagecache_page)
12331e8f889bSDavid Gibson {
12341e8f889bSDavid Gibson 	struct page *old_page, *new_page;
123579ac6ba4SDavid Gibson 	int avoidcopy;
123604f2cbe3SMel Gorman 	int outside_reserve = 0;
12371e8f889bSDavid Gibson 
12381e8f889bSDavid Gibson 	old_page = pte_page(pte);
12391e8f889bSDavid Gibson 
124004f2cbe3SMel Gorman retry_avoidcopy:
12411e8f889bSDavid Gibson 	/* If no-one else is actually using this page, avoid the copy
12421e8f889bSDavid Gibson 	 * and just make the page writable */
12431e8f889bSDavid Gibson 	avoidcopy = (page_count(old_page) == 1);
12441e8f889bSDavid Gibson 	if (avoidcopy) {
12451e8f889bSDavid Gibson 		set_huge_ptep_writable(vma, address, ptep);
124683c54070SNick Piggin 		return 0;
12471e8f889bSDavid Gibson 	}
12481e8f889bSDavid Gibson 
124904f2cbe3SMel Gorman 	/*
125004f2cbe3SMel Gorman 	 * If the process that created a MAP_PRIVATE mapping is about to
125104f2cbe3SMel Gorman 	 * perform a COW due to a shared page count, attempt to satisfy
125204f2cbe3SMel Gorman 	 * the allocation without using the existing reserves. The pagecache
125304f2cbe3SMel Gorman 	 * page is used to determine if the reserve at this address was
125404f2cbe3SMel Gorman 	 * consumed or not. If reserves were used, a partial faulted mapping
125504f2cbe3SMel Gorman 	 * at the time of fork() could consume its reserves on COW instead
125604f2cbe3SMel Gorman 	 * of the full address range.
125704f2cbe3SMel Gorman 	 */
125804f2cbe3SMel Gorman 	if (!(vma->vm_flags & VM_SHARED) &&
125904f2cbe3SMel Gorman 			is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
126004f2cbe3SMel Gorman 			old_page != pagecache_page)
126104f2cbe3SMel Gorman 		outside_reserve = 1;
126204f2cbe3SMel Gorman 
12631e8f889bSDavid Gibson 	page_cache_get(old_page);
126404f2cbe3SMel Gorman 	new_page = alloc_huge_page(vma, address, outside_reserve);
12651e8f889bSDavid Gibson 
12662fc39cecSAdam Litke 	if (IS_ERR(new_page)) {
12671e8f889bSDavid Gibson 		page_cache_release(old_page);
126804f2cbe3SMel Gorman 
126904f2cbe3SMel Gorman 		/*
127004f2cbe3SMel Gorman 		 * If a process owning a MAP_PRIVATE mapping fails to COW,
127104f2cbe3SMel Gorman 		 * it is due to references held by a child and an insufficient
127204f2cbe3SMel Gorman 		 * huge page pool. To guarantee the original mappers
127304f2cbe3SMel Gorman 		 * reliability, unmap the page from child processes. The child
127404f2cbe3SMel Gorman 		 * may get SIGKILLed if it later faults.
127504f2cbe3SMel Gorman 		 */
127604f2cbe3SMel Gorman 		if (outside_reserve) {
127704f2cbe3SMel Gorman 			BUG_ON(huge_pte_none(pte));
127804f2cbe3SMel Gorman 			if (unmap_ref_private(mm, vma, old_page, address)) {
127904f2cbe3SMel Gorman 				BUG_ON(page_count(old_page) != 1);
128004f2cbe3SMel Gorman 				BUG_ON(huge_pte_none(pte));
128104f2cbe3SMel Gorman 				goto retry_avoidcopy;
128204f2cbe3SMel Gorman 			}
128304f2cbe3SMel Gorman 			WARN_ON_ONCE(1);
128404f2cbe3SMel Gorman 		}
128504f2cbe3SMel Gorman 
12862fc39cecSAdam Litke 		return -PTR_ERR(new_page);
12871e8f889bSDavid Gibson 	}
12881e8f889bSDavid Gibson 
12891e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
12909de455b2SAtsushi Nemoto 	copy_huge_page(new_page, old_page, address, vma);
12910ed361deSNick Piggin 	__SetPageUptodate(new_page);
12921e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
12931e8f889bSDavid Gibson 
12941e8f889bSDavid Gibson 	ptep = huge_pte_offset(mm, address & HPAGE_MASK);
12957f2e9525SGerald Schaefer 	if (likely(pte_same(huge_ptep_get(ptep), pte))) {
12961e8f889bSDavid Gibson 		/* Break COW */
12978fe627ecSGerald Schaefer 		huge_ptep_clear_flush(vma, address, ptep);
12981e8f889bSDavid Gibson 		set_huge_pte_at(mm, address, ptep,
12991e8f889bSDavid Gibson 				make_huge_pte(vma, new_page, 1));
13001e8f889bSDavid Gibson 		/* Make the old page be freed below */
13011e8f889bSDavid Gibson 		new_page = old_page;
13021e8f889bSDavid Gibson 	}
13031e8f889bSDavid Gibson 	page_cache_release(new_page);
13041e8f889bSDavid Gibson 	page_cache_release(old_page);
130583c54070SNick Piggin 	return 0;
13061e8f889bSDavid Gibson }
13071e8f889bSDavid Gibson 
130804f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */
130904f2cbe3SMel Gorman static struct page *hugetlbfs_pagecache_page(struct vm_area_struct *vma,
131004f2cbe3SMel Gorman 			unsigned long address)
131104f2cbe3SMel Gorman {
131204f2cbe3SMel Gorman 	struct address_space *mapping;
1313e7c4b0bfSAndy Whitcroft 	pgoff_t idx;
131404f2cbe3SMel Gorman 
131504f2cbe3SMel Gorman 	mapping = vma->vm_file->f_mapping;
1316e7c4b0bfSAndy Whitcroft 	idx = vma_pagecache_offset(vma, address);
131704f2cbe3SMel Gorman 
131804f2cbe3SMel Gorman 	return find_lock_page(mapping, idx);
131904f2cbe3SMel Gorman }
132004f2cbe3SMel Gorman 
1321a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
13221e8f889bSDavid Gibson 			unsigned long address, pte_t *ptep, int write_access)
1323ac9b9c66SHugh Dickins {
1324ac9b9c66SHugh Dickins 	int ret = VM_FAULT_SIGBUS;
1325e7c4b0bfSAndy Whitcroft 	pgoff_t idx;
13264c887265SAdam Litke 	unsigned long size;
13274c887265SAdam Litke 	struct page *page;
13284c887265SAdam Litke 	struct address_space *mapping;
13291e8f889bSDavid Gibson 	pte_t new_pte;
13304c887265SAdam Litke 
133104f2cbe3SMel Gorman 	/*
133204f2cbe3SMel Gorman 	 * Currently, we are forced to kill the process in the event the
133304f2cbe3SMel Gorman 	 * original mapper has unmapped pages from the child due to a failed
133404f2cbe3SMel Gorman 	 * COW. Warn that such a situation has occured as it may not be obvious
133504f2cbe3SMel Gorman 	 */
133604f2cbe3SMel Gorman 	if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
133704f2cbe3SMel Gorman 		printk(KERN_WARNING
133804f2cbe3SMel Gorman 			"PID %d killed due to inadequate hugepage pool\n",
133904f2cbe3SMel Gorman 			current->pid);
134004f2cbe3SMel Gorman 		return ret;
134104f2cbe3SMel Gorman 	}
134204f2cbe3SMel Gorman 
13434c887265SAdam Litke 	mapping = vma->vm_file->f_mapping;
1344e7c4b0bfSAndy Whitcroft 	idx = vma_pagecache_offset(vma, address);
13454c887265SAdam Litke 
13464c887265SAdam Litke 	/*
13474c887265SAdam Litke 	 * Use page lock to guard against racing truncation
13484c887265SAdam Litke 	 * before we get page_table_lock.
13494c887265SAdam Litke 	 */
13506bda666aSChristoph Lameter retry:
13516bda666aSChristoph Lameter 	page = find_lock_page(mapping, idx);
13526bda666aSChristoph Lameter 	if (!page) {
1353ebed4bfcSHugh Dickins 		size = i_size_read(mapping->host) >> HPAGE_SHIFT;
1354ebed4bfcSHugh Dickins 		if (idx >= size)
1355ebed4bfcSHugh Dickins 			goto out;
135604f2cbe3SMel Gorman 		page = alloc_huge_page(vma, address, 0);
13572fc39cecSAdam Litke 		if (IS_ERR(page)) {
13582fc39cecSAdam Litke 			ret = -PTR_ERR(page);
13596bda666aSChristoph Lameter 			goto out;
13606bda666aSChristoph Lameter 		}
136179ac6ba4SDavid Gibson 		clear_huge_page(page, address);
13620ed361deSNick Piggin 		__SetPageUptodate(page);
1363ac9b9c66SHugh Dickins 
13646bda666aSChristoph Lameter 		if (vma->vm_flags & VM_SHARED) {
13656bda666aSChristoph Lameter 			int err;
136645c682a6SKen Chen 			struct inode *inode = mapping->host;
13676bda666aSChristoph Lameter 
13686bda666aSChristoph Lameter 			err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
13696bda666aSChristoph Lameter 			if (err) {
13706bda666aSChristoph Lameter 				put_page(page);
13716bda666aSChristoph Lameter 				if (err == -EEXIST)
13726bda666aSChristoph Lameter 					goto retry;
13736bda666aSChristoph Lameter 				goto out;
13746bda666aSChristoph Lameter 			}
137545c682a6SKen Chen 
137645c682a6SKen Chen 			spin_lock(&inode->i_lock);
137745c682a6SKen Chen 			inode->i_blocks += BLOCKS_PER_HUGEPAGE;
137845c682a6SKen Chen 			spin_unlock(&inode->i_lock);
13796bda666aSChristoph Lameter 		} else
13806bda666aSChristoph Lameter 			lock_page(page);
13816bda666aSChristoph Lameter 	}
13821e8f889bSDavid Gibson 
1383ac9b9c66SHugh Dickins 	spin_lock(&mm->page_table_lock);
13844c887265SAdam Litke 	size = i_size_read(mapping->host) >> HPAGE_SHIFT;
13854c887265SAdam Litke 	if (idx >= size)
13864c887265SAdam Litke 		goto backout;
13874c887265SAdam Litke 
138883c54070SNick Piggin 	ret = 0;
13897f2e9525SGerald Schaefer 	if (!huge_pte_none(huge_ptep_get(ptep)))
13904c887265SAdam Litke 		goto backout;
13914c887265SAdam Litke 
13921e8f889bSDavid Gibson 	new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
13931e8f889bSDavid Gibson 				&& (vma->vm_flags & VM_SHARED)));
13941e8f889bSDavid Gibson 	set_huge_pte_at(mm, address, ptep, new_pte);
13951e8f889bSDavid Gibson 
13961e8f889bSDavid Gibson 	if (write_access && !(vma->vm_flags & VM_SHARED)) {
13971e8f889bSDavid Gibson 		/* Optimization, do the COW without a second fault */
139804f2cbe3SMel Gorman 		ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
13991e8f889bSDavid Gibson 	}
14001e8f889bSDavid Gibson 
1401ac9b9c66SHugh Dickins 	spin_unlock(&mm->page_table_lock);
14024c887265SAdam Litke 	unlock_page(page);
14034c887265SAdam Litke out:
1404ac9b9c66SHugh Dickins 	return ret;
14054c887265SAdam Litke 
14064c887265SAdam Litke backout:
14074c887265SAdam Litke 	spin_unlock(&mm->page_table_lock);
14084c887265SAdam Litke 	unlock_page(page);
14094c887265SAdam Litke 	put_page(page);
14104c887265SAdam Litke 	goto out;
1411ac9b9c66SHugh Dickins }
1412ac9b9c66SHugh Dickins 
141386e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
141486e5216fSAdam Litke 			unsigned long address, int write_access)
141586e5216fSAdam Litke {
141686e5216fSAdam Litke 	pte_t *ptep;
141786e5216fSAdam Litke 	pte_t entry;
14181e8f889bSDavid Gibson 	int ret;
14193935baa9SDavid Gibson 	static DEFINE_MUTEX(hugetlb_instantiation_mutex);
142086e5216fSAdam Litke 
142186e5216fSAdam Litke 	ptep = huge_pte_alloc(mm, address);
142286e5216fSAdam Litke 	if (!ptep)
142386e5216fSAdam Litke 		return VM_FAULT_OOM;
142486e5216fSAdam Litke 
14253935baa9SDavid Gibson 	/*
14263935baa9SDavid Gibson 	 * Serialize hugepage allocation and instantiation, so that we don't
14273935baa9SDavid Gibson 	 * get spurious allocation failures if two CPUs race to instantiate
14283935baa9SDavid Gibson 	 * the same page in the page cache.
14293935baa9SDavid Gibson 	 */
14303935baa9SDavid Gibson 	mutex_lock(&hugetlb_instantiation_mutex);
14317f2e9525SGerald Schaefer 	entry = huge_ptep_get(ptep);
14327f2e9525SGerald Schaefer 	if (huge_pte_none(entry)) {
14333935baa9SDavid Gibson 		ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
14343935baa9SDavid Gibson 		mutex_unlock(&hugetlb_instantiation_mutex);
14353935baa9SDavid Gibson 		return ret;
14363935baa9SDavid Gibson 	}
143786e5216fSAdam Litke 
143883c54070SNick Piggin 	ret = 0;
14391e8f889bSDavid Gibson 
14401e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
14411e8f889bSDavid Gibson 	/* Check for a racing update before calling hugetlb_cow */
14427f2e9525SGerald Schaefer 	if (likely(pte_same(entry, huge_ptep_get(ptep))))
144304f2cbe3SMel Gorman 		if (write_access && !pte_write(entry)) {
144404f2cbe3SMel Gorman 			struct page *page;
144504f2cbe3SMel Gorman 			page = hugetlbfs_pagecache_page(vma, address);
144604f2cbe3SMel Gorman 			ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
144704f2cbe3SMel Gorman 			if (page) {
144804f2cbe3SMel Gorman 				unlock_page(page);
144904f2cbe3SMel Gorman 				put_page(page);
145004f2cbe3SMel Gorman 			}
145104f2cbe3SMel Gorman 		}
14521e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
14533935baa9SDavid Gibson 	mutex_unlock(&hugetlb_instantiation_mutex);
14541e8f889bSDavid Gibson 
14551e8f889bSDavid Gibson 	return ret;
145686e5216fSAdam Litke }
145786e5216fSAdam Litke 
145863551ae0SDavid Gibson int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
145963551ae0SDavid Gibson 			struct page **pages, struct vm_area_struct **vmas,
14605b23dbe8SAdam Litke 			unsigned long *position, int *length, int i,
14615b23dbe8SAdam Litke 			int write)
146263551ae0SDavid Gibson {
1463d5d4b0aaSChen, Kenneth W 	unsigned long pfn_offset;
1464d5d4b0aaSChen, Kenneth W 	unsigned long vaddr = *position;
146563551ae0SDavid Gibson 	int remainder = *length;
146663551ae0SDavid Gibson 
14671c59827dSHugh Dickins 	spin_lock(&mm->page_table_lock);
146863551ae0SDavid Gibson 	while (vaddr < vma->vm_end && remainder) {
146963551ae0SDavid Gibson 		pte_t *pte;
147063551ae0SDavid Gibson 		struct page *page;
147163551ae0SDavid Gibson 
14724c887265SAdam Litke 		/*
14734c887265SAdam Litke 		 * Some archs (sparc64, sh*) have multiple pte_ts to
14744c887265SAdam Litke 		 * each hugepage.  We have to make * sure we get the
14754c887265SAdam Litke 		 * first, for the page indexing below to work.
14764c887265SAdam Litke 		 */
147763551ae0SDavid Gibson 		pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
147863551ae0SDavid Gibson 
14797f2e9525SGerald Schaefer 		if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
14807f2e9525SGerald Schaefer 		    (write && !pte_write(huge_ptep_get(pte)))) {
14814c887265SAdam Litke 			int ret;
14824c887265SAdam Litke 
14834c887265SAdam Litke 			spin_unlock(&mm->page_table_lock);
14845b23dbe8SAdam Litke 			ret = hugetlb_fault(mm, vma, vaddr, write);
14854c887265SAdam Litke 			spin_lock(&mm->page_table_lock);
1486a89182c7SAdam Litke 			if (!(ret & VM_FAULT_ERROR))
14874c887265SAdam Litke 				continue;
14884c887265SAdam Litke 
14891c59827dSHugh Dickins 			remainder = 0;
14901c59827dSHugh Dickins 			if (!i)
14911c59827dSHugh Dickins 				i = -EFAULT;
14921c59827dSHugh Dickins 			break;
14931c59827dSHugh Dickins 		}
149463551ae0SDavid Gibson 
1495d5d4b0aaSChen, Kenneth W 		pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
14967f2e9525SGerald Schaefer 		page = pte_page(huge_ptep_get(pte));
1497d5d4b0aaSChen, Kenneth W same_page:
1498d6692183SChen, Kenneth W 		if (pages) {
149963551ae0SDavid Gibson 			get_page(page);
1500d5d4b0aaSChen, Kenneth W 			pages[i] = page + pfn_offset;
1501d6692183SChen, Kenneth W 		}
150263551ae0SDavid Gibson 
150363551ae0SDavid Gibson 		if (vmas)
150463551ae0SDavid Gibson 			vmas[i] = vma;
150563551ae0SDavid Gibson 
150663551ae0SDavid Gibson 		vaddr += PAGE_SIZE;
1507d5d4b0aaSChen, Kenneth W 		++pfn_offset;
150863551ae0SDavid Gibson 		--remainder;
150963551ae0SDavid Gibson 		++i;
1510d5d4b0aaSChen, Kenneth W 		if (vaddr < vma->vm_end && remainder &&
1511d5d4b0aaSChen, Kenneth W 				pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
1512d5d4b0aaSChen, Kenneth W 			/*
1513d5d4b0aaSChen, Kenneth W 			 * We use pfn_offset to avoid touching the pageframes
1514d5d4b0aaSChen, Kenneth W 			 * of this compound page.
1515d5d4b0aaSChen, Kenneth W 			 */
1516d5d4b0aaSChen, Kenneth W 			goto same_page;
1517d5d4b0aaSChen, Kenneth W 		}
151863551ae0SDavid Gibson 	}
15191c59827dSHugh Dickins 	spin_unlock(&mm->page_table_lock);
152063551ae0SDavid Gibson 	*length = remainder;
152163551ae0SDavid Gibson 	*position = vaddr;
152263551ae0SDavid Gibson 
152363551ae0SDavid Gibson 	return i;
152463551ae0SDavid Gibson }
15258f860591SZhang, Yanmin 
15268f860591SZhang, Yanmin void hugetlb_change_protection(struct vm_area_struct *vma,
15278f860591SZhang, Yanmin 		unsigned long address, unsigned long end, pgprot_t newprot)
15288f860591SZhang, Yanmin {
15298f860591SZhang, Yanmin 	struct mm_struct *mm = vma->vm_mm;
15308f860591SZhang, Yanmin 	unsigned long start = address;
15318f860591SZhang, Yanmin 	pte_t *ptep;
15328f860591SZhang, Yanmin 	pte_t pte;
15338f860591SZhang, Yanmin 
15348f860591SZhang, Yanmin 	BUG_ON(address >= end);
15358f860591SZhang, Yanmin 	flush_cache_range(vma, address, end);
15368f860591SZhang, Yanmin 
153739dde65cSChen, Kenneth W 	spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
15388f860591SZhang, Yanmin 	spin_lock(&mm->page_table_lock);
15398f860591SZhang, Yanmin 	for (; address < end; address += HPAGE_SIZE) {
15408f860591SZhang, Yanmin 		ptep = huge_pte_offset(mm, address);
15418f860591SZhang, Yanmin 		if (!ptep)
15428f860591SZhang, Yanmin 			continue;
154339dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
154439dde65cSChen, Kenneth W 			continue;
15457f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(ptep))) {
15468f860591SZhang, Yanmin 			pte = huge_ptep_get_and_clear(mm, address, ptep);
15478f860591SZhang, Yanmin 			pte = pte_mkhuge(pte_modify(pte, newprot));
15488f860591SZhang, Yanmin 			set_huge_pte_at(mm, address, ptep, pte);
15498f860591SZhang, Yanmin 		}
15508f860591SZhang, Yanmin 	}
15518f860591SZhang, Yanmin 	spin_unlock(&mm->page_table_lock);
155239dde65cSChen, Kenneth W 	spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
15538f860591SZhang, Yanmin 
15548f860591SZhang, Yanmin 	flush_tlb_range(vma, start, end);
15558f860591SZhang, Yanmin }
15568f860591SZhang, Yanmin 
1557a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode,
1558a1e78772SMel Gorman 					long from, long to,
1559a1e78772SMel Gorman 					struct vm_area_struct *vma)
1560e4e574b7SAdam Litke {
1561e4e574b7SAdam Litke 	long ret, chg;
1562e4e574b7SAdam Litke 
1563a1e78772SMel Gorman 	/*
1564a1e78772SMel Gorman 	 * Shared mappings base their reservation on the number of pages that
1565a1e78772SMel Gorman 	 * are already allocated on behalf of the file. Private mappings need
1566a1e78772SMel Gorman 	 * to reserve the full area even if read-only as mprotect() may be
1567a1e78772SMel Gorman 	 * called to make the mapping read-write. Assume !vma is a shm mapping
1568a1e78772SMel Gorman 	 */
1569a1e78772SMel Gorman 	if (!vma || vma->vm_flags & VM_SHARED)
1570e4e574b7SAdam Litke 		chg = region_chg(&inode->i_mapping->private_list, from, to);
1571a1e78772SMel Gorman 	else {
1572a1e78772SMel Gorman 		chg = to - from;
1573a1e78772SMel Gorman 		set_vma_resv_huge_pages(vma, chg);
157404f2cbe3SMel Gorman 		set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
1575a1e78772SMel Gorman 	}
1576a1e78772SMel Gorman 
1577e4e574b7SAdam Litke 	if (chg < 0)
1578e4e574b7SAdam Litke 		return chg;
15798a630112SKen Chen 
158090d8b7e6SAdam Litke 	if (hugetlb_get_quota(inode->i_mapping, chg))
158190d8b7e6SAdam Litke 		return -ENOSPC;
1582a43a8c39SChen, Kenneth W 	ret = hugetlb_acct_memory(chg);
158368842c9bSKen Chen 	if (ret < 0) {
158468842c9bSKen Chen 		hugetlb_put_quota(inode->i_mapping, chg);
1585a43a8c39SChen, Kenneth W 		return ret;
158668842c9bSKen Chen 	}
1587a1e78772SMel Gorman 	if (!vma || vma->vm_flags & VM_SHARED)
1588a43a8c39SChen, Kenneth W 		region_add(&inode->i_mapping->private_list, from, to);
1589a43a8c39SChen, Kenneth W 	return 0;
1590a43a8c39SChen, Kenneth W }
1591a43a8c39SChen, Kenneth W 
1592a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1593a43a8c39SChen, Kenneth W {
1594a43a8c39SChen, Kenneth W 	long chg = region_truncate(&inode->i_mapping->private_list, offset);
159545c682a6SKen Chen 
159645c682a6SKen Chen 	spin_lock(&inode->i_lock);
159745c682a6SKen Chen 	inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
159845c682a6SKen Chen 	spin_unlock(&inode->i_lock);
159945c682a6SKen Chen 
160090d8b7e6SAdam Litke 	hugetlb_put_quota(inode->i_mapping, (chg - freed));
160190d8b7e6SAdam Litke 	hugetlb_acct_memory(-(chg - freed));
1602a43a8c39SChen, Kenneth W }
1603