xref: /openbmc/linux/mm/hugetlb.c (revision ef009b25)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Generic hugetlb support.
31da177e4SLinus Torvalds  * (C) William Irwin, April 2004
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds #include <linux/list.h>
61da177e4SLinus Torvalds #include <linux/init.h>
71da177e4SLinus Torvalds #include <linux/module.h>
81da177e4SLinus Torvalds #include <linux/mm.h>
9e1759c21SAlexey Dobriyan #include <linux/seq_file.h>
101da177e4SLinus Torvalds #include <linux/sysctl.h>
111da177e4SLinus Torvalds #include <linux/highmem.h>
12cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
131da177e4SLinus Torvalds #include <linux/nodemask.h>
1463551ae0SDavid Gibson #include <linux/pagemap.h>
155da7ca86SChristoph Lameter #include <linux/mempolicy.h>
16aea47ff3SChristoph Lameter #include <linux/cpuset.h>
173935baa9SDavid Gibson #include <linux/mutex.h>
18aa888a74SAndi Kleen #include <linux/bootmem.h>
19a3437870SNishanth Aravamudan #include <linux/sysfs.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
210fe6e20bSNaoya Horiguchi #include <linux/rmap.h>
22fd6a03edSNaoya Horiguchi #include <linux/swap.h>
23fd6a03edSNaoya Horiguchi #include <linux/swapops.h>
24d6606683SLinus Torvalds 
2563551ae0SDavid Gibson #include <asm/page.h>
2663551ae0SDavid Gibson #include <asm/pgtable.h>
2732f84528SChris Forbes #include <linux/io.h>
2863551ae0SDavid Gibson 
2963551ae0SDavid Gibson #include <linux/hugetlb.h>
309a305230SLee Schermerhorn #include <linux/node.h>
317835e98bSNick Piggin #include "internal.h"
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
34396faf03SMel Gorman static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
35396faf03SMel Gorman unsigned long hugepages_treat_as_movable;
36a5516438SAndi Kleen 
37e5ff2159SAndi Kleen static int max_hstate;
38e5ff2159SAndi Kleen unsigned int default_hstate_idx;
39e5ff2159SAndi Kleen struct hstate hstates[HUGE_MAX_HSTATE];
40e5ff2159SAndi Kleen 
4153ba51d2SJon Tollefson __initdata LIST_HEAD(huge_boot_pages);
4253ba51d2SJon Tollefson 
43e5ff2159SAndi Kleen /* for command line parsing */
44e5ff2159SAndi Kleen static struct hstate * __initdata parsed_hstate;
45e5ff2159SAndi Kleen static unsigned long __initdata default_hstate_max_huge_pages;
46e11bfbfcSNick Piggin static unsigned long __initdata default_hstate_size;
47e5ff2159SAndi Kleen 
48e5ff2159SAndi Kleen #define for_each_hstate(h) \
49e5ff2159SAndi Kleen 	for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
50396faf03SMel Gorman 
513935baa9SDavid Gibson /*
523935baa9SDavid Gibson  * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
533935baa9SDavid Gibson  */
543935baa9SDavid Gibson static DEFINE_SPINLOCK(hugetlb_lock);
550bd0f9fbSEric Paris 
56e7c4b0bfSAndy Whitcroft /*
5796822904SAndy Whitcroft  * Region tracking -- allows tracking of reservations and instantiated pages
5896822904SAndy Whitcroft  *                    across the pages in a mapping.
5984afd99bSAndy Whitcroft  *
6084afd99bSAndy Whitcroft  * The region data structures are protected by a combination of the mmap_sem
6184afd99bSAndy Whitcroft  * and the hugetlb_instantion_mutex.  To access or modify a region the caller
6284afd99bSAndy Whitcroft  * must either hold the mmap_sem for write, or the mmap_sem for read and
6384afd99bSAndy Whitcroft  * the hugetlb_instantiation mutex:
6484afd99bSAndy Whitcroft  *
6584afd99bSAndy Whitcroft  *	down_write(&mm->mmap_sem);
6684afd99bSAndy Whitcroft  * or
6784afd99bSAndy Whitcroft  *	down_read(&mm->mmap_sem);
6884afd99bSAndy Whitcroft  *	mutex_lock(&hugetlb_instantiation_mutex);
6996822904SAndy Whitcroft  */
7096822904SAndy Whitcroft struct file_region {
7196822904SAndy Whitcroft 	struct list_head link;
7296822904SAndy Whitcroft 	long from;
7396822904SAndy Whitcroft 	long to;
7496822904SAndy Whitcroft };
7596822904SAndy Whitcroft 
7696822904SAndy Whitcroft static long region_add(struct list_head *head, long f, long t)
7796822904SAndy Whitcroft {
7896822904SAndy Whitcroft 	struct file_region *rg, *nrg, *trg;
7996822904SAndy Whitcroft 
8096822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
8196822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
8296822904SAndy Whitcroft 		if (f <= rg->to)
8396822904SAndy Whitcroft 			break;
8496822904SAndy Whitcroft 
8596822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
8696822904SAndy Whitcroft 	if (f > rg->from)
8796822904SAndy Whitcroft 		f = rg->from;
8896822904SAndy Whitcroft 
8996822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
9096822904SAndy Whitcroft 	nrg = rg;
9196822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
9296822904SAndy Whitcroft 		if (&rg->link == head)
9396822904SAndy Whitcroft 			break;
9496822904SAndy Whitcroft 		if (rg->from > t)
9596822904SAndy Whitcroft 			break;
9696822904SAndy Whitcroft 
9796822904SAndy Whitcroft 		/* If this area reaches higher then extend our area to
9896822904SAndy Whitcroft 		 * include it completely.  If this is not the first area
9996822904SAndy Whitcroft 		 * which we intend to reuse, free it. */
10096822904SAndy Whitcroft 		if (rg->to > t)
10196822904SAndy Whitcroft 			t = rg->to;
10296822904SAndy Whitcroft 		if (rg != nrg) {
10396822904SAndy Whitcroft 			list_del(&rg->link);
10496822904SAndy Whitcroft 			kfree(rg);
10596822904SAndy Whitcroft 		}
10696822904SAndy Whitcroft 	}
10796822904SAndy Whitcroft 	nrg->from = f;
10896822904SAndy Whitcroft 	nrg->to = t;
10996822904SAndy Whitcroft 	return 0;
11096822904SAndy Whitcroft }
11196822904SAndy Whitcroft 
11296822904SAndy Whitcroft static long region_chg(struct list_head *head, long f, long t)
11396822904SAndy Whitcroft {
11496822904SAndy Whitcroft 	struct file_region *rg, *nrg;
11596822904SAndy Whitcroft 	long chg = 0;
11696822904SAndy Whitcroft 
11796822904SAndy Whitcroft 	/* Locate the region we are before or in. */
11896822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
11996822904SAndy Whitcroft 		if (f <= rg->to)
12096822904SAndy Whitcroft 			break;
12196822904SAndy Whitcroft 
12296822904SAndy Whitcroft 	/* If we are below the current region then a new region is required.
12396822904SAndy Whitcroft 	 * Subtle, allocate a new region at the position but make it zero
12496822904SAndy Whitcroft 	 * size such that we can guarantee to record the reservation. */
12596822904SAndy Whitcroft 	if (&rg->link == head || t < rg->from) {
12696822904SAndy Whitcroft 		nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
12796822904SAndy Whitcroft 		if (!nrg)
12896822904SAndy Whitcroft 			return -ENOMEM;
12996822904SAndy Whitcroft 		nrg->from = f;
13096822904SAndy Whitcroft 		nrg->to   = f;
13196822904SAndy Whitcroft 		INIT_LIST_HEAD(&nrg->link);
13296822904SAndy Whitcroft 		list_add(&nrg->link, rg->link.prev);
13396822904SAndy Whitcroft 
13496822904SAndy Whitcroft 		return t - f;
13596822904SAndy Whitcroft 	}
13696822904SAndy Whitcroft 
13796822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
13896822904SAndy Whitcroft 	if (f > rg->from)
13996822904SAndy Whitcroft 		f = rg->from;
14096822904SAndy Whitcroft 	chg = t - f;
14196822904SAndy Whitcroft 
14296822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
14396822904SAndy Whitcroft 	list_for_each_entry(rg, rg->link.prev, link) {
14496822904SAndy Whitcroft 		if (&rg->link == head)
14596822904SAndy Whitcroft 			break;
14696822904SAndy Whitcroft 		if (rg->from > t)
14796822904SAndy Whitcroft 			return chg;
14896822904SAndy Whitcroft 
14925985edcSLucas De Marchi 		/* We overlap with this area, if it extends further than
15096822904SAndy Whitcroft 		 * us then we must extend ourselves.  Account for its
15196822904SAndy Whitcroft 		 * existing reservation. */
15296822904SAndy Whitcroft 		if (rg->to > t) {
15396822904SAndy Whitcroft 			chg += rg->to - t;
15496822904SAndy Whitcroft 			t = rg->to;
15596822904SAndy Whitcroft 		}
15696822904SAndy Whitcroft 		chg -= rg->to - rg->from;
15796822904SAndy Whitcroft 	}
15896822904SAndy Whitcroft 	return chg;
15996822904SAndy Whitcroft }
16096822904SAndy Whitcroft 
16196822904SAndy Whitcroft static long region_truncate(struct list_head *head, long end)
16296822904SAndy Whitcroft {
16396822904SAndy Whitcroft 	struct file_region *rg, *trg;
16496822904SAndy Whitcroft 	long chg = 0;
16596822904SAndy Whitcroft 
16696822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
16796822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
16896822904SAndy Whitcroft 		if (end <= rg->to)
16996822904SAndy Whitcroft 			break;
17096822904SAndy Whitcroft 	if (&rg->link == head)
17196822904SAndy Whitcroft 		return 0;
17296822904SAndy Whitcroft 
17396822904SAndy Whitcroft 	/* If we are in the middle of a region then adjust it. */
17496822904SAndy Whitcroft 	if (end > rg->from) {
17596822904SAndy Whitcroft 		chg = rg->to - end;
17696822904SAndy Whitcroft 		rg->to = end;
17796822904SAndy Whitcroft 		rg = list_entry(rg->link.next, typeof(*rg), link);
17896822904SAndy Whitcroft 	}
17996822904SAndy Whitcroft 
18096822904SAndy Whitcroft 	/* Drop any remaining regions. */
18196822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
18296822904SAndy Whitcroft 		if (&rg->link == head)
18396822904SAndy Whitcroft 			break;
18496822904SAndy Whitcroft 		chg += rg->to - rg->from;
18596822904SAndy Whitcroft 		list_del(&rg->link);
18696822904SAndy Whitcroft 		kfree(rg);
18796822904SAndy Whitcroft 	}
18896822904SAndy Whitcroft 	return chg;
18996822904SAndy Whitcroft }
19096822904SAndy Whitcroft 
19184afd99bSAndy Whitcroft static long region_count(struct list_head *head, long f, long t)
19284afd99bSAndy Whitcroft {
19384afd99bSAndy Whitcroft 	struct file_region *rg;
19484afd99bSAndy Whitcroft 	long chg = 0;
19584afd99bSAndy Whitcroft 
19684afd99bSAndy Whitcroft 	/* Locate each segment we overlap with, and count that overlap. */
19784afd99bSAndy Whitcroft 	list_for_each_entry(rg, head, link) {
19884afd99bSAndy Whitcroft 		int seg_from;
19984afd99bSAndy Whitcroft 		int seg_to;
20084afd99bSAndy Whitcroft 
20184afd99bSAndy Whitcroft 		if (rg->to <= f)
20284afd99bSAndy Whitcroft 			continue;
20384afd99bSAndy Whitcroft 		if (rg->from >= t)
20484afd99bSAndy Whitcroft 			break;
20584afd99bSAndy Whitcroft 
20684afd99bSAndy Whitcroft 		seg_from = max(rg->from, f);
20784afd99bSAndy Whitcroft 		seg_to = min(rg->to, t);
20884afd99bSAndy Whitcroft 
20984afd99bSAndy Whitcroft 		chg += seg_to - seg_from;
21084afd99bSAndy Whitcroft 	}
21184afd99bSAndy Whitcroft 
21284afd99bSAndy Whitcroft 	return chg;
21384afd99bSAndy Whitcroft }
21484afd99bSAndy Whitcroft 
21596822904SAndy Whitcroft /*
216e7c4b0bfSAndy Whitcroft  * Convert the address within this vma to the page offset within
217e7c4b0bfSAndy Whitcroft  * the mapping, in pagecache page units; huge pages here.
218e7c4b0bfSAndy Whitcroft  */
219a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h,
220a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long address)
221e7c4b0bfSAndy Whitcroft {
222a5516438SAndi Kleen 	return ((address - vma->vm_start) >> huge_page_shift(h)) +
223a5516438SAndi Kleen 			(vma->vm_pgoff >> huge_page_order(h));
224e7c4b0bfSAndy Whitcroft }
225e7c4b0bfSAndy Whitcroft 
2260fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
2270fe6e20bSNaoya Horiguchi 				     unsigned long address)
2280fe6e20bSNaoya Horiguchi {
2290fe6e20bSNaoya Horiguchi 	return vma_hugecache_offset(hstate_vma(vma), vma, address);
2300fe6e20bSNaoya Horiguchi }
2310fe6e20bSNaoya Horiguchi 
23284afd99bSAndy Whitcroft /*
23308fba699SMel Gorman  * Return the size of the pages allocated when backing a VMA. In the majority
23408fba699SMel Gorman  * cases this will be same size as used by the page table entries.
23508fba699SMel Gorman  */
23608fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
23708fba699SMel Gorman {
23808fba699SMel Gorman 	struct hstate *hstate;
23908fba699SMel Gorman 
24008fba699SMel Gorman 	if (!is_vm_hugetlb_page(vma))
24108fba699SMel Gorman 		return PAGE_SIZE;
24208fba699SMel Gorman 
24308fba699SMel Gorman 	hstate = hstate_vma(vma);
24408fba699SMel Gorman 
24508fba699SMel Gorman 	return 1UL << (hstate->order + PAGE_SHIFT);
24608fba699SMel Gorman }
247f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
24808fba699SMel Gorman 
24908fba699SMel Gorman /*
2503340289dSMel Gorman  * Return the page size being used by the MMU to back a VMA. In the majority
2513340289dSMel Gorman  * of cases, the page size used by the kernel matches the MMU size. On
2523340289dSMel Gorman  * architectures where it differs, an architecture-specific version of this
2533340289dSMel Gorman  * function is required.
2543340289dSMel Gorman  */
2553340289dSMel Gorman #ifndef vma_mmu_pagesize
2563340289dSMel Gorman unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
2573340289dSMel Gorman {
2583340289dSMel Gorman 	return vma_kernel_pagesize(vma);
2593340289dSMel Gorman }
2603340289dSMel Gorman #endif
2613340289dSMel Gorman 
2623340289dSMel Gorman /*
26384afd99bSAndy Whitcroft  * Flags for MAP_PRIVATE reservations.  These are stored in the bottom
26484afd99bSAndy Whitcroft  * bits of the reservation map pointer, which are always clear due to
26584afd99bSAndy Whitcroft  * alignment.
26684afd99bSAndy Whitcroft  */
26784afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER    (1UL << 0)
26884afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1)
26904f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
27084afd99bSAndy Whitcroft 
271a1e78772SMel Gorman /*
272a1e78772SMel Gorman  * These helpers are used to track how many pages are reserved for
273a1e78772SMel Gorman  * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
274a1e78772SMel Gorman  * is guaranteed to have their future faults succeed.
275a1e78772SMel Gorman  *
276a1e78772SMel Gorman  * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
277a1e78772SMel Gorman  * the reserve counters are updated with the hugetlb_lock held. It is safe
278a1e78772SMel Gorman  * to reset the VMA at fork() time as it is not in use yet and there is no
279a1e78772SMel Gorman  * chance of the global counters getting corrupted as a result of the values.
28084afd99bSAndy Whitcroft  *
28184afd99bSAndy Whitcroft  * The private mapping reservation is represented in a subtly different
28284afd99bSAndy Whitcroft  * manner to a shared mapping.  A shared mapping has a region map associated
28384afd99bSAndy Whitcroft  * with the underlying file, this region map represents the backing file
28484afd99bSAndy Whitcroft  * pages which have ever had a reservation assigned which this persists even
28584afd99bSAndy Whitcroft  * after the page is instantiated.  A private mapping has a region map
28684afd99bSAndy Whitcroft  * associated with the original mmap which is attached to all VMAs which
28784afd99bSAndy Whitcroft  * reference it, this region map represents those offsets which have consumed
28884afd99bSAndy Whitcroft  * reservation ie. where pages have been instantiated.
289a1e78772SMel Gorman  */
290e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma)
291e7c4b0bfSAndy Whitcroft {
292e7c4b0bfSAndy Whitcroft 	return (unsigned long)vma->vm_private_data;
293e7c4b0bfSAndy Whitcroft }
294e7c4b0bfSAndy Whitcroft 
295e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma,
296e7c4b0bfSAndy Whitcroft 							unsigned long value)
297e7c4b0bfSAndy Whitcroft {
298e7c4b0bfSAndy Whitcroft 	vma->vm_private_data = (void *)value;
299e7c4b0bfSAndy Whitcroft }
300e7c4b0bfSAndy Whitcroft 
30184afd99bSAndy Whitcroft struct resv_map {
30284afd99bSAndy Whitcroft 	struct kref refs;
30384afd99bSAndy Whitcroft 	struct list_head regions;
30484afd99bSAndy Whitcroft };
30584afd99bSAndy Whitcroft 
3062a4b3dedSHarvey Harrison static struct resv_map *resv_map_alloc(void)
30784afd99bSAndy Whitcroft {
30884afd99bSAndy Whitcroft 	struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
30984afd99bSAndy Whitcroft 	if (!resv_map)
31084afd99bSAndy Whitcroft 		return NULL;
31184afd99bSAndy Whitcroft 
31284afd99bSAndy Whitcroft 	kref_init(&resv_map->refs);
31384afd99bSAndy Whitcroft 	INIT_LIST_HEAD(&resv_map->regions);
31484afd99bSAndy Whitcroft 
31584afd99bSAndy Whitcroft 	return resv_map;
31684afd99bSAndy Whitcroft }
31784afd99bSAndy Whitcroft 
3182a4b3dedSHarvey Harrison static void resv_map_release(struct kref *ref)
31984afd99bSAndy Whitcroft {
32084afd99bSAndy Whitcroft 	struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
32184afd99bSAndy Whitcroft 
32284afd99bSAndy Whitcroft 	/* Clear out any active regions before we release the map. */
32384afd99bSAndy Whitcroft 	region_truncate(&resv_map->regions, 0);
32484afd99bSAndy Whitcroft 	kfree(resv_map);
32584afd99bSAndy Whitcroft }
32684afd99bSAndy Whitcroft 
32784afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
328a1e78772SMel Gorman {
329a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
330f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE))
33184afd99bSAndy Whitcroft 		return (struct resv_map *)(get_vma_private_data(vma) &
33284afd99bSAndy Whitcroft 							~HPAGE_RESV_MASK);
3332a4b3dedSHarvey Harrison 	return NULL;
334a1e78772SMel Gorman }
335a1e78772SMel Gorman 
33684afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
337a1e78772SMel Gorman {
338a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
339f83a275dSMel Gorman 	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
340a1e78772SMel Gorman 
34184afd99bSAndy Whitcroft 	set_vma_private_data(vma, (get_vma_private_data(vma) &
34284afd99bSAndy Whitcroft 				HPAGE_RESV_MASK) | (unsigned long)map);
34304f2cbe3SMel Gorman }
34404f2cbe3SMel Gorman 
34504f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
34604f2cbe3SMel Gorman {
34704f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
348f83a275dSMel Gorman 	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
349e7c4b0bfSAndy Whitcroft 
350e7c4b0bfSAndy Whitcroft 	set_vma_private_data(vma, get_vma_private_data(vma) | flags);
35104f2cbe3SMel Gorman }
35204f2cbe3SMel Gorman 
35304f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
35404f2cbe3SMel Gorman {
35504f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
356e7c4b0bfSAndy Whitcroft 
357e7c4b0bfSAndy Whitcroft 	return (get_vma_private_data(vma) & flag) != 0;
358a1e78772SMel Gorman }
359a1e78772SMel Gorman 
360a1e78772SMel Gorman /* Decrement the reserved pages in the hugepage pool by one */
361a5516438SAndi Kleen static void decrement_hugepage_resv_vma(struct hstate *h,
362a5516438SAndi Kleen 			struct vm_area_struct *vma)
363a1e78772SMel Gorman {
364c37f9fb1SAndy Whitcroft 	if (vma->vm_flags & VM_NORESERVE)
365c37f9fb1SAndy Whitcroft 		return;
366c37f9fb1SAndy Whitcroft 
367f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE) {
368a1e78772SMel Gorman 		/* Shared mappings always use reserves */
369a5516438SAndi Kleen 		h->resv_huge_pages--;
37084afd99bSAndy Whitcroft 	} else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
371a1e78772SMel Gorman 		/*
372a1e78772SMel Gorman 		 * Only the process that called mmap() has reserves for
373a1e78772SMel Gorman 		 * private mappings.
374a1e78772SMel Gorman 		 */
375a5516438SAndi Kleen 		h->resv_huge_pages--;
376a1e78772SMel Gorman 	}
377a1e78772SMel Gorman }
378a1e78772SMel Gorman 
37904f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
380a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
381a1e78772SMel Gorman {
382a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
383f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE))
384a1e78772SMel Gorman 		vma->vm_private_data = (void *)0;
385a1e78772SMel Gorman }
386a1e78772SMel Gorman 
387a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */
3887f09ca51SMel Gorman static int vma_has_reserves(struct vm_area_struct *vma)
389a1e78772SMel Gorman {
390f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE)
391a1e78772SMel Gorman 		return 1;
3927f09ca51SMel Gorman 	if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
3937f09ca51SMel Gorman 		return 1;
3947f09ca51SMel Gorman 	return 0;
395a1e78772SMel Gorman }
396a1e78772SMel Gorman 
3970ebabb41SNaoya Horiguchi static void copy_gigantic_page(struct page *dst, struct page *src)
3980ebabb41SNaoya Horiguchi {
3990ebabb41SNaoya Horiguchi 	int i;
4000ebabb41SNaoya Horiguchi 	struct hstate *h = page_hstate(src);
4010ebabb41SNaoya Horiguchi 	struct page *dst_base = dst;
4020ebabb41SNaoya Horiguchi 	struct page *src_base = src;
4030ebabb41SNaoya Horiguchi 
4040ebabb41SNaoya Horiguchi 	for (i = 0; i < pages_per_huge_page(h); ) {
4050ebabb41SNaoya Horiguchi 		cond_resched();
4060ebabb41SNaoya Horiguchi 		copy_highpage(dst, src);
4070ebabb41SNaoya Horiguchi 
4080ebabb41SNaoya Horiguchi 		i++;
4090ebabb41SNaoya Horiguchi 		dst = mem_map_next(dst, dst_base, i);
4100ebabb41SNaoya Horiguchi 		src = mem_map_next(src, src_base, i);
4110ebabb41SNaoya Horiguchi 	}
4120ebabb41SNaoya Horiguchi }
4130ebabb41SNaoya Horiguchi 
4140ebabb41SNaoya Horiguchi void copy_huge_page(struct page *dst, struct page *src)
4150ebabb41SNaoya Horiguchi {
4160ebabb41SNaoya Horiguchi 	int i;
4170ebabb41SNaoya Horiguchi 	struct hstate *h = page_hstate(src);
4180ebabb41SNaoya Horiguchi 
4190ebabb41SNaoya Horiguchi 	if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) {
4200ebabb41SNaoya Horiguchi 		copy_gigantic_page(dst, src);
4210ebabb41SNaoya Horiguchi 		return;
4220ebabb41SNaoya Horiguchi 	}
4230ebabb41SNaoya Horiguchi 
4240ebabb41SNaoya Horiguchi 	might_sleep();
4250ebabb41SNaoya Horiguchi 	for (i = 0; i < pages_per_huge_page(h); i++) {
4260ebabb41SNaoya Horiguchi 		cond_resched();
4270ebabb41SNaoya Horiguchi 		copy_highpage(dst + i, src + i);
4280ebabb41SNaoya Horiguchi 	}
4290ebabb41SNaoya Horiguchi }
4300ebabb41SNaoya Horiguchi 
431a5516438SAndi Kleen static void enqueue_huge_page(struct hstate *h, struct page *page)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	int nid = page_to_nid(page);
434a5516438SAndi Kleen 	list_add(&page->lru, &h->hugepage_freelists[nid]);
435a5516438SAndi Kleen 	h->free_huge_pages++;
436a5516438SAndi Kleen 	h->free_huge_pages_node[nid]++;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
439bf50bab2SNaoya Horiguchi static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
440bf50bab2SNaoya Horiguchi {
441bf50bab2SNaoya Horiguchi 	struct page *page;
442bf50bab2SNaoya Horiguchi 
443bf50bab2SNaoya Horiguchi 	if (list_empty(&h->hugepage_freelists[nid]))
444bf50bab2SNaoya Horiguchi 		return NULL;
445bf50bab2SNaoya Horiguchi 	page = list_entry(h->hugepage_freelists[nid].next, struct page, lru);
446bf50bab2SNaoya Horiguchi 	list_del(&page->lru);
447a9869b83SNaoya Horiguchi 	set_page_refcounted(page);
448bf50bab2SNaoya Horiguchi 	h->free_huge_pages--;
449bf50bab2SNaoya Horiguchi 	h->free_huge_pages_node[nid]--;
450bf50bab2SNaoya Horiguchi 	return page;
451bf50bab2SNaoya Horiguchi }
452bf50bab2SNaoya Horiguchi 
453a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h,
454a5516438SAndi Kleen 				struct vm_area_struct *vma,
45504f2cbe3SMel Gorman 				unsigned long address, int avoid_reserve)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 	struct page *page = NULL;
458480eccf9SLee Schermerhorn 	struct mempolicy *mpol;
45919770b32SMel Gorman 	nodemask_t *nodemask;
460c0ff7453SMiao Xie 	struct zonelist *zonelist;
461dd1a239fSMel Gorman 	struct zone *zone;
462dd1a239fSMel Gorman 	struct zoneref *z;
4631da177e4SLinus Torvalds 
464c0ff7453SMiao Xie 	get_mems_allowed();
465c0ff7453SMiao Xie 	zonelist = huge_zonelist(vma, address,
466c0ff7453SMiao Xie 					htlb_alloc_mask, &mpol, &nodemask);
467a1e78772SMel Gorman 	/*
468a1e78772SMel Gorman 	 * A child process with MAP_PRIVATE mappings created by their parent
469a1e78772SMel Gorman 	 * have no page reserves. This check ensures that reservations are
470a1e78772SMel Gorman 	 * not "stolen". The child may still get SIGKILLed
471a1e78772SMel Gorman 	 */
4727f09ca51SMel Gorman 	if (!vma_has_reserves(vma) &&
473a5516438SAndi Kleen 			h->free_huge_pages - h->resv_huge_pages == 0)
474c0ff7453SMiao Xie 		goto err;
475a1e78772SMel Gorman 
47604f2cbe3SMel Gorman 	/* If reserves cannot be used, ensure enough pages are in the pool */
477a5516438SAndi Kleen 	if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
4786eab04a8SJustin P. Mattock 		goto err;
47904f2cbe3SMel Gorman 
48019770b32SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
48119770b32SMel Gorman 						MAX_NR_ZONES - 1, nodemask) {
482bf50bab2SNaoya Horiguchi 		if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask)) {
483bf50bab2SNaoya Horiguchi 			page = dequeue_huge_page_node(h, zone_to_nid(zone));
484bf50bab2SNaoya Horiguchi 			if (page) {
48504f2cbe3SMel Gorman 				if (!avoid_reserve)
486a5516438SAndi Kleen 					decrement_hugepage_resv_vma(h, vma);
4875ab3ee7bSKen Chen 				break;
4881da177e4SLinus Torvalds 			}
4893abf7afdSAndrew Morton 		}
490bf50bab2SNaoya Horiguchi 	}
491c0ff7453SMiao Xie err:
49252cd3b07SLee Schermerhorn 	mpol_cond_put(mpol);
493c0ff7453SMiao Xie 	put_mems_allowed();
4941da177e4SLinus Torvalds 	return page;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
497a5516438SAndi Kleen static void update_and_free_page(struct hstate *h, struct page *page)
4986af2acb6SAdam Litke {
4996af2acb6SAdam Litke 	int i;
500a5516438SAndi Kleen 
50118229df5SAndy Whitcroft 	VM_BUG_ON(h->order >= MAX_ORDER);
50218229df5SAndy Whitcroft 
503a5516438SAndi Kleen 	h->nr_huge_pages--;
504a5516438SAndi Kleen 	h->nr_huge_pages_node[page_to_nid(page)]--;
505a5516438SAndi Kleen 	for (i = 0; i < pages_per_huge_page(h); i++) {
50632f84528SChris Forbes 		page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
50732f84528SChris Forbes 				1 << PG_referenced | 1 << PG_dirty |
50832f84528SChris Forbes 				1 << PG_active | 1 << PG_reserved |
5096af2acb6SAdam Litke 				1 << PG_private | 1 << PG_writeback);
5106af2acb6SAdam Litke 	}
5116af2acb6SAdam Litke 	set_compound_page_dtor(page, NULL);
5126af2acb6SAdam Litke 	set_page_refcounted(page);
5137f2e9525SGerald Schaefer 	arch_release_hugepage(page);
514a5516438SAndi Kleen 	__free_pages(page, huge_page_order(h));
5156af2acb6SAdam Litke }
5166af2acb6SAdam Litke 
517e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size)
518e5ff2159SAndi Kleen {
519e5ff2159SAndi Kleen 	struct hstate *h;
520e5ff2159SAndi Kleen 
521e5ff2159SAndi Kleen 	for_each_hstate(h) {
522e5ff2159SAndi Kleen 		if (huge_page_size(h) == size)
523e5ff2159SAndi Kleen 			return h;
524e5ff2159SAndi Kleen 	}
525e5ff2159SAndi Kleen 	return NULL;
526e5ff2159SAndi Kleen }
527e5ff2159SAndi Kleen 
52827a85ef1SDavid Gibson static void free_huge_page(struct page *page)
52927a85ef1SDavid Gibson {
530a5516438SAndi Kleen 	/*
531a5516438SAndi Kleen 	 * Can't pass hstate in here because it is called from the
532a5516438SAndi Kleen 	 * compound page destructor.
533a5516438SAndi Kleen 	 */
534e5ff2159SAndi Kleen 	struct hstate *h = page_hstate(page);
5357893d1d5SAdam Litke 	int nid = page_to_nid(page);
536c79fb75eSAdam Litke 	struct address_space *mapping;
53727a85ef1SDavid Gibson 
538c79fb75eSAdam Litke 	mapping = (struct address_space *) page_private(page);
539e5df70abSAndy Whitcroft 	set_page_private(page, 0);
54023be7468SMel Gorman 	page->mapping = NULL;
5417893d1d5SAdam Litke 	BUG_ON(page_count(page));
5420fe6e20bSNaoya Horiguchi 	BUG_ON(page_mapcount(page));
54327a85ef1SDavid Gibson 	INIT_LIST_HEAD(&page->lru);
54427a85ef1SDavid Gibson 
54527a85ef1SDavid Gibson 	spin_lock(&hugetlb_lock);
546aa888a74SAndi Kleen 	if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
547a5516438SAndi Kleen 		update_and_free_page(h, page);
548a5516438SAndi Kleen 		h->surplus_huge_pages--;
549a5516438SAndi Kleen 		h->surplus_huge_pages_node[nid]--;
5507893d1d5SAdam Litke 	} else {
551a5516438SAndi Kleen 		enqueue_huge_page(h, page);
5527893d1d5SAdam Litke 	}
55327a85ef1SDavid Gibson 	spin_unlock(&hugetlb_lock);
554c79fb75eSAdam Litke 	if (mapping)
5559a119c05SAdam Litke 		hugetlb_put_quota(mapping, 1);
55627a85ef1SDavid Gibson }
55727a85ef1SDavid Gibson 
558a5516438SAndi Kleen static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
559b7ba30c6SAndi Kleen {
560b7ba30c6SAndi Kleen 	set_compound_page_dtor(page, free_huge_page);
561b7ba30c6SAndi Kleen 	spin_lock(&hugetlb_lock);
562a5516438SAndi Kleen 	h->nr_huge_pages++;
563a5516438SAndi Kleen 	h->nr_huge_pages_node[nid]++;
564b7ba30c6SAndi Kleen 	spin_unlock(&hugetlb_lock);
565b7ba30c6SAndi Kleen 	put_page(page); /* free it into the hugepage allocator */
566b7ba30c6SAndi Kleen }
567b7ba30c6SAndi Kleen 
56820a0307cSWu Fengguang static void prep_compound_gigantic_page(struct page *page, unsigned long order)
56920a0307cSWu Fengguang {
57020a0307cSWu Fengguang 	int i;
57120a0307cSWu Fengguang 	int nr_pages = 1 << order;
57220a0307cSWu Fengguang 	struct page *p = page + 1;
57320a0307cSWu Fengguang 
57420a0307cSWu Fengguang 	/* we rely on prep_new_huge_page to set the destructor */
57520a0307cSWu Fengguang 	set_compound_order(page, order);
57620a0307cSWu Fengguang 	__SetPageHead(page);
57720a0307cSWu Fengguang 	for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
57820a0307cSWu Fengguang 		__SetPageTail(p);
57958a84aa9SYouquan Song 		set_page_count(p, 0);
58020a0307cSWu Fengguang 		p->first_page = page;
58120a0307cSWu Fengguang 	}
58220a0307cSWu Fengguang }
58320a0307cSWu Fengguang 
58420a0307cSWu Fengguang int PageHuge(struct page *page)
58520a0307cSWu Fengguang {
58620a0307cSWu Fengguang 	compound_page_dtor *dtor;
58720a0307cSWu Fengguang 
58820a0307cSWu Fengguang 	if (!PageCompound(page))
58920a0307cSWu Fengguang 		return 0;
59020a0307cSWu Fengguang 
59120a0307cSWu Fengguang 	page = compound_head(page);
59220a0307cSWu Fengguang 	dtor = get_compound_page_dtor(page);
59320a0307cSWu Fengguang 
59420a0307cSWu Fengguang 	return dtor == free_huge_page;
59520a0307cSWu Fengguang }
59643131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge);
59743131e14SNaoya Horiguchi 
598a5516438SAndi Kleen static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds 	struct page *page;
601f96efd58SJoe Jin 
602aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
603aa888a74SAndi Kleen 		return NULL;
604aa888a74SAndi Kleen 
6056484eb3eSMel Gorman 	page = alloc_pages_exact_node(nid,
606551883aeSNishanth Aravamudan 		htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
607551883aeSNishanth Aravamudan 						__GFP_REPEAT|__GFP_NOWARN,
608a5516438SAndi Kleen 		huge_page_order(h));
6091da177e4SLinus Torvalds 	if (page) {
6107f2e9525SGerald Schaefer 		if (arch_prepare_hugepage(page)) {
611caff3a2cSGerald Schaefer 			__free_pages(page, huge_page_order(h));
6127b8ee84dSHarvey Harrison 			return NULL;
6137f2e9525SGerald Schaefer 		}
614a5516438SAndi Kleen 		prep_new_huge_page(h, page, nid);
6151da177e4SLinus Torvalds 	}
61663b4613cSNishanth Aravamudan 
61763b4613cSNishanth Aravamudan 	return page;
61863b4613cSNishanth Aravamudan }
61963b4613cSNishanth Aravamudan 
6205ced66c9SAndi Kleen /*
6216ae11b27SLee Schermerhorn  * common helper functions for hstate_next_node_to_{alloc|free}.
6226ae11b27SLee Schermerhorn  * We may have allocated or freed a huge page based on a different
6236ae11b27SLee Schermerhorn  * nodes_allowed previously, so h->next_node_to_{alloc|free} might
6246ae11b27SLee Schermerhorn  * be outside of *nodes_allowed.  Ensure that we use an allowed
6256ae11b27SLee Schermerhorn  * node for alloc or free.
6269a76db09SLee Schermerhorn  */
6276ae11b27SLee Schermerhorn static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
6289a76db09SLee Schermerhorn {
6296ae11b27SLee Schermerhorn 	nid = next_node(nid, *nodes_allowed);
6309a76db09SLee Schermerhorn 	if (nid == MAX_NUMNODES)
6316ae11b27SLee Schermerhorn 		nid = first_node(*nodes_allowed);
6329a76db09SLee Schermerhorn 	VM_BUG_ON(nid >= MAX_NUMNODES);
6339a76db09SLee Schermerhorn 
6349a76db09SLee Schermerhorn 	return nid;
6359a76db09SLee Schermerhorn }
6369a76db09SLee Schermerhorn 
6376ae11b27SLee Schermerhorn static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
6385ced66c9SAndi Kleen {
6396ae11b27SLee Schermerhorn 	if (!node_isset(nid, *nodes_allowed))
6406ae11b27SLee Schermerhorn 		nid = next_node_allowed(nid, nodes_allowed);
6419a76db09SLee Schermerhorn 	return nid;
6425ced66c9SAndi Kleen }
6435ced66c9SAndi Kleen 
6446ae11b27SLee Schermerhorn /*
6456ae11b27SLee Schermerhorn  * returns the previously saved node ["this node"] from which to
6466ae11b27SLee Schermerhorn  * allocate a persistent huge page for the pool and advance the
6476ae11b27SLee Schermerhorn  * next node from which to allocate, handling wrap at end of node
6486ae11b27SLee Schermerhorn  * mask.
6496ae11b27SLee Schermerhorn  */
6506ae11b27SLee Schermerhorn static int hstate_next_node_to_alloc(struct hstate *h,
6516ae11b27SLee Schermerhorn 					nodemask_t *nodes_allowed)
6526ae11b27SLee Schermerhorn {
6536ae11b27SLee Schermerhorn 	int nid;
6546ae11b27SLee Schermerhorn 
6556ae11b27SLee Schermerhorn 	VM_BUG_ON(!nodes_allowed);
6566ae11b27SLee Schermerhorn 
6576ae11b27SLee Schermerhorn 	nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
6586ae11b27SLee Schermerhorn 	h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
6596ae11b27SLee Schermerhorn 
6606ae11b27SLee Schermerhorn 	return nid;
6616ae11b27SLee Schermerhorn }
6626ae11b27SLee Schermerhorn 
6636ae11b27SLee Schermerhorn static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
66463b4613cSNishanth Aravamudan {
66563b4613cSNishanth Aravamudan 	struct page *page;
66663b4613cSNishanth Aravamudan 	int start_nid;
66763b4613cSNishanth Aravamudan 	int next_nid;
66863b4613cSNishanth Aravamudan 	int ret = 0;
66963b4613cSNishanth Aravamudan 
6706ae11b27SLee Schermerhorn 	start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
671e8c5c824SLee Schermerhorn 	next_nid = start_nid;
67263b4613cSNishanth Aravamudan 
67363b4613cSNishanth Aravamudan 	do {
674e8c5c824SLee Schermerhorn 		page = alloc_fresh_huge_page_node(h, next_nid);
6759a76db09SLee Schermerhorn 		if (page) {
67663b4613cSNishanth Aravamudan 			ret = 1;
6779a76db09SLee Schermerhorn 			break;
6789a76db09SLee Schermerhorn 		}
6796ae11b27SLee Schermerhorn 		next_nid = hstate_next_node_to_alloc(h, nodes_allowed);
6809a76db09SLee Schermerhorn 	} while (next_nid != start_nid);
68163b4613cSNishanth Aravamudan 
6823b116300SAdam Litke 	if (ret)
6833b116300SAdam Litke 		count_vm_event(HTLB_BUDDY_PGALLOC);
6843b116300SAdam Litke 	else
6853b116300SAdam Litke 		count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
6863b116300SAdam Litke 
68763b4613cSNishanth Aravamudan 	return ret;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
690e8c5c824SLee Schermerhorn /*
6916ae11b27SLee Schermerhorn  * helper for free_pool_huge_page() - return the previously saved
6926ae11b27SLee Schermerhorn  * node ["this node"] from which to free a huge page.  Advance the
6936ae11b27SLee Schermerhorn  * next node id whether or not we find a free huge page to free so
6946ae11b27SLee Schermerhorn  * that the next attempt to free addresses the next node.
695e8c5c824SLee Schermerhorn  */
6966ae11b27SLee Schermerhorn static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
697e8c5c824SLee Schermerhorn {
6986ae11b27SLee Schermerhorn 	int nid;
6999a76db09SLee Schermerhorn 
7006ae11b27SLee Schermerhorn 	VM_BUG_ON(!nodes_allowed);
7016ae11b27SLee Schermerhorn 
7026ae11b27SLee Schermerhorn 	nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
7036ae11b27SLee Schermerhorn 	h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
7046ae11b27SLee Schermerhorn 
7059a76db09SLee Schermerhorn 	return nid;
706e8c5c824SLee Schermerhorn }
707e8c5c824SLee Schermerhorn 
708e8c5c824SLee Schermerhorn /*
709e8c5c824SLee Schermerhorn  * Free huge page from pool from next node to free.
710e8c5c824SLee Schermerhorn  * Attempt to keep persistent huge pages more or less
711e8c5c824SLee Schermerhorn  * balanced over allowed nodes.
712e8c5c824SLee Schermerhorn  * Called with hugetlb_lock locked.
713e8c5c824SLee Schermerhorn  */
7146ae11b27SLee Schermerhorn static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
7156ae11b27SLee Schermerhorn 							 bool acct_surplus)
716e8c5c824SLee Schermerhorn {
717e8c5c824SLee Schermerhorn 	int start_nid;
718e8c5c824SLee Schermerhorn 	int next_nid;
719e8c5c824SLee Schermerhorn 	int ret = 0;
720e8c5c824SLee Schermerhorn 
7216ae11b27SLee Schermerhorn 	start_nid = hstate_next_node_to_free(h, nodes_allowed);
722e8c5c824SLee Schermerhorn 	next_nid = start_nid;
723e8c5c824SLee Schermerhorn 
724e8c5c824SLee Schermerhorn 	do {
725685f3457SLee Schermerhorn 		/*
726685f3457SLee Schermerhorn 		 * If we're returning unused surplus pages, only examine
727685f3457SLee Schermerhorn 		 * nodes with surplus pages.
728685f3457SLee Schermerhorn 		 */
729685f3457SLee Schermerhorn 		if ((!acct_surplus || h->surplus_huge_pages_node[next_nid]) &&
730685f3457SLee Schermerhorn 		    !list_empty(&h->hugepage_freelists[next_nid])) {
731e8c5c824SLee Schermerhorn 			struct page *page =
732e8c5c824SLee Schermerhorn 				list_entry(h->hugepage_freelists[next_nid].next,
733e8c5c824SLee Schermerhorn 					  struct page, lru);
734e8c5c824SLee Schermerhorn 			list_del(&page->lru);
735e8c5c824SLee Schermerhorn 			h->free_huge_pages--;
736e8c5c824SLee Schermerhorn 			h->free_huge_pages_node[next_nid]--;
737685f3457SLee Schermerhorn 			if (acct_surplus) {
738685f3457SLee Schermerhorn 				h->surplus_huge_pages--;
739685f3457SLee Schermerhorn 				h->surplus_huge_pages_node[next_nid]--;
740685f3457SLee Schermerhorn 			}
741e8c5c824SLee Schermerhorn 			update_and_free_page(h, page);
742e8c5c824SLee Schermerhorn 			ret = 1;
7439a76db09SLee Schermerhorn 			break;
744e8c5c824SLee Schermerhorn 		}
7456ae11b27SLee Schermerhorn 		next_nid = hstate_next_node_to_free(h, nodes_allowed);
7469a76db09SLee Schermerhorn 	} while (next_nid != start_nid);
747e8c5c824SLee Schermerhorn 
748e8c5c824SLee Schermerhorn 	return ret;
749e8c5c824SLee Schermerhorn }
750e8c5c824SLee Schermerhorn 
751bf50bab2SNaoya Horiguchi static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
7527893d1d5SAdam Litke {
7537893d1d5SAdam Litke 	struct page *page;
754bf50bab2SNaoya Horiguchi 	unsigned int r_nid;
7557893d1d5SAdam Litke 
756aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
757aa888a74SAndi Kleen 		return NULL;
758aa888a74SAndi Kleen 
759d1c3fb1fSNishanth Aravamudan 	/*
760d1c3fb1fSNishanth Aravamudan 	 * Assume we will successfully allocate the surplus page to
761d1c3fb1fSNishanth Aravamudan 	 * prevent racing processes from causing the surplus to exceed
762d1c3fb1fSNishanth Aravamudan 	 * overcommit
763d1c3fb1fSNishanth Aravamudan 	 *
764d1c3fb1fSNishanth Aravamudan 	 * This however introduces a different race, where a process B
765d1c3fb1fSNishanth Aravamudan 	 * tries to grow the static hugepage pool while alloc_pages() is
766d1c3fb1fSNishanth Aravamudan 	 * called by process A. B will only examine the per-node
767d1c3fb1fSNishanth Aravamudan 	 * counters in determining if surplus huge pages can be
768d1c3fb1fSNishanth Aravamudan 	 * converted to normal huge pages in adjust_pool_surplus(). A
769d1c3fb1fSNishanth Aravamudan 	 * won't be able to increment the per-node counter, until the
770d1c3fb1fSNishanth Aravamudan 	 * lock is dropped by B, but B doesn't drop hugetlb_lock until
771d1c3fb1fSNishanth Aravamudan 	 * no more huge pages can be converted from surplus to normal
772d1c3fb1fSNishanth Aravamudan 	 * state (and doesn't try to convert again). Thus, we have a
773d1c3fb1fSNishanth Aravamudan 	 * case where a surplus huge page exists, the pool is grown, and
774d1c3fb1fSNishanth Aravamudan 	 * the surplus huge page still exists after, even though it
775d1c3fb1fSNishanth Aravamudan 	 * should just have been converted to a normal huge page. This
776d1c3fb1fSNishanth Aravamudan 	 * does not leak memory, though, as the hugepage will be freed
777d1c3fb1fSNishanth Aravamudan 	 * once it is out of use. It also does not allow the counters to
778d1c3fb1fSNishanth Aravamudan 	 * go out of whack in adjust_pool_surplus() as we don't modify
779d1c3fb1fSNishanth Aravamudan 	 * the node values until we've gotten the hugepage and only the
780d1c3fb1fSNishanth Aravamudan 	 * per-node value is checked there.
781d1c3fb1fSNishanth Aravamudan 	 */
782d1c3fb1fSNishanth Aravamudan 	spin_lock(&hugetlb_lock);
783a5516438SAndi Kleen 	if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
784d1c3fb1fSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
785d1c3fb1fSNishanth Aravamudan 		return NULL;
786d1c3fb1fSNishanth Aravamudan 	} else {
787a5516438SAndi Kleen 		h->nr_huge_pages++;
788a5516438SAndi Kleen 		h->surplus_huge_pages++;
789d1c3fb1fSNishanth Aravamudan 	}
790d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
791d1c3fb1fSNishanth Aravamudan 
792bf50bab2SNaoya Horiguchi 	if (nid == NUMA_NO_NODE)
793551883aeSNishanth Aravamudan 		page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
794551883aeSNishanth Aravamudan 				   __GFP_REPEAT|__GFP_NOWARN,
795a5516438SAndi Kleen 				   huge_page_order(h));
796bf50bab2SNaoya Horiguchi 	else
797bf50bab2SNaoya Horiguchi 		page = alloc_pages_exact_node(nid,
798bf50bab2SNaoya Horiguchi 			htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
799bf50bab2SNaoya Horiguchi 			__GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
800d1c3fb1fSNishanth Aravamudan 
801caff3a2cSGerald Schaefer 	if (page && arch_prepare_hugepage(page)) {
802caff3a2cSGerald Schaefer 		__free_pages(page, huge_page_order(h));
803caff3a2cSGerald Schaefer 		return NULL;
804caff3a2cSGerald Schaefer 	}
805caff3a2cSGerald Schaefer 
8067893d1d5SAdam Litke 	spin_lock(&hugetlb_lock);
807d1c3fb1fSNishanth Aravamudan 	if (page) {
808bf50bab2SNaoya Horiguchi 		r_nid = page_to_nid(page);
809d1c3fb1fSNishanth Aravamudan 		set_compound_page_dtor(page, free_huge_page);
810d1c3fb1fSNishanth Aravamudan 		/*
811d1c3fb1fSNishanth Aravamudan 		 * We incremented the global counters already
812d1c3fb1fSNishanth Aravamudan 		 */
813bf50bab2SNaoya Horiguchi 		h->nr_huge_pages_node[r_nid]++;
814bf50bab2SNaoya Horiguchi 		h->surplus_huge_pages_node[r_nid]++;
8153b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC);
816d1c3fb1fSNishanth Aravamudan 	} else {
817a5516438SAndi Kleen 		h->nr_huge_pages--;
818a5516438SAndi Kleen 		h->surplus_huge_pages--;
8193b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
8207893d1d5SAdam Litke 	}
821d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
8227893d1d5SAdam Litke 
8237893d1d5SAdam Litke 	return page;
8247893d1d5SAdam Litke }
8257893d1d5SAdam Litke 
826e4e574b7SAdam Litke /*
827bf50bab2SNaoya Horiguchi  * This allocation function is useful in the context where vma is irrelevant.
828bf50bab2SNaoya Horiguchi  * E.g. soft-offlining uses this function because it only cares physical
829bf50bab2SNaoya Horiguchi  * address of error page.
830bf50bab2SNaoya Horiguchi  */
831bf50bab2SNaoya Horiguchi struct page *alloc_huge_page_node(struct hstate *h, int nid)
832bf50bab2SNaoya Horiguchi {
833bf50bab2SNaoya Horiguchi 	struct page *page;
834bf50bab2SNaoya Horiguchi 
835bf50bab2SNaoya Horiguchi 	spin_lock(&hugetlb_lock);
836bf50bab2SNaoya Horiguchi 	page = dequeue_huge_page_node(h, nid);
837bf50bab2SNaoya Horiguchi 	spin_unlock(&hugetlb_lock);
838bf50bab2SNaoya Horiguchi 
839bf50bab2SNaoya Horiguchi 	if (!page)
840bf50bab2SNaoya Horiguchi 		page = alloc_buddy_huge_page(h, nid);
841bf50bab2SNaoya Horiguchi 
842bf50bab2SNaoya Horiguchi 	return page;
843bf50bab2SNaoya Horiguchi }
844bf50bab2SNaoya Horiguchi 
845bf50bab2SNaoya Horiguchi /*
84625985edcSLucas De Marchi  * Increase the hugetlb pool such that it can accommodate a reservation
847e4e574b7SAdam Litke  * of size 'delta'.
848e4e574b7SAdam Litke  */
849a5516438SAndi Kleen static int gather_surplus_pages(struct hstate *h, int delta)
850e4e574b7SAdam Litke {
851e4e574b7SAdam Litke 	struct list_head surplus_list;
852e4e574b7SAdam Litke 	struct page *page, *tmp;
853e4e574b7SAdam Litke 	int ret, i;
854e4e574b7SAdam Litke 	int needed, allocated;
855e4e574b7SAdam Litke 
856a5516438SAndi Kleen 	needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
857ac09b3a1SAdam Litke 	if (needed <= 0) {
858a5516438SAndi Kleen 		h->resv_huge_pages += delta;
859e4e574b7SAdam Litke 		return 0;
860ac09b3a1SAdam Litke 	}
861e4e574b7SAdam Litke 
862e4e574b7SAdam Litke 	allocated = 0;
863e4e574b7SAdam Litke 	INIT_LIST_HEAD(&surplus_list);
864e4e574b7SAdam Litke 
865e4e574b7SAdam Litke 	ret = -ENOMEM;
866e4e574b7SAdam Litke retry:
867e4e574b7SAdam Litke 	spin_unlock(&hugetlb_lock);
868e4e574b7SAdam Litke 	for (i = 0; i < needed; i++) {
869bf50bab2SNaoya Horiguchi 		page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
870a9869b83SNaoya Horiguchi 		if (!page)
871e4e574b7SAdam Litke 			/*
872e4e574b7SAdam Litke 			 * We were not able to allocate enough pages to
873e4e574b7SAdam Litke 			 * satisfy the entire reservation so we free what
874e4e574b7SAdam Litke 			 * we've allocated so far.
875e4e574b7SAdam Litke 			 */
876e4e574b7SAdam Litke 			goto free;
877e4e574b7SAdam Litke 
878e4e574b7SAdam Litke 		list_add(&page->lru, &surplus_list);
879e4e574b7SAdam Litke 	}
880e4e574b7SAdam Litke 	allocated += needed;
881e4e574b7SAdam Litke 
882e4e574b7SAdam Litke 	/*
883e4e574b7SAdam Litke 	 * After retaking hugetlb_lock, we need to recalculate 'needed'
884e4e574b7SAdam Litke 	 * because either resv_huge_pages or free_huge_pages may have changed.
885e4e574b7SAdam Litke 	 */
886e4e574b7SAdam Litke 	spin_lock(&hugetlb_lock);
887a5516438SAndi Kleen 	needed = (h->resv_huge_pages + delta) -
888a5516438SAndi Kleen 			(h->free_huge_pages + allocated);
889e4e574b7SAdam Litke 	if (needed > 0)
890e4e574b7SAdam Litke 		goto retry;
891e4e574b7SAdam Litke 
892e4e574b7SAdam Litke 	/*
893e4e574b7SAdam Litke 	 * The surplus_list now contains _at_least_ the number of extra pages
89425985edcSLucas De Marchi 	 * needed to accommodate the reservation.  Add the appropriate number
895e4e574b7SAdam Litke 	 * of pages to the hugetlb pool and free the extras back to the buddy
896ac09b3a1SAdam Litke 	 * allocator.  Commit the entire reservation here to prevent another
897ac09b3a1SAdam Litke 	 * process from stealing the pages as they are added to the pool but
898ac09b3a1SAdam Litke 	 * before they are reserved.
899e4e574b7SAdam Litke 	 */
900e4e574b7SAdam Litke 	needed += allocated;
901a5516438SAndi Kleen 	h->resv_huge_pages += delta;
902e4e574b7SAdam Litke 	ret = 0;
903a9869b83SNaoya Horiguchi 
90419fc3f0aSAdam Litke 	/* Free the needed pages to the hugetlb pool */
90519fc3f0aSAdam Litke 	list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
90619fc3f0aSAdam Litke 		if ((--needed) < 0)
90719fc3f0aSAdam Litke 			break;
90819fc3f0aSAdam Litke 		list_del(&page->lru);
909a9869b83SNaoya Horiguchi 		/*
910a9869b83SNaoya Horiguchi 		 * This page is now managed by the hugetlb allocator and has
911a9869b83SNaoya Horiguchi 		 * no users -- drop the buddy allocator's reference.
912a9869b83SNaoya Horiguchi 		 */
913a9869b83SNaoya Horiguchi 		put_page_testzero(page);
914a9869b83SNaoya Horiguchi 		VM_BUG_ON(page_count(page));
915a5516438SAndi Kleen 		enqueue_huge_page(h, page);
91619fc3f0aSAdam Litke 	}
917b0365c8dSHillf Danton 	spin_unlock(&hugetlb_lock);
91819fc3f0aSAdam Litke 
91919fc3f0aSAdam Litke 	/* Free unnecessary surplus pages to the buddy allocator */
920a9869b83SNaoya Horiguchi free:
92119fc3f0aSAdam Litke 	if (!list_empty(&surplus_list)) {
922e4e574b7SAdam Litke 		list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
923e4e574b7SAdam Litke 			list_del(&page->lru);
924a9869b83SNaoya Horiguchi 			put_page(page);
925a9869b83SNaoya Horiguchi 		}
926af767cbdSAdam Litke 	}
92719fc3f0aSAdam Litke 	spin_lock(&hugetlb_lock);
928e4e574b7SAdam Litke 
929e4e574b7SAdam Litke 	return ret;
930e4e574b7SAdam Litke }
931e4e574b7SAdam Litke 
932e4e574b7SAdam Litke /*
933e4e574b7SAdam Litke  * When releasing a hugetlb pool reservation, any surplus pages that were
934e4e574b7SAdam Litke  * allocated to satisfy the reservation must be explicitly freed if they were
935e4e574b7SAdam Litke  * never used.
936685f3457SLee Schermerhorn  * Called with hugetlb_lock held.
937e4e574b7SAdam Litke  */
938a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h,
939a5516438SAndi Kleen 					unsigned long unused_resv_pages)
940e4e574b7SAdam Litke {
941e4e574b7SAdam Litke 	unsigned long nr_pages;
942e4e574b7SAdam Litke 
943ac09b3a1SAdam Litke 	/* Uncommit the reservation */
944a5516438SAndi Kleen 	h->resv_huge_pages -= unused_resv_pages;
945ac09b3a1SAdam Litke 
946aa888a74SAndi Kleen 	/* Cannot return gigantic pages currently */
947aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
948aa888a74SAndi Kleen 		return;
949aa888a74SAndi Kleen 
950a5516438SAndi Kleen 	nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
951e4e574b7SAdam Litke 
952685f3457SLee Schermerhorn 	/*
953685f3457SLee Schermerhorn 	 * We want to release as many surplus pages as possible, spread
9549b5e5d0fSLee Schermerhorn 	 * evenly across all nodes with memory. Iterate across these nodes
9559b5e5d0fSLee Schermerhorn 	 * until we can no longer free unreserved surplus pages. This occurs
9569b5e5d0fSLee Schermerhorn 	 * when the nodes with surplus pages have no free pages.
9579b5e5d0fSLee Schermerhorn 	 * free_pool_huge_page() will balance the the freed pages across the
9589b5e5d0fSLee Schermerhorn 	 * on-line nodes with memory and will handle the hstate accounting.
959685f3457SLee Schermerhorn 	 */
960685f3457SLee Schermerhorn 	while (nr_pages--) {
9619b5e5d0fSLee Schermerhorn 		if (!free_pool_huge_page(h, &node_states[N_HIGH_MEMORY], 1))
962685f3457SLee Schermerhorn 			break;
963e4e574b7SAdam Litke 	}
964e4e574b7SAdam Litke }
965e4e574b7SAdam Litke 
966c37f9fb1SAndy Whitcroft /*
967c37f9fb1SAndy Whitcroft  * Determine if the huge page at addr within the vma has an associated
968c37f9fb1SAndy Whitcroft  * reservation.  Where it does not we will need to logically increase
969c37f9fb1SAndy Whitcroft  * reservation and actually increase quota before an allocation can occur.
970c37f9fb1SAndy Whitcroft  * Where any new reservation would be required the reservation change is
971c37f9fb1SAndy Whitcroft  * prepared, but not committed.  Once the page has been quota'd allocated
972c37f9fb1SAndy Whitcroft  * an instantiated the change should be committed via vma_commit_reservation.
973c37f9fb1SAndy Whitcroft  * No action is required on failure.
974c37f9fb1SAndy Whitcroft  */
975e2f17d94SRoel Kluin static long vma_needs_reservation(struct hstate *h,
976a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long addr)
977c37f9fb1SAndy Whitcroft {
978c37f9fb1SAndy Whitcroft 	struct address_space *mapping = vma->vm_file->f_mapping;
979c37f9fb1SAndy Whitcroft 	struct inode *inode = mapping->host;
980c37f9fb1SAndy Whitcroft 
981f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE) {
982a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
983c37f9fb1SAndy Whitcroft 		return region_chg(&inode->i_mapping->private_list,
984c37f9fb1SAndy Whitcroft 							idx, idx + 1);
985c37f9fb1SAndy Whitcroft 
98684afd99bSAndy Whitcroft 	} else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
987c37f9fb1SAndy Whitcroft 		return 1;
988c37f9fb1SAndy Whitcroft 
98984afd99bSAndy Whitcroft 	} else  {
990e2f17d94SRoel Kluin 		long err;
991a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
99284afd99bSAndy Whitcroft 		struct resv_map *reservations = vma_resv_map(vma);
99384afd99bSAndy Whitcroft 
99484afd99bSAndy Whitcroft 		err = region_chg(&reservations->regions, idx, idx + 1);
99584afd99bSAndy Whitcroft 		if (err < 0)
99684afd99bSAndy Whitcroft 			return err;
997c37f9fb1SAndy Whitcroft 		return 0;
998c37f9fb1SAndy Whitcroft 	}
99984afd99bSAndy Whitcroft }
1000a5516438SAndi Kleen static void vma_commit_reservation(struct hstate *h,
1001a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long addr)
1002c37f9fb1SAndy Whitcroft {
1003c37f9fb1SAndy Whitcroft 	struct address_space *mapping = vma->vm_file->f_mapping;
1004c37f9fb1SAndy Whitcroft 	struct inode *inode = mapping->host;
1005c37f9fb1SAndy Whitcroft 
1006f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE) {
1007a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
1008c37f9fb1SAndy Whitcroft 		region_add(&inode->i_mapping->private_list, idx, idx + 1);
100984afd99bSAndy Whitcroft 
101084afd99bSAndy Whitcroft 	} else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1011a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
101284afd99bSAndy Whitcroft 		struct resv_map *reservations = vma_resv_map(vma);
101384afd99bSAndy Whitcroft 
101484afd99bSAndy Whitcroft 		/* Mark this page used in the map. */
101584afd99bSAndy Whitcroft 		region_add(&reservations->regions, idx, idx + 1);
1016c37f9fb1SAndy Whitcroft 	}
1017c37f9fb1SAndy Whitcroft }
1018c37f9fb1SAndy Whitcroft 
1019348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma,
102004f2cbe3SMel Gorman 				    unsigned long addr, int avoid_reserve)
1021348ea204SAdam Litke {
1022a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
1023348ea204SAdam Litke 	struct page *page;
10242fc39cecSAdam Litke 	struct address_space *mapping = vma->vm_file->f_mapping;
1025a1e78772SMel Gorman 	struct inode *inode = mapping->host;
1026e2f17d94SRoel Kluin 	long chg;
10272fc39cecSAdam Litke 
1028a1e78772SMel Gorman 	/*
1029a1e78772SMel Gorman 	 * Processes that did not create the mapping will have no reserves and
1030a1e78772SMel Gorman 	 * will not have accounted against quota. Check that the quota can be
1031a1e78772SMel Gorman 	 * made before satisfying the allocation
1032c37f9fb1SAndy Whitcroft 	 * MAP_NORESERVE mappings may also need pages and quota allocated
1033c37f9fb1SAndy Whitcroft 	 * if no reserve mapping overlaps.
1034a1e78772SMel Gorman 	 */
1035a5516438SAndi Kleen 	chg = vma_needs_reservation(h, vma, addr);
1036c37f9fb1SAndy Whitcroft 	if (chg < 0)
1037e0dcd8a0SHugh Dickins 		return ERR_PTR(-VM_FAULT_OOM);
1038c37f9fb1SAndy Whitcroft 	if (chg)
1039a1e78772SMel Gorman 		if (hugetlb_get_quota(inode->i_mapping, chg))
1040e0dcd8a0SHugh Dickins 			return ERR_PTR(-VM_FAULT_SIGBUS);
104190d8b7e6SAdam Litke 
1042a1e78772SMel Gorman 	spin_lock(&hugetlb_lock);
1043a5516438SAndi Kleen 	page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
1044a1e78772SMel Gorman 	spin_unlock(&hugetlb_lock);
1045a1e78772SMel Gorman 
1046a1e78772SMel Gorman 	if (!page) {
1047bf50bab2SNaoya Horiguchi 		page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
1048a1e78772SMel Gorman 		if (!page) {
1049a1e78772SMel Gorman 			hugetlb_put_quota(inode->i_mapping, chg);
10504a6018f7SMel Gorman 			return ERR_PTR(-VM_FAULT_SIGBUS);
1051a1e78772SMel Gorman 		}
1052a1e78772SMel Gorman 	}
1053a1e78772SMel Gorman 
10542fc39cecSAdam Litke 	set_page_private(page, (unsigned long) mapping);
1055a1e78772SMel Gorman 
1056a5516438SAndi Kleen 	vma_commit_reservation(h, vma, addr);
1057c37f9fb1SAndy Whitcroft 
10587893d1d5SAdam Litke 	return page;
1059b45b5bd6SDavid Gibson }
1060b45b5bd6SDavid Gibson 
106191f47662SCyrill Gorcunov int __weak alloc_bootmem_huge_page(struct hstate *h)
1062aa888a74SAndi Kleen {
1063aa888a74SAndi Kleen 	struct huge_bootmem_page *m;
10649b5e5d0fSLee Schermerhorn 	int nr_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
1065aa888a74SAndi Kleen 
1066aa888a74SAndi Kleen 	while (nr_nodes) {
1067aa888a74SAndi Kleen 		void *addr;
1068aa888a74SAndi Kleen 
1069aa888a74SAndi Kleen 		addr = __alloc_bootmem_node_nopanic(
10706ae11b27SLee Schermerhorn 				NODE_DATA(hstate_next_node_to_alloc(h,
10719b5e5d0fSLee Schermerhorn 						&node_states[N_HIGH_MEMORY])),
1072aa888a74SAndi Kleen 				huge_page_size(h), huge_page_size(h), 0);
1073aa888a74SAndi Kleen 
1074aa888a74SAndi Kleen 		if (addr) {
1075aa888a74SAndi Kleen 			/*
1076aa888a74SAndi Kleen 			 * Use the beginning of the huge page to store the
1077aa888a74SAndi Kleen 			 * huge_bootmem_page struct (until gather_bootmem
1078aa888a74SAndi Kleen 			 * puts them into the mem_map).
1079aa888a74SAndi Kleen 			 */
1080aa888a74SAndi Kleen 			m = addr;
1081aa888a74SAndi Kleen 			goto found;
1082aa888a74SAndi Kleen 		}
1083aa888a74SAndi Kleen 		nr_nodes--;
1084aa888a74SAndi Kleen 	}
1085aa888a74SAndi Kleen 	return 0;
1086aa888a74SAndi Kleen 
1087aa888a74SAndi Kleen found:
1088aa888a74SAndi Kleen 	BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
1089aa888a74SAndi Kleen 	/* Put them into a private list first because mem_map is not up yet */
1090aa888a74SAndi Kleen 	list_add(&m->list, &huge_boot_pages);
1091aa888a74SAndi Kleen 	m->hstate = h;
1092aa888a74SAndi Kleen 	return 1;
1093aa888a74SAndi Kleen }
1094aa888a74SAndi Kleen 
109518229df5SAndy Whitcroft static void prep_compound_huge_page(struct page *page, int order)
109618229df5SAndy Whitcroft {
109718229df5SAndy Whitcroft 	if (unlikely(order > (MAX_ORDER - 1)))
109818229df5SAndy Whitcroft 		prep_compound_gigantic_page(page, order);
109918229df5SAndy Whitcroft 	else
110018229df5SAndy Whitcroft 		prep_compound_page(page, order);
110118229df5SAndy Whitcroft }
110218229df5SAndy Whitcroft 
1103aa888a74SAndi Kleen /* Put bootmem huge pages into the standard lists after mem_map is up */
1104aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void)
1105aa888a74SAndi Kleen {
1106aa888a74SAndi Kleen 	struct huge_bootmem_page *m;
1107aa888a74SAndi Kleen 
1108aa888a74SAndi Kleen 	list_for_each_entry(m, &huge_boot_pages, list) {
1109aa888a74SAndi Kleen 		struct hstate *h = m->hstate;
1110ee8f248dSBecky Bruce 		struct page *page;
1111ee8f248dSBecky Bruce 
1112ee8f248dSBecky Bruce #ifdef CONFIG_HIGHMEM
1113ee8f248dSBecky Bruce 		page = pfn_to_page(m->phys >> PAGE_SHIFT);
1114ee8f248dSBecky Bruce 		free_bootmem_late((unsigned long)m,
1115ee8f248dSBecky Bruce 				  sizeof(struct huge_bootmem_page));
1116ee8f248dSBecky Bruce #else
1117ee8f248dSBecky Bruce 		page = virt_to_page(m);
1118ee8f248dSBecky Bruce #endif
1119aa888a74SAndi Kleen 		__ClearPageReserved(page);
1120aa888a74SAndi Kleen 		WARN_ON(page_count(page) != 1);
112118229df5SAndy Whitcroft 		prep_compound_huge_page(page, h->order);
1122aa888a74SAndi Kleen 		prep_new_huge_page(h, page, page_to_nid(page));
1123b0320c7bSRafael Aquini 		/*
1124b0320c7bSRafael Aquini 		 * If we had gigantic hugepages allocated at boot time, we need
1125b0320c7bSRafael Aquini 		 * to restore the 'stolen' pages to totalram_pages in order to
1126b0320c7bSRafael Aquini 		 * fix confusing memory reports from free(1) and another
1127b0320c7bSRafael Aquini 		 * side-effects, like CommitLimit going negative.
1128b0320c7bSRafael Aquini 		 */
1129b0320c7bSRafael Aquini 		if (h->order > (MAX_ORDER - 1))
1130b0320c7bSRafael Aquini 			totalram_pages += 1 << h->order;
1131aa888a74SAndi Kleen 	}
1132aa888a74SAndi Kleen }
1133aa888a74SAndi Kleen 
11348faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
11351da177e4SLinus Torvalds {
11361da177e4SLinus Torvalds 	unsigned long i;
11371da177e4SLinus Torvalds 
1138e5ff2159SAndi Kleen 	for (i = 0; i < h->max_huge_pages; ++i) {
1139aa888a74SAndi Kleen 		if (h->order >= MAX_ORDER) {
1140aa888a74SAndi Kleen 			if (!alloc_bootmem_huge_page(h))
1141aa888a74SAndi Kleen 				break;
11429b5e5d0fSLee Schermerhorn 		} else if (!alloc_fresh_huge_page(h,
11439b5e5d0fSLee Schermerhorn 					 &node_states[N_HIGH_MEMORY]))
11441da177e4SLinus Torvalds 			break;
11451da177e4SLinus Torvalds 	}
11468faa8b07SAndi Kleen 	h->max_huge_pages = i;
1147e5ff2159SAndi Kleen }
1148e5ff2159SAndi Kleen 
1149e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void)
1150e5ff2159SAndi Kleen {
1151e5ff2159SAndi Kleen 	struct hstate *h;
1152e5ff2159SAndi Kleen 
1153e5ff2159SAndi Kleen 	for_each_hstate(h) {
11548faa8b07SAndi Kleen 		/* oversize hugepages were init'ed in early boot */
11558faa8b07SAndi Kleen 		if (h->order < MAX_ORDER)
11568faa8b07SAndi Kleen 			hugetlb_hstate_alloc_pages(h);
1157e5ff2159SAndi Kleen 	}
1158e5ff2159SAndi Kleen }
1159e5ff2159SAndi Kleen 
11604abd32dbSAndi Kleen static char * __init memfmt(char *buf, unsigned long n)
11614abd32dbSAndi Kleen {
11624abd32dbSAndi Kleen 	if (n >= (1UL << 30))
11634abd32dbSAndi Kleen 		sprintf(buf, "%lu GB", n >> 30);
11644abd32dbSAndi Kleen 	else if (n >= (1UL << 20))
11654abd32dbSAndi Kleen 		sprintf(buf, "%lu MB", n >> 20);
11664abd32dbSAndi Kleen 	else
11674abd32dbSAndi Kleen 		sprintf(buf, "%lu KB", n >> 10);
11684abd32dbSAndi Kleen 	return buf;
11694abd32dbSAndi Kleen }
11704abd32dbSAndi Kleen 
1171e5ff2159SAndi Kleen static void __init report_hugepages(void)
1172e5ff2159SAndi Kleen {
1173e5ff2159SAndi Kleen 	struct hstate *h;
1174e5ff2159SAndi Kleen 
1175e5ff2159SAndi Kleen 	for_each_hstate(h) {
11764abd32dbSAndi Kleen 		char buf[32];
11774abd32dbSAndi Kleen 		printk(KERN_INFO "HugeTLB registered %s page size, "
11784abd32dbSAndi Kleen 				 "pre-allocated %ld pages\n",
11794abd32dbSAndi Kleen 			memfmt(buf, huge_page_size(h)),
11804abd32dbSAndi Kleen 			h->free_huge_pages);
1181e5ff2159SAndi Kleen 	}
1182e5ff2159SAndi Kleen }
1183e5ff2159SAndi Kleen 
11841da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM
11856ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count,
11866ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
11871da177e4SLinus Torvalds {
11884415cc8dSChristoph Lameter 	int i;
11894415cc8dSChristoph Lameter 
1190aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1191aa888a74SAndi Kleen 		return;
1192aa888a74SAndi Kleen 
11936ae11b27SLee Schermerhorn 	for_each_node_mask(i, *nodes_allowed) {
11941da177e4SLinus Torvalds 		struct page *page, *next;
1195a5516438SAndi Kleen 		struct list_head *freel = &h->hugepage_freelists[i];
1196a5516438SAndi Kleen 		list_for_each_entry_safe(page, next, freel, lru) {
1197a5516438SAndi Kleen 			if (count >= h->nr_huge_pages)
11986b0c880dSAdam Litke 				return;
11991da177e4SLinus Torvalds 			if (PageHighMem(page))
12001da177e4SLinus Torvalds 				continue;
12011da177e4SLinus Torvalds 			list_del(&page->lru);
1202e5ff2159SAndi Kleen 			update_and_free_page(h, page);
1203a5516438SAndi Kleen 			h->free_huge_pages--;
1204a5516438SAndi Kleen 			h->free_huge_pages_node[page_to_nid(page)]--;
12051da177e4SLinus Torvalds 		}
12061da177e4SLinus Torvalds 	}
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds #else
12096ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count,
12106ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
12111da177e4SLinus Torvalds {
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds #endif
12141da177e4SLinus Torvalds 
121520a0307cSWu Fengguang /*
121620a0307cSWu Fengguang  * Increment or decrement surplus_huge_pages.  Keep node-specific counters
121720a0307cSWu Fengguang  * balanced by operating on them in a round-robin fashion.
121820a0307cSWu Fengguang  * Returns 1 if an adjustment was made.
121920a0307cSWu Fengguang  */
12206ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
12216ae11b27SLee Schermerhorn 				int delta)
122220a0307cSWu Fengguang {
1223e8c5c824SLee Schermerhorn 	int start_nid, next_nid;
122420a0307cSWu Fengguang 	int ret = 0;
122520a0307cSWu Fengguang 
122620a0307cSWu Fengguang 	VM_BUG_ON(delta != -1 && delta != 1);
122720a0307cSWu Fengguang 
1228e8c5c824SLee Schermerhorn 	if (delta < 0)
12296ae11b27SLee Schermerhorn 		start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
1230e8c5c824SLee Schermerhorn 	else
12316ae11b27SLee Schermerhorn 		start_nid = hstate_next_node_to_free(h, nodes_allowed);
1232e8c5c824SLee Schermerhorn 	next_nid = start_nid;
1233e8c5c824SLee Schermerhorn 
1234e8c5c824SLee Schermerhorn 	do {
1235e8c5c824SLee Schermerhorn 		int nid = next_nid;
1236e8c5c824SLee Schermerhorn 		if (delta < 0)  {
1237e8c5c824SLee Schermerhorn 			/*
1238e8c5c824SLee Schermerhorn 			 * To shrink on this node, there must be a surplus page
1239e8c5c824SLee Schermerhorn 			 */
12409a76db09SLee Schermerhorn 			if (!h->surplus_huge_pages_node[nid]) {
12416ae11b27SLee Schermerhorn 				next_nid = hstate_next_node_to_alloc(h,
12426ae11b27SLee Schermerhorn 								nodes_allowed);
124320a0307cSWu Fengguang 				continue;
1244e8c5c824SLee Schermerhorn 			}
12459a76db09SLee Schermerhorn 		}
1246e8c5c824SLee Schermerhorn 		if (delta > 0) {
1247e8c5c824SLee Schermerhorn 			/*
1248e8c5c824SLee Schermerhorn 			 * Surplus cannot exceed the total number of pages
1249e8c5c824SLee Schermerhorn 			 */
1250e8c5c824SLee Schermerhorn 			if (h->surplus_huge_pages_node[nid] >=
12519a76db09SLee Schermerhorn 						h->nr_huge_pages_node[nid]) {
12526ae11b27SLee Schermerhorn 				next_nid = hstate_next_node_to_free(h,
12536ae11b27SLee Schermerhorn 								nodes_allowed);
125420a0307cSWu Fengguang 				continue;
1255e8c5c824SLee Schermerhorn 			}
12569a76db09SLee Schermerhorn 		}
125720a0307cSWu Fengguang 
125820a0307cSWu Fengguang 		h->surplus_huge_pages += delta;
125920a0307cSWu Fengguang 		h->surplus_huge_pages_node[nid] += delta;
126020a0307cSWu Fengguang 		ret = 1;
126120a0307cSWu Fengguang 		break;
1262e8c5c824SLee Schermerhorn 	} while (next_nid != start_nid);
126320a0307cSWu Fengguang 
126420a0307cSWu Fengguang 	return ret;
126520a0307cSWu Fengguang }
126620a0307cSWu Fengguang 
1267a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
12686ae11b27SLee Schermerhorn static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
12696ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
12701da177e4SLinus Torvalds {
12717893d1d5SAdam Litke 	unsigned long min_count, ret;
12721da177e4SLinus Torvalds 
1273aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1274aa888a74SAndi Kleen 		return h->max_huge_pages;
1275aa888a74SAndi Kleen 
12767893d1d5SAdam Litke 	/*
12777893d1d5SAdam Litke 	 * Increase the pool size
12787893d1d5SAdam Litke 	 * First take pages out of surplus state.  Then make up the
12797893d1d5SAdam Litke 	 * remaining difference by allocating fresh huge pages.
1280d1c3fb1fSNishanth Aravamudan 	 *
1281d1c3fb1fSNishanth Aravamudan 	 * We might race with alloc_buddy_huge_page() here and be unable
1282d1c3fb1fSNishanth Aravamudan 	 * to convert a surplus huge page to a normal huge page. That is
1283d1c3fb1fSNishanth Aravamudan 	 * not critical, though, it just means the overall size of the
1284d1c3fb1fSNishanth Aravamudan 	 * pool might be one hugepage larger than it needs to be, but
1285d1c3fb1fSNishanth Aravamudan 	 * within all the constraints specified by the sysctls.
12867893d1d5SAdam Litke 	 */
12871da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
1288a5516438SAndi Kleen 	while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
12896ae11b27SLee Schermerhorn 		if (!adjust_pool_surplus(h, nodes_allowed, -1))
12907893d1d5SAdam Litke 			break;
12917893d1d5SAdam Litke 	}
12927893d1d5SAdam Litke 
1293a5516438SAndi Kleen 	while (count > persistent_huge_pages(h)) {
12947893d1d5SAdam Litke 		/*
12957893d1d5SAdam Litke 		 * If this allocation races such that we no longer need the
12967893d1d5SAdam Litke 		 * page, free_huge_page will handle it by freeing the page
12977893d1d5SAdam Litke 		 * and reducing the surplus.
12987893d1d5SAdam Litke 		 */
12997893d1d5SAdam Litke 		spin_unlock(&hugetlb_lock);
13006ae11b27SLee Schermerhorn 		ret = alloc_fresh_huge_page(h, nodes_allowed);
13017893d1d5SAdam Litke 		spin_lock(&hugetlb_lock);
13027893d1d5SAdam Litke 		if (!ret)
13037893d1d5SAdam Litke 			goto out;
13047893d1d5SAdam Litke 
1305536240f2SMel Gorman 		/* Bail for signals. Probably ctrl-c from user */
1306536240f2SMel Gorman 		if (signal_pending(current))
1307536240f2SMel Gorman 			goto out;
13087893d1d5SAdam Litke 	}
13097893d1d5SAdam Litke 
13107893d1d5SAdam Litke 	/*
13117893d1d5SAdam Litke 	 * Decrease the pool size
13127893d1d5SAdam Litke 	 * First return free pages to the buddy allocator (being careful
13137893d1d5SAdam Litke 	 * to keep enough around to satisfy reservations).  Then place
13147893d1d5SAdam Litke 	 * pages into surplus state as needed so the pool will shrink
13157893d1d5SAdam Litke 	 * to the desired size as pages become free.
1316d1c3fb1fSNishanth Aravamudan 	 *
1317d1c3fb1fSNishanth Aravamudan 	 * By placing pages into the surplus state independent of the
1318d1c3fb1fSNishanth Aravamudan 	 * overcommit value, we are allowing the surplus pool size to
1319d1c3fb1fSNishanth Aravamudan 	 * exceed overcommit. There are few sane options here. Since
1320d1c3fb1fSNishanth Aravamudan 	 * alloc_buddy_huge_page() is checking the global counter,
1321d1c3fb1fSNishanth Aravamudan 	 * though, we'll note that we're not allowed to exceed surplus
1322d1c3fb1fSNishanth Aravamudan 	 * and won't grow the pool anywhere else. Not until one of the
1323d1c3fb1fSNishanth Aravamudan 	 * sysctls are changed, or the surplus pages go out of use.
13247893d1d5SAdam Litke 	 */
1325a5516438SAndi Kleen 	min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
13266b0c880dSAdam Litke 	min_count = max(count, min_count);
13276ae11b27SLee Schermerhorn 	try_to_free_low(h, min_count, nodes_allowed);
1328a5516438SAndi Kleen 	while (min_count < persistent_huge_pages(h)) {
13296ae11b27SLee Schermerhorn 		if (!free_pool_huge_page(h, nodes_allowed, 0))
13301da177e4SLinus Torvalds 			break;
13311da177e4SLinus Torvalds 	}
1332a5516438SAndi Kleen 	while (count < persistent_huge_pages(h)) {
13336ae11b27SLee Schermerhorn 		if (!adjust_pool_surplus(h, nodes_allowed, 1))
13347893d1d5SAdam Litke 			break;
13357893d1d5SAdam Litke 	}
13367893d1d5SAdam Litke out:
1337a5516438SAndi Kleen 	ret = persistent_huge_pages(h);
13381da177e4SLinus Torvalds 	spin_unlock(&hugetlb_lock);
13397893d1d5SAdam Litke 	return ret;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
1342a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \
1343a3437870SNishanth Aravamudan 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1344a3437870SNishanth Aravamudan 
1345a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \
1346a3437870SNishanth Aravamudan 	static struct kobj_attribute _name##_attr = \
1347a3437870SNishanth Aravamudan 		__ATTR(_name, 0644, _name##_show, _name##_store)
1348a3437870SNishanth Aravamudan 
1349a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj;
1350a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
1351a3437870SNishanth Aravamudan 
13529a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
13539a305230SLee Schermerhorn 
13549a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
1355a3437870SNishanth Aravamudan {
1356a3437870SNishanth Aravamudan 	int i;
13579a305230SLee Schermerhorn 
1358a3437870SNishanth Aravamudan 	for (i = 0; i < HUGE_MAX_HSTATE; i++)
13599a305230SLee Schermerhorn 		if (hstate_kobjs[i] == kobj) {
13609a305230SLee Schermerhorn 			if (nidp)
13619a305230SLee Schermerhorn 				*nidp = NUMA_NO_NODE;
1362a3437870SNishanth Aravamudan 			return &hstates[i];
13639a305230SLee Schermerhorn 		}
13649a305230SLee Schermerhorn 
13659a305230SLee Schermerhorn 	return kobj_to_node_hstate(kobj, nidp);
1366a3437870SNishanth Aravamudan }
1367a3437870SNishanth Aravamudan 
136806808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj,
1369a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1370a3437870SNishanth Aravamudan {
13719a305230SLee Schermerhorn 	struct hstate *h;
13729a305230SLee Schermerhorn 	unsigned long nr_huge_pages;
13739a305230SLee Schermerhorn 	int nid;
13749a305230SLee Schermerhorn 
13759a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
13769a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
13779a305230SLee Schermerhorn 		nr_huge_pages = h->nr_huge_pages;
13789a305230SLee Schermerhorn 	else
13799a305230SLee Schermerhorn 		nr_huge_pages = h->nr_huge_pages_node[nid];
13809a305230SLee Schermerhorn 
13819a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", nr_huge_pages);
1382a3437870SNishanth Aravamudan }
1383adbe8726SEric B Munson 
138406808b08SLee Schermerhorn static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
138506808b08SLee Schermerhorn 			struct kobject *kobj, struct kobj_attribute *attr,
138606808b08SLee Schermerhorn 			const char *buf, size_t len)
1387a3437870SNishanth Aravamudan {
1388a3437870SNishanth Aravamudan 	int err;
13899a305230SLee Schermerhorn 	int nid;
139006808b08SLee Schermerhorn 	unsigned long count;
13919a305230SLee Schermerhorn 	struct hstate *h;
1392bad44b5bSDavid Rientjes 	NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
1393a3437870SNishanth Aravamudan 
139406808b08SLee Schermerhorn 	err = strict_strtoul(buf, 10, &count);
139573ae31e5SEric B Munson 	if (err)
1396adbe8726SEric B Munson 		goto out;
1397a3437870SNishanth Aravamudan 
13989a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
1399adbe8726SEric B Munson 	if (h->order >= MAX_ORDER) {
1400adbe8726SEric B Munson 		err = -EINVAL;
1401adbe8726SEric B Munson 		goto out;
1402adbe8726SEric B Munson 	}
1403adbe8726SEric B Munson 
14049a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE) {
14059a305230SLee Schermerhorn 		/*
14069a305230SLee Schermerhorn 		 * global hstate attribute
14079a305230SLee Schermerhorn 		 */
14089a305230SLee Schermerhorn 		if (!(obey_mempolicy &&
14099a305230SLee Schermerhorn 				init_nodemask_of_mempolicy(nodes_allowed))) {
141006808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
14119a305230SLee Schermerhorn 			nodes_allowed = &node_states[N_HIGH_MEMORY];
141206808b08SLee Schermerhorn 		}
14139a305230SLee Schermerhorn 	} else if (nodes_allowed) {
14149a305230SLee Schermerhorn 		/*
14159a305230SLee Schermerhorn 		 * per node hstate attribute: adjust count to global,
14169a305230SLee Schermerhorn 		 * but restrict alloc/free to the specified node.
14179a305230SLee Schermerhorn 		 */
14189a305230SLee Schermerhorn 		count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
14199a305230SLee Schermerhorn 		init_nodemask_of_node(nodes_allowed, nid);
14209a305230SLee Schermerhorn 	} else
14219a305230SLee Schermerhorn 		nodes_allowed = &node_states[N_HIGH_MEMORY];
14229a305230SLee Schermerhorn 
142306808b08SLee Schermerhorn 	h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
1424a3437870SNishanth Aravamudan 
14259b5e5d0fSLee Schermerhorn 	if (nodes_allowed != &node_states[N_HIGH_MEMORY])
142606808b08SLee Schermerhorn 		NODEMASK_FREE(nodes_allowed);
142706808b08SLee Schermerhorn 
142806808b08SLee Schermerhorn 	return len;
1429adbe8726SEric B Munson out:
1430adbe8726SEric B Munson 	NODEMASK_FREE(nodes_allowed);
1431adbe8726SEric B Munson 	return err;
143206808b08SLee Schermerhorn }
143306808b08SLee Schermerhorn 
143406808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj,
143506808b08SLee Schermerhorn 				       struct kobj_attribute *attr, char *buf)
143606808b08SLee Schermerhorn {
143706808b08SLee Schermerhorn 	return nr_hugepages_show_common(kobj, attr, buf);
143806808b08SLee Schermerhorn }
143906808b08SLee Schermerhorn 
144006808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj,
144106808b08SLee Schermerhorn 	       struct kobj_attribute *attr, const char *buf, size_t len)
144206808b08SLee Schermerhorn {
144306808b08SLee Schermerhorn 	return nr_hugepages_store_common(false, kobj, attr, buf, len);
1444a3437870SNishanth Aravamudan }
1445a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages);
1446a3437870SNishanth Aravamudan 
144706808b08SLee Schermerhorn #ifdef CONFIG_NUMA
144806808b08SLee Schermerhorn 
144906808b08SLee Schermerhorn /*
145006808b08SLee Schermerhorn  * hstate attribute for optionally mempolicy-based constraint on persistent
145106808b08SLee Schermerhorn  * huge page alloc/free.
145206808b08SLee Schermerhorn  */
145306808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
145406808b08SLee Schermerhorn 				       struct kobj_attribute *attr, char *buf)
145506808b08SLee Schermerhorn {
145606808b08SLee Schermerhorn 	return nr_hugepages_show_common(kobj, attr, buf);
145706808b08SLee Schermerhorn }
145806808b08SLee Schermerhorn 
145906808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
146006808b08SLee Schermerhorn 	       struct kobj_attribute *attr, const char *buf, size_t len)
146106808b08SLee Schermerhorn {
146206808b08SLee Schermerhorn 	return nr_hugepages_store_common(true, kobj, attr, buf, len);
146306808b08SLee Schermerhorn }
146406808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy);
146506808b08SLee Schermerhorn #endif
146606808b08SLee Schermerhorn 
146706808b08SLee Schermerhorn 
1468a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
1469a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1470a3437870SNishanth Aravamudan {
14719a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1472a3437870SNishanth Aravamudan 	return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
1473a3437870SNishanth Aravamudan }
1474adbe8726SEric B Munson 
1475a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
1476a3437870SNishanth Aravamudan 		struct kobj_attribute *attr, const char *buf, size_t count)
1477a3437870SNishanth Aravamudan {
1478a3437870SNishanth Aravamudan 	int err;
1479a3437870SNishanth Aravamudan 	unsigned long input;
14809a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1481a3437870SNishanth Aravamudan 
1482adbe8726SEric B Munson 	if (h->order >= MAX_ORDER)
1483adbe8726SEric B Munson 		return -EINVAL;
1484adbe8726SEric B Munson 
1485a3437870SNishanth Aravamudan 	err = strict_strtoul(buf, 10, &input);
1486a3437870SNishanth Aravamudan 	if (err)
148773ae31e5SEric B Munson 		return err;
1488a3437870SNishanth Aravamudan 
1489a3437870SNishanth Aravamudan 	spin_lock(&hugetlb_lock);
1490a3437870SNishanth Aravamudan 	h->nr_overcommit_huge_pages = input;
1491a3437870SNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
1492a3437870SNishanth Aravamudan 
1493a3437870SNishanth Aravamudan 	return count;
1494a3437870SNishanth Aravamudan }
1495a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages);
1496a3437870SNishanth Aravamudan 
1497a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj,
1498a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1499a3437870SNishanth Aravamudan {
15009a305230SLee Schermerhorn 	struct hstate *h;
15019a305230SLee Schermerhorn 	unsigned long free_huge_pages;
15029a305230SLee Schermerhorn 	int nid;
15039a305230SLee Schermerhorn 
15049a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
15059a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
15069a305230SLee Schermerhorn 		free_huge_pages = h->free_huge_pages;
15079a305230SLee Schermerhorn 	else
15089a305230SLee Schermerhorn 		free_huge_pages = h->free_huge_pages_node[nid];
15099a305230SLee Schermerhorn 
15109a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", free_huge_pages);
1511a3437870SNishanth Aravamudan }
1512a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages);
1513a3437870SNishanth Aravamudan 
1514a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj,
1515a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1516a3437870SNishanth Aravamudan {
15179a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1518a3437870SNishanth Aravamudan 	return sprintf(buf, "%lu\n", h->resv_huge_pages);
1519a3437870SNishanth Aravamudan }
1520a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages);
1521a3437870SNishanth Aravamudan 
1522a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj,
1523a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1524a3437870SNishanth Aravamudan {
15259a305230SLee Schermerhorn 	struct hstate *h;
15269a305230SLee Schermerhorn 	unsigned long surplus_huge_pages;
15279a305230SLee Schermerhorn 	int nid;
15289a305230SLee Schermerhorn 
15299a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
15309a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
15319a305230SLee Schermerhorn 		surplus_huge_pages = h->surplus_huge_pages;
15329a305230SLee Schermerhorn 	else
15339a305230SLee Schermerhorn 		surplus_huge_pages = h->surplus_huge_pages_node[nid];
15349a305230SLee Schermerhorn 
15359a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", surplus_huge_pages);
1536a3437870SNishanth Aravamudan }
1537a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages);
1538a3437870SNishanth Aravamudan 
1539a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = {
1540a3437870SNishanth Aravamudan 	&nr_hugepages_attr.attr,
1541a3437870SNishanth Aravamudan 	&nr_overcommit_hugepages_attr.attr,
1542a3437870SNishanth Aravamudan 	&free_hugepages_attr.attr,
1543a3437870SNishanth Aravamudan 	&resv_hugepages_attr.attr,
1544a3437870SNishanth Aravamudan 	&surplus_hugepages_attr.attr,
154506808b08SLee Schermerhorn #ifdef CONFIG_NUMA
154606808b08SLee Schermerhorn 	&nr_hugepages_mempolicy_attr.attr,
154706808b08SLee Schermerhorn #endif
1548a3437870SNishanth Aravamudan 	NULL,
1549a3437870SNishanth Aravamudan };
1550a3437870SNishanth Aravamudan 
1551a3437870SNishanth Aravamudan static struct attribute_group hstate_attr_group = {
1552a3437870SNishanth Aravamudan 	.attrs = hstate_attrs,
1553a3437870SNishanth Aravamudan };
1554a3437870SNishanth Aravamudan 
1555094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
15569a305230SLee Schermerhorn 				    struct kobject **hstate_kobjs,
15579a305230SLee Schermerhorn 				    struct attribute_group *hstate_attr_group)
1558a3437870SNishanth Aravamudan {
1559a3437870SNishanth Aravamudan 	int retval;
15609a305230SLee Schermerhorn 	int hi = h - hstates;
1561a3437870SNishanth Aravamudan 
15629a305230SLee Schermerhorn 	hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
15639a305230SLee Schermerhorn 	if (!hstate_kobjs[hi])
1564a3437870SNishanth Aravamudan 		return -ENOMEM;
1565a3437870SNishanth Aravamudan 
15669a305230SLee Schermerhorn 	retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
1567a3437870SNishanth Aravamudan 	if (retval)
15689a305230SLee Schermerhorn 		kobject_put(hstate_kobjs[hi]);
1569a3437870SNishanth Aravamudan 
1570a3437870SNishanth Aravamudan 	return retval;
1571a3437870SNishanth Aravamudan }
1572a3437870SNishanth Aravamudan 
1573a3437870SNishanth Aravamudan static void __init hugetlb_sysfs_init(void)
1574a3437870SNishanth Aravamudan {
1575a3437870SNishanth Aravamudan 	struct hstate *h;
1576a3437870SNishanth Aravamudan 	int err;
1577a3437870SNishanth Aravamudan 
1578a3437870SNishanth Aravamudan 	hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
1579a3437870SNishanth Aravamudan 	if (!hugepages_kobj)
1580a3437870SNishanth Aravamudan 		return;
1581a3437870SNishanth Aravamudan 
1582a3437870SNishanth Aravamudan 	for_each_hstate(h) {
15839a305230SLee Schermerhorn 		err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
15849a305230SLee Schermerhorn 					 hstate_kobjs, &hstate_attr_group);
1585a3437870SNishanth Aravamudan 		if (err)
1586a3437870SNishanth Aravamudan 			printk(KERN_ERR "Hugetlb: Unable to add hstate %s",
1587a3437870SNishanth Aravamudan 								h->name);
1588a3437870SNishanth Aravamudan 	}
1589a3437870SNishanth Aravamudan }
1590a3437870SNishanth Aravamudan 
15919a305230SLee Schermerhorn #ifdef CONFIG_NUMA
15929a305230SLee Schermerhorn 
15939a305230SLee Schermerhorn /*
15949a305230SLee Schermerhorn  * node_hstate/s - associate per node hstate attributes, via their kobjects,
159510fbcf4cSKay Sievers  * with node devices in node_devices[] using a parallel array.  The array
159610fbcf4cSKay Sievers  * index of a node device or _hstate == node id.
159710fbcf4cSKay Sievers  * This is here to avoid any static dependency of the node device driver, in
15989a305230SLee Schermerhorn  * the base kernel, on the hugetlb module.
15999a305230SLee Schermerhorn  */
16009a305230SLee Schermerhorn struct node_hstate {
16019a305230SLee Schermerhorn 	struct kobject		*hugepages_kobj;
16029a305230SLee Schermerhorn 	struct kobject		*hstate_kobjs[HUGE_MAX_HSTATE];
16039a305230SLee Schermerhorn };
16049a305230SLee Schermerhorn struct node_hstate node_hstates[MAX_NUMNODES];
16059a305230SLee Schermerhorn 
16069a305230SLee Schermerhorn /*
160710fbcf4cSKay Sievers  * A subset of global hstate attributes for node devices
16089a305230SLee Schermerhorn  */
16099a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = {
16109a305230SLee Schermerhorn 	&nr_hugepages_attr.attr,
16119a305230SLee Schermerhorn 	&free_hugepages_attr.attr,
16129a305230SLee Schermerhorn 	&surplus_hugepages_attr.attr,
16139a305230SLee Schermerhorn 	NULL,
16149a305230SLee Schermerhorn };
16159a305230SLee Schermerhorn 
16169a305230SLee Schermerhorn static struct attribute_group per_node_hstate_attr_group = {
16179a305230SLee Schermerhorn 	.attrs = per_node_hstate_attrs,
16189a305230SLee Schermerhorn };
16199a305230SLee Schermerhorn 
16209a305230SLee Schermerhorn /*
162110fbcf4cSKay Sievers  * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
16229a305230SLee Schermerhorn  * Returns node id via non-NULL nidp.
16239a305230SLee Schermerhorn  */
16249a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
16259a305230SLee Schermerhorn {
16269a305230SLee Schermerhorn 	int nid;
16279a305230SLee Schermerhorn 
16289a305230SLee Schermerhorn 	for (nid = 0; nid < nr_node_ids; nid++) {
16299a305230SLee Schermerhorn 		struct node_hstate *nhs = &node_hstates[nid];
16309a305230SLee Schermerhorn 		int i;
16319a305230SLee Schermerhorn 		for (i = 0; i < HUGE_MAX_HSTATE; i++)
16329a305230SLee Schermerhorn 			if (nhs->hstate_kobjs[i] == kobj) {
16339a305230SLee Schermerhorn 				if (nidp)
16349a305230SLee Schermerhorn 					*nidp = nid;
16359a305230SLee Schermerhorn 				return &hstates[i];
16369a305230SLee Schermerhorn 			}
16379a305230SLee Schermerhorn 	}
16389a305230SLee Schermerhorn 
16399a305230SLee Schermerhorn 	BUG();
16409a305230SLee Schermerhorn 	return NULL;
16419a305230SLee Schermerhorn }
16429a305230SLee Schermerhorn 
16439a305230SLee Schermerhorn /*
164410fbcf4cSKay Sievers  * Unregister hstate attributes from a single node device.
16459a305230SLee Schermerhorn  * No-op if no hstate attributes attached.
16469a305230SLee Schermerhorn  */
16479a305230SLee Schermerhorn void hugetlb_unregister_node(struct node *node)
16489a305230SLee Schermerhorn {
16499a305230SLee Schermerhorn 	struct hstate *h;
165010fbcf4cSKay Sievers 	struct node_hstate *nhs = &node_hstates[node->dev.id];
16519a305230SLee Schermerhorn 
16529a305230SLee Schermerhorn 	if (!nhs->hugepages_kobj)
16539b5e5d0fSLee Schermerhorn 		return;		/* no hstate attributes */
16549a305230SLee Schermerhorn 
16559a305230SLee Schermerhorn 	for_each_hstate(h)
16569a305230SLee Schermerhorn 		if (nhs->hstate_kobjs[h - hstates]) {
16579a305230SLee Schermerhorn 			kobject_put(nhs->hstate_kobjs[h - hstates]);
16589a305230SLee Schermerhorn 			nhs->hstate_kobjs[h - hstates] = NULL;
16599a305230SLee Schermerhorn 		}
16609a305230SLee Schermerhorn 
16619a305230SLee Schermerhorn 	kobject_put(nhs->hugepages_kobj);
16629a305230SLee Schermerhorn 	nhs->hugepages_kobj = NULL;
16639a305230SLee Schermerhorn }
16649a305230SLee Schermerhorn 
16659a305230SLee Schermerhorn /*
166610fbcf4cSKay Sievers  * hugetlb module exit:  unregister hstate attributes from node devices
16679a305230SLee Schermerhorn  * that have them.
16689a305230SLee Schermerhorn  */
16699a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void)
16709a305230SLee Schermerhorn {
16719a305230SLee Schermerhorn 	int nid;
16729a305230SLee Schermerhorn 
16739a305230SLee Schermerhorn 	/*
167410fbcf4cSKay Sievers 	 * disable node device registrations.
16759a305230SLee Schermerhorn 	 */
16769a305230SLee Schermerhorn 	register_hugetlbfs_with_node(NULL, NULL);
16779a305230SLee Schermerhorn 
16789a305230SLee Schermerhorn 	/*
16799a305230SLee Schermerhorn 	 * remove hstate attributes from any nodes that have them.
16809a305230SLee Schermerhorn 	 */
16819a305230SLee Schermerhorn 	for (nid = 0; nid < nr_node_ids; nid++)
16829a305230SLee Schermerhorn 		hugetlb_unregister_node(&node_devices[nid]);
16839a305230SLee Schermerhorn }
16849a305230SLee Schermerhorn 
16859a305230SLee Schermerhorn /*
168610fbcf4cSKay Sievers  * Register hstate attributes for a single node device.
16879a305230SLee Schermerhorn  * No-op if attributes already registered.
16889a305230SLee Schermerhorn  */
16899a305230SLee Schermerhorn void hugetlb_register_node(struct node *node)
16909a305230SLee Schermerhorn {
16919a305230SLee Schermerhorn 	struct hstate *h;
169210fbcf4cSKay Sievers 	struct node_hstate *nhs = &node_hstates[node->dev.id];
16939a305230SLee Schermerhorn 	int err;
16949a305230SLee Schermerhorn 
16959a305230SLee Schermerhorn 	if (nhs->hugepages_kobj)
16969a305230SLee Schermerhorn 		return;		/* already allocated */
16979a305230SLee Schermerhorn 
16989a305230SLee Schermerhorn 	nhs->hugepages_kobj = kobject_create_and_add("hugepages",
169910fbcf4cSKay Sievers 							&node->dev.kobj);
17009a305230SLee Schermerhorn 	if (!nhs->hugepages_kobj)
17019a305230SLee Schermerhorn 		return;
17029a305230SLee Schermerhorn 
17039a305230SLee Schermerhorn 	for_each_hstate(h) {
17049a305230SLee Schermerhorn 		err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
17059a305230SLee Schermerhorn 						nhs->hstate_kobjs,
17069a305230SLee Schermerhorn 						&per_node_hstate_attr_group);
17079a305230SLee Schermerhorn 		if (err) {
17089a305230SLee Schermerhorn 			printk(KERN_ERR "Hugetlb: Unable to add hstate %s"
17099a305230SLee Schermerhorn 					" for node %d\n",
171010fbcf4cSKay Sievers 						h->name, node->dev.id);
17119a305230SLee Schermerhorn 			hugetlb_unregister_node(node);
17129a305230SLee Schermerhorn 			break;
17139a305230SLee Schermerhorn 		}
17149a305230SLee Schermerhorn 	}
17159a305230SLee Schermerhorn }
17169a305230SLee Schermerhorn 
17179a305230SLee Schermerhorn /*
17189b5e5d0fSLee Schermerhorn  * hugetlb init time:  register hstate attributes for all registered node
171910fbcf4cSKay Sievers  * devices of nodes that have memory.  All on-line nodes should have
172010fbcf4cSKay Sievers  * registered their associated device by this time.
17219a305230SLee Schermerhorn  */
17229a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void)
17239a305230SLee Schermerhorn {
17249a305230SLee Schermerhorn 	int nid;
17259a305230SLee Schermerhorn 
17269b5e5d0fSLee Schermerhorn 	for_each_node_state(nid, N_HIGH_MEMORY) {
17279a305230SLee Schermerhorn 		struct node *node = &node_devices[nid];
172810fbcf4cSKay Sievers 		if (node->dev.id == nid)
17299a305230SLee Schermerhorn 			hugetlb_register_node(node);
17309a305230SLee Schermerhorn 	}
17319a305230SLee Schermerhorn 
17329a305230SLee Schermerhorn 	/*
173310fbcf4cSKay Sievers 	 * Let the node device driver know we're here so it can
17349a305230SLee Schermerhorn 	 * [un]register hstate attributes on node hotplug.
17359a305230SLee Schermerhorn 	 */
17369a305230SLee Schermerhorn 	register_hugetlbfs_with_node(hugetlb_register_node,
17379a305230SLee Schermerhorn 				     hugetlb_unregister_node);
17389a305230SLee Schermerhorn }
17399a305230SLee Schermerhorn #else	/* !CONFIG_NUMA */
17409a305230SLee Schermerhorn 
17419a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
17429a305230SLee Schermerhorn {
17439a305230SLee Schermerhorn 	BUG();
17449a305230SLee Schermerhorn 	if (nidp)
17459a305230SLee Schermerhorn 		*nidp = -1;
17469a305230SLee Schermerhorn 	return NULL;
17479a305230SLee Schermerhorn }
17489a305230SLee Schermerhorn 
17499a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) { }
17509a305230SLee Schermerhorn 
17519a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { }
17529a305230SLee Schermerhorn 
17539a305230SLee Schermerhorn #endif
17549a305230SLee Schermerhorn 
1755a3437870SNishanth Aravamudan static void __exit hugetlb_exit(void)
1756a3437870SNishanth Aravamudan {
1757a3437870SNishanth Aravamudan 	struct hstate *h;
1758a3437870SNishanth Aravamudan 
17599a305230SLee Schermerhorn 	hugetlb_unregister_all_nodes();
17609a305230SLee Schermerhorn 
1761a3437870SNishanth Aravamudan 	for_each_hstate(h) {
1762a3437870SNishanth Aravamudan 		kobject_put(hstate_kobjs[h - hstates]);
1763a3437870SNishanth Aravamudan 	}
1764a3437870SNishanth Aravamudan 
1765a3437870SNishanth Aravamudan 	kobject_put(hugepages_kobj);
1766a3437870SNishanth Aravamudan }
1767a3437870SNishanth Aravamudan module_exit(hugetlb_exit);
1768a3437870SNishanth Aravamudan 
1769a3437870SNishanth Aravamudan static int __init hugetlb_init(void)
1770a3437870SNishanth Aravamudan {
17710ef89d25SBenjamin Herrenschmidt 	/* Some platform decide whether they support huge pages at boot
17720ef89d25SBenjamin Herrenschmidt 	 * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
17730ef89d25SBenjamin Herrenschmidt 	 * there is no such support
17740ef89d25SBenjamin Herrenschmidt 	 */
17750ef89d25SBenjamin Herrenschmidt 	if (HPAGE_SHIFT == 0)
17760ef89d25SBenjamin Herrenschmidt 		return 0;
1777a3437870SNishanth Aravamudan 
1778e11bfbfcSNick Piggin 	if (!size_to_hstate(default_hstate_size)) {
1779e11bfbfcSNick Piggin 		default_hstate_size = HPAGE_SIZE;
1780e11bfbfcSNick Piggin 		if (!size_to_hstate(default_hstate_size))
1781a3437870SNishanth Aravamudan 			hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
1782a3437870SNishanth Aravamudan 	}
1783e11bfbfcSNick Piggin 	default_hstate_idx = size_to_hstate(default_hstate_size) - hstates;
1784e11bfbfcSNick Piggin 	if (default_hstate_max_huge_pages)
1785e11bfbfcSNick Piggin 		default_hstate.max_huge_pages = default_hstate_max_huge_pages;
1786a3437870SNishanth Aravamudan 
1787a3437870SNishanth Aravamudan 	hugetlb_init_hstates();
1788a3437870SNishanth Aravamudan 
1789aa888a74SAndi Kleen 	gather_bootmem_prealloc();
1790aa888a74SAndi Kleen 
1791a3437870SNishanth Aravamudan 	report_hugepages();
1792a3437870SNishanth Aravamudan 
1793a3437870SNishanth Aravamudan 	hugetlb_sysfs_init();
1794a3437870SNishanth Aravamudan 
17959a305230SLee Schermerhorn 	hugetlb_register_all_nodes();
17969a305230SLee Schermerhorn 
1797a3437870SNishanth Aravamudan 	return 0;
1798a3437870SNishanth Aravamudan }
1799a3437870SNishanth Aravamudan module_init(hugetlb_init);
1800a3437870SNishanth Aravamudan 
1801a3437870SNishanth Aravamudan /* Should be called on processing a hugepagesz=... option */
1802a3437870SNishanth Aravamudan void __init hugetlb_add_hstate(unsigned order)
1803a3437870SNishanth Aravamudan {
1804a3437870SNishanth Aravamudan 	struct hstate *h;
18058faa8b07SAndi Kleen 	unsigned long i;
18068faa8b07SAndi Kleen 
1807a3437870SNishanth Aravamudan 	if (size_to_hstate(PAGE_SIZE << order)) {
1808a3437870SNishanth Aravamudan 		printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
1809a3437870SNishanth Aravamudan 		return;
1810a3437870SNishanth Aravamudan 	}
1811a3437870SNishanth Aravamudan 	BUG_ON(max_hstate >= HUGE_MAX_HSTATE);
1812a3437870SNishanth Aravamudan 	BUG_ON(order == 0);
1813a3437870SNishanth Aravamudan 	h = &hstates[max_hstate++];
1814a3437870SNishanth Aravamudan 	h->order = order;
1815a3437870SNishanth Aravamudan 	h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
18168faa8b07SAndi Kleen 	h->nr_huge_pages = 0;
18178faa8b07SAndi Kleen 	h->free_huge_pages = 0;
18188faa8b07SAndi Kleen 	for (i = 0; i < MAX_NUMNODES; ++i)
18198faa8b07SAndi Kleen 		INIT_LIST_HEAD(&h->hugepage_freelists[i]);
18209b5e5d0fSLee Schermerhorn 	h->next_nid_to_alloc = first_node(node_states[N_HIGH_MEMORY]);
18219b5e5d0fSLee Schermerhorn 	h->next_nid_to_free = first_node(node_states[N_HIGH_MEMORY]);
1822a3437870SNishanth Aravamudan 	snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
1823a3437870SNishanth Aravamudan 					huge_page_size(h)/1024);
18248faa8b07SAndi Kleen 
1825a3437870SNishanth Aravamudan 	parsed_hstate = h;
1826a3437870SNishanth Aravamudan }
1827a3437870SNishanth Aravamudan 
1828e11bfbfcSNick Piggin static int __init hugetlb_nrpages_setup(char *s)
1829a3437870SNishanth Aravamudan {
1830a3437870SNishanth Aravamudan 	unsigned long *mhp;
18318faa8b07SAndi Kleen 	static unsigned long *last_mhp;
1832a3437870SNishanth Aravamudan 
1833a3437870SNishanth Aravamudan 	/*
1834a3437870SNishanth Aravamudan 	 * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
1835a3437870SNishanth Aravamudan 	 * so this hugepages= parameter goes to the "default hstate".
1836a3437870SNishanth Aravamudan 	 */
1837a3437870SNishanth Aravamudan 	if (!max_hstate)
1838a3437870SNishanth Aravamudan 		mhp = &default_hstate_max_huge_pages;
1839a3437870SNishanth Aravamudan 	else
1840a3437870SNishanth Aravamudan 		mhp = &parsed_hstate->max_huge_pages;
1841a3437870SNishanth Aravamudan 
18428faa8b07SAndi Kleen 	if (mhp == last_mhp) {
18438faa8b07SAndi Kleen 		printk(KERN_WARNING "hugepages= specified twice without "
18448faa8b07SAndi Kleen 			"interleaving hugepagesz=, ignoring\n");
18458faa8b07SAndi Kleen 		return 1;
18468faa8b07SAndi Kleen 	}
18478faa8b07SAndi Kleen 
1848a3437870SNishanth Aravamudan 	if (sscanf(s, "%lu", mhp) <= 0)
1849a3437870SNishanth Aravamudan 		*mhp = 0;
1850a3437870SNishanth Aravamudan 
18518faa8b07SAndi Kleen 	/*
18528faa8b07SAndi Kleen 	 * Global state is always initialized later in hugetlb_init.
18538faa8b07SAndi Kleen 	 * But we need to allocate >= MAX_ORDER hstates here early to still
18548faa8b07SAndi Kleen 	 * use the bootmem allocator.
18558faa8b07SAndi Kleen 	 */
18568faa8b07SAndi Kleen 	if (max_hstate && parsed_hstate->order >= MAX_ORDER)
18578faa8b07SAndi Kleen 		hugetlb_hstate_alloc_pages(parsed_hstate);
18588faa8b07SAndi Kleen 
18598faa8b07SAndi Kleen 	last_mhp = mhp;
18608faa8b07SAndi Kleen 
1861a3437870SNishanth Aravamudan 	return 1;
1862a3437870SNishanth Aravamudan }
1863e11bfbfcSNick Piggin __setup("hugepages=", hugetlb_nrpages_setup);
1864e11bfbfcSNick Piggin 
1865e11bfbfcSNick Piggin static int __init hugetlb_default_setup(char *s)
1866e11bfbfcSNick Piggin {
1867e11bfbfcSNick Piggin 	default_hstate_size = memparse(s, &s);
1868e11bfbfcSNick Piggin 	return 1;
1869e11bfbfcSNick Piggin }
1870e11bfbfcSNick Piggin __setup("default_hugepagesz=", hugetlb_default_setup);
1871a3437870SNishanth Aravamudan 
18728a213460SNishanth Aravamudan static unsigned int cpuset_mems_nr(unsigned int *array)
18738a213460SNishanth Aravamudan {
18748a213460SNishanth Aravamudan 	int node;
18758a213460SNishanth Aravamudan 	unsigned int nr = 0;
18768a213460SNishanth Aravamudan 
18778a213460SNishanth Aravamudan 	for_each_node_mask(node, cpuset_current_mems_allowed)
18788a213460SNishanth Aravamudan 		nr += array[node];
18798a213460SNishanth Aravamudan 
18808a213460SNishanth Aravamudan 	return nr;
18818a213460SNishanth Aravamudan }
18828a213460SNishanth Aravamudan 
18838a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL
188406808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
188506808b08SLee Schermerhorn 			 struct ctl_table *table, int write,
188606808b08SLee Schermerhorn 			 void __user *buffer, size_t *length, loff_t *ppos)
18871da177e4SLinus Torvalds {
1888e5ff2159SAndi Kleen 	struct hstate *h = &default_hstate;
1889e5ff2159SAndi Kleen 	unsigned long tmp;
189008d4a246SMichal Hocko 	int ret;
1891e5ff2159SAndi Kleen 
1892e5ff2159SAndi Kleen 	tmp = h->max_huge_pages;
1893e5ff2159SAndi Kleen 
1894adbe8726SEric B Munson 	if (write && h->order >= MAX_ORDER)
1895adbe8726SEric B Munson 		return -EINVAL;
1896adbe8726SEric B Munson 
1897e5ff2159SAndi Kleen 	table->data = &tmp;
1898e5ff2159SAndi Kleen 	table->maxlen = sizeof(unsigned long);
189908d4a246SMichal Hocko 	ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
190008d4a246SMichal Hocko 	if (ret)
190108d4a246SMichal Hocko 		goto out;
1902e5ff2159SAndi Kleen 
190306808b08SLee Schermerhorn 	if (write) {
1904bad44b5bSDavid Rientjes 		NODEMASK_ALLOC(nodemask_t, nodes_allowed,
1905bad44b5bSDavid Rientjes 						GFP_KERNEL | __GFP_NORETRY);
190606808b08SLee Schermerhorn 		if (!(obey_mempolicy &&
190706808b08SLee Schermerhorn 			       init_nodemask_of_mempolicy(nodes_allowed))) {
190806808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
190906808b08SLee Schermerhorn 			nodes_allowed = &node_states[N_HIGH_MEMORY];
191006808b08SLee Schermerhorn 		}
191106808b08SLee Schermerhorn 		h->max_huge_pages = set_max_huge_pages(h, tmp, nodes_allowed);
191206808b08SLee Schermerhorn 
191306808b08SLee Schermerhorn 		if (nodes_allowed != &node_states[N_HIGH_MEMORY])
191406808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
191506808b08SLee Schermerhorn 	}
191608d4a246SMichal Hocko out:
191708d4a246SMichal Hocko 	return ret;
19181da177e4SLinus Torvalds }
1919396faf03SMel Gorman 
192006808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write,
192106808b08SLee Schermerhorn 			  void __user *buffer, size_t *length, loff_t *ppos)
192206808b08SLee Schermerhorn {
192306808b08SLee Schermerhorn 
192406808b08SLee Schermerhorn 	return hugetlb_sysctl_handler_common(false, table, write,
192506808b08SLee Schermerhorn 							buffer, length, ppos);
192606808b08SLee Schermerhorn }
192706808b08SLee Schermerhorn 
192806808b08SLee Schermerhorn #ifdef CONFIG_NUMA
192906808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
193006808b08SLee Schermerhorn 			  void __user *buffer, size_t *length, loff_t *ppos)
193106808b08SLee Schermerhorn {
193206808b08SLee Schermerhorn 	return hugetlb_sysctl_handler_common(true, table, write,
193306808b08SLee Schermerhorn 							buffer, length, ppos);
193406808b08SLee Schermerhorn }
193506808b08SLee Schermerhorn #endif /* CONFIG_NUMA */
193606808b08SLee Schermerhorn 
1937396faf03SMel Gorman int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
19388d65af78SAlexey Dobriyan 			void __user *buffer,
1939396faf03SMel Gorman 			size_t *length, loff_t *ppos)
1940396faf03SMel Gorman {
19418d65af78SAlexey Dobriyan 	proc_dointvec(table, write, buffer, length, ppos);
1942396faf03SMel Gorman 	if (hugepages_treat_as_movable)
1943396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
1944396faf03SMel Gorman 	else
1945396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER;
1946396faf03SMel Gorman 	return 0;
1947396faf03SMel Gorman }
1948396faf03SMel Gorman 
1949a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write,
19508d65af78SAlexey Dobriyan 			void __user *buffer,
1951a3d0c6aaSNishanth Aravamudan 			size_t *length, loff_t *ppos)
1952a3d0c6aaSNishanth Aravamudan {
1953a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
1954e5ff2159SAndi Kleen 	unsigned long tmp;
195508d4a246SMichal Hocko 	int ret;
1956e5ff2159SAndi Kleen 
1957e5ff2159SAndi Kleen 	tmp = h->nr_overcommit_huge_pages;
1958e5ff2159SAndi Kleen 
1959adbe8726SEric B Munson 	if (write && h->order >= MAX_ORDER)
1960adbe8726SEric B Munson 		return -EINVAL;
1961adbe8726SEric B Munson 
1962e5ff2159SAndi Kleen 	table->data = &tmp;
1963e5ff2159SAndi Kleen 	table->maxlen = sizeof(unsigned long);
196408d4a246SMichal Hocko 	ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
196508d4a246SMichal Hocko 	if (ret)
196608d4a246SMichal Hocko 		goto out;
1967e5ff2159SAndi Kleen 
1968e5ff2159SAndi Kleen 	if (write) {
1969064d9efeSNishanth Aravamudan 		spin_lock(&hugetlb_lock);
1970e5ff2159SAndi Kleen 		h->nr_overcommit_huge_pages = tmp;
1971a3d0c6aaSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
1972e5ff2159SAndi Kleen 	}
197308d4a246SMichal Hocko out:
197408d4a246SMichal Hocko 	return ret;
1975a3d0c6aaSNishanth Aravamudan }
1976a3d0c6aaSNishanth Aravamudan 
19771da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */
19781da177e4SLinus Torvalds 
1979e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m)
19801da177e4SLinus Torvalds {
1981a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
1982e1759c21SAlexey Dobriyan 	seq_printf(m,
19831da177e4SLinus Torvalds 			"HugePages_Total:   %5lu\n"
19841da177e4SLinus Torvalds 			"HugePages_Free:    %5lu\n"
1985b45b5bd6SDavid Gibson 			"HugePages_Rsvd:    %5lu\n"
19867893d1d5SAdam Litke 			"HugePages_Surp:    %5lu\n"
19874f98a2feSRik van Riel 			"Hugepagesize:   %8lu kB\n",
1988a5516438SAndi Kleen 			h->nr_huge_pages,
1989a5516438SAndi Kleen 			h->free_huge_pages,
1990a5516438SAndi Kleen 			h->resv_huge_pages,
1991a5516438SAndi Kleen 			h->surplus_huge_pages,
1992a5516438SAndi Kleen 			1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf)
19961da177e4SLinus Torvalds {
1997a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
19981da177e4SLinus Torvalds 	return sprintf(buf,
19991da177e4SLinus Torvalds 		"Node %d HugePages_Total: %5u\n"
2000a1de0919SNishanth Aravamudan 		"Node %d HugePages_Free:  %5u\n"
2001a1de0919SNishanth Aravamudan 		"Node %d HugePages_Surp:  %5u\n",
2002a5516438SAndi Kleen 		nid, h->nr_huge_pages_node[nid],
2003a5516438SAndi Kleen 		nid, h->free_huge_pages_node[nid],
2004a5516438SAndi Kleen 		nid, h->surplus_huge_pages_node[nid]);
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
20081da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void)
20091da177e4SLinus Torvalds {
2010a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
2011a5516438SAndi Kleen 	return h->nr_huge_pages * pages_per_huge_page(h);
20121da177e4SLinus Torvalds }
20131da177e4SLinus Torvalds 
2014a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta)
2015fc1b8a73SMel Gorman {
2016fc1b8a73SMel Gorman 	int ret = -ENOMEM;
2017fc1b8a73SMel Gorman 
2018fc1b8a73SMel Gorman 	spin_lock(&hugetlb_lock);
2019fc1b8a73SMel Gorman 	/*
2020fc1b8a73SMel Gorman 	 * When cpuset is configured, it breaks the strict hugetlb page
2021fc1b8a73SMel Gorman 	 * reservation as the accounting is done on a global variable. Such
2022fc1b8a73SMel Gorman 	 * reservation is completely rubbish in the presence of cpuset because
2023fc1b8a73SMel Gorman 	 * the reservation is not checked against page availability for the
2024fc1b8a73SMel Gorman 	 * current cpuset. Application can still potentially OOM'ed by kernel
2025fc1b8a73SMel Gorman 	 * with lack of free htlb page in cpuset that the task is in.
2026fc1b8a73SMel Gorman 	 * Attempt to enforce strict accounting with cpuset is almost
2027fc1b8a73SMel Gorman 	 * impossible (or too ugly) because cpuset is too fluid that
2028fc1b8a73SMel Gorman 	 * task or memory node can be dynamically moved between cpusets.
2029fc1b8a73SMel Gorman 	 *
2030fc1b8a73SMel Gorman 	 * The change of semantics for shared hugetlb mapping with cpuset is
2031fc1b8a73SMel Gorman 	 * undesirable. However, in order to preserve some of the semantics,
2032fc1b8a73SMel Gorman 	 * we fall back to check against current free page availability as
2033fc1b8a73SMel Gorman 	 * a best attempt and hopefully to minimize the impact of changing
2034fc1b8a73SMel Gorman 	 * semantics that cpuset has.
2035fc1b8a73SMel Gorman 	 */
2036fc1b8a73SMel Gorman 	if (delta > 0) {
2037a5516438SAndi Kleen 		if (gather_surplus_pages(h, delta) < 0)
2038fc1b8a73SMel Gorman 			goto out;
2039fc1b8a73SMel Gorman 
2040a5516438SAndi Kleen 		if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
2041a5516438SAndi Kleen 			return_unused_surplus_pages(h, delta);
2042fc1b8a73SMel Gorman 			goto out;
2043fc1b8a73SMel Gorman 		}
2044fc1b8a73SMel Gorman 	}
2045fc1b8a73SMel Gorman 
2046fc1b8a73SMel Gorman 	ret = 0;
2047fc1b8a73SMel Gorman 	if (delta < 0)
2048a5516438SAndi Kleen 		return_unused_surplus_pages(h, (unsigned long) -delta);
2049fc1b8a73SMel Gorman 
2050fc1b8a73SMel Gorman out:
2051fc1b8a73SMel Gorman 	spin_unlock(&hugetlb_lock);
2052fc1b8a73SMel Gorman 	return ret;
2053fc1b8a73SMel Gorman }
2054fc1b8a73SMel Gorman 
205584afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma)
205684afd99bSAndy Whitcroft {
205784afd99bSAndy Whitcroft 	struct resv_map *reservations = vma_resv_map(vma);
205884afd99bSAndy Whitcroft 
205984afd99bSAndy Whitcroft 	/*
206084afd99bSAndy Whitcroft 	 * This new VMA should share its siblings reservation map if present.
206184afd99bSAndy Whitcroft 	 * The VMA will only ever have a valid reservation map pointer where
206284afd99bSAndy Whitcroft 	 * it is being copied for another still existing VMA.  As that VMA
206325985edcSLucas De Marchi 	 * has a reference to the reservation map it cannot disappear until
206484afd99bSAndy Whitcroft 	 * after this open call completes.  It is therefore safe to take a
206584afd99bSAndy Whitcroft 	 * new reference here without additional locking.
206684afd99bSAndy Whitcroft 	 */
206784afd99bSAndy Whitcroft 	if (reservations)
206884afd99bSAndy Whitcroft 		kref_get(&reservations->refs);
206984afd99bSAndy Whitcroft }
207084afd99bSAndy Whitcroft 
2071a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma)
2072a1e78772SMel Gorman {
2073a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
207484afd99bSAndy Whitcroft 	struct resv_map *reservations = vma_resv_map(vma);
207584afd99bSAndy Whitcroft 	unsigned long reserve;
207684afd99bSAndy Whitcroft 	unsigned long start;
207784afd99bSAndy Whitcroft 	unsigned long end;
207884afd99bSAndy Whitcroft 
207984afd99bSAndy Whitcroft 	if (reservations) {
2080a5516438SAndi Kleen 		start = vma_hugecache_offset(h, vma, vma->vm_start);
2081a5516438SAndi Kleen 		end = vma_hugecache_offset(h, vma, vma->vm_end);
208284afd99bSAndy Whitcroft 
208384afd99bSAndy Whitcroft 		reserve = (end - start) -
208484afd99bSAndy Whitcroft 			region_count(&reservations->regions, start, end);
208584afd99bSAndy Whitcroft 
208684afd99bSAndy Whitcroft 		kref_put(&reservations->refs, resv_map_release);
208784afd99bSAndy Whitcroft 
20887251ff78SAdam Litke 		if (reserve) {
2089a5516438SAndi Kleen 			hugetlb_acct_memory(h, -reserve);
20907251ff78SAdam Litke 			hugetlb_put_quota(vma->vm_file->f_mapping, reserve);
20917251ff78SAdam Litke 		}
2092a1e78772SMel Gorman 	}
209384afd99bSAndy Whitcroft }
2094a1e78772SMel Gorman 
20951da177e4SLinus Torvalds /*
20961da177e4SLinus Torvalds  * We cannot handle pagefaults against hugetlb pages at all.  They cause
20971da177e4SLinus Torvalds  * handle_mm_fault() to try to instantiate regular-sized pages in the
20981da177e4SLinus Torvalds  * hugegpage VMA.  do_page_fault() is supposed to trap this, so BUG is we get
20991da177e4SLinus Torvalds  * this far.
21001da177e4SLinus Torvalds  */
2101d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
21021da177e4SLinus Torvalds {
21031da177e4SLinus Torvalds 	BUG();
2104d0217ac0SNick Piggin 	return 0;
21051da177e4SLinus Torvalds }
21061da177e4SLinus Torvalds 
2107f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = {
2108d0217ac0SNick Piggin 	.fault = hugetlb_vm_op_fault,
210984afd99bSAndy Whitcroft 	.open = hugetlb_vm_op_open,
2110a1e78772SMel Gorman 	.close = hugetlb_vm_op_close,
21111da177e4SLinus Torvalds };
21121da177e4SLinus Torvalds 
21131e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
21141e8f889bSDavid Gibson 				int writable)
211563551ae0SDavid Gibson {
211663551ae0SDavid Gibson 	pte_t entry;
211763551ae0SDavid Gibson 
21181e8f889bSDavid Gibson 	if (writable) {
211963551ae0SDavid Gibson 		entry =
212063551ae0SDavid Gibson 		    pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
212163551ae0SDavid Gibson 	} else {
21227f2e9525SGerald Schaefer 		entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
212363551ae0SDavid Gibson 	}
212463551ae0SDavid Gibson 	entry = pte_mkyoung(entry);
212563551ae0SDavid Gibson 	entry = pte_mkhuge(entry);
212663551ae0SDavid Gibson 
212763551ae0SDavid Gibson 	return entry;
212863551ae0SDavid Gibson }
212963551ae0SDavid Gibson 
21301e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma,
21311e8f889bSDavid Gibson 				   unsigned long address, pte_t *ptep)
21321e8f889bSDavid Gibson {
21331e8f889bSDavid Gibson 	pte_t entry;
21341e8f889bSDavid Gibson 
21357f2e9525SGerald Schaefer 	entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
213632f84528SChris Forbes 	if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
21374b3073e1SRussell King 		update_mmu_cache(vma, address, ptep);
21381e8f889bSDavid Gibson }
21391e8f889bSDavid Gibson 
21401e8f889bSDavid Gibson 
214163551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
214263551ae0SDavid Gibson 			    struct vm_area_struct *vma)
214363551ae0SDavid Gibson {
214463551ae0SDavid Gibson 	pte_t *src_pte, *dst_pte, entry;
214563551ae0SDavid Gibson 	struct page *ptepage;
21461c59827dSHugh Dickins 	unsigned long addr;
21471e8f889bSDavid Gibson 	int cow;
2148a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2149a5516438SAndi Kleen 	unsigned long sz = huge_page_size(h);
21501e8f889bSDavid Gibson 
21511e8f889bSDavid Gibson 	cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
215263551ae0SDavid Gibson 
2153a5516438SAndi Kleen 	for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
2154c74df32cSHugh Dickins 		src_pte = huge_pte_offset(src, addr);
2155c74df32cSHugh Dickins 		if (!src_pte)
2156c74df32cSHugh Dickins 			continue;
2157a5516438SAndi Kleen 		dst_pte = huge_pte_alloc(dst, addr, sz);
215863551ae0SDavid Gibson 		if (!dst_pte)
215963551ae0SDavid Gibson 			goto nomem;
2160c5c99429SLarry Woodman 
2161c5c99429SLarry Woodman 		/* If the pagetables are shared don't copy or take references */
2162c5c99429SLarry Woodman 		if (dst_pte == src_pte)
2163c5c99429SLarry Woodman 			continue;
2164c5c99429SLarry Woodman 
2165c74df32cSHugh Dickins 		spin_lock(&dst->page_table_lock);
216646478758SNick Piggin 		spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
21677f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(src_pte))) {
21681e8f889bSDavid Gibson 			if (cow)
21697f2e9525SGerald Schaefer 				huge_ptep_set_wrprotect(src, addr, src_pte);
21707f2e9525SGerald Schaefer 			entry = huge_ptep_get(src_pte);
217163551ae0SDavid Gibson 			ptepage = pte_page(entry);
217263551ae0SDavid Gibson 			get_page(ptepage);
21730fe6e20bSNaoya Horiguchi 			page_dup_rmap(ptepage);
217463551ae0SDavid Gibson 			set_huge_pte_at(dst, addr, dst_pte, entry);
21751c59827dSHugh Dickins 		}
21761c59827dSHugh Dickins 		spin_unlock(&src->page_table_lock);
2177c74df32cSHugh Dickins 		spin_unlock(&dst->page_table_lock);
217863551ae0SDavid Gibson 	}
217963551ae0SDavid Gibson 	return 0;
218063551ae0SDavid Gibson 
218163551ae0SDavid Gibson nomem:
218263551ae0SDavid Gibson 	return -ENOMEM;
218363551ae0SDavid Gibson }
218463551ae0SDavid Gibson 
2185290408d4SNaoya Horiguchi static int is_hugetlb_entry_migration(pte_t pte)
2186290408d4SNaoya Horiguchi {
2187290408d4SNaoya Horiguchi 	swp_entry_t swp;
2188290408d4SNaoya Horiguchi 
2189290408d4SNaoya Horiguchi 	if (huge_pte_none(pte) || pte_present(pte))
2190290408d4SNaoya Horiguchi 		return 0;
2191290408d4SNaoya Horiguchi 	swp = pte_to_swp_entry(pte);
219232f84528SChris Forbes 	if (non_swap_entry(swp) && is_migration_entry(swp))
2193290408d4SNaoya Horiguchi 		return 1;
219432f84528SChris Forbes 	else
2195290408d4SNaoya Horiguchi 		return 0;
2196290408d4SNaoya Horiguchi }
2197290408d4SNaoya Horiguchi 
2198fd6a03edSNaoya Horiguchi static int is_hugetlb_entry_hwpoisoned(pte_t pte)
2199fd6a03edSNaoya Horiguchi {
2200fd6a03edSNaoya Horiguchi 	swp_entry_t swp;
2201fd6a03edSNaoya Horiguchi 
2202fd6a03edSNaoya Horiguchi 	if (huge_pte_none(pte) || pte_present(pte))
2203fd6a03edSNaoya Horiguchi 		return 0;
2204fd6a03edSNaoya Horiguchi 	swp = pte_to_swp_entry(pte);
220532f84528SChris Forbes 	if (non_swap_entry(swp) && is_hwpoison_entry(swp))
2206fd6a03edSNaoya Horiguchi 		return 1;
220732f84528SChris Forbes 	else
2208fd6a03edSNaoya Horiguchi 		return 0;
2209fd6a03edSNaoya Horiguchi }
2210fd6a03edSNaoya Horiguchi 
2211502717f4SChen, Kenneth W void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
221204f2cbe3SMel Gorman 			    unsigned long end, struct page *ref_page)
221363551ae0SDavid Gibson {
221463551ae0SDavid Gibson 	struct mm_struct *mm = vma->vm_mm;
221563551ae0SDavid Gibson 	unsigned long address;
2216c7546f8fSDavid Gibson 	pte_t *ptep;
221763551ae0SDavid Gibson 	pte_t pte;
221863551ae0SDavid Gibson 	struct page *page;
2219fe1668aeSChen, Kenneth W 	struct page *tmp;
2220a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2221a5516438SAndi Kleen 	unsigned long sz = huge_page_size(h);
2222a5516438SAndi Kleen 
2223c0a499c2SChen, Kenneth W 	/*
22243d48ae45SPeter Zijlstra 	 * A page gathering list, protected by per file i_mmap_mutex. The
2225c0a499c2SChen, Kenneth W 	 * lock is used to avoid list corruption from multiple unmapping
2226c0a499c2SChen, Kenneth W 	 * of the same page since we are using page->lru.
2227c0a499c2SChen, Kenneth W 	 */
2228fe1668aeSChen, Kenneth W 	LIST_HEAD(page_list);
222963551ae0SDavid Gibson 
223063551ae0SDavid Gibson 	WARN_ON(!is_vm_hugetlb_page(vma));
2231a5516438SAndi Kleen 	BUG_ON(start & ~huge_page_mask(h));
2232a5516438SAndi Kleen 	BUG_ON(end & ~huge_page_mask(h));
223363551ae0SDavid Gibson 
2234cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_start(mm, start, end);
2235508034a3SHugh Dickins 	spin_lock(&mm->page_table_lock);
2236a5516438SAndi Kleen 	for (address = start; address < end; address += sz) {
2237c7546f8fSDavid Gibson 		ptep = huge_pte_offset(mm, address);
2238c7546f8fSDavid Gibson 		if (!ptep)
2239c7546f8fSDavid Gibson 			continue;
2240c7546f8fSDavid Gibson 
224139dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
224239dde65cSChen, Kenneth W 			continue;
224339dde65cSChen, Kenneth W 
224404f2cbe3SMel Gorman 		/*
224504f2cbe3SMel Gorman 		 * If a reference page is supplied, it is because a specific
224604f2cbe3SMel Gorman 		 * page is being unmapped, not a range. Ensure the page we
224704f2cbe3SMel Gorman 		 * are about to unmap is the actual page of interest.
224804f2cbe3SMel Gorman 		 */
224904f2cbe3SMel Gorman 		if (ref_page) {
225004f2cbe3SMel Gorman 			pte = huge_ptep_get(ptep);
225104f2cbe3SMel Gorman 			if (huge_pte_none(pte))
225204f2cbe3SMel Gorman 				continue;
225304f2cbe3SMel Gorman 			page = pte_page(pte);
225404f2cbe3SMel Gorman 			if (page != ref_page)
225504f2cbe3SMel Gorman 				continue;
225604f2cbe3SMel Gorman 
225704f2cbe3SMel Gorman 			/*
225804f2cbe3SMel Gorman 			 * Mark the VMA as having unmapped its page so that
225904f2cbe3SMel Gorman 			 * future faults in this VMA will fail rather than
226004f2cbe3SMel Gorman 			 * looking like data was lost
226104f2cbe3SMel Gorman 			 */
226204f2cbe3SMel Gorman 			set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
226304f2cbe3SMel Gorman 		}
226404f2cbe3SMel Gorman 
2265c7546f8fSDavid Gibson 		pte = huge_ptep_get_and_clear(mm, address, ptep);
22667f2e9525SGerald Schaefer 		if (huge_pte_none(pte))
226763551ae0SDavid Gibson 			continue;
2268c7546f8fSDavid Gibson 
2269fd6a03edSNaoya Horiguchi 		/*
2270fd6a03edSNaoya Horiguchi 		 * HWPoisoned hugepage is already unmapped and dropped reference
2271fd6a03edSNaoya Horiguchi 		 */
2272fd6a03edSNaoya Horiguchi 		if (unlikely(is_hugetlb_entry_hwpoisoned(pte)))
2273fd6a03edSNaoya Horiguchi 			continue;
2274fd6a03edSNaoya Horiguchi 
227563551ae0SDavid Gibson 		page = pte_page(pte);
22766649a386SKen Chen 		if (pte_dirty(pte))
22776649a386SKen Chen 			set_page_dirty(page);
2278fe1668aeSChen, Kenneth W 		list_add(&page->lru, &page_list);
227963551ae0SDavid Gibson 	}
22801da177e4SLinus Torvalds 	spin_unlock(&mm->page_table_lock);
2281508034a3SHugh Dickins 	flush_tlb_range(vma, start, end);
2282cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start, end);
2283fe1668aeSChen, Kenneth W 	list_for_each_entry_safe(page, tmp, &page_list, lru) {
22840fe6e20bSNaoya Horiguchi 		page_remove_rmap(page);
2285fe1668aeSChen, Kenneth W 		list_del(&page->lru);
2286fe1668aeSChen, Kenneth W 		put_page(page);
2287fe1668aeSChen, Kenneth W 	}
22881da177e4SLinus Torvalds }
228963551ae0SDavid Gibson 
2290502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
229104f2cbe3SMel Gorman 			  unsigned long end, struct page *ref_page)
2292502717f4SChen, Kenneth W {
22933d48ae45SPeter Zijlstra 	mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
229404f2cbe3SMel Gorman 	__unmap_hugepage_range(vma, start, end, ref_page);
22953d48ae45SPeter Zijlstra 	mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
2296502717f4SChen, Kenneth W }
2297502717f4SChen, Kenneth W 
229804f2cbe3SMel Gorman /*
229904f2cbe3SMel Gorman  * This is called when the original mapper is failing to COW a MAP_PRIVATE
230004f2cbe3SMel Gorman  * mappping it owns the reserve page for. The intention is to unmap the page
230104f2cbe3SMel Gorman  * from other VMAs and let the children be SIGKILLed if they are faulting the
230204f2cbe3SMel Gorman  * same region.
230304f2cbe3SMel Gorman  */
23042a4b3dedSHarvey Harrison static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
23052a4b3dedSHarvey Harrison 				struct page *page, unsigned long address)
230604f2cbe3SMel Gorman {
23077526674dSAdam Litke 	struct hstate *h = hstate_vma(vma);
230804f2cbe3SMel Gorman 	struct vm_area_struct *iter_vma;
230904f2cbe3SMel Gorman 	struct address_space *mapping;
231004f2cbe3SMel Gorman 	struct prio_tree_iter iter;
231104f2cbe3SMel Gorman 	pgoff_t pgoff;
231204f2cbe3SMel Gorman 
231304f2cbe3SMel Gorman 	/*
231404f2cbe3SMel Gorman 	 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
231504f2cbe3SMel Gorman 	 * from page cache lookup which is in HPAGE_SIZE units.
231604f2cbe3SMel Gorman 	 */
23177526674dSAdam Litke 	address = address & huge_page_mask(h);
231804f2cbe3SMel Gorman 	pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
231904f2cbe3SMel Gorman 		+ (vma->vm_pgoff >> PAGE_SHIFT);
232004f2cbe3SMel Gorman 	mapping = (struct address_space *)page_private(page);
232104f2cbe3SMel Gorman 
23224eb2b1dcSMel Gorman 	/*
23234eb2b1dcSMel Gorman 	 * Take the mapping lock for the duration of the table walk. As
23244eb2b1dcSMel Gorman 	 * this mapping should be shared between all the VMAs,
23254eb2b1dcSMel Gorman 	 * __unmap_hugepage_range() is called as the lock is already held
23264eb2b1dcSMel Gorman 	 */
23273d48ae45SPeter Zijlstra 	mutex_lock(&mapping->i_mmap_mutex);
232804f2cbe3SMel Gorman 	vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
232904f2cbe3SMel Gorman 		/* Do not unmap the current VMA */
233004f2cbe3SMel Gorman 		if (iter_vma == vma)
233104f2cbe3SMel Gorman 			continue;
233204f2cbe3SMel Gorman 
233304f2cbe3SMel Gorman 		/*
233404f2cbe3SMel Gorman 		 * Unmap the page from other VMAs without their own reserves.
233504f2cbe3SMel Gorman 		 * They get marked to be SIGKILLed if they fault in these
233604f2cbe3SMel Gorman 		 * areas. This is because a future no-page fault on this VMA
233704f2cbe3SMel Gorman 		 * could insert a zeroed page instead of the data existing
233804f2cbe3SMel Gorman 		 * from the time of fork. This would look like data corruption
233904f2cbe3SMel Gorman 		 */
234004f2cbe3SMel Gorman 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
23414eb2b1dcSMel Gorman 			__unmap_hugepage_range(iter_vma,
23427526674dSAdam Litke 				address, address + huge_page_size(h),
234304f2cbe3SMel Gorman 				page);
234404f2cbe3SMel Gorman 	}
23453d48ae45SPeter Zijlstra 	mutex_unlock(&mapping->i_mmap_mutex);
234604f2cbe3SMel Gorman 
234704f2cbe3SMel Gorman 	return 1;
234804f2cbe3SMel Gorman }
234904f2cbe3SMel Gorman 
23500fe6e20bSNaoya Horiguchi /*
23510fe6e20bSNaoya Horiguchi  * Hugetlb_cow() should be called with page lock of the original hugepage held.
2352ef009b25SMichal Hocko  * Called with hugetlb_instantiation_mutex held and pte_page locked so we
2353ef009b25SMichal Hocko  * cannot race with other handlers or page migration.
2354ef009b25SMichal Hocko  * Keep the pte_same checks anyway to make transition from the mutex easier.
23550fe6e20bSNaoya Horiguchi  */
23561e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
235704f2cbe3SMel Gorman 			unsigned long address, pte_t *ptep, pte_t pte,
235804f2cbe3SMel Gorman 			struct page *pagecache_page)
23591e8f889bSDavid Gibson {
2360a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
23611e8f889bSDavid Gibson 	struct page *old_page, *new_page;
236279ac6ba4SDavid Gibson 	int avoidcopy;
236304f2cbe3SMel Gorman 	int outside_reserve = 0;
23641e8f889bSDavid Gibson 
23651e8f889bSDavid Gibson 	old_page = pte_page(pte);
23661e8f889bSDavid Gibson 
236704f2cbe3SMel Gorman retry_avoidcopy:
23681e8f889bSDavid Gibson 	/* If no-one else is actually using this page, avoid the copy
23691e8f889bSDavid Gibson 	 * and just make the page writable */
23700fe6e20bSNaoya Horiguchi 	avoidcopy = (page_mapcount(old_page) == 1);
23711e8f889bSDavid Gibson 	if (avoidcopy) {
23720fe6e20bSNaoya Horiguchi 		if (PageAnon(old_page))
23730fe6e20bSNaoya Horiguchi 			page_move_anon_rmap(old_page, vma, address);
23741e8f889bSDavid Gibson 		set_huge_ptep_writable(vma, address, ptep);
237583c54070SNick Piggin 		return 0;
23761e8f889bSDavid Gibson 	}
23771e8f889bSDavid Gibson 
237804f2cbe3SMel Gorman 	/*
237904f2cbe3SMel Gorman 	 * If the process that created a MAP_PRIVATE mapping is about to
238004f2cbe3SMel Gorman 	 * perform a COW due to a shared page count, attempt to satisfy
238104f2cbe3SMel Gorman 	 * the allocation without using the existing reserves. The pagecache
238204f2cbe3SMel Gorman 	 * page is used to determine if the reserve at this address was
238304f2cbe3SMel Gorman 	 * consumed or not. If reserves were used, a partial faulted mapping
238404f2cbe3SMel Gorman 	 * at the time of fork() could consume its reserves on COW instead
238504f2cbe3SMel Gorman 	 * of the full address range.
238604f2cbe3SMel Gorman 	 */
2387f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE) &&
238804f2cbe3SMel Gorman 			is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
238904f2cbe3SMel Gorman 			old_page != pagecache_page)
239004f2cbe3SMel Gorman 		outside_reserve = 1;
239104f2cbe3SMel Gorman 
23921e8f889bSDavid Gibson 	page_cache_get(old_page);
2393b76c8cfbSLarry Woodman 
2394b76c8cfbSLarry Woodman 	/* Drop page_table_lock as buddy allocator may be called */
2395b76c8cfbSLarry Woodman 	spin_unlock(&mm->page_table_lock);
239604f2cbe3SMel Gorman 	new_page = alloc_huge_page(vma, address, outside_reserve);
23971e8f889bSDavid Gibson 
23982fc39cecSAdam Litke 	if (IS_ERR(new_page)) {
23991e8f889bSDavid Gibson 		page_cache_release(old_page);
240004f2cbe3SMel Gorman 
240104f2cbe3SMel Gorman 		/*
240204f2cbe3SMel Gorman 		 * If a process owning a MAP_PRIVATE mapping fails to COW,
240304f2cbe3SMel Gorman 		 * it is due to references held by a child and an insufficient
240404f2cbe3SMel Gorman 		 * huge page pool. To guarantee the original mappers
240504f2cbe3SMel Gorman 		 * reliability, unmap the page from child processes. The child
240604f2cbe3SMel Gorman 		 * may get SIGKILLed if it later faults.
240704f2cbe3SMel Gorman 		 */
240804f2cbe3SMel Gorman 		if (outside_reserve) {
240904f2cbe3SMel Gorman 			BUG_ON(huge_pte_none(pte));
241004f2cbe3SMel Gorman 			if (unmap_ref_private(mm, vma, old_page, address)) {
241104f2cbe3SMel Gorman 				BUG_ON(page_count(old_page) != 1);
241204f2cbe3SMel Gorman 				BUG_ON(huge_pte_none(pte));
2413b76c8cfbSLarry Woodman 				spin_lock(&mm->page_table_lock);
2414a734bcc8SHillf Danton 				ptep = huge_pte_offset(mm, address & huge_page_mask(h));
2415a734bcc8SHillf Danton 				if (likely(pte_same(huge_ptep_get(ptep), pte)))
241604f2cbe3SMel Gorman 					goto retry_avoidcopy;
2417a734bcc8SHillf Danton 				/*
2418a734bcc8SHillf Danton 				 * race occurs while re-acquiring page_table_lock, and
2419a734bcc8SHillf Danton 				 * our job is done.
2420a734bcc8SHillf Danton 				 */
2421a734bcc8SHillf Danton 				return 0;
242204f2cbe3SMel Gorman 			}
242304f2cbe3SMel Gorman 			WARN_ON_ONCE(1);
242404f2cbe3SMel Gorman 		}
242504f2cbe3SMel Gorman 
2426b76c8cfbSLarry Woodman 		/* Caller expects lock to be held */
2427b76c8cfbSLarry Woodman 		spin_lock(&mm->page_table_lock);
24282fc39cecSAdam Litke 		return -PTR_ERR(new_page);
24291e8f889bSDavid Gibson 	}
24301e8f889bSDavid Gibson 
24310fe6e20bSNaoya Horiguchi 	/*
24320fe6e20bSNaoya Horiguchi 	 * When the original hugepage is shared one, it does not have
24330fe6e20bSNaoya Horiguchi 	 * anon_vma prepared.
24340fe6e20bSNaoya Horiguchi 	 */
243544e2aa93SDean Nelson 	if (unlikely(anon_vma_prepare(vma))) {
2436ea4039a3SHillf Danton 		page_cache_release(new_page);
2437ea4039a3SHillf Danton 		page_cache_release(old_page);
243844e2aa93SDean Nelson 		/* Caller expects lock to be held */
243944e2aa93SDean Nelson 		spin_lock(&mm->page_table_lock);
24400fe6e20bSNaoya Horiguchi 		return VM_FAULT_OOM;
244144e2aa93SDean Nelson 	}
24420fe6e20bSNaoya Horiguchi 
244347ad8475SAndrea Arcangeli 	copy_user_huge_page(new_page, old_page, address, vma,
244447ad8475SAndrea Arcangeli 			    pages_per_huge_page(h));
24450ed361deSNick Piggin 	__SetPageUptodate(new_page);
24461e8f889bSDavid Gibson 
2447b76c8cfbSLarry Woodman 	/*
2448b76c8cfbSLarry Woodman 	 * Retake the page_table_lock to check for racing updates
2449b76c8cfbSLarry Woodman 	 * before the page tables are altered
2450b76c8cfbSLarry Woodman 	 */
2451b76c8cfbSLarry Woodman 	spin_lock(&mm->page_table_lock);
2452a5516438SAndi Kleen 	ptep = huge_pte_offset(mm, address & huge_page_mask(h));
24537f2e9525SGerald Schaefer 	if (likely(pte_same(huge_ptep_get(ptep), pte))) {
24541e8f889bSDavid Gibson 		/* Break COW */
24553edd4fc9SDoug Doan 		mmu_notifier_invalidate_range_start(mm,
24563edd4fc9SDoug Doan 			address & huge_page_mask(h),
24573edd4fc9SDoug Doan 			(address & huge_page_mask(h)) + huge_page_size(h));
24588fe627ecSGerald Schaefer 		huge_ptep_clear_flush(vma, address, ptep);
24591e8f889bSDavid Gibson 		set_huge_pte_at(mm, address, ptep,
24601e8f889bSDavid Gibson 				make_huge_pte(vma, new_page, 1));
24610fe6e20bSNaoya Horiguchi 		page_remove_rmap(old_page);
2462cd67f0d2SNaoya Horiguchi 		hugepage_add_new_anon_rmap(new_page, vma, address);
24631e8f889bSDavid Gibson 		/* Make the old page be freed below */
24641e8f889bSDavid Gibson 		new_page = old_page;
24653edd4fc9SDoug Doan 		mmu_notifier_invalidate_range_end(mm,
24663edd4fc9SDoug Doan 			address & huge_page_mask(h),
24673edd4fc9SDoug Doan 			(address & huge_page_mask(h)) + huge_page_size(h));
24681e8f889bSDavid Gibson 	}
24691e8f889bSDavid Gibson 	page_cache_release(new_page);
24701e8f889bSDavid Gibson 	page_cache_release(old_page);
247183c54070SNick Piggin 	return 0;
24721e8f889bSDavid Gibson }
24731e8f889bSDavid Gibson 
247404f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */
2475a5516438SAndi Kleen static struct page *hugetlbfs_pagecache_page(struct hstate *h,
2476a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long address)
247704f2cbe3SMel Gorman {
247804f2cbe3SMel Gorman 	struct address_space *mapping;
2479e7c4b0bfSAndy Whitcroft 	pgoff_t idx;
248004f2cbe3SMel Gorman 
248104f2cbe3SMel Gorman 	mapping = vma->vm_file->f_mapping;
2482a5516438SAndi Kleen 	idx = vma_hugecache_offset(h, vma, address);
248304f2cbe3SMel Gorman 
248404f2cbe3SMel Gorman 	return find_lock_page(mapping, idx);
248504f2cbe3SMel Gorman }
248604f2cbe3SMel Gorman 
24873ae77f43SHugh Dickins /*
24883ae77f43SHugh Dickins  * Return whether there is a pagecache page to back given address within VMA.
24893ae77f43SHugh Dickins  * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
24903ae77f43SHugh Dickins  */
24913ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h,
24922a15efc9SHugh Dickins 			struct vm_area_struct *vma, unsigned long address)
24932a15efc9SHugh Dickins {
24942a15efc9SHugh Dickins 	struct address_space *mapping;
24952a15efc9SHugh Dickins 	pgoff_t idx;
24962a15efc9SHugh Dickins 	struct page *page;
24972a15efc9SHugh Dickins 
24982a15efc9SHugh Dickins 	mapping = vma->vm_file->f_mapping;
24992a15efc9SHugh Dickins 	idx = vma_hugecache_offset(h, vma, address);
25002a15efc9SHugh Dickins 
25012a15efc9SHugh Dickins 	page = find_get_page(mapping, idx);
25022a15efc9SHugh Dickins 	if (page)
25032a15efc9SHugh Dickins 		put_page(page);
25042a15efc9SHugh Dickins 	return page != NULL;
25052a15efc9SHugh Dickins }
25062a15efc9SHugh Dickins 
2507a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
2508788c7df4SHugh Dickins 			unsigned long address, pte_t *ptep, unsigned int flags)
2509ac9b9c66SHugh Dickins {
2510a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2511ac9b9c66SHugh Dickins 	int ret = VM_FAULT_SIGBUS;
2512e7c4b0bfSAndy Whitcroft 	pgoff_t idx;
25134c887265SAdam Litke 	unsigned long size;
25144c887265SAdam Litke 	struct page *page;
25154c887265SAdam Litke 	struct address_space *mapping;
25161e8f889bSDavid Gibson 	pte_t new_pte;
25174c887265SAdam Litke 
251804f2cbe3SMel Gorman 	/*
251904f2cbe3SMel Gorman 	 * Currently, we are forced to kill the process in the event the
252004f2cbe3SMel Gorman 	 * original mapper has unmapped pages from the child due to a failed
252125985edcSLucas De Marchi 	 * COW. Warn that such a situation has occurred as it may not be obvious
252204f2cbe3SMel Gorman 	 */
252304f2cbe3SMel Gorman 	if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
252404f2cbe3SMel Gorman 		printk(KERN_WARNING
252504f2cbe3SMel Gorman 			"PID %d killed due to inadequate hugepage pool\n",
252604f2cbe3SMel Gorman 			current->pid);
252704f2cbe3SMel Gorman 		return ret;
252804f2cbe3SMel Gorman 	}
252904f2cbe3SMel Gorman 
25304c887265SAdam Litke 	mapping = vma->vm_file->f_mapping;
2531a5516438SAndi Kleen 	idx = vma_hugecache_offset(h, vma, address);
25324c887265SAdam Litke 
25334c887265SAdam Litke 	/*
25344c887265SAdam Litke 	 * Use page lock to guard against racing truncation
25354c887265SAdam Litke 	 * before we get page_table_lock.
25364c887265SAdam Litke 	 */
25376bda666aSChristoph Lameter retry:
25386bda666aSChristoph Lameter 	page = find_lock_page(mapping, idx);
25396bda666aSChristoph Lameter 	if (!page) {
2540a5516438SAndi Kleen 		size = i_size_read(mapping->host) >> huge_page_shift(h);
2541ebed4bfcSHugh Dickins 		if (idx >= size)
2542ebed4bfcSHugh Dickins 			goto out;
254304f2cbe3SMel Gorman 		page = alloc_huge_page(vma, address, 0);
25442fc39cecSAdam Litke 		if (IS_ERR(page)) {
25452fc39cecSAdam Litke 			ret = -PTR_ERR(page);
25466bda666aSChristoph Lameter 			goto out;
25476bda666aSChristoph Lameter 		}
254847ad8475SAndrea Arcangeli 		clear_huge_page(page, address, pages_per_huge_page(h));
25490ed361deSNick Piggin 		__SetPageUptodate(page);
2550ac9b9c66SHugh Dickins 
2551f83a275dSMel Gorman 		if (vma->vm_flags & VM_MAYSHARE) {
25526bda666aSChristoph Lameter 			int err;
255345c682a6SKen Chen 			struct inode *inode = mapping->host;
25546bda666aSChristoph Lameter 
25556bda666aSChristoph Lameter 			err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
25566bda666aSChristoph Lameter 			if (err) {
25576bda666aSChristoph Lameter 				put_page(page);
25586bda666aSChristoph Lameter 				if (err == -EEXIST)
25596bda666aSChristoph Lameter 					goto retry;
25606bda666aSChristoph Lameter 				goto out;
25616bda666aSChristoph Lameter 			}
256245c682a6SKen Chen 
256345c682a6SKen Chen 			spin_lock(&inode->i_lock);
2564a5516438SAndi Kleen 			inode->i_blocks += blocks_per_huge_page(h);
256545c682a6SKen Chen 			spin_unlock(&inode->i_lock);
25660fe6e20bSNaoya Horiguchi 			page_dup_rmap(page);
256723be7468SMel Gorman 		} else {
25686bda666aSChristoph Lameter 			lock_page(page);
25690fe6e20bSNaoya Horiguchi 			if (unlikely(anon_vma_prepare(vma))) {
25700fe6e20bSNaoya Horiguchi 				ret = VM_FAULT_OOM;
25710fe6e20bSNaoya Horiguchi 				goto backout_unlocked;
257223be7468SMel Gorman 			}
25730fe6e20bSNaoya Horiguchi 			hugepage_add_new_anon_rmap(page, vma, address);
25740fe6e20bSNaoya Horiguchi 		}
25750fe6e20bSNaoya Horiguchi 	} else {
257657303d80SAndy Whitcroft 		/*
2577998b4382SNaoya Horiguchi 		 * If memory error occurs between mmap() and fault, some process
2578998b4382SNaoya Horiguchi 		 * don't have hwpoisoned swap entry for errored virtual address.
2579998b4382SNaoya Horiguchi 		 * So we need to block hugepage fault by PG_hwpoison bit check.
2580fd6a03edSNaoya Horiguchi 		 */
2581fd6a03edSNaoya Horiguchi 		if (unlikely(PageHWPoison(page))) {
2582aa50d3a7SAndi Kleen 			ret = VM_FAULT_HWPOISON |
2583aa50d3a7SAndi Kleen 			      VM_FAULT_SET_HINDEX(h - hstates);
2584fd6a03edSNaoya Horiguchi 			goto backout_unlocked;
25856bda666aSChristoph Lameter 		}
2586998b4382SNaoya Horiguchi 		page_dup_rmap(page);
2587998b4382SNaoya Horiguchi 	}
25881e8f889bSDavid Gibson 
258957303d80SAndy Whitcroft 	/*
259057303d80SAndy Whitcroft 	 * If we are going to COW a private mapping later, we examine the
259157303d80SAndy Whitcroft 	 * pending reservations for this page now. This will ensure that
259257303d80SAndy Whitcroft 	 * any allocations necessary to record that reservation occur outside
259357303d80SAndy Whitcroft 	 * the spinlock.
259457303d80SAndy Whitcroft 	 */
2595788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
25962b26736cSAndy Whitcroft 		if (vma_needs_reservation(h, vma, address) < 0) {
25972b26736cSAndy Whitcroft 			ret = VM_FAULT_OOM;
25982b26736cSAndy Whitcroft 			goto backout_unlocked;
25992b26736cSAndy Whitcroft 		}
260057303d80SAndy Whitcroft 
2601ac9b9c66SHugh Dickins 	spin_lock(&mm->page_table_lock);
2602a5516438SAndi Kleen 	size = i_size_read(mapping->host) >> huge_page_shift(h);
26034c887265SAdam Litke 	if (idx >= size)
26044c887265SAdam Litke 		goto backout;
26054c887265SAdam Litke 
260683c54070SNick Piggin 	ret = 0;
26077f2e9525SGerald Schaefer 	if (!huge_pte_none(huge_ptep_get(ptep)))
26084c887265SAdam Litke 		goto backout;
26094c887265SAdam Litke 
26101e8f889bSDavid Gibson 	new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
26111e8f889bSDavid Gibson 				&& (vma->vm_flags & VM_SHARED)));
26121e8f889bSDavid Gibson 	set_huge_pte_at(mm, address, ptep, new_pte);
26131e8f889bSDavid Gibson 
2614788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
26151e8f889bSDavid Gibson 		/* Optimization, do the COW without a second fault */
261604f2cbe3SMel Gorman 		ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
26171e8f889bSDavid Gibson 	}
26181e8f889bSDavid Gibson 
2619ac9b9c66SHugh Dickins 	spin_unlock(&mm->page_table_lock);
26204c887265SAdam Litke 	unlock_page(page);
26214c887265SAdam Litke out:
2622ac9b9c66SHugh Dickins 	return ret;
26234c887265SAdam Litke 
26244c887265SAdam Litke backout:
26254c887265SAdam Litke 	spin_unlock(&mm->page_table_lock);
26262b26736cSAndy Whitcroft backout_unlocked:
26274c887265SAdam Litke 	unlock_page(page);
26284c887265SAdam Litke 	put_page(page);
26294c887265SAdam Litke 	goto out;
2630ac9b9c66SHugh Dickins }
2631ac9b9c66SHugh Dickins 
263286e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2633788c7df4SHugh Dickins 			unsigned long address, unsigned int flags)
263486e5216fSAdam Litke {
263586e5216fSAdam Litke 	pte_t *ptep;
263686e5216fSAdam Litke 	pte_t entry;
26371e8f889bSDavid Gibson 	int ret;
26380fe6e20bSNaoya Horiguchi 	struct page *page = NULL;
263957303d80SAndy Whitcroft 	struct page *pagecache_page = NULL;
26403935baa9SDavid Gibson 	static DEFINE_MUTEX(hugetlb_instantiation_mutex);
2641a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
264286e5216fSAdam Litke 
2643fd6a03edSNaoya Horiguchi 	ptep = huge_pte_offset(mm, address);
2644fd6a03edSNaoya Horiguchi 	if (ptep) {
2645fd6a03edSNaoya Horiguchi 		entry = huge_ptep_get(ptep);
2646290408d4SNaoya Horiguchi 		if (unlikely(is_hugetlb_entry_migration(entry))) {
2647290408d4SNaoya Horiguchi 			migration_entry_wait(mm, (pmd_t *)ptep, address);
2648290408d4SNaoya Horiguchi 			return 0;
2649290408d4SNaoya Horiguchi 		} else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
2650aa50d3a7SAndi Kleen 			return VM_FAULT_HWPOISON_LARGE |
2651aa50d3a7SAndi Kleen 			       VM_FAULT_SET_HINDEX(h - hstates);
2652fd6a03edSNaoya Horiguchi 	}
2653fd6a03edSNaoya Horiguchi 
2654a5516438SAndi Kleen 	ptep = huge_pte_alloc(mm, address, huge_page_size(h));
265586e5216fSAdam Litke 	if (!ptep)
265686e5216fSAdam Litke 		return VM_FAULT_OOM;
265786e5216fSAdam Litke 
26583935baa9SDavid Gibson 	/*
26593935baa9SDavid Gibson 	 * Serialize hugepage allocation and instantiation, so that we don't
26603935baa9SDavid Gibson 	 * get spurious allocation failures if two CPUs race to instantiate
26613935baa9SDavid Gibson 	 * the same page in the page cache.
26623935baa9SDavid Gibson 	 */
26633935baa9SDavid Gibson 	mutex_lock(&hugetlb_instantiation_mutex);
26647f2e9525SGerald Schaefer 	entry = huge_ptep_get(ptep);
26657f2e9525SGerald Schaefer 	if (huge_pte_none(entry)) {
2666788c7df4SHugh Dickins 		ret = hugetlb_no_page(mm, vma, address, ptep, flags);
2667b4d1d99fSDavid Gibson 		goto out_mutex;
26683935baa9SDavid Gibson 	}
266986e5216fSAdam Litke 
267083c54070SNick Piggin 	ret = 0;
26711e8f889bSDavid Gibson 
267257303d80SAndy Whitcroft 	/*
267357303d80SAndy Whitcroft 	 * If we are going to COW the mapping later, we examine the pending
267457303d80SAndy Whitcroft 	 * reservations for this page now. This will ensure that any
267557303d80SAndy Whitcroft 	 * allocations necessary to record that reservation occur outside the
267657303d80SAndy Whitcroft 	 * spinlock. For private mappings, we also lookup the pagecache
267757303d80SAndy Whitcroft 	 * page now as it is used to determine if a reservation has been
267857303d80SAndy Whitcroft 	 * consumed.
267957303d80SAndy Whitcroft 	 */
2680788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !pte_write(entry)) {
26812b26736cSAndy Whitcroft 		if (vma_needs_reservation(h, vma, address) < 0) {
26822b26736cSAndy Whitcroft 			ret = VM_FAULT_OOM;
2683b4d1d99fSDavid Gibson 			goto out_mutex;
26842b26736cSAndy Whitcroft 		}
268557303d80SAndy Whitcroft 
2686f83a275dSMel Gorman 		if (!(vma->vm_flags & VM_MAYSHARE))
268757303d80SAndy Whitcroft 			pagecache_page = hugetlbfs_pagecache_page(h,
268857303d80SAndy Whitcroft 								vma, address);
268957303d80SAndy Whitcroft 	}
269057303d80SAndy Whitcroft 
269156c9cfb1SNaoya Horiguchi 	/*
269256c9cfb1SNaoya Horiguchi 	 * hugetlb_cow() requires page locks of pte_page(entry) and
269356c9cfb1SNaoya Horiguchi 	 * pagecache_page, so here we need take the former one
269456c9cfb1SNaoya Horiguchi 	 * when page != pagecache_page or !pagecache_page.
269556c9cfb1SNaoya Horiguchi 	 * Note that locking order is always pagecache_page -> page,
269656c9cfb1SNaoya Horiguchi 	 * so no worry about deadlock.
269756c9cfb1SNaoya Horiguchi 	 */
26980fe6e20bSNaoya Horiguchi 	page = pte_page(entry);
269956c9cfb1SNaoya Horiguchi 	if (page != pagecache_page)
27000fe6e20bSNaoya Horiguchi 		lock_page(page);
27010fe6e20bSNaoya Horiguchi 
27021e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
27031e8f889bSDavid Gibson 	/* Check for a racing update before calling hugetlb_cow */
2704b4d1d99fSDavid Gibson 	if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
2705b4d1d99fSDavid Gibson 		goto out_page_table_lock;
2706b4d1d99fSDavid Gibson 
2707b4d1d99fSDavid Gibson 
2708788c7df4SHugh Dickins 	if (flags & FAULT_FLAG_WRITE) {
2709b4d1d99fSDavid Gibson 		if (!pte_write(entry)) {
271057303d80SAndy Whitcroft 			ret = hugetlb_cow(mm, vma, address, ptep, entry,
271157303d80SAndy Whitcroft 							pagecache_page);
2712b4d1d99fSDavid Gibson 			goto out_page_table_lock;
2713b4d1d99fSDavid Gibson 		}
2714b4d1d99fSDavid Gibson 		entry = pte_mkdirty(entry);
2715b4d1d99fSDavid Gibson 	}
2716b4d1d99fSDavid Gibson 	entry = pte_mkyoung(entry);
2717788c7df4SHugh Dickins 	if (huge_ptep_set_access_flags(vma, address, ptep, entry,
2718788c7df4SHugh Dickins 						flags & FAULT_FLAG_WRITE))
27194b3073e1SRussell King 		update_mmu_cache(vma, address, ptep);
2720b4d1d99fSDavid Gibson 
2721b4d1d99fSDavid Gibson out_page_table_lock:
27221e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
272357303d80SAndy Whitcroft 
272457303d80SAndy Whitcroft 	if (pagecache_page) {
272557303d80SAndy Whitcroft 		unlock_page(pagecache_page);
272657303d80SAndy Whitcroft 		put_page(pagecache_page);
272757303d80SAndy Whitcroft 	}
27281f64d69cSDean Nelson 	if (page != pagecache_page)
272956c9cfb1SNaoya Horiguchi 		unlock_page(page);
273057303d80SAndy Whitcroft 
2731b4d1d99fSDavid Gibson out_mutex:
27323935baa9SDavid Gibson 	mutex_unlock(&hugetlb_instantiation_mutex);
27331e8f889bSDavid Gibson 
27341e8f889bSDavid Gibson 	return ret;
273586e5216fSAdam Litke }
273686e5216fSAdam Litke 
2737ceb86879SAndi Kleen /* Can be overriden by architectures */
2738ceb86879SAndi Kleen __attribute__((weak)) struct page *
2739ceb86879SAndi Kleen follow_huge_pud(struct mm_struct *mm, unsigned long address,
2740ceb86879SAndi Kleen 	       pud_t *pud, int write)
2741ceb86879SAndi Kleen {
2742ceb86879SAndi Kleen 	BUG();
2743ceb86879SAndi Kleen 	return NULL;
2744ceb86879SAndi Kleen }
2745ceb86879SAndi Kleen 
274663551ae0SDavid Gibson int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
274763551ae0SDavid Gibson 			struct page **pages, struct vm_area_struct **vmas,
27485b23dbe8SAdam Litke 			unsigned long *position, int *length, int i,
27492a15efc9SHugh Dickins 			unsigned int flags)
275063551ae0SDavid Gibson {
2751d5d4b0aaSChen, Kenneth W 	unsigned long pfn_offset;
2752d5d4b0aaSChen, Kenneth W 	unsigned long vaddr = *position;
275363551ae0SDavid Gibson 	int remainder = *length;
2754a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
275563551ae0SDavid Gibson 
27561c59827dSHugh Dickins 	spin_lock(&mm->page_table_lock);
275763551ae0SDavid Gibson 	while (vaddr < vma->vm_end && remainder) {
275863551ae0SDavid Gibson 		pte_t *pte;
27592a15efc9SHugh Dickins 		int absent;
276063551ae0SDavid Gibson 		struct page *page;
276163551ae0SDavid Gibson 
27624c887265SAdam Litke 		/*
27634c887265SAdam Litke 		 * Some archs (sparc64, sh*) have multiple pte_ts to
27642a15efc9SHugh Dickins 		 * each hugepage.  We have to make sure we get the
27654c887265SAdam Litke 		 * first, for the page indexing below to work.
27664c887265SAdam Litke 		 */
2767a5516438SAndi Kleen 		pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
27682a15efc9SHugh Dickins 		absent = !pte || huge_pte_none(huge_ptep_get(pte));
276963551ae0SDavid Gibson 
27702a15efc9SHugh Dickins 		/*
27712a15efc9SHugh Dickins 		 * When coredumping, it suits get_dump_page if we just return
27723ae77f43SHugh Dickins 		 * an error where there's an empty slot with no huge pagecache
27733ae77f43SHugh Dickins 		 * to back it.  This way, we avoid allocating a hugepage, and
27743ae77f43SHugh Dickins 		 * the sparse dumpfile avoids allocating disk blocks, but its
27753ae77f43SHugh Dickins 		 * huge holes still show up with zeroes where they need to be.
27762a15efc9SHugh Dickins 		 */
27773ae77f43SHugh Dickins 		if (absent && (flags & FOLL_DUMP) &&
27783ae77f43SHugh Dickins 		    !hugetlbfs_pagecache_present(h, vma, vaddr)) {
27792a15efc9SHugh Dickins 			remainder = 0;
27802a15efc9SHugh Dickins 			break;
27812a15efc9SHugh Dickins 		}
27822a15efc9SHugh Dickins 
27832a15efc9SHugh Dickins 		if (absent ||
27842a15efc9SHugh Dickins 		    ((flags & FOLL_WRITE) && !pte_write(huge_ptep_get(pte)))) {
27854c887265SAdam Litke 			int ret;
27864c887265SAdam Litke 
27874c887265SAdam Litke 			spin_unlock(&mm->page_table_lock);
27882a15efc9SHugh Dickins 			ret = hugetlb_fault(mm, vma, vaddr,
27892a15efc9SHugh Dickins 				(flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
27904c887265SAdam Litke 			spin_lock(&mm->page_table_lock);
2791a89182c7SAdam Litke 			if (!(ret & VM_FAULT_ERROR))
27924c887265SAdam Litke 				continue;
27934c887265SAdam Litke 
27941c59827dSHugh Dickins 			remainder = 0;
27951c59827dSHugh Dickins 			break;
27961c59827dSHugh Dickins 		}
279763551ae0SDavid Gibson 
2798a5516438SAndi Kleen 		pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
27997f2e9525SGerald Schaefer 		page = pte_page(huge_ptep_get(pte));
2800d5d4b0aaSChen, Kenneth W same_page:
2801d6692183SChen, Kenneth W 		if (pages) {
280269d177c2SAndy Whitcroft 			pages[i] = mem_map_offset(page, pfn_offset);
28034b2e38adSKOSAKI Motohiro 			get_page(pages[i]);
2804d6692183SChen, Kenneth W 		}
280563551ae0SDavid Gibson 
280663551ae0SDavid Gibson 		if (vmas)
280763551ae0SDavid Gibson 			vmas[i] = vma;
280863551ae0SDavid Gibson 
280963551ae0SDavid Gibson 		vaddr += PAGE_SIZE;
2810d5d4b0aaSChen, Kenneth W 		++pfn_offset;
281163551ae0SDavid Gibson 		--remainder;
281263551ae0SDavid Gibson 		++i;
2813d5d4b0aaSChen, Kenneth W 		if (vaddr < vma->vm_end && remainder &&
2814a5516438SAndi Kleen 				pfn_offset < pages_per_huge_page(h)) {
2815d5d4b0aaSChen, Kenneth W 			/*
2816d5d4b0aaSChen, Kenneth W 			 * We use pfn_offset to avoid touching the pageframes
2817d5d4b0aaSChen, Kenneth W 			 * of this compound page.
2818d5d4b0aaSChen, Kenneth W 			 */
2819d5d4b0aaSChen, Kenneth W 			goto same_page;
2820d5d4b0aaSChen, Kenneth W 		}
282163551ae0SDavid Gibson 	}
28221c59827dSHugh Dickins 	spin_unlock(&mm->page_table_lock);
282363551ae0SDavid Gibson 	*length = remainder;
282463551ae0SDavid Gibson 	*position = vaddr;
282563551ae0SDavid Gibson 
28262a15efc9SHugh Dickins 	return i ? i : -EFAULT;
282763551ae0SDavid Gibson }
28288f860591SZhang, Yanmin 
28298f860591SZhang, Yanmin void hugetlb_change_protection(struct vm_area_struct *vma,
28308f860591SZhang, Yanmin 		unsigned long address, unsigned long end, pgprot_t newprot)
28318f860591SZhang, Yanmin {
28328f860591SZhang, Yanmin 	struct mm_struct *mm = vma->vm_mm;
28338f860591SZhang, Yanmin 	unsigned long start = address;
28348f860591SZhang, Yanmin 	pte_t *ptep;
28358f860591SZhang, Yanmin 	pte_t pte;
2836a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
28378f860591SZhang, Yanmin 
28388f860591SZhang, Yanmin 	BUG_ON(address >= end);
28398f860591SZhang, Yanmin 	flush_cache_range(vma, address, end);
28408f860591SZhang, Yanmin 
28413d48ae45SPeter Zijlstra 	mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
28428f860591SZhang, Yanmin 	spin_lock(&mm->page_table_lock);
2843a5516438SAndi Kleen 	for (; address < end; address += huge_page_size(h)) {
28448f860591SZhang, Yanmin 		ptep = huge_pte_offset(mm, address);
28458f860591SZhang, Yanmin 		if (!ptep)
28468f860591SZhang, Yanmin 			continue;
284739dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
284839dde65cSChen, Kenneth W 			continue;
28497f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(ptep))) {
28508f860591SZhang, Yanmin 			pte = huge_ptep_get_and_clear(mm, address, ptep);
28518f860591SZhang, Yanmin 			pte = pte_mkhuge(pte_modify(pte, newprot));
28528f860591SZhang, Yanmin 			set_huge_pte_at(mm, address, ptep, pte);
28538f860591SZhang, Yanmin 		}
28548f860591SZhang, Yanmin 	}
28558f860591SZhang, Yanmin 	spin_unlock(&mm->page_table_lock);
28563d48ae45SPeter Zijlstra 	mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
28578f860591SZhang, Yanmin 
28588f860591SZhang, Yanmin 	flush_tlb_range(vma, start, end);
28598f860591SZhang, Yanmin }
28608f860591SZhang, Yanmin 
2861a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode,
2862a1e78772SMel Gorman 					long from, long to,
28635a6fe125SMel Gorman 					struct vm_area_struct *vma,
2864ca16d140SKOSAKI Motohiro 					vm_flags_t vm_flags)
2865e4e574b7SAdam Litke {
286617c9d12eSMel Gorman 	long ret, chg;
2867a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
2868e4e574b7SAdam Litke 
2869a1e78772SMel Gorman 	/*
287017c9d12eSMel Gorman 	 * Only apply hugepage reservation if asked. At fault time, an
287117c9d12eSMel Gorman 	 * attempt will be made for VM_NORESERVE to allocate a page
287217c9d12eSMel Gorman 	 * and filesystem quota without using reserves
287317c9d12eSMel Gorman 	 */
2874ca16d140SKOSAKI Motohiro 	if (vm_flags & VM_NORESERVE)
287517c9d12eSMel Gorman 		return 0;
287617c9d12eSMel Gorman 
287717c9d12eSMel Gorman 	/*
2878a1e78772SMel Gorman 	 * Shared mappings base their reservation on the number of pages that
2879a1e78772SMel Gorman 	 * are already allocated on behalf of the file. Private mappings need
2880a1e78772SMel Gorman 	 * to reserve the full area even if read-only as mprotect() may be
2881a1e78772SMel Gorman 	 * called to make the mapping read-write. Assume !vma is a shm mapping
2882a1e78772SMel Gorman 	 */
2883f83a275dSMel Gorman 	if (!vma || vma->vm_flags & VM_MAYSHARE)
2884e4e574b7SAdam Litke 		chg = region_chg(&inode->i_mapping->private_list, from, to);
28855a6fe125SMel Gorman 	else {
28865a6fe125SMel Gorman 		struct resv_map *resv_map = resv_map_alloc();
28875a6fe125SMel Gorman 		if (!resv_map)
28885a6fe125SMel Gorman 			return -ENOMEM;
28895a6fe125SMel Gorman 
289017c9d12eSMel Gorman 		chg = to - from;
289117c9d12eSMel Gorman 
28925a6fe125SMel Gorman 		set_vma_resv_map(vma, resv_map);
28935a6fe125SMel Gorman 		set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
28945a6fe125SMel Gorman 	}
28955a6fe125SMel Gorman 
289617c9d12eSMel Gorman 	if (chg < 0)
289717c9d12eSMel Gorman 		return chg;
289817c9d12eSMel Gorman 
289917c9d12eSMel Gorman 	/* There must be enough filesystem quota for the mapping */
290017c9d12eSMel Gorman 	if (hugetlb_get_quota(inode->i_mapping, chg))
290117c9d12eSMel Gorman 		return -ENOSPC;
290217c9d12eSMel Gorman 
290317c9d12eSMel Gorman 	/*
290417c9d12eSMel Gorman 	 * Check enough hugepages are available for the reservation.
290517c9d12eSMel Gorman 	 * Hand back the quota if there are not
290617c9d12eSMel Gorman 	 */
290717c9d12eSMel Gorman 	ret = hugetlb_acct_memory(h, chg);
290817c9d12eSMel Gorman 	if (ret < 0) {
290917c9d12eSMel Gorman 		hugetlb_put_quota(inode->i_mapping, chg);
291017c9d12eSMel Gorman 		return ret;
291117c9d12eSMel Gorman 	}
291217c9d12eSMel Gorman 
291317c9d12eSMel Gorman 	/*
291417c9d12eSMel Gorman 	 * Account for the reservations made. Shared mappings record regions
291517c9d12eSMel Gorman 	 * that have reservations as they are shared by multiple VMAs.
291617c9d12eSMel Gorman 	 * When the last VMA disappears, the region map says how much
291717c9d12eSMel Gorman 	 * the reservation was and the page cache tells how much of
291817c9d12eSMel Gorman 	 * the reservation was consumed. Private mappings are per-VMA and
291917c9d12eSMel Gorman 	 * only the consumed reservations are tracked. When the VMA
292017c9d12eSMel Gorman 	 * disappears, the original reservation is the VMA size and the
292117c9d12eSMel Gorman 	 * consumed reservations are stored in the map. Hence, nothing
292217c9d12eSMel Gorman 	 * else has to be done for private mappings here
292317c9d12eSMel Gorman 	 */
2924f83a275dSMel Gorman 	if (!vma || vma->vm_flags & VM_MAYSHARE)
292517c9d12eSMel Gorman 		region_add(&inode->i_mapping->private_list, from, to);
2926a43a8c39SChen, Kenneth W 	return 0;
2927a43a8c39SChen, Kenneth W }
2928a43a8c39SChen, Kenneth W 
2929a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
2930a43a8c39SChen, Kenneth W {
2931a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
2932a43a8c39SChen, Kenneth W 	long chg = region_truncate(&inode->i_mapping->private_list, offset);
293345c682a6SKen Chen 
293445c682a6SKen Chen 	spin_lock(&inode->i_lock);
2935e4c6f8beSEric Sandeen 	inode->i_blocks -= (blocks_per_huge_page(h) * freed);
293645c682a6SKen Chen 	spin_unlock(&inode->i_lock);
293745c682a6SKen Chen 
293890d8b7e6SAdam Litke 	hugetlb_put_quota(inode->i_mapping, (chg - freed));
2939a5516438SAndi Kleen 	hugetlb_acct_memory(h, -(chg - freed));
2940a43a8c39SChen, Kenneth W }
294193f70f90SNaoya Horiguchi 
2942d5bd9106SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
2943d5bd9106SAndi Kleen 
29446de2b1aaSNaoya Horiguchi /* Should be called in hugetlb_lock */
29456de2b1aaSNaoya Horiguchi static int is_hugepage_on_freelist(struct page *hpage)
29466de2b1aaSNaoya Horiguchi {
29476de2b1aaSNaoya Horiguchi 	struct page *page;
29486de2b1aaSNaoya Horiguchi 	struct page *tmp;
29496de2b1aaSNaoya Horiguchi 	struct hstate *h = page_hstate(hpage);
29506de2b1aaSNaoya Horiguchi 	int nid = page_to_nid(hpage);
29516de2b1aaSNaoya Horiguchi 
29526de2b1aaSNaoya Horiguchi 	list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru)
29536de2b1aaSNaoya Horiguchi 		if (page == hpage)
29546de2b1aaSNaoya Horiguchi 			return 1;
29556de2b1aaSNaoya Horiguchi 	return 0;
29566de2b1aaSNaoya Horiguchi }
29576de2b1aaSNaoya Horiguchi 
295893f70f90SNaoya Horiguchi /*
295993f70f90SNaoya Horiguchi  * This function is called from memory failure code.
296093f70f90SNaoya Horiguchi  * Assume the caller holds page lock of the head page.
296193f70f90SNaoya Horiguchi  */
29626de2b1aaSNaoya Horiguchi int dequeue_hwpoisoned_huge_page(struct page *hpage)
296393f70f90SNaoya Horiguchi {
296493f70f90SNaoya Horiguchi 	struct hstate *h = page_hstate(hpage);
296593f70f90SNaoya Horiguchi 	int nid = page_to_nid(hpage);
29666de2b1aaSNaoya Horiguchi 	int ret = -EBUSY;
296793f70f90SNaoya Horiguchi 
296893f70f90SNaoya Horiguchi 	spin_lock(&hugetlb_lock);
29696de2b1aaSNaoya Horiguchi 	if (is_hugepage_on_freelist(hpage)) {
297093f70f90SNaoya Horiguchi 		list_del(&hpage->lru);
29718c6c2ecbSNaoya Horiguchi 		set_page_refcounted(hpage);
297293f70f90SNaoya Horiguchi 		h->free_huge_pages--;
297393f70f90SNaoya Horiguchi 		h->free_huge_pages_node[nid]--;
29746de2b1aaSNaoya Horiguchi 		ret = 0;
297593f70f90SNaoya Horiguchi 	}
29766de2b1aaSNaoya Horiguchi 	spin_unlock(&hugetlb_lock);
29776de2b1aaSNaoya Horiguchi 	return ret;
29786de2b1aaSNaoya Horiguchi }
29796de2b1aaSNaoya Horiguchi #endif
2980