xref: /openbmc/linux/mm/hugetlb.c (revision 0fe6e20b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Generic hugetlb support.
31da177e4SLinus Torvalds  * (C) William Irwin, April 2004
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds #include <linux/list.h>
61da177e4SLinus Torvalds #include <linux/init.h>
71da177e4SLinus Torvalds #include <linux/module.h>
81da177e4SLinus Torvalds #include <linux/mm.h>
9e1759c21SAlexey Dobriyan #include <linux/seq_file.h>
101da177e4SLinus Torvalds #include <linux/sysctl.h>
111da177e4SLinus Torvalds #include <linux/highmem.h>
12cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
131da177e4SLinus Torvalds #include <linux/nodemask.h>
1463551ae0SDavid Gibson #include <linux/pagemap.h>
155da7ca86SChristoph Lameter #include <linux/mempolicy.h>
16aea47ff3SChristoph Lameter #include <linux/cpuset.h>
173935baa9SDavid Gibson #include <linux/mutex.h>
18aa888a74SAndi Kleen #include <linux/bootmem.h>
19a3437870SNishanth Aravamudan #include <linux/sysfs.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
210fe6e20bSNaoya Horiguchi #include <linux/rmap.h>
22d6606683SLinus Torvalds 
2363551ae0SDavid Gibson #include <asm/page.h>
2463551ae0SDavid Gibson #include <asm/pgtable.h>
2578a34ae2SAdrian Bunk #include <asm/io.h>
2663551ae0SDavid Gibson 
2763551ae0SDavid Gibson #include <linux/hugetlb.h>
289a305230SLee Schermerhorn #include <linux/node.h>
297835e98bSNick Piggin #include "internal.h"
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
32396faf03SMel Gorman static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
33396faf03SMel Gorman unsigned long hugepages_treat_as_movable;
34a5516438SAndi Kleen 
35e5ff2159SAndi Kleen static int max_hstate;
36e5ff2159SAndi Kleen unsigned int default_hstate_idx;
37e5ff2159SAndi Kleen struct hstate hstates[HUGE_MAX_HSTATE];
38e5ff2159SAndi Kleen 
3953ba51d2SJon Tollefson __initdata LIST_HEAD(huge_boot_pages);
4053ba51d2SJon Tollefson 
41e5ff2159SAndi Kleen /* for command line parsing */
42e5ff2159SAndi Kleen static struct hstate * __initdata parsed_hstate;
43e5ff2159SAndi Kleen static unsigned long __initdata default_hstate_max_huge_pages;
44e11bfbfcSNick Piggin static unsigned long __initdata default_hstate_size;
45e5ff2159SAndi Kleen 
46e5ff2159SAndi Kleen #define for_each_hstate(h) \
47e5ff2159SAndi Kleen 	for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
48396faf03SMel Gorman 
493935baa9SDavid Gibson /*
503935baa9SDavid Gibson  * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
513935baa9SDavid Gibson  */
523935baa9SDavid Gibson static DEFINE_SPINLOCK(hugetlb_lock);
530bd0f9fbSEric Paris 
54e7c4b0bfSAndy Whitcroft /*
5596822904SAndy Whitcroft  * Region tracking -- allows tracking of reservations and instantiated pages
5696822904SAndy Whitcroft  *                    across the pages in a mapping.
5784afd99bSAndy Whitcroft  *
5884afd99bSAndy Whitcroft  * The region data structures are protected by a combination of the mmap_sem
5984afd99bSAndy Whitcroft  * and the hugetlb_instantion_mutex.  To access or modify a region the caller
6084afd99bSAndy Whitcroft  * must either hold the mmap_sem for write, or the mmap_sem for read and
6184afd99bSAndy Whitcroft  * the hugetlb_instantiation mutex:
6284afd99bSAndy Whitcroft  *
6384afd99bSAndy Whitcroft  * 	down_write(&mm->mmap_sem);
6484afd99bSAndy Whitcroft  * or
6584afd99bSAndy Whitcroft  * 	down_read(&mm->mmap_sem);
6684afd99bSAndy Whitcroft  * 	mutex_lock(&hugetlb_instantiation_mutex);
6796822904SAndy Whitcroft  */
6896822904SAndy Whitcroft struct file_region {
6996822904SAndy Whitcroft 	struct list_head link;
7096822904SAndy Whitcroft 	long from;
7196822904SAndy Whitcroft 	long to;
7296822904SAndy Whitcroft };
7396822904SAndy Whitcroft 
7496822904SAndy Whitcroft static long region_add(struct list_head *head, long f, long t)
7596822904SAndy Whitcroft {
7696822904SAndy Whitcroft 	struct file_region *rg, *nrg, *trg;
7796822904SAndy Whitcroft 
7896822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
7996822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
8096822904SAndy Whitcroft 		if (f <= rg->to)
8196822904SAndy Whitcroft 			break;
8296822904SAndy Whitcroft 
8396822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
8496822904SAndy Whitcroft 	if (f > rg->from)
8596822904SAndy Whitcroft 		f = rg->from;
8696822904SAndy Whitcroft 
8796822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
8896822904SAndy Whitcroft 	nrg = rg;
8996822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
9096822904SAndy Whitcroft 		if (&rg->link == head)
9196822904SAndy Whitcroft 			break;
9296822904SAndy Whitcroft 		if (rg->from > t)
9396822904SAndy Whitcroft 			break;
9496822904SAndy Whitcroft 
9596822904SAndy Whitcroft 		/* If this area reaches higher then extend our area to
9696822904SAndy Whitcroft 		 * include it completely.  If this is not the first area
9796822904SAndy Whitcroft 		 * which we intend to reuse, free it. */
9896822904SAndy Whitcroft 		if (rg->to > t)
9996822904SAndy Whitcroft 			t = rg->to;
10096822904SAndy Whitcroft 		if (rg != nrg) {
10196822904SAndy Whitcroft 			list_del(&rg->link);
10296822904SAndy Whitcroft 			kfree(rg);
10396822904SAndy Whitcroft 		}
10496822904SAndy Whitcroft 	}
10596822904SAndy Whitcroft 	nrg->from = f;
10696822904SAndy Whitcroft 	nrg->to = t;
10796822904SAndy Whitcroft 	return 0;
10896822904SAndy Whitcroft }
10996822904SAndy Whitcroft 
11096822904SAndy Whitcroft static long region_chg(struct list_head *head, long f, long t)
11196822904SAndy Whitcroft {
11296822904SAndy Whitcroft 	struct file_region *rg, *nrg;
11396822904SAndy Whitcroft 	long chg = 0;
11496822904SAndy Whitcroft 
11596822904SAndy Whitcroft 	/* Locate the region we are before or in. */
11696822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
11796822904SAndy Whitcroft 		if (f <= rg->to)
11896822904SAndy Whitcroft 			break;
11996822904SAndy Whitcroft 
12096822904SAndy Whitcroft 	/* If we are below the current region then a new region is required.
12196822904SAndy Whitcroft 	 * Subtle, allocate a new region at the position but make it zero
12296822904SAndy Whitcroft 	 * size such that we can guarantee to record the reservation. */
12396822904SAndy Whitcroft 	if (&rg->link == head || t < rg->from) {
12496822904SAndy Whitcroft 		nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
12596822904SAndy Whitcroft 		if (!nrg)
12696822904SAndy Whitcroft 			return -ENOMEM;
12796822904SAndy Whitcroft 		nrg->from = f;
12896822904SAndy Whitcroft 		nrg->to   = f;
12996822904SAndy Whitcroft 		INIT_LIST_HEAD(&nrg->link);
13096822904SAndy Whitcroft 		list_add(&nrg->link, rg->link.prev);
13196822904SAndy Whitcroft 
13296822904SAndy Whitcroft 		return t - f;
13396822904SAndy Whitcroft 	}
13496822904SAndy Whitcroft 
13596822904SAndy Whitcroft 	/* Round our left edge to the current segment if it encloses us. */
13696822904SAndy Whitcroft 	if (f > rg->from)
13796822904SAndy Whitcroft 		f = rg->from;
13896822904SAndy Whitcroft 	chg = t - f;
13996822904SAndy Whitcroft 
14096822904SAndy Whitcroft 	/* Check for and consume any regions we now overlap with. */
14196822904SAndy Whitcroft 	list_for_each_entry(rg, rg->link.prev, link) {
14296822904SAndy Whitcroft 		if (&rg->link == head)
14396822904SAndy Whitcroft 			break;
14496822904SAndy Whitcroft 		if (rg->from > t)
14596822904SAndy Whitcroft 			return chg;
14696822904SAndy Whitcroft 
14796822904SAndy Whitcroft 		/* We overlap with this area, if it extends futher than
14896822904SAndy Whitcroft 		 * us then we must extend ourselves.  Account for its
14996822904SAndy Whitcroft 		 * existing reservation. */
15096822904SAndy Whitcroft 		if (rg->to > t) {
15196822904SAndy Whitcroft 			chg += rg->to - t;
15296822904SAndy Whitcroft 			t = rg->to;
15396822904SAndy Whitcroft 		}
15496822904SAndy Whitcroft 		chg -= rg->to - rg->from;
15596822904SAndy Whitcroft 	}
15696822904SAndy Whitcroft 	return chg;
15796822904SAndy Whitcroft }
15896822904SAndy Whitcroft 
15996822904SAndy Whitcroft static long region_truncate(struct list_head *head, long end)
16096822904SAndy Whitcroft {
16196822904SAndy Whitcroft 	struct file_region *rg, *trg;
16296822904SAndy Whitcroft 	long chg = 0;
16396822904SAndy Whitcroft 
16496822904SAndy Whitcroft 	/* Locate the region we are either in or before. */
16596822904SAndy Whitcroft 	list_for_each_entry(rg, head, link)
16696822904SAndy Whitcroft 		if (end <= rg->to)
16796822904SAndy Whitcroft 			break;
16896822904SAndy Whitcroft 	if (&rg->link == head)
16996822904SAndy Whitcroft 		return 0;
17096822904SAndy Whitcroft 
17196822904SAndy Whitcroft 	/* If we are in the middle of a region then adjust it. */
17296822904SAndy Whitcroft 	if (end > rg->from) {
17396822904SAndy Whitcroft 		chg = rg->to - end;
17496822904SAndy Whitcroft 		rg->to = end;
17596822904SAndy Whitcroft 		rg = list_entry(rg->link.next, typeof(*rg), link);
17696822904SAndy Whitcroft 	}
17796822904SAndy Whitcroft 
17896822904SAndy Whitcroft 	/* Drop any remaining regions. */
17996822904SAndy Whitcroft 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
18096822904SAndy Whitcroft 		if (&rg->link == head)
18196822904SAndy Whitcroft 			break;
18296822904SAndy Whitcroft 		chg += rg->to - rg->from;
18396822904SAndy Whitcroft 		list_del(&rg->link);
18496822904SAndy Whitcroft 		kfree(rg);
18596822904SAndy Whitcroft 	}
18696822904SAndy Whitcroft 	return chg;
18796822904SAndy Whitcroft }
18896822904SAndy Whitcroft 
18984afd99bSAndy Whitcroft static long region_count(struct list_head *head, long f, long t)
19084afd99bSAndy Whitcroft {
19184afd99bSAndy Whitcroft 	struct file_region *rg;
19284afd99bSAndy Whitcroft 	long chg = 0;
19384afd99bSAndy Whitcroft 
19484afd99bSAndy Whitcroft 	/* Locate each segment we overlap with, and count that overlap. */
19584afd99bSAndy Whitcroft 	list_for_each_entry(rg, head, link) {
19684afd99bSAndy Whitcroft 		int seg_from;
19784afd99bSAndy Whitcroft 		int seg_to;
19884afd99bSAndy Whitcroft 
19984afd99bSAndy Whitcroft 		if (rg->to <= f)
20084afd99bSAndy Whitcroft 			continue;
20184afd99bSAndy Whitcroft 		if (rg->from >= t)
20284afd99bSAndy Whitcroft 			break;
20384afd99bSAndy Whitcroft 
20484afd99bSAndy Whitcroft 		seg_from = max(rg->from, f);
20584afd99bSAndy Whitcroft 		seg_to = min(rg->to, t);
20684afd99bSAndy Whitcroft 
20784afd99bSAndy Whitcroft 		chg += seg_to - seg_from;
20884afd99bSAndy Whitcroft 	}
20984afd99bSAndy Whitcroft 
21084afd99bSAndy Whitcroft 	return chg;
21184afd99bSAndy Whitcroft }
21284afd99bSAndy Whitcroft 
21396822904SAndy Whitcroft /*
214e7c4b0bfSAndy Whitcroft  * Convert the address within this vma to the page offset within
215e7c4b0bfSAndy Whitcroft  * the mapping, in pagecache page units; huge pages here.
216e7c4b0bfSAndy Whitcroft  */
217a5516438SAndi Kleen static pgoff_t vma_hugecache_offset(struct hstate *h,
218a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long address)
219e7c4b0bfSAndy Whitcroft {
220a5516438SAndi Kleen 	return ((address - vma->vm_start) >> huge_page_shift(h)) +
221a5516438SAndi Kleen 			(vma->vm_pgoff >> huge_page_order(h));
222e7c4b0bfSAndy Whitcroft }
223e7c4b0bfSAndy Whitcroft 
2240fe6e20bSNaoya Horiguchi pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
2250fe6e20bSNaoya Horiguchi 				     unsigned long address)
2260fe6e20bSNaoya Horiguchi {
2270fe6e20bSNaoya Horiguchi 	return vma_hugecache_offset(hstate_vma(vma), vma, address);
2280fe6e20bSNaoya Horiguchi }
2290fe6e20bSNaoya Horiguchi 
23084afd99bSAndy Whitcroft /*
23108fba699SMel Gorman  * Return the size of the pages allocated when backing a VMA. In the majority
23208fba699SMel Gorman  * cases this will be same size as used by the page table entries.
23308fba699SMel Gorman  */
23408fba699SMel Gorman unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
23508fba699SMel Gorman {
23608fba699SMel Gorman 	struct hstate *hstate;
23708fba699SMel Gorman 
23808fba699SMel Gorman 	if (!is_vm_hugetlb_page(vma))
23908fba699SMel Gorman 		return PAGE_SIZE;
24008fba699SMel Gorman 
24108fba699SMel Gorman 	hstate = hstate_vma(vma);
24208fba699SMel Gorman 
24308fba699SMel Gorman 	return 1UL << (hstate->order + PAGE_SHIFT);
24408fba699SMel Gorman }
245f340ca0fSJoerg Roedel EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
24608fba699SMel Gorman 
24708fba699SMel Gorman /*
2483340289dSMel Gorman  * Return the page size being used by the MMU to back a VMA. In the majority
2493340289dSMel Gorman  * of cases, the page size used by the kernel matches the MMU size. On
2503340289dSMel Gorman  * architectures where it differs, an architecture-specific version of this
2513340289dSMel Gorman  * function is required.
2523340289dSMel Gorman  */
2533340289dSMel Gorman #ifndef vma_mmu_pagesize
2543340289dSMel Gorman unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
2553340289dSMel Gorman {
2563340289dSMel Gorman 	return vma_kernel_pagesize(vma);
2573340289dSMel Gorman }
2583340289dSMel Gorman #endif
2593340289dSMel Gorman 
2603340289dSMel Gorman /*
26184afd99bSAndy Whitcroft  * Flags for MAP_PRIVATE reservations.  These are stored in the bottom
26284afd99bSAndy Whitcroft  * bits of the reservation map pointer, which are always clear due to
26384afd99bSAndy Whitcroft  * alignment.
26484afd99bSAndy Whitcroft  */
26584afd99bSAndy Whitcroft #define HPAGE_RESV_OWNER    (1UL << 0)
26684afd99bSAndy Whitcroft #define HPAGE_RESV_UNMAPPED (1UL << 1)
26704f2cbe3SMel Gorman #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
26884afd99bSAndy Whitcroft 
269a1e78772SMel Gorman /*
270a1e78772SMel Gorman  * These helpers are used to track how many pages are reserved for
271a1e78772SMel Gorman  * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
272a1e78772SMel Gorman  * is guaranteed to have their future faults succeed.
273a1e78772SMel Gorman  *
274a1e78772SMel Gorman  * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
275a1e78772SMel Gorman  * the reserve counters are updated with the hugetlb_lock held. It is safe
276a1e78772SMel Gorman  * to reset the VMA at fork() time as it is not in use yet and there is no
277a1e78772SMel Gorman  * chance of the global counters getting corrupted as a result of the values.
27884afd99bSAndy Whitcroft  *
27984afd99bSAndy Whitcroft  * The private mapping reservation is represented in a subtly different
28084afd99bSAndy Whitcroft  * manner to a shared mapping.  A shared mapping has a region map associated
28184afd99bSAndy Whitcroft  * with the underlying file, this region map represents the backing file
28284afd99bSAndy Whitcroft  * pages which have ever had a reservation assigned which this persists even
28384afd99bSAndy Whitcroft  * after the page is instantiated.  A private mapping has a region map
28484afd99bSAndy Whitcroft  * associated with the original mmap which is attached to all VMAs which
28584afd99bSAndy Whitcroft  * reference it, this region map represents those offsets which have consumed
28684afd99bSAndy Whitcroft  * reservation ie. where pages have been instantiated.
287a1e78772SMel Gorman  */
288e7c4b0bfSAndy Whitcroft static unsigned long get_vma_private_data(struct vm_area_struct *vma)
289e7c4b0bfSAndy Whitcroft {
290e7c4b0bfSAndy Whitcroft 	return (unsigned long)vma->vm_private_data;
291e7c4b0bfSAndy Whitcroft }
292e7c4b0bfSAndy Whitcroft 
293e7c4b0bfSAndy Whitcroft static void set_vma_private_data(struct vm_area_struct *vma,
294e7c4b0bfSAndy Whitcroft 							unsigned long value)
295e7c4b0bfSAndy Whitcroft {
296e7c4b0bfSAndy Whitcroft 	vma->vm_private_data = (void *)value;
297e7c4b0bfSAndy Whitcroft }
298e7c4b0bfSAndy Whitcroft 
29984afd99bSAndy Whitcroft struct resv_map {
30084afd99bSAndy Whitcroft 	struct kref refs;
30184afd99bSAndy Whitcroft 	struct list_head regions;
30284afd99bSAndy Whitcroft };
30384afd99bSAndy Whitcroft 
3042a4b3dedSHarvey Harrison static struct resv_map *resv_map_alloc(void)
30584afd99bSAndy Whitcroft {
30684afd99bSAndy Whitcroft 	struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
30784afd99bSAndy Whitcroft 	if (!resv_map)
30884afd99bSAndy Whitcroft 		return NULL;
30984afd99bSAndy Whitcroft 
31084afd99bSAndy Whitcroft 	kref_init(&resv_map->refs);
31184afd99bSAndy Whitcroft 	INIT_LIST_HEAD(&resv_map->regions);
31284afd99bSAndy Whitcroft 
31384afd99bSAndy Whitcroft 	return resv_map;
31484afd99bSAndy Whitcroft }
31584afd99bSAndy Whitcroft 
3162a4b3dedSHarvey Harrison static void resv_map_release(struct kref *ref)
31784afd99bSAndy Whitcroft {
31884afd99bSAndy Whitcroft 	struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
31984afd99bSAndy Whitcroft 
32084afd99bSAndy Whitcroft 	/* Clear out any active regions before we release the map. */
32184afd99bSAndy Whitcroft 	region_truncate(&resv_map->regions, 0);
32284afd99bSAndy Whitcroft 	kfree(resv_map);
32384afd99bSAndy Whitcroft }
32484afd99bSAndy Whitcroft 
32584afd99bSAndy Whitcroft static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
326a1e78772SMel Gorman {
327a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
328f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE))
32984afd99bSAndy Whitcroft 		return (struct resv_map *)(get_vma_private_data(vma) &
33084afd99bSAndy Whitcroft 							~HPAGE_RESV_MASK);
3312a4b3dedSHarvey Harrison 	return NULL;
332a1e78772SMel Gorman }
333a1e78772SMel Gorman 
33484afd99bSAndy Whitcroft static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
335a1e78772SMel Gorman {
336a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
337f83a275dSMel Gorman 	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
338a1e78772SMel Gorman 
33984afd99bSAndy Whitcroft 	set_vma_private_data(vma, (get_vma_private_data(vma) &
34084afd99bSAndy Whitcroft 				HPAGE_RESV_MASK) | (unsigned long)map);
34104f2cbe3SMel Gorman }
34204f2cbe3SMel Gorman 
34304f2cbe3SMel Gorman static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
34404f2cbe3SMel Gorman {
34504f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
346f83a275dSMel Gorman 	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
347e7c4b0bfSAndy Whitcroft 
348e7c4b0bfSAndy Whitcroft 	set_vma_private_data(vma, get_vma_private_data(vma) | flags);
34904f2cbe3SMel Gorman }
35004f2cbe3SMel Gorman 
35104f2cbe3SMel Gorman static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
35204f2cbe3SMel Gorman {
35304f2cbe3SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
354e7c4b0bfSAndy Whitcroft 
355e7c4b0bfSAndy Whitcroft 	return (get_vma_private_data(vma) & flag) != 0;
356a1e78772SMel Gorman }
357a1e78772SMel Gorman 
358a1e78772SMel Gorman /* Decrement the reserved pages in the hugepage pool by one */
359a5516438SAndi Kleen static void decrement_hugepage_resv_vma(struct hstate *h,
360a5516438SAndi Kleen 			struct vm_area_struct *vma)
361a1e78772SMel Gorman {
362c37f9fb1SAndy Whitcroft 	if (vma->vm_flags & VM_NORESERVE)
363c37f9fb1SAndy Whitcroft 		return;
364c37f9fb1SAndy Whitcroft 
365f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE) {
366a1e78772SMel Gorman 		/* Shared mappings always use reserves */
367a5516438SAndi Kleen 		h->resv_huge_pages--;
36884afd99bSAndy Whitcroft 	} else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
369a1e78772SMel Gorman 		/*
370a1e78772SMel Gorman 		 * Only the process that called mmap() has reserves for
371a1e78772SMel Gorman 		 * private mappings.
372a1e78772SMel Gorman 		 */
373a5516438SAndi Kleen 		h->resv_huge_pages--;
374a1e78772SMel Gorman 	}
375a1e78772SMel Gorman }
376a1e78772SMel Gorman 
37704f2cbe3SMel Gorman /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
378a1e78772SMel Gorman void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
379a1e78772SMel Gorman {
380a1e78772SMel Gorman 	VM_BUG_ON(!is_vm_hugetlb_page(vma));
381f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE))
382a1e78772SMel Gorman 		vma->vm_private_data = (void *)0;
383a1e78772SMel Gorman }
384a1e78772SMel Gorman 
385a1e78772SMel Gorman /* Returns true if the VMA has associated reserve pages */
3867f09ca51SMel Gorman static int vma_has_reserves(struct vm_area_struct *vma)
387a1e78772SMel Gorman {
388f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE)
389a1e78772SMel Gorman 		return 1;
3907f09ca51SMel Gorman 	if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
3917f09ca51SMel Gorman 		return 1;
3927f09ca51SMel Gorman 	return 0;
393a1e78772SMel Gorman }
394a1e78772SMel Gorman 
39569d177c2SAndy Whitcroft static void clear_gigantic_page(struct page *page,
39669d177c2SAndy Whitcroft 			unsigned long addr, unsigned long sz)
39769d177c2SAndy Whitcroft {
39869d177c2SAndy Whitcroft 	int i;
39969d177c2SAndy Whitcroft 	struct page *p = page;
40069d177c2SAndy Whitcroft 
40169d177c2SAndy Whitcroft 	might_sleep();
40269d177c2SAndy Whitcroft 	for (i = 0; i < sz/PAGE_SIZE; i++, p = mem_map_next(p, page, i)) {
40369d177c2SAndy Whitcroft 		cond_resched();
40469d177c2SAndy Whitcroft 		clear_user_highpage(p, addr + i * PAGE_SIZE);
40569d177c2SAndy Whitcroft 	}
40669d177c2SAndy Whitcroft }
407a5516438SAndi Kleen static void clear_huge_page(struct page *page,
408a5516438SAndi Kleen 			unsigned long addr, unsigned long sz)
40979ac6ba4SDavid Gibson {
41079ac6ba4SDavid Gibson 	int i;
41179ac6ba4SDavid Gibson 
41274dbdd23SAndrea Arcangeli 	if (unlikely(sz/PAGE_SIZE > MAX_ORDER_NR_PAGES)) {
413ebdd4aeaSHannes Eder 		clear_gigantic_page(page, addr, sz);
414ebdd4aeaSHannes Eder 		return;
415ebdd4aeaSHannes Eder 	}
41669d177c2SAndy Whitcroft 
41779ac6ba4SDavid Gibson 	might_sleep();
418a5516438SAndi Kleen 	for (i = 0; i < sz/PAGE_SIZE; i++) {
41979ac6ba4SDavid Gibson 		cond_resched();
420281e0e3bSRalf Baechle 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
42179ac6ba4SDavid Gibson 	}
42279ac6ba4SDavid Gibson }
42379ac6ba4SDavid Gibson 
42469d177c2SAndy Whitcroft static void copy_gigantic_page(struct page *dst, struct page *src,
42569d177c2SAndy Whitcroft 			   unsigned long addr, struct vm_area_struct *vma)
42669d177c2SAndy Whitcroft {
42769d177c2SAndy Whitcroft 	int i;
42869d177c2SAndy Whitcroft 	struct hstate *h = hstate_vma(vma);
42969d177c2SAndy Whitcroft 	struct page *dst_base = dst;
43069d177c2SAndy Whitcroft 	struct page *src_base = src;
43169d177c2SAndy Whitcroft 	might_sleep();
43269d177c2SAndy Whitcroft 	for (i = 0; i < pages_per_huge_page(h); ) {
43369d177c2SAndy Whitcroft 		cond_resched();
43469d177c2SAndy Whitcroft 		copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
43569d177c2SAndy Whitcroft 
43669d177c2SAndy Whitcroft 		i++;
43769d177c2SAndy Whitcroft 		dst = mem_map_next(dst, dst_base, i);
43869d177c2SAndy Whitcroft 		src = mem_map_next(src, src_base, i);
43969d177c2SAndy Whitcroft 	}
44069d177c2SAndy Whitcroft }
44179ac6ba4SDavid Gibson static void copy_huge_page(struct page *dst, struct page *src,
4429de455b2SAtsushi Nemoto 			   unsigned long addr, struct vm_area_struct *vma)
44379ac6ba4SDavid Gibson {
44479ac6ba4SDavid Gibson 	int i;
445a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
44679ac6ba4SDavid Gibson 
447ebdd4aeaSHannes Eder 	if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) {
448ebdd4aeaSHannes Eder 		copy_gigantic_page(dst, src, addr, vma);
449ebdd4aeaSHannes Eder 		return;
450ebdd4aeaSHannes Eder 	}
45169d177c2SAndy Whitcroft 
45279ac6ba4SDavid Gibson 	might_sleep();
453a5516438SAndi Kleen 	for (i = 0; i < pages_per_huge_page(h); i++) {
45479ac6ba4SDavid Gibson 		cond_resched();
4559de455b2SAtsushi Nemoto 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
45679ac6ba4SDavid Gibson 	}
45779ac6ba4SDavid Gibson }
45879ac6ba4SDavid Gibson 
459a5516438SAndi Kleen static void enqueue_huge_page(struct hstate *h, struct page *page)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	int nid = page_to_nid(page);
462a5516438SAndi Kleen 	list_add(&page->lru, &h->hugepage_freelists[nid]);
463a5516438SAndi Kleen 	h->free_huge_pages++;
464a5516438SAndi Kleen 	h->free_huge_pages_node[nid]++;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
467a5516438SAndi Kleen static struct page *dequeue_huge_page_vma(struct hstate *h,
468a5516438SAndi Kleen 				struct vm_area_struct *vma,
46904f2cbe3SMel Gorman 				unsigned long address, int avoid_reserve)
4701da177e4SLinus Torvalds {
47131a5c6e4SNishanth Aravamudan 	int nid;
4721da177e4SLinus Torvalds 	struct page *page = NULL;
473480eccf9SLee Schermerhorn 	struct mempolicy *mpol;
47419770b32SMel Gorman 	nodemask_t *nodemask;
475c0ff7453SMiao Xie 	struct zonelist *zonelist;
476dd1a239fSMel Gorman 	struct zone *zone;
477dd1a239fSMel Gorman 	struct zoneref *z;
4781da177e4SLinus Torvalds 
479c0ff7453SMiao Xie 	get_mems_allowed();
480c0ff7453SMiao Xie 	zonelist = huge_zonelist(vma, address,
481c0ff7453SMiao Xie 					htlb_alloc_mask, &mpol, &nodemask);
482a1e78772SMel Gorman 	/*
483a1e78772SMel Gorman 	 * A child process with MAP_PRIVATE mappings created by their parent
484a1e78772SMel Gorman 	 * have no page reserves. This check ensures that reservations are
485a1e78772SMel Gorman 	 * not "stolen". The child may still get SIGKILLed
486a1e78772SMel Gorman 	 */
4877f09ca51SMel Gorman 	if (!vma_has_reserves(vma) &&
488a5516438SAndi Kleen 			h->free_huge_pages - h->resv_huge_pages == 0)
489c0ff7453SMiao Xie 		goto err;
490a1e78772SMel Gorman 
49104f2cbe3SMel Gorman 	/* If reserves cannot be used, ensure enough pages are in the pool */
492a5516438SAndi Kleen 	if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
493c0ff7453SMiao Xie 		goto err;;
49404f2cbe3SMel Gorman 
49519770b32SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
49619770b32SMel Gorman 						MAX_NR_ZONES - 1, nodemask) {
49754a6eb5cSMel Gorman 		nid = zone_to_nid(zone);
49854a6eb5cSMel Gorman 		if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
499a5516438SAndi Kleen 		    !list_empty(&h->hugepage_freelists[nid])) {
500a5516438SAndi Kleen 			page = list_entry(h->hugepage_freelists[nid].next,
5011da177e4SLinus Torvalds 					  struct page, lru);
5021da177e4SLinus Torvalds 			list_del(&page->lru);
503a5516438SAndi Kleen 			h->free_huge_pages--;
504a5516438SAndi Kleen 			h->free_huge_pages_node[nid]--;
50504f2cbe3SMel Gorman 
50604f2cbe3SMel Gorman 			if (!avoid_reserve)
507a5516438SAndi Kleen 				decrement_hugepage_resv_vma(h, vma);
508a1e78772SMel Gorman 
5095ab3ee7bSKen Chen 			break;
5101da177e4SLinus Torvalds 		}
5113abf7afdSAndrew Morton 	}
512c0ff7453SMiao Xie err:
51352cd3b07SLee Schermerhorn 	mpol_cond_put(mpol);
514c0ff7453SMiao Xie 	put_mems_allowed();
5151da177e4SLinus Torvalds 	return page;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
518a5516438SAndi Kleen static void update_and_free_page(struct hstate *h, struct page *page)
5196af2acb6SAdam Litke {
5206af2acb6SAdam Litke 	int i;
521a5516438SAndi Kleen 
52218229df5SAndy Whitcroft 	VM_BUG_ON(h->order >= MAX_ORDER);
52318229df5SAndy Whitcroft 
524a5516438SAndi Kleen 	h->nr_huge_pages--;
525a5516438SAndi Kleen 	h->nr_huge_pages_node[page_to_nid(page)]--;
526a5516438SAndi Kleen 	for (i = 0; i < pages_per_huge_page(h); i++) {
5276af2acb6SAdam Litke 		page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
5286af2acb6SAdam Litke 				1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
5296af2acb6SAdam Litke 				1 << PG_private | 1<< PG_writeback);
5306af2acb6SAdam Litke 	}
5316af2acb6SAdam Litke 	set_compound_page_dtor(page, NULL);
5326af2acb6SAdam Litke 	set_page_refcounted(page);
5337f2e9525SGerald Schaefer 	arch_release_hugepage(page);
534a5516438SAndi Kleen 	__free_pages(page, huge_page_order(h));
5356af2acb6SAdam Litke }
5366af2acb6SAdam Litke 
537e5ff2159SAndi Kleen struct hstate *size_to_hstate(unsigned long size)
538e5ff2159SAndi Kleen {
539e5ff2159SAndi Kleen 	struct hstate *h;
540e5ff2159SAndi Kleen 
541e5ff2159SAndi Kleen 	for_each_hstate(h) {
542e5ff2159SAndi Kleen 		if (huge_page_size(h) == size)
543e5ff2159SAndi Kleen 			return h;
544e5ff2159SAndi Kleen 	}
545e5ff2159SAndi Kleen 	return NULL;
546e5ff2159SAndi Kleen }
547e5ff2159SAndi Kleen 
54827a85ef1SDavid Gibson static void free_huge_page(struct page *page)
54927a85ef1SDavid Gibson {
550a5516438SAndi Kleen 	/*
551a5516438SAndi Kleen 	 * Can't pass hstate in here because it is called from the
552a5516438SAndi Kleen 	 * compound page destructor.
553a5516438SAndi Kleen 	 */
554e5ff2159SAndi Kleen 	struct hstate *h = page_hstate(page);
5557893d1d5SAdam Litke 	int nid = page_to_nid(page);
556c79fb75eSAdam Litke 	struct address_space *mapping;
55727a85ef1SDavid Gibson 
558c79fb75eSAdam Litke 	mapping = (struct address_space *) page_private(page);
559e5df70abSAndy Whitcroft 	set_page_private(page, 0);
56023be7468SMel Gorman 	page->mapping = NULL;
5617893d1d5SAdam Litke 	BUG_ON(page_count(page));
5620fe6e20bSNaoya Horiguchi 	BUG_ON(page_mapcount(page));
56327a85ef1SDavid Gibson 	INIT_LIST_HEAD(&page->lru);
56427a85ef1SDavid Gibson 
56527a85ef1SDavid Gibson 	spin_lock(&hugetlb_lock);
566aa888a74SAndi Kleen 	if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
567a5516438SAndi Kleen 		update_and_free_page(h, page);
568a5516438SAndi Kleen 		h->surplus_huge_pages--;
569a5516438SAndi Kleen 		h->surplus_huge_pages_node[nid]--;
5707893d1d5SAdam Litke 	} else {
571a5516438SAndi Kleen 		enqueue_huge_page(h, page);
5727893d1d5SAdam Litke 	}
57327a85ef1SDavid Gibson 	spin_unlock(&hugetlb_lock);
574c79fb75eSAdam Litke 	if (mapping)
5759a119c05SAdam Litke 		hugetlb_put_quota(mapping, 1);
57627a85ef1SDavid Gibson }
57727a85ef1SDavid Gibson 
578a5516438SAndi Kleen static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
579b7ba30c6SAndi Kleen {
580b7ba30c6SAndi Kleen 	set_compound_page_dtor(page, free_huge_page);
581b7ba30c6SAndi Kleen 	spin_lock(&hugetlb_lock);
582a5516438SAndi Kleen 	h->nr_huge_pages++;
583a5516438SAndi Kleen 	h->nr_huge_pages_node[nid]++;
584b7ba30c6SAndi Kleen 	spin_unlock(&hugetlb_lock);
585b7ba30c6SAndi Kleen 	put_page(page); /* free it into the hugepage allocator */
586b7ba30c6SAndi Kleen }
587b7ba30c6SAndi Kleen 
58820a0307cSWu Fengguang static void prep_compound_gigantic_page(struct page *page, unsigned long order)
58920a0307cSWu Fengguang {
59020a0307cSWu Fengguang 	int i;
59120a0307cSWu Fengguang 	int nr_pages = 1 << order;
59220a0307cSWu Fengguang 	struct page *p = page + 1;
59320a0307cSWu Fengguang 
59420a0307cSWu Fengguang 	/* we rely on prep_new_huge_page to set the destructor */
59520a0307cSWu Fengguang 	set_compound_order(page, order);
59620a0307cSWu Fengguang 	__SetPageHead(page);
59720a0307cSWu Fengguang 	for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
59820a0307cSWu Fengguang 		__SetPageTail(p);
59920a0307cSWu Fengguang 		p->first_page = page;
60020a0307cSWu Fengguang 	}
60120a0307cSWu Fengguang }
60220a0307cSWu Fengguang 
60320a0307cSWu Fengguang int PageHuge(struct page *page)
60420a0307cSWu Fengguang {
60520a0307cSWu Fengguang 	compound_page_dtor *dtor;
60620a0307cSWu Fengguang 
60720a0307cSWu Fengguang 	if (!PageCompound(page))
60820a0307cSWu Fengguang 		return 0;
60920a0307cSWu Fengguang 
61020a0307cSWu Fengguang 	page = compound_head(page);
61120a0307cSWu Fengguang 	dtor = get_compound_page_dtor(page);
61220a0307cSWu Fengguang 
61320a0307cSWu Fengguang 	return dtor == free_huge_page;
61420a0307cSWu Fengguang }
61520a0307cSWu Fengguang 
616a5516438SAndi Kleen static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds 	struct page *page;
619f96efd58SJoe Jin 
620aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
621aa888a74SAndi Kleen 		return NULL;
622aa888a74SAndi Kleen 
6236484eb3eSMel Gorman 	page = alloc_pages_exact_node(nid,
624551883aeSNishanth Aravamudan 		htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
625551883aeSNishanth Aravamudan 						__GFP_REPEAT|__GFP_NOWARN,
626a5516438SAndi Kleen 		huge_page_order(h));
6271da177e4SLinus Torvalds 	if (page) {
6287f2e9525SGerald Schaefer 		if (arch_prepare_hugepage(page)) {
629caff3a2cSGerald Schaefer 			__free_pages(page, huge_page_order(h));
6307b8ee84dSHarvey Harrison 			return NULL;
6317f2e9525SGerald Schaefer 		}
632a5516438SAndi Kleen 		prep_new_huge_page(h, page, nid);
6331da177e4SLinus Torvalds 	}
63463b4613cSNishanth Aravamudan 
63563b4613cSNishanth Aravamudan 	return page;
63663b4613cSNishanth Aravamudan }
63763b4613cSNishanth Aravamudan 
6385ced66c9SAndi Kleen /*
6396ae11b27SLee Schermerhorn  * common helper functions for hstate_next_node_to_{alloc|free}.
6406ae11b27SLee Schermerhorn  * We may have allocated or freed a huge page based on a different
6416ae11b27SLee Schermerhorn  * nodes_allowed previously, so h->next_node_to_{alloc|free} might
6426ae11b27SLee Schermerhorn  * be outside of *nodes_allowed.  Ensure that we use an allowed
6436ae11b27SLee Schermerhorn  * node for alloc or free.
6449a76db09SLee Schermerhorn  */
6456ae11b27SLee Schermerhorn static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
6469a76db09SLee Schermerhorn {
6476ae11b27SLee Schermerhorn 	nid = next_node(nid, *nodes_allowed);
6489a76db09SLee Schermerhorn 	if (nid == MAX_NUMNODES)
6496ae11b27SLee Schermerhorn 		nid = first_node(*nodes_allowed);
6509a76db09SLee Schermerhorn 	VM_BUG_ON(nid >= MAX_NUMNODES);
6519a76db09SLee Schermerhorn 
6529a76db09SLee Schermerhorn 	return nid;
6539a76db09SLee Schermerhorn }
6549a76db09SLee Schermerhorn 
6556ae11b27SLee Schermerhorn static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
6565ced66c9SAndi Kleen {
6576ae11b27SLee Schermerhorn 	if (!node_isset(nid, *nodes_allowed))
6586ae11b27SLee Schermerhorn 		nid = next_node_allowed(nid, nodes_allowed);
6599a76db09SLee Schermerhorn 	return nid;
6605ced66c9SAndi Kleen }
6615ced66c9SAndi Kleen 
6626ae11b27SLee Schermerhorn /*
6636ae11b27SLee Schermerhorn  * returns the previously saved node ["this node"] from which to
6646ae11b27SLee Schermerhorn  * allocate a persistent huge page for the pool and advance the
6656ae11b27SLee Schermerhorn  * next node from which to allocate, handling wrap at end of node
6666ae11b27SLee Schermerhorn  * mask.
6676ae11b27SLee Schermerhorn  */
6686ae11b27SLee Schermerhorn static int hstate_next_node_to_alloc(struct hstate *h,
6696ae11b27SLee Schermerhorn 					nodemask_t *nodes_allowed)
6706ae11b27SLee Schermerhorn {
6716ae11b27SLee Schermerhorn 	int nid;
6726ae11b27SLee Schermerhorn 
6736ae11b27SLee Schermerhorn 	VM_BUG_ON(!nodes_allowed);
6746ae11b27SLee Schermerhorn 
6756ae11b27SLee Schermerhorn 	nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
6766ae11b27SLee Schermerhorn 	h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
6776ae11b27SLee Schermerhorn 
6786ae11b27SLee Schermerhorn 	return nid;
6796ae11b27SLee Schermerhorn }
6806ae11b27SLee Schermerhorn 
6816ae11b27SLee Schermerhorn static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
68263b4613cSNishanth Aravamudan {
68363b4613cSNishanth Aravamudan 	struct page *page;
68463b4613cSNishanth Aravamudan 	int start_nid;
68563b4613cSNishanth Aravamudan 	int next_nid;
68663b4613cSNishanth Aravamudan 	int ret = 0;
68763b4613cSNishanth Aravamudan 
6886ae11b27SLee Schermerhorn 	start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
689e8c5c824SLee Schermerhorn 	next_nid = start_nid;
69063b4613cSNishanth Aravamudan 
69163b4613cSNishanth Aravamudan 	do {
692e8c5c824SLee Schermerhorn 		page = alloc_fresh_huge_page_node(h, next_nid);
6939a76db09SLee Schermerhorn 		if (page) {
69463b4613cSNishanth Aravamudan 			ret = 1;
6959a76db09SLee Schermerhorn 			break;
6969a76db09SLee Schermerhorn 		}
6976ae11b27SLee Schermerhorn 		next_nid = hstate_next_node_to_alloc(h, nodes_allowed);
6989a76db09SLee Schermerhorn 	} while (next_nid != start_nid);
69963b4613cSNishanth Aravamudan 
7003b116300SAdam Litke 	if (ret)
7013b116300SAdam Litke 		count_vm_event(HTLB_BUDDY_PGALLOC);
7023b116300SAdam Litke 	else
7033b116300SAdam Litke 		count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
7043b116300SAdam Litke 
70563b4613cSNishanth Aravamudan 	return ret;
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
708e8c5c824SLee Schermerhorn /*
7096ae11b27SLee Schermerhorn  * helper for free_pool_huge_page() - return the previously saved
7106ae11b27SLee Schermerhorn  * node ["this node"] from which to free a huge page.  Advance the
7116ae11b27SLee Schermerhorn  * next node id whether or not we find a free huge page to free so
7126ae11b27SLee Schermerhorn  * that the next attempt to free addresses the next node.
713e8c5c824SLee Schermerhorn  */
7146ae11b27SLee Schermerhorn static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
715e8c5c824SLee Schermerhorn {
7166ae11b27SLee Schermerhorn 	int nid;
7179a76db09SLee Schermerhorn 
7186ae11b27SLee Schermerhorn 	VM_BUG_ON(!nodes_allowed);
7196ae11b27SLee Schermerhorn 
7206ae11b27SLee Schermerhorn 	nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
7216ae11b27SLee Schermerhorn 	h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
7226ae11b27SLee Schermerhorn 
7239a76db09SLee Schermerhorn 	return nid;
724e8c5c824SLee Schermerhorn }
725e8c5c824SLee Schermerhorn 
726e8c5c824SLee Schermerhorn /*
727e8c5c824SLee Schermerhorn  * Free huge page from pool from next node to free.
728e8c5c824SLee Schermerhorn  * Attempt to keep persistent huge pages more or less
729e8c5c824SLee Schermerhorn  * balanced over allowed nodes.
730e8c5c824SLee Schermerhorn  * Called with hugetlb_lock locked.
731e8c5c824SLee Schermerhorn  */
7326ae11b27SLee Schermerhorn static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
7336ae11b27SLee Schermerhorn 							 bool acct_surplus)
734e8c5c824SLee Schermerhorn {
735e8c5c824SLee Schermerhorn 	int start_nid;
736e8c5c824SLee Schermerhorn 	int next_nid;
737e8c5c824SLee Schermerhorn 	int ret = 0;
738e8c5c824SLee Schermerhorn 
7396ae11b27SLee Schermerhorn 	start_nid = hstate_next_node_to_free(h, nodes_allowed);
740e8c5c824SLee Schermerhorn 	next_nid = start_nid;
741e8c5c824SLee Schermerhorn 
742e8c5c824SLee Schermerhorn 	do {
743685f3457SLee Schermerhorn 		/*
744685f3457SLee Schermerhorn 		 * If we're returning unused surplus pages, only examine
745685f3457SLee Schermerhorn 		 * nodes with surplus pages.
746685f3457SLee Schermerhorn 		 */
747685f3457SLee Schermerhorn 		if ((!acct_surplus || h->surplus_huge_pages_node[next_nid]) &&
748685f3457SLee Schermerhorn 		    !list_empty(&h->hugepage_freelists[next_nid])) {
749e8c5c824SLee Schermerhorn 			struct page *page =
750e8c5c824SLee Schermerhorn 				list_entry(h->hugepage_freelists[next_nid].next,
751e8c5c824SLee Schermerhorn 					  struct page, lru);
752e8c5c824SLee Schermerhorn 			list_del(&page->lru);
753e8c5c824SLee Schermerhorn 			h->free_huge_pages--;
754e8c5c824SLee Schermerhorn 			h->free_huge_pages_node[next_nid]--;
755685f3457SLee Schermerhorn 			if (acct_surplus) {
756685f3457SLee Schermerhorn 				h->surplus_huge_pages--;
757685f3457SLee Schermerhorn 				h->surplus_huge_pages_node[next_nid]--;
758685f3457SLee Schermerhorn 			}
759e8c5c824SLee Schermerhorn 			update_and_free_page(h, page);
760e8c5c824SLee Schermerhorn 			ret = 1;
7619a76db09SLee Schermerhorn 			break;
762e8c5c824SLee Schermerhorn 		}
7636ae11b27SLee Schermerhorn 		next_nid = hstate_next_node_to_free(h, nodes_allowed);
7649a76db09SLee Schermerhorn 	} while (next_nid != start_nid);
765e8c5c824SLee Schermerhorn 
766e8c5c824SLee Schermerhorn 	return ret;
767e8c5c824SLee Schermerhorn }
768e8c5c824SLee Schermerhorn 
769a5516438SAndi Kleen static struct page *alloc_buddy_huge_page(struct hstate *h,
770a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long address)
7717893d1d5SAdam Litke {
7727893d1d5SAdam Litke 	struct page *page;
773d1c3fb1fSNishanth Aravamudan 	unsigned int nid;
7747893d1d5SAdam Litke 
775aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
776aa888a74SAndi Kleen 		return NULL;
777aa888a74SAndi Kleen 
778d1c3fb1fSNishanth Aravamudan 	/*
779d1c3fb1fSNishanth Aravamudan 	 * Assume we will successfully allocate the surplus page to
780d1c3fb1fSNishanth Aravamudan 	 * prevent racing processes from causing the surplus to exceed
781d1c3fb1fSNishanth Aravamudan 	 * overcommit
782d1c3fb1fSNishanth Aravamudan 	 *
783d1c3fb1fSNishanth Aravamudan 	 * This however introduces a different race, where a process B
784d1c3fb1fSNishanth Aravamudan 	 * tries to grow the static hugepage pool while alloc_pages() is
785d1c3fb1fSNishanth Aravamudan 	 * called by process A. B will only examine the per-node
786d1c3fb1fSNishanth Aravamudan 	 * counters in determining if surplus huge pages can be
787d1c3fb1fSNishanth Aravamudan 	 * converted to normal huge pages in adjust_pool_surplus(). A
788d1c3fb1fSNishanth Aravamudan 	 * won't be able to increment the per-node counter, until the
789d1c3fb1fSNishanth Aravamudan 	 * lock is dropped by B, but B doesn't drop hugetlb_lock until
790d1c3fb1fSNishanth Aravamudan 	 * no more huge pages can be converted from surplus to normal
791d1c3fb1fSNishanth Aravamudan 	 * state (and doesn't try to convert again). Thus, we have a
792d1c3fb1fSNishanth Aravamudan 	 * case where a surplus huge page exists, the pool is grown, and
793d1c3fb1fSNishanth Aravamudan 	 * the surplus huge page still exists after, even though it
794d1c3fb1fSNishanth Aravamudan 	 * should just have been converted to a normal huge page. This
795d1c3fb1fSNishanth Aravamudan 	 * does not leak memory, though, as the hugepage will be freed
796d1c3fb1fSNishanth Aravamudan 	 * once it is out of use. It also does not allow the counters to
797d1c3fb1fSNishanth Aravamudan 	 * go out of whack in adjust_pool_surplus() as we don't modify
798d1c3fb1fSNishanth Aravamudan 	 * the node values until we've gotten the hugepage and only the
799d1c3fb1fSNishanth Aravamudan 	 * per-node value is checked there.
800d1c3fb1fSNishanth Aravamudan 	 */
801d1c3fb1fSNishanth Aravamudan 	spin_lock(&hugetlb_lock);
802a5516438SAndi Kleen 	if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
803d1c3fb1fSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
804d1c3fb1fSNishanth Aravamudan 		return NULL;
805d1c3fb1fSNishanth Aravamudan 	} else {
806a5516438SAndi Kleen 		h->nr_huge_pages++;
807a5516438SAndi Kleen 		h->surplus_huge_pages++;
808d1c3fb1fSNishanth Aravamudan 	}
809d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
810d1c3fb1fSNishanth Aravamudan 
811551883aeSNishanth Aravamudan 	page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
812551883aeSNishanth Aravamudan 					__GFP_REPEAT|__GFP_NOWARN,
813a5516438SAndi Kleen 					huge_page_order(h));
814d1c3fb1fSNishanth Aravamudan 
815caff3a2cSGerald Schaefer 	if (page && arch_prepare_hugepage(page)) {
816caff3a2cSGerald Schaefer 		__free_pages(page, huge_page_order(h));
817caff3a2cSGerald Schaefer 		return NULL;
818caff3a2cSGerald Schaefer 	}
819caff3a2cSGerald Schaefer 
8207893d1d5SAdam Litke 	spin_lock(&hugetlb_lock);
821d1c3fb1fSNishanth Aravamudan 	if (page) {
8222668db91SAdam Litke 		/*
8232668db91SAdam Litke 		 * This page is now managed by the hugetlb allocator and has
8242668db91SAdam Litke 		 * no users -- drop the buddy allocator's reference.
8252668db91SAdam Litke 		 */
8262668db91SAdam Litke 		put_page_testzero(page);
8272668db91SAdam Litke 		VM_BUG_ON(page_count(page));
828d1c3fb1fSNishanth Aravamudan 		nid = page_to_nid(page);
829d1c3fb1fSNishanth Aravamudan 		set_compound_page_dtor(page, free_huge_page);
830d1c3fb1fSNishanth Aravamudan 		/*
831d1c3fb1fSNishanth Aravamudan 		 * We incremented the global counters already
832d1c3fb1fSNishanth Aravamudan 		 */
833a5516438SAndi Kleen 		h->nr_huge_pages_node[nid]++;
834a5516438SAndi Kleen 		h->surplus_huge_pages_node[nid]++;
8353b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC);
836d1c3fb1fSNishanth Aravamudan 	} else {
837a5516438SAndi Kleen 		h->nr_huge_pages--;
838a5516438SAndi Kleen 		h->surplus_huge_pages--;
8393b116300SAdam Litke 		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
8407893d1d5SAdam Litke 	}
841d1c3fb1fSNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
8427893d1d5SAdam Litke 
8437893d1d5SAdam Litke 	return page;
8447893d1d5SAdam Litke }
8457893d1d5SAdam Litke 
846e4e574b7SAdam Litke /*
847e4e574b7SAdam Litke  * Increase the hugetlb pool such that it can accomodate a reservation
848e4e574b7SAdam Litke  * of size 'delta'.
849e4e574b7SAdam Litke  */
850a5516438SAndi Kleen static int gather_surplus_pages(struct hstate *h, int delta)
851e4e574b7SAdam Litke {
852e4e574b7SAdam Litke 	struct list_head surplus_list;
853e4e574b7SAdam Litke 	struct page *page, *tmp;
854e4e574b7SAdam Litke 	int ret, i;
855e4e574b7SAdam Litke 	int needed, allocated;
856e4e574b7SAdam Litke 
857a5516438SAndi Kleen 	needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
858ac09b3a1SAdam Litke 	if (needed <= 0) {
859a5516438SAndi Kleen 		h->resv_huge_pages += delta;
860e4e574b7SAdam Litke 		return 0;
861ac09b3a1SAdam Litke 	}
862e4e574b7SAdam Litke 
863e4e574b7SAdam Litke 	allocated = 0;
864e4e574b7SAdam Litke 	INIT_LIST_HEAD(&surplus_list);
865e4e574b7SAdam Litke 
866e4e574b7SAdam Litke 	ret = -ENOMEM;
867e4e574b7SAdam Litke retry:
868e4e574b7SAdam Litke 	spin_unlock(&hugetlb_lock);
869e4e574b7SAdam Litke 	for (i = 0; i < needed; i++) {
870a5516438SAndi Kleen 		page = alloc_buddy_huge_page(h, NULL, 0);
871e4e574b7SAdam Litke 		if (!page) {
872e4e574b7SAdam Litke 			/*
873e4e574b7SAdam Litke 			 * We were not able to allocate enough pages to
874e4e574b7SAdam Litke 			 * satisfy the entire reservation so we free what
875e4e574b7SAdam Litke 			 * we've allocated so far.
876e4e574b7SAdam Litke 			 */
877e4e574b7SAdam Litke 			spin_lock(&hugetlb_lock);
878e4e574b7SAdam Litke 			needed = 0;
879e4e574b7SAdam Litke 			goto free;
880e4e574b7SAdam Litke 		}
881e4e574b7SAdam Litke 
882e4e574b7SAdam Litke 		list_add(&page->lru, &surplus_list);
883e4e574b7SAdam Litke 	}
884e4e574b7SAdam Litke 	allocated += needed;
885e4e574b7SAdam Litke 
886e4e574b7SAdam Litke 	/*
887e4e574b7SAdam Litke 	 * After retaking hugetlb_lock, we need to recalculate 'needed'
888e4e574b7SAdam Litke 	 * because either resv_huge_pages or free_huge_pages may have changed.
889e4e574b7SAdam Litke 	 */
890e4e574b7SAdam Litke 	spin_lock(&hugetlb_lock);
891a5516438SAndi Kleen 	needed = (h->resv_huge_pages + delta) -
892a5516438SAndi Kleen 			(h->free_huge_pages + allocated);
893e4e574b7SAdam Litke 	if (needed > 0)
894e4e574b7SAdam Litke 		goto retry;
895e4e574b7SAdam Litke 
896e4e574b7SAdam Litke 	/*
897e4e574b7SAdam Litke 	 * The surplus_list now contains _at_least_ the number of extra pages
898e4e574b7SAdam Litke 	 * needed to accomodate the reservation.  Add the appropriate number
899e4e574b7SAdam Litke 	 * of pages to the hugetlb pool and free the extras back to the buddy
900ac09b3a1SAdam Litke 	 * allocator.  Commit the entire reservation here to prevent another
901ac09b3a1SAdam Litke 	 * process from stealing the pages as they are added to the pool but
902ac09b3a1SAdam Litke 	 * before they are reserved.
903e4e574b7SAdam Litke 	 */
904e4e574b7SAdam Litke 	needed += allocated;
905a5516438SAndi Kleen 	h->resv_huge_pages += delta;
906e4e574b7SAdam Litke 	ret = 0;
907e4e574b7SAdam Litke free:
90819fc3f0aSAdam Litke 	/* Free the needed pages to the hugetlb pool */
90919fc3f0aSAdam Litke 	list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
91019fc3f0aSAdam Litke 		if ((--needed) < 0)
91119fc3f0aSAdam Litke 			break;
91219fc3f0aSAdam Litke 		list_del(&page->lru);
913a5516438SAndi Kleen 		enqueue_huge_page(h, page);
91419fc3f0aSAdam Litke 	}
91519fc3f0aSAdam Litke 
91619fc3f0aSAdam Litke 	/* Free unnecessary surplus pages to the buddy allocator */
91719fc3f0aSAdam Litke 	if (!list_empty(&surplus_list)) {
91819fc3f0aSAdam Litke 		spin_unlock(&hugetlb_lock);
919e4e574b7SAdam Litke 		list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
920e4e574b7SAdam Litke 			list_del(&page->lru);
921af767cbdSAdam Litke 			/*
9222668db91SAdam Litke 			 * The page has a reference count of zero already, so
9232668db91SAdam Litke 			 * call free_huge_page directly instead of using
9242668db91SAdam Litke 			 * put_page.  This must be done with hugetlb_lock
925af767cbdSAdam Litke 			 * unlocked which is safe because free_huge_page takes
926af767cbdSAdam Litke 			 * hugetlb_lock before deciding how to free the page.
927af767cbdSAdam Litke 			 */
9282668db91SAdam Litke 			free_huge_page(page);
929af767cbdSAdam Litke 		}
93019fc3f0aSAdam Litke 		spin_lock(&hugetlb_lock);
931e4e574b7SAdam Litke 	}
932e4e574b7SAdam Litke 
933e4e574b7SAdam Litke 	return ret;
934e4e574b7SAdam Litke }
935e4e574b7SAdam Litke 
936e4e574b7SAdam Litke /*
937e4e574b7SAdam Litke  * When releasing a hugetlb pool reservation, any surplus pages that were
938e4e574b7SAdam Litke  * allocated to satisfy the reservation must be explicitly freed if they were
939e4e574b7SAdam Litke  * never used.
940685f3457SLee Schermerhorn  * Called with hugetlb_lock held.
941e4e574b7SAdam Litke  */
942a5516438SAndi Kleen static void return_unused_surplus_pages(struct hstate *h,
943a5516438SAndi Kleen 					unsigned long unused_resv_pages)
944e4e574b7SAdam Litke {
945e4e574b7SAdam Litke 	unsigned long nr_pages;
946e4e574b7SAdam Litke 
947ac09b3a1SAdam Litke 	/* Uncommit the reservation */
948a5516438SAndi Kleen 	h->resv_huge_pages -= unused_resv_pages;
949ac09b3a1SAdam Litke 
950aa888a74SAndi Kleen 	/* Cannot return gigantic pages currently */
951aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
952aa888a74SAndi Kleen 		return;
953aa888a74SAndi Kleen 
954a5516438SAndi Kleen 	nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
955e4e574b7SAdam Litke 
956685f3457SLee Schermerhorn 	/*
957685f3457SLee Schermerhorn 	 * We want to release as many surplus pages as possible, spread
9589b5e5d0fSLee Schermerhorn 	 * evenly across all nodes with memory. Iterate across these nodes
9599b5e5d0fSLee Schermerhorn 	 * until we can no longer free unreserved surplus pages. This occurs
9609b5e5d0fSLee Schermerhorn 	 * when the nodes with surplus pages have no free pages.
9619b5e5d0fSLee Schermerhorn 	 * free_pool_huge_page() will balance the the freed pages across the
9629b5e5d0fSLee Schermerhorn 	 * on-line nodes with memory and will handle the hstate accounting.
963685f3457SLee Schermerhorn 	 */
964685f3457SLee Schermerhorn 	while (nr_pages--) {
9659b5e5d0fSLee Schermerhorn 		if (!free_pool_huge_page(h, &node_states[N_HIGH_MEMORY], 1))
966685f3457SLee Schermerhorn 			break;
967e4e574b7SAdam Litke 	}
968e4e574b7SAdam Litke }
969e4e574b7SAdam Litke 
970c37f9fb1SAndy Whitcroft /*
971c37f9fb1SAndy Whitcroft  * Determine if the huge page at addr within the vma has an associated
972c37f9fb1SAndy Whitcroft  * reservation.  Where it does not we will need to logically increase
973c37f9fb1SAndy Whitcroft  * reservation and actually increase quota before an allocation can occur.
974c37f9fb1SAndy Whitcroft  * Where any new reservation would be required the reservation change is
975c37f9fb1SAndy Whitcroft  * prepared, but not committed.  Once the page has been quota'd allocated
976c37f9fb1SAndy Whitcroft  * an instantiated the change should be committed via vma_commit_reservation.
977c37f9fb1SAndy Whitcroft  * No action is required on failure.
978c37f9fb1SAndy Whitcroft  */
979e2f17d94SRoel Kluin static long vma_needs_reservation(struct hstate *h,
980a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long addr)
981c37f9fb1SAndy Whitcroft {
982c37f9fb1SAndy Whitcroft 	struct address_space *mapping = vma->vm_file->f_mapping;
983c37f9fb1SAndy Whitcroft 	struct inode *inode = mapping->host;
984c37f9fb1SAndy Whitcroft 
985f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE) {
986a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
987c37f9fb1SAndy Whitcroft 		return region_chg(&inode->i_mapping->private_list,
988c37f9fb1SAndy Whitcroft 							idx, idx + 1);
989c37f9fb1SAndy Whitcroft 
99084afd99bSAndy Whitcroft 	} else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
991c37f9fb1SAndy Whitcroft 		return 1;
992c37f9fb1SAndy Whitcroft 
99384afd99bSAndy Whitcroft 	} else  {
994e2f17d94SRoel Kluin 		long err;
995a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
99684afd99bSAndy Whitcroft 		struct resv_map *reservations = vma_resv_map(vma);
99784afd99bSAndy Whitcroft 
99884afd99bSAndy Whitcroft 		err = region_chg(&reservations->regions, idx, idx + 1);
99984afd99bSAndy Whitcroft 		if (err < 0)
100084afd99bSAndy Whitcroft 			return err;
1001c37f9fb1SAndy Whitcroft 		return 0;
1002c37f9fb1SAndy Whitcroft 	}
100384afd99bSAndy Whitcroft }
1004a5516438SAndi Kleen static void vma_commit_reservation(struct hstate *h,
1005a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long addr)
1006c37f9fb1SAndy Whitcroft {
1007c37f9fb1SAndy Whitcroft 	struct address_space *mapping = vma->vm_file->f_mapping;
1008c37f9fb1SAndy Whitcroft 	struct inode *inode = mapping->host;
1009c37f9fb1SAndy Whitcroft 
1010f83a275dSMel Gorman 	if (vma->vm_flags & VM_MAYSHARE) {
1011a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
1012c37f9fb1SAndy Whitcroft 		region_add(&inode->i_mapping->private_list, idx, idx + 1);
101384afd99bSAndy Whitcroft 
101484afd99bSAndy Whitcroft 	} else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1015a5516438SAndi Kleen 		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
101684afd99bSAndy Whitcroft 		struct resv_map *reservations = vma_resv_map(vma);
101784afd99bSAndy Whitcroft 
101884afd99bSAndy Whitcroft 		/* Mark this page used in the map. */
101984afd99bSAndy Whitcroft 		region_add(&reservations->regions, idx, idx + 1);
1020c37f9fb1SAndy Whitcroft 	}
1021c37f9fb1SAndy Whitcroft }
1022c37f9fb1SAndy Whitcroft 
1023348ea204SAdam Litke static struct page *alloc_huge_page(struct vm_area_struct *vma,
102404f2cbe3SMel Gorman 				    unsigned long addr, int avoid_reserve)
1025348ea204SAdam Litke {
1026a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
1027348ea204SAdam Litke 	struct page *page;
10282fc39cecSAdam Litke 	struct address_space *mapping = vma->vm_file->f_mapping;
1029a1e78772SMel Gorman 	struct inode *inode = mapping->host;
1030e2f17d94SRoel Kluin 	long chg;
10312fc39cecSAdam Litke 
1032a1e78772SMel Gorman 	/*
1033a1e78772SMel Gorman 	 * Processes that did not create the mapping will have no reserves and
1034a1e78772SMel Gorman 	 * will not have accounted against quota. Check that the quota can be
1035a1e78772SMel Gorman 	 * made before satisfying the allocation
1036c37f9fb1SAndy Whitcroft 	 * MAP_NORESERVE mappings may also need pages and quota allocated
1037c37f9fb1SAndy Whitcroft 	 * if no reserve mapping overlaps.
1038a1e78772SMel Gorman 	 */
1039a5516438SAndi Kleen 	chg = vma_needs_reservation(h, vma, addr);
1040c37f9fb1SAndy Whitcroft 	if (chg < 0)
1041c37f9fb1SAndy Whitcroft 		return ERR_PTR(chg);
1042c37f9fb1SAndy Whitcroft 	if (chg)
1043a1e78772SMel Gorman 		if (hugetlb_get_quota(inode->i_mapping, chg))
1044a1e78772SMel Gorman 			return ERR_PTR(-ENOSPC);
104590d8b7e6SAdam Litke 
1046a1e78772SMel Gorman 	spin_lock(&hugetlb_lock);
1047a5516438SAndi Kleen 	page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
1048a1e78772SMel Gorman 	spin_unlock(&hugetlb_lock);
1049a1e78772SMel Gorman 
1050a1e78772SMel Gorman 	if (!page) {
1051a5516438SAndi Kleen 		page = alloc_buddy_huge_page(h, vma, addr);
1052a1e78772SMel Gorman 		if (!page) {
1053a1e78772SMel Gorman 			hugetlb_put_quota(inode->i_mapping, chg);
10544a6018f7SMel Gorman 			return ERR_PTR(-VM_FAULT_SIGBUS);
1055a1e78772SMel Gorman 		}
1056a1e78772SMel Gorman 	}
1057a1e78772SMel Gorman 
1058348ea204SAdam Litke 	set_page_refcounted(page);
10592fc39cecSAdam Litke 	set_page_private(page, (unsigned long) mapping);
1060a1e78772SMel Gorman 
1061a5516438SAndi Kleen 	vma_commit_reservation(h, vma, addr);
1062c37f9fb1SAndy Whitcroft 
10637893d1d5SAdam Litke 	return page;
1064b45b5bd6SDavid Gibson }
1065b45b5bd6SDavid Gibson 
106691f47662SCyrill Gorcunov int __weak alloc_bootmem_huge_page(struct hstate *h)
1067aa888a74SAndi Kleen {
1068aa888a74SAndi Kleen 	struct huge_bootmem_page *m;
10699b5e5d0fSLee Schermerhorn 	int nr_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
1070aa888a74SAndi Kleen 
1071aa888a74SAndi Kleen 	while (nr_nodes) {
1072aa888a74SAndi Kleen 		void *addr;
1073aa888a74SAndi Kleen 
1074aa888a74SAndi Kleen 		addr = __alloc_bootmem_node_nopanic(
10756ae11b27SLee Schermerhorn 				NODE_DATA(hstate_next_node_to_alloc(h,
10769b5e5d0fSLee Schermerhorn 						&node_states[N_HIGH_MEMORY])),
1077aa888a74SAndi Kleen 				huge_page_size(h), huge_page_size(h), 0);
1078aa888a74SAndi Kleen 
1079aa888a74SAndi Kleen 		if (addr) {
1080aa888a74SAndi Kleen 			/*
1081aa888a74SAndi Kleen 			 * Use the beginning of the huge page to store the
1082aa888a74SAndi Kleen 			 * huge_bootmem_page struct (until gather_bootmem
1083aa888a74SAndi Kleen 			 * puts them into the mem_map).
1084aa888a74SAndi Kleen 			 */
1085aa888a74SAndi Kleen 			m = addr;
1086aa888a74SAndi Kleen 			goto found;
1087aa888a74SAndi Kleen 		}
1088aa888a74SAndi Kleen 		nr_nodes--;
1089aa888a74SAndi Kleen 	}
1090aa888a74SAndi Kleen 	return 0;
1091aa888a74SAndi Kleen 
1092aa888a74SAndi Kleen found:
1093aa888a74SAndi Kleen 	BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
1094aa888a74SAndi Kleen 	/* Put them into a private list first because mem_map is not up yet */
1095aa888a74SAndi Kleen 	list_add(&m->list, &huge_boot_pages);
1096aa888a74SAndi Kleen 	m->hstate = h;
1097aa888a74SAndi Kleen 	return 1;
1098aa888a74SAndi Kleen }
1099aa888a74SAndi Kleen 
110018229df5SAndy Whitcroft static void prep_compound_huge_page(struct page *page, int order)
110118229df5SAndy Whitcroft {
110218229df5SAndy Whitcroft 	if (unlikely(order > (MAX_ORDER - 1)))
110318229df5SAndy Whitcroft 		prep_compound_gigantic_page(page, order);
110418229df5SAndy Whitcroft 	else
110518229df5SAndy Whitcroft 		prep_compound_page(page, order);
110618229df5SAndy Whitcroft }
110718229df5SAndy Whitcroft 
1108aa888a74SAndi Kleen /* Put bootmem huge pages into the standard lists after mem_map is up */
1109aa888a74SAndi Kleen static void __init gather_bootmem_prealloc(void)
1110aa888a74SAndi Kleen {
1111aa888a74SAndi Kleen 	struct huge_bootmem_page *m;
1112aa888a74SAndi Kleen 
1113aa888a74SAndi Kleen 	list_for_each_entry(m, &huge_boot_pages, list) {
1114aa888a74SAndi Kleen 		struct page *page = virt_to_page(m);
1115aa888a74SAndi Kleen 		struct hstate *h = m->hstate;
1116aa888a74SAndi Kleen 		__ClearPageReserved(page);
1117aa888a74SAndi Kleen 		WARN_ON(page_count(page) != 1);
111818229df5SAndy Whitcroft 		prep_compound_huge_page(page, h->order);
1119aa888a74SAndi Kleen 		prep_new_huge_page(h, page, page_to_nid(page));
1120aa888a74SAndi Kleen 	}
1121aa888a74SAndi Kleen }
1122aa888a74SAndi Kleen 
11238faa8b07SAndi Kleen static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
11241da177e4SLinus Torvalds {
11251da177e4SLinus Torvalds 	unsigned long i;
11261da177e4SLinus Torvalds 
1127e5ff2159SAndi Kleen 	for (i = 0; i < h->max_huge_pages; ++i) {
1128aa888a74SAndi Kleen 		if (h->order >= MAX_ORDER) {
1129aa888a74SAndi Kleen 			if (!alloc_bootmem_huge_page(h))
1130aa888a74SAndi Kleen 				break;
11319b5e5d0fSLee Schermerhorn 		} else if (!alloc_fresh_huge_page(h,
11329b5e5d0fSLee Schermerhorn 					 &node_states[N_HIGH_MEMORY]))
11331da177e4SLinus Torvalds 			break;
11341da177e4SLinus Torvalds 	}
11358faa8b07SAndi Kleen 	h->max_huge_pages = i;
1136e5ff2159SAndi Kleen }
1137e5ff2159SAndi Kleen 
1138e5ff2159SAndi Kleen static void __init hugetlb_init_hstates(void)
1139e5ff2159SAndi Kleen {
1140e5ff2159SAndi Kleen 	struct hstate *h;
1141e5ff2159SAndi Kleen 
1142e5ff2159SAndi Kleen 	for_each_hstate(h) {
11438faa8b07SAndi Kleen 		/* oversize hugepages were init'ed in early boot */
11448faa8b07SAndi Kleen 		if (h->order < MAX_ORDER)
11458faa8b07SAndi Kleen 			hugetlb_hstate_alloc_pages(h);
1146e5ff2159SAndi Kleen 	}
1147e5ff2159SAndi Kleen }
1148e5ff2159SAndi Kleen 
11494abd32dbSAndi Kleen static char * __init memfmt(char *buf, unsigned long n)
11504abd32dbSAndi Kleen {
11514abd32dbSAndi Kleen 	if (n >= (1UL << 30))
11524abd32dbSAndi Kleen 		sprintf(buf, "%lu GB", n >> 30);
11534abd32dbSAndi Kleen 	else if (n >= (1UL << 20))
11544abd32dbSAndi Kleen 		sprintf(buf, "%lu MB", n >> 20);
11554abd32dbSAndi Kleen 	else
11564abd32dbSAndi Kleen 		sprintf(buf, "%lu KB", n >> 10);
11574abd32dbSAndi Kleen 	return buf;
11584abd32dbSAndi Kleen }
11594abd32dbSAndi Kleen 
1160e5ff2159SAndi Kleen static void __init report_hugepages(void)
1161e5ff2159SAndi Kleen {
1162e5ff2159SAndi Kleen 	struct hstate *h;
1163e5ff2159SAndi Kleen 
1164e5ff2159SAndi Kleen 	for_each_hstate(h) {
11654abd32dbSAndi Kleen 		char buf[32];
11664abd32dbSAndi Kleen 		printk(KERN_INFO "HugeTLB registered %s page size, "
11674abd32dbSAndi Kleen 				 "pre-allocated %ld pages\n",
11684abd32dbSAndi Kleen 			memfmt(buf, huge_page_size(h)),
11694abd32dbSAndi Kleen 			h->free_huge_pages);
1170e5ff2159SAndi Kleen 	}
1171e5ff2159SAndi Kleen }
1172e5ff2159SAndi Kleen 
11731da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM
11746ae11b27SLee Schermerhorn static void try_to_free_low(struct hstate *h, unsigned long count,
11756ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
11761da177e4SLinus Torvalds {
11774415cc8dSChristoph Lameter 	int i;
11784415cc8dSChristoph Lameter 
1179aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1180aa888a74SAndi Kleen 		return;
1181aa888a74SAndi Kleen 
11826ae11b27SLee Schermerhorn 	for_each_node_mask(i, *nodes_allowed) {
11831da177e4SLinus Torvalds 		struct page *page, *next;
1184a5516438SAndi Kleen 		struct list_head *freel = &h->hugepage_freelists[i];
1185a5516438SAndi Kleen 		list_for_each_entry_safe(page, next, freel, lru) {
1186a5516438SAndi Kleen 			if (count >= h->nr_huge_pages)
11876b0c880dSAdam Litke 				return;
11881da177e4SLinus Torvalds 			if (PageHighMem(page))
11891da177e4SLinus Torvalds 				continue;
11901da177e4SLinus Torvalds 			list_del(&page->lru);
1191e5ff2159SAndi Kleen 			update_and_free_page(h, page);
1192a5516438SAndi Kleen 			h->free_huge_pages--;
1193a5516438SAndi Kleen 			h->free_huge_pages_node[page_to_nid(page)]--;
11941da177e4SLinus Torvalds 		}
11951da177e4SLinus Torvalds 	}
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds #else
11986ae11b27SLee Schermerhorn static inline void try_to_free_low(struct hstate *h, unsigned long count,
11996ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
12001da177e4SLinus Torvalds {
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds #endif
12031da177e4SLinus Torvalds 
120420a0307cSWu Fengguang /*
120520a0307cSWu Fengguang  * Increment or decrement surplus_huge_pages.  Keep node-specific counters
120620a0307cSWu Fengguang  * balanced by operating on them in a round-robin fashion.
120720a0307cSWu Fengguang  * Returns 1 if an adjustment was made.
120820a0307cSWu Fengguang  */
12096ae11b27SLee Schermerhorn static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
12106ae11b27SLee Schermerhorn 				int delta)
121120a0307cSWu Fengguang {
1212e8c5c824SLee Schermerhorn 	int start_nid, next_nid;
121320a0307cSWu Fengguang 	int ret = 0;
121420a0307cSWu Fengguang 
121520a0307cSWu Fengguang 	VM_BUG_ON(delta != -1 && delta != 1);
121620a0307cSWu Fengguang 
1217e8c5c824SLee Schermerhorn 	if (delta < 0)
12186ae11b27SLee Schermerhorn 		start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
1219e8c5c824SLee Schermerhorn 	else
12206ae11b27SLee Schermerhorn 		start_nid = hstate_next_node_to_free(h, nodes_allowed);
1221e8c5c824SLee Schermerhorn 	next_nid = start_nid;
1222e8c5c824SLee Schermerhorn 
1223e8c5c824SLee Schermerhorn 	do {
1224e8c5c824SLee Schermerhorn 		int nid = next_nid;
1225e8c5c824SLee Schermerhorn 		if (delta < 0)  {
1226e8c5c824SLee Schermerhorn 			/*
1227e8c5c824SLee Schermerhorn 			 * To shrink on this node, there must be a surplus page
1228e8c5c824SLee Schermerhorn 			 */
12299a76db09SLee Schermerhorn 			if (!h->surplus_huge_pages_node[nid]) {
12306ae11b27SLee Schermerhorn 				next_nid = hstate_next_node_to_alloc(h,
12316ae11b27SLee Schermerhorn 								nodes_allowed);
123220a0307cSWu Fengguang 				continue;
1233e8c5c824SLee Schermerhorn 			}
12349a76db09SLee Schermerhorn 		}
1235e8c5c824SLee Schermerhorn 		if (delta > 0) {
1236e8c5c824SLee Schermerhorn 			/*
1237e8c5c824SLee Schermerhorn 			 * Surplus cannot exceed the total number of pages
1238e8c5c824SLee Schermerhorn 			 */
1239e8c5c824SLee Schermerhorn 			if (h->surplus_huge_pages_node[nid] >=
12409a76db09SLee Schermerhorn 						h->nr_huge_pages_node[nid]) {
12416ae11b27SLee Schermerhorn 				next_nid = hstate_next_node_to_free(h,
12426ae11b27SLee Schermerhorn 								nodes_allowed);
124320a0307cSWu Fengguang 				continue;
1244e8c5c824SLee Schermerhorn 			}
12459a76db09SLee Schermerhorn 		}
124620a0307cSWu Fengguang 
124720a0307cSWu Fengguang 		h->surplus_huge_pages += delta;
124820a0307cSWu Fengguang 		h->surplus_huge_pages_node[nid] += delta;
124920a0307cSWu Fengguang 		ret = 1;
125020a0307cSWu Fengguang 		break;
1251e8c5c824SLee Schermerhorn 	} while (next_nid != start_nid);
125220a0307cSWu Fengguang 
125320a0307cSWu Fengguang 	return ret;
125420a0307cSWu Fengguang }
125520a0307cSWu Fengguang 
1256a5516438SAndi Kleen #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
12576ae11b27SLee Schermerhorn static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
12586ae11b27SLee Schermerhorn 						nodemask_t *nodes_allowed)
12591da177e4SLinus Torvalds {
12607893d1d5SAdam Litke 	unsigned long min_count, ret;
12611da177e4SLinus Torvalds 
1262aa888a74SAndi Kleen 	if (h->order >= MAX_ORDER)
1263aa888a74SAndi Kleen 		return h->max_huge_pages;
1264aa888a74SAndi Kleen 
12657893d1d5SAdam Litke 	/*
12667893d1d5SAdam Litke 	 * Increase the pool size
12677893d1d5SAdam Litke 	 * First take pages out of surplus state.  Then make up the
12687893d1d5SAdam Litke 	 * remaining difference by allocating fresh huge pages.
1269d1c3fb1fSNishanth Aravamudan 	 *
1270d1c3fb1fSNishanth Aravamudan 	 * We might race with alloc_buddy_huge_page() here and be unable
1271d1c3fb1fSNishanth Aravamudan 	 * to convert a surplus huge page to a normal huge page. That is
1272d1c3fb1fSNishanth Aravamudan 	 * not critical, though, it just means the overall size of the
1273d1c3fb1fSNishanth Aravamudan 	 * pool might be one hugepage larger than it needs to be, but
1274d1c3fb1fSNishanth Aravamudan 	 * within all the constraints specified by the sysctls.
12757893d1d5SAdam Litke 	 */
12761da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
1277a5516438SAndi Kleen 	while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
12786ae11b27SLee Schermerhorn 		if (!adjust_pool_surplus(h, nodes_allowed, -1))
12797893d1d5SAdam Litke 			break;
12807893d1d5SAdam Litke 	}
12817893d1d5SAdam Litke 
1282a5516438SAndi Kleen 	while (count > persistent_huge_pages(h)) {
12837893d1d5SAdam Litke 		/*
12847893d1d5SAdam Litke 		 * If this allocation races such that we no longer need the
12857893d1d5SAdam Litke 		 * page, free_huge_page will handle it by freeing the page
12867893d1d5SAdam Litke 		 * and reducing the surplus.
12877893d1d5SAdam Litke 		 */
12887893d1d5SAdam Litke 		spin_unlock(&hugetlb_lock);
12896ae11b27SLee Schermerhorn 		ret = alloc_fresh_huge_page(h, nodes_allowed);
12907893d1d5SAdam Litke 		spin_lock(&hugetlb_lock);
12917893d1d5SAdam Litke 		if (!ret)
12927893d1d5SAdam Litke 			goto out;
12937893d1d5SAdam Litke 
1294536240f2SMel Gorman 		/* Bail for signals. Probably ctrl-c from user */
1295536240f2SMel Gorman 		if (signal_pending(current))
1296536240f2SMel Gorman 			goto out;
12977893d1d5SAdam Litke 	}
12987893d1d5SAdam Litke 
12997893d1d5SAdam Litke 	/*
13007893d1d5SAdam Litke 	 * Decrease the pool size
13017893d1d5SAdam Litke 	 * First return free pages to the buddy allocator (being careful
13027893d1d5SAdam Litke 	 * to keep enough around to satisfy reservations).  Then place
13037893d1d5SAdam Litke 	 * pages into surplus state as needed so the pool will shrink
13047893d1d5SAdam Litke 	 * to the desired size as pages become free.
1305d1c3fb1fSNishanth Aravamudan 	 *
1306d1c3fb1fSNishanth Aravamudan 	 * By placing pages into the surplus state independent of the
1307d1c3fb1fSNishanth Aravamudan 	 * overcommit value, we are allowing the surplus pool size to
1308d1c3fb1fSNishanth Aravamudan 	 * exceed overcommit. There are few sane options here. Since
1309d1c3fb1fSNishanth Aravamudan 	 * alloc_buddy_huge_page() is checking the global counter,
1310d1c3fb1fSNishanth Aravamudan 	 * though, we'll note that we're not allowed to exceed surplus
1311d1c3fb1fSNishanth Aravamudan 	 * and won't grow the pool anywhere else. Not until one of the
1312d1c3fb1fSNishanth Aravamudan 	 * sysctls are changed, or the surplus pages go out of use.
13137893d1d5SAdam Litke 	 */
1314a5516438SAndi Kleen 	min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
13156b0c880dSAdam Litke 	min_count = max(count, min_count);
13166ae11b27SLee Schermerhorn 	try_to_free_low(h, min_count, nodes_allowed);
1317a5516438SAndi Kleen 	while (min_count < persistent_huge_pages(h)) {
13186ae11b27SLee Schermerhorn 		if (!free_pool_huge_page(h, nodes_allowed, 0))
13191da177e4SLinus Torvalds 			break;
13201da177e4SLinus Torvalds 	}
1321a5516438SAndi Kleen 	while (count < persistent_huge_pages(h)) {
13226ae11b27SLee Schermerhorn 		if (!adjust_pool_surplus(h, nodes_allowed, 1))
13237893d1d5SAdam Litke 			break;
13247893d1d5SAdam Litke 	}
13257893d1d5SAdam Litke out:
1326a5516438SAndi Kleen 	ret = persistent_huge_pages(h);
13271da177e4SLinus Torvalds 	spin_unlock(&hugetlb_lock);
13287893d1d5SAdam Litke 	return ret;
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
1331a3437870SNishanth Aravamudan #define HSTATE_ATTR_RO(_name) \
1332a3437870SNishanth Aravamudan 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1333a3437870SNishanth Aravamudan 
1334a3437870SNishanth Aravamudan #define HSTATE_ATTR(_name) \
1335a3437870SNishanth Aravamudan 	static struct kobj_attribute _name##_attr = \
1336a3437870SNishanth Aravamudan 		__ATTR(_name, 0644, _name##_show, _name##_store)
1337a3437870SNishanth Aravamudan 
1338a3437870SNishanth Aravamudan static struct kobject *hugepages_kobj;
1339a3437870SNishanth Aravamudan static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
1340a3437870SNishanth Aravamudan 
13419a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
13429a305230SLee Schermerhorn 
13439a305230SLee Schermerhorn static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
1344a3437870SNishanth Aravamudan {
1345a3437870SNishanth Aravamudan 	int i;
13469a305230SLee Schermerhorn 
1347a3437870SNishanth Aravamudan 	for (i = 0; i < HUGE_MAX_HSTATE; i++)
13489a305230SLee Schermerhorn 		if (hstate_kobjs[i] == kobj) {
13499a305230SLee Schermerhorn 			if (nidp)
13509a305230SLee Schermerhorn 				*nidp = NUMA_NO_NODE;
1351a3437870SNishanth Aravamudan 			return &hstates[i];
13529a305230SLee Schermerhorn 		}
13539a305230SLee Schermerhorn 
13549a305230SLee Schermerhorn 	return kobj_to_node_hstate(kobj, nidp);
1355a3437870SNishanth Aravamudan }
1356a3437870SNishanth Aravamudan 
135706808b08SLee Schermerhorn static ssize_t nr_hugepages_show_common(struct kobject *kobj,
1358a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1359a3437870SNishanth Aravamudan {
13609a305230SLee Schermerhorn 	struct hstate *h;
13619a305230SLee Schermerhorn 	unsigned long nr_huge_pages;
13629a305230SLee Schermerhorn 	int nid;
13639a305230SLee Schermerhorn 
13649a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
13659a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
13669a305230SLee Schermerhorn 		nr_huge_pages = h->nr_huge_pages;
13679a305230SLee Schermerhorn 	else
13689a305230SLee Schermerhorn 		nr_huge_pages = h->nr_huge_pages_node[nid];
13699a305230SLee Schermerhorn 
13709a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", nr_huge_pages);
1371a3437870SNishanth Aravamudan }
137206808b08SLee Schermerhorn static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
137306808b08SLee Schermerhorn 			struct kobject *kobj, struct kobj_attribute *attr,
137406808b08SLee Schermerhorn 			const char *buf, size_t len)
1375a3437870SNishanth Aravamudan {
1376a3437870SNishanth Aravamudan 	int err;
13779a305230SLee Schermerhorn 	int nid;
137806808b08SLee Schermerhorn 	unsigned long count;
13799a305230SLee Schermerhorn 	struct hstate *h;
1380bad44b5bSDavid Rientjes 	NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
1381a3437870SNishanth Aravamudan 
138206808b08SLee Schermerhorn 	err = strict_strtoul(buf, 10, &count);
1383a3437870SNishanth Aravamudan 	if (err)
1384a3437870SNishanth Aravamudan 		return 0;
1385a3437870SNishanth Aravamudan 
13869a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
13879a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE) {
13889a305230SLee Schermerhorn 		/*
13899a305230SLee Schermerhorn 		 * global hstate attribute
13909a305230SLee Schermerhorn 		 */
13919a305230SLee Schermerhorn 		if (!(obey_mempolicy &&
13929a305230SLee Schermerhorn 				init_nodemask_of_mempolicy(nodes_allowed))) {
139306808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
13949a305230SLee Schermerhorn 			nodes_allowed = &node_states[N_HIGH_MEMORY];
139506808b08SLee Schermerhorn 		}
13969a305230SLee Schermerhorn 	} else if (nodes_allowed) {
13979a305230SLee Schermerhorn 		/*
13989a305230SLee Schermerhorn 		 * per node hstate attribute: adjust count to global,
13999a305230SLee Schermerhorn 		 * but restrict alloc/free to the specified node.
14009a305230SLee Schermerhorn 		 */
14019a305230SLee Schermerhorn 		count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
14029a305230SLee Schermerhorn 		init_nodemask_of_node(nodes_allowed, nid);
14039a305230SLee Schermerhorn 	} else
14049a305230SLee Schermerhorn 		nodes_allowed = &node_states[N_HIGH_MEMORY];
14059a305230SLee Schermerhorn 
140606808b08SLee Schermerhorn 	h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
1407a3437870SNishanth Aravamudan 
14089b5e5d0fSLee Schermerhorn 	if (nodes_allowed != &node_states[N_HIGH_MEMORY])
140906808b08SLee Schermerhorn 		NODEMASK_FREE(nodes_allowed);
141006808b08SLee Schermerhorn 
141106808b08SLee Schermerhorn 	return len;
141206808b08SLee Schermerhorn }
141306808b08SLee Schermerhorn 
141406808b08SLee Schermerhorn static ssize_t nr_hugepages_show(struct kobject *kobj,
141506808b08SLee Schermerhorn 				       struct kobj_attribute *attr, char *buf)
141606808b08SLee Schermerhorn {
141706808b08SLee Schermerhorn 	return nr_hugepages_show_common(kobj, attr, buf);
141806808b08SLee Schermerhorn }
141906808b08SLee Schermerhorn 
142006808b08SLee Schermerhorn static ssize_t nr_hugepages_store(struct kobject *kobj,
142106808b08SLee Schermerhorn 	       struct kobj_attribute *attr, const char *buf, size_t len)
142206808b08SLee Schermerhorn {
142306808b08SLee Schermerhorn 	return nr_hugepages_store_common(false, kobj, attr, buf, len);
1424a3437870SNishanth Aravamudan }
1425a3437870SNishanth Aravamudan HSTATE_ATTR(nr_hugepages);
1426a3437870SNishanth Aravamudan 
142706808b08SLee Schermerhorn #ifdef CONFIG_NUMA
142806808b08SLee Schermerhorn 
142906808b08SLee Schermerhorn /*
143006808b08SLee Schermerhorn  * hstate attribute for optionally mempolicy-based constraint on persistent
143106808b08SLee Schermerhorn  * huge page alloc/free.
143206808b08SLee Schermerhorn  */
143306808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
143406808b08SLee Schermerhorn 				       struct kobj_attribute *attr, char *buf)
143506808b08SLee Schermerhorn {
143606808b08SLee Schermerhorn 	return nr_hugepages_show_common(kobj, attr, buf);
143706808b08SLee Schermerhorn }
143806808b08SLee Schermerhorn 
143906808b08SLee Schermerhorn static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
144006808b08SLee Schermerhorn 	       struct kobj_attribute *attr, const char *buf, size_t len)
144106808b08SLee Schermerhorn {
144206808b08SLee Schermerhorn 	return nr_hugepages_store_common(true, kobj, attr, buf, len);
144306808b08SLee Schermerhorn }
144406808b08SLee Schermerhorn HSTATE_ATTR(nr_hugepages_mempolicy);
144506808b08SLee Schermerhorn #endif
144606808b08SLee Schermerhorn 
144706808b08SLee Schermerhorn 
1448a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
1449a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1450a3437870SNishanth Aravamudan {
14519a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1452a3437870SNishanth Aravamudan 	return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
1453a3437870SNishanth Aravamudan }
1454a3437870SNishanth Aravamudan static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
1455a3437870SNishanth Aravamudan 		struct kobj_attribute *attr, const char *buf, size_t count)
1456a3437870SNishanth Aravamudan {
1457a3437870SNishanth Aravamudan 	int err;
1458a3437870SNishanth Aravamudan 	unsigned long input;
14599a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1460a3437870SNishanth Aravamudan 
1461a3437870SNishanth Aravamudan 	err = strict_strtoul(buf, 10, &input);
1462a3437870SNishanth Aravamudan 	if (err)
1463a3437870SNishanth Aravamudan 		return 0;
1464a3437870SNishanth Aravamudan 
1465a3437870SNishanth Aravamudan 	spin_lock(&hugetlb_lock);
1466a3437870SNishanth Aravamudan 	h->nr_overcommit_huge_pages = input;
1467a3437870SNishanth Aravamudan 	spin_unlock(&hugetlb_lock);
1468a3437870SNishanth Aravamudan 
1469a3437870SNishanth Aravamudan 	return count;
1470a3437870SNishanth Aravamudan }
1471a3437870SNishanth Aravamudan HSTATE_ATTR(nr_overcommit_hugepages);
1472a3437870SNishanth Aravamudan 
1473a3437870SNishanth Aravamudan static ssize_t free_hugepages_show(struct kobject *kobj,
1474a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1475a3437870SNishanth Aravamudan {
14769a305230SLee Schermerhorn 	struct hstate *h;
14779a305230SLee Schermerhorn 	unsigned long free_huge_pages;
14789a305230SLee Schermerhorn 	int nid;
14799a305230SLee Schermerhorn 
14809a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
14819a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
14829a305230SLee Schermerhorn 		free_huge_pages = h->free_huge_pages;
14839a305230SLee Schermerhorn 	else
14849a305230SLee Schermerhorn 		free_huge_pages = h->free_huge_pages_node[nid];
14859a305230SLee Schermerhorn 
14869a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", free_huge_pages);
1487a3437870SNishanth Aravamudan }
1488a3437870SNishanth Aravamudan HSTATE_ATTR_RO(free_hugepages);
1489a3437870SNishanth Aravamudan 
1490a3437870SNishanth Aravamudan static ssize_t resv_hugepages_show(struct kobject *kobj,
1491a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1492a3437870SNishanth Aravamudan {
14939a305230SLee Schermerhorn 	struct hstate *h = kobj_to_hstate(kobj, NULL);
1494a3437870SNishanth Aravamudan 	return sprintf(buf, "%lu\n", h->resv_huge_pages);
1495a3437870SNishanth Aravamudan }
1496a3437870SNishanth Aravamudan HSTATE_ATTR_RO(resv_hugepages);
1497a3437870SNishanth Aravamudan 
1498a3437870SNishanth Aravamudan static ssize_t surplus_hugepages_show(struct kobject *kobj,
1499a3437870SNishanth Aravamudan 					struct kobj_attribute *attr, char *buf)
1500a3437870SNishanth Aravamudan {
15019a305230SLee Schermerhorn 	struct hstate *h;
15029a305230SLee Schermerhorn 	unsigned long surplus_huge_pages;
15039a305230SLee Schermerhorn 	int nid;
15049a305230SLee Schermerhorn 
15059a305230SLee Schermerhorn 	h = kobj_to_hstate(kobj, &nid);
15069a305230SLee Schermerhorn 	if (nid == NUMA_NO_NODE)
15079a305230SLee Schermerhorn 		surplus_huge_pages = h->surplus_huge_pages;
15089a305230SLee Schermerhorn 	else
15099a305230SLee Schermerhorn 		surplus_huge_pages = h->surplus_huge_pages_node[nid];
15109a305230SLee Schermerhorn 
15119a305230SLee Schermerhorn 	return sprintf(buf, "%lu\n", surplus_huge_pages);
1512a3437870SNishanth Aravamudan }
1513a3437870SNishanth Aravamudan HSTATE_ATTR_RO(surplus_hugepages);
1514a3437870SNishanth Aravamudan 
1515a3437870SNishanth Aravamudan static struct attribute *hstate_attrs[] = {
1516a3437870SNishanth Aravamudan 	&nr_hugepages_attr.attr,
1517a3437870SNishanth Aravamudan 	&nr_overcommit_hugepages_attr.attr,
1518a3437870SNishanth Aravamudan 	&free_hugepages_attr.attr,
1519a3437870SNishanth Aravamudan 	&resv_hugepages_attr.attr,
1520a3437870SNishanth Aravamudan 	&surplus_hugepages_attr.attr,
152106808b08SLee Schermerhorn #ifdef CONFIG_NUMA
152206808b08SLee Schermerhorn 	&nr_hugepages_mempolicy_attr.attr,
152306808b08SLee Schermerhorn #endif
1524a3437870SNishanth Aravamudan 	NULL,
1525a3437870SNishanth Aravamudan };
1526a3437870SNishanth Aravamudan 
1527a3437870SNishanth Aravamudan static struct attribute_group hstate_attr_group = {
1528a3437870SNishanth Aravamudan 	.attrs = hstate_attrs,
1529a3437870SNishanth Aravamudan };
1530a3437870SNishanth Aravamudan 
1531094e9539SJeff Mahoney static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
15329a305230SLee Schermerhorn 				    struct kobject **hstate_kobjs,
15339a305230SLee Schermerhorn 				    struct attribute_group *hstate_attr_group)
1534a3437870SNishanth Aravamudan {
1535a3437870SNishanth Aravamudan 	int retval;
15369a305230SLee Schermerhorn 	int hi = h - hstates;
1537a3437870SNishanth Aravamudan 
15389a305230SLee Schermerhorn 	hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
15399a305230SLee Schermerhorn 	if (!hstate_kobjs[hi])
1540a3437870SNishanth Aravamudan 		return -ENOMEM;
1541a3437870SNishanth Aravamudan 
15429a305230SLee Schermerhorn 	retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
1543a3437870SNishanth Aravamudan 	if (retval)
15449a305230SLee Schermerhorn 		kobject_put(hstate_kobjs[hi]);
1545a3437870SNishanth Aravamudan 
1546a3437870SNishanth Aravamudan 	return retval;
1547a3437870SNishanth Aravamudan }
1548a3437870SNishanth Aravamudan 
1549a3437870SNishanth Aravamudan static void __init hugetlb_sysfs_init(void)
1550a3437870SNishanth Aravamudan {
1551a3437870SNishanth Aravamudan 	struct hstate *h;
1552a3437870SNishanth Aravamudan 	int err;
1553a3437870SNishanth Aravamudan 
1554a3437870SNishanth Aravamudan 	hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
1555a3437870SNishanth Aravamudan 	if (!hugepages_kobj)
1556a3437870SNishanth Aravamudan 		return;
1557a3437870SNishanth Aravamudan 
1558a3437870SNishanth Aravamudan 	for_each_hstate(h) {
15599a305230SLee Schermerhorn 		err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
15609a305230SLee Schermerhorn 					 hstate_kobjs, &hstate_attr_group);
1561a3437870SNishanth Aravamudan 		if (err)
1562a3437870SNishanth Aravamudan 			printk(KERN_ERR "Hugetlb: Unable to add hstate %s",
1563a3437870SNishanth Aravamudan 								h->name);
1564a3437870SNishanth Aravamudan 	}
1565a3437870SNishanth Aravamudan }
1566a3437870SNishanth Aravamudan 
15679a305230SLee Schermerhorn #ifdef CONFIG_NUMA
15689a305230SLee Schermerhorn 
15699a305230SLee Schermerhorn /*
15709a305230SLee Schermerhorn  * node_hstate/s - associate per node hstate attributes, via their kobjects,
15719a305230SLee Schermerhorn  * with node sysdevs in node_devices[] using a parallel array.  The array
15729a305230SLee Schermerhorn  * index of a node sysdev or _hstate == node id.
15739a305230SLee Schermerhorn  * This is here to avoid any static dependency of the node sysdev driver, in
15749a305230SLee Schermerhorn  * the base kernel, on the hugetlb module.
15759a305230SLee Schermerhorn  */
15769a305230SLee Schermerhorn struct node_hstate {
15779a305230SLee Schermerhorn 	struct kobject		*hugepages_kobj;
15789a305230SLee Schermerhorn 	struct kobject		*hstate_kobjs[HUGE_MAX_HSTATE];
15799a305230SLee Schermerhorn };
15809a305230SLee Schermerhorn struct node_hstate node_hstates[MAX_NUMNODES];
15819a305230SLee Schermerhorn 
15829a305230SLee Schermerhorn /*
15839a305230SLee Schermerhorn  * A subset of global hstate attributes for node sysdevs
15849a305230SLee Schermerhorn  */
15859a305230SLee Schermerhorn static struct attribute *per_node_hstate_attrs[] = {
15869a305230SLee Schermerhorn 	&nr_hugepages_attr.attr,
15879a305230SLee Schermerhorn 	&free_hugepages_attr.attr,
15889a305230SLee Schermerhorn 	&surplus_hugepages_attr.attr,
15899a305230SLee Schermerhorn 	NULL,
15909a305230SLee Schermerhorn };
15919a305230SLee Schermerhorn 
15929a305230SLee Schermerhorn static struct attribute_group per_node_hstate_attr_group = {
15939a305230SLee Schermerhorn 	.attrs = per_node_hstate_attrs,
15949a305230SLee Schermerhorn };
15959a305230SLee Schermerhorn 
15969a305230SLee Schermerhorn /*
15979a305230SLee Schermerhorn  * kobj_to_node_hstate - lookup global hstate for node sysdev hstate attr kobj.
15989a305230SLee Schermerhorn  * Returns node id via non-NULL nidp.
15999a305230SLee Schermerhorn  */
16009a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
16019a305230SLee Schermerhorn {
16029a305230SLee Schermerhorn 	int nid;
16039a305230SLee Schermerhorn 
16049a305230SLee Schermerhorn 	for (nid = 0; nid < nr_node_ids; nid++) {
16059a305230SLee Schermerhorn 		struct node_hstate *nhs = &node_hstates[nid];
16069a305230SLee Schermerhorn 		int i;
16079a305230SLee Schermerhorn 		for (i = 0; i < HUGE_MAX_HSTATE; i++)
16089a305230SLee Schermerhorn 			if (nhs->hstate_kobjs[i] == kobj) {
16099a305230SLee Schermerhorn 				if (nidp)
16109a305230SLee Schermerhorn 					*nidp = nid;
16119a305230SLee Schermerhorn 				return &hstates[i];
16129a305230SLee Schermerhorn 			}
16139a305230SLee Schermerhorn 	}
16149a305230SLee Schermerhorn 
16159a305230SLee Schermerhorn 	BUG();
16169a305230SLee Schermerhorn 	return NULL;
16179a305230SLee Schermerhorn }
16189a305230SLee Schermerhorn 
16199a305230SLee Schermerhorn /*
16209a305230SLee Schermerhorn  * Unregister hstate attributes from a single node sysdev.
16219a305230SLee Schermerhorn  * No-op if no hstate attributes attached.
16229a305230SLee Schermerhorn  */
16239a305230SLee Schermerhorn void hugetlb_unregister_node(struct node *node)
16249a305230SLee Schermerhorn {
16259a305230SLee Schermerhorn 	struct hstate *h;
16269a305230SLee Schermerhorn 	struct node_hstate *nhs = &node_hstates[node->sysdev.id];
16279a305230SLee Schermerhorn 
16289a305230SLee Schermerhorn 	if (!nhs->hugepages_kobj)
16299b5e5d0fSLee Schermerhorn 		return;		/* no hstate attributes */
16309a305230SLee Schermerhorn 
16319a305230SLee Schermerhorn 	for_each_hstate(h)
16329a305230SLee Schermerhorn 		if (nhs->hstate_kobjs[h - hstates]) {
16339a305230SLee Schermerhorn 			kobject_put(nhs->hstate_kobjs[h - hstates]);
16349a305230SLee Schermerhorn 			nhs->hstate_kobjs[h - hstates] = NULL;
16359a305230SLee Schermerhorn 		}
16369a305230SLee Schermerhorn 
16379a305230SLee Schermerhorn 	kobject_put(nhs->hugepages_kobj);
16389a305230SLee Schermerhorn 	nhs->hugepages_kobj = NULL;
16399a305230SLee Schermerhorn }
16409a305230SLee Schermerhorn 
16419a305230SLee Schermerhorn /*
16429a305230SLee Schermerhorn  * hugetlb module exit:  unregister hstate attributes from node sysdevs
16439a305230SLee Schermerhorn  * that have them.
16449a305230SLee Schermerhorn  */
16459a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void)
16469a305230SLee Schermerhorn {
16479a305230SLee Schermerhorn 	int nid;
16489a305230SLee Schermerhorn 
16499a305230SLee Schermerhorn 	/*
16509a305230SLee Schermerhorn 	 * disable node sysdev registrations.
16519a305230SLee Schermerhorn 	 */
16529a305230SLee Schermerhorn 	register_hugetlbfs_with_node(NULL, NULL);
16539a305230SLee Schermerhorn 
16549a305230SLee Schermerhorn 	/*
16559a305230SLee Schermerhorn 	 * remove hstate attributes from any nodes that have them.
16569a305230SLee Schermerhorn 	 */
16579a305230SLee Schermerhorn 	for (nid = 0; nid < nr_node_ids; nid++)
16589a305230SLee Schermerhorn 		hugetlb_unregister_node(&node_devices[nid]);
16599a305230SLee Schermerhorn }
16609a305230SLee Schermerhorn 
16619a305230SLee Schermerhorn /*
16629a305230SLee Schermerhorn  * Register hstate attributes for a single node sysdev.
16639a305230SLee Schermerhorn  * No-op if attributes already registered.
16649a305230SLee Schermerhorn  */
16659a305230SLee Schermerhorn void hugetlb_register_node(struct node *node)
16669a305230SLee Schermerhorn {
16679a305230SLee Schermerhorn 	struct hstate *h;
16689a305230SLee Schermerhorn 	struct node_hstate *nhs = &node_hstates[node->sysdev.id];
16699a305230SLee Schermerhorn 	int err;
16709a305230SLee Schermerhorn 
16719a305230SLee Schermerhorn 	if (nhs->hugepages_kobj)
16729a305230SLee Schermerhorn 		return;		/* already allocated */
16739a305230SLee Schermerhorn 
16749a305230SLee Schermerhorn 	nhs->hugepages_kobj = kobject_create_and_add("hugepages",
16759a305230SLee Schermerhorn 							&node->sysdev.kobj);
16769a305230SLee Schermerhorn 	if (!nhs->hugepages_kobj)
16779a305230SLee Schermerhorn 		return;
16789a305230SLee Schermerhorn 
16799a305230SLee Schermerhorn 	for_each_hstate(h) {
16809a305230SLee Schermerhorn 		err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
16819a305230SLee Schermerhorn 						nhs->hstate_kobjs,
16829a305230SLee Schermerhorn 						&per_node_hstate_attr_group);
16839a305230SLee Schermerhorn 		if (err) {
16849a305230SLee Schermerhorn 			printk(KERN_ERR "Hugetlb: Unable to add hstate %s"
16859a305230SLee Schermerhorn 					" for node %d\n",
16869a305230SLee Schermerhorn 						h->name, node->sysdev.id);
16879a305230SLee Schermerhorn 			hugetlb_unregister_node(node);
16889a305230SLee Schermerhorn 			break;
16899a305230SLee Schermerhorn 		}
16909a305230SLee Schermerhorn 	}
16919a305230SLee Schermerhorn }
16929a305230SLee Schermerhorn 
16939a305230SLee Schermerhorn /*
16949b5e5d0fSLee Schermerhorn  * hugetlb init time:  register hstate attributes for all registered node
16959b5e5d0fSLee Schermerhorn  * sysdevs of nodes that have memory.  All on-line nodes should have
16969b5e5d0fSLee Schermerhorn  * registered their associated sysdev by this time.
16979a305230SLee Schermerhorn  */
16989a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void)
16999a305230SLee Schermerhorn {
17009a305230SLee Schermerhorn 	int nid;
17019a305230SLee Schermerhorn 
17029b5e5d0fSLee Schermerhorn 	for_each_node_state(nid, N_HIGH_MEMORY) {
17039a305230SLee Schermerhorn 		struct node *node = &node_devices[nid];
17049a305230SLee Schermerhorn 		if (node->sysdev.id == nid)
17059a305230SLee Schermerhorn 			hugetlb_register_node(node);
17069a305230SLee Schermerhorn 	}
17079a305230SLee Schermerhorn 
17089a305230SLee Schermerhorn 	/*
17099a305230SLee Schermerhorn 	 * Let the node sysdev driver know we're here so it can
17109a305230SLee Schermerhorn 	 * [un]register hstate attributes on node hotplug.
17119a305230SLee Schermerhorn 	 */
17129a305230SLee Schermerhorn 	register_hugetlbfs_with_node(hugetlb_register_node,
17139a305230SLee Schermerhorn 				     hugetlb_unregister_node);
17149a305230SLee Schermerhorn }
17159a305230SLee Schermerhorn #else	/* !CONFIG_NUMA */
17169a305230SLee Schermerhorn 
17179a305230SLee Schermerhorn static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
17189a305230SLee Schermerhorn {
17199a305230SLee Schermerhorn 	BUG();
17209a305230SLee Schermerhorn 	if (nidp)
17219a305230SLee Schermerhorn 		*nidp = -1;
17229a305230SLee Schermerhorn 	return NULL;
17239a305230SLee Schermerhorn }
17249a305230SLee Schermerhorn 
17259a305230SLee Schermerhorn static void hugetlb_unregister_all_nodes(void) { }
17269a305230SLee Schermerhorn 
17279a305230SLee Schermerhorn static void hugetlb_register_all_nodes(void) { }
17289a305230SLee Schermerhorn 
17299a305230SLee Schermerhorn #endif
17309a305230SLee Schermerhorn 
1731a3437870SNishanth Aravamudan static void __exit hugetlb_exit(void)
1732a3437870SNishanth Aravamudan {
1733a3437870SNishanth Aravamudan 	struct hstate *h;
1734a3437870SNishanth Aravamudan 
17359a305230SLee Schermerhorn 	hugetlb_unregister_all_nodes();
17369a305230SLee Schermerhorn 
1737a3437870SNishanth Aravamudan 	for_each_hstate(h) {
1738a3437870SNishanth Aravamudan 		kobject_put(hstate_kobjs[h - hstates]);
1739a3437870SNishanth Aravamudan 	}
1740a3437870SNishanth Aravamudan 
1741a3437870SNishanth Aravamudan 	kobject_put(hugepages_kobj);
1742a3437870SNishanth Aravamudan }
1743a3437870SNishanth Aravamudan module_exit(hugetlb_exit);
1744a3437870SNishanth Aravamudan 
1745a3437870SNishanth Aravamudan static int __init hugetlb_init(void)
1746a3437870SNishanth Aravamudan {
17470ef89d25SBenjamin Herrenschmidt 	/* Some platform decide whether they support huge pages at boot
17480ef89d25SBenjamin Herrenschmidt 	 * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
17490ef89d25SBenjamin Herrenschmidt 	 * there is no such support
17500ef89d25SBenjamin Herrenschmidt 	 */
17510ef89d25SBenjamin Herrenschmidt 	if (HPAGE_SHIFT == 0)
17520ef89d25SBenjamin Herrenschmidt 		return 0;
1753a3437870SNishanth Aravamudan 
1754e11bfbfcSNick Piggin 	if (!size_to_hstate(default_hstate_size)) {
1755e11bfbfcSNick Piggin 		default_hstate_size = HPAGE_SIZE;
1756e11bfbfcSNick Piggin 		if (!size_to_hstate(default_hstate_size))
1757a3437870SNishanth Aravamudan 			hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
1758a3437870SNishanth Aravamudan 	}
1759e11bfbfcSNick Piggin 	default_hstate_idx = size_to_hstate(default_hstate_size) - hstates;
1760e11bfbfcSNick Piggin 	if (default_hstate_max_huge_pages)
1761e11bfbfcSNick Piggin 		default_hstate.max_huge_pages = default_hstate_max_huge_pages;
1762a3437870SNishanth Aravamudan 
1763a3437870SNishanth Aravamudan 	hugetlb_init_hstates();
1764a3437870SNishanth Aravamudan 
1765aa888a74SAndi Kleen 	gather_bootmem_prealloc();
1766aa888a74SAndi Kleen 
1767a3437870SNishanth Aravamudan 	report_hugepages();
1768a3437870SNishanth Aravamudan 
1769a3437870SNishanth Aravamudan 	hugetlb_sysfs_init();
1770a3437870SNishanth Aravamudan 
17719a305230SLee Schermerhorn 	hugetlb_register_all_nodes();
17729a305230SLee Schermerhorn 
1773a3437870SNishanth Aravamudan 	return 0;
1774a3437870SNishanth Aravamudan }
1775a3437870SNishanth Aravamudan module_init(hugetlb_init);
1776a3437870SNishanth Aravamudan 
1777a3437870SNishanth Aravamudan /* Should be called on processing a hugepagesz=... option */
1778a3437870SNishanth Aravamudan void __init hugetlb_add_hstate(unsigned order)
1779a3437870SNishanth Aravamudan {
1780a3437870SNishanth Aravamudan 	struct hstate *h;
17818faa8b07SAndi Kleen 	unsigned long i;
17828faa8b07SAndi Kleen 
1783a3437870SNishanth Aravamudan 	if (size_to_hstate(PAGE_SIZE << order)) {
1784a3437870SNishanth Aravamudan 		printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
1785a3437870SNishanth Aravamudan 		return;
1786a3437870SNishanth Aravamudan 	}
1787a3437870SNishanth Aravamudan 	BUG_ON(max_hstate >= HUGE_MAX_HSTATE);
1788a3437870SNishanth Aravamudan 	BUG_ON(order == 0);
1789a3437870SNishanth Aravamudan 	h = &hstates[max_hstate++];
1790a3437870SNishanth Aravamudan 	h->order = order;
1791a3437870SNishanth Aravamudan 	h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
17928faa8b07SAndi Kleen 	h->nr_huge_pages = 0;
17938faa8b07SAndi Kleen 	h->free_huge_pages = 0;
17948faa8b07SAndi Kleen 	for (i = 0; i < MAX_NUMNODES; ++i)
17958faa8b07SAndi Kleen 		INIT_LIST_HEAD(&h->hugepage_freelists[i]);
17969b5e5d0fSLee Schermerhorn 	h->next_nid_to_alloc = first_node(node_states[N_HIGH_MEMORY]);
17979b5e5d0fSLee Schermerhorn 	h->next_nid_to_free = first_node(node_states[N_HIGH_MEMORY]);
1798a3437870SNishanth Aravamudan 	snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
1799a3437870SNishanth Aravamudan 					huge_page_size(h)/1024);
18008faa8b07SAndi Kleen 
1801a3437870SNishanth Aravamudan 	parsed_hstate = h;
1802a3437870SNishanth Aravamudan }
1803a3437870SNishanth Aravamudan 
1804e11bfbfcSNick Piggin static int __init hugetlb_nrpages_setup(char *s)
1805a3437870SNishanth Aravamudan {
1806a3437870SNishanth Aravamudan 	unsigned long *mhp;
18078faa8b07SAndi Kleen 	static unsigned long *last_mhp;
1808a3437870SNishanth Aravamudan 
1809a3437870SNishanth Aravamudan 	/*
1810a3437870SNishanth Aravamudan 	 * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
1811a3437870SNishanth Aravamudan 	 * so this hugepages= parameter goes to the "default hstate".
1812a3437870SNishanth Aravamudan 	 */
1813a3437870SNishanth Aravamudan 	if (!max_hstate)
1814a3437870SNishanth Aravamudan 		mhp = &default_hstate_max_huge_pages;
1815a3437870SNishanth Aravamudan 	else
1816a3437870SNishanth Aravamudan 		mhp = &parsed_hstate->max_huge_pages;
1817a3437870SNishanth Aravamudan 
18188faa8b07SAndi Kleen 	if (mhp == last_mhp) {
18198faa8b07SAndi Kleen 		printk(KERN_WARNING "hugepages= specified twice without "
18208faa8b07SAndi Kleen 			"interleaving hugepagesz=, ignoring\n");
18218faa8b07SAndi Kleen 		return 1;
18228faa8b07SAndi Kleen 	}
18238faa8b07SAndi Kleen 
1824a3437870SNishanth Aravamudan 	if (sscanf(s, "%lu", mhp) <= 0)
1825a3437870SNishanth Aravamudan 		*mhp = 0;
1826a3437870SNishanth Aravamudan 
18278faa8b07SAndi Kleen 	/*
18288faa8b07SAndi Kleen 	 * Global state is always initialized later in hugetlb_init.
18298faa8b07SAndi Kleen 	 * But we need to allocate >= MAX_ORDER hstates here early to still
18308faa8b07SAndi Kleen 	 * use the bootmem allocator.
18318faa8b07SAndi Kleen 	 */
18328faa8b07SAndi Kleen 	if (max_hstate && parsed_hstate->order >= MAX_ORDER)
18338faa8b07SAndi Kleen 		hugetlb_hstate_alloc_pages(parsed_hstate);
18348faa8b07SAndi Kleen 
18358faa8b07SAndi Kleen 	last_mhp = mhp;
18368faa8b07SAndi Kleen 
1837a3437870SNishanth Aravamudan 	return 1;
1838a3437870SNishanth Aravamudan }
1839e11bfbfcSNick Piggin __setup("hugepages=", hugetlb_nrpages_setup);
1840e11bfbfcSNick Piggin 
1841e11bfbfcSNick Piggin static int __init hugetlb_default_setup(char *s)
1842e11bfbfcSNick Piggin {
1843e11bfbfcSNick Piggin 	default_hstate_size = memparse(s, &s);
1844e11bfbfcSNick Piggin 	return 1;
1845e11bfbfcSNick Piggin }
1846e11bfbfcSNick Piggin __setup("default_hugepagesz=", hugetlb_default_setup);
1847a3437870SNishanth Aravamudan 
18488a213460SNishanth Aravamudan static unsigned int cpuset_mems_nr(unsigned int *array)
18498a213460SNishanth Aravamudan {
18508a213460SNishanth Aravamudan 	int node;
18518a213460SNishanth Aravamudan 	unsigned int nr = 0;
18528a213460SNishanth Aravamudan 
18538a213460SNishanth Aravamudan 	for_each_node_mask(node, cpuset_current_mems_allowed)
18548a213460SNishanth Aravamudan 		nr += array[node];
18558a213460SNishanth Aravamudan 
18568a213460SNishanth Aravamudan 	return nr;
18578a213460SNishanth Aravamudan }
18588a213460SNishanth Aravamudan 
18598a213460SNishanth Aravamudan #ifdef CONFIG_SYSCTL
186006808b08SLee Schermerhorn static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
186106808b08SLee Schermerhorn 			 struct ctl_table *table, int write,
186206808b08SLee Schermerhorn 			 void __user *buffer, size_t *length, loff_t *ppos)
18631da177e4SLinus Torvalds {
1864e5ff2159SAndi Kleen 	struct hstate *h = &default_hstate;
1865e5ff2159SAndi Kleen 	unsigned long tmp;
1866e5ff2159SAndi Kleen 
1867e5ff2159SAndi Kleen 	if (!write)
1868e5ff2159SAndi Kleen 		tmp = h->max_huge_pages;
1869e5ff2159SAndi Kleen 
1870e5ff2159SAndi Kleen 	table->data = &tmp;
1871e5ff2159SAndi Kleen 	table->maxlen = sizeof(unsigned long);
18728d65af78SAlexey Dobriyan 	proc_doulongvec_minmax(table, write, buffer, length, ppos);
1873e5ff2159SAndi Kleen 
187406808b08SLee Schermerhorn 	if (write) {
1875bad44b5bSDavid Rientjes 		NODEMASK_ALLOC(nodemask_t, nodes_allowed,
1876bad44b5bSDavid Rientjes 						GFP_KERNEL | __GFP_NORETRY);
187706808b08SLee Schermerhorn 		if (!(obey_mempolicy &&
187806808b08SLee Schermerhorn 			       init_nodemask_of_mempolicy(nodes_allowed))) {
187906808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
188006808b08SLee Schermerhorn 			nodes_allowed = &node_states[N_HIGH_MEMORY];
188106808b08SLee Schermerhorn 		}
188206808b08SLee Schermerhorn 		h->max_huge_pages = set_max_huge_pages(h, tmp, nodes_allowed);
188306808b08SLee Schermerhorn 
188406808b08SLee Schermerhorn 		if (nodes_allowed != &node_states[N_HIGH_MEMORY])
188506808b08SLee Schermerhorn 			NODEMASK_FREE(nodes_allowed);
188606808b08SLee Schermerhorn 	}
1887e5ff2159SAndi Kleen 
18881da177e4SLinus Torvalds 	return 0;
18891da177e4SLinus Torvalds }
1890396faf03SMel Gorman 
189106808b08SLee Schermerhorn int hugetlb_sysctl_handler(struct ctl_table *table, int write,
189206808b08SLee Schermerhorn 			  void __user *buffer, size_t *length, loff_t *ppos)
189306808b08SLee Schermerhorn {
189406808b08SLee Schermerhorn 
189506808b08SLee Schermerhorn 	return hugetlb_sysctl_handler_common(false, table, write,
189606808b08SLee Schermerhorn 							buffer, length, ppos);
189706808b08SLee Schermerhorn }
189806808b08SLee Schermerhorn 
189906808b08SLee Schermerhorn #ifdef CONFIG_NUMA
190006808b08SLee Schermerhorn int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
190106808b08SLee Schermerhorn 			  void __user *buffer, size_t *length, loff_t *ppos)
190206808b08SLee Schermerhorn {
190306808b08SLee Schermerhorn 	return hugetlb_sysctl_handler_common(true, table, write,
190406808b08SLee Schermerhorn 							buffer, length, ppos);
190506808b08SLee Schermerhorn }
190606808b08SLee Schermerhorn #endif /* CONFIG_NUMA */
190706808b08SLee Schermerhorn 
1908396faf03SMel Gorman int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
19098d65af78SAlexey Dobriyan 			void __user *buffer,
1910396faf03SMel Gorman 			size_t *length, loff_t *ppos)
1911396faf03SMel Gorman {
19128d65af78SAlexey Dobriyan 	proc_dointvec(table, write, buffer, length, ppos);
1913396faf03SMel Gorman 	if (hugepages_treat_as_movable)
1914396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
1915396faf03SMel Gorman 	else
1916396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER;
1917396faf03SMel Gorman 	return 0;
1918396faf03SMel Gorman }
1919396faf03SMel Gorman 
1920a3d0c6aaSNishanth Aravamudan int hugetlb_overcommit_handler(struct ctl_table *table, int write,
19218d65af78SAlexey Dobriyan 			void __user *buffer,
1922a3d0c6aaSNishanth Aravamudan 			size_t *length, loff_t *ppos)
1923a3d0c6aaSNishanth Aravamudan {
1924a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
1925e5ff2159SAndi Kleen 	unsigned long tmp;
1926e5ff2159SAndi Kleen 
1927e5ff2159SAndi Kleen 	if (!write)
1928e5ff2159SAndi Kleen 		tmp = h->nr_overcommit_huge_pages;
1929e5ff2159SAndi Kleen 
1930e5ff2159SAndi Kleen 	table->data = &tmp;
1931e5ff2159SAndi Kleen 	table->maxlen = sizeof(unsigned long);
19328d65af78SAlexey Dobriyan 	proc_doulongvec_minmax(table, write, buffer, length, ppos);
1933e5ff2159SAndi Kleen 
1934e5ff2159SAndi Kleen 	if (write) {
1935064d9efeSNishanth Aravamudan 		spin_lock(&hugetlb_lock);
1936e5ff2159SAndi Kleen 		h->nr_overcommit_huge_pages = tmp;
1937a3d0c6aaSNishanth Aravamudan 		spin_unlock(&hugetlb_lock);
1938e5ff2159SAndi Kleen 	}
1939e5ff2159SAndi Kleen 
1940a3d0c6aaSNishanth Aravamudan 	return 0;
1941a3d0c6aaSNishanth Aravamudan }
1942a3d0c6aaSNishanth Aravamudan 
19431da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */
19441da177e4SLinus Torvalds 
1945e1759c21SAlexey Dobriyan void hugetlb_report_meminfo(struct seq_file *m)
19461da177e4SLinus Torvalds {
1947a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
1948e1759c21SAlexey Dobriyan 	seq_printf(m,
19491da177e4SLinus Torvalds 			"HugePages_Total:   %5lu\n"
19501da177e4SLinus Torvalds 			"HugePages_Free:    %5lu\n"
1951b45b5bd6SDavid Gibson 			"HugePages_Rsvd:    %5lu\n"
19527893d1d5SAdam Litke 			"HugePages_Surp:    %5lu\n"
19534f98a2feSRik van Riel 			"Hugepagesize:   %8lu kB\n",
1954a5516438SAndi Kleen 			h->nr_huge_pages,
1955a5516438SAndi Kleen 			h->free_huge_pages,
1956a5516438SAndi Kleen 			h->resv_huge_pages,
1957a5516438SAndi Kleen 			h->surplus_huge_pages,
1958a5516438SAndi Kleen 			1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf)
19621da177e4SLinus Torvalds {
1963a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
19641da177e4SLinus Torvalds 	return sprintf(buf,
19651da177e4SLinus Torvalds 		"Node %d HugePages_Total: %5u\n"
1966a1de0919SNishanth Aravamudan 		"Node %d HugePages_Free:  %5u\n"
1967a1de0919SNishanth Aravamudan 		"Node %d HugePages_Surp:  %5u\n",
1968a5516438SAndi Kleen 		nid, h->nr_huge_pages_node[nid],
1969a5516438SAndi Kleen 		nid, h->free_huge_pages_node[nid],
1970a5516438SAndi Kleen 		nid, h->surplus_huge_pages_node[nid]);
19711da177e4SLinus Torvalds }
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
19741da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void)
19751da177e4SLinus Torvalds {
1976a5516438SAndi Kleen 	struct hstate *h = &default_hstate;
1977a5516438SAndi Kleen 	return h->nr_huge_pages * pages_per_huge_page(h);
19781da177e4SLinus Torvalds }
19791da177e4SLinus Torvalds 
1980a5516438SAndi Kleen static int hugetlb_acct_memory(struct hstate *h, long delta)
1981fc1b8a73SMel Gorman {
1982fc1b8a73SMel Gorman 	int ret = -ENOMEM;
1983fc1b8a73SMel Gorman 
1984fc1b8a73SMel Gorman 	spin_lock(&hugetlb_lock);
1985fc1b8a73SMel Gorman 	/*
1986fc1b8a73SMel Gorman 	 * When cpuset is configured, it breaks the strict hugetlb page
1987fc1b8a73SMel Gorman 	 * reservation as the accounting is done on a global variable. Such
1988fc1b8a73SMel Gorman 	 * reservation is completely rubbish in the presence of cpuset because
1989fc1b8a73SMel Gorman 	 * the reservation is not checked against page availability for the
1990fc1b8a73SMel Gorman 	 * current cpuset. Application can still potentially OOM'ed by kernel
1991fc1b8a73SMel Gorman 	 * with lack of free htlb page in cpuset that the task is in.
1992fc1b8a73SMel Gorman 	 * Attempt to enforce strict accounting with cpuset is almost
1993fc1b8a73SMel Gorman 	 * impossible (or too ugly) because cpuset is too fluid that
1994fc1b8a73SMel Gorman 	 * task or memory node can be dynamically moved between cpusets.
1995fc1b8a73SMel Gorman 	 *
1996fc1b8a73SMel Gorman 	 * The change of semantics for shared hugetlb mapping with cpuset is
1997fc1b8a73SMel Gorman 	 * undesirable. However, in order to preserve some of the semantics,
1998fc1b8a73SMel Gorman 	 * we fall back to check against current free page availability as
1999fc1b8a73SMel Gorman 	 * a best attempt and hopefully to minimize the impact of changing
2000fc1b8a73SMel Gorman 	 * semantics that cpuset has.
2001fc1b8a73SMel Gorman 	 */
2002fc1b8a73SMel Gorman 	if (delta > 0) {
2003a5516438SAndi Kleen 		if (gather_surplus_pages(h, delta) < 0)
2004fc1b8a73SMel Gorman 			goto out;
2005fc1b8a73SMel Gorman 
2006a5516438SAndi Kleen 		if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
2007a5516438SAndi Kleen 			return_unused_surplus_pages(h, delta);
2008fc1b8a73SMel Gorman 			goto out;
2009fc1b8a73SMel Gorman 		}
2010fc1b8a73SMel Gorman 	}
2011fc1b8a73SMel Gorman 
2012fc1b8a73SMel Gorman 	ret = 0;
2013fc1b8a73SMel Gorman 	if (delta < 0)
2014a5516438SAndi Kleen 		return_unused_surplus_pages(h, (unsigned long) -delta);
2015fc1b8a73SMel Gorman 
2016fc1b8a73SMel Gorman out:
2017fc1b8a73SMel Gorman 	spin_unlock(&hugetlb_lock);
2018fc1b8a73SMel Gorman 	return ret;
2019fc1b8a73SMel Gorman }
2020fc1b8a73SMel Gorman 
202184afd99bSAndy Whitcroft static void hugetlb_vm_op_open(struct vm_area_struct *vma)
202284afd99bSAndy Whitcroft {
202384afd99bSAndy Whitcroft 	struct resv_map *reservations = vma_resv_map(vma);
202484afd99bSAndy Whitcroft 
202584afd99bSAndy Whitcroft 	/*
202684afd99bSAndy Whitcroft 	 * This new VMA should share its siblings reservation map if present.
202784afd99bSAndy Whitcroft 	 * The VMA will only ever have a valid reservation map pointer where
202884afd99bSAndy Whitcroft 	 * it is being copied for another still existing VMA.  As that VMA
202984afd99bSAndy Whitcroft 	 * has a reference to the reservation map it cannot dissappear until
203084afd99bSAndy Whitcroft 	 * after this open call completes.  It is therefore safe to take a
203184afd99bSAndy Whitcroft 	 * new reference here without additional locking.
203284afd99bSAndy Whitcroft 	 */
203384afd99bSAndy Whitcroft 	if (reservations)
203484afd99bSAndy Whitcroft 		kref_get(&reservations->refs);
203584afd99bSAndy Whitcroft }
203684afd99bSAndy Whitcroft 
2037a1e78772SMel Gorman static void hugetlb_vm_op_close(struct vm_area_struct *vma)
2038a1e78772SMel Gorman {
2039a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
204084afd99bSAndy Whitcroft 	struct resv_map *reservations = vma_resv_map(vma);
204184afd99bSAndy Whitcroft 	unsigned long reserve;
204284afd99bSAndy Whitcroft 	unsigned long start;
204384afd99bSAndy Whitcroft 	unsigned long end;
204484afd99bSAndy Whitcroft 
204584afd99bSAndy Whitcroft 	if (reservations) {
2046a5516438SAndi Kleen 		start = vma_hugecache_offset(h, vma, vma->vm_start);
2047a5516438SAndi Kleen 		end = vma_hugecache_offset(h, vma, vma->vm_end);
204884afd99bSAndy Whitcroft 
204984afd99bSAndy Whitcroft 		reserve = (end - start) -
205084afd99bSAndy Whitcroft 			region_count(&reservations->regions, start, end);
205184afd99bSAndy Whitcroft 
205284afd99bSAndy Whitcroft 		kref_put(&reservations->refs, resv_map_release);
205384afd99bSAndy Whitcroft 
20547251ff78SAdam Litke 		if (reserve) {
2055a5516438SAndi Kleen 			hugetlb_acct_memory(h, -reserve);
20567251ff78SAdam Litke 			hugetlb_put_quota(vma->vm_file->f_mapping, reserve);
20577251ff78SAdam Litke 		}
2058a1e78772SMel Gorman 	}
205984afd99bSAndy Whitcroft }
2060a1e78772SMel Gorman 
20611da177e4SLinus Torvalds /*
20621da177e4SLinus Torvalds  * We cannot handle pagefaults against hugetlb pages at all.  They cause
20631da177e4SLinus Torvalds  * handle_mm_fault() to try to instantiate regular-sized pages in the
20641da177e4SLinus Torvalds  * hugegpage VMA.  do_page_fault() is supposed to trap this, so BUG is we get
20651da177e4SLinus Torvalds  * this far.
20661da177e4SLinus Torvalds  */
2067d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
20681da177e4SLinus Torvalds {
20691da177e4SLinus Torvalds 	BUG();
2070d0217ac0SNick Piggin 	return 0;
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds 
2073f0f37e2fSAlexey Dobriyan const struct vm_operations_struct hugetlb_vm_ops = {
2074d0217ac0SNick Piggin 	.fault = hugetlb_vm_op_fault,
207584afd99bSAndy Whitcroft 	.open = hugetlb_vm_op_open,
2076a1e78772SMel Gorman 	.close = hugetlb_vm_op_close,
20771da177e4SLinus Torvalds };
20781da177e4SLinus Torvalds 
20791e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
20801e8f889bSDavid Gibson 				int writable)
208163551ae0SDavid Gibson {
208263551ae0SDavid Gibson 	pte_t entry;
208363551ae0SDavid Gibson 
20841e8f889bSDavid Gibson 	if (writable) {
208563551ae0SDavid Gibson 		entry =
208663551ae0SDavid Gibson 		    pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
208763551ae0SDavid Gibson 	} else {
20887f2e9525SGerald Schaefer 		entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
208963551ae0SDavid Gibson 	}
209063551ae0SDavid Gibson 	entry = pte_mkyoung(entry);
209163551ae0SDavid Gibson 	entry = pte_mkhuge(entry);
209263551ae0SDavid Gibson 
209363551ae0SDavid Gibson 	return entry;
209463551ae0SDavid Gibson }
209563551ae0SDavid Gibson 
20961e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma,
20971e8f889bSDavid Gibson 				   unsigned long address, pte_t *ptep)
20981e8f889bSDavid Gibson {
20991e8f889bSDavid Gibson 	pte_t entry;
21001e8f889bSDavid Gibson 
21017f2e9525SGerald Schaefer 	entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
21027f2e9525SGerald Schaefer 	if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
21034b3073e1SRussell King 		update_mmu_cache(vma, address, ptep);
21041e8f889bSDavid Gibson 	}
21058dab5241SBenjamin Herrenschmidt }
21061e8f889bSDavid Gibson 
21071e8f889bSDavid Gibson 
210863551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
210963551ae0SDavid Gibson 			    struct vm_area_struct *vma)
211063551ae0SDavid Gibson {
211163551ae0SDavid Gibson 	pte_t *src_pte, *dst_pte, entry;
211263551ae0SDavid Gibson 	struct page *ptepage;
21131c59827dSHugh Dickins 	unsigned long addr;
21141e8f889bSDavid Gibson 	int cow;
2115a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2116a5516438SAndi Kleen 	unsigned long sz = huge_page_size(h);
21171e8f889bSDavid Gibson 
21181e8f889bSDavid Gibson 	cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
211963551ae0SDavid Gibson 
2120a5516438SAndi Kleen 	for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
2121c74df32cSHugh Dickins 		src_pte = huge_pte_offset(src, addr);
2122c74df32cSHugh Dickins 		if (!src_pte)
2123c74df32cSHugh Dickins 			continue;
2124a5516438SAndi Kleen 		dst_pte = huge_pte_alloc(dst, addr, sz);
212563551ae0SDavid Gibson 		if (!dst_pte)
212663551ae0SDavid Gibson 			goto nomem;
2127c5c99429SLarry Woodman 
2128c5c99429SLarry Woodman 		/* If the pagetables are shared don't copy or take references */
2129c5c99429SLarry Woodman 		if (dst_pte == src_pte)
2130c5c99429SLarry Woodman 			continue;
2131c5c99429SLarry Woodman 
2132c74df32cSHugh Dickins 		spin_lock(&dst->page_table_lock);
213346478758SNick Piggin 		spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
21347f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(src_pte))) {
21351e8f889bSDavid Gibson 			if (cow)
21367f2e9525SGerald Schaefer 				huge_ptep_set_wrprotect(src, addr, src_pte);
21377f2e9525SGerald Schaefer 			entry = huge_ptep_get(src_pte);
213863551ae0SDavid Gibson 			ptepage = pte_page(entry);
213963551ae0SDavid Gibson 			get_page(ptepage);
21400fe6e20bSNaoya Horiguchi 			page_dup_rmap(ptepage);
214163551ae0SDavid Gibson 			set_huge_pte_at(dst, addr, dst_pte, entry);
21421c59827dSHugh Dickins 		}
21431c59827dSHugh Dickins 		spin_unlock(&src->page_table_lock);
2144c74df32cSHugh Dickins 		spin_unlock(&dst->page_table_lock);
214563551ae0SDavid Gibson 	}
214663551ae0SDavid Gibson 	return 0;
214763551ae0SDavid Gibson 
214863551ae0SDavid Gibson nomem:
214963551ae0SDavid Gibson 	return -ENOMEM;
215063551ae0SDavid Gibson }
215163551ae0SDavid Gibson 
2152502717f4SChen, Kenneth W void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
215304f2cbe3SMel Gorman 			    unsigned long end, struct page *ref_page)
215463551ae0SDavid Gibson {
215563551ae0SDavid Gibson 	struct mm_struct *mm = vma->vm_mm;
215663551ae0SDavid Gibson 	unsigned long address;
2157c7546f8fSDavid Gibson 	pte_t *ptep;
215863551ae0SDavid Gibson 	pte_t pte;
215963551ae0SDavid Gibson 	struct page *page;
2160fe1668aeSChen, Kenneth W 	struct page *tmp;
2161a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2162a5516438SAndi Kleen 	unsigned long sz = huge_page_size(h);
2163a5516438SAndi Kleen 
2164c0a499c2SChen, Kenneth W 	/*
2165c0a499c2SChen, Kenneth W 	 * A page gathering list, protected by per file i_mmap_lock. The
2166c0a499c2SChen, Kenneth W 	 * lock is used to avoid list corruption from multiple unmapping
2167c0a499c2SChen, Kenneth W 	 * of the same page since we are using page->lru.
2168c0a499c2SChen, Kenneth W 	 */
2169fe1668aeSChen, Kenneth W 	LIST_HEAD(page_list);
217063551ae0SDavid Gibson 
217163551ae0SDavid Gibson 	WARN_ON(!is_vm_hugetlb_page(vma));
2172a5516438SAndi Kleen 	BUG_ON(start & ~huge_page_mask(h));
2173a5516438SAndi Kleen 	BUG_ON(end & ~huge_page_mask(h));
217463551ae0SDavid Gibson 
2175cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_start(mm, start, end);
2176508034a3SHugh Dickins 	spin_lock(&mm->page_table_lock);
2177a5516438SAndi Kleen 	for (address = start; address < end; address += sz) {
2178c7546f8fSDavid Gibson 		ptep = huge_pte_offset(mm, address);
2179c7546f8fSDavid Gibson 		if (!ptep)
2180c7546f8fSDavid Gibson 			continue;
2181c7546f8fSDavid Gibson 
218239dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
218339dde65cSChen, Kenneth W 			continue;
218439dde65cSChen, Kenneth W 
218504f2cbe3SMel Gorman 		/*
218604f2cbe3SMel Gorman 		 * If a reference page is supplied, it is because a specific
218704f2cbe3SMel Gorman 		 * page is being unmapped, not a range. Ensure the page we
218804f2cbe3SMel Gorman 		 * are about to unmap is the actual page of interest.
218904f2cbe3SMel Gorman 		 */
219004f2cbe3SMel Gorman 		if (ref_page) {
219104f2cbe3SMel Gorman 			pte = huge_ptep_get(ptep);
219204f2cbe3SMel Gorman 			if (huge_pte_none(pte))
219304f2cbe3SMel Gorman 				continue;
219404f2cbe3SMel Gorman 			page = pte_page(pte);
219504f2cbe3SMel Gorman 			if (page != ref_page)
219604f2cbe3SMel Gorman 				continue;
219704f2cbe3SMel Gorman 
219804f2cbe3SMel Gorman 			/*
219904f2cbe3SMel Gorman 			 * Mark the VMA as having unmapped its page so that
220004f2cbe3SMel Gorman 			 * future faults in this VMA will fail rather than
220104f2cbe3SMel Gorman 			 * looking like data was lost
220204f2cbe3SMel Gorman 			 */
220304f2cbe3SMel Gorman 			set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
220404f2cbe3SMel Gorman 		}
220504f2cbe3SMel Gorman 
2206c7546f8fSDavid Gibson 		pte = huge_ptep_get_and_clear(mm, address, ptep);
22077f2e9525SGerald Schaefer 		if (huge_pte_none(pte))
220863551ae0SDavid Gibson 			continue;
2209c7546f8fSDavid Gibson 
221063551ae0SDavid Gibson 		page = pte_page(pte);
22116649a386SKen Chen 		if (pte_dirty(pte))
22126649a386SKen Chen 			set_page_dirty(page);
2213fe1668aeSChen, Kenneth W 		list_add(&page->lru, &page_list);
221463551ae0SDavid Gibson 	}
22151da177e4SLinus Torvalds 	spin_unlock(&mm->page_table_lock);
2216508034a3SHugh Dickins 	flush_tlb_range(vma, start, end);
2217cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start, end);
2218fe1668aeSChen, Kenneth W 	list_for_each_entry_safe(page, tmp, &page_list, lru) {
22190fe6e20bSNaoya Horiguchi 		page_remove_rmap(page);
2220fe1668aeSChen, Kenneth W 		list_del(&page->lru);
2221fe1668aeSChen, Kenneth W 		put_page(page);
2222fe1668aeSChen, Kenneth W 	}
22231da177e4SLinus Torvalds }
222463551ae0SDavid Gibson 
2225502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
222604f2cbe3SMel Gorman 			  unsigned long end, struct page *ref_page)
2227502717f4SChen, Kenneth W {
2228502717f4SChen, Kenneth W 	spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
222904f2cbe3SMel Gorman 	__unmap_hugepage_range(vma, start, end, ref_page);
2230502717f4SChen, Kenneth W 	spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
2231502717f4SChen, Kenneth W }
2232502717f4SChen, Kenneth W 
223304f2cbe3SMel Gorman /*
223404f2cbe3SMel Gorman  * This is called when the original mapper is failing to COW a MAP_PRIVATE
223504f2cbe3SMel Gorman  * mappping it owns the reserve page for. The intention is to unmap the page
223604f2cbe3SMel Gorman  * from other VMAs and let the children be SIGKILLed if they are faulting the
223704f2cbe3SMel Gorman  * same region.
223804f2cbe3SMel Gorman  */
22392a4b3dedSHarvey Harrison static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
22402a4b3dedSHarvey Harrison 				struct page *page, unsigned long address)
224104f2cbe3SMel Gorman {
22427526674dSAdam Litke 	struct hstate *h = hstate_vma(vma);
224304f2cbe3SMel Gorman 	struct vm_area_struct *iter_vma;
224404f2cbe3SMel Gorman 	struct address_space *mapping;
224504f2cbe3SMel Gorman 	struct prio_tree_iter iter;
224604f2cbe3SMel Gorman 	pgoff_t pgoff;
224704f2cbe3SMel Gorman 
224804f2cbe3SMel Gorman 	/*
224904f2cbe3SMel Gorman 	 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
225004f2cbe3SMel Gorman 	 * from page cache lookup which is in HPAGE_SIZE units.
225104f2cbe3SMel Gorman 	 */
22527526674dSAdam Litke 	address = address & huge_page_mask(h);
225304f2cbe3SMel Gorman 	pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
225404f2cbe3SMel Gorman 		+ (vma->vm_pgoff >> PAGE_SHIFT);
225504f2cbe3SMel Gorman 	mapping = (struct address_space *)page_private(page);
225604f2cbe3SMel Gorman 
22574eb2b1dcSMel Gorman 	/*
22584eb2b1dcSMel Gorman 	 * Take the mapping lock for the duration of the table walk. As
22594eb2b1dcSMel Gorman 	 * this mapping should be shared between all the VMAs,
22604eb2b1dcSMel Gorman 	 * __unmap_hugepage_range() is called as the lock is already held
22614eb2b1dcSMel Gorman 	 */
22624eb2b1dcSMel Gorman 	spin_lock(&mapping->i_mmap_lock);
226304f2cbe3SMel Gorman 	vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
226404f2cbe3SMel Gorman 		/* Do not unmap the current VMA */
226504f2cbe3SMel Gorman 		if (iter_vma == vma)
226604f2cbe3SMel Gorman 			continue;
226704f2cbe3SMel Gorman 
226804f2cbe3SMel Gorman 		/*
226904f2cbe3SMel Gorman 		 * Unmap the page from other VMAs without their own reserves.
227004f2cbe3SMel Gorman 		 * They get marked to be SIGKILLed if they fault in these
227104f2cbe3SMel Gorman 		 * areas. This is because a future no-page fault on this VMA
227204f2cbe3SMel Gorman 		 * could insert a zeroed page instead of the data existing
227304f2cbe3SMel Gorman 		 * from the time of fork. This would look like data corruption
227404f2cbe3SMel Gorman 		 */
227504f2cbe3SMel Gorman 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
22764eb2b1dcSMel Gorman 			__unmap_hugepage_range(iter_vma,
22777526674dSAdam Litke 				address, address + huge_page_size(h),
227804f2cbe3SMel Gorman 				page);
227904f2cbe3SMel Gorman 	}
22804eb2b1dcSMel Gorman 	spin_unlock(&mapping->i_mmap_lock);
228104f2cbe3SMel Gorman 
228204f2cbe3SMel Gorman 	return 1;
228304f2cbe3SMel Gorman }
228404f2cbe3SMel Gorman 
22850fe6e20bSNaoya Horiguchi /*
22860fe6e20bSNaoya Horiguchi  * Hugetlb_cow() should be called with page lock of the original hugepage held.
22870fe6e20bSNaoya Horiguchi  */
22881e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
228904f2cbe3SMel Gorman 			unsigned long address, pte_t *ptep, pte_t pte,
229004f2cbe3SMel Gorman 			struct page *pagecache_page)
22911e8f889bSDavid Gibson {
2292a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
22931e8f889bSDavid Gibson 	struct page *old_page, *new_page;
229479ac6ba4SDavid Gibson 	int avoidcopy;
229504f2cbe3SMel Gorman 	int outside_reserve = 0;
22961e8f889bSDavid Gibson 
22971e8f889bSDavid Gibson 	old_page = pte_page(pte);
22981e8f889bSDavid Gibson 
229904f2cbe3SMel Gorman retry_avoidcopy:
23001e8f889bSDavid Gibson 	/* If no-one else is actually using this page, avoid the copy
23011e8f889bSDavid Gibson 	 * and just make the page writable */
23020fe6e20bSNaoya Horiguchi 	avoidcopy = (page_mapcount(old_page) == 1);
23031e8f889bSDavid Gibson 	if (avoidcopy) {
23040fe6e20bSNaoya Horiguchi 		if (!trylock_page(old_page))
23050fe6e20bSNaoya Horiguchi 			if (PageAnon(old_page))
23060fe6e20bSNaoya Horiguchi 				page_move_anon_rmap(old_page, vma, address);
23071e8f889bSDavid Gibson 		set_huge_ptep_writable(vma, address, ptep);
230883c54070SNick Piggin 		return 0;
23091e8f889bSDavid Gibson 	}
23101e8f889bSDavid Gibson 
231104f2cbe3SMel Gorman 	/*
231204f2cbe3SMel Gorman 	 * If the process that created a MAP_PRIVATE mapping is about to
231304f2cbe3SMel Gorman 	 * perform a COW due to a shared page count, attempt to satisfy
231404f2cbe3SMel Gorman 	 * the allocation without using the existing reserves. The pagecache
231504f2cbe3SMel Gorman 	 * page is used to determine if the reserve at this address was
231604f2cbe3SMel Gorman 	 * consumed or not. If reserves were used, a partial faulted mapping
231704f2cbe3SMel Gorman 	 * at the time of fork() could consume its reserves on COW instead
231804f2cbe3SMel Gorman 	 * of the full address range.
231904f2cbe3SMel Gorman 	 */
2320f83a275dSMel Gorman 	if (!(vma->vm_flags & VM_MAYSHARE) &&
232104f2cbe3SMel Gorman 			is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
232204f2cbe3SMel Gorman 			old_page != pagecache_page)
232304f2cbe3SMel Gorman 		outside_reserve = 1;
232404f2cbe3SMel Gorman 
23251e8f889bSDavid Gibson 	page_cache_get(old_page);
2326b76c8cfbSLarry Woodman 
2327b76c8cfbSLarry Woodman 	/* Drop page_table_lock as buddy allocator may be called */
2328b76c8cfbSLarry Woodman 	spin_unlock(&mm->page_table_lock);
232904f2cbe3SMel Gorman 	new_page = alloc_huge_page(vma, address, outside_reserve);
23301e8f889bSDavid Gibson 
23312fc39cecSAdam Litke 	if (IS_ERR(new_page)) {
23321e8f889bSDavid Gibson 		page_cache_release(old_page);
233304f2cbe3SMel Gorman 
233404f2cbe3SMel Gorman 		/*
233504f2cbe3SMel Gorman 		 * If a process owning a MAP_PRIVATE mapping fails to COW,
233604f2cbe3SMel Gorman 		 * it is due to references held by a child and an insufficient
233704f2cbe3SMel Gorman 		 * huge page pool. To guarantee the original mappers
233804f2cbe3SMel Gorman 		 * reliability, unmap the page from child processes. The child
233904f2cbe3SMel Gorman 		 * may get SIGKILLed if it later faults.
234004f2cbe3SMel Gorman 		 */
234104f2cbe3SMel Gorman 		if (outside_reserve) {
234204f2cbe3SMel Gorman 			BUG_ON(huge_pte_none(pte));
234304f2cbe3SMel Gorman 			if (unmap_ref_private(mm, vma, old_page, address)) {
234404f2cbe3SMel Gorman 				BUG_ON(page_count(old_page) != 1);
234504f2cbe3SMel Gorman 				BUG_ON(huge_pte_none(pte));
2346b76c8cfbSLarry Woodman 				spin_lock(&mm->page_table_lock);
234704f2cbe3SMel Gorman 				goto retry_avoidcopy;
234804f2cbe3SMel Gorman 			}
234904f2cbe3SMel Gorman 			WARN_ON_ONCE(1);
235004f2cbe3SMel Gorman 		}
235104f2cbe3SMel Gorman 
2352b76c8cfbSLarry Woodman 		/* Caller expects lock to be held */
2353b76c8cfbSLarry Woodman 		spin_lock(&mm->page_table_lock);
23542fc39cecSAdam Litke 		return -PTR_ERR(new_page);
23551e8f889bSDavid Gibson 	}
23561e8f889bSDavid Gibson 
23570fe6e20bSNaoya Horiguchi 	/*
23580fe6e20bSNaoya Horiguchi 	 * When the original hugepage is shared one, it does not have
23590fe6e20bSNaoya Horiguchi 	 * anon_vma prepared.
23600fe6e20bSNaoya Horiguchi 	 */
23610fe6e20bSNaoya Horiguchi 	if (unlikely(anon_vma_prepare(vma)))
23620fe6e20bSNaoya Horiguchi 		return VM_FAULT_OOM;
23630fe6e20bSNaoya Horiguchi 
23649de455b2SAtsushi Nemoto 	copy_huge_page(new_page, old_page, address, vma);
23650ed361deSNick Piggin 	__SetPageUptodate(new_page);
23661e8f889bSDavid Gibson 
2367b76c8cfbSLarry Woodman 	/*
2368b76c8cfbSLarry Woodman 	 * Retake the page_table_lock to check for racing updates
2369b76c8cfbSLarry Woodman 	 * before the page tables are altered
2370b76c8cfbSLarry Woodman 	 */
2371b76c8cfbSLarry Woodman 	spin_lock(&mm->page_table_lock);
2372a5516438SAndi Kleen 	ptep = huge_pte_offset(mm, address & huge_page_mask(h));
23737f2e9525SGerald Schaefer 	if (likely(pte_same(huge_ptep_get(ptep), pte))) {
23741e8f889bSDavid Gibson 		/* Break COW */
23758fe627ecSGerald Schaefer 		huge_ptep_clear_flush(vma, address, ptep);
23761e8f889bSDavid Gibson 		set_huge_pte_at(mm, address, ptep,
23771e8f889bSDavid Gibson 				make_huge_pte(vma, new_page, 1));
23780fe6e20bSNaoya Horiguchi 		page_remove_rmap(old_page);
23790fe6e20bSNaoya Horiguchi 		hugepage_add_anon_rmap(new_page, vma, address);
23801e8f889bSDavid Gibson 		/* Make the old page be freed below */
23811e8f889bSDavid Gibson 		new_page = old_page;
23821e8f889bSDavid Gibson 	}
23831e8f889bSDavid Gibson 	page_cache_release(new_page);
23841e8f889bSDavid Gibson 	page_cache_release(old_page);
238583c54070SNick Piggin 	return 0;
23861e8f889bSDavid Gibson }
23871e8f889bSDavid Gibson 
238804f2cbe3SMel Gorman /* Return the pagecache page at a given address within a VMA */
2389a5516438SAndi Kleen static struct page *hugetlbfs_pagecache_page(struct hstate *h,
2390a5516438SAndi Kleen 			struct vm_area_struct *vma, unsigned long address)
239104f2cbe3SMel Gorman {
239204f2cbe3SMel Gorman 	struct address_space *mapping;
2393e7c4b0bfSAndy Whitcroft 	pgoff_t idx;
239404f2cbe3SMel Gorman 
239504f2cbe3SMel Gorman 	mapping = vma->vm_file->f_mapping;
2396a5516438SAndi Kleen 	idx = vma_hugecache_offset(h, vma, address);
239704f2cbe3SMel Gorman 
239804f2cbe3SMel Gorman 	return find_lock_page(mapping, idx);
239904f2cbe3SMel Gorman }
240004f2cbe3SMel Gorman 
24013ae77f43SHugh Dickins /*
24023ae77f43SHugh Dickins  * Return whether there is a pagecache page to back given address within VMA.
24033ae77f43SHugh Dickins  * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
24043ae77f43SHugh Dickins  */
24053ae77f43SHugh Dickins static bool hugetlbfs_pagecache_present(struct hstate *h,
24062a15efc9SHugh Dickins 			struct vm_area_struct *vma, unsigned long address)
24072a15efc9SHugh Dickins {
24082a15efc9SHugh Dickins 	struct address_space *mapping;
24092a15efc9SHugh Dickins 	pgoff_t idx;
24102a15efc9SHugh Dickins 	struct page *page;
24112a15efc9SHugh Dickins 
24122a15efc9SHugh Dickins 	mapping = vma->vm_file->f_mapping;
24132a15efc9SHugh Dickins 	idx = vma_hugecache_offset(h, vma, address);
24142a15efc9SHugh Dickins 
24152a15efc9SHugh Dickins 	page = find_get_page(mapping, idx);
24162a15efc9SHugh Dickins 	if (page)
24172a15efc9SHugh Dickins 		put_page(page);
24182a15efc9SHugh Dickins 	return page != NULL;
24192a15efc9SHugh Dickins }
24202a15efc9SHugh Dickins 
2421a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
2422788c7df4SHugh Dickins 			unsigned long address, pte_t *ptep, unsigned int flags)
2423ac9b9c66SHugh Dickins {
2424a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2425ac9b9c66SHugh Dickins 	int ret = VM_FAULT_SIGBUS;
2426e7c4b0bfSAndy Whitcroft 	pgoff_t idx;
24274c887265SAdam Litke 	unsigned long size;
24284c887265SAdam Litke 	struct page *page;
24294c887265SAdam Litke 	struct address_space *mapping;
24301e8f889bSDavid Gibson 	pte_t new_pte;
24314c887265SAdam Litke 
243204f2cbe3SMel Gorman 	/*
243304f2cbe3SMel Gorman 	 * Currently, we are forced to kill the process in the event the
243404f2cbe3SMel Gorman 	 * original mapper has unmapped pages from the child due to a failed
243504f2cbe3SMel Gorman 	 * COW. Warn that such a situation has occured as it may not be obvious
243604f2cbe3SMel Gorman 	 */
243704f2cbe3SMel Gorman 	if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
243804f2cbe3SMel Gorman 		printk(KERN_WARNING
243904f2cbe3SMel Gorman 			"PID %d killed due to inadequate hugepage pool\n",
244004f2cbe3SMel Gorman 			current->pid);
244104f2cbe3SMel Gorman 		return ret;
244204f2cbe3SMel Gorman 	}
244304f2cbe3SMel Gorman 
24444c887265SAdam Litke 	mapping = vma->vm_file->f_mapping;
2445a5516438SAndi Kleen 	idx = vma_hugecache_offset(h, vma, address);
24464c887265SAdam Litke 
24474c887265SAdam Litke 	/*
24484c887265SAdam Litke 	 * Use page lock to guard against racing truncation
24494c887265SAdam Litke 	 * before we get page_table_lock.
24504c887265SAdam Litke 	 */
24516bda666aSChristoph Lameter retry:
24526bda666aSChristoph Lameter 	page = find_lock_page(mapping, idx);
24536bda666aSChristoph Lameter 	if (!page) {
2454a5516438SAndi Kleen 		size = i_size_read(mapping->host) >> huge_page_shift(h);
2455ebed4bfcSHugh Dickins 		if (idx >= size)
2456ebed4bfcSHugh Dickins 			goto out;
245704f2cbe3SMel Gorman 		page = alloc_huge_page(vma, address, 0);
24582fc39cecSAdam Litke 		if (IS_ERR(page)) {
24592fc39cecSAdam Litke 			ret = -PTR_ERR(page);
24606bda666aSChristoph Lameter 			goto out;
24616bda666aSChristoph Lameter 		}
2462a5516438SAndi Kleen 		clear_huge_page(page, address, huge_page_size(h));
24630ed361deSNick Piggin 		__SetPageUptodate(page);
2464ac9b9c66SHugh Dickins 
2465f83a275dSMel Gorman 		if (vma->vm_flags & VM_MAYSHARE) {
24666bda666aSChristoph Lameter 			int err;
246745c682a6SKen Chen 			struct inode *inode = mapping->host;
24686bda666aSChristoph Lameter 
24696bda666aSChristoph Lameter 			err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
24706bda666aSChristoph Lameter 			if (err) {
24716bda666aSChristoph Lameter 				put_page(page);
24726bda666aSChristoph Lameter 				if (err == -EEXIST)
24736bda666aSChristoph Lameter 					goto retry;
24746bda666aSChristoph Lameter 				goto out;
24756bda666aSChristoph Lameter 			}
247645c682a6SKen Chen 
247745c682a6SKen Chen 			spin_lock(&inode->i_lock);
2478a5516438SAndi Kleen 			inode->i_blocks += blocks_per_huge_page(h);
247945c682a6SKen Chen 			spin_unlock(&inode->i_lock);
24800fe6e20bSNaoya Horiguchi 			page_dup_rmap(page);
248123be7468SMel Gorman 		} else {
24826bda666aSChristoph Lameter 			lock_page(page);
24830fe6e20bSNaoya Horiguchi 			if (unlikely(anon_vma_prepare(vma))) {
24840fe6e20bSNaoya Horiguchi 				ret = VM_FAULT_OOM;
24850fe6e20bSNaoya Horiguchi 				goto backout_unlocked;
248623be7468SMel Gorman 			}
24870fe6e20bSNaoya Horiguchi 			hugepage_add_new_anon_rmap(page, vma, address);
24880fe6e20bSNaoya Horiguchi 		}
24890fe6e20bSNaoya Horiguchi 	} else {
24900fe6e20bSNaoya Horiguchi 		page_dup_rmap(page);
24916bda666aSChristoph Lameter 	}
24921e8f889bSDavid Gibson 
249357303d80SAndy Whitcroft 	/*
249457303d80SAndy Whitcroft 	 * If we are going to COW a private mapping later, we examine the
249557303d80SAndy Whitcroft 	 * pending reservations for this page now. This will ensure that
249657303d80SAndy Whitcroft 	 * any allocations necessary to record that reservation occur outside
249757303d80SAndy Whitcroft 	 * the spinlock.
249857303d80SAndy Whitcroft 	 */
2499788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
25002b26736cSAndy Whitcroft 		if (vma_needs_reservation(h, vma, address) < 0) {
25012b26736cSAndy Whitcroft 			ret = VM_FAULT_OOM;
25022b26736cSAndy Whitcroft 			goto backout_unlocked;
25032b26736cSAndy Whitcroft 		}
250457303d80SAndy Whitcroft 
2505ac9b9c66SHugh Dickins 	spin_lock(&mm->page_table_lock);
2506a5516438SAndi Kleen 	size = i_size_read(mapping->host) >> huge_page_shift(h);
25074c887265SAdam Litke 	if (idx >= size)
25084c887265SAdam Litke 		goto backout;
25094c887265SAdam Litke 
251083c54070SNick Piggin 	ret = 0;
25117f2e9525SGerald Schaefer 	if (!huge_pte_none(huge_ptep_get(ptep)))
25124c887265SAdam Litke 		goto backout;
25134c887265SAdam Litke 
25141e8f889bSDavid Gibson 	new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
25151e8f889bSDavid Gibson 				&& (vma->vm_flags & VM_SHARED)));
25161e8f889bSDavid Gibson 	set_huge_pte_at(mm, address, ptep, new_pte);
25171e8f889bSDavid Gibson 
2518788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
25191e8f889bSDavid Gibson 		/* Optimization, do the COW without a second fault */
252004f2cbe3SMel Gorman 		ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
25211e8f889bSDavid Gibson 	}
25221e8f889bSDavid Gibson 
2523ac9b9c66SHugh Dickins 	spin_unlock(&mm->page_table_lock);
25244c887265SAdam Litke 	unlock_page(page);
25254c887265SAdam Litke out:
2526ac9b9c66SHugh Dickins 	return ret;
25274c887265SAdam Litke 
25284c887265SAdam Litke backout:
25294c887265SAdam Litke 	spin_unlock(&mm->page_table_lock);
25302b26736cSAndy Whitcroft backout_unlocked:
25314c887265SAdam Litke 	unlock_page(page);
25324c887265SAdam Litke 	put_page(page);
25334c887265SAdam Litke 	goto out;
2534ac9b9c66SHugh Dickins }
2535ac9b9c66SHugh Dickins 
253686e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2537788c7df4SHugh Dickins 			unsigned long address, unsigned int flags)
253886e5216fSAdam Litke {
253986e5216fSAdam Litke 	pte_t *ptep;
254086e5216fSAdam Litke 	pte_t entry;
25411e8f889bSDavid Gibson 	int ret;
25420fe6e20bSNaoya Horiguchi 	struct page *page = NULL;
254357303d80SAndy Whitcroft 	struct page *pagecache_page = NULL;
25443935baa9SDavid Gibson 	static DEFINE_MUTEX(hugetlb_instantiation_mutex);
2545a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
254686e5216fSAdam Litke 
2547a5516438SAndi Kleen 	ptep = huge_pte_alloc(mm, address, huge_page_size(h));
254886e5216fSAdam Litke 	if (!ptep)
254986e5216fSAdam Litke 		return VM_FAULT_OOM;
255086e5216fSAdam Litke 
25513935baa9SDavid Gibson 	/*
25523935baa9SDavid Gibson 	 * Serialize hugepage allocation and instantiation, so that we don't
25533935baa9SDavid Gibson 	 * get spurious allocation failures if two CPUs race to instantiate
25543935baa9SDavid Gibson 	 * the same page in the page cache.
25553935baa9SDavid Gibson 	 */
25563935baa9SDavid Gibson 	mutex_lock(&hugetlb_instantiation_mutex);
25577f2e9525SGerald Schaefer 	entry = huge_ptep_get(ptep);
25587f2e9525SGerald Schaefer 	if (huge_pte_none(entry)) {
2559788c7df4SHugh Dickins 		ret = hugetlb_no_page(mm, vma, address, ptep, flags);
2560b4d1d99fSDavid Gibson 		goto out_mutex;
25613935baa9SDavid Gibson 	}
256286e5216fSAdam Litke 
256383c54070SNick Piggin 	ret = 0;
25641e8f889bSDavid Gibson 
256557303d80SAndy Whitcroft 	/*
256657303d80SAndy Whitcroft 	 * If we are going to COW the mapping later, we examine the pending
256757303d80SAndy Whitcroft 	 * reservations for this page now. This will ensure that any
256857303d80SAndy Whitcroft 	 * allocations necessary to record that reservation occur outside the
256957303d80SAndy Whitcroft 	 * spinlock. For private mappings, we also lookup the pagecache
257057303d80SAndy Whitcroft 	 * page now as it is used to determine if a reservation has been
257157303d80SAndy Whitcroft 	 * consumed.
257257303d80SAndy Whitcroft 	 */
2573788c7df4SHugh Dickins 	if ((flags & FAULT_FLAG_WRITE) && !pte_write(entry)) {
25742b26736cSAndy Whitcroft 		if (vma_needs_reservation(h, vma, address) < 0) {
25752b26736cSAndy Whitcroft 			ret = VM_FAULT_OOM;
2576b4d1d99fSDavid Gibson 			goto out_mutex;
25772b26736cSAndy Whitcroft 		}
257857303d80SAndy Whitcroft 
2579f83a275dSMel Gorman 		if (!(vma->vm_flags & VM_MAYSHARE))
258057303d80SAndy Whitcroft 			pagecache_page = hugetlbfs_pagecache_page(h,
258157303d80SAndy Whitcroft 								vma, address);
258257303d80SAndy Whitcroft 	}
258357303d80SAndy Whitcroft 
25840fe6e20bSNaoya Horiguchi 	if (!pagecache_page) {
25850fe6e20bSNaoya Horiguchi 		page = pte_page(entry);
25860fe6e20bSNaoya Horiguchi 		lock_page(page);
25870fe6e20bSNaoya Horiguchi 	}
25880fe6e20bSNaoya Horiguchi 
25891e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
25901e8f889bSDavid Gibson 	/* Check for a racing update before calling hugetlb_cow */
2591b4d1d99fSDavid Gibson 	if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
2592b4d1d99fSDavid Gibson 		goto out_page_table_lock;
2593b4d1d99fSDavid Gibson 
2594b4d1d99fSDavid Gibson 
2595788c7df4SHugh Dickins 	if (flags & FAULT_FLAG_WRITE) {
2596b4d1d99fSDavid Gibson 		if (!pte_write(entry)) {
259757303d80SAndy Whitcroft 			ret = hugetlb_cow(mm, vma, address, ptep, entry,
259857303d80SAndy Whitcroft 							pagecache_page);
2599b4d1d99fSDavid Gibson 			goto out_page_table_lock;
2600b4d1d99fSDavid Gibson 		}
2601b4d1d99fSDavid Gibson 		entry = pte_mkdirty(entry);
2602b4d1d99fSDavid Gibson 	}
2603b4d1d99fSDavid Gibson 	entry = pte_mkyoung(entry);
2604788c7df4SHugh Dickins 	if (huge_ptep_set_access_flags(vma, address, ptep, entry,
2605788c7df4SHugh Dickins 						flags & FAULT_FLAG_WRITE))
26064b3073e1SRussell King 		update_mmu_cache(vma, address, ptep);
2607b4d1d99fSDavid Gibson 
2608b4d1d99fSDavid Gibson out_page_table_lock:
26091e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
261057303d80SAndy Whitcroft 
261157303d80SAndy Whitcroft 	if (pagecache_page) {
261257303d80SAndy Whitcroft 		unlock_page(pagecache_page);
261357303d80SAndy Whitcroft 		put_page(pagecache_page);
26140fe6e20bSNaoya Horiguchi 	} else {
26150fe6e20bSNaoya Horiguchi 		unlock_page(page);
261657303d80SAndy Whitcroft 	}
261757303d80SAndy Whitcroft 
2618b4d1d99fSDavid Gibson out_mutex:
26193935baa9SDavid Gibson 	mutex_unlock(&hugetlb_instantiation_mutex);
26201e8f889bSDavid Gibson 
26211e8f889bSDavid Gibson 	return ret;
262286e5216fSAdam Litke }
262386e5216fSAdam Litke 
2624ceb86879SAndi Kleen /* Can be overriden by architectures */
2625ceb86879SAndi Kleen __attribute__((weak)) struct page *
2626ceb86879SAndi Kleen follow_huge_pud(struct mm_struct *mm, unsigned long address,
2627ceb86879SAndi Kleen 	       pud_t *pud, int write)
2628ceb86879SAndi Kleen {
2629ceb86879SAndi Kleen 	BUG();
2630ceb86879SAndi Kleen 	return NULL;
2631ceb86879SAndi Kleen }
2632ceb86879SAndi Kleen 
263363551ae0SDavid Gibson int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
263463551ae0SDavid Gibson 			struct page **pages, struct vm_area_struct **vmas,
26355b23dbe8SAdam Litke 			unsigned long *position, int *length, int i,
26362a15efc9SHugh Dickins 			unsigned int flags)
263763551ae0SDavid Gibson {
2638d5d4b0aaSChen, Kenneth W 	unsigned long pfn_offset;
2639d5d4b0aaSChen, Kenneth W 	unsigned long vaddr = *position;
264063551ae0SDavid Gibson 	int remainder = *length;
2641a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
264263551ae0SDavid Gibson 
26431c59827dSHugh Dickins 	spin_lock(&mm->page_table_lock);
264463551ae0SDavid Gibson 	while (vaddr < vma->vm_end && remainder) {
264563551ae0SDavid Gibson 		pte_t *pte;
26462a15efc9SHugh Dickins 		int absent;
264763551ae0SDavid Gibson 		struct page *page;
264863551ae0SDavid Gibson 
26494c887265SAdam Litke 		/*
26504c887265SAdam Litke 		 * Some archs (sparc64, sh*) have multiple pte_ts to
26512a15efc9SHugh Dickins 		 * each hugepage.  We have to make sure we get the
26524c887265SAdam Litke 		 * first, for the page indexing below to work.
26534c887265SAdam Litke 		 */
2654a5516438SAndi Kleen 		pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
26552a15efc9SHugh Dickins 		absent = !pte || huge_pte_none(huge_ptep_get(pte));
265663551ae0SDavid Gibson 
26572a15efc9SHugh Dickins 		/*
26582a15efc9SHugh Dickins 		 * When coredumping, it suits get_dump_page if we just return
26593ae77f43SHugh Dickins 		 * an error where there's an empty slot with no huge pagecache
26603ae77f43SHugh Dickins 		 * to back it.  This way, we avoid allocating a hugepage, and
26613ae77f43SHugh Dickins 		 * the sparse dumpfile avoids allocating disk blocks, but its
26623ae77f43SHugh Dickins 		 * huge holes still show up with zeroes where they need to be.
26632a15efc9SHugh Dickins 		 */
26643ae77f43SHugh Dickins 		if (absent && (flags & FOLL_DUMP) &&
26653ae77f43SHugh Dickins 		    !hugetlbfs_pagecache_present(h, vma, vaddr)) {
26662a15efc9SHugh Dickins 			remainder = 0;
26672a15efc9SHugh Dickins 			break;
26682a15efc9SHugh Dickins 		}
26692a15efc9SHugh Dickins 
26702a15efc9SHugh Dickins 		if (absent ||
26712a15efc9SHugh Dickins 		    ((flags & FOLL_WRITE) && !pte_write(huge_ptep_get(pte)))) {
26724c887265SAdam Litke 			int ret;
26734c887265SAdam Litke 
26744c887265SAdam Litke 			spin_unlock(&mm->page_table_lock);
26752a15efc9SHugh Dickins 			ret = hugetlb_fault(mm, vma, vaddr,
26762a15efc9SHugh Dickins 				(flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
26774c887265SAdam Litke 			spin_lock(&mm->page_table_lock);
2678a89182c7SAdam Litke 			if (!(ret & VM_FAULT_ERROR))
26794c887265SAdam Litke 				continue;
26804c887265SAdam Litke 
26811c59827dSHugh Dickins 			remainder = 0;
26821c59827dSHugh Dickins 			break;
26831c59827dSHugh Dickins 		}
268463551ae0SDavid Gibson 
2685a5516438SAndi Kleen 		pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
26867f2e9525SGerald Schaefer 		page = pte_page(huge_ptep_get(pte));
2687d5d4b0aaSChen, Kenneth W same_page:
2688d6692183SChen, Kenneth W 		if (pages) {
268969d177c2SAndy Whitcroft 			pages[i] = mem_map_offset(page, pfn_offset);
26904b2e38adSKOSAKI Motohiro 			get_page(pages[i]);
2691d6692183SChen, Kenneth W 		}
269263551ae0SDavid Gibson 
269363551ae0SDavid Gibson 		if (vmas)
269463551ae0SDavid Gibson 			vmas[i] = vma;
269563551ae0SDavid Gibson 
269663551ae0SDavid Gibson 		vaddr += PAGE_SIZE;
2697d5d4b0aaSChen, Kenneth W 		++pfn_offset;
269863551ae0SDavid Gibson 		--remainder;
269963551ae0SDavid Gibson 		++i;
2700d5d4b0aaSChen, Kenneth W 		if (vaddr < vma->vm_end && remainder &&
2701a5516438SAndi Kleen 				pfn_offset < pages_per_huge_page(h)) {
2702d5d4b0aaSChen, Kenneth W 			/*
2703d5d4b0aaSChen, Kenneth W 			 * We use pfn_offset to avoid touching the pageframes
2704d5d4b0aaSChen, Kenneth W 			 * of this compound page.
2705d5d4b0aaSChen, Kenneth W 			 */
2706d5d4b0aaSChen, Kenneth W 			goto same_page;
2707d5d4b0aaSChen, Kenneth W 		}
270863551ae0SDavid Gibson 	}
27091c59827dSHugh Dickins 	spin_unlock(&mm->page_table_lock);
271063551ae0SDavid Gibson 	*length = remainder;
271163551ae0SDavid Gibson 	*position = vaddr;
271263551ae0SDavid Gibson 
27132a15efc9SHugh Dickins 	return i ? i : -EFAULT;
271463551ae0SDavid Gibson }
27158f860591SZhang, Yanmin 
27168f860591SZhang, Yanmin void hugetlb_change_protection(struct vm_area_struct *vma,
27178f860591SZhang, Yanmin 		unsigned long address, unsigned long end, pgprot_t newprot)
27188f860591SZhang, Yanmin {
27198f860591SZhang, Yanmin 	struct mm_struct *mm = vma->vm_mm;
27208f860591SZhang, Yanmin 	unsigned long start = address;
27218f860591SZhang, Yanmin 	pte_t *ptep;
27228f860591SZhang, Yanmin 	pte_t pte;
2723a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
27248f860591SZhang, Yanmin 
27258f860591SZhang, Yanmin 	BUG_ON(address >= end);
27268f860591SZhang, Yanmin 	flush_cache_range(vma, address, end);
27278f860591SZhang, Yanmin 
272839dde65cSChen, Kenneth W 	spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
27298f860591SZhang, Yanmin 	spin_lock(&mm->page_table_lock);
2730a5516438SAndi Kleen 	for (; address < end; address += huge_page_size(h)) {
27318f860591SZhang, Yanmin 		ptep = huge_pte_offset(mm, address);
27328f860591SZhang, Yanmin 		if (!ptep)
27338f860591SZhang, Yanmin 			continue;
273439dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
273539dde65cSChen, Kenneth W 			continue;
27367f2e9525SGerald Schaefer 		if (!huge_pte_none(huge_ptep_get(ptep))) {
27378f860591SZhang, Yanmin 			pte = huge_ptep_get_and_clear(mm, address, ptep);
27388f860591SZhang, Yanmin 			pte = pte_mkhuge(pte_modify(pte, newprot));
27398f860591SZhang, Yanmin 			set_huge_pte_at(mm, address, ptep, pte);
27408f860591SZhang, Yanmin 		}
27418f860591SZhang, Yanmin 	}
27428f860591SZhang, Yanmin 	spin_unlock(&mm->page_table_lock);
274339dde65cSChen, Kenneth W 	spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
27448f860591SZhang, Yanmin 
27458f860591SZhang, Yanmin 	flush_tlb_range(vma, start, end);
27468f860591SZhang, Yanmin }
27478f860591SZhang, Yanmin 
2748a1e78772SMel Gorman int hugetlb_reserve_pages(struct inode *inode,
2749a1e78772SMel Gorman 					long from, long to,
27505a6fe125SMel Gorman 					struct vm_area_struct *vma,
27515a6fe125SMel Gorman 					int acctflag)
2752e4e574b7SAdam Litke {
275317c9d12eSMel Gorman 	long ret, chg;
2754a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
2755e4e574b7SAdam Litke 
2756a1e78772SMel Gorman 	/*
275717c9d12eSMel Gorman 	 * Only apply hugepage reservation if asked. At fault time, an
275817c9d12eSMel Gorman 	 * attempt will be made for VM_NORESERVE to allocate a page
275917c9d12eSMel Gorman 	 * and filesystem quota without using reserves
276017c9d12eSMel Gorman 	 */
276117c9d12eSMel Gorman 	if (acctflag & VM_NORESERVE)
276217c9d12eSMel Gorman 		return 0;
276317c9d12eSMel Gorman 
276417c9d12eSMel Gorman 	/*
2765a1e78772SMel Gorman 	 * Shared mappings base their reservation on the number of pages that
2766a1e78772SMel Gorman 	 * are already allocated on behalf of the file. Private mappings need
2767a1e78772SMel Gorman 	 * to reserve the full area even if read-only as mprotect() may be
2768a1e78772SMel Gorman 	 * called to make the mapping read-write. Assume !vma is a shm mapping
2769a1e78772SMel Gorman 	 */
2770f83a275dSMel Gorman 	if (!vma || vma->vm_flags & VM_MAYSHARE)
2771e4e574b7SAdam Litke 		chg = region_chg(&inode->i_mapping->private_list, from, to);
27725a6fe125SMel Gorman 	else {
27735a6fe125SMel Gorman 		struct resv_map *resv_map = resv_map_alloc();
27745a6fe125SMel Gorman 		if (!resv_map)
27755a6fe125SMel Gorman 			return -ENOMEM;
27765a6fe125SMel Gorman 
277717c9d12eSMel Gorman 		chg = to - from;
277817c9d12eSMel Gorman 
27795a6fe125SMel Gorman 		set_vma_resv_map(vma, resv_map);
27805a6fe125SMel Gorman 		set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
27815a6fe125SMel Gorman 	}
27825a6fe125SMel Gorman 
278317c9d12eSMel Gorman 	if (chg < 0)
278417c9d12eSMel Gorman 		return chg;
278517c9d12eSMel Gorman 
278617c9d12eSMel Gorman 	/* There must be enough filesystem quota for the mapping */
278717c9d12eSMel Gorman 	if (hugetlb_get_quota(inode->i_mapping, chg))
278817c9d12eSMel Gorman 		return -ENOSPC;
278917c9d12eSMel Gorman 
279017c9d12eSMel Gorman 	/*
279117c9d12eSMel Gorman 	 * Check enough hugepages are available for the reservation.
279217c9d12eSMel Gorman 	 * Hand back the quota if there are not
279317c9d12eSMel Gorman 	 */
279417c9d12eSMel Gorman 	ret = hugetlb_acct_memory(h, chg);
279517c9d12eSMel Gorman 	if (ret < 0) {
279617c9d12eSMel Gorman 		hugetlb_put_quota(inode->i_mapping, chg);
279717c9d12eSMel Gorman 		return ret;
279817c9d12eSMel Gorman 	}
279917c9d12eSMel Gorman 
280017c9d12eSMel Gorman 	/*
280117c9d12eSMel Gorman 	 * Account for the reservations made. Shared mappings record regions
280217c9d12eSMel Gorman 	 * that have reservations as they are shared by multiple VMAs.
280317c9d12eSMel Gorman 	 * When the last VMA disappears, the region map says how much
280417c9d12eSMel Gorman 	 * the reservation was and the page cache tells how much of
280517c9d12eSMel Gorman 	 * the reservation was consumed. Private mappings are per-VMA and
280617c9d12eSMel Gorman 	 * only the consumed reservations are tracked. When the VMA
280717c9d12eSMel Gorman 	 * disappears, the original reservation is the VMA size and the
280817c9d12eSMel Gorman 	 * consumed reservations are stored in the map. Hence, nothing
280917c9d12eSMel Gorman 	 * else has to be done for private mappings here
281017c9d12eSMel Gorman 	 */
2811f83a275dSMel Gorman 	if (!vma || vma->vm_flags & VM_MAYSHARE)
281217c9d12eSMel Gorman 		region_add(&inode->i_mapping->private_list, from, to);
2813a43a8c39SChen, Kenneth W 	return 0;
2814a43a8c39SChen, Kenneth W }
2815a43a8c39SChen, Kenneth W 
2816a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
2817a43a8c39SChen, Kenneth W {
2818a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
2819a43a8c39SChen, Kenneth W 	long chg = region_truncate(&inode->i_mapping->private_list, offset);
282045c682a6SKen Chen 
282145c682a6SKen Chen 	spin_lock(&inode->i_lock);
2822e4c6f8beSEric Sandeen 	inode->i_blocks -= (blocks_per_huge_page(h) * freed);
282345c682a6SKen Chen 	spin_unlock(&inode->i_lock);
282445c682a6SKen Chen 
282590d8b7e6SAdam Litke 	hugetlb_put_quota(inode->i_mapping, (chg - freed));
2826a5516438SAndi Kleen 	hugetlb_acct_memory(h, -(chg - freed));
2827a43a8c39SChen, Kenneth W }
2828