xref: /openbmc/linux/mm/hugetlb.c (revision 3b32123d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Generic hugetlb support.
36d49e352SNadia Yvette Chambers  * (C) Nadia Yvette Chambers, 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>
163b32123dSGideon Israel Dsouza #include <linux/compiler.h>
17aea47ff3SChristoph Lameter #include <linux/cpuset.h>
183935baa9SDavid Gibson #include <linux/mutex.h>
19aa888a74SAndi Kleen #include <linux/bootmem.h>
20a3437870SNishanth Aravamudan #include <linux/sysfs.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
220fe6e20bSNaoya Horiguchi #include <linux/rmap.h>
23fd6a03edSNaoya Horiguchi #include <linux/swap.h>
24fd6a03edSNaoya Horiguchi #include <linux/swapops.h>
25c8721bbbSNaoya Horiguchi #include <linux/page-isolation.h>
268382d914SDavidlohr Bueso #include <linux/jhash.h>
27d6606683SLinus Torvalds 
2863551ae0SDavid Gibson #include <asm/page.h>
2963551ae0SDavid Gibson #include <asm/pgtable.h>
3024669e58SAneesh Kumar K.V #include <asm/tlb.h>
3163551ae0SDavid Gibson 
3224669e58SAneesh Kumar K.V #include <linux/io.h>
3363551ae0SDavid Gibson #include <linux/hugetlb.h>
349dd540e2SAneesh Kumar K.V #include <linux/hugetlb_cgroup.h>
359a305230SLee Schermerhorn #include <linux/node.h>
367835e98bSNick Piggin #include "internal.h"
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
39396faf03SMel Gorman unsigned long hugepages_treat_as_movable;
40a5516438SAndi Kleen 
41c3f38a38SAneesh Kumar K.V int hugetlb_max_hstate __read_mostly;
42e5ff2159SAndi Kleen unsigned int default_hstate_idx;
43e5ff2159SAndi Kleen struct hstate hstates[HUGE_MAX_HSTATE];
44e5ff2159SAndi Kleen 
4553ba51d2SJon Tollefson __initdata LIST_HEAD(huge_boot_pages);
4653ba51d2SJon Tollefson 
47e5ff2159SAndi Kleen /* for command line parsing */
48e5ff2159SAndi Kleen static struct hstate * __initdata parsed_hstate;
49e5ff2159SAndi Kleen static unsigned long __initdata default_hstate_max_huge_pages;
50e11bfbfcSNick Piggin static unsigned long __initdata default_hstate_size;
51e5ff2159SAndi Kleen 
523935baa9SDavid Gibson /*
5331caf665SNaoya Horiguchi  * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
5431caf665SNaoya Horiguchi  * free_huge_pages, and surplus_huge_pages.
553935baa9SDavid Gibson  */
56c3f38a38SAneesh Kumar K.V DEFINE_SPINLOCK(hugetlb_lock);
570bd0f9fbSEric Paris 
588382d914SDavidlohr Bueso /*
598382d914SDavidlohr Bueso  * Serializes faults on the same logical page.  This is used to
608382d914SDavidlohr Bueso  * prevent spurious OOMs when the hugepage pool is fully utilized.
618382d914SDavidlohr Bueso  */
628382d914SDavidlohr Bueso static int num_fault_mutexes;
638382d914SDavidlohr Bueso static struct mutex *htlb_fault_mutex_table ____cacheline_aligned_in_smp;
648382d914SDavidlohr Bueso 
6590481622SDavid Gibson static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
6690481622SDavid Gibson {
6790481622SDavid Gibson 	bool free = (spool->count == 0) && (spool->used_hpages == 0);
6890481622SDavid Gibson 
6990481622SDavid Gibson 	spin_unlock(&spool->lock);
7090481622SDavid Gibson 
7190481622SDavid Gibson 	/* If no pages are used, and no other handles to the subpool
7290481622SDavid Gibson 	 * remain, free the subpool the subpool remain */
7390481622SDavid Gibson 	if (free)
7490481622SDavid Gibson 		kfree(spool);
7590481622SDavid Gibson }
7690481622SDavid Gibson 
7790481622SDavid Gibson struct hugepage_subpool *hugepage_new_subpool(long nr_blocks)
7890481622SDavid Gibson {
7990481622SDavid Gibson 	struct hugepage_subpool *spool;
8090481622SDavid Gibson 
8190481622SDavid Gibson 	spool = kmalloc(sizeof(*spool), GFP_KERNEL);
8290481622SDavid Gibson 	if (!spool)
8390481622SDavid Gibson 		return NULL;
8490481622SDavid Gibson 
8590481622SDavid Gibson 	spin_lock_init(&spool->lock);
8690481622SDavid Gibson 	spool->count = 1;
8790481622SDavid Gibson 	spool->max_hpages = nr_blocks;
8890481622SDavid Gibson 	spool->used_hpages = 0;
8990481622SDavid Gibson 
9090481622SDavid Gibson 	return spool;
9190481622SDavid Gibson }
9290481622SDavid Gibson 
9390481622SDavid Gibson void hugepage_put_subpool(struct hugepage_subpool *spool)
9490481622SDavid Gibson {
9590481622SDavid Gibson 	spin_lock(&spool->lock);
9690481622SDavid Gibson 	BUG_ON(!spool->count);
9790481622SDavid Gibson 	spool->count--;
9890481622SDavid Gibson 	unlock_or_release_subpool(spool);
9990481622SDavid Gibson }
10090481622SDavid Gibson 
10190481622SDavid Gibson static int hugepage_subpool_get_pages(struct hugepage_subpool *spool,
10290481622SDavid Gibson 				      long delta)
10390481622SDavid Gibson {
10490481622SDavid Gibson 	int ret = 0;
10590481622SDavid Gibson 
10690481622SDavid Gibson 	if (!spool)
10790481622SDavid Gibson 		return 0;
10890481622SDavid Gibson 
10990481622SDavid Gibson 	spin_lock(&spool->lock);
11090481622SDavid Gibson 	if ((spool->used_hpages + delta) <= spool->max_hpages) {
11190481622SDavid Gibson 		spool->used_hpages += delta;
11290481622SDavid Gibson 	} else {
11390481622SDavid Gibson 		ret = -ENOMEM;
11490481622SDavid Gibson 	}
11590481622SDavid Gibson 	spin_unlock(&spool->lock);
11690481622SDavid Gibson 
11790481622SDavid Gibson 	return ret;
11890481622SDavid Gibson }
11990481622SDavid Gibson 
12090481622SDavid Gibson static void hugepage_subpool_put_pages(struct hugepage_subpool *spool,
12190481622SDavid Gibson 				       long delta)
12290481622SDavid Gibson {
12390481622SDavid Gibson 	if (!spool)
12490481622SDavid Gibson 		return;
12590481622SDavid Gibson 
12690481622SDavid Gibson 	spin_lock(&spool->lock);
12790481622SDavid Gibson 	spool->used_hpages -= delta;
12890481622SDavid Gibson 	/* If hugetlbfs_put_super couldn't free spool due to
12990481622SDavid Gibson 	* an outstanding quota reference, free it now. */
13090481622SDavid Gibson 	unlock_or_release_subpool(spool);
13190481622SDavid Gibson }
13290481622SDavid Gibson 
13390481622SDavid Gibson static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
13490481622SDavid Gibson {
13590481622SDavid Gibson 	return HUGETLBFS_SB(inode->i_sb)->spool;
13690481622SDavid Gibson }
13790481622SDavid Gibson 
13890481622SDavid Gibson static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
13990481622SDavid Gibson {
140496ad9aaSAl Viro 	return subpool_inode(file_inode(vma->vm_file));
14190481622SDavid Gibson }
14290481622SDavid Gibson 
143e7c4b0bfSAndy Whitcroft /*
14496822904SAndy Whitcroft  * Region tracking -- allows tracking of reservations and instantiated pages
14596822904SAndy Whitcroft  *                    across the pages in a mapping.
14684afd99bSAndy Whitcroft  *
1477b24d861SDavidlohr Bueso  * The region data structures are embedded into a resv_map and
1487b24d861SDavidlohr Bueso  * protected by a resv_map's lock
14996822904SAndy Whitcroft  */
15096822904SAndy Whitcroft struct file_region {
15196822904SAndy Whitcroft 	struct list_head link;
15296822904SAndy Whitcroft 	long from;
15396822904SAndy Whitcroft 	long to;
15496822904SAndy Whitcroft };
15596822904SAndy Whitcroft 
1561406ec9bSJoonsoo Kim static long region_add(struct resv_map *resv, long f, long t)
15796822904SAndy Whitcroft {
1581406ec9bSJoonsoo Kim 	struct list_head *head = &resv->regions;
15996822904SAndy Whitcroft 	struct file_region *rg, *nrg, *trg;
16096822904SAndy Whitcroft 
1617b24d861SDavidlohr Bueso 	spin_lock(&resv->lock);
16296822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
16396822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
16496822904SAndy Whitcroft 		if (f <= rg->to)
16596822904SAndy Whitcroft 			break;
16696822904SAndy Whitcroft 
16796822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
16896822904SAndy Whitcroft 	if (f > rg->from)
16996822904SAndy Whitcroft 		f = rg->from;
17096822904SAndy Whitcroft 
17196822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
17296822904SAndy Whitcroft 	nrg = rg;
17396822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
17496822904SAndy Whitcroft 		if (&rg->link == head)
17596822904SAndy Whitcroft 			break;
17696822904SAndy Whitcroft 		if (rg->from > t)
17796822904SAndy Whitcroft 			break;
17896822904SAndy Whitcroft 
17996822904SAndy Whitcroft 		/* If this area reaches higher then extend our area to
18096822904SAndy Whitcroft 		 * include it completely.  If this is not the first area
18196822904SAndy Whitcroft 		 * which we intend to reuse, free it. */
18296822904SAndy Whitcroft 		if (rg->to > t)
18396822904SAndy Whitcroft 			t = rg->to;
18496822904SAndy Whitcroft 		if (rg != nrg) {
18596822904SAndy Whitcroft 			list_del(&rg->link);
18696822904SAndy Whitcroft 			kfree(rg);
18796822904SAndy Whitcroft 		}
18896822904SAndy Whitcroft 	}
18996822904SAndy Whitcroft 	nrg->from = f;
19096822904SAndy Whitcroft 	nrg->to = t;
1917b24d861SDavidlohr Bueso 	spin_unlock(&resv->lock);
19296822904SAndy Whitcroft 	return 0;
19396822904SAndy Whitcroft }
19496822904SAndy Whitcroft 
1951406ec9bSJoonsoo Kim static long region_chg(struct resv_map *resv, long f, long t)
19696822904SAndy Whitcroft {
1971406ec9bSJoonsoo Kim 	struct list_head *head = &resv->regions;
1987b24d861SDavidlohr Bueso 	struct file_region *rg, *nrg = NULL;
19996822904SAndy Whitcroft 	long chg = 0;
20096822904SAndy Whitcroft 
2017b24d861SDavidlohr Bueso retry:
2027b24d861SDavidlohr Bueso 	spin_lock(&resv->lock);
20396822904SAndy Whitcroft 	/* Locate the region we are before or in. */
20496822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
20596822904SAndy Whitcroft 		if (f <= rg->to)
20696822904SAndy Whitcroft 			break;
20796822904SAndy Whitcroft 
20896822904SAndy Whitcroft 	/* If we are below the current region then a new region is required.
20996822904SAndy Whitcroft 	 * Subtle, allocate a new region at the position but make it zero
21096822904SAndy Whitcroft 	 * size such that we can guarantee to record the reservation. */
21196822904SAndy Whitcroft 	if (&rg->link == head || t < rg->from) {
2127b24d861SDavidlohr Bueso 		if (!nrg) {
2137b24d861SDavidlohr Bueso 			spin_unlock(&resv->lock);
21496822904SAndy Whitcroft 			nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
21596822904SAndy Whitcroft 			if (!nrg)
21696822904SAndy Whitcroft 				return -ENOMEM;
2177b24d861SDavidlohr Bueso 
21896822904SAndy Whitcroft 			nrg->from = f;
21996822904SAndy Whitcroft 			nrg->to   = f;
22096822904SAndy Whitcroft 			INIT_LIST_HEAD(&nrg->link);
2217b24d861SDavidlohr Bueso 			goto retry;
2227b24d861SDavidlohr Bueso 		}
22396822904SAndy Whitcroft 
2247b24d861SDavidlohr Bueso 		list_add(&nrg->link, rg->link.prev);
2257b24d861SDavidlohr Bueso 		chg = t - f;
2267b24d861SDavidlohr Bueso 		goto out_nrg;
22796822904SAndy Whitcroft 	}
22896822904SAndy Whitcroft 
22996822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
23096822904SAndy Whitcroft 	if (f > rg->from)
23196822904SAndy Whitcroft 		f = rg->from;
23296822904SAndy Whitcroft 	chg = t - f;
23396822904SAndy Whitcroft 
23496822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
23596822904SAndy Whitcroft 	list_for_each_entry(rg, rg->link.prev, link) {
23696822904SAndy Whitcroft 		if (&rg->link == head)
23796822904SAndy Whitcroft 			break;
23896822904SAndy Whitcroft 		if (rg->from > t)
2397b24d861SDavidlohr Bueso 			goto out;
24096822904SAndy Whitcroft 
24125985edcSLucas De Marchi 		/* We overlap with this area, if it extends further than
24296822904SAndy Whitcroft 		 * us then we must extend ourselves.  Account for its
24396822904SAndy Whitcroft 		 * existing reservation. */
24496822904SAndy Whitcroft 		if (rg->to > t) {
24596822904SAndy Whitcroft 			chg += rg->to - t;
24696822904SAndy Whitcroft 			t = rg->to;
24796822904SAndy Whitcroft 		}
24896822904SAndy Whitcroft 		chg -= rg->to - rg->from;
24996822904SAndy Whitcroft 	}
2507b24d861SDavidlohr Bueso 
2517b24d861SDavidlohr Bueso out:
2527b24d861SDavidlohr Bueso 	spin_unlock(&resv->lock);
2537b24d861SDavidlohr Bueso 	/*  We already know we raced and no longer need the new region */
2547b24d861SDavidlohr Bueso 	kfree(nrg);
2557b24d861SDavidlohr Bueso 	return chg;
2567b24d861SDavidlohr Bueso out_nrg:
2577b24d861SDavidlohr Bueso 	spin_unlock(&resv->lock);
25896822904SAndy Whitcroft 	return chg;
25996822904SAndy Whitcroft }
26096822904SAndy Whitcroft 
2611406ec9bSJoonsoo Kim static long region_truncate(struct resv_map *resv, long end)
26296822904SAndy Whitcroft {
2631406ec9bSJoonsoo Kim 	struct list_head *head = &resv->regions;
26496822904SAndy Whitcroft 	struct file_region *rg, *trg;
26596822904SAndy Whitcroft 	long chg = 0;
26696822904SAndy Whitcroft 
2677b24d861SDavidlohr Bueso 	spin_lock(&resv->lock);
26896822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
26996822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
27096822904SAndy Whitcroft 		if (end <= rg->to)
27196822904SAndy Whitcroft 			break;
27296822904SAndy Whitcroft 	if (&rg->link == head)
2737b24d861SDavidlohr Bueso 		goto out;
27496822904SAndy Whitcroft 
27596822904SAndy Whitcroft 	/* If we are in the middle of a region then adjust it. */
27696822904SAndy Whitcroft 	if (end > rg->from) {
27796822904SAndy Whitcroft 		chg = rg->to - end;
27896822904SAndy Whitcroft 		rg->to = end;
27996822904SAndy Whitcroft 		rg = list_entry(rg->link.next, typeof(*rg), link);
28096822904SAndy Whitcroft 	}
28196822904SAndy Whitcroft 
28296822904SAndy Whitcroft 	/* Drop any remaining regions. */
28396822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
28496822904SAndy Whitcroft 		if (&rg->link == head)
28596822904SAndy Whitcroft 			break;
28696822904SAndy Whitcroft 		chg += rg->to - rg->from;
28796822904SAndy Whitcroft 		list_del(&rg->link);
28896822904SAndy Whitcroft 		kfree(rg);
28996822904SAndy Whitcroft 	}
2907b24d861SDavidlohr Bueso 
2917b24d861SDavidlohr Bueso out:
2927b24d861SDavidlohr Bueso 	spin_unlock(&resv->lock);
29396822904SAndy Whitcroft 	return chg;
29496822904SAndy Whitcroft }
29596822904SAndy Whitcroft 
2961406ec9bSJoonsoo Kim static long region_count(struct resv_map *resv, long f, long t)
29784afd99bSAndy Whitcroft {
2981406ec9bSJoonsoo Kim 	struct list_head *head = &resv->regions;
29984afd99bSAndy Whitcroft 	struct file_region *rg;
30084afd99bSAndy Whitcroft 	long chg = 0;
30184afd99bSAndy Whitcroft 
3027b24d861SDavidlohr Bueso 	spin_lock(&resv->lock);
30384afd99bSAndy Whitcroft 	/* Locate each segment we overlap with, and count that overlap. */
30484afd99bSAndy Whitcroft 	list_for_each_entry(rg, head, link) {
305f2135a4aSWang Sheng-Hui 		long seg_from;
306f2135a4aSWang Sheng-Hui 		long seg_to;
30784afd99bSAndy Whitcroft 
30884afd99bSAndy Whitcroft 		if (rg->to <= f)
30984afd99bSAndy Whitcroft 			continue;
31084afd99bSAndy Whitcroft 		if (rg->from >= t)
31184afd99bSAndy Whitcroft 			break;
31284afd99bSAndy Whitcroft 
31384afd99bSAndy Whitcroft 		seg_from = max(rg->from, f);
31484afd99bSAndy Whitcroft 		seg_to = min(rg->to, t);
31584afd99bSAndy Whitcroft 
31684afd99bSAndy Whitcroft 		chg += seg_to - seg_from;
31784afd99bSAndy Whitcroft 	}
3187b24d861SDavidlohr Bueso 	spin_unlock(&resv->lock);
31984afd99bSAndy Whitcroft 
32084afd99bSAndy Whitcroft 	return chg;
32184afd99bSAndy Whitcroft }
32284afd99bSAndy Whitcroft 
32396822904SAndy Whitcroft /*
324e7c4b0bfSAndy Whitcroft  * Convert the address within this vma to the page offset within
325e7c4b0bfSAndy Whitcroft  * the mapping, in pagecache page units; huge pages here.
326e7c4b0bfSAndy Whitcroft  */
327a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h,
328a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long address)
329e7c4b0bfSAndy Whitcroft {
330a5516438SAndi Kleen 	return ((address - vma->vm_start) >> huge_page_shift(h)) +
331a5516438SAndi Kleen 			(vma->vm_pgoff >> huge_page_order(h));
332e7c4b0bfSAndy Whitcroft }
333e7c4b0bfSAndy Whitcroft 
3340fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
3350fe6e20bSNaoya Horiguchi 				     unsigned long address)
3360fe6e20bSNaoya Horiguchi {
3370fe6e20bSNaoya Horiguchi 	return vma_hugecache_offset(hstate_vma(vma), vma, address);
3380fe6e20bSNaoya Horiguchi }
3390fe6e20bSNaoya Horiguchi 
34084afd99bSAndy Whitcroft /*
34108fba699SMel Gorman  * Return the size of the pages allocated when backing a VMA. In the majority
34208fba699SMel Gorman  * cases this will be same size as used by the page table entries.
34308fba699SMel Gorman  */
34408fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
34508fba699SMel Gorman {
34608fba699SMel Gorman 	struct hstate *hstate;
34708fba699SMel Gorman 
34808fba699SMel Gorman 	if (!is_vm_hugetlb_page(vma))
34908fba699SMel Gorman 		return PAGE_SIZE;
35008fba699SMel Gorman 
35108fba699SMel Gorman 	hstate = hstate_vma(vma);
35208fba699SMel Gorman 
3532415cf12SWanpeng Li 	return 1UL << huge_page_shift(hstate);
35408fba699SMel Gorman }
355f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
35608fba699SMel Gorman 
35708fba699SMel Gorman /*
3583340289dSMel Gorman  * Return the page size being used by the MMU to back a VMA. In the majority
3593340289dSMel Gorman  * of cases, the page size used by the kernel matches the MMU size. On
3603340289dSMel Gorman  * architectures where it differs, an architecture-specific version of this
3613340289dSMel Gorman  * function is required.
3623340289dSMel Gorman  */
3633340289dSMel Gorman #ifndef vma_mmu_pagesize
3643340289dSMel Gorman unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
3653340289dSMel Gorman {
3663340289dSMel Gorman 	return vma_kernel_pagesize(vma);
3673340289dSMel Gorman }
3683340289dSMel Gorman #endif
3693340289dSMel Gorman 
3703340289dSMel Gorman /*
37184afd99bSAndy Whitcroft  * Flags for MAP_PRIVATE reservations.  These are stored in the bottom
37284afd99bSAndy Whitcroft  * bits of the reservation map pointer, which are always clear due to
37384afd99bSAndy Whitcroft  * alignment.
37484afd99bSAndy Whitcroft  */
37584afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER    (1UL << 0)
37684afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1)
37704f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
37884afd99bSAndy Whitcroft 
379a1e78772SMel Gorman /*
380a1e78772SMel Gorman  * These helpers are used to track how many pages are reserved for
381a1e78772SMel Gorman  * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
382a1e78772SMel Gorman  * is guaranteed to have their future faults succeed.
383a1e78772SMel Gorman  *
384a1e78772SMel Gorman  * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
385a1e78772SMel Gorman  * the reserve counters are updated with the hugetlb_lock held. It is safe
386a1e78772SMel Gorman  * to reset the VMA at fork() time as it is not in use yet and there is no
387a1e78772SMel Gorman  * chance of the global counters getting corrupted as a result of the values.
38884afd99bSAndy Whitcroft  *
38984afd99bSAndy Whitcroft  * The private mapping reservation is represented in a subtly different
39084afd99bSAndy Whitcroft  * manner to a shared mapping.  A shared mapping has a region map associated
39184afd99bSAndy Whitcroft  * with the underlying file, this region map represents the backing file
39284afd99bSAndy Whitcroft  * pages which have ever had a reservation assigned which this persists even
39384afd99bSAndy Whitcroft  * after the page is instantiated.  A private mapping has a region map
39484afd99bSAndy Whitcroft  * associated with the original mmap which is attached to all VMAs which
39584afd99bSAndy Whitcroft  * reference it, this region map represents those offsets which have consumed
39684afd99bSAndy Whitcroft  * reservation ie. where pages have been instantiated.
397a1e78772SMel Gorman  */
398e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma)
399e7c4b0bfSAndy Whitcroft {
400e7c4b0bfSAndy Whitcroft 	return (unsigned long)vma->vm_private_data;
401e7c4b0bfSAndy Whitcroft }
402e7c4b0bfSAndy Whitcroft 
403e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma,
404e7c4b0bfSAndy Whitcroft 							unsigned long value)
405e7c4b0bfSAndy Whitcroft {
406e7c4b0bfSAndy Whitcroft 	vma->vm_private_data = (void *)value;
407e7c4b0bfSAndy Whitcroft }
408e7c4b0bfSAndy Whitcroft 
4099119a41eSJoonsoo Kim struct resv_map *resv_map_alloc(void)
41084afd99bSAndy Whitcroft {
41184afd99bSAndy Whitcroft 	struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
41284afd99bSAndy Whitcroft 	if (!resv_map)
41384afd99bSAndy Whitcroft 		return NULL;
41484afd99bSAndy Whitcroft 
41584afd99bSAndy Whitcroft 	kref_init(&resv_map->refs);
4167b24d861SDavidlohr Bueso 	spin_lock_init(&resv_map->lock);
41784afd99bSAndy Whitcroft 	INIT_LIST_HEAD(&resv_map->regions);
41884afd99bSAndy Whitcroft 
41984afd99bSAndy Whitcroft 	return resv_map;
42084afd99bSAndy Whitcroft }
42184afd99bSAndy Whitcroft 
4229119a41eSJoonsoo Kim void resv_map_release(struct kref *ref)
42384afd99bSAndy Whitcroft {
42484afd99bSAndy Whitcroft 	struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
42584afd99bSAndy Whitcroft 
42684afd99bSAndy Whitcroft 	/* Clear out any active regions before we release the map. */
4271406ec9bSJoonsoo Kim 	region_truncate(resv_map, 0);
42884afd99bSAndy Whitcroft 	kfree(resv_map);
42984afd99bSAndy Whitcroft }
43084afd99bSAndy Whitcroft 
4314e35f483SJoonsoo Kim static inline struct resv_map *inode_resv_map(struct inode *inode)
4324e35f483SJoonsoo Kim {
4334e35f483SJoonsoo Kim 	return inode->i_mapping->private_data;
4344e35f483SJoonsoo Kim }
4354e35f483SJoonsoo Kim 
43684afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
437a1e78772SMel Gorman {
438a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
4394e35f483SJoonsoo Kim 	if (vma->vm_flags & VM_MAYSHARE) {
4404e35f483SJoonsoo Kim 		struct address_space *mapping = vma->vm_file->f_mapping;
4414e35f483SJoonsoo Kim 		struct inode *inode = mapping->host;
4424e35f483SJoonsoo Kim 
4434e35f483SJoonsoo Kim 		return inode_resv_map(inode);
4444e35f483SJoonsoo Kim 
4454e35f483SJoonsoo Kim 	} else {
44684afd99bSAndy Whitcroft 		return (struct resv_map *)(get_vma_private_data(vma) &
44784afd99bSAndy Whitcroft 							~HPAGE_RESV_MASK);
4484e35f483SJoonsoo Kim 	}
449a1e78772SMel Gorman }
450a1e78772SMel Gorman 
45184afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
452a1e78772SMel Gorman {
453a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
454f83a275dSMel Gorman 	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
455a1e78772SMel Gorman 
45684afd99bSAndy Whitcroft 	set_vma_private_data(vma, (get_vma_private_data(vma) &
45784afd99bSAndy Whitcroft 				HPAGE_RESV_MASK) | (unsigned long)map);
45804f2cbe3SMel Gorman }
45904f2cbe3SMel Gorman 
46004f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
46104f2cbe3SMel Gorman {
46204f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
463f83a275dSMel Gorman 	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
464e7c4b0bfSAndy Whitcroft 
465e7c4b0bfSAndy Whitcroft 	set_vma_private_data(vma, get_vma_private_data(vma) | flags);
46604f2cbe3SMel Gorman }
46704f2cbe3SMel Gorman 
46804f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
46904f2cbe3SMel Gorman {
47004f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
471e7c4b0bfSAndy Whitcroft 
472e7c4b0bfSAndy Whitcroft 	return (get_vma_private_data(vma) & flag) != 0;
473a1e78772SMel Gorman }
474a1e78772SMel Gorman 
47504f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
476a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
477a1e78772SMel Gorman {
478a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
479f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE))
480a1e78772SMel Gorman 		vma->vm_private_data = (void *)0;
481a1e78772SMel Gorman }
482a1e78772SMel Gorman 
483a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */
484af0ed73eSJoonsoo Kim static int vma_has_reserves(struct vm_area_struct *vma, long chg)
485a1e78772SMel Gorman {
486af0ed73eSJoonsoo Kim 	if (vma->vm_flags & VM_NORESERVE) {
487af0ed73eSJoonsoo Kim 		/*
488af0ed73eSJoonsoo Kim 		 * This address is already reserved by other process(chg == 0),
489af0ed73eSJoonsoo Kim 		 * so, we should decrement reserved count. Without decrementing,
490af0ed73eSJoonsoo Kim 		 * reserve count remains after releasing inode, because this
491af0ed73eSJoonsoo Kim 		 * allocated page will go into page cache and is regarded as
492af0ed73eSJoonsoo Kim 		 * coming from reserved pool in releasing step.  Currently, we
493af0ed73eSJoonsoo Kim 		 * don't have any other solution to deal with this situation
494af0ed73eSJoonsoo Kim 		 * properly, so add work-around here.
495af0ed73eSJoonsoo Kim 		 */
496af0ed73eSJoonsoo Kim 		if (vma->vm_flags & VM_MAYSHARE && chg == 0)
497af0ed73eSJoonsoo Kim 			return 1;
498af0ed73eSJoonsoo Kim 		else
49972231b03SJoonsoo Kim 			return 0;
500af0ed73eSJoonsoo Kim 	}
501a63884e9SJoonsoo Kim 
502a63884e9SJoonsoo Kim 	/* Shared mappings always use reserves */
503f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE)
504a1e78772SMel Gorman 		return 1;
505a63884e9SJoonsoo Kim 
506a63884e9SJoonsoo Kim 	/*
507a63884e9SJoonsoo Kim 	 * Only the process that called mmap() has reserves for
508a63884e9SJoonsoo Kim 	 * private mappings.
509a63884e9SJoonsoo Kim 	 */
5107f09ca51SMel Gorman 	if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
5117f09ca51SMel Gorman 		return 1;
512a63884e9SJoonsoo Kim 
5137f09ca51SMel Gorman 	return 0;
514a1e78772SMel Gorman }
515a1e78772SMel Gorman 
516a5516438SAndi Kleen static void enqueue_huge_page(struct hstate *h, struct page *page)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	int nid = page_to_nid(page);
5190edaecfaSAneesh Kumar K.V 	list_move(&page->lru, &h->hugepage_freelists[nid]);
520a5516438SAndi Kleen 	h->free_huge_pages++;
521a5516438SAndi Kleen 	h->free_huge_pages_node[nid]++;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
524bf50bab2SNaoya Horiguchi static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
525bf50bab2SNaoya Horiguchi {
526bf50bab2SNaoya Horiguchi 	struct page *page;
527bf50bab2SNaoya Horiguchi 
528c8721bbbSNaoya Horiguchi 	list_for_each_entry(page, &h->hugepage_freelists[nid], lru)
529c8721bbbSNaoya Horiguchi 		if (!is_migrate_isolate_page(page))
530c8721bbbSNaoya Horiguchi 			break;
531c8721bbbSNaoya Horiguchi 	/*
532c8721bbbSNaoya Horiguchi 	 * if 'non-isolated free hugepage' not found on the list,
533c8721bbbSNaoya Horiguchi 	 * the allocation fails.
534c8721bbbSNaoya Horiguchi 	 */
535c8721bbbSNaoya Horiguchi 	if (&h->hugepage_freelists[nid] == &page->lru)
536bf50bab2SNaoya Horiguchi 		return NULL;
5370edaecfaSAneesh Kumar K.V 	list_move(&page->lru, &h->hugepage_activelist);
538a9869b83SNaoya Horiguchi 	set_page_refcounted(page);
539bf50bab2SNaoya Horiguchi 	h->free_huge_pages--;
540bf50bab2SNaoya Horiguchi 	h->free_huge_pages_node[nid]--;
541bf50bab2SNaoya Horiguchi 	return page;
542bf50bab2SNaoya Horiguchi }
543bf50bab2SNaoya Horiguchi 
54486cdb465SNaoya Horiguchi /* Movability of hugepages depends on migration support. */
54586cdb465SNaoya Horiguchi static inline gfp_t htlb_alloc_mask(struct hstate *h)
54686cdb465SNaoya Horiguchi {
54786cdb465SNaoya Horiguchi 	if (hugepages_treat_as_movable || hugepage_migration_support(h))
54886cdb465SNaoya Horiguchi 		return GFP_HIGHUSER_MOVABLE;
54986cdb465SNaoya Horiguchi 	else
55086cdb465SNaoya Horiguchi 		return GFP_HIGHUSER;
55186cdb465SNaoya Horiguchi }
55286cdb465SNaoya Horiguchi 
553a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h,
554a5516438SAndi Kleen 				struct vm_area_struct *vma,
555af0ed73eSJoonsoo Kim 				unsigned long address, int avoid_reserve,
556af0ed73eSJoonsoo Kim 				long chg)
5571da177e4SLinus Torvalds {
558b1c12cbcSKonstantin Khlebnikov 	struct page *page = NULL;
559480eccf9SLee Schermerhorn 	struct mempolicy *mpol;
56019770b32SMel Gorman 	nodemask_t *nodemask;
561c0ff7453SMiao Xie 	struct zonelist *zonelist;
562dd1a239fSMel Gorman 	struct zone *zone;
563dd1a239fSMel Gorman 	struct zoneref *z;
564cc9a6c87SMel Gorman 	unsigned int cpuset_mems_cookie;
5651da177e4SLinus Torvalds 
566a1e78772SMel Gorman 	/*
567a1e78772SMel Gorman 	 * A child process with MAP_PRIVATE mappings created by their parent
568a1e78772SMel Gorman 	 * have no page reserves. This check ensures that reservations are
569a1e78772SMel Gorman 	 * not "stolen". The child may still get SIGKILLed
570a1e78772SMel Gorman 	 */
571af0ed73eSJoonsoo Kim 	if (!vma_has_reserves(vma, chg) &&
572a5516438SAndi Kleen 			h->free_huge_pages - h->resv_huge_pages == 0)
573c0ff7453SMiao Xie 		goto err;
574a1e78772SMel Gorman 
57504f2cbe3SMel Gorman 	/* If reserves cannot be used, ensure enough pages are in the pool */
576a5516438SAndi Kleen 	if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
5776eab04a8SJustin P. Mattock 		goto err;
57804f2cbe3SMel Gorman 
5799966c4bbSJoonsoo Kim retry_cpuset:
580d26914d1SMel Gorman 	cpuset_mems_cookie = read_mems_allowed_begin();
5819966c4bbSJoonsoo Kim 	zonelist = huge_zonelist(vma, address,
58286cdb465SNaoya Horiguchi 					htlb_alloc_mask(h), &mpol, &nodemask);
5839966c4bbSJoonsoo Kim 
58419770b32SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
58519770b32SMel Gorman 						MAX_NR_ZONES - 1, nodemask) {
58686cdb465SNaoya Horiguchi 		if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask(h))) {
587bf50bab2SNaoya Horiguchi 			page = dequeue_huge_page_node(h, zone_to_nid(zone));
588bf50bab2SNaoya Horiguchi 			if (page) {
589af0ed73eSJoonsoo Kim 				if (avoid_reserve)
590af0ed73eSJoonsoo Kim 					break;
591af0ed73eSJoonsoo Kim 				if (!vma_has_reserves(vma, chg))
592af0ed73eSJoonsoo Kim 					break;
593af0ed73eSJoonsoo Kim 
59407443a85SJoonsoo Kim 				SetPagePrivate(page);
595a63884e9SJoonsoo Kim 				h->resv_huge_pages--;
5965ab3ee7bSKen Chen 				break;
5971da177e4SLinus Torvalds 			}
5983abf7afdSAndrew Morton 		}
599bf50bab2SNaoya Horiguchi 	}
600cc9a6c87SMel Gorman 
601cc9a6c87SMel Gorman 	mpol_cond_put(mpol);
602d26914d1SMel Gorman 	if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
603cc9a6c87SMel Gorman 		goto retry_cpuset;
604cc9a6c87SMel Gorman 	return page;
605cc9a6c87SMel Gorman 
606c0ff7453SMiao Xie err:
607cc9a6c87SMel Gorman 	return NULL;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
610a5516438SAndi Kleen static void update_and_free_page(struct hstate *h, struct page *page)
6116af2acb6SAdam Litke {
6126af2acb6SAdam Litke 	int i;
613a5516438SAndi Kleen 
61418229df5SAndy Whitcroft 	VM_BUG_ON(h->order >= MAX_ORDER);
61518229df5SAndy Whitcroft 
616a5516438SAndi Kleen 	h->nr_huge_pages--;
617a5516438SAndi Kleen 	h->nr_huge_pages_node[page_to_nid(page)]--;
618a5516438SAndi Kleen 	for (i = 0; i < pages_per_huge_page(h); i++) {
61932f84528SChris Forbes 		page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
62032f84528SChris Forbes 				1 << PG_referenced | 1 << PG_dirty |
62132f84528SChris Forbes 				1 << PG_active | 1 << PG_reserved |
6226af2acb6SAdam Litke 				1 << PG_private | 1 << PG_writeback);
6236af2acb6SAdam Litke 	}
624309381feSSasha Levin 	VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page);
6256af2acb6SAdam Litke 	set_compound_page_dtor(page, NULL);
6266af2acb6SAdam Litke 	set_page_refcounted(page);
6277f2e9525SGerald Schaefer 	arch_release_hugepage(page);
628a5516438SAndi Kleen 	__free_pages(page, huge_page_order(h));
6296af2acb6SAdam Litke }
6306af2acb6SAdam Litke 
631e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size)
632e5ff2159SAndi Kleen {
633e5ff2159SAndi Kleen 	struct hstate *h;
634e5ff2159SAndi Kleen 
635e5ff2159SAndi Kleen 	for_each_hstate(h) {
636e5ff2159SAndi Kleen 		if (huge_page_size(h) == size)
637e5ff2159SAndi Kleen 			return h;
638e5ff2159SAndi Kleen 	}
639e5ff2159SAndi Kleen 	return NULL;
640e5ff2159SAndi Kleen }
641e5ff2159SAndi Kleen 
64227a85ef1SDavid Gibson static void free_huge_page(struct page *page)
64327a85ef1SDavid Gibson {
644a5516438SAndi Kleen 	/*
645a5516438SAndi Kleen 	 * Can't pass hstate in here because it is called from the
646a5516438SAndi Kleen 	 * compound page destructor.
647a5516438SAndi Kleen 	 */
648e5ff2159SAndi Kleen 	struct hstate *h = page_hstate(page);
6497893d1d5SAdam Litke 	int nid = page_to_nid(page);
65090481622SDavid Gibson 	struct hugepage_subpool *spool =
65190481622SDavid Gibson 		(struct hugepage_subpool *)page_private(page);
65207443a85SJoonsoo Kim 	bool restore_reserve;
65327a85ef1SDavid Gibson 
654e5df70abSAndy Whitcroft 	set_page_private(page, 0);
65523be7468SMel Gorman 	page->mapping = NULL;
6567893d1d5SAdam Litke 	BUG_ON(page_count(page));
6570fe6e20bSNaoya Horiguchi 	BUG_ON(page_mapcount(page));
65807443a85SJoonsoo Kim 	restore_reserve = PagePrivate(page);
65916c794b4SJoonsoo Kim 	ClearPagePrivate(page);
66027a85ef1SDavid Gibson 
66127a85ef1SDavid Gibson 	spin_lock(&hugetlb_lock);
6626d76dcf4SAneesh Kumar K.V 	hugetlb_cgroup_uncharge_page(hstate_index(h),
6636d76dcf4SAneesh Kumar K.V 				     pages_per_huge_page(h), page);
66407443a85SJoonsoo Kim 	if (restore_reserve)
66507443a85SJoonsoo Kim 		h->resv_huge_pages++;
66607443a85SJoonsoo Kim 
667aa888a74SAndi Kleen 	if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
6680edaecfaSAneesh Kumar K.V 		/* remove the page from active list */
6690edaecfaSAneesh Kumar K.V 		list_del(&page->lru);
670a5516438SAndi Kleen 		update_and_free_page(h, page);
671a5516438SAndi Kleen 		h->surplus_huge_pages--;
672a5516438SAndi Kleen 		h->surplus_huge_pages_node[nid]--;
6737893d1d5SAdam Litke 	} else {
6745d3a551cSWill Deacon 		arch_clear_hugepage_flags(page);
675a5516438SAndi Kleen 		enqueue_huge_page(h, page);
6767893d1d5SAdam Litke 	}
67727a85ef1SDavid Gibson 	spin_unlock(&hugetlb_lock);
67890481622SDavid Gibson 	hugepage_subpool_put_pages(spool, 1);
67927a85ef1SDavid Gibson }
68027a85ef1SDavid Gibson 
681a5516438SAndi Kleen static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
682b7ba30c6SAndi Kleen {
6830edaecfaSAneesh Kumar K.V 	INIT_LIST_HEAD(&page->lru);
684b7ba30c6SAndi Kleen 	set_compound_page_dtor(page, free_huge_page);
685b7ba30c6SAndi Kleen 	spin_lock(&hugetlb_lock);
6869dd540e2SAneesh Kumar K.V 	set_hugetlb_cgroup(page, NULL);
687a5516438SAndi Kleen 	h->nr_huge_pages++;
688a5516438SAndi Kleen 	h->nr_huge_pages_node[nid]++;
689b7ba30c6SAndi Kleen 	spin_unlock(&hugetlb_lock);
690b7ba30c6SAndi Kleen 	put_page(page); /* free it into the hugepage allocator */
691b7ba30c6SAndi Kleen }
692b7ba30c6SAndi Kleen 
693f412c97aSDavid Rientjes static void __init prep_compound_gigantic_page(struct page *page,
694f412c97aSDavid Rientjes 					       unsigned long order)
69520a0307cSWu Fengguang {
69620a0307cSWu Fengguang 	int i;
69720a0307cSWu Fengguang 	int nr_pages = 1 << order;
69820a0307cSWu Fengguang 	struct page *p = page + 1;
69920a0307cSWu Fengguang 
70020a0307cSWu Fengguang 	/* we rely on prep_new_huge_page to set the destructor */
70120a0307cSWu Fengguang 	set_compound_order(page, order);
70220a0307cSWu Fengguang 	__SetPageHead(page);
703ef5a22beSAndrea Arcangeli 	__ClearPageReserved(page);
70420a0307cSWu Fengguang 	for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
70520a0307cSWu Fengguang 		__SetPageTail(p);
706ef5a22beSAndrea Arcangeli 		/*
707ef5a22beSAndrea Arcangeli 		 * For gigantic hugepages allocated through bootmem at
708ef5a22beSAndrea Arcangeli 		 * boot, it's safer to be consistent with the not-gigantic
709ef5a22beSAndrea Arcangeli 		 * hugepages and clear the PG_reserved bit from all tail pages
710ef5a22beSAndrea Arcangeli 		 * too.  Otherwse drivers using get_user_pages() to access tail
711ef5a22beSAndrea Arcangeli 		 * pages may get the reference counting wrong if they see
712ef5a22beSAndrea Arcangeli 		 * PG_reserved set on a tail page (despite the head page not
713ef5a22beSAndrea Arcangeli 		 * having PG_reserved set).  Enforcing this consistency between
714ef5a22beSAndrea Arcangeli 		 * head and tail pages allows drivers to optimize away a check
715ef5a22beSAndrea Arcangeli 		 * on the head page when they need know if put_page() is needed
716ef5a22beSAndrea Arcangeli 		 * after get_user_pages().
717ef5a22beSAndrea Arcangeli 		 */
718ef5a22beSAndrea Arcangeli 		__ClearPageReserved(p);
71958a84aa9SYouquan Song 		set_page_count(p, 0);
72020a0307cSWu Fengguang 		p->first_page = page;
72120a0307cSWu Fengguang 	}
72220a0307cSWu Fengguang }
72320a0307cSWu Fengguang 
7247795912cSAndrew Morton /*
7257795912cSAndrew Morton  * PageHuge() only returns true for hugetlbfs pages, but not for normal or
7267795912cSAndrew Morton  * transparent huge pages.  See the PageTransHuge() documentation for more
7277795912cSAndrew Morton  * details.
7287795912cSAndrew Morton  */
72920a0307cSWu Fengguang int PageHuge(struct page *page)
73020a0307cSWu Fengguang {
73120a0307cSWu Fengguang 	if (!PageCompound(page))
73220a0307cSWu Fengguang 		return 0;
73320a0307cSWu Fengguang 
73420a0307cSWu Fengguang 	page = compound_head(page);
735758f66a2SAndrew Morton 	return get_compound_page_dtor(page) == free_huge_page;
73620a0307cSWu Fengguang }
73743131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge);
73843131e14SNaoya Horiguchi 
73927c73ae7SAndrea Arcangeli /*
74027c73ae7SAndrea Arcangeli  * PageHeadHuge() only returns true for hugetlbfs head page, but not for
74127c73ae7SAndrea Arcangeli  * normal or transparent huge pages.
74227c73ae7SAndrea Arcangeli  */
74327c73ae7SAndrea Arcangeli int PageHeadHuge(struct page *page_head)
74427c73ae7SAndrea Arcangeli {
74527c73ae7SAndrea Arcangeli 	if (!PageHead(page_head))
74627c73ae7SAndrea Arcangeli 		return 0;
74727c73ae7SAndrea Arcangeli 
748758f66a2SAndrew Morton 	return get_compound_page_dtor(page_head) == free_huge_page;
74927c73ae7SAndrea Arcangeli }
75027c73ae7SAndrea Arcangeli 
75113d60f4bSZhang Yi pgoff_t __basepage_index(struct page *page)
75213d60f4bSZhang Yi {
75313d60f4bSZhang Yi 	struct page *page_head = compound_head(page);
75413d60f4bSZhang Yi 	pgoff_t index = page_index(page_head);
75513d60f4bSZhang Yi 	unsigned long compound_idx;
75613d60f4bSZhang Yi 
75713d60f4bSZhang Yi 	if (!PageHuge(page_head))
75813d60f4bSZhang Yi 		return page_index(page);
75913d60f4bSZhang Yi 
76013d60f4bSZhang Yi 	if (compound_order(page_head) >= MAX_ORDER)
76113d60f4bSZhang Yi 		compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
76213d60f4bSZhang Yi 	else
76313d60f4bSZhang Yi 		compound_idx = page - page_head;
76413d60f4bSZhang Yi 
76513d60f4bSZhang Yi 	return (index << compound_order(page_head)) + compound_idx;
76613d60f4bSZhang Yi }
76713d60f4bSZhang Yi 
768a5516438SAndi Kleen static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	struct page *page;
771f96efd58SJoe Jin 
772aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
773aa888a74SAndi Kleen 		return NULL;
774aa888a74SAndi Kleen 
7756484eb3eSMel Gorman 	page = alloc_pages_exact_node(nid,
77686cdb465SNaoya Horiguchi 		htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE|
777551883aeSNishanth Aravamudan 						__GFP_REPEAT|__GFP_NOWARN,
778a5516438SAndi Kleen 		huge_page_order(h));
7791da177e4SLinus Torvalds 	if (page) {
7807f2e9525SGerald Schaefer 		if (arch_prepare_hugepage(page)) {
781caff3a2cSGerald Schaefer 			__free_pages(page, huge_page_order(h));
7827b8ee84dSHarvey Harrison 			return NULL;
7837f2e9525SGerald Schaefer 		}
784a5516438SAndi Kleen 		prep_new_huge_page(h, page, nid);
7851da177e4SLinus Torvalds 	}
78663b4613cSNishanth Aravamudan 
78763b4613cSNishanth Aravamudan 	return page;
78863b4613cSNishanth Aravamudan }
78963b4613cSNishanth Aravamudan 
7905ced66c9SAndi Kleen /*
7916ae11b27SLee Schermerhorn  * common helper functions for hstate_next_node_to_{alloc|free}.
7926ae11b27SLee Schermerhorn  * We may have allocated or freed a huge page based on a different
7936ae11b27SLee Schermerhorn  * nodes_allowed previously, so h->next_node_to_{alloc|free} might
7946ae11b27SLee Schermerhorn  * be outside of *nodes_allowed.  Ensure that we use an allowed
7956ae11b27SLee Schermerhorn  * node for alloc or free.
7969a76db09SLee Schermerhorn  */
7976ae11b27SLee Schermerhorn static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
7989a76db09SLee Schermerhorn {
7996ae11b27SLee Schermerhorn 	nid = next_node(nid, *nodes_allowed);
8009a76db09SLee Schermerhorn 	if (nid == MAX_NUMNODES)
8016ae11b27SLee Schermerhorn 		nid = first_node(*nodes_allowed);
8029a76db09SLee Schermerhorn 	VM_BUG_ON(nid >= MAX_NUMNODES);
8039a76db09SLee Schermerhorn 
8049a76db09SLee Schermerhorn 	return nid;
8059a76db09SLee Schermerhorn }
8069a76db09SLee Schermerhorn 
8076ae11b27SLee Schermerhorn static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
8085ced66c9SAndi Kleen {
8096ae11b27SLee Schermerhorn 	if (!node_isset(nid, *nodes_allowed))
8106ae11b27SLee Schermerhorn 		nid = next_node_allowed(nid, nodes_allowed);
8119a76db09SLee Schermerhorn 	return nid;
8125ced66c9SAndi Kleen }
8135ced66c9SAndi Kleen 
8146ae11b27SLee Schermerhorn /*
8156ae11b27SLee Schermerhorn  * returns the previously saved node ["this node"] from which to
8166ae11b27SLee Schermerhorn  * allocate a persistent huge page for the pool and advance the
8176ae11b27SLee Schermerhorn  * next node from which to allocate, handling wrap at end of node
8186ae11b27SLee Schermerhorn  * mask.
8196ae11b27SLee Schermerhorn  */
8206ae11b27SLee Schermerhorn static int hstate_next_node_to_alloc(struct hstate *h,
8216ae11b27SLee Schermerhorn 					nodemask_t *nodes_allowed)
8226ae11b27SLee Schermerhorn {
8236ae11b27SLee Schermerhorn 	int nid;
8246ae11b27SLee Schermerhorn 
8256ae11b27SLee Schermerhorn 	VM_BUG_ON(!nodes_allowed);
8266ae11b27SLee Schermerhorn 
8276ae11b27SLee Schermerhorn 	nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
8286ae11b27SLee Schermerhorn 	h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
8296ae11b27SLee Schermerhorn 
8306ae11b27SLee Schermerhorn 	return nid;
8316ae11b27SLee Schermerhorn }
8326ae11b27SLee Schermerhorn 
833e8c5c824SLee Schermerhorn /*
8346ae11b27SLee Schermerhorn  * helper for free_pool_huge_page() - return the previously saved
8356ae11b27SLee Schermerhorn  * node ["this node"] from which to free a huge page.  Advance the
8366ae11b27SLee Schermerhorn  * next node id whether or not we find a free huge page to free so
8376ae11b27SLee Schermerhorn  * that the next attempt to free addresses the next node.
838e8c5c824SLee Schermerhorn  */
8396ae11b27SLee Schermerhorn static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
840e8c5c824SLee Schermerhorn {
8416ae11b27SLee Schermerhorn 	int nid;
8429a76db09SLee Schermerhorn 
8436ae11b27SLee Schermerhorn 	VM_BUG_ON(!nodes_allowed);
8446ae11b27SLee Schermerhorn 
8456ae11b27SLee Schermerhorn 	nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
8466ae11b27SLee Schermerhorn 	h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
8476ae11b27SLee Schermerhorn 
8489a76db09SLee Schermerhorn 	return nid;
849e8c5c824SLee Schermerhorn }
850e8c5c824SLee Schermerhorn 
851b2261026SJoonsoo Kim #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask)		\
852b2261026SJoonsoo Kim 	for (nr_nodes = nodes_weight(*mask);				\
853b2261026SJoonsoo Kim 		nr_nodes > 0 &&						\
854b2261026SJoonsoo Kim 		((node = hstate_next_node_to_alloc(hs, mask)) || 1);	\
855b2261026SJoonsoo Kim 		nr_nodes--)
856b2261026SJoonsoo Kim 
857b2261026SJoonsoo Kim #define for_each_node_mask_to_free(hs, nr_nodes, node, mask)		\
858b2261026SJoonsoo Kim 	for (nr_nodes = nodes_weight(*mask);				\
859b2261026SJoonsoo Kim 		nr_nodes > 0 &&						\
860b2261026SJoonsoo Kim 		((node = hstate_next_node_to_free(hs, mask)) || 1);	\
861b2261026SJoonsoo Kim 		nr_nodes--)
862b2261026SJoonsoo Kim 
863b2261026SJoonsoo Kim static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
864b2261026SJoonsoo Kim {
865b2261026SJoonsoo Kim 	struct page *page;
866b2261026SJoonsoo Kim 	int nr_nodes, node;
867b2261026SJoonsoo Kim 	int ret = 0;
868b2261026SJoonsoo Kim 
869b2261026SJoonsoo Kim 	for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
870b2261026SJoonsoo Kim 		page = alloc_fresh_huge_page_node(h, node);
871b2261026SJoonsoo Kim 		if (page) {
872b2261026SJoonsoo Kim 			ret = 1;
873b2261026SJoonsoo Kim 			break;
874b2261026SJoonsoo Kim 		}
875b2261026SJoonsoo Kim 	}
876b2261026SJoonsoo Kim 
877b2261026SJoonsoo Kim 	if (ret)
878b2261026SJoonsoo Kim 		count_vm_event(HTLB_BUDDY_PGALLOC);
879b2261026SJoonsoo Kim 	else
880b2261026SJoonsoo Kim 		count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
881b2261026SJoonsoo Kim 
882b2261026SJoonsoo Kim 	return ret;
883b2261026SJoonsoo Kim }
884b2261026SJoonsoo Kim 
885e8c5c824SLee Schermerhorn /*
886e8c5c824SLee Schermerhorn  * Free huge page from pool from next node to free.
887e8c5c824SLee Schermerhorn  * Attempt to keep persistent huge pages more or less
888e8c5c824SLee Schermerhorn  * balanced over allowed nodes.
889e8c5c824SLee Schermerhorn  * Called with hugetlb_lock locked.
890e8c5c824SLee Schermerhorn  */
8916ae11b27SLee Schermerhorn static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
8926ae11b27SLee Schermerhorn 							 bool acct_surplus)
893e8c5c824SLee Schermerhorn {
894b2261026SJoonsoo Kim 	int nr_nodes, node;
895e8c5c824SLee Schermerhorn 	int ret = 0;
896e8c5c824SLee Schermerhorn 
897b2261026SJoonsoo Kim 	for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
898685f3457SLee Schermerhorn 		/*
899685f3457SLee Schermerhorn 		 * If we're returning unused surplus pages, only examine
900685f3457SLee Schermerhorn 		 * nodes with surplus pages.
901685f3457SLee Schermerhorn 		 */
902b2261026SJoonsoo Kim 		if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
903b2261026SJoonsoo Kim 		    !list_empty(&h->hugepage_freelists[node])) {
904e8c5c824SLee Schermerhorn 			struct page *page =
905b2261026SJoonsoo Kim 				list_entry(h->hugepage_freelists[node].next,
906e8c5c824SLee Schermerhorn 					  struct page, lru);
907e8c5c824SLee Schermerhorn 			list_del(&page->lru);
908e8c5c824SLee Schermerhorn 			h->free_huge_pages--;
909b2261026SJoonsoo Kim 			h->free_huge_pages_node[node]--;
910685f3457SLee Schermerhorn 			if (acct_surplus) {
911685f3457SLee Schermerhorn 				h->surplus_huge_pages--;
912b2261026SJoonsoo Kim 				h->surplus_huge_pages_node[node]--;
913685f3457SLee Schermerhorn 			}
914e8c5c824SLee Schermerhorn 			update_and_free_page(h, page);
915e8c5c824SLee Schermerhorn 			ret = 1;
9169a76db09SLee Schermerhorn 			break;
917e8c5c824SLee Schermerhorn 		}
918b2261026SJoonsoo Kim 	}
919e8c5c824SLee Schermerhorn 
920e8c5c824SLee Schermerhorn 	return ret;
921e8c5c824SLee Schermerhorn }
922e8c5c824SLee Schermerhorn 
923c8721bbbSNaoya Horiguchi /*
924c8721bbbSNaoya Horiguchi  * Dissolve a given free hugepage into free buddy pages. This function does
925c8721bbbSNaoya Horiguchi  * nothing for in-use (including surplus) hugepages.
926c8721bbbSNaoya Horiguchi  */
927c8721bbbSNaoya Horiguchi static void dissolve_free_huge_page(struct page *page)
928c8721bbbSNaoya Horiguchi {
929c8721bbbSNaoya Horiguchi 	spin_lock(&hugetlb_lock);
930c8721bbbSNaoya Horiguchi 	if (PageHuge(page) && !page_count(page)) {
931c8721bbbSNaoya Horiguchi 		struct hstate *h = page_hstate(page);
932c8721bbbSNaoya Horiguchi 		int nid = page_to_nid(page);
933c8721bbbSNaoya Horiguchi 		list_del(&page->lru);
934c8721bbbSNaoya Horiguchi 		h->free_huge_pages--;
935c8721bbbSNaoya Horiguchi 		h->free_huge_pages_node[nid]--;
936c8721bbbSNaoya Horiguchi 		update_and_free_page(h, page);
937c8721bbbSNaoya Horiguchi 	}
938c8721bbbSNaoya Horiguchi 	spin_unlock(&hugetlb_lock);
939c8721bbbSNaoya Horiguchi }
940c8721bbbSNaoya Horiguchi 
941c8721bbbSNaoya Horiguchi /*
942c8721bbbSNaoya Horiguchi  * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
943c8721bbbSNaoya Horiguchi  * make specified memory blocks removable from the system.
944c8721bbbSNaoya Horiguchi  * Note that start_pfn should aligned with (minimum) hugepage size.
945c8721bbbSNaoya Horiguchi  */
946c8721bbbSNaoya Horiguchi void dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
947c8721bbbSNaoya Horiguchi {
948c8721bbbSNaoya Horiguchi 	unsigned int order = 8 * sizeof(void *);
949c8721bbbSNaoya Horiguchi 	unsigned long pfn;
950c8721bbbSNaoya Horiguchi 	struct hstate *h;
951c8721bbbSNaoya Horiguchi 
952c8721bbbSNaoya Horiguchi 	/* Set scan step to minimum hugepage size */
953c8721bbbSNaoya Horiguchi 	for_each_hstate(h)
954c8721bbbSNaoya Horiguchi 		if (order > huge_page_order(h))
955c8721bbbSNaoya Horiguchi 			order = huge_page_order(h);
956c8721bbbSNaoya Horiguchi 	VM_BUG_ON(!IS_ALIGNED(start_pfn, 1 << order));
957c8721bbbSNaoya Horiguchi 	for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order)
958c8721bbbSNaoya Horiguchi 		dissolve_free_huge_page(pfn_to_page(pfn));
959c8721bbbSNaoya Horiguchi }
960c8721bbbSNaoya Horiguchi 
961bf50bab2SNaoya Horiguchi static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
9627893d1d5SAdam Litke {
9637893d1d5SAdam Litke 	struct page *page;
964bf50bab2SNaoya Horiguchi 	unsigned int r_nid;
9657893d1d5SAdam Litke 
966aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
967aa888a74SAndi Kleen 		return NULL;
968aa888a74SAndi Kleen 
969d1c3fb1fSNishanth Aravamudan 	/*
970d1c3fb1fSNishanth Aravamudan 	 * Assume we will successfully allocate the surplus page to
971d1c3fb1fSNishanth Aravamudan 	 * prevent racing processes from causing the surplus to exceed
972d1c3fb1fSNishanth Aravamudan 	 * overcommit
973d1c3fb1fSNishanth Aravamudan 	 *
974d1c3fb1fSNishanth Aravamudan 	 * This however introduces a different race, where a process B
975d1c3fb1fSNishanth Aravamudan 	 * tries to grow the static hugepage pool while alloc_pages() is
976d1c3fb1fSNishanth Aravamudan 	 * called by process A. B will only examine the per-node
977d1c3fb1fSNishanth Aravamudan 	 * counters in determining if surplus huge pages can be
978d1c3fb1fSNishanth Aravamudan 	 * converted to normal huge pages in adjust_pool_surplus(). A
979d1c3fb1fSNishanth Aravamudan 	 * won't be able to increment the per-node counter, until the
980d1c3fb1fSNishanth Aravamudan 	 * lock is dropped by B, but B doesn't drop hugetlb_lock until
981d1c3fb1fSNishanth Aravamudan 	 * no more huge pages can be converted from surplus to normal
982d1c3fb1fSNishanth Aravamudan 	 * state (and doesn't try to convert again). Thus, we have a
983d1c3fb1fSNishanth Aravamudan 	 * case where a surplus huge page exists, the pool is grown, and
984d1c3fb1fSNishanth Aravamudan 	 * the surplus huge page still exists after, even though it
985d1c3fb1fSNishanth Aravamudan 	 * should just have been converted to a normal huge page. This
986d1c3fb1fSNishanth Aravamudan 	 * does not leak memory, though, as the hugepage will be freed
987d1c3fb1fSNishanth Aravamudan 	 * once it is out of use. It also does not allow the counters to
988d1c3fb1fSNishanth Aravamudan 	 * go out of whack in adjust_pool_surplus() as we don't modify
989d1c3fb1fSNishanth Aravamudan 	 * the node values until we've gotten the hugepage and only the
990d1c3fb1fSNishanth Aravamudan 	 * per-node value is checked there.
991d1c3fb1fSNishanth Aravamudan 	 */
992d1c3fb1fSNishanth Aravamudan 	spin_lock(&hugetlb_lock);
993a5516438SAndi Kleen 	if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
994d1c3fb1fSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
995d1c3fb1fSNishanth Aravamudan 		return NULL;
996d1c3fb1fSNishanth Aravamudan 	} else {
997a5516438SAndi Kleen 		h->nr_huge_pages++;
998a5516438SAndi Kleen 		h->surplus_huge_pages++;
999d1c3fb1fSNishanth Aravamudan 	}
1000d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
1001d1c3fb1fSNishanth Aravamudan 
1002bf50bab2SNaoya Horiguchi 	if (nid == NUMA_NO_NODE)
100386cdb465SNaoya Horiguchi 		page = alloc_pages(htlb_alloc_mask(h)|__GFP_COMP|
1004551883aeSNishanth Aravamudan 				   __GFP_REPEAT|__GFP_NOWARN,
1005a5516438SAndi Kleen 				   huge_page_order(h));
1006bf50bab2SNaoya Horiguchi 	else
1007bf50bab2SNaoya Horiguchi 		page = alloc_pages_exact_node(nid,
100886cdb465SNaoya Horiguchi 			htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE|
1009bf50bab2SNaoya Horiguchi 			__GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
1010d1c3fb1fSNishanth Aravamudan 
1011caff3a2cSGerald Schaefer 	if (page && arch_prepare_hugepage(page)) {
1012caff3a2cSGerald Schaefer 		__free_pages(page, huge_page_order(h));
1013ea5768c7SHillf Danton 		page = NULL;
1014caff3a2cSGerald Schaefer 	}
1015caff3a2cSGerald Schaefer 
10167893d1d5SAdam Litke 	spin_lock(&hugetlb_lock);
1017d1c3fb1fSNishanth Aravamudan 	if (page) {
10180edaecfaSAneesh Kumar K.V 		INIT_LIST_HEAD(&page->lru);
1019bf50bab2SNaoya Horiguchi 		r_nid = page_to_nid(page);
1020d1c3fb1fSNishanth Aravamudan 		set_compound_page_dtor(page, free_huge_page);
10219dd540e2SAneesh Kumar K.V 		set_hugetlb_cgroup(page, NULL);
1022d1c3fb1fSNishanth Aravamudan 		/*
1023d1c3fb1fSNishanth Aravamudan 		 * We incremented the global counters already
1024d1c3fb1fSNishanth Aravamudan 		 */
1025bf50bab2SNaoya Horiguchi 		h->nr_huge_pages_node[r_nid]++;
1026bf50bab2SNaoya Horiguchi 		h->surplus_huge_pages_node[r_nid]++;
10273b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC);
1028d1c3fb1fSNishanth Aravamudan 	} else {
1029a5516438SAndi Kleen 		h->nr_huge_pages--;
1030a5516438SAndi Kleen 		h->surplus_huge_pages--;
10313b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
10327893d1d5SAdam Litke 	}
1033d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
10347893d1d5SAdam Litke 
10357893d1d5SAdam Litke 	return page;
10367893d1d5SAdam Litke }
10377893d1d5SAdam Litke 
1038e4e574b7SAdam Litke /*
1039bf50bab2SNaoya Horiguchi  * This allocation function is useful in the context where vma is irrelevant.
1040bf50bab2SNaoya Horiguchi  * E.g. soft-offlining uses this function because it only cares physical
1041bf50bab2SNaoya Horiguchi  * address of error page.
1042bf50bab2SNaoya Horiguchi  */
1043bf50bab2SNaoya Horiguchi struct page *alloc_huge_page_node(struct hstate *h, int nid)
1044bf50bab2SNaoya Horiguchi {
10454ef91848SJoonsoo Kim 	struct page *page = NULL;
1046bf50bab2SNaoya Horiguchi 
1047bf50bab2SNaoya Horiguchi 	spin_lock(&hugetlb_lock);
10484ef91848SJoonsoo Kim 	if (h->free_huge_pages - h->resv_huge_pages > 0)
1049bf50bab2SNaoya Horiguchi 		page = dequeue_huge_page_node(h, nid);
1050bf50bab2SNaoya Horiguchi 	spin_unlock(&hugetlb_lock);
1051bf50bab2SNaoya Horiguchi 
105294ae8ba7SAneesh Kumar K.V 	if (!page)
1053bf50bab2SNaoya Horiguchi 		page = alloc_buddy_huge_page(h, nid);
1054bf50bab2SNaoya Horiguchi 
1055bf50bab2SNaoya Horiguchi 	return page;
1056bf50bab2SNaoya Horiguchi }
1057bf50bab2SNaoya Horiguchi 
1058bf50bab2SNaoya Horiguchi /*
105925985edcSLucas De Marchi  * Increase the hugetlb pool such that it can accommodate a reservation
1060e4e574b7SAdam Litke  * of size 'delta'.
1061e4e574b7SAdam Litke  */
1062a5516438SAndi Kleen static int gather_surplus_pages(struct hstate *h, int delta)
1063e4e574b7SAdam Litke {
1064e4e574b7SAdam Litke 	struct list_head surplus_list;
1065e4e574b7SAdam Litke 	struct page *page, *tmp;
1066e4e574b7SAdam Litke 	int ret, i;
1067e4e574b7SAdam Litke 	int needed, allocated;
106828073b02SHillf Danton 	bool alloc_ok = true;
1069e4e574b7SAdam Litke 
1070a5516438SAndi Kleen 	needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
1071ac09b3a1SAdam Litke 	if (needed <= 0) {
1072a5516438SAndi Kleen 		h->resv_huge_pages += delta;
1073e4e574b7SAdam Litke 		return 0;
1074ac09b3a1SAdam Litke 	}
1075e4e574b7SAdam Litke 
1076e4e574b7SAdam Litke 	allocated = 0;
1077e4e574b7SAdam Litke 	INIT_LIST_HEAD(&surplus_list);
1078e4e574b7SAdam Litke 
1079e4e574b7SAdam Litke 	ret = -ENOMEM;
1080e4e574b7SAdam Litke retry:
1081e4e574b7SAdam Litke 	spin_unlock(&hugetlb_lock);
1082e4e574b7SAdam Litke 	for (i = 0; i < needed; i++) {
1083bf50bab2SNaoya Horiguchi 		page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
108428073b02SHillf Danton 		if (!page) {
108528073b02SHillf Danton 			alloc_ok = false;
108628073b02SHillf Danton 			break;
108728073b02SHillf Danton 		}
1088e4e574b7SAdam Litke 		list_add(&page->lru, &surplus_list);
1089e4e574b7SAdam Litke 	}
109028073b02SHillf Danton 	allocated += i;
1091e4e574b7SAdam Litke 
1092e4e574b7SAdam Litke 	/*
1093e4e574b7SAdam Litke 	 * After retaking hugetlb_lock, we need to recalculate 'needed'
1094e4e574b7SAdam Litke 	 * because either resv_huge_pages or free_huge_pages may have changed.
1095e4e574b7SAdam Litke 	 */
1096e4e574b7SAdam Litke 	spin_lock(&hugetlb_lock);
1097a5516438SAndi Kleen 	needed = (h->resv_huge_pages + delta) -
1098a5516438SAndi Kleen 			(h->free_huge_pages + allocated);
109928073b02SHillf Danton 	if (needed > 0) {
110028073b02SHillf Danton 		if (alloc_ok)
1101e4e574b7SAdam Litke 			goto retry;
110228073b02SHillf Danton 		/*
110328073b02SHillf Danton 		 * We were not able to allocate enough pages to
110428073b02SHillf Danton 		 * satisfy the entire reservation so we free what
110528073b02SHillf Danton 		 * we've allocated so far.
110628073b02SHillf Danton 		 */
110728073b02SHillf Danton 		goto free;
110828073b02SHillf Danton 	}
1109e4e574b7SAdam Litke 	/*
1110e4e574b7SAdam Litke 	 * The surplus_list now contains _at_least_ the number of extra pages
111125985edcSLucas De Marchi 	 * needed to accommodate the reservation.  Add the appropriate number
1112e4e574b7SAdam Litke 	 * of pages to the hugetlb pool and free the extras back to the buddy
1113ac09b3a1SAdam Litke 	 * allocator.  Commit the entire reservation here to prevent another
1114ac09b3a1SAdam Litke 	 * process from stealing the pages as they are added to the pool but
1115ac09b3a1SAdam Litke 	 * before they are reserved.
1116e4e574b7SAdam Litke 	 */
1117e4e574b7SAdam Litke 	needed += allocated;
1118a5516438SAndi Kleen 	h->resv_huge_pages += delta;
1119e4e574b7SAdam Litke 	ret = 0;
1120a9869b83SNaoya Horiguchi 
112119fc3f0aSAdam Litke 	/* Free the needed pages to the hugetlb pool */
112219fc3f0aSAdam Litke 	list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
112319fc3f0aSAdam Litke 		if ((--needed) < 0)
112419fc3f0aSAdam Litke 			break;
1125a9869b83SNaoya Horiguchi 		/*
1126a9869b83SNaoya Horiguchi 		 * This page is now managed by the hugetlb allocator and has
1127a9869b83SNaoya Horiguchi 		 * no users -- drop the buddy allocator's reference.
1128a9869b83SNaoya Horiguchi 		 */
1129a9869b83SNaoya Horiguchi 		put_page_testzero(page);
1130309381feSSasha Levin 		VM_BUG_ON_PAGE(page_count(page), page);
1131a5516438SAndi Kleen 		enqueue_huge_page(h, page);
113219fc3f0aSAdam Litke 	}
113328073b02SHillf Danton free:
1134b0365c8dSHillf Danton 	spin_unlock(&hugetlb_lock);
113519fc3f0aSAdam Litke 
113619fc3f0aSAdam Litke 	/* Free unnecessary surplus pages to the buddy allocator */
1137c0d934baSJoonsoo Kim 	list_for_each_entry_safe(page, tmp, &surplus_list, lru)
1138a9869b83SNaoya Horiguchi 		put_page(page);
113919fc3f0aSAdam Litke 	spin_lock(&hugetlb_lock);
1140e4e574b7SAdam Litke 
1141e4e574b7SAdam Litke 	return ret;
1142e4e574b7SAdam Litke }
1143e4e574b7SAdam Litke 
1144e4e574b7SAdam Litke /*
1145e4e574b7SAdam Litke  * When releasing a hugetlb pool reservation, any surplus pages that were
1146e4e574b7SAdam Litke  * allocated to satisfy the reservation must be explicitly freed if they were
1147e4e574b7SAdam Litke  * never used.
1148685f3457SLee Schermerhorn  * Called with hugetlb_lock held.
1149e4e574b7SAdam Litke  */
1150a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h,
1151a5516438SAndi Kleen 					unsigned long unused_resv_pages)
1152e4e574b7SAdam Litke {
1153e4e574b7SAdam Litke 	unsigned long nr_pages;
1154e4e574b7SAdam Litke 
1155ac09b3a1SAdam Litke 	/* Uncommit the reservation */
1156a5516438SAndi Kleen 	h->resv_huge_pages -= unused_resv_pages;
1157ac09b3a1SAdam Litke 
1158aa888a74SAndi Kleen 	/* Cannot return gigantic pages currently */
1159aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1160aa888a74SAndi Kleen 		return;
1161aa888a74SAndi Kleen 
1162a5516438SAndi Kleen 	nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
1163e4e574b7SAdam Litke 
1164685f3457SLee Schermerhorn 	/*
1165685f3457SLee Schermerhorn 	 * We want to release as many surplus pages as possible, spread
11669b5e5d0fSLee Schermerhorn 	 * evenly across all nodes with memory. Iterate across these nodes
11679b5e5d0fSLee Schermerhorn 	 * until we can no longer free unreserved surplus pages. This occurs
11689b5e5d0fSLee Schermerhorn 	 * when the nodes with surplus pages have no free pages.
11699b5e5d0fSLee Schermerhorn 	 * free_pool_huge_page() will balance the the freed pages across the
11709b5e5d0fSLee Schermerhorn 	 * on-line nodes with memory and will handle the hstate accounting.
1171685f3457SLee Schermerhorn 	 */
1172685f3457SLee Schermerhorn 	while (nr_pages--) {
11738cebfcd0SLai Jiangshan 		if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
1174685f3457SLee Schermerhorn 			break;
1175e4e574b7SAdam Litke 	}
1176e4e574b7SAdam Litke }
1177e4e574b7SAdam Litke 
1178c37f9fb1SAndy Whitcroft /*
1179c37f9fb1SAndy Whitcroft  * Determine if the huge page at addr within the vma has an associated
1180c37f9fb1SAndy Whitcroft  * reservation.  Where it does not we will need to logically increase
118190481622SDavid Gibson  * reservation and actually increase subpool usage before an allocation
118290481622SDavid Gibson  * can occur.  Where any new reservation would be required the
118390481622SDavid Gibson  * reservation change is prepared, but not committed.  Once the page
118490481622SDavid Gibson  * has been allocated from the subpool and instantiated the change should
118590481622SDavid Gibson  * be committed via vma_commit_reservation.  No action is required on
118690481622SDavid Gibson  * failure.
1187c37f9fb1SAndy Whitcroft  */
1188e2f17d94SRoel Kluin static long vma_needs_reservation(struct hstate *h,
1189a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long addr)
1190c37f9fb1SAndy Whitcroft {
11914e35f483SJoonsoo Kim 	struct resv_map *resv;
11924e35f483SJoonsoo Kim 	pgoff_t idx;
11934e35f483SJoonsoo Kim 	long chg;
1194c37f9fb1SAndy Whitcroft 
11954e35f483SJoonsoo Kim 	resv = vma_resv_map(vma);
11964e35f483SJoonsoo Kim 	if (!resv)
1197c37f9fb1SAndy Whitcroft 		return 1;
1198c37f9fb1SAndy Whitcroft 
11994e35f483SJoonsoo Kim 	idx = vma_hugecache_offset(h, vma, addr);
12004e35f483SJoonsoo Kim 	chg = region_chg(resv, idx, idx + 1);
120184afd99bSAndy Whitcroft 
12024e35f483SJoonsoo Kim 	if (vma->vm_flags & VM_MAYSHARE)
12034e35f483SJoonsoo Kim 		return chg;
12044e35f483SJoonsoo Kim 	else
12054e35f483SJoonsoo Kim 		return chg < 0 ? chg : 0;
120684afd99bSAndy Whitcroft }
1207a5516438SAndi Kleen static void vma_commit_reservation(struct hstate *h,
1208a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long addr)
1209c37f9fb1SAndy Whitcroft {
12104e35f483SJoonsoo Kim 	struct resv_map *resv;
12114e35f483SJoonsoo Kim 	pgoff_t idx;
1212c37f9fb1SAndy Whitcroft 
12134e35f483SJoonsoo Kim 	resv = vma_resv_map(vma);
12144e35f483SJoonsoo Kim 	if (!resv)
12154e35f483SJoonsoo Kim 		return;
12169119a41eSJoonsoo Kim 
12174e35f483SJoonsoo Kim 	idx = vma_hugecache_offset(h, vma, addr);
12181406ec9bSJoonsoo Kim 	region_add(resv, idx, idx + 1);
1219c37f9fb1SAndy Whitcroft }
1220c37f9fb1SAndy Whitcroft 
1221348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma,
122204f2cbe3SMel Gorman 				    unsigned long addr, int avoid_reserve)
1223348ea204SAdam Litke {
122490481622SDavid Gibson 	struct hugepage_subpool *spool = subpool_vma(vma);
1225a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
1226348ea204SAdam Litke 	struct page *page;
1227e2f17d94SRoel Kluin 	long chg;
12286d76dcf4SAneesh Kumar K.V 	int ret, idx;
12296d76dcf4SAneesh Kumar K.V 	struct hugetlb_cgroup *h_cg;
12302fc39cecSAdam Litke 
12316d76dcf4SAneesh Kumar K.V 	idx = hstate_index(h);
1232a1e78772SMel Gorman 	/*
123390481622SDavid Gibson 	 * Processes that did not create the mapping will have no
123490481622SDavid Gibson 	 * reserves and will not have accounted against subpool
123590481622SDavid Gibson 	 * limit. Check that the subpool limit can be made before
123690481622SDavid Gibson 	 * satisfying the allocation MAP_NORESERVE mappings may also
123790481622SDavid Gibson 	 * need pages and subpool limit allocated allocated if no reserve
123890481622SDavid Gibson 	 * mapping overlaps.
1239a1e78772SMel Gorman 	 */
1240a5516438SAndi Kleen 	chg = vma_needs_reservation(h, vma, addr);
1241c37f9fb1SAndy Whitcroft 	if (chg < 0)
124276dcee75SAneesh Kumar K.V 		return ERR_PTR(-ENOMEM);
12438bb3f12eSJoonsoo Kim 	if (chg || avoid_reserve)
12448bb3f12eSJoonsoo Kim 		if (hugepage_subpool_get_pages(spool, 1))
124576dcee75SAneesh Kumar K.V 			return ERR_PTR(-ENOSPC);
124690d8b7e6SAdam Litke 
12476d76dcf4SAneesh Kumar K.V 	ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
12486d76dcf4SAneesh Kumar K.V 	if (ret) {
12498bb3f12eSJoonsoo Kim 		if (chg || avoid_reserve)
12508bb3f12eSJoonsoo Kim 			hugepage_subpool_put_pages(spool, 1);
12516d76dcf4SAneesh Kumar K.V 		return ERR_PTR(-ENOSPC);
12526d76dcf4SAneesh Kumar K.V 	}
1253a1e78772SMel Gorman 	spin_lock(&hugetlb_lock);
1254af0ed73eSJoonsoo Kim 	page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg);
125581a6fcaeSJoonsoo Kim 	if (!page) {
125694ae8ba7SAneesh Kumar K.V 		spin_unlock(&hugetlb_lock);
1257bf50bab2SNaoya Horiguchi 		page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
1258a1e78772SMel Gorman 		if (!page) {
12596d76dcf4SAneesh Kumar K.V 			hugetlb_cgroup_uncharge_cgroup(idx,
12606d76dcf4SAneesh Kumar K.V 						       pages_per_huge_page(h),
12616d76dcf4SAneesh Kumar K.V 						       h_cg);
12628bb3f12eSJoonsoo Kim 			if (chg || avoid_reserve)
12638bb3f12eSJoonsoo Kim 				hugepage_subpool_put_pages(spool, 1);
126476dcee75SAneesh Kumar K.V 			return ERR_PTR(-ENOSPC);
1265a1e78772SMel Gorman 		}
126679dbb236SAneesh Kumar K.V 		spin_lock(&hugetlb_lock);
126779dbb236SAneesh Kumar K.V 		list_move(&page->lru, &h->hugepage_activelist);
126881a6fcaeSJoonsoo Kim 		/* Fall through */
1269a1e78772SMel Gorman 	}
127081a6fcaeSJoonsoo Kim 	hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
127181a6fcaeSJoonsoo Kim 	spin_unlock(&hugetlb_lock);
1272a1e78772SMel Gorman 
127390481622SDavid Gibson 	set_page_private(page, (unsigned long)spool);
1274a1e78772SMel Gorman 
1275a5516438SAndi Kleen 	vma_commit_reservation(h, vma, addr);
12767893d1d5SAdam Litke 	return page;
1277b45b5bd6SDavid Gibson }
1278b45b5bd6SDavid Gibson 
127974060e4dSNaoya Horiguchi /*
128074060e4dSNaoya Horiguchi  * alloc_huge_page()'s wrapper which simply returns the page if allocation
128174060e4dSNaoya Horiguchi  * succeeds, otherwise NULL. This function is called from new_vma_page(),
128274060e4dSNaoya Horiguchi  * where no ERR_VALUE is expected to be returned.
128374060e4dSNaoya Horiguchi  */
128474060e4dSNaoya Horiguchi struct page *alloc_huge_page_noerr(struct vm_area_struct *vma,
128574060e4dSNaoya Horiguchi 				unsigned long addr, int avoid_reserve)
128674060e4dSNaoya Horiguchi {
128774060e4dSNaoya Horiguchi 	struct page *page = alloc_huge_page(vma, addr, avoid_reserve);
128874060e4dSNaoya Horiguchi 	if (IS_ERR(page))
128974060e4dSNaoya Horiguchi 		page = NULL;
129074060e4dSNaoya Horiguchi 	return page;
129174060e4dSNaoya Horiguchi }
129274060e4dSNaoya Horiguchi 
129391f47662SCyrill Gorcunov int __weak alloc_bootmem_huge_page(struct hstate *h)
1294aa888a74SAndi Kleen {
1295aa888a74SAndi Kleen 	struct huge_bootmem_page *m;
1296b2261026SJoonsoo Kim 	int nr_nodes, node;
1297aa888a74SAndi Kleen 
1298b2261026SJoonsoo Kim 	for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
1299aa888a74SAndi Kleen 		void *addr;
1300aa888a74SAndi Kleen 
13018b89a116SGrygorii Strashko 		addr = memblock_virt_alloc_try_nid_nopanic(
13028b89a116SGrygorii Strashko 				huge_page_size(h), huge_page_size(h),
13038b89a116SGrygorii Strashko 				0, BOOTMEM_ALLOC_ACCESSIBLE, node);
1304aa888a74SAndi Kleen 		if (addr) {
1305aa888a74SAndi Kleen 			/*
1306aa888a74SAndi Kleen 			 * Use the beginning of the huge page to store the
1307aa888a74SAndi Kleen 			 * huge_bootmem_page struct (until gather_bootmem
1308aa888a74SAndi Kleen 			 * puts them into the mem_map).
1309aa888a74SAndi Kleen 			 */
1310aa888a74SAndi Kleen 			m = addr;
1311aa888a74SAndi Kleen 			goto found;
1312aa888a74SAndi Kleen 		}
1313aa888a74SAndi Kleen 	}
1314aa888a74SAndi Kleen 	return 0;
1315aa888a74SAndi Kleen 
1316aa888a74SAndi Kleen found:
1317aa888a74SAndi Kleen 	BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
1318aa888a74SAndi Kleen 	/* Put them into a private list first because mem_map is not up yet */
1319aa888a74SAndi Kleen 	list_add(&m->list, &huge_boot_pages);
1320aa888a74SAndi Kleen 	m->hstate = h;
1321aa888a74SAndi Kleen 	return 1;
1322aa888a74SAndi Kleen }
1323aa888a74SAndi Kleen 
1324f412c97aSDavid Rientjes static void __init prep_compound_huge_page(struct page *page, int order)
132518229df5SAndy Whitcroft {
132618229df5SAndy Whitcroft 	if (unlikely(order > (MAX_ORDER - 1)))
132718229df5SAndy Whitcroft 		prep_compound_gigantic_page(page, order);
132818229df5SAndy Whitcroft 	else
132918229df5SAndy Whitcroft 		prep_compound_page(page, order);
133018229df5SAndy Whitcroft }
133118229df5SAndy Whitcroft 
1332aa888a74SAndi Kleen /* Put bootmem huge pages into the standard lists after mem_map is up */
1333aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void)
1334aa888a74SAndi Kleen {
1335aa888a74SAndi Kleen 	struct huge_bootmem_page *m;
1336aa888a74SAndi Kleen 
1337aa888a74SAndi Kleen 	list_for_each_entry(m, &huge_boot_pages, list) {
1338aa888a74SAndi Kleen 		struct hstate *h = m->hstate;
1339ee8f248dSBecky Bruce 		struct page *page;
1340ee8f248dSBecky Bruce 
1341ee8f248dSBecky Bruce #ifdef CONFIG_HIGHMEM
1342ee8f248dSBecky Bruce 		page = pfn_to_page(m->phys >> PAGE_SHIFT);
13438b89a116SGrygorii Strashko 		memblock_free_late(__pa(m),
1344ee8f248dSBecky Bruce 				   sizeof(struct huge_bootmem_page));
1345ee8f248dSBecky Bruce #else
1346ee8f248dSBecky Bruce 		page = virt_to_page(m);
1347ee8f248dSBecky Bruce #endif
1348aa888a74SAndi Kleen 		WARN_ON(page_count(page) != 1);
134918229df5SAndy Whitcroft 		prep_compound_huge_page(page, h->order);
1350ef5a22beSAndrea Arcangeli 		WARN_ON(PageReserved(page));
1351aa888a74SAndi Kleen 		prep_new_huge_page(h, page, page_to_nid(page));
1352b0320c7bSRafael Aquini 		/*
1353b0320c7bSRafael Aquini 		 * If we had gigantic hugepages allocated at boot time, we need
1354b0320c7bSRafael Aquini 		 * to restore the 'stolen' pages to totalram_pages in order to
1355b0320c7bSRafael Aquini 		 * fix confusing memory reports from free(1) and another
1356b0320c7bSRafael Aquini 		 * side-effects, like CommitLimit going negative.
1357b0320c7bSRafael Aquini 		 */
1358b0320c7bSRafael Aquini 		if (h->order > (MAX_ORDER - 1))
13593dcc0571SJiang Liu 			adjust_managed_page_count(page, 1 << h->order);
1360aa888a74SAndi Kleen 	}
1361aa888a74SAndi Kleen }
1362aa888a74SAndi Kleen 
13638faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
13641da177e4SLinus Torvalds {
13651da177e4SLinus Torvalds 	unsigned long i;
13661da177e4SLinus Torvalds 
1367e5ff2159SAndi Kleen 	for (i = 0; i < h->max_huge_pages; ++i) {
1368aa888a74SAndi Kleen 		if (h->order >= MAX_ORDER) {
1369aa888a74SAndi Kleen 			if (!alloc_bootmem_huge_page(h))
1370aa888a74SAndi Kleen 				break;
13719b5e5d0fSLee Schermerhorn 		} else if (!alloc_fresh_huge_page(h,
13728cebfcd0SLai Jiangshan 					 &node_states[N_MEMORY]))
13731da177e4SLinus Torvalds 			break;
13741da177e4SLinus Torvalds 	}
13758faa8b07SAndi Kleen 	h->max_huge_pages = i;
1376e5ff2159SAndi Kleen }
1377e5ff2159SAndi Kleen 
1378e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void)
1379e5ff2159SAndi Kleen {
1380e5ff2159SAndi Kleen 	struct hstate *h;
1381e5ff2159SAndi Kleen 
1382e5ff2159SAndi Kleen 	for_each_hstate(h) {
13838faa8b07SAndi Kleen 		/* oversize hugepages were init'ed in early boot */
13848faa8b07SAndi Kleen 		if (h->order < MAX_ORDER)
13858faa8b07SAndi Kleen 			hugetlb_hstate_alloc_pages(h);
1386e5ff2159SAndi Kleen 	}
1387e5ff2159SAndi Kleen }
1388e5ff2159SAndi Kleen 
13894abd32dbSAndi Kleen static char * __init memfmt(char *buf, unsigned long n)
13904abd32dbSAndi Kleen {
13914abd32dbSAndi Kleen 	if (n >= (1UL << 30))
13924abd32dbSAndi Kleen 		sprintf(buf, "%lu GB", n >> 30);
13934abd32dbSAndi Kleen 	else if (n >= (1UL << 20))
13944abd32dbSAndi Kleen 		sprintf(buf, "%lu MB", n >> 20);
13954abd32dbSAndi Kleen 	else
13964abd32dbSAndi Kleen 		sprintf(buf, "%lu KB", n >> 10);
13974abd32dbSAndi Kleen 	return buf;
13984abd32dbSAndi Kleen }
13994abd32dbSAndi Kleen 
1400e5ff2159SAndi Kleen static void __init report_hugepages(void)
1401e5ff2159SAndi Kleen {
1402e5ff2159SAndi Kleen 	struct hstate *h;
1403e5ff2159SAndi Kleen 
1404e5ff2159SAndi Kleen 	for_each_hstate(h) {
14054abd32dbSAndi Kleen 		char buf[32];
1406ffb22af5SAndrew Morton 		pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
14074abd32dbSAndi Kleen 			memfmt(buf, huge_page_size(h)),
14084abd32dbSAndi Kleen 			h->free_huge_pages);
1409e5ff2159SAndi Kleen 	}
1410e5ff2159SAndi Kleen }
1411e5ff2159SAndi Kleen 
14121da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM
14136ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count,
14146ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
14151da177e4SLinus Torvalds {
14164415cc8dSChristoph Lameter 	int i;
14174415cc8dSChristoph Lameter 
1418aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1419aa888a74SAndi Kleen 		return;
1420aa888a74SAndi Kleen 
14216ae11b27SLee Schermerhorn 	for_each_node_mask(i, *nodes_allowed) {
14221da177e4SLinus Torvalds 		struct page *page, *next;
1423a5516438SAndi Kleen 		struct list_head *freel = &h->hugepage_freelists[i];
1424a5516438SAndi Kleen 		list_for_each_entry_safe(page, next, freel, lru) {
1425a5516438SAndi Kleen 			if (count >= h->nr_huge_pages)
14266b0c880dSAdam Litke 				return;
14271da177e4SLinus Torvalds 			if (PageHighMem(page))
14281da177e4SLinus Torvalds 				continue;
14291da177e4SLinus Torvalds 			list_del(&page->lru);
1430e5ff2159SAndi Kleen 			update_and_free_page(h, page);
1431a5516438SAndi Kleen 			h->free_huge_pages--;
1432a5516438SAndi Kleen 			h->free_huge_pages_node[page_to_nid(page)]--;
14331da177e4SLinus Torvalds 		}
14341da177e4SLinus Torvalds 	}
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds #else
14376ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count,
14386ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
14391da177e4SLinus Torvalds {
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds #endif
14421da177e4SLinus Torvalds 
144320a0307cSWu Fengguang /*
144420a0307cSWu Fengguang  * Increment or decrement surplus_huge_pages.  Keep node-specific counters
144520a0307cSWu Fengguang  * balanced by operating on them in a round-robin fashion.
144620a0307cSWu Fengguang  * Returns 1 if an adjustment was made.
144720a0307cSWu Fengguang  */
14486ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
14496ae11b27SLee Schermerhorn 				int delta)
145020a0307cSWu Fengguang {
1451b2261026SJoonsoo Kim 	int nr_nodes, node;
145220a0307cSWu Fengguang 
145320a0307cSWu Fengguang 	VM_BUG_ON(delta != -1 && delta != 1);
145420a0307cSWu Fengguang 
1455e8c5c824SLee Schermerhorn 	if (delta < 0) {
1456b2261026SJoonsoo Kim 		for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
1457b2261026SJoonsoo Kim 			if (h->surplus_huge_pages_node[node])
1458b2261026SJoonsoo Kim 				goto found;
1459b2261026SJoonsoo Kim 		}
1460b2261026SJoonsoo Kim 	} else {
1461b2261026SJoonsoo Kim 		for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
1462b2261026SJoonsoo Kim 			if (h->surplus_huge_pages_node[node] <
1463b2261026SJoonsoo Kim 					h->nr_huge_pages_node[node])
1464b2261026SJoonsoo Kim 				goto found;
1465e8c5c824SLee Schermerhorn 		}
14669a76db09SLee Schermerhorn 	}
1467b2261026SJoonsoo Kim 	return 0;
146820a0307cSWu Fengguang 
1469b2261026SJoonsoo Kim found:
147020a0307cSWu Fengguang 	h->surplus_huge_pages += delta;
1471b2261026SJoonsoo Kim 	h->surplus_huge_pages_node[node] += delta;
1472b2261026SJoonsoo Kim 	return 1;
147320a0307cSWu Fengguang }
147420a0307cSWu Fengguang 
1475a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
14766ae11b27SLee Schermerhorn static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
14776ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
14781da177e4SLinus Torvalds {
14797893d1d5SAdam Litke 	unsigned long min_count, ret;
14801da177e4SLinus Torvalds 
1481aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1482aa888a74SAndi Kleen 		return h->max_huge_pages;
1483aa888a74SAndi Kleen 
14847893d1d5SAdam Litke 	/*
14857893d1d5SAdam Litke 	 * Increase the pool size
14867893d1d5SAdam Litke 	 * First take pages out of surplus state.  Then make up the
14877893d1d5SAdam Litke 	 * remaining difference by allocating fresh huge pages.
1488d1c3fb1fSNishanth Aravamudan 	 *
1489d1c3fb1fSNishanth Aravamudan 	 * We might race with alloc_buddy_huge_page() here and be unable
1490d1c3fb1fSNishanth Aravamudan 	 * to convert a surplus huge page to a normal huge page. That is
1491d1c3fb1fSNishanth Aravamudan 	 * not critical, though, it just means the overall size of the
1492d1c3fb1fSNishanth Aravamudan 	 * pool might be one hugepage larger than it needs to be, but
1493d1c3fb1fSNishanth Aravamudan 	 * within all the constraints specified by the sysctls.
14947893d1d5SAdam Litke 	 */
14951da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
1496a5516438SAndi Kleen 	while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
14976ae11b27SLee Schermerhorn 		if (!adjust_pool_surplus(h, nodes_allowed, -1))
14987893d1d5SAdam Litke 			break;
14997893d1d5SAdam Litke 	}
15007893d1d5SAdam Litke 
1501a5516438SAndi Kleen 	while (count > persistent_huge_pages(h)) {
15027893d1d5SAdam Litke 		/*
15037893d1d5SAdam Litke 		 * If this allocation races such that we no longer need the
15047893d1d5SAdam Litke 		 * page, free_huge_page will handle it by freeing the page
15057893d1d5SAdam Litke 		 * and reducing the surplus.
15067893d1d5SAdam Litke 		 */
15077893d1d5SAdam Litke 		spin_unlock(&hugetlb_lock);
15086ae11b27SLee Schermerhorn 		ret = alloc_fresh_huge_page(h, nodes_allowed);
15097893d1d5SAdam Litke 		spin_lock(&hugetlb_lock);
15107893d1d5SAdam Litke 		if (!ret)
15117893d1d5SAdam Litke 			goto out;
15127893d1d5SAdam Litke 
1513536240f2SMel Gorman 		/* Bail for signals. Probably ctrl-c from user */
1514536240f2SMel Gorman 		if (signal_pending(current))
1515536240f2SMel Gorman 			goto out;
15167893d1d5SAdam Litke 	}
15177893d1d5SAdam Litke 
15187893d1d5SAdam Litke 	/*
15197893d1d5SAdam Litke 	 * Decrease the pool size
15207893d1d5SAdam Litke 	 * First return free pages to the buddy allocator (being careful
15217893d1d5SAdam Litke 	 * to keep enough around to satisfy reservations).  Then place
15227893d1d5SAdam Litke 	 * pages into surplus state as needed so the pool will shrink
15237893d1d5SAdam Litke 	 * to the desired size as pages become free.
1524d1c3fb1fSNishanth Aravamudan 	 *
1525d1c3fb1fSNishanth Aravamudan 	 * By placing pages into the surplus state independent of the
1526d1c3fb1fSNishanth Aravamudan 	 * overcommit value, we are allowing the surplus pool size to
1527d1c3fb1fSNishanth Aravamudan 	 * exceed overcommit. There are few sane options here. Since
1528d1c3fb1fSNishanth Aravamudan 	 * alloc_buddy_huge_page() is checking the global counter,
1529d1c3fb1fSNishanth Aravamudan 	 * though, we'll note that we're not allowed to exceed surplus
1530d1c3fb1fSNishanth Aravamudan 	 * and won't grow the pool anywhere else. Not until one of the
1531d1c3fb1fSNishanth Aravamudan 	 * sysctls are changed, or the surplus pages go out of use.
15327893d1d5SAdam Litke 	 */
1533a5516438SAndi Kleen 	min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
15346b0c880dSAdam Litke 	min_count = max(count, min_count);
15356ae11b27SLee Schermerhorn 	try_to_free_low(h, min_count, nodes_allowed);
1536a5516438SAndi Kleen 	while (min_count < persistent_huge_pages(h)) {
15376ae11b27SLee Schermerhorn 		if (!free_pool_huge_page(h, nodes_allowed, 0))
15381da177e4SLinus Torvalds 			break;
15391da177e4SLinus Torvalds 	}
1540a5516438SAndi Kleen 	while (count < persistent_huge_pages(h)) {
15416ae11b27SLee Schermerhorn 		if (!adjust_pool_surplus(h, nodes_allowed, 1))
15427893d1d5SAdam Litke 			break;
15437893d1d5SAdam Litke 	}
15447893d1d5SAdam Litke out:
1545a5516438SAndi Kleen 	ret = persistent_huge_pages(h);
15461da177e4SLinus Torvalds 	spin_unlock(&hugetlb_lock);
15477893d1d5SAdam Litke 	return ret;
15481da177e4SLinus Torvalds }
15491da177e4SLinus Torvalds 
1550a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \
1551a3437870SNishanth Aravamudan 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1552a3437870SNishanth Aravamudan 
1553a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \
1554a3437870SNishanth Aravamudan 	static struct kobj_attribute _name##_attr = \
1555a3437870SNishanth Aravamudan 		__ATTR(_name, 0644, _name##_show, _name##_store)
1556a3437870SNishanth Aravamudan 
1557a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj;
1558a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
1559a3437870SNishanth Aravamudan 
15609a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
15619a305230SLee Schermerhorn 
15629a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
1563a3437870SNishanth Aravamudan {
1564a3437870SNishanth Aravamudan 	int i;
15659a305230SLee Schermerhorn 
1566a3437870SNishanth Aravamudan 	for (i = 0; i < HUGE_MAX_HSTATE; i++)
15679a305230SLee Schermerhorn 		if (hstate_kobjs[i] == kobj) {
15689a305230SLee Schermerhorn 			if (nidp)
15699a305230SLee Schermerhorn 				*nidp = NUMA_NO_NODE;
1570a3437870SNishanth Aravamudan 			return &hstates[i];
15719a305230SLee Schermerhorn 		}
15729a305230SLee Schermerhorn 
15739a305230SLee Schermerhorn 	return kobj_to_node_hstate(kobj, nidp);
1574a3437870SNishanth Aravamudan }
1575a3437870SNishanth Aravamudan 
157606808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj,
1577a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1578a3437870SNishanth Aravamudan {
15799a305230SLee Schermerhorn 	struct hstate *h;
15809a305230SLee Schermerhorn 	unsigned long nr_huge_pages;
15819a305230SLee Schermerhorn 	int nid;
15829a305230SLee Schermerhorn 
15839a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
15849a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
15859a305230SLee Schermerhorn 		nr_huge_pages = h->nr_huge_pages;
15869a305230SLee Schermerhorn 	else
15879a305230SLee Schermerhorn 		nr_huge_pages = h->nr_huge_pages_node[nid];
15889a305230SLee Schermerhorn 
15899a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", nr_huge_pages);
1590a3437870SNishanth Aravamudan }
1591adbe8726SEric B Munson 
159206808b08SLee Schermerhorn static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
159306808b08SLee Schermerhorn 			struct kobject *kobj, struct kobj_attribute *attr,
159406808b08SLee Schermerhorn 			const char *buf, size_t len)
1595a3437870SNishanth Aravamudan {
1596a3437870SNishanth Aravamudan 	int err;
15979a305230SLee Schermerhorn 	int nid;
159806808b08SLee Schermerhorn 	unsigned long count;
15999a305230SLee Schermerhorn 	struct hstate *h;
1600bad44b5bSDavid Rientjes 	NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
1601a3437870SNishanth Aravamudan 
16023dbb95f7SJingoo Han 	err = kstrtoul(buf, 10, &count);
160373ae31e5SEric B Munson 	if (err)
1604adbe8726SEric B Munson 		goto out;
1605a3437870SNishanth Aravamudan 
16069a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
1607adbe8726SEric B Munson 	if (h->order >= MAX_ORDER) {
1608adbe8726SEric B Munson 		err = -EINVAL;
1609adbe8726SEric B Munson 		goto out;
1610adbe8726SEric B Munson 	}
1611adbe8726SEric B Munson 
16129a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE) {
16139a305230SLee Schermerhorn 		/*
16149a305230SLee Schermerhorn 		 * global hstate attribute
16159a305230SLee Schermerhorn 		 */
16169a305230SLee Schermerhorn 		if (!(obey_mempolicy &&
16179a305230SLee Schermerhorn 				init_nodemask_of_mempolicy(nodes_allowed))) {
161806808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
16198cebfcd0SLai Jiangshan 			nodes_allowed = &node_states[N_MEMORY];
162006808b08SLee Schermerhorn 		}
16219a305230SLee Schermerhorn 	} else if (nodes_allowed) {
16229a305230SLee Schermerhorn 		/*
16239a305230SLee Schermerhorn 		 * per node hstate attribute: adjust count to global,
16249a305230SLee Schermerhorn 		 * but restrict alloc/free to the specified node.
16259a305230SLee Schermerhorn 		 */
16269a305230SLee Schermerhorn 		count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
16279a305230SLee Schermerhorn 		init_nodemask_of_node(nodes_allowed, nid);
16289a305230SLee Schermerhorn 	} else
16298cebfcd0SLai Jiangshan 		nodes_allowed = &node_states[N_MEMORY];
16309a305230SLee Schermerhorn 
163106808b08SLee Schermerhorn 	h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
1632a3437870SNishanth Aravamudan 
16338cebfcd0SLai Jiangshan 	if (nodes_allowed != &node_states[N_MEMORY])
163406808b08SLee Schermerhorn 		NODEMASK_FREE(nodes_allowed);
163506808b08SLee Schermerhorn 
163606808b08SLee Schermerhorn 	return len;
1637adbe8726SEric B Munson out:
1638adbe8726SEric B Munson 	NODEMASK_FREE(nodes_allowed);
1639adbe8726SEric B Munson 	return err;
164006808b08SLee Schermerhorn }
164106808b08SLee Schermerhorn 
164206808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj,
164306808b08SLee Schermerhorn 				       struct kobj_attribute *attr, char *buf)
164406808b08SLee Schermerhorn {
164506808b08SLee Schermerhorn 	return nr_hugepages_show_common(kobj, attr, buf);
164606808b08SLee Schermerhorn }
164706808b08SLee Schermerhorn 
164806808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj,
164906808b08SLee Schermerhorn 	       struct kobj_attribute *attr, const char *buf, size_t len)
165006808b08SLee Schermerhorn {
165106808b08SLee Schermerhorn 	return nr_hugepages_store_common(false, kobj, attr, buf, len);
1652a3437870SNishanth Aravamudan }
1653a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages);
1654a3437870SNishanth Aravamudan 
165506808b08SLee Schermerhorn #ifdef CONFIG_NUMA
165606808b08SLee Schermerhorn 
165706808b08SLee Schermerhorn /*
165806808b08SLee Schermerhorn  * hstate attribute for optionally mempolicy-based constraint on persistent
165906808b08SLee Schermerhorn  * huge page alloc/free.
166006808b08SLee Schermerhorn  */
166106808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
166206808b08SLee Schermerhorn 				       struct kobj_attribute *attr, char *buf)
166306808b08SLee Schermerhorn {
166406808b08SLee Schermerhorn 	return nr_hugepages_show_common(kobj, attr, buf);
166506808b08SLee Schermerhorn }
166606808b08SLee Schermerhorn 
166706808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
166806808b08SLee Schermerhorn 	       struct kobj_attribute *attr, const char *buf, size_t len)
166906808b08SLee Schermerhorn {
167006808b08SLee Schermerhorn 	return nr_hugepages_store_common(true, kobj, attr, buf, len);
167106808b08SLee Schermerhorn }
167206808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy);
167306808b08SLee Schermerhorn #endif
167406808b08SLee Schermerhorn 
167506808b08SLee Schermerhorn 
1676a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
1677a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1678a3437870SNishanth Aravamudan {
16799a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1680a3437870SNishanth Aravamudan 	return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
1681a3437870SNishanth Aravamudan }
1682adbe8726SEric B Munson 
1683a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
1684a3437870SNishanth Aravamudan 		struct kobj_attribute *attr, const char *buf, size_t count)
1685a3437870SNishanth Aravamudan {
1686a3437870SNishanth Aravamudan 	int err;
1687a3437870SNishanth Aravamudan 	unsigned long input;
16889a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1689a3437870SNishanth Aravamudan 
1690adbe8726SEric B Munson 	if (h->order >= MAX_ORDER)
1691adbe8726SEric B Munson 		return -EINVAL;
1692adbe8726SEric B Munson 
16933dbb95f7SJingoo Han 	err = kstrtoul(buf, 10, &input);
1694a3437870SNishanth Aravamudan 	if (err)
169573ae31e5SEric B Munson 		return err;
1696a3437870SNishanth Aravamudan 
1697a3437870SNishanth Aravamudan 	spin_lock(&hugetlb_lock);
1698a3437870SNishanth Aravamudan 	h->nr_overcommit_huge_pages = input;
1699a3437870SNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
1700a3437870SNishanth Aravamudan 
1701a3437870SNishanth Aravamudan 	return count;
1702a3437870SNishanth Aravamudan }
1703a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages);
1704a3437870SNishanth Aravamudan 
1705a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj,
1706a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1707a3437870SNishanth Aravamudan {
17089a305230SLee Schermerhorn 	struct hstate *h;
17099a305230SLee Schermerhorn 	unsigned long free_huge_pages;
17109a305230SLee Schermerhorn 	int nid;
17119a305230SLee Schermerhorn 
17129a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
17139a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
17149a305230SLee Schermerhorn 		free_huge_pages = h->free_huge_pages;
17159a305230SLee Schermerhorn 	else
17169a305230SLee Schermerhorn 		free_huge_pages = h->free_huge_pages_node[nid];
17179a305230SLee Schermerhorn 
17189a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", free_huge_pages);
1719a3437870SNishanth Aravamudan }
1720a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages);
1721a3437870SNishanth Aravamudan 
1722a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj,
1723a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1724a3437870SNishanth Aravamudan {
17259a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1726a3437870SNishanth Aravamudan 	return sprintf(buf, "%lu\n", h->resv_huge_pages);
1727a3437870SNishanth Aravamudan }
1728a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages);
1729a3437870SNishanth Aravamudan 
1730a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj,
1731a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1732a3437870SNishanth Aravamudan {
17339a305230SLee Schermerhorn 	struct hstate *h;
17349a305230SLee Schermerhorn 	unsigned long surplus_huge_pages;
17359a305230SLee Schermerhorn 	int nid;
17369a305230SLee Schermerhorn 
17379a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
17389a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
17399a305230SLee Schermerhorn 		surplus_huge_pages = h->surplus_huge_pages;
17409a305230SLee Schermerhorn 	else
17419a305230SLee Schermerhorn 		surplus_huge_pages = h->surplus_huge_pages_node[nid];
17429a305230SLee Schermerhorn 
17439a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", surplus_huge_pages);
1744a3437870SNishanth Aravamudan }
1745a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages);
1746a3437870SNishanth Aravamudan 
1747a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = {
1748a3437870SNishanth Aravamudan 	&nr_hugepages_attr.attr,
1749a3437870SNishanth Aravamudan 	&nr_overcommit_hugepages_attr.attr,
1750a3437870SNishanth Aravamudan 	&free_hugepages_attr.attr,
1751a3437870SNishanth Aravamudan 	&resv_hugepages_attr.attr,
1752a3437870SNishanth Aravamudan 	&surplus_hugepages_attr.attr,
175306808b08SLee Schermerhorn #ifdef CONFIG_NUMA
175406808b08SLee Schermerhorn 	&nr_hugepages_mempolicy_attr.attr,
175506808b08SLee Schermerhorn #endif
1756a3437870SNishanth Aravamudan 	NULL,
1757a3437870SNishanth Aravamudan };
1758a3437870SNishanth Aravamudan 
1759a3437870SNishanth Aravamudan static struct attribute_group hstate_attr_group = {
1760a3437870SNishanth Aravamudan 	.attrs = hstate_attrs,
1761a3437870SNishanth Aravamudan };
1762a3437870SNishanth Aravamudan 
1763094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
17649a305230SLee Schermerhorn 				    struct kobject **hstate_kobjs,
17659a305230SLee Schermerhorn 				    struct attribute_group *hstate_attr_group)
1766a3437870SNishanth Aravamudan {
1767a3437870SNishanth Aravamudan 	int retval;
1768972dc4deSAneesh Kumar K.V 	int hi = hstate_index(h);
1769a3437870SNishanth Aravamudan 
17709a305230SLee Schermerhorn 	hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
17719a305230SLee Schermerhorn 	if (!hstate_kobjs[hi])
1772a3437870SNishanth Aravamudan 		return -ENOMEM;
1773a3437870SNishanth Aravamudan 
17749a305230SLee Schermerhorn 	retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
1775a3437870SNishanth Aravamudan 	if (retval)
17769a305230SLee Schermerhorn 		kobject_put(hstate_kobjs[hi]);
1777a3437870SNishanth Aravamudan 
1778a3437870SNishanth Aravamudan 	return retval;
1779a3437870SNishanth Aravamudan }
1780a3437870SNishanth Aravamudan 
1781a3437870SNishanth Aravamudan static void __init hugetlb_sysfs_init(void)
1782a3437870SNishanth Aravamudan {
1783a3437870SNishanth Aravamudan 	struct hstate *h;
1784a3437870SNishanth Aravamudan 	int err;
1785a3437870SNishanth Aravamudan 
1786a3437870SNishanth Aravamudan 	hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
1787a3437870SNishanth Aravamudan 	if (!hugepages_kobj)
1788a3437870SNishanth Aravamudan 		return;
1789a3437870SNishanth Aravamudan 
1790a3437870SNishanth Aravamudan 	for_each_hstate(h) {
17919a305230SLee Schermerhorn 		err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
17929a305230SLee Schermerhorn 					 hstate_kobjs, &hstate_attr_group);
1793a3437870SNishanth Aravamudan 		if (err)
1794ffb22af5SAndrew Morton 			pr_err("Hugetlb: Unable to add hstate %s", h->name);
1795a3437870SNishanth Aravamudan 	}
1796a3437870SNishanth Aravamudan }
1797a3437870SNishanth Aravamudan 
17989a305230SLee Schermerhorn #ifdef CONFIG_NUMA
17999a305230SLee Schermerhorn 
18009a305230SLee Schermerhorn /*
18019a305230SLee Schermerhorn  * node_hstate/s - associate per node hstate attributes, via their kobjects,
180210fbcf4cSKay Sievers  * with node devices in node_devices[] using a parallel array.  The array
180310fbcf4cSKay Sievers  * index of a node device or _hstate == node id.
180410fbcf4cSKay Sievers  * This is here to avoid any static dependency of the node device driver, in
18059a305230SLee Schermerhorn  * the base kernel, on the hugetlb module.
18069a305230SLee Schermerhorn  */
18079a305230SLee Schermerhorn struct node_hstate {
18089a305230SLee Schermerhorn 	struct kobject		*hugepages_kobj;
18099a305230SLee Schermerhorn 	struct kobject		*hstate_kobjs[HUGE_MAX_HSTATE];
18109a305230SLee Schermerhorn };
18119a305230SLee Schermerhorn struct node_hstate node_hstates[MAX_NUMNODES];
18129a305230SLee Schermerhorn 
18139a305230SLee Schermerhorn /*
181410fbcf4cSKay Sievers  * A subset of global hstate attributes for node devices
18159a305230SLee Schermerhorn  */
18169a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = {
18179a305230SLee Schermerhorn 	&nr_hugepages_attr.attr,
18189a305230SLee Schermerhorn 	&free_hugepages_attr.attr,
18199a305230SLee Schermerhorn 	&surplus_hugepages_attr.attr,
18209a305230SLee Schermerhorn 	NULL,
18219a305230SLee Schermerhorn };
18229a305230SLee Schermerhorn 
18239a305230SLee Schermerhorn static struct attribute_group per_node_hstate_attr_group = {
18249a305230SLee Schermerhorn 	.attrs = per_node_hstate_attrs,
18259a305230SLee Schermerhorn };
18269a305230SLee Schermerhorn 
18279a305230SLee Schermerhorn /*
182810fbcf4cSKay Sievers  * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
18299a305230SLee Schermerhorn  * Returns node id via non-NULL nidp.
18309a305230SLee Schermerhorn  */
18319a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
18329a305230SLee Schermerhorn {
18339a305230SLee Schermerhorn 	int nid;
18349a305230SLee Schermerhorn 
18359a305230SLee Schermerhorn 	for (nid = 0; nid < nr_node_ids; nid++) {
18369a305230SLee Schermerhorn 		struct node_hstate *nhs = &node_hstates[nid];
18379a305230SLee Schermerhorn 		int i;
18389a305230SLee Schermerhorn 		for (i = 0; i < HUGE_MAX_HSTATE; i++)
18399a305230SLee Schermerhorn 			if (nhs->hstate_kobjs[i] == kobj) {
18409a305230SLee Schermerhorn 				if (nidp)
18419a305230SLee Schermerhorn 					*nidp = nid;
18429a305230SLee Schermerhorn 				return &hstates[i];
18439a305230SLee Schermerhorn 			}
18449a305230SLee Schermerhorn 	}
18459a305230SLee Schermerhorn 
18469a305230SLee Schermerhorn 	BUG();
18479a305230SLee Schermerhorn 	return NULL;
18489a305230SLee Schermerhorn }
18499a305230SLee Schermerhorn 
18509a305230SLee Schermerhorn /*
185110fbcf4cSKay Sievers  * Unregister hstate attributes from a single node device.
18529a305230SLee Schermerhorn  * No-op if no hstate attributes attached.
18539a305230SLee Schermerhorn  */
18543cd8b44fSClaudiu Ghioc static void hugetlb_unregister_node(struct node *node)
18559a305230SLee Schermerhorn {
18569a305230SLee Schermerhorn 	struct hstate *h;
185710fbcf4cSKay Sievers 	struct node_hstate *nhs = &node_hstates[node->dev.id];
18589a305230SLee Schermerhorn 
18599a305230SLee Schermerhorn 	if (!nhs->hugepages_kobj)
18609b5e5d0fSLee Schermerhorn 		return;		/* no hstate attributes */
18619a305230SLee Schermerhorn 
1862972dc4deSAneesh Kumar K.V 	for_each_hstate(h) {
1863972dc4deSAneesh Kumar K.V 		int idx = hstate_index(h);
1864972dc4deSAneesh Kumar K.V 		if (nhs->hstate_kobjs[idx]) {
1865972dc4deSAneesh Kumar K.V 			kobject_put(nhs->hstate_kobjs[idx]);
1866972dc4deSAneesh Kumar K.V 			nhs->hstate_kobjs[idx] = NULL;
1867972dc4deSAneesh Kumar K.V 		}
18689a305230SLee Schermerhorn 	}
18699a305230SLee Schermerhorn 
18709a305230SLee Schermerhorn 	kobject_put(nhs->hugepages_kobj);
18719a305230SLee Schermerhorn 	nhs->hugepages_kobj = NULL;
18729a305230SLee Schermerhorn }
18739a305230SLee Schermerhorn 
18749a305230SLee Schermerhorn /*
187510fbcf4cSKay Sievers  * hugetlb module exit:  unregister hstate attributes from node devices
18769a305230SLee Schermerhorn  * that have them.
18779a305230SLee Schermerhorn  */
18789a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void)
18799a305230SLee Schermerhorn {
18809a305230SLee Schermerhorn 	int nid;
18819a305230SLee Schermerhorn 
18829a305230SLee Schermerhorn 	/*
188310fbcf4cSKay Sievers 	 * disable node device registrations.
18849a305230SLee Schermerhorn 	 */
18859a305230SLee Schermerhorn 	register_hugetlbfs_with_node(NULL, NULL);
18869a305230SLee Schermerhorn 
18879a305230SLee Schermerhorn 	/*
18889a305230SLee Schermerhorn 	 * remove hstate attributes from any nodes that have them.
18899a305230SLee Schermerhorn 	 */
18909a305230SLee Schermerhorn 	for (nid = 0; nid < nr_node_ids; nid++)
18918732794bSWen Congyang 		hugetlb_unregister_node(node_devices[nid]);
18929a305230SLee Schermerhorn }
18939a305230SLee Schermerhorn 
18949a305230SLee Schermerhorn /*
189510fbcf4cSKay Sievers  * Register hstate attributes for a single node device.
18969a305230SLee Schermerhorn  * No-op if attributes already registered.
18979a305230SLee Schermerhorn  */
18983cd8b44fSClaudiu Ghioc static void hugetlb_register_node(struct node *node)
18999a305230SLee Schermerhorn {
19009a305230SLee Schermerhorn 	struct hstate *h;
190110fbcf4cSKay Sievers 	struct node_hstate *nhs = &node_hstates[node->dev.id];
19029a305230SLee Schermerhorn 	int err;
19039a305230SLee Schermerhorn 
19049a305230SLee Schermerhorn 	if (nhs->hugepages_kobj)
19059a305230SLee Schermerhorn 		return;		/* already allocated */
19069a305230SLee Schermerhorn 
19079a305230SLee Schermerhorn 	nhs->hugepages_kobj = kobject_create_and_add("hugepages",
190810fbcf4cSKay Sievers 							&node->dev.kobj);
19099a305230SLee Schermerhorn 	if (!nhs->hugepages_kobj)
19109a305230SLee Schermerhorn 		return;
19119a305230SLee Schermerhorn 
19129a305230SLee Schermerhorn 	for_each_hstate(h) {
19139a305230SLee Schermerhorn 		err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
19149a305230SLee Schermerhorn 						nhs->hstate_kobjs,
19159a305230SLee Schermerhorn 						&per_node_hstate_attr_group);
19169a305230SLee Schermerhorn 		if (err) {
1917ffb22af5SAndrew Morton 			pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
191810fbcf4cSKay Sievers 				h->name, node->dev.id);
19199a305230SLee Schermerhorn 			hugetlb_unregister_node(node);
19209a305230SLee Schermerhorn 			break;
19219a305230SLee Schermerhorn 		}
19229a305230SLee Schermerhorn 	}
19239a305230SLee Schermerhorn }
19249a305230SLee Schermerhorn 
19259a305230SLee Schermerhorn /*
19269b5e5d0fSLee Schermerhorn  * hugetlb init time:  register hstate attributes for all registered node
192710fbcf4cSKay Sievers  * devices of nodes that have memory.  All on-line nodes should have
192810fbcf4cSKay Sievers  * registered their associated device by this time.
19299a305230SLee Schermerhorn  */
19309a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void)
19319a305230SLee Schermerhorn {
19329a305230SLee Schermerhorn 	int nid;
19339a305230SLee Schermerhorn 
19348cebfcd0SLai Jiangshan 	for_each_node_state(nid, N_MEMORY) {
19358732794bSWen Congyang 		struct node *node = node_devices[nid];
193610fbcf4cSKay Sievers 		if (node->dev.id == nid)
19379a305230SLee Schermerhorn 			hugetlb_register_node(node);
19389a305230SLee Schermerhorn 	}
19399a305230SLee Schermerhorn 
19409a305230SLee Schermerhorn 	/*
194110fbcf4cSKay Sievers 	 * Let the node device driver know we're here so it can
19429a305230SLee Schermerhorn 	 * [un]register hstate attributes on node hotplug.
19439a305230SLee Schermerhorn 	 */
19449a305230SLee Schermerhorn 	register_hugetlbfs_with_node(hugetlb_register_node,
19459a305230SLee Schermerhorn 				     hugetlb_unregister_node);
19469a305230SLee Schermerhorn }
19479a305230SLee Schermerhorn #else	/* !CONFIG_NUMA */
19489a305230SLee Schermerhorn 
19499a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
19509a305230SLee Schermerhorn {
19519a305230SLee Schermerhorn 	BUG();
19529a305230SLee Schermerhorn 	if (nidp)
19539a305230SLee Schermerhorn 		*nidp = -1;
19549a305230SLee Schermerhorn 	return NULL;
19559a305230SLee Schermerhorn }
19569a305230SLee Schermerhorn 
19579a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) { }
19589a305230SLee Schermerhorn 
19599a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { }
19609a305230SLee Schermerhorn 
19619a305230SLee Schermerhorn #endif
19629a305230SLee Schermerhorn 
1963a3437870SNishanth Aravamudan static void __exit hugetlb_exit(void)
1964a3437870SNishanth Aravamudan {
1965a3437870SNishanth Aravamudan 	struct hstate *h;
1966a3437870SNishanth Aravamudan 
19679a305230SLee Schermerhorn 	hugetlb_unregister_all_nodes();
19689a305230SLee Schermerhorn 
1969a3437870SNishanth Aravamudan 	for_each_hstate(h) {
1970972dc4deSAneesh Kumar K.V 		kobject_put(hstate_kobjs[hstate_index(h)]);
1971a3437870SNishanth Aravamudan 	}
1972a3437870SNishanth Aravamudan 
1973a3437870SNishanth Aravamudan 	kobject_put(hugepages_kobj);
19748382d914SDavidlohr Bueso 	kfree(htlb_fault_mutex_table);
1975a3437870SNishanth Aravamudan }
1976a3437870SNishanth Aravamudan module_exit(hugetlb_exit);
1977a3437870SNishanth Aravamudan 
1978a3437870SNishanth Aravamudan static int __init hugetlb_init(void)
1979a3437870SNishanth Aravamudan {
19808382d914SDavidlohr Bueso 	int i;
19818382d914SDavidlohr Bueso 
19820ef89d25SBenjamin Herrenschmidt 	/* Some platform decide whether they support huge pages at boot
19830ef89d25SBenjamin Herrenschmidt 	 * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
19840ef89d25SBenjamin Herrenschmidt 	 * there is no such support
19850ef89d25SBenjamin Herrenschmidt 	 */
19860ef89d25SBenjamin Herrenschmidt 	if (HPAGE_SHIFT == 0)
19870ef89d25SBenjamin Herrenschmidt 		return 0;
1988a3437870SNishanth Aravamudan 
1989e11bfbfcSNick Piggin 	if (!size_to_hstate(default_hstate_size)) {
1990e11bfbfcSNick Piggin 		default_hstate_size = HPAGE_SIZE;
1991e11bfbfcSNick Piggin 		if (!size_to_hstate(default_hstate_size))
1992a3437870SNishanth Aravamudan 			hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
1993a3437870SNishanth Aravamudan 	}
1994972dc4deSAneesh Kumar K.V 	default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
1995e11bfbfcSNick Piggin 	if (default_hstate_max_huge_pages)
1996e11bfbfcSNick Piggin 		default_hstate.max_huge_pages = default_hstate_max_huge_pages;
1997a3437870SNishanth Aravamudan 
1998a3437870SNishanth Aravamudan 	hugetlb_init_hstates();
1999aa888a74SAndi Kleen 	gather_bootmem_prealloc();
2000a3437870SNishanth Aravamudan 	report_hugepages();
2001a3437870SNishanth Aravamudan 
2002a3437870SNishanth Aravamudan 	hugetlb_sysfs_init();
20039a305230SLee Schermerhorn 	hugetlb_register_all_nodes();
20047179e7bfSJianguo Wu 	hugetlb_cgroup_file_init();
20059a305230SLee Schermerhorn 
20068382d914SDavidlohr Bueso #ifdef CONFIG_SMP
20078382d914SDavidlohr Bueso 	num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
20088382d914SDavidlohr Bueso #else
20098382d914SDavidlohr Bueso 	num_fault_mutexes = 1;
20108382d914SDavidlohr Bueso #endif
20118382d914SDavidlohr Bueso 	htlb_fault_mutex_table =
20128382d914SDavidlohr Bueso 		kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL);
20138382d914SDavidlohr Bueso 	BUG_ON(!htlb_fault_mutex_table);
20148382d914SDavidlohr Bueso 
20158382d914SDavidlohr Bueso 	for (i = 0; i < num_fault_mutexes; i++)
20168382d914SDavidlohr Bueso 		mutex_init(&htlb_fault_mutex_table[i]);
2017a3437870SNishanth Aravamudan 	return 0;
2018a3437870SNishanth Aravamudan }
2019a3437870SNishanth Aravamudan module_init(hugetlb_init);
2020a3437870SNishanth Aravamudan 
2021a3437870SNishanth Aravamudan /* Should be called on processing a hugepagesz=... option */
2022a3437870SNishanth Aravamudan void __init hugetlb_add_hstate(unsigned order)
2023a3437870SNishanth Aravamudan {
2024a3437870SNishanth Aravamudan 	struct hstate *h;
20258faa8b07SAndi Kleen 	unsigned long i;
20268faa8b07SAndi Kleen 
2027a3437870SNishanth Aravamudan 	if (size_to_hstate(PAGE_SIZE << order)) {
2028ffb22af5SAndrew Morton 		pr_warning("hugepagesz= specified twice, ignoring\n");
2029a3437870SNishanth Aravamudan 		return;
2030a3437870SNishanth Aravamudan 	}
203147d38344SAneesh Kumar K.V 	BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
2032a3437870SNishanth Aravamudan 	BUG_ON(order == 0);
203347d38344SAneesh Kumar K.V 	h = &hstates[hugetlb_max_hstate++];
2034a3437870SNishanth Aravamudan 	h->order = order;
2035a3437870SNishanth Aravamudan 	h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
20368faa8b07SAndi Kleen 	h->nr_huge_pages = 0;
20378faa8b07SAndi Kleen 	h->free_huge_pages = 0;
20388faa8b07SAndi Kleen 	for (i = 0; i < MAX_NUMNODES; ++i)
20398faa8b07SAndi Kleen 		INIT_LIST_HEAD(&h->hugepage_freelists[i]);
20400edaecfaSAneesh Kumar K.V 	INIT_LIST_HEAD(&h->hugepage_activelist);
20418cebfcd0SLai Jiangshan 	h->next_nid_to_alloc = first_node(node_states[N_MEMORY]);
20428cebfcd0SLai Jiangshan 	h->next_nid_to_free = first_node(node_states[N_MEMORY]);
2043a3437870SNishanth Aravamudan 	snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
2044a3437870SNishanth Aravamudan 					huge_page_size(h)/1024);
20458faa8b07SAndi Kleen 
2046a3437870SNishanth Aravamudan 	parsed_hstate = h;
2047a3437870SNishanth Aravamudan }
2048a3437870SNishanth Aravamudan 
2049e11bfbfcSNick Piggin static int __init hugetlb_nrpages_setup(char *s)
2050a3437870SNishanth Aravamudan {
2051a3437870SNishanth Aravamudan 	unsigned long *mhp;
20528faa8b07SAndi Kleen 	static unsigned long *last_mhp;
2053a3437870SNishanth Aravamudan 
2054a3437870SNishanth Aravamudan 	/*
205547d38344SAneesh Kumar K.V 	 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
2056a3437870SNishanth Aravamudan 	 * so this hugepages= parameter goes to the "default hstate".
2057a3437870SNishanth Aravamudan 	 */
205847d38344SAneesh Kumar K.V 	if (!hugetlb_max_hstate)
2059a3437870SNishanth Aravamudan 		mhp = &default_hstate_max_huge_pages;
2060a3437870SNishanth Aravamudan 	else
2061a3437870SNishanth Aravamudan 		mhp = &parsed_hstate->max_huge_pages;
2062a3437870SNishanth Aravamudan 
20638faa8b07SAndi Kleen 	if (mhp == last_mhp) {
2064ffb22af5SAndrew Morton 		pr_warning("hugepages= specified twice without "
20658faa8b07SAndi Kleen 			   "interleaving hugepagesz=, ignoring\n");
20668faa8b07SAndi Kleen 		return 1;
20678faa8b07SAndi Kleen 	}
20688faa8b07SAndi Kleen 
2069a3437870SNishanth Aravamudan 	if (sscanf(s, "%lu", mhp) <= 0)
2070a3437870SNishanth Aravamudan 		*mhp = 0;
2071a3437870SNishanth Aravamudan 
20728faa8b07SAndi Kleen 	/*
20738faa8b07SAndi Kleen 	 * Global state is always initialized later in hugetlb_init.
20748faa8b07SAndi Kleen 	 * But we need to allocate >= MAX_ORDER hstates here early to still
20758faa8b07SAndi Kleen 	 * use the bootmem allocator.
20768faa8b07SAndi Kleen 	 */
207747d38344SAneesh Kumar K.V 	if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
20788faa8b07SAndi Kleen 		hugetlb_hstate_alloc_pages(parsed_hstate);
20798faa8b07SAndi Kleen 
20808faa8b07SAndi Kleen 	last_mhp = mhp;
20818faa8b07SAndi Kleen 
2082a3437870SNishanth Aravamudan 	return 1;
2083a3437870SNishanth Aravamudan }
2084e11bfbfcSNick Piggin __setup("hugepages=", hugetlb_nrpages_setup);
2085e11bfbfcSNick Piggin 
2086e11bfbfcSNick Piggin static int __init hugetlb_default_setup(char *s)
2087e11bfbfcSNick Piggin {
2088e11bfbfcSNick Piggin 	default_hstate_size = memparse(s, &s);
2089e11bfbfcSNick Piggin 	return 1;
2090e11bfbfcSNick Piggin }
2091e11bfbfcSNick Piggin __setup("default_hugepagesz=", hugetlb_default_setup);
2092a3437870SNishanth Aravamudan 
20938a213460SNishanth Aravamudan static unsigned int cpuset_mems_nr(unsigned int *array)
20948a213460SNishanth Aravamudan {
20958a213460SNishanth Aravamudan 	int node;
20968a213460SNishanth Aravamudan 	unsigned int nr = 0;
20978a213460SNishanth Aravamudan 
20988a213460SNishanth Aravamudan 	for_each_node_mask(node, cpuset_current_mems_allowed)
20998a213460SNishanth Aravamudan 		nr += array[node];
21008a213460SNishanth Aravamudan 
21018a213460SNishanth Aravamudan 	return nr;
21028a213460SNishanth Aravamudan }
21038a213460SNishanth Aravamudan 
21048a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL
210506808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
210606808b08SLee Schermerhorn 			 struct ctl_table *table, int write,
210706808b08SLee Schermerhorn 			 void __user *buffer, size_t *length, loff_t *ppos)
21081da177e4SLinus Torvalds {
2109e5ff2159SAndi Kleen 	struct hstate *h = &default_hstate;
2110e5ff2159SAndi Kleen 	unsigned long tmp;
211108d4a246SMichal Hocko 	int ret;
2112e5ff2159SAndi Kleen 
2113e5ff2159SAndi Kleen 	tmp = h->max_huge_pages;
2114e5ff2159SAndi Kleen 
2115adbe8726SEric B Munson 	if (write && h->order >= MAX_ORDER)
2116adbe8726SEric B Munson 		return -EINVAL;
2117adbe8726SEric B Munson 
2118e5ff2159SAndi Kleen 	table->data = &tmp;
2119e5ff2159SAndi Kleen 	table->maxlen = sizeof(unsigned long);
212008d4a246SMichal Hocko 	ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
212108d4a246SMichal Hocko 	if (ret)
212208d4a246SMichal Hocko 		goto out;
2123e5ff2159SAndi Kleen 
212406808b08SLee Schermerhorn 	if (write) {
2125bad44b5bSDavid Rientjes 		NODEMASK_ALLOC(nodemask_t, nodes_allowed,
2126bad44b5bSDavid Rientjes 						GFP_KERNEL | __GFP_NORETRY);
212706808b08SLee Schermerhorn 		if (!(obey_mempolicy &&
212806808b08SLee Schermerhorn 			       init_nodemask_of_mempolicy(nodes_allowed))) {
212906808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
21308cebfcd0SLai Jiangshan 			nodes_allowed = &node_states[N_MEMORY];
213106808b08SLee Schermerhorn 		}
213206808b08SLee Schermerhorn 		h->max_huge_pages = set_max_huge_pages(h, tmp, nodes_allowed);
213306808b08SLee Schermerhorn 
21348cebfcd0SLai Jiangshan 		if (nodes_allowed != &node_states[N_MEMORY])
213506808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
213606808b08SLee Schermerhorn 	}
213708d4a246SMichal Hocko out:
213808d4a246SMichal Hocko 	return ret;
21391da177e4SLinus Torvalds }
2140396faf03SMel Gorman 
214106808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write,
214206808b08SLee Schermerhorn 			  void __user *buffer, size_t *length, loff_t *ppos)
214306808b08SLee Schermerhorn {
214406808b08SLee Schermerhorn 
214506808b08SLee Schermerhorn 	return hugetlb_sysctl_handler_common(false, table, write,
214606808b08SLee Schermerhorn 							buffer, length, ppos);
214706808b08SLee Schermerhorn }
214806808b08SLee Schermerhorn 
214906808b08SLee Schermerhorn #ifdef CONFIG_NUMA
215006808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
215106808b08SLee Schermerhorn 			  void __user *buffer, size_t *length, loff_t *ppos)
215206808b08SLee Schermerhorn {
215306808b08SLee Schermerhorn 	return hugetlb_sysctl_handler_common(true, table, write,
215406808b08SLee Schermerhorn 							buffer, length, ppos);
215506808b08SLee Schermerhorn }
215606808b08SLee Schermerhorn #endif /* CONFIG_NUMA */
215706808b08SLee Schermerhorn 
2158a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write,
21598d65af78SAlexey Dobriyan 			void __user *buffer,
2160a3d0c6aaSNishanth Aravamudan 			size_t *length, loff_t *ppos)
2161a3d0c6aaSNishanth Aravamudan {
2162a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
2163e5ff2159SAndi Kleen 	unsigned long tmp;
216408d4a246SMichal Hocko 	int ret;
2165e5ff2159SAndi Kleen 
2166e5ff2159SAndi Kleen 	tmp = h->nr_overcommit_huge_pages;
2167e5ff2159SAndi Kleen 
2168adbe8726SEric B Munson 	if (write && h->order >= MAX_ORDER)
2169adbe8726SEric B Munson 		return -EINVAL;
2170adbe8726SEric B Munson 
2171e5ff2159SAndi Kleen 	table->data = &tmp;
2172e5ff2159SAndi Kleen 	table->maxlen = sizeof(unsigned long);
217308d4a246SMichal Hocko 	ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
217408d4a246SMichal Hocko 	if (ret)
217508d4a246SMichal Hocko 		goto out;
2176e5ff2159SAndi Kleen 
2177e5ff2159SAndi Kleen 	if (write) {
2178064d9efeSNishanth Aravamudan 		spin_lock(&hugetlb_lock);
2179e5ff2159SAndi Kleen 		h->nr_overcommit_huge_pages = tmp;
2180a3d0c6aaSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
2181e5ff2159SAndi Kleen 	}
218208d4a246SMichal Hocko out:
218308d4a246SMichal Hocko 	return ret;
2184a3d0c6aaSNishanth Aravamudan }
2185a3d0c6aaSNishanth Aravamudan 
21861da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */
21871da177e4SLinus Torvalds 
2188e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m)
21891da177e4SLinus Torvalds {
2190a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
2191e1759c21SAlexey Dobriyan 	seq_printf(m,
21921da177e4SLinus Torvalds 			"HugePages_Total:   %5lu\n"
21931da177e4SLinus Torvalds 			"HugePages_Free:    %5lu\n"
2194b45b5bd6SDavid Gibson 			"HugePages_Rsvd:    %5lu\n"
21957893d1d5SAdam Litke 			"HugePages_Surp:    %5lu\n"
21964f98a2feSRik van Riel 			"Hugepagesize:   %8lu kB\n",
2197a5516438SAndi Kleen 			h->nr_huge_pages,
2198a5516438SAndi Kleen 			h->free_huge_pages,
2199a5516438SAndi Kleen 			h->resv_huge_pages,
2200a5516438SAndi Kleen 			h->surplus_huge_pages,
2201a5516438SAndi Kleen 			1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
22021da177e4SLinus Torvalds }
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf)
22051da177e4SLinus Torvalds {
2206a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
22071da177e4SLinus Torvalds 	return sprintf(buf,
22081da177e4SLinus Torvalds 		"Node %d HugePages_Total: %5u\n"
2209a1de0919SNishanth Aravamudan 		"Node %d HugePages_Free:  %5u\n"
2210a1de0919SNishanth Aravamudan 		"Node %d HugePages_Surp:  %5u\n",
2211a5516438SAndi Kleen 		nid, h->nr_huge_pages_node[nid],
2212a5516438SAndi Kleen 		nid, h->free_huge_pages_node[nid],
2213a5516438SAndi Kleen 		nid, h->surplus_huge_pages_node[nid]);
22141da177e4SLinus Torvalds }
22151da177e4SLinus Torvalds 
2216949f7ec5SDavid Rientjes void hugetlb_show_meminfo(void)
2217949f7ec5SDavid Rientjes {
2218949f7ec5SDavid Rientjes 	struct hstate *h;
2219949f7ec5SDavid Rientjes 	int nid;
2220949f7ec5SDavid Rientjes 
2221949f7ec5SDavid Rientjes 	for_each_node_state(nid, N_MEMORY)
2222949f7ec5SDavid Rientjes 		for_each_hstate(h)
2223949f7ec5SDavid Rientjes 			pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
2224949f7ec5SDavid Rientjes 				nid,
2225949f7ec5SDavid Rientjes 				h->nr_huge_pages_node[nid],
2226949f7ec5SDavid Rientjes 				h->free_huge_pages_node[nid],
2227949f7ec5SDavid Rientjes 				h->surplus_huge_pages_node[nid],
2228949f7ec5SDavid Rientjes 				1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
2229949f7ec5SDavid Rientjes }
2230949f7ec5SDavid Rientjes 
22311da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
22321da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void)
22331da177e4SLinus Torvalds {
2234d0028588SWanpeng Li 	struct hstate *h;
2235d0028588SWanpeng Li 	unsigned long nr_total_pages = 0;
2236d0028588SWanpeng Li 
2237d0028588SWanpeng Li 	for_each_hstate(h)
2238d0028588SWanpeng Li 		nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
2239d0028588SWanpeng Li 	return nr_total_pages;
22401da177e4SLinus Torvalds }
22411da177e4SLinus Torvalds 
2242a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta)
2243fc1b8a73SMel Gorman {
2244fc1b8a73SMel Gorman 	int ret = -ENOMEM;
2245fc1b8a73SMel Gorman 
2246fc1b8a73SMel Gorman 	spin_lock(&hugetlb_lock);
2247fc1b8a73SMel Gorman 	/*
2248fc1b8a73SMel Gorman 	 * When cpuset is configured, it breaks the strict hugetlb page
2249fc1b8a73SMel Gorman 	 * reservation as the accounting is done on a global variable. Such
2250fc1b8a73SMel Gorman 	 * reservation is completely rubbish in the presence of cpuset because
2251fc1b8a73SMel Gorman 	 * the reservation is not checked against page availability for the
2252fc1b8a73SMel Gorman 	 * current cpuset. Application can still potentially OOM'ed by kernel
2253fc1b8a73SMel Gorman 	 * with lack of free htlb page in cpuset that the task is in.
2254fc1b8a73SMel Gorman 	 * Attempt to enforce strict accounting with cpuset is almost
2255fc1b8a73SMel Gorman 	 * impossible (or too ugly) because cpuset is too fluid that
2256fc1b8a73SMel Gorman 	 * task or memory node can be dynamically moved between cpusets.
2257fc1b8a73SMel Gorman 	 *
2258fc1b8a73SMel Gorman 	 * The change of semantics for shared hugetlb mapping with cpuset is
2259fc1b8a73SMel Gorman 	 * undesirable. However, in order to preserve some of the semantics,
2260fc1b8a73SMel Gorman 	 * we fall back to check against current free page availability as
2261fc1b8a73SMel Gorman 	 * a best attempt and hopefully to minimize the impact of changing
2262fc1b8a73SMel Gorman 	 * semantics that cpuset has.
2263fc1b8a73SMel Gorman 	 */
2264fc1b8a73SMel Gorman 	if (delta > 0) {
2265a5516438SAndi Kleen 		if (gather_surplus_pages(h, delta) < 0)
2266fc1b8a73SMel Gorman 			goto out;
2267fc1b8a73SMel Gorman 
2268a5516438SAndi Kleen 		if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
2269a5516438SAndi Kleen 			return_unused_surplus_pages(h, delta);
2270fc1b8a73SMel Gorman 			goto out;
2271fc1b8a73SMel Gorman 		}
2272fc1b8a73SMel Gorman 	}
2273fc1b8a73SMel Gorman 
2274fc1b8a73SMel Gorman 	ret = 0;
2275fc1b8a73SMel Gorman 	if (delta < 0)
2276a5516438SAndi Kleen 		return_unused_surplus_pages(h, (unsigned long) -delta);
2277fc1b8a73SMel Gorman 
2278fc1b8a73SMel Gorman out:
2279fc1b8a73SMel Gorman 	spin_unlock(&hugetlb_lock);
2280fc1b8a73SMel Gorman 	return ret;
2281fc1b8a73SMel Gorman }
2282fc1b8a73SMel Gorman 
228384afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma)
228484afd99bSAndy Whitcroft {
2285f522c3acSJoonsoo Kim 	struct resv_map *resv = vma_resv_map(vma);
228684afd99bSAndy Whitcroft 
228784afd99bSAndy Whitcroft 	/*
228884afd99bSAndy Whitcroft 	 * This new VMA should share its siblings reservation map if present.
228984afd99bSAndy Whitcroft 	 * The VMA will only ever have a valid reservation map pointer where
229084afd99bSAndy Whitcroft 	 * it is being copied for another still existing VMA.  As that VMA
229125985edcSLucas De Marchi 	 * has a reference to the reservation map it cannot disappear until
229284afd99bSAndy Whitcroft 	 * after this open call completes.  It is therefore safe to take a
229384afd99bSAndy Whitcroft 	 * new reference here without additional locking.
229484afd99bSAndy Whitcroft 	 */
22954e35f483SJoonsoo Kim 	if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
2296f522c3acSJoonsoo Kim 		kref_get(&resv->refs);
229784afd99bSAndy Whitcroft }
229884afd99bSAndy Whitcroft 
2299a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma)
2300a1e78772SMel Gorman {
2301a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2302f522c3acSJoonsoo Kim 	struct resv_map *resv = vma_resv_map(vma);
230390481622SDavid Gibson 	struct hugepage_subpool *spool = subpool_vma(vma);
23044e35f483SJoonsoo Kim 	unsigned long reserve, start, end;
230584afd99bSAndy Whitcroft 
23064e35f483SJoonsoo Kim 	if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
23074e35f483SJoonsoo Kim 		return;
23084e35f483SJoonsoo Kim 
2309a5516438SAndi Kleen 	start = vma_hugecache_offset(h, vma, vma->vm_start);
2310a5516438SAndi Kleen 	end = vma_hugecache_offset(h, vma, vma->vm_end);
231184afd99bSAndy Whitcroft 
23124e35f483SJoonsoo Kim 	reserve = (end - start) - region_count(resv, start, end);
231384afd99bSAndy Whitcroft 
2314f031dd27SJoonsoo Kim 	kref_put(&resv->refs, resv_map_release);
231584afd99bSAndy Whitcroft 
23167251ff78SAdam Litke 	if (reserve) {
2317a5516438SAndi Kleen 		hugetlb_acct_memory(h, -reserve);
231890481622SDavid Gibson 		hugepage_subpool_put_pages(spool, reserve);
23197251ff78SAdam Litke 	}
2320a1e78772SMel Gorman }
2321a1e78772SMel Gorman 
23221da177e4SLinus Torvalds /*
23231da177e4SLinus Torvalds  * We cannot handle pagefaults against hugetlb pages at all.  They cause
23241da177e4SLinus Torvalds  * handle_mm_fault() to try to instantiate regular-sized pages in the
23251da177e4SLinus Torvalds  * hugegpage VMA.  do_page_fault() is supposed to trap this, so BUG is we get
23261da177e4SLinus Torvalds  * this far.
23271da177e4SLinus Torvalds  */
2328d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
23291da177e4SLinus Torvalds {
23301da177e4SLinus Torvalds 	BUG();
2331d0217ac0SNick Piggin 	return 0;
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
2334f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = {
2335d0217ac0SNick Piggin 	.fault = hugetlb_vm_op_fault,
233684afd99bSAndy Whitcroft 	.open = hugetlb_vm_op_open,
2337a1e78772SMel Gorman 	.close = hugetlb_vm_op_close,
23381da177e4SLinus Torvalds };
23391da177e4SLinus Torvalds 
23401e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
23411e8f889bSDavid Gibson 				int writable)
234263551ae0SDavid Gibson {
234363551ae0SDavid Gibson 	pte_t entry;
234463551ae0SDavid Gibson 
23451e8f889bSDavid Gibson 	if (writable) {
2346106c992aSGerald Schaefer 		entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
2347106c992aSGerald Schaefer 					 vma->vm_page_prot)));
234863551ae0SDavid Gibson 	} else {
2349106c992aSGerald Schaefer 		entry = huge_pte_wrprotect(mk_huge_pte(page,
2350106c992aSGerald Schaefer 					   vma->vm_page_prot));
235163551ae0SDavid Gibson 	}
235263551ae0SDavid Gibson 	entry = pte_mkyoung(entry);
235363551ae0SDavid Gibson 	entry = pte_mkhuge(entry);
2354d9ed9faaSChris Metcalf 	entry = arch_make_huge_pte(entry, vma, page, writable);
235563551ae0SDavid Gibson 
235663551ae0SDavid Gibson 	return entry;
235763551ae0SDavid Gibson }
235863551ae0SDavid Gibson 
23591e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma,
23601e8f889bSDavid Gibson 				   unsigned long address, pte_t *ptep)
23611e8f889bSDavid Gibson {
23621e8f889bSDavid Gibson 	pte_t entry;
23631e8f889bSDavid Gibson 
2364106c992aSGerald Schaefer 	entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
236532f84528SChris Forbes 	if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
23664b3073e1SRussell King 		update_mmu_cache(vma, address, ptep);
23671e8f889bSDavid Gibson }
23681e8f889bSDavid Gibson 
23691e8f889bSDavid Gibson 
237063551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
237163551ae0SDavid Gibson 			    struct vm_area_struct *vma)
237263551ae0SDavid Gibson {
237363551ae0SDavid Gibson 	pte_t *src_pte, *dst_pte, entry;
237463551ae0SDavid Gibson 	struct page *ptepage;
23751c59827dSHugh Dickins 	unsigned long addr;
23761e8f889bSDavid Gibson 	int cow;
2377a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2378a5516438SAndi Kleen 	unsigned long sz = huge_page_size(h);
2379e8569dd2SAndreas Sandberg 	unsigned long mmun_start;	/* For mmu_notifiers */
2380e8569dd2SAndreas Sandberg 	unsigned long mmun_end;		/* For mmu_notifiers */
2381e8569dd2SAndreas Sandberg 	int ret = 0;
23821e8f889bSDavid Gibson 
23831e8f889bSDavid Gibson 	cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
238463551ae0SDavid Gibson 
2385e8569dd2SAndreas Sandberg 	mmun_start = vma->vm_start;
2386e8569dd2SAndreas Sandberg 	mmun_end = vma->vm_end;
2387e8569dd2SAndreas Sandberg 	if (cow)
2388e8569dd2SAndreas Sandberg 		mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end);
2389e8569dd2SAndreas Sandberg 
2390a5516438SAndi Kleen 	for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
2391cb900f41SKirill A. Shutemov 		spinlock_t *src_ptl, *dst_ptl;
2392c74df32cSHugh Dickins 		src_pte = huge_pte_offset(src, addr);
2393c74df32cSHugh Dickins 		if (!src_pte)
2394c74df32cSHugh Dickins 			continue;
2395a5516438SAndi Kleen 		dst_pte = huge_pte_alloc(dst, addr, sz);
2396e8569dd2SAndreas Sandberg 		if (!dst_pte) {
2397e8569dd2SAndreas Sandberg 			ret = -ENOMEM;
2398e8569dd2SAndreas Sandberg 			break;
2399e8569dd2SAndreas Sandberg 		}
2400c5c99429SLarry Woodman 
2401c5c99429SLarry Woodman 		/* If the pagetables are shared don't copy or take references */
2402c5c99429SLarry Woodman 		if (dst_pte == src_pte)
2403c5c99429SLarry Woodman 			continue;
2404c5c99429SLarry Woodman 
2405cb900f41SKirill A. Shutemov 		dst_ptl = huge_pte_lock(h, dst, dst_pte);
2406cb900f41SKirill A. Shutemov 		src_ptl = huge_pte_lockptr(h, src, src_pte);
2407cb900f41SKirill A. Shutemov 		spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
24087f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(src_pte))) {
24091e8f889bSDavid Gibson 			if (cow)
24107f2e9525SGerald Schaefer 				huge_ptep_set_wrprotect(src, addr, src_pte);
24117f2e9525SGerald Schaefer 			entry = huge_ptep_get(src_pte);
241263551ae0SDavid Gibson 			ptepage = pte_page(entry);
241363551ae0SDavid Gibson 			get_page(ptepage);
24140fe6e20bSNaoya Horiguchi 			page_dup_rmap(ptepage);
241563551ae0SDavid Gibson 			set_huge_pte_at(dst, addr, dst_pte, entry);
24161c59827dSHugh Dickins 		}
2417cb900f41SKirill A. Shutemov 		spin_unlock(src_ptl);
2418cb900f41SKirill A. Shutemov 		spin_unlock(dst_ptl);
241963551ae0SDavid Gibson 	}
242063551ae0SDavid Gibson 
2421e8569dd2SAndreas Sandberg 	if (cow)
2422e8569dd2SAndreas Sandberg 		mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end);
2423e8569dd2SAndreas Sandberg 
2424e8569dd2SAndreas Sandberg 	return ret;
242563551ae0SDavid Gibson }
242663551ae0SDavid Gibson 
2427290408d4SNaoya Horiguchi static int is_hugetlb_entry_migration(pte_t pte)
2428290408d4SNaoya Horiguchi {
2429290408d4SNaoya Horiguchi 	swp_entry_t swp;
2430290408d4SNaoya Horiguchi 
2431290408d4SNaoya Horiguchi 	if (huge_pte_none(pte) || pte_present(pte))
2432290408d4SNaoya Horiguchi 		return 0;
2433290408d4SNaoya Horiguchi 	swp = pte_to_swp_entry(pte);
243432f84528SChris Forbes 	if (non_swap_entry(swp) && is_migration_entry(swp))
2435290408d4SNaoya Horiguchi 		return 1;
243632f84528SChris Forbes 	else
2437290408d4SNaoya Horiguchi 		return 0;
2438290408d4SNaoya Horiguchi }
2439290408d4SNaoya Horiguchi 
2440fd6a03edSNaoya Horiguchi static int is_hugetlb_entry_hwpoisoned(pte_t pte)
2441fd6a03edSNaoya Horiguchi {
2442fd6a03edSNaoya Horiguchi 	swp_entry_t swp;
2443fd6a03edSNaoya Horiguchi 
2444fd6a03edSNaoya Horiguchi 	if (huge_pte_none(pte) || pte_present(pte))
2445fd6a03edSNaoya Horiguchi 		return 0;
2446fd6a03edSNaoya Horiguchi 	swp = pte_to_swp_entry(pte);
244732f84528SChris Forbes 	if (non_swap_entry(swp) && is_hwpoison_entry(swp))
2448fd6a03edSNaoya Horiguchi 		return 1;
244932f84528SChris Forbes 	else
2450fd6a03edSNaoya Horiguchi 		return 0;
2451fd6a03edSNaoya Horiguchi }
2452fd6a03edSNaoya Horiguchi 
245324669e58SAneesh Kumar K.V void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
245424669e58SAneesh Kumar K.V 			    unsigned long start, unsigned long end,
245524669e58SAneesh Kumar K.V 			    struct page *ref_page)
245663551ae0SDavid Gibson {
245724669e58SAneesh Kumar K.V 	int force_flush = 0;
245863551ae0SDavid Gibson 	struct mm_struct *mm = vma->vm_mm;
245963551ae0SDavid Gibson 	unsigned long address;
2460c7546f8fSDavid Gibson 	pte_t *ptep;
246163551ae0SDavid Gibson 	pte_t pte;
2462cb900f41SKirill A. Shutemov 	spinlock_t *ptl;
246363551ae0SDavid Gibson 	struct page *page;
2464a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2465a5516438SAndi Kleen 	unsigned long sz = huge_page_size(h);
24662ec74c3eSSagi Grimberg 	const unsigned long mmun_start = start;	/* For mmu_notifiers */
24672ec74c3eSSagi Grimberg 	const unsigned long mmun_end   = end;	/* For mmu_notifiers */
2468a5516438SAndi Kleen 
246963551ae0SDavid Gibson 	WARN_ON(!is_vm_hugetlb_page(vma));
2470a5516438SAndi Kleen 	BUG_ON(start & ~huge_page_mask(h));
2471a5516438SAndi Kleen 	BUG_ON(end & ~huge_page_mask(h));
247263551ae0SDavid Gibson 
247324669e58SAneesh Kumar K.V 	tlb_start_vma(tlb, vma);
24742ec74c3eSSagi Grimberg 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
247524669e58SAneesh Kumar K.V again:
2476a5516438SAndi Kleen 	for (address = start; address < end; address += sz) {
2477c7546f8fSDavid Gibson 		ptep = huge_pte_offset(mm, address);
2478c7546f8fSDavid Gibson 		if (!ptep)
2479c7546f8fSDavid Gibson 			continue;
2480c7546f8fSDavid Gibson 
2481cb900f41SKirill A. Shutemov 		ptl = huge_pte_lock(h, mm, ptep);
248239dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
2483cb900f41SKirill A. Shutemov 			goto unlock;
248439dde65cSChen, Kenneth W 
24856629326bSHillf Danton 		pte = huge_ptep_get(ptep);
24866629326bSHillf Danton 		if (huge_pte_none(pte))
2487cb900f41SKirill A. Shutemov 			goto unlock;
24886629326bSHillf Danton 
24896629326bSHillf Danton 		/*
24906629326bSHillf Danton 		 * HWPoisoned hugepage is already unmapped and dropped reference
24916629326bSHillf Danton 		 */
24928c4894c6SNaoya Horiguchi 		if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
2493106c992aSGerald Schaefer 			huge_pte_clear(mm, address, ptep);
2494cb900f41SKirill A. Shutemov 			goto unlock;
24958c4894c6SNaoya Horiguchi 		}
24966629326bSHillf Danton 
24976629326bSHillf Danton 		page = pte_page(pte);
249804f2cbe3SMel Gorman 		/*
249904f2cbe3SMel Gorman 		 * If a reference page is supplied, it is because a specific
250004f2cbe3SMel Gorman 		 * page is being unmapped, not a range. Ensure the page we
250104f2cbe3SMel Gorman 		 * are about to unmap is the actual page of interest.
250204f2cbe3SMel Gorman 		 */
250304f2cbe3SMel Gorman 		if (ref_page) {
250404f2cbe3SMel Gorman 			if (page != ref_page)
2505cb900f41SKirill A. Shutemov 				goto unlock;
250604f2cbe3SMel Gorman 
250704f2cbe3SMel Gorman 			/*
250804f2cbe3SMel Gorman 			 * Mark the VMA as having unmapped its page so that
250904f2cbe3SMel Gorman 			 * future faults in this VMA will fail rather than
251004f2cbe3SMel Gorman 			 * looking like data was lost
251104f2cbe3SMel Gorman 			 */
251204f2cbe3SMel Gorman 			set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
251304f2cbe3SMel Gorman 		}
251404f2cbe3SMel Gorman 
2515c7546f8fSDavid Gibson 		pte = huge_ptep_get_and_clear(mm, address, ptep);
251624669e58SAneesh Kumar K.V 		tlb_remove_tlb_entry(tlb, ptep, address);
2517106c992aSGerald Schaefer 		if (huge_pte_dirty(pte))
25186649a386SKen Chen 			set_page_dirty(page);
25199e81130bSHillf Danton 
252024669e58SAneesh Kumar K.V 		page_remove_rmap(page);
252124669e58SAneesh Kumar K.V 		force_flush = !__tlb_remove_page(tlb, page);
2522cb900f41SKirill A. Shutemov 		if (force_flush) {
2523cb900f41SKirill A. Shutemov 			spin_unlock(ptl);
25249e81130bSHillf Danton 			break;
252563551ae0SDavid Gibson 		}
2526cb900f41SKirill A. Shutemov 		/* Bail out after unmapping reference page if supplied */
2527cb900f41SKirill A. Shutemov 		if (ref_page) {
2528cb900f41SKirill A. Shutemov 			spin_unlock(ptl);
2529cb900f41SKirill A. Shutemov 			break;
2530cb900f41SKirill A. Shutemov 		}
2531cb900f41SKirill A. Shutemov unlock:
2532cb900f41SKirill A. Shutemov 		spin_unlock(ptl);
2533cb900f41SKirill A. Shutemov 	}
253424669e58SAneesh Kumar K.V 	/*
253524669e58SAneesh Kumar K.V 	 * mmu_gather ran out of room to batch pages, we break out of
253624669e58SAneesh Kumar K.V 	 * the PTE lock to avoid doing the potential expensive TLB invalidate
253724669e58SAneesh Kumar K.V 	 * and page-free while holding it.
253824669e58SAneesh Kumar K.V 	 */
253924669e58SAneesh Kumar K.V 	if (force_flush) {
254024669e58SAneesh Kumar K.V 		force_flush = 0;
254124669e58SAneesh Kumar K.V 		tlb_flush_mmu(tlb);
254224669e58SAneesh Kumar K.V 		if (address < end && !ref_page)
254324669e58SAneesh Kumar K.V 			goto again;
2544fe1668aeSChen, Kenneth W 	}
25452ec74c3eSSagi Grimberg 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
254624669e58SAneesh Kumar K.V 	tlb_end_vma(tlb, vma);
25471da177e4SLinus Torvalds }
254863551ae0SDavid Gibson 
2549d833352aSMel Gorman void __unmap_hugepage_range_final(struct mmu_gather *tlb,
2550d833352aSMel Gorman 			  struct vm_area_struct *vma, unsigned long start,
2551d833352aSMel Gorman 			  unsigned long end, struct page *ref_page)
2552d833352aSMel Gorman {
2553d833352aSMel Gorman 	__unmap_hugepage_range(tlb, vma, start, end, ref_page);
2554d833352aSMel Gorman 
2555d833352aSMel Gorman 	/*
2556d833352aSMel Gorman 	 * Clear this flag so that x86's huge_pmd_share page_table_shareable
2557d833352aSMel Gorman 	 * test will fail on a vma being torn down, and not grab a page table
2558d833352aSMel Gorman 	 * on its way out.  We're lucky that the flag has such an appropriate
2559d833352aSMel Gorman 	 * name, and can in fact be safely cleared here. We could clear it
2560d833352aSMel Gorman 	 * before the __unmap_hugepage_range above, but all that's necessary
2561d833352aSMel Gorman 	 * is to clear it before releasing the i_mmap_mutex. This works
2562d833352aSMel Gorman 	 * because in the context this is called, the VMA is about to be
2563d833352aSMel Gorman 	 * destroyed and the i_mmap_mutex is held.
2564d833352aSMel Gorman 	 */
2565d833352aSMel Gorman 	vma->vm_flags &= ~VM_MAYSHARE;
2566d833352aSMel Gorman }
2567d833352aSMel Gorman 
2568502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
256904f2cbe3SMel Gorman 			  unsigned long end, struct page *ref_page)
2570502717f4SChen, Kenneth W {
257124669e58SAneesh Kumar K.V 	struct mm_struct *mm;
257224669e58SAneesh Kumar K.V 	struct mmu_gather tlb;
257324669e58SAneesh Kumar K.V 
257424669e58SAneesh Kumar K.V 	mm = vma->vm_mm;
257524669e58SAneesh Kumar K.V 
25762b047252SLinus Torvalds 	tlb_gather_mmu(&tlb, mm, start, end);
257724669e58SAneesh Kumar K.V 	__unmap_hugepage_range(&tlb, vma, start, end, ref_page);
257824669e58SAneesh Kumar K.V 	tlb_finish_mmu(&tlb, start, end);
2579502717f4SChen, Kenneth W }
2580502717f4SChen, Kenneth W 
258104f2cbe3SMel Gorman /*
258204f2cbe3SMel Gorman  * This is called when the original mapper is failing to COW a MAP_PRIVATE
258304f2cbe3SMel Gorman  * mappping it owns the reserve page for. The intention is to unmap the page
258404f2cbe3SMel Gorman  * from other VMAs and let the children be SIGKILLed if they are faulting the
258504f2cbe3SMel Gorman  * same region.
258604f2cbe3SMel Gorman  */
25872a4b3dedSHarvey Harrison static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
25882a4b3dedSHarvey Harrison 				struct page *page, unsigned long address)
258904f2cbe3SMel Gorman {
25907526674dSAdam Litke 	struct hstate *h = hstate_vma(vma);
259104f2cbe3SMel Gorman 	struct vm_area_struct *iter_vma;
259204f2cbe3SMel Gorman 	struct address_space *mapping;
259304f2cbe3SMel Gorman 	pgoff_t pgoff;
259404f2cbe3SMel Gorman 
259504f2cbe3SMel Gorman 	/*
259604f2cbe3SMel Gorman 	 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
259704f2cbe3SMel Gorman 	 * from page cache lookup which is in HPAGE_SIZE units.
259804f2cbe3SMel Gorman 	 */
25997526674dSAdam Litke 	address = address & huge_page_mask(h);
260036e4f20aSMichal Hocko 	pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
260136e4f20aSMichal Hocko 			vma->vm_pgoff;
2602496ad9aaSAl Viro 	mapping = file_inode(vma->vm_file)->i_mapping;
260304f2cbe3SMel Gorman 
26044eb2b1dcSMel Gorman 	/*
26054eb2b1dcSMel Gorman 	 * Take the mapping lock for the duration of the table walk. As
26064eb2b1dcSMel Gorman 	 * this mapping should be shared between all the VMAs,
26074eb2b1dcSMel Gorman 	 * __unmap_hugepage_range() is called as the lock is already held
26084eb2b1dcSMel Gorman 	 */
26093d48ae45SPeter Zijlstra 	mutex_lock(&mapping->i_mmap_mutex);
26106b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
261104f2cbe3SMel Gorman 		/* Do not unmap the current VMA */
261204f2cbe3SMel Gorman 		if (iter_vma == vma)
261304f2cbe3SMel Gorman 			continue;
261404f2cbe3SMel Gorman 
261504f2cbe3SMel Gorman 		/*
261604f2cbe3SMel Gorman 		 * Unmap the page from other VMAs without their own reserves.
261704f2cbe3SMel Gorman 		 * They get marked to be SIGKILLed if they fault in these
261804f2cbe3SMel Gorman 		 * areas. This is because a future no-page fault on this VMA
261904f2cbe3SMel Gorman 		 * could insert a zeroed page instead of the data existing
262004f2cbe3SMel Gorman 		 * from the time of fork. This would look like data corruption
262104f2cbe3SMel Gorman 		 */
262204f2cbe3SMel Gorman 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
262324669e58SAneesh Kumar K.V 			unmap_hugepage_range(iter_vma, address,
262424669e58SAneesh Kumar K.V 					     address + huge_page_size(h), page);
262504f2cbe3SMel Gorman 	}
26263d48ae45SPeter Zijlstra 	mutex_unlock(&mapping->i_mmap_mutex);
262704f2cbe3SMel Gorman 
262804f2cbe3SMel Gorman 	return 1;
262904f2cbe3SMel Gorman }
263004f2cbe3SMel Gorman 
26310fe6e20bSNaoya Horiguchi /*
26320fe6e20bSNaoya Horiguchi  * Hugetlb_cow() should be called with page lock of the original hugepage held.
2633ef009b25SMichal Hocko  * Called with hugetlb_instantiation_mutex held and pte_page locked so we
2634ef009b25SMichal Hocko  * cannot race with other handlers or page migration.
2635ef009b25SMichal Hocko  * Keep the pte_same checks anyway to make transition from the mutex easier.
26360fe6e20bSNaoya Horiguchi  */
26371e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
263804f2cbe3SMel Gorman 			unsigned long address, pte_t *ptep, pte_t pte,
2639cb900f41SKirill A. Shutemov 			struct page *pagecache_page, spinlock_t *ptl)
26401e8f889bSDavid Gibson {
2641a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
26421e8f889bSDavid Gibson 	struct page *old_page, *new_page;
264304f2cbe3SMel Gorman 	int outside_reserve = 0;
26442ec74c3eSSagi Grimberg 	unsigned long mmun_start;	/* For mmu_notifiers */
26452ec74c3eSSagi Grimberg 	unsigned long mmun_end;		/* For mmu_notifiers */
26461e8f889bSDavid Gibson 
26471e8f889bSDavid Gibson 	old_page = pte_page(pte);
26481e8f889bSDavid Gibson 
264904f2cbe3SMel Gorman retry_avoidcopy:
26501e8f889bSDavid Gibson 	/* If no-one else is actually using this page, avoid the copy
26511e8f889bSDavid Gibson 	 * and just make the page writable */
265237a2140dSJoonsoo Kim 	if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
26530fe6e20bSNaoya Horiguchi 		page_move_anon_rmap(old_page, vma, address);
26541e8f889bSDavid Gibson 		set_huge_ptep_writable(vma, address, ptep);
265583c54070SNick Piggin 		return 0;
26561e8f889bSDavid Gibson 	}
26571e8f889bSDavid Gibson 
265804f2cbe3SMel Gorman 	/*
265904f2cbe3SMel Gorman 	 * If the process that created a MAP_PRIVATE mapping is about to
266004f2cbe3SMel Gorman 	 * perform a COW due to a shared page count, attempt to satisfy
266104f2cbe3SMel Gorman 	 * the allocation without using the existing reserves. The pagecache
266204f2cbe3SMel Gorman 	 * page is used to determine if the reserve at this address was
266304f2cbe3SMel Gorman 	 * consumed or not. If reserves were used, a partial faulted mapping
266404f2cbe3SMel Gorman 	 * at the time of fork() could consume its reserves on COW instead
266504f2cbe3SMel Gorman 	 * of the full address range.
266604f2cbe3SMel Gorman 	 */
26675944d011SJoonsoo Kim 	if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
266804f2cbe3SMel Gorman 			old_page != pagecache_page)
266904f2cbe3SMel Gorman 		outside_reserve = 1;
267004f2cbe3SMel Gorman 
26711e8f889bSDavid Gibson 	page_cache_get(old_page);
2672b76c8cfbSLarry Woodman 
2673cb900f41SKirill A. Shutemov 	/* Drop page table lock as buddy allocator may be called */
2674cb900f41SKirill A. Shutemov 	spin_unlock(ptl);
267504f2cbe3SMel Gorman 	new_page = alloc_huge_page(vma, address, outside_reserve);
26761e8f889bSDavid Gibson 
26772fc39cecSAdam Litke 	if (IS_ERR(new_page)) {
267876dcee75SAneesh Kumar K.V 		long err = PTR_ERR(new_page);
26791e8f889bSDavid Gibson 		page_cache_release(old_page);
268004f2cbe3SMel Gorman 
268104f2cbe3SMel Gorman 		/*
268204f2cbe3SMel Gorman 		 * If a process owning a MAP_PRIVATE mapping fails to COW,
268304f2cbe3SMel Gorman 		 * it is due to references held by a child and an insufficient
268404f2cbe3SMel Gorman 		 * huge page pool. To guarantee the original mappers
268504f2cbe3SMel Gorman 		 * reliability, unmap the page from child processes. The child
268604f2cbe3SMel Gorman 		 * may get SIGKILLed if it later faults.
268704f2cbe3SMel Gorman 		 */
268804f2cbe3SMel Gorman 		if (outside_reserve) {
268904f2cbe3SMel Gorman 			BUG_ON(huge_pte_none(pte));
269004f2cbe3SMel Gorman 			if (unmap_ref_private(mm, vma, old_page, address)) {
269104f2cbe3SMel Gorman 				BUG_ON(huge_pte_none(pte));
2692cb900f41SKirill A. Shutemov 				spin_lock(ptl);
2693a734bcc8SHillf Danton 				ptep = huge_pte_offset(mm, address & huge_page_mask(h));
2694a9af0c5dSNaoya Horiguchi 				if (likely(ptep &&
2695a9af0c5dSNaoya Horiguchi 					   pte_same(huge_ptep_get(ptep), pte)))
269604f2cbe3SMel Gorman 					goto retry_avoidcopy;
2697a734bcc8SHillf Danton 				/*
2698cb900f41SKirill A. Shutemov 				 * race occurs while re-acquiring page table
2699cb900f41SKirill A. Shutemov 				 * lock, and our job is done.
2700a734bcc8SHillf Danton 				 */
2701a734bcc8SHillf Danton 				return 0;
270204f2cbe3SMel Gorman 			}
270304f2cbe3SMel Gorman 			WARN_ON_ONCE(1);
270404f2cbe3SMel Gorman 		}
270504f2cbe3SMel Gorman 
2706b76c8cfbSLarry Woodman 		/* Caller expects lock to be held */
2707cb900f41SKirill A. Shutemov 		spin_lock(ptl);
270876dcee75SAneesh Kumar K.V 		if (err == -ENOMEM)
270976dcee75SAneesh Kumar K.V 			return VM_FAULT_OOM;
271076dcee75SAneesh Kumar K.V 		else
271176dcee75SAneesh Kumar K.V 			return VM_FAULT_SIGBUS;
27121e8f889bSDavid Gibson 	}
27131e8f889bSDavid Gibson 
27140fe6e20bSNaoya Horiguchi 	/*
27150fe6e20bSNaoya Horiguchi 	 * When the original hugepage is shared one, it does not have
27160fe6e20bSNaoya Horiguchi 	 * anon_vma prepared.
27170fe6e20bSNaoya Horiguchi 	 */
271844e2aa93SDean Nelson 	if (unlikely(anon_vma_prepare(vma))) {
2719ea4039a3SHillf Danton 		page_cache_release(new_page);
2720ea4039a3SHillf Danton 		page_cache_release(old_page);
272144e2aa93SDean Nelson 		/* Caller expects lock to be held */
2722cb900f41SKirill A. Shutemov 		spin_lock(ptl);
27230fe6e20bSNaoya Horiguchi 		return VM_FAULT_OOM;
272444e2aa93SDean Nelson 	}
27250fe6e20bSNaoya Horiguchi 
272647ad8475SAndrea Arcangeli 	copy_user_huge_page(new_page, old_page, address, vma,
272747ad8475SAndrea Arcangeli 			    pages_per_huge_page(h));
27280ed361deSNick Piggin 	__SetPageUptodate(new_page);
27291e8f889bSDavid Gibson 
27302ec74c3eSSagi Grimberg 	mmun_start = address & huge_page_mask(h);
27312ec74c3eSSagi Grimberg 	mmun_end = mmun_start + huge_page_size(h);
27322ec74c3eSSagi Grimberg 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2733b76c8cfbSLarry Woodman 	/*
2734cb900f41SKirill A. Shutemov 	 * Retake the page table lock to check for racing updates
2735b76c8cfbSLarry Woodman 	 * before the page tables are altered
2736b76c8cfbSLarry Woodman 	 */
2737cb900f41SKirill A. Shutemov 	spin_lock(ptl);
2738a5516438SAndi Kleen 	ptep = huge_pte_offset(mm, address & huge_page_mask(h));
2739a9af0c5dSNaoya Horiguchi 	if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
274007443a85SJoonsoo Kim 		ClearPagePrivate(new_page);
274107443a85SJoonsoo Kim 
27421e8f889bSDavid Gibson 		/* Break COW */
27438fe627ecSGerald Schaefer 		huge_ptep_clear_flush(vma, address, ptep);
27441e8f889bSDavid Gibson 		set_huge_pte_at(mm, address, ptep,
27451e8f889bSDavid Gibson 				make_huge_pte(vma, new_page, 1));
27460fe6e20bSNaoya Horiguchi 		page_remove_rmap(old_page);
2747cd67f0d2SNaoya Horiguchi 		hugepage_add_new_anon_rmap(new_page, vma, address);
27481e8f889bSDavid Gibson 		/* Make the old page be freed below */
27491e8f889bSDavid Gibson 		new_page = old_page;
27501e8f889bSDavid Gibson 	}
2751cb900f41SKirill A. Shutemov 	spin_unlock(ptl);
27522ec74c3eSSagi Grimberg 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
27531e8f889bSDavid Gibson 	page_cache_release(new_page);
27541e8f889bSDavid Gibson 	page_cache_release(old_page);
27558312034fSJoonsoo Kim 
27568312034fSJoonsoo Kim 	/* Caller expects lock to be held */
2757cb900f41SKirill A. Shutemov 	spin_lock(ptl);
275883c54070SNick Piggin 	return 0;
27591e8f889bSDavid Gibson }
27601e8f889bSDavid Gibson 
276104f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */
2762a5516438SAndi Kleen static struct page *hugetlbfs_pagecache_page(struct hstate *h,
2763a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long address)
276404f2cbe3SMel Gorman {
276504f2cbe3SMel Gorman 	struct address_space *mapping;
2766e7c4b0bfSAndy Whitcroft 	pgoff_t idx;
276704f2cbe3SMel Gorman 
276804f2cbe3SMel Gorman 	mapping = vma->vm_file->f_mapping;
2769a5516438SAndi Kleen 	idx = vma_hugecache_offset(h, vma, address);
277004f2cbe3SMel Gorman 
277104f2cbe3SMel Gorman 	return find_lock_page(mapping, idx);
277204f2cbe3SMel Gorman }
277304f2cbe3SMel Gorman 
27743ae77f43SHugh Dickins /*
27753ae77f43SHugh Dickins  * Return whether there is a pagecache page to back given address within VMA.
27763ae77f43SHugh Dickins  * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
27773ae77f43SHugh Dickins  */
27783ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h,
27792a15efc9SHugh Dickins 			struct vm_area_struct *vma, unsigned long address)
27802a15efc9SHugh Dickins {
27812a15efc9SHugh Dickins 	struct address_space *mapping;
27822a15efc9SHugh Dickins 	pgoff_t idx;
27832a15efc9SHugh Dickins 	struct page *page;
27842a15efc9SHugh Dickins 
27852a15efc9SHugh Dickins 	mapping = vma->vm_file->f_mapping;
27862a15efc9SHugh Dickins 	idx = vma_hugecache_offset(h, vma, address);
27872a15efc9SHugh Dickins 
27882a15efc9SHugh Dickins 	page = find_get_page(mapping, idx);
27892a15efc9SHugh Dickins 	if (page)
27902a15efc9SHugh Dickins 		put_page(page);
27912a15efc9SHugh Dickins 	return page != NULL;
27922a15efc9SHugh Dickins }
27932a15efc9SHugh Dickins 
2794a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
27958382d914SDavidlohr Bueso 			   struct address_space *mapping, pgoff_t idx,
2796788c7df4SHugh Dickins 			   unsigned long address, pte_t *ptep, unsigned int flags)
2797ac9b9c66SHugh Dickins {
2798a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2799ac9b9c66SHugh Dickins 	int ret = VM_FAULT_SIGBUS;
2800409eb8c2SHillf Danton 	int anon_rmap = 0;
28014c887265SAdam Litke 	unsigned long size;
28024c887265SAdam Litke 	struct page *page;
28031e8f889bSDavid Gibson 	pte_t new_pte;
2804cb900f41SKirill A. Shutemov 	spinlock_t *ptl;
28054c887265SAdam Litke 
280604f2cbe3SMel Gorman 	/*
280704f2cbe3SMel Gorman 	 * Currently, we are forced to kill the process in the event the
280804f2cbe3SMel Gorman 	 * original mapper has unmapped pages from the child due to a failed
280925985edcSLucas De Marchi 	 * COW. Warn that such a situation has occurred as it may not be obvious
281004f2cbe3SMel Gorman 	 */
281104f2cbe3SMel Gorman 	if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
2812ffb22af5SAndrew Morton 		pr_warning("PID %d killed due to inadequate hugepage pool\n",
281304f2cbe3SMel Gorman 			   current->pid);
281404f2cbe3SMel Gorman 		return ret;
281504f2cbe3SMel Gorman 	}
281604f2cbe3SMel Gorman 
28174c887265SAdam Litke 	/*
28184c887265SAdam Litke 	 * Use page lock to guard against racing truncation
28194c887265SAdam Litke 	 * before we get page_table_lock.
28204c887265SAdam Litke 	 */
28216bda666aSChristoph Lameter retry:
28226bda666aSChristoph Lameter 	page = find_lock_page(mapping, idx);
28236bda666aSChristoph Lameter 	if (!page) {
2824a5516438SAndi Kleen 		size = i_size_read(mapping->host) >> huge_page_shift(h);
2825ebed4bfcSHugh Dickins 		if (idx >= size)
2826ebed4bfcSHugh Dickins 			goto out;
282704f2cbe3SMel Gorman 		page = alloc_huge_page(vma, address, 0);
28282fc39cecSAdam Litke 		if (IS_ERR(page)) {
282976dcee75SAneesh Kumar K.V 			ret = PTR_ERR(page);
283076dcee75SAneesh Kumar K.V 			if (ret == -ENOMEM)
283176dcee75SAneesh Kumar K.V 				ret = VM_FAULT_OOM;
283276dcee75SAneesh Kumar K.V 			else
283376dcee75SAneesh Kumar K.V 				ret = VM_FAULT_SIGBUS;
28346bda666aSChristoph Lameter 			goto out;
28356bda666aSChristoph Lameter 		}
283647ad8475SAndrea Arcangeli 		clear_huge_page(page, address, pages_per_huge_page(h));
28370ed361deSNick Piggin 		__SetPageUptodate(page);
2838ac9b9c66SHugh Dickins 
2839f83a275dSMel Gorman 		if (vma->vm_flags & VM_MAYSHARE) {
28406bda666aSChristoph Lameter 			int err;
284145c682a6SKen Chen 			struct inode *inode = mapping->host;
28426bda666aSChristoph Lameter 
28436bda666aSChristoph Lameter 			err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
28446bda666aSChristoph Lameter 			if (err) {
28456bda666aSChristoph Lameter 				put_page(page);
28466bda666aSChristoph Lameter 				if (err == -EEXIST)
28476bda666aSChristoph Lameter 					goto retry;
28486bda666aSChristoph Lameter 				goto out;
28496bda666aSChristoph Lameter 			}
285007443a85SJoonsoo Kim 			ClearPagePrivate(page);
285145c682a6SKen Chen 
285245c682a6SKen Chen 			spin_lock(&inode->i_lock);
2853a5516438SAndi Kleen 			inode->i_blocks += blocks_per_huge_page(h);
285445c682a6SKen Chen 			spin_unlock(&inode->i_lock);
285523be7468SMel Gorman 		} else {
28566bda666aSChristoph Lameter 			lock_page(page);
28570fe6e20bSNaoya Horiguchi 			if (unlikely(anon_vma_prepare(vma))) {
28580fe6e20bSNaoya Horiguchi 				ret = VM_FAULT_OOM;
28590fe6e20bSNaoya Horiguchi 				goto backout_unlocked;
286023be7468SMel Gorman 			}
2861409eb8c2SHillf Danton 			anon_rmap = 1;
28620fe6e20bSNaoya Horiguchi 		}
28630fe6e20bSNaoya Horiguchi 	} else {
286457303d80SAndy Whitcroft 		/*
2865998b4382SNaoya Horiguchi 		 * If memory error occurs between mmap() and fault, some process
2866998b4382SNaoya Horiguchi 		 * don't have hwpoisoned swap entry for errored virtual address.
2867998b4382SNaoya Horiguchi 		 * So we need to block hugepage fault by PG_hwpoison bit check.
2868fd6a03edSNaoya Horiguchi 		 */
2869fd6a03edSNaoya Horiguchi 		if (unlikely(PageHWPoison(page))) {
2870aa50d3a7SAndi Kleen 			ret = VM_FAULT_HWPOISON |
2871972dc4deSAneesh Kumar K.V 				VM_FAULT_SET_HINDEX(hstate_index(h));
2872fd6a03edSNaoya Horiguchi 			goto backout_unlocked;
28736bda666aSChristoph Lameter 		}
2874998b4382SNaoya Horiguchi 	}
28751e8f889bSDavid Gibson 
287657303d80SAndy Whitcroft 	/*
287757303d80SAndy Whitcroft 	 * If we are going to COW a private mapping later, we examine the
287857303d80SAndy Whitcroft 	 * pending reservations for this page now. This will ensure that
287957303d80SAndy Whitcroft 	 * any allocations necessary to record that reservation occur outside
288057303d80SAndy Whitcroft 	 * the spinlock.
288157303d80SAndy Whitcroft 	 */
2882788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
28832b26736cSAndy Whitcroft 		if (vma_needs_reservation(h, vma, address) < 0) {
28842b26736cSAndy Whitcroft 			ret = VM_FAULT_OOM;
28852b26736cSAndy Whitcroft 			goto backout_unlocked;
28862b26736cSAndy Whitcroft 		}
288757303d80SAndy Whitcroft 
2888cb900f41SKirill A. Shutemov 	ptl = huge_pte_lockptr(h, mm, ptep);
2889cb900f41SKirill A. Shutemov 	spin_lock(ptl);
2890a5516438SAndi Kleen 	size = i_size_read(mapping->host) >> huge_page_shift(h);
28914c887265SAdam Litke 	if (idx >= size)
28924c887265SAdam Litke 		goto backout;
28934c887265SAdam Litke 
289483c54070SNick Piggin 	ret = 0;
28957f2e9525SGerald Schaefer 	if (!huge_pte_none(huge_ptep_get(ptep)))
28964c887265SAdam Litke 		goto backout;
28974c887265SAdam Litke 
289807443a85SJoonsoo Kim 	if (anon_rmap) {
289907443a85SJoonsoo Kim 		ClearPagePrivate(page);
2900409eb8c2SHillf Danton 		hugepage_add_new_anon_rmap(page, vma, address);
290107443a85SJoonsoo Kim 	}
2902409eb8c2SHillf Danton 	else
2903409eb8c2SHillf Danton 		page_dup_rmap(page);
29041e8f889bSDavid Gibson 	new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
29051e8f889bSDavid Gibson 				&& (vma->vm_flags & VM_SHARED)));
29061e8f889bSDavid Gibson 	set_huge_pte_at(mm, address, ptep, new_pte);
29071e8f889bSDavid Gibson 
2908788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
29091e8f889bSDavid Gibson 		/* Optimization, do the COW without a second fault */
2910cb900f41SKirill A. Shutemov 		ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page, ptl);
29111e8f889bSDavid Gibson 	}
29121e8f889bSDavid Gibson 
2913cb900f41SKirill A. Shutemov 	spin_unlock(ptl);
29144c887265SAdam Litke 	unlock_page(page);
29154c887265SAdam Litke out:
2916ac9b9c66SHugh Dickins 	return ret;
29174c887265SAdam Litke 
29184c887265SAdam Litke backout:
2919cb900f41SKirill A. Shutemov 	spin_unlock(ptl);
29202b26736cSAndy Whitcroft backout_unlocked:
29214c887265SAdam Litke 	unlock_page(page);
29224c887265SAdam Litke 	put_page(page);
29234c887265SAdam Litke 	goto out;
2924ac9b9c66SHugh Dickins }
2925ac9b9c66SHugh Dickins 
29268382d914SDavidlohr Bueso #ifdef CONFIG_SMP
29278382d914SDavidlohr Bueso static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm,
29288382d914SDavidlohr Bueso 			    struct vm_area_struct *vma,
29298382d914SDavidlohr Bueso 			    struct address_space *mapping,
29308382d914SDavidlohr Bueso 			    pgoff_t idx, unsigned long address)
29318382d914SDavidlohr Bueso {
29328382d914SDavidlohr Bueso 	unsigned long key[2];
29338382d914SDavidlohr Bueso 	u32 hash;
29348382d914SDavidlohr Bueso 
29358382d914SDavidlohr Bueso 	if (vma->vm_flags & VM_SHARED) {
29368382d914SDavidlohr Bueso 		key[0] = (unsigned long) mapping;
29378382d914SDavidlohr Bueso 		key[1] = idx;
29388382d914SDavidlohr Bueso 	} else {
29398382d914SDavidlohr Bueso 		key[0] = (unsigned long) mm;
29408382d914SDavidlohr Bueso 		key[1] = address >> huge_page_shift(h);
29418382d914SDavidlohr Bueso 	}
29428382d914SDavidlohr Bueso 
29438382d914SDavidlohr Bueso 	hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0);
29448382d914SDavidlohr Bueso 
29458382d914SDavidlohr Bueso 	return hash & (num_fault_mutexes - 1);
29468382d914SDavidlohr Bueso }
29478382d914SDavidlohr Bueso #else
29488382d914SDavidlohr Bueso /*
29498382d914SDavidlohr Bueso  * For uniprocesor systems we always use a single mutex, so just
29508382d914SDavidlohr Bueso  * return 0 and avoid the hashing overhead.
29518382d914SDavidlohr Bueso  */
29528382d914SDavidlohr Bueso static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm,
29538382d914SDavidlohr Bueso 			    struct vm_area_struct *vma,
29548382d914SDavidlohr Bueso 			    struct address_space *mapping,
29558382d914SDavidlohr Bueso 			    pgoff_t idx, unsigned long address)
29568382d914SDavidlohr Bueso {
29578382d914SDavidlohr Bueso 	return 0;
29588382d914SDavidlohr Bueso }
29598382d914SDavidlohr Bueso #endif
29608382d914SDavidlohr Bueso 
296186e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2962788c7df4SHugh Dickins 			unsigned long address, unsigned int flags)
296386e5216fSAdam Litke {
29648382d914SDavidlohr Bueso 	pte_t *ptep, entry;
2965cb900f41SKirill A. Shutemov 	spinlock_t *ptl;
29661e8f889bSDavid Gibson 	int ret;
29678382d914SDavidlohr Bueso 	u32 hash;
29688382d914SDavidlohr Bueso 	pgoff_t idx;
29690fe6e20bSNaoya Horiguchi 	struct page *page = NULL;
297057303d80SAndy Whitcroft 	struct page *pagecache_page = NULL;
2971a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
29728382d914SDavidlohr Bueso 	struct address_space *mapping;
297386e5216fSAdam Litke 
29741e16a539SKAMEZAWA Hiroyuki 	address &= huge_page_mask(h);
29751e16a539SKAMEZAWA Hiroyuki 
2976fd6a03edSNaoya Horiguchi 	ptep = huge_pte_offset(mm, address);
2977fd6a03edSNaoya Horiguchi 	if (ptep) {
2978fd6a03edSNaoya Horiguchi 		entry = huge_ptep_get(ptep);
2979290408d4SNaoya Horiguchi 		if (unlikely(is_hugetlb_entry_migration(entry))) {
2980cb900f41SKirill A. Shutemov 			migration_entry_wait_huge(vma, mm, ptep);
2981290408d4SNaoya Horiguchi 			return 0;
2982290408d4SNaoya Horiguchi 		} else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
2983aa50d3a7SAndi Kleen 			return VM_FAULT_HWPOISON_LARGE |
2984972dc4deSAneesh Kumar K.V 				VM_FAULT_SET_HINDEX(hstate_index(h));
2985fd6a03edSNaoya Horiguchi 	}
2986fd6a03edSNaoya Horiguchi 
2987a5516438SAndi Kleen 	ptep = huge_pte_alloc(mm, address, huge_page_size(h));
298886e5216fSAdam Litke 	if (!ptep)
298986e5216fSAdam Litke 		return VM_FAULT_OOM;
299086e5216fSAdam Litke 
29918382d914SDavidlohr Bueso 	mapping = vma->vm_file->f_mapping;
29928382d914SDavidlohr Bueso 	idx = vma_hugecache_offset(h, vma, address);
29938382d914SDavidlohr Bueso 
29943935baa9SDavid Gibson 	/*
29953935baa9SDavid Gibson 	 * Serialize hugepage allocation and instantiation, so that we don't
29963935baa9SDavid Gibson 	 * get spurious allocation failures if two CPUs race to instantiate
29973935baa9SDavid Gibson 	 * the same page in the page cache.
29983935baa9SDavid Gibson 	 */
29998382d914SDavidlohr Bueso 	hash = fault_mutex_hash(h, mm, vma, mapping, idx, address);
30008382d914SDavidlohr Bueso 	mutex_lock(&htlb_fault_mutex_table[hash]);
30018382d914SDavidlohr Bueso 
30027f2e9525SGerald Schaefer 	entry = huge_ptep_get(ptep);
30037f2e9525SGerald Schaefer 	if (huge_pte_none(entry)) {
30048382d914SDavidlohr Bueso 		ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags);
3005b4d1d99fSDavid Gibson 		goto out_mutex;
30063935baa9SDavid Gibson 	}
300786e5216fSAdam Litke 
300883c54070SNick Piggin 	ret = 0;
30091e8f889bSDavid Gibson 
301057303d80SAndy Whitcroft 	/*
301157303d80SAndy Whitcroft 	 * If we are going to COW the mapping later, we examine the pending
301257303d80SAndy Whitcroft 	 * reservations for this page now. This will ensure that any
301357303d80SAndy Whitcroft 	 * allocations necessary to record that reservation occur outside the
301457303d80SAndy Whitcroft 	 * spinlock. For private mappings, we also lookup the pagecache
301557303d80SAndy Whitcroft 	 * page now as it is used to determine if a reservation has been
301657303d80SAndy Whitcroft 	 * consumed.
301757303d80SAndy Whitcroft 	 */
3018106c992aSGerald Schaefer 	if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
30192b26736cSAndy Whitcroft 		if (vma_needs_reservation(h, vma, address) < 0) {
30202b26736cSAndy Whitcroft 			ret = VM_FAULT_OOM;
3021b4d1d99fSDavid Gibson 			goto out_mutex;
30222b26736cSAndy Whitcroft 		}
302357303d80SAndy Whitcroft 
3024f83a275dSMel Gorman 		if (!(vma->vm_flags & VM_MAYSHARE))
302557303d80SAndy Whitcroft 			pagecache_page = hugetlbfs_pagecache_page(h,
302657303d80SAndy Whitcroft 								vma, address);
302757303d80SAndy Whitcroft 	}
302857303d80SAndy Whitcroft 
302956c9cfb1SNaoya Horiguchi 	/*
303056c9cfb1SNaoya Horiguchi 	 * hugetlb_cow() requires page locks of pte_page(entry) and
303156c9cfb1SNaoya Horiguchi 	 * pagecache_page, so here we need take the former one
303256c9cfb1SNaoya Horiguchi 	 * when page != pagecache_page or !pagecache_page.
303356c9cfb1SNaoya Horiguchi 	 * Note that locking order is always pagecache_page -> page,
303456c9cfb1SNaoya Horiguchi 	 * so no worry about deadlock.
303556c9cfb1SNaoya Horiguchi 	 */
30360fe6e20bSNaoya Horiguchi 	page = pte_page(entry);
303766aebce7SChris Metcalf 	get_page(page);
303856c9cfb1SNaoya Horiguchi 	if (page != pagecache_page)
30390fe6e20bSNaoya Horiguchi 		lock_page(page);
30400fe6e20bSNaoya Horiguchi 
3041cb900f41SKirill A. Shutemov 	ptl = huge_pte_lockptr(h, mm, ptep);
3042cb900f41SKirill A. Shutemov 	spin_lock(ptl);
30431e8f889bSDavid Gibson 	/* Check for a racing update before calling hugetlb_cow */
3044b4d1d99fSDavid Gibson 	if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
3045cb900f41SKirill A. Shutemov 		goto out_ptl;
3046b4d1d99fSDavid Gibson 
3047b4d1d99fSDavid Gibson 
3048788c7df4SHugh Dickins 	if (flags & FAULT_FLAG_WRITE) {
3049106c992aSGerald Schaefer 		if (!huge_pte_write(entry)) {
305057303d80SAndy Whitcroft 			ret = hugetlb_cow(mm, vma, address, ptep, entry,
3051cb900f41SKirill A. Shutemov 					pagecache_page, ptl);
3052cb900f41SKirill A. Shutemov 			goto out_ptl;
3053b4d1d99fSDavid Gibson 		}
3054106c992aSGerald Schaefer 		entry = huge_pte_mkdirty(entry);
3055b4d1d99fSDavid Gibson 	}
3056b4d1d99fSDavid Gibson 	entry = pte_mkyoung(entry);
3057788c7df4SHugh Dickins 	if (huge_ptep_set_access_flags(vma, address, ptep, entry,
3058788c7df4SHugh Dickins 						flags & FAULT_FLAG_WRITE))
30594b3073e1SRussell King 		update_mmu_cache(vma, address, ptep);
3060b4d1d99fSDavid Gibson 
3061cb900f41SKirill A. Shutemov out_ptl:
3062cb900f41SKirill A. Shutemov 	spin_unlock(ptl);
306357303d80SAndy Whitcroft 
306457303d80SAndy Whitcroft 	if (pagecache_page) {
306557303d80SAndy Whitcroft 		unlock_page(pagecache_page);
306657303d80SAndy Whitcroft 		put_page(pagecache_page);
306757303d80SAndy Whitcroft 	}
30681f64d69cSDean Nelson 	if (page != pagecache_page)
306956c9cfb1SNaoya Horiguchi 		unlock_page(page);
307066aebce7SChris Metcalf 	put_page(page);
307157303d80SAndy Whitcroft 
3072b4d1d99fSDavid Gibson out_mutex:
30738382d914SDavidlohr Bueso 	mutex_unlock(&htlb_fault_mutex_table[hash]);
30741e8f889bSDavid Gibson 	return ret;
307586e5216fSAdam Litke }
307686e5216fSAdam Litke 
307728a35716SMichel Lespinasse long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
307863551ae0SDavid Gibson 			 struct page **pages, struct vm_area_struct **vmas,
307928a35716SMichel Lespinasse 			 unsigned long *position, unsigned long *nr_pages,
308028a35716SMichel Lespinasse 			 long i, unsigned int flags)
308163551ae0SDavid Gibson {
3082d5d4b0aaSChen, Kenneth W 	unsigned long pfn_offset;
3083d5d4b0aaSChen, Kenneth W 	unsigned long vaddr = *position;
308428a35716SMichel Lespinasse 	unsigned long remainder = *nr_pages;
3085a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
308663551ae0SDavid Gibson 
308763551ae0SDavid Gibson 	while (vaddr < vma->vm_end && remainder) {
308863551ae0SDavid Gibson 		pte_t *pte;
3089cb900f41SKirill A. Shutemov 		spinlock_t *ptl = NULL;
30902a15efc9SHugh Dickins 		int absent;
309163551ae0SDavid Gibson 		struct page *page;
309263551ae0SDavid Gibson 
30934c887265SAdam Litke 		/*
30944c887265SAdam Litke 		 * Some archs (sparc64, sh*) have multiple pte_ts to
30952a15efc9SHugh Dickins 		 * each hugepage.  We have to make sure we get the
30964c887265SAdam Litke 		 * first, for the page indexing below to work.
3097cb900f41SKirill A. Shutemov 		 *
3098cb900f41SKirill A. Shutemov 		 * Note that page table lock is not held when pte is null.
30994c887265SAdam Litke 		 */
3100a5516438SAndi Kleen 		pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
3101cb900f41SKirill A. Shutemov 		if (pte)
3102cb900f41SKirill A. Shutemov 			ptl = huge_pte_lock(h, mm, pte);
31032a15efc9SHugh Dickins 		absent = !pte || huge_pte_none(huge_ptep_get(pte));
310463551ae0SDavid Gibson 
31052a15efc9SHugh Dickins 		/*
31062a15efc9SHugh Dickins 		 * When coredumping, it suits get_dump_page if we just return
31073ae77f43SHugh Dickins 		 * an error where there's an empty slot with no huge pagecache
31083ae77f43SHugh Dickins 		 * to back it.  This way, we avoid allocating a hugepage, and
31093ae77f43SHugh Dickins 		 * the sparse dumpfile avoids allocating disk blocks, but its
31103ae77f43SHugh Dickins 		 * huge holes still show up with zeroes where they need to be.
31112a15efc9SHugh Dickins 		 */
31123ae77f43SHugh Dickins 		if (absent && (flags & FOLL_DUMP) &&
31133ae77f43SHugh Dickins 		    !hugetlbfs_pagecache_present(h, vma, vaddr)) {
3114cb900f41SKirill A. Shutemov 			if (pte)
3115cb900f41SKirill A. Shutemov 				spin_unlock(ptl);
31162a15efc9SHugh Dickins 			remainder = 0;
31172a15efc9SHugh Dickins 			break;
31182a15efc9SHugh Dickins 		}
31192a15efc9SHugh Dickins 
31209cc3a5bdSNaoya Horiguchi 		/*
31219cc3a5bdSNaoya Horiguchi 		 * We need call hugetlb_fault for both hugepages under migration
31229cc3a5bdSNaoya Horiguchi 		 * (in which case hugetlb_fault waits for the migration,) and
31239cc3a5bdSNaoya Horiguchi 		 * hwpoisoned hugepages (in which case we need to prevent the
31249cc3a5bdSNaoya Horiguchi 		 * caller from accessing to them.) In order to do this, we use
31259cc3a5bdSNaoya Horiguchi 		 * here is_swap_pte instead of is_hugetlb_entry_migration and
31269cc3a5bdSNaoya Horiguchi 		 * is_hugetlb_entry_hwpoisoned. This is because it simply covers
31279cc3a5bdSNaoya Horiguchi 		 * both cases, and because we can't follow correct pages
31289cc3a5bdSNaoya Horiguchi 		 * directly from any kind of swap entries.
31299cc3a5bdSNaoya Horiguchi 		 */
31309cc3a5bdSNaoya Horiguchi 		if (absent || is_swap_pte(huge_ptep_get(pte)) ||
3131106c992aSGerald Schaefer 		    ((flags & FOLL_WRITE) &&
3132106c992aSGerald Schaefer 		      !huge_pte_write(huge_ptep_get(pte)))) {
31334c887265SAdam Litke 			int ret;
31344c887265SAdam Litke 
3135cb900f41SKirill A. Shutemov 			if (pte)
3136cb900f41SKirill A. Shutemov 				spin_unlock(ptl);
31372a15efc9SHugh Dickins 			ret = hugetlb_fault(mm, vma, vaddr,
31382a15efc9SHugh Dickins 				(flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
3139a89182c7SAdam Litke 			if (!(ret & VM_FAULT_ERROR))
31404c887265SAdam Litke 				continue;
31414c887265SAdam Litke 
31421c59827dSHugh Dickins 			remainder = 0;
31431c59827dSHugh Dickins 			break;
31441c59827dSHugh Dickins 		}
314563551ae0SDavid Gibson 
3146a5516438SAndi Kleen 		pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
31477f2e9525SGerald Schaefer 		page = pte_page(huge_ptep_get(pte));
3148d5d4b0aaSChen, Kenneth W same_page:
3149d6692183SChen, Kenneth W 		if (pages) {
315069d177c2SAndy Whitcroft 			pages[i] = mem_map_offset(page, pfn_offset);
3151a0368d4eSAndrea Arcangeli 			get_page_foll(pages[i]);
3152d6692183SChen, Kenneth W 		}
315363551ae0SDavid Gibson 
315463551ae0SDavid Gibson 		if (vmas)
315563551ae0SDavid Gibson 			vmas[i] = vma;
315663551ae0SDavid Gibson 
315763551ae0SDavid Gibson 		vaddr += PAGE_SIZE;
3158d5d4b0aaSChen, Kenneth W 		++pfn_offset;
315963551ae0SDavid Gibson 		--remainder;
316063551ae0SDavid Gibson 		++i;
3161d5d4b0aaSChen, Kenneth W 		if (vaddr < vma->vm_end && remainder &&
3162a5516438SAndi Kleen 				pfn_offset < pages_per_huge_page(h)) {
3163d5d4b0aaSChen, Kenneth W 			/*
3164d5d4b0aaSChen, Kenneth W 			 * We use pfn_offset to avoid touching the pageframes
3165d5d4b0aaSChen, Kenneth W 			 * of this compound page.
3166d5d4b0aaSChen, Kenneth W 			 */
3167d5d4b0aaSChen, Kenneth W 			goto same_page;
3168d5d4b0aaSChen, Kenneth W 		}
3169cb900f41SKirill A. Shutemov 		spin_unlock(ptl);
317063551ae0SDavid Gibson 	}
317128a35716SMichel Lespinasse 	*nr_pages = remainder;
317263551ae0SDavid Gibson 	*position = vaddr;
317363551ae0SDavid Gibson 
31742a15efc9SHugh Dickins 	return i ? i : -EFAULT;
317563551ae0SDavid Gibson }
31768f860591SZhang, Yanmin 
31777da4d641SPeter Zijlstra unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
31788f860591SZhang, Yanmin 		unsigned long address, unsigned long end, pgprot_t newprot)
31798f860591SZhang, Yanmin {
31808f860591SZhang, Yanmin 	struct mm_struct *mm = vma->vm_mm;
31818f860591SZhang, Yanmin 	unsigned long start = address;
31828f860591SZhang, Yanmin 	pte_t *ptep;
31838f860591SZhang, Yanmin 	pte_t pte;
3184a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
31857da4d641SPeter Zijlstra 	unsigned long pages = 0;
31868f860591SZhang, Yanmin 
31878f860591SZhang, Yanmin 	BUG_ON(address >= end);
31888f860591SZhang, Yanmin 	flush_cache_range(vma, address, end);
31898f860591SZhang, Yanmin 
3190a5338093SRik van Riel 	mmu_notifier_invalidate_range_start(mm, start, end);
31913d48ae45SPeter Zijlstra 	mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
3192a5516438SAndi Kleen 	for (; address < end; address += huge_page_size(h)) {
3193cb900f41SKirill A. Shutemov 		spinlock_t *ptl;
31948f860591SZhang, Yanmin 		ptep = huge_pte_offset(mm, address);
31958f860591SZhang, Yanmin 		if (!ptep)
31968f860591SZhang, Yanmin 			continue;
3197cb900f41SKirill A. Shutemov 		ptl = huge_pte_lock(h, mm, ptep);
31987da4d641SPeter Zijlstra 		if (huge_pmd_unshare(mm, &address, ptep)) {
31997da4d641SPeter Zijlstra 			pages++;
3200cb900f41SKirill A. Shutemov 			spin_unlock(ptl);
320139dde65cSChen, Kenneth W 			continue;
32027da4d641SPeter Zijlstra 		}
32037f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(ptep))) {
32048f860591SZhang, Yanmin 			pte = huge_ptep_get_and_clear(mm, address, ptep);
3205106c992aSGerald Schaefer 			pte = pte_mkhuge(huge_pte_modify(pte, newprot));
3206be7517d6STony Lu 			pte = arch_make_huge_pte(pte, vma, NULL, 0);
32078f860591SZhang, Yanmin 			set_huge_pte_at(mm, address, ptep, pte);
32087da4d641SPeter Zijlstra 			pages++;
32098f860591SZhang, Yanmin 		}
3210cb900f41SKirill A. Shutemov 		spin_unlock(ptl);
32118f860591SZhang, Yanmin 	}
3212d833352aSMel Gorman 	/*
3213d833352aSMel Gorman 	 * Must flush TLB before releasing i_mmap_mutex: x86's huge_pmd_unshare
3214d833352aSMel Gorman 	 * may have cleared our pud entry and done put_page on the page table:
3215d833352aSMel Gorman 	 * once we release i_mmap_mutex, another task can do the final put_page
3216d833352aSMel Gorman 	 * and that page table be reused and filled with junk.
3217d833352aSMel Gorman 	 */
32188f860591SZhang, Yanmin 	flush_tlb_range(vma, start, end);
3219d833352aSMel Gorman 	mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
3220a5338093SRik van Riel 	mmu_notifier_invalidate_range_end(mm, start, end);
32217da4d641SPeter Zijlstra 
32227da4d641SPeter Zijlstra 	return pages << h->order;
32238f860591SZhang, Yanmin }
32248f860591SZhang, Yanmin 
3225a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode,
3226a1e78772SMel Gorman 					long from, long to,
32275a6fe125SMel Gorman 					struct vm_area_struct *vma,
3228ca16d140SKOSAKI Motohiro 					vm_flags_t vm_flags)
3229e4e574b7SAdam Litke {
323017c9d12eSMel Gorman 	long ret, chg;
3231a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
323290481622SDavid Gibson 	struct hugepage_subpool *spool = subpool_inode(inode);
32339119a41eSJoonsoo Kim 	struct resv_map *resv_map;
3234e4e574b7SAdam Litke 
3235a1e78772SMel Gorman 	/*
323617c9d12eSMel Gorman 	 * Only apply hugepage reservation if asked. At fault time, an
323717c9d12eSMel Gorman 	 * attempt will be made for VM_NORESERVE to allocate a page
323890481622SDavid Gibson 	 * without using reserves
323917c9d12eSMel Gorman 	 */
3240ca16d140SKOSAKI Motohiro 	if (vm_flags & VM_NORESERVE)
324117c9d12eSMel Gorman 		return 0;
324217c9d12eSMel Gorman 
324317c9d12eSMel Gorman 	/*
3244a1e78772SMel Gorman 	 * Shared mappings base their reservation on the number of pages that
3245a1e78772SMel Gorman 	 * are already allocated on behalf of the file. Private mappings need
3246a1e78772SMel Gorman 	 * to reserve the full area even if read-only as mprotect() may be
3247a1e78772SMel Gorman 	 * called to make the mapping read-write. Assume !vma is a shm mapping
3248a1e78772SMel Gorman 	 */
32499119a41eSJoonsoo Kim 	if (!vma || vma->vm_flags & VM_MAYSHARE) {
32504e35f483SJoonsoo Kim 		resv_map = inode_resv_map(inode);
32519119a41eSJoonsoo Kim 
32521406ec9bSJoonsoo Kim 		chg = region_chg(resv_map, from, to);
32539119a41eSJoonsoo Kim 
32549119a41eSJoonsoo Kim 	} else {
32559119a41eSJoonsoo Kim 		resv_map = resv_map_alloc();
32565a6fe125SMel Gorman 		if (!resv_map)
32575a6fe125SMel Gorman 			return -ENOMEM;
32585a6fe125SMel Gorman 
325917c9d12eSMel Gorman 		chg = to - from;
326017c9d12eSMel Gorman 
32615a6fe125SMel Gorman 		set_vma_resv_map(vma, resv_map);
32625a6fe125SMel Gorman 		set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
32635a6fe125SMel Gorman 	}
32645a6fe125SMel Gorman 
3265c50ac050SDave Hansen 	if (chg < 0) {
3266c50ac050SDave Hansen 		ret = chg;
3267c50ac050SDave Hansen 		goto out_err;
3268c50ac050SDave Hansen 	}
326917c9d12eSMel Gorman 
327090481622SDavid Gibson 	/* There must be enough pages in the subpool for the mapping */
3271c50ac050SDave Hansen 	if (hugepage_subpool_get_pages(spool, chg)) {
3272c50ac050SDave Hansen 		ret = -ENOSPC;
3273c50ac050SDave Hansen 		goto out_err;
3274c50ac050SDave Hansen 	}
327517c9d12eSMel Gorman 
327617c9d12eSMel Gorman 	/*
327717c9d12eSMel Gorman 	 * Check enough hugepages are available for the reservation.
327890481622SDavid Gibson 	 * Hand the pages back to the subpool if there are not
327917c9d12eSMel Gorman 	 */
328017c9d12eSMel Gorman 	ret = hugetlb_acct_memory(h, chg);
328117c9d12eSMel Gorman 	if (ret < 0) {
328290481622SDavid Gibson 		hugepage_subpool_put_pages(spool, chg);
3283c50ac050SDave Hansen 		goto out_err;
328417c9d12eSMel Gorman 	}
328517c9d12eSMel Gorman 
328617c9d12eSMel Gorman 	/*
328717c9d12eSMel Gorman 	 * Account for the reservations made. Shared mappings record regions
328817c9d12eSMel Gorman 	 * that have reservations as they are shared by multiple VMAs.
328917c9d12eSMel Gorman 	 * When the last VMA disappears, the region map says how much
329017c9d12eSMel Gorman 	 * the reservation was and the page cache tells how much of
329117c9d12eSMel Gorman 	 * the reservation was consumed. Private mappings are per-VMA and
329217c9d12eSMel Gorman 	 * only the consumed reservations are tracked. When the VMA
329317c9d12eSMel Gorman 	 * disappears, the original reservation is the VMA size and the
329417c9d12eSMel Gorman 	 * consumed reservations are stored in the map. Hence, nothing
329517c9d12eSMel Gorman 	 * else has to be done for private mappings here
329617c9d12eSMel Gorman 	 */
3297f83a275dSMel Gorman 	if (!vma || vma->vm_flags & VM_MAYSHARE)
32981406ec9bSJoonsoo Kim 		region_add(resv_map, from, to);
3299a43a8c39SChen, Kenneth W 	return 0;
3300c50ac050SDave Hansen out_err:
3301f031dd27SJoonsoo Kim 	if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
3302f031dd27SJoonsoo Kim 		kref_put(&resv_map->refs, resv_map_release);
3303c50ac050SDave Hansen 	return ret;
3304a43a8c39SChen, Kenneth W }
3305a43a8c39SChen, Kenneth W 
3306a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
3307a43a8c39SChen, Kenneth W {
3308a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
33094e35f483SJoonsoo Kim 	struct resv_map *resv_map = inode_resv_map(inode);
33109119a41eSJoonsoo Kim 	long chg = 0;
331190481622SDavid Gibson 	struct hugepage_subpool *spool = subpool_inode(inode);
331245c682a6SKen Chen 
33139119a41eSJoonsoo Kim 	if (resv_map)
33141406ec9bSJoonsoo Kim 		chg = region_truncate(resv_map, offset);
331545c682a6SKen Chen 	spin_lock(&inode->i_lock);
3316e4c6f8beSEric Sandeen 	inode->i_blocks -= (blocks_per_huge_page(h) * freed);
331745c682a6SKen Chen 	spin_unlock(&inode->i_lock);
331845c682a6SKen Chen 
331990481622SDavid Gibson 	hugepage_subpool_put_pages(spool, (chg - freed));
3320a5516438SAndi Kleen 	hugetlb_acct_memory(h, -(chg - freed));
3321a43a8c39SChen, Kenneth W }
332293f70f90SNaoya Horiguchi 
33233212b535SSteve Capper #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
33243212b535SSteve Capper static unsigned long page_table_shareable(struct vm_area_struct *svma,
33253212b535SSteve Capper 				struct vm_area_struct *vma,
33263212b535SSteve Capper 				unsigned long addr, pgoff_t idx)
33273212b535SSteve Capper {
33283212b535SSteve Capper 	unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
33293212b535SSteve Capper 				svma->vm_start;
33303212b535SSteve Capper 	unsigned long sbase = saddr & PUD_MASK;
33313212b535SSteve Capper 	unsigned long s_end = sbase + PUD_SIZE;
33323212b535SSteve Capper 
33333212b535SSteve Capper 	/* Allow segments to share if only one is marked locked */
33343212b535SSteve Capper 	unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED;
33353212b535SSteve Capper 	unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED;
33363212b535SSteve Capper 
33373212b535SSteve Capper 	/*
33383212b535SSteve Capper 	 * match the virtual addresses, permission and the alignment of the
33393212b535SSteve Capper 	 * page table page.
33403212b535SSteve Capper 	 */
33413212b535SSteve Capper 	if (pmd_index(addr) != pmd_index(saddr) ||
33423212b535SSteve Capper 	    vm_flags != svm_flags ||
33433212b535SSteve Capper 	    sbase < svma->vm_start || svma->vm_end < s_end)
33443212b535SSteve Capper 		return 0;
33453212b535SSteve Capper 
33463212b535SSteve Capper 	return saddr;
33473212b535SSteve Capper }
33483212b535SSteve Capper 
33493212b535SSteve Capper static int vma_shareable(struct vm_area_struct *vma, unsigned long addr)
33503212b535SSteve Capper {
33513212b535SSteve Capper 	unsigned long base = addr & PUD_MASK;
33523212b535SSteve Capper 	unsigned long end = base + PUD_SIZE;
33533212b535SSteve Capper 
33543212b535SSteve Capper 	/*
33553212b535SSteve Capper 	 * check on proper vm_flags and page table alignment
33563212b535SSteve Capper 	 */
33573212b535SSteve Capper 	if (vma->vm_flags & VM_MAYSHARE &&
33583212b535SSteve Capper 	    vma->vm_start <= base && end <= vma->vm_end)
33593212b535SSteve Capper 		return 1;
33603212b535SSteve Capper 	return 0;
33613212b535SSteve Capper }
33623212b535SSteve Capper 
33633212b535SSteve Capper /*
33643212b535SSteve Capper  * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
33653212b535SSteve Capper  * and returns the corresponding pte. While this is not necessary for the
33663212b535SSteve Capper  * !shared pmd case because we can allocate the pmd later as well, it makes the
33673212b535SSteve Capper  * code much cleaner. pmd allocation is essential for the shared case because
33683212b535SSteve Capper  * pud has to be populated inside the same i_mmap_mutex section - otherwise
33693212b535SSteve Capper  * racing tasks could either miss the sharing (see huge_pte_offset) or select a
33703212b535SSteve Capper  * bad pmd for sharing.
33713212b535SSteve Capper  */
33723212b535SSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
33733212b535SSteve Capper {
33743212b535SSteve Capper 	struct vm_area_struct *vma = find_vma(mm, addr);
33753212b535SSteve Capper 	struct address_space *mapping = vma->vm_file->f_mapping;
33763212b535SSteve Capper 	pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
33773212b535SSteve Capper 			vma->vm_pgoff;
33783212b535SSteve Capper 	struct vm_area_struct *svma;
33793212b535SSteve Capper 	unsigned long saddr;
33803212b535SSteve Capper 	pte_t *spte = NULL;
33813212b535SSteve Capper 	pte_t *pte;
3382cb900f41SKirill A. Shutemov 	spinlock_t *ptl;
33833212b535SSteve Capper 
33843212b535SSteve Capper 	if (!vma_shareable(vma, addr))
33853212b535SSteve Capper 		return (pte_t *)pmd_alloc(mm, pud, addr);
33863212b535SSteve Capper 
33873212b535SSteve Capper 	mutex_lock(&mapping->i_mmap_mutex);
33883212b535SSteve Capper 	vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
33893212b535SSteve Capper 		if (svma == vma)
33903212b535SSteve Capper 			continue;
33913212b535SSteve Capper 
33923212b535SSteve Capper 		saddr = page_table_shareable(svma, vma, addr, idx);
33933212b535SSteve Capper 		if (saddr) {
33943212b535SSteve Capper 			spte = huge_pte_offset(svma->vm_mm, saddr);
33953212b535SSteve Capper 			if (spte) {
33963212b535SSteve Capper 				get_page(virt_to_page(spte));
33973212b535SSteve Capper 				break;
33983212b535SSteve Capper 			}
33993212b535SSteve Capper 		}
34003212b535SSteve Capper 	}
34013212b535SSteve Capper 
34023212b535SSteve Capper 	if (!spte)
34033212b535SSteve Capper 		goto out;
34043212b535SSteve Capper 
3405cb900f41SKirill A. Shutemov 	ptl = huge_pte_lockptr(hstate_vma(vma), mm, spte);
3406cb900f41SKirill A. Shutemov 	spin_lock(ptl);
34073212b535SSteve Capper 	if (pud_none(*pud))
34083212b535SSteve Capper 		pud_populate(mm, pud,
34093212b535SSteve Capper 				(pmd_t *)((unsigned long)spte & PAGE_MASK));
34103212b535SSteve Capper 	else
34113212b535SSteve Capper 		put_page(virt_to_page(spte));
3412cb900f41SKirill A. Shutemov 	spin_unlock(ptl);
34133212b535SSteve Capper out:
34143212b535SSteve Capper 	pte = (pte_t *)pmd_alloc(mm, pud, addr);
34153212b535SSteve Capper 	mutex_unlock(&mapping->i_mmap_mutex);
34163212b535SSteve Capper 	return pte;
34173212b535SSteve Capper }
34183212b535SSteve Capper 
34193212b535SSteve Capper /*
34203212b535SSteve Capper  * unmap huge page backed by shared pte.
34213212b535SSteve Capper  *
34223212b535SSteve Capper  * Hugetlb pte page is ref counted at the time of mapping.  If pte is shared
34233212b535SSteve Capper  * indicated by page_count > 1, unmap is achieved by clearing pud and
34243212b535SSteve Capper  * decrementing the ref count. If count == 1, the pte page is not shared.
34253212b535SSteve Capper  *
3426cb900f41SKirill A. Shutemov  * called with page table lock held.
34273212b535SSteve Capper  *
34283212b535SSteve Capper  * returns: 1 successfully unmapped a shared pte page
34293212b535SSteve Capper  *	    0 the underlying pte page is not shared, or it is the last user
34303212b535SSteve Capper  */
34313212b535SSteve Capper int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
34323212b535SSteve Capper {
34333212b535SSteve Capper 	pgd_t *pgd = pgd_offset(mm, *addr);
34343212b535SSteve Capper 	pud_t *pud = pud_offset(pgd, *addr);
34353212b535SSteve Capper 
34363212b535SSteve Capper 	BUG_ON(page_count(virt_to_page(ptep)) == 0);
34373212b535SSteve Capper 	if (page_count(virt_to_page(ptep)) == 1)
34383212b535SSteve Capper 		return 0;
34393212b535SSteve Capper 
34403212b535SSteve Capper 	pud_clear(pud);
34413212b535SSteve Capper 	put_page(virt_to_page(ptep));
34423212b535SSteve Capper 	*addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
34433212b535SSteve Capper 	return 1;
34443212b535SSteve Capper }
34459e5fc74cSSteve Capper #define want_pmd_share()	(1)
34469e5fc74cSSteve Capper #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
34479e5fc74cSSteve Capper pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
34489e5fc74cSSteve Capper {
34499e5fc74cSSteve Capper 	return NULL;
34509e5fc74cSSteve Capper }
34519e5fc74cSSteve Capper #define want_pmd_share()	(0)
34523212b535SSteve Capper #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
34533212b535SSteve Capper 
34549e5fc74cSSteve Capper #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
34559e5fc74cSSteve Capper pte_t *huge_pte_alloc(struct mm_struct *mm,
34569e5fc74cSSteve Capper 			unsigned long addr, unsigned long sz)
34579e5fc74cSSteve Capper {
34589e5fc74cSSteve Capper 	pgd_t *pgd;
34599e5fc74cSSteve Capper 	pud_t *pud;
34609e5fc74cSSteve Capper 	pte_t *pte = NULL;
34619e5fc74cSSteve Capper 
34629e5fc74cSSteve Capper 	pgd = pgd_offset(mm, addr);
34639e5fc74cSSteve Capper 	pud = pud_alloc(mm, pgd, addr);
34649e5fc74cSSteve Capper 	if (pud) {
34659e5fc74cSSteve Capper 		if (sz == PUD_SIZE) {
34669e5fc74cSSteve Capper 			pte = (pte_t *)pud;
34679e5fc74cSSteve Capper 		} else {
34689e5fc74cSSteve Capper 			BUG_ON(sz != PMD_SIZE);
34699e5fc74cSSteve Capper 			if (want_pmd_share() && pud_none(*pud))
34709e5fc74cSSteve Capper 				pte = huge_pmd_share(mm, addr, pud);
34719e5fc74cSSteve Capper 			else
34729e5fc74cSSteve Capper 				pte = (pte_t *)pmd_alloc(mm, pud, addr);
34739e5fc74cSSteve Capper 		}
34749e5fc74cSSteve Capper 	}
34759e5fc74cSSteve Capper 	BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
34769e5fc74cSSteve Capper 
34779e5fc74cSSteve Capper 	return pte;
34789e5fc74cSSteve Capper }
34799e5fc74cSSteve Capper 
34809e5fc74cSSteve Capper pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
34819e5fc74cSSteve Capper {
34829e5fc74cSSteve Capper 	pgd_t *pgd;
34839e5fc74cSSteve Capper 	pud_t *pud;
34849e5fc74cSSteve Capper 	pmd_t *pmd = NULL;
34859e5fc74cSSteve Capper 
34869e5fc74cSSteve Capper 	pgd = pgd_offset(mm, addr);
34879e5fc74cSSteve Capper 	if (pgd_present(*pgd)) {
34889e5fc74cSSteve Capper 		pud = pud_offset(pgd, addr);
34899e5fc74cSSteve Capper 		if (pud_present(*pud)) {
34909e5fc74cSSteve Capper 			if (pud_huge(*pud))
34919e5fc74cSSteve Capper 				return (pte_t *)pud;
34929e5fc74cSSteve Capper 			pmd = pmd_offset(pud, addr);
34939e5fc74cSSteve Capper 		}
34949e5fc74cSSteve Capper 	}
34959e5fc74cSSteve Capper 	return (pte_t *) pmd;
34969e5fc74cSSteve Capper }
34979e5fc74cSSteve Capper 
34989e5fc74cSSteve Capper struct page *
34999e5fc74cSSteve Capper follow_huge_pmd(struct mm_struct *mm, unsigned long address,
35009e5fc74cSSteve Capper 		pmd_t *pmd, int write)
35019e5fc74cSSteve Capper {
35029e5fc74cSSteve Capper 	struct page *page;
35039e5fc74cSSteve Capper 
35049e5fc74cSSteve Capper 	page = pte_page(*(pte_t *)pmd);
35059e5fc74cSSteve Capper 	if (page)
35069e5fc74cSSteve Capper 		page += ((address & ~PMD_MASK) >> PAGE_SHIFT);
35079e5fc74cSSteve Capper 	return page;
35089e5fc74cSSteve Capper }
35099e5fc74cSSteve Capper 
35109e5fc74cSSteve Capper struct page *
35119e5fc74cSSteve Capper follow_huge_pud(struct mm_struct *mm, unsigned long address,
35129e5fc74cSSteve Capper 		pud_t *pud, int write)
35139e5fc74cSSteve Capper {
35149e5fc74cSSteve Capper 	struct page *page;
35159e5fc74cSSteve Capper 
35169e5fc74cSSteve Capper 	page = pte_page(*(pte_t *)pud);
35179e5fc74cSSteve Capper 	if (page)
35189e5fc74cSSteve Capper 		page += ((address & ~PUD_MASK) >> PAGE_SHIFT);
35199e5fc74cSSteve Capper 	return page;
35209e5fc74cSSteve Capper }
35219e5fc74cSSteve Capper 
35229e5fc74cSSteve Capper #else /* !CONFIG_ARCH_WANT_GENERAL_HUGETLB */
35239e5fc74cSSteve Capper 
35249e5fc74cSSteve Capper /* Can be overriden by architectures */
35253b32123dSGideon Israel Dsouza struct page * __weak
35269e5fc74cSSteve Capper follow_huge_pud(struct mm_struct *mm, unsigned long address,
35279e5fc74cSSteve Capper 	       pud_t *pud, int write)
35289e5fc74cSSteve Capper {
35299e5fc74cSSteve Capper 	BUG();
35309e5fc74cSSteve Capper 	return NULL;
35319e5fc74cSSteve Capper }
35329e5fc74cSSteve Capper 
35339e5fc74cSSteve Capper #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
35349e5fc74cSSteve Capper 
3535d5bd9106SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
3536d5bd9106SAndi Kleen 
35376de2b1aaSNaoya Horiguchi /* Should be called in hugetlb_lock */
35386de2b1aaSNaoya Horiguchi static int is_hugepage_on_freelist(struct page *hpage)
35396de2b1aaSNaoya Horiguchi {
35406de2b1aaSNaoya Horiguchi 	struct page *page;
35416de2b1aaSNaoya Horiguchi 	struct page *tmp;
35426de2b1aaSNaoya Horiguchi 	struct hstate *h = page_hstate(hpage);
35436de2b1aaSNaoya Horiguchi 	int nid = page_to_nid(hpage);
35446de2b1aaSNaoya Horiguchi 
35456de2b1aaSNaoya Horiguchi 	list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru)
35466de2b1aaSNaoya Horiguchi 		if (page == hpage)
35476de2b1aaSNaoya Horiguchi 			return 1;
35486de2b1aaSNaoya Horiguchi 	return 0;
35496de2b1aaSNaoya Horiguchi }
35506de2b1aaSNaoya Horiguchi 
355193f70f90SNaoya Horiguchi /*
355293f70f90SNaoya Horiguchi  * This function is called from memory failure code.
355393f70f90SNaoya Horiguchi  * Assume the caller holds page lock of the head page.
355493f70f90SNaoya Horiguchi  */
35556de2b1aaSNaoya Horiguchi int dequeue_hwpoisoned_huge_page(struct page *hpage)
355693f70f90SNaoya Horiguchi {
355793f70f90SNaoya Horiguchi 	struct hstate *h = page_hstate(hpage);
355893f70f90SNaoya Horiguchi 	int nid = page_to_nid(hpage);
35596de2b1aaSNaoya Horiguchi 	int ret = -EBUSY;
356093f70f90SNaoya Horiguchi 
356193f70f90SNaoya Horiguchi 	spin_lock(&hugetlb_lock);
35626de2b1aaSNaoya Horiguchi 	if (is_hugepage_on_freelist(hpage)) {
356356f2fb14SNaoya Horiguchi 		/*
356456f2fb14SNaoya Horiguchi 		 * Hwpoisoned hugepage isn't linked to activelist or freelist,
356556f2fb14SNaoya Horiguchi 		 * but dangling hpage->lru can trigger list-debug warnings
356656f2fb14SNaoya Horiguchi 		 * (this happens when we call unpoison_memory() on it),
356756f2fb14SNaoya Horiguchi 		 * so let it point to itself with list_del_init().
356856f2fb14SNaoya Horiguchi 		 */
356956f2fb14SNaoya Horiguchi 		list_del_init(&hpage->lru);
35708c6c2ecbSNaoya Horiguchi 		set_page_refcounted(hpage);
357193f70f90SNaoya Horiguchi 		h->free_huge_pages--;
357293f70f90SNaoya Horiguchi 		h->free_huge_pages_node[nid]--;
35736de2b1aaSNaoya Horiguchi 		ret = 0;
357493f70f90SNaoya Horiguchi 	}
35756de2b1aaSNaoya Horiguchi 	spin_unlock(&hugetlb_lock);
35766de2b1aaSNaoya Horiguchi 	return ret;
35776de2b1aaSNaoya Horiguchi }
35786de2b1aaSNaoya Horiguchi #endif
357931caf665SNaoya Horiguchi 
358031caf665SNaoya Horiguchi bool isolate_huge_page(struct page *page, struct list_head *list)
358131caf665SNaoya Horiguchi {
3582309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageHead(page), page);
358331caf665SNaoya Horiguchi 	if (!get_page_unless_zero(page))
358431caf665SNaoya Horiguchi 		return false;
358531caf665SNaoya Horiguchi 	spin_lock(&hugetlb_lock);
358631caf665SNaoya Horiguchi 	list_move_tail(&page->lru, list);
358731caf665SNaoya Horiguchi 	spin_unlock(&hugetlb_lock);
358831caf665SNaoya Horiguchi 	return true;
358931caf665SNaoya Horiguchi }
359031caf665SNaoya Horiguchi 
359131caf665SNaoya Horiguchi void putback_active_hugepage(struct page *page)
359231caf665SNaoya Horiguchi {
3593309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageHead(page), page);
359431caf665SNaoya Horiguchi 	spin_lock(&hugetlb_lock);
359531caf665SNaoya Horiguchi 	list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
359631caf665SNaoya Horiguchi 	spin_unlock(&hugetlb_lock);
359731caf665SNaoya Horiguchi 	put_page(page);
359831caf665SNaoya Horiguchi }
3599c8721bbbSNaoya Horiguchi 
3600c8721bbbSNaoya Horiguchi bool is_hugepage_active(struct page *page)
3601c8721bbbSNaoya Horiguchi {
3602309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageHuge(page), page);
3603c8721bbbSNaoya Horiguchi 	/*
3604c8721bbbSNaoya Horiguchi 	 * This function can be called for a tail page because the caller,
3605c8721bbbSNaoya Horiguchi 	 * scan_movable_pages, scans through a given pfn-range which typically
3606c8721bbbSNaoya Horiguchi 	 * covers one memory block. In systems using gigantic hugepage (1GB
3607c8721bbbSNaoya Horiguchi 	 * for x86_64,) a hugepage is larger than a memory block, and we don't
3608c8721bbbSNaoya Horiguchi 	 * support migrating such large hugepages for now, so return false
3609c8721bbbSNaoya Horiguchi 	 * when called for tail pages.
3610c8721bbbSNaoya Horiguchi 	 */
3611c8721bbbSNaoya Horiguchi 	if (PageTail(page))
3612c8721bbbSNaoya Horiguchi 		return false;
3613c8721bbbSNaoya Horiguchi 	/*
3614c8721bbbSNaoya Horiguchi 	 * Refcount of a hwpoisoned hugepages is 1, but they are not active,
3615c8721bbbSNaoya Horiguchi 	 * so we should return false for them.
3616c8721bbbSNaoya Horiguchi 	 */
3617c8721bbbSNaoya Horiguchi 	if (unlikely(PageHWPoison(page)))
3618c8721bbbSNaoya Horiguchi 		return false;
3619c8721bbbSNaoya Horiguchi 	return page_count(page) > 0;
3620c8721bbbSNaoya Horiguchi }
3621