xref: /openbmc/linux/mm/hugetlb.c (revision 6af2acb6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Generic hugetlb support.
31da177e4SLinus Torvalds  * (C) William Irwin, April 2004
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds #include <linux/gfp.h>
61da177e4SLinus Torvalds #include <linux/list.h>
71da177e4SLinus Torvalds #include <linux/init.h>
81da177e4SLinus Torvalds #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/mm.h>
101da177e4SLinus Torvalds #include <linux/sysctl.h>
111da177e4SLinus Torvalds #include <linux/highmem.h>
121da177e4SLinus Torvalds #include <linux/nodemask.h>
1363551ae0SDavid Gibson #include <linux/pagemap.h>
145da7ca86SChristoph Lameter #include <linux/mempolicy.h>
15aea47ff3SChristoph Lameter #include <linux/cpuset.h>
163935baa9SDavid Gibson #include <linux/mutex.h>
175da7ca86SChristoph Lameter 
1863551ae0SDavid Gibson #include <asm/page.h>
1963551ae0SDavid Gibson #include <asm/pgtable.h>
2063551ae0SDavid Gibson 
2163551ae0SDavid Gibson #include <linux/hugetlb.h>
227835e98bSNick Piggin #include "internal.h"
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
25a43a8c39SChen, Kenneth W static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages;
261da177e4SLinus Torvalds unsigned long max_huge_pages;
271da177e4SLinus Torvalds static struct list_head hugepage_freelists[MAX_NUMNODES];
281da177e4SLinus Torvalds static unsigned int nr_huge_pages_node[MAX_NUMNODES];
291da177e4SLinus Torvalds static unsigned int free_huge_pages_node[MAX_NUMNODES];
30396faf03SMel Gorman static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
31396faf03SMel Gorman unsigned long hugepages_treat_as_movable;
32396faf03SMel Gorman 
333935baa9SDavid Gibson /*
343935baa9SDavid Gibson  * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
353935baa9SDavid Gibson  */
363935baa9SDavid Gibson static DEFINE_SPINLOCK(hugetlb_lock);
370bd0f9fbSEric Paris 
3879ac6ba4SDavid Gibson static void clear_huge_page(struct page *page, unsigned long addr)
3979ac6ba4SDavid Gibson {
4079ac6ba4SDavid Gibson 	int i;
4179ac6ba4SDavid Gibson 
4279ac6ba4SDavid Gibson 	might_sleep();
4379ac6ba4SDavid Gibson 	for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
4479ac6ba4SDavid Gibson 		cond_resched();
45281e0e3bSRalf Baechle 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
4679ac6ba4SDavid Gibson 	}
4779ac6ba4SDavid Gibson }
4879ac6ba4SDavid Gibson 
4979ac6ba4SDavid Gibson static void copy_huge_page(struct page *dst, struct page *src,
509de455b2SAtsushi Nemoto 			   unsigned long addr, struct vm_area_struct *vma)
5179ac6ba4SDavid Gibson {
5279ac6ba4SDavid Gibson 	int i;
5379ac6ba4SDavid Gibson 
5479ac6ba4SDavid Gibson 	might_sleep();
5579ac6ba4SDavid Gibson 	for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
5679ac6ba4SDavid Gibson 		cond_resched();
579de455b2SAtsushi Nemoto 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
5879ac6ba4SDavid Gibson 	}
5979ac6ba4SDavid Gibson }
6079ac6ba4SDavid Gibson 
611da177e4SLinus Torvalds static void enqueue_huge_page(struct page *page)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	int nid = page_to_nid(page);
641da177e4SLinus Torvalds 	list_add(&page->lru, &hugepage_freelists[nid]);
651da177e4SLinus Torvalds 	free_huge_pages++;
661da177e4SLinus Torvalds 	free_huge_pages_node[nid]++;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
695da7ca86SChristoph Lameter static struct page *dequeue_huge_page(struct vm_area_struct *vma,
705da7ca86SChristoph Lameter 				unsigned long address)
711da177e4SLinus Torvalds {
7231a5c6e4SNishanth Aravamudan 	int nid;
731da177e4SLinus Torvalds 	struct page *page = NULL;
74480eccf9SLee Schermerhorn 	struct mempolicy *mpol;
75396faf03SMel Gorman 	struct zonelist *zonelist = huge_zonelist(vma, address,
76480eccf9SLee Schermerhorn 					htlb_alloc_mask, &mpol);
7796df9333SChristoph Lameter 	struct zone **z;
781da177e4SLinus Torvalds 
7996df9333SChristoph Lameter 	for (z = zonelist->zones; *z; z++) {
8089fa3024SChristoph Lameter 		nid = zone_to_nid(*z);
81396faf03SMel Gorman 		if (cpuset_zone_allowed_softwall(*z, htlb_alloc_mask) &&
823abf7afdSAndrew Morton 		    !list_empty(&hugepage_freelists[nid])) {
831da177e4SLinus Torvalds 			page = list_entry(hugepage_freelists[nid].next,
841da177e4SLinus Torvalds 					  struct page, lru);
851da177e4SLinus Torvalds 			list_del(&page->lru);
861da177e4SLinus Torvalds 			free_huge_pages--;
871da177e4SLinus Torvalds 			free_huge_pages_node[nid]--;
885ab3ee7bSKen Chen 			break;
891da177e4SLinus Torvalds 		}
903abf7afdSAndrew Morton 	}
91480eccf9SLee Schermerhorn 	mpol_free(mpol);	/* unref if mpol !NULL */
921da177e4SLinus Torvalds 	return page;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
956af2acb6SAdam Litke static void update_and_free_page(struct page *page)
966af2acb6SAdam Litke {
976af2acb6SAdam Litke 	int i;
986af2acb6SAdam Litke 	nr_huge_pages--;
996af2acb6SAdam Litke 	nr_huge_pages_node[page_to_nid(page)]--;
1006af2acb6SAdam Litke 	for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
1016af2acb6SAdam Litke 		page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
1026af2acb6SAdam Litke 				1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
1036af2acb6SAdam Litke 				1 << PG_private | 1<< PG_writeback);
1046af2acb6SAdam Litke 	}
1056af2acb6SAdam Litke 	set_compound_page_dtor(page, NULL);
1066af2acb6SAdam Litke 	set_page_refcounted(page);
1076af2acb6SAdam Litke 	__free_pages(page, HUGETLB_PAGE_ORDER);
1086af2acb6SAdam Litke }
1096af2acb6SAdam Litke 
11027a85ef1SDavid Gibson static void free_huge_page(struct page *page)
11127a85ef1SDavid Gibson {
11227a85ef1SDavid Gibson 	BUG_ON(page_count(page));
11327a85ef1SDavid Gibson 
11427a85ef1SDavid Gibson 	INIT_LIST_HEAD(&page->lru);
11527a85ef1SDavid Gibson 
11627a85ef1SDavid Gibson 	spin_lock(&hugetlb_lock);
11727a85ef1SDavid Gibson 	enqueue_huge_page(page);
11827a85ef1SDavid Gibson 	spin_unlock(&hugetlb_lock);
11927a85ef1SDavid Gibson }
12027a85ef1SDavid Gibson 
121a482289dSNick Piggin static int alloc_fresh_huge_page(void)
1221da177e4SLinus Torvalds {
123f96efd58SJoe Jin 	static int prev_nid;
1241da177e4SLinus Torvalds 	struct page *page;
125f96efd58SJoe Jin 	int nid;
126f96efd58SJoe Jin 
1277ed5cb2bSHugh Dickins 	/*
1287ed5cb2bSHugh Dickins 	 * Copy static prev_nid to local nid, work on that, then copy it
1297ed5cb2bSHugh Dickins 	 * back to prev_nid afterwards: otherwise there's a window in which
1307ed5cb2bSHugh Dickins 	 * a racer might pass invalid nid MAX_NUMNODES to alloc_pages_node.
1317ed5cb2bSHugh Dickins 	 * But we don't need to use a spin_lock here: it really doesn't
1327ed5cb2bSHugh Dickins 	 * matter if occasionally a racer chooses the same nid as we do.
1337ed5cb2bSHugh Dickins 	 */
134f96efd58SJoe Jin 	nid = next_node(prev_nid, node_online_map);
135fdb7cc59SPaul Jackson 	if (nid == MAX_NUMNODES)
136fdb7cc59SPaul Jackson 		nid = first_node(node_online_map);
137f96efd58SJoe Jin 	prev_nid = nid;
138f96efd58SJoe Jin 
139396faf03SMel Gorman 	page = alloc_pages_node(nid, htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN,
140f96efd58SJoe Jin 					HUGETLB_PAGE_ORDER);
1411da177e4SLinus Torvalds 	if (page) {
14233f2ef89SAndy Whitcroft 		set_compound_page_dtor(page, free_huge_page);
1430bd0f9fbSEric Paris 		spin_lock(&hugetlb_lock);
1441da177e4SLinus Torvalds 		nr_huge_pages++;
1451da177e4SLinus Torvalds 		nr_huge_pages_node[page_to_nid(page)]++;
1460bd0f9fbSEric Paris 		spin_unlock(&hugetlb_lock);
147a482289dSNick Piggin 		put_page(page); /* free it into the hugepage allocator */
148a482289dSNick Piggin 		return 1;
1491da177e4SLinus Torvalds 	}
150a482289dSNick Piggin 	return 0;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
15327a85ef1SDavid Gibson static struct page *alloc_huge_page(struct vm_area_struct *vma,
15427a85ef1SDavid Gibson 				    unsigned long addr)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	struct page *page;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
159a43a8c39SChen, Kenneth W 	if (vma->vm_flags & VM_MAYSHARE)
160a43a8c39SChen, Kenneth W 		resv_huge_pages--;
161a43a8c39SChen, Kenneth W 	else if (free_huge_pages <= resv_huge_pages)
162b45b5bd6SDavid Gibson 		goto fail;
163b45b5bd6SDavid Gibson 
164b45b5bd6SDavid Gibson 	page = dequeue_huge_page(vma, addr);
165b45b5bd6SDavid Gibson 	if (!page)
166b45b5bd6SDavid Gibson 		goto fail;
167b45b5bd6SDavid Gibson 
1681da177e4SLinus Torvalds 	spin_unlock(&hugetlb_lock);
1697835e98bSNick Piggin 	set_page_refcounted(page);
1701da177e4SLinus Torvalds 	return page;
171b45b5bd6SDavid Gibson 
172b45b5bd6SDavid Gibson fail:
173ace4bd29SKen Chen 	if (vma->vm_flags & VM_MAYSHARE)
174ace4bd29SKen Chen 		resv_huge_pages++;
175b45b5bd6SDavid Gibson 	spin_unlock(&hugetlb_lock);
176b45b5bd6SDavid Gibson 	return NULL;
177b45b5bd6SDavid Gibson }
178b45b5bd6SDavid Gibson 
1791da177e4SLinus Torvalds static int __init hugetlb_init(void)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	unsigned long i;
1821da177e4SLinus Torvalds 
1833c726f8dSBenjamin Herrenschmidt 	if (HPAGE_SHIFT == 0)
1843c726f8dSBenjamin Herrenschmidt 		return 0;
1853c726f8dSBenjamin Herrenschmidt 
1861da177e4SLinus Torvalds 	for (i = 0; i < MAX_NUMNODES; ++i)
1871da177e4SLinus Torvalds 		INIT_LIST_HEAD(&hugepage_freelists[i]);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	for (i = 0; i < max_huge_pages; ++i) {
190a482289dSNick Piggin 		if (!alloc_fresh_huge_page())
1911da177e4SLinus Torvalds 			break;
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 	max_huge_pages = free_huge_pages = nr_huge_pages = i;
1941da177e4SLinus Torvalds 	printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
1951da177e4SLinus Torvalds 	return 0;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds module_init(hugetlb_init);
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds static int __init hugetlb_setup(char *s)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	if (sscanf(s, "%lu", &max_huge_pages) <= 0)
2021da177e4SLinus Torvalds 		max_huge_pages = 0;
2031da177e4SLinus Torvalds 	return 1;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds __setup("hugepages=", hugetlb_setup);
2061da177e4SLinus Torvalds 
2078a630112SKen Chen static unsigned int cpuset_mems_nr(unsigned int *array)
2088a630112SKen Chen {
2098a630112SKen Chen 	int node;
2108a630112SKen Chen 	unsigned int nr = 0;
2118a630112SKen Chen 
2128a630112SKen Chen 	for_each_node_mask(node, cpuset_current_mems_allowed)
2138a630112SKen Chen 		nr += array[node];
2148a630112SKen Chen 
2158a630112SKen Chen 	return nr;
2168a630112SKen Chen }
2178a630112SKen Chen 
2181da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
2191da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM
2201da177e4SLinus Torvalds static void try_to_free_low(unsigned long count)
2211da177e4SLinus Torvalds {
2224415cc8dSChristoph Lameter 	int i;
2234415cc8dSChristoph Lameter 
2241da177e4SLinus Torvalds 	for (i = 0; i < MAX_NUMNODES; ++i) {
2251da177e4SLinus Torvalds 		struct page *page, *next;
2261da177e4SLinus Torvalds 		list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
2271da177e4SLinus Torvalds 			if (PageHighMem(page))
2281da177e4SLinus Torvalds 				continue;
2291da177e4SLinus Torvalds 			list_del(&page->lru);
2301da177e4SLinus Torvalds 			update_and_free_page(page);
2311da177e4SLinus Torvalds 			free_huge_pages--;
2324415cc8dSChristoph Lameter 			free_huge_pages_node[page_to_nid(page)]--;
2331da177e4SLinus Torvalds 			if (count >= nr_huge_pages)
2341da177e4SLinus Torvalds 				return;
2351da177e4SLinus Torvalds 		}
2361da177e4SLinus Torvalds 	}
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds #else
2391da177e4SLinus Torvalds static inline void try_to_free_low(unsigned long count)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds #endif
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static unsigned long set_max_huge_pages(unsigned long count)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	while (count > nr_huge_pages) {
247a482289dSNick Piggin 		if (!alloc_fresh_huge_page())
2481da177e4SLinus Torvalds 			return nr_huge_pages;
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds 	if (count >= nr_huge_pages)
2511da177e4SLinus Torvalds 		return nr_huge_pages;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	spin_lock(&hugetlb_lock);
254a43a8c39SChen, Kenneth W 	count = max(count, resv_huge_pages);
2551da177e4SLinus Torvalds 	try_to_free_low(count);
2561da177e4SLinus Torvalds 	while (count < nr_huge_pages) {
2575da7ca86SChristoph Lameter 		struct page *page = dequeue_huge_page(NULL, 0);
2581da177e4SLinus Torvalds 		if (!page)
2591da177e4SLinus Torvalds 			break;
2601da177e4SLinus Torvalds 		update_and_free_page(page);
2611da177e4SLinus Torvalds 	}
2621da177e4SLinus Torvalds 	spin_unlock(&hugetlb_lock);
2631da177e4SLinus Torvalds 	return nr_huge_pages;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds int hugetlb_sysctl_handler(struct ctl_table *table, int write,
2671da177e4SLinus Torvalds 			   struct file *file, void __user *buffer,
2681da177e4SLinus Torvalds 			   size_t *length, loff_t *ppos)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
2711da177e4SLinus Torvalds 	max_huge_pages = set_max_huge_pages(max_huge_pages);
2721da177e4SLinus Torvalds 	return 0;
2731da177e4SLinus Torvalds }
274396faf03SMel Gorman 
275396faf03SMel Gorman int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
276396faf03SMel Gorman 			struct file *file, void __user *buffer,
277396faf03SMel Gorman 			size_t *length, loff_t *ppos)
278396faf03SMel Gorman {
279396faf03SMel Gorman 	proc_dointvec(table, write, file, buffer, length, ppos);
280396faf03SMel Gorman 	if (hugepages_treat_as_movable)
281396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
282396faf03SMel Gorman 	else
283396faf03SMel Gorman 		htlb_alloc_mask = GFP_HIGHUSER;
284396faf03SMel Gorman 	return 0;
285396faf03SMel Gorman }
286396faf03SMel Gorman 
2871da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds int hugetlb_report_meminfo(char *buf)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	return sprintf(buf,
2921da177e4SLinus Torvalds 			"HugePages_Total: %5lu\n"
2931da177e4SLinus Torvalds 			"HugePages_Free:  %5lu\n"
294b45b5bd6SDavid Gibson 			"HugePages_Rsvd:  %5lu\n"
2951da177e4SLinus Torvalds 			"Hugepagesize:    %5lu kB\n",
2961da177e4SLinus Torvalds 			nr_huge_pages,
2971da177e4SLinus Torvalds 			free_huge_pages,
298a43a8c39SChen, Kenneth W 			resv_huge_pages,
2991da177e4SLinus Torvalds 			HPAGE_SIZE/1024);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds int hugetlb_report_node_meminfo(int nid, char *buf)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	return sprintf(buf,
3051da177e4SLinus Torvalds 		"Node %d HugePages_Total: %5u\n"
3061da177e4SLinus Torvalds 		"Node %d HugePages_Free:  %5u\n",
3071da177e4SLinus Torvalds 		nid, nr_huge_pages_node[nid],
3081da177e4SLinus Torvalds 		nid, free_huge_pages_node[nid]);
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
3121da177e4SLinus Torvalds unsigned long hugetlb_total_pages(void)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds /*
3181da177e4SLinus Torvalds  * We cannot handle pagefaults against hugetlb pages at all.  They cause
3191da177e4SLinus Torvalds  * handle_mm_fault() to try to instantiate regular-sized pages in the
3201da177e4SLinus Torvalds  * hugegpage VMA.  do_page_fault() is supposed to trap this, so BUG is we get
3211da177e4SLinus Torvalds  * this far.
3221da177e4SLinus Torvalds  */
323d0217ac0SNick Piggin static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	BUG();
326d0217ac0SNick Piggin 	return 0;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds struct vm_operations_struct hugetlb_vm_ops = {
330d0217ac0SNick Piggin 	.fault = hugetlb_vm_op_fault,
3311da177e4SLinus Torvalds };
3321da177e4SLinus Torvalds 
3331e8f889bSDavid Gibson static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
3341e8f889bSDavid Gibson 				int writable)
33563551ae0SDavid Gibson {
33663551ae0SDavid Gibson 	pte_t entry;
33763551ae0SDavid Gibson 
3381e8f889bSDavid Gibson 	if (writable) {
33963551ae0SDavid Gibson 		entry =
34063551ae0SDavid Gibson 		    pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
34163551ae0SDavid Gibson 	} else {
34263551ae0SDavid Gibson 		entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
34363551ae0SDavid Gibson 	}
34463551ae0SDavid Gibson 	entry = pte_mkyoung(entry);
34563551ae0SDavid Gibson 	entry = pte_mkhuge(entry);
34663551ae0SDavid Gibson 
34763551ae0SDavid Gibson 	return entry;
34863551ae0SDavid Gibson }
34963551ae0SDavid Gibson 
3501e8f889bSDavid Gibson static void set_huge_ptep_writable(struct vm_area_struct *vma,
3511e8f889bSDavid Gibson 				   unsigned long address, pte_t *ptep)
3521e8f889bSDavid Gibson {
3531e8f889bSDavid Gibson 	pte_t entry;
3541e8f889bSDavid Gibson 
3551e8f889bSDavid Gibson 	entry = pte_mkwrite(pte_mkdirty(*ptep));
3568dab5241SBenjamin Herrenschmidt 	if (ptep_set_access_flags(vma, address, ptep, entry, 1)) {
3571e8f889bSDavid Gibson 		update_mmu_cache(vma, address, entry);
3581e8f889bSDavid Gibson 	}
3598dab5241SBenjamin Herrenschmidt }
3601e8f889bSDavid Gibson 
3611e8f889bSDavid Gibson 
36263551ae0SDavid Gibson int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
36363551ae0SDavid Gibson 			    struct vm_area_struct *vma)
36463551ae0SDavid Gibson {
36563551ae0SDavid Gibson 	pte_t *src_pte, *dst_pte, entry;
36663551ae0SDavid Gibson 	struct page *ptepage;
3671c59827dSHugh Dickins 	unsigned long addr;
3681e8f889bSDavid Gibson 	int cow;
3691e8f889bSDavid Gibson 
3701e8f889bSDavid Gibson 	cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
37163551ae0SDavid Gibson 
3721c59827dSHugh Dickins 	for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
373c74df32cSHugh Dickins 		src_pte = huge_pte_offset(src, addr);
374c74df32cSHugh Dickins 		if (!src_pte)
375c74df32cSHugh Dickins 			continue;
37663551ae0SDavid Gibson 		dst_pte = huge_pte_alloc(dst, addr);
37763551ae0SDavid Gibson 		if (!dst_pte)
37863551ae0SDavid Gibson 			goto nomem;
379c74df32cSHugh Dickins 		spin_lock(&dst->page_table_lock);
3801c59827dSHugh Dickins 		spin_lock(&src->page_table_lock);
381c74df32cSHugh Dickins 		if (!pte_none(*src_pte)) {
3821e8f889bSDavid Gibson 			if (cow)
3831e8f889bSDavid Gibson 				ptep_set_wrprotect(src, addr, src_pte);
38463551ae0SDavid Gibson 			entry = *src_pte;
38563551ae0SDavid Gibson 			ptepage = pte_page(entry);
38663551ae0SDavid Gibson 			get_page(ptepage);
38763551ae0SDavid Gibson 			set_huge_pte_at(dst, addr, dst_pte, entry);
3881c59827dSHugh Dickins 		}
3891c59827dSHugh Dickins 		spin_unlock(&src->page_table_lock);
390c74df32cSHugh Dickins 		spin_unlock(&dst->page_table_lock);
39163551ae0SDavid Gibson 	}
39263551ae0SDavid Gibson 	return 0;
39363551ae0SDavid Gibson 
39463551ae0SDavid Gibson nomem:
39563551ae0SDavid Gibson 	return -ENOMEM;
39663551ae0SDavid Gibson }
39763551ae0SDavid Gibson 
398502717f4SChen, Kenneth W void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
39963551ae0SDavid Gibson 			    unsigned long end)
40063551ae0SDavid Gibson {
40163551ae0SDavid Gibson 	struct mm_struct *mm = vma->vm_mm;
40263551ae0SDavid Gibson 	unsigned long address;
403c7546f8fSDavid Gibson 	pte_t *ptep;
40463551ae0SDavid Gibson 	pte_t pte;
40563551ae0SDavid Gibson 	struct page *page;
406fe1668aeSChen, Kenneth W 	struct page *tmp;
407c0a499c2SChen, Kenneth W 	/*
408c0a499c2SChen, Kenneth W 	 * A page gathering list, protected by per file i_mmap_lock. The
409c0a499c2SChen, Kenneth W 	 * lock is used to avoid list corruption from multiple unmapping
410c0a499c2SChen, Kenneth W 	 * of the same page since we are using page->lru.
411c0a499c2SChen, Kenneth W 	 */
412fe1668aeSChen, Kenneth W 	LIST_HEAD(page_list);
41363551ae0SDavid Gibson 
41463551ae0SDavid Gibson 	WARN_ON(!is_vm_hugetlb_page(vma));
41563551ae0SDavid Gibson 	BUG_ON(start & ~HPAGE_MASK);
41663551ae0SDavid Gibson 	BUG_ON(end & ~HPAGE_MASK);
41763551ae0SDavid Gibson 
418508034a3SHugh Dickins 	spin_lock(&mm->page_table_lock);
41963551ae0SDavid Gibson 	for (address = start; address < end; address += HPAGE_SIZE) {
420c7546f8fSDavid Gibson 		ptep = huge_pte_offset(mm, address);
421c7546f8fSDavid Gibson 		if (!ptep)
422c7546f8fSDavid Gibson 			continue;
423c7546f8fSDavid Gibson 
42439dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
42539dde65cSChen, Kenneth W 			continue;
42639dde65cSChen, Kenneth W 
427c7546f8fSDavid Gibson 		pte = huge_ptep_get_and_clear(mm, address, ptep);
42863551ae0SDavid Gibson 		if (pte_none(pte))
42963551ae0SDavid Gibson 			continue;
430c7546f8fSDavid Gibson 
43163551ae0SDavid Gibson 		page = pte_page(pte);
4326649a386SKen Chen 		if (pte_dirty(pte))
4336649a386SKen Chen 			set_page_dirty(page);
434fe1668aeSChen, Kenneth W 		list_add(&page->lru, &page_list);
43563551ae0SDavid Gibson 	}
4361da177e4SLinus Torvalds 	spin_unlock(&mm->page_table_lock);
437508034a3SHugh Dickins 	flush_tlb_range(vma, start, end);
438fe1668aeSChen, Kenneth W 	list_for_each_entry_safe(page, tmp, &page_list, lru) {
439fe1668aeSChen, Kenneth W 		list_del(&page->lru);
440fe1668aeSChen, Kenneth W 		put_page(page);
441fe1668aeSChen, Kenneth W 	}
4421da177e4SLinus Torvalds }
44363551ae0SDavid Gibson 
444502717f4SChen, Kenneth W void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
445502717f4SChen, Kenneth W 			  unsigned long end)
446502717f4SChen, Kenneth W {
447502717f4SChen, Kenneth W 	/*
448502717f4SChen, Kenneth W 	 * It is undesirable to test vma->vm_file as it should be non-null
449502717f4SChen, Kenneth W 	 * for valid hugetlb area. However, vm_file will be NULL in the error
450502717f4SChen, Kenneth W 	 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
451502717f4SChen, Kenneth W 	 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
452502717f4SChen, Kenneth W 	 * to clean up. Since no pte has actually been setup, it is safe to
453502717f4SChen, Kenneth W 	 * do nothing in this case.
454502717f4SChen, Kenneth W 	 */
455502717f4SChen, Kenneth W 	if (vma->vm_file) {
456502717f4SChen, Kenneth W 		spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
457502717f4SChen, Kenneth W 		__unmap_hugepage_range(vma, start, end);
458502717f4SChen, Kenneth W 		spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
459502717f4SChen, Kenneth W 	}
460502717f4SChen, Kenneth W }
461502717f4SChen, Kenneth W 
4621e8f889bSDavid Gibson static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
4631e8f889bSDavid Gibson 			unsigned long address, pte_t *ptep, pte_t pte)
4641e8f889bSDavid Gibson {
4651e8f889bSDavid Gibson 	struct page *old_page, *new_page;
46679ac6ba4SDavid Gibson 	int avoidcopy;
4671e8f889bSDavid Gibson 
4681e8f889bSDavid Gibson 	old_page = pte_page(pte);
4691e8f889bSDavid Gibson 
4701e8f889bSDavid Gibson 	/* If no-one else is actually using this page, avoid the copy
4711e8f889bSDavid Gibson 	 * and just make the page writable */
4721e8f889bSDavid Gibson 	avoidcopy = (page_count(old_page) == 1);
4731e8f889bSDavid Gibson 	if (avoidcopy) {
4741e8f889bSDavid Gibson 		set_huge_ptep_writable(vma, address, ptep);
47583c54070SNick Piggin 		return 0;
4761e8f889bSDavid Gibson 	}
4771e8f889bSDavid Gibson 
4781e8f889bSDavid Gibson 	page_cache_get(old_page);
4795da7ca86SChristoph Lameter 	new_page = alloc_huge_page(vma, address);
4801e8f889bSDavid Gibson 
4811e8f889bSDavid Gibson 	if (!new_page) {
4821e8f889bSDavid Gibson 		page_cache_release(old_page);
4830df420d8SChristoph Lameter 		return VM_FAULT_OOM;
4841e8f889bSDavid Gibson 	}
4851e8f889bSDavid Gibson 
4861e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
4879de455b2SAtsushi Nemoto 	copy_huge_page(new_page, old_page, address, vma);
4881e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
4891e8f889bSDavid Gibson 
4901e8f889bSDavid Gibson 	ptep = huge_pte_offset(mm, address & HPAGE_MASK);
4911e8f889bSDavid Gibson 	if (likely(pte_same(*ptep, pte))) {
4921e8f889bSDavid Gibson 		/* Break COW */
4931e8f889bSDavid Gibson 		set_huge_pte_at(mm, address, ptep,
4941e8f889bSDavid Gibson 				make_huge_pte(vma, new_page, 1));
4951e8f889bSDavid Gibson 		/* Make the old page be freed below */
4961e8f889bSDavid Gibson 		new_page = old_page;
4971e8f889bSDavid Gibson 	}
4981e8f889bSDavid Gibson 	page_cache_release(new_page);
4991e8f889bSDavid Gibson 	page_cache_release(old_page);
50083c54070SNick Piggin 	return 0;
5011e8f889bSDavid Gibson }
5021e8f889bSDavid Gibson 
503a1ed3ddaSRobert P. J. Day static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
5041e8f889bSDavid Gibson 			unsigned long address, pte_t *ptep, int write_access)
505ac9b9c66SHugh Dickins {
506ac9b9c66SHugh Dickins 	int ret = VM_FAULT_SIGBUS;
5074c887265SAdam Litke 	unsigned long idx;
5084c887265SAdam Litke 	unsigned long size;
5094c887265SAdam Litke 	struct page *page;
5104c887265SAdam Litke 	struct address_space *mapping;
5111e8f889bSDavid Gibson 	pte_t new_pte;
5124c887265SAdam Litke 
5134c887265SAdam Litke 	mapping = vma->vm_file->f_mapping;
5144c887265SAdam Litke 	idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
5154c887265SAdam Litke 		+ (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
5164c887265SAdam Litke 
5174c887265SAdam Litke 	/*
5184c887265SAdam Litke 	 * Use page lock to guard against racing truncation
5194c887265SAdam Litke 	 * before we get page_table_lock.
5204c887265SAdam Litke 	 */
5216bda666aSChristoph Lameter retry:
5226bda666aSChristoph Lameter 	page = find_lock_page(mapping, idx);
5236bda666aSChristoph Lameter 	if (!page) {
524ebed4bfcSHugh Dickins 		size = i_size_read(mapping->host) >> HPAGE_SHIFT;
525ebed4bfcSHugh Dickins 		if (idx >= size)
526ebed4bfcSHugh Dickins 			goto out;
5276bda666aSChristoph Lameter 		if (hugetlb_get_quota(mapping))
5284c887265SAdam Litke 			goto out;
5296bda666aSChristoph Lameter 		page = alloc_huge_page(vma, address);
5306bda666aSChristoph Lameter 		if (!page) {
5316bda666aSChristoph Lameter 			hugetlb_put_quota(mapping);
5320df420d8SChristoph Lameter 			ret = VM_FAULT_OOM;
5336bda666aSChristoph Lameter 			goto out;
5346bda666aSChristoph Lameter 		}
53579ac6ba4SDavid Gibson 		clear_huge_page(page, address);
536ac9b9c66SHugh Dickins 
5376bda666aSChristoph Lameter 		if (vma->vm_flags & VM_SHARED) {
5386bda666aSChristoph Lameter 			int err;
5396bda666aSChristoph Lameter 
5406bda666aSChristoph Lameter 			err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
5416bda666aSChristoph Lameter 			if (err) {
5426bda666aSChristoph Lameter 				put_page(page);
5436bda666aSChristoph Lameter 				hugetlb_put_quota(mapping);
5446bda666aSChristoph Lameter 				if (err == -EEXIST)
5456bda666aSChristoph Lameter 					goto retry;
5466bda666aSChristoph Lameter 				goto out;
5476bda666aSChristoph Lameter 			}
5486bda666aSChristoph Lameter 		} else
5496bda666aSChristoph Lameter 			lock_page(page);
5506bda666aSChristoph Lameter 	}
5511e8f889bSDavid Gibson 
552ac9b9c66SHugh Dickins 	spin_lock(&mm->page_table_lock);
5534c887265SAdam Litke 	size = i_size_read(mapping->host) >> HPAGE_SHIFT;
5544c887265SAdam Litke 	if (idx >= size)
5554c887265SAdam Litke 		goto backout;
5564c887265SAdam Litke 
55783c54070SNick Piggin 	ret = 0;
55886e5216fSAdam Litke 	if (!pte_none(*ptep))
5594c887265SAdam Litke 		goto backout;
5604c887265SAdam Litke 
5611e8f889bSDavid Gibson 	new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
5621e8f889bSDavid Gibson 				&& (vma->vm_flags & VM_SHARED)));
5631e8f889bSDavid Gibson 	set_huge_pte_at(mm, address, ptep, new_pte);
5641e8f889bSDavid Gibson 
5651e8f889bSDavid Gibson 	if (write_access && !(vma->vm_flags & VM_SHARED)) {
5661e8f889bSDavid Gibson 		/* Optimization, do the COW without a second fault */
5671e8f889bSDavid Gibson 		ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
5681e8f889bSDavid Gibson 	}
5691e8f889bSDavid Gibson 
570ac9b9c66SHugh Dickins 	spin_unlock(&mm->page_table_lock);
5714c887265SAdam Litke 	unlock_page(page);
5724c887265SAdam Litke out:
573ac9b9c66SHugh Dickins 	return ret;
5744c887265SAdam Litke 
5754c887265SAdam Litke backout:
5764c887265SAdam Litke 	spin_unlock(&mm->page_table_lock);
5774c887265SAdam Litke 	hugetlb_put_quota(mapping);
5784c887265SAdam Litke 	unlock_page(page);
5794c887265SAdam Litke 	put_page(page);
5804c887265SAdam Litke 	goto out;
581ac9b9c66SHugh Dickins }
582ac9b9c66SHugh Dickins 
58386e5216fSAdam Litke int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
58486e5216fSAdam Litke 			unsigned long address, int write_access)
58586e5216fSAdam Litke {
58686e5216fSAdam Litke 	pte_t *ptep;
58786e5216fSAdam Litke 	pte_t entry;
5881e8f889bSDavid Gibson 	int ret;
5893935baa9SDavid Gibson 	static DEFINE_MUTEX(hugetlb_instantiation_mutex);
59086e5216fSAdam Litke 
59186e5216fSAdam Litke 	ptep = huge_pte_alloc(mm, address);
59286e5216fSAdam Litke 	if (!ptep)
59386e5216fSAdam Litke 		return VM_FAULT_OOM;
59486e5216fSAdam Litke 
5953935baa9SDavid Gibson 	/*
5963935baa9SDavid Gibson 	 * Serialize hugepage allocation and instantiation, so that we don't
5973935baa9SDavid Gibson 	 * get spurious allocation failures if two CPUs race to instantiate
5983935baa9SDavid Gibson 	 * the same page in the page cache.
5993935baa9SDavid Gibson 	 */
6003935baa9SDavid Gibson 	mutex_lock(&hugetlb_instantiation_mutex);
60186e5216fSAdam Litke 	entry = *ptep;
6023935baa9SDavid Gibson 	if (pte_none(entry)) {
6033935baa9SDavid Gibson 		ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
6043935baa9SDavid Gibson 		mutex_unlock(&hugetlb_instantiation_mutex);
6053935baa9SDavid Gibson 		return ret;
6063935baa9SDavid Gibson 	}
60786e5216fSAdam Litke 
60883c54070SNick Piggin 	ret = 0;
6091e8f889bSDavid Gibson 
6101e8f889bSDavid Gibson 	spin_lock(&mm->page_table_lock);
6111e8f889bSDavid Gibson 	/* Check for a racing update before calling hugetlb_cow */
6121e8f889bSDavid Gibson 	if (likely(pte_same(entry, *ptep)))
6131e8f889bSDavid Gibson 		if (write_access && !pte_write(entry))
6141e8f889bSDavid Gibson 			ret = hugetlb_cow(mm, vma, address, ptep, entry);
6151e8f889bSDavid Gibson 	spin_unlock(&mm->page_table_lock);
6163935baa9SDavid Gibson 	mutex_unlock(&hugetlb_instantiation_mutex);
6171e8f889bSDavid Gibson 
6181e8f889bSDavid Gibson 	return ret;
61986e5216fSAdam Litke }
62086e5216fSAdam Litke 
62163551ae0SDavid Gibson int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
62263551ae0SDavid Gibson 			struct page **pages, struct vm_area_struct **vmas,
62363551ae0SDavid Gibson 			unsigned long *position, int *length, int i)
62463551ae0SDavid Gibson {
625d5d4b0aaSChen, Kenneth W 	unsigned long pfn_offset;
626d5d4b0aaSChen, Kenneth W 	unsigned long vaddr = *position;
62763551ae0SDavid Gibson 	int remainder = *length;
62863551ae0SDavid Gibson 
6291c59827dSHugh Dickins 	spin_lock(&mm->page_table_lock);
63063551ae0SDavid Gibson 	while (vaddr < vma->vm_end && remainder) {
63163551ae0SDavid Gibson 		pte_t *pte;
63263551ae0SDavid Gibson 		struct page *page;
63363551ae0SDavid Gibson 
6344c887265SAdam Litke 		/*
6354c887265SAdam Litke 		 * Some archs (sparc64, sh*) have multiple pte_ts to
6364c887265SAdam Litke 		 * each hugepage.  We have to make * sure we get the
6374c887265SAdam Litke 		 * first, for the page indexing below to work.
6384c887265SAdam Litke 		 */
63963551ae0SDavid Gibson 		pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
64063551ae0SDavid Gibson 
6411c59827dSHugh Dickins 		if (!pte || pte_none(*pte)) {
6424c887265SAdam Litke 			int ret;
6434c887265SAdam Litke 
6444c887265SAdam Litke 			spin_unlock(&mm->page_table_lock);
6454c887265SAdam Litke 			ret = hugetlb_fault(mm, vma, vaddr, 0);
6464c887265SAdam Litke 			spin_lock(&mm->page_table_lock);
647a89182c7SAdam Litke 			if (!(ret & VM_FAULT_ERROR))
6484c887265SAdam Litke 				continue;
6494c887265SAdam Litke 
6501c59827dSHugh Dickins 			remainder = 0;
6511c59827dSHugh Dickins 			if (!i)
6521c59827dSHugh Dickins 				i = -EFAULT;
6531c59827dSHugh Dickins 			break;
6541c59827dSHugh Dickins 		}
65563551ae0SDavid Gibson 
656d5d4b0aaSChen, Kenneth W 		pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
657d5d4b0aaSChen, Kenneth W 		page = pte_page(*pte);
658d5d4b0aaSChen, Kenneth W same_page:
659d6692183SChen, Kenneth W 		if (pages) {
66063551ae0SDavid Gibson 			get_page(page);
661d5d4b0aaSChen, Kenneth W 			pages[i] = page + pfn_offset;
662d6692183SChen, Kenneth W 		}
66363551ae0SDavid Gibson 
66463551ae0SDavid Gibson 		if (vmas)
66563551ae0SDavid Gibson 			vmas[i] = vma;
66663551ae0SDavid Gibson 
66763551ae0SDavid Gibson 		vaddr += PAGE_SIZE;
668d5d4b0aaSChen, Kenneth W 		++pfn_offset;
66963551ae0SDavid Gibson 		--remainder;
67063551ae0SDavid Gibson 		++i;
671d5d4b0aaSChen, Kenneth W 		if (vaddr < vma->vm_end && remainder &&
672d5d4b0aaSChen, Kenneth W 				pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
673d5d4b0aaSChen, Kenneth W 			/*
674d5d4b0aaSChen, Kenneth W 			 * We use pfn_offset to avoid touching the pageframes
675d5d4b0aaSChen, Kenneth W 			 * of this compound page.
676d5d4b0aaSChen, Kenneth W 			 */
677d5d4b0aaSChen, Kenneth W 			goto same_page;
678d5d4b0aaSChen, Kenneth W 		}
67963551ae0SDavid Gibson 	}
6801c59827dSHugh Dickins 	spin_unlock(&mm->page_table_lock);
68163551ae0SDavid Gibson 	*length = remainder;
68263551ae0SDavid Gibson 	*position = vaddr;
68363551ae0SDavid Gibson 
68463551ae0SDavid Gibson 	return i;
68563551ae0SDavid Gibson }
6868f860591SZhang, Yanmin 
6878f860591SZhang, Yanmin void hugetlb_change_protection(struct vm_area_struct *vma,
6888f860591SZhang, Yanmin 		unsigned long address, unsigned long end, pgprot_t newprot)
6898f860591SZhang, Yanmin {
6908f860591SZhang, Yanmin 	struct mm_struct *mm = vma->vm_mm;
6918f860591SZhang, Yanmin 	unsigned long start = address;
6928f860591SZhang, Yanmin 	pte_t *ptep;
6938f860591SZhang, Yanmin 	pte_t pte;
6948f860591SZhang, Yanmin 
6958f860591SZhang, Yanmin 	BUG_ON(address >= end);
6968f860591SZhang, Yanmin 	flush_cache_range(vma, address, end);
6978f860591SZhang, Yanmin 
69839dde65cSChen, Kenneth W 	spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
6998f860591SZhang, Yanmin 	spin_lock(&mm->page_table_lock);
7008f860591SZhang, Yanmin 	for (; address < end; address += HPAGE_SIZE) {
7018f860591SZhang, Yanmin 		ptep = huge_pte_offset(mm, address);
7028f860591SZhang, Yanmin 		if (!ptep)
7038f860591SZhang, Yanmin 			continue;
70439dde65cSChen, Kenneth W 		if (huge_pmd_unshare(mm, &address, ptep))
70539dde65cSChen, Kenneth W 			continue;
7068f860591SZhang, Yanmin 		if (!pte_none(*ptep)) {
7078f860591SZhang, Yanmin 			pte = huge_ptep_get_and_clear(mm, address, ptep);
7088f860591SZhang, Yanmin 			pte = pte_mkhuge(pte_modify(pte, newprot));
7098f860591SZhang, Yanmin 			set_huge_pte_at(mm, address, ptep, pte);
7108f860591SZhang, Yanmin 		}
7118f860591SZhang, Yanmin 	}
7128f860591SZhang, Yanmin 	spin_unlock(&mm->page_table_lock);
71339dde65cSChen, Kenneth W 	spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
7148f860591SZhang, Yanmin 
7158f860591SZhang, Yanmin 	flush_tlb_range(vma, start, end);
7168f860591SZhang, Yanmin }
7178f860591SZhang, Yanmin 
718a43a8c39SChen, Kenneth W struct file_region {
719a43a8c39SChen, Kenneth W 	struct list_head link;
720a43a8c39SChen, Kenneth W 	long from;
721a43a8c39SChen, Kenneth W 	long to;
722a43a8c39SChen, Kenneth W };
723a43a8c39SChen, Kenneth W 
724a43a8c39SChen, Kenneth W static long region_add(struct list_head *head, long f, long t)
725a43a8c39SChen, Kenneth W {
726a43a8c39SChen, Kenneth W 	struct file_region *rg, *nrg, *trg;
727a43a8c39SChen, Kenneth W 
728a43a8c39SChen, Kenneth W 	/* Locate the region we are either in or before. */
729a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, head, link)
730a43a8c39SChen, Kenneth W 		if (f <= rg->to)
731a43a8c39SChen, Kenneth W 			break;
732a43a8c39SChen, Kenneth W 
733a43a8c39SChen, Kenneth W 	/* Round our left edge to the current segment if it encloses us. */
734a43a8c39SChen, Kenneth W 	if (f > rg->from)
735a43a8c39SChen, Kenneth W 		f = rg->from;
736a43a8c39SChen, Kenneth W 
737a43a8c39SChen, Kenneth W 	/* Check for and consume any regions we now overlap with. */
738a43a8c39SChen, Kenneth W 	nrg = rg;
739a43a8c39SChen, Kenneth W 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
740a43a8c39SChen, Kenneth W 		if (&rg->link == head)
741a43a8c39SChen, Kenneth W 			break;
742a43a8c39SChen, Kenneth W 		if (rg->from > t)
743a43a8c39SChen, Kenneth W 			break;
744a43a8c39SChen, Kenneth W 
745a43a8c39SChen, Kenneth W 		/* If this area reaches higher then extend our area to
746a43a8c39SChen, Kenneth W 		 * include it completely.  If this is not the first area
747a43a8c39SChen, Kenneth W 		 * which we intend to reuse, free it. */
748a43a8c39SChen, Kenneth W 		if (rg->to > t)
749a43a8c39SChen, Kenneth W 			t = rg->to;
750a43a8c39SChen, Kenneth W 		if (rg != nrg) {
751a43a8c39SChen, Kenneth W 			list_del(&rg->link);
752a43a8c39SChen, Kenneth W 			kfree(rg);
753a43a8c39SChen, Kenneth W 		}
754a43a8c39SChen, Kenneth W 	}
755a43a8c39SChen, Kenneth W 	nrg->from = f;
756a43a8c39SChen, Kenneth W 	nrg->to = t;
757a43a8c39SChen, Kenneth W 	return 0;
758a43a8c39SChen, Kenneth W }
759a43a8c39SChen, Kenneth W 
760a43a8c39SChen, Kenneth W static long region_chg(struct list_head *head, long f, long t)
761a43a8c39SChen, Kenneth W {
762a43a8c39SChen, Kenneth W 	struct file_region *rg, *nrg;
763a43a8c39SChen, Kenneth W 	long chg = 0;
764a43a8c39SChen, Kenneth W 
765a43a8c39SChen, Kenneth W 	/* Locate the region we are before or in. */
766a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, head, link)
767a43a8c39SChen, Kenneth W 		if (f <= rg->to)
768a43a8c39SChen, Kenneth W 			break;
769a43a8c39SChen, Kenneth W 
770a43a8c39SChen, Kenneth W 	/* If we are below the current region then a new region is required.
771a43a8c39SChen, Kenneth W 	 * Subtle, allocate a new region at the position but make it zero
772a43a8c39SChen, Kenneth W 	 * size such that we can guarentee to record the reservation. */
773a43a8c39SChen, Kenneth W 	if (&rg->link == head || t < rg->from) {
774a43a8c39SChen, Kenneth W 		nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
775a43a8c39SChen, Kenneth W 		if (nrg == 0)
776a43a8c39SChen, Kenneth W 			return -ENOMEM;
777a43a8c39SChen, Kenneth W 		nrg->from = f;
778a43a8c39SChen, Kenneth W 		nrg->to   = f;
779a43a8c39SChen, Kenneth W 		INIT_LIST_HEAD(&nrg->link);
780a43a8c39SChen, Kenneth W 		list_add(&nrg->link, rg->link.prev);
781a43a8c39SChen, Kenneth W 
782a43a8c39SChen, Kenneth W 		return t - f;
783a43a8c39SChen, Kenneth W 	}
784a43a8c39SChen, Kenneth W 
785a43a8c39SChen, Kenneth W 	/* Round our left edge to the current segment if it encloses us. */
786a43a8c39SChen, Kenneth W 	if (f > rg->from)
787a43a8c39SChen, Kenneth W 		f = rg->from;
788a43a8c39SChen, Kenneth W 	chg = t - f;
789a43a8c39SChen, Kenneth W 
790a43a8c39SChen, Kenneth W 	/* Check for and consume any regions we now overlap with. */
791a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, rg->link.prev, link) {
792a43a8c39SChen, Kenneth W 		if (&rg->link == head)
793a43a8c39SChen, Kenneth W 			break;
794a43a8c39SChen, Kenneth W 		if (rg->from > t)
795a43a8c39SChen, Kenneth W 			return chg;
796a43a8c39SChen, Kenneth W 
797a43a8c39SChen, Kenneth W 		/* We overlap with this area, if it extends futher than
798a43a8c39SChen, Kenneth W 		 * us then we must extend ourselves.  Account for its
799a43a8c39SChen, Kenneth W 		 * existing reservation. */
800a43a8c39SChen, Kenneth W 		if (rg->to > t) {
801a43a8c39SChen, Kenneth W 			chg += rg->to - t;
802a43a8c39SChen, Kenneth W 			t = rg->to;
803a43a8c39SChen, Kenneth W 		}
804a43a8c39SChen, Kenneth W 		chg -= rg->to - rg->from;
805a43a8c39SChen, Kenneth W 	}
806a43a8c39SChen, Kenneth W 	return chg;
807a43a8c39SChen, Kenneth W }
808a43a8c39SChen, Kenneth W 
809a43a8c39SChen, Kenneth W static long region_truncate(struct list_head *head, long end)
810a43a8c39SChen, Kenneth W {
811a43a8c39SChen, Kenneth W 	struct file_region *rg, *trg;
812a43a8c39SChen, Kenneth W 	long chg = 0;
813a43a8c39SChen, Kenneth W 
814a43a8c39SChen, Kenneth W 	/* Locate the region we are either in or before. */
815a43a8c39SChen, Kenneth W 	list_for_each_entry(rg, head, link)
816a43a8c39SChen, Kenneth W 		if (end <= rg->to)
817a43a8c39SChen, Kenneth W 			break;
818a43a8c39SChen, Kenneth W 	if (&rg->link == head)
819a43a8c39SChen, Kenneth W 		return 0;
820a43a8c39SChen, Kenneth W 
821a43a8c39SChen, Kenneth W 	/* If we are in the middle of a region then adjust it. */
822a43a8c39SChen, Kenneth W 	if (end > rg->from) {
823a43a8c39SChen, Kenneth W 		chg = rg->to - end;
824a43a8c39SChen, Kenneth W 		rg->to = end;
825a43a8c39SChen, Kenneth W 		rg = list_entry(rg->link.next, typeof(*rg), link);
826a43a8c39SChen, Kenneth W 	}
827a43a8c39SChen, Kenneth W 
828a43a8c39SChen, Kenneth W 	/* Drop any remaining regions. */
829a43a8c39SChen, Kenneth W 	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
830a43a8c39SChen, Kenneth W 		if (&rg->link == head)
831a43a8c39SChen, Kenneth W 			break;
832a43a8c39SChen, Kenneth W 		chg += rg->to - rg->from;
833a43a8c39SChen, Kenneth W 		list_del(&rg->link);
834a43a8c39SChen, Kenneth W 		kfree(rg);
835a43a8c39SChen, Kenneth W 	}
836a43a8c39SChen, Kenneth W 	return chg;
837a43a8c39SChen, Kenneth W }
838a43a8c39SChen, Kenneth W 
839a43a8c39SChen, Kenneth W static int hugetlb_acct_memory(long delta)
840a43a8c39SChen, Kenneth W {
841a43a8c39SChen, Kenneth W 	int ret = -ENOMEM;
842a43a8c39SChen, Kenneth W 
843a43a8c39SChen, Kenneth W 	spin_lock(&hugetlb_lock);
844a43a8c39SChen, Kenneth W 	if ((delta + resv_huge_pages) <= free_huge_pages) {
845a43a8c39SChen, Kenneth W 		resv_huge_pages += delta;
846a43a8c39SChen, Kenneth W 		ret = 0;
847a43a8c39SChen, Kenneth W 	}
848a43a8c39SChen, Kenneth W 	spin_unlock(&hugetlb_lock);
849a43a8c39SChen, Kenneth W 	return ret;
850a43a8c39SChen, Kenneth W }
851a43a8c39SChen, Kenneth W 
852a43a8c39SChen, Kenneth W int hugetlb_reserve_pages(struct inode *inode, long from, long to)
853a43a8c39SChen, Kenneth W {
854a43a8c39SChen, Kenneth W 	long ret, chg;
855a43a8c39SChen, Kenneth W 
856a43a8c39SChen, Kenneth W 	chg = region_chg(&inode->i_mapping->private_list, from, to);
857a43a8c39SChen, Kenneth W 	if (chg < 0)
858a43a8c39SChen, Kenneth W 		return chg;
8598a630112SKen Chen 	/*
8608a630112SKen Chen 	 * When cpuset is configured, it breaks the strict hugetlb page
8618a630112SKen Chen 	 * reservation as the accounting is done on a global variable. Such
8628a630112SKen Chen 	 * reservation is completely rubbish in the presence of cpuset because
8638a630112SKen Chen 	 * the reservation is not checked against page availability for the
8648a630112SKen Chen 	 * current cpuset. Application can still potentially OOM'ed by kernel
8658a630112SKen Chen 	 * with lack of free htlb page in cpuset that the task is in.
8668a630112SKen Chen 	 * Attempt to enforce strict accounting with cpuset is almost
8678a630112SKen Chen 	 * impossible (or too ugly) because cpuset is too fluid that
8688a630112SKen Chen 	 * task or memory node can be dynamically moved between cpusets.
8698a630112SKen Chen 	 *
8708a630112SKen Chen 	 * The change of semantics for shared hugetlb mapping with cpuset is
8718a630112SKen Chen 	 * undesirable. However, in order to preserve some of the semantics,
8728a630112SKen Chen 	 * we fall back to check against current free page availability as
8738a630112SKen Chen 	 * a best attempt and hopefully to minimize the impact of changing
8748a630112SKen Chen 	 * semantics that cpuset has.
8758a630112SKen Chen 	 */
8768a630112SKen Chen 	if (chg > cpuset_mems_nr(free_huge_pages_node))
8778a630112SKen Chen 		return -ENOMEM;
8788a630112SKen Chen 
879a43a8c39SChen, Kenneth W 	ret = hugetlb_acct_memory(chg);
880a43a8c39SChen, Kenneth W 	if (ret < 0)
881a43a8c39SChen, Kenneth W 		return ret;
882a43a8c39SChen, Kenneth W 	region_add(&inode->i_mapping->private_list, from, to);
883a43a8c39SChen, Kenneth W 	return 0;
884a43a8c39SChen, Kenneth W }
885a43a8c39SChen, Kenneth W 
886a43a8c39SChen, Kenneth W void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
887a43a8c39SChen, Kenneth W {
888a43a8c39SChen, Kenneth W 	long chg = region_truncate(&inode->i_mapping->private_list, offset);
889a43a8c39SChen, Kenneth W 	hugetlb_acct_memory(freed - chg);
890a43a8c39SChen, Kenneth W }
891