xref: /openbmc/linux/mm/hugetlb.c (revision 6d49e352)
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>
16aea47ff3SChristoph Lameter #include <linux/cpuset.h>
173935baa9SDavid Gibson #include <linux/mutex.h>
18aa888a74SAndi Kleen #include <linux/bootmem.h>
19a3437870SNishanth Aravamudan #include <linux/sysfs.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
210fe6e20bSNaoya Horiguchi #include <linux/rmap.h>
22fd6a03edSNaoya Horiguchi #include <linux/swap.h>
23fd6a03edSNaoya Horiguchi #include <linux/swapops.h>
24d6606683SLinus Torvalds 
2563551ae0SDavid Gibson #include <asm/page.h>
2663551ae0SDavid Gibson #include <asm/pgtable.h>
2724669e58SAneesh Kumar K.V #include <asm/tlb.h>
2863551ae0SDavid Gibson 
2924669e58SAneesh Kumar K.V #include <linux/io.h>
3063551ae0SDavid Gibson #include <linux/hugetlb.h>
319dd540e2SAneesh Kumar K.V #include <linux/hugetlb_cgroup.h>
329a305230SLee Schermerhorn #include <linux/node.h>
337835e98bSNick Piggin #include "internal.h"
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
36396faf03SMel Gorman static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
37396faf03SMel Gorman unsigned long hugepages_treat_as_movable;
38a5516438SAndi Kleen 
39c3f38a38SAneesh Kumar K.V int hugetlb_max_hstate __read_mostly;
40e5ff2159SAndi Kleen unsigned int default_hstate_idx;
41e5ff2159SAndi Kleen struct hstate hstates[HUGE_MAX_HSTATE];
42e5ff2159SAndi Kleen 
4353ba51d2SJon Tollefson __initdata LIST_HEAD(huge_boot_pages);
4453ba51d2SJon Tollefson 
45e5ff2159SAndi Kleen /* for command line parsing */
46e5ff2159SAndi Kleen static struct hstate * __initdata parsed_hstate;
47e5ff2159SAndi Kleen static unsigned long __initdata default_hstate_max_huge_pages;
48e11bfbfcSNick Piggin static unsigned long __initdata default_hstate_size;
49e5ff2159SAndi Kleen 
503935baa9SDavid Gibson /*
513935baa9SDavid Gibson  * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
523935baa9SDavid Gibson  */
53c3f38a38SAneesh Kumar K.V DEFINE_SPINLOCK(hugetlb_lock);
540bd0f9fbSEric Paris 
5590481622SDavid Gibson static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
5690481622SDavid Gibson {
5790481622SDavid Gibson 	bool free = (spool->count == 0) && (spool->used_hpages == 0);
5890481622SDavid Gibson 
5990481622SDavid Gibson 	spin_unlock(&spool->lock);
6090481622SDavid Gibson 
6190481622SDavid Gibson 	/* If no pages are used, and no other handles to the subpool
6290481622SDavid Gibson 	 * remain, free the subpool the subpool remain */
6390481622SDavid Gibson 	if (free)
6490481622SDavid Gibson 		kfree(spool);
6590481622SDavid Gibson }
6690481622SDavid Gibson 
6790481622SDavid Gibson struct hugepage_subpool *hugepage_new_subpool(long nr_blocks)
6890481622SDavid Gibson {
6990481622SDavid Gibson 	struct hugepage_subpool *spool;
7090481622SDavid Gibson 
7190481622SDavid Gibson 	spool = kmalloc(sizeof(*spool), GFP_KERNEL);
7290481622SDavid Gibson 	if (!spool)
7390481622SDavid Gibson 		return NULL;
7490481622SDavid Gibson 
7590481622SDavid Gibson 	spin_lock_init(&spool->lock);
7690481622SDavid Gibson 	spool->count = 1;
7790481622SDavid Gibson 	spool->max_hpages = nr_blocks;
7890481622SDavid Gibson 	spool->used_hpages = 0;
7990481622SDavid Gibson 
8090481622SDavid Gibson 	return spool;
8190481622SDavid Gibson }
8290481622SDavid Gibson 
8390481622SDavid Gibson void hugepage_put_subpool(struct hugepage_subpool *spool)
8490481622SDavid Gibson {
8590481622SDavid Gibson 	spin_lock(&spool->lock);
8690481622SDavid Gibson 	BUG_ON(!spool->count);
8790481622SDavid Gibson 	spool->count--;
8890481622SDavid Gibson 	unlock_or_release_subpool(spool);
8990481622SDavid Gibson }
9090481622SDavid Gibson 
9190481622SDavid Gibson static int hugepage_subpool_get_pages(struct hugepage_subpool *spool,
9290481622SDavid Gibson 				      long delta)
9390481622SDavid Gibson {
9490481622SDavid Gibson 	int ret = 0;
9590481622SDavid Gibson 
9690481622SDavid Gibson 	if (!spool)
9790481622SDavid Gibson 		return 0;
9890481622SDavid Gibson 
9990481622SDavid Gibson 	spin_lock(&spool->lock);
10090481622SDavid Gibson 	if ((spool->used_hpages + delta) <= spool->max_hpages) {
10190481622SDavid Gibson 		spool->used_hpages += delta;
10290481622SDavid Gibson 	} else {
10390481622SDavid Gibson 		ret = -ENOMEM;
10490481622SDavid Gibson 	}
10590481622SDavid Gibson 	spin_unlock(&spool->lock);
10690481622SDavid Gibson 
10790481622SDavid Gibson 	return ret;
10890481622SDavid Gibson }
10990481622SDavid Gibson 
11090481622SDavid Gibson static void hugepage_subpool_put_pages(struct hugepage_subpool *spool,
11190481622SDavid Gibson 				       long delta)
11290481622SDavid Gibson {
11390481622SDavid Gibson 	if (!spool)
11490481622SDavid Gibson 		return;
11590481622SDavid Gibson 
11690481622SDavid Gibson 	spin_lock(&spool->lock);
11790481622SDavid Gibson 	spool->used_hpages -= delta;
11890481622SDavid Gibson 	/* If hugetlbfs_put_super couldn't free spool due to
11990481622SDavid Gibson 	* an outstanding quota reference, free it now. */
12090481622SDavid Gibson 	unlock_or_release_subpool(spool);
12190481622SDavid Gibson }
12290481622SDavid Gibson 
12390481622SDavid Gibson static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
12490481622SDavid Gibson {
12590481622SDavid Gibson 	return HUGETLBFS_SB(inode->i_sb)->spool;
12690481622SDavid Gibson }
12790481622SDavid Gibson 
12890481622SDavid Gibson static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
12990481622SDavid Gibson {
13090481622SDavid Gibson 	return subpool_inode(vma->vm_file->f_dentry->d_inode);
13190481622SDavid Gibson }
13290481622SDavid Gibson 
133e7c4b0bfSAndy Whitcroft /*
13496822904SAndy Whitcroft  * Region tracking -- allows tracking of reservations and instantiated pages
13596822904SAndy Whitcroft  *                    across the pages in a mapping.
13684afd99bSAndy Whitcroft  *
13784afd99bSAndy Whitcroft  * The region data structures are protected by a combination of the mmap_sem
13884afd99bSAndy Whitcroft  * and the hugetlb_instantion_mutex.  To access or modify a region the caller
13984afd99bSAndy Whitcroft  * must either hold the mmap_sem for write, or the mmap_sem for read and
14084afd99bSAndy Whitcroft  * the hugetlb_instantiation mutex:
14184afd99bSAndy Whitcroft  *
14284afd99bSAndy Whitcroft  *	down_write(&mm->mmap_sem);
14384afd99bSAndy Whitcroft  * or
14484afd99bSAndy Whitcroft  *	down_read(&mm->mmap_sem);
14584afd99bSAndy Whitcroft  *	mutex_lock(&hugetlb_instantiation_mutex);
14696822904SAndy Whitcroft  */
14796822904SAndy Whitcroft struct file_region {
14896822904SAndy Whitcroft 	struct list_head link;
14996822904SAndy Whitcroft 	long from;
15096822904SAndy Whitcroft 	long to;
15196822904SAndy Whitcroft };
15296822904SAndy Whitcroft 
15396822904SAndy Whitcroft static long region_add(struct list_head *head, long f, long t)
15496822904SAndy Whitcroft {
15596822904SAndy Whitcroft 	struct file_region *rg, *nrg, *trg;
15696822904SAndy Whitcroft 
15796822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
15896822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
15996822904SAndy Whitcroft 		if (f <= rg->to)
16096822904SAndy Whitcroft 			break;
16196822904SAndy Whitcroft 
16296822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
16396822904SAndy Whitcroft 	if (f > rg->from)
16496822904SAndy Whitcroft 		f = rg->from;
16596822904SAndy Whitcroft 
16696822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
16796822904SAndy Whitcroft 	nrg = rg;
16896822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
16996822904SAndy Whitcroft 		if (&rg->link == head)
17096822904SAndy Whitcroft 			break;
17196822904SAndy Whitcroft 		if (rg->from > t)
17296822904SAndy Whitcroft 			break;
17396822904SAndy Whitcroft 
17496822904SAndy Whitcroft 		/* If this area reaches higher then extend our area to
17596822904SAndy Whitcroft 		 * include it completely.  If this is not the first area
17696822904SAndy Whitcroft 		 * which we intend to reuse, free it. */
17796822904SAndy Whitcroft 		if (rg->to > t)
17896822904SAndy Whitcroft 			t = rg->to;
17996822904SAndy Whitcroft 		if (rg != nrg) {
18096822904SAndy Whitcroft 			list_del(&rg->link);
18196822904SAndy Whitcroft 			kfree(rg);
18296822904SAndy Whitcroft 		}
18396822904SAndy Whitcroft 	}
18496822904SAndy Whitcroft 	nrg->from = f;
18596822904SAndy Whitcroft 	nrg->to = t;
18696822904SAndy Whitcroft 	return 0;
18796822904SAndy Whitcroft }
18896822904SAndy Whitcroft 
18996822904SAndy Whitcroft static long region_chg(struct list_head *head, long f, long t)
19096822904SAndy Whitcroft {
19196822904SAndy Whitcroft 	struct file_region *rg, *nrg;
19296822904SAndy Whitcroft 	long chg = 0;
19396822904SAndy Whitcroft 
19496822904SAndy Whitcroft 	/* Locate the region we are before or in. */
19596822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
19696822904SAndy Whitcroft 		if (f <= rg->to)
19796822904SAndy Whitcroft 			break;
19896822904SAndy Whitcroft 
19996822904SAndy Whitcroft 	/* If we are below the current region then a new region is required.
20096822904SAndy Whitcroft 	 * Subtle, allocate a new region at the position but make it zero
20196822904SAndy Whitcroft 	 * size such that we can guarantee to record the reservation. */
20296822904SAndy Whitcroft 	if (&rg->link == head || t < rg->from) {
20396822904SAndy Whitcroft 		nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
20496822904SAndy Whitcroft 		if (!nrg)
20596822904SAndy Whitcroft 			return -ENOMEM;
20696822904SAndy Whitcroft 		nrg->from = f;
20796822904SAndy Whitcroft 		nrg->to   = f;
20896822904SAndy Whitcroft 		INIT_LIST_HEAD(&nrg->link);
20996822904SAndy Whitcroft 		list_add(&nrg->link, rg->link.prev);
21096822904SAndy Whitcroft 
21196822904SAndy Whitcroft 		return t - f;
21296822904SAndy Whitcroft 	}
21396822904SAndy Whitcroft 
21496822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
21596822904SAndy Whitcroft 	if (f > rg->from)
21696822904SAndy Whitcroft 		f = rg->from;
21796822904SAndy Whitcroft 	chg = t - f;
21896822904SAndy Whitcroft 
21996822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
22096822904SAndy Whitcroft 	list_for_each_entry(rg, rg->link.prev, link) {
22196822904SAndy Whitcroft 		if (&rg->link == head)
22296822904SAndy Whitcroft 			break;
22396822904SAndy Whitcroft 		if (rg->from > t)
22496822904SAndy Whitcroft 			return chg;
22596822904SAndy Whitcroft 
22625985edcSLucas De Marchi 		/* We overlap with this area, if it extends further than
22796822904SAndy Whitcroft 		 * us then we must extend ourselves.  Account for its
22896822904SAndy Whitcroft 		 * existing reservation. */
22996822904SAndy Whitcroft 		if (rg->to > t) {
23096822904SAndy Whitcroft 			chg += rg->to - t;
23196822904SAndy Whitcroft 			t = rg->to;
23296822904SAndy Whitcroft 		}
23396822904SAndy Whitcroft 		chg -= rg->to - rg->from;
23496822904SAndy Whitcroft 	}
23596822904SAndy Whitcroft 	return chg;
23696822904SAndy Whitcroft }
23796822904SAndy Whitcroft 
23896822904SAndy Whitcroft static long region_truncate(struct list_head *head, long end)
23996822904SAndy Whitcroft {
24096822904SAndy Whitcroft 	struct file_region *rg, *trg;
24196822904SAndy Whitcroft 	long chg = 0;
24296822904SAndy Whitcroft 
24396822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
24496822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
24596822904SAndy Whitcroft 		if (end <= rg->to)
24696822904SAndy Whitcroft 			break;
24796822904SAndy Whitcroft 	if (&rg->link == head)
24896822904SAndy Whitcroft 		return 0;
24996822904SAndy Whitcroft 
25096822904SAndy Whitcroft 	/* If we are in the middle of a region then adjust it. */
25196822904SAndy Whitcroft 	if (end > rg->from) {
25296822904SAndy Whitcroft 		chg = rg->to - end;
25396822904SAndy Whitcroft 		rg->to = end;
25496822904SAndy Whitcroft 		rg = list_entry(rg->link.next, typeof(*rg), link);
25596822904SAndy Whitcroft 	}
25696822904SAndy Whitcroft 
25796822904SAndy Whitcroft 	/* Drop any remaining regions. */
25896822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
25996822904SAndy Whitcroft 		if (&rg->link == head)
26096822904SAndy Whitcroft 			break;
26196822904SAndy Whitcroft 		chg += rg->to - rg->from;
26296822904SAndy Whitcroft 		list_del(&rg->link);
26396822904SAndy Whitcroft 		kfree(rg);
26496822904SAndy Whitcroft 	}
26596822904SAndy Whitcroft 	return chg;
26696822904SAndy Whitcroft }
26796822904SAndy Whitcroft 
26884afd99bSAndy Whitcroft static long region_count(struct list_head *head, long f, long t)
26984afd99bSAndy Whitcroft {
27084afd99bSAndy Whitcroft 	struct file_region *rg;
27184afd99bSAndy Whitcroft 	long chg = 0;
27284afd99bSAndy Whitcroft 
27384afd99bSAndy Whitcroft 	/* Locate each segment we overlap with, and count that overlap. */
27484afd99bSAndy Whitcroft 	list_for_each_entry(rg, head, link) {
275f2135a4aSWang Sheng-Hui 		long seg_from;
276f2135a4aSWang Sheng-Hui 		long seg_to;
27784afd99bSAndy Whitcroft 
27884afd99bSAndy Whitcroft 		if (rg->to <= f)
27984afd99bSAndy Whitcroft 			continue;
28084afd99bSAndy Whitcroft 		if (rg->from >= t)
28184afd99bSAndy Whitcroft 			break;
28284afd99bSAndy Whitcroft 
28384afd99bSAndy Whitcroft 		seg_from = max(rg->from, f);
28484afd99bSAndy Whitcroft 		seg_to = min(rg->to, t);
28584afd99bSAndy Whitcroft 
28684afd99bSAndy Whitcroft 		chg += seg_to - seg_from;
28784afd99bSAndy Whitcroft 	}
28884afd99bSAndy Whitcroft 
28984afd99bSAndy Whitcroft 	return chg;
29084afd99bSAndy Whitcroft }
29184afd99bSAndy Whitcroft 
29296822904SAndy Whitcroft /*
293e7c4b0bfSAndy Whitcroft  * Convert the address within this vma to the page offset within
294e7c4b0bfSAndy Whitcroft  * the mapping, in pagecache page units; huge pages here.
295e7c4b0bfSAndy Whitcroft  */
296a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h,
297a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long address)
298e7c4b0bfSAndy Whitcroft {
299a5516438SAndi Kleen 	return ((address - vma->vm_start) >> huge_page_shift(h)) +
300a5516438SAndi Kleen 			(vma->vm_pgoff >> huge_page_order(h));
301e7c4b0bfSAndy Whitcroft }
302e7c4b0bfSAndy Whitcroft 
3030fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
3040fe6e20bSNaoya Horiguchi 				     unsigned long address)
3050fe6e20bSNaoya Horiguchi {
3060fe6e20bSNaoya Horiguchi 	return vma_hugecache_offset(hstate_vma(vma), vma, address);
3070fe6e20bSNaoya Horiguchi }
3080fe6e20bSNaoya Horiguchi 
30984afd99bSAndy Whitcroft /*
31008fba699SMel Gorman  * Return the size of the pages allocated when backing a VMA. In the majority
31108fba699SMel Gorman  * cases this will be same size as used by the page table entries.
31208fba699SMel Gorman  */
31308fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
31408fba699SMel Gorman {
31508fba699SMel Gorman 	struct hstate *hstate;
31608fba699SMel Gorman 
31708fba699SMel Gorman 	if (!is_vm_hugetlb_page(vma))
31808fba699SMel Gorman 		return PAGE_SIZE;
31908fba699SMel Gorman 
32008fba699SMel Gorman 	hstate = hstate_vma(vma);
32108fba699SMel Gorman 
32208fba699SMel Gorman 	return 1UL << (hstate->order + PAGE_SHIFT);
32308fba699SMel Gorman }
324f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
32508fba699SMel Gorman 
32608fba699SMel Gorman /*
3273340289dSMel Gorman  * Return the page size being used by the MMU to back a VMA. In the majority
3283340289dSMel Gorman  * of cases, the page size used by the kernel matches the MMU size. On
3293340289dSMel Gorman  * architectures where it differs, an architecture-specific version of this
3303340289dSMel Gorman  * function is required.
3313340289dSMel Gorman  */
3323340289dSMel Gorman #ifndef vma_mmu_pagesize
3333340289dSMel Gorman unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
3343340289dSMel Gorman {
3353340289dSMel Gorman 	return vma_kernel_pagesize(vma);
3363340289dSMel Gorman }
3373340289dSMel Gorman #endif
3383340289dSMel Gorman 
3393340289dSMel Gorman /*
34084afd99bSAndy Whitcroft  * Flags for MAP_PRIVATE reservations.  These are stored in the bottom
34184afd99bSAndy Whitcroft  * bits of the reservation map pointer, which are always clear due to
34284afd99bSAndy Whitcroft  * alignment.
34384afd99bSAndy Whitcroft  */
34484afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER    (1UL << 0)
34584afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1)
34604f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
34784afd99bSAndy Whitcroft 
348a1e78772SMel Gorman /*
349a1e78772SMel Gorman  * These helpers are used to track how many pages are reserved for
350a1e78772SMel Gorman  * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
351a1e78772SMel Gorman  * is guaranteed to have their future faults succeed.
352a1e78772SMel Gorman  *
353a1e78772SMel Gorman  * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
354a1e78772SMel Gorman  * the reserve counters are updated with the hugetlb_lock held. It is safe
355a1e78772SMel Gorman  * to reset the VMA at fork() time as it is not in use yet and there is no
356a1e78772SMel Gorman  * chance of the global counters getting corrupted as a result of the values.
35784afd99bSAndy Whitcroft  *
35884afd99bSAndy Whitcroft  * The private mapping reservation is represented in a subtly different
35984afd99bSAndy Whitcroft  * manner to a shared mapping.  A shared mapping has a region map associated
36084afd99bSAndy Whitcroft  * with the underlying file, this region map represents the backing file
36184afd99bSAndy Whitcroft  * pages which have ever had a reservation assigned which this persists even
36284afd99bSAndy Whitcroft  * after the page is instantiated.  A private mapping has a region map
36384afd99bSAndy Whitcroft  * associated with the original mmap which is attached to all VMAs which
36484afd99bSAndy Whitcroft  * reference it, this region map represents those offsets which have consumed
36584afd99bSAndy Whitcroft  * reservation ie. where pages have been instantiated.
366a1e78772SMel Gorman  */
367e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma)
368e7c4b0bfSAndy Whitcroft {
369e7c4b0bfSAndy Whitcroft 	return (unsigned long)vma->vm_private_data;
370e7c4b0bfSAndy Whitcroft }
371e7c4b0bfSAndy Whitcroft 
372e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma,
373e7c4b0bfSAndy Whitcroft 							unsigned long value)
374e7c4b0bfSAndy Whitcroft {
375e7c4b0bfSAndy Whitcroft 	vma->vm_private_data = (void *)value;
376e7c4b0bfSAndy Whitcroft }
377e7c4b0bfSAndy Whitcroft 
37884afd99bSAndy Whitcroft struct resv_map {
37984afd99bSAndy Whitcroft 	struct kref refs;
38084afd99bSAndy Whitcroft 	struct list_head regions;
38184afd99bSAndy Whitcroft };
38284afd99bSAndy Whitcroft 
3832a4b3dedSHarvey Harrison static struct resv_map *resv_map_alloc(void)
38484afd99bSAndy Whitcroft {
38584afd99bSAndy Whitcroft 	struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
38684afd99bSAndy Whitcroft 	if (!resv_map)
38784afd99bSAndy Whitcroft 		return NULL;
38884afd99bSAndy Whitcroft 
38984afd99bSAndy Whitcroft 	kref_init(&resv_map->refs);
39084afd99bSAndy Whitcroft 	INIT_LIST_HEAD(&resv_map->regions);
39184afd99bSAndy Whitcroft 
39284afd99bSAndy Whitcroft 	return resv_map;
39384afd99bSAndy Whitcroft }
39484afd99bSAndy Whitcroft 
3952a4b3dedSHarvey Harrison static void resv_map_release(struct kref *ref)
39684afd99bSAndy Whitcroft {
39784afd99bSAndy Whitcroft 	struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
39884afd99bSAndy Whitcroft 
39984afd99bSAndy Whitcroft 	/* Clear out any active regions before we release the map. */
40084afd99bSAndy Whitcroft 	region_truncate(&resv_map->regions, 0);
40184afd99bSAndy Whitcroft 	kfree(resv_map);
40284afd99bSAndy Whitcroft }
40384afd99bSAndy Whitcroft 
40484afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
405a1e78772SMel Gorman {
406a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
407f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE))
40884afd99bSAndy Whitcroft 		return (struct resv_map *)(get_vma_private_data(vma) &
40984afd99bSAndy Whitcroft 							~HPAGE_RESV_MASK);
4102a4b3dedSHarvey Harrison 	return NULL;
411a1e78772SMel Gorman }
412a1e78772SMel Gorman 
41384afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
414a1e78772SMel Gorman {
415a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
416f83a275dSMel Gorman 	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
417a1e78772SMel Gorman 
41884afd99bSAndy Whitcroft 	set_vma_private_data(vma, (get_vma_private_data(vma) &
41984afd99bSAndy Whitcroft 				HPAGE_RESV_MASK) | (unsigned long)map);
42004f2cbe3SMel Gorman }
42104f2cbe3SMel Gorman 
42204f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
42304f2cbe3SMel Gorman {
42404f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
425f83a275dSMel Gorman 	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
426e7c4b0bfSAndy Whitcroft 
427e7c4b0bfSAndy Whitcroft 	set_vma_private_data(vma, get_vma_private_data(vma) | flags);
42804f2cbe3SMel Gorman }
42904f2cbe3SMel Gorman 
43004f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
43104f2cbe3SMel Gorman {
43204f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
433e7c4b0bfSAndy Whitcroft 
434e7c4b0bfSAndy Whitcroft 	return (get_vma_private_data(vma) & flag) != 0;
435a1e78772SMel Gorman }
436a1e78772SMel Gorman 
437a1e78772SMel Gorman /* Decrement the reserved pages in the hugepage pool by one */
438a5516438SAndi Kleen static void decrement_hugepage_resv_vma(struct hstate *h,
439a5516438SAndi Kleen 			struct vm_area_struct *vma)
440a1e78772SMel Gorman {
441c37f9fb1SAndy Whitcroft 	if (vma->vm_flags & VM_NORESERVE)
442c37f9fb1SAndy Whitcroft 		return;
443c37f9fb1SAndy Whitcroft 
444f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE) {
445a1e78772SMel Gorman 		/* Shared mappings always use reserves */
446a5516438SAndi Kleen 		h->resv_huge_pages--;
44784afd99bSAndy Whitcroft 	} else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
448a1e78772SMel Gorman 		/*
449a1e78772SMel Gorman 		 * Only the process that called mmap() has reserves for
450a1e78772SMel Gorman 		 * private mappings.
451a1e78772SMel Gorman 		 */
452a5516438SAndi Kleen 		h->resv_huge_pages--;
453a1e78772SMel Gorman 	}
454a1e78772SMel Gorman }
455a1e78772SMel Gorman 
45604f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
457a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
458a1e78772SMel Gorman {
459a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
460f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE))
461a1e78772SMel Gorman 		vma->vm_private_data = (void *)0;
462a1e78772SMel Gorman }
463a1e78772SMel Gorman 
464a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */
4657f09ca51SMel Gorman static int vma_has_reserves(struct vm_area_struct *vma)
466a1e78772SMel Gorman {
467f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE)
468a1e78772SMel Gorman 		return 1;
4697f09ca51SMel Gorman 	if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
4707f09ca51SMel Gorman 		return 1;
4717f09ca51SMel Gorman 	return 0;
472a1e78772SMel Gorman }
473a1e78772SMel Gorman 
4740ebabb41SNaoya Horiguchi static void copy_gigantic_page(struct page *dst, struct page *src)
4750ebabb41SNaoya Horiguchi {
4760ebabb41SNaoya Horiguchi 	int i;
4770ebabb41SNaoya Horiguchi 	struct hstate *h = page_hstate(src);
4780ebabb41SNaoya Horiguchi 	struct page *dst_base = dst;
4790ebabb41SNaoya Horiguchi 	struct page *src_base = src;
4800ebabb41SNaoya Horiguchi 
4810ebabb41SNaoya Horiguchi 	for (i = 0; i < pages_per_huge_page(h); ) {
4820ebabb41SNaoya Horiguchi 		cond_resched();
4830ebabb41SNaoya Horiguchi 		copy_highpage(dst, src);
4840ebabb41SNaoya Horiguchi 
4850ebabb41SNaoya Horiguchi 		i++;
4860ebabb41SNaoya Horiguchi 		dst = mem_map_next(dst, dst_base, i);
4870ebabb41SNaoya Horiguchi 		src = mem_map_next(src, src_base, i);
4880ebabb41SNaoya Horiguchi 	}
4890ebabb41SNaoya Horiguchi }
4900ebabb41SNaoya Horiguchi 
4910ebabb41SNaoya Horiguchi void copy_huge_page(struct page *dst, struct page *src)
4920ebabb41SNaoya Horiguchi {
4930ebabb41SNaoya Horiguchi 	int i;
4940ebabb41SNaoya Horiguchi 	struct hstate *h = page_hstate(src);
4950ebabb41SNaoya Horiguchi 
4960ebabb41SNaoya Horiguchi 	if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) {
4970ebabb41SNaoya Horiguchi 		copy_gigantic_page(dst, src);
4980ebabb41SNaoya Horiguchi 		return;
4990ebabb41SNaoya Horiguchi 	}
5000ebabb41SNaoya Horiguchi 
5010ebabb41SNaoya Horiguchi 	might_sleep();
5020ebabb41SNaoya Horiguchi 	for (i = 0; i < pages_per_huge_page(h); i++) {
5030ebabb41SNaoya Horiguchi 		cond_resched();
5040ebabb41SNaoya Horiguchi 		copy_highpage(dst + i, src + i);
5050ebabb41SNaoya Horiguchi 	}
5060ebabb41SNaoya Horiguchi }
5070ebabb41SNaoya Horiguchi 
508a5516438SAndi Kleen static void enqueue_huge_page(struct hstate *h, struct page *page)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds 	int nid = page_to_nid(page);
5110edaecfaSAneesh Kumar K.V 	list_move(&page->lru, &h->hugepage_freelists[nid]);
512a5516438SAndi Kleen 	h->free_huge_pages++;
513a5516438SAndi Kleen 	h->free_huge_pages_node[nid]++;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
516bf50bab2SNaoya Horiguchi static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
517bf50bab2SNaoya Horiguchi {
518bf50bab2SNaoya Horiguchi 	struct page *page;
519bf50bab2SNaoya Horiguchi 
520bf50bab2SNaoya Horiguchi 	if (list_empty(&h->hugepage_freelists[nid]))
521bf50bab2SNaoya Horiguchi 		return NULL;
522bf50bab2SNaoya Horiguchi 	page = list_entry(h->hugepage_freelists[nid].next, struct page, lru);
5230edaecfaSAneesh Kumar K.V 	list_move(&page->lru, &h->hugepage_activelist);
524a9869b83SNaoya Horiguchi 	set_page_refcounted(page);
525bf50bab2SNaoya Horiguchi 	h->free_huge_pages--;
526bf50bab2SNaoya Horiguchi 	h->free_huge_pages_node[nid]--;
527bf50bab2SNaoya Horiguchi 	return page;
528bf50bab2SNaoya Horiguchi }
529bf50bab2SNaoya Horiguchi 
530a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h,
531a5516438SAndi Kleen 				struct vm_area_struct *vma,
53204f2cbe3SMel Gorman 				unsigned long address, int avoid_reserve)
5331da177e4SLinus Torvalds {
534b1c12cbcSKonstantin Khlebnikov 	struct page *page = NULL;
535480eccf9SLee Schermerhorn 	struct mempolicy *mpol;
53619770b32SMel Gorman 	nodemask_t *nodemask;
537c0ff7453SMiao Xie 	struct zonelist *zonelist;
538dd1a239fSMel Gorman 	struct zone *zone;
539dd1a239fSMel Gorman 	struct zoneref *z;
540cc9a6c87SMel Gorman 	unsigned int cpuset_mems_cookie;
5411da177e4SLinus Torvalds 
542cc9a6c87SMel Gorman retry_cpuset:
543cc9a6c87SMel Gorman 	cpuset_mems_cookie = get_mems_allowed();
544c0ff7453SMiao Xie 	zonelist = huge_zonelist(vma, address,
545c0ff7453SMiao Xie 					htlb_alloc_mask, &mpol, &nodemask);
546a1e78772SMel Gorman 	/*
547a1e78772SMel Gorman 	 * A child process with MAP_PRIVATE mappings created by their parent
548a1e78772SMel Gorman 	 * have no page reserves. This check ensures that reservations are
549a1e78772SMel Gorman 	 * not "stolen". The child may still get SIGKILLed
550a1e78772SMel Gorman 	 */
5517f09ca51SMel Gorman 	if (!vma_has_reserves(vma) &&
552a5516438SAndi Kleen 			h->free_huge_pages - h->resv_huge_pages == 0)
553c0ff7453SMiao Xie 		goto err;
554a1e78772SMel Gorman 
55504f2cbe3SMel Gorman 	/* If reserves cannot be used, ensure enough pages are in the pool */
556a5516438SAndi Kleen 	if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
5576eab04a8SJustin P. Mattock 		goto err;
55804f2cbe3SMel Gorman 
55919770b32SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
56019770b32SMel Gorman 						MAX_NR_ZONES - 1, nodemask) {
561bf50bab2SNaoya Horiguchi 		if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask)) {
562bf50bab2SNaoya Horiguchi 			page = dequeue_huge_page_node(h, zone_to_nid(zone));
563bf50bab2SNaoya Horiguchi 			if (page) {
56404f2cbe3SMel Gorman 				if (!avoid_reserve)
565a5516438SAndi Kleen 					decrement_hugepage_resv_vma(h, vma);
5665ab3ee7bSKen Chen 				break;
5671da177e4SLinus Torvalds 			}
5683abf7afdSAndrew Morton 		}
569bf50bab2SNaoya Horiguchi 	}
570cc9a6c87SMel Gorman 
571cc9a6c87SMel Gorman 	mpol_cond_put(mpol);
572cc9a6c87SMel Gorman 	if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
573cc9a6c87SMel Gorman 		goto retry_cpuset;
574cc9a6c87SMel Gorman 	return page;
575cc9a6c87SMel Gorman 
576c0ff7453SMiao Xie err:
57752cd3b07SLee Schermerhorn 	mpol_cond_put(mpol);
578cc9a6c87SMel Gorman 	return NULL;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
581a5516438SAndi Kleen static void update_and_free_page(struct hstate *h, struct page *page)
5826af2acb6SAdam Litke {
5836af2acb6SAdam Litke 	int i;
584a5516438SAndi Kleen 
58518229df5SAndy Whitcroft 	VM_BUG_ON(h->order >= MAX_ORDER);
58618229df5SAndy Whitcroft 
587a5516438SAndi Kleen 	h->nr_huge_pages--;
588a5516438SAndi Kleen 	h->nr_huge_pages_node[page_to_nid(page)]--;
589a5516438SAndi Kleen 	for (i = 0; i < pages_per_huge_page(h); i++) {
59032f84528SChris Forbes 		page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
59132f84528SChris Forbes 				1 << PG_referenced | 1 << PG_dirty |
59232f84528SChris Forbes 				1 << PG_active | 1 << PG_reserved |
5936af2acb6SAdam Litke 				1 << PG_private | 1 << PG_writeback);
5946af2acb6SAdam Litke 	}
5959dd540e2SAneesh Kumar K.V 	VM_BUG_ON(hugetlb_cgroup_from_page(page));
5966af2acb6SAdam Litke 	set_compound_page_dtor(page, NULL);
5976af2acb6SAdam Litke 	set_page_refcounted(page);
5987f2e9525SGerald Schaefer 	arch_release_hugepage(page);
599a5516438SAndi Kleen 	__free_pages(page, huge_page_order(h));
6006af2acb6SAdam Litke }
6016af2acb6SAdam Litke 
602e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size)
603e5ff2159SAndi Kleen {
604e5ff2159SAndi Kleen 	struct hstate *h;
605e5ff2159SAndi Kleen 
606e5ff2159SAndi Kleen 	for_each_hstate(h) {
607e5ff2159SAndi Kleen 		if (huge_page_size(h) == size)
608e5ff2159SAndi Kleen 			return h;
609e5ff2159SAndi Kleen 	}
610e5ff2159SAndi Kleen 	return NULL;
611e5ff2159SAndi Kleen }
612e5ff2159SAndi Kleen 
61327a85ef1SDavid Gibson static void free_huge_page(struct page *page)
61427a85ef1SDavid Gibson {
615a5516438SAndi Kleen 	/*
616a5516438SAndi Kleen 	 * Can't pass hstate in here because it is called from the
617a5516438SAndi Kleen 	 * compound page destructor.
618a5516438SAndi Kleen 	 */
619e5ff2159SAndi Kleen 	struct hstate *h = page_hstate(page);
6207893d1d5SAdam Litke 	int nid = page_to_nid(page);
62190481622SDavid Gibson 	struct hugepage_subpool *spool =
62290481622SDavid Gibson 		(struct hugepage_subpool *)page_private(page);
62327a85ef1SDavid Gibson 
624e5df70abSAndy Whitcroft 	set_page_private(page, 0);
62523be7468SMel Gorman 	page->mapping = NULL;
6267893d1d5SAdam Litke 	BUG_ON(page_count(page));
6270fe6e20bSNaoya Horiguchi 	BUG_ON(page_mapcount(page));
62827a85ef1SDavid Gibson 
62927a85ef1SDavid Gibson 	spin_lock(&hugetlb_lock);
6306d76dcf4SAneesh Kumar K.V 	hugetlb_cgroup_uncharge_page(hstate_index(h),
6316d76dcf4SAneesh Kumar K.V 				     pages_per_huge_page(h), page);
632aa888a74SAndi Kleen 	if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
6330edaecfaSAneesh Kumar K.V 		/* remove the page from active list */
6340edaecfaSAneesh Kumar K.V 		list_del(&page->lru);
635a5516438SAndi Kleen 		update_and_free_page(h, page);
636a5516438SAndi Kleen 		h->surplus_huge_pages--;
637a5516438SAndi Kleen 		h->surplus_huge_pages_node[nid]--;
6387893d1d5SAdam Litke 	} else {
6395d3a551cSWill Deacon 		arch_clear_hugepage_flags(page);
640a5516438SAndi Kleen 		enqueue_huge_page(h, page);
6417893d1d5SAdam Litke 	}
64227a85ef1SDavid Gibson 	spin_unlock(&hugetlb_lock);
64390481622SDavid Gibson 	hugepage_subpool_put_pages(spool, 1);
64427a85ef1SDavid Gibson }
64527a85ef1SDavid Gibson 
646a5516438SAndi Kleen static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
647b7ba30c6SAndi Kleen {
6480edaecfaSAneesh Kumar K.V 	INIT_LIST_HEAD(&page->lru);
649b7ba30c6SAndi Kleen 	set_compound_page_dtor(page, free_huge_page);
650b7ba30c6SAndi Kleen 	spin_lock(&hugetlb_lock);
6519dd540e2SAneesh Kumar K.V 	set_hugetlb_cgroup(page, NULL);
652a5516438SAndi Kleen 	h->nr_huge_pages++;
653a5516438SAndi Kleen 	h->nr_huge_pages_node[nid]++;
654b7ba30c6SAndi Kleen 	spin_unlock(&hugetlb_lock);
655b7ba30c6SAndi Kleen 	put_page(page); /* free it into the hugepage allocator */
656b7ba30c6SAndi Kleen }
657b7ba30c6SAndi Kleen 
65820a0307cSWu Fengguang static void prep_compound_gigantic_page(struct page *page, unsigned long order)
65920a0307cSWu Fengguang {
66020a0307cSWu Fengguang 	int i;
66120a0307cSWu Fengguang 	int nr_pages = 1 << order;
66220a0307cSWu Fengguang 	struct page *p = page + 1;
66320a0307cSWu Fengguang 
66420a0307cSWu Fengguang 	/* we rely on prep_new_huge_page to set the destructor */
66520a0307cSWu Fengguang 	set_compound_order(page, order);
66620a0307cSWu Fengguang 	__SetPageHead(page);
66720a0307cSWu Fengguang 	for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
66820a0307cSWu Fengguang 		__SetPageTail(p);
66958a84aa9SYouquan Song 		set_page_count(p, 0);
67020a0307cSWu Fengguang 		p->first_page = page;
67120a0307cSWu Fengguang 	}
67220a0307cSWu Fengguang }
67320a0307cSWu Fengguang 
6747795912cSAndrew Morton /*
6757795912cSAndrew Morton  * PageHuge() only returns true for hugetlbfs pages, but not for normal or
6767795912cSAndrew Morton  * transparent huge pages.  See the PageTransHuge() documentation for more
6777795912cSAndrew Morton  * details.
6787795912cSAndrew Morton  */
67920a0307cSWu Fengguang int PageHuge(struct page *page)
68020a0307cSWu Fengguang {
68120a0307cSWu Fengguang 	compound_page_dtor *dtor;
68220a0307cSWu Fengguang 
68320a0307cSWu Fengguang 	if (!PageCompound(page))
68420a0307cSWu Fengguang 		return 0;
68520a0307cSWu Fengguang 
68620a0307cSWu Fengguang 	page = compound_head(page);
68720a0307cSWu Fengguang 	dtor = get_compound_page_dtor(page);
68820a0307cSWu Fengguang 
68920a0307cSWu Fengguang 	return dtor == free_huge_page;
69020a0307cSWu Fengguang }
69143131e14SNaoya Horiguchi EXPORT_SYMBOL_GPL(PageHuge);
69243131e14SNaoya Horiguchi 
693a5516438SAndi Kleen static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
6941da177e4SLinus Torvalds {
6951da177e4SLinus Torvalds 	struct page *page;
696f96efd58SJoe Jin 
697aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
698aa888a74SAndi Kleen 		return NULL;
699aa888a74SAndi Kleen 
7006484eb3eSMel Gorman 	page = alloc_pages_exact_node(nid,
701551883aeSNishanth Aravamudan 		htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
702551883aeSNishanth Aravamudan 						__GFP_REPEAT|__GFP_NOWARN,
703a5516438SAndi Kleen 		huge_page_order(h));
7041da177e4SLinus Torvalds 	if (page) {
7057f2e9525SGerald Schaefer 		if (arch_prepare_hugepage(page)) {
706caff3a2cSGerald Schaefer 			__free_pages(page, huge_page_order(h));
7077b8ee84dSHarvey Harrison 			return NULL;
7087f2e9525SGerald Schaefer 		}
709a5516438SAndi Kleen 		prep_new_huge_page(h, page, nid);
7101da177e4SLinus Torvalds 	}
71163b4613cSNishanth Aravamudan 
71263b4613cSNishanth Aravamudan 	return page;
71363b4613cSNishanth Aravamudan }
71463b4613cSNishanth Aravamudan 
7155ced66c9SAndi Kleen /*
7166ae11b27SLee Schermerhorn  * common helper functions for hstate_next_node_to_{alloc|free}.
7176ae11b27SLee Schermerhorn  * We may have allocated or freed a huge page based on a different
7186ae11b27SLee Schermerhorn  * nodes_allowed previously, so h->next_node_to_{alloc|free} might
7196ae11b27SLee Schermerhorn  * be outside of *nodes_allowed.  Ensure that we use an allowed
7206ae11b27SLee Schermerhorn  * node for alloc or free.
7219a76db09SLee Schermerhorn  */
7226ae11b27SLee Schermerhorn static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
7239a76db09SLee Schermerhorn {
7246ae11b27SLee Schermerhorn 	nid = next_node(nid, *nodes_allowed);
7259a76db09SLee Schermerhorn 	if (nid == MAX_NUMNODES)
7266ae11b27SLee Schermerhorn 		nid = first_node(*nodes_allowed);
7279a76db09SLee Schermerhorn 	VM_BUG_ON(nid >= MAX_NUMNODES);
7289a76db09SLee Schermerhorn 
7299a76db09SLee Schermerhorn 	return nid;
7309a76db09SLee Schermerhorn }
7319a76db09SLee Schermerhorn 
7326ae11b27SLee Schermerhorn static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
7335ced66c9SAndi Kleen {
7346ae11b27SLee Schermerhorn 	if (!node_isset(nid, *nodes_allowed))
7356ae11b27SLee Schermerhorn 		nid = next_node_allowed(nid, nodes_allowed);
7369a76db09SLee Schermerhorn 	return nid;
7375ced66c9SAndi Kleen }
7385ced66c9SAndi Kleen 
7396ae11b27SLee Schermerhorn /*
7406ae11b27SLee Schermerhorn  * returns the previously saved node ["this node"] from which to
7416ae11b27SLee Schermerhorn  * allocate a persistent huge page for the pool and advance the
7426ae11b27SLee Schermerhorn  * next node from which to allocate, handling wrap at end of node
7436ae11b27SLee Schermerhorn  * mask.
7446ae11b27SLee Schermerhorn  */
7456ae11b27SLee Schermerhorn static int hstate_next_node_to_alloc(struct hstate *h,
7466ae11b27SLee Schermerhorn 					nodemask_t *nodes_allowed)
7476ae11b27SLee Schermerhorn {
7486ae11b27SLee Schermerhorn 	int nid;
7496ae11b27SLee Schermerhorn 
7506ae11b27SLee Schermerhorn 	VM_BUG_ON(!nodes_allowed);
7516ae11b27SLee Schermerhorn 
7526ae11b27SLee Schermerhorn 	nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
7536ae11b27SLee Schermerhorn 	h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
7546ae11b27SLee Schermerhorn 
7556ae11b27SLee Schermerhorn 	return nid;
7566ae11b27SLee Schermerhorn }
7576ae11b27SLee Schermerhorn 
7586ae11b27SLee Schermerhorn static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
75963b4613cSNishanth Aravamudan {
76063b4613cSNishanth Aravamudan 	struct page *page;
76163b4613cSNishanth Aravamudan 	int start_nid;
76263b4613cSNishanth Aravamudan 	int next_nid;
76363b4613cSNishanth Aravamudan 	int ret = 0;
76463b4613cSNishanth Aravamudan 
7656ae11b27SLee Schermerhorn 	start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
766e8c5c824SLee Schermerhorn 	next_nid = start_nid;
76763b4613cSNishanth Aravamudan 
76863b4613cSNishanth Aravamudan 	do {
769e8c5c824SLee Schermerhorn 		page = alloc_fresh_huge_page_node(h, next_nid);
7709a76db09SLee Schermerhorn 		if (page) {
77163b4613cSNishanth Aravamudan 			ret = 1;
7729a76db09SLee Schermerhorn 			break;
7739a76db09SLee Schermerhorn 		}
7746ae11b27SLee Schermerhorn 		next_nid = hstate_next_node_to_alloc(h, nodes_allowed);
7759a76db09SLee Schermerhorn 	} while (next_nid != start_nid);
77663b4613cSNishanth Aravamudan 
7773b116300SAdam Litke 	if (ret)
7783b116300SAdam Litke 		count_vm_event(HTLB_BUDDY_PGALLOC);
7793b116300SAdam Litke 	else
7803b116300SAdam Litke 		count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
7813b116300SAdam Litke 
78263b4613cSNishanth Aravamudan 	return ret;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
785e8c5c824SLee Schermerhorn /*
7866ae11b27SLee Schermerhorn  * helper for free_pool_huge_page() - return the previously saved
7876ae11b27SLee Schermerhorn  * node ["this node"] from which to free a huge page.  Advance the
7886ae11b27SLee Schermerhorn  * next node id whether or not we find a free huge page to free so
7896ae11b27SLee Schermerhorn  * that the next attempt to free addresses the next node.
790e8c5c824SLee Schermerhorn  */
7916ae11b27SLee Schermerhorn static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
792e8c5c824SLee Schermerhorn {
7936ae11b27SLee Schermerhorn 	int nid;
7949a76db09SLee Schermerhorn 
7956ae11b27SLee Schermerhorn 	VM_BUG_ON(!nodes_allowed);
7966ae11b27SLee Schermerhorn 
7976ae11b27SLee Schermerhorn 	nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
7986ae11b27SLee Schermerhorn 	h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
7996ae11b27SLee Schermerhorn 
8009a76db09SLee Schermerhorn 	return nid;
801e8c5c824SLee Schermerhorn }
802e8c5c824SLee Schermerhorn 
803e8c5c824SLee Schermerhorn /*
804e8c5c824SLee Schermerhorn  * Free huge page from pool from next node to free.
805e8c5c824SLee Schermerhorn  * Attempt to keep persistent huge pages more or less
806e8c5c824SLee Schermerhorn  * balanced over allowed nodes.
807e8c5c824SLee Schermerhorn  * Called with hugetlb_lock locked.
808e8c5c824SLee Schermerhorn  */
8096ae11b27SLee Schermerhorn static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
8106ae11b27SLee Schermerhorn 							 bool acct_surplus)
811e8c5c824SLee Schermerhorn {
812e8c5c824SLee Schermerhorn 	int start_nid;
813e8c5c824SLee Schermerhorn 	int next_nid;
814e8c5c824SLee Schermerhorn 	int ret = 0;
815e8c5c824SLee Schermerhorn 
8166ae11b27SLee Schermerhorn 	start_nid = hstate_next_node_to_free(h, nodes_allowed);
817e8c5c824SLee Schermerhorn 	next_nid = start_nid;
818e8c5c824SLee Schermerhorn 
819e8c5c824SLee Schermerhorn 	do {
820685f3457SLee Schermerhorn 		/*
821685f3457SLee Schermerhorn 		 * If we're returning unused surplus pages, only examine
822685f3457SLee Schermerhorn 		 * nodes with surplus pages.
823685f3457SLee Schermerhorn 		 */
824685f3457SLee Schermerhorn 		if ((!acct_surplus || h->surplus_huge_pages_node[next_nid]) &&
825685f3457SLee Schermerhorn 		    !list_empty(&h->hugepage_freelists[next_nid])) {
826e8c5c824SLee Schermerhorn 			struct page *page =
827e8c5c824SLee Schermerhorn 				list_entry(h->hugepage_freelists[next_nid].next,
828e8c5c824SLee Schermerhorn 					  struct page, lru);
829e8c5c824SLee Schermerhorn 			list_del(&page->lru);
830e8c5c824SLee Schermerhorn 			h->free_huge_pages--;
831e8c5c824SLee Schermerhorn 			h->free_huge_pages_node[next_nid]--;
832685f3457SLee Schermerhorn 			if (acct_surplus) {
833685f3457SLee Schermerhorn 				h->surplus_huge_pages--;
834685f3457SLee Schermerhorn 				h->surplus_huge_pages_node[next_nid]--;
835685f3457SLee Schermerhorn 			}
836e8c5c824SLee Schermerhorn 			update_and_free_page(h, page);
837e8c5c824SLee Schermerhorn 			ret = 1;
8389a76db09SLee Schermerhorn 			break;
839e8c5c824SLee Schermerhorn 		}
8406ae11b27SLee Schermerhorn 		next_nid = hstate_next_node_to_free(h, nodes_allowed);
8419a76db09SLee Schermerhorn 	} while (next_nid != start_nid);
842e8c5c824SLee Schermerhorn 
843e8c5c824SLee Schermerhorn 	return ret;
844e8c5c824SLee Schermerhorn }
845e8c5c824SLee Schermerhorn 
846bf50bab2SNaoya Horiguchi static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
8477893d1d5SAdam Litke {
8487893d1d5SAdam Litke 	struct page *page;
849bf50bab2SNaoya Horiguchi 	unsigned int r_nid;
8507893d1d5SAdam Litke 
851aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
852aa888a74SAndi Kleen 		return NULL;
853aa888a74SAndi Kleen 
854d1c3fb1fSNishanth Aravamudan 	/*
855d1c3fb1fSNishanth Aravamudan 	 * Assume we will successfully allocate the surplus page to
856d1c3fb1fSNishanth Aravamudan 	 * prevent racing processes from causing the surplus to exceed
857d1c3fb1fSNishanth Aravamudan 	 * overcommit
858d1c3fb1fSNishanth Aravamudan 	 *
859d1c3fb1fSNishanth Aravamudan 	 * This however introduces a different race, where a process B
860d1c3fb1fSNishanth Aravamudan 	 * tries to grow the static hugepage pool while alloc_pages() is
861d1c3fb1fSNishanth Aravamudan 	 * called by process A. B will only examine the per-node
862d1c3fb1fSNishanth Aravamudan 	 * counters in determining if surplus huge pages can be
863d1c3fb1fSNishanth Aravamudan 	 * converted to normal huge pages in adjust_pool_surplus(). A
864d1c3fb1fSNishanth Aravamudan 	 * won't be able to increment the per-node counter, until the
865d1c3fb1fSNishanth Aravamudan 	 * lock is dropped by B, but B doesn't drop hugetlb_lock until
866d1c3fb1fSNishanth Aravamudan 	 * no more huge pages can be converted from surplus to normal
867d1c3fb1fSNishanth Aravamudan 	 * state (and doesn't try to convert again). Thus, we have a
868d1c3fb1fSNishanth Aravamudan 	 * case where a surplus huge page exists, the pool is grown, and
869d1c3fb1fSNishanth Aravamudan 	 * the surplus huge page still exists after, even though it
870d1c3fb1fSNishanth Aravamudan 	 * should just have been converted to a normal huge page. This
871d1c3fb1fSNishanth Aravamudan 	 * does not leak memory, though, as the hugepage will be freed
872d1c3fb1fSNishanth Aravamudan 	 * once it is out of use. It also does not allow the counters to
873d1c3fb1fSNishanth Aravamudan 	 * go out of whack in adjust_pool_surplus() as we don't modify
874d1c3fb1fSNishanth Aravamudan 	 * the node values until we've gotten the hugepage and only the
875d1c3fb1fSNishanth Aravamudan 	 * per-node value is checked there.
876d1c3fb1fSNishanth Aravamudan 	 */
877d1c3fb1fSNishanth Aravamudan 	spin_lock(&hugetlb_lock);
878a5516438SAndi Kleen 	if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
879d1c3fb1fSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
880d1c3fb1fSNishanth Aravamudan 		return NULL;
881d1c3fb1fSNishanth Aravamudan 	} else {
882a5516438SAndi Kleen 		h->nr_huge_pages++;
883a5516438SAndi Kleen 		h->surplus_huge_pages++;
884d1c3fb1fSNishanth Aravamudan 	}
885d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
886d1c3fb1fSNishanth Aravamudan 
887bf50bab2SNaoya Horiguchi 	if (nid == NUMA_NO_NODE)
888551883aeSNishanth Aravamudan 		page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
889551883aeSNishanth Aravamudan 				   __GFP_REPEAT|__GFP_NOWARN,
890a5516438SAndi Kleen 				   huge_page_order(h));
891bf50bab2SNaoya Horiguchi 	else
892bf50bab2SNaoya Horiguchi 		page = alloc_pages_exact_node(nid,
893bf50bab2SNaoya Horiguchi 			htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
894bf50bab2SNaoya Horiguchi 			__GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
895d1c3fb1fSNishanth Aravamudan 
896caff3a2cSGerald Schaefer 	if (page && arch_prepare_hugepage(page)) {
897caff3a2cSGerald Schaefer 		__free_pages(page, huge_page_order(h));
898ea5768c7SHillf Danton 		page = NULL;
899caff3a2cSGerald Schaefer 	}
900caff3a2cSGerald Schaefer 
9017893d1d5SAdam Litke 	spin_lock(&hugetlb_lock);
902d1c3fb1fSNishanth Aravamudan 	if (page) {
9030edaecfaSAneesh Kumar K.V 		INIT_LIST_HEAD(&page->lru);
904bf50bab2SNaoya Horiguchi 		r_nid = page_to_nid(page);
905d1c3fb1fSNishanth Aravamudan 		set_compound_page_dtor(page, free_huge_page);
9069dd540e2SAneesh Kumar K.V 		set_hugetlb_cgroup(page, NULL);
907d1c3fb1fSNishanth Aravamudan 		/*
908d1c3fb1fSNishanth Aravamudan 		 * We incremented the global counters already
909d1c3fb1fSNishanth Aravamudan 		 */
910bf50bab2SNaoya Horiguchi 		h->nr_huge_pages_node[r_nid]++;
911bf50bab2SNaoya Horiguchi 		h->surplus_huge_pages_node[r_nid]++;
9123b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC);
913d1c3fb1fSNishanth Aravamudan 	} else {
914a5516438SAndi Kleen 		h->nr_huge_pages--;
915a5516438SAndi Kleen 		h->surplus_huge_pages--;
9163b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
9177893d1d5SAdam Litke 	}
918d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
9197893d1d5SAdam Litke 
9207893d1d5SAdam Litke 	return page;
9217893d1d5SAdam Litke }
9227893d1d5SAdam Litke 
923e4e574b7SAdam Litke /*
924bf50bab2SNaoya Horiguchi  * This allocation function is useful in the context where vma is irrelevant.
925bf50bab2SNaoya Horiguchi  * E.g. soft-offlining uses this function because it only cares physical
926bf50bab2SNaoya Horiguchi  * address of error page.
927bf50bab2SNaoya Horiguchi  */
928bf50bab2SNaoya Horiguchi struct page *alloc_huge_page_node(struct hstate *h, int nid)
929bf50bab2SNaoya Horiguchi {
930bf50bab2SNaoya Horiguchi 	struct page *page;
931bf50bab2SNaoya Horiguchi 
932bf50bab2SNaoya Horiguchi 	spin_lock(&hugetlb_lock);
933bf50bab2SNaoya Horiguchi 	page = dequeue_huge_page_node(h, nid);
934bf50bab2SNaoya Horiguchi 	spin_unlock(&hugetlb_lock);
935bf50bab2SNaoya Horiguchi 
93694ae8ba7SAneesh Kumar K.V 	if (!page)
937bf50bab2SNaoya Horiguchi 		page = alloc_buddy_huge_page(h, nid);
938bf50bab2SNaoya Horiguchi 
939bf50bab2SNaoya Horiguchi 	return page;
940bf50bab2SNaoya Horiguchi }
941bf50bab2SNaoya Horiguchi 
942bf50bab2SNaoya Horiguchi /*
94325985edcSLucas De Marchi  * Increase the hugetlb pool such that it can accommodate a reservation
944e4e574b7SAdam Litke  * of size 'delta'.
945e4e574b7SAdam Litke  */
946a5516438SAndi Kleen static int gather_surplus_pages(struct hstate *h, int delta)
947e4e574b7SAdam Litke {
948e4e574b7SAdam Litke 	struct list_head surplus_list;
949e4e574b7SAdam Litke 	struct page *page, *tmp;
950e4e574b7SAdam Litke 	int ret, i;
951e4e574b7SAdam Litke 	int needed, allocated;
95228073b02SHillf Danton 	bool alloc_ok = true;
953e4e574b7SAdam Litke 
954a5516438SAndi Kleen 	needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
955ac09b3a1SAdam Litke 	if (needed <= 0) {
956a5516438SAndi Kleen 		h->resv_huge_pages += delta;
957e4e574b7SAdam Litke 		return 0;
958ac09b3a1SAdam Litke 	}
959e4e574b7SAdam Litke 
960e4e574b7SAdam Litke 	allocated = 0;
961e4e574b7SAdam Litke 	INIT_LIST_HEAD(&surplus_list);
962e4e574b7SAdam Litke 
963e4e574b7SAdam Litke 	ret = -ENOMEM;
964e4e574b7SAdam Litke retry:
965e4e574b7SAdam Litke 	spin_unlock(&hugetlb_lock);
966e4e574b7SAdam Litke 	for (i = 0; i < needed; i++) {
967bf50bab2SNaoya Horiguchi 		page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
96828073b02SHillf Danton 		if (!page) {
96928073b02SHillf Danton 			alloc_ok = false;
97028073b02SHillf Danton 			break;
97128073b02SHillf Danton 		}
972e4e574b7SAdam Litke 		list_add(&page->lru, &surplus_list);
973e4e574b7SAdam Litke 	}
97428073b02SHillf Danton 	allocated += i;
975e4e574b7SAdam Litke 
976e4e574b7SAdam Litke 	/*
977e4e574b7SAdam Litke 	 * After retaking hugetlb_lock, we need to recalculate 'needed'
978e4e574b7SAdam Litke 	 * because either resv_huge_pages or free_huge_pages may have changed.
979e4e574b7SAdam Litke 	 */
980e4e574b7SAdam Litke 	spin_lock(&hugetlb_lock);
981a5516438SAndi Kleen 	needed = (h->resv_huge_pages + delta) -
982a5516438SAndi Kleen 			(h->free_huge_pages + allocated);
98328073b02SHillf Danton 	if (needed > 0) {
98428073b02SHillf Danton 		if (alloc_ok)
985e4e574b7SAdam Litke 			goto retry;
98628073b02SHillf Danton 		/*
98728073b02SHillf Danton 		 * We were not able to allocate enough pages to
98828073b02SHillf Danton 		 * satisfy the entire reservation so we free what
98928073b02SHillf Danton 		 * we've allocated so far.
99028073b02SHillf Danton 		 */
99128073b02SHillf Danton 		goto free;
99228073b02SHillf Danton 	}
993e4e574b7SAdam Litke 	/*
994e4e574b7SAdam Litke 	 * The surplus_list now contains _at_least_ the number of extra pages
99525985edcSLucas De Marchi 	 * needed to accommodate the reservation.  Add the appropriate number
996e4e574b7SAdam Litke 	 * of pages to the hugetlb pool and free the extras back to the buddy
997ac09b3a1SAdam Litke 	 * allocator.  Commit the entire reservation here to prevent another
998ac09b3a1SAdam Litke 	 * process from stealing the pages as they are added to the pool but
999ac09b3a1SAdam Litke 	 * before they are reserved.
1000e4e574b7SAdam Litke 	 */
1001e4e574b7SAdam Litke 	needed += allocated;
1002a5516438SAndi Kleen 	h->resv_huge_pages += delta;
1003e4e574b7SAdam Litke 	ret = 0;
1004a9869b83SNaoya Horiguchi 
100519fc3f0aSAdam Litke 	/* Free the needed pages to the hugetlb pool */
100619fc3f0aSAdam Litke 	list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
100719fc3f0aSAdam Litke 		if ((--needed) < 0)
100819fc3f0aSAdam Litke 			break;
1009a9869b83SNaoya Horiguchi 		/*
1010a9869b83SNaoya Horiguchi 		 * This page is now managed by the hugetlb allocator and has
1011a9869b83SNaoya Horiguchi 		 * no users -- drop the buddy allocator's reference.
1012a9869b83SNaoya Horiguchi 		 */
1013a9869b83SNaoya Horiguchi 		put_page_testzero(page);
1014a9869b83SNaoya Horiguchi 		VM_BUG_ON(page_count(page));
1015a5516438SAndi Kleen 		enqueue_huge_page(h, page);
101619fc3f0aSAdam Litke 	}
101728073b02SHillf Danton free:
1018b0365c8dSHillf Danton 	spin_unlock(&hugetlb_lock);
101919fc3f0aSAdam Litke 
102019fc3f0aSAdam Litke 	/* Free unnecessary surplus pages to the buddy allocator */
102119fc3f0aSAdam Litke 	if (!list_empty(&surplus_list)) {
1022e4e574b7SAdam Litke 		list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
1023a9869b83SNaoya Horiguchi 			put_page(page);
1024a9869b83SNaoya Horiguchi 		}
1025af767cbdSAdam Litke 	}
102619fc3f0aSAdam Litke 	spin_lock(&hugetlb_lock);
1027e4e574b7SAdam Litke 
1028e4e574b7SAdam Litke 	return ret;
1029e4e574b7SAdam Litke }
1030e4e574b7SAdam Litke 
1031e4e574b7SAdam Litke /*
1032e4e574b7SAdam Litke  * When releasing a hugetlb pool reservation, any surplus pages that were
1033e4e574b7SAdam Litke  * allocated to satisfy the reservation must be explicitly freed if they were
1034e4e574b7SAdam Litke  * never used.
1035685f3457SLee Schermerhorn  * Called with hugetlb_lock held.
1036e4e574b7SAdam Litke  */
1037a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h,
1038a5516438SAndi Kleen 					unsigned long unused_resv_pages)
1039e4e574b7SAdam Litke {
1040e4e574b7SAdam Litke 	unsigned long nr_pages;
1041e4e574b7SAdam Litke 
1042ac09b3a1SAdam Litke 	/* Uncommit the reservation */
1043a5516438SAndi Kleen 	h->resv_huge_pages -= unused_resv_pages;
1044ac09b3a1SAdam Litke 
1045aa888a74SAndi Kleen 	/* Cannot return gigantic pages currently */
1046aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1047aa888a74SAndi Kleen 		return;
1048aa888a74SAndi Kleen 
1049a5516438SAndi Kleen 	nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
1050e4e574b7SAdam Litke 
1051685f3457SLee Schermerhorn 	/*
1052685f3457SLee Schermerhorn 	 * We want to release as many surplus pages as possible, spread
10539b5e5d0fSLee Schermerhorn 	 * evenly across all nodes with memory. Iterate across these nodes
10549b5e5d0fSLee Schermerhorn 	 * until we can no longer free unreserved surplus pages. This occurs
10559b5e5d0fSLee Schermerhorn 	 * when the nodes with surplus pages have no free pages.
10569b5e5d0fSLee Schermerhorn 	 * free_pool_huge_page() will balance the the freed pages across the
10579b5e5d0fSLee Schermerhorn 	 * on-line nodes with memory and will handle the hstate accounting.
1058685f3457SLee Schermerhorn 	 */
1059685f3457SLee Schermerhorn 	while (nr_pages--) {
10609b5e5d0fSLee Schermerhorn 		if (!free_pool_huge_page(h, &node_states[N_HIGH_MEMORY], 1))
1061685f3457SLee Schermerhorn 			break;
1062e4e574b7SAdam Litke 	}
1063e4e574b7SAdam Litke }
1064e4e574b7SAdam Litke 
1065c37f9fb1SAndy Whitcroft /*
1066c37f9fb1SAndy Whitcroft  * Determine if the huge page at addr within the vma has an associated
1067c37f9fb1SAndy Whitcroft  * reservation.  Where it does not we will need to logically increase
106890481622SDavid Gibson  * reservation and actually increase subpool usage before an allocation
106990481622SDavid Gibson  * can occur.  Where any new reservation would be required the
107090481622SDavid Gibson  * reservation change is prepared, but not committed.  Once the page
107190481622SDavid Gibson  * has been allocated from the subpool and instantiated the change should
107290481622SDavid Gibson  * be committed via vma_commit_reservation.  No action is required on
107390481622SDavid Gibson  * failure.
1074c37f9fb1SAndy Whitcroft  */
1075e2f17d94SRoel Kluin static long vma_needs_reservation(struct hstate *h,
1076a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long addr)
1077c37f9fb1SAndy Whitcroft {
1078c37f9fb1SAndy Whitcroft 	struct address_space *mapping = vma->vm_file->f_mapping;
1079c37f9fb1SAndy Whitcroft 	struct inode *inode = mapping->host;
1080c37f9fb1SAndy Whitcroft 
1081f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE) {
1082a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
1083c37f9fb1SAndy Whitcroft 		return region_chg(&inode->i_mapping->private_list,
1084c37f9fb1SAndy Whitcroft 							idx, idx + 1);
1085c37f9fb1SAndy Whitcroft 
108684afd99bSAndy Whitcroft 	} else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1087c37f9fb1SAndy Whitcroft 		return 1;
1088c37f9fb1SAndy Whitcroft 
108984afd99bSAndy Whitcroft 	} else  {
1090e2f17d94SRoel Kluin 		long err;
1091a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
109284afd99bSAndy Whitcroft 		struct resv_map *reservations = vma_resv_map(vma);
109384afd99bSAndy Whitcroft 
109484afd99bSAndy Whitcroft 		err = region_chg(&reservations->regions, idx, idx + 1);
109584afd99bSAndy Whitcroft 		if (err < 0)
109684afd99bSAndy Whitcroft 			return err;
1097c37f9fb1SAndy Whitcroft 		return 0;
1098c37f9fb1SAndy Whitcroft 	}
109984afd99bSAndy Whitcroft }
1100a5516438SAndi Kleen static void vma_commit_reservation(struct hstate *h,
1101a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long addr)
1102c37f9fb1SAndy Whitcroft {
1103c37f9fb1SAndy Whitcroft 	struct address_space *mapping = vma->vm_file->f_mapping;
1104c37f9fb1SAndy Whitcroft 	struct inode *inode = mapping->host;
1105c37f9fb1SAndy Whitcroft 
1106f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE) {
1107a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
1108c37f9fb1SAndy Whitcroft 		region_add(&inode->i_mapping->private_list, idx, idx + 1);
110984afd99bSAndy Whitcroft 
111084afd99bSAndy Whitcroft 	} else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1111a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
111284afd99bSAndy Whitcroft 		struct resv_map *reservations = vma_resv_map(vma);
111384afd99bSAndy Whitcroft 
111484afd99bSAndy Whitcroft 		/* Mark this page used in the map. */
111584afd99bSAndy Whitcroft 		region_add(&reservations->regions, idx, idx + 1);
1116c37f9fb1SAndy Whitcroft 	}
1117c37f9fb1SAndy Whitcroft }
1118c37f9fb1SAndy Whitcroft 
1119348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma,
112004f2cbe3SMel Gorman 				    unsigned long addr, int avoid_reserve)
1121348ea204SAdam Litke {
112290481622SDavid Gibson 	struct hugepage_subpool *spool = subpool_vma(vma);
1123a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
1124348ea204SAdam Litke 	struct page *page;
1125e2f17d94SRoel Kluin 	long chg;
11266d76dcf4SAneesh Kumar K.V 	int ret, idx;
11276d76dcf4SAneesh Kumar K.V 	struct hugetlb_cgroup *h_cg;
11282fc39cecSAdam Litke 
11296d76dcf4SAneesh Kumar K.V 	idx = hstate_index(h);
1130a1e78772SMel Gorman 	/*
113190481622SDavid Gibson 	 * Processes that did not create the mapping will have no
113290481622SDavid Gibson 	 * reserves and will not have accounted against subpool
113390481622SDavid Gibson 	 * limit. Check that the subpool limit can be made before
113490481622SDavid Gibson 	 * satisfying the allocation MAP_NORESERVE mappings may also
113590481622SDavid Gibson 	 * need pages and subpool limit allocated allocated if no reserve
113690481622SDavid Gibson 	 * mapping overlaps.
1137a1e78772SMel Gorman 	 */
1138a5516438SAndi Kleen 	chg = vma_needs_reservation(h, vma, addr);
1139c37f9fb1SAndy Whitcroft 	if (chg < 0)
114076dcee75SAneesh Kumar K.V 		return ERR_PTR(-ENOMEM);
1141c37f9fb1SAndy Whitcroft 	if (chg)
114290481622SDavid Gibson 		if (hugepage_subpool_get_pages(spool, chg))
114376dcee75SAneesh Kumar K.V 			return ERR_PTR(-ENOSPC);
114490d8b7e6SAdam Litke 
11456d76dcf4SAneesh Kumar K.V 	ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
11466d76dcf4SAneesh Kumar K.V 	if (ret) {
11476d76dcf4SAneesh Kumar K.V 		hugepage_subpool_put_pages(spool, chg);
11486d76dcf4SAneesh Kumar K.V 		return ERR_PTR(-ENOSPC);
11496d76dcf4SAneesh Kumar K.V 	}
1150a1e78772SMel Gorman 	spin_lock(&hugetlb_lock);
1151a5516438SAndi Kleen 	page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
115294ae8ba7SAneesh Kumar K.V 	if (page) {
115394ae8ba7SAneesh Kumar K.V 		/* update page cgroup details */
115494ae8ba7SAneesh Kumar K.V 		hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h),
115594ae8ba7SAneesh Kumar K.V 					     h_cg, page);
1156a1e78772SMel Gorman 		spin_unlock(&hugetlb_lock);
115794ae8ba7SAneesh Kumar K.V 	} else {
115894ae8ba7SAneesh Kumar K.V 		spin_unlock(&hugetlb_lock);
1159bf50bab2SNaoya Horiguchi 		page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
1160a1e78772SMel Gorman 		if (!page) {
11616d76dcf4SAneesh Kumar K.V 			hugetlb_cgroup_uncharge_cgroup(idx,
11626d76dcf4SAneesh Kumar K.V 						       pages_per_huge_page(h),
11636d76dcf4SAneesh Kumar K.V 						       h_cg);
116490481622SDavid Gibson 			hugepage_subpool_put_pages(spool, chg);
116576dcee75SAneesh Kumar K.V 			return ERR_PTR(-ENOSPC);
1166a1e78772SMel Gorman 		}
116779dbb236SAneesh Kumar K.V 		spin_lock(&hugetlb_lock);
116894ae8ba7SAneesh Kumar K.V 		hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h),
116994ae8ba7SAneesh Kumar K.V 					     h_cg, page);
117079dbb236SAneesh Kumar K.V 		list_move(&page->lru, &h->hugepage_activelist);
117179dbb236SAneesh Kumar K.V 		spin_unlock(&hugetlb_lock);
1172a1e78772SMel Gorman 	}
1173a1e78772SMel Gorman 
117490481622SDavid Gibson 	set_page_private(page, (unsigned long)spool);
1175a1e78772SMel Gorman 
1176a5516438SAndi Kleen 	vma_commit_reservation(h, vma, addr);
11777893d1d5SAdam Litke 	return page;
1178b45b5bd6SDavid Gibson }
1179b45b5bd6SDavid Gibson 
118091f47662SCyrill Gorcunov int __weak alloc_bootmem_huge_page(struct hstate *h)
1181aa888a74SAndi Kleen {
1182aa888a74SAndi Kleen 	struct huge_bootmem_page *m;
11839b5e5d0fSLee Schermerhorn 	int nr_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
1184aa888a74SAndi Kleen 
1185aa888a74SAndi Kleen 	while (nr_nodes) {
1186aa888a74SAndi Kleen 		void *addr;
1187aa888a74SAndi Kleen 
1188aa888a74SAndi Kleen 		addr = __alloc_bootmem_node_nopanic(
11896ae11b27SLee Schermerhorn 				NODE_DATA(hstate_next_node_to_alloc(h,
11909b5e5d0fSLee Schermerhorn 						&node_states[N_HIGH_MEMORY])),
1191aa888a74SAndi Kleen 				huge_page_size(h), huge_page_size(h), 0);
1192aa888a74SAndi Kleen 
1193aa888a74SAndi Kleen 		if (addr) {
1194aa888a74SAndi Kleen 			/*
1195aa888a74SAndi Kleen 			 * Use the beginning of the huge page to store the
1196aa888a74SAndi Kleen 			 * huge_bootmem_page struct (until gather_bootmem
1197aa888a74SAndi Kleen 			 * puts them into the mem_map).
1198aa888a74SAndi Kleen 			 */
1199aa888a74SAndi Kleen 			m = addr;
1200aa888a74SAndi Kleen 			goto found;
1201aa888a74SAndi Kleen 		}
1202aa888a74SAndi Kleen 		nr_nodes--;
1203aa888a74SAndi Kleen 	}
1204aa888a74SAndi Kleen 	return 0;
1205aa888a74SAndi Kleen 
1206aa888a74SAndi Kleen found:
1207aa888a74SAndi Kleen 	BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
1208aa888a74SAndi Kleen 	/* Put them into a private list first because mem_map is not up yet */
1209aa888a74SAndi Kleen 	list_add(&m->list, &huge_boot_pages);
1210aa888a74SAndi Kleen 	m->hstate = h;
1211aa888a74SAndi Kleen 	return 1;
1212aa888a74SAndi Kleen }
1213aa888a74SAndi Kleen 
121418229df5SAndy Whitcroft static void prep_compound_huge_page(struct page *page, int order)
121518229df5SAndy Whitcroft {
121618229df5SAndy Whitcroft 	if (unlikely(order > (MAX_ORDER - 1)))
121718229df5SAndy Whitcroft 		prep_compound_gigantic_page(page, order);
121818229df5SAndy Whitcroft 	else
121918229df5SAndy Whitcroft 		prep_compound_page(page, order);
122018229df5SAndy Whitcroft }
122118229df5SAndy Whitcroft 
1222aa888a74SAndi Kleen /* Put bootmem huge pages into the standard lists after mem_map is up */
1223aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void)
1224aa888a74SAndi Kleen {
1225aa888a74SAndi Kleen 	struct huge_bootmem_page *m;
1226aa888a74SAndi Kleen 
1227aa888a74SAndi Kleen 	list_for_each_entry(m, &huge_boot_pages, list) {
1228aa888a74SAndi Kleen 		struct hstate *h = m->hstate;
1229ee8f248dSBecky Bruce 		struct page *page;
1230ee8f248dSBecky Bruce 
1231ee8f248dSBecky Bruce #ifdef CONFIG_HIGHMEM
1232ee8f248dSBecky Bruce 		page = pfn_to_page(m->phys >> PAGE_SHIFT);
1233ee8f248dSBecky Bruce 		free_bootmem_late((unsigned long)m,
1234ee8f248dSBecky Bruce 				  sizeof(struct huge_bootmem_page));
1235ee8f248dSBecky Bruce #else
1236ee8f248dSBecky Bruce 		page = virt_to_page(m);
1237ee8f248dSBecky Bruce #endif
1238aa888a74SAndi Kleen 		__ClearPageReserved(page);
1239aa888a74SAndi Kleen 		WARN_ON(page_count(page) != 1);
124018229df5SAndy Whitcroft 		prep_compound_huge_page(page, h->order);
1241aa888a74SAndi Kleen 		prep_new_huge_page(h, page, page_to_nid(page));
1242b0320c7bSRafael Aquini 		/*
1243b0320c7bSRafael Aquini 		 * If we had gigantic hugepages allocated at boot time, we need
1244b0320c7bSRafael Aquini 		 * to restore the 'stolen' pages to totalram_pages in order to
1245b0320c7bSRafael Aquini 		 * fix confusing memory reports from free(1) and another
1246b0320c7bSRafael Aquini 		 * side-effects, like CommitLimit going negative.
1247b0320c7bSRafael Aquini 		 */
1248b0320c7bSRafael Aquini 		if (h->order > (MAX_ORDER - 1))
1249b0320c7bSRafael Aquini 			totalram_pages += 1 << h->order;
1250aa888a74SAndi Kleen 	}
1251aa888a74SAndi Kleen }
1252aa888a74SAndi Kleen 
12538faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
12541da177e4SLinus Torvalds {
12551da177e4SLinus Torvalds 	unsigned long i;
12561da177e4SLinus Torvalds 
1257e5ff2159SAndi Kleen 	for (i = 0; i < h->max_huge_pages; ++i) {
1258aa888a74SAndi Kleen 		if (h->order >= MAX_ORDER) {
1259aa888a74SAndi Kleen 			if (!alloc_bootmem_huge_page(h))
1260aa888a74SAndi Kleen 				break;
12619b5e5d0fSLee Schermerhorn 		} else if (!alloc_fresh_huge_page(h,
12629b5e5d0fSLee Schermerhorn 					 &node_states[N_HIGH_MEMORY]))
12631da177e4SLinus Torvalds 			break;
12641da177e4SLinus Torvalds 	}
12658faa8b07SAndi Kleen 	h->max_huge_pages = i;
1266e5ff2159SAndi Kleen }
1267e5ff2159SAndi Kleen 
1268e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void)
1269e5ff2159SAndi Kleen {
1270e5ff2159SAndi Kleen 	struct hstate *h;
1271e5ff2159SAndi Kleen 
1272e5ff2159SAndi Kleen 	for_each_hstate(h) {
12738faa8b07SAndi Kleen 		/* oversize hugepages were init'ed in early boot */
12748faa8b07SAndi Kleen 		if (h->order < MAX_ORDER)
12758faa8b07SAndi Kleen 			hugetlb_hstate_alloc_pages(h);
1276e5ff2159SAndi Kleen 	}
1277e5ff2159SAndi Kleen }
1278e5ff2159SAndi Kleen 
12794abd32dbSAndi Kleen static char * __init memfmt(char *buf, unsigned long n)
12804abd32dbSAndi Kleen {
12814abd32dbSAndi Kleen 	if (n >= (1UL << 30))
12824abd32dbSAndi Kleen 		sprintf(buf, "%lu GB", n >> 30);
12834abd32dbSAndi Kleen 	else if (n >= (1UL << 20))
12844abd32dbSAndi Kleen 		sprintf(buf, "%lu MB", n >> 20);
12854abd32dbSAndi Kleen 	else
12864abd32dbSAndi Kleen 		sprintf(buf, "%lu KB", n >> 10);
12874abd32dbSAndi Kleen 	return buf;
12884abd32dbSAndi Kleen }
12894abd32dbSAndi Kleen 
1290e5ff2159SAndi Kleen static void __init report_hugepages(void)
1291e5ff2159SAndi Kleen {
1292e5ff2159SAndi Kleen 	struct hstate *h;
1293e5ff2159SAndi Kleen 
1294e5ff2159SAndi Kleen 	for_each_hstate(h) {
12954abd32dbSAndi Kleen 		char buf[32];
12964abd32dbSAndi Kleen 		printk(KERN_INFO "HugeTLB registered %s page size, "
12974abd32dbSAndi Kleen 				 "pre-allocated %ld pages\n",
12984abd32dbSAndi Kleen 			memfmt(buf, huge_page_size(h)),
12994abd32dbSAndi Kleen 			h->free_huge_pages);
1300e5ff2159SAndi Kleen 	}
1301e5ff2159SAndi Kleen }
1302e5ff2159SAndi Kleen 
13031da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM
13046ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count,
13056ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
13061da177e4SLinus Torvalds {
13074415cc8dSChristoph Lameter 	int i;
13084415cc8dSChristoph Lameter 
1309aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1310aa888a74SAndi Kleen 		return;
1311aa888a74SAndi Kleen 
13126ae11b27SLee Schermerhorn 	for_each_node_mask(i, *nodes_allowed) {
13131da177e4SLinus Torvalds 		struct page *page, *next;
1314a5516438SAndi Kleen 		struct list_head *freel = &h->hugepage_freelists[i];
1315a5516438SAndi Kleen 		list_for_each_entry_safe(page, next, freel, lru) {
1316a5516438SAndi Kleen 			if (count >= h->nr_huge_pages)
13176b0c880dSAdam Litke 				return;
13181da177e4SLinus Torvalds 			if (PageHighMem(page))
13191da177e4SLinus Torvalds 				continue;
13201da177e4SLinus Torvalds 			list_del(&page->lru);
1321e5ff2159SAndi Kleen 			update_and_free_page(h, page);
1322a5516438SAndi Kleen 			h->free_huge_pages--;
1323a5516438SAndi Kleen 			h->free_huge_pages_node[page_to_nid(page)]--;
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds #else
13286ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count,
13296ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
13301da177e4SLinus Torvalds {
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds #endif
13331da177e4SLinus Torvalds 
133420a0307cSWu Fengguang /*
133520a0307cSWu Fengguang  * Increment or decrement surplus_huge_pages.  Keep node-specific counters
133620a0307cSWu Fengguang  * balanced by operating on them in a round-robin fashion.
133720a0307cSWu Fengguang  * Returns 1 if an adjustment was made.
133820a0307cSWu Fengguang  */
13396ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
13406ae11b27SLee Schermerhorn 				int delta)
134120a0307cSWu Fengguang {
1342e8c5c824SLee Schermerhorn 	int start_nid, next_nid;
134320a0307cSWu Fengguang 	int ret = 0;
134420a0307cSWu Fengguang 
134520a0307cSWu Fengguang 	VM_BUG_ON(delta != -1 && delta != 1);
134620a0307cSWu Fengguang 
1347e8c5c824SLee Schermerhorn 	if (delta < 0)
13486ae11b27SLee Schermerhorn 		start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
1349e8c5c824SLee Schermerhorn 	else
13506ae11b27SLee Schermerhorn 		start_nid = hstate_next_node_to_free(h, nodes_allowed);
1351e8c5c824SLee Schermerhorn 	next_nid = start_nid;
1352e8c5c824SLee Schermerhorn 
1353e8c5c824SLee Schermerhorn 	do {
1354e8c5c824SLee Schermerhorn 		int nid = next_nid;
1355e8c5c824SLee Schermerhorn 		if (delta < 0)  {
1356e8c5c824SLee Schermerhorn 			/*
1357e8c5c824SLee Schermerhorn 			 * To shrink on this node, there must be a surplus page
1358e8c5c824SLee Schermerhorn 			 */
13599a76db09SLee Schermerhorn 			if (!h->surplus_huge_pages_node[nid]) {
13606ae11b27SLee Schermerhorn 				next_nid = hstate_next_node_to_alloc(h,
13616ae11b27SLee Schermerhorn 								nodes_allowed);
136220a0307cSWu Fengguang 				continue;
1363e8c5c824SLee Schermerhorn 			}
13649a76db09SLee Schermerhorn 		}
1365e8c5c824SLee Schermerhorn 		if (delta > 0) {
1366e8c5c824SLee Schermerhorn 			/*
1367e8c5c824SLee Schermerhorn 			 * Surplus cannot exceed the total number of pages
1368e8c5c824SLee Schermerhorn 			 */
1369e8c5c824SLee Schermerhorn 			if (h->surplus_huge_pages_node[nid] >=
13709a76db09SLee Schermerhorn 						h->nr_huge_pages_node[nid]) {
13716ae11b27SLee Schermerhorn 				next_nid = hstate_next_node_to_free(h,
13726ae11b27SLee Schermerhorn 								nodes_allowed);
137320a0307cSWu Fengguang 				continue;
1374e8c5c824SLee Schermerhorn 			}
13759a76db09SLee Schermerhorn 		}
137620a0307cSWu Fengguang 
137720a0307cSWu Fengguang 		h->surplus_huge_pages += delta;
137820a0307cSWu Fengguang 		h->surplus_huge_pages_node[nid] += delta;
137920a0307cSWu Fengguang 		ret = 1;
138020a0307cSWu Fengguang 		break;
1381e8c5c824SLee Schermerhorn 	} while (next_nid != start_nid);
138220a0307cSWu Fengguang 
138320a0307cSWu Fengguang 	return ret;
138420a0307cSWu Fengguang }
138520a0307cSWu Fengguang 
1386a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
13876ae11b27SLee Schermerhorn static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
13886ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
13891da177e4SLinus Torvalds {
13907893d1d5SAdam Litke 	unsigned long min_count, ret;
13911da177e4SLinus Torvalds 
1392aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1393aa888a74SAndi Kleen 		return h->max_huge_pages;
1394aa888a74SAndi Kleen 
13957893d1d5SAdam Litke 	/*
13967893d1d5SAdam Litke 	 * Increase the pool size
13977893d1d5SAdam Litke 	 * First take pages out of surplus state.  Then make up the
13987893d1d5SAdam Litke 	 * remaining difference by allocating fresh huge pages.
1399d1c3fb1fSNishanth Aravamudan 	 *
1400d1c3fb1fSNishanth Aravamudan 	 * We might race with alloc_buddy_huge_page() here and be unable
1401d1c3fb1fSNishanth Aravamudan 	 * to convert a surplus huge page to a normal huge page. That is
1402d1c3fb1fSNishanth Aravamudan 	 * not critical, though, it just means the overall size of the
1403d1c3fb1fSNishanth Aravamudan 	 * pool might be one hugepage larger than it needs to be, but
1404d1c3fb1fSNishanth Aravamudan 	 * within all the constraints specified by the sysctls.
14057893d1d5SAdam Litke 	 */
14061da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
1407a5516438SAndi Kleen 	while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
14086ae11b27SLee Schermerhorn 		if (!adjust_pool_surplus(h, nodes_allowed, -1))
14097893d1d5SAdam Litke 			break;
14107893d1d5SAdam Litke 	}
14117893d1d5SAdam Litke 
1412a5516438SAndi Kleen 	while (count > persistent_huge_pages(h)) {
14137893d1d5SAdam Litke 		/*
14147893d1d5SAdam Litke 		 * If this allocation races such that we no longer need the
14157893d1d5SAdam Litke 		 * page, free_huge_page will handle it by freeing the page
14167893d1d5SAdam Litke 		 * and reducing the surplus.
14177893d1d5SAdam Litke 		 */
14187893d1d5SAdam Litke 		spin_unlock(&hugetlb_lock);
14196ae11b27SLee Schermerhorn 		ret = alloc_fresh_huge_page(h, nodes_allowed);
14207893d1d5SAdam Litke 		spin_lock(&hugetlb_lock);
14217893d1d5SAdam Litke 		if (!ret)
14227893d1d5SAdam Litke 			goto out;
14237893d1d5SAdam Litke 
1424536240f2SMel Gorman 		/* Bail for signals. Probably ctrl-c from user */
1425536240f2SMel Gorman 		if (signal_pending(current))
1426536240f2SMel Gorman 			goto out;
14277893d1d5SAdam Litke 	}
14287893d1d5SAdam Litke 
14297893d1d5SAdam Litke 	/*
14307893d1d5SAdam Litke 	 * Decrease the pool size
14317893d1d5SAdam Litke 	 * First return free pages to the buddy allocator (being careful
14327893d1d5SAdam Litke 	 * to keep enough around to satisfy reservations).  Then place
14337893d1d5SAdam Litke 	 * pages into surplus state as needed so the pool will shrink
14347893d1d5SAdam Litke 	 * to the desired size as pages become free.
1435d1c3fb1fSNishanth Aravamudan 	 *
1436d1c3fb1fSNishanth Aravamudan 	 * By placing pages into the surplus state independent of the
1437d1c3fb1fSNishanth Aravamudan 	 * overcommit value, we are allowing the surplus pool size to
1438d1c3fb1fSNishanth Aravamudan 	 * exceed overcommit. There are few sane options here. Since
1439d1c3fb1fSNishanth Aravamudan 	 * alloc_buddy_huge_page() is checking the global counter,
1440d1c3fb1fSNishanth Aravamudan 	 * though, we'll note that we're not allowed to exceed surplus
1441d1c3fb1fSNishanth Aravamudan 	 * and won't grow the pool anywhere else. Not until one of the
1442d1c3fb1fSNishanth Aravamudan 	 * sysctls are changed, or the surplus pages go out of use.
14437893d1d5SAdam Litke 	 */
1444a5516438SAndi Kleen 	min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
14456b0c880dSAdam Litke 	min_count = max(count, min_count);
14466ae11b27SLee Schermerhorn 	try_to_free_low(h, min_count, nodes_allowed);
1447a5516438SAndi Kleen 	while (min_count < persistent_huge_pages(h)) {
14486ae11b27SLee Schermerhorn 		if (!free_pool_huge_page(h, nodes_allowed, 0))
14491da177e4SLinus Torvalds 			break;
14501da177e4SLinus Torvalds 	}
1451a5516438SAndi Kleen 	while (count < persistent_huge_pages(h)) {
14526ae11b27SLee Schermerhorn 		if (!adjust_pool_surplus(h, nodes_allowed, 1))
14537893d1d5SAdam Litke 			break;
14547893d1d5SAdam Litke 	}
14557893d1d5SAdam Litke out:
1456a5516438SAndi Kleen 	ret = persistent_huge_pages(h);
14571da177e4SLinus Torvalds 	spin_unlock(&hugetlb_lock);
14587893d1d5SAdam Litke 	return ret;
14591da177e4SLinus Torvalds }
14601da177e4SLinus Torvalds 
1461a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \
1462a3437870SNishanth Aravamudan 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1463a3437870SNishanth Aravamudan 
1464a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \
1465a3437870SNishanth Aravamudan 	static struct kobj_attribute _name##_attr = \
1466a3437870SNishanth Aravamudan 		__ATTR(_name, 0644, _name##_show, _name##_store)
1467a3437870SNishanth Aravamudan 
1468a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj;
1469a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
1470a3437870SNishanth Aravamudan 
14719a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
14729a305230SLee Schermerhorn 
14739a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
1474a3437870SNishanth Aravamudan {
1475a3437870SNishanth Aravamudan 	int i;
14769a305230SLee Schermerhorn 
1477a3437870SNishanth Aravamudan 	for (i = 0; i < HUGE_MAX_HSTATE; i++)
14789a305230SLee Schermerhorn 		if (hstate_kobjs[i] == kobj) {
14799a305230SLee Schermerhorn 			if (nidp)
14809a305230SLee Schermerhorn 				*nidp = NUMA_NO_NODE;
1481a3437870SNishanth Aravamudan 			return &hstates[i];
14829a305230SLee Schermerhorn 		}
14839a305230SLee Schermerhorn 
14849a305230SLee Schermerhorn 	return kobj_to_node_hstate(kobj, nidp);
1485a3437870SNishanth Aravamudan }
1486a3437870SNishanth Aravamudan 
148706808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj,
1488a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1489a3437870SNishanth Aravamudan {
14909a305230SLee Schermerhorn 	struct hstate *h;
14919a305230SLee Schermerhorn 	unsigned long nr_huge_pages;
14929a305230SLee Schermerhorn 	int nid;
14939a305230SLee Schermerhorn 
14949a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
14959a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
14969a305230SLee Schermerhorn 		nr_huge_pages = h->nr_huge_pages;
14979a305230SLee Schermerhorn 	else
14989a305230SLee Schermerhorn 		nr_huge_pages = h->nr_huge_pages_node[nid];
14999a305230SLee Schermerhorn 
15009a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", nr_huge_pages);
1501a3437870SNishanth Aravamudan }
1502adbe8726SEric B Munson 
150306808b08SLee Schermerhorn static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
150406808b08SLee Schermerhorn 			struct kobject *kobj, struct kobj_attribute *attr,
150506808b08SLee Schermerhorn 			const char *buf, size_t len)
1506a3437870SNishanth Aravamudan {
1507a3437870SNishanth Aravamudan 	int err;
15089a305230SLee Schermerhorn 	int nid;
150906808b08SLee Schermerhorn 	unsigned long count;
15109a305230SLee Schermerhorn 	struct hstate *h;
1511bad44b5bSDavid Rientjes 	NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
1512a3437870SNishanth Aravamudan 
151306808b08SLee Schermerhorn 	err = strict_strtoul(buf, 10, &count);
151473ae31e5SEric B Munson 	if (err)
1515adbe8726SEric B Munson 		goto out;
1516a3437870SNishanth Aravamudan 
15179a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
1518adbe8726SEric B Munson 	if (h->order >= MAX_ORDER) {
1519adbe8726SEric B Munson 		err = -EINVAL;
1520adbe8726SEric B Munson 		goto out;
1521adbe8726SEric B Munson 	}
1522adbe8726SEric B Munson 
15239a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE) {
15249a305230SLee Schermerhorn 		/*
15259a305230SLee Schermerhorn 		 * global hstate attribute
15269a305230SLee Schermerhorn 		 */
15279a305230SLee Schermerhorn 		if (!(obey_mempolicy &&
15289a305230SLee Schermerhorn 				init_nodemask_of_mempolicy(nodes_allowed))) {
152906808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
15309a305230SLee Schermerhorn 			nodes_allowed = &node_states[N_HIGH_MEMORY];
153106808b08SLee Schermerhorn 		}
15329a305230SLee Schermerhorn 	} else if (nodes_allowed) {
15339a305230SLee Schermerhorn 		/*
15349a305230SLee Schermerhorn 		 * per node hstate attribute: adjust count to global,
15359a305230SLee Schermerhorn 		 * but restrict alloc/free to the specified node.
15369a305230SLee Schermerhorn 		 */
15379a305230SLee Schermerhorn 		count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
15389a305230SLee Schermerhorn 		init_nodemask_of_node(nodes_allowed, nid);
15399a305230SLee Schermerhorn 	} else
15409a305230SLee Schermerhorn 		nodes_allowed = &node_states[N_HIGH_MEMORY];
15419a305230SLee Schermerhorn 
154206808b08SLee Schermerhorn 	h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
1543a3437870SNishanth Aravamudan 
15449b5e5d0fSLee Schermerhorn 	if (nodes_allowed != &node_states[N_HIGH_MEMORY])
154506808b08SLee Schermerhorn 		NODEMASK_FREE(nodes_allowed);
154606808b08SLee Schermerhorn 
154706808b08SLee Schermerhorn 	return len;
1548adbe8726SEric B Munson out:
1549adbe8726SEric B Munson 	NODEMASK_FREE(nodes_allowed);
1550adbe8726SEric B Munson 	return err;
155106808b08SLee Schermerhorn }
155206808b08SLee Schermerhorn 
155306808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj,
155406808b08SLee Schermerhorn 				       struct kobj_attribute *attr, char *buf)
155506808b08SLee Schermerhorn {
155606808b08SLee Schermerhorn 	return nr_hugepages_show_common(kobj, attr, buf);
155706808b08SLee Schermerhorn }
155806808b08SLee Schermerhorn 
155906808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj,
156006808b08SLee Schermerhorn 	       struct kobj_attribute *attr, const char *buf, size_t len)
156106808b08SLee Schermerhorn {
156206808b08SLee Schermerhorn 	return nr_hugepages_store_common(false, kobj, attr, buf, len);
1563a3437870SNishanth Aravamudan }
1564a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages);
1565a3437870SNishanth Aravamudan 
156606808b08SLee Schermerhorn #ifdef CONFIG_NUMA
156706808b08SLee Schermerhorn 
156806808b08SLee Schermerhorn /*
156906808b08SLee Schermerhorn  * hstate attribute for optionally mempolicy-based constraint on persistent
157006808b08SLee Schermerhorn  * huge page alloc/free.
157106808b08SLee Schermerhorn  */
157206808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
157306808b08SLee Schermerhorn 				       struct kobj_attribute *attr, char *buf)
157406808b08SLee Schermerhorn {
157506808b08SLee Schermerhorn 	return nr_hugepages_show_common(kobj, attr, buf);
157606808b08SLee Schermerhorn }
157706808b08SLee Schermerhorn 
157806808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
157906808b08SLee Schermerhorn 	       struct kobj_attribute *attr, const char *buf, size_t len)
158006808b08SLee Schermerhorn {
158106808b08SLee Schermerhorn 	return nr_hugepages_store_common(true, kobj, attr, buf, len);
158206808b08SLee Schermerhorn }
158306808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy);
158406808b08SLee Schermerhorn #endif
158506808b08SLee Schermerhorn 
158606808b08SLee Schermerhorn 
1587a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
1588a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1589a3437870SNishanth Aravamudan {
15909a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1591a3437870SNishanth Aravamudan 	return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
1592a3437870SNishanth Aravamudan }
1593adbe8726SEric B Munson 
1594a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
1595a3437870SNishanth Aravamudan 		struct kobj_attribute *attr, const char *buf, size_t count)
1596a3437870SNishanth Aravamudan {
1597a3437870SNishanth Aravamudan 	int err;
1598a3437870SNishanth Aravamudan 	unsigned long input;
15999a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1600a3437870SNishanth Aravamudan 
1601adbe8726SEric B Munson 	if (h->order >= MAX_ORDER)
1602adbe8726SEric B Munson 		return -EINVAL;
1603adbe8726SEric B Munson 
1604a3437870SNishanth Aravamudan 	err = strict_strtoul(buf, 10, &input);
1605a3437870SNishanth Aravamudan 	if (err)
160673ae31e5SEric B Munson 		return err;
1607a3437870SNishanth Aravamudan 
1608a3437870SNishanth Aravamudan 	spin_lock(&hugetlb_lock);
1609a3437870SNishanth Aravamudan 	h->nr_overcommit_huge_pages = input;
1610a3437870SNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
1611a3437870SNishanth Aravamudan 
1612a3437870SNishanth Aravamudan 	return count;
1613a3437870SNishanth Aravamudan }
1614a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages);
1615a3437870SNishanth Aravamudan 
1616a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj,
1617a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1618a3437870SNishanth Aravamudan {
16199a305230SLee Schermerhorn 	struct hstate *h;
16209a305230SLee Schermerhorn 	unsigned long free_huge_pages;
16219a305230SLee Schermerhorn 	int nid;
16229a305230SLee Schermerhorn 
16239a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
16249a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
16259a305230SLee Schermerhorn 		free_huge_pages = h->free_huge_pages;
16269a305230SLee Schermerhorn 	else
16279a305230SLee Schermerhorn 		free_huge_pages = h->free_huge_pages_node[nid];
16289a305230SLee Schermerhorn 
16299a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", free_huge_pages);
1630a3437870SNishanth Aravamudan }
1631a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages);
1632a3437870SNishanth Aravamudan 
1633a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj,
1634a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1635a3437870SNishanth Aravamudan {
16369a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1637a3437870SNishanth Aravamudan 	return sprintf(buf, "%lu\n", h->resv_huge_pages);
1638a3437870SNishanth Aravamudan }
1639a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages);
1640a3437870SNishanth Aravamudan 
1641a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj,
1642a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1643a3437870SNishanth Aravamudan {
16449a305230SLee Schermerhorn 	struct hstate *h;
16459a305230SLee Schermerhorn 	unsigned long surplus_huge_pages;
16469a305230SLee Schermerhorn 	int nid;
16479a305230SLee Schermerhorn 
16489a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
16499a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
16509a305230SLee Schermerhorn 		surplus_huge_pages = h->surplus_huge_pages;
16519a305230SLee Schermerhorn 	else
16529a305230SLee Schermerhorn 		surplus_huge_pages = h->surplus_huge_pages_node[nid];
16539a305230SLee Schermerhorn 
16549a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", surplus_huge_pages);
1655a3437870SNishanth Aravamudan }
1656a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages);
1657a3437870SNishanth Aravamudan 
1658a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = {
1659a3437870SNishanth Aravamudan 	&nr_hugepages_attr.attr,
1660a3437870SNishanth Aravamudan 	&nr_overcommit_hugepages_attr.attr,
1661a3437870SNishanth Aravamudan 	&free_hugepages_attr.attr,
1662a3437870SNishanth Aravamudan 	&resv_hugepages_attr.attr,
1663a3437870SNishanth Aravamudan 	&surplus_hugepages_attr.attr,
166406808b08SLee Schermerhorn #ifdef CONFIG_NUMA
166506808b08SLee Schermerhorn 	&nr_hugepages_mempolicy_attr.attr,
166606808b08SLee Schermerhorn #endif
1667a3437870SNishanth Aravamudan 	NULL,
1668a3437870SNishanth Aravamudan };
1669a3437870SNishanth Aravamudan 
1670a3437870SNishanth Aravamudan static struct attribute_group hstate_attr_group = {
1671a3437870SNishanth Aravamudan 	.attrs = hstate_attrs,
1672a3437870SNishanth Aravamudan };
1673a3437870SNishanth Aravamudan 
1674094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
16759a305230SLee Schermerhorn 				    struct kobject **hstate_kobjs,
16769a305230SLee Schermerhorn 				    struct attribute_group *hstate_attr_group)
1677a3437870SNishanth Aravamudan {
1678a3437870SNishanth Aravamudan 	int retval;
1679972dc4deSAneesh Kumar K.V 	int hi = hstate_index(h);
1680a3437870SNishanth Aravamudan 
16819a305230SLee Schermerhorn 	hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
16829a305230SLee Schermerhorn 	if (!hstate_kobjs[hi])
1683a3437870SNishanth Aravamudan 		return -ENOMEM;
1684a3437870SNishanth Aravamudan 
16859a305230SLee Schermerhorn 	retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
1686a3437870SNishanth Aravamudan 	if (retval)
16879a305230SLee Schermerhorn 		kobject_put(hstate_kobjs[hi]);
1688a3437870SNishanth Aravamudan 
1689a3437870SNishanth Aravamudan 	return retval;
1690a3437870SNishanth Aravamudan }
1691a3437870SNishanth Aravamudan 
1692a3437870SNishanth Aravamudan static void __init hugetlb_sysfs_init(void)
1693a3437870SNishanth Aravamudan {
1694a3437870SNishanth Aravamudan 	struct hstate *h;
1695a3437870SNishanth Aravamudan 	int err;
1696a3437870SNishanth Aravamudan 
1697a3437870SNishanth Aravamudan 	hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
1698a3437870SNishanth Aravamudan 	if (!hugepages_kobj)
1699a3437870SNishanth Aravamudan 		return;
1700a3437870SNishanth Aravamudan 
1701a3437870SNishanth Aravamudan 	for_each_hstate(h) {
17029a305230SLee Schermerhorn 		err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
17039a305230SLee Schermerhorn 					 hstate_kobjs, &hstate_attr_group);
1704a3437870SNishanth Aravamudan 		if (err)
1705a3437870SNishanth Aravamudan 			printk(KERN_ERR "Hugetlb: Unable to add hstate %s",
1706a3437870SNishanth Aravamudan 								h->name);
1707a3437870SNishanth Aravamudan 	}
1708a3437870SNishanth Aravamudan }
1709a3437870SNishanth Aravamudan 
17109a305230SLee Schermerhorn #ifdef CONFIG_NUMA
17119a305230SLee Schermerhorn 
17129a305230SLee Schermerhorn /*
17139a305230SLee Schermerhorn  * node_hstate/s - associate per node hstate attributes, via their kobjects,
171410fbcf4cSKay Sievers  * with node devices in node_devices[] using a parallel array.  The array
171510fbcf4cSKay Sievers  * index of a node device or _hstate == node id.
171610fbcf4cSKay Sievers  * This is here to avoid any static dependency of the node device driver, in
17179a305230SLee Schermerhorn  * the base kernel, on the hugetlb module.
17189a305230SLee Schermerhorn  */
17199a305230SLee Schermerhorn struct node_hstate {
17209a305230SLee Schermerhorn 	struct kobject		*hugepages_kobj;
17219a305230SLee Schermerhorn 	struct kobject		*hstate_kobjs[HUGE_MAX_HSTATE];
17229a305230SLee Schermerhorn };
17239a305230SLee Schermerhorn struct node_hstate node_hstates[MAX_NUMNODES];
17249a305230SLee Schermerhorn 
17259a305230SLee Schermerhorn /*
172610fbcf4cSKay Sievers  * A subset of global hstate attributes for node devices
17279a305230SLee Schermerhorn  */
17289a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = {
17299a305230SLee Schermerhorn 	&nr_hugepages_attr.attr,
17309a305230SLee Schermerhorn 	&free_hugepages_attr.attr,
17319a305230SLee Schermerhorn 	&surplus_hugepages_attr.attr,
17329a305230SLee Schermerhorn 	NULL,
17339a305230SLee Schermerhorn };
17349a305230SLee Schermerhorn 
17359a305230SLee Schermerhorn static struct attribute_group per_node_hstate_attr_group = {
17369a305230SLee Schermerhorn 	.attrs = per_node_hstate_attrs,
17379a305230SLee Schermerhorn };
17389a305230SLee Schermerhorn 
17399a305230SLee Schermerhorn /*
174010fbcf4cSKay Sievers  * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
17419a305230SLee Schermerhorn  * Returns node id via non-NULL nidp.
17429a305230SLee Schermerhorn  */
17439a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
17449a305230SLee Schermerhorn {
17459a305230SLee Schermerhorn 	int nid;
17469a305230SLee Schermerhorn 
17479a305230SLee Schermerhorn 	for (nid = 0; nid < nr_node_ids; nid++) {
17489a305230SLee Schermerhorn 		struct node_hstate *nhs = &node_hstates[nid];
17499a305230SLee Schermerhorn 		int i;
17509a305230SLee Schermerhorn 		for (i = 0; i < HUGE_MAX_HSTATE; i++)
17519a305230SLee Schermerhorn 			if (nhs->hstate_kobjs[i] == kobj) {
17529a305230SLee Schermerhorn 				if (nidp)
17539a305230SLee Schermerhorn 					*nidp = nid;
17549a305230SLee Schermerhorn 				return &hstates[i];
17559a305230SLee Schermerhorn 			}
17569a305230SLee Schermerhorn 	}
17579a305230SLee Schermerhorn 
17589a305230SLee Schermerhorn 	BUG();
17599a305230SLee Schermerhorn 	return NULL;
17609a305230SLee Schermerhorn }
17619a305230SLee Schermerhorn 
17629a305230SLee Schermerhorn /*
176310fbcf4cSKay Sievers  * Unregister hstate attributes from a single node device.
17649a305230SLee Schermerhorn  * No-op if no hstate attributes attached.
17659a305230SLee Schermerhorn  */
17669a305230SLee Schermerhorn void hugetlb_unregister_node(struct node *node)
17679a305230SLee Schermerhorn {
17689a305230SLee Schermerhorn 	struct hstate *h;
176910fbcf4cSKay Sievers 	struct node_hstate *nhs = &node_hstates[node->dev.id];
17709a305230SLee Schermerhorn 
17719a305230SLee Schermerhorn 	if (!nhs->hugepages_kobj)
17729b5e5d0fSLee Schermerhorn 		return;		/* no hstate attributes */
17739a305230SLee Schermerhorn 
1774972dc4deSAneesh Kumar K.V 	for_each_hstate(h) {
1775972dc4deSAneesh Kumar K.V 		int idx = hstate_index(h);
1776972dc4deSAneesh Kumar K.V 		if (nhs->hstate_kobjs[idx]) {
1777972dc4deSAneesh Kumar K.V 			kobject_put(nhs->hstate_kobjs[idx]);
1778972dc4deSAneesh Kumar K.V 			nhs->hstate_kobjs[idx] = NULL;
1779972dc4deSAneesh Kumar K.V 		}
17809a305230SLee Schermerhorn 	}
17819a305230SLee Schermerhorn 
17829a305230SLee Schermerhorn 	kobject_put(nhs->hugepages_kobj);
17839a305230SLee Schermerhorn 	nhs->hugepages_kobj = NULL;
17849a305230SLee Schermerhorn }
17859a305230SLee Schermerhorn 
17869a305230SLee Schermerhorn /*
178710fbcf4cSKay Sievers  * hugetlb module exit:  unregister hstate attributes from node devices
17889a305230SLee Schermerhorn  * that have them.
17899a305230SLee Schermerhorn  */
17909a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void)
17919a305230SLee Schermerhorn {
17929a305230SLee Schermerhorn 	int nid;
17939a305230SLee Schermerhorn 
17949a305230SLee Schermerhorn 	/*
179510fbcf4cSKay Sievers 	 * disable node device registrations.
17969a305230SLee Schermerhorn 	 */
17979a305230SLee Schermerhorn 	register_hugetlbfs_with_node(NULL, NULL);
17989a305230SLee Schermerhorn 
17999a305230SLee Schermerhorn 	/*
18009a305230SLee Schermerhorn 	 * remove hstate attributes from any nodes that have them.
18019a305230SLee Schermerhorn 	 */
18029a305230SLee Schermerhorn 	for (nid = 0; nid < nr_node_ids; nid++)
18039a305230SLee Schermerhorn 		hugetlb_unregister_node(&node_devices[nid]);
18049a305230SLee Schermerhorn }
18059a305230SLee Schermerhorn 
18069a305230SLee Schermerhorn /*
180710fbcf4cSKay Sievers  * Register hstate attributes for a single node device.
18089a305230SLee Schermerhorn  * No-op if attributes already registered.
18099a305230SLee Schermerhorn  */
18109a305230SLee Schermerhorn void hugetlb_register_node(struct node *node)
18119a305230SLee Schermerhorn {
18129a305230SLee Schermerhorn 	struct hstate *h;
181310fbcf4cSKay Sievers 	struct node_hstate *nhs = &node_hstates[node->dev.id];
18149a305230SLee Schermerhorn 	int err;
18159a305230SLee Schermerhorn 
18169a305230SLee Schermerhorn 	if (nhs->hugepages_kobj)
18179a305230SLee Schermerhorn 		return;		/* already allocated */
18189a305230SLee Schermerhorn 
18199a305230SLee Schermerhorn 	nhs->hugepages_kobj = kobject_create_and_add("hugepages",
182010fbcf4cSKay Sievers 							&node->dev.kobj);
18219a305230SLee Schermerhorn 	if (!nhs->hugepages_kobj)
18229a305230SLee Schermerhorn 		return;
18239a305230SLee Schermerhorn 
18249a305230SLee Schermerhorn 	for_each_hstate(h) {
18259a305230SLee Schermerhorn 		err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
18269a305230SLee Schermerhorn 						nhs->hstate_kobjs,
18279a305230SLee Schermerhorn 						&per_node_hstate_attr_group);
18289a305230SLee Schermerhorn 		if (err) {
18299a305230SLee Schermerhorn 			printk(KERN_ERR "Hugetlb: Unable to add hstate %s"
18309a305230SLee Schermerhorn 					" for node %d\n",
183110fbcf4cSKay Sievers 						h->name, node->dev.id);
18329a305230SLee Schermerhorn 			hugetlb_unregister_node(node);
18339a305230SLee Schermerhorn 			break;
18349a305230SLee Schermerhorn 		}
18359a305230SLee Schermerhorn 	}
18369a305230SLee Schermerhorn }
18379a305230SLee Schermerhorn 
18389a305230SLee Schermerhorn /*
18399b5e5d0fSLee Schermerhorn  * hugetlb init time:  register hstate attributes for all registered node
184010fbcf4cSKay Sievers  * devices of nodes that have memory.  All on-line nodes should have
184110fbcf4cSKay Sievers  * registered their associated device by this time.
18429a305230SLee Schermerhorn  */
18439a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void)
18449a305230SLee Schermerhorn {
18459a305230SLee Schermerhorn 	int nid;
18469a305230SLee Schermerhorn 
18479b5e5d0fSLee Schermerhorn 	for_each_node_state(nid, N_HIGH_MEMORY) {
18489a305230SLee Schermerhorn 		struct node *node = &node_devices[nid];
184910fbcf4cSKay Sievers 		if (node->dev.id == nid)
18509a305230SLee Schermerhorn 			hugetlb_register_node(node);
18519a305230SLee Schermerhorn 	}
18529a305230SLee Schermerhorn 
18539a305230SLee Schermerhorn 	/*
185410fbcf4cSKay Sievers 	 * Let the node device driver know we're here so it can
18559a305230SLee Schermerhorn 	 * [un]register hstate attributes on node hotplug.
18569a305230SLee Schermerhorn 	 */
18579a305230SLee Schermerhorn 	register_hugetlbfs_with_node(hugetlb_register_node,
18589a305230SLee Schermerhorn 				     hugetlb_unregister_node);
18599a305230SLee Schermerhorn }
18609a305230SLee Schermerhorn #else	/* !CONFIG_NUMA */
18619a305230SLee Schermerhorn 
18629a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
18639a305230SLee Schermerhorn {
18649a305230SLee Schermerhorn 	BUG();
18659a305230SLee Schermerhorn 	if (nidp)
18669a305230SLee Schermerhorn 		*nidp = -1;
18679a305230SLee Schermerhorn 	return NULL;
18689a305230SLee Schermerhorn }
18699a305230SLee Schermerhorn 
18709a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) { }
18719a305230SLee Schermerhorn 
18729a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { }
18739a305230SLee Schermerhorn 
18749a305230SLee Schermerhorn #endif
18759a305230SLee Schermerhorn 
1876a3437870SNishanth Aravamudan static void __exit hugetlb_exit(void)
1877a3437870SNishanth Aravamudan {
1878a3437870SNishanth Aravamudan 	struct hstate *h;
1879a3437870SNishanth Aravamudan 
18809a305230SLee Schermerhorn 	hugetlb_unregister_all_nodes();
18819a305230SLee Schermerhorn 
1882a3437870SNishanth Aravamudan 	for_each_hstate(h) {
1883972dc4deSAneesh Kumar K.V 		kobject_put(hstate_kobjs[hstate_index(h)]);
1884a3437870SNishanth Aravamudan 	}
1885a3437870SNishanth Aravamudan 
1886a3437870SNishanth Aravamudan 	kobject_put(hugepages_kobj);
1887a3437870SNishanth Aravamudan }
1888a3437870SNishanth Aravamudan module_exit(hugetlb_exit);
1889a3437870SNishanth Aravamudan 
1890a3437870SNishanth Aravamudan static int __init hugetlb_init(void)
1891a3437870SNishanth Aravamudan {
18920ef89d25SBenjamin Herrenschmidt 	/* Some platform decide whether they support huge pages at boot
18930ef89d25SBenjamin Herrenschmidt 	 * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
18940ef89d25SBenjamin Herrenschmidt 	 * there is no such support
18950ef89d25SBenjamin Herrenschmidt 	 */
18960ef89d25SBenjamin Herrenschmidt 	if (HPAGE_SHIFT == 0)
18970ef89d25SBenjamin Herrenschmidt 		return 0;
1898a3437870SNishanth Aravamudan 
1899e11bfbfcSNick Piggin 	if (!size_to_hstate(default_hstate_size)) {
1900e11bfbfcSNick Piggin 		default_hstate_size = HPAGE_SIZE;
1901e11bfbfcSNick Piggin 		if (!size_to_hstate(default_hstate_size))
1902a3437870SNishanth Aravamudan 			hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
1903a3437870SNishanth Aravamudan 	}
1904972dc4deSAneesh Kumar K.V 	default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
1905e11bfbfcSNick Piggin 	if (default_hstate_max_huge_pages)
1906e11bfbfcSNick Piggin 		default_hstate.max_huge_pages = default_hstate_max_huge_pages;
1907a3437870SNishanth Aravamudan 
1908a3437870SNishanth Aravamudan 	hugetlb_init_hstates();
1909a3437870SNishanth Aravamudan 
1910aa888a74SAndi Kleen 	gather_bootmem_prealloc();
1911aa888a74SAndi Kleen 
1912a3437870SNishanth Aravamudan 	report_hugepages();
1913a3437870SNishanth Aravamudan 
1914a3437870SNishanth Aravamudan 	hugetlb_sysfs_init();
1915a3437870SNishanth Aravamudan 
19169a305230SLee Schermerhorn 	hugetlb_register_all_nodes();
19179a305230SLee Schermerhorn 
1918a3437870SNishanth Aravamudan 	return 0;
1919a3437870SNishanth Aravamudan }
1920a3437870SNishanth Aravamudan module_init(hugetlb_init);
1921a3437870SNishanth Aravamudan 
1922a3437870SNishanth Aravamudan /* Should be called on processing a hugepagesz=... option */
1923a3437870SNishanth Aravamudan void __init hugetlb_add_hstate(unsigned order)
1924a3437870SNishanth Aravamudan {
1925a3437870SNishanth Aravamudan 	struct hstate *h;
19268faa8b07SAndi Kleen 	unsigned long i;
19278faa8b07SAndi Kleen 
1928a3437870SNishanth Aravamudan 	if (size_to_hstate(PAGE_SIZE << order)) {
1929a3437870SNishanth Aravamudan 		printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
1930a3437870SNishanth Aravamudan 		return;
1931a3437870SNishanth Aravamudan 	}
193247d38344SAneesh Kumar K.V 	BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
1933a3437870SNishanth Aravamudan 	BUG_ON(order == 0);
193447d38344SAneesh Kumar K.V 	h = &hstates[hugetlb_max_hstate++];
1935a3437870SNishanth Aravamudan 	h->order = order;
1936a3437870SNishanth Aravamudan 	h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
19378faa8b07SAndi Kleen 	h->nr_huge_pages = 0;
19388faa8b07SAndi Kleen 	h->free_huge_pages = 0;
19398faa8b07SAndi Kleen 	for (i = 0; i < MAX_NUMNODES; ++i)
19408faa8b07SAndi Kleen 		INIT_LIST_HEAD(&h->hugepage_freelists[i]);
19410edaecfaSAneesh Kumar K.V 	INIT_LIST_HEAD(&h->hugepage_activelist);
19429b5e5d0fSLee Schermerhorn 	h->next_nid_to_alloc = first_node(node_states[N_HIGH_MEMORY]);
19439b5e5d0fSLee Schermerhorn 	h->next_nid_to_free = first_node(node_states[N_HIGH_MEMORY]);
1944a3437870SNishanth Aravamudan 	snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
1945a3437870SNishanth Aravamudan 					huge_page_size(h)/1024);
1946abb8206cSAneesh Kumar K.V 	/*
1947abb8206cSAneesh Kumar K.V 	 * Add cgroup control files only if the huge page consists
1948abb8206cSAneesh Kumar K.V 	 * of more than two normal pages. This is because we use
1949abb8206cSAneesh Kumar K.V 	 * page[2].lru.next for storing cgoup details.
1950abb8206cSAneesh Kumar K.V 	 */
1951abb8206cSAneesh Kumar K.V 	if (order >= HUGETLB_CGROUP_MIN_ORDER)
1952abb8206cSAneesh Kumar K.V 		hugetlb_cgroup_file_init(hugetlb_max_hstate - 1);
19538faa8b07SAndi Kleen 
1954a3437870SNishanth Aravamudan 	parsed_hstate = h;
1955a3437870SNishanth Aravamudan }
1956a3437870SNishanth Aravamudan 
1957e11bfbfcSNick Piggin static int __init hugetlb_nrpages_setup(char *s)
1958a3437870SNishanth Aravamudan {
1959a3437870SNishanth Aravamudan 	unsigned long *mhp;
19608faa8b07SAndi Kleen 	static unsigned long *last_mhp;
1961a3437870SNishanth Aravamudan 
1962a3437870SNishanth Aravamudan 	/*
196347d38344SAneesh Kumar K.V 	 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
1964a3437870SNishanth Aravamudan 	 * so this hugepages= parameter goes to the "default hstate".
1965a3437870SNishanth Aravamudan 	 */
196647d38344SAneesh Kumar K.V 	if (!hugetlb_max_hstate)
1967a3437870SNishanth Aravamudan 		mhp = &default_hstate_max_huge_pages;
1968a3437870SNishanth Aravamudan 	else
1969a3437870SNishanth Aravamudan 		mhp = &parsed_hstate->max_huge_pages;
1970a3437870SNishanth Aravamudan 
19718faa8b07SAndi Kleen 	if (mhp == last_mhp) {
19728faa8b07SAndi Kleen 		printk(KERN_WARNING "hugepages= specified twice without "
19738faa8b07SAndi Kleen 			"interleaving hugepagesz=, ignoring\n");
19748faa8b07SAndi Kleen 		return 1;
19758faa8b07SAndi Kleen 	}
19768faa8b07SAndi Kleen 
1977a3437870SNishanth Aravamudan 	if (sscanf(s, "%lu", mhp) <= 0)
1978a3437870SNishanth Aravamudan 		*mhp = 0;
1979a3437870SNishanth Aravamudan 
19808faa8b07SAndi Kleen 	/*
19818faa8b07SAndi Kleen 	 * Global state is always initialized later in hugetlb_init.
19828faa8b07SAndi Kleen 	 * But we need to allocate >= MAX_ORDER hstates here early to still
19838faa8b07SAndi Kleen 	 * use the bootmem allocator.
19848faa8b07SAndi Kleen 	 */
198547d38344SAneesh Kumar K.V 	if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
19868faa8b07SAndi Kleen 		hugetlb_hstate_alloc_pages(parsed_hstate);
19878faa8b07SAndi Kleen 
19888faa8b07SAndi Kleen 	last_mhp = mhp;
19898faa8b07SAndi Kleen 
1990a3437870SNishanth Aravamudan 	return 1;
1991a3437870SNishanth Aravamudan }
1992e11bfbfcSNick Piggin __setup("hugepages=", hugetlb_nrpages_setup);
1993e11bfbfcSNick Piggin 
1994e11bfbfcSNick Piggin static int __init hugetlb_default_setup(char *s)
1995e11bfbfcSNick Piggin {
1996e11bfbfcSNick Piggin 	default_hstate_size = memparse(s, &s);
1997e11bfbfcSNick Piggin 	return 1;
1998e11bfbfcSNick Piggin }
1999e11bfbfcSNick Piggin __setup("default_hugepagesz=", hugetlb_default_setup);
2000a3437870SNishanth Aravamudan 
20018a213460SNishanth Aravamudan static unsigned int cpuset_mems_nr(unsigned int *array)
20028a213460SNishanth Aravamudan {
20038a213460SNishanth Aravamudan 	int node;
20048a213460SNishanth Aravamudan 	unsigned int nr = 0;
20058a213460SNishanth Aravamudan 
20068a213460SNishanth Aravamudan 	for_each_node_mask(node, cpuset_current_mems_allowed)
20078a213460SNishanth Aravamudan 		nr += array[node];
20088a213460SNishanth Aravamudan 
20098a213460SNishanth Aravamudan 	return nr;
20108a213460SNishanth Aravamudan }
20118a213460SNishanth Aravamudan 
20128a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL
201306808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
201406808b08SLee Schermerhorn 			 struct ctl_table *table, int write,
201506808b08SLee Schermerhorn 			 void __user *buffer, size_t *length, loff_t *ppos)
20161da177e4SLinus Torvalds {
2017e5ff2159SAndi Kleen 	struct hstate *h = &default_hstate;
2018e5ff2159SAndi Kleen 	unsigned long tmp;
201908d4a246SMichal Hocko 	int ret;
2020e5ff2159SAndi Kleen 
2021e5ff2159SAndi Kleen 	tmp = h->max_huge_pages;
2022e5ff2159SAndi Kleen 
2023adbe8726SEric B Munson 	if (write && h->order >= MAX_ORDER)
2024adbe8726SEric B Munson 		return -EINVAL;
2025adbe8726SEric B Munson 
2026e5ff2159SAndi Kleen 	table->data = &tmp;
2027e5ff2159SAndi Kleen 	table->maxlen = sizeof(unsigned long);
202808d4a246SMichal Hocko 	ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
202908d4a246SMichal Hocko 	if (ret)
203008d4a246SMichal Hocko 		goto out;
2031e5ff2159SAndi Kleen 
203206808b08SLee Schermerhorn 	if (write) {
2033bad44b5bSDavid Rientjes 		NODEMASK_ALLOC(nodemask_t, nodes_allowed,
2034bad44b5bSDavid Rientjes 						GFP_KERNEL | __GFP_NORETRY);
203506808b08SLee Schermerhorn 		if (!(obey_mempolicy &&
203606808b08SLee Schermerhorn 			       init_nodemask_of_mempolicy(nodes_allowed))) {
203706808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
203806808b08SLee Schermerhorn 			nodes_allowed = &node_states[N_HIGH_MEMORY];
203906808b08SLee Schermerhorn 		}
204006808b08SLee Schermerhorn 		h->max_huge_pages = set_max_huge_pages(h, tmp, nodes_allowed);
204106808b08SLee Schermerhorn 
204206808b08SLee Schermerhorn 		if (nodes_allowed != &node_states[N_HIGH_MEMORY])
204306808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
204406808b08SLee Schermerhorn 	}
204508d4a246SMichal Hocko out:
204608d4a246SMichal Hocko 	return ret;
20471da177e4SLinus Torvalds }
2048396faf03SMel Gorman 
204906808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write,
205006808b08SLee Schermerhorn 			  void __user *buffer, size_t *length, loff_t *ppos)
205106808b08SLee Schermerhorn {
205206808b08SLee Schermerhorn 
205306808b08SLee Schermerhorn 	return hugetlb_sysctl_handler_common(false, table, write,
205406808b08SLee Schermerhorn 							buffer, length, ppos);
205506808b08SLee Schermerhorn }
205606808b08SLee Schermerhorn 
205706808b08SLee Schermerhorn #ifdef CONFIG_NUMA
205806808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
205906808b08SLee Schermerhorn 			  void __user *buffer, size_t *length, loff_t *ppos)
206006808b08SLee Schermerhorn {
206106808b08SLee Schermerhorn 	return hugetlb_sysctl_handler_common(true, table, write,
206206808b08SLee Schermerhorn 							buffer, length, ppos);
206306808b08SLee Schermerhorn }
206406808b08SLee Schermerhorn #endif /* CONFIG_NUMA */
206506808b08SLee Schermerhorn 
2066396faf03SMel Gorman int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
20678d65af78SAlexey Dobriyan 			void __user *buffer,
2068396faf03SMel Gorman 			size_t *length, loff_t *ppos)
2069396faf03SMel Gorman {
20708d65af78SAlexey Dobriyan 	proc_dointvec(table, write, buffer, length, ppos);
2071396faf03SMel Gorman 	if (hugepages_treat_as_movable)
2072396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
2073396faf03SMel Gorman 	else
2074396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER;
2075396faf03SMel Gorman 	return 0;
2076396faf03SMel Gorman }
2077396faf03SMel Gorman 
2078a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write,
20798d65af78SAlexey Dobriyan 			void __user *buffer,
2080a3d0c6aaSNishanth Aravamudan 			size_t *length, loff_t *ppos)
2081a3d0c6aaSNishanth Aravamudan {
2082a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
2083e5ff2159SAndi Kleen 	unsigned long tmp;
208408d4a246SMichal Hocko 	int ret;
2085e5ff2159SAndi Kleen 
2086e5ff2159SAndi Kleen 	tmp = h->nr_overcommit_huge_pages;
2087e5ff2159SAndi Kleen 
2088adbe8726SEric B Munson 	if (write && h->order >= MAX_ORDER)
2089adbe8726SEric B Munson 		return -EINVAL;
2090adbe8726SEric B Munson 
2091e5ff2159SAndi Kleen 	table->data = &tmp;
2092e5ff2159SAndi Kleen 	table->maxlen = sizeof(unsigned long);
209308d4a246SMichal Hocko 	ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
209408d4a246SMichal Hocko 	if (ret)
209508d4a246SMichal Hocko 		goto out;
2096e5ff2159SAndi Kleen 
2097e5ff2159SAndi Kleen 	if (write) {
2098064d9efeSNishanth Aravamudan 		spin_lock(&hugetlb_lock);
2099e5ff2159SAndi Kleen 		h->nr_overcommit_huge_pages = tmp;
2100a3d0c6aaSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
2101e5ff2159SAndi Kleen 	}
210208d4a246SMichal Hocko out:
210308d4a246SMichal Hocko 	return ret;
2104a3d0c6aaSNishanth Aravamudan }
2105a3d0c6aaSNishanth Aravamudan 
21061da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */
21071da177e4SLinus Torvalds 
2108e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m)
21091da177e4SLinus Torvalds {
2110a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
2111e1759c21SAlexey Dobriyan 	seq_printf(m,
21121da177e4SLinus Torvalds 			"HugePages_Total:   %5lu\n"
21131da177e4SLinus Torvalds 			"HugePages_Free:    %5lu\n"
2114b45b5bd6SDavid Gibson 			"HugePages_Rsvd:    %5lu\n"
21157893d1d5SAdam Litke 			"HugePages_Surp:    %5lu\n"
21164f98a2feSRik van Riel 			"Hugepagesize:   %8lu kB\n",
2117a5516438SAndi Kleen 			h->nr_huge_pages,
2118a5516438SAndi Kleen 			h->free_huge_pages,
2119a5516438SAndi Kleen 			h->resv_huge_pages,
2120a5516438SAndi Kleen 			h->surplus_huge_pages,
2121a5516438SAndi Kleen 			1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
21221da177e4SLinus Torvalds }
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf)
21251da177e4SLinus Torvalds {
2126a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
21271da177e4SLinus Torvalds 	return sprintf(buf,
21281da177e4SLinus Torvalds 		"Node %d HugePages_Total: %5u\n"
2129a1de0919SNishanth Aravamudan 		"Node %d HugePages_Free:  %5u\n"
2130a1de0919SNishanth Aravamudan 		"Node %d HugePages_Surp:  %5u\n",
2131a5516438SAndi Kleen 		nid, h->nr_huge_pages_node[nid],
2132a5516438SAndi Kleen 		nid, h->free_huge_pages_node[nid],
2133a5516438SAndi Kleen 		nid, h->surplus_huge_pages_node[nid]);
21341da177e4SLinus Torvalds }
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
21371da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void)
21381da177e4SLinus Torvalds {
2139a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
2140a5516438SAndi Kleen 	return h->nr_huge_pages * pages_per_huge_page(h);
21411da177e4SLinus Torvalds }
21421da177e4SLinus Torvalds 
2143a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta)
2144fc1b8a73SMel Gorman {
2145fc1b8a73SMel Gorman 	int ret = -ENOMEM;
2146fc1b8a73SMel Gorman 
2147fc1b8a73SMel Gorman 	spin_lock(&hugetlb_lock);
2148fc1b8a73SMel Gorman 	/*
2149fc1b8a73SMel Gorman 	 * When cpuset is configured, it breaks the strict hugetlb page
2150fc1b8a73SMel Gorman 	 * reservation as the accounting is done on a global variable. Such
2151fc1b8a73SMel Gorman 	 * reservation is completely rubbish in the presence of cpuset because
2152fc1b8a73SMel Gorman 	 * the reservation is not checked against page availability for the
2153fc1b8a73SMel Gorman 	 * current cpuset. Application can still potentially OOM'ed by kernel
2154fc1b8a73SMel Gorman 	 * with lack of free htlb page in cpuset that the task is in.
2155fc1b8a73SMel Gorman 	 * Attempt to enforce strict accounting with cpuset is almost
2156fc1b8a73SMel Gorman 	 * impossible (or too ugly) because cpuset is too fluid that
2157fc1b8a73SMel Gorman 	 * task or memory node can be dynamically moved between cpusets.
2158fc1b8a73SMel Gorman 	 *
2159fc1b8a73SMel Gorman 	 * The change of semantics for shared hugetlb mapping with cpuset is
2160fc1b8a73SMel Gorman 	 * undesirable. However, in order to preserve some of the semantics,
2161fc1b8a73SMel Gorman 	 * we fall back to check against current free page availability as
2162fc1b8a73SMel Gorman 	 * a best attempt and hopefully to minimize the impact of changing
2163fc1b8a73SMel Gorman 	 * semantics that cpuset has.
2164fc1b8a73SMel Gorman 	 */
2165fc1b8a73SMel Gorman 	if (delta > 0) {
2166a5516438SAndi Kleen 		if (gather_surplus_pages(h, delta) < 0)
2167fc1b8a73SMel Gorman 			goto out;
2168fc1b8a73SMel Gorman 
2169a5516438SAndi Kleen 		if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
2170a5516438SAndi Kleen 			return_unused_surplus_pages(h, delta);
2171fc1b8a73SMel Gorman 			goto out;
2172fc1b8a73SMel Gorman 		}
2173fc1b8a73SMel Gorman 	}
2174fc1b8a73SMel Gorman 
2175fc1b8a73SMel Gorman 	ret = 0;
2176fc1b8a73SMel Gorman 	if (delta < 0)
2177a5516438SAndi Kleen 		return_unused_surplus_pages(h, (unsigned long) -delta);
2178fc1b8a73SMel Gorman 
2179fc1b8a73SMel Gorman out:
2180fc1b8a73SMel Gorman 	spin_unlock(&hugetlb_lock);
2181fc1b8a73SMel Gorman 	return ret;
2182fc1b8a73SMel Gorman }
2183fc1b8a73SMel Gorman 
218484afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma)
218584afd99bSAndy Whitcroft {
218684afd99bSAndy Whitcroft 	struct resv_map *reservations = vma_resv_map(vma);
218784afd99bSAndy Whitcroft 
218884afd99bSAndy Whitcroft 	/*
218984afd99bSAndy Whitcroft 	 * This new VMA should share its siblings reservation map if present.
219084afd99bSAndy Whitcroft 	 * The VMA will only ever have a valid reservation map pointer where
219184afd99bSAndy Whitcroft 	 * it is being copied for another still existing VMA.  As that VMA
219225985edcSLucas De Marchi 	 * has a reference to the reservation map it cannot disappear until
219384afd99bSAndy Whitcroft 	 * after this open call completes.  It is therefore safe to take a
219484afd99bSAndy Whitcroft 	 * new reference here without additional locking.
219584afd99bSAndy Whitcroft 	 */
219684afd99bSAndy Whitcroft 	if (reservations)
219784afd99bSAndy Whitcroft 		kref_get(&reservations->refs);
219884afd99bSAndy Whitcroft }
219984afd99bSAndy Whitcroft 
2200c50ac050SDave Hansen static void resv_map_put(struct vm_area_struct *vma)
2201c50ac050SDave Hansen {
2202c50ac050SDave Hansen 	struct resv_map *reservations = vma_resv_map(vma);
2203c50ac050SDave Hansen 
2204c50ac050SDave Hansen 	if (!reservations)
2205c50ac050SDave Hansen 		return;
2206c50ac050SDave Hansen 	kref_put(&reservations->refs, resv_map_release);
2207c50ac050SDave Hansen }
2208c50ac050SDave Hansen 
2209a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma)
2210a1e78772SMel Gorman {
2211a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
221284afd99bSAndy Whitcroft 	struct resv_map *reservations = vma_resv_map(vma);
221390481622SDavid Gibson 	struct hugepage_subpool *spool = subpool_vma(vma);
221484afd99bSAndy Whitcroft 	unsigned long reserve;
221584afd99bSAndy Whitcroft 	unsigned long start;
221684afd99bSAndy Whitcroft 	unsigned long end;
221784afd99bSAndy Whitcroft 
221884afd99bSAndy Whitcroft 	if (reservations) {
2219a5516438SAndi Kleen 		start = vma_hugecache_offset(h, vma, vma->vm_start);
2220a5516438SAndi Kleen 		end = vma_hugecache_offset(h, vma, vma->vm_end);
222184afd99bSAndy Whitcroft 
222284afd99bSAndy Whitcroft 		reserve = (end - start) -
222384afd99bSAndy Whitcroft 			region_count(&reservations->regions, start, end);
222484afd99bSAndy Whitcroft 
2225c50ac050SDave Hansen 		resv_map_put(vma);
222684afd99bSAndy Whitcroft 
22277251ff78SAdam Litke 		if (reserve) {
2228a5516438SAndi Kleen 			hugetlb_acct_memory(h, -reserve);
222990481622SDavid Gibson 			hugepage_subpool_put_pages(spool, reserve);
22307251ff78SAdam Litke 		}
2231a1e78772SMel Gorman 	}
223284afd99bSAndy Whitcroft }
2233a1e78772SMel Gorman 
22341da177e4SLinus Torvalds /*
22351da177e4SLinus Torvalds  * We cannot handle pagefaults against hugetlb pages at all.  They cause
22361da177e4SLinus Torvalds  * handle_mm_fault() to try to instantiate regular-sized pages in the
22371da177e4SLinus Torvalds  * hugegpage VMA.  do_page_fault() is supposed to trap this, so BUG is we get
22381da177e4SLinus Torvalds  * this far.
22391da177e4SLinus Torvalds  */
2240d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
22411da177e4SLinus Torvalds {
22421da177e4SLinus Torvalds 	BUG();
2243d0217ac0SNick Piggin 	return 0;
22441da177e4SLinus Torvalds }
22451da177e4SLinus Torvalds 
2246f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = {
2247d0217ac0SNick Piggin 	.fault = hugetlb_vm_op_fault,
224884afd99bSAndy Whitcroft 	.open = hugetlb_vm_op_open,
2249a1e78772SMel Gorman 	.close = hugetlb_vm_op_close,
22501da177e4SLinus Torvalds };
22511da177e4SLinus Torvalds 
22521e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
22531e8f889bSDavid Gibson 				int writable)
225463551ae0SDavid Gibson {
225563551ae0SDavid Gibson 	pte_t entry;
225663551ae0SDavid Gibson 
22571e8f889bSDavid Gibson 	if (writable) {
225863551ae0SDavid Gibson 		entry =
225963551ae0SDavid Gibson 		    pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
226063551ae0SDavid Gibson 	} else {
22617f2e9525SGerald Schaefer 		entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
226263551ae0SDavid Gibson 	}
226363551ae0SDavid Gibson 	entry = pte_mkyoung(entry);
226463551ae0SDavid Gibson 	entry = pte_mkhuge(entry);
2265d9ed9faaSChris Metcalf 	entry = arch_make_huge_pte(entry, vma, page, writable);
226663551ae0SDavid Gibson 
226763551ae0SDavid Gibson 	return entry;
226863551ae0SDavid Gibson }
226963551ae0SDavid Gibson 
22701e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma,
22711e8f889bSDavid Gibson 				   unsigned long address, pte_t *ptep)
22721e8f889bSDavid Gibson {
22731e8f889bSDavid Gibson 	pte_t entry;
22741e8f889bSDavid Gibson 
22757f2e9525SGerald Schaefer 	entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
227632f84528SChris Forbes 	if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
22774b3073e1SRussell King 		update_mmu_cache(vma, address, ptep);
22781e8f889bSDavid Gibson }
22791e8f889bSDavid Gibson 
22801e8f889bSDavid Gibson 
228163551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
228263551ae0SDavid Gibson 			    struct vm_area_struct *vma)
228363551ae0SDavid Gibson {
228463551ae0SDavid Gibson 	pte_t *src_pte, *dst_pte, entry;
228563551ae0SDavid Gibson 	struct page *ptepage;
22861c59827dSHugh Dickins 	unsigned long addr;
22871e8f889bSDavid Gibson 	int cow;
2288a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2289a5516438SAndi Kleen 	unsigned long sz = huge_page_size(h);
22901e8f889bSDavid Gibson 
22911e8f889bSDavid Gibson 	cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
229263551ae0SDavid Gibson 
2293a5516438SAndi Kleen 	for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
2294c74df32cSHugh Dickins 		src_pte = huge_pte_offset(src, addr);
2295c74df32cSHugh Dickins 		if (!src_pte)
2296c74df32cSHugh Dickins 			continue;
2297a5516438SAndi Kleen 		dst_pte = huge_pte_alloc(dst, addr, sz);
229863551ae0SDavid Gibson 		if (!dst_pte)
229963551ae0SDavid Gibson 			goto nomem;
2300c5c99429SLarry Woodman 
2301c5c99429SLarry Woodman 		/* If the pagetables are shared don't copy or take references */
2302c5c99429SLarry Woodman 		if (dst_pte == src_pte)
2303c5c99429SLarry Woodman 			continue;
2304c5c99429SLarry Woodman 
2305c74df32cSHugh Dickins 		spin_lock(&dst->page_table_lock);
230646478758SNick Piggin 		spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
23077f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(src_pte))) {
23081e8f889bSDavid Gibson 			if (cow)
23097f2e9525SGerald Schaefer 				huge_ptep_set_wrprotect(src, addr, src_pte);
23107f2e9525SGerald Schaefer 			entry = huge_ptep_get(src_pte);
231163551ae0SDavid Gibson 			ptepage = pte_page(entry);
231263551ae0SDavid Gibson 			get_page(ptepage);
23130fe6e20bSNaoya Horiguchi 			page_dup_rmap(ptepage);
231463551ae0SDavid Gibson 			set_huge_pte_at(dst, addr, dst_pte, entry);
23151c59827dSHugh Dickins 		}
23161c59827dSHugh Dickins 		spin_unlock(&src->page_table_lock);
2317c74df32cSHugh Dickins 		spin_unlock(&dst->page_table_lock);
231863551ae0SDavid Gibson 	}
231963551ae0SDavid Gibson 	return 0;
232063551ae0SDavid Gibson 
232163551ae0SDavid Gibson nomem:
232263551ae0SDavid Gibson 	return -ENOMEM;
232363551ae0SDavid Gibson }
232463551ae0SDavid Gibson 
2325290408d4SNaoya Horiguchi static int is_hugetlb_entry_migration(pte_t pte)
2326290408d4SNaoya Horiguchi {
2327290408d4SNaoya Horiguchi 	swp_entry_t swp;
2328290408d4SNaoya Horiguchi 
2329290408d4SNaoya Horiguchi 	if (huge_pte_none(pte) || pte_present(pte))
2330290408d4SNaoya Horiguchi 		return 0;
2331290408d4SNaoya Horiguchi 	swp = pte_to_swp_entry(pte);
233232f84528SChris Forbes 	if (non_swap_entry(swp) && is_migration_entry(swp))
2333290408d4SNaoya Horiguchi 		return 1;
233432f84528SChris Forbes 	else
2335290408d4SNaoya Horiguchi 		return 0;
2336290408d4SNaoya Horiguchi }
2337290408d4SNaoya Horiguchi 
2338fd6a03edSNaoya Horiguchi static int is_hugetlb_entry_hwpoisoned(pte_t pte)
2339fd6a03edSNaoya Horiguchi {
2340fd6a03edSNaoya Horiguchi 	swp_entry_t swp;
2341fd6a03edSNaoya Horiguchi 
2342fd6a03edSNaoya Horiguchi 	if (huge_pte_none(pte) || pte_present(pte))
2343fd6a03edSNaoya Horiguchi 		return 0;
2344fd6a03edSNaoya Horiguchi 	swp = pte_to_swp_entry(pte);
234532f84528SChris Forbes 	if (non_swap_entry(swp) && is_hwpoison_entry(swp))
2346fd6a03edSNaoya Horiguchi 		return 1;
234732f84528SChris Forbes 	else
2348fd6a03edSNaoya Horiguchi 		return 0;
2349fd6a03edSNaoya Horiguchi }
2350fd6a03edSNaoya Horiguchi 
235124669e58SAneesh Kumar K.V void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
235224669e58SAneesh Kumar K.V 			    unsigned long start, unsigned long end,
235324669e58SAneesh Kumar K.V 			    struct page *ref_page)
235463551ae0SDavid Gibson {
235524669e58SAneesh Kumar K.V 	int force_flush = 0;
235663551ae0SDavid Gibson 	struct mm_struct *mm = vma->vm_mm;
235763551ae0SDavid Gibson 	unsigned long address;
2358c7546f8fSDavid Gibson 	pte_t *ptep;
235963551ae0SDavid Gibson 	pte_t pte;
236063551ae0SDavid Gibson 	struct page *page;
2361a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2362a5516438SAndi Kleen 	unsigned long sz = huge_page_size(h);
23632ec74c3eSSagi Grimberg 	const unsigned long mmun_start = start;	/* For mmu_notifiers */
23642ec74c3eSSagi Grimberg 	const unsigned long mmun_end   = end;	/* For mmu_notifiers */
2365a5516438SAndi Kleen 
236663551ae0SDavid Gibson 	WARN_ON(!is_vm_hugetlb_page(vma));
2367a5516438SAndi Kleen 	BUG_ON(start & ~huge_page_mask(h));
2368a5516438SAndi Kleen 	BUG_ON(end & ~huge_page_mask(h));
236963551ae0SDavid Gibson 
237024669e58SAneesh Kumar K.V 	tlb_start_vma(tlb, vma);
23712ec74c3eSSagi Grimberg 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
237224669e58SAneesh Kumar K.V again:
2373508034a3SHugh Dickins 	spin_lock(&mm->page_table_lock);
2374a5516438SAndi Kleen 	for (address = start; address < end; address += sz) {
2375c7546f8fSDavid Gibson 		ptep = huge_pte_offset(mm, address);
2376c7546f8fSDavid Gibson 		if (!ptep)
2377c7546f8fSDavid Gibson 			continue;
2378c7546f8fSDavid Gibson 
237939dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
238039dde65cSChen, Kenneth W 			continue;
238139dde65cSChen, Kenneth W 
23826629326bSHillf Danton 		pte = huge_ptep_get(ptep);
23836629326bSHillf Danton 		if (huge_pte_none(pte))
23846629326bSHillf Danton 			continue;
23856629326bSHillf Danton 
23866629326bSHillf Danton 		/*
23876629326bSHillf Danton 		 * HWPoisoned hugepage is already unmapped and dropped reference
23886629326bSHillf Danton 		 */
23896629326bSHillf Danton 		if (unlikely(is_hugetlb_entry_hwpoisoned(pte)))
23906629326bSHillf Danton 			continue;
23916629326bSHillf Danton 
23926629326bSHillf Danton 		page = pte_page(pte);
239304f2cbe3SMel Gorman 		/*
239404f2cbe3SMel Gorman 		 * If a reference page is supplied, it is because a specific
239504f2cbe3SMel Gorman 		 * page is being unmapped, not a range. Ensure the page we
239604f2cbe3SMel Gorman 		 * are about to unmap is the actual page of interest.
239704f2cbe3SMel Gorman 		 */
239804f2cbe3SMel Gorman 		if (ref_page) {
239904f2cbe3SMel Gorman 			if (page != ref_page)
240004f2cbe3SMel Gorman 				continue;
240104f2cbe3SMel Gorman 
240204f2cbe3SMel Gorman 			/*
240304f2cbe3SMel Gorman 			 * Mark the VMA as having unmapped its page so that
240404f2cbe3SMel Gorman 			 * future faults in this VMA will fail rather than
240504f2cbe3SMel Gorman 			 * looking like data was lost
240604f2cbe3SMel Gorman 			 */
240704f2cbe3SMel Gorman 			set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
240804f2cbe3SMel Gorman 		}
240904f2cbe3SMel Gorman 
2410c7546f8fSDavid Gibson 		pte = huge_ptep_get_and_clear(mm, address, ptep);
241124669e58SAneesh Kumar K.V 		tlb_remove_tlb_entry(tlb, ptep, address);
24126649a386SKen Chen 		if (pte_dirty(pte))
24136649a386SKen Chen 			set_page_dirty(page);
24149e81130bSHillf Danton 
241524669e58SAneesh Kumar K.V 		page_remove_rmap(page);
241624669e58SAneesh Kumar K.V 		force_flush = !__tlb_remove_page(tlb, page);
241724669e58SAneesh Kumar K.V 		if (force_flush)
241824669e58SAneesh Kumar K.V 			break;
24199e81130bSHillf Danton 		/* Bail out after unmapping reference page if supplied */
24209e81130bSHillf Danton 		if (ref_page)
24219e81130bSHillf Danton 			break;
242263551ae0SDavid Gibson 	}
2423cd2934a3SAl Viro 	spin_unlock(&mm->page_table_lock);
242424669e58SAneesh Kumar K.V 	/*
242524669e58SAneesh Kumar K.V 	 * mmu_gather ran out of room to batch pages, we break out of
242624669e58SAneesh Kumar K.V 	 * the PTE lock to avoid doing the potential expensive TLB invalidate
242724669e58SAneesh Kumar K.V 	 * and page-free while holding it.
242824669e58SAneesh Kumar K.V 	 */
242924669e58SAneesh Kumar K.V 	if (force_flush) {
243024669e58SAneesh Kumar K.V 		force_flush = 0;
243124669e58SAneesh Kumar K.V 		tlb_flush_mmu(tlb);
243224669e58SAneesh Kumar K.V 		if (address < end && !ref_page)
243324669e58SAneesh Kumar K.V 			goto again;
2434fe1668aeSChen, Kenneth W 	}
24352ec74c3eSSagi Grimberg 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
243624669e58SAneesh Kumar K.V 	tlb_end_vma(tlb, vma);
24371da177e4SLinus Torvalds }
243863551ae0SDavid Gibson 
2439d833352aSMel Gorman void __unmap_hugepage_range_final(struct mmu_gather *tlb,
2440d833352aSMel Gorman 			  struct vm_area_struct *vma, unsigned long start,
2441d833352aSMel Gorman 			  unsigned long end, struct page *ref_page)
2442d833352aSMel Gorman {
2443d833352aSMel Gorman 	__unmap_hugepage_range(tlb, vma, start, end, ref_page);
2444d833352aSMel Gorman 
2445d833352aSMel Gorman 	/*
2446d833352aSMel Gorman 	 * Clear this flag so that x86's huge_pmd_share page_table_shareable
2447d833352aSMel Gorman 	 * test will fail on a vma being torn down, and not grab a page table
2448d833352aSMel Gorman 	 * on its way out.  We're lucky that the flag has such an appropriate
2449d833352aSMel Gorman 	 * name, and can in fact be safely cleared here. We could clear it
2450d833352aSMel Gorman 	 * before the __unmap_hugepage_range above, but all that's necessary
2451d833352aSMel Gorman 	 * is to clear it before releasing the i_mmap_mutex. This works
2452d833352aSMel Gorman 	 * because in the context this is called, the VMA is about to be
2453d833352aSMel Gorman 	 * destroyed and the i_mmap_mutex is held.
2454d833352aSMel Gorman 	 */
2455d833352aSMel Gorman 	vma->vm_flags &= ~VM_MAYSHARE;
2456d833352aSMel Gorman }
2457d833352aSMel Gorman 
2458502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
245904f2cbe3SMel Gorman 			  unsigned long end, struct page *ref_page)
2460502717f4SChen, Kenneth W {
246124669e58SAneesh Kumar K.V 	struct mm_struct *mm;
246224669e58SAneesh Kumar K.V 	struct mmu_gather tlb;
246324669e58SAneesh Kumar K.V 
246424669e58SAneesh Kumar K.V 	mm = vma->vm_mm;
246524669e58SAneesh Kumar K.V 
246624669e58SAneesh Kumar K.V 	tlb_gather_mmu(&tlb, mm, 0);
246724669e58SAneesh Kumar K.V 	__unmap_hugepage_range(&tlb, vma, start, end, ref_page);
246824669e58SAneesh Kumar K.V 	tlb_finish_mmu(&tlb, start, end);
2469502717f4SChen, Kenneth W }
2470502717f4SChen, Kenneth W 
247104f2cbe3SMel Gorman /*
247204f2cbe3SMel Gorman  * This is called when the original mapper is failing to COW a MAP_PRIVATE
247304f2cbe3SMel Gorman  * mappping it owns the reserve page for. The intention is to unmap the page
247404f2cbe3SMel Gorman  * from other VMAs and let the children be SIGKILLed if they are faulting the
247504f2cbe3SMel Gorman  * same region.
247604f2cbe3SMel Gorman  */
24772a4b3dedSHarvey Harrison static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
24782a4b3dedSHarvey Harrison 				struct page *page, unsigned long address)
247904f2cbe3SMel Gorman {
24807526674dSAdam Litke 	struct hstate *h = hstate_vma(vma);
248104f2cbe3SMel Gorman 	struct vm_area_struct *iter_vma;
248204f2cbe3SMel Gorman 	struct address_space *mapping;
248304f2cbe3SMel Gorman 	pgoff_t pgoff;
248404f2cbe3SMel Gorman 
248504f2cbe3SMel Gorman 	/*
248604f2cbe3SMel Gorman 	 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
248704f2cbe3SMel Gorman 	 * from page cache lookup which is in HPAGE_SIZE units.
248804f2cbe3SMel Gorman 	 */
24897526674dSAdam Litke 	address = address & huge_page_mask(h);
249036e4f20aSMichal Hocko 	pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
249136e4f20aSMichal Hocko 			vma->vm_pgoff;
249290481622SDavid Gibson 	mapping = vma->vm_file->f_dentry->d_inode->i_mapping;
249304f2cbe3SMel Gorman 
24944eb2b1dcSMel Gorman 	/*
24954eb2b1dcSMel Gorman 	 * Take the mapping lock for the duration of the table walk. As
24964eb2b1dcSMel Gorman 	 * this mapping should be shared between all the VMAs,
24974eb2b1dcSMel Gorman 	 * __unmap_hugepage_range() is called as the lock is already held
24984eb2b1dcSMel Gorman 	 */
24993d48ae45SPeter Zijlstra 	mutex_lock(&mapping->i_mmap_mutex);
25006b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
250104f2cbe3SMel Gorman 		/* Do not unmap the current VMA */
250204f2cbe3SMel Gorman 		if (iter_vma == vma)
250304f2cbe3SMel Gorman 			continue;
250404f2cbe3SMel Gorman 
250504f2cbe3SMel Gorman 		/*
250604f2cbe3SMel Gorman 		 * Unmap the page from other VMAs without their own reserves.
250704f2cbe3SMel Gorman 		 * They get marked to be SIGKILLed if they fault in these
250804f2cbe3SMel Gorman 		 * areas. This is because a future no-page fault on this VMA
250904f2cbe3SMel Gorman 		 * could insert a zeroed page instead of the data existing
251004f2cbe3SMel Gorman 		 * from the time of fork. This would look like data corruption
251104f2cbe3SMel Gorman 		 */
251204f2cbe3SMel Gorman 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
251324669e58SAneesh Kumar K.V 			unmap_hugepage_range(iter_vma, address,
251424669e58SAneesh Kumar K.V 					     address + huge_page_size(h), page);
251504f2cbe3SMel Gorman 	}
25163d48ae45SPeter Zijlstra 	mutex_unlock(&mapping->i_mmap_mutex);
251704f2cbe3SMel Gorman 
251804f2cbe3SMel Gorman 	return 1;
251904f2cbe3SMel Gorman }
252004f2cbe3SMel Gorman 
25210fe6e20bSNaoya Horiguchi /*
25220fe6e20bSNaoya Horiguchi  * Hugetlb_cow() should be called with page lock of the original hugepage held.
2523ef009b25SMichal Hocko  * Called with hugetlb_instantiation_mutex held and pte_page locked so we
2524ef009b25SMichal Hocko  * cannot race with other handlers or page migration.
2525ef009b25SMichal Hocko  * Keep the pte_same checks anyway to make transition from the mutex easier.
25260fe6e20bSNaoya Horiguchi  */
25271e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
252804f2cbe3SMel Gorman 			unsigned long address, pte_t *ptep, pte_t pte,
252904f2cbe3SMel Gorman 			struct page *pagecache_page)
25301e8f889bSDavid Gibson {
2531a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
25321e8f889bSDavid Gibson 	struct page *old_page, *new_page;
253379ac6ba4SDavid Gibson 	int avoidcopy;
253404f2cbe3SMel Gorman 	int outside_reserve = 0;
25352ec74c3eSSagi Grimberg 	unsigned long mmun_start;	/* For mmu_notifiers */
25362ec74c3eSSagi Grimberg 	unsigned long mmun_end;		/* For mmu_notifiers */
25371e8f889bSDavid Gibson 
25381e8f889bSDavid Gibson 	old_page = pte_page(pte);
25391e8f889bSDavid Gibson 
254004f2cbe3SMel Gorman retry_avoidcopy:
25411e8f889bSDavid Gibson 	/* If no-one else is actually using this page, avoid the copy
25421e8f889bSDavid Gibson 	 * and just make the page writable */
25430fe6e20bSNaoya Horiguchi 	avoidcopy = (page_mapcount(old_page) == 1);
25441e8f889bSDavid Gibson 	if (avoidcopy) {
25450fe6e20bSNaoya Horiguchi 		if (PageAnon(old_page))
25460fe6e20bSNaoya Horiguchi 			page_move_anon_rmap(old_page, vma, address);
25471e8f889bSDavid Gibson 		set_huge_ptep_writable(vma, address, ptep);
254883c54070SNick Piggin 		return 0;
25491e8f889bSDavid Gibson 	}
25501e8f889bSDavid Gibson 
255104f2cbe3SMel Gorman 	/*
255204f2cbe3SMel Gorman 	 * If the process that created a MAP_PRIVATE mapping is about to
255304f2cbe3SMel Gorman 	 * perform a COW due to a shared page count, attempt to satisfy
255404f2cbe3SMel Gorman 	 * the allocation without using the existing reserves. The pagecache
255504f2cbe3SMel Gorman 	 * page is used to determine if the reserve at this address was
255604f2cbe3SMel Gorman 	 * consumed or not. If reserves were used, a partial faulted mapping
255704f2cbe3SMel Gorman 	 * at the time of fork() could consume its reserves on COW instead
255804f2cbe3SMel Gorman 	 * of the full address range.
255904f2cbe3SMel Gorman 	 */
2560f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE) &&
256104f2cbe3SMel Gorman 			is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
256204f2cbe3SMel Gorman 			old_page != pagecache_page)
256304f2cbe3SMel Gorman 		outside_reserve = 1;
256404f2cbe3SMel Gorman 
25651e8f889bSDavid Gibson 	page_cache_get(old_page);
2566b76c8cfbSLarry Woodman 
2567b76c8cfbSLarry Woodman 	/* Drop page_table_lock as buddy allocator may be called */
2568b76c8cfbSLarry Woodman 	spin_unlock(&mm->page_table_lock);
256904f2cbe3SMel Gorman 	new_page = alloc_huge_page(vma, address, outside_reserve);
25701e8f889bSDavid Gibson 
25712fc39cecSAdam Litke 	if (IS_ERR(new_page)) {
257276dcee75SAneesh Kumar K.V 		long err = PTR_ERR(new_page);
25731e8f889bSDavid Gibson 		page_cache_release(old_page);
257404f2cbe3SMel Gorman 
257504f2cbe3SMel Gorman 		/*
257604f2cbe3SMel Gorman 		 * If a process owning a MAP_PRIVATE mapping fails to COW,
257704f2cbe3SMel Gorman 		 * it is due to references held by a child and an insufficient
257804f2cbe3SMel Gorman 		 * huge page pool. To guarantee the original mappers
257904f2cbe3SMel Gorman 		 * reliability, unmap the page from child processes. The child
258004f2cbe3SMel Gorman 		 * may get SIGKILLed if it later faults.
258104f2cbe3SMel Gorman 		 */
258204f2cbe3SMel Gorman 		if (outside_reserve) {
258304f2cbe3SMel Gorman 			BUG_ON(huge_pte_none(pte));
258404f2cbe3SMel Gorman 			if (unmap_ref_private(mm, vma, old_page, address)) {
258504f2cbe3SMel Gorman 				BUG_ON(huge_pte_none(pte));
2586b76c8cfbSLarry Woodman 				spin_lock(&mm->page_table_lock);
2587a734bcc8SHillf Danton 				ptep = huge_pte_offset(mm, address & huge_page_mask(h));
2588a734bcc8SHillf Danton 				if (likely(pte_same(huge_ptep_get(ptep), pte)))
258904f2cbe3SMel Gorman 					goto retry_avoidcopy;
2590a734bcc8SHillf Danton 				/*
2591a734bcc8SHillf Danton 				 * race occurs while re-acquiring page_table_lock, and
2592a734bcc8SHillf Danton 				 * our job is done.
2593a734bcc8SHillf Danton 				 */
2594a734bcc8SHillf Danton 				return 0;
259504f2cbe3SMel Gorman 			}
259604f2cbe3SMel Gorman 			WARN_ON_ONCE(1);
259704f2cbe3SMel Gorman 		}
259804f2cbe3SMel Gorman 
2599b76c8cfbSLarry Woodman 		/* Caller expects lock to be held */
2600b76c8cfbSLarry Woodman 		spin_lock(&mm->page_table_lock);
260176dcee75SAneesh Kumar K.V 		if (err == -ENOMEM)
260276dcee75SAneesh Kumar K.V 			return VM_FAULT_OOM;
260376dcee75SAneesh Kumar K.V 		else
260476dcee75SAneesh Kumar K.V 			return VM_FAULT_SIGBUS;
26051e8f889bSDavid Gibson 	}
26061e8f889bSDavid Gibson 
26070fe6e20bSNaoya Horiguchi 	/*
26080fe6e20bSNaoya Horiguchi 	 * When the original hugepage is shared one, it does not have
26090fe6e20bSNaoya Horiguchi 	 * anon_vma prepared.
26100fe6e20bSNaoya Horiguchi 	 */
261144e2aa93SDean Nelson 	if (unlikely(anon_vma_prepare(vma))) {
2612ea4039a3SHillf Danton 		page_cache_release(new_page);
2613ea4039a3SHillf Danton 		page_cache_release(old_page);
261444e2aa93SDean Nelson 		/* Caller expects lock to be held */
261544e2aa93SDean Nelson 		spin_lock(&mm->page_table_lock);
26160fe6e20bSNaoya Horiguchi 		return VM_FAULT_OOM;
261744e2aa93SDean Nelson 	}
26180fe6e20bSNaoya Horiguchi 
261947ad8475SAndrea Arcangeli 	copy_user_huge_page(new_page, old_page, address, vma,
262047ad8475SAndrea Arcangeli 			    pages_per_huge_page(h));
26210ed361deSNick Piggin 	__SetPageUptodate(new_page);
26221e8f889bSDavid Gibson 
26232ec74c3eSSagi Grimberg 	mmun_start = address & huge_page_mask(h);
26242ec74c3eSSagi Grimberg 	mmun_end = mmun_start + huge_page_size(h);
26252ec74c3eSSagi Grimberg 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2626b76c8cfbSLarry Woodman 	/*
2627b76c8cfbSLarry Woodman 	 * Retake the page_table_lock to check for racing updates
2628b76c8cfbSLarry Woodman 	 * before the page tables are altered
2629b76c8cfbSLarry Woodman 	 */
2630b76c8cfbSLarry Woodman 	spin_lock(&mm->page_table_lock);
2631a5516438SAndi Kleen 	ptep = huge_pte_offset(mm, address & huge_page_mask(h));
26327f2e9525SGerald Schaefer 	if (likely(pte_same(huge_ptep_get(ptep), pte))) {
26331e8f889bSDavid Gibson 		/* Break COW */
26348fe627ecSGerald Schaefer 		huge_ptep_clear_flush(vma, address, ptep);
26351e8f889bSDavid Gibson 		set_huge_pte_at(mm, address, ptep,
26361e8f889bSDavid Gibson 				make_huge_pte(vma, new_page, 1));
26370fe6e20bSNaoya Horiguchi 		page_remove_rmap(old_page);
2638cd67f0d2SNaoya Horiguchi 		hugepage_add_new_anon_rmap(new_page, vma, address);
26391e8f889bSDavid Gibson 		/* Make the old page be freed below */
26401e8f889bSDavid Gibson 		new_page = old_page;
26411e8f889bSDavid Gibson 	}
26422ec74c3eSSagi Grimberg 	spin_unlock(&mm->page_table_lock);
26432ec74c3eSSagi Grimberg 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
26442ec74c3eSSagi Grimberg 	/* Caller expects lock to be held */
26452ec74c3eSSagi Grimberg 	spin_lock(&mm->page_table_lock);
26461e8f889bSDavid Gibson 	page_cache_release(new_page);
26471e8f889bSDavid Gibson 	page_cache_release(old_page);
264883c54070SNick Piggin 	return 0;
26491e8f889bSDavid Gibson }
26501e8f889bSDavid Gibson 
265104f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */
2652a5516438SAndi Kleen static struct page *hugetlbfs_pagecache_page(struct hstate *h,
2653a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long address)
265404f2cbe3SMel Gorman {
265504f2cbe3SMel Gorman 	struct address_space *mapping;
2656e7c4b0bfSAndy Whitcroft 	pgoff_t idx;
265704f2cbe3SMel Gorman 
265804f2cbe3SMel Gorman 	mapping = vma->vm_file->f_mapping;
2659a5516438SAndi Kleen 	idx = vma_hugecache_offset(h, vma, address);
266004f2cbe3SMel Gorman 
266104f2cbe3SMel Gorman 	return find_lock_page(mapping, idx);
266204f2cbe3SMel Gorman }
266304f2cbe3SMel Gorman 
26643ae77f43SHugh Dickins /*
26653ae77f43SHugh Dickins  * Return whether there is a pagecache page to back given address within VMA.
26663ae77f43SHugh Dickins  * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
26673ae77f43SHugh Dickins  */
26683ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h,
26692a15efc9SHugh Dickins 			struct vm_area_struct *vma, unsigned long address)
26702a15efc9SHugh Dickins {
26712a15efc9SHugh Dickins 	struct address_space *mapping;
26722a15efc9SHugh Dickins 	pgoff_t idx;
26732a15efc9SHugh Dickins 	struct page *page;
26742a15efc9SHugh Dickins 
26752a15efc9SHugh Dickins 	mapping = vma->vm_file->f_mapping;
26762a15efc9SHugh Dickins 	idx = vma_hugecache_offset(h, vma, address);
26772a15efc9SHugh Dickins 
26782a15efc9SHugh Dickins 	page = find_get_page(mapping, idx);
26792a15efc9SHugh Dickins 	if (page)
26802a15efc9SHugh Dickins 		put_page(page);
26812a15efc9SHugh Dickins 	return page != NULL;
26822a15efc9SHugh Dickins }
26832a15efc9SHugh Dickins 
2684a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
2685788c7df4SHugh Dickins 			unsigned long address, pte_t *ptep, unsigned int flags)
2686ac9b9c66SHugh Dickins {
2687a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2688ac9b9c66SHugh Dickins 	int ret = VM_FAULT_SIGBUS;
2689409eb8c2SHillf Danton 	int anon_rmap = 0;
2690e7c4b0bfSAndy Whitcroft 	pgoff_t idx;
26914c887265SAdam Litke 	unsigned long size;
26924c887265SAdam Litke 	struct page *page;
26934c887265SAdam Litke 	struct address_space *mapping;
26941e8f889bSDavid Gibson 	pte_t new_pte;
26954c887265SAdam Litke 
269604f2cbe3SMel Gorman 	/*
269704f2cbe3SMel Gorman 	 * Currently, we are forced to kill the process in the event the
269804f2cbe3SMel Gorman 	 * original mapper has unmapped pages from the child due to a failed
269925985edcSLucas De Marchi 	 * COW. Warn that such a situation has occurred as it may not be obvious
270004f2cbe3SMel Gorman 	 */
270104f2cbe3SMel Gorman 	if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
270204f2cbe3SMel Gorman 		printk(KERN_WARNING
270304f2cbe3SMel Gorman 			"PID %d killed due to inadequate hugepage pool\n",
270404f2cbe3SMel Gorman 			current->pid);
270504f2cbe3SMel Gorman 		return ret;
270604f2cbe3SMel Gorman 	}
270704f2cbe3SMel Gorman 
27084c887265SAdam Litke 	mapping = vma->vm_file->f_mapping;
2709a5516438SAndi Kleen 	idx = vma_hugecache_offset(h, vma, address);
27104c887265SAdam Litke 
27114c887265SAdam Litke 	/*
27124c887265SAdam Litke 	 * Use page lock to guard against racing truncation
27134c887265SAdam Litke 	 * before we get page_table_lock.
27144c887265SAdam Litke 	 */
27156bda666aSChristoph Lameter retry:
27166bda666aSChristoph Lameter 	page = find_lock_page(mapping, idx);
27176bda666aSChristoph Lameter 	if (!page) {
2718a5516438SAndi Kleen 		size = i_size_read(mapping->host) >> huge_page_shift(h);
2719ebed4bfcSHugh Dickins 		if (idx >= size)
2720ebed4bfcSHugh Dickins 			goto out;
272104f2cbe3SMel Gorman 		page = alloc_huge_page(vma, address, 0);
27222fc39cecSAdam Litke 		if (IS_ERR(page)) {
272376dcee75SAneesh Kumar K.V 			ret = PTR_ERR(page);
272476dcee75SAneesh Kumar K.V 			if (ret == -ENOMEM)
272576dcee75SAneesh Kumar K.V 				ret = VM_FAULT_OOM;
272676dcee75SAneesh Kumar K.V 			else
272776dcee75SAneesh Kumar K.V 				ret = VM_FAULT_SIGBUS;
27286bda666aSChristoph Lameter 			goto out;
27296bda666aSChristoph Lameter 		}
273047ad8475SAndrea Arcangeli 		clear_huge_page(page, address, pages_per_huge_page(h));
27310ed361deSNick Piggin 		__SetPageUptodate(page);
2732ac9b9c66SHugh Dickins 
2733f83a275dSMel Gorman 		if (vma->vm_flags & VM_MAYSHARE) {
27346bda666aSChristoph Lameter 			int err;
273545c682a6SKen Chen 			struct inode *inode = mapping->host;
27366bda666aSChristoph Lameter 
27376bda666aSChristoph Lameter 			err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
27386bda666aSChristoph Lameter 			if (err) {
27396bda666aSChristoph Lameter 				put_page(page);
27406bda666aSChristoph Lameter 				if (err == -EEXIST)
27416bda666aSChristoph Lameter 					goto retry;
27426bda666aSChristoph Lameter 				goto out;
27436bda666aSChristoph Lameter 			}
274445c682a6SKen Chen 
274545c682a6SKen Chen 			spin_lock(&inode->i_lock);
2746a5516438SAndi Kleen 			inode->i_blocks += blocks_per_huge_page(h);
274745c682a6SKen Chen 			spin_unlock(&inode->i_lock);
274823be7468SMel Gorman 		} else {
27496bda666aSChristoph Lameter 			lock_page(page);
27500fe6e20bSNaoya Horiguchi 			if (unlikely(anon_vma_prepare(vma))) {
27510fe6e20bSNaoya Horiguchi 				ret = VM_FAULT_OOM;
27520fe6e20bSNaoya Horiguchi 				goto backout_unlocked;
275323be7468SMel Gorman 			}
2754409eb8c2SHillf Danton 			anon_rmap = 1;
27550fe6e20bSNaoya Horiguchi 		}
27560fe6e20bSNaoya Horiguchi 	} else {
275757303d80SAndy Whitcroft 		/*
2758998b4382SNaoya Horiguchi 		 * If memory error occurs between mmap() and fault, some process
2759998b4382SNaoya Horiguchi 		 * don't have hwpoisoned swap entry for errored virtual address.
2760998b4382SNaoya Horiguchi 		 * So we need to block hugepage fault by PG_hwpoison bit check.
2761fd6a03edSNaoya Horiguchi 		 */
2762fd6a03edSNaoya Horiguchi 		if (unlikely(PageHWPoison(page))) {
2763aa50d3a7SAndi Kleen 			ret = VM_FAULT_HWPOISON |
2764972dc4deSAneesh Kumar K.V 				VM_FAULT_SET_HINDEX(hstate_index(h));
2765fd6a03edSNaoya Horiguchi 			goto backout_unlocked;
27666bda666aSChristoph Lameter 		}
2767998b4382SNaoya Horiguchi 	}
27681e8f889bSDavid Gibson 
276957303d80SAndy Whitcroft 	/*
277057303d80SAndy Whitcroft 	 * If we are going to COW a private mapping later, we examine the
277157303d80SAndy Whitcroft 	 * pending reservations for this page now. This will ensure that
277257303d80SAndy Whitcroft 	 * any allocations necessary to record that reservation occur outside
277357303d80SAndy Whitcroft 	 * the spinlock.
277457303d80SAndy Whitcroft 	 */
2775788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
27762b26736cSAndy Whitcroft 		if (vma_needs_reservation(h, vma, address) < 0) {
27772b26736cSAndy Whitcroft 			ret = VM_FAULT_OOM;
27782b26736cSAndy Whitcroft 			goto backout_unlocked;
27792b26736cSAndy Whitcroft 		}
278057303d80SAndy Whitcroft 
2781ac9b9c66SHugh Dickins 	spin_lock(&mm->page_table_lock);
2782a5516438SAndi Kleen 	size = i_size_read(mapping->host) >> huge_page_shift(h);
27834c887265SAdam Litke 	if (idx >= size)
27844c887265SAdam Litke 		goto backout;
27854c887265SAdam Litke 
278683c54070SNick Piggin 	ret = 0;
27877f2e9525SGerald Schaefer 	if (!huge_pte_none(huge_ptep_get(ptep)))
27884c887265SAdam Litke 		goto backout;
27894c887265SAdam Litke 
2790409eb8c2SHillf Danton 	if (anon_rmap)
2791409eb8c2SHillf Danton 		hugepage_add_new_anon_rmap(page, vma, address);
2792409eb8c2SHillf Danton 	else
2793409eb8c2SHillf Danton 		page_dup_rmap(page);
27941e8f889bSDavid Gibson 	new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
27951e8f889bSDavid Gibson 				&& (vma->vm_flags & VM_SHARED)));
27961e8f889bSDavid Gibson 	set_huge_pte_at(mm, address, ptep, new_pte);
27971e8f889bSDavid Gibson 
2798788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
27991e8f889bSDavid Gibson 		/* Optimization, do the COW without a second fault */
280004f2cbe3SMel Gorman 		ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
28011e8f889bSDavid Gibson 	}
28021e8f889bSDavid Gibson 
2803ac9b9c66SHugh Dickins 	spin_unlock(&mm->page_table_lock);
28044c887265SAdam Litke 	unlock_page(page);
28054c887265SAdam Litke out:
2806ac9b9c66SHugh Dickins 	return ret;
28074c887265SAdam Litke 
28084c887265SAdam Litke backout:
28094c887265SAdam Litke 	spin_unlock(&mm->page_table_lock);
28102b26736cSAndy Whitcroft backout_unlocked:
28114c887265SAdam Litke 	unlock_page(page);
28124c887265SAdam Litke 	put_page(page);
28134c887265SAdam Litke 	goto out;
2814ac9b9c66SHugh Dickins }
2815ac9b9c66SHugh Dickins 
281686e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2817788c7df4SHugh Dickins 			unsigned long address, unsigned int flags)
281886e5216fSAdam Litke {
281986e5216fSAdam Litke 	pte_t *ptep;
282086e5216fSAdam Litke 	pte_t entry;
28211e8f889bSDavid Gibson 	int ret;
28220fe6e20bSNaoya Horiguchi 	struct page *page = NULL;
282357303d80SAndy Whitcroft 	struct page *pagecache_page = NULL;
28243935baa9SDavid Gibson 	static DEFINE_MUTEX(hugetlb_instantiation_mutex);
2825a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
282686e5216fSAdam Litke 
28271e16a539SKAMEZAWA Hiroyuki 	address &= huge_page_mask(h);
28281e16a539SKAMEZAWA Hiroyuki 
2829fd6a03edSNaoya Horiguchi 	ptep = huge_pte_offset(mm, address);
2830fd6a03edSNaoya Horiguchi 	if (ptep) {
2831fd6a03edSNaoya Horiguchi 		entry = huge_ptep_get(ptep);
2832290408d4SNaoya Horiguchi 		if (unlikely(is_hugetlb_entry_migration(entry))) {
2833290408d4SNaoya Horiguchi 			migration_entry_wait(mm, (pmd_t *)ptep, address);
2834290408d4SNaoya Horiguchi 			return 0;
2835290408d4SNaoya Horiguchi 		} else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
2836aa50d3a7SAndi Kleen 			return VM_FAULT_HWPOISON_LARGE |
2837972dc4deSAneesh Kumar K.V 				VM_FAULT_SET_HINDEX(hstate_index(h));
2838fd6a03edSNaoya Horiguchi 	}
2839fd6a03edSNaoya Horiguchi 
2840a5516438SAndi Kleen 	ptep = huge_pte_alloc(mm, address, huge_page_size(h));
284186e5216fSAdam Litke 	if (!ptep)
284286e5216fSAdam Litke 		return VM_FAULT_OOM;
284386e5216fSAdam Litke 
28443935baa9SDavid Gibson 	/*
28453935baa9SDavid Gibson 	 * Serialize hugepage allocation and instantiation, so that we don't
28463935baa9SDavid Gibson 	 * get spurious allocation failures if two CPUs race to instantiate
28473935baa9SDavid Gibson 	 * the same page in the page cache.
28483935baa9SDavid Gibson 	 */
28493935baa9SDavid Gibson 	mutex_lock(&hugetlb_instantiation_mutex);
28507f2e9525SGerald Schaefer 	entry = huge_ptep_get(ptep);
28517f2e9525SGerald Schaefer 	if (huge_pte_none(entry)) {
2852788c7df4SHugh Dickins 		ret = hugetlb_no_page(mm, vma, address, ptep, flags);
2853b4d1d99fSDavid Gibson 		goto out_mutex;
28543935baa9SDavid Gibson 	}
285586e5216fSAdam Litke 
285683c54070SNick Piggin 	ret = 0;
28571e8f889bSDavid Gibson 
285857303d80SAndy Whitcroft 	/*
285957303d80SAndy Whitcroft 	 * If we are going to COW the mapping later, we examine the pending
286057303d80SAndy Whitcroft 	 * reservations for this page now. This will ensure that any
286157303d80SAndy Whitcroft 	 * allocations necessary to record that reservation occur outside the
286257303d80SAndy Whitcroft 	 * spinlock. For private mappings, we also lookup the pagecache
286357303d80SAndy Whitcroft 	 * page now as it is used to determine if a reservation has been
286457303d80SAndy Whitcroft 	 * consumed.
286557303d80SAndy Whitcroft 	 */
2866788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !pte_write(entry)) {
28672b26736cSAndy Whitcroft 		if (vma_needs_reservation(h, vma, address) < 0) {
28682b26736cSAndy Whitcroft 			ret = VM_FAULT_OOM;
2869b4d1d99fSDavid Gibson 			goto out_mutex;
28702b26736cSAndy Whitcroft 		}
287157303d80SAndy Whitcroft 
2872f83a275dSMel Gorman 		if (!(vma->vm_flags & VM_MAYSHARE))
287357303d80SAndy Whitcroft 			pagecache_page = hugetlbfs_pagecache_page(h,
287457303d80SAndy Whitcroft 								vma, address);
287557303d80SAndy Whitcroft 	}
287657303d80SAndy Whitcroft 
287756c9cfb1SNaoya Horiguchi 	/*
287856c9cfb1SNaoya Horiguchi 	 * hugetlb_cow() requires page locks of pte_page(entry) and
287956c9cfb1SNaoya Horiguchi 	 * pagecache_page, so here we need take the former one
288056c9cfb1SNaoya Horiguchi 	 * when page != pagecache_page or !pagecache_page.
288156c9cfb1SNaoya Horiguchi 	 * Note that locking order is always pagecache_page -> page,
288256c9cfb1SNaoya Horiguchi 	 * so no worry about deadlock.
288356c9cfb1SNaoya Horiguchi 	 */
28840fe6e20bSNaoya Horiguchi 	page = pte_page(entry);
288566aebce7SChris Metcalf 	get_page(page);
288656c9cfb1SNaoya Horiguchi 	if (page != pagecache_page)
28870fe6e20bSNaoya Horiguchi 		lock_page(page);
28880fe6e20bSNaoya Horiguchi 
28891e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
28901e8f889bSDavid Gibson 	/* Check for a racing update before calling hugetlb_cow */
2891b4d1d99fSDavid Gibson 	if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
2892b4d1d99fSDavid Gibson 		goto out_page_table_lock;
2893b4d1d99fSDavid Gibson 
2894b4d1d99fSDavid Gibson 
2895788c7df4SHugh Dickins 	if (flags & FAULT_FLAG_WRITE) {
2896b4d1d99fSDavid Gibson 		if (!pte_write(entry)) {
289757303d80SAndy Whitcroft 			ret = hugetlb_cow(mm, vma, address, ptep, entry,
289857303d80SAndy Whitcroft 							pagecache_page);
2899b4d1d99fSDavid Gibson 			goto out_page_table_lock;
2900b4d1d99fSDavid Gibson 		}
2901b4d1d99fSDavid Gibson 		entry = pte_mkdirty(entry);
2902b4d1d99fSDavid Gibson 	}
2903b4d1d99fSDavid Gibson 	entry = pte_mkyoung(entry);
2904788c7df4SHugh Dickins 	if (huge_ptep_set_access_flags(vma, address, ptep, entry,
2905788c7df4SHugh Dickins 						flags & FAULT_FLAG_WRITE))
29064b3073e1SRussell King 		update_mmu_cache(vma, address, ptep);
2907b4d1d99fSDavid Gibson 
2908b4d1d99fSDavid Gibson out_page_table_lock:
29091e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
291057303d80SAndy Whitcroft 
291157303d80SAndy Whitcroft 	if (pagecache_page) {
291257303d80SAndy Whitcroft 		unlock_page(pagecache_page);
291357303d80SAndy Whitcroft 		put_page(pagecache_page);
291457303d80SAndy Whitcroft 	}
29151f64d69cSDean Nelson 	if (page != pagecache_page)
291656c9cfb1SNaoya Horiguchi 		unlock_page(page);
291766aebce7SChris Metcalf 	put_page(page);
291857303d80SAndy Whitcroft 
2919b4d1d99fSDavid Gibson out_mutex:
29203935baa9SDavid Gibson 	mutex_unlock(&hugetlb_instantiation_mutex);
29211e8f889bSDavid Gibson 
29221e8f889bSDavid Gibson 	return ret;
292386e5216fSAdam Litke }
292486e5216fSAdam Litke 
2925ceb86879SAndi Kleen /* Can be overriden by architectures */
2926ceb86879SAndi Kleen __attribute__((weak)) struct page *
2927ceb86879SAndi Kleen follow_huge_pud(struct mm_struct *mm, unsigned long address,
2928ceb86879SAndi Kleen 	       pud_t *pud, int write)
2929ceb86879SAndi Kleen {
2930ceb86879SAndi Kleen 	BUG();
2931ceb86879SAndi Kleen 	return NULL;
2932ceb86879SAndi Kleen }
2933ceb86879SAndi Kleen 
293463551ae0SDavid Gibson int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
293563551ae0SDavid Gibson 			struct page **pages, struct vm_area_struct **vmas,
29365b23dbe8SAdam Litke 			unsigned long *position, int *length, int i,
29372a15efc9SHugh Dickins 			unsigned int flags)
293863551ae0SDavid Gibson {
2939d5d4b0aaSChen, Kenneth W 	unsigned long pfn_offset;
2940d5d4b0aaSChen, Kenneth W 	unsigned long vaddr = *position;
294163551ae0SDavid Gibson 	int remainder = *length;
2942a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
294363551ae0SDavid Gibson 
29441c59827dSHugh Dickins 	spin_lock(&mm->page_table_lock);
294563551ae0SDavid Gibson 	while (vaddr < vma->vm_end && remainder) {
294663551ae0SDavid Gibson 		pte_t *pte;
29472a15efc9SHugh Dickins 		int absent;
294863551ae0SDavid Gibson 		struct page *page;
294963551ae0SDavid Gibson 
29504c887265SAdam Litke 		/*
29514c887265SAdam Litke 		 * Some archs (sparc64, sh*) have multiple pte_ts to
29522a15efc9SHugh Dickins 		 * each hugepage.  We have to make sure we get the
29534c887265SAdam Litke 		 * first, for the page indexing below to work.
29544c887265SAdam Litke 		 */
2955a5516438SAndi Kleen 		pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
29562a15efc9SHugh Dickins 		absent = !pte || huge_pte_none(huge_ptep_get(pte));
295763551ae0SDavid Gibson 
29582a15efc9SHugh Dickins 		/*
29592a15efc9SHugh Dickins 		 * When coredumping, it suits get_dump_page if we just return
29603ae77f43SHugh Dickins 		 * an error where there's an empty slot with no huge pagecache
29613ae77f43SHugh Dickins 		 * to back it.  This way, we avoid allocating a hugepage, and
29623ae77f43SHugh Dickins 		 * the sparse dumpfile avoids allocating disk blocks, but its
29633ae77f43SHugh Dickins 		 * huge holes still show up with zeroes where they need to be.
29642a15efc9SHugh Dickins 		 */
29653ae77f43SHugh Dickins 		if (absent && (flags & FOLL_DUMP) &&
29663ae77f43SHugh Dickins 		    !hugetlbfs_pagecache_present(h, vma, vaddr)) {
29672a15efc9SHugh Dickins 			remainder = 0;
29682a15efc9SHugh Dickins 			break;
29692a15efc9SHugh Dickins 		}
29702a15efc9SHugh Dickins 
29712a15efc9SHugh Dickins 		if (absent ||
29722a15efc9SHugh Dickins 		    ((flags & FOLL_WRITE) && !pte_write(huge_ptep_get(pte)))) {
29734c887265SAdam Litke 			int ret;
29744c887265SAdam Litke 
29754c887265SAdam Litke 			spin_unlock(&mm->page_table_lock);
29762a15efc9SHugh Dickins 			ret = hugetlb_fault(mm, vma, vaddr,
29772a15efc9SHugh Dickins 				(flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
29784c887265SAdam Litke 			spin_lock(&mm->page_table_lock);
2979a89182c7SAdam Litke 			if (!(ret & VM_FAULT_ERROR))
29804c887265SAdam Litke 				continue;
29814c887265SAdam Litke 
29821c59827dSHugh Dickins 			remainder = 0;
29831c59827dSHugh Dickins 			break;
29841c59827dSHugh Dickins 		}
298563551ae0SDavid Gibson 
2986a5516438SAndi Kleen 		pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
29877f2e9525SGerald Schaefer 		page = pte_page(huge_ptep_get(pte));
2988d5d4b0aaSChen, Kenneth W same_page:
2989d6692183SChen, Kenneth W 		if (pages) {
299069d177c2SAndy Whitcroft 			pages[i] = mem_map_offset(page, pfn_offset);
29914b2e38adSKOSAKI Motohiro 			get_page(pages[i]);
2992d6692183SChen, Kenneth W 		}
299363551ae0SDavid Gibson 
299463551ae0SDavid Gibson 		if (vmas)
299563551ae0SDavid Gibson 			vmas[i] = vma;
299663551ae0SDavid Gibson 
299763551ae0SDavid Gibson 		vaddr += PAGE_SIZE;
2998d5d4b0aaSChen, Kenneth W 		++pfn_offset;
299963551ae0SDavid Gibson 		--remainder;
300063551ae0SDavid Gibson 		++i;
3001d5d4b0aaSChen, Kenneth W 		if (vaddr < vma->vm_end && remainder &&
3002a5516438SAndi Kleen 				pfn_offset < pages_per_huge_page(h)) {
3003d5d4b0aaSChen, Kenneth W 			/*
3004d5d4b0aaSChen, Kenneth W 			 * We use pfn_offset to avoid touching the pageframes
3005d5d4b0aaSChen, Kenneth W 			 * of this compound page.
3006d5d4b0aaSChen, Kenneth W 			 */
3007d5d4b0aaSChen, Kenneth W 			goto same_page;
3008d5d4b0aaSChen, Kenneth W 		}
300963551ae0SDavid Gibson 	}
30101c59827dSHugh Dickins 	spin_unlock(&mm->page_table_lock);
301163551ae0SDavid Gibson 	*length = remainder;
301263551ae0SDavid Gibson 	*position = vaddr;
301363551ae0SDavid Gibson 
30142a15efc9SHugh Dickins 	return i ? i : -EFAULT;
301563551ae0SDavid Gibson }
30168f860591SZhang, Yanmin 
30178f860591SZhang, Yanmin void hugetlb_change_protection(struct vm_area_struct *vma,
30188f860591SZhang, Yanmin 		unsigned long address, unsigned long end, pgprot_t newprot)
30198f860591SZhang, Yanmin {
30208f860591SZhang, Yanmin 	struct mm_struct *mm = vma->vm_mm;
30218f860591SZhang, Yanmin 	unsigned long start = address;
30228f860591SZhang, Yanmin 	pte_t *ptep;
30238f860591SZhang, Yanmin 	pte_t pte;
3024a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
30258f860591SZhang, Yanmin 
30268f860591SZhang, Yanmin 	BUG_ON(address >= end);
30278f860591SZhang, Yanmin 	flush_cache_range(vma, address, end);
30288f860591SZhang, Yanmin 
30293d48ae45SPeter Zijlstra 	mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
30308f860591SZhang, Yanmin 	spin_lock(&mm->page_table_lock);
3031a5516438SAndi Kleen 	for (; address < end; address += huge_page_size(h)) {
30328f860591SZhang, Yanmin 		ptep = huge_pte_offset(mm, address);
30338f860591SZhang, Yanmin 		if (!ptep)
30348f860591SZhang, Yanmin 			continue;
303539dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
303639dde65cSChen, Kenneth W 			continue;
30377f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(ptep))) {
30388f860591SZhang, Yanmin 			pte = huge_ptep_get_and_clear(mm, address, ptep);
30398f860591SZhang, Yanmin 			pte = pte_mkhuge(pte_modify(pte, newprot));
30408f860591SZhang, Yanmin 			set_huge_pte_at(mm, address, ptep, pte);
30418f860591SZhang, Yanmin 		}
30428f860591SZhang, Yanmin 	}
30438f860591SZhang, Yanmin 	spin_unlock(&mm->page_table_lock);
3044d833352aSMel Gorman 	/*
3045d833352aSMel Gorman 	 * Must flush TLB before releasing i_mmap_mutex: x86's huge_pmd_unshare
3046d833352aSMel Gorman 	 * may have cleared our pud entry and done put_page on the page table:
3047d833352aSMel Gorman 	 * once we release i_mmap_mutex, another task can do the final put_page
3048d833352aSMel Gorman 	 * and that page table be reused and filled with junk.
3049d833352aSMel Gorman 	 */
30508f860591SZhang, Yanmin 	flush_tlb_range(vma, start, end);
3051d833352aSMel Gorman 	mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
30528f860591SZhang, Yanmin }
30538f860591SZhang, Yanmin 
3054a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode,
3055a1e78772SMel Gorman 					long from, long to,
30565a6fe125SMel Gorman 					struct vm_area_struct *vma,
3057ca16d140SKOSAKI Motohiro 					vm_flags_t vm_flags)
3058e4e574b7SAdam Litke {
305917c9d12eSMel Gorman 	long ret, chg;
3060a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
306190481622SDavid Gibson 	struct hugepage_subpool *spool = subpool_inode(inode);
3062e4e574b7SAdam Litke 
3063a1e78772SMel Gorman 	/*
306417c9d12eSMel Gorman 	 * Only apply hugepage reservation if asked. At fault time, an
306517c9d12eSMel Gorman 	 * attempt will be made for VM_NORESERVE to allocate a page
306690481622SDavid Gibson 	 * without using reserves
306717c9d12eSMel Gorman 	 */
3068ca16d140SKOSAKI Motohiro 	if (vm_flags & VM_NORESERVE)
306917c9d12eSMel Gorman 		return 0;
307017c9d12eSMel Gorman 
307117c9d12eSMel Gorman 	/*
3072a1e78772SMel Gorman 	 * Shared mappings base their reservation on the number of pages that
3073a1e78772SMel Gorman 	 * are already allocated on behalf of the file. Private mappings need
3074a1e78772SMel Gorman 	 * to reserve the full area even if read-only as mprotect() may be
3075a1e78772SMel Gorman 	 * called to make the mapping read-write. Assume !vma is a shm mapping
3076a1e78772SMel Gorman 	 */
3077f83a275dSMel Gorman 	if (!vma || vma->vm_flags & VM_MAYSHARE)
3078e4e574b7SAdam Litke 		chg = region_chg(&inode->i_mapping->private_list, from, to);
30795a6fe125SMel Gorman 	else {
30805a6fe125SMel Gorman 		struct resv_map *resv_map = resv_map_alloc();
30815a6fe125SMel Gorman 		if (!resv_map)
30825a6fe125SMel Gorman 			return -ENOMEM;
30835a6fe125SMel Gorman 
308417c9d12eSMel Gorman 		chg = to - from;
308517c9d12eSMel Gorman 
30865a6fe125SMel Gorman 		set_vma_resv_map(vma, resv_map);
30875a6fe125SMel Gorman 		set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
30885a6fe125SMel Gorman 	}
30895a6fe125SMel Gorman 
3090c50ac050SDave Hansen 	if (chg < 0) {
3091c50ac050SDave Hansen 		ret = chg;
3092c50ac050SDave Hansen 		goto out_err;
3093c50ac050SDave Hansen 	}
309417c9d12eSMel Gorman 
309590481622SDavid Gibson 	/* There must be enough pages in the subpool for the mapping */
3096c50ac050SDave Hansen 	if (hugepage_subpool_get_pages(spool, chg)) {
3097c50ac050SDave Hansen 		ret = -ENOSPC;
3098c50ac050SDave Hansen 		goto out_err;
3099c50ac050SDave Hansen 	}
310017c9d12eSMel Gorman 
310117c9d12eSMel Gorman 	/*
310217c9d12eSMel Gorman 	 * Check enough hugepages are available for the reservation.
310390481622SDavid Gibson 	 * Hand the pages back to the subpool if there are not
310417c9d12eSMel Gorman 	 */
310517c9d12eSMel Gorman 	ret = hugetlb_acct_memory(h, chg);
310617c9d12eSMel Gorman 	if (ret < 0) {
310790481622SDavid Gibson 		hugepage_subpool_put_pages(spool, chg);
3108c50ac050SDave Hansen 		goto out_err;
310917c9d12eSMel Gorman 	}
311017c9d12eSMel Gorman 
311117c9d12eSMel Gorman 	/*
311217c9d12eSMel Gorman 	 * Account for the reservations made. Shared mappings record regions
311317c9d12eSMel Gorman 	 * that have reservations as they are shared by multiple VMAs.
311417c9d12eSMel Gorman 	 * When the last VMA disappears, the region map says how much
311517c9d12eSMel Gorman 	 * the reservation was and the page cache tells how much of
311617c9d12eSMel Gorman 	 * the reservation was consumed. Private mappings are per-VMA and
311717c9d12eSMel Gorman 	 * only the consumed reservations are tracked. When the VMA
311817c9d12eSMel Gorman 	 * disappears, the original reservation is the VMA size and the
311917c9d12eSMel Gorman 	 * consumed reservations are stored in the map. Hence, nothing
312017c9d12eSMel Gorman 	 * else has to be done for private mappings here
312117c9d12eSMel Gorman 	 */
3122f83a275dSMel Gorman 	if (!vma || vma->vm_flags & VM_MAYSHARE)
312317c9d12eSMel Gorman 		region_add(&inode->i_mapping->private_list, from, to);
3124a43a8c39SChen, Kenneth W 	return 0;
3125c50ac050SDave Hansen out_err:
31264523e145SDave Hansen 	if (vma)
3127c50ac050SDave Hansen 		resv_map_put(vma);
3128c50ac050SDave Hansen 	return ret;
3129a43a8c39SChen, Kenneth W }
3130a43a8c39SChen, Kenneth W 
3131a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
3132a43a8c39SChen, Kenneth W {
3133a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
3134a43a8c39SChen, Kenneth W 	long chg = region_truncate(&inode->i_mapping->private_list, offset);
313590481622SDavid Gibson 	struct hugepage_subpool *spool = subpool_inode(inode);
313645c682a6SKen Chen 
313745c682a6SKen Chen 	spin_lock(&inode->i_lock);
3138e4c6f8beSEric Sandeen 	inode->i_blocks -= (blocks_per_huge_page(h) * freed);
313945c682a6SKen Chen 	spin_unlock(&inode->i_lock);
314045c682a6SKen Chen 
314190481622SDavid Gibson 	hugepage_subpool_put_pages(spool, (chg - freed));
3142a5516438SAndi Kleen 	hugetlb_acct_memory(h, -(chg - freed));
3143a43a8c39SChen, Kenneth W }
314493f70f90SNaoya Horiguchi 
3145d5bd9106SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
3146d5bd9106SAndi Kleen 
31476de2b1aaSNaoya Horiguchi /* Should be called in hugetlb_lock */
31486de2b1aaSNaoya Horiguchi static int is_hugepage_on_freelist(struct page *hpage)
31496de2b1aaSNaoya Horiguchi {
31506de2b1aaSNaoya Horiguchi 	struct page *page;
31516de2b1aaSNaoya Horiguchi 	struct page *tmp;
31526de2b1aaSNaoya Horiguchi 	struct hstate *h = page_hstate(hpage);
31536de2b1aaSNaoya Horiguchi 	int nid = page_to_nid(hpage);
31546de2b1aaSNaoya Horiguchi 
31556de2b1aaSNaoya Horiguchi 	list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru)
31566de2b1aaSNaoya Horiguchi 		if (page == hpage)
31576de2b1aaSNaoya Horiguchi 			return 1;
31586de2b1aaSNaoya Horiguchi 	return 0;
31596de2b1aaSNaoya Horiguchi }
31606de2b1aaSNaoya Horiguchi 
316193f70f90SNaoya Horiguchi /*
316293f70f90SNaoya Horiguchi  * This function is called from memory failure code.
316393f70f90SNaoya Horiguchi  * Assume the caller holds page lock of the head page.
316493f70f90SNaoya Horiguchi  */
31656de2b1aaSNaoya Horiguchi int dequeue_hwpoisoned_huge_page(struct page *hpage)
316693f70f90SNaoya Horiguchi {
316793f70f90SNaoya Horiguchi 	struct hstate *h = page_hstate(hpage);
316893f70f90SNaoya Horiguchi 	int nid = page_to_nid(hpage);
31696de2b1aaSNaoya Horiguchi 	int ret = -EBUSY;
317093f70f90SNaoya Horiguchi 
317193f70f90SNaoya Horiguchi 	spin_lock(&hugetlb_lock);
31726de2b1aaSNaoya Horiguchi 	if (is_hugepage_on_freelist(hpage)) {
317393f70f90SNaoya Horiguchi 		list_del(&hpage->lru);
31748c6c2ecbSNaoya Horiguchi 		set_page_refcounted(hpage);
317593f70f90SNaoya Horiguchi 		h->free_huge_pages--;
317693f70f90SNaoya Horiguchi 		h->free_huge_pages_node[nid]--;
31776de2b1aaSNaoya Horiguchi 		ret = 0;
317893f70f90SNaoya Horiguchi 	}
31796de2b1aaSNaoya Horiguchi 	spin_unlock(&hugetlb_lock);
31806de2b1aaSNaoya Horiguchi 	return ret;
31816de2b1aaSNaoya Horiguchi }
31826de2b1aaSNaoya Horiguchi #endif
3183