xref: /linux/mm/memory.c (revision 9532fec118d485ea37ab6e3ea372d68cd8b4cd0d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/memory.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * demand-loading started 01.12.91 - seems it is high on the list of
91da177e4SLinus Torvalds  * things wanted, and it should be easy to implement. - Linus
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
141da177e4SLinus Torvalds  * pages started 02.12.91, seems to work. - Linus.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
171da177e4SLinus Torvalds  * would have taken more than the 6M I have free, but it worked well as
181da177e4SLinus Torvalds  * far as I could see.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /*
241da177e4SLinus Torvalds  * Real VM (paging to/from disk) started 18.12.91. Much more work and
251da177e4SLinus Torvalds  * thought has to go into this. Oh, well..
261da177e4SLinus Torvalds  * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
271da177e4SLinus Torvalds  *		Found it. Everything seems to work now.
281da177e4SLinus Torvalds  * 20.12.91  -  Ok, making the swap-device changeable like the root.
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds /*
321da177e4SLinus Torvalds  * 05.04.94  -  Multi-page memory management added for v1.1.
331da177e4SLinus Torvalds  * 		Idea by Alex Bligh (alex@cconcepts.co.uk)
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  * 16.07.99  -  Support of BIGMEM added by Gerhard Wichert, Siemens AG
361da177e4SLinus Torvalds  *		(Gerhard.Wichert@pdb.siemens.de)
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/kernel_stat.h>
421da177e4SLinus Torvalds #include <linux/mm.h>
431da177e4SLinus Torvalds #include <linux/hugetlb.h>
441da177e4SLinus Torvalds #include <linux/mman.h>
451da177e4SLinus Torvalds #include <linux/swap.h>
461da177e4SLinus Torvalds #include <linux/highmem.h>
471da177e4SLinus Torvalds #include <linux/pagemap.h>
489a840895SHugh Dickins #include <linux/ksm.h>
491da177e4SLinus Torvalds #include <linux/rmap.h>
50b95f1b31SPaul Gortmaker #include <linux/export.h>
510ff92245SShailabh Nagar #include <linux/delayacct.h>
521da177e4SLinus Torvalds #include <linux/init.h>
53edc79b2aSPeter Zijlstra #include <linux/writeback.h>
548a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
55cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
563dc14741SHugh Dickins #include <linux/kallsyms.h>
573dc14741SHugh Dickins #include <linux/swapops.h>
583dc14741SHugh Dickins #include <linux/elf.h>
595a0e3ad6STejun Heo #include <linux/gfp.h>
604daae3b4SMel Gorman #include <linux/migrate.h>
611da177e4SLinus Torvalds 
626952b61dSAlexey Dobriyan #include <asm/io.h>
631da177e4SLinus Torvalds #include <asm/pgalloc.h>
641da177e4SLinus Torvalds #include <asm/uaccess.h>
651da177e4SLinus Torvalds #include <asm/tlb.h>
661da177e4SLinus Torvalds #include <asm/tlbflush.h>
671da177e4SLinus Torvalds #include <asm/pgtable.h>
681da177e4SLinus Torvalds 
6942b77728SJan Beulich #include "internal.h"
7042b77728SJan Beulich 
71d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES
721da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */
731da177e4SLinus Torvalds unsigned long max_mapnr;
741da177e4SLinus Torvalds struct page *mem_map;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr);
771da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
781da177e4SLinus Torvalds #endif
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds unsigned long num_physpages;
811da177e4SLinus Torvalds /*
821da177e4SLinus Torvalds  * A number of key systems in x86 including ioremap() rely on the assumption
831da177e4SLinus Torvalds  * that high_memory defines the upper bound on direct map memory, then end
841da177e4SLinus Torvalds  * of ZONE_NORMAL.  Under CONFIG_DISCONTIG this means that max_low_pfn and
851da177e4SLinus Torvalds  * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
861da177e4SLinus Torvalds  * and ZONE_HIGHMEM.
871da177e4SLinus Torvalds  */
881da177e4SLinus Torvalds void * high_memory;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds EXPORT_SYMBOL(num_physpages);
911da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory);
921da177e4SLinus Torvalds 
9332a93233SIngo Molnar /*
9432a93233SIngo Molnar  * Randomize the address space (stacks, mmaps, brk, etc.).
9532a93233SIngo Molnar  *
9632a93233SIngo Molnar  * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
9732a93233SIngo Molnar  *   as ancient (libc5 based) binaries can segfault. )
9832a93233SIngo Molnar  */
9932a93233SIngo Molnar int randomize_va_space __read_mostly =
10032a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK
10132a93233SIngo Molnar 					1;
10232a93233SIngo Molnar #else
10332a93233SIngo Molnar 					2;
10432a93233SIngo Molnar #endif
105a62eaf15SAndi Kleen 
106a62eaf15SAndi Kleen static int __init disable_randmaps(char *s)
107a62eaf15SAndi Kleen {
108a62eaf15SAndi Kleen 	randomize_va_space = 0;
1099b41046cSOGAWA Hirofumi 	return 1;
110a62eaf15SAndi Kleen }
111a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps);
112a62eaf15SAndi Kleen 
11362eede62SHugh Dickins unsigned long zero_pfn __read_mostly;
11403f6462aSHugh Dickins unsigned long highest_memmap_pfn __read_mostly;
115a13ea5b7SHugh Dickins 
116a13ea5b7SHugh Dickins /*
117a13ea5b7SHugh Dickins  * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
118a13ea5b7SHugh Dickins  */
119a13ea5b7SHugh Dickins static int __init init_zero_pfn(void)
120a13ea5b7SHugh Dickins {
121a13ea5b7SHugh Dickins 	zero_pfn = page_to_pfn(ZERO_PAGE(0));
122a13ea5b7SHugh Dickins 	return 0;
123a13ea5b7SHugh Dickins }
124a13ea5b7SHugh Dickins core_initcall(init_zero_pfn);
125a62eaf15SAndi Kleen 
126d559db08SKAMEZAWA Hiroyuki 
12734e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
12834e55232SKAMEZAWA Hiroyuki 
129ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm)
13034e55232SKAMEZAWA Hiroyuki {
13134e55232SKAMEZAWA Hiroyuki 	int i;
13234e55232SKAMEZAWA Hiroyuki 
13334e55232SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++) {
13405af2e10SDavid Rientjes 		if (current->rss_stat.count[i]) {
13505af2e10SDavid Rientjes 			add_mm_counter(mm, i, current->rss_stat.count[i]);
13605af2e10SDavid Rientjes 			current->rss_stat.count[i] = 0;
13734e55232SKAMEZAWA Hiroyuki 		}
13834e55232SKAMEZAWA Hiroyuki 	}
13905af2e10SDavid Rientjes 	current->rss_stat.events = 0;
14034e55232SKAMEZAWA Hiroyuki }
14134e55232SKAMEZAWA Hiroyuki 
14234e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
14334e55232SKAMEZAWA Hiroyuki {
14434e55232SKAMEZAWA Hiroyuki 	struct task_struct *task = current;
14534e55232SKAMEZAWA Hiroyuki 
14634e55232SKAMEZAWA Hiroyuki 	if (likely(task->mm == mm))
14734e55232SKAMEZAWA Hiroyuki 		task->rss_stat.count[member] += val;
14834e55232SKAMEZAWA Hiroyuki 	else
14934e55232SKAMEZAWA Hiroyuki 		add_mm_counter(mm, member, val);
15034e55232SKAMEZAWA Hiroyuki }
15134e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
15234e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
15334e55232SKAMEZAWA Hiroyuki 
15434e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */
15534e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH	(64)
15634e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
15734e55232SKAMEZAWA Hiroyuki {
15834e55232SKAMEZAWA Hiroyuki 	if (unlikely(task != current))
15934e55232SKAMEZAWA Hiroyuki 		return;
16034e55232SKAMEZAWA Hiroyuki 	if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
161ea48cf78SDavid Rientjes 		sync_mm_rss(task->mm);
16234e55232SKAMEZAWA Hiroyuki }
1639547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */
16434e55232SKAMEZAWA Hiroyuki 
16534e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
16634e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
16734e55232SKAMEZAWA Hiroyuki 
16834e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
16934e55232SKAMEZAWA Hiroyuki {
17034e55232SKAMEZAWA Hiroyuki }
17134e55232SKAMEZAWA Hiroyuki 
1729547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */
1739547d01bSPeter Zijlstra 
1749547d01bSPeter Zijlstra #ifdef HAVE_GENERIC_MMU_GATHER
1759547d01bSPeter Zijlstra 
1769547d01bSPeter Zijlstra static int tlb_next_batch(struct mmu_gather *tlb)
1779547d01bSPeter Zijlstra {
1789547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
1799547d01bSPeter Zijlstra 
1809547d01bSPeter Zijlstra 	batch = tlb->active;
1819547d01bSPeter Zijlstra 	if (batch->next) {
1829547d01bSPeter Zijlstra 		tlb->active = batch->next;
1839547d01bSPeter Zijlstra 		return 1;
1849547d01bSPeter Zijlstra 	}
1859547d01bSPeter Zijlstra 
1869547d01bSPeter Zijlstra 	batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
1879547d01bSPeter Zijlstra 	if (!batch)
1889547d01bSPeter Zijlstra 		return 0;
1899547d01bSPeter Zijlstra 
1909547d01bSPeter Zijlstra 	batch->next = NULL;
1919547d01bSPeter Zijlstra 	batch->nr   = 0;
1929547d01bSPeter Zijlstra 	batch->max  = MAX_GATHER_BATCH;
1939547d01bSPeter Zijlstra 
1949547d01bSPeter Zijlstra 	tlb->active->next = batch;
1959547d01bSPeter Zijlstra 	tlb->active = batch;
1969547d01bSPeter Zijlstra 
1979547d01bSPeter Zijlstra 	return 1;
1989547d01bSPeter Zijlstra }
1999547d01bSPeter Zijlstra 
2009547d01bSPeter Zijlstra /* tlb_gather_mmu
2019547d01bSPeter Zijlstra  *	Called to initialize an (on-stack) mmu_gather structure for page-table
2029547d01bSPeter Zijlstra  *	tear-down from @mm. The @fullmm argument is used when @mm is without
2039547d01bSPeter Zijlstra  *	users and we're going to destroy the full address space (exit/execve).
2049547d01bSPeter Zijlstra  */
2059547d01bSPeter Zijlstra void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm)
2069547d01bSPeter Zijlstra {
2079547d01bSPeter Zijlstra 	tlb->mm = mm;
2089547d01bSPeter Zijlstra 
2099547d01bSPeter Zijlstra 	tlb->fullmm     = fullmm;
210597e1c35SAlex Shi 	tlb->start	= -1UL;
211597e1c35SAlex Shi 	tlb->end	= 0;
2129547d01bSPeter Zijlstra 	tlb->need_flush = 0;
2139547d01bSPeter Zijlstra 	tlb->fast_mode  = (num_possible_cpus() == 1);
2149547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2159547d01bSPeter Zijlstra 	tlb->local.nr   = 0;
2169547d01bSPeter Zijlstra 	tlb->local.max  = ARRAY_SIZE(tlb->__pages);
2179547d01bSPeter Zijlstra 	tlb->active     = &tlb->local;
2189547d01bSPeter Zijlstra 
2199547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2209547d01bSPeter Zijlstra 	tlb->batch = NULL;
22134e55232SKAMEZAWA Hiroyuki #endif
2229547d01bSPeter Zijlstra }
2239547d01bSPeter Zijlstra 
2249547d01bSPeter Zijlstra void tlb_flush_mmu(struct mmu_gather *tlb)
2259547d01bSPeter Zijlstra {
2269547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
2279547d01bSPeter Zijlstra 
2289547d01bSPeter Zijlstra 	if (!tlb->need_flush)
2299547d01bSPeter Zijlstra 		return;
2309547d01bSPeter Zijlstra 	tlb->need_flush = 0;
2319547d01bSPeter Zijlstra 	tlb_flush(tlb);
2329547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2339547d01bSPeter Zijlstra 	tlb_table_flush(tlb);
2349547d01bSPeter Zijlstra #endif
2359547d01bSPeter Zijlstra 
2369547d01bSPeter Zijlstra 	if (tlb_fast_mode(tlb))
2379547d01bSPeter Zijlstra 		return;
2389547d01bSPeter Zijlstra 
2399547d01bSPeter Zijlstra 	for (batch = &tlb->local; batch; batch = batch->next) {
2409547d01bSPeter Zijlstra 		free_pages_and_swap_cache(batch->pages, batch->nr);
2419547d01bSPeter Zijlstra 		batch->nr = 0;
2429547d01bSPeter Zijlstra 	}
2439547d01bSPeter Zijlstra 	tlb->active = &tlb->local;
2449547d01bSPeter Zijlstra }
2459547d01bSPeter Zijlstra 
2469547d01bSPeter Zijlstra /* tlb_finish_mmu
2479547d01bSPeter Zijlstra  *	Called at the end of the shootdown operation to free up any resources
2489547d01bSPeter Zijlstra  *	that were required.
2499547d01bSPeter Zijlstra  */
2509547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
2519547d01bSPeter Zijlstra {
2529547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch, *next;
2539547d01bSPeter Zijlstra 
254597e1c35SAlex Shi 	tlb->start = start;
255597e1c35SAlex Shi 	tlb->end   = end;
2569547d01bSPeter Zijlstra 	tlb_flush_mmu(tlb);
2579547d01bSPeter Zijlstra 
2589547d01bSPeter Zijlstra 	/* keep the page table cache within bounds */
2599547d01bSPeter Zijlstra 	check_pgt_cache();
2609547d01bSPeter Zijlstra 
2619547d01bSPeter Zijlstra 	for (batch = tlb->local.next; batch; batch = next) {
2629547d01bSPeter Zijlstra 		next = batch->next;
2639547d01bSPeter Zijlstra 		free_pages((unsigned long)batch, 0);
2649547d01bSPeter Zijlstra 	}
2659547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2669547d01bSPeter Zijlstra }
2679547d01bSPeter Zijlstra 
2689547d01bSPeter Zijlstra /* __tlb_remove_page
2699547d01bSPeter Zijlstra  *	Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
2709547d01bSPeter Zijlstra  *	handling the additional races in SMP caused by other CPUs caching valid
2719547d01bSPeter Zijlstra  *	mappings in their TLBs. Returns the number of free page slots left.
2729547d01bSPeter Zijlstra  *	When out of page slots we must call tlb_flush_mmu().
2739547d01bSPeter Zijlstra  */
2749547d01bSPeter Zijlstra int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
2759547d01bSPeter Zijlstra {
2769547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
2779547d01bSPeter Zijlstra 
278f21760b1SShaohua Li 	VM_BUG_ON(!tlb->need_flush);
2799547d01bSPeter Zijlstra 
2809547d01bSPeter Zijlstra 	if (tlb_fast_mode(tlb)) {
2819547d01bSPeter Zijlstra 		free_page_and_swap_cache(page);
2829547d01bSPeter Zijlstra 		return 1; /* avoid calling tlb_flush_mmu() */
2839547d01bSPeter Zijlstra 	}
2849547d01bSPeter Zijlstra 
2859547d01bSPeter Zijlstra 	batch = tlb->active;
2869547d01bSPeter Zijlstra 	batch->pages[batch->nr++] = page;
2879547d01bSPeter Zijlstra 	if (batch->nr == batch->max) {
2889547d01bSPeter Zijlstra 		if (!tlb_next_batch(tlb))
2899547d01bSPeter Zijlstra 			return 0;
2900b43c3aaSShaohua Li 		batch = tlb->active;
2919547d01bSPeter Zijlstra 	}
2929547d01bSPeter Zijlstra 	VM_BUG_ON(batch->nr > batch->max);
2939547d01bSPeter Zijlstra 
2949547d01bSPeter Zijlstra 	return batch->max - batch->nr;
2959547d01bSPeter Zijlstra }
2969547d01bSPeter Zijlstra 
2979547d01bSPeter Zijlstra #endif /* HAVE_GENERIC_MMU_GATHER */
29834e55232SKAMEZAWA Hiroyuki 
29926723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
30026723911SPeter Zijlstra 
30126723911SPeter Zijlstra /*
30226723911SPeter Zijlstra  * See the comment near struct mmu_table_batch.
30326723911SPeter Zijlstra  */
30426723911SPeter Zijlstra 
30526723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg)
30626723911SPeter Zijlstra {
30726723911SPeter Zijlstra 	/* Simply deliver the interrupt */
30826723911SPeter Zijlstra }
30926723911SPeter Zijlstra 
31026723911SPeter Zijlstra static void tlb_remove_table_one(void *table)
31126723911SPeter Zijlstra {
31226723911SPeter Zijlstra 	/*
31326723911SPeter Zijlstra 	 * This isn't an RCU grace period and hence the page-tables cannot be
31426723911SPeter Zijlstra 	 * assumed to be actually RCU-freed.
31526723911SPeter Zijlstra 	 *
31626723911SPeter Zijlstra 	 * It is however sufficient for software page-table walkers that rely on
31726723911SPeter Zijlstra 	 * IRQ disabling. See the comment near struct mmu_table_batch.
31826723911SPeter Zijlstra 	 */
31926723911SPeter Zijlstra 	smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
32026723911SPeter Zijlstra 	__tlb_remove_table(table);
32126723911SPeter Zijlstra }
32226723911SPeter Zijlstra 
32326723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head)
32426723911SPeter Zijlstra {
32526723911SPeter Zijlstra 	struct mmu_table_batch *batch;
32626723911SPeter Zijlstra 	int i;
32726723911SPeter Zijlstra 
32826723911SPeter Zijlstra 	batch = container_of(head, struct mmu_table_batch, rcu);
32926723911SPeter Zijlstra 
33026723911SPeter Zijlstra 	for (i = 0; i < batch->nr; i++)
33126723911SPeter Zijlstra 		__tlb_remove_table(batch->tables[i]);
33226723911SPeter Zijlstra 
33326723911SPeter Zijlstra 	free_page((unsigned long)batch);
33426723911SPeter Zijlstra }
33526723911SPeter Zijlstra 
33626723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb)
33726723911SPeter Zijlstra {
33826723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
33926723911SPeter Zijlstra 
34026723911SPeter Zijlstra 	if (*batch) {
34126723911SPeter Zijlstra 		call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
34226723911SPeter Zijlstra 		*batch = NULL;
34326723911SPeter Zijlstra 	}
34426723911SPeter Zijlstra }
34526723911SPeter Zijlstra 
34626723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table)
34726723911SPeter Zijlstra {
34826723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
34926723911SPeter Zijlstra 
35026723911SPeter Zijlstra 	tlb->need_flush = 1;
35126723911SPeter Zijlstra 
35226723911SPeter Zijlstra 	/*
35326723911SPeter Zijlstra 	 * When there's less then two users of this mm there cannot be a
35426723911SPeter Zijlstra 	 * concurrent page-table walk.
35526723911SPeter Zijlstra 	 */
35626723911SPeter Zijlstra 	if (atomic_read(&tlb->mm->mm_users) < 2) {
35726723911SPeter Zijlstra 		__tlb_remove_table(table);
35826723911SPeter Zijlstra 		return;
35926723911SPeter Zijlstra 	}
36026723911SPeter Zijlstra 
36126723911SPeter Zijlstra 	if (*batch == NULL) {
36226723911SPeter Zijlstra 		*batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
36326723911SPeter Zijlstra 		if (*batch == NULL) {
36426723911SPeter Zijlstra 			tlb_remove_table_one(table);
36526723911SPeter Zijlstra 			return;
36626723911SPeter Zijlstra 		}
36726723911SPeter Zijlstra 		(*batch)->nr = 0;
36826723911SPeter Zijlstra 	}
36926723911SPeter Zijlstra 	(*batch)->tables[(*batch)->nr++] = table;
37026723911SPeter Zijlstra 	if ((*batch)->nr == MAX_TABLE_BATCH)
37126723911SPeter Zijlstra 		tlb_table_flush(tlb);
37226723911SPeter Zijlstra }
37326723911SPeter Zijlstra 
3749547d01bSPeter Zijlstra #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
37526723911SPeter Zijlstra 
3761da177e4SLinus Torvalds /*
3771da177e4SLinus Torvalds  * If a p?d_bad entry is found while walking page tables, report
3781da177e4SLinus Torvalds  * the error, before resetting entry to p?d_none.  Usually (but
3791da177e4SLinus Torvalds  * very seldom) called out from the p?d_none_or_clear_bad macros.
3801da177e4SLinus Torvalds  */
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	pgd_ERROR(*pgd);
3851da177e4SLinus Torvalds 	pgd_clear(pgd);
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	pud_ERROR(*pud);
3911da177e4SLinus Torvalds 	pud_clear(pud);
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd)
3951da177e4SLinus Torvalds {
3961da177e4SLinus Torvalds 	pmd_ERROR(*pmd);
3971da177e4SLinus Torvalds 	pmd_clear(pmd);
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /*
4011da177e4SLinus Torvalds  * Note: this doesn't free the actual pages themselves. That
4021da177e4SLinus Torvalds  * has been handled earlier when unmapping all the memory regions.
4031da177e4SLinus Torvalds  */
4049e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
4059e1b32caSBenjamin Herrenschmidt 			   unsigned long addr)
4061da177e4SLinus Torvalds {
4072f569afdSMartin Schwidefsky 	pgtable_t token = pmd_pgtable(*pmd);
4081da177e4SLinus Torvalds 	pmd_clear(pmd);
4099e1b32caSBenjamin Herrenschmidt 	pte_free_tlb(tlb, token, addr);
4101da177e4SLinus Torvalds 	tlb->mm->nr_ptes--;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
413e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
414e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
415e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	pmd_t *pmd;
4181da177e4SLinus Torvalds 	unsigned long next;
419e0da382cSHugh Dickins 	unsigned long start;
4201da177e4SLinus Torvalds 
421e0da382cSHugh Dickins 	start = addr;
4221da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
4231da177e4SLinus Torvalds 	do {
4241da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
4251da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
4261da177e4SLinus Torvalds 			continue;
4279e1b32caSBenjamin Herrenschmidt 		free_pte_range(tlb, pmd, addr);
4281da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
4291da177e4SLinus Torvalds 
430e0da382cSHugh Dickins 	start &= PUD_MASK;
431e0da382cSHugh Dickins 	if (start < floor)
432e0da382cSHugh Dickins 		return;
433e0da382cSHugh Dickins 	if (ceiling) {
434e0da382cSHugh Dickins 		ceiling &= PUD_MASK;
435e0da382cSHugh Dickins 		if (!ceiling)
436e0da382cSHugh Dickins 			return;
4371da177e4SLinus Torvalds 	}
438e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
439e0da382cSHugh Dickins 		return;
440e0da382cSHugh Dickins 
441e0da382cSHugh Dickins 	pmd = pmd_offset(pud, start);
442e0da382cSHugh Dickins 	pud_clear(pud);
4439e1b32caSBenjamin Herrenschmidt 	pmd_free_tlb(tlb, pmd, start);
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
446e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
447e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
448e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	pud_t *pud;
4511da177e4SLinus Torvalds 	unsigned long next;
452e0da382cSHugh Dickins 	unsigned long start;
4531da177e4SLinus Torvalds 
454e0da382cSHugh Dickins 	start = addr;
4551da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
4561da177e4SLinus Torvalds 	do {
4571da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
4581da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
4591da177e4SLinus Torvalds 			continue;
460e0da382cSHugh Dickins 		free_pmd_range(tlb, pud, addr, next, floor, ceiling);
4611da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
4621da177e4SLinus Torvalds 
463e0da382cSHugh Dickins 	start &= PGDIR_MASK;
464e0da382cSHugh Dickins 	if (start < floor)
465e0da382cSHugh Dickins 		return;
466e0da382cSHugh Dickins 	if (ceiling) {
467e0da382cSHugh Dickins 		ceiling &= PGDIR_MASK;
468e0da382cSHugh Dickins 		if (!ceiling)
469e0da382cSHugh Dickins 			return;
4701da177e4SLinus Torvalds 	}
471e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
472e0da382cSHugh Dickins 		return;
473e0da382cSHugh Dickins 
474e0da382cSHugh Dickins 	pud = pud_offset(pgd, start);
475e0da382cSHugh Dickins 	pgd_clear(pgd);
4769e1b32caSBenjamin Herrenschmidt 	pud_free_tlb(tlb, pud, start);
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds /*
480e0da382cSHugh Dickins  * This function frees user-level page tables of a process.
481e0da382cSHugh Dickins  *
4821da177e4SLinus Torvalds  * Must be called with pagetable lock held.
4831da177e4SLinus Torvalds  */
48442b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb,
485e0da382cSHugh Dickins 			unsigned long addr, unsigned long end,
486e0da382cSHugh Dickins 			unsigned long floor, unsigned long ceiling)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	pgd_t *pgd;
4891da177e4SLinus Torvalds 	unsigned long next;
4901da177e4SLinus Torvalds 
491e0da382cSHugh Dickins 	/*
492e0da382cSHugh Dickins 	 * The next few lines have given us lots of grief...
493e0da382cSHugh Dickins 	 *
494e0da382cSHugh Dickins 	 * Why are we testing PMD* at this top level?  Because often
495e0da382cSHugh Dickins 	 * there will be no work to do at all, and we'd prefer not to
496e0da382cSHugh Dickins 	 * go all the way down to the bottom just to discover that.
497e0da382cSHugh Dickins 	 *
498e0da382cSHugh Dickins 	 * Why all these "- 1"s?  Because 0 represents both the bottom
499e0da382cSHugh Dickins 	 * of the address space and the top of it (using -1 for the
500e0da382cSHugh Dickins 	 * top wouldn't help much: the masks would do the wrong thing).
501e0da382cSHugh Dickins 	 * The rule is that addr 0 and floor 0 refer to the bottom of
502e0da382cSHugh Dickins 	 * the address space, but end 0 and ceiling 0 refer to the top
503e0da382cSHugh Dickins 	 * Comparisons need to use "end - 1" and "ceiling - 1" (though
504e0da382cSHugh Dickins 	 * that end 0 case should be mythical).
505e0da382cSHugh Dickins 	 *
506e0da382cSHugh Dickins 	 * Wherever addr is brought up or ceiling brought down, we must
507e0da382cSHugh Dickins 	 * be careful to reject "the opposite 0" before it confuses the
508e0da382cSHugh Dickins 	 * subsequent tests.  But what about where end is brought down
509e0da382cSHugh Dickins 	 * by PMD_SIZE below? no, end can't go down to 0 there.
510e0da382cSHugh Dickins 	 *
511e0da382cSHugh Dickins 	 * Whereas we round start (addr) and ceiling down, by different
512e0da382cSHugh Dickins 	 * masks at different levels, in order to test whether a table
513e0da382cSHugh Dickins 	 * now has no other vmas using it, so can be freed, we don't
514e0da382cSHugh Dickins 	 * bother to round floor or end up - the tests don't need that.
515e0da382cSHugh Dickins 	 */
516e0da382cSHugh Dickins 
517e0da382cSHugh Dickins 	addr &= PMD_MASK;
518e0da382cSHugh Dickins 	if (addr < floor) {
519e0da382cSHugh Dickins 		addr += PMD_SIZE;
520e0da382cSHugh Dickins 		if (!addr)
521e0da382cSHugh Dickins 			return;
522e0da382cSHugh Dickins 	}
523e0da382cSHugh Dickins 	if (ceiling) {
524e0da382cSHugh Dickins 		ceiling &= PMD_MASK;
525e0da382cSHugh Dickins 		if (!ceiling)
526e0da382cSHugh Dickins 			return;
527e0da382cSHugh Dickins 	}
528e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
529e0da382cSHugh Dickins 		end -= PMD_SIZE;
530e0da382cSHugh Dickins 	if (addr > end - 1)
531e0da382cSHugh Dickins 		return;
532e0da382cSHugh Dickins 
53342b77728SJan Beulich 	pgd = pgd_offset(tlb->mm, addr);
5341da177e4SLinus Torvalds 	do {
5351da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
5361da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
5371da177e4SLinus Torvalds 			continue;
53842b77728SJan Beulich 		free_pud_range(tlb, pgd, addr, next, floor, ceiling);
5391da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
540e0da382cSHugh Dickins }
541e0da382cSHugh Dickins 
54242b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
543e0da382cSHugh Dickins 		unsigned long floor, unsigned long ceiling)
544e0da382cSHugh Dickins {
545e0da382cSHugh Dickins 	while (vma) {
546e0da382cSHugh Dickins 		struct vm_area_struct *next = vma->vm_next;
547e0da382cSHugh Dickins 		unsigned long addr = vma->vm_start;
548e0da382cSHugh Dickins 
5498f4f8c16SHugh Dickins 		/*
55025d9e2d1Snpiggin@suse.de 		 * Hide vma from rmap and truncate_pagecache before freeing
55125d9e2d1Snpiggin@suse.de 		 * pgtables
5528f4f8c16SHugh Dickins 		 */
5535beb4930SRik van Riel 		unlink_anon_vmas(vma);
5548f4f8c16SHugh Dickins 		unlink_file_vma(vma);
5558f4f8c16SHugh Dickins 
5569da61aefSDavid Gibson 		if (is_vm_hugetlb_page(vma)) {
5573bf5ee95SHugh Dickins 			hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
5583bf5ee95SHugh Dickins 				floor, next? next->vm_start: ceiling);
5593bf5ee95SHugh Dickins 		} else {
5603bf5ee95SHugh Dickins 			/*
5613bf5ee95SHugh Dickins 			 * Optimization: gather nearby vmas into one call down
5623bf5ee95SHugh Dickins 			 */
5633bf5ee95SHugh Dickins 			while (next && next->vm_start <= vma->vm_end + PMD_SIZE
5644866920bSDavid Gibson 			       && !is_vm_hugetlb_page(next)) {
565e0da382cSHugh Dickins 				vma = next;
566e0da382cSHugh Dickins 				next = vma->vm_next;
5675beb4930SRik van Riel 				unlink_anon_vmas(vma);
5688f4f8c16SHugh Dickins 				unlink_file_vma(vma);
569e0da382cSHugh Dickins 			}
5703bf5ee95SHugh Dickins 			free_pgd_range(tlb, addr, vma->vm_end,
571e0da382cSHugh Dickins 				floor, next? next->vm_start: ceiling);
5723bf5ee95SHugh Dickins 		}
573e0da382cSHugh Dickins 		vma = next;
574e0da382cSHugh Dickins 	}
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
5778ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
5788ac1f832SAndrea Arcangeli 		pmd_t *pmd, unsigned long address)
5791da177e4SLinus Torvalds {
5802f569afdSMartin Schwidefsky 	pgtable_t new = pte_alloc_one(mm, address);
5818ac1f832SAndrea Arcangeli 	int wait_split_huge_page;
5821da177e4SLinus Torvalds 	if (!new)
5831bb3630eSHugh Dickins 		return -ENOMEM;
5841bb3630eSHugh Dickins 
585362a61adSNick Piggin 	/*
586362a61adSNick Piggin 	 * Ensure all pte setup (eg. pte page lock and page clearing) are
587362a61adSNick Piggin 	 * visible before the pte is made visible to other CPUs by being
588362a61adSNick Piggin 	 * put into page tables.
589362a61adSNick Piggin 	 *
590362a61adSNick Piggin 	 * The other side of the story is the pointer chasing in the page
591362a61adSNick Piggin 	 * table walking code (when walking the page table without locking;
592362a61adSNick Piggin 	 * ie. most of the time). Fortunately, these data accesses consist
593362a61adSNick Piggin 	 * of a chain of data-dependent loads, meaning most CPUs (alpha
594362a61adSNick Piggin 	 * being the notable exception) will already guarantee loads are
595362a61adSNick Piggin 	 * seen in-order. See the alpha page table accessors for the
596362a61adSNick Piggin 	 * smp_read_barrier_depends() barriers in page table walking code.
597362a61adSNick Piggin 	 */
598362a61adSNick Piggin 	smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
599362a61adSNick Piggin 
600c74df32cSHugh Dickins 	spin_lock(&mm->page_table_lock);
6018ac1f832SAndrea Arcangeli 	wait_split_huge_page = 0;
6028ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
6031da177e4SLinus Torvalds 		mm->nr_ptes++;
6041da177e4SLinus Torvalds 		pmd_populate(mm, pmd, new);
6052f569afdSMartin Schwidefsky 		new = NULL;
6068ac1f832SAndrea Arcangeli 	} else if (unlikely(pmd_trans_splitting(*pmd)))
6078ac1f832SAndrea Arcangeli 		wait_split_huge_page = 1;
608c74df32cSHugh Dickins 	spin_unlock(&mm->page_table_lock);
6092f569afdSMartin Schwidefsky 	if (new)
6102f569afdSMartin Schwidefsky 		pte_free(mm, new);
6118ac1f832SAndrea Arcangeli 	if (wait_split_huge_page)
6128ac1f832SAndrea Arcangeli 		wait_split_huge_page(vma->anon_vma, pmd);
6131bb3630eSHugh Dickins 	return 0;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6161bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
6171da177e4SLinus Torvalds {
6181bb3630eSHugh Dickins 	pte_t *new = pte_alloc_one_kernel(&init_mm, address);
6191da177e4SLinus Torvalds 	if (!new)
6201bb3630eSHugh Dickins 		return -ENOMEM;
6211da177e4SLinus Torvalds 
622362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
623362a61adSNick Piggin 
624872fec16SHugh Dickins 	spin_lock(&init_mm.page_table_lock);
6258ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
626872fec16SHugh Dickins 		pmd_populate_kernel(&init_mm, pmd, new);
6272f569afdSMartin Schwidefsky 		new = NULL;
6288ac1f832SAndrea Arcangeli 	} else
6298ac1f832SAndrea Arcangeli 		VM_BUG_ON(pmd_trans_splitting(*pmd));
630872fec16SHugh Dickins 	spin_unlock(&init_mm.page_table_lock);
6312f569afdSMartin Schwidefsky 	if (new)
6322f569afdSMartin Schwidefsky 		pte_free_kernel(&init_mm, new);
6331bb3630eSHugh Dickins 	return 0;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
636d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss)
637ae859762SHugh Dickins {
638d559db08SKAMEZAWA Hiroyuki 	memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
639d559db08SKAMEZAWA Hiroyuki }
640d559db08SKAMEZAWA Hiroyuki 
641d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
642d559db08SKAMEZAWA Hiroyuki {
643d559db08SKAMEZAWA Hiroyuki 	int i;
644d559db08SKAMEZAWA Hiroyuki 
64534e55232SKAMEZAWA Hiroyuki 	if (current->mm == mm)
64605af2e10SDavid Rientjes 		sync_mm_rss(mm);
647d559db08SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++)
648d559db08SKAMEZAWA Hiroyuki 		if (rss[i])
649d559db08SKAMEZAWA Hiroyuki 			add_mm_counter(mm, i, rss[i]);
650ae859762SHugh Dickins }
651ae859762SHugh Dickins 
6521da177e4SLinus Torvalds /*
6536aab341eSLinus Torvalds  * This function is called to print an error when a bad pte
6546aab341eSLinus Torvalds  * is found. For example, we might have a PFN-mapped pte in
6556aab341eSLinus Torvalds  * a region that doesn't allow it.
656b5810039SNick Piggin  *
657b5810039SNick Piggin  * The calling function must still handle the error.
658b5810039SNick Piggin  */
6593dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
6603dc14741SHugh Dickins 			  pte_t pte, struct page *page)
661b5810039SNick Piggin {
6623dc14741SHugh Dickins 	pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
6633dc14741SHugh Dickins 	pud_t *pud = pud_offset(pgd, addr);
6643dc14741SHugh Dickins 	pmd_t *pmd = pmd_offset(pud, addr);
6653dc14741SHugh Dickins 	struct address_space *mapping;
6663dc14741SHugh Dickins 	pgoff_t index;
667d936cf9bSHugh Dickins 	static unsigned long resume;
668d936cf9bSHugh Dickins 	static unsigned long nr_shown;
669d936cf9bSHugh Dickins 	static unsigned long nr_unshown;
670d936cf9bSHugh Dickins 
671d936cf9bSHugh Dickins 	/*
672d936cf9bSHugh Dickins 	 * Allow a burst of 60 reports, then keep quiet for that minute;
673d936cf9bSHugh Dickins 	 * or allow a steady drip of one report per second.
674d936cf9bSHugh Dickins 	 */
675d936cf9bSHugh Dickins 	if (nr_shown == 60) {
676d936cf9bSHugh Dickins 		if (time_before(jiffies, resume)) {
677d936cf9bSHugh Dickins 			nr_unshown++;
678d936cf9bSHugh Dickins 			return;
679d936cf9bSHugh Dickins 		}
680d936cf9bSHugh Dickins 		if (nr_unshown) {
6811e9e6365SHugh Dickins 			printk(KERN_ALERT
6821e9e6365SHugh Dickins 				"BUG: Bad page map: %lu messages suppressed\n",
683d936cf9bSHugh Dickins 				nr_unshown);
684d936cf9bSHugh Dickins 			nr_unshown = 0;
685d936cf9bSHugh Dickins 		}
686d936cf9bSHugh Dickins 		nr_shown = 0;
687d936cf9bSHugh Dickins 	}
688d936cf9bSHugh Dickins 	if (nr_shown++ == 0)
689d936cf9bSHugh Dickins 		resume = jiffies + 60 * HZ;
6903dc14741SHugh Dickins 
6913dc14741SHugh Dickins 	mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
6923dc14741SHugh Dickins 	index = linear_page_index(vma, addr);
6933dc14741SHugh Dickins 
6941e9e6365SHugh Dickins 	printk(KERN_ALERT
6951e9e6365SHugh Dickins 		"BUG: Bad page map in process %s  pte:%08llx pmd:%08llx\n",
6963dc14741SHugh Dickins 		current->comm,
6973dc14741SHugh Dickins 		(long long)pte_val(pte), (long long)pmd_val(*pmd));
698718a3821SWu Fengguang 	if (page)
699718a3821SWu Fengguang 		dump_page(page);
7001e9e6365SHugh Dickins 	printk(KERN_ALERT
7013dc14741SHugh Dickins 		"addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
7023dc14741SHugh Dickins 		(void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
7033dc14741SHugh Dickins 	/*
7043dc14741SHugh Dickins 	 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
7053dc14741SHugh Dickins 	 */
7063dc14741SHugh Dickins 	if (vma->vm_ops)
7071e9e6365SHugh Dickins 		print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n",
7083dc14741SHugh Dickins 				(unsigned long)vma->vm_ops->fault);
7093dc14741SHugh Dickins 	if (vma->vm_file && vma->vm_file->f_op)
7101e9e6365SHugh Dickins 		print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n",
7113dc14741SHugh Dickins 				(unsigned long)vma->vm_file->f_op->mmap);
712b5810039SNick Piggin 	dump_stack();
7133dc14741SHugh Dickins 	add_taint(TAINT_BAD_PAGE);
714b5810039SNick Piggin }
715b5810039SNick Piggin 
7162ec74c3eSSagi Grimberg static inline bool is_cow_mapping(vm_flags_t flags)
71767121172SLinus Torvalds {
71867121172SLinus Torvalds 	return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
71967121172SLinus Torvalds }
72067121172SLinus Torvalds 
72162eede62SHugh Dickins #ifndef is_zero_pfn
72262eede62SHugh Dickins static inline int is_zero_pfn(unsigned long pfn)
72362eede62SHugh Dickins {
72462eede62SHugh Dickins 	return pfn == zero_pfn;
72562eede62SHugh Dickins }
72662eede62SHugh Dickins #endif
72762eede62SHugh Dickins 
72862eede62SHugh Dickins #ifndef my_zero_pfn
72962eede62SHugh Dickins static inline unsigned long my_zero_pfn(unsigned long addr)
73062eede62SHugh Dickins {
73162eede62SHugh Dickins 	return zero_pfn;
73262eede62SHugh Dickins }
73362eede62SHugh Dickins #endif
73462eede62SHugh Dickins 
735b5810039SNick Piggin /*
7367e675137SNick Piggin  * vm_normal_page -- This function gets the "struct page" associated with a pte.
7376aab341eSLinus Torvalds  *
7387e675137SNick Piggin  * "Special" mappings do not wish to be associated with a "struct page" (either
7397e675137SNick Piggin  * it doesn't exist, or it exists but they don't want to touch it). In this
7407e675137SNick Piggin  * case, NULL is returned here. "Normal" mappings do have a struct page.
741b379d790SJared Hulbert  *
7427e675137SNick Piggin  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
7437e675137SNick Piggin  * pte bit, in which case this function is trivial. Secondly, an architecture
7447e675137SNick Piggin  * may not have a spare pte bit, which requires a more complicated scheme,
7457e675137SNick Piggin  * described below.
7467e675137SNick Piggin  *
7477e675137SNick Piggin  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
7487e675137SNick Piggin  * special mapping (even if there are underlying and valid "struct pages").
7497e675137SNick Piggin  * COWed pages of a VM_PFNMAP are always normal.
7506aab341eSLinus Torvalds  *
751b379d790SJared Hulbert  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
752b379d790SJared Hulbert  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
7537e675137SNick Piggin  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
7547e675137SNick Piggin  * mapping will always honor the rule
7556aab341eSLinus Torvalds  *
7566aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
7576aab341eSLinus Torvalds  *
7587e675137SNick Piggin  * And for normal mappings this is false.
759b379d790SJared Hulbert  *
7607e675137SNick Piggin  * This restricts such mappings to be a linear translation from virtual address
7617e675137SNick Piggin  * to pfn. To get around this restriction, we allow arbitrary mappings so long
7627e675137SNick Piggin  * as the vma is not a COW mapping; in that case, we know that all ptes are
7637e675137SNick Piggin  * special (because none can have been COWed).
764b379d790SJared Hulbert  *
765b379d790SJared Hulbert  *
7667e675137SNick Piggin  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
7677e675137SNick Piggin  *
768b379d790SJared Hulbert  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
769b379d790SJared Hulbert  * page" backing, however the difference is that _all_ pages with a struct
770b379d790SJared Hulbert  * page (that is, those where pfn_valid is true) are refcounted and considered
771b379d790SJared Hulbert  * normal pages by the VM. The disadvantage is that pages are refcounted
772b379d790SJared Hulbert  * (which can be slower and simply not an option for some PFNMAP users). The
773b379d790SJared Hulbert  * advantage is that we don't have to follow the strict linearity rule of
774b379d790SJared Hulbert  * PFNMAP mappings in order to support COWable mappings.
775b379d790SJared Hulbert  *
776ee498ed7SHugh Dickins  */
7777e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL
7787e675137SNick Piggin # define HAVE_PTE_SPECIAL 1
7797e675137SNick Piggin #else
7807e675137SNick Piggin # define HAVE_PTE_SPECIAL 0
7817e675137SNick Piggin #endif
7827e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
7837e675137SNick Piggin 				pte_t pte)
784ee498ed7SHugh Dickins {
78522b31eecSHugh Dickins 	unsigned long pfn = pte_pfn(pte);
7867e675137SNick Piggin 
7877e675137SNick Piggin 	if (HAVE_PTE_SPECIAL) {
78822b31eecSHugh Dickins 		if (likely(!pte_special(pte)))
78922b31eecSHugh Dickins 			goto check_pfn;
790a13ea5b7SHugh Dickins 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
791a13ea5b7SHugh Dickins 			return NULL;
79262eede62SHugh Dickins 		if (!is_zero_pfn(pfn))
79322b31eecSHugh Dickins 			print_bad_pte(vma, addr, pte, NULL);
7947e675137SNick Piggin 		return NULL;
7957e675137SNick Piggin 	}
7967e675137SNick Piggin 
7977e675137SNick Piggin 	/* !HAVE_PTE_SPECIAL case follows: */
7987e675137SNick Piggin 
799b379d790SJared Hulbert 	if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
800b379d790SJared Hulbert 		if (vma->vm_flags & VM_MIXEDMAP) {
801b379d790SJared Hulbert 			if (!pfn_valid(pfn))
802b379d790SJared Hulbert 				return NULL;
803b379d790SJared Hulbert 			goto out;
804b379d790SJared Hulbert 		} else {
8057e675137SNick Piggin 			unsigned long off;
8067e675137SNick Piggin 			off = (addr - vma->vm_start) >> PAGE_SHIFT;
8076aab341eSLinus Torvalds 			if (pfn == vma->vm_pgoff + off)
8086aab341eSLinus Torvalds 				return NULL;
80967121172SLinus Torvalds 			if (!is_cow_mapping(vma->vm_flags))
810fb155c16SLinus Torvalds 				return NULL;
8116aab341eSLinus Torvalds 		}
812b379d790SJared Hulbert 	}
8136aab341eSLinus Torvalds 
81462eede62SHugh Dickins 	if (is_zero_pfn(pfn))
81562eede62SHugh Dickins 		return NULL;
81622b31eecSHugh Dickins check_pfn:
81722b31eecSHugh Dickins 	if (unlikely(pfn > highest_memmap_pfn)) {
81822b31eecSHugh Dickins 		print_bad_pte(vma, addr, pte, NULL);
81922b31eecSHugh Dickins 		return NULL;
82022b31eecSHugh Dickins 	}
8216aab341eSLinus Torvalds 
8226aab341eSLinus Torvalds 	/*
8237e675137SNick Piggin 	 * NOTE! We still have PageReserved() pages in the page tables.
8247e675137SNick Piggin 	 * eg. VDSO mappings can cause them to exist.
8256aab341eSLinus Torvalds 	 */
826b379d790SJared Hulbert out:
8276aab341eSLinus Torvalds 	return pfn_to_page(pfn);
828ee498ed7SHugh Dickins }
829ee498ed7SHugh Dickins 
830ee498ed7SHugh Dickins /*
8311da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
8321da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
8331da177e4SLinus Torvalds  * covered by this vma.
8341da177e4SLinus Torvalds  */
8351da177e4SLinus Torvalds 
836570a335bSHugh Dickins static inline unsigned long
8371da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
838b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
8398c103762SHugh Dickins 		unsigned long addr, int *rss)
8401da177e4SLinus Torvalds {
841b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
8421da177e4SLinus Torvalds 	pte_t pte = *src_pte;
8431da177e4SLinus Torvalds 	struct page *page;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
8461da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
8471da177e4SLinus Torvalds 		if (!pte_file(pte)) {
8480697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(pte);
8490697212aSChristoph Lameter 
850570a335bSHugh Dickins 			if (swap_duplicate(entry) < 0)
851570a335bSHugh Dickins 				return entry.val;
852570a335bSHugh Dickins 
8531da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
8541da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
8551da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
856f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
857f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
858f412ac08SHugh Dickins 						 &src_mm->mmlist);
8591da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
8601da177e4SLinus Torvalds 			}
861b084d435SKAMEZAWA Hiroyuki 			if (likely(!non_swap_entry(entry)))
862b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]++;
8639f9f1acdSKonstantin Khlebnikov 			else if (is_migration_entry(entry)) {
8649f9f1acdSKonstantin Khlebnikov 				page = migration_entry_to_page(entry);
8659f9f1acdSKonstantin Khlebnikov 
8669f9f1acdSKonstantin Khlebnikov 				if (PageAnon(page))
8679f9f1acdSKonstantin Khlebnikov 					rss[MM_ANONPAGES]++;
8689f9f1acdSKonstantin Khlebnikov 				else
8699f9f1acdSKonstantin Khlebnikov 					rss[MM_FILEPAGES]++;
8709f9f1acdSKonstantin Khlebnikov 
8719f9f1acdSKonstantin Khlebnikov 				if (is_write_migration_entry(entry) &&
8720697212aSChristoph Lameter 				    is_cow_mapping(vm_flags)) {
8730697212aSChristoph Lameter 					/*
8749f9f1acdSKonstantin Khlebnikov 					 * COW mappings require pages in both
8759f9f1acdSKonstantin Khlebnikov 					 * parent and child to be set to read.
8760697212aSChristoph Lameter 					 */
8770697212aSChristoph Lameter 					make_migration_entry_read(&entry);
8780697212aSChristoph Lameter 					pte = swp_entry_to_pte(entry);
8790697212aSChristoph Lameter 					set_pte_at(src_mm, addr, src_pte, pte);
8800697212aSChristoph Lameter 				}
8811da177e4SLinus Torvalds 			}
8829f9f1acdSKonstantin Khlebnikov 		}
883ae859762SHugh Dickins 		goto out_set_pte;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	/*
8871da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
8881da177e4SLinus Torvalds 	 * in the parent and the child
8891da177e4SLinus Torvalds 	 */
89067121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
8911da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
8923dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	/*
8961da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
8971da177e4SLinus Torvalds 	 * the child
8981da177e4SLinus Torvalds 	 */
8991da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
9001da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
9011da177e4SLinus Torvalds 	pte = pte_mkold(pte);
9026aab341eSLinus Torvalds 
9036aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
9046aab341eSLinus Torvalds 	if (page) {
9051da177e4SLinus Torvalds 		get_page(page);
90621333b2bSHugh Dickins 		page_dup_rmap(page);
907d559db08SKAMEZAWA Hiroyuki 		if (PageAnon(page))
908d559db08SKAMEZAWA Hiroyuki 			rss[MM_ANONPAGES]++;
909d559db08SKAMEZAWA Hiroyuki 		else
910d559db08SKAMEZAWA Hiroyuki 			rss[MM_FILEPAGES]++;
9116aab341eSLinus Torvalds 	}
912ae859762SHugh Dickins 
913ae859762SHugh Dickins out_set_pte:
914ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
915570a335bSHugh Dickins 	return 0;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
91871e3aac0SAndrea Arcangeli int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9191da177e4SLinus Torvalds 		   pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
9201da177e4SLinus Torvalds 		   unsigned long addr, unsigned long end)
9211da177e4SLinus Torvalds {
922c36987e2SDaisuke Nishimura 	pte_t *orig_src_pte, *orig_dst_pte;
9231da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
924c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
925e040f218SHugh Dickins 	int progress = 0;
926d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
927570a335bSHugh Dickins 	swp_entry_t entry = (swp_entry_t){0};
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds again:
930d559db08SKAMEZAWA Hiroyuki 	init_rss_vec(rss);
931d559db08SKAMEZAWA Hiroyuki 
932c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
9331da177e4SLinus Torvalds 	if (!dst_pte)
9341da177e4SLinus Torvalds 		return -ENOMEM;
935ece0e2b6SPeter Zijlstra 	src_pte = pte_offset_map(src_pmd, addr);
9364c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
937f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
938c36987e2SDaisuke Nishimura 	orig_src_pte = src_pte;
939c36987e2SDaisuke Nishimura 	orig_dst_pte = dst_pte;
9406606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	do {
9431da177e4SLinus Torvalds 		/*
9441da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
9451da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
9461da177e4SLinus Torvalds 		 */
947e040f218SHugh Dickins 		if (progress >= 32) {
948e040f218SHugh Dickins 			progress = 0;
949e040f218SHugh Dickins 			if (need_resched() ||
95095c354feSNick Piggin 			    spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
9511da177e4SLinus Torvalds 				break;
952e040f218SHugh Dickins 		}
9531da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
9541da177e4SLinus Torvalds 			progress++;
9551da177e4SLinus Torvalds 			continue;
9561da177e4SLinus Torvalds 		}
957570a335bSHugh Dickins 		entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
958570a335bSHugh Dickins 							vma, addr, rss);
959570a335bSHugh Dickins 		if (entry.val)
960570a335bSHugh Dickins 			break;
9611da177e4SLinus Torvalds 		progress += 8;
9621da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
9631da177e4SLinus Torvalds 
9646606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
965c74df32cSHugh Dickins 	spin_unlock(src_ptl);
966ece0e2b6SPeter Zijlstra 	pte_unmap(orig_src_pte);
967d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(dst_mm, rss);
968c36987e2SDaisuke Nishimura 	pte_unmap_unlock(orig_dst_pte, dst_ptl);
969c74df32cSHugh Dickins 	cond_resched();
970570a335bSHugh Dickins 
971570a335bSHugh Dickins 	if (entry.val) {
972570a335bSHugh Dickins 		if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
973570a335bSHugh Dickins 			return -ENOMEM;
974570a335bSHugh Dickins 		progress = 0;
975570a335bSHugh Dickins 	}
9761da177e4SLinus Torvalds 	if (addr != end)
9771da177e4SLinus Torvalds 		goto again;
9781da177e4SLinus Torvalds 	return 0;
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9821da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
9831da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9841da177e4SLinus Torvalds {
9851da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
9861da177e4SLinus Torvalds 	unsigned long next;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
9891da177e4SLinus Torvalds 	if (!dst_pmd)
9901da177e4SLinus Torvalds 		return -ENOMEM;
9911da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
9921da177e4SLinus Torvalds 	do {
9931da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
99471e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*src_pmd)) {
99571e3aac0SAndrea Arcangeli 			int err;
99614d1a55cSAndrea Arcangeli 			VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
99771e3aac0SAndrea Arcangeli 			err = copy_huge_pmd(dst_mm, src_mm,
99871e3aac0SAndrea Arcangeli 					    dst_pmd, src_pmd, addr, vma);
99971e3aac0SAndrea Arcangeli 			if (err == -ENOMEM)
100071e3aac0SAndrea Arcangeli 				return -ENOMEM;
100171e3aac0SAndrea Arcangeli 			if (!err)
100271e3aac0SAndrea Arcangeli 				continue;
100371e3aac0SAndrea Arcangeli 			/* fall through */
100471e3aac0SAndrea Arcangeli 		}
10051da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
10061da177e4SLinus Torvalds 			continue;
10071da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
10081da177e4SLinus Torvalds 						vma, addr, next))
10091da177e4SLinus Torvalds 			return -ENOMEM;
10101da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
10111da177e4SLinus Torvalds 	return 0;
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10151da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
10161da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
10171da177e4SLinus Torvalds {
10181da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
10191da177e4SLinus Torvalds 	unsigned long next;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
10221da177e4SLinus Torvalds 	if (!dst_pud)
10231da177e4SLinus Torvalds 		return -ENOMEM;
10241da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
10251da177e4SLinus Torvalds 	do {
10261da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
10271da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
10281da177e4SLinus Torvalds 			continue;
10291da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
10301da177e4SLinus Torvalds 						vma, addr, next))
10311da177e4SLinus Torvalds 			return -ENOMEM;
10321da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
10331da177e4SLinus Torvalds 	return 0;
10341da177e4SLinus Torvalds }
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10371da177e4SLinus Torvalds 		struct vm_area_struct *vma)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
10401da177e4SLinus Torvalds 	unsigned long next;
10411da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
10421da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
10432ec74c3eSSagi Grimberg 	unsigned long mmun_start;	/* For mmu_notifiers */
10442ec74c3eSSagi Grimberg 	unsigned long mmun_end;		/* For mmu_notifiers */
10452ec74c3eSSagi Grimberg 	bool is_cow;
1046cddb8a5cSAndrea Arcangeli 	int ret;
10471da177e4SLinus Torvalds 
1048d992895bSNick Piggin 	/*
1049d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
1050d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
1051d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
1052d992895bSNick Piggin 	 * efficient than faulting.
1053d992895bSNick Piggin 	 */
10544b6e1e37SKonstantin Khlebnikov 	if (!(vma->vm_flags & (VM_HUGETLB | VM_NONLINEAR |
10554b6e1e37SKonstantin Khlebnikov 			       VM_PFNMAP | VM_MIXEDMAP))) {
1056d992895bSNick Piggin 		if (!vma->anon_vma)
1057d992895bSNick Piggin 			return 0;
1058d992895bSNick Piggin 	}
1059d992895bSNick Piggin 
10601da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
10611da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
10621da177e4SLinus Torvalds 
1063b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP)) {
10642ab64037Svenkatesh.pallipadi@intel.com 		/*
10652ab64037Svenkatesh.pallipadi@intel.com 		 * We do not free on error cases below as remove_vma
10662ab64037Svenkatesh.pallipadi@intel.com 		 * gets called on error from higher level routine
10672ab64037Svenkatesh.pallipadi@intel.com 		 */
10685180da41SSuresh Siddha 		ret = track_pfn_copy(vma);
10692ab64037Svenkatesh.pallipadi@intel.com 		if (ret)
10702ab64037Svenkatesh.pallipadi@intel.com 			return ret;
10712ab64037Svenkatesh.pallipadi@intel.com 	}
10722ab64037Svenkatesh.pallipadi@intel.com 
1073cddb8a5cSAndrea Arcangeli 	/*
1074cddb8a5cSAndrea Arcangeli 	 * We need to invalidate the secondary MMU mappings only when
1075cddb8a5cSAndrea Arcangeli 	 * there could be a permission downgrade on the ptes of the
1076cddb8a5cSAndrea Arcangeli 	 * parent mm. And a permission downgrade will only happen if
1077cddb8a5cSAndrea Arcangeli 	 * is_cow_mapping() returns true.
1078cddb8a5cSAndrea Arcangeli 	 */
10792ec74c3eSSagi Grimberg 	is_cow = is_cow_mapping(vma->vm_flags);
10802ec74c3eSSagi Grimberg 	mmun_start = addr;
10812ec74c3eSSagi Grimberg 	mmun_end   = end;
10822ec74c3eSSagi Grimberg 	if (is_cow)
10832ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_start(src_mm, mmun_start,
10842ec74c3eSSagi Grimberg 						    mmun_end);
1085cddb8a5cSAndrea Arcangeli 
1086cddb8a5cSAndrea Arcangeli 	ret = 0;
10871da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
10881da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
10891da177e4SLinus Torvalds 	do {
10901da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
10911da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
10921da177e4SLinus Torvalds 			continue;
1093cddb8a5cSAndrea Arcangeli 		if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
1094cddb8a5cSAndrea Arcangeli 					    vma, addr, next))) {
1095cddb8a5cSAndrea Arcangeli 			ret = -ENOMEM;
1096cddb8a5cSAndrea Arcangeli 			break;
1097cddb8a5cSAndrea Arcangeli 		}
10981da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
1099cddb8a5cSAndrea Arcangeli 
11002ec74c3eSSagi Grimberg 	if (is_cow)
11012ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
1102cddb8a5cSAndrea Arcangeli 	return ret;
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds 
110551c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
1106b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
11071da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
110897a89413SPeter Zijlstra 				struct zap_details *details)
11091da177e4SLinus Torvalds {
1110b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
1111d16dfc55SPeter Zijlstra 	int force_flush = 0;
1112d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
111397a89413SPeter Zijlstra 	spinlock_t *ptl;
11145f1a1907SSteven Rostedt 	pte_t *start_pte;
111597a89413SPeter Zijlstra 	pte_t *pte;
1116d559db08SKAMEZAWA Hiroyuki 
1117d16dfc55SPeter Zijlstra again:
1118e303297eSPeter Zijlstra 	init_rss_vec(rss);
11195f1a1907SSteven Rostedt 	start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
11205f1a1907SSteven Rostedt 	pte = start_pte;
11216606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
11221da177e4SLinus Torvalds 	do {
11231da177e4SLinus Torvalds 		pte_t ptent = *pte;
112451c6f666SRobin Holt 		if (pte_none(ptent)) {
11251da177e4SLinus Torvalds 			continue;
112651c6f666SRobin Holt 		}
112751c6f666SRobin Holt 
11286f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
11296f5e6b9eSHugh Dickins 			struct page *page;
11306f5e6b9eSHugh Dickins 
11316aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
11321da177e4SLinus Torvalds 			if (unlikely(details) && page) {
11331da177e4SLinus Torvalds 				/*
11341da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
11351da177e4SLinus Torvalds 				 * invalidate cache without truncating:
11361da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
11371da177e4SLinus Torvalds 				 */
11381da177e4SLinus Torvalds 				if (details->check_mapping &&
11391da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
11401da177e4SLinus Torvalds 					continue;
11411da177e4SLinus Torvalds 				/*
11421da177e4SLinus Torvalds 				 * Each page->index must be checked when
11431da177e4SLinus Torvalds 				 * invalidating or truncating nonlinear.
11441da177e4SLinus Torvalds 				 */
11451da177e4SLinus Torvalds 				if (details->nonlinear_vma &&
11461da177e4SLinus Torvalds 				    (page->index < details->first_index ||
11471da177e4SLinus Torvalds 				     page->index > details->last_index))
11481da177e4SLinus Torvalds 					continue;
11491da177e4SLinus Torvalds 			}
1150b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
1151a600388dSZachary Amsden 							tlb->fullmm);
11521da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
11531da177e4SLinus Torvalds 			if (unlikely(!page))
11541da177e4SLinus Torvalds 				continue;
11551da177e4SLinus Torvalds 			if (unlikely(details) && details->nonlinear_vma
11561da177e4SLinus Torvalds 			    && linear_page_index(details->nonlinear_vma,
11571da177e4SLinus Torvalds 						addr) != page->index)
1158b5810039SNick Piggin 				set_pte_at(mm, addr, pte,
11591da177e4SLinus Torvalds 					   pgoff_to_pte(page->index));
11601da177e4SLinus Torvalds 			if (PageAnon(page))
1161d559db08SKAMEZAWA Hiroyuki 				rss[MM_ANONPAGES]--;
11626237bcd9SHugh Dickins 			else {
11636237bcd9SHugh Dickins 				if (pte_dirty(ptent))
11646237bcd9SHugh Dickins 					set_page_dirty(page);
11654917e5d0SJohannes Weiner 				if (pte_young(ptent) &&
11664917e5d0SJohannes Weiner 				    likely(!VM_SequentialReadHint(vma)))
1167bf3f3bc5SNick Piggin 					mark_page_accessed(page);
1168d559db08SKAMEZAWA Hiroyuki 				rss[MM_FILEPAGES]--;
11696237bcd9SHugh Dickins 			}
1170edc315fdSHugh Dickins 			page_remove_rmap(page);
11713dc14741SHugh Dickins 			if (unlikely(page_mapcount(page) < 0))
11723dc14741SHugh Dickins 				print_bad_pte(vma, addr, ptent, page);
1173d16dfc55SPeter Zijlstra 			force_flush = !__tlb_remove_page(tlb, page);
1174d16dfc55SPeter Zijlstra 			if (force_flush)
1175d16dfc55SPeter Zijlstra 				break;
11761da177e4SLinus Torvalds 			continue;
11771da177e4SLinus Torvalds 		}
11781da177e4SLinus Torvalds 		/*
11791da177e4SLinus Torvalds 		 * If details->check_mapping, we leave swap entries;
11801da177e4SLinus Torvalds 		 * if details->nonlinear_vma, we leave file entries.
11811da177e4SLinus Torvalds 		 */
11821da177e4SLinus Torvalds 		if (unlikely(details))
11831da177e4SLinus Torvalds 			continue;
11842509ef26SHugh Dickins 		if (pte_file(ptent)) {
11852509ef26SHugh Dickins 			if (unlikely(!(vma->vm_flags & VM_NONLINEAR)))
11862509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
1187b084d435SKAMEZAWA Hiroyuki 		} else {
1188b084d435SKAMEZAWA Hiroyuki 			swp_entry_t entry = pte_to_swp_entry(ptent);
1189b084d435SKAMEZAWA Hiroyuki 
1190b084d435SKAMEZAWA Hiroyuki 			if (!non_swap_entry(entry))
1191b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]--;
11929f9f1acdSKonstantin Khlebnikov 			else if (is_migration_entry(entry)) {
11939f9f1acdSKonstantin Khlebnikov 				struct page *page;
11949f9f1acdSKonstantin Khlebnikov 
11959f9f1acdSKonstantin Khlebnikov 				page = migration_entry_to_page(entry);
11969f9f1acdSKonstantin Khlebnikov 
11979f9f1acdSKonstantin Khlebnikov 				if (PageAnon(page))
11989f9f1acdSKonstantin Khlebnikov 					rss[MM_ANONPAGES]--;
11999f9f1acdSKonstantin Khlebnikov 				else
12009f9f1acdSKonstantin Khlebnikov 					rss[MM_FILEPAGES]--;
12019f9f1acdSKonstantin Khlebnikov 			}
1202b084d435SKAMEZAWA Hiroyuki 			if (unlikely(!free_swap_and_cache(entry)))
12032509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
1204b084d435SKAMEZAWA Hiroyuki 		}
12059888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
120697a89413SPeter Zijlstra 	} while (pte++, addr += PAGE_SIZE, addr != end);
1207ae859762SHugh Dickins 
1208d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(mm, rss);
12096606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
12105f1a1907SSteven Rostedt 	pte_unmap_unlock(start_pte, ptl);
121151c6f666SRobin Holt 
1212d16dfc55SPeter Zijlstra 	/*
1213d16dfc55SPeter Zijlstra 	 * mmu_gather ran out of room to batch pages, we break out of
1214d16dfc55SPeter Zijlstra 	 * the PTE lock to avoid doing the potential expensive TLB invalidate
1215d16dfc55SPeter Zijlstra 	 * and page-free while holding it.
1216d16dfc55SPeter Zijlstra 	 */
1217d16dfc55SPeter Zijlstra 	if (force_flush) {
1218d16dfc55SPeter Zijlstra 		force_flush = 0;
1219597e1c35SAlex Shi 
1220597e1c35SAlex Shi #ifdef HAVE_GENERIC_MMU_GATHER
1221597e1c35SAlex Shi 		tlb->start = addr;
1222597e1c35SAlex Shi 		tlb->end = end;
1223597e1c35SAlex Shi #endif
1224d16dfc55SPeter Zijlstra 		tlb_flush_mmu(tlb);
1225d16dfc55SPeter Zijlstra 		if (addr != end)
1226d16dfc55SPeter Zijlstra 			goto again;
1227d16dfc55SPeter Zijlstra 	}
1228d16dfc55SPeter Zijlstra 
122951c6f666SRobin Holt 	return addr;
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds 
123251c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1233b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
12341da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
123597a89413SPeter Zijlstra 				struct zap_details *details)
12361da177e4SLinus Torvalds {
12371da177e4SLinus Torvalds 	pmd_t *pmd;
12381da177e4SLinus Torvalds 	unsigned long next;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
12411da177e4SLinus Torvalds 	do {
12421da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
124371e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*pmd)) {
124414d1a55cSAndrea Arcangeli 			if (next - addr != HPAGE_PMD_SIZE) {
1245e0897d75SDavid Rientjes #ifdef CONFIG_DEBUG_VM
1246e0897d75SDavid Rientjes 				if (!rwsem_is_locked(&tlb->mm->mmap_sem)) {
1247e0897d75SDavid Rientjes 					pr_err("%s: mmap_sem is unlocked! addr=0x%lx end=0x%lx vma->vm_start=0x%lx vma->vm_end=0x%lx\n",
1248e0897d75SDavid Rientjes 						__func__, addr, end,
1249e0897d75SDavid Rientjes 						vma->vm_start,
1250e0897d75SDavid Rientjes 						vma->vm_end);
1251e0897d75SDavid Rientjes 					BUG();
1252e0897d75SDavid Rientjes 				}
1253e0897d75SDavid Rientjes #endif
125471e3aac0SAndrea Arcangeli 				split_huge_page_pmd(vma->vm_mm, pmd);
1255f21760b1SShaohua Li 			} else if (zap_huge_pmd(tlb, vma, pmd, addr))
12561a5a9906SAndrea Arcangeli 				goto next;
125771e3aac0SAndrea Arcangeli 			/* fall through */
125871e3aac0SAndrea Arcangeli 		}
12591a5a9906SAndrea Arcangeli 		/*
12601a5a9906SAndrea Arcangeli 		 * Here there can be other concurrent MADV_DONTNEED or
12611a5a9906SAndrea Arcangeli 		 * trans huge page faults running, and if the pmd is
12621a5a9906SAndrea Arcangeli 		 * none or trans huge it can change under us. This is
12631a5a9906SAndrea Arcangeli 		 * because MADV_DONTNEED holds the mmap_sem in read
12641a5a9906SAndrea Arcangeli 		 * mode.
12651a5a9906SAndrea Arcangeli 		 */
12661a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
12671a5a9906SAndrea Arcangeli 			goto next;
126897a89413SPeter Zijlstra 		next = zap_pte_range(tlb, vma, pmd, addr, next, details);
12691a5a9906SAndrea Arcangeli next:
127097a89413SPeter Zijlstra 		cond_resched();
127197a89413SPeter Zijlstra 	} while (pmd++, addr = next, addr != end);
127251c6f666SRobin Holt 
127351c6f666SRobin Holt 	return addr;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
127651c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1277b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
12781da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
127997a89413SPeter Zijlstra 				struct zap_details *details)
12801da177e4SLinus Torvalds {
12811da177e4SLinus Torvalds 	pud_t *pud;
12821da177e4SLinus Torvalds 	unsigned long next;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
12851da177e4SLinus Torvalds 	do {
12861da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
128797a89413SPeter Zijlstra 		if (pud_none_or_clear_bad(pud))
12881da177e4SLinus Torvalds 			continue;
128997a89413SPeter Zijlstra 		next = zap_pmd_range(tlb, vma, pud, addr, next, details);
129097a89413SPeter Zijlstra 	} while (pud++, addr = next, addr != end);
129151c6f666SRobin Holt 
129251c6f666SRobin Holt 	return addr;
12931da177e4SLinus Torvalds }
12941da177e4SLinus Torvalds 
1295038c7aa1SAl Viro static void unmap_page_range(struct mmu_gather *tlb,
129651c6f666SRobin Holt 			     struct vm_area_struct *vma,
12971da177e4SLinus Torvalds 			     unsigned long addr, unsigned long end,
129897a89413SPeter Zijlstra 			     struct zap_details *details)
12991da177e4SLinus Torvalds {
13001da177e4SLinus Torvalds 	pgd_t *pgd;
13011da177e4SLinus Torvalds 	unsigned long next;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	if (details && !details->check_mapping && !details->nonlinear_vma)
13041da177e4SLinus Torvalds 		details = NULL;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1307569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_start();
13081da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
13091da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
13101da177e4SLinus Torvalds 	do {
13111da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
131297a89413SPeter Zijlstra 		if (pgd_none_or_clear_bad(pgd))
13131da177e4SLinus Torvalds 			continue;
131497a89413SPeter Zijlstra 		next = zap_pud_range(tlb, vma, pgd, addr, next, details);
131597a89413SPeter Zijlstra 	} while (pgd++, addr = next, addr != end);
13161da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
1317569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_end();
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
1320f5cc4eefSAl Viro 
1321f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb,
13221da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
13234f74d2c8SLinus Torvalds 		unsigned long end_addr,
13241da177e4SLinus Torvalds 		struct zap_details *details)
13251da177e4SLinus Torvalds {
1326f5cc4eefSAl Viro 	unsigned long start = max(vma->vm_start, start_addr);
13271da177e4SLinus Torvalds 	unsigned long end;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	if (start >= vma->vm_end)
1330f5cc4eefSAl Viro 		return;
13311da177e4SLinus Torvalds 	end = min(vma->vm_end, end_addr);
13321da177e4SLinus Torvalds 	if (end <= vma->vm_start)
1333f5cc4eefSAl Viro 		return;
13341da177e4SLinus Torvalds 
1335cbc91f71SSrikar Dronamraju 	if (vma->vm_file)
1336cbc91f71SSrikar Dronamraju 		uprobe_munmap(vma, start, end);
1337cbc91f71SSrikar Dronamraju 
1338b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP))
13395180da41SSuresh Siddha 		untrack_pfn(vma, 0, 0);
13402ab64037Svenkatesh.pallipadi@intel.com 
13418b2a1238SAl Viro 	if (start != end) {
134251c6f666SRobin Holt 		if (unlikely(is_vm_hugetlb_page(vma))) {
1343a137e1ccSAndi Kleen 			/*
1344a137e1ccSAndi Kleen 			 * It is undesirable to test vma->vm_file as it
1345a137e1ccSAndi Kleen 			 * should be non-null for valid hugetlb area.
1346a137e1ccSAndi Kleen 			 * However, vm_file will be NULL in the error
1347a137e1ccSAndi Kleen 			 * cleanup path of do_mmap_pgoff. When
1348a137e1ccSAndi Kleen 			 * hugetlbfs ->mmap method fails,
1349a137e1ccSAndi Kleen 			 * do_mmap_pgoff() nullifies vma->vm_file
1350a137e1ccSAndi Kleen 			 * before calling this function to clean up.
1351a137e1ccSAndi Kleen 			 * Since no pte has actually been setup, it is
1352a137e1ccSAndi Kleen 			 * safe to do nothing in this case.
1353a137e1ccSAndi Kleen 			 */
135424669e58SAneesh Kumar K.V 			if (vma->vm_file) {
135524669e58SAneesh Kumar K.V 				mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
1356d833352aSMel Gorman 				__unmap_hugepage_range_final(tlb, vma, start, end, NULL);
135724669e58SAneesh Kumar K.V 				mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
135824669e58SAneesh Kumar K.V 			}
135951c6f666SRobin Holt 		} else
1360038c7aa1SAl Viro 			unmap_page_range(tlb, vma, start, end, details);
136197a89413SPeter Zijlstra 	}
136251c6f666SRobin Holt }
13631da177e4SLinus Torvalds 
1364f5cc4eefSAl Viro /**
1365f5cc4eefSAl Viro  * unmap_vmas - unmap a range of memory covered by a list of vma's
1366f5cc4eefSAl Viro  * @tlb: address of the caller's struct mmu_gather
1367f5cc4eefSAl Viro  * @vma: the starting vma
1368f5cc4eefSAl Viro  * @start_addr: virtual address at which to start unmapping
1369f5cc4eefSAl Viro  * @end_addr: virtual address at which to end unmapping
1370f5cc4eefSAl Viro  *
1371f5cc4eefSAl Viro  * Unmap all pages in the vma list.
1372f5cc4eefSAl Viro  *
1373f5cc4eefSAl Viro  * Only addresses between `start' and `end' will be unmapped.
1374f5cc4eefSAl Viro  *
1375f5cc4eefSAl Viro  * The VMA list must be sorted in ascending virtual address order.
1376f5cc4eefSAl Viro  *
1377f5cc4eefSAl Viro  * unmap_vmas() assumes that the caller will flush the whole unmapped address
1378f5cc4eefSAl Viro  * range after unmap_vmas() returns.  So the only responsibility here is to
1379f5cc4eefSAl Viro  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1380f5cc4eefSAl Viro  * drops the lock and schedules.
1381f5cc4eefSAl Viro  */
1382f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb,
1383f5cc4eefSAl Viro 		struct vm_area_struct *vma, unsigned long start_addr,
13844f74d2c8SLinus Torvalds 		unsigned long end_addr)
1385f5cc4eefSAl Viro {
1386f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1387f5cc4eefSAl Viro 
1388f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
1389f5cc4eefSAl Viro 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
13904f74d2c8SLinus Torvalds 		unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
1391cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds /**
13951da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
13961da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
1397eb4546bbSRandy Dunlap  * @start: starting address of pages to zap
13981da177e4SLinus Torvalds  * @size: number of bytes to zap
13991da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
1400f5cc4eefSAl Viro  *
1401f5cc4eefSAl Viro  * Caller must protect the VMA list
14021da177e4SLinus Torvalds  */
14037e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start,
14041da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
14051da177e4SLinus Torvalds {
14061da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1407d16dfc55SPeter Zijlstra 	struct mmu_gather tlb;
14087e027b14SLinus Torvalds 	unsigned long end = start + size;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	lru_add_drain();
1411d16dfc55SPeter Zijlstra 	tlb_gather_mmu(&tlb, mm, 0);
1412365e9c87SHugh Dickins 	update_hiwater_rss(mm);
14137e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_start(mm, start, end);
14147e027b14SLinus Torvalds 	for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
14154f74d2c8SLinus Torvalds 		unmap_single_vma(&tlb, vma, start, end, details);
14167e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_end(mm, start, end);
14177e027b14SLinus Torvalds 	tlb_finish_mmu(&tlb, start, end);
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds 
1420c627f9ccSJack Steiner /**
1421f5cc4eefSAl Viro  * zap_page_range_single - remove user pages in a given range
1422f5cc4eefSAl Viro  * @vma: vm_area_struct holding the applicable pages
1423f5cc4eefSAl Viro  * @address: starting address of pages to zap
1424f5cc4eefSAl Viro  * @size: number of bytes to zap
1425f5cc4eefSAl Viro  * @details: details of nonlinear truncation or shared cache invalidation
1426f5cc4eefSAl Viro  *
1427f5cc4eefSAl Viro  * The range must fit into one VMA.
1428f5cc4eefSAl Viro  */
1429f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1430f5cc4eefSAl Viro 		unsigned long size, struct zap_details *details)
1431f5cc4eefSAl Viro {
1432f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1433f5cc4eefSAl Viro 	struct mmu_gather tlb;
1434f5cc4eefSAl Viro 	unsigned long end = address + size;
1435f5cc4eefSAl Viro 
1436f5cc4eefSAl Viro 	lru_add_drain();
1437f5cc4eefSAl Viro 	tlb_gather_mmu(&tlb, mm, 0);
1438f5cc4eefSAl Viro 	update_hiwater_rss(mm);
1439f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, address, end);
14404f74d2c8SLinus Torvalds 	unmap_single_vma(&tlb, vma, address, end, details);
1441f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_end(mm, address, end);
1442f5cc4eefSAl Viro 	tlb_finish_mmu(&tlb, address, end);
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
1445c627f9ccSJack Steiner /**
1446c627f9ccSJack Steiner  * zap_vma_ptes - remove ptes mapping the vma
1447c627f9ccSJack Steiner  * @vma: vm_area_struct holding ptes to be zapped
1448c627f9ccSJack Steiner  * @address: starting address of pages to zap
1449c627f9ccSJack Steiner  * @size: number of bytes to zap
1450c627f9ccSJack Steiner  *
1451c627f9ccSJack Steiner  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1452c627f9ccSJack Steiner  *
1453c627f9ccSJack Steiner  * The entire address range must be fully contained within the vma.
1454c627f9ccSJack Steiner  *
1455c627f9ccSJack Steiner  * Returns 0 if successful.
1456c627f9ccSJack Steiner  */
1457c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1458c627f9ccSJack Steiner 		unsigned long size)
1459c627f9ccSJack Steiner {
1460c627f9ccSJack Steiner 	if (address < vma->vm_start || address + size > vma->vm_end ||
1461c627f9ccSJack Steiner 	    		!(vma->vm_flags & VM_PFNMAP))
1462c627f9ccSJack Steiner 		return -1;
1463f5cc4eefSAl Viro 	zap_page_range_single(vma, address, size, NULL);
1464c627f9ccSJack Steiner 	return 0;
1465c627f9ccSJack Steiner }
1466c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes);
1467c627f9ccSJack Steiner 
1468142762bdSJohannes Weiner /**
1469142762bdSJohannes Weiner  * follow_page - look up a page descriptor from a user-virtual address
1470142762bdSJohannes Weiner  * @vma: vm_area_struct mapping @address
1471142762bdSJohannes Weiner  * @address: virtual address to look up
1472142762bdSJohannes Weiner  * @flags: flags modifying lookup behaviour
1473142762bdSJohannes Weiner  *
1474142762bdSJohannes Weiner  * @flags can have FOLL_ flags set, defined in <linux/mm.h>
1475142762bdSJohannes Weiner  *
1476142762bdSJohannes Weiner  * Returns the mapped (struct page *), %NULL if no mapping exists, or
1477142762bdSJohannes Weiner  * an error pointer if there is a mapping to something not represented
1478142762bdSJohannes Weiner  * by a page descriptor (see also vm_normal_page()).
14791da177e4SLinus Torvalds  */
14806aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1481deceb6cdSHugh Dickins 			unsigned int flags)
14821da177e4SLinus Torvalds {
14831da177e4SLinus Torvalds 	pgd_t *pgd;
14841da177e4SLinus Torvalds 	pud_t *pud;
14851da177e4SLinus Torvalds 	pmd_t *pmd;
14861da177e4SLinus Torvalds 	pte_t *ptep, pte;
1487deceb6cdSHugh Dickins 	spinlock_t *ptl;
14881da177e4SLinus Torvalds 	struct page *page;
14896aab341eSLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
14901da177e4SLinus Torvalds 
1491deceb6cdSHugh Dickins 	page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
1492deceb6cdSHugh Dickins 	if (!IS_ERR(page)) {
1493deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
1494deceb6cdSHugh Dickins 		goto out;
1495deceb6cdSHugh Dickins 	}
14961da177e4SLinus Torvalds 
1497deceb6cdSHugh Dickins 	page = NULL;
14981da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
14991da177e4SLinus Torvalds 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
1500deceb6cdSHugh Dickins 		goto no_page_table;
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	pud = pud_offset(pgd, address);
1503ceb86879SAndi Kleen 	if (pud_none(*pud))
1504ceb86879SAndi Kleen 		goto no_page_table;
15058a07651eSHugh Dickins 	if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
1506ceb86879SAndi Kleen 		BUG_ON(flags & FOLL_GET);
1507ceb86879SAndi Kleen 		page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
1508ceb86879SAndi Kleen 		goto out;
1509ceb86879SAndi Kleen 	}
1510ceb86879SAndi Kleen 	if (unlikely(pud_bad(*pud)))
1511deceb6cdSHugh Dickins 		goto no_page_table;
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	pmd = pmd_offset(pud, address);
1514aeed5fceSHugh Dickins 	if (pmd_none(*pmd))
1515deceb6cdSHugh Dickins 		goto no_page_table;
151671e3aac0SAndrea Arcangeli 	if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
1517deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
1518deceb6cdSHugh Dickins 		page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
1519deceb6cdSHugh Dickins 		goto out;
1520deceb6cdSHugh Dickins 	}
15210b9d7052SAndrea Arcangeli 	if ((flags & FOLL_NUMA) && pmd_numa(*pmd))
15220b9d7052SAndrea Arcangeli 		goto no_page_table;
152371e3aac0SAndrea Arcangeli 	if (pmd_trans_huge(*pmd)) {
1524500d65d4SAndrea Arcangeli 		if (flags & FOLL_SPLIT) {
1525500d65d4SAndrea Arcangeli 			split_huge_page_pmd(mm, pmd);
1526500d65d4SAndrea Arcangeli 			goto split_fallthrough;
1527500d65d4SAndrea Arcangeli 		}
152871e3aac0SAndrea Arcangeli 		spin_lock(&mm->page_table_lock);
152971e3aac0SAndrea Arcangeli 		if (likely(pmd_trans_huge(*pmd))) {
153071e3aac0SAndrea Arcangeli 			if (unlikely(pmd_trans_splitting(*pmd))) {
153171e3aac0SAndrea Arcangeli 				spin_unlock(&mm->page_table_lock);
153271e3aac0SAndrea Arcangeli 				wait_split_huge_page(vma->anon_vma, pmd);
153371e3aac0SAndrea Arcangeli 			} else {
1534b676b293SDavid Rientjes 				page = follow_trans_huge_pmd(vma, address,
153571e3aac0SAndrea Arcangeli 							     pmd, flags);
153671e3aac0SAndrea Arcangeli 				spin_unlock(&mm->page_table_lock);
153771e3aac0SAndrea Arcangeli 				goto out;
153871e3aac0SAndrea Arcangeli 			}
153971e3aac0SAndrea Arcangeli 		} else
154071e3aac0SAndrea Arcangeli 			spin_unlock(&mm->page_table_lock);
154171e3aac0SAndrea Arcangeli 		/* fall through */
154271e3aac0SAndrea Arcangeli 	}
1543500d65d4SAndrea Arcangeli split_fallthrough:
1544aeed5fceSHugh Dickins 	if (unlikely(pmd_bad(*pmd)))
1545aeed5fceSHugh Dickins 		goto no_page_table;
1546aeed5fceSHugh Dickins 
1547deceb6cdSHugh Dickins 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	pte = *ptep;
1550deceb6cdSHugh Dickins 	if (!pte_present(pte))
155189f5b7daSLinus Torvalds 		goto no_page;
15520b9d7052SAndrea Arcangeli 	if ((flags & FOLL_NUMA) && pte_numa(pte))
15530b9d7052SAndrea Arcangeli 		goto no_page;
1554deceb6cdSHugh Dickins 	if ((flags & FOLL_WRITE) && !pte_write(pte))
1555deceb6cdSHugh Dickins 		goto unlock;
1556a13ea5b7SHugh Dickins 
15576aab341eSLinus Torvalds 	page = vm_normal_page(vma, address, pte);
1558a13ea5b7SHugh Dickins 	if (unlikely(!page)) {
1559a13ea5b7SHugh Dickins 		if ((flags & FOLL_DUMP) ||
156062eede62SHugh Dickins 		    !is_zero_pfn(pte_pfn(pte)))
156189f5b7daSLinus Torvalds 			goto bad_page;
1562a13ea5b7SHugh Dickins 		page = pte_page(pte);
1563a13ea5b7SHugh Dickins 	}
1564deceb6cdSHugh Dickins 
1565deceb6cdSHugh Dickins 	if (flags & FOLL_GET)
156670b50f94SAndrea Arcangeli 		get_page_foll(page);
1567deceb6cdSHugh Dickins 	if (flags & FOLL_TOUCH) {
1568deceb6cdSHugh Dickins 		if ((flags & FOLL_WRITE) &&
1569deceb6cdSHugh Dickins 		    !pte_dirty(pte) && !PageDirty(page))
1570f33ea7f4SNick Piggin 			set_page_dirty(page);
1571bd775c42SKOSAKI Motohiro 		/*
1572bd775c42SKOSAKI Motohiro 		 * pte_mkyoung() would be more correct here, but atomic care
1573bd775c42SKOSAKI Motohiro 		 * is needed to avoid losing the dirty bit: it is easier to use
1574bd775c42SKOSAKI Motohiro 		 * mark_page_accessed().
1575bd775c42SKOSAKI Motohiro 		 */
15761da177e4SLinus Torvalds 		mark_page_accessed(page);
15771da177e4SLinus Torvalds 	}
1578a1fde08cSLinus Torvalds 	if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1579110d74a9SMichel Lespinasse 		/*
1580110d74a9SMichel Lespinasse 		 * The preliminary mapping check is mainly to avoid the
1581110d74a9SMichel Lespinasse 		 * pointless overhead of lock_page on the ZERO_PAGE
1582110d74a9SMichel Lespinasse 		 * which might bounce very badly if there is contention.
1583110d74a9SMichel Lespinasse 		 *
1584110d74a9SMichel Lespinasse 		 * If the page is already locked, we don't need to
1585110d74a9SMichel Lespinasse 		 * handle it now - vmscan will handle it later if and
1586110d74a9SMichel Lespinasse 		 * when it attempts to reclaim the page.
1587110d74a9SMichel Lespinasse 		 */
1588110d74a9SMichel Lespinasse 		if (page->mapping && trylock_page(page)) {
1589110d74a9SMichel Lespinasse 			lru_add_drain();  /* push cached pages to LRU */
1590110d74a9SMichel Lespinasse 			/*
1591e6c509f8SHugh Dickins 			 * Because we lock page here, and migration is
1592e6c509f8SHugh Dickins 			 * blocked by the pte's page reference, and we
1593e6c509f8SHugh Dickins 			 * know the page is still mapped, we don't even
1594e6c509f8SHugh Dickins 			 * need to check for file-cache page truncation.
1595110d74a9SMichel Lespinasse 			 */
1596110d74a9SMichel Lespinasse 			mlock_vma_page(page);
1597110d74a9SMichel Lespinasse 			unlock_page(page);
1598110d74a9SMichel Lespinasse 		}
1599110d74a9SMichel Lespinasse 	}
1600deceb6cdSHugh Dickins unlock:
1601deceb6cdSHugh Dickins 	pte_unmap_unlock(ptep, ptl);
16021da177e4SLinus Torvalds out:
1603deceb6cdSHugh Dickins 	return page;
1604deceb6cdSHugh Dickins 
160589f5b7daSLinus Torvalds bad_page:
160689f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
160789f5b7daSLinus Torvalds 	return ERR_PTR(-EFAULT);
160889f5b7daSLinus Torvalds 
160989f5b7daSLinus Torvalds no_page:
161089f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
161189f5b7daSLinus Torvalds 	if (!pte_none(pte))
161289f5b7daSLinus Torvalds 		return page;
16138e4b9a60SHugh Dickins 
1614deceb6cdSHugh Dickins no_page_table:
1615deceb6cdSHugh Dickins 	/*
1616deceb6cdSHugh Dickins 	 * When core dumping an enormous anonymous area that nobody
16178e4b9a60SHugh Dickins 	 * has touched so far, we don't want to allocate unnecessary pages or
16188e4b9a60SHugh Dickins 	 * page tables.  Return error instead of NULL to skip handle_mm_fault,
16198e4b9a60SHugh Dickins 	 * then get_dump_page() will return NULL to leave a hole in the dump.
16208e4b9a60SHugh Dickins 	 * But we can only make this optimization where a hole would surely
16218e4b9a60SHugh Dickins 	 * be zero-filled if handle_mm_fault() actually did handle it.
1622deceb6cdSHugh Dickins 	 */
16238e4b9a60SHugh Dickins 	if ((flags & FOLL_DUMP) &&
16248e4b9a60SHugh Dickins 	    (!vma->vm_ops || !vma->vm_ops->fault))
16258e4b9a60SHugh Dickins 		return ERR_PTR(-EFAULT);
1626deceb6cdSHugh Dickins 	return page;
16271da177e4SLinus Torvalds }
16281da177e4SLinus Torvalds 
162995042f9eSLinus Torvalds static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
163095042f9eSLinus Torvalds {
1631a09a79f6SMikulas Patocka 	return stack_guard_page_start(vma, addr) ||
1632a09a79f6SMikulas Patocka 	       stack_guard_page_end(vma, addr+PAGE_SIZE);
163395042f9eSLinus Torvalds }
163495042f9eSLinus Torvalds 
16350014bd99SHuang Ying /**
16360014bd99SHuang Ying  * __get_user_pages() - pin user pages in memory
16370014bd99SHuang Ying  * @tsk:	task_struct of target task
16380014bd99SHuang Ying  * @mm:		mm_struct of target mm
16390014bd99SHuang Ying  * @start:	starting user address
16400014bd99SHuang Ying  * @nr_pages:	number of pages from start to pin
16410014bd99SHuang Ying  * @gup_flags:	flags modifying pin behaviour
16420014bd99SHuang Ying  * @pages:	array that receives pointers to the pages pinned.
16430014bd99SHuang Ying  *		Should be at least nr_pages long. Or NULL, if caller
16440014bd99SHuang Ying  *		only intends to ensure the pages are faulted in.
16450014bd99SHuang Ying  * @vmas:	array of pointers to vmas corresponding to each page.
16460014bd99SHuang Ying  *		Or NULL if the caller does not require them.
16470014bd99SHuang Ying  * @nonblocking: whether waiting for disk IO or mmap_sem contention
16480014bd99SHuang Ying  *
16490014bd99SHuang Ying  * Returns number of pages pinned. This may be fewer than the number
16500014bd99SHuang Ying  * requested. If nr_pages is 0 or negative, returns 0. If no pages
16510014bd99SHuang Ying  * were pinned, returns -errno. Each page returned must be released
16520014bd99SHuang Ying  * with a put_page() call when it is finished with. vmas will only
16530014bd99SHuang Ying  * remain valid while mmap_sem is held.
16540014bd99SHuang Ying  *
16550014bd99SHuang Ying  * Must be called with mmap_sem held for read or write.
16560014bd99SHuang Ying  *
16570014bd99SHuang Ying  * __get_user_pages walks a process's page tables and takes a reference to
16580014bd99SHuang Ying  * each struct page that each user address corresponds to at a given
16590014bd99SHuang Ying  * instant. That is, it takes the page that would be accessed if a user
16600014bd99SHuang Ying  * thread accesses the given user virtual address at that instant.
16610014bd99SHuang Ying  *
16620014bd99SHuang Ying  * This does not guarantee that the page exists in the user mappings when
16630014bd99SHuang Ying  * __get_user_pages returns, and there may even be a completely different
16640014bd99SHuang Ying  * page there in some cases (eg. if mmapped pagecache has been invalidated
16650014bd99SHuang Ying  * and subsequently re faulted). However it does guarantee that the page
16660014bd99SHuang Ying  * won't be freed completely. And mostly callers simply care that the page
16670014bd99SHuang Ying  * contains data that was valid *at some point in time*. Typically, an IO
16680014bd99SHuang Ying  * or similar operation cannot guarantee anything stronger anyway because
16690014bd99SHuang Ying  * locks can't be held over the syscall boundary.
16700014bd99SHuang Ying  *
16710014bd99SHuang Ying  * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
16720014bd99SHuang Ying  * the page is written to, set_page_dirty (or set_page_dirty_lock, as
16730014bd99SHuang Ying  * appropriate) must be called after the page is finished with, and
16740014bd99SHuang Ying  * before put_page is called.
16750014bd99SHuang Ying  *
16760014bd99SHuang Ying  * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
16770014bd99SHuang Ying  * or mmap_sem contention, and if waiting is needed to pin all pages,
16780014bd99SHuang Ying  * *@nonblocking will be set to 0.
16790014bd99SHuang Ying  *
16800014bd99SHuang Ying  * In most cases, get_user_pages or get_user_pages_fast should be used
16810014bd99SHuang Ying  * instead of __get_user_pages. __get_user_pages should be used only if
16820014bd99SHuang Ying  * you need some special @gup_flags.
16830014bd99SHuang Ying  */
1684b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
168558fa879eSHugh Dickins 		     unsigned long start, int nr_pages, unsigned int gup_flags,
168653a7706dSMichel Lespinasse 		     struct page **pages, struct vm_area_struct **vmas,
168753a7706dSMichel Lespinasse 		     int *nonblocking)
16881da177e4SLinus Torvalds {
16891da177e4SLinus Torvalds 	int i;
169058fa879eSHugh Dickins 	unsigned long vm_flags;
16911da177e4SLinus Torvalds 
16929d73777eSPeter Zijlstra 	if (nr_pages <= 0)
1693900cf086SJonathan Corbet 		return 0;
169458fa879eSHugh Dickins 
169558fa879eSHugh Dickins 	VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
169658fa879eSHugh Dickins 
16971da177e4SLinus Torvalds 	/*
16981da177e4SLinus Torvalds 	 * Require read or write permissions.
169958fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
17001da177e4SLinus Torvalds 	 */
170158fa879eSHugh Dickins 	vm_flags  = (gup_flags & FOLL_WRITE) ?
170258fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
170358fa879eSHugh Dickins 	vm_flags &= (gup_flags & FOLL_FORCE) ?
170458fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
17050b9d7052SAndrea Arcangeli 
17060b9d7052SAndrea Arcangeli 	/*
17070b9d7052SAndrea Arcangeli 	 * If FOLL_FORCE and FOLL_NUMA are both set, handle_mm_fault
17080b9d7052SAndrea Arcangeli 	 * would be called on PROT_NONE ranges. We must never invoke
17090b9d7052SAndrea Arcangeli 	 * handle_mm_fault on PROT_NONE ranges or the NUMA hinting
17100b9d7052SAndrea Arcangeli 	 * page faults would unprotect the PROT_NONE ranges if
17110b9d7052SAndrea Arcangeli 	 * _PAGE_NUMA and _PAGE_PROTNONE are sharing the same pte/pmd
17120b9d7052SAndrea Arcangeli 	 * bitflag. So to avoid that, don't set FOLL_NUMA if
17130b9d7052SAndrea Arcangeli 	 * FOLL_FORCE is set.
17140b9d7052SAndrea Arcangeli 	 */
17150b9d7052SAndrea Arcangeli 	if (!(gup_flags & FOLL_FORCE))
17160b9d7052SAndrea Arcangeli 		gup_flags |= FOLL_NUMA;
17170b9d7052SAndrea Arcangeli 
17181da177e4SLinus Torvalds 	i = 0;
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	do {
17211da177e4SLinus Torvalds 		struct vm_area_struct *vma;
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 		vma = find_extend_vma(mm, start);
1724e7f22e20SStephen Wilson 		if (!vma && in_gate_area(mm, start)) {
17251da177e4SLinus Torvalds 			unsigned long pg = start & PAGE_MASK;
17261da177e4SLinus Torvalds 			pgd_t *pgd;
17271da177e4SLinus Torvalds 			pud_t *pud;
17281da177e4SLinus Torvalds 			pmd_t *pmd;
17291da177e4SLinus Torvalds 			pte_t *pte;
1730b291f000SNick Piggin 
1731b291f000SNick Piggin 			/* user gate pages are read-only */
173258fa879eSHugh Dickins 			if (gup_flags & FOLL_WRITE)
17331da177e4SLinus Torvalds 				return i ? : -EFAULT;
17341da177e4SLinus Torvalds 			if (pg > TASK_SIZE)
17351da177e4SLinus Torvalds 				pgd = pgd_offset_k(pg);
17361da177e4SLinus Torvalds 			else
17371da177e4SLinus Torvalds 				pgd = pgd_offset_gate(mm, pg);
17381da177e4SLinus Torvalds 			BUG_ON(pgd_none(*pgd));
17391da177e4SLinus Torvalds 			pud = pud_offset(pgd, pg);
17401da177e4SLinus Torvalds 			BUG_ON(pud_none(*pud));
17411da177e4SLinus Torvalds 			pmd = pmd_offset(pud, pg);
1742690dbe1cSHugh Dickins 			if (pmd_none(*pmd))
1743690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1744f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
17451da177e4SLinus Torvalds 			pte = pte_offset_map(pmd, pg);
1746690dbe1cSHugh Dickins 			if (pte_none(*pte)) {
1747690dbe1cSHugh Dickins 				pte_unmap(pte);
1748690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1749690dbe1cSHugh Dickins 			}
175095042f9eSLinus Torvalds 			vma = get_gate_vma(mm);
17511da177e4SLinus Torvalds 			if (pages) {
1752de51257aSHugh Dickins 				struct page *page;
1753de51257aSHugh Dickins 
175495042f9eSLinus Torvalds 				page = vm_normal_page(vma, start, *pte);
1755de51257aSHugh Dickins 				if (!page) {
1756de51257aSHugh Dickins 					if (!(gup_flags & FOLL_DUMP) &&
1757de51257aSHugh Dickins 					     is_zero_pfn(pte_pfn(*pte)))
1758de51257aSHugh Dickins 						page = pte_page(*pte);
1759de51257aSHugh Dickins 					else {
1760de51257aSHugh Dickins 						pte_unmap(pte);
1761de51257aSHugh Dickins 						return i ? : -EFAULT;
1762de51257aSHugh Dickins 					}
1763de51257aSHugh Dickins 				}
17646aab341eSLinus Torvalds 				pages[i] = page;
17656aab341eSLinus Torvalds 				get_page(page);
17661da177e4SLinus Torvalds 			}
17671da177e4SLinus Torvalds 			pte_unmap(pte);
176895042f9eSLinus Torvalds 			goto next_page;
17691da177e4SLinus Torvalds 		}
17701da177e4SLinus Torvalds 
1771b291f000SNick Piggin 		if (!vma ||
1772b291f000SNick Piggin 		    (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
17731c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
17741da177e4SLinus Torvalds 			return i ? : -EFAULT;
17751da177e4SLinus Torvalds 
17762a15efc9SHugh Dickins 		if (is_vm_hugetlb_page(vma)) {
17772a15efc9SHugh Dickins 			i = follow_hugetlb_page(mm, vma, pages, vmas,
177858fa879eSHugh Dickins 					&start, &nr_pages, i, gup_flags);
17792a15efc9SHugh Dickins 			continue;
17802a15efc9SHugh Dickins 		}
1781deceb6cdSHugh Dickins 
17821da177e4SLinus Torvalds 		do {
178308ef4729SHugh Dickins 			struct page *page;
178458fa879eSHugh Dickins 			unsigned int foll_flags = gup_flags;
17851da177e4SLinus Torvalds 
1786462e00ccSEthan Solomita 			/*
17874779280dSYing Han 			 * If we have a pending SIGKILL, don't keep faulting
17881c3aff1cSHugh Dickins 			 * pages and potentially allocating memory.
1789462e00ccSEthan Solomita 			 */
17901c3aff1cSHugh Dickins 			if (unlikely(fatal_signal_pending(current)))
17914779280dSYing Han 				return i ? i : -ERESTARTSYS;
1792462e00ccSEthan Solomita 
1793deceb6cdSHugh Dickins 			cond_resched();
17946aab341eSLinus Torvalds 			while (!(page = follow_page(vma, start, foll_flags))) {
1795a68d2ebcSLinus Torvalds 				int ret;
179653a7706dSMichel Lespinasse 				unsigned int fault_flags = 0;
179753a7706dSMichel Lespinasse 
1798a09a79f6SMikulas Patocka 				/* For mlock, just skip the stack guard page. */
1799a09a79f6SMikulas Patocka 				if (foll_flags & FOLL_MLOCK) {
1800a09a79f6SMikulas Patocka 					if (stack_guard_page(vma, start))
1801a09a79f6SMikulas Patocka 						goto next_page;
1802a09a79f6SMikulas Patocka 				}
180353a7706dSMichel Lespinasse 				if (foll_flags & FOLL_WRITE)
180453a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_WRITE;
180553a7706dSMichel Lespinasse 				if (nonblocking)
180653a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_ALLOW_RETRY;
1807318b275fSGleb Natapov 				if (foll_flags & FOLL_NOWAIT)
1808318b275fSGleb Natapov 					fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
1809d06063ccSLinus Torvalds 
1810d26ed650SHugh Dickins 				ret = handle_mm_fault(mm, vma, start,
181153a7706dSMichel Lespinasse 							fault_flags);
1812d26ed650SHugh Dickins 
181383c54070SNick Piggin 				if (ret & VM_FAULT_ERROR) {
181483c54070SNick Piggin 					if (ret & VM_FAULT_OOM)
181583c54070SNick Piggin 						return i ? i : -ENOMEM;
181669ebb83eSHuang Ying 					if (ret & (VM_FAULT_HWPOISON |
181769ebb83eSHuang Ying 						   VM_FAULT_HWPOISON_LARGE)) {
181869ebb83eSHuang Ying 						if (i)
181969ebb83eSHuang Ying 							return i;
182069ebb83eSHuang Ying 						else if (gup_flags & FOLL_HWPOISON)
182169ebb83eSHuang Ying 							return -EHWPOISON;
182269ebb83eSHuang Ying 						else
182369ebb83eSHuang Ying 							return -EFAULT;
182469ebb83eSHuang Ying 					}
182569ebb83eSHuang Ying 					if (ret & VM_FAULT_SIGBUS)
182683c54070SNick Piggin 						return i ? i : -EFAULT;
182783c54070SNick Piggin 					BUG();
182883c54070SNick Piggin 				}
1829e7f22e20SStephen Wilson 
1830e7f22e20SStephen Wilson 				if (tsk) {
183183c54070SNick Piggin 					if (ret & VM_FAULT_MAJOR)
183283c54070SNick Piggin 						tsk->maj_flt++;
183383c54070SNick Piggin 					else
183483c54070SNick Piggin 						tsk->min_flt++;
1835e7f22e20SStephen Wilson 				}
183683c54070SNick Piggin 
183753a7706dSMichel Lespinasse 				if (ret & VM_FAULT_RETRY) {
1838318b275fSGleb Natapov 					if (nonblocking)
183953a7706dSMichel Lespinasse 						*nonblocking = 0;
184053a7706dSMichel Lespinasse 					return i;
184153a7706dSMichel Lespinasse 				}
184253a7706dSMichel Lespinasse 
1843f33ea7f4SNick Piggin 				/*
184483c54070SNick Piggin 				 * The VM_FAULT_WRITE bit tells us that
184583c54070SNick Piggin 				 * do_wp_page has broken COW when necessary,
184683c54070SNick Piggin 				 * even if maybe_mkwrite decided not to set
184783c54070SNick Piggin 				 * pte_write. We can thus safely do subsequent
1848878b63acSHugh Dickins 				 * page lookups as if they were reads. But only
1849878b63acSHugh Dickins 				 * do so when looping for pte_write is futile:
1850878b63acSHugh Dickins 				 * in some cases userspace may also be wanting
1851878b63acSHugh Dickins 				 * to write to the gotten user page, which a
1852878b63acSHugh Dickins 				 * read fault here might prevent (a readonly
1853878b63acSHugh Dickins 				 * page might get reCOWed by userspace write).
1854f33ea7f4SNick Piggin 				 */
1855878b63acSHugh Dickins 				if ((ret & VM_FAULT_WRITE) &&
1856878b63acSHugh Dickins 				    !(vma->vm_flags & VM_WRITE))
1857deceb6cdSHugh Dickins 					foll_flags &= ~FOLL_WRITE;
1858a68d2ebcSLinus Torvalds 
18597f7bbbe5SBenjamin Herrenschmidt 				cond_resched();
18601da177e4SLinus Torvalds 			}
186189f5b7daSLinus Torvalds 			if (IS_ERR(page))
186289f5b7daSLinus Torvalds 				return i ? i : PTR_ERR(page);
18631da177e4SLinus Torvalds 			if (pages) {
186408ef4729SHugh Dickins 				pages[i] = page;
186503beb076SJames Bottomley 
1866a6f36be3SRussell King 				flush_anon_page(vma, page, start);
186708ef4729SHugh Dickins 				flush_dcache_page(page);
18681da177e4SLinus Torvalds 			}
186995042f9eSLinus Torvalds next_page:
18701da177e4SLinus Torvalds 			if (vmas)
18711da177e4SLinus Torvalds 				vmas[i] = vma;
18721da177e4SLinus Torvalds 			i++;
18731da177e4SLinus Torvalds 			start += PAGE_SIZE;
18749d73777eSPeter Zijlstra 			nr_pages--;
18759d73777eSPeter Zijlstra 		} while (nr_pages && start < vma->vm_end);
18769d73777eSPeter Zijlstra 	} while (nr_pages);
18771da177e4SLinus Torvalds 	return i;
18781da177e4SLinus Torvalds }
18790014bd99SHuang Ying EXPORT_SYMBOL(__get_user_pages);
1880b291f000SNick Piggin 
18812efaca92SBenjamin Herrenschmidt /*
18822efaca92SBenjamin Herrenschmidt  * fixup_user_fault() - manually resolve a user page fault
18832efaca92SBenjamin Herrenschmidt  * @tsk:	the task_struct to use for page fault accounting, or
18842efaca92SBenjamin Herrenschmidt  *		NULL if faults are not to be recorded.
18852efaca92SBenjamin Herrenschmidt  * @mm:		mm_struct of target mm
18862efaca92SBenjamin Herrenschmidt  * @address:	user address
18872efaca92SBenjamin Herrenschmidt  * @fault_flags:flags to pass down to handle_mm_fault()
18882efaca92SBenjamin Herrenschmidt  *
18892efaca92SBenjamin Herrenschmidt  * This is meant to be called in the specific scenario where for locking reasons
18902efaca92SBenjamin Herrenschmidt  * we try to access user memory in atomic context (within a pagefault_disable()
18912efaca92SBenjamin Herrenschmidt  * section), this returns -EFAULT, and we want to resolve the user fault before
18922efaca92SBenjamin Herrenschmidt  * trying again.
18932efaca92SBenjamin Herrenschmidt  *
18942efaca92SBenjamin Herrenschmidt  * Typically this is meant to be used by the futex code.
18952efaca92SBenjamin Herrenschmidt  *
18962efaca92SBenjamin Herrenschmidt  * The main difference with get_user_pages() is that this function will
18972efaca92SBenjamin Herrenschmidt  * unconditionally call handle_mm_fault() which will in turn perform all the
18982efaca92SBenjamin Herrenschmidt  * necessary SW fixup of the dirty and young bits in the PTE, while
18992efaca92SBenjamin Herrenschmidt  * handle_mm_fault() only guarantees to update these in the struct page.
19002efaca92SBenjamin Herrenschmidt  *
19012efaca92SBenjamin Herrenschmidt  * This is important for some architectures where those bits also gate the
19022efaca92SBenjamin Herrenschmidt  * access permission to the page because they are maintained in software.  On
19032efaca92SBenjamin Herrenschmidt  * such architectures, gup() will not be enough to make a subsequent access
19042efaca92SBenjamin Herrenschmidt  * succeed.
19052efaca92SBenjamin Herrenschmidt  *
19062efaca92SBenjamin Herrenschmidt  * This should be called with the mm_sem held for read.
19072efaca92SBenjamin Herrenschmidt  */
19082efaca92SBenjamin Herrenschmidt int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
19092efaca92SBenjamin Herrenschmidt 		     unsigned long address, unsigned int fault_flags)
19102efaca92SBenjamin Herrenschmidt {
19112efaca92SBenjamin Herrenschmidt 	struct vm_area_struct *vma;
19122efaca92SBenjamin Herrenschmidt 	int ret;
19132efaca92SBenjamin Herrenschmidt 
19142efaca92SBenjamin Herrenschmidt 	vma = find_extend_vma(mm, address);
19152efaca92SBenjamin Herrenschmidt 	if (!vma || address < vma->vm_start)
19162efaca92SBenjamin Herrenschmidt 		return -EFAULT;
19172efaca92SBenjamin Herrenschmidt 
19182efaca92SBenjamin Herrenschmidt 	ret = handle_mm_fault(mm, vma, address, fault_flags);
19192efaca92SBenjamin Herrenschmidt 	if (ret & VM_FAULT_ERROR) {
19202efaca92SBenjamin Herrenschmidt 		if (ret & VM_FAULT_OOM)
19212efaca92SBenjamin Herrenschmidt 			return -ENOMEM;
19222efaca92SBenjamin Herrenschmidt 		if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
19232efaca92SBenjamin Herrenschmidt 			return -EHWPOISON;
19242efaca92SBenjamin Herrenschmidt 		if (ret & VM_FAULT_SIGBUS)
19252efaca92SBenjamin Herrenschmidt 			return -EFAULT;
19262efaca92SBenjamin Herrenschmidt 		BUG();
19272efaca92SBenjamin Herrenschmidt 	}
19282efaca92SBenjamin Herrenschmidt 	if (tsk) {
19292efaca92SBenjamin Herrenschmidt 		if (ret & VM_FAULT_MAJOR)
19302efaca92SBenjamin Herrenschmidt 			tsk->maj_flt++;
19312efaca92SBenjamin Herrenschmidt 		else
19322efaca92SBenjamin Herrenschmidt 			tsk->min_flt++;
19332efaca92SBenjamin Herrenschmidt 	}
19342efaca92SBenjamin Herrenschmidt 	return 0;
19352efaca92SBenjamin Herrenschmidt }
19362efaca92SBenjamin Herrenschmidt 
19372efaca92SBenjamin Herrenschmidt /*
1938d2bf6be8SNick Piggin  * get_user_pages() - pin user pages in memory
1939e7f22e20SStephen Wilson  * @tsk:	the task_struct to use for page fault accounting, or
1940e7f22e20SStephen Wilson  *		NULL if faults are not to be recorded.
1941d2bf6be8SNick Piggin  * @mm:		mm_struct of target mm
1942d2bf6be8SNick Piggin  * @start:	starting user address
19439d73777eSPeter Zijlstra  * @nr_pages:	number of pages from start to pin
1944d2bf6be8SNick Piggin  * @write:	whether pages will be written to by the caller
1945d2bf6be8SNick Piggin  * @force:	whether to force write access even if user mapping is
1946d2bf6be8SNick Piggin  *		readonly. This will result in the page being COWed even
1947d2bf6be8SNick Piggin  *		in MAP_SHARED mappings. You do not want this.
1948d2bf6be8SNick Piggin  * @pages:	array that receives pointers to the pages pinned.
1949d2bf6be8SNick Piggin  *		Should be at least nr_pages long. Or NULL, if caller
1950d2bf6be8SNick Piggin  *		only intends to ensure the pages are faulted in.
1951d2bf6be8SNick Piggin  * @vmas:	array of pointers to vmas corresponding to each page.
1952d2bf6be8SNick Piggin  *		Or NULL if the caller does not require them.
1953d2bf6be8SNick Piggin  *
1954d2bf6be8SNick Piggin  * Returns number of pages pinned. This may be fewer than the number
19559d73777eSPeter Zijlstra  * requested. If nr_pages is 0 or negative, returns 0. If no pages
1956d2bf6be8SNick Piggin  * were pinned, returns -errno. Each page returned must be released
1957d2bf6be8SNick Piggin  * with a put_page() call when it is finished with. vmas will only
1958d2bf6be8SNick Piggin  * remain valid while mmap_sem is held.
1959d2bf6be8SNick Piggin  *
1960d2bf6be8SNick Piggin  * Must be called with mmap_sem held for read or write.
1961d2bf6be8SNick Piggin  *
1962d2bf6be8SNick Piggin  * get_user_pages walks a process's page tables and takes a reference to
1963d2bf6be8SNick Piggin  * each struct page that each user address corresponds to at a given
1964d2bf6be8SNick Piggin  * instant. That is, it takes the page that would be accessed if a user
1965d2bf6be8SNick Piggin  * thread accesses the given user virtual address at that instant.
1966d2bf6be8SNick Piggin  *
1967d2bf6be8SNick Piggin  * This does not guarantee that the page exists in the user mappings when
1968d2bf6be8SNick Piggin  * get_user_pages returns, and there may even be a completely different
1969d2bf6be8SNick Piggin  * page there in some cases (eg. if mmapped pagecache has been invalidated
1970d2bf6be8SNick Piggin  * and subsequently re faulted). However it does guarantee that the page
1971d2bf6be8SNick Piggin  * won't be freed completely. And mostly callers simply care that the page
1972d2bf6be8SNick Piggin  * contains data that was valid *at some point in time*. Typically, an IO
1973d2bf6be8SNick Piggin  * or similar operation cannot guarantee anything stronger anyway because
1974d2bf6be8SNick Piggin  * locks can't be held over the syscall boundary.
1975d2bf6be8SNick Piggin  *
1976d2bf6be8SNick Piggin  * If write=0, the page must not be written to. If the page is written to,
1977d2bf6be8SNick Piggin  * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
1978d2bf6be8SNick Piggin  * after the page is finished with, and before put_page is called.
1979d2bf6be8SNick Piggin  *
1980d2bf6be8SNick Piggin  * get_user_pages is typically used for fewer-copy IO operations, to get a
1981d2bf6be8SNick Piggin  * handle on the memory by some means other than accesses via the user virtual
1982d2bf6be8SNick Piggin  * addresses. The pages may be submitted for DMA to devices or accessed via
1983d2bf6be8SNick Piggin  * their kernel linear mapping (via the kmap APIs). Care should be taken to
1984d2bf6be8SNick Piggin  * use the correct cache flushing APIs.
1985d2bf6be8SNick Piggin  *
1986d2bf6be8SNick Piggin  * See also get_user_pages_fast, for performance critical applications.
1987d2bf6be8SNick Piggin  */
1988b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
19899d73777eSPeter Zijlstra 		unsigned long start, int nr_pages, int write, int force,
1990b291f000SNick Piggin 		struct page **pages, struct vm_area_struct **vmas)
1991b291f000SNick Piggin {
199258fa879eSHugh Dickins 	int flags = FOLL_TOUCH;
1993b291f000SNick Piggin 
199458fa879eSHugh Dickins 	if (pages)
199558fa879eSHugh Dickins 		flags |= FOLL_GET;
1996b291f000SNick Piggin 	if (write)
199758fa879eSHugh Dickins 		flags |= FOLL_WRITE;
1998b291f000SNick Piggin 	if (force)
199958fa879eSHugh Dickins 		flags |= FOLL_FORCE;
2000b291f000SNick Piggin 
200153a7706dSMichel Lespinasse 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
200253a7706dSMichel Lespinasse 				NULL);
2003b291f000SNick Piggin }
20041da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages);
20051da177e4SLinus Torvalds 
2006f3e8fccdSHugh Dickins /**
2007f3e8fccdSHugh Dickins  * get_dump_page() - pin user page in memory while writing it to core dump
2008f3e8fccdSHugh Dickins  * @addr: user address
2009f3e8fccdSHugh Dickins  *
2010f3e8fccdSHugh Dickins  * Returns struct page pointer of user page pinned for dump,
2011f3e8fccdSHugh Dickins  * to be freed afterwards by page_cache_release() or put_page().
2012f3e8fccdSHugh Dickins  *
2013f3e8fccdSHugh Dickins  * Returns NULL on any kind of failure - a hole must then be inserted into
2014f3e8fccdSHugh Dickins  * the corefile, to preserve alignment with its headers; and also returns
2015f3e8fccdSHugh Dickins  * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
2016f3e8fccdSHugh Dickins  * allowing a hole to be left in the corefile to save diskspace.
2017f3e8fccdSHugh Dickins  *
2018f3e8fccdSHugh Dickins  * Called without mmap_sem, but after all other threads have been killed.
2019f3e8fccdSHugh Dickins  */
2020f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE
2021f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr)
2022f3e8fccdSHugh Dickins {
2023f3e8fccdSHugh Dickins 	struct vm_area_struct *vma;
2024f3e8fccdSHugh Dickins 	struct page *page;
2025f3e8fccdSHugh Dickins 
2026f3e8fccdSHugh Dickins 	if (__get_user_pages(current, current->mm, addr, 1,
202753a7706dSMichel Lespinasse 			     FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
202853a7706dSMichel Lespinasse 			     NULL) < 1)
2029f3e8fccdSHugh Dickins 		return NULL;
2030f3e8fccdSHugh Dickins 	flush_cache_page(vma, addr, page_to_pfn(page));
2031f3e8fccdSHugh Dickins 	return page;
2032f3e8fccdSHugh Dickins }
2033f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */
2034f3e8fccdSHugh Dickins 
203525ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
2036920c7a5dSHarvey Harrison 			spinlock_t **ptl)
2037c9cfcddfSLinus Torvalds {
2038c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
2039c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
2040c9cfcddfSLinus Torvalds 	if (pud) {
204149c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
2042f66055abSAndrea Arcangeli 		if (pmd) {
2043f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
2044c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
2045c9cfcddfSLinus Torvalds 		}
2046f66055abSAndrea Arcangeli 	}
2047c9cfcddfSLinus Torvalds 	return NULL;
2048c9cfcddfSLinus Torvalds }
2049c9cfcddfSLinus Torvalds 
20501da177e4SLinus Torvalds /*
2051238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
2052238f58d8SLinus Torvalds  *
2053238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
2054238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
2055238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
2056238f58d8SLinus Torvalds  */
2057423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr,
2058423bad60SNick Piggin 			struct page *page, pgprot_t prot)
2059238f58d8SLinus Torvalds {
2060423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
2061238f58d8SLinus Torvalds 	int retval;
2062238f58d8SLinus Torvalds 	pte_t *pte;
2063238f58d8SLinus Torvalds 	spinlock_t *ptl;
2064238f58d8SLinus Torvalds 
2065238f58d8SLinus Torvalds 	retval = -EINVAL;
2066a145dd41SLinus Torvalds 	if (PageAnon(page))
20675b4e655eSKAMEZAWA Hiroyuki 		goto out;
2068238f58d8SLinus Torvalds 	retval = -ENOMEM;
2069238f58d8SLinus Torvalds 	flush_dcache_page(page);
2070c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
2071238f58d8SLinus Torvalds 	if (!pte)
20725b4e655eSKAMEZAWA Hiroyuki 		goto out;
2073238f58d8SLinus Torvalds 	retval = -EBUSY;
2074238f58d8SLinus Torvalds 	if (!pte_none(*pte))
2075238f58d8SLinus Torvalds 		goto out_unlock;
2076238f58d8SLinus Torvalds 
2077238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
2078238f58d8SLinus Torvalds 	get_page(page);
207934e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_FILEPAGES);
2080238f58d8SLinus Torvalds 	page_add_file_rmap(page);
2081238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
2082238f58d8SLinus Torvalds 
2083238f58d8SLinus Torvalds 	retval = 0;
20848a9f3ccdSBalbir Singh 	pte_unmap_unlock(pte, ptl);
20858a9f3ccdSBalbir Singh 	return retval;
2086238f58d8SLinus Torvalds out_unlock:
2087238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
2088238f58d8SLinus Torvalds out:
2089238f58d8SLinus Torvalds 	return retval;
2090238f58d8SLinus Torvalds }
2091238f58d8SLinus Torvalds 
2092bfa5bf6dSRolf Eike Beer /**
2093bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
2094bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
2095bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
2096bfa5bf6dSRolf Eike Beer  * @page: source kernel page
2097bfa5bf6dSRolf Eike Beer  *
2098a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
2099a145dd41SLinus Torvalds  * into a user vma.
2100a145dd41SLinus Torvalds  *
2101a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
2102a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
2103a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
21048dfcc9baSNick Piggin  * (see split_page()).
2105a145dd41SLinus Torvalds  *
2106a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
2107a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
2108a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
2109a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
2110a145dd41SLinus Torvalds  * ask for a shared writable mapping!
2111a145dd41SLinus Torvalds  *
2112a145dd41SLinus Torvalds  * The page does not need to be reserved.
21134b6e1e37SKonstantin Khlebnikov  *
21144b6e1e37SKonstantin Khlebnikov  * Usually this function is called from f_op->mmap() handler
21154b6e1e37SKonstantin Khlebnikov  * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
21164b6e1e37SKonstantin Khlebnikov  * Caller must set VM_MIXEDMAP on vma if it wants to call this
21174b6e1e37SKonstantin Khlebnikov  * function from other places, for example from page-fault handler.
2118a145dd41SLinus Torvalds  */
2119423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
2120423bad60SNick Piggin 			struct page *page)
2121a145dd41SLinus Torvalds {
2122a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
2123a145dd41SLinus Torvalds 		return -EFAULT;
2124a145dd41SLinus Torvalds 	if (!page_count(page))
2125a145dd41SLinus Torvalds 		return -EINVAL;
21264b6e1e37SKonstantin Khlebnikov 	if (!(vma->vm_flags & VM_MIXEDMAP)) {
21274b6e1e37SKonstantin Khlebnikov 		BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
21284b6e1e37SKonstantin Khlebnikov 		BUG_ON(vma->vm_flags & VM_PFNMAP);
21294b6e1e37SKonstantin Khlebnikov 		vma->vm_flags |= VM_MIXEDMAP;
21304b6e1e37SKonstantin Khlebnikov 	}
2131423bad60SNick Piggin 	return insert_page(vma, addr, page, vma->vm_page_prot);
2132a145dd41SLinus Torvalds }
2133e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
2134a145dd41SLinus Torvalds 
2135423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2136423bad60SNick Piggin 			unsigned long pfn, pgprot_t prot)
2137423bad60SNick Piggin {
2138423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
2139423bad60SNick Piggin 	int retval;
2140423bad60SNick Piggin 	pte_t *pte, entry;
2141423bad60SNick Piggin 	spinlock_t *ptl;
2142423bad60SNick Piggin 
2143423bad60SNick Piggin 	retval = -ENOMEM;
2144423bad60SNick Piggin 	pte = get_locked_pte(mm, addr, &ptl);
2145423bad60SNick Piggin 	if (!pte)
2146423bad60SNick Piggin 		goto out;
2147423bad60SNick Piggin 	retval = -EBUSY;
2148423bad60SNick Piggin 	if (!pte_none(*pte))
2149423bad60SNick Piggin 		goto out_unlock;
2150423bad60SNick Piggin 
2151423bad60SNick Piggin 	/* Ok, finally just insert the thing.. */
2152423bad60SNick Piggin 	entry = pte_mkspecial(pfn_pte(pfn, prot));
2153423bad60SNick Piggin 	set_pte_at(mm, addr, pte, entry);
21544b3073e1SRussell King 	update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
2155423bad60SNick Piggin 
2156423bad60SNick Piggin 	retval = 0;
2157423bad60SNick Piggin out_unlock:
2158423bad60SNick Piggin 	pte_unmap_unlock(pte, ptl);
2159423bad60SNick Piggin out:
2160423bad60SNick Piggin 	return retval;
2161423bad60SNick Piggin }
2162423bad60SNick Piggin 
2163e0dc0d8fSNick Piggin /**
2164e0dc0d8fSNick Piggin  * vm_insert_pfn - insert single pfn into user vma
2165e0dc0d8fSNick Piggin  * @vma: user vma to map to
2166e0dc0d8fSNick Piggin  * @addr: target user address of this page
2167e0dc0d8fSNick Piggin  * @pfn: source kernel pfn
2168e0dc0d8fSNick Piggin  *
2169c462f179SRobert P. J. Day  * Similar to vm_insert_page, this allows drivers to insert individual pages
2170e0dc0d8fSNick Piggin  * they've allocated into a user vma. Same comments apply.
2171e0dc0d8fSNick Piggin  *
2172e0dc0d8fSNick Piggin  * This function should only be called from a vm_ops->fault handler, and
2173e0dc0d8fSNick Piggin  * in that case the handler should return NULL.
21740d71d10aSNick Piggin  *
21750d71d10aSNick Piggin  * vma cannot be a COW mapping.
21760d71d10aSNick Piggin  *
21770d71d10aSNick Piggin  * As this is called only for pages that do not currently exist, we
21780d71d10aSNick Piggin  * do not need to flush old virtual caches or the TLB.
2179e0dc0d8fSNick Piggin  */
2180e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2181e0dc0d8fSNick Piggin 			unsigned long pfn)
2182e0dc0d8fSNick Piggin {
21832ab64037Svenkatesh.pallipadi@intel.com 	int ret;
2184e4b866edSvenkatesh.pallipadi@intel.com 	pgprot_t pgprot = vma->vm_page_prot;
21857e675137SNick Piggin 	/*
21867e675137SNick Piggin 	 * Technically, architectures with pte_special can avoid all these
21877e675137SNick Piggin 	 * restrictions (same for remap_pfn_range).  However we would like
21887e675137SNick Piggin 	 * consistency in testing and feature parity among all, so we should
21897e675137SNick Piggin 	 * try to keep these invariants in place for everybody.
21907e675137SNick Piggin 	 */
2191b379d790SJared Hulbert 	BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
2192b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
2193b379d790SJared Hulbert 						(VM_PFNMAP|VM_MIXEDMAP));
2194b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
2195b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
2196e0dc0d8fSNick Piggin 
2197423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
2198423bad60SNick Piggin 		return -EFAULT;
21995180da41SSuresh Siddha 	if (track_pfn_insert(vma, &pgprot, pfn))
22002ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
22012ab64037Svenkatesh.pallipadi@intel.com 
2202e4b866edSvenkatesh.pallipadi@intel.com 	ret = insert_pfn(vma, addr, pfn, pgprot);
22032ab64037Svenkatesh.pallipadi@intel.com 
22042ab64037Svenkatesh.pallipadi@intel.com 	return ret;
2205e0dc0d8fSNick Piggin }
2206e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn);
2207e0dc0d8fSNick Piggin 
2208423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2209423bad60SNick Piggin 			unsigned long pfn)
2210423bad60SNick Piggin {
2211423bad60SNick Piggin 	BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
2212423bad60SNick Piggin 
2213423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
2214423bad60SNick Piggin 		return -EFAULT;
2215423bad60SNick Piggin 
2216423bad60SNick Piggin 	/*
2217423bad60SNick Piggin 	 * If we don't have pte special, then we have to use the pfn_valid()
2218423bad60SNick Piggin 	 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
2219423bad60SNick Piggin 	 * refcount the page if pfn_valid is true (hence insert_page rather
222062eede62SHugh Dickins 	 * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
222162eede62SHugh Dickins 	 * without pte special, it would there be refcounted as a normal page.
2222423bad60SNick Piggin 	 */
2223423bad60SNick Piggin 	if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
2224423bad60SNick Piggin 		struct page *page;
2225423bad60SNick Piggin 
2226423bad60SNick Piggin 		page = pfn_to_page(pfn);
2227423bad60SNick Piggin 		return insert_page(vma, addr, page, vma->vm_page_prot);
2228423bad60SNick Piggin 	}
2229423bad60SNick Piggin 	return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
2230423bad60SNick Piggin }
2231423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed);
2232423bad60SNick Piggin 
2233a145dd41SLinus Torvalds /*
22341da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
22351da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
22361da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
22371da177e4SLinus Torvalds  */
22381da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
22391da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
22401da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
22411da177e4SLinus Torvalds {
22421da177e4SLinus Torvalds 	pte_t *pte;
2243c74df32cSHugh Dickins 	spinlock_t *ptl;
22441da177e4SLinus Torvalds 
2245c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
22461da177e4SLinus Torvalds 	if (!pte)
22471da177e4SLinus Torvalds 		return -ENOMEM;
22486606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
22491da177e4SLinus Torvalds 	do {
22501da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
22517e675137SNick Piggin 		set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
22521da177e4SLinus Torvalds 		pfn++;
22531da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
22546606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
2255c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
22561da177e4SLinus Torvalds 	return 0;
22571da177e4SLinus Torvalds }
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
22601da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
22611da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
22621da177e4SLinus Torvalds {
22631da177e4SLinus Torvalds 	pmd_t *pmd;
22641da177e4SLinus Torvalds 	unsigned long next;
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
22671da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
22681da177e4SLinus Torvalds 	if (!pmd)
22691da177e4SLinus Torvalds 		return -ENOMEM;
2270f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
22711da177e4SLinus Torvalds 	do {
22721da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
22731da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
22741da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
22751da177e4SLinus Torvalds 			return -ENOMEM;
22761da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
22771da177e4SLinus Torvalds 	return 0;
22781da177e4SLinus Torvalds }
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
22811da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
22821da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
22831da177e4SLinus Torvalds {
22841da177e4SLinus Torvalds 	pud_t *pud;
22851da177e4SLinus Torvalds 	unsigned long next;
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
22881da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
22891da177e4SLinus Torvalds 	if (!pud)
22901da177e4SLinus Torvalds 		return -ENOMEM;
22911da177e4SLinus Torvalds 	do {
22921da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
22931da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
22941da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
22951da177e4SLinus Torvalds 			return -ENOMEM;
22961da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
22971da177e4SLinus Torvalds 	return 0;
22981da177e4SLinus Torvalds }
22991da177e4SLinus Torvalds 
2300bfa5bf6dSRolf Eike Beer /**
2301bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
2302bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
2303bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
2304bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
2305bfa5bf6dSRolf Eike Beer  * @size: size of map area
2306bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
2307bfa5bf6dSRolf Eike Beer  *
2308bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
2309bfa5bf6dSRolf Eike Beer  */
23101da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
23111da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
23121da177e4SLinus Torvalds {
23131da177e4SLinus Torvalds 	pgd_t *pgd;
23141da177e4SLinus Torvalds 	unsigned long next;
23152d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
23161da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
23171da177e4SLinus Torvalds 	int err;
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds 	/*
23201da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
23211da177e4SLinus Torvalds 	 * rest of the world about it:
23221da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
23231da177e4SLinus Torvalds 	 *	(accesses can have side effects).
23246aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
23256aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
23266aab341eSLinus Torvalds 	 *	with them.
2327314e51b9SKonstantin Khlebnikov 	 *   VM_DONTEXPAND
2328314e51b9SKonstantin Khlebnikov 	 *      Disable vma merging and expanding with mremap().
2329314e51b9SKonstantin Khlebnikov 	 *   VM_DONTDUMP
2330314e51b9SKonstantin Khlebnikov 	 *      Omit vma from core dump, even when VM_IO turned off.
2331fb155c16SLinus Torvalds 	 *
2332fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
2333fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
2334fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
2335b3b9c293SKonstantin Khlebnikov 	 * See vm_normal_page() for details.
23361da177e4SLinus Torvalds 	 */
2337b3b9c293SKonstantin Khlebnikov 	if (is_cow_mapping(vma->vm_flags)) {
2338b3b9c293SKonstantin Khlebnikov 		if (addr != vma->vm_start || end != vma->vm_end)
2339b3b9c293SKonstantin Khlebnikov 			return -EINVAL;
23406aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
2341b3b9c293SKonstantin Khlebnikov 	}
2342b3b9c293SKonstantin Khlebnikov 
2343b3b9c293SKonstantin Khlebnikov 	err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
2344b3b9c293SKonstantin Khlebnikov 	if (err)
23453c8bb73aSvenkatesh.pallipadi@intel.com 		return -EINVAL;
2346fb155c16SLinus Torvalds 
2347314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 	BUG_ON(addr >= end);
23501da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
23511da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
23521da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
23531da177e4SLinus Torvalds 	do {
23541da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
23551da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
23561da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
23571da177e4SLinus Torvalds 		if (err)
23581da177e4SLinus Torvalds 			break;
23591da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
23602ab64037Svenkatesh.pallipadi@intel.com 
23612ab64037Svenkatesh.pallipadi@intel.com 	if (err)
23625180da41SSuresh Siddha 		untrack_pfn(vma, pfn, PAGE_ALIGN(size));
23632ab64037Svenkatesh.pallipadi@intel.com 
23641da177e4SLinus Torvalds 	return err;
23651da177e4SLinus Torvalds }
23661da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
23671da177e4SLinus Torvalds 
2368aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2369aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2370aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2371aee16b3cSJeremy Fitzhardinge {
2372aee16b3cSJeremy Fitzhardinge 	pte_t *pte;
2373aee16b3cSJeremy Fitzhardinge 	int err;
23742f569afdSMartin Schwidefsky 	pgtable_t token;
237594909914SBorislav Petkov 	spinlock_t *uninitialized_var(ptl);
2376aee16b3cSJeremy Fitzhardinge 
2377aee16b3cSJeremy Fitzhardinge 	pte = (mm == &init_mm) ?
2378aee16b3cSJeremy Fitzhardinge 		pte_alloc_kernel(pmd, addr) :
2379aee16b3cSJeremy Fitzhardinge 		pte_alloc_map_lock(mm, pmd, addr, &ptl);
2380aee16b3cSJeremy Fitzhardinge 	if (!pte)
2381aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2382aee16b3cSJeremy Fitzhardinge 
2383aee16b3cSJeremy Fitzhardinge 	BUG_ON(pmd_huge(*pmd));
2384aee16b3cSJeremy Fitzhardinge 
238538e0edb1SJeremy Fitzhardinge 	arch_enter_lazy_mmu_mode();
238638e0edb1SJeremy Fitzhardinge 
23872f569afdSMartin Schwidefsky 	token = pmd_pgtable(*pmd);
2388aee16b3cSJeremy Fitzhardinge 
2389aee16b3cSJeremy Fitzhardinge 	do {
2390c36987e2SDaisuke Nishimura 		err = fn(pte++, token, addr, data);
2391aee16b3cSJeremy Fitzhardinge 		if (err)
2392aee16b3cSJeremy Fitzhardinge 			break;
2393c36987e2SDaisuke Nishimura 	} while (addr += PAGE_SIZE, addr != end);
2394aee16b3cSJeremy Fitzhardinge 
239538e0edb1SJeremy Fitzhardinge 	arch_leave_lazy_mmu_mode();
239638e0edb1SJeremy Fitzhardinge 
2397aee16b3cSJeremy Fitzhardinge 	if (mm != &init_mm)
2398aee16b3cSJeremy Fitzhardinge 		pte_unmap_unlock(pte-1, ptl);
2399aee16b3cSJeremy Fitzhardinge 	return err;
2400aee16b3cSJeremy Fitzhardinge }
2401aee16b3cSJeremy Fitzhardinge 
2402aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2403aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2404aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2405aee16b3cSJeremy Fitzhardinge {
2406aee16b3cSJeremy Fitzhardinge 	pmd_t *pmd;
2407aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2408aee16b3cSJeremy Fitzhardinge 	int err;
2409aee16b3cSJeremy Fitzhardinge 
2410ceb86879SAndi Kleen 	BUG_ON(pud_huge(*pud));
2411ceb86879SAndi Kleen 
2412aee16b3cSJeremy Fitzhardinge 	pmd = pmd_alloc(mm, pud, addr);
2413aee16b3cSJeremy Fitzhardinge 	if (!pmd)
2414aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2415aee16b3cSJeremy Fitzhardinge 	do {
2416aee16b3cSJeremy Fitzhardinge 		next = pmd_addr_end(addr, end);
2417aee16b3cSJeremy Fitzhardinge 		err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2418aee16b3cSJeremy Fitzhardinge 		if (err)
2419aee16b3cSJeremy Fitzhardinge 			break;
2420aee16b3cSJeremy Fitzhardinge 	} while (pmd++, addr = next, addr != end);
2421aee16b3cSJeremy Fitzhardinge 	return err;
2422aee16b3cSJeremy Fitzhardinge }
2423aee16b3cSJeremy Fitzhardinge 
2424aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
2425aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2426aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2427aee16b3cSJeremy Fitzhardinge {
2428aee16b3cSJeremy Fitzhardinge 	pud_t *pud;
2429aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2430aee16b3cSJeremy Fitzhardinge 	int err;
2431aee16b3cSJeremy Fitzhardinge 
2432aee16b3cSJeremy Fitzhardinge 	pud = pud_alloc(mm, pgd, addr);
2433aee16b3cSJeremy Fitzhardinge 	if (!pud)
2434aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2435aee16b3cSJeremy Fitzhardinge 	do {
2436aee16b3cSJeremy Fitzhardinge 		next = pud_addr_end(addr, end);
2437aee16b3cSJeremy Fitzhardinge 		err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2438aee16b3cSJeremy Fitzhardinge 		if (err)
2439aee16b3cSJeremy Fitzhardinge 			break;
2440aee16b3cSJeremy Fitzhardinge 	} while (pud++, addr = next, addr != end);
2441aee16b3cSJeremy Fitzhardinge 	return err;
2442aee16b3cSJeremy Fitzhardinge }
2443aee16b3cSJeremy Fitzhardinge 
2444aee16b3cSJeremy Fitzhardinge /*
2445aee16b3cSJeremy Fitzhardinge  * Scan a region of virtual memory, filling in page tables as necessary
2446aee16b3cSJeremy Fitzhardinge  * and calling a provided function on each leaf page table.
2447aee16b3cSJeremy Fitzhardinge  */
2448aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2449aee16b3cSJeremy Fitzhardinge 			unsigned long size, pte_fn_t fn, void *data)
2450aee16b3cSJeremy Fitzhardinge {
2451aee16b3cSJeremy Fitzhardinge 	pgd_t *pgd;
2452aee16b3cSJeremy Fitzhardinge 	unsigned long next;
245357250a5bSJeremy Fitzhardinge 	unsigned long end = addr + size;
2454aee16b3cSJeremy Fitzhardinge 	int err;
2455aee16b3cSJeremy Fitzhardinge 
2456aee16b3cSJeremy Fitzhardinge 	BUG_ON(addr >= end);
2457aee16b3cSJeremy Fitzhardinge 	pgd = pgd_offset(mm, addr);
2458aee16b3cSJeremy Fitzhardinge 	do {
2459aee16b3cSJeremy Fitzhardinge 		next = pgd_addr_end(addr, end);
2460aee16b3cSJeremy Fitzhardinge 		err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
2461aee16b3cSJeremy Fitzhardinge 		if (err)
2462aee16b3cSJeremy Fitzhardinge 			break;
2463aee16b3cSJeremy Fitzhardinge 	} while (pgd++, addr = next, addr != end);
246457250a5bSJeremy Fitzhardinge 
2465aee16b3cSJeremy Fitzhardinge 	return err;
2466aee16b3cSJeremy Fitzhardinge }
2467aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range);
2468aee16b3cSJeremy Fitzhardinge 
24691da177e4SLinus Torvalds /*
24708f4e2101SHugh Dickins  * handle_pte_fault chooses page fault handler according to an entry
24718f4e2101SHugh Dickins  * which was read non-atomically.  Before making any commitment, on
24728f4e2101SHugh Dickins  * those architectures or configurations (e.g. i386 with PAE) which
2473a335b2e1SRyota Ozaki  * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault
24748f4e2101SHugh Dickins  * must check under lock before unmapping the pte and proceeding
24758f4e2101SHugh Dickins  * (but do_wp_page is only called after already making such a check;
2476a335b2e1SRyota Ozaki  * and do_anonymous_page can safely check later on).
24778f4e2101SHugh Dickins  */
24784c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
24798f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
24808f4e2101SHugh Dickins {
24818f4e2101SHugh Dickins 	int same = 1;
24828f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
24838f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
24844c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
24854c21e2f2SHugh Dickins 		spin_lock(ptl);
24868f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
24874c21e2f2SHugh Dickins 		spin_unlock(ptl);
24888f4e2101SHugh Dickins 	}
24898f4e2101SHugh Dickins #endif
24908f4e2101SHugh Dickins 	pte_unmap(page_table);
24918f4e2101SHugh Dickins 	return same;
24928f4e2101SHugh Dickins }
24938f4e2101SHugh Dickins 
24949de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
24956aab341eSLinus Torvalds {
24966aab341eSLinus Torvalds 	/*
24976aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
24986aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
24996aab341eSLinus Torvalds 	 * just copying from the original user address. If that
25006aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
25016aab341eSLinus Torvalds 	 */
25026aab341eSLinus Torvalds 	if (unlikely(!src)) {
25039b04c5feSCong Wang 		void *kaddr = kmap_atomic(dst);
25045d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
25055d2a2dbbSLinus Torvalds 
25065d2a2dbbSLinus Torvalds 		/*
25075d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
25085d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
25095d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
25105d2a2dbbSLinus Torvalds 		 * zeroes.
25115d2a2dbbSLinus Torvalds 		 */
25125d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
25133ecb01dfSJan Beulich 			clear_page(kaddr);
25149b04c5feSCong Wang 		kunmap_atomic(kaddr);
2515c4ec7b0dSDmitriy Monakhov 		flush_dcache_page(dst);
25160ed361deSNick Piggin 	} else
25179de455b2SAtsushi Nemoto 		copy_user_highpage(dst, src, va, vma);
25186aab341eSLinus Torvalds }
25196aab341eSLinus Torvalds 
25201da177e4SLinus Torvalds /*
25211da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
25221da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
25231da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
25241da177e4SLinus Torvalds  *
25251da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
25261da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
25271da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
25281da177e4SLinus Torvalds  * COW.
25291da177e4SLinus Torvalds  *
25301da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
25311da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
25321da177e4SLinus Torvalds  * and potentially makes it more efficient.
25331da177e4SLinus Torvalds  *
25348f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
25358f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
25368f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
25371da177e4SLinus Torvalds  */
25381da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
253965500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
25408f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
2541e6219ec8SNamhyung Kim 	__releases(ptl)
25421da177e4SLinus Torvalds {
25432ec74c3eSSagi Grimberg 	struct page *old_page, *new_page = NULL;
25441da177e4SLinus Torvalds 	pte_t entry;
2545b009c024SMichel Lespinasse 	int ret = 0;
2546a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
2547d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
25481756954cSDavid Rientjes 	unsigned long mmun_start = 0;	/* For mmu_notifiers */
25491756954cSDavid Rientjes 	unsigned long mmun_end = 0;	/* For mmu_notifiers */
25501da177e4SLinus Torvalds 
25516aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
2552251b97f5SPeter Zijlstra 	if (!old_page) {
2553251b97f5SPeter Zijlstra 		/*
2554251b97f5SPeter Zijlstra 		 * VM_MIXEDMAP !pfn_valid() case
2555251b97f5SPeter Zijlstra 		 *
2556251b97f5SPeter Zijlstra 		 * We should not cow pages in a shared writeable mapping.
2557251b97f5SPeter Zijlstra 		 * Just mark the pages writable as we can't do any dirty
2558251b97f5SPeter Zijlstra 		 * accounting on raw pfn maps.
2559251b97f5SPeter Zijlstra 		 */
2560251b97f5SPeter Zijlstra 		if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2561251b97f5SPeter Zijlstra 				     (VM_WRITE|VM_SHARED))
2562251b97f5SPeter Zijlstra 			goto reuse;
2563920fc356SHugh Dickins 		goto gotten;
2564251b97f5SPeter Zijlstra 	}
25651da177e4SLinus Torvalds 
2566d08b3851SPeter Zijlstra 	/*
2567ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
2568ee6a6457SPeter Zijlstra 	 * not dirty accountable.
2569d08b3851SPeter Zijlstra 	 */
25709a840895SHugh Dickins 	if (PageAnon(old_page) && !PageKsm(old_page)) {
2571ab967d86SHugh Dickins 		if (!trylock_page(old_page)) {
2572ab967d86SHugh Dickins 			page_cache_get(old_page);
2573ab967d86SHugh Dickins 			pte_unmap_unlock(page_table, ptl);
2574ab967d86SHugh Dickins 			lock_page(old_page);
2575ab967d86SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address,
2576ab967d86SHugh Dickins 							 &ptl);
2577ab967d86SHugh Dickins 			if (!pte_same(*page_table, orig_pte)) {
2578ab967d86SHugh Dickins 				unlock_page(old_page);
2579ab967d86SHugh Dickins 				goto unlock;
2580ab967d86SHugh Dickins 			}
2581ab967d86SHugh Dickins 			page_cache_release(old_page);
2582ab967d86SHugh Dickins 		}
2583b009c024SMichel Lespinasse 		if (reuse_swap_page(old_page)) {
2584c44b6743SRik van Riel 			/*
2585c44b6743SRik van Riel 			 * The page is all ours.  Move it to our anon_vma so
2586c44b6743SRik van Riel 			 * the rmap code will not search our parent or siblings.
2587c44b6743SRik van Riel 			 * Protected against the rmap code by the page lock.
2588c44b6743SRik van Riel 			 */
2589c44b6743SRik van Riel 			page_move_anon_rmap(old_page, vma, address);
2590ee6a6457SPeter Zijlstra 			unlock_page(old_page);
2591b009c024SMichel Lespinasse 			goto reuse;
2592b009c024SMichel Lespinasse 		}
2593b009c024SMichel Lespinasse 		unlock_page(old_page);
2594ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2595d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
2596ee6a6457SPeter Zijlstra 		/*
2597ee6a6457SPeter Zijlstra 		 * Only catch write-faults on shared writable pages,
2598ee6a6457SPeter Zijlstra 		 * read-only shared pages can get COWed by
2599ee6a6457SPeter Zijlstra 		 * get_user_pages(.write=1, .force=1).
2600ee6a6457SPeter Zijlstra 		 */
26019637a5efSDavid Howells 		if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2602c2ec175cSNick Piggin 			struct vm_fault vmf;
2603c2ec175cSNick Piggin 			int tmp;
2604c2ec175cSNick Piggin 
2605c2ec175cSNick Piggin 			vmf.virtual_address = (void __user *)(address &
2606c2ec175cSNick Piggin 								PAGE_MASK);
2607c2ec175cSNick Piggin 			vmf.pgoff = old_page->index;
2608c2ec175cSNick Piggin 			vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
2609c2ec175cSNick Piggin 			vmf.page = old_page;
2610c2ec175cSNick Piggin 
26119637a5efSDavid Howells 			/*
26129637a5efSDavid Howells 			 * Notify the address space that the page is about to
26139637a5efSDavid Howells 			 * become writable so that it can prohibit this or wait
26149637a5efSDavid Howells 			 * for the page to get into an appropriate state.
26159637a5efSDavid Howells 			 *
26169637a5efSDavid Howells 			 * We do this without the lock held, so that it can
26179637a5efSDavid Howells 			 * sleep if it needs to.
26189637a5efSDavid Howells 			 */
26199637a5efSDavid Howells 			page_cache_get(old_page);
26209637a5efSDavid Howells 			pte_unmap_unlock(page_table, ptl);
26219637a5efSDavid Howells 
2622c2ec175cSNick Piggin 			tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
2623c2ec175cSNick Piggin 			if (unlikely(tmp &
2624c2ec175cSNick Piggin 					(VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
2625c2ec175cSNick Piggin 				ret = tmp;
26269637a5efSDavid Howells 				goto unwritable_page;
2627c2ec175cSNick Piggin 			}
2628b827e496SNick Piggin 			if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
2629b827e496SNick Piggin 				lock_page(old_page);
2630b827e496SNick Piggin 				if (!old_page->mapping) {
2631b827e496SNick Piggin 					ret = 0; /* retry the fault */
2632b827e496SNick Piggin 					unlock_page(old_page);
2633b827e496SNick Piggin 					goto unwritable_page;
2634b827e496SNick Piggin 				}
2635b827e496SNick Piggin 			} else
2636b827e496SNick Piggin 				VM_BUG_ON(!PageLocked(old_page));
26379637a5efSDavid Howells 
26389637a5efSDavid Howells 			/*
26399637a5efSDavid Howells 			 * Since we dropped the lock we need to revalidate
26409637a5efSDavid Howells 			 * the PTE as someone else may have changed it.  If
26419637a5efSDavid Howells 			 * they did, we just return, as we can count on the
26429637a5efSDavid Howells 			 * MMU to tell us if they didn't also make it writable.
26439637a5efSDavid Howells 			 */
26449637a5efSDavid Howells 			page_table = pte_offset_map_lock(mm, pmd, address,
26459637a5efSDavid Howells 							 &ptl);
2646b827e496SNick Piggin 			if (!pte_same(*page_table, orig_pte)) {
2647b827e496SNick Piggin 				unlock_page(old_page);
26489637a5efSDavid Howells 				goto unlock;
2649b827e496SNick Piggin 			}
2650a200ee18SPeter Zijlstra 
2651a200ee18SPeter Zijlstra 			page_mkwrite = 1;
26529637a5efSDavid Howells 		}
2653d08b3851SPeter Zijlstra 		dirty_page = old_page;
2654d08b3851SPeter Zijlstra 		get_page(dirty_page);
26559637a5efSDavid Howells 
2656251b97f5SPeter Zijlstra reuse:
2657eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
265865500d23SHugh Dickins 		entry = pte_mkyoung(orig_pte);
265965500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2660954ffcb3SKAMEZAWA Hiroyuki 		if (ptep_set_access_flags(vma, address, page_table, entry,1))
26614b3073e1SRussell King 			update_mmu_cache(vma, address, page_table);
266272ddc8f7SMichel Lespinasse 		pte_unmap_unlock(page_table, ptl);
266365500d23SHugh Dickins 		ret |= VM_FAULT_WRITE;
266472ddc8f7SMichel Lespinasse 
266572ddc8f7SMichel Lespinasse 		if (!dirty_page)
266672ddc8f7SMichel Lespinasse 			return ret;
266772ddc8f7SMichel Lespinasse 
266872ddc8f7SMichel Lespinasse 		/*
266972ddc8f7SMichel Lespinasse 		 * Yes, Virginia, this is actually required to prevent a race
267072ddc8f7SMichel Lespinasse 		 * with clear_page_dirty_for_io() from clearing the page dirty
267172ddc8f7SMichel Lespinasse 		 * bit after it clear all dirty ptes, but before a racing
267272ddc8f7SMichel Lespinasse 		 * do_wp_page installs a dirty pte.
267372ddc8f7SMichel Lespinasse 		 *
2674a335b2e1SRyota Ozaki 		 * __do_fault is protected similarly.
267572ddc8f7SMichel Lespinasse 		 */
267672ddc8f7SMichel Lespinasse 		if (!page_mkwrite) {
267772ddc8f7SMichel Lespinasse 			wait_on_page_locked(dirty_page);
267872ddc8f7SMichel Lespinasse 			set_page_dirty_balance(dirty_page, page_mkwrite);
267941c4d25fSJan Kara 			/* file_update_time outside page_lock */
268041c4d25fSJan Kara 			if (vma->vm_file)
268141c4d25fSJan Kara 				file_update_time(vma->vm_file);
268272ddc8f7SMichel Lespinasse 		}
268372ddc8f7SMichel Lespinasse 		put_page(dirty_page);
268472ddc8f7SMichel Lespinasse 		if (page_mkwrite) {
268572ddc8f7SMichel Lespinasse 			struct address_space *mapping = dirty_page->mapping;
268672ddc8f7SMichel Lespinasse 
268772ddc8f7SMichel Lespinasse 			set_page_dirty(dirty_page);
268872ddc8f7SMichel Lespinasse 			unlock_page(dirty_page);
268972ddc8f7SMichel Lespinasse 			page_cache_release(dirty_page);
269072ddc8f7SMichel Lespinasse 			if (mapping)	{
269172ddc8f7SMichel Lespinasse 				/*
269272ddc8f7SMichel Lespinasse 				 * Some device drivers do not set page.mapping
269372ddc8f7SMichel Lespinasse 				 * but still dirty their pages
269472ddc8f7SMichel Lespinasse 				 */
269572ddc8f7SMichel Lespinasse 				balance_dirty_pages_ratelimited(mapping);
269672ddc8f7SMichel Lespinasse 			}
269772ddc8f7SMichel Lespinasse 		}
269872ddc8f7SMichel Lespinasse 
269972ddc8f7SMichel Lespinasse 		return ret;
27001da177e4SLinus Torvalds 	}
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	/*
27031da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
27041da177e4SLinus Torvalds 	 */
27051da177e4SLinus Torvalds 	page_cache_get(old_page);
2706920fc356SHugh Dickins gotten:
27078f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
27081da177e4SLinus Torvalds 
27091da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
271065500d23SHugh Dickins 		goto oom;
2711a13ea5b7SHugh Dickins 
271262eede62SHugh Dickins 	if (is_zero_pfn(pte_pfn(orig_pte))) {
2713a13ea5b7SHugh Dickins 		new_page = alloc_zeroed_user_highpage_movable(vma, address);
2714a13ea5b7SHugh Dickins 		if (!new_page)
2715a13ea5b7SHugh Dickins 			goto oom;
2716a13ea5b7SHugh Dickins 	} else {
2717769848c0SMel Gorman 		new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
27181da177e4SLinus Torvalds 		if (!new_page)
271965500d23SHugh Dickins 			goto oom;
2720a13ea5b7SHugh Dickins 		cow_user_page(new_page, old_page, address, vma);
2721a13ea5b7SHugh Dickins 	}
2722a13ea5b7SHugh Dickins 	__SetPageUptodate(new_page);
2723a13ea5b7SHugh Dickins 
27242c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))
27258a9f3ccdSBalbir Singh 		goto oom_free_new;
27268a9f3ccdSBalbir Singh 
27276bdb913fSHaggai Eran 	mmun_start  = address & PAGE_MASK;
27281756954cSDavid Rientjes 	mmun_end    = mmun_start + PAGE_SIZE;
27296bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
27306bdb913fSHaggai Eran 
27311da177e4SLinus Torvalds 	/*
27321da177e4SLinus Torvalds 	 * Re-check the pte - we dropped the lock
27331da177e4SLinus Torvalds 	 */
27348f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
273565500d23SHugh Dickins 	if (likely(pte_same(*page_table, orig_pte))) {
2736920fc356SHugh Dickins 		if (old_page) {
27374294621fSHugh Dickins 			if (!PageAnon(old_page)) {
273834e55232SKAMEZAWA Hiroyuki 				dec_mm_counter_fast(mm, MM_FILEPAGES);
273934e55232SKAMEZAWA Hiroyuki 				inc_mm_counter_fast(mm, MM_ANONPAGES);
27404294621fSHugh Dickins 			}
2741920fc356SHugh Dickins 		} else
274234e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
2743eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
274465500d23SHugh Dickins 		entry = mk_pte(new_page, vma->vm_page_prot);
274565500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
27464ce072f1SSiddha, Suresh B 		/*
27474ce072f1SSiddha, Suresh B 		 * Clear the pte entry and flush it first, before updating the
27484ce072f1SSiddha, Suresh B 		 * pte with the new entry. This will avoid a race condition
27494ce072f1SSiddha, Suresh B 		 * seen in the presence of one thread doing SMC and another
27504ce072f1SSiddha, Suresh B 		 * thread doing COW.
27514ce072f1SSiddha, Suresh B 		 */
2752828502d3SIzik Eidus 		ptep_clear_flush(vma, address, page_table);
27539617d95eSNick Piggin 		page_add_new_anon_rmap(new_page, vma, address);
2754828502d3SIzik Eidus 		/*
2755828502d3SIzik Eidus 		 * We call the notify macro here because, when using secondary
2756828502d3SIzik Eidus 		 * mmu page tables (such as kvm shadow page tables), we want the
2757828502d3SIzik Eidus 		 * new page to be mapped directly into the secondary page table.
2758828502d3SIzik Eidus 		 */
2759828502d3SIzik Eidus 		set_pte_at_notify(mm, address, page_table, entry);
27604b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
2761945754a1SNick Piggin 		if (old_page) {
2762945754a1SNick Piggin 			/*
2763945754a1SNick Piggin 			 * Only after switching the pte to the new page may
2764945754a1SNick Piggin 			 * we remove the mapcount here. Otherwise another
2765945754a1SNick Piggin 			 * process may come and find the rmap count decremented
2766945754a1SNick Piggin 			 * before the pte is switched to the new page, and
2767945754a1SNick Piggin 			 * "reuse" the old page writing into it while our pte
2768945754a1SNick Piggin 			 * here still points into it and can be read by other
2769945754a1SNick Piggin 			 * threads.
2770945754a1SNick Piggin 			 *
2771945754a1SNick Piggin 			 * The critical issue is to order this
2772945754a1SNick Piggin 			 * page_remove_rmap with the ptp_clear_flush above.
2773945754a1SNick Piggin 			 * Those stores are ordered by (if nothing else,)
2774945754a1SNick Piggin 			 * the barrier present in the atomic_add_negative
2775945754a1SNick Piggin 			 * in page_remove_rmap.
2776945754a1SNick Piggin 			 *
2777945754a1SNick Piggin 			 * Then the TLB flush in ptep_clear_flush ensures that
2778945754a1SNick Piggin 			 * no process can access the old page before the
2779945754a1SNick Piggin 			 * decremented mapcount is visible. And the old page
2780945754a1SNick Piggin 			 * cannot be reused until after the decremented
2781945754a1SNick Piggin 			 * mapcount is visible. So transitively, TLBs to
2782945754a1SNick Piggin 			 * old page will be flushed before it can be reused.
2783945754a1SNick Piggin 			 */
2784edc315fdSHugh Dickins 			page_remove_rmap(old_page);
2785945754a1SNick Piggin 		}
2786945754a1SNick Piggin 
27871da177e4SLinus Torvalds 		/* Free the old page.. */
27881da177e4SLinus Torvalds 		new_page = old_page;
2789f33ea7f4SNick Piggin 		ret |= VM_FAULT_WRITE;
27908a9f3ccdSBalbir Singh 	} else
27918a9f3ccdSBalbir Singh 		mem_cgroup_uncharge_page(new_page);
27928a9f3ccdSBalbir Singh 
27936bdb913fSHaggai Eran 	if (new_page)
27946bdb913fSHaggai Eran 		page_cache_release(new_page);
279565500d23SHugh Dickins unlock:
27968f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
27971756954cSDavid Rientjes 	if (mmun_end > mmun_start)
27986bdb913fSHaggai Eran 		mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2799e15f8c01SMichel Lespinasse 	if (old_page) {
2800e15f8c01SMichel Lespinasse 		/*
2801e15f8c01SMichel Lespinasse 		 * Don't let another task, with possibly unlocked vma,
2802e15f8c01SMichel Lespinasse 		 * keep the mlocked page.
2803e15f8c01SMichel Lespinasse 		 */
2804e15f8c01SMichel Lespinasse 		if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) {
2805e15f8c01SMichel Lespinasse 			lock_page(old_page);	/* LRU manipulation */
2806e15f8c01SMichel Lespinasse 			munlock_vma_page(old_page);
2807e15f8c01SMichel Lespinasse 			unlock_page(old_page);
2808e15f8c01SMichel Lespinasse 		}
2809e15f8c01SMichel Lespinasse 		page_cache_release(old_page);
2810e15f8c01SMichel Lespinasse 	}
2811f33ea7f4SNick Piggin 	return ret;
28128a9f3ccdSBalbir Singh oom_free_new:
28136dbf6d3bSHugh Dickins 	page_cache_release(new_page);
281465500d23SHugh Dickins oom:
2815b827e496SNick Piggin 	if (old_page) {
2816b827e496SNick Piggin 		if (page_mkwrite) {
2817b827e496SNick Piggin 			unlock_page(old_page);
28181da177e4SLinus Torvalds 			page_cache_release(old_page);
2819b827e496SNick Piggin 		}
2820b827e496SNick Piggin 		page_cache_release(old_page);
2821b827e496SNick Piggin 	}
28221da177e4SLinus Torvalds 	return VM_FAULT_OOM;
28239637a5efSDavid Howells 
28249637a5efSDavid Howells unwritable_page:
28259637a5efSDavid Howells 	page_cache_release(old_page);
2826c2ec175cSNick Piggin 	return ret;
28271da177e4SLinus Torvalds }
28281da177e4SLinus Torvalds 
282997a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma,
28301da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
28311da177e4SLinus Torvalds 		struct zap_details *details)
28321da177e4SLinus Torvalds {
2833f5cc4eefSAl Viro 	zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
28341da177e4SLinus Torvalds }
28351da177e4SLinus Torvalds 
28366b2dbba8SMichel Lespinasse static inline void unmap_mapping_range_tree(struct rb_root *root,
28371da177e4SLinus Torvalds 					    struct zap_details *details)
28381da177e4SLinus Torvalds {
28391da177e4SLinus Torvalds 	struct vm_area_struct *vma;
28401da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
28411da177e4SLinus Torvalds 
28426b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, root,
28431da177e4SLinus Torvalds 			details->first_index, details->last_index) {
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
28461da177e4SLinus Torvalds 		vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
28471da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
28481da177e4SLinus Torvalds 		zba = details->first_index;
28491da177e4SLinus Torvalds 		if (zba < vba)
28501da177e4SLinus Torvalds 			zba = vba;
28511da177e4SLinus Torvalds 		zea = details->last_index;
28521da177e4SLinus Torvalds 		if (zea > vea)
28531da177e4SLinus Torvalds 			zea = vea;
28541da177e4SLinus Torvalds 
285597a89413SPeter Zijlstra 		unmap_mapping_range_vma(vma,
28561da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
28571da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
285897a89413SPeter Zijlstra 				details);
28591da177e4SLinus Torvalds 	}
28601da177e4SLinus Torvalds }
28611da177e4SLinus Torvalds 
28621da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head,
28631da177e4SLinus Torvalds 					    struct zap_details *details)
28641da177e4SLinus Torvalds {
28651da177e4SLinus Torvalds 	struct vm_area_struct *vma;
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds 	/*
28681da177e4SLinus Torvalds 	 * In nonlinear VMAs there is no correspondence between virtual address
28691da177e4SLinus Torvalds 	 * offset and file offset.  So we must perform an exhaustive search
28701da177e4SLinus Torvalds 	 * across *all* the pages in each nonlinear VMA, not just the pages
28711da177e4SLinus Torvalds 	 * whose virtual address lies outside the file truncation point.
28721da177e4SLinus Torvalds 	 */
28736b2dbba8SMichel Lespinasse 	list_for_each_entry(vma, head, shared.nonlinear) {
28741da177e4SLinus Torvalds 		details->nonlinear_vma = vma;
287597a89413SPeter Zijlstra 		unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details);
28761da177e4SLinus Torvalds 	}
28771da177e4SLinus Torvalds }
28781da177e4SLinus Torvalds 
28791da177e4SLinus Torvalds /**
288072fd4a35SRobert P. J. Day  * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
28813d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
28821da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
28831da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
288425d9e2d1Snpiggin@suse.de  * boundary.  Note that this is different from truncate_pagecache(), which
28851da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
28861da177e4SLinus Torvalds  * partial pages.
28871da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
28881da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
28891da177e4SLinus Torvalds  * end of the file.
28901da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
28911da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
28921da177e4SLinus Torvalds  */
28931da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
28941da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
28951da177e4SLinus Torvalds {
28961da177e4SLinus Torvalds 	struct zap_details details;
28971da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
28981da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds 	/* Check for overflow. */
29011da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
29021da177e4SLinus Torvalds 		long long holeend =
29031da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
29041da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
29051da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
29061da177e4SLinus Torvalds 	}
29071da177e4SLinus Torvalds 
29081da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
29091da177e4SLinus Torvalds 	details.nonlinear_vma = NULL;
29101da177e4SLinus Torvalds 	details.first_index = hba;
29111da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
29121da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
29131da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
29141da177e4SLinus Torvalds 
291597a89413SPeter Zijlstra 
29163d48ae45SPeter Zijlstra 	mutex_lock(&mapping->i_mmap_mutex);
29176b2dbba8SMichel Lespinasse 	if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
29181da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
29191da177e4SLinus Torvalds 	if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
29201da177e4SLinus Torvalds 		unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
29213d48ae45SPeter Zijlstra 	mutex_unlock(&mapping->i_mmap_mutex);
29221da177e4SLinus Torvalds }
29231da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds /*
29268f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
29278f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
29288f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
29291da177e4SLinus Torvalds  */
293065500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
293165500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
293230c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
29331da177e4SLinus Torvalds {
29348f4e2101SHugh Dickins 	spinlock_t *ptl;
29354969c119SAndrea Arcangeli 	struct page *page, *swapcache = NULL;
293665500d23SHugh Dickins 	swp_entry_t entry;
29371da177e4SLinus Torvalds 	pte_t pte;
2938d065bd81SMichel Lespinasse 	int locked;
293956039efaSKAMEZAWA Hiroyuki 	struct mem_cgroup *ptr;
2940ad8c2ee8SRik van Riel 	int exclusive = 0;
294183c54070SNick Piggin 	int ret = 0;
29421da177e4SLinus Torvalds 
29434c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
29448f4e2101SHugh Dickins 		goto out;
294565500d23SHugh Dickins 
294665500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
2947d1737fdbSAndi Kleen 	if (unlikely(non_swap_entry(entry))) {
29480697212aSChristoph Lameter 		if (is_migration_entry(entry)) {
29490697212aSChristoph Lameter 			migration_entry_wait(mm, pmd, address);
2950d1737fdbSAndi Kleen 		} else if (is_hwpoison_entry(entry)) {
2951d1737fdbSAndi Kleen 			ret = VM_FAULT_HWPOISON;
2952d1737fdbSAndi Kleen 		} else {
2953d1737fdbSAndi Kleen 			print_bad_pte(vma, address, orig_pte, NULL);
2954d99be1a8SHugh Dickins 			ret = VM_FAULT_SIGBUS;
2955d1737fdbSAndi Kleen 		}
29560697212aSChristoph Lameter 		goto out;
29570697212aSChristoph Lameter 	}
29580ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
29591da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
29601da177e4SLinus Torvalds 	if (!page) {
296102098feaSHugh Dickins 		page = swapin_readahead(entry,
296202098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, vma, address);
29631da177e4SLinus Torvalds 		if (!page) {
29641da177e4SLinus Torvalds 			/*
29658f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
29668f4e2101SHugh Dickins 			 * while we released the pte lock.
29671da177e4SLinus Torvalds 			 */
29688f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
29691da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
29701da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
29710ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
297265500d23SHugh Dickins 			goto unlock;
29731da177e4SLinus Torvalds 		}
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
29761da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
2977f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
2978456f998eSYing Han 		mem_cgroup_count_vm_event(mm, PGMAJFAULT);
2979d1737fdbSAndi Kleen 	} else if (PageHWPoison(page)) {
298071f72525SWu Fengguang 		/*
298171f72525SWu Fengguang 		 * hwpoisoned dirty swapcache pages are kept for killing
298271f72525SWu Fengguang 		 * owner processes (which may be unknown at hwpoison time)
298371f72525SWu Fengguang 		 */
2984d1737fdbSAndi Kleen 		ret = VM_FAULT_HWPOISON;
2985d1737fdbSAndi Kleen 		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
29864779cb31SAndi Kleen 		goto out_release;
29871da177e4SLinus Torvalds 	}
29881da177e4SLinus Torvalds 
2989d065bd81SMichel Lespinasse 	locked = lock_page_or_retry(page, mm, flags);
2990e709ffd6SRik van Riel 
299120a1022dSBalbir Singh 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2992d065bd81SMichel Lespinasse 	if (!locked) {
2993d065bd81SMichel Lespinasse 		ret |= VM_FAULT_RETRY;
2994d065bd81SMichel Lespinasse 		goto out_release;
2995d065bd81SMichel Lespinasse 	}
29961da177e4SLinus Torvalds 
29974969c119SAndrea Arcangeli 	/*
299831c4a3d3SHugh Dickins 	 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
299931c4a3d3SHugh Dickins 	 * release the swapcache from under us.  The page pin, and pte_same
300031c4a3d3SHugh Dickins 	 * test below, are not enough to exclude that.  Even if it is still
300131c4a3d3SHugh Dickins 	 * swapcache, we need to check that the page's swap has not changed.
30024969c119SAndrea Arcangeli 	 */
300331c4a3d3SHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
30044969c119SAndrea Arcangeli 		goto out_page;
30054969c119SAndrea Arcangeli 
30064969c119SAndrea Arcangeli 	if (ksm_might_need_to_copy(page, vma, address)) {
30074969c119SAndrea Arcangeli 		swapcache = page;
30084969c119SAndrea Arcangeli 		page = ksm_does_need_to_copy(page, vma, address);
30094969c119SAndrea Arcangeli 
30104969c119SAndrea Arcangeli 		if (unlikely(!page)) {
30115ad64688SHugh Dickins 			ret = VM_FAULT_OOM;
30124969c119SAndrea Arcangeli 			page = swapcache;
30134969c119SAndrea Arcangeli 			swapcache = NULL;
30144969c119SAndrea Arcangeli 			goto out_page;
30154969c119SAndrea Arcangeli 		}
30165ad64688SHugh Dickins 	}
30175ad64688SHugh Dickins 
30182c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) {
3019073e587eSKAMEZAWA Hiroyuki 		ret = VM_FAULT_OOM;
3020bc43f75cSJohannes Weiner 		goto out_page;
3021073e587eSKAMEZAWA Hiroyuki 	}
3022073e587eSKAMEZAWA Hiroyuki 
30231da177e4SLinus Torvalds 	/*
30248f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
30251da177e4SLinus Torvalds 	 */
30268f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
30279e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
3028b8107480SKirill Korotaev 		goto out_nomap;
3029b8107480SKirill Korotaev 
3030b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
3031b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
3032b8107480SKirill Korotaev 		goto out_nomap;
30331da177e4SLinus Torvalds 	}
30341da177e4SLinus Torvalds 
30358c7c6e34SKAMEZAWA Hiroyuki 	/*
30368c7c6e34SKAMEZAWA Hiroyuki 	 * The page isn't present yet, go ahead with the fault.
30378c7c6e34SKAMEZAWA Hiroyuki 	 *
30388c7c6e34SKAMEZAWA Hiroyuki 	 * Be careful about the sequence of operations here.
30398c7c6e34SKAMEZAWA Hiroyuki 	 * To get its accounting right, reuse_swap_page() must be called
30408c7c6e34SKAMEZAWA Hiroyuki 	 * while the page is counted on swap but not yet in mapcount i.e.
30418c7c6e34SKAMEZAWA Hiroyuki 	 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
30428c7c6e34SKAMEZAWA Hiroyuki 	 * must be called after the swap_free(), or it will never succeed.
304303f3c433SKAMEZAWA Hiroyuki 	 * Because delete_from_swap_page() may be called by reuse_swap_page(),
304403f3c433SKAMEZAWA Hiroyuki 	 * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry
304503f3c433SKAMEZAWA Hiroyuki 	 * in page->private. In this case, a record in swap_cgroup  is silently
304603f3c433SKAMEZAWA Hiroyuki 	 * discarded at swap_free().
30478c7c6e34SKAMEZAWA Hiroyuki 	 */
30481da177e4SLinus Torvalds 
304934e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
3050b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter_fast(mm, MM_SWAPENTS);
30511da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
305230c9f3a9SLinus Torvalds 	if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
30531da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
305430c9f3a9SLinus Torvalds 		flags &= ~FAULT_FLAG_WRITE;
30559a5b489bSAndrea Arcangeli 		ret |= VM_FAULT_WRITE;
3056ad8c2ee8SRik van Riel 		exclusive = 1;
30571da177e4SLinus Torvalds 	}
30581da177e4SLinus Torvalds 	flush_icache_page(vma, page);
30591da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
3060ad8c2ee8SRik van Riel 	do_page_add_anon_rmap(page, vma, address, exclusive);
306103f3c433SKAMEZAWA Hiroyuki 	/* It's better to call commit-charge after rmap is established */
306203f3c433SKAMEZAWA Hiroyuki 	mem_cgroup_commit_charge_swapin(page, ptr);
30631da177e4SLinus Torvalds 
3064c475a8abSHugh Dickins 	swap_free(entry);
3065b291f000SNick Piggin 	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
3066a2c43eedSHugh Dickins 		try_to_free_swap(page);
3067c475a8abSHugh Dickins 	unlock_page(page);
30684969c119SAndrea Arcangeli 	if (swapcache) {
30694969c119SAndrea Arcangeli 		/*
30704969c119SAndrea Arcangeli 		 * Hold the lock to avoid the swap entry to be reused
30714969c119SAndrea Arcangeli 		 * until we take the PT lock for the pte_same() check
30724969c119SAndrea Arcangeli 		 * (to avoid false positives from pte_same). For
30734969c119SAndrea Arcangeli 		 * further safety release the lock after the swap_free
30744969c119SAndrea Arcangeli 		 * so that the swap count won't change under a
30754969c119SAndrea Arcangeli 		 * parallel locked swapcache.
30764969c119SAndrea Arcangeli 		 */
30774969c119SAndrea Arcangeli 		unlock_page(swapcache);
30784969c119SAndrea Arcangeli 		page_cache_release(swapcache);
30794969c119SAndrea Arcangeli 	}
3080c475a8abSHugh Dickins 
308130c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
308261469f1dSHugh Dickins 		ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
308361469f1dSHugh Dickins 		if (ret & VM_FAULT_ERROR)
308461469f1dSHugh Dickins 			ret &= VM_FAULT_ERROR;
30851da177e4SLinus Torvalds 		goto out;
30861da177e4SLinus Torvalds 	}
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
30894b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
309065500d23SHugh Dickins unlock:
30918f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
30921da177e4SLinus Torvalds out:
30931da177e4SLinus Torvalds 	return ret;
3094b8107480SKirill Korotaev out_nomap:
30957a81b88cSKAMEZAWA Hiroyuki 	mem_cgroup_cancel_charge_swapin(ptr);
30968f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
3097bc43f75cSJohannes Weiner out_page:
3098b8107480SKirill Korotaev 	unlock_page(page);
30994779cb31SAndi Kleen out_release:
3100b8107480SKirill Korotaev 	page_cache_release(page);
31014969c119SAndrea Arcangeli 	if (swapcache) {
31024969c119SAndrea Arcangeli 		unlock_page(swapcache);
31034969c119SAndrea Arcangeli 		page_cache_release(swapcache);
31044969c119SAndrea Arcangeli 	}
310565500d23SHugh Dickins 	return ret;
31061da177e4SLinus Torvalds }
31071da177e4SLinus Torvalds 
31081da177e4SLinus Torvalds /*
31098ca3eb08SLuck, Tony  * This is like a special single-page "expand_{down|up}wards()",
31108ca3eb08SLuck, Tony  * except we must first make sure that 'address{-|+}PAGE_SIZE'
3111320b2b8dSLinus Torvalds  * doesn't hit another vma.
3112320b2b8dSLinus Torvalds  */
3113320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
3114320b2b8dSLinus Torvalds {
3115320b2b8dSLinus Torvalds 	address &= PAGE_MASK;
3116320b2b8dSLinus Torvalds 	if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
31170e8e50e2SLinus Torvalds 		struct vm_area_struct *prev = vma->vm_prev;
3118320b2b8dSLinus Torvalds 
31190e8e50e2SLinus Torvalds 		/*
31200e8e50e2SLinus Torvalds 		 * Is there a mapping abutting this one below?
31210e8e50e2SLinus Torvalds 		 *
31220e8e50e2SLinus Torvalds 		 * That's only ok if it's the same stack mapping
31230e8e50e2SLinus Torvalds 		 * that has gotten split..
31240e8e50e2SLinus Torvalds 		 */
31250e8e50e2SLinus Torvalds 		if (prev && prev->vm_end == address)
31260e8e50e2SLinus Torvalds 			return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
31270e8e50e2SLinus Torvalds 
3128d05f3169SMichal Hocko 		expand_downwards(vma, address - PAGE_SIZE);
3129320b2b8dSLinus Torvalds 	}
31308ca3eb08SLuck, Tony 	if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
31318ca3eb08SLuck, Tony 		struct vm_area_struct *next = vma->vm_next;
31328ca3eb08SLuck, Tony 
31338ca3eb08SLuck, Tony 		/* As VM_GROWSDOWN but s/below/above/ */
31348ca3eb08SLuck, Tony 		if (next && next->vm_start == address + PAGE_SIZE)
31358ca3eb08SLuck, Tony 			return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
31368ca3eb08SLuck, Tony 
31378ca3eb08SLuck, Tony 		expand_upwards(vma, address + PAGE_SIZE);
31388ca3eb08SLuck, Tony 	}
3139320b2b8dSLinus Torvalds 	return 0;
3140320b2b8dSLinus Torvalds }
3141320b2b8dSLinus Torvalds 
3142320b2b8dSLinus Torvalds /*
31438f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
31448f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
31458f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
31461da177e4SLinus Torvalds  */
314765500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
314865500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
314930c9f3a9SLinus Torvalds 		unsigned int flags)
31501da177e4SLinus Torvalds {
31518f4e2101SHugh Dickins 	struct page *page;
31528f4e2101SHugh Dickins 	spinlock_t *ptl;
31531da177e4SLinus Torvalds 	pte_t entry;
31541da177e4SLinus Torvalds 
31555528f913SLinus Torvalds 	pte_unmap(page_table);
3156320b2b8dSLinus Torvalds 
315711ac5524SLinus Torvalds 	/* Check if we need to add a guard page to the stack */
315811ac5524SLinus Torvalds 	if (check_stack_guard_page(vma, address) < 0)
315911ac5524SLinus Torvalds 		return VM_FAULT_SIGBUS;
316011ac5524SLinus Torvalds 
316111ac5524SLinus Torvalds 	/* Use the zero-page for reads */
316262eede62SHugh Dickins 	if (!(flags & FAULT_FLAG_WRITE)) {
316362eede62SHugh Dickins 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
316462eede62SHugh Dickins 						vma->vm_page_prot));
316511ac5524SLinus Torvalds 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
3166a13ea5b7SHugh Dickins 		if (!pte_none(*page_table))
3167a13ea5b7SHugh Dickins 			goto unlock;
3168a13ea5b7SHugh Dickins 		goto setpte;
3169a13ea5b7SHugh Dickins 	}
3170a13ea5b7SHugh Dickins 
31711da177e4SLinus Torvalds 	/* Allocate our own private page. */
31721da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
317365500d23SHugh Dickins 		goto oom;
3174769848c0SMel Gorman 	page = alloc_zeroed_user_highpage_movable(vma, address);
31751da177e4SLinus Torvalds 	if (!page)
317665500d23SHugh Dickins 		goto oom;
31770ed361deSNick Piggin 	__SetPageUptodate(page);
31781da177e4SLinus Torvalds 
31792c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))
31808a9f3ccdSBalbir Singh 		goto oom_free_page;
31818a9f3ccdSBalbir Singh 
318265500d23SHugh Dickins 	entry = mk_pte(page, vma->vm_page_prot);
31831ac0cb5dSHugh Dickins 	if (vma->vm_flags & VM_WRITE)
31841ac0cb5dSHugh Dickins 		entry = pte_mkwrite(pte_mkdirty(entry));
31858f4e2101SHugh Dickins 
31868f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
31871c2fb7a4SAndrea Arcangeli 	if (!pte_none(*page_table))
31888f4e2101SHugh Dickins 		goto release;
31899ba69294SHugh Dickins 
319034e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
31919617d95eSNick Piggin 	page_add_new_anon_rmap(page, vma, address);
3192a13ea5b7SHugh Dickins setpte:
319365500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
31964b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
319765500d23SHugh Dickins unlock:
31988f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
319983c54070SNick Piggin 	return 0;
32008f4e2101SHugh Dickins release:
32018a9f3ccdSBalbir Singh 	mem_cgroup_uncharge_page(page);
32028f4e2101SHugh Dickins 	page_cache_release(page);
32038f4e2101SHugh Dickins 	goto unlock;
32048a9f3ccdSBalbir Singh oom_free_page:
32056dbf6d3bSHugh Dickins 	page_cache_release(page);
320665500d23SHugh Dickins oom:
32071da177e4SLinus Torvalds 	return VM_FAULT_OOM;
32081da177e4SLinus Torvalds }
32091da177e4SLinus Torvalds 
32101da177e4SLinus Torvalds /*
321154cb8821SNick Piggin  * __do_fault() tries to create a new page mapping. It aggressively
32121da177e4SLinus Torvalds  * tries to share with existing pages, but makes a separate copy if
321354cb8821SNick Piggin  * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
321454cb8821SNick Piggin  * the next page fault.
32151da177e4SLinus Torvalds  *
32161da177e4SLinus Torvalds  * As this is called only for pages that do not currently exist, we
32171da177e4SLinus Torvalds  * do not need to flush old virtual caches or the TLB.
32181da177e4SLinus Torvalds  *
32198f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
322016abfa08SHugh Dickins  * but allow concurrent faults), and pte neither mapped nor locked.
32218f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
32221da177e4SLinus Torvalds  */
322354cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
322416abfa08SHugh Dickins 		unsigned long address, pmd_t *pmd,
322554cb8821SNick Piggin 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
32261da177e4SLinus Torvalds {
322716abfa08SHugh Dickins 	pte_t *page_table;
32288f4e2101SHugh Dickins 	spinlock_t *ptl;
3229d0217ac0SNick Piggin 	struct page *page;
32301d65f86dSKAMEZAWA Hiroyuki 	struct page *cow_page;
32311da177e4SLinus Torvalds 	pte_t entry;
32321da177e4SLinus Torvalds 	int anon = 0;
3233d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
3234d0217ac0SNick Piggin 	struct vm_fault vmf;
3235d0217ac0SNick Piggin 	int ret;
3236a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
323754cb8821SNick Piggin 
32381d65f86dSKAMEZAWA Hiroyuki 	/*
32391d65f86dSKAMEZAWA Hiroyuki 	 * If we do COW later, allocate page befor taking lock_page()
32401d65f86dSKAMEZAWA Hiroyuki 	 * on the file cache page. This will reduce lock holding time.
32411d65f86dSKAMEZAWA Hiroyuki 	 */
32421d65f86dSKAMEZAWA Hiroyuki 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
32431d65f86dSKAMEZAWA Hiroyuki 
32441d65f86dSKAMEZAWA Hiroyuki 		if (unlikely(anon_vma_prepare(vma)))
32451d65f86dSKAMEZAWA Hiroyuki 			return VM_FAULT_OOM;
32461d65f86dSKAMEZAWA Hiroyuki 
32471d65f86dSKAMEZAWA Hiroyuki 		cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
32481d65f86dSKAMEZAWA Hiroyuki 		if (!cow_page)
32491d65f86dSKAMEZAWA Hiroyuki 			return VM_FAULT_OOM;
32501d65f86dSKAMEZAWA Hiroyuki 
32511d65f86dSKAMEZAWA Hiroyuki 		if (mem_cgroup_newpage_charge(cow_page, mm, GFP_KERNEL)) {
32521d65f86dSKAMEZAWA Hiroyuki 			page_cache_release(cow_page);
32531d65f86dSKAMEZAWA Hiroyuki 			return VM_FAULT_OOM;
32541d65f86dSKAMEZAWA Hiroyuki 		}
32551d65f86dSKAMEZAWA Hiroyuki 	} else
32561d65f86dSKAMEZAWA Hiroyuki 		cow_page = NULL;
32571d65f86dSKAMEZAWA Hiroyuki 
3258d0217ac0SNick Piggin 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
3259d0217ac0SNick Piggin 	vmf.pgoff = pgoff;
3260d0217ac0SNick Piggin 	vmf.flags = flags;
3261d0217ac0SNick Piggin 	vmf.page = NULL;
32621da177e4SLinus Torvalds 
3263d0217ac0SNick Piggin 	ret = vma->vm_ops->fault(vma, &vmf);
3264d065bd81SMichel Lespinasse 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3265d065bd81SMichel Lespinasse 			    VM_FAULT_RETRY)))
32661d65f86dSKAMEZAWA Hiroyuki 		goto uncharge_out;
32671da177e4SLinus Torvalds 
3268a3b947eaSAndi Kleen 	if (unlikely(PageHWPoison(vmf.page))) {
3269a3b947eaSAndi Kleen 		if (ret & VM_FAULT_LOCKED)
3270a3b947eaSAndi Kleen 			unlock_page(vmf.page);
32711d65f86dSKAMEZAWA Hiroyuki 		ret = VM_FAULT_HWPOISON;
32721d65f86dSKAMEZAWA Hiroyuki 		goto uncharge_out;
3273a3b947eaSAndi Kleen 	}
3274a3b947eaSAndi Kleen 
3275d00806b1SNick Piggin 	/*
3276d0217ac0SNick Piggin 	 * For consistency in subsequent calls, make the faulted page always
3277d00806b1SNick Piggin 	 * locked.
3278d00806b1SNick Piggin 	 */
327983c54070SNick Piggin 	if (unlikely(!(ret & VM_FAULT_LOCKED)))
3280d0217ac0SNick Piggin 		lock_page(vmf.page);
328154cb8821SNick Piggin 	else
3282d0217ac0SNick Piggin 		VM_BUG_ON(!PageLocked(vmf.page));
3283d00806b1SNick Piggin 
32841da177e4SLinus Torvalds 	/*
32851da177e4SLinus Torvalds 	 * Should we do an early C-O-W break?
32861da177e4SLinus Torvalds 	 */
3287d0217ac0SNick Piggin 	page = vmf.page;
328854cb8821SNick Piggin 	if (flags & FAULT_FLAG_WRITE) {
32899637a5efSDavid Howells 		if (!(vma->vm_flags & VM_SHARED)) {
32901d65f86dSKAMEZAWA Hiroyuki 			page = cow_page;
329154cb8821SNick Piggin 			anon = 1;
3292d0217ac0SNick Piggin 			copy_user_highpage(page, vmf.page, address, vma);
32930ed361deSNick Piggin 			__SetPageUptodate(page);
32949637a5efSDavid Howells 		} else {
329554cb8821SNick Piggin 			/*
329654cb8821SNick Piggin 			 * If the page will be shareable, see if the backing
32979637a5efSDavid Howells 			 * address space wants to know that the page is about
329854cb8821SNick Piggin 			 * to become writable
329954cb8821SNick Piggin 			 */
330069676147SMark Fasheh 			if (vma->vm_ops->page_mkwrite) {
3301c2ec175cSNick Piggin 				int tmp;
3302c2ec175cSNick Piggin 
330369676147SMark Fasheh 				unlock_page(page);
3304b827e496SNick Piggin 				vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
3305c2ec175cSNick Piggin 				tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
3306c2ec175cSNick Piggin 				if (unlikely(tmp &
3307c2ec175cSNick Piggin 					  (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
3308c2ec175cSNick Piggin 					ret = tmp;
3309b827e496SNick Piggin 					goto unwritable_page;
331069676147SMark Fasheh 				}
3311b827e496SNick Piggin 				if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
331269676147SMark Fasheh 					lock_page(page);
3313d0217ac0SNick Piggin 					if (!page->mapping) {
3314b827e496SNick Piggin 						ret = 0; /* retry the fault */
3315b827e496SNick Piggin 						unlock_page(page);
3316b827e496SNick Piggin 						goto unwritable_page;
3317d0217ac0SNick Piggin 					}
3318b827e496SNick Piggin 				} else
3319b827e496SNick Piggin 					VM_BUG_ON(!PageLocked(page));
3320a200ee18SPeter Zijlstra 				page_mkwrite = 1;
33219637a5efSDavid Howells 			}
33229637a5efSDavid Howells 		}
332354cb8821SNick Piggin 
33241da177e4SLinus Torvalds 	}
33251da177e4SLinus Torvalds 
33268f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
33271da177e4SLinus Torvalds 
33281da177e4SLinus Torvalds 	/*
33291da177e4SLinus Torvalds 	 * This silly early PAGE_DIRTY setting removes a race
33301da177e4SLinus Torvalds 	 * due to the bad i386 page protection. But it's valid
33311da177e4SLinus Torvalds 	 * for other architectures too.
33321da177e4SLinus Torvalds 	 *
333330c9f3a9SLinus Torvalds 	 * Note that if FAULT_FLAG_WRITE is set, we either now have
33341da177e4SLinus Torvalds 	 * an exclusive copy of the page, or this is a shared mapping,
33351da177e4SLinus Torvalds 	 * so we can make it writable and dirty to avoid having to
33361da177e4SLinus Torvalds 	 * handle that later.
33371da177e4SLinus Torvalds 	 */
33381da177e4SLinus Torvalds 	/* Only go through if we didn't race with anybody else... */
33391c2fb7a4SAndrea Arcangeli 	if (likely(pte_same(*page_table, orig_pte))) {
3340d00806b1SNick Piggin 		flush_icache_page(vma, page);
3341d00806b1SNick Piggin 		entry = mk_pte(page, vma->vm_page_prot);
334254cb8821SNick Piggin 		if (flags & FAULT_FLAG_WRITE)
33431da177e4SLinus Torvalds 			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
33441da177e4SLinus Torvalds 		if (anon) {
334534e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
3346d00806b1SNick Piggin 			page_add_new_anon_rmap(page, vma, address);
3347f57e88a8SHugh Dickins 		} else {
334834e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_FILEPAGES);
3349d00806b1SNick Piggin 			page_add_file_rmap(page);
335054cb8821SNick Piggin 			if (flags & FAULT_FLAG_WRITE) {
3351d00806b1SNick Piggin 				dirty_page = page;
3352d08b3851SPeter Zijlstra 				get_page(dirty_page);
3353d08b3851SPeter Zijlstra 			}
33544294621fSHugh Dickins 		}
335564d6519dSLee Schermerhorn 		set_pte_at(mm, address, page_table, entry);
33561da177e4SLinus Torvalds 
3357d00806b1SNick Piggin 		/* no need to invalidate: a not-present page won't be cached */
33584b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
3359d00806b1SNick Piggin 	} else {
33601d65f86dSKAMEZAWA Hiroyuki 		if (cow_page)
33611d65f86dSKAMEZAWA Hiroyuki 			mem_cgroup_uncharge_page(cow_page);
3362d00806b1SNick Piggin 		if (anon)
3363d00806b1SNick Piggin 			page_cache_release(page);
3364d00806b1SNick Piggin 		else
336554cb8821SNick Piggin 			anon = 1; /* no anon but release faulted_page */
3366d00806b1SNick Piggin 	}
3367d00806b1SNick Piggin 
33688f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
3369d00806b1SNick Piggin 
3370b827e496SNick Piggin 	if (dirty_page) {
3371b827e496SNick Piggin 		struct address_space *mapping = page->mapping;
337241c4d25fSJan Kara 		int dirtied = 0;
33738f7b3d15SAnton Salikhmetov 
3374b827e496SNick Piggin 		if (set_page_dirty(dirty_page))
337541c4d25fSJan Kara 			dirtied = 1;
3376b827e496SNick Piggin 		unlock_page(dirty_page);
3377d08b3851SPeter Zijlstra 		put_page(dirty_page);
337841c4d25fSJan Kara 		if ((dirtied || page_mkwrite) && mapping) {
3379b827e496SNick Piggin 			/*
3380b827e496SNick Piggin 			 * Some device drivers do not set page.mapping but still
3381b827e496SNick Piggin 			 * dirty their pages
3382b827e496SNick Piggin 			 */
3383b827e496SNick Piggin 			balance_dirty_pages_ratelimited(mapping);
3384d08b3851SPeter Zijlstra 		}
3385d00806b1SNick Piggin 
3386b827e496SNick Piggin 		/* file_update_time outside page_lock */
338741c4d25fSJan Kara 		if (vma->vm_file && !page_mkwrite)
3388b827e496SNick Piggin 			file_update_time(vma->vm_file);
3389b827e496SNick Piggin 	} else {
3390b827e496SNick Piggin 		unlock_page(vmf.page);
3391b827e496SNick Piggin 		if (anon)
3392b827e496SNick Piggin 			page_cache_release(vmf.page);
3393b827e496SNick Piggin 	}
3394b827e496SNick Piggin 
3395b827e496SNick Piggin 	return ret;
3396b827e496SNick Piggin 
3397b827e496SNick Piggin unwritable_page:
3398b827e496SNick Piggin 	page_cache_release(page);
339983c54070SNick Piggin 	return ret;
34001d65f86dSKAMEZAWA Hiroyuki uncharge_out:
34011d65f86dSKAMEZAWA Hiroyuki 	/* fs's fault handler get error */
34021d65f86dSKAMEZAWA Hiroyuki 	if (cow_page) {
34031d65f86dSKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_page(cow_page);
34041d65f86dSKAMEZAWA Hiroyuki 		page_cache_release(cow_page);
34051d65f86dSKAMEZAWA Hiroyuki 	}
34061d65f86dSKAMEZAWA Hiroyuki 	return ret;
340754cb8821SNick Piggin }
3408d00806b1SNick Piggin 
340954cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
341054cb8821SNick Piggin 		unsigned long address, pte_t *page_table, pmd_t *pmd,
341130c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
341254cb8821SNick Piggin {
341354cb8821SNick Piggin 	pgoff_t pgoff = (((address & PAGE_MASK)
34140da7e01fSDean Nelson 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
341554cb8821SNick Piggin 
341616abfa08SHugh Dickins 	pte_unmap(page_table);
341716abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
341854cb8821SNick Piggin }
341954cb8821SNick Piggin 
3420f4b81804SJes Sorensen /*
34211da177e4SLinus Torvalds  * Fault of a previously existing named mapping. Repopulate the pte
34221da177e4SLinus Torvalds  * from the encoded file_pte if possible. This enables swappable
34231da177e4SLinus Torvalds  * nonlinear vmas.
34248f4e2101SHugh Dickins  *
34258f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
34268f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
34278f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
34281da177e4SLinus Torvalds  */
3429d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
343065500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
343130c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
34321da177e4SLinus Torvalds {
343365500d23SHugh Dickins 	pgoff_t pgoff;
34341da177e4SLinus Torvalds 
343530c9f3a9SLinus Torvalds 	flags |= FAULT_FLAG_NONLINEAR;
343630c9f3a9SLinus Torvalds 
34374c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
343883c54070SNick Piggin 		return 0;
34391da177e4SLinus Torvalds 
34402509ef26SHugh Dickins 	if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
344165500d23SHugh Dickins 		/*
344265500d23SHugh Dickins 		 * Page table corrupted: show pte and kill process.
344365500d23SHugh Dickins 		 */
34443dc14741SHugh Dickins 		print_bad_pte(vma, address, orig_pte, NULL);
3445d99be1a8SHugh Dickins 		return VM_FAULT_SIGBUS;
344665500d23SHugh Dickins 	}
344765500d23SHugh Dickins 
344865500d23SHugh Dickins 	pgoff = pte_to_pgoff(orig_pte);
344916abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
34501da177e4SLinus Torvalds }
34511da177e4SLinus Torvalds 
3452*9532fec1SMel Gorman int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
3453*9532fec1SMel Gorman 				unsigned long addr, int current_nid)
3454*9532fec1SMel Gorman {
3455*9532fec1SMel Gorman 	get_page(page);
3456*9532fec1SMel Gorman 
3457*9532fec1SMel Gorman 	count_vm_numa_event(NUMA_HINT_FAULTS);
3458*9532fec1SMel Gorman 	if (current_nid == numa_node_id())
3459*9532fec1SMel Gorman 		count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
3460*9532fec1SMel Gorman 
3461*9532fec1SMel Gorman 	return mpol_misplaced(page, vma, addr);
3462*9532fec1SMel Gorman }
3463*9532fec1SMel Gorman 
3464d10e63f2SMel Gorman int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3465d10e63f2SMel Gorman 		   unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
3466d10e63f2SMel Gorman {
34674daae3b4SMel Gorman 	struct page *page = NULL;
3468d10e63f2SMel Gorman 	spinlock_t *ptl;
3469cbee9f88SPeter Zijlstra 	int current_nid = -1;
3470cbee9f88SPeter Zijlstra 	int target_nid;
3471d10e63f2SMel Gorman 
3472d10e63f2SMel Gorman 	/*
3473d10e63f2SMel Gorman 	* The "pte" at this point cannot be used safely without
3474d10e63f2SMel Gorman 	* validation through pte_unmap_same(). It's of NUMA type but
3475d10e63f2SMel Gorman 	* the pfn may be screwed if the read is non atomic.
3476d10e63f2SMel Gorman 	*
3477d10e63f2SMel Gorman 	* ptep_modify_prot_start is not called as this is clearing
3478d10e63f2SMel Gorman 	* the _PAGE_NUMA bit and it is not really expected that there
3479d10e63f2SMel Gorman 	* would be concurrent hardware modifications to the PTE.
3480d10e63f2SMel Gorman 	*/
3481d10e63f2SMel Gorman 	ptl = pte_lockptr(mm, pmd);
3482d10e63f2SMel Gorman 	spin_lock(ptl);
34834daae3b4SMel Gorman 	if (unlikely(!pte_same(*ptep, pte))) {
34844daae3b4SMel Gorman 		pte_unmap_unlock(ptep, ptl);
34854daae3b4SMel Gorman 		goto out;
34864daae3b4SMel Gorman 	}
34874daae3b4SMel Gorman 
3488d10e63f2SMel Gorman 	pte = pte_mknonnuma(pte);
3489d10e63f2SMel Gorman 	set_pte_at(mm, addr, ptep, pte);
3490d10e63f2SMel Gorman 	update_mmu_cache(vma, addr, ptep);
3491d10e63f2SMel Gorman 
3492d10e63f2SMel Gorman 	page = vm_normal_page(vma, addr, pte);
3493d10e63f2SMel Gorman 	if (!page) {
3494d10e63f2SMel Gorman 		pte_unmap_unlock(ptep, ptl);
3495d10e63f2SMel Gorman 		return 0;
3496d10e63f2SMel Gorman 	}
3497d10e63f2SMel Gorman 
34984daae3b4SMel Gorman 	current_nid = page_to_nid(page);
3499*9532fec1SMel Gorman 	target_nid = numa_migrate_prep(page, vma, addr, current_nid);
3500d10e63f2SMel Gorman 	pte_unmap_unlock(ptep, ptl);
35014daae3b4SMel Gorman 	if (target_nid == -1) {
35024daae3b4SMel Gorman 		/*
35034daae3b4SMel Gorman 		 * Account for the fault against the current node if it not
35044daae3b4SMel Gorman 		 * being replaced regardless of where the page is located.
35054daae3b4SMel Gorman 		 */
35064daae3b4SMel Gorman 		current_nid = numa_node_id();
35074daae3b4SMel Gorman 		put_page(page);
35084daae3b4SMel Gorman 		goto out;
35094daae3b4SMel Gorman 	}
35104daae3b4SMel Gorman 
35114daae3b4SMel Gorman 	/* Migrate to the requested node */
35124daae3b4SMel Gorman 	if (migrate_misplaced_page(page, target_nid))
35134daae3b4SMel Gorman 		current_nid = target_nid;
35144daae3b4SMel Gorman 
35154daae3b4SMel Gorman out:
3516*9532fec1SMel Gorman 	if (current_nid != -1)
3517cbee9f88SPeter Zijlstra 		task_numa_fault(current_nid, 1);
3518d10e63f2SMel Gorman 	return 0;
3519d10e63f2SMel Gorman }
3520d10e63f2SMel Gorman 
3521d10e63f2SMel Gorman /* NUMA hinting page fault entry point for regular pmds */
3522d10e63f2SMel Gorman #ifdef CONFIG_NUMA_BALANCING
3523d10e63f2SMel Gorman static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3524d10e63f2SMel Gorman 		     unsigned long addr, pmd_t *pmdp)
3525d10e63f2SMel Gorman {
3526d10e63f2SMel Gorman 	pmd_t pmd;
3527d10e63f2SMel Gorman 	pte_t *pte, *orig_pte;
3528d10e63f2SMel Gorman 	unsigned long _addr = addr & PMD_MASK;
3529d10e63f2SMel Gorman 	unsigned long offset;
3530d10e63f2SMel Gorman 	spinlock_t *ptl;
3531d10e63f2SMel Gorman 	bool numa = false;
353203c5a6e1SMel Gorman 	int local_nid = numa_node_id();
3533d10e63f2SMel Gorman 
3534d10e63f2SMel Gorman 	spin_lock(&mm->page_table_lock);
3535d10e63f2SMel Gorman 	pmd = *pmdp;
3536d10e63f2SMel Gorman 	if (pmd_numa(pmd)) {
3537d10e63f2SMel Gorman 		set_pmd_at(mm, _addr, pmdp, pmd_mknonnuma(pmd));
3538d10e63f2SMel Gorman 		numa = true;
3539d10e63f2SMel Gorman 	}
3540d10e63f2SMel Gorman 	spin_unlock(&mm->page_table_lock);
3541d10e63f2SMel Gorman 
3542d10e63f2SMel Gorman 	if (!numa)
3543d10e63f2SMel Gorman 		return 0;
3544d10e63f2SMel Gorman 
3545d10e63f2SMel Gorman 	/* we're in a page fault so some vma must be in the range */
3546d10e63f2SMel Gorman 	BUG_ON(!vma);
3547d10e63f2SMel Gorman 	BUG_ON(vma->vm_start >= _addr + PMD_SIZE);
3548d10e63f2SMel Gorman 	offset = max(_addr, vma->vm_start) & ~PMD_MASK;
3549d10e63f2SMel Gorman 	VM_BUG_ON(offset >= PMD_SIZE);
3550d10e63f2SMel Gorman 	orig_pte = pte = pte_offset_map_lock(mm, pmdp, _addr, &ptl);
3551d10e63f2SMel Gorman 	pte += offset >> PAGE_SHIFT;
3552d10e63f2SMel Gorman 	for (addr = _addr + offset; addr < _addr + PMD_SIZE; pte++, addr += PAGE_SIZE) {
3553d10e63f2SMel Gorman 		pte_t pteval = *pte;
3554d10e63f2SMel Gorman 		struct page *page;
3555*9532fec1SMel Gorman 		int curr_nid = local_nid;
3556*9532fec1SMel Gorman 		int target_nid;
3557d10e63f2SMel Gorman 		if (!pte_present(pteval))
3558d10e63f2SMel Gorman 			continue;
3559d10e63f2SMel Gorman 		if (!pte_numa(pteval))
3560d10e63f2SMel Gorman 			continue;
3561d10e63f2SMel Gorman 		if (addr >= vma->vm_end) {
3562d10e63f2SMel Gorman 			vma = find_vma(mm, addr);
3563d10e63f2SMel Gorman 			/* there's a pte present so there must be a vma */
3564d10e63f2SMel Gorman 			BUG_ON(!vma);
3565d10e63f2SMel Gorman 			BUG_ON(addr < vma->vm_start);
3566d10e63f2SMel Gorman 		}
3567d10e63f2SMel Gorman 		if (pte_numa(pteval)) {
3568d10e63f2SMel Gorman 			pteval = pte_mknonnuma(pteval);
3569d10e63f2SMel Gorman 			set_pte_at(mm, addr, pte, pteval);
3570d10e63f2SMel Gorman 		}
3571d10e63f2SMel Gorman 		page = vm_normal_page(vma, addr, pteval);
3572d10e63f2SMel Gorman 		if (unlikely(!page))
3573d10e63f2SMel Gorman 			continue;
3574cbee9f88SPeter Zijlstra 		/* only check non-shared pages */
3575cbee9f88SPeter Zijlstra 		if (unlikely(page_mapcount(page) != 1))
3576cbee9f88SPeter Zijlstra 			continue;
3577*9532fec1SMel Gorman 
3578*9532fec1SMel Gorman 		/*
3579*9532fec1SMel Gorman 		 * Note that the NUMA fault is later accounted to either
3580*9532fec1SMel Gorman 		 * the node that is currently running or where the page is
3581*9532fec1SMel Gorman 		 * migrated to.
3582*9532fec1SMel Gorman 		 */
3583*9532fec1SMel Gorman 		curr_nid = local_nid;
3584*9532fec1SMel Gorman 		target_nid = numa_migrate_prep(page, vma, addr,
3585*9532fec1SMel Gorman 					       page_to_nid(page));
3586*9532fec1SMel Gorman 		if (target_nid == -1) {
3587*9532fec1SMel Gorman 			put_page(page);
3588*9532fec1SMel Gorman 			continue;
3589*9532fec1SMel Gorman 		}
3590*9532fec1SMel Gorman 
3591*9532fec1SMel Gorman 		/* Migrate to the requested node */
3592cbee9f88SPeter Zijlstra 		pte_unmap_unlock(pte, ptl);
3593*9532fec1SMel Gorman 		if (migrate_misplaced_page(page, target_nid))
3594*9532fec1SMel Gorman 			curr_nid = target_nid;
3595cbee9f88SPeter Zijlstra 		task_numa_fault(curr_nid, 1);
3596cbee9f88SPeter Zijlstra 
3597cbee9f88SPeter Zijlstra 		pte = pte_offset_map_lock(mm, pmdp, addr, &ptl);
3598d10e63f2SMel Gorman 	}
3599d10e63f2SMel Gorman 	pte_unmap_unlock(orig_pte, ptl);
3600d10e63f2SMel Gorman 
3601d10e63f2SMel Gorman 	return 0;
3602d10e63f2SMel Gorman }
3603d10e63f2SMel Gorman #else
3604d10e63f2SMel Gorman static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3605d10e63f2SMel Gorman 		     unsigned long addr, pmd_t *pmdp)
3606d10e63f2SMel Gorman {
3607d10e63f2SMel Gorman 	BUG();
3608d10e63f2SMel Gorman }
3609d10e63f2SMel Gorman #endif /* CONFIG_NUMA_BALANCING */
3610d10e63f2SMel Gorman 
36111da177e4SLinus Torvalds /*
36121da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
36131da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
36141da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
36151da177e4SLinus Torvalds  *
36161da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
36171da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
36181da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
36191da177e4SLinus Torvalds  *
3620c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
3621c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
3622c74df32cSHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
36231da177e4SLinus Torvalds  */
362471e3aac0SAndrea Arcangeli int handle_pte_fault(struct mm_struct *mm,
36251da177e4SLinus Torvalds 		     struct vm_area_struct *vma, unsigned long address,
362630c9f3a9SLinus Torvalds 		     pte_t *pte, pmd_t *pmd, unsigned int flags)
36271da177e4SLinus Torvalds {
36281da177e4SLinus Torvalds 	pte_t entry;
36298f4e2101SHugh Dickins 	spinlock_t *ptl;
36301da177e4SLinus Torvalds 
36318dab5241SBenjamin Herrenschmidt 	entry = *pte;
36321da177e4SLinus Torvalds 	if (!pte_present(entry)) {
363365500d23SHugh Dickins 		if (pte_none(entry)) {
3634f4b81804SJes Sorensen 			if (vma->vm_ops) {
36353c18ddd1SNick Piggin 				if (likely(vma->vm_ops->fault))
363654cb8821SNick Piggin 					return do_linear_fault(mm, vma, address,
363730c9f3a9SLinus Torvalds 						pte, pmd, flags, entry);
3638f4b81804SJes Sorensen 			}
3639f4b81804SJes Sorensen 			return do_anonymous_page(mm, vma, address,
364030c9f3a9SLinus Torvalds 						 pte, pmd, flags);
364165500d23SHugh Dickins 		}
36421da177e4SLinus Torvalds 		if (pte_file(entry))
3643d0217ac0SNick Piggin 			return do_nonlinear_fault(mm, vma, address,
364430c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
364565500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
364630c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
36471da177e4SLinus Torvalds 	}
36481da177e4SLinus Torvalds 
3649d10e63f2SMel Gorman 	if (pte_numa(entry))
3650d10e63f2SMel Gorman 		return do_numa_page(mm, vma, address, entry, pte, pmd);
3651d10e63f2SMel Gorman 
36524c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
36538f4e2101SHugh Dickins 	spin_lock(ptl);
36548f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
36558f4e2101SHugh Dickins 		goto unlock;
365630c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
36571da177e4SLinus Torvalds 		if (!pte_write(entry))
36588f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
36598f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
36601da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
36611da177e4SLinus Torvalds 	}
36621da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
366330c9f3a9SLinus Torvalds 	if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
36644b3073e1SRussell King 		update_mmu_cache(vma, address, pte);
36651a44e149SAndrea Arcangeli 	} else {
36661a44e149SAndrea Arcangeli 		/*
36671a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
36681a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
36691a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
36701a44e149SAndrea Arcangeli 		 * with threads.
36711a44e149SAndrea Arcangeli 		 */
367230c9f3a9SLinus Torvalds 		if (flags & FAULT_FLAG_WRITE)
367361c77326SShaohua Li 			flush_tlb_fix_spurious_fault(vma, address);
36741a44e149SAndrea Arcangeli 	}
36758f4e2101SHugh Dickins unlock:
36768f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
367783c54070SNick Piggin 	return 0;
36781da177e4SLinus Torvalds }
36791da177e4SLinus Torvalds 
36801da177e4SLinus Torvalds /*
36811da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
36821da177e4SLinus Torvalds  */
368383c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3684d06063ccSLinus Torvalds 		unsigned long address, unsigned int flags)
36851da177e4SLinus Torvalds {
36861da177e4SLinus Torvalds 	pgd_t *pgd;
36871da177e4SLinus Torvalds 	pud_t *pud;
36881da177e4SLinus Torvalds 	pmd_t *pmd;
36891da177e4SLinus Torvalds 	pte_t *pte;
36901da177e4SLinus Torvalds 
36911da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
36921da177e4SLinus Torvalds 
3693f8891e5eSChristoph Lameter 	count_vm_event(PGFAULT);
3694456f998eSYing Han 	mem_cgroup_count_vm_event(mm, PGFAULT);
36951da177e4SLinus Torvalds 
369634e55232SKAMEZAWA Hiroyuki 	/* do counter updates before entering really critical section. */
369734e55232SKAMEZAWA Hiroyuki 	check_sync_rss_stat(current);
369834e55232SKAMEZAWA Hiroyuki 
3699ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
370030c9f3a9SLinus Torvalds 		return hugetlb_fault(mm, vma, address, flags);
37011da177e4SLinus Torvalds 
37021f1d06c3SDavid Rientjes retry:
37031da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
37041da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
37051da177e4SLinus Torvalds 	if (!pud)
3706c74df32cSHugh Dickins 		return VM_FAULT_OOM;
37071da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
37081da177e4SLinus Torvalds 	if (!pmd)
3709c74df32cSHugh Dickins 		return VM_FAULT_OOM;
371071e3aac0SAndrea Arcangeli 	if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
371171e3aac0SAndrea Arcangeli 		if (!vma->vm_ops)
371271e3aac0SAndrea Arcangeli 			return do_huge_pmd_anonymous_page(mm, vma, address,
371371e3aac0SAndrea Arcangeli 							  pmd, flags);
371471e3aac0SAndrea Arcangeli 	} else {
371571e3aac0SAndrea Arcangeli 		pmd_t orig_pmd = *pmd;
37161f1d06c3SDavid Rientjes 		int ret;
37171f1d06c3SDavid Rientjes 
371871e3aac0SAndrea Arcangeli 		barrier();
371971e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(orig_pmd)) {
3720d10e63f2SMel Gorman 			if (pmd_numa(*pmd))
37214daae3b4SMel Gorman 				return do_huge_pmd_numa_page(mm, vma, address,
3722d10e63f2SMel Gorman 							     orig_pmd, pmd);
3723d10e63f2SMel Gorman 
3724d10e63f2SMel Gorman 			if ((flags & FAULT_FLAG_WRITE) && !pmd_write(orig_pmd)) {
37251f1d06c3SDavid Rientjes 				ret = do_huge_pmd_wp_page(mm, vma, address, pmd,
37261f1d06c3SDavid Rientjes 							  orig_pmd);
37271f1d06c3SDavid Rientjes 				/*
37281f1d06c3SDavid Rientjes 				 * If COW results in an oom, the huge pmd will
37291f1d06c3SDavid Rientjes 				 * have been split, so retry the fault on the
37301f1d06c3SDavid Rientjes 				 * pte for a smaller charge.
37311f1d06c3SDavid Rientjes 				 */
37321f1d06c3SDavid Rientjes 				if (unlikely(ret & VM_FAULT_OOM))
37331f1d06c3SDavid Rientjes 					goto retry;
37341f1d06c3SDavid Rientjes 				return ret;
37351f1d06c3SDavid Rientjes 			}
3736d10e63f2SMel Gorman 
373771e3aac0SAndrea Arcangeli 			return 0;
373871e3aac0SAndrea Arcangeli 		}
373971e3aac0SAndrea Arcangeli 	}
374071e3aac0SAndrea Arcangeli 
3741d10e63f2SMel Gorman 	if (pmd_numa(*pmd))
3742d10e63f2SMel Gorman 		return do_pmd_numa_page(mm, vma, address, pmd);
3743d10e63f2SMel Gorman 
374471e3aac0SAndrea Arcangeli 	/*
374571e3aac0SAndrea Arcangeli 	 * Use __pte_alloc instead of pte_alloc_map, because we can't
374671e3aac0SAndrea Arcangeli 	 * run pte_offset_map on the pmd, if an huge pmd could
374771e3aac0SAndrea Arcangeli 	 * materialize from under us from a different thread.
374871e3aac0SAndrea Arcangeli 	 */
37494fd01770SMel Gorman 	if (unlikely(pmd_none(*pmd)) &&
37504fd01770SMel Gorman 	    unlikely(__pte_alloc(mm, vma, pmd, address)))
3751c74df32cSHugh Dickins 		return VM_FAULT_OOM;
375271e3aac0SAndrea Arcangeli 	/* if an huge pmd materialized from under us just retry later */
375371e3aac0SAndrea Arcangeli 	if (unlikely(pmd_trans_huge(*pmd)))
375471e3aac0SAndrea Arcangeli 		return 0;
375571e3aac0SAndrea Arcangeli 	/*
375671e3aac0SAndrea Arcangeli 	 * A regular pmd is established and it can't morph into a huge pmd
375771e3aac0SAndrea Arcangeli 	 * from under us anymore at this point because we hold the mmap_sem
375871e3aac0SAndrea Arcangeli 	 * read mode and khugepaged takes it in write mode. So now it's
375971e3aac0SAndrea Arcangeli 	 * safe to run pte_offset_map().
376071e3aac0SAndrea Arcangeli 	 */
376171e3aac0SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
37621da177e4SLinus Torvalds 
376330c9f3a9SLinus Torvalds 	return handle_pte_fault(mm, vma, address, pte, pmd, flags);
37641da177e4SLinus Torvalds }
37651da177e4SLinus Torvalds 
37661da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
37671da177e4SLinus Torvalds /*
37681da177e4SLinus Torvalds  * Allocate page upper directory.
3769872fec16SHugh Dickins  * We've already handled the fast-path in-line.
37701da177e4SLinus Torvalds  */
37711bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
37721da177e4SLinus Torvalds {
3773c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
3774c74df32cSHugh Dickins 	if (!new)
37751bb3630eSHugh Dickins 		return -ENOMEM;
37761da177e4SLinus Torvalds 
3777362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3778362a61adSNick Piggin 
3779872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
37801bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
37815e541973SBenjamin Herrenschmidt 		pud_free(mm, new);
37821bb3630eSHugh Dickins 	else
37831da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
3784872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
37851bb3630eSHugh Dickins 	return 0;
37861da177e4SLinus Torvalds }
37871da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
37881da177e4SLinus Torvalds 
37891da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
37901da177e4SLinus Torvalds /*
37911da177e4SLinus Torvalds  * Allocate page middle directory.
3792872fec16SHugh Dickins  * We've already handled the fast-path in-line.
37931da177e4SLinus Torvalds  */
37941bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
37951da177e4SLinus Torvalds {
3796c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
3797c74df32cSHugh Dickins 	if (!new)
37981bb3630eSHugh Dickins 		return -ENOMEM;
37991da177e4SLinus Torvalds 
3800362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3801362a61adSNick Piggin 
3802872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
38031da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
38041bb3630eSHugh Dickins 	if (pud_present(*pud))		/* Another has populated it */
38055e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
38061bb3630eSHugh Dickins 	else
38071da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
38081da177e4SLinus Torvalds #else
38091bb3630eSHugh Dickins 	if (pgd_present(*pud))		/* Another has populated it */
38105e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
38111bb3630eSHugh Dickins 	else
38121da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
38131da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
3814872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
38151bb3630eSHugh Dickins 	return 0;
38161da177e4SLinus Torvalds }
38171da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
38181da177e4SLinus Torvalds 
38191da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end)
38201da177e4SLinus Torvalds {
38211da177e4SLinus Torvalds 	int ret, len, write;
38221da177e4SLinus Torvalds 	struct vm_area_struct * vma;
38231da177e4SLinus Torvalds 
38241da177e4SLinus Torvalds 	vma = find_vma(current->mm, addr);
38251da177e4SLinus Torvalds 	if (!vma)
3826a477097dSKOSAKI Motohiro 		return -ENOMEM;
38275ecfda04SMichel Lespinasse 	/*
38285ecfda04SMichel Lespinasse 	 * We want to touch writable mappings with a write fault in order
38295ecfda04SMichel Lespinasse 	 * to break COW, except for shared mappings because these don't COW
38305ecfda04SMichel Lespinasse 	 * and we would not want to dirty them for nothing.
38315ecfda04SMichel Lespinasse 	 */
38325ecfda04SMichel Lespinasse 	write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE;
38335bcb28b1SEric Sesterhenn 	BUG_ON(addr >= end);
38345bcb28b1SEric Sesterhenn 	BUG_ON(end > vma->vm_end);
383568e116a3SRolf Eike Beer 	len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
38361da177e4SLinus Torvalds 	ret = get_user_pages(current, current->mm, addr,
38371da177e4SLinus Torvalds 			len, write, 0, NULL, NULL);
3838c11d69d8SLee Schermerhorn 	if (ret < 0)
38391da177e4SLinus Torvalds 		return ret;
38409978ad58SLee Schermerhorn 	return ret == len ? 0 : -EFAULT;
38411da177e4SLinus Torvalds }
38421da177e4SLinus Torvalds 
38431da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA)
38441da177e4SLinus Torvalds 
38451da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR)
38465ce7852cSAdrian Bunk static struct vm_area_struct gate_vma;
38471da177e4SLinus Torvalds 
38481da177e4SLinus Torvalds static int __init gate_vma_init(void)
38491da177e4SLinus Torvalds {
38501da177e4SLinus Torvalds 	gate_vma.vm_mm = NULL;
38511da177e4SLinus Torvalds 	gate_vma.vm_start = FIXADDR_USER_START;
38521da177e4SLinus Torvalds 	gate_vma.vm_end = FIXADDR_USER_END;
3853b6558c4aSRoland McGrath 	gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
3854b6558c4aSRoland McGrath 	gate_vma.vm_page_prot = __P101;
3855909af768SJason Baron 
38561da177e4SLinus Torvalds 	return 0;
38571da177e4SLinus Torvalds }
38581da177e4SLinus Torvalds __initcall(gate_vma_init);
38591da177e4SLinus Torvalds #endif
38601da177e4SLinus Torvalds 
386131db58b3SStephen Wilson struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
38621da177e4SLinus Torvalds {
38631da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
38641da177e4SLinus Torvalds 	return &gate_vma;
38651da177e4SLinus Torvalds #else
38661da177e4SLinus Torvalds 	return NULL;
38671da177e4SLinus Torvalds #endif
38681da177e4SLinus Torvalds }
38691da177e4SLinus Torvalds 
3870cae5d390SStephen Wilson int in_gate_area_no_mm(unsigned long addr)
38711da177e4SLinus Torvalds {
38721da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
38731da177e4SLinus Torvalds 	if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
38741da177e4SLinus Torvalds 		return 1;
38751da177e4SLinus Torvalds #endif
38761da177e4SLinus Torvalds 	return 0;
38771da177e4SLinus Torvalds }
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds #endif	/* __HAVE_ARCH_GATE_AREA */
38800ec76a11SDavid Howells 
38811b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address,
3882f8ad0f49SJohannes Weiner 		pte_t **ptepp, spinlock_t **ptlp)
3883f8ad0f49SJohannes Weiner {
3884f8ad0f49SJohannes Weiner 	pgd_t *pgd;
3885f8ad0f49SJohannes Weiner 	pud_t *pud;
3886f8ad0f49SJohannes Weiner 	pmd_t *pmd;
3887f8ad0f49SJohannes Weiner 	pte_t *ptep;
3888f8ad0f49SJohannes Weiner 
3889f8ad0f49SJohannes Weiner 	pgd = pgd_offset(mm, address);
3890f8ad0f49SJohannes Weiner 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3891f8ad0f49SJohannes Weiner 		goto out;
3892f8ad0f49SJohannes Weiner 
3893f8ad0f49SJohannes Weiner 	pud = pud_offset(pgd, address);
3894f8ad0f49SJohannes Weiner 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3895f8ad0f49SJohannes Weiner 		goto out;
3896f8ad0f49SJohannes Weiner 
3897f8ad0f49SJohannes Weiner 	pmd = pmd_offset(pud, address);
3898f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
3899f8ad0f49SJohannes Weiner 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3900f8ad0f49SJohannes Weiner 		goto out;
3901f8ad0f49SJohannes Weiner 
3902f8ad0f49SJohannes Weiner 	/* We cannot handle huge page PFN maps. Luckily they don't exist. */
3903f8ad0f49SJohannes Weiner 	if (pmd_huge(*pmd))
3904f8ad0f49SJohannes Weiner 		goto out;
3905f8ad0f49SJohannes Weiner 
3906f8ad0f49SJohannes Weiner 	ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3907f8ad0f49SJohannes Weiner 	if (!ptep)
3908f8ad0f49SJohannes Weiner 		goto out;
3909f8ad0f49SJohannes Weiner 	if (!pte_present(*ptep))
3910f8ad0f49SJohannes Weiner 		goto unlock;
3911f8ad0f49SJohannes Weiner 	*ptepp = ptep;
3912f8ad0f49SJohannes Weiner 	return 0;
3913f8ad0f49SJohannes Weiner unlock:
3914f8ad0f49SJohannes Weiner 	pte_unmap_unlock(ptep, *ptlp);
3915f8ad0f49SJohannes Weiner out:
3916f8ad0f49SJohannes Weiner 	return -EINVAL;
3917f8ad0f49SJohannes Weiner }
3918f8ad0f49SJohannes Weiner 
39191b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address,
39201b36ba81SNamhyung Kim 			     pte_t **ptepp, spinlock_t **ptlp)
39211b36ba81SNamhyung Kim {
39221b36ba81SNamhyung Kim 	int res;
39231b36ba81SNamhyung Kim 
39241b36ba81SNamhyung Kim 	/* (void) is needed to make gcc happy */
39251b36ba81SNamhyung Kim 	(void) __cond_lock(*ptlp,
39261b36ba81SNamhyung Kim 			   !(res = __follow_pte(mm, address, ptepp, ptlp)));
39271b36ba81SNamhyung Kim 	return res;
39281b36ba81SNamhyung Kim }
39291b36ba81SNamhyung Kim 
39303b6748e2SJohannes Weiner /**
39313b6748e2SJohannes Weiner  * follow_pfn - look up PFN at a user virtual address
39323b6748e2SJohannes Weiner  * @vma: memory mapping
39333b6748e2SJohannes Weiner  * @address: user virtual address
39343b6748e2SJohannes Weiner  * @pfn: location to store found PFN
39353b6748e2SJohannes Weiner  *
39363b6748e2SJohannes Weiner  * Only IO mappings and raw PFN mappings are allowed.
39373b6748e2SJohannes Weiner  *
39383b6748e2SJohannes Weiner  * Returns zero and the pfn at @pfn on success, -ve otherwise.
39393b6748e2SJohannes Weiner  */
39403b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address,
39413b6748e2SJohannes Weiner 	unsigned long *pfn)
39423b6748e2SJohannes Weiner {
39433b6748e2SJohannes Weiner 	int ret = -EINVAL;
39443b6748e2SJohannes Weiner 	spinlock_t *ptl;
39453b6748e2SJohannes Weiner 	pte_t *ptep;
39463b6748e2SJohannes Weiner 
39473b6748e2SJohannes Weiner 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
39483b6748e2SJohannes Weiner 		return ret;
39493b6748e2SJohannes Weiner 
39503b6748e2SJohannes Weiner 	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
39513b6748e2SJohannes Weiner 	if (ret)
39523b6748e2SJohannes Weiner 		return ret;
39533b6748e2SJohannes Weiner 	*pfn = pte_pfn(*ptep);
39543b6748e2SJohannes Weiner 	pte_unmap_unlock(ptep, ptl);
39553b6748e2SJohannes Weiner 	return 0;
39563b6748e2SJohannes Weiner }
39573b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn);
39583b6748e2SJohannes Weiner 
395928b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
3960d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma,
396128b2ee20SRik van Riel 		unsigned long address, unsigned int flags,
3962d87fe660Svenkatesh.pallipadi@intel.com 		unsigned long *prot, resource_size_t *phys)
396328b2ee20SRik van Riel {
396403668a4dSJohannes Weiner 	int ret = -EINVAL;
396528b2ee20SRik van Riel 	pte_t *ptep, pte;
396628b2ee20SRik van Riel 	spinlock_t *ptl;
396728b2ee20SRik van Riel 
3968d87fe660Svenkatesh.pallipadi@intel.com 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3969d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
397028b2ee20SRik van Riel 
397103668a4dSJohannes Weiner 	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
3972d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
397328b2ee20SRik van Riel 	pte = *ptep;
397403668a4dSJohannes Weiner 
397528b2ee20SRik van Riel 	if ((flags & FOLL_WRITE) && !pte_write(pte))
397628b2ee20SRik van Riel 		goto unlock;
397728b2ee20SRik van Riel 
397828b2ee20SRik van Riel 	*prot = pgprot_val(pte_pgprot(pte));
397903668a4dSJohannes Weiner 	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
398028b2ee20SRik van Riel 
398103668a4dSJohannes Weiner 	ret = 0;
398228b2ee20SRik van Riel unlock:
398328b2ee20SRik van Riel 	pte_unmap_unlock(ptep, ptl);
398428b2ee20SRik van Riel out:
3985d87fe660Svenkatesh.pallipadi@intel.com 	return ret;
398628b2ee20SRik van Riel }
398728b2ee20SRik van Riel 
398828b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
398928b2ee20SRik van Riel 			void *buf, int len, int write)
399028b2ee20SRik van Riel {
399128b2ee20SRik van Riel 	resource_size_t phys_addr;
399228b2ee20SRik van Riel 	unsigned long prot = 0;
39932bc7273bSKOSAKI Motohiro 	void __iomem *maddr;
399428b2ee20SRik van Riel 	int offset = addr & (PAGE_SIZE-1);
399528b2ee20SRik van Riel 
3996d87fe660Svenkatesh.pallipadi@intel.com 	if (follow_phys(vma, addr, write, &prot, &phys_addr))
399728b2ee20SRik van Riel 		return -EINVAL;
399828b2ee20SRik van Riel 
399928b2ee20SRik van Riel 	maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot);
400028b2ee20SRik van Riel 	if (write)
400128b2ee20SRik van Riel 		memcpy_toio(maddr + offset, buf, len);
400228b2ee20SRik van Riel 	else
400328b2ee20SRik van Riel 		memcpy_fromio(buf, maddr + offset, len);
400428b2ee20SRik van Riel 	iounmap(maddr);
400528b2ee20SRik van Riel 
400628b2ee20SRik van Riel 	return len;
400728b2ee20SRik van Riel }
400828b2ee20SRik van Riel #endif
400928b2ee20SRik van Riel 
40100ec76a11SDavid Howells /*
4011206cb636SStephen Wilson  * Access another process' address space as given in mm.  If non-NULL, use the
4012206cb636SStephen Wilson  * given task for page fault accounting.
40130ec76a11SDavid Howells  */
4014206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
4015206cb636SStephen Wilson 		unsigned long addr, void *buf, int len, int write)
40160ec76a11SDavid Howells {
40170ec76a11SDavid Howells 	struct vm_area_struct *vma;
40180ec76a11SDavid Howells 	void *old_buf = buf;
40190ec76a11SDavid Howells 
40200ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
4021183ff22bSSimon Arlott 	/* ignore errors, just check how much was successfully transferred */
40220ec76a11SDavid Howells 	while (len) {
40230ec76a11SDavid Howells 		int bytes, ret, offset;
40240ec76a11SDavid Howells 		void *maddr;
402528b2ee20SRik van Riel 		struct page *page = NULL;
40260ec76a11SDavid Howells 
40270ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
40280ec76a11SDavid Howells 				write, 1, &page, &vma);
402928b2ee20SRik van Riel 		if (ret <= 0) {
403028b2ee20SRik van Riel 			/*
403128b2ee20SRik van Riel 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
403228b2ee20SRik van Riel 			 * we can access using slightly different code.
403328b2ee20SRik van Riel 			 */
403428b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
403528b2ee20SRik van Riel 			vma = find_vma(mm, addr);
4036fe936dfcSMichael Ellerman 			if (!vma || vma->vm_start > addr)
40370ec76a11SDavid Howells 				break;
403828b2ee20SRik van Riel 			if (vma->vm_ops && vma->vm_ops->access)
403928b2ee20SRik van Riel 				ret = vma->vm_ops->access(vma, addr, buf,
404028b2ee20SRik van Riel 							  len, write);
404128b2ee20SRik van Riel 			if (ret <= 0)
404228b2ee20SRik van Riel #endif
404328b2ee20SRik van Riel 				break;
404428b2ee20SRik van Riel 			bytes = ret;
404528b2ee20SRik van Riel 		} else {
40460ec76a11SDavid Howells 			bytes = len;
40470ec76a11SDavid Howells 			offset = addr & (PAGE_SIZE-1);
40480ec76a11SDavid Howells 			if (bytes > PAGE_SIZE-offset)
40490ec76a11SDavid Howells 				bytes = PAGE_SIZE-offset;
40500ec76a11SDavid Howells 
40510ec76a11SDavid Howells 			maddr = kmap(page);
40520ec76a11SDavid Howells 			if (write) {
40530ec76a11SDavid Howells 				copy_to_user_page(vma, page, addr,
40540ec76a11SDavid Howells 						  maddr + offset, buf, bytes);
40550ec76a11SDavid Howells 				set_page_dirty_lock(page);
40560ec76a11SDavid Howells 			} else {
40570ec76a11SDavid Howells 				copy_from_user_page(vma, page, addr,
40580ec76a11SDavid Howells 						    buf, maddr + offset, bytes);
40590ec76a11SDavid Howells 			}
40600ec76a11SDavid Howells 			kunmap(page);
40610ec76a11SDavid Howells 			page_cache_release(page);
406228b2ee20SRik van Riel 		}
40630ec76a11SDavid Howells 		len -= bytes;
40640ec76a11SDavid Howells 		buf += bytes;
40650ec76a11SDavid Howells 		addr += bytes;
40660ec76a11SDavid Howells 	}
40670ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
40680ec76a11SDavid Howells 
40690ec76a11SDavid Howells 	return buf - old_buf;
40700ec76a11SDavid Howells }
407103252919SAndi Kleen 
40725ddd36b9SStephen Wilson /**
4073ae91dbfcSRandy Dunlap  * access_remote_vm - access another process' address space
40745ddd36b9SStephen Wilson  * @mm:		the mm_struct of the target address space
40755ddd36b9SStephen Wilson  * @addr:	start address to access
40765ddd36b9SStephen Wilson  * @buf:	source or destination buffer
40775ddd36b9SStephen Wilson  * @len:	number of bytes to transfer
40785ddd36b9SStephen Wilson  * @write:	whether the access is a write
40795ddd36b9SStephen Wilson  *
40805ddd36b9SStephen Wilson  * The caller must hold a reference on @mm.
40815ddd36b9SStephen Wilson  */
40825ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr,
40835ddd36b9SStephen Wilson 		void *buf, int len, int write)
40845ddd36b9SStephen Wilson {
40855ddd36b9SStephen Wilson 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
40865ddd36b9SStephen Wilson }
40875ddd36b9SStephen Wilson 
408803252919SAndi Kleen /*
4089206cb636SStephen Wilson  * Access another process' address space.
4090206cb636SStephen Wilson  * Source/target buffer must be kernel space,
4091206cb636SStephen Wilson  * Do not walk the page table directly, use get_user_pages
4092206cb636SStephen Wilson  */
4093206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr,
4094206cb636SStephen Wilson 		void *buf, int len, int write)
4095206cb636SStephen Wilson {
4096206cb636SStephen Wilson 	struct mm_struct *mm;
4097206cb636SStephen Wilson 	int ret;
4098206cb636SStephen Wilson 
4099206cb636SStephen Wilson 	mm = get_task_mm(tsk);
4100206cb636SStephen Wilson 	if (!mm)
4101206cb636SStephen Wilson 		return 0;
4102206cb636SStephen Wilson 
4103206cb636SStephen Wilson 	ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
4104206cb636SStephen Wilson 	mmput(mm);
4105206cb636SStephen Wilson 
4106206cb636SStephen Wilson 	return ret;
4107206cb636SStephen Wilson }
4108206cb636SStephen Wilson 
410903252919SAndi Kleen /*
411003252919SAndi Kleen  * Print the name of a VMA.
411103252919SAndi Kleen  */
411203252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip)
411303252919SAndi Kleen {
411403252919SAndi Kleen 	struct mm_struct *mm = current->mm;
411503252919SAndi Kleen 	struct vm_area_struct *vma;
411603252919SAndi Kleen 
4117e8bff74aSIngo Molnar 	/*
4118e8bff74aSIngo Molnar 	 * Do not print if we are in atomic
4119e8bff74aSIngo Molnar 	 * contexts (in exception stacks, etc.):
4120e8bff74aSIngo Molnar 	 */
4121e8bff74aSIngo Molnar 	if (preempt_count())
4122e8bff74aSIngo Molnar 		return;
4123e8bff74aSIngo Molnar 
412403252919SAndi Kleen 	down_read(&mm->mmap_sem);
412503252919SAndi Kleen 	vma = find_vma(mm, ip);
412603252919SAndi Kleen 	if (vma && vma->vm_file) {
412703252919SAndi Kleen 		struct file *f = vma->vm_file;
412803252919SAndi Kleen 		char *buf = (char *)__get_free_page(GFP_KERNEL);
412903252919SAndi Kleen 		if (buf) {
413003252919SAndi Kleen 			char *p, *s;
413103252919SAndi Kleen 
4132cf28b486SJan Blunck 			p = d_path(&f->f_path, buf, PAGE_SIZE);
413303252919SAndi Kleen 			if (IS_ERR(p))
413403252919SAndi Kleen 				p = "?";
413503252919SAndi Kleen 			s = strrchr(p, '/');
413603252919SAndi Kleen 			if (s)
413703252919SAndi Kleen 				p = s+1;
413803252919SAndi Kleen 			printk("%s%s[%lx+%lx]", prefix, p,
413903252919SAndi Kleen 					vma->vm_start,
414003252919SAndi Kleen 					vma->vm_end - vma->vm_start);
414103252919SAndi Kleen 			free_page((unsigned long)buf);
414203252919SAndi Kleen 		}
414303252919SAndi Kleen 	}
414451a07e50SJeff Liu 	up_read(&mm->mmap_sem);
414503252919SAndi Kleen }
41463ee1afa3SNick Piggin 
41473ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
41483ee1afa3SNick Piggin void might_fault(void)
41493ee1afa3SNick Piggin {
415095156f00SPeter Zijlstra 	/*
415195156f00SPeter Zijlstra 	 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
415295156f00SPeter Zijlstra 	 * holding the mmap_sem, this is safe because kernel memory doesn't
415395156f00SPeter Zijlstra 	 * get paged out, therefore we'll never actually fault, and the
415495156f00SPeter Zijlstra 	 * below annotations will generate false positives.
415595156f00SPeter Zijlstra 	 */
415695156f00SPeter Zijlstra 	if (segment_eq(get_fs(), KERNEL_DS))
415795156f00SPeter Zijlstra 		return;
415895156f00SPeter Zijlstra 
41593ee1afa3SNick Piggin 	might_sleep();
41603ee1afa3SNick Piggin 	/*
41613ee1afa3SNick Piggin 	 * it would be nicer only to annotate paths which are not under
41623ee1afa3SNick Piggin 	 * pagefault_disable, however that requires a larger audit and
41633ee1afa3SNick Piggin 	 * providing helpers like get_user_atomic.
41643ee1afa3SNick Piggin 	 */
41653ee1afa3SNick Piggin 	if (!in_atomic() && current->mm)
41663ee1afa3SNick Piggin 		might_lock_read(&current->mm->mmap_sem);
41673ee1afa3SNick Piggin }
41683ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault);
41693ee1afa3SNick Piggin #endif
417047ad8475SAndrea Arcangeli 
417147ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
417247ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page,
417347ad8475SAndrea Arcangeli 				unsigned long addr,
417447ad8475SAndrea Arcangeli 				unsigned int pages_per_huge_page)
417547ad8475SAndrea Arcangeli {
417647ad8475SAndrea Arcangeli 	int i;
417747ad8475SAndrea Arcangeli 	struct page *p = page;
417847ad8475SAndrea Arcangeli 
417947ad8475SAndrea Arcangeli 	might_sleep();
418047ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page;
418147ad8475SAndrea Arcangeli 	     i++, p = mem_map_next(p, page, i)) {
418247ad8475SAndrea Arcangeli 		cond_resched();
418347ad8475SAndrea Arcangeli 		clear_user_highpage(p, addr + i * PAGE_SIZE);
418447ad8475SAndrea Arcangeli 	}
418547ad8475SAndrea Arcangeli }
418647ad8475SAndrea Arcangeli void clear_huge_page(struct page *page,
418747ad8475SAndrea Arcangeli 		     unsigned long addr, unsigned int pages_per_huge_page)
418847ad8475SAndrea Arcangeli {
418947ad8475SAndrea Arcangeli 	int i;
419047ad8475SAndrea Arcangeli 
419147ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
419247ad8475SAndrea Arcangeli 		clear_gigantic_page(page, addr, pages_per_huge_page);
419347ad8475SAndrea Arcangeli 		return;
419447ad8475SAndrea Arcangeli 	}
419547ad8475SAndrea Arcangeli 
419647ad8475SAndrea Arcangeli 	might_sleep();
419747ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
419847ad8475SAndrea Arcangeli 		cond_resched();
419947ad8475SAndrea Arcangeli 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
420047ad8475SAndrea Arcangeli 	}
420147ad8475SAndrea Arcangeli }
420247ad8475SAndrea Arcangeli 
420347ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src,
420447ad8475SAndrea Arcangeli 				    unsigned long addr,
420547ad8475SAndrea Arcangeli 				    struct vm_area_struct *vma,
420647ad8475SAndrea Arcangeli 				    unsigned int pages_per_huge_page)
420747ad8475SAndrea Arcangeli {
420847ad8475SAndrea Arcangeli 	int i;
420947ad8475SAndrea Arcangeli 	struct page *dst_base = dst;
421047ad8475SAndrea Arcangeli 	struct page *src_base = src;
421147ad8475SAndrea Arcangeli 
421247ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; ) {
421347ad8475SAndrea Arcangeli 		cond_resched();
421447ad8475SAndrea Arcangeli 		copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
421547ad8475SAndrea Arcangeli 
421647ad8475SAndrea Arcangeli 		i++;
421747ad8475SAndrea Arcangeli 		dst = mem_map_next(dst, dst_base, i);
421847ad8475SAndrea Arcangeli 		src = mem_map_next(src, src_base, i);
421947ad8475SAndrea Arcangeli 	}
422047ad8475SAndrea Arcangeli }
422147ad8475SAndrea Arcangeli 
422247ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src,
422347ad8475SAndrea Arcangeli 			 unsigned long addr, struct vm_area_struct *vma,
422447ad8475SAndrea Arcangeli 			 unsigned int pages_per_huge_page)
422547ad8475SAndrea Arcangeli {
422647ad8475SAndrea Arcangeli 	int i;
422747ad8475SAndrea Arcangeli 
422847ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
422947ad8475SAndrea Arcangeli 		copy_user_gigantic_page(dst, src, addr, vma,
423047ad8475SAndrea Arcangeli 					pages_per_huge_page);
423147ad8475SAndrea Arcangeli 		return;
423247ad8475SAndrea Arcangeli 	}
423347ad8475SAndrea Arcangeli 
423447ad8475SAndrea Arcangeli 	might_sleep();
423547ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
423647ad8475SAndrea Arcangeli 		cond_resched();
423747ad8475SAndrea Arcangeli 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
423847ad8475SAndrea Arcangeli 	}
423947ad8475SAndrea Arcangeli }
424047ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
4241