xref: /linux/mm/memory.c (revision f0b791a34cb3cffd2bbc3ca4365c9b719fa2c9f3)
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>
612fbc57c5SAndy Shevchenko #include <linux/string.h>
620abdd7a8SDan Williams #include <linux/dma-debug.h>
631da177e4SLinus Torvalds 
646952b61dSAlexey Dobriyan #include <asm/io.h>
651da177e4SLinus Torvalds #include <asm/pgalloc.h>
661da177e4SLinus Torvalds #include <asm/uaccess.h>
671da177e4SLinus Torvalds #include <asm/tlb.h>
681da177e4SLinus Torvalds #include <asm/tlbflush.h>
691da177e4SLinus Torvalds #include <asm/pgtable.h>
701da177e4SLinus Torvalds 
7142b77728SJan Beulich #include "internal.h"
7242b77728SJan Beulich 
7390572890SPeter Zijlstra #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
7490572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
7575980e97SPeter Zijlstra #endif
7675980e97SPeter Zijlstra 
77d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES
781da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */
791da177e4SLinus Torvalds unsigned long max_mapnr;
801da177e4SLinus Torvalds struct page *mem_map;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr);
831da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
841da177e4SLinus Torvalds #endif
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /*
871da177e4SLinus Torvalds  * A number of key systems in x86 including ioremap() rely on the assumption
881da177e4SLinus Torvalds  * that high_memory defines the upper bound on direct map memory, then end
891da177e4SLinus Torvalds  * of ZONE_NORMAL.  Under CONFIG_DISCONTIG this means that max_low_pfn and
901da177e4SLinus Torvalds  * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
911da177e4SLinus Torvalds  * and ZONE_HIGHMEM.
921da177e4SLinus Torvalds  */
931da177e4SLinus Torvalds void * high_memory;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory);
961da177e4SLinus Torvalds 
9732a93233SIngo Molnar /*
9832a93233SIngo Molnar  * Randomize the address space (stacks, mmaps, brk, etc.).
9932a93233SIngo Molnar  *
10032a93233SIngo Molnar  * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
10132a93233SIngo Molnar  *   as ancient (libc5 based) binaries can segfault. )
10232a93233SIngo Molnar  */
10332a93233SIngo Molnar int randomize_va_space __read_mostly =
10432a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK
10532a93233SIngo Molnar 					1;
10632a93233SIngo Molnar #else
10732a93233SIngo Molnar 					2;
10832a93233SIngo Molnar #endif
109a62eaf15SAndi Kleen 
110a62eaf15SAndi Kleen static int __init disable_randmaps(char *s)
111a62eaf15SAndi Kleen {
112a62eaf15SAndi Kleen 	randomize_va_space = 0;
1139b41046cSOGAWA Hirofumi 	return 1;
114a62eaf15SAndi Kleen }
115a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps);
116a62eaf15SAndi Kleen 
11762eede62SHugh Dickins unsigned long zero_pfn __read_mostly;
11803f6462aSHugh Dickins unsigned long highest_memmap_pfn __read_mostly;
119a13ea5b7SHugh Dickins 
120a13ea5b7SHugh Dickins /*
121a13ea5b7SHugh Dickins  * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
122a13ea5b7SHugh Dickins  */
123a13ea5b7SHugh Dickins static int __init init_zero_pfn(void)
124a13ea5b7SHugh Dickins {
125a13ea5b7SHugh Dickins 	zero_pfn = page_to_pfn(ZERO_PAGE(0));
126a13ea5b7SHugh Dickins 	return 0;
127a13ea5b7SHugh Dickins }
128a13ea5b7SHugh Dickins core_initcall(init_zero_pfn);
129a62eaf15SAndi Kleen 
130d559db08SKAMEZAWA Hiroyuki 
13134e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
13234e55232SKAMEZAWA Hiroyuki 
133ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm)
13434e55232SKAMEZAWA Hiroyuki {
13534e55232SKAMEZAWA Hiroyuki 	int i;
13634e55232SKAMEZAWA Hiroyuki 
13734e55232SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++) {
13805af2e10SDavid Rientjes 		if (current->rss_stat.count[i]) {
13905af2e10SDavid Rientjes 			add_mm_counter(mm, i, current->rss_stat.count[i]);
14005af2e10SDavid Rientjes 			current->rss_stat.count[i] = 0;
14134e55232SKAMEZAWA Hiroyuki 		}
14234e55232SKAMEZAWA Hiroyuki 	}
14305af2e10SDavid Rientjes 	current->rss_stat.events = 0;
14434e55232SKAMEZAWA Hiroyuki }
14534e55232SKAMEZAWA Hiroyuki 
14634e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
14734e55232SKAMEZAWA Hiroyuki {
14834e55232SKAMEZAWA Hiroyuki 	struct task_struct *task = current;
14934e55232SKAMEZAWA Hiroyuki 
15034e55232SKAMEZAWA Hiroyuki 	if (likely(task->mm == mm))
15134e55232SKAMEZAWA Hiroyuki 		task->rss_stat.count[member] += val;
15234e55232SKAMEZAWA Hiroyuki 	else
15334e55232SKAMEZAWA Hiroyuki 		add_mm_counter(mm, member, val);
15434e55232SKAMEZAWA Hiroyuki }
15534e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
15634e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
15734e55232SKAMEZAWA Hiroyuki 
15834e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */
15934e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH	(64)
16034e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
16134e55232SKAMEZAWA Hiroyuki {
16234e55232SKAMEZAWA Hiroyuki 	if (unlikely(task != current))
16334e55232SKAMEZAWA Hiroyuki 		return;
16434e55232SKAMEZAWA Hiroyuki 	if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
165ea48cf78SDavid Rientjes 		sync_mm_rss(task->mm);
16634e55232SKAMEZAWA Hiroyuki }
1679547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */
16834e55232SKAMEZAWA Hiroyuki 
16934e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
17034e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
17134e55232SKAMEZAWA Hiroyuki 
17234e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
17334e55232SKAMEZAWA Hiroyuki {
17434e55232SKAMEZAWA Hiroyuki }
17534e55232SKAMEZAWA Hiroyuki 
1769547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */
1779547d01bSPeter Zijlstra 
1789547d01bSPeter Zijlstra #ifdef HAVE_GENERIC_MMU_GATHER
1799547d01bSPeter Zijlstra 
1809547d01bSPeter Zijlstra static int tlb_next_batch(struct mmu_gather *tlb)
1819547d01bSPeter Zijlstra {
1829547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
1839547d01bSPeter Zijlstra 
1849547d01bSPeter Zijlstra 	batch = tlb->active;
1859547d01bSPeter Zijlstra 	if (batch->next) {
1869547d01bSPeter Zijlstra 		tlb->active = batch->next;
1879547d01bSPeter Zijlstra 		return 1;
1889547d01bSPeter Zijlstra 	}
1899547d01bSPeter Zijlstra 
19053a59fc6SMichal Hocko 	if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
19153a59fc6SMichal Hocko 		return 0;
19253a59fc6SMichal Hocko 
1939547d01bSPeter Zijlstra 	batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
1949547d01bSPeter Zijlstra 	if (!batch)
1959547d01bSPeter Zijlstra 		return 0;
1969547d01bSPeter Zijlstra 
19753a59fc6SMichal Hocko 	tlb->batch_count++;
1989547d01bSPeter Zijlstra 	batch->next = NULL;
1999547d01bSPeter Zijlstra 	batch->nr   = 0;
2009547d01bSPeter Zijlstra 	batch->max  = MAX_GATHER_BATCH;
2019547d01bSPeter Zijlstra 
2029547d01bSPeter Zijlstra 	tlb->active->next = batch;
2039547d01bSPeter Zijlstra 	tlb->active = batch;
2049547d01bSPeter Zijlstra 
2059547d01bSPeter Zijlstra 	return 1;
2069547d01bSPeter Zijlstra }
2079547d01bSPeter Zijlstra 
2089547d01bSPeter Zijlstra /* tlb_gather_mmu
2099547d01bSPeter Zijlstra  *	Called to initialize an (on-stack) mmu_gather structure for page-table
2109547d01bSPeter Zijlstra  *	tear-down from @mm. The @fullmm argument is used when @mm is without
2119547d01bSPeter Zijlstra  *	users and we're going to destroy the full address space (exit/execve).
2129547d01bSPeter Zijlstra  */
2132b047252SLinus Torvalds void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
2149547d01bSPeter Zijlstra {
2159547d01bSPeter Zijlstra 	tlb->mm = mm;
2169547d01bSPeter Zijlstra 
2172b047252SLinus Torvalds 	/* Is it from 0 to ~0? */
2182b047252SLinus Torvalds 	tlb->fullmm     = !(start | (end+1));
2191de14c3cSDave Hansen 	tlb->need_flush_all = 0;
2202b047252SLinus Torvalds 	tlb->start	= start;
2212b047252SLinus Torvalds 	tlb->end	= end;
2229547d01bSPeter Zijlstra 	tlb->need_flush = 0;
2239547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2249547d01bSPeter Zijlstra 	tlb->local.nr   = 0;
2259547d01bSPeter Zijlstra 	tlb->local.max  = ARRAY_SIZE(tlb->__pages);
2269547d01bSPeter Zijlstra 	tlb->active     = &tlb->local;
22753a59fc6SMichal Hocko 	tlb->batch_count = 0;
2289547d01bSPeter Zijlstra 
2299547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2309547d01bSPeter Zijlstra 	tlb->batch = NULL;
23134e55232SKAMEZAWA Hiroyuki #endif
2329547d01bSPeter Zijlstra }
2339547d01bSPeter Zijlstra 
2349547d01bSPeter Zijlstra void tlb_flush_mmu(struct mmu_gather *tlb)
2359547d01bSPeter Zijlstra {
2369547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
2379547d01bSPeter Zijlstra 
2389547d01bSPeter Zijlstra 	if (!tlb->need_flush)
2399547d01bSPeter Zijlstra 		return;
2409547d01bSPeter Zijlstra 	tlb->need_flush = 0;
2419547d01bSPeter Zijlstra 	tlb_flush(tlb);
2429547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2439547d01bSPeter Zijlstra 	tlb_table_flush(tlb);
2449547d01bSPeter Zijlstra #endif
2459547d01bSPeter Zijlstra 
2469547d01bSPeter Zijlstra 	for (batch = &tlb->local; batch; batch = batch->next) {
2479547d01bSPeter Zijlstra 		free_pages_and_swap_cache(batch->pages, batch->nr);
2489547d01bSPeter Zijlstra 		batch->nr = 0;
2499547d01bSPeter Zijlstra 	}
2509547d01bSPeter Zijlstra 	tlb->active = &tlb->local;
2519547d01bSPeter Zijlstra }
2529547d01bSPeter Zijlstra 
2539547d01bSPeter Zijlstra /* tlb_finish_mmu
2549547d01bSPeter Zijlstra  *	Called at the end of the shootdown operation to free up any resources
2559547d01bSPeter Zijlstra  *	that were required.
2569547d01bSPeter Zijlstra  */
2579547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
2589547d01bSPeter Zijlstra {
2599547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch, *next;
2609547d01bSPeter Zijlstra 
2619547d01bSPeter Zijlstra 	tlb_flush_mmu(tlb);
2629547d01bSPeter Zijlstra 
2639547d01bSPeter Zijlstra 	/* keep the page table cache within bounds */
2649547d01bSPeter Zijlstra 	check_pgt_cache();
2659547d01bSPeter Zijlstra 
2669547d01bSPeter Zijlstra 	for (batch = tlb->local.next; batch; batch = next) {
2679547d01bSPeter Zijlstra 		next = batch->next;
2689547d01bSPeter Zijlstra 		free_pages((unsigned long)batch, 0);
2699547d01bSPeter Zijlstra 	}
2709547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2719547d01bSPeter Zijlstra }
2729547d01bSPeter Zijlstra 
2739547d01bSPeter Zijlstra /* __tlb_remove_page
2749547d01bSPeter Zijlstra  *	Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
2759547d01bSPeter Zijlstra  *	handling the additional races in SMP caused by other CPUs caching valid
2769547d01bSPeter Zijlstra  *	mappings in their TLBs. Returns the number of free page slots left.
2779547d01bSPeter Zijlstra  *	When out of page slots we must call tlb_flush_mmu().
2789547d01bSPeter Zijlstra  */
2799547d01bSPeter Zijlstra int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
2809547d01bSPeter Zijlstra {
2819547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
2829547d01bSPeter Zijlstra 
283f21760b1SShaohua Li 	VM_BUG_ON(!tlb->need_flush);
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  * Note: this doesn't free the actual pages themselves. That
3781da177e4SLinus Torvalds  * has been handled earlier when unmapping all the memory regions.
3791da177e4SLinus Torvalds  */
3809e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
3819e1b32caSBenjamin Herrenschmidt 			   unsigned long addr)
3821da177e4SLinus Torvalds {
3832f569afdSMartin Schwidefsky 	pgtable_t token = pmd_pgtable(*pmd);
3841da177e4SLinus Torvalds 	pmd_clear(pmd);
3859e1b32caSBenjamin Herrenschmidt 	pte_free_tlb(tlb, token, addr);
386e1f56c89SKirill A. Shutemov 	atomic_long_dec(&tlb->mm->nr_ptes);
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
389e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
390e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
391e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	pmd_t *pmd;
3941da177e4SLinus Torvalds 	unsigned long next;
395e0da382cSHugh Dickins 	unsigned long start;
3961da177e4SLinus Torvalds 
397e0da382cSHugh Dickins 	start = addr;
3981da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
3991da177e4SLinus Torvalds 	do {
4001da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
4011da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
4021da177e4SLinus Torvalds 			continue;
4039e1b32caSBenjamin Herrenschmidt 		free_pte_range(tlb, pmd, addr);
4041da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
4051da177e4SLinus Torvalds 
406e0da382cSHugh Dickins 	start &= PUD_MASK;
407e0da382cSHugh Dickins 	if (start < floor)
408e0da382cSHugh Dickins 		return;
409e0da382cSHugh Dickins 	if (ceiling) {
410e0da382cSHugh Dickins 		ceiling &= PUD_MASK;
411e0da382cSHugh Dickins 		if (!ceiling)
412e0da382cSHugh Dickins 			return;
4131da177e4SLinus Torvalds 	}
414e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
415e0da382cSHugh Dickins 		return;
416e0da382cSHugh Dickins 
417e0da382cSHugh Dickins 	pmd = pmd_offset(pud, start);
418e0da382cSHugh Dickins 	pud_clear(pud);
4199e1b32caSBenjamin Herrenschmidt 	pmd_free_tlb(tlb, pmd, start);
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
422e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
423e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
424e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	pud_t *pud;
4271da177e4SLinus Torvalds 	unsigned long next;
428e0da382cSHugh Dickins 	unsigned long start;
4291da177e4SLinus Torvalds 
430e0da382cSHugh Dickins 	start = addr;
4311da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
4321da177e4SLinus Torvalds 	do {
4331da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
4341da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
4351da177e4SLinus Torvalds 			continue;
436e0da382cSHugh Dickins 		free_pmd_range(tlb, pud, addr, next, floor, ceiling);
4371da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
4381da177e4SLinus Torvalds 
439e0da382cSHugh Dickins 	start &= PGDIR_MASK;
440e0da382cSHugh Dickins 	if (start < floor)
441e0da382cSHugh Dickins 		return;
442e0da382cSHugh Dickins 	if (ceiling) {
443e0da382cSHugh Dickins 		ceiling &= PGDIR_MASK;
444e0da382cSHugh Dickins 		if (!ceiling)
445e0da382cSHugh Dickins 			return;
4461da177e4SLinus Torvalds 	}
447e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
448e0da382cSHugh Dickins 		return;
449e0da382cSHugh Dickins 
450e0da382cSHugh Dickins 	pud = pud_offset(pgd, start);
451e0da382cSHugh Dickins 	pgd_clear(pgd);
4529e1b32caSBenjamin Herrenschmidt 	pud_free_tlb(tlb, pud, start);
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /*
456e0da382cSHugh Dickins  * This function frees user-level page tables of a process.
4571da177e4SLinus Torvalds  */
45842b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb,
459e0da382cSHugh Dickins 			unsigned long addr, unsigned long end,
460e0da382cSHugh Dickins 			unsigned long floor, unsigned long ceiling)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	pgd_t *pgd;
4631da177e4SLinus Torvalds 	unsigned long next;
4641da177e4SLinus Torvalds 
465e0da382cSHugh Dickins 	/*
466e0da382cSHugh Dickins 	 * The next few lines have given us lots of grief...
467e0da382cSHugh Dickins 	 *
468e0da382cSHugh Dickins 	 * Why are we testing PMD* at this top level?  Because often
469e0da382cSHugh Dickins 	 * there will be no work to do at all, and we'd prefer not to
470e0da382cSHugh Dickins 	 * go all the way down to the bottom just to discover that.
471e0da382cSHugh Dickins 	 *
472e0da382cSHugh Dickins 	 * Why all these "- 1"s?  Because 0 represents both the bottom
473e0da382cSHugh Dickins 	 * of the address space and the top of it (using -1 for the
474e0da382cSHugh Dickins 	 * top wouldn't help much: the masks would do the wrong thing).
475e0da382cSHugh Dickins 	 * The rule is that addr 0 and floor 0 refer to the bottom of
476e0da382cSHugh Dickins 	 * the address space, but end 0 and ceiling 0 refer to the top
477e0da382cSHugh Dickins 	 * Comparisons need to use "end - 1" and "ceiling - 1" (though
478e0da382cSHugh Dickins 	 * that end 0 case should be mythical).
479e0da382cSHugh Dickins 	 *
480e0da382cSHugh Dickins 	 * Wherever addr is brought up or ceiling brought down, we must
481e0da382cSHugh Dickins 	 * be careful to reject "the opposite 0" before it confuses the
482e0da382cSHugh Dickins 	 * subsequent tests.  But what about where end is brought down
483e0da382cSHugh Dickins 	 * by PMD_SIZE below? no, end can't go down to 0 there.
484e0da382cSHugh Dickins 	 *
485e0da382cSHugh Dickins 	 * Whereas we round start (addr) and ceiling down, by different
486e0da382cSHugh Dickins 	 * masks at different levels, in order to test whether a table
487e0da382cSHugh Dickins 	 * now has no other vmas using it, so can be freed, we don't
488e0da382cSHugh Dickins 	 * bother to round floor or end up - the tests don't need that.
489e0da382cSHugh Dickins 	 */
490e0da382cSHugh Dickins 
491e0da382cSHugh Dickins 	addr &= PMD_MASK;
492e0da382cSHugh Dickins 	if (addr < floor) {
493e0da382cSHugh Dickins 		addr += PMD_SIZE;
494e0da382cSHugh Dickins 		if (!addr)
495e0da382cSHugh Dickins 			return;
496e0da382cSHugh Dickins 	}
497e0da382cSHugh Dickins 	if (ceiling) {
498e0da382cSHugh Dickins 		ceiling &= PMD_MASK;
499e0da382cSHugh Dickins 		if (!ceiling)
500e0da382cSHugh Dickins 			return;
501e0da382cSHugh Dickins 	}
502e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
503e0da382cSHugh Dickins 		end -= PMD_SIZE;
504e0da382cSHugh Dickins 	if (addr > end - 1)
505e0da382cSHugh Dickins 		return;
506e0da382cSHugh Dickins 
50742b77728SJan Beulich 	pgd = pgd_offset(tlb->mm, addr);
5081da177e4SLinus Torvalds 	do {
5091da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
5101da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
5111da177e4SLinus Torvalds 			continue;
51242b77728SJan Beulich 		free_pud_range(tlb, pgd, addr, next, floor, ceiling);
5131da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
514e0da382cSHugh Dickins }
515e0da382cSHugh Dickins 
51642b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
517e0da382cSHugh Dickins 		unsigned long floor, unsigned long ceiling)
518e0da382cSHugh Dickins {
519e0da382cSHugh Dickins 	while (vma) {
520e0da382cSHugh Dickins 		struct vm_area_struct *next = vma->vm_next;
521e0da382cSHugh Dickins 		unsigned long addr = vma->vm_start;
522e0da382cSHugh Dickins 
5238f4f8c16SHugh Dickins 		/*
52425d9e2d1Snpiggin@suse.de 		 * Hide vma from rmap and truncate_pagecache before freeing
52525d9e2d1Snpiggin@suse.de 		 * pgtables
5268f4f8c16SHugh Dickins 		 */
5275beb4930SRik van Riel 		unlink_anon_vmas(vma);
5288f4f8c16SHugh Dickins 		unlink_file_vma(vma);
5298f4f8c16SHugh Dickins 
5309da61aefSDavid Gibson 		if (is_vm_hugetlb_page(vma)) {
5313bf5ee95SHugh Dickins 			hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
5323bf5ee95SHugh Dickins 				floor, next? next->vm_start: ceiling);
5333bf5ee95SHugh Dickins 		} else {
5343bf5ee95SHugh Dickins 			/*
5353bf5ee95SHugh Dickins 			 * Optimization: gather nearby vmas into one call down
5363bf5ee95SHugh Dickins 			 */
5373bf5ee95SHugh Dickins 			while (next && next->vm_start <= vma->vm_end + PMD_SIZE
5384866920bSDavid Gibson 			       && !is_vm_hugetlb_page(next)) {
539e0da382cSHugh Dickins 				vma = next;
540e0da382cSHugh Dickins 				next = vma->vm_next;
5415beb4930SRik van Riel 				unlink_anon_vmas(vma);
5428f4f8c16SHugh Dickins 				unlink_file_vma(vma);
543e0da382cSHugh Dickins 			}
5443bf5ee95SHugh Dickins 			free_pgd_range(tlb, addr, vma->vm_end,
545e0da382cSHugh Dickins 				floor, next? next->vm_start: ceiling);
5463bf5ee95SHugh Dickins 		}
547e0da382cSHugh Dickins 		vma = next;
548e0da382cSHugh Dickins 	}
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5518ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
5528ac1f832SAndrea Arcangeli 		pmd_t *pmd, unsigned long address)
5531da177e4SLinus Torvalds {
554c4088ebdSKirill A. Shutemov 	spinlock_t *ptl;
5552f569afdSMartin Schwidefsky 	pgtable_t new = pte_alloc_one(mm, address);
5568ac1f832SAndrea Arcangeli 	int wait_split_huge_page;
5571da177e4SLinus Torvalds 	if (!new)
5581bb3630eSHugh Dickins 		return -ENOMEM;
5591bb3630eSHugh Dickins 
560362a61adSNick Piggin 	/*
561362a61adSNick Piggin 	 * Ensure all pte setup (eg. pte page lock and page clearing) are
562362a61adSNick Piggin 	 * visible before the pte is made visible to other CPUs by being
563362a61adSNick Piggin 	 * put into page tables.
564362a61adSNick Piggin 	 *
565362a61adSNick Piggin 	 * The other side of the story is the pointer chasing in the page
566362a61adSNick Piggin 	 * table walking code (when walking the page table without locking;
567362a61adSNick Piggin 	 * ie. most of the time). Fortunately, these data accesses consist
568362a61adSNick Piggin 	 * of a chain of data-dependent loads, meaning most CPUs (alpha
569362a61adSNick Piggin 	 * being the notable exception) will already guarantee loads are
570362a61adSNick Piggin 	 * seen in-order. See the alpha page table accessors for the
571362a61adSNick Piggin 	 * smp_read_barrier_depends() barriers in page table walking code.
572362a61adSNick Piggin 	 */
573362a61adSNick Piggin 	smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
574362a61adSNick Piggin 
575c4088ebdSKirill A. Shutemov 	ptl = pmd_lock(mm, pmd);
5768ac1f832SAndrea Arcangeli 	wait_split_huge_page = 0;
5778ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
578e1f56c89SKirill A. Shutemov 		atomic_long_inc(&mm->nr_ptes);
5791da177e4SLinus Torvalds 		pmd_populate(mm, pmd, new);
5802f569afdSMartin Schwidefsky 		new = NULL;
5818ac1f832SAndrea Arcangeli 	} else if (unlikely(pmd_trans_splitting(*pmd)))
5828ac1f832SAndrea Arcangeli 		wait_split_huge_page = 1;
583c4088ebdSKirill A. Shutemov 	spin_unlock(ptl);
5842f569afdSMartin Schwidefsky 	if (new)
5852f569afdSMartin Schwidefsky 		pte_free(mm, new);
5868ac1f832SAndrea Arcangeli 	if (wait_split_huge_page)
5878ac1f832SAndrea Arcangeli 		wait_split_huge_page(vma->anon_vma, pmd);
5881bb3630eSHugh Dickins 	return 0;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
5921da177e4SLinus Torvalds {
5931bb3630eSHugh Dickins 	pte_t *new = pte_alloc_one_kernel(&init_mm, address);
5941da177e4SLinus Torvalds 	if (!new)
5951bb3630eSHugh Dickins 		return -ENOMEM;
5961da177e4SLinus Torvalds 
597362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
598362a61adSNick Piggin 
599872fec16SHugh Dickins 	spin_lock(&init_mm.page_table_lock);
6008ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
601872fec16SHugh Dickins 		pmd_populate_kernel(&init_mm, pmd, new);
6022f569afdSMartin Schwidefsky 		new = NULL;
6038ac1f832SAndrea Arcangeli 	} else
6048ac1f832SAndrea Arcangeli 		VM_BUG_ON(pmd_trans_splitting(*pmd));
605872fec16SHugh Dickins 	spin_unlock(&init_mm.page_table_lock);
6062f569afdSMartin Schwidefsky 	if (new)
6072f569afdSMartin Schwidefsky 		pte_free_kernel(&init_mm, new);
6081bb3630eSHugh Dickins 	return 0;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
611d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss)
612ae859762SHugh Dickins {
613d559db08SKAMEZAWA Hiroyuki 	memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
614d559db08SKAMEZAWA Hiroyuki }
615d559db08SKAMEZAWA Hiroyuki 
616d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
617d559db08SKAMEZAWA Hiroyuki {
618d559db08SKAMEZAWA Hiroyuki 	int i;
619d559db08SKAMEZAWA Hiroyuki 
62034e55232SKAMEZAWA Hiroyuki 	if (current->mm == mm)
62105af2e10SDavid Rientjes 		sync_mm_rss(mm);
622d559db08SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++)
623d559db08SKAMEZAWA Hiroyuki 		if (rss[i])
624d559db08SKAMEZAWA Hiroyuki 			add_mm_counter(mm, i, rss[i]);
625ae859762SHugh Dickins }
626ae859762SHugh Dickins 
6271da177e4SLinus Torvalds /*
6286aab341eSLinus Torvalds  * This function is called to print an error when a bad pte
6296aab341eSLinus Torvalds  * is found. For example, we might have a PFN-mapped pte in
6306aab341eSLinus Torvalds  * a region that doesn't allow it.
631b5810039SNick Piggin  *
632b5810039SNick Piggin  * The calling function must still handle the error.
633b5810039SNick Piggin  */
6343dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
6353dc14741SHugh Dickins 			  pte_t pte, struct page *page)
636b5810039SNick Piggin {
6373dc14741SHugh Dickins 	pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
6383dc14741SHugh Dickins 	pud_t *pud = pud_offset(pgd, addr);
6393dc14741SHugh Dickins 	pmd_t *pmd = pmd_offset(pud, addr);
6403dc14741SHugh Dickins 	struct address_space *mapping;
6413dc14741SHugh Dickins 	pgoff_t index;
642d936cf9bSHugh Dickins 	static unsigned long resume;
643d936cf9bSHugh Dickins 	static unsigned long nr_shown;
644d936cf9bSHugh Dickins 	static unsigned long nr_unshown;
645d936cf9bSHugh Dickins 
646d936cf9bSHugh Dickins 	/*
647d936cf9bSHugh Dickins 	 * Allow a burst of 60 reports, then keep quiet for that minute;
648d936cf9bSHugh Dickins 	 * or allow a steady drip of one report per second.
649d936cf9bSHugh Dickins 	 */
650d936cf9bSHugh Dickins 	if (nr_shown == 60) {
651d936cf9bSHugh Dickins 		if (time_before(jiffies, resume)) {
652d936cf9bSHugh Dickins 			nr_unshown++;
653d936cf9bSHugh Dickins 			return;
654d936cf9bSHugh Dickins 		}
655d936cf9bSHugh Dickins 		if (nr_unshown) {
6561e9e6365SHugh Dickins 			printk(KERN_ALERT
6571e9e6365SHugh Dickins 				"BUG: Bad page map: %lu messages suppressed\n",
658d936cf9bSHugh Dickins 				nr_unshown);
659d936cf9bSHugh Dickins 			nr_unshown = 0;
660d936cf9bSHugh Dickins 		}
661d936cf9bSHugh Dickins 		nr_shown = 0;
662d936cf9bSHugh Dickins 	}
663d936cf9bSHugh Dickins 	if (nr_shown++ == 0)
664d936cf9bSHugh Dickins 		resume = jiffies + 60 * HZ;
6653dc14741SHugh Dickins 
6663dc14741SHugh Dickins 	mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
6673dc14741SHugh Dickins 	index = linear_page_index(vma, addr);
6683dc14741SHugh Dickins 
6691e9e6365SHugh Dickins 	printk(KERN_ALERT
6701e9e6365SHugh Dickins 		"BUG: Bad page map in process %s  pte:%08llx pmd:%08llx\n",
6713dc14741SHugh Dickins 		current->comm,
6723dc14741SHugh Dickins 		(long long)pte_val(pte), (long long)pmd_val(*pmd));
673718a3821SWu Fengguang 	if (page)
674*f0b791a3SDave Hansen 		dump_page(page, "bad pte");
6751e9e6365SHugh Dickins 	printk(KERN_ALERT
6763dc14741SHugh Dickins 		"addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
6773dc14741SHugh Dickins 		(void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
6783dc14741SHugh Dickins 	/*
6793dc14741SHugh Dickins 	 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
6803dc14741SHugh Dickins 	 */
6813dc14741SHugh Dickins 	if (vma->vm_ops)
682071361d3SJoe Perches 		printk(KERN_ALERT "vma->vm_ops->fault: %pSR\n",
683071361d3SJoe Perches 		       vma->vm_ops->fault);
68472c2d531SAl Viro 	if (vma->vm_file)
685071361d3SJoe Perches 		printk(KERN_ALERT "vma->vm_file->f_op->mmap: %pSR\n",
686071361d3SJoe Perches 		       vma->vm_file->f_op->mmap);
687b5810039SNick Piggin 	dump_stack();
688373d4d09SRusty Russell 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
689b5810039SNick Piggin }
690b5810039SNick Piggin 
6912ec74c3eSSagi Grimberg static inline bool is_cow_mapping(vm_flags_t flags)
69267121172SLinus Torvalds {
69367121172SLinus Torvalds 	return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
69467121172SLinus Torvalds }
69567121172SLinus Torvalds 
696b5810039SNick Piggin /*
6977e675137SNick Piggin  * vm_normal_page -- This function gets the "struct page" associated with a pte.
6986aab341eSLinus Torvalds  *
6997e675137SNick Piggin  * "Special" mappings do not wish to be associated with a "struct page" (either
7007e675137SNick Piggin  * it doesn't exist, or it exists but they don't want to touch it). In this
7017e675137SNick Piggin  * case, NULL is returned here. "Normal" mappings do have a struct page.
702b379d790SJared Hulbert  *
7037e675137SNick Piggin  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
7047e675137SNick Piggin  * pte bit, in which case this function is trivial. Secondly, an architecture
7057e675137SNick Piggin  * may not have a spare pte bit, which requires a more complicated scheme,
7067e675137SNick Piggin  * described below.
7077e675137SNick Piggin  *
7087e675137SNick Piggin  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
7097e675137SNick Piggin  * special mapping (even if there are underlying and valid "struct pages").
7107e675137SNick Piggin  * COWed pages of a VM_PFNMAP are always normal.
7116aab341eSLinus Torvalds  *
712b379d790SJared Hulbert  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
713b379d790SJared Hulbert  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
7147e675137SNick Piggin  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
7157e675137SNick Piggin  * mapping will always honor the rule
7166aab341eSLinus Torvalds  *
7176aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
7186aab341eSLinus Torvalds  *
7197e675137SNick Piggin  * And for normal mappings this is false.
720b379d790SJared Hulbert  *
7217e675137SNick Piggin  * This restricts such mappings to be a linear translation from virtual address
7227e675137SNick Piggin  * to pfn. To get around this restriction, we allow arbitrary mappings so long
7237e675137SNick Piggin  * as the vma is not a COW mapping; in that case, we know that all ptes are
7247e675137SNick Piggin  * special (because none can have been COWed).
725b379d790SJared Hulbert  *
726b379d790SJared Hulbert  *
7277e675137SNick Piggin  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
7287e675137SNick Piggin  *
729b379d790SJared Hulbert  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
730b379d790SJared Hulbert  * page" backing, however the difference is that _all_ pages with a struct
731b379d790SJared Hulbert  * page (that is, those where pfn_valid is true) are refcounted and considered
732b379d790SJared Hulbert  * normal pages by the VM. The disadvantage is that pages are refcounted
733b379d790SJared Hulbert  * (which can be slower and simply not an option for some PFNMAP users). The
734b379d790SJared Hulbert  * advantage is that we don't have to follow the strict linearity rule of
735b379d790SJared Hulbert  * PFNMAP mappings in order to support COWable mappings.
736b379d790SJared Hulbert  *
737ee498ed7SHugh Dickins  */
7387e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL
7397e675137SNick Piggin # define HAVE_PTE_SPECIAL 1
7407e675137SNick Piggin #else
7417e675137SNick Piggin # define HAVE_PTE_SPECIAL 0
7427e675137SNick Piggin #endif
7437e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
7447e675137SNick Piggin 				pte_t pte)
745ee498ed7SHugh Dickins {
74622b31eecSHugh Dickins 	unsigned long pfn = pte_pfn(pte);
7477e675137SNick Piggin 
7487e675137SNick Piggin 	if (HAVE_PTE_SPECIAL) {
74922b31eecSHugh Dickins 		if (likely(!pte_special(pte)))
75022b31eecSHugh Dickins 			goto check_pfn;
751a13ea5b7SHugh Dickins 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
752a13ea5b7SHugh Dickins 			return NULL;
75362eede62SHugh Dickins 		if (!is_zero_pfn(pfn))
75422b31eecSHugh Dickins 			print_bad_pte(vma, addr, pte, NULL);
7557e675137SNick Piggin 		return NULL;
7567e675137SNick Piggin 	}
7577e675137SNick Piggin 
7587e675137SNick Piggin 	/* !HAVE_PTE_SPECIAL case follows: */
7597e675137SNick Piggin 
760b379d790SJared Hulbert 	if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
761b379d790SJared Hulbert 		if (vma->vm_flags & VM_MIXEDMAP) {
762b379d790SJared Hulbert 			if (!pfn_valid(pfn))
763b379d790SJared Hulbert 				return NULL;
764b379d790SJared Hulbert 			goto out;
765b379d790SJared Hulbert 		} else {
7667e675137SNick Piggin 			unsigned long off;
7677e675137SNick Piggin 			off = (addr - vma->vm_start) >> PAGE_SHIFT;
7686aab341eSLinus Torvalds 			if (pfn == vma->vm_pgoff + off)
7696aab341eSLinus Torvalds 				return NULL;
77067121172SLinus Torvalds 			if (!is_cow_mapping(vma->vm_flags))
771fb155c16SLinus Torvalds 				return NULL;
7726aab341eSLinus Torvalds 		}
773b379d790SJared Hulbert 	}
7746aab341eSLinus Torvalds 
77562eede62SHugh Dickins 	if (is_zero_pfn(pfn))
77662eede62SHugh Dickins 		return NULL;
77722b31eecSHugh Dickins check_pfn:
77822b31eecSHugh Dickins 	if (unlikely(pfn > highest_memmap_pfn)) {
77922b31eecSHugh Dickins 		print_bad_pte(vma, addr, pte, NULL);
78022b31eecSHugh Dickins 		return NULL;
78122b31eecSHugh Dickins 	}
7826aab341eSLinus Torvalds 
7836aab341eSLinus Torvalds 	/*
7847e675137SNick Piggin 	 * NOTE! We still have PageReserved() pages in the page tables.
7857e675137SNick Piggin 	 * eg. VDSO mappings can cause them to exist.
7866aab341eSLinus Torvalds 	 */
787b379d790SJared Hulbert out:
7886aab341eSLinus Torvalds 	return pfn_to_page(pfn);
789ee498ed7SHugh Dickins }
790ee498ed7SHugh Dickins 
791ee498ed7SHugh Dickins /*
7921da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
7931da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
7941da177e4SLinus Torvalds  * covered by this vma.
7951da177e4SLinus Torvalds  */
7961da177e4SLinus Torvalds 
797570a335bSHugh Dickins static inline unsigned long
7981da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
799b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
8008c103762SHugh Dickins 		unsigned long addr, int *rss)
8011da177e4SLinus Torvalds {
802b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
8031da177e4SLinus Torvalds 	pte_t pte = *src_pte;
8041da177e4SLinus Torvalds 	struct page *page;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
8071da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
8081da177e4SLinus Torvalds 		if (!pte_file(pte)) {
8090697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(pte);
8100697212aSChristoph Lameter 
811570a335bSHugh Dickins 			if (swap_duplicate(entry) < 0)
812570a335bSHugh Dickins 				return entry.val;
813570a335bSHugh Dickins 
8141da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
8151da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
8161da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
817f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
818f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
819f412ac08SHugh Dickins 						 &src_mm->mmlist);
8201da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
8211da177e4SLinus Torvalds 			}
822b084d435SKAMEZAWA Hiroyuki 			if (likely(!non_swap_entry(entry)))
823b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]++;
8249f9f1acdSKonstantin Khlebnikov 			else if (is_migration_entry(entry)) {
8259f9f1acdSKonstantin Khlebnikov 				page = migration_entry_to_page(entry);
8269f9f1acdSKonstantin Khlebnikov 
8279f9f1acdSKonstantin Khlebnikov 				if (PageAnon(page))
8289f9f1acdSKonstantin Khlebnikov 					rss[MM_ANONPAGES]++;
8299f9f1acdSKonstantin Khlebnikov 				else
8309f9f1acdSKonstantin Khlebnikov 					rss[MM_FILEPAGES]++;
8319f9f1acdSKonstantin Khlebnikov 
8329f9f1acdSKonstantin Khlebnikov 				if (is_write_migration_entry(entry) &&
8330697212aSChristoph Lameter 				    is_cow_mapping(vm_flags)) {
8340697212aSChristoph Lameter 					/*
8359f9f1acdSKonstantin Khlebnikov 					 * COW mappings require pages in both
8369f9f1acdSKonstantin Khlebnikov 					 * parent and child to be set to read.
8370697212aSChristoph Lameter 					 */
8380697212aSChristoph Lameter 					make_migration_entry_read(&entry);
8390697212aSChristoph Lameter 					pte = swp_entry_to_pte(entry);
840c3d16e16SCyrill Gorcunov 					if (pte_swp_soft_dirty(*src_pte))
841c3d16e16SCyrill Gorcunov 						pte = pte_swp_mksoft_dirty(pte);
8420697212aSChristoph Lameter 					set_pte_at(src_mm, addr, src_pte, pte);
8430697212aSChristoph Lameter 				}
8441da177e4SLinus Torvalds 			}
8459f9f1acdSKonstantin Khlebnikov 		}
846ae859762SHugh Dickins 		goto out_set_pte;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	/*
8501da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
8511da177e4SLinus Torvalds 	 * in the parent and the child
8521da177e4SLinus Torvalds 	 */
85367121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
8541da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
8553dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	/*
8591da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
8601da177e4SLinus Torvalds 	 * the child
8611da177e4SLinus Torvalds 	 */
8621da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
8631da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
8641da177e4SLinus Torvalds 	pte = pte_mkold(pte);
8656aab341eSLinus Torvalds 
8666aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
8676aab341eSLinus Torvalds 	if (page) {
8681da177e4SLinus Torvalds 		get_page(page);
86921333b2bSHugh Dickins 		page_dup_rmap(page);
870d559db08SKAMEZAWA Hiroyuki 		if (PageAnon(page))
871d559db08SKAMEZAWA Hiroyuki 			rss[MM_ANONPAGES]++;
872d559db08SKAMEZAWA Hiroyuki 		else
873d559db08SKAMEZAWA Hiroyuki 			rss[MM_FILEPAGES]++;
8746aab341eSLinus Torvalds 	}
875ae859762SHugh Dickins 
876ae859762SHugh Dickins out_set_pte:
877ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
878570a335bSHugh Dickins 	return 0;
8791da177e4SLinus Torvalds }
8801da177e4SLinus Torvalds 
88171e3aac0SAndrea Arcangeli int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
8821da177e4SLinus Torvalds 		   pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
8831da177e4SLinus Torvalds 		   unsigned long addr, unsigned long end)
8841da177e4SLinus Torvalds {
885c36987e2SDaisuke Nishimura 	pte_t *orig_src_pte, *orig_dst_pte;
8861da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
887c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
888e040f218SHugh Dickins 	int progress = 0;
889d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
890570a335bSHugh Dickins 	swp_entry_t entry = (swp_entry_t){0};
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds again:
893d559db08SKAMEZAWA Hiroyuki 	init_rss_vec(rss);
894d559db08SKAMEZAWA Hiroyuki 
895c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
8961da177e4SLinus Torvalds 	if (!dst_pte)
8971da177e4SLinus Torvalds 		return -ENOMEM;
898ece0e2b6SPeter Zijlstra 	src_pte = pte_offset_map(src_pmd, addr);
8994c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
900f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
901c36987e2SDaisuke Nishimura 	orig_src_pte = src_pte;
902c36987e2SDaisuke Nishimura 	orig_dst_pte = dst_pte;
9036606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	do {
9061da177e4SLinus Torvalds 		/*
9071da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
9081da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
9091da177e4SLinus Torvalds 		 */
910e040f218SHugh Dickins 		if (progress >= 32) {
911e040f218SHugh Dickins 			progress = 0;
912e040f218SHugh Dickins 			if (need_resched() ||
91395c354feSNick Piggin 			    spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
9141da177e4SLinus Torvalds 				break;
915e040f218SHugh Dickins 		}
9161da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
9171da177e4SLinus Torvalds 			progress++;
9181da177e4SLinus Torvalds 			continue;
9191da177e4SLinus Torvalds 		}
920570a335bSHugh Dickins 		entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
921570a335bSHugh Dickins 							vma, addr, rss);
922570a335bSHugh Dickins 		if (entry.val)
923570a335bSHugh Dickins 			break;
9241da177e4SLinus Torvalds 		progress += 8;
9251da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
9261da177e4SLinus Torvalds 
9276606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
928c74df32cSHugh Dickins 	spin_unlock(src_ptl);
929ece0e2b6SPeter Zijlstra 	pte_unmap(orig_src_pte);
930d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(dst_mm, rss);
931c36987e2SDaisuke Nishimura 	pte_unmap_unlock(orig_dst_pte, dst_ptl);
932c74df32cSHugh Dickins 	cond_resched();
933570a335bSHugh Dickins 
934570a335bSHugh Dickins 	if (entry.val) {
935570a335bSHugh Dickins 		if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
936570a335bSHugh Dickins 			return -ENOMEM;
937570a335bSHugh Dickins 		progress = 0;
938570a335bSHugh Dickins 	}
9391da177e4SLinus Torvalds 	if (addr != end)
9401da177e4SLinus Torvalds 		goto again;
9411da177e4SLinus Torvalds 	return 0;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9451da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
9461da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9471da177e4SLinus Torvalds {
9481da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
9491da177e4SLinus Torvalds 	unsigned long next;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
9521da177e4SLinus Torvalds 	if (!dst_pmd)
9531da177e4SLinus Torvalds 		return -ENOMEM;
9541da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
9551da177e4SLinus Torvalds 	do {
9561da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
95771e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*src_pmd)) {
95871e3aac0SAndrea Arcangeli 			int err;
95914d1a55cSAndrea Arcangeli 			VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
96071e3aac0SAndrea Arcangeli 			err = copy_huge_pmd(dst_mm, src_mm,
96171e3aac0SAndrea Arcangeli 					    dst_pmd, src_pmd, addr, vma);
96271e3aac0SAndrea Arcangeli 			if (err == -ENOMEM)
96371e3aac0SAndrea Arcangeli 				return -ENOMEM;
96471e3aac0SAndrea Arcangeli 			if (!err)
96571e3aac0SAndrea Arcangeli 				continue;
96671e3aac0SAndrea Arcangeli 			/* fall through */
96771e3aac0SAndrea Arcangeli 		}
9681da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
9691da177e4SLinus Torvalds 			continue;
9701da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
9711da177e4SLinus Torvalds 						vma, addr, next))
9721da177e4SLinus Torvalds 			return -ENOMEM;
9731da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
9741da177e4SLinus Torvalds 	return 0;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9781da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
9791da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
9821da177e4SLinus Torvalds 	unsigned long next;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
9851da177e4SLinus Torvalds 	if (!dst_pud)
9861da177e4SLinus Torvalds 		return -ENOMEM;
9871da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
9881da177e4SLinus Torvalds 	do {
9891da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
9901da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
9911da177e4SLinus Torvalds 			continue;
9921da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
9931da177e4SLinus Torvalds 						vma, addr, next))
9941da177e4SLinus Torvalds 			return -ENOMEM;
9951da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
9961da177e4SLinus Torvalds 	return 0;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10001da177e4SLinus Torvalds 		struct vm_area_struct *vma)
10011da177e4SLinus Torvalds {
10021da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
10031da177e4SLinus Torvalds 	unsigned long next;
10041da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
10051da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
10062ec74c3eSSagi Grimberg 	unsigned long mmun_start;	/* For mmu_notifiers */
10072ec74c3eSSagi Grimberg 	unsigned long mmun_end;		/* For mmu_notifiers */
10082ec74c3eSSagi Grimberg 	bool is_cow;
1009cddb8a5cSAndrea Arcangeli 	int ret;
10101da177e4SLinus Torvalds 
1011d992895bSNick Piggin 	/*
1012d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
1013d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
1014d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
1015d992895bSNick Piggin 	 * efficient than faulting.
1016d992895bSNick Piggin 	 */
10174b6e1e37SKonstantin Khlebnikov 	if (!(vma->vm_flags & (VM_HUGETLB | VM_NONLINEAR |
10184b6e1e37SKonstantin Khlebnikov 			       VM_PFNMAP | VM_MIXEDMAP))) {
1019d992895bSNick Piggin 		if (!vma->anon_vma)
1020d992895bSNick Piggin 			return 0;
1021d992895bSNick Piggin 	}
1022d992895bSNick Piggin 
10231da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
10241da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
10251da177e4SLinus Torvalds 
1026b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP)) {
10272ab64037Svenkatesh.pallipadi@intel.com 		/*
10282ab64037Svenkatesh.pallipadi@intel.com 		 * We do not free on error cases below as remove_vma
10292ab64037Svenkatesh.pallipadi@intel.com 		 * gets called on error from higher level routine
10302ab64037Svenkatesh.pallipadi@intel.com 		 */
10315180da41SSuresh Siddha 		ret = track_pfn_copy(vma);
10322ab64037Svenkatesh.pallipadi@intel.com 		if (ret)
10332ab64037Svenkatesh.pallipadi@intel.com 			return ret;
10342ab64037Svenkatesh.pallipadi@intel.com 	}
10352ab64037Svenkatesh.pallipadi@intel.com 
1036cddb8a5cSAndrea Arcangeli 	/*
1037cddb8a5cSAndrea Arcangeli 	 * We need to invalidate the secondary MMU mappings only when
1038cddb8a5cSAndrea Arcangeli 	 * there could be a permission downgrade on the ptes of the
1039cddb8a5cSAndrea Arcangeli 	 * parent mm. And a permission downgrade will only happen if
1040cddb8a5cSAndrea Arcangeli 	 * is_cow_mapping() returns true.
1041cddb8a5cSAndrea Arcangeli 	 */
10422ec74c3eSSagi Grimberg 	is_cow = is_cow_mapping(vma->vm_flags);
10432ec74c3eSSagi Grimberg 	mmun_start = addr;
10442ec74c3eSSagi Grimberg 	mmun_end   = end;
10452ec74c3eSSagi Grimberg 	if (is_cow)
10462ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_start(src_mm, mmun_start,
10472ec74c3eSSagi Grimberg 						    mmun_end);
1048cddb8a5cSAndrea Arcangeli 
1049cddb8a5cSAndrea Arcangeli 	ret = 0;
10501da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
10511da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
10521da177e4SLinus Torvalds 	do {
10531da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
10541da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
10551da177e4SLinus Torvalds 			continue;
1056cddb8a5cSAndrea Arcangeli 		if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
1057cddb8a5cSAndrea Arcangeli 					    vma, addr, next))) {
1058cddb8a5cSAndrea Arcangeli 			ret = -ENOMEM;
1059cddb8a5cSAndrea Arcangeli 			break;
1060cddb8a5cSAndrea Arcangeli 		}
10611da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
1062cddb8a5cSAndrea Arcangeli 
10632ec74c3eSSagi Grimberg 	if (is_cow)
10642ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
1065cddb8a5cSAndrea Arcangeli 	return ret;
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
106851c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
1069b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
10701da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
107197a89413SPeter Zijlstra 				struct zap_details *details)
10721da177e4SLinus Torvalds {
1073b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
1074d16dfc55SPeter Zijlstra 	int force_flush = 0;
1075d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
107697a89413SPeter Zijlstra 	spinlock_t *ptl;
10775f1a1907SSteven Rostedt 	pte_t *start_pte;
107897a89413SPeter Zijlstra 	pte_t *pte;
1079d559db08SKAMEZAWA Hiroyuki 
1080d16dfc55SPeter Zijlstra again:
1081e303297eSPeter Zijlstra 	init_rss_vec(rss);
10825f1a1907SSteven Rostedt 	start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
10835f1a1907SSteven Rostedt 	pte = start_pte;
10846606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
10851da177e4SLinus Torvalds 	do {
10861da177e4SLinus Torvalds 		pte_t ptent = *pte;
108751c6f666SRobin Holt 		if (pte_none(ptent)) {
10881da177e4SLinus Torvalds 			continue;
108951c6f666SRobin Holt 		}
109051c6f666SRobin Holt 
10916f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
10926f5e6b9eSHugh Dickins 			struct page *page;
10936f5e6b9eSHugh Dickins 
10946aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
10951da177e4SLinus Torvalds 			if (unlikely(details) && page) {
10961da177e4SLinus Torvalds 				/*
10971da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
10981da177e4SLinus Torvalds 				 * invalidate cache without truncating:
10991da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
11001da177e4SLinus Torvalds 				 */
11011da177e4SLinus Torvalds 				if (details->check_mapping &&
11021da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
11031da177e4SLinus Torvalds 					continue;
11041da177e4SLinus Torvalds 				/*
11051da177e4SLinus Torvalds 				 * Each page->index must be checked when
11061da177e4SLinus Torvalds 				 * invalidating or truncating nonlinear.
11071da177e4SLinus Torvalds 				 */
11081da177e4SLinus Torvalds 				if (details->nonlinear_vma &&
11091da177e4SLinus Torvalds 				    (page->index < details->first_index ||
11101da177e4SLinus Torvalds 				     page->index > details->last_index))
11111da177e4SLinus Torvalds 					continue;
11121da177e4SLinus Torvalds 			}
1113b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
1114a600388dSZachary Amsden 							tlb->fullmm);
11151da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
11161da177e4SLinus Torvalds 			if (unlikely(!page))
11171da177e4SLinus Torvalds 				continue;
11181da177e4SLinus Torvalds 			if (unlikely(details) && details->nonlinear_vma
11191da177e4SLinus Torvalds 			    && linear_page_index(details->nonlinear_vma,
112041bb3476SCyrill Gorcunov 						addr) != page->index) {
112141bb3476SCyrill Gorcunov 				pte_t ptfile = pgoff_to_pte(page->index);
112241bb3476SCyrill Gorcunov 				if (pte_soft_dirty(ptent))
112341bb3476SCyrill Gorcunov 					pte_file_mksoft_dirty(ptfile);
112441bb3476SCyrill Gorcunov 				set_pte_at(mm, addr, pte, ptfile);
112541bb3476SCyrill Gorcunov 			}
11261da177e4SLinus Torvalds 			if (PageAnon(page))
1127d559db08SKAMEZAWA Hiroyuki 				rss[MM_ANONPAGES]--;
11286237bcd9SHugh Dickins 			else {
11296237bcd9SHugh Dickins 				if (pte_dirty(ptent))
11306237bcd9SHugh Dickins 					set_page_dirty(page);
11314917e5d0SJohannes Weiner 				if (pte_young(ptent) &&
113264363aadSJoe Perches 				    likely(!(vma->vm_flags & VM_SEQ_READ)))
1133bf3f3bc5SNick Piggin 					mark_page_accessed(page);
1134d559db08SKAMEZAWA Hiroyuki 				rss[MM_FILEPAGES]--;
11356237bcd9SHugh Dickins 			}
1136edc315fdSHugh Dickins 			page_remove_rmap(page);
11373dc14741SHugh Dickins 			if (unlikely(page_mapcount(page) < 0))
11383dc14741SHugh Dickins 				print_bad_pte(vma, addr, ptent, page);
1139d16dfc55SPeter Zijlstra 			force_flush = !__tlb_remove_page(tlb, page);
1140d16dfc55SPeter Zijlstra 			if (force_flush)
1141d16dfc55SPeter Zijlstra 				break;
11421da177e4SLinus Torvalds 			continue;
11431da177e4SLinus Torvalds 		}
11441da177e4SLinus Torvalds 		/*
11451da177e4SLinus Torvalds 		 * If details->check_mapping, we leave swap entries;
11461da177e4SLinus Torvalds 		 * if details->nonlinear_vma, we leave file entries.
11471da177e4SLinus Torvalds 		 */
11481da177e4SLinus Torvalds 		if (unlikely(details))
11491da177e4SLinus Torvalds 			continue;
11502509ef26SHugh Dickins 		if (pte_file(ptent)) {
11512509ef26SHugh Dickins 			if (unlikely(!(vma->vm_flags & VM_NONLINEAR)))
11522509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
1153b084d435SKAMEZAWA Hiroyuki 		} else {
1154b084d435SKAMEZAWA Hiroyuki 			swp_entry_t entry = pte_to_swp_entry(ptent);
1155b084d435SKAMEZAWA Hiroyuki 
1156b084d435SKAMEZAWA Hiroyuki 			if (!non_swap_entry(entry))
1157b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]--;
11589f9f1acdSKonstantin Khlebnikov 			else if (is_migration_entry(entry)) {
11599f9f1acdSKonstantin Khlebnikov 				struct page *page;
11609f9f1acdSKonstantin Khlebnikov 
11619f9f1acdSKonstantin Khlebnikov 				page = migration_entry_to_page(entry);
11629f9f1acdSKonstantin Khlebnikov 
11639f9f1acdSKonstantin Khlebnikov 				if (PageAnon(page))
11649f9f1acdSKonstantin Khlebnikov 					rss[MM_ANONPAGES]--;
11659f9f1acdSKonstantin Khlebnikov 				else
11669f9f1acdSKonstantin Khlebnikov 					rss[MM_FILEPAGES]--;
11679f9f1acdSKonstantin Khlebnikov 			}
1168b084d435SKAMEZAWA Hiroyuki 			if (unlikely(!free_swap_and_cache(entry)))
11692509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
1170b084d435SKAMEZAWA Hiroyuki 		}
11719888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
117297a89413SPeter Zijlstra 	} while (pte++, addr += PAGE_SIZE, addr != end);
1173ae859762SHugh Dickins 
1174d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(mm, rss);
11756606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
11765f1a1907SSteven Rostedt 	pte_unmap_unlock(start_pte, ptl);
117751c6f666SRobin Holt 
1178d16dfc55SPeter Zijlstra 	/*
1179d16dfc55SPeter Zijlstra 	 * mmu_gather ran out of room to batch pages, we break out of
1180d16dfc55SPeter Zijlstra 	 * the PTE lock to avoid doing the potential expensive TLB invalidate
1181d16dfc55SPeter Zijlstra 	 * and page-free while holding it.
1182d16dfc55SPeter Zijlstra 	 */
1183d16dfc55SPeter Zijlstra 	if (force_flush) {
11842b047252SLinus Torvalds 		unsigned long old_end;
11852b047252SLinus Torvalds 
1186d16dfc55SPeter Zijlstra 		force_flush = 0;
1187597e1c35SAlex Shi 
11882b047252SLinus Torvalds 		/*
11892b047252SLinus Torvalds 		 * Flush the TLB just for the previous segment,
11902b047252SLinus Torvalds 		 * then update the range to be the remaining
11912b047252SLinus Torvalds 		 * TLB range.
11922b047252SLinus Torvalds 		 */
11932b047252SLinus Torvalds 		old_end = tlb->end;
1194e6c495a9SVineet Gupta 		tlb->end = addr;
11952b047252SLinus Torvalds 
1196d16dfc55SPeter Zijlstra 		tlb_flush_mmu(tlb);
11972b047252SLinus Torvalds 
11982b047252SLinus Torvalds 		tlb->start = addr;
11992b047252SLinus Torvalds 		tlb->end = old_end;
12002b047252SLinus Torvalds 
12012b047252SLinus Torvalds 		if (addr != end)
1202d16dfc55SPeter Zijlstra 			goto again;
1203d16dfc55SPeter Zijlstra 	}
1204d16dfc55SPeter Zijlstra 
120551c6f666SRobin Holt 	return addr;
12061da177e4SLinus Torvalds }
12071da177e4SLinus Torvalds 
120851c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1209b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
12101da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
121197a89413SPeter Zijlstra 				struct zap_details *details)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	pmd_t *pmd;
12141da177e4SLinus Torvalds 	unsigned long next;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
12171da177e4SLinus Torvalds 	do {
12181da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
121971e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*pmd)) {
122014d1a55cSAndrea Arcangeli 			if (next - addr != HPAGE_PMD_SIZE) {
1221e0897d75SDavid Rientjes #ifdef CONFIG_DEBUG_VM
1222e0897d75SDavid Rientjes 				if (!rwsem_is_locked(&tlb->mm->mmap_sem)) {
1223e0897d75SDavid 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",
1224e0897d75SDavid Rientjes 						__func__, addr, end,
1225e0897d75SDavid Rientjes 						vma->vm_start,
1226e0897d75SDavid Rientjes 						vma->vm_end);
1227e0897d75SDavid Rientjes 					BUG();
1228e0897d75SDavid Rientjes 				}
1229e0897d75SDavid Rientjes #endif
1230e180377fSKirill A. Shutemov 				split_huge_page_pmd(vma, addr, pmd);
1231f21760b1SShaohua Li 			} else if (zap_huge_pmd(tlb, vma, pmd, addr))
12321a5a9906SAndrea Arcangeli 				goto next;
123371e3aac0SAndrea Arcangeli 			/* fall through */
123471e3aac0SAndrea Arcangeli 		}
12351a5a9906SAndrea Arcangeli 		/*
12361a5a9906SAndrea Arcangeli 		 * Here there can be other concurrent MADV_DONTNEED or
12371a5a9906SAndrea Arcangeli 		 * trans huge page faults running, and if the pmd is
12381a5a9906SAndrea Arcangeli 		 * none or trans huge it can change under us. This is
12391a5a9906SAndrea Arcangeli 		 * because MADV_DONTNEED holds the mmap_sem in read
12401a5a9906SAndrea Arcangeli 		 * mode.
12411a5a9906SAndrea Arcangeli 		 */
12421a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
12431a5a9906SAndrea Arcangeli 			goto next;
124497a89413SPeter Zijlstra 		next = zap_pte_range(tlb, vma, pmd, addr, next, details);
12451a5a9906SAndrea Arcangeli next:
124697a89413SPeter Zijlstra 		cond_resched();
124797a89413SPeter Zijlstra 	} while (pmd++, addr = next, addr != end);
124851c6f666SRobin Holt 
124951c6f666SRobin Holt 	return addr;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
125251c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1253b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
12541da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
125597a89413SPeter Zijlstra 				struct zap_details *details)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	pud_t *pud;
12581da177e4SLinus Torvalds 	unsigned long next;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
12611da177e4SLinus Torvalds 	do {
12621da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
126397a89413SPeter Zijlstra 		if (pud_none_or_clear_bad(pud))
12641da177e4SLinus Torvalds 			continue;
126597a89413SPeter Zijlstra 		next = zap_pmd_range(tlb, vma, pud, addr, next, details);
126697a89413SPeter Zijlstra 	} while (pud++, addr = next, addr != end);
126751c6f666SRobin Holt 
126851c6f666SRobin Holt 	return addr;
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
1271038c7aa1SAl Viro static void unmap_page_range(struct mmu_gather *tlb,
127251c6f666SRobin Holt 			     struct vm_area_struct *vma,
12731da177e4SLinus Torvalds 			     unsigned long addr, unsigned long end,
127497a89413SPeter Zijlstra 			     struct zap_details *details)
12751da177e4SLinus Torvalds {
12761da177e4SLinus Torvalds 	pgd_t *pgd;
12771da177e4SLinus Torvalds 	unsigned long next;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	if (details && !details->check_mapping && !details->nonlinear_vma)
12801da177e4SLinus Torvalds 		details = NULL;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1283569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_start();
12841da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
12851da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
12861da177e4SLinus Torvalds 	do {
12871da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
128897a89413SPeter Zijlstra 		if (pgd_none_or_clear_bad(pgd))
12891da177e4SLinus Torvalds 			continue;
129097a89413SPeter Zijlstra 		next = zap_pud_range(tlb, vma, pgd, addr, next, details);
129197a89413SPeter Zijlstra 	} while (pgd++, addr = next, addr != end);
12921da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
1293569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_end();
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
1296f5cc4eefSAl Viro 
1297f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb,
12981da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
12994f74d2c8SLinus Torvalds 		unsigned long end_addr,
13001da177e4SLinus Torvalds 		struct zap_details *details)
13011da177e4SLinus Torvalds {
1302f5cc4eefSAl Viro 	unsigned long start = max(vma->vm_start, start_addr);
13031da177e4SLinus Torvalds 	unsigned long end;
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	if (start >= vma->vm_end)
1306f5cc4eefSAl Viro 		return;
13071da177e4SLinus Torvalds 	end = min(vma->vm_end, end_addr);
13081da177e4SLinus Torvalds 	if (end <= vma->vm_start)
1309f5cc4eefSAl Viro 		return;
13101da177e4SLinus Torvalds 
1311cbc91f71SSrikar Dronamraju 	if (vma->vm_file)
1312cbc91f71SSrikar Dronamraju 		uprobe_munmap(vma, start, end);
1313cbc91f71SSrikar Dronamraju 
1314b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP))
13155180da41SSuresh Siddha 		untrack_pfn(vma, 0, 0);
13162ab64037Svenkatesh.pallipadi@intel.com 
13178b2a1238SAl Viro 	if (start != end) {
131851c6f666SRobin Holt 		if (unlikely(is_vm_hugetlb_page(vma))) {
1319a137e1ccSAndi Kleen 			/*
1320a137e1ccSAndi Kleen 			 * It is undesirable to test vma->vm_file as it
1321a137e1ccSAndi Kleen 			 * should be non-null for valid hugetlb area.
1322a137e1ccSAndi Kleen 			 * However, vm_file will be NULL in the error
1323a137e1ccSAndi Kleen 			 * cleanup path of do_mmap_pgoff. When
1324a137e1ccSAndi Kleen 			 * hugetlbfs ->mmap method fails,
1325a137e1ccSAndi Kleen 			 * do_mmap_pgoff() nullifies vma->vm_file
1326a137e1ccSAndi Kleen 			 * before calling this function to clean up.
1327a137e1ccSAndi Kleen 			 * Since no pte has actually been setup, it is
1328a137e1ccSAndi Kleen 			 * safe to do nothing in this case.
1329a137e1ccSAndi Kleen 			 */
133024669e58SAneesh Kumar K.V 			if (vma->vm_file) {
133124669e58SAneesh Kumar K.V 				mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
1332d833352aSMel Gorman 				__unmap_hugepage_range_final(tlb, vma, start, end, NULL);
133324669e58SAneesh Kumar K.V 				mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
133424669e58SAneesh Kumar K.V 			}
133551c6f666SRobin Holt 		} else
1336038c7aa1SAl Viro 			unmap_page_range(tlb, vma, start, end, details);
133797a89413SPeter Zijlstra 	}
133851c6f666SRobin Holt }
13391da177e4SLinus Torvalds 
1340f5cc4eefSAl Viro /**
1341f5cc4eefSAl Viro  * unmap_vmas - unmap a range of memory covered by a list of vma's
1342f5cc4eefSAl Viro  * @tlb: address of the caller's struct mmu_gather
1343f5cc4eefSAl Viro  * @vma: the starting vma
1344f5cc4eefSAl Viro  * @start_addr: virtual address at which to start unmapping
1345f5cc4eefSAl Viro  * @end_addr: virtual address at which to end unmapping
1346f5cc4eefSAl Viro  *
1347f5cc4eefSAl Viro  * Unmap all pages in the vma list.
1348f5cc4eefSAl Viro  *
1349f5cc4eefSAl Viro  * Only addresses between `start' and `end' will be unmapped.
1350f5cc4eefSAl Viro  *
1351f5cc4eefSAl Viro  * The VMA list must be sorted in ascending virtual address order.
1352f5cc4eefSAl Viro  *
1353f5cc4eefSAl Viro  * unmap_vmas() assumes that the caller will flush the whole unmapped address
1354f5cc4eefSAl Viro  * range after unmap_vmas() returns.  So the only responsibility here is to
1355f5cc4eefSAl Viro  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1356f5cc4eefSAl Viro  * drops the lock and schedules.
1357f5cc4eefSAl Viro  */
1358f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb,
1359f5cc4eefSAl Viro 		struct vm_area_struct *vma, unsigned long start_addr,
13604f74d2c8SLinus Torvalds 		unsigned long end_addr)
1361f5cc4eefSAl Viro {
1362f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1363f5cc4eefSAl Viro 
1364f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
1365f5cc4eefSAl Viro 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
13664f74d2c8SLinus Torvalds 		unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
1367cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds /**
13711da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
13721da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
1373eb4546bbSRandy Dunlap  * @start: starting address of pages to zap
13741da177e4SLinus Torvalds  * @size: number of bytes to zap
13751da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
1376f5cc4eefSAl Viro  *
1377f5cc4eefSAl Viro  * Caller must protect the VMA list
13781da177e4SLinus Torvalds  */
13797e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start,
13801da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
13811da177e4SLinus Torvalds {
13821da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1383d16dfc55SPeter Zijlstra 	struct mmu_gather tlb;
13847e027b14SLinus Torvalds 	unsigned long end = start + size;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	lru_add_drain();
13872b047252SLinus Torvalds 	tlb_gather_mmu(&tlb, mm, start, end);
1388365e9c87SHugh Dickins 	update_hiwater_rss(mm);
13897e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_start(mm, start, end);
13907e027b14SLinus Torvalds 	for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
13914f74d2c8SLinus Torvalds 		unmap_single_vma(&tlb, vma, start, end, details);
13927e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_end(mm, start, end);
13937e027b14SLinus Torvalds 	tlb_finish_mmu(&tlb, start, end);
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
1396c627f9ccSJack Steiner /**
1397f5cc4eefSAl Viro  * zap_page_range_single - remove user pages in a given range
1398f5cc4eefSAl Viro  * @vma: vm_area_struct holding the applicable pages
1399f5cc4eefSAl Viro  * @address: starting address of pages to zap
1400f5cc4eefSAl Viro  * @size: number of bytes to zap
1401f5cc4eefSAl Viro  * @details: details of nonlinear truncation or shared cache invalidation
1402f5cc4eefSAl Viro  *
1403f5cc4eefSAl Viro  * The range must fit into one VMA.
1404f5cc4eefSAl Viro  */
1405f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1406f5cc4eefSAl Viro 		unsigned long size, struct zap_details *details)
1407f5cc4eefSAl Viro {
1408f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1409f5cc4eefSAl Viro 	struct mmu_gather tlb;
1410f5cc4eefSAl Viro 	unsigned long end = address + size;
1411f5cc4eefSAl Viro 
1412f5cc4eefSAl Viro 	lru_add_drain();
14132b047252SLinus Torvalds 	tlb_gather_mmu(&tlb, mm, address, end);
1414f5cc4eefSAl Viro 	update_hiwater_rss(mm);
1415f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, address, end);
14164f74d2c8SLinus Torvalds 	unmap_single_vma(&tlb, vma, address, end, details);
1417f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_end(mm, address, end);
1418f5cc4eefSAl Viro 	tlb_finish_mmu(&tlb, address, end);
14191da177e4SLinus Torvalds }
14201da177e4SLinus Torvalds 
1421c627f9ccSJack Steiner /**
1422c627f9ccSJack Steiner  * zap_vma_ptes - remove ptes mapping the vma
1423c627f9ccSJack Steiner  * @vma: vm_area_struct holding ptes to be zapped
1424c627f9ccSJack Steiner  * @address: starting address of pages to zap
1425c627f9ccSJack Steiner  * @size: number of bytes to zap
1426c627f9ccSJack Steiner  *
1427c627f9ccSJack Steiner  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1428c627f9ccSJack Steiner  *
1429c627f9ccSJack Steiner  * The entire address range must be fully contained within the vma.
1430c627f9ccSJack Steiner  *
1431c627f9ccSJack Steiner  * Returns 0 if successful.
1432c627f9ccSJack Steiner  */
1433c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1434c627f9ccSJack Steiner 		unsigned long size)
1435c627f9ccSJack Steiner {
1436c627f9ccSJack Steiner 	if (address < vma->vm_start || address + size > vma->vm_end ||
1437c627f9ccSJack Steiner 	    		!(vma->vm_flags & VM_PFNMAP))
1438c627f9ccSJack Steiner 		return -1;
1439f5cc4eefSAl Viro 	zap_page_range_single(vma, address, size, NULL);
1440c627f9ccSJack Steiner 	return 0;
1441c627f9ccSJack Steiner }
1442c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes);
1443c627f9ccSJack Steiner 
1444142762bdSJohannes Weiner /**
1445240aadeeSMichel Lespinasse  * follow_page_mask - look up a page descriptor from a user-virtual address
1446142762bdSJohannes Weiner  * @vma: vm_area_struct mapping @address
1447142762bdSJohannes Weiner  * @address: virtual address to look up
1448142762bdSJohannes Weiner  * @flags: flags modifying lookup behaviour
1449240aadeeSMichel Lespinasse  * @page_mask: on output, *page_mask is set according to the size of the page
1450142762bdSJohannes Weiner  *
1451142762bdSJohannes Weiner  * @flags can have FOLL_ flags set, defined in <linux/mm.h>
1452142762bdSJohannes Weiner  *
1453142762bdSJohannes Weiner  * Returns the mapped (struct page *), %NULL if no mapping exists, or
1454142762bdSJohannes Weiner  * an error pointer if there is a mapping to something not represented
1455142762bdSJohannes Weiner  * by a page descriptor (see also vm_normal_page()).
14561da177e4SLinus Torvalds  */
1457240aadeeSMichel Lespinasse struct page *follow_page_mask(struct vm_area_struct *vma,
1458240aadeeSMichel Lespinasse 			      unsigned long address, unsigned int flags,
1459240aadeeSMichel Lespinasse 			      unsigned int *page_mask)
14601da177e4SLinus Torvalds {
14611da177e4SLinus Torvalds 	pgd_t *pgd;
14621da177e4SLinus Torvalds 	pud_t *pud;
14631da177e4SLinus Torvalds 	pmd_t *pmd;
14641da177e4SLinus Torvalds 	pte_t *ptep, pte;
1465deceb6cdSHugh Dickins 	spinlock_t *ptl;
14661da177e4SLinus Torvalds 	struct page *page;
14676aab341eSLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
14681da177e4SLinus Torvalds 
1469240aadeeSMichel Lespinasse 	*page_mask = 0;
1470240aadeeSMichel Lespinasse 
1471deceb6cdSHugh Dickins 	page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
1472deceb6cdSHugh Dickins 	if (!IS_ERR(page)) {
1473deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
1474deceb6cdSHugh Dickins 		goto out;
1475deceb6cdSHugh Dickins 	}
14761da177e4SLinus Torvalds 
1477deceb6cdSHugh Dickins 	page = NULL;
14781da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
14791da177e4SLinus Torvalds 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
1480deceb6cdSHugh Dickins 		goto no_page_table;
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	pud = pud_offset(pgd, address);
1483ceb86879SAndi Kleen 	if (pud_none(*pud))
1484ceb86879SAndi Kleen 		goto no_page_table;
14858a07651eSHugh Dickins 	if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
1486e632a938SNaoya Horiguchi 		if (flags & FOLL_GET)
1487e632a938SNaoya Horiguchi 			goto out;
1488ceb86879SAndi Kleen 		page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
1489ceb86879SAndi Kleen 		goto out;
1490ceb86879SAndi Kleen 	}
1491ceb86879SAndi Kleen 	if (unlikely(pud_bad(*pud)))
1492deceb6cdSHugh Dickins 		goto no_page_table;
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 	pmd = pmd_offset(pud, address);
1495aeed5fceSHugh Dickins 	if (pmd_none(*pmd))
1496deceb6cdSHugh Dickins 		goto no_page_table;
149771e3aac0SAndrea Arcangeli 	if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
1498deceb6cdSHugh Dickins 		page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
1499e632a938SNaoya Horiguchi 		if (flags & FOLL_GET) {
1500e632a938SNaoya Horiguchi 			/*
1501e632a938SNaoya Horiguchi 			 * Refcount on tail pages are not well-defined and
1502e632a938SNaoya Horiguchi 			 * shouldn't be taken. The caller should handle a NULL
1503e632a938SNaoya Horiguchi 			 * return when trying to follow tail pages.
1504e632a938SNaoya Horiguchi 			 */
1505e632a938SNaoya Horiguchi 			if (PageHead(page))
1506e632a938SNaoya Horiguchi 				get_page(page);
1507e632a938SNaoya Horiguchi 			else {
1508e632a938SNaoya Horiguchi 				page = NULL;
1509e632a938SNaoya Horiguchi 				goto out;
1510e632a938SNaoya Horiguchi 			}
1511e632a938SNaoya Horiguchi 		}
1512deceb6cdSHugh Dickins 		goto out;
1513deceb6cdSHugh Dickins 	}
15140b9d7052SAndrea Arcangeli 	if ((flags & FOLL_NUMA) && pmd_numa(*pmd))
15150b9d7052SAndrea Arcangeli 		goto no_page_table;
151671e3aac0SAndrea Arcangeli 	if (pmd_trans_huge(*pmd)) {
1517500d65d4SAndrea Arcangeli 		if (flags & FOLL_SPLIT) {
1518e180377fSKirill A. Shutemov 			split_huge_page_pmd(vma, address, pmd);
1519500d65d4SAndrea Arcangeli 			goto split_fallthrough;
1520500d65d4SAndrea Arcangeli 		}
1521c4088ebdSKirill A. Shutemov 		ptl = pmd_lock(mm, pmd);
152271e3aac0SAndrea Arcangeli 		if (likely(pmd_trans_huge(*pmd))) {
152371e3aac0SAndrea Arcangeli 			if (unlikely(pmd_trans_splitting(*pmd))) {
1524c4088ebdSKirill A. Shutemov 				spin_unlock(ptl);
152571e3aac0SAndrea Arcangeli 				wait_split_huge_page(vma->anon_vma, pmd);
152671e3aac0SAndrea Arcangeli 			} else {
1527b676b293SDavid Rientjes 				page = follow_trans_huge_pmd(vma, address,
152871e3aac0SAndrea Arcangeli 							     pmd, flags);
1529c4088ebdSKirill A. Shutemov 				spin_unlock(ptl);
1530240aadeeSMichel Lespinasse 				*page_mask = HPAGE_PMD_NR - 1;
153171e3aac0SAndrea Arcangeli 				goto out;
153271e3aac0SAndrea Arcangeli 			}
153371e3aac0SAndrea Arcangeli 		} else
1534c4088ebdSKirill A. Shutemov 			spin_unlock(ptl);
153571e3aac0SAndrea Arcangeli 		/* fall through */
153671e3aac0SAndrea Arcangeli 	}
1537500d65d4SAndrea Arcangeli split_fallthrough:
1538aeed5fceSHugh Dickins 	if (unlikely(pmd_bad(*pmd)))
1539aeed5fceSHugh Dickins 		goto no_page_table;
1540aeed5fceSHugh Dickins 
1541deceb6cdSHugh Dickins 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	pte = *ptep;
15445117b3b8SHugh Dickins 	if (!pte_present(pte)) {
15455117b3b8SHugh Dickins 		swp_entry_t entry;
15465117b3b8SHugh Dickins 		/*
15475117b3b8SHugh Dickins 		 * KSM's break_ksm() relies upon recognizing a ksm page
15485117b3b8SHugh Dickins 		 * even while it is being migrated, so for that case we
15495117b3b8SHugh Dickins 		 * need migration_entry_wait().
15505117b3b8SHugh Dickins 		 */
15515117b3b8SHugh Dickins 		if (likely(!(flags & FOLL_MIGRATION)))
155289f5b7daSLinus Torvalds 			goto no_page;
15535117b3b8SHugh Dickins 		if (pte_none(pte) || pte_file(pte))
15545117b3b8SHugh Dickins 			goto no_page;
15555117b3b8SHugh Dickins 		entry = pte_to_swp_entry(pte);
15565117b3b8SHugh Dickins 		if (!is_migration_entry(entry))
15575117b3b8SHugh Dickins 			goto no_page;
15585117b3b8SHugh Dickins 		pte_unmap_unlock(ptep, ptl);
15595117b3b8SHugh Dickins 		migration_entry_wait(mm, pmd, address);
15605117b3b8SHugh Dickins 		goto split_fallthrough;
15615117b3b8SHugh Dickins 	}
15620b9d7052SAndrea Arcangeli 	if ((flags & FOLL_NUMA) && pte_numa(pte))
15630b9d7052SAndrea Arcangeli 		goto no_page;
1564deceb6cdSHugh Dickins 	if ((flags & FOLL_WRITE) && !pte_write(pte))
1565deceb6cdSHugh Dickins 		goto unlock;
1566a13ea5b7SHugh Dickins 
15676aab341eSLinus Torvalds 	page = vm_normal_page(vma, address, pte);
1568a13ea5b7SHugh Dickins 	if (unlikely(!page)) {
1569a13ea5b7SHugh Dickins 		if ((flags & FOLL_DUMP) ||
157062eede62SHugh Dickins 		    !is_zero_pfn(pte_pfn(pte)))
157189f5b7daSLinus Torvalds 			goto bad_page;
1572a13ea5b7SHugh Dickins 		page = pte_page(pte);
1573a13ea5b7SHugh Dickins 	}
1574deceb6cdSHugh Dickins 
1575deceb6cdSHugh Dickins 	if (flags & FOLL_GET)
157670b50f94SAndrea Arcangeli 		get_page_foll(page);
1577deceb6cdSHugh Dickins 	if (flags & FOLL_TOUCH) {
1578deceb6cdSHugh Dickins 		if ((flags & FOLL_WRITE) &&
1579deceb6cdSHugh Dickins 		    !pte_dirty(pte) && !PageDirty(page))
1580f33ea7f4SNick Piggin 			set_page_dirty(page);
1581bd775c42SKOSAKI Motohiro 		/*
1582bd775c42SKOSAKI Motohiro 		 * pte_mkyoung() would be more correct here, but atomic care
1583bd775c42SKOSAKI Motohiro 		 * is needed to avoid losing the dirty bit: it is easier to use
1584bd775c42SKOSAKI Motohiro 		 * mark_page_accessed().
1585bd775c42SKOSAKI Motohiro 		 */
15861da177e4SLinus Torvalds 		mark_page_accessed(page);
15871da177e4SLinus Torvalds 	}
1588a1fde08cSLinus Torvalds 	if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1589110d74a9SMichel Lespinasse 		/*
1590110d74a9SMichel Lespinasse 		 * The preliminary mapping check is mainly to avoid the
1591110d74a9SMichel Lespinasse 		 * pointless overhead of lock_page on the ZERO_PAGE
1592110d74a9SMichel Lespinasse 		 * which might bounce very badly if there is contention.
1593110d74a9SMichel Lespinasse 		 *
1594110d74a9SMichel Lespinasse 		 * If the page is already locked, we don't need to
1595110d74a9SMichel Lespinasse 		 * handle it now - vmscan will handle it later if and
1596110d74a9SMichel Lespinasse 		 * when it attempts to reclaim the page.
1597110d74a9SMichel Lespinasse 		 */
1598110d74a9SMichel Lespinasse 		if (page->mapping && trylock_page(page)) {
1599110d74a9SMichel Lespinasse 			lru_add_drain();  /* push cached pages to LRU */
1600110d74a9SMichel Lespinasse 			/*
1601e6c509f8SHugh Dickins 			 * Because we lock page here, and migration is
1602e6c509f8SHugh Dickins 			 * blocked by the pte's page reference, and we
1603e6c509f8SHugh Dickins 			 * know the page is still mapped, we don't even
1604e6c509f8SHugh Dickins 			 * need to check for file-cache page truncation.
1605110d74a9SMichel Lespinasse 			 */
1606110d74a9SMichel Lespinasse 			mlock_vma_page(page);
1607110d74a9SMichel Lespinasse 			unlock_page(page);
1608110d74a9SMichel Lespinasse 		}
1609110d74a9SMichel Lespinasse 	}
1610deceb6cdSHugh Dickins unlock:
1611deceb6cdSHugh Dickins 	pte_unmap_unlock(ptep, ptl);
16121da177e4SLinus Torvalds out:
1613deceb6cdSHugh Dickins 	return page;
1614deceb6cdSHugh Dickins 
161589f5b7daSLinus Torvalds bad_page:
161689f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
161789f5b7daSLinus Torvalds 	return ERR_PTR(-EFAULT);
161889f5b7daSLinus Torvalds 
161989f5b7daSLinus Torvalds no_page:
162089f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
162189f5b7daSLinus Torvalds 	if (!pte_none(pte))
162289f5b7daSLinus Torvalds 		return page;
16238e4b9a60SHugh Dickins 
1624deceb6cdSHugh Dickins no_page_table:
1625deceb6cdSHugh Dickins 	/*
1626deceb6cdSHugh Dickins 	 * When core dumping an enormous anonymous area that nobody
16278e4b9a60SHugh Dickins 	 * has touched so far, we don't want to allocate unnecessary pages or
16288e4b9a60SHugh Dickins 	 * page tables.  Return error instead of NULL to skip handle_mm_fault,
16298e4b9a60SHugh Dickins 	 * then get_dump_page() will return NULL to leave a hole in the dump.
16308e4b9a60SHugh Dickins 	 * But we can only make this optimization where a hole would surely
16318e4b9a60SHugh Dickins 	 * be zero-filled if handle_mm_fault() actually did handle it.
1632deceb6cdSHugh Dickins 	 */
16338e4b9a60SHugh Dickins 	if ((flags & FOLL_DUMP) &&
16348e4b9a60SHugh Dickins 	    (!vma->vm_ops || !vma->vm_ops->fault))
16358e4b9a60SHugh Dickins 		return ERR_PTR(-EFAULT);
1636deceb6cdSHugh Dickins 	return page;
16371da177e4SLinus Torvalds }
16381da177e4SLinus Torvalds 
163995042f9eSLinus Torvalds static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
164095042f9eSLinus Torvalds {
1641a09a79f6SMikulas Patocka 	return stack_guard_page_start(vma, addr) ||
1642a09a79f6SMikulas Patocka 	       stack_guard_page_end(vma, addr+PAGE_SIZE);
164395042f9eSLinus Torvalds }
164495042f9eSLinus Torvalds 
16450014bd99SHuang Ying /**
16460014bd99SHuang Ying  * __get_user_pages() - pin user pages in memory
16470014bd99SHuang Ying  * @tsk:	task_struct of target task
16480014bd99SHuang Ying  * @mm:		mm_struct of target mm
16490014bd99SHuang Ying  * @start:	starting user address
16500014bd99SHuang Ying  * @nr_pages:	number of pages from start to pin
16510014bd99SHuang Ying  * @gup_flags:	flags modifying pin behaviour
16520014bd99SHuang Ying  * @pages:	array that receives pointers to the pages pinned.
16530014bd99SHuang Ying  *		Should be at least nr_pages long. Or NULL, if caller
16540014bd99SHuang Ying  *		only intends to ensure the pages are faulted in.
16550014bd99SHuang Ying  * @vmas:	array of pointers to vmas corresponding to each page.
16560014bd99SHuang Ying  *		Or NULL if the caller does not require them.
16570014bd99SHuang Ying  * @nonblocking: whether waiting for disk IO or mmap_sem contention
16580014bd99SHuang Ying  *
16590014bd99SHuang Ying  * Returns number of pages pinned. This may be fewer than the number
16600014bd99SHuang Ying  * requested. If nr_pages is 0 or negative, returns 0. If no pages
16610014bd99SHuang Ying  * were pinned, returns -errno. Each page returned must be released
16620014bd99SHuang Ying  * with a put_page() call when it is finished with. vmas will only
16630014bd99SHuang Ying  * remain valid while mmap_sem is held.
16640014bd99SHuang Ying  *
16650014bd99SHuang Ying  * Must be called with mmap_sem held for read or write.
16660014bd99SHuang Ying  *
16670014bd99SHuang Ying  * __get_user_pages walks a process's page tables and takes a reference to
16680014bd99SHuang Ying  * each struct page that each user address corresponds to at a given
16690014bd99SHuang Ying  * instant. That is, it takes the page that would be accessed if a user
16700014bd99SHuang Ying  * thread accesses the given user virtual address at that instant.
16710014bd99SHuang Ying  *
16720014bd99SHuang Ying  * This does not guarantee that the page exists in the user mappings when
16730014bd99SHuang Ying  * __get_user_pages returns, and there may even be a completely different
16740014bd99SHuang Ying  * page there in some cases (eg. if mmapped pagecache has been invalidated
16750014bd99SHuang Ying  * and subsequently re faulted). However it does guarantee that the page
16760014bd99SHuang Ying  * won't be freed completely. And mostly callers simply care that the page
16770014bd99SHuang Ying  * contains data that was valid *at some point in time*. Typically, an IO
16780014bd99SHuang Ying  * or similar operation cannot guarantee anything stronger anyway because
16790014bd99SHuang Ying  * locks can't be held over the syscall boundary.
16800014bd99SHuang Ying  *
16810014bd99SHuang Ying  * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
16820014bd99SHuang Ying  * the page is written to, set_page_dirty (or set_page_dirty_lock, as
16830014bd99SHuang Ying  * appropriate) must be called after the page is finished with, and
16840014bd99SHuang Ying  * before put_page is called.
16850014bd99SHuang Ying  *
16860014bd99SHuang Ying  * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
16870014bd99SHuang Ying  * or mmap_sem contention, and if waiting is needed to pin all pages,
16880014bd99SHuang Ying  * *@nonblocking will be set to 0.
16890014bd99SHuang Ying  *
16900014bd99SHuang Ying  * In most cases, get_user_pages or get_user_pages_fast should be used
16910014bd99SHuang Ying  * instead of __get_user_pages. __get_user_pages should be used only if
16920014bd99SHuang Ying  * you need some special @gup_flags.
16930014bd99SHuang Ying  */
169428a35716SMichel Lespinasse long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
169528a35716SMichel Lespinasse 		unsigned long start, unsigned long nr_pages,
169628a35716SMichel Lespinasse 		unsigned int gup_flags, struct page **pages,
169728a35716SMichel Lespinasse 		struct vm_area_struct **vmas, int *nonblocking)
16981da177e4SLinus Torvalds {
169928a35716SMichel Lespinasse 	long i;
170058fa879eSHugh Dickins 	unsigned long vm_flags;
1701240aadeeSMichel Lespinasse 	unsigned int page_mask;
17021da177e4SLinus Torvalds 
170328a35716SMichel Lespinasse 	if (!nr_pages)
1704900cf086SJonathan Corbet 		return 0;
170558fa879eSHugh Dickins 
170658fa879eSHugh Dickins 	VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
170758fa879eSHugh Dickins 
17081da177e4SLinus Torvalds 	/*
17091da177e4SLinus Torvalds 	 * Require read or write permissions.
171058fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
17111da177e4SLinus Torvalds 	 */
171258fa879eSHugh Dickins 	vm_flags  = (gup_flags & FOLL_WRITE) ?
171358fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
171458fa879eSHugh Dickins 	vm_flags &= (gup_flags & FOLL_FORCE) ?
171558fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
17160b9d7052SAndrea Arcangeli 
17170b9d7052SAndrea Arcangeli 	/*
17180b9d7052SAndrea Arcangeli 	 * If FOLL_FORCE and FOLL_NUMA are both set, handle_mm_fault
17190b9d7052SAndrea Arcangeli 	 * would be called on PROT_NONE ranges. We must never invoke
17200b9d7052SAndrea Arcangeli 	 * handle_mm_fault on PROT_NONE ranges or the NUMA hinting
17210b9d7052SAndrea Arcangeli 	 * page faults would unprotect the PROT_NONE ranges if
17220b9d7052SAndrea Arcangeli 	 * _PAGE_NUMA and _PAGE_PROTNONE are sharing the same pte/pmd
17230b9d7052SAndrea Arcangeli 	 * bitflag. So to avoid that, don't set FOLL_NUMA if
17240b9d7052SAndrea Arcangeli 	 * FOLL_FORCE is set.
17250b9d7052SAndrea Arcangeli 	 */
17260b9d7052SAndrea Arcangeli 	if (!(gup_flags & FOLL_FORCE))
17270b9d7052SAndrea Arcangeli 		gup_flags |= FOLL_NUMA;
17280b9d7052SAndrea Arcangeli 
17291da177e4SLinus Torvalds 	i = 0;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	do {
17321da177e4SLinus Torvalds 		struct vm_area_struct *vma;
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 		vma = find_extend_vma(mm, start);
1735e7f22e20SStephen Wilson 		if (!vma && in_gate_area(mm, start)) {
17361da177e4SLinus Torvalds 			unsigned long pg = start & PAGE_MASK;
17371da177e4SLinus Torvalds 			pgd_t *pgd;
17381da177e4SLinus Torvalds 			pud_t *pud;
17391da177e4SLinus Torvalds 			pmd_t *pmd;
17401da177e4SLinus Torvalds 			pte_t *pte;
1741b291f000SNick Piggin 
1742b291f000SNick Piggin 			/* user gate pages are read-only */
174358fa879eSHugh Dickins 			if (gup_flags & FOLL_WRITE)
17441da177e4SLinus Torvalds 				return i ? : -EFAULT;
17451da177e4SLinus Torvalds 			if (pg > TASK_SIZE)
17461da177e4SLinus Torvalds 				pgd = pgd_offset_k(pg);
17471da177e4SLinus Torvalds 			else
17481da177e4SLinus Torvalds 				pgd = pgd_offset_gate(mm, pg);
17491da177e4SLinus Torvalds 			BUG_ON(pgd_none(*pgd));
17501da177e4SLinus Torvalds 			pud = pud_offset(pgd, pg);
17511da177e4SLinus Torvalds 			BUG_ON(pud_none(*pud));
17521da177e4SLinus Torvalds 			pmd = pmd_offset(pud, pg);
1753690dbe1cSHugh Dickins 			if (pmd_none(*pmd))
1754690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1755f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
17561da177e4SLinus Torvalds 			pte = pte_offset_map(pmd, pg);
1757690dbe1cSHugh Dickins 			if (pte_none(*pte)) {
1758690dbe1cSHugh Dickins 				pte_unmap(pte);
1759690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1760690dbe1cSHugh Dickins 			}
176195042f9eSLinus Torvalds 			vma = get_gate_vma(mm);
17621da177e4SLinus Torvalds 			if (pages) {
1763de51257aSHugh Dickins 				struct page *page;
1764de51257aSHugh Dickins 
176595042f9eSLinus Torvalds 				page = vm_normal_page(vma, start, *pte);
1766de51257aSHugh Dickins 				if (!page) {
1767de51257aSHugh Dickins 					if (!(gup_flags & FOLL_DUMP) &&
1768de51257aSHugh Dickins 					     is_zero_pfn(pte_pfn(*pte)))
1769de51257aSHugh Dickins 						page = pte_page(*pte);
1770de51257aSHugh Dickins 					else {
1771de51257aSHugh Dickins 						pte_unmap(pte);
1772de51257aSHugh Dickins 						return i ? : -EFAULT;
1773de51257aSHugh Dickins 					}
1774de51257aSHugh Dickins 				}
17756aab341eSLinus Torvalds 				pages[i] = page;
17766aab341eSLinus Torvalds 				get_page(page);
17771da177e4SLinus Torvalds 			}
17781da177e4SLinus Torvalds 			pte_unmap(pte);
1779240aadeeSMichel Lespinasse 			page_mask = 0;
178095042f9eSLinus Torvalds 			goto next_page;
17811da177e4SLinus Torvalds 		}
17821da177e4SLinus Torvalds 
1783b291f000SNick Piggin 		if (!vma ||
1784b291f000SNick Piggin 		    (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
17851c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
17861da177e4SLinus Torvalds 			return i ? : -EFAULT;
17871da177e4SLinus Torvalds 
17882a15efc9SHugh Dickins 		if (is_vm_hugetlb_page(vma)) {
17892a15efc9SHugh Dickins 			i = follow_hugetlb_page(mm, vma, pages, vmas,
179058fa879eSHugh Dickins 					&start, &nr_pages, i, gup_flags);
17912a15efc9SHugh Dickins 			continue;
17922a15efc9SHugh Dickins 		}
1793deceb6cdSHugh Dickins 
17941da177e4SLinus Torvalds 		do {
179508ef4729SHugh Dickins 			struct page *page;
179658fa879eSHugh Dickins 			unsigned int foll_flags = gup_flags;
1797240aadeeSMichel Lespinasse 			unsigned int page_increm;
17981da177e4SLinus Torvalds 
1799462e00ccSEthan Solomita 			/*
18004779280dSYing Han 			 * If we have a pending SIGKILL, don't keep faulting
18011c3aff1cSHugh Dickins 			 * pages and potentially allocating memory.
1802462e00ccSEthan Solomita 			 */
18031c3aff1cSHugh Dickins 			if (unlikely(fatal_signal_pending(current)))
18044779280dSYing Han 				return i ? i : -ERESTARTSYS;
1805462e00ccSEthan Solomita 
1806deceb6cdSHugh Dickins 			cond_resched();
1807240aadeeSMichel Lespinasse 			while (!(page = follow_page_mask(vma, start,
1808240aadeeSMichel Lespinasse 						foll_flags, &page_mask))) {
1809a68d2ebcSLinus Torvalds 				int ret;
181053a7706dSMichel Lespinasse 				unsigned int fault_flags = 0;
181153a7706dSMichel Lespinasse 
1812a09a79f6SMikulas Patocka 				/* For mlock, just skip the stack guard page. */
1813a09a79f6SMikulas Patocka 				if (foll_flags & FOLL_MLOCK) {
1814a09a79f6SMikulas Patocka 					if (stack_guard_page(vma, start))
1815a09a79f6SMikulas Patocka 						goto next_page;
1816a09a79f6SMikulas Patocka 				}
181753a7706dSMichel Lespinasse 				if (foll_flags & FOLL_WRITE)
181853a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_WRITE;
181953a7706dSMichel Lespinasse 				if (nonblocking)
182053a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_ALLOW_RETRY;
1821318b275fSGleb Natapov 				if (foll_flags & FOLL_NOWAIT)
1822318b275fSGleb Natapov 					fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
1823d06063ccSLinus Torvalds 
1824d26ed650SHugh Dickins 				ret = handle_mm_fault(mm, vma, start,
182553a7706dSMichel Lespinasse 							fault_flags);
1826d26ed650SHugh Dickins 
182783c54070SNick Piggin 				if (ret & VM_FAULT_ERROR) {
182883c54070SNick Piggin 					if (ret & VM_FAULT_OOM)
182983c54070SNick Piggin 						return i ? i : -ENOMEM;
183069ebb83eSHuang Ying 					if (ret & (VM_FAULT_HWPOISON |
183169ebb83eSHuang Ying 						   VM_FAULT_HWPOISON_LARGE)) {
183269ebb83eSHuang Ying 						if (i)
183369ebb83eSHuang Ying 							return i;
183469ebb83eSHuang Ying 						else if (gup_flags & FOLL_HWPOISON)
183569ebb83eSHuang Ying 							return -EHWPOISON;
183669ebb83eSHuang Ying 						else
183769ebb83eSHuang Ying 							return -EFAULT;
183869ebb83eSHuang Ying 					}
183969ebb83eSHuang Ying 					if (ret & VM_FAULT_SIGBUS)
184083c54070SNick Piggin 						return i ? i : -EFAULT;
184183c54070SNick Piggin 					BUG();
184283c54070SNick Piggin 				}
1843e7f22e20SStephen Wilson 
1844e7f22e20SStephen Wilson 				if (tsk) {
184583c54070SNick Piggin 					if (ret & VM_FAULT_MAJOR)
184683c54070SNick Piggin 						tsk->maj_flt++;
184783c54070SNick Piggin 					else
184883c54070SNick Piggin 						tsk->min_flt++;
1849e7f22e20SStephen Wilson 				}
185083c54070SNick Piggin 
185153a7706dSMichel Lespinasse 				if (ret & VM_FAULT_RETRY) {
1852318b275fSGleb Natapov 					if (nonblocking)
185353a7706dSMichel Lespinasse 						*nonblocking = 0;
185453a7706dSMichel Lespinasse 					return i;
185553a7706dSMichel Lespinasse 				}
185653a7706dSMichel Lespinasse 
1857f33ea7f4SNick Piggin 				/*
185883c54070SNick Piggin 				 * The VM_FAULT_WRITE bit tells us that
185983c54070SNick Piggin 				 * do_wp_page has broken COW when necessary,
186083c54070SNick Piggin 				 * even if maybe_mkwrite decided not to set
186183c54070SNick Piggin 				 * pte_write. We can thus safely do subsequent
1862878b63acSHugh Dickins 				 * page lookups as if they were reads. But only
1863878b63acSHugh Dickins 				 * do so when looping for pte_write is futile:
1864878b63acSHugh Dickins 				 * in some cases userspace may also be wanting
1865878b63acSHugh Dickins 				 * to write to the gotten user page, which a
1866878b63acSHugh Dickins 				 * read fault here might prevent (a readonly
1867878b63acSHugh Dickins 				 * page might get reCOWed by userspace write).
1868f33ea7f4SNick Piggin 				 */
1869878b63acSHugh Dickins 				if ((ret & VM_FAULT_WRITE) &&
1870878b63acSHugh Dickins 				    !(vma->vm_flags & VM_WRITE))
1871deceb6cdSHugh Dickins 					foll_flags &= ~FOLL_WRITE;
1872a68d2ebcSLinus Torvalds 
18737f7bbbe5SBenjamin Herrenschmidt 				cond_resched();
18741da177e4SLinus Torvalds 			}
187589f5b7daSLinus Torvalds 			if (IS_ERR(page))
187689f5b7daSLinus Torvalds 				return i ? i : PTR_ERR(page);
18771da177e4SLinus Torvalds 			if (pages) {
187808ef4729SHugh Dickins 				pages[i] = page;
187903beb076SJames Bottomley 
1880a6f36be3SRussell King 				flush_anon_page(vma, page, start);
188108ef4729SHugh Dickins 				flush_dcache_page(page);
1882240aadeeSMichel Lespinasse 				page_mask = 0;
18831da177e4SLinus Torvalds 			}
188495042f9eSLinus Torvalds next_page:
1885240aadeeSMichel Lespinasse 			if (vmas) {
18861da177e4SLinus Torvalds 				vmas[i] = vma;
1887240aadeeSMichel Lespinasse 				page_mask = 0;
1888240aadeeSMichel Lespinasse 			}
1889240aadeeSMichel Lespinasse 			page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
1890240aadeeSMichel Lespinasse 			if (page_increm > nr_pages)
1891240aadeeSMichel Lespinasse 				page_increm = nr_pages;
1892240aadeeSMichel Lespinasse 			i += page_increm;
1893240aadeeSMichel Lespinasse 			start += page_increm * PAGE_SIZE;
1894240aadeeSMichel Lespinasse 			nr_pages -= page_increm;
18959d73777eSPeter Zijlstra 		} while (nr_pages && start < vma->vm_end);
18969d73777eSPeter Zijlstra 	} while (nr_pages);
18971da177e4SLinus Torvalds 	return i;
18981da177e4SLinus Torvalds }
18990014bd99SHuang Ying EXPORT_SYMBOL(__get_user_pages);
1900b291f000SNick Piggin 
19012efaca92SBenjamin Herrenschmidt /*
19022efaca92SBenjamin Herrenschmidt  * fixup_user_fault() - manually resolve a user page fault
19032efaca92SBenjamin Herrenschmidt  * @tsk:	the task_struct to use for page fault accounting, or
19042efaca92SBenjamin Herrenschmidt  *		NULL if faults are not to be recorded.
19052efaca92SBenjamin Herrenschmidt  * @mm:		mm_struct of target mm
19062efaca92SBenjamin Herrenschmidt  * @address:	user address
19072efaca92SBenjamin Herrenschmidt  * @fault_flags:flags to pass down to handle_mm_fault()
19082efaca92SBenjamin Herrenschmidt  *
19092efaca92SBenjamin Herrenschmidt  * This is meant to be called in the specific scenario where for locking reasons
19102efaca92SBenjamin Herrenschmidt  * we try to access user memory in atomic context (within a pagefault_disable()
19112efaca92SBenjamin Herrenschmidt  * section), this returns -EFAULT, and we want to resolve the user fault before
19122efaca92SBenjamin Herrenschmidt  * trying again.
19132efaca92SBenjamin Herrenschmidt  *
19142efaca92SBenjamin Herrenschmidt  * Typically this is meant to be used by the futex code.
19152efaca92SBenjamin Herrenschmidt  *
19162efaca92SBenjamin Herrenschmidt  * The main difference with get_user_pages() is that this function will
19172efaca92SBenjamin Herrenschmidt  * unconditionally call handle_mm_fault() which will in turn perform all the
19182efaca92SBenjamin Herrenschmidt  * necessary SW fixup of the dirty and young bits in the PTE, while
19192efaca92SBenjamin Herrenschmidt  * handle_mm_fault() only guarantees to update these in the struct page.
19202efaca92SBenjamin Herrenschmidt  *
19212efaca92SBenjamin Herrenschmidt  * This is important for some architectures where those bits also gate the
19222efaca92SBenjamin Herrenschmidt  * access permission to the page because they are maintained in software.  On
19232efaca92SBenjamin Herrenschmidt  * such architectures, gup() will not be enough to make a subsequent access
19242efaca92SBenjamin Herrenschmidt  * succeed.
19252efaca92SBenjamin Herrenschmidt  *
19262efaca92SBenjamin Herrenschmidt  * This should be called with the mm_sem held for read.
19272efaca92SBenjamin Herrenschmidt  */
19282efaca92SBenjamin Herrenschmidt int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
19292efaca92SBenjamin Herrenschmidt 		     unsigned long address, unsigned int fault_flags)
19302efaca92SBenjamin Herrenschmidt {
19312efaca92SBenjamin Herrenschmidt 	struct vm_area_struct *vma;
19322efaca92SBenjamin Herrenschmidt 	int ret;
19332efaca92SBenjamin Herrenschmidt 
19342efaca92SBenjamin Herrenschmidt 	vma = find_extend_vma(mm, address);
19352efaca92SBenjamin Herrenschmidt 	if (!vma || address < vma->vm_start)
19362efaca92SBenjamin Herrenschmidt 		return -EFAULT;
19372efaca92SBenjamin Herrenschmidt 
19382efaca92SBenjamin Herrenschmidt 	ret = handle_mm_fault(mm, vma, address, fault_flags);
19392efaca92SBenjamin Herrenschmidt 	if (ret & VM_FAULT_ERROR) {
19402efaca92SBenjamin Herrenschmidt 		if (ret & VM_FAULT_OOM)
19412efaca92SBenjamin Herrenschmidt 			return -ENOMEM;
19422efaca92SBenjamin Herrenschmidt 		if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
19432efaca92SBenjamin Herrenschmidt 			return -EHWPOISON;
19442efaca92SBenjamin Herrenschmidt 		if (ret & VM_FAULT_SIGBUS)
19452efaca92SBenjamin Herrenschmidt 			return -EFAULT;
19462efaca92SBenjamin Herrenschmidt 		BUG();
19472efaca92SBenjamin Herrenschmidt 	}
19482efaca92SBenjamin Herrenschmidt 	if (tsk) {
19492efaca92SBenjamin Herrenschmidt 		if (ret & VM_FAULT_MAJOR)
19502efaca92SBenjamin Herrenschmidt 			tsk->maj_flt++;
19512efaca92SBenjamin Herrenschmidt 		else
19522efaca92SBenjamin Herrenschmidt 			tsk->min_flt++;
19532efaca92SBenjamin Herrenschmidt 	}
19542efaca92SBenjamin Herrenschmidt 	return 0;
19552efaca92SBenjamin Herrenschmidt }
19562efaca92SBenjamin Herrenschmidt 
19572efaca92SBenjamin Herrenschmidt /*
1958d2bf6be8SNick Piggin  * get_user_pages() - pin user pages in memory
1959e7f22e20SStephen Wilson  * @tsk:	the task_struct to use for page fault accounting, or
1960e7f22e20SStephen Wilson  *		NULL if faults are not to be recorded.
1961d2bf6be8SNick Piggin  * @mm:		mm_struct of target mm
1962d2bf6be8SNick Piggin  * @start:	starting user address
19639d73777eSPeter Zijlstra  * @nr_pages:	number of pages from start to pin
1964d2bf6be8SNick Piggin  * @write:	whether pages will be written to by the caller
1965d2bf6be8SNick Piggin  * @force:	whether to force write access even if user mapping is
1966d2bf6be8SNick Piggin  *		readonly. This will result in the page being COWed even
1967d2bf6be8SNick Piggin  *		in MAP_SHARED mappings. You do not want this.
1968d2bf6be8SNick Piggin  * @pages:	array that receives pointers to the pages pinned.
1969d2bf6be8SNick Piggin  *		Should be at least nr_pages long. Or NULL, if caller
1970d2bf6be8SNick Piggin  *		only intends to ensure the pages are faulted in.
1971d2bf6be8SNick Piggin  * @vmas:	array of pointers to vmas corresponding to each page.
1972d2bf6be8SNick Piggin  *		Or NULL if the caller does not require them.
1973d2bf6be8SNick Piggin  *
1974d2bf6be8SNick Piggin  * Returns number of pages pinned. This may be fewer than the number
19759d73777eSPeter Zijlstra  * requested. If nr_pages is 0 or negative, returns 0. If no pages
1976d2bf6be8SNick Piggin  * were pinned, returns -errno. Each page returned must be released
1977d2bf6be8SNick Piggin  * with a put_page() call when it is finished with. vmas will only
1978d2bf6be8SNick Piggin  * remain valid while mmap_sem is held.
1979d2bf6be8SNick Piggin  *
1980d2bf6be8SNick Piggin  * Must be called with mmap_sem held for read or write.
1981d2bf6be8SNick Piggin  *
1982d2bf6be8SNick Piggin  * get_user_pages walks a process's page tables and takes a reference to
1983d2bf6be8SNick Piggin  * each struct page that each user address corresponds to at a given
1984d2bf6be8SNick Piggin  * instant. That is, it takes the page that would be accessed if a user
1985d2bf6be8SNick Piggin  * thread accesses the given user virtual address at that instant.
1986d2bf6be8SNick Piggin  *
1987d2bf6be8SNick Piggin  * This does not guarantee that the page exists in the user mappings when
1988d2bf6be8SNick Piggin  * get_user_pages returns, and there may even be a completely different
1989d2bf6be8SNick Piggin  * page there in some cases (eg. if mmapped pagecache has been invalidated
1990d2bf6be8SNick Piggin  * and subsequently re faulted). However it does guarantee that the page
1991d2bf6be8SNick Piggin  * won't be freed completely. And mostly callers simply care that the page
1992d2bf6be8SNick Piggin  * contains data that was valid *at some point in time*. Typically, an IO
1993d2bf6be8SNick Piggin  * or similar operation cannot guarantee anything stronger anyway because
1994d2bf6be8SNick Piggin  * locks can't be held over the syscall boundary.
1995d2bf6be8SNick Piggin  *
1996d2bf6be8SNick Piggin  * If write=0, the page must not be written to. If the page is written to,
1997d2bf6be8SNick Piggin  * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
1998d2bf6be8SNick Piggin  * after the page is finished with, and before put_page is called.
1999d2bf6be8SNick Piggin  *
2000d2bf6be8SNick Piggin  * get_user_pages is typically used for fewer-copy IO operations, to get a
2001d2bf6be8SNick Piggin  * handle on the memory by some means other than accesses via the user virtual
2002d2bf6be8SNick Piggin  * addresses. The pages may be submitted for DMA to devices or accessed via
2003d2bf6be8SNick Piggin  * their kernel linear mapping (via the kmap APIs). Care should be taken to
2004d2bf6be8SNick Piggin  * use the correct cache flushing APIs.
2005d2bf6be8SNick Piggin  *
2006d2bf6be8SNick Piggin  * See also get_user_pages_fast, for performance critical applications.
2007d2bf6be8SNick Piggin  */
200828a35716SMichel Lespinasse long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
200928a35716SMichel Lespinasse 		unsigned long start, unsigned long nr_pages, int write,
201028a35716SMichel Lespinasse 		int force, struct page **pages, struct vm_area_struct **vmas)
2011b291f000SNick Piggin {
201258fa879eSHugh Dickins 	int flags = FOLL_TOUCH;
2013b291f000SNick Piggin 
201458fa879eSHugh Dickins 	if (pages)
201558fa879eSHugh Dickins 		flags |= FOLL_GET;
2016b291f000SNick Piggin 	if (write)
201758fa879eSHugh Dickins 		flags |= FOLL_WRITE;
2018b291f000SNick Piggin 	if (force)
201958fa879eSHugh Dickins 		flags |= FOLL_FORCE;
2020b291f000SNick Piggin 
202153a7706dSMichel Lespinasse 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
202253a7706dSMichel Lespinasse 				NULL);
2023b291f000SNick Piggin }
20241da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages);
20251da177e4SLinus Torvalds 
2026f3e8fccdSHugh Dickins /**
2027f3e8fccdSHugh Dickins  * get_dump_page() - pin user page in memory while writing it to core dump
2028f3e8fccdSHugh Dickins  * @addr: user address
2029f3e8fccdSHugh Dickins  *
2030f3e8fccdSHugh Dickins  * Returns struct page pointer of user page pinned for dump,
2031f3e8fccdSHugh Dickins  * to be freed afterwards by page_cache_release() or put_page().
2032f3e8fccdSHugh Dickins  *
2033f3e8fccdSHugh Dickins  * Returns NULL on any kind of failure - a hole must then be inserted into
2034f3e8fccdSHugh Dickins  * the corefile, to preserve alignment with its headers; and also returns
2035f3e8fccdSHugh Dickins  * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
2036f3e8fccdSHugh Dickins  * allowing a hole to be left in the corefile to save diskspace.
2037f3e8fccdSHugh Dickins  *
2038f3e8fccdSHugh Dickins  * Called without mmap_sem, but after all other threads have been killed.
2039f3e8fccdSHugh Dickins  */
2040f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE
2041f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr)
2042f3e8fccdSHugh Dickins {
2043f3e8fccdSHugh Dickins 	struct vm_area_struct *vma;
2044f3e8fccdSHugh Dickins 	struct page *page;
2045f3e8fccdSHugh Dickins 
2046f3e8fccdSHugh Dickins 	if (__get_user_pages(current, current->mm, addr, 1,
204753a7706dSMichel Lespinasse 			     FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
204853a7706dSMichel Lespinasse 			     NULL) < 1)
2049f3e8fccdSHugh Dickins 		return NULL;
2050f3e8fccdSHugh Dickins 	flush_cache_page(vma, addr, page_to_pfn(page));
2051f3e8fccdSHugh Dickins 	return page;
2052f3e8fccdSHugh Dickins }
2053f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */
2054f3e8fccdSHugh Dickins 
205525ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
2056920c7a5dSHarvey Harrison 			spinlock_t **ptl)
2057c9cfcddfSLinus Torvalds {
2058c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
2059c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
2060c9cfcddfSLinus Torvalds 	if (pud) {
206149c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
2062f66055abSAndrea Arcangeli 		if (pmd) {
2063f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
2064c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
2065c9cfcddfSLinus Torvalds 		}
2066f66055abSAndrea Arcangeli 	}
2067c9cfcddfSLinus Torvalds 	return NULL;
2068c9cfcddfSLinus Torvalds }
2069c9cfcddfSLinus Torvalds 
20701da177e4SLinus Torvalds /*
2071238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
2072238f58d8SLinus Torvalds  *
2073238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
2074238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
2075238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
2076238f58d8SLinus Torvalds  */
2077423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr,
2078423bad60SNick Piggin 			struct page *page, pgprot_t prot)
2079238f58d8SLinus Torvalds {
2080423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
2081238f58d8SLinus Torvalds 	int retval;
2082238f58d8SLinus Torvalds 	pte_t *pte;
2083238f58d8SLinus Torvalds 	spinlock_t *ptl;
2084238f58d8SLinus Torvalds 
2085238f58d8SLinus Torvalds 	retval = -EINVAL;
2086a145dd41SLinus Torvalds 	if (PageAnon(page))
20875b4e655eSKAMEZAWA Hiroyuki 		goto out;
2088238f58d8SLinus Torvalds 	retval = -ENOMEM;
2089238f58d8SLinus Torvalds 	flush_dcache_page(page);
2090c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
2091238f58d8SLinus Torvalds 	if (!pte)
20925b4e655eSKAMEZAWA Hiroyuki 		goto out;
2093238f58d8SLinus Torvalds 	retval = -EBUSY;
2094238f58d8SLinus Torvalds 	if (!pte_none(*pte))
2095238f58d8SLinus Torvalds 		goto out_unlock;
2096238f58d8SLinus Torvalds 
2097238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
2098238f58d8SLinus Torvalds 	get_page(page);
209934e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_FILEPAGES);
2100238f58d8SLinus Torvalds 	page_add_file_rmap(page);
2101238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
2102238f58d8SLinus Torvalds 
2103238f58d8SLinus Torvalds 	retval = 0;
21048a9f3ccdSBalbir Singh 	pte_unmap_unlock(pte, ptl);
21058a9f3ccdSBalbir Singh 	return retval;
2106238f58d8SLinus Torvalds out_unlock:
2107238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
2108238f58d8SLinus Torvalds out:
2109238f58d8SLinus Torvalds 	return retval;
2110238f58d8SLinus Torvalds }
2111238f58d8SLinus Torvalds 
2112bfa5bf6dSRolf Eike Beer /**
2113bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
2114bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
2115bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
2116bfa5bf6dSRolf Eike Beer  * @page: source kernel page
2117bfa5bf6dSRolf Eike Beer  *
2118a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
2119a145dd41SLinus Torvalds  * into a user vma.
2120a145dd41SLinus Torvalds  *
2121a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
2122a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
2123a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
21248dfcc9baSNick Piggin  * (see split_page()).
2125a145dd41SLinus Torvalds  *
2126a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
2127a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
2128a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
2129a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
2130a145dd41SLinus Torvalds  * ask for a shared writable mapping!
2131a145dd41SLinus Torvalds  *
2132a145dd41SLinus Torvalds  * The page does not need to be reserved.
21334b6e1e37SKonstantin Khlebnikov  *
21344b6e1e37SKonstantin Khlebnikov  * Usually this function is called from f_op->mmap() handler
21354b6e1e37SKonstantin Khlebnikov  * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
21364b6e1e37SKonstantin Khlebnikov  * Caller must set VM_MIXEDMAP on vma if it wants to call this
21374b6e1e37SKonstantin Khlebnikov  * function from other places, for example from page-fault handler.
2138a145dd41SLinus Torvalds  */
2139423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
2140423bad60SNick Piggin 			struct page *page)
2141a145dd41SLinus Torvalds {
2142a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
2143a145dd41SLinus Torvalds 		return -EFAULT;
2144a145dd41SLinus Torvalds 	if (!page_count(page))
2145a145dd41SLinus Torvalds 		return -EINVAL;
21464b6e1e37SKonstantin Khlebnikov 	if (!(vma->vm_flags & VM_MIXEDMAP)) {
21474b6e1e37SKonstantin Khlebnikov 		BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
21484b6e1e37SKonstantin Khlebnikov 		BUG_ON(vma->vm_flags & VM_PFNMAP);
21494b6e1e37SKonstantin Khlebnikov 		vma->vm_flags |= VM_MIXEDMAP;
21504b6e1e37SKonstantin Khlebnikov 	}
2151423bad60SNick Piggin 	return insert_page(vma, addr, page, vma->vm_page_prot);
2152a145dd41SLinus Torvalds }
2153e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
2154a145dd41SLinus Torvalds 
2155423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2156423bad60SNick Piggin 			unsigned long pfn, pgprot_t prot)
2157423bad60SNick Piggin {
2158423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
2159423bad60SNick Piggin 	int retval;
2160423bad60SNick Piggin 	pte_t *pte, entry;
2161423bad60SNick Piggin 	spinlock_t *ptl;
2162423bad60SNick Piggin 
2163423bad60SNick Piggin 	retval = -ENOMEM;
2164423bad60SNick Piggin 	pte = get_locked_pte(mm, addr, &ptl);
2165423bad60SNick Piggin 	if (!pte)
2166423bad60SNick Piggin 		goto out;
2167423bad60SNick Piggin 	retval = -EBUSY;
2168423bad60SNick Piggin 	if (!pte_none(*pte))
2169423bad60SNick Piggin 		goto out_unlock;
2170423bad60SNick Piggin 
2171423bad60SNick Piggin 	/* Ok, finally just insert the thing.. */
2172423bad60SNick Piggin 	entry = pte_mkspecial(pfn_pte(pfn, prot));
2173423bad60SNick Piggin 	set_pte_at(mm, addr, pte, entry);
21744b3073e1SRussell King 	update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
2175423bad60SNick Piggin 
2176423bad60SNick Piggin 	retval = 0;
2177423bad60SNick Piggin out_unlock:
2178423bad60SNick Piggin 	pte_unmap_unlock(pte, ptl);
2179423bad60SNick Piggin out:
2180423bad60SNick Piggin 	return retval;
2181423bad60SNick Piggin }
2182423bad60SNick Piggin 
2183e0dc0d8fSNick Piggin /**
2184e0dc0d8fSNick Piggin  * vm_insert_pfn - insert single pfn into user vma
2185e0dc0d8fSNick Piggin  * @vma: user vma to map to
2186e0dc0d8fSNick Piggin  * @addr: target user address of this page
2187e0dc0d8fSNick Piggin  * @pfn: source kernel pfn
2188e0dc0d8fSNick Piggin  *
2189c462f179SRobert P. J. Day  * Similar to vm_insert_page, this allows drivers to insert individual pages
2190e0dc0d8fSNick Piggin  * they've allocated into a user vma. Same comments apply.
2191e0dc0d8fSNick Piggin  *
2192e0dc0d8fSNick Piggin  * This function should only be called from a vm_ops->fault handler, and
2193e0dc0d8fSNick Piggin  * in that case the handler should return NULL.
21940d71d10aSNick Piggin  *
21950d71d10aSNick Piggin  * vma cannot be a COW mapping.
21960d71d10aSNick Piggin  *
21970d71d10aSNick Piggin  * As this is called only for pages that do not currently exist, we
21980d71d10aSNick Piggin  * do not need to flush old virtual caches or the TLB.
2199e0dc0d8fSNick Piggin  */
2200e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2201e0dc0d8fSNick Piggin 			unsigned long pfn)
2202e0dc0d8fSNick Piggin {
22032ab64037Svenkatesh.pallipadi@intel.com 	int ret;
2204e4b866edSvenkatesh.pallipadi@intel.com 	pgprot_t pgprot = vma->vm_page_prot;
22057e675137SNick Piggin 	/*
22067e675137SNick Piggin 	 * Technically, architectures with pte_special can avoid all these
22077e675137SNick Piggin 	 * restrictions (same for remap_pfn_range).  However we would like
22087e675137SNick Piggin 	 * consistency in testing and feature parity among all, so we should
22097e675137SNick Piggin 	 * try to keep these invariants in place for everybody.
22107e675137SNick Piggin 	 */
2211b379d790SJared Hulbert 	BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
2212b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
2213b379d790SJared Hulbert 						(VM_PFNMAP|VM_MIXEDMAP));
2214b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
2215b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
2216e0dc0d8fSNick Piggin 
2217423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
2218423bad60SNick Piggin 		return -EFAULT;
22195180da41SSuresh Siddha 	if (track_pfn_insert(vma, &pgprot, pfn))
22202ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
22212ab64037Svenkatesh.pallipadi@intel.com 
2222e4b866edSvenkatesh.pallipadi@intel.com 	ret = insert_pfn(vma, addr, pfn, pgprot);
22232ab64037Svenkatesh.pallipadi@intel.com 
22242ab64037Svenkatesh.pallipadi@intel.com 	return ret;
2225e0dc0d8fSNick Piggin }
2226e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn);
2227e0dc0d8fSNick Piggin 
2228423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2229423bad60SNick Piggin 			unsigned long pfn)
2230423bad60SNick Piggin {
2231423bad60SNick Piggin 	BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
2232423bad60SNick Piggin 
2233423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
2234423bad60SNick Piggin 		return -EFAULT;
2235423bad60SNick Piggin 
2236423bad60SNick Piggin 	/*
2237423bad60SNick Piggin 	 * If we don't have pte special, then we have to use the pfn_valid()
2238423bad60SNick Piggin 	 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
2239423bad60SNick Piggin 	 * refcount the page if pfn_valid is true (hence insert_page rather
224062eede62SHugh Dickins 	 * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
224162eede62SHugh Dickins 	 * without pte special, it would there be refcounted as a normal page.
2242423bad60SNick Piggin 	 */
2243423bad60SNick Piggin 	if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
2244423bad60SNick Piggin 		struct page *page;
2245423bad60SNick Piggin 
2246423bad60SNick Piggin 		page = pfn_to_page(pfn);
2247423bad60SNick Piggin 		return insert_page(vma, addr, page, vma->vm_page_prot);
2248423bad60SNick Piggin 	}
2249423bad60SNick Piggin 	return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
2250423bad60SNick Piggin }
2251423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed);
2252423bad60SNick Piggin 
2253a145dd41SLinus Torvalds /*
22541da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
22551da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
22561da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
22571da177e4SLinus Torvalds  */
22581da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
22591da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
22601da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
22611da177e4SLinus Torvalds {
22621da177e4SLinus Torvalds 	pte_t *pte;
2263c74df32cSHugh Dickins 	spinlock_t *ptl;
22641da177e4SLinus Torvalds 
2265c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
22661da177e4SLinus Torvalds 	if (!pte)
22671da177e4SLinus Torvalds 		return -ENOMEM;
22686606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
22691da177e4SLinus Torvalds 	do {
22701da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
22717e675137SNick Piggin 		set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
22721da177e4SLinus Torvalds 		pfn++;
22731da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
22746606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
2275c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
22761da177e4SLinus Torvalds 	return 0;
22771da177e4SLinus Torvalds }
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
22801da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
22811da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
22821da177e4SLinus Torvalds {
22831da177e4SLinus Torvalds 	pmd_t *pmd;
22841da177e4SLinus Torvalds 	unsigned long next;
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
22871da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
22881da177e4SLinus Torvalds 	if (!pmd)
22891da177e4SLinus Torvalds 		return -ENOMEM;
2290f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
22911da177e4SLinus Torvalds 	do {
22921da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
22931da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
22941da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
22951da177e4SLinus Torvalds 			return -ENOMEM;
22961da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
22971da177e4SLinus Torvalds 	return 0;
22981da177e4SLinus Torvalds }
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
23011da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
23021da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
23031da177e4SLinus Torvalds {
23041da177e4SLinus Torvalds 	pud_t *pud;
23051da177e4SLinus Torvalds 	unsigned long next;
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
23081da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
23091da177e4SLinus Torvalds 	if (!pud)
23101da177e4SLinus Torvalds 		return -ENOMEM;
23111da177e4SLinus Torvalds 	do {
23121da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
23131da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
23141da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
23151da177e4SLinus Torvalds 			return -ENOMEM;
23161da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
23171da177e4SLinus Torvalds 	return 0;
23181da177e4SLinus Torvalds }
23191da177e4SLinus Torvalds 
2320bfa5bf6dSRolf Eike Beer /**
2321bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
2322bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
2323bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
2324bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
2325bfa5bf6dSRolf Eike Beer  * @size: size of map area
2326bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
2327bfa5bf6dSRolf Eike Beer  *
2328bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
2329bfa5bf6dSRolf Eike Beer  */
23301da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
23311da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
23321da177e4SLinus Torvalds {
23331da177e4SLinus Torvalds 	pgd_t *pgd;
23341da177e4SLinus Torvalds 	unsigned long next;
23352d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
23361da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
23371da177e4SLinus Torvalds 	int err;
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 	/*
23401da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
23411da177e4SLinus Torvalds 	 * rest of the world about it:
23421da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
23431da177e4SLinus Torvalds 	 *	(accesses can have side effects).
23446aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
23456aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
23466aab341eSLinus Torvalds 	 *	with them.
2347314e51b9SKonstantin Khlebnikov 	 *   VM_DONTEXPAND
2348314e51b9SKonstantin Khlebnikov 	 *      Disable vma merging and expanding with mremap().
2349314e51b9SKonstantin Khlebnikov 	 *   VM_DONTDUMP
2350314e51b9SKonstantin Khlebnikov 	 *      Omit vma from core dump, even when VM_IO turned off.
2351fb155c16SLinus Torvalds 	 *
2352fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
2353fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
2354fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
2355b3b9c293SKonstantin Khlebnikov 	 * See vm_normal_page() for details.
23561da177e4SLinus Torvalds 	 */
2357b3b9c293SKonstantin Khlebnikov 	if (is_cow_mapping(vma->vm_flags)) {
2358b3b9c293SKonstantin Khlebnikov 		if (addr != vma->vm_start || end != vma->vm_end)
2359b3b9c293SKonstantin Khlebnikov 			return -EINVAL;
23606aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
2361b3b9c293SKonstantin Khlebnikov 	}
2362b3b9c293SKonstantin Khlebnikov 
2363b3b9c293SKonstantin Khlebnikov 	err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
2364b3b9c293SKonstantin Khlebnikov 	if (err)
23653c8bb73aSvenkatesh.pallipadi@intel.com 		return -EINVAL;
2366fb155c16SLinus Torvalds 
2367314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	BUG_ON(addr >= end);
23701da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
23711da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
23721da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
23731da177e4SLinus Torvalds 	do {
23741da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
23751da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
23761da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
23771da177e4SLinus Torvalds 		if (err)
23781da177e4SLinus Torvalds 			break;
23791da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
23802ab64037Svenkatesh.pallipadi@intel.com 
23812ab64037Svenkatesh.pallipadi@intel.com 	if (err)
23825180da41SSuresh Siddha 		untrack_pfn(vma, pfn, PAGE_ALIGN(size));
23832ab64037Svenkatesh.pallipadi@intel.com 
23841da177e4SLinus Torvalds 	return err;
23851da177e4SLinus Torvalds }
23861da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
23871da177e4SLinus Torvalds 
2388b4cbb197SLinus Torvalds /**
2389b4cbb197SLinus Torvalds  * vm_iomap_memory - remap memory to userspace
2390b4cbb197SLinus Torvalds  * @vma: user vma to map to
2391b4cbb197SLinus Torvalds  * @start: start of area
2392b4cbb197SLinus Torvalds  * @len: size of area
2393b4cbb197SLinus Torvalds  *
2394b4cbb197SLinus Torvalds  * This is a simplified io_remap_pfn_range() for common driver use. The
2395b4cbb197SLinus Torvalds  * driver just needs to give us the physical memory range to be mapped,
2396b4cbb197SLinus Torvalds  * we'll figure out the rest from the vma information.
2397b4cbb197SLinus Torvalds  *
2398b4cbb197SLinus Torvalds  * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2399b4cbb197SLinus Torvalds  * whatever write-combining details or similar.
2400b4cbb197SLinus Torvalds  */
2401b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2402b4cbb197SLinus Torvalds {
2403b4cbb197SLinus Torvalds 	unsigned long vm_len, pfn, pages;
2404b4cbb197SLinus Torvalds 
2405b4cbb197SLinus Torvalds 	/* Check that the physical memory area passed in looks valid */
2406b4cbb197SLinus Torvalds 	if (start + len < start)
2407b4cbb197SLinus Torvalds 		return -EINVAL;
2408b4cbb197SLinus Torvalds 	/*
2409b4cbb197SLinus Torvalds 	 * You *really* shouldn't map things that aren't page-aligned,
2410b4cbb197SLinus Torvalds 	 * but we've historically allowed it because IO memory might
2411b4cbb197SLinus Torvalds 	 * just have smaller alignment.
2412b4cbb197SLinus Torvalds 	 */
2413b4cbb197SLinus Torvalds 	len += start & ~PAGE_MASK;
2414b4cbb197SLinus Torvalds 	pfn = start >> PAGE_SHIFT;
2415b4cbb197SLinus Torvalds 	pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2416b4cbb197SLinus Torvalds 	if (pfn + pages < pfn)
2417b4cbb197SLinus Torvalds 		return -EINVAL;
2418b4cbb197SLinus Torvalds 
2419b4cbb197SLinus Torvalds 	/* We start the mapping 'vm_pgoff' pages into the area */
2420b4cbb197SLinus Torvalds 	if (vma->vm_pgoff > pages)
2421b4cbb197SLinus Torvalds 		return -EINVAL;
2422b4cbb197SLinus Torvalds 	pfn += vma->vm_pgoff;
2423b4cbb197SLinus Torvalds 	pages -= vma->vm_pgoff;
2424b4cbb197SLinus Torvalds 
2425b4cbb197SLinus Torvalds 	/* Can we fit all of the mapping? */
2426b4cbb197SLinus Torvalds 	vm_len = vma->vm_end - vma->vm_start;
2427b4cbb197SLinus Torvalds 	if (vm_len >> PAGE_SHIFT > pages)
2428b4cbb197SLinus Torvalds 		return -EINVAL;
2429b4cbb197SLinus Torvalds 
2430b4cbb197SLinus Torvalds 	/* Ok, let it rip */
2431b4cbb197SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2432b4cbb197SLinus Torvalds }
2433b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
2434b4cbb197SLinus Torvalds 
2435aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2436aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2437aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2438aee16b3cSJeremy Fitzhardinge {
2439aee16b3cSJeremy Fitzhardinge 	pte_t *pte;
2440aee16b3cSJeremy Fitzhardinge 	int err;
24412f569afdSMartin Schwidefsky 	pgtable_t token;
244294909914SBorislav Petkov 	spinlock_t *uninitialized_var(ptl);
2443aee16b3cSJeremy Fitzhardinge 
2444aee16b3cSJeremy Fitzhardinge 	pte = (mm == &init_mm) ?
2445aee16b3cSJeremy Fitzhardinge 		pte_alloc_kernel(pmd, addr) :
2446aee16b3cSJeremy Fitzhardinge 		pte_alloc_map_lock(mm, pmd, addr, &ptl);
2447aee16b3cSJeremy Fitzhardinge 	if (!pte)
2448aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2449aee16b3cSJeremy Fitzhardinge 
2450aee16b3cSJeremy Fitzhardinge 	BUG_ON(pmd_huge(*pmd));
2451aee16b3cSJeremy Fitzhardinge 
245238e0edb1SJeremy Fitzhardinge 	arch_enter_lazy_mmu_mode();
245338e0edb1SJeremy Fitzhardinge 
24542f569afdSMartin Schwidefsky 	token = pmd_pgtable(*pmd);
2455aee16b3cSJeremy Fitzhardinge 
2456aee16b3cSJeremy Fitzhardinge 	do {
2457c36987e2SDaisuke Nishimura 		err = fn(pte++, token, addr, data);
2458aee16b3cSJeremy Fitzhardinge 		if (err)
2459aee16b3cSJeremy Fitzhardinge 			break;
2460c36987e2SDaisuke Nishimura 	} while (addr += PAGE_SIZE, addr != end);
2461aee16b3cSJeremy Fitzhardinge 
246238e0edb1SJeremy Fitzhardinge 	arch_leave_lazy_mmu_mode();
246338e0edb1SJeremy Fitzhardinge 
2464aee16b3cSJeremy Fitzhardinge 	if (mm != &init_mm)
2465aee16b3cSJeremy Fitzhardinge 		pte_unmap_unlock(pte-1, ptl);
2466aee16b3cSJeremy Fitzhardinge 	return err;
2467aee16b3cSJeremy Fitzhardinge }
2468aee16b3cSJeremy Fitzhardinge 
2469aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2470aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2471aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2472aee16b3cSJeremy Fitzhardinge {
2473aee16b3cSJeremy Fitzhardinge 	pmd_t *pmd;
2474aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2475aee16b3cSJeremy Fitzhardinge 	int err;
2476aee16b3cSJeremy Fitzhardinge 
2477ceb86879SAndi Kleen 	BUG_ON(pud_huge(*pud));
2478ceb86879SAndi Kleen 
2479aee16b3cSJeremy Fitzhardinge 	pmd = pmd_alloc(mm, pud, addr);
2480aee16b3cSJeremy Fitzhardinge 	if (!pmd)
2481aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2482aee16b3cSJeremy Fitzhardinge 	do {
2483aee16b3cSJeremy Fitzhardinge 		next = pmd_addr_end(addr, end);
2484aee16b3cSJeremy Fitzhardinge 		err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2485aee16b3cSJeremy Fitzhardinge 		if (err)
2486aee16b3cSJeremy Fitzhardinge 			break;
2487aee16b3cSJeremy Fitzhardinge 	} while (pmd++, addr = next, addr != end);
2488aee16b3cSJeremy Fitzhardinge 	return err;
2489aee16b3cSJeremy Fitzhardinge }
2490aee16b3cSJeremy Fitzhardinge 
2491aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
2492aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2493aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2494aee16b3cSJeremy Fitzhardinge {
2495aee16b3cSJeremy Fitzhardinge 	pud_t *pud;
2496aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2497aee16b3cSJeremy Fitzhardinge 	int err;
2498aee16b3cSJeremy Fitzhardinge 
2499aee16b3cSJeremy Fitzhardinge 	pud = pud_alloc(mm, pgd, addr);
2500aee16b3cSJeremy Fitzhardinge 	if (!pud)
2501aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2502aee16b3cSJeremy Fitzhardinge 	do {
2503aee16b3cSJeremy Fitzhardinge 		next = pud_addr_end(addr, end);
2504aee16b3cSJeremy Fitzhardinge 		err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2505aee16b3cSJeremy Fitzhardinge 		if (err)
2506aee16b3cSJeremy Fitzhardinge 			break;
2507aee16b3cSJeremy Fitzhardinge 	} while (pud++, addr = next, addr != end);
2508aee16b3cSJeremy Fitzhardinge 	return err;
2509aee16b3cSJeremy Fitzhardinge }
2510aee16b3cSJeremy Fitzhardinge 
2511aee16b3cSJeremy Fitzhardinge /*
2512aee16b3cSJeremy Fitzhardinge  * Scan a region of virtual memory, filling in page tables as necessary
2513aee16b3cSJeremy Fitzhardinge  * and calling a provided function on each leaf page table.
2514aee16b3cSJeremy Fitzhardinge  */
2515aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2516aee16b3cSJeremy Fitzhardinge 			unsigned long size, pte_fn_t fn, void *data)
2517aee16b3cSJeremy Fitzhardinge {
2518aee16b3cSJeremy Fitzhardinge 	pgd_t *pgd;
2519aee16b3cSJeremy Fitzhardinge 	unsigned long next;
252057250a5bSJeremy Fitzhardinge 	unsigned long end = addr + size;
2521aee16b3cSJeremy Fitzhardinge 	int err;
2522aee16b3cSJeremy Fitzhardinge 
2523aee16b3cSJeremy Fitzhardinge 	BUG_ON(addr >= end);
2524aee16b3cSJeremy Fitzhardinge 	pgd = pgd_offset(mm, addr);
2525aee16b3cSJeremy Fitzhardinge 	do {
2526aee16b3cSJeremy Fitzhardinge 		next = pgd_addr_end(addr, end);
2527aee16b3cSJeremy Fitzhardinge 		err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
2528aee16b3cSJeremy Fitzhardinge 		if (err)
2529aee16b3cSJeremy Fitzhardinge 			break;
2530aee16b3cSJeremy Fitzhardinge 	} while (pgd++, addr = next, addr != end);
253157250a5bSJeremy Fitzhardinge 
2532aee16b3cSJeremy Fitzhardinge 	return err;
2533aee16b3cSJeremy Fitzhardinge }
2534aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range);
2535aee16b3cSJeremy Fitzhardinge 
25361da177e4SLinus Torvalds /*
25378f4e2101SHugh Dickins  * handle_pte_fault chooses page fault handler according to an entry
25388f4e2101SHugh Dickins  * which was read non-atomically.  Before making any commitment, on
25398f4e2101SHugh Dickins  * those architectures or configurations (e.g. i386 with PAE) which
2540a335b2e1SRyota Ozaki  * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault
25418f4e2101SHugh Dickins  * must check under lock before unmapping the pte and proceeding
25428f4e2101SHugh Dickins  * (but do_wp_page is only called after already making such a check;
2543a335b2e1SRyota Ozaki  * and do_anonymous_page can safely check later on).
25448f4e2101SHugh Dickins  */
25454c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
25468f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
25478f4e2101SHugh Dickins {
25488f4e2101SHugh Dickins 	int same = 1;
25498f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
25508f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
25514c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
25524c21e2f2SHugh Dickins 		spin_lock(ptl);
25538f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
25544c21e2f2SHugh Dickins 		spin_unlock(ptl);
25558f4e2101SHugh Dickins 	}
25568f4e2101SHugh Dickins #endif
25578f4e2101SHugh Dickins 	pte_unmap(page_table);
25588f4e2101SHugh Dickins 	return same;
25598f4e2101SHugh Dickins }
25608f4e2101SHugh Dickins 
25619de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
25626aab341eSLinus Torvalds {
25630abdd7a8SDan Williams 	debug_dma_assert_idle(src);
25640abdd7a8SDan Williams 
25656aab341eSLinus Torvalds 	/*
25666aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
25676aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
25686aab341eSLinus Torvalds 	 * just copying from the original user address. If that
25696aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
25706aab341eSLinus Torvalds 	 */
25716aab341eSLinus Torvalds 	if (unlikely(!src)) {
25729b04c5feSCong Wang 		void *kaddr = kmap_atomic(dst);
25735d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
25745d2a2dbbSLinus Torvalds 
25755d2a2dbbSLinus Torvalds 		/*
25765d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
25775d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
25785d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
25795d2a2dbbSLinus Torvalds 		 * zeroes.
25805d2a2dbbSLinus Torvalds 		 */
25815d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
25823ecb01dfSJan Beulich 			clear_page(kaddr);
25839b04c5feSCong Wang 		kunmap_atomic(kaddr);
2584c4ec7b0dSDmitriy Monakhov 		flush_dcache_page(dst);
25850ed361deSNick Piggin 	} else
25869de455b2SAtsushi Nemoto 		copy_user_highpage(dst, src, va, vma);
25876aab341eSLinus Torvalds }
25886aab341eSLinus Torvalds 
25891da177e4SLinus Torvalds /*
25901da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
25911da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
25921da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
25931da177e4SLinus Torvalds  *
25941da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
25951da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
25961da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
25971da177e4SLinus Torvalds  * COW.
25981da177e4SLinus Torvalds  *
25991da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
26001da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
26011da177e4SLinus Torvalds  * and potentially makes it more efficient.
26021da177e4SLinus Torvalds  *
26038f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
26048f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
26058f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
26061da177e4SLinus Torvalds  */
26071da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
260865500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
26098f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
2610e6219ec8SNamhyung Kim 	__releases(ptl)
26111da177e4SLinus Torvalds {
26122ec74c3eSSagi Grimberg 	struct page *old_page, *new_page = NULL;
26131da177e4SLinus Torvalds 	pte_t entry;
2614b009c024SMichel Lespinasse 	int ret = 0;
2615a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
2616d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
26171756954cSDavid Rientjes 	unsigned long mmun_start = 0;	/* For mmu_notifiers */
26181756954cSDavid Rientjes 	unsigned long mmun_end = 0;	/* For mmu_notifiers */
26191da177e4SLinus Torvalds 
26206aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
2621251b97f5SPeter Zijlstra 	if (!old_page) {
2622251b97f5SPeter Zijlstra 		/*
2623251b97f5SPeter Zijlstra 		 * VM_MIXEDMAP !pfn_valid() case
2624251b97f5SPeter Zijlstra 		 *
2625251b97f5SPeter Zijlstra 		 * We should not cow pages in a shared writeable mapping.
2626251b97f5SPeter Zijlstra 		 * Just mark the pages writable as we can't do any dirty
2627251b97f5SPeter Zijlstra 		 * accounting on raw pfn maps.
2628251b97f5SPeter Zijlstra 		 */
2629251b97f5SPeter Zijlstra 		if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2630251b97f5SPeter Zijlstra 				     (VM_WRITE|VM_SHARED))
2631251b97f5SPeter Zijlstra 			goto reuse;
2632920fc356SHugh Dickins 		goto gotten;
2633251b97f5SPeter Zijlstra 	}
26341da177e4SLinus Torvalds 
2635d08b3851SPeter Zijlstra 	/*
2636ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
2637ee6a6457SPeter Zijlstra 	 * not dirty accountable.
2638d08b3851SPeter Zijlstra 	 */
26399a840895SHugh Dickins 	if (PageAnon(old_page) && !PageKsm(old_page)) {
2640ab967d86SHugh Dickins 		if (!trylock_page(old_page)) {
2641ab967d86SHugh Dickins 			page_cache_get(old_page);
2642ab967d86SHugh Dickins 			pte_unmap_unlock(page_table, ptl);
2643ab967d86SHugh Dickins 			lock_page(old_page);
2644ab967d86SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address,
2645ab967d86SHugh Dickins 							 &ptl);
2646ab967d86SHugh Dickins 			if (!pte_same(*page_table, orig_pte)) {
2647ab967d86SHugh Dickins 				unlock_page(old_page);
2648ab967d86SHugh Dickins 				goto unlock;
2649ab967d86SHugh Dickins 			}
2650ab967d86SHugh Dickins 			page_cache_release(old_page);
2651ab967d86SHugh Dickins 		}
2652b009c024SMichel Lespinasse 		if (reuse_swap_page(old_page)) {
2653c44b6743SRik van Riel 			/*
2654c44b6743SRik van Riel 			 * The page is all ours.  Move it to our anon_vma so
2655c44b6743SRik van Riel 			 * the rmap code will not search our parent or siblings.
2656c44b6743SRik van Riel 			 * Protected against the rmap code by the page lock.
2657c44b6743SRik van Riel 			 */
2658c44b6743SRik van Riel 			page_move_anon_rmap(old_page, vma, address);
2659ee6a6457SPeter Zijlstra 			unlock_page(old_page);
2660b009c024SMichel Lespinasse 			goto reuse;
2661b009c024SMichel Lespinasse 		}
2662b009c024SMichel Lespinasse 		unlock_page(old_page);
2663ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2664d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
2665ee6a6457SPeter Zijlstra 		/*
2666ee6a6457SPeter Zijlstra 		 * Only catch write-faults on shared writable pages,
2667ee6a6457SPeter Zijlstra 		 * read-only shared pages can get COWed by
2668ee6a6457SPeter Zijlstra 		 * get_user_pages(.write=1, .force=1).
2669ee6a6457SPeter Zijlstra 		 */
26709637a5efSDavid Howells 		if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2671c2ec175cSNick Piggin 			struct vm_fault vmf;
2672c2ec175cSNick Piggin 			int tmp;
2673c2ec175cSNick Piggin 
2674c2ec175cSNick Piggin 			vmf.virtual_address = (void __user *)(address &
2675c2ec175cSNick Piggin 								PAGE_MASK);
2676c2ec175cSNick Piggin 			vmf.pgoff = old_page->index;
2677c2ec175cSNick Piggin 			vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
2678c2ec175cSNick Piggin 			vmf.page = old_page;
2679c2ec175cSNick Piggin 
26809637a5efSDavid Howells 			/*
26819637a5efSDavid Howells 			 * Notify the address space that the page is about to
26829637a5efSDavid Howells 			 * become writable so that it can prohibit this or wait
26839637a5efSDavid Howells 			 * for the page to get into an appropriate state.
26849637a5efSDavid Howells 			 *
26859637a5efSDavid Howells 			 * We do this without the lock held, so that it can
26869637a5efSDavid Howells 			 * sleep if it needs to.
26879637a5efSDavid Howells 			 */
26889637a5efSDavid Howells 			page_cache_get(old_page);
26899637a5efSDavid Howells 			pte_unmap_unlock(page_table, ptl);
26909637a5efSDavid Howells 
2691c2ec175cSNick Piggin 			tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
2692c2ec175cSNick Piggin 			if (unlikely(tmp &
2693c2ec175cSNick Piggin 					(VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
2694c2ec175cSNick Piggin 				ret = tmp;
26959637a5efSDavid Howells 				goto unwritable_page;
2696c2ec175cSNick Piggin 			}
2697b827e496SNick Piggin 			if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
2698b827e496SNick Piggin 				lock_page(old_page);
2699b827e496SNick Piggin 				if (!old_page->mapping) {
2700b827e496SNick Piggin 					ret = 0; /* retry the fault */
2701b827e496SNick Piggin 					unlock_page(old_page);
2702b827e496SNick Piggin 					goto unwritable_page;
2703b827e496SNick Piggin 				}
2704b827e496SNick Piggin 			} else
2705b827e496SNick Piggin 				VM_BUG_ON(!PageLocked(old_page));
27069637a5efSDavid Howells 
27079637a5efSDavid Howells 			/*
27089637a5efSDavid Howells 			 * Since we dropped the lock we need to revalidate
27099637a5efSDavid Howells 			 * the PTE as someone else may have changed it.  If
27109637a5efSDavid Howells 			 * they did, we just return, as we can count on the
27119637a5efSDavid Howells 			 * MMU to tell us if they didn't also make it writable.
27129637a5efSDavid Howells 			 */
27139637a5efSDavid Howells 			page_table = pte_offset_map_lock(mm, pmd, address,
27149637a5efSDavid Howells 							 &ptl);
2715b827e496SNick Piggin 			if (!pte_same(*page_table, orig_pte)) {
2716b827e496SNick Piggin 				unlock_page(old_page);
27179637a5efSDavid Howells 				goto unlock;
2718b827e496SNick Piggin 			}
2719a200ee18SPeter Zijlstra 
2720a200ee18SPeter Zijlstra 			page_mkwrite = 1;
27219637a5efSDavid Howells 		}
2722d08b3851SPeter Zijlstra 		dirty_page = old_page;
2723d08b3851SPeter Zijlstra 		get_page(dirty_page);
27249637a5efSDavid Howells 
2725251b97f5SPeter Zijlstra reuse:
27268c8a743cSPeter Zijlstra 		/*
27278c8a743cSPeter Zijlstra 		 * Clear the pages cpupid information as the existing
27288c8a743cSPeter Zijlstra 		 * information potentially belongs to a now completely
27298c8a743cSPeter Zijlstra 		 * unrelated process.
27308c8a743cSPeter Zijlstra 		 */
27318c8a743cSPeter Zijlstra 		if (old_page)
27328c8a743cSPeter Zijlstra 			page_cpupid_xchg_last(old_page, (1 << LAST_CPUPID_SHIFT) - 1);
27338c8a743cSPeter Zijlstra 
2734eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
273565500d23SHugh Dickins 		entry = pte_mkyoung(orig_pte);
273665500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2737954ffcb3SKAMEZAWA Hiroyuki 		if (ptep_set_access_flags(vma, address, page_table, entry,1))
27384b3073e1SRussell King 			update_mmu_cache(vma, address, page_table);
273972ddc8f7SMichel Lespinasse 		pte_unmap_unlock(page_table, ptl);
274065500d23SHugh Dickins 		ret |= VM_FAULT_WRITE;
274172ddc8f7SMichel Lespinasse 
274272ddc8f7SMichel Lespinasse 		if (!dirty_page)
274372ddc8f7SMichel Lespinasse 			return ret;
274472ddc8f7SMichel Lespinasse 
274572ddc8f7SMichel Lespinasse 		/*
274672ddc8f7SMichel Lespinasse 		 * Yes, Virginia, this is actually required to prevent a race
274772ddc8f7SMichel Lespinasse 		 * with clear_page_dirty_for_io() from clearing the page dirty
274872ddc8f7SMichel Lespinasse 		 * bit after it clear all dirty ptes, but before a racing
274972ddc8f7SMichel Lespinasse 		 * do_wp_page installs a dirty pte.
275072ddc8f7SMichel Lespinasse 		 *
2751a335b2e1SRyota Ozaki 		 * __do_fault is protected similarly.
275272ddc8f7SMichel Lespinasse 		 */
275372ddc8f7SMichel Lespinasse 		if (!page_mkwrite) {
275472ddc8f7SMichel Lespinasse 			wait_on_page_locked(dirty_page);
275572ddc8f7SMichel Lespinasse 			set_page_dirty_balance(dirty_page, page_mkwrite);
275641c4d25fSJan Kara 			/* file_update_time outside page_lock */
275741c4d25fSJan Kara 			if (vma->vm_file)
275841c4d25fSJan Kara 				file_update_time(vma->vm_file);
275972ddc8f7SMichel Lespinasse 		}
276072ddc8f7SMichel Lespinasse 		put_page(dirty_page);
276172ddc8f7SMichel Lespinasse 		if (page_mkwrite) {
276272ddc8f7SMichel Lespinasse 			struct address_space *mapping = dirty_page->mapping;
276372ddc8f7SMichel Lespinasse 
276472ddc8f7SMichel Lespinasse 			set_page_dirty(dirty_page);
276572ddc8f7SMichel Lespinasse 			unlock_page(dirty_page);
276672ddc8f7SMichel Lespinasse 			page_cache_release(dirty_page);
276772ddc8f7SMichel Lespinasse 			if (mapping)	{
276872ddc8f7SMichel Lespinasse 				/*
276972ddc8f7SMichel Lespinasse 				 * Some device drivers do not set page.mapping
277072ddc8f7SMichel Lespinasse 				 * but still dirty their pages
277172ddc8f7SMichel Lespinasse 				 */
277272ddc8f7SMichel Lespinasse 				balance_dirty_pages_ratelimited(mapping);
277372ddc8f7SMichel Lespinasse 			}
277472ddc8f7SMichel Lespinasse 		}
277572ddc8f7SMichel Lespinasse 
277672ddc8f7SMichel Lespinasse 		return ret;
27771da177e4SLinus Torvalds 	}
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds 	/*
27801da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
27811da177e4SLinus Torvalds 	 */
27821da177e4SLinus Torvalds 	page_cache_get(old_page);
2783920fc356SHugh Dickins gotten:
27848f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
27851da177e4SLinus Torvalds 
27861da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
278765500d23SHugh Dickins 		goto oom;
2788a13ea5b7SHugh Dickins 
278962eede62SHugh Dickins 	if (is_zero_pfn(pte_pfn(orig_pte))) {
2790a13ea5b7SHugh Dickins 		new_page = alloc_zeroed_user_highpage_movable(vma, address);
2791a13ea5b7SHugh Dickins 		if (!new_page)
2792a13ea5b7SHugh Dickins 			goto oom;
2793a13ea5b7SHugh Dickins 	} else {
2794769848c0SMel Gorman 		new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
27951da177e4SLinus Torvalds 		if (!new_page)
279665500d23SHugh Dickins 			goto oom;
2797a13ea5b7SHugh Dickins 		cow_user_page(new_page, old_page, address, vma);
2798a13ea5b7SHugh Dickins 	}
2799a13ea5b7SHugh Dickins 	__SetPageUptodate(new_page);
2800a13ea5b7SHugh Dickins 
28012c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))
28028a9f3ccdSBalbir Singh 		goto oom_free_new;
28038a9f3ccdSBalbir Singh 
28046bdb913fSHaggai Eran 	mmun_start  = address & PAGE_MASK;
28051756954cSDavid Rientjes 	mmun_end    = mmun_start + PAGE_SIZE;
28066bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
28076bdb913fSHaggai Eran 
28081da177e4SLinus Torvalds 	/*
28091da177e4SLinus Torvalds 	 * Re-check the pte - we dropped the lock
28101da177e4SLinus Torvalds 	 */
28118f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
281265500d23SHugh Dickins 	if (likely(pte_same(*page_table, orig_pte))) {
2813920fc356SHugh Dickins 		if (old_page) {
28144294621fSHugh Dickins 			if (!PageAnon(old_page)) {
281534e55232SKAMEZAWA Hiroyuki 				dec_mm_counter_fast(mm, MM_FILEPAGES);
281634e55232SKAMEZAWA Hiroyuki 				inc_mm_counter_fast(mm, MM_ANONPAGES);
28174294621fSHugh Dickins 			}
2818920fc356SHugh Dickins 		} else
281934e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
2820eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
282165500d23SHugh Dickins 		entry = mk_pte(new_page, vma->vm_page_prot);
282265500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
28234ce072f1SSiddha, Suresh B 		/*
28244ce072f1SSiddha, Suresh B 		 * Clear the pte entry and flush it first, before updating the
28254ce072f1SSiddha, Suresh B 		 * pte with the new entry. This will avoid a race condition
28264ce072f1SSiddha, Suresh B 		 * seen in the presence of one thread doing SMC and another
28274ce072f1SSiddha, Suresh B 		 * thread doing COW.
28284ce072f1SSiddha, Suresh B 		 */
2829828502d3SIzik Eidus 		ptep_clear_flush(vma, address, page_table);
28309617d95eSNick Piggin 		page_add_new_anon_rmap(new_page, vma, address);
2831828502d3SIzik Eidus 		/*
2832828502d3SIzik Eidus 		 * We call the notify macro here because, when using secondary
2833828502d3SIzik Eidus 		 * mmu page tables (such as kvm shadow page tables), we want the
2834828502d3SIzik Eidus 		 * new page to be mapped directly into the secondary page table.
2835828502d3SIzik Eidus 		 */
2836828502d3SIzik Eidus 		set_pte_at_notify(mm, address, page_table, entry);
28374b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
2838945754a1SNick Piggin 		if (old_page) {
2839945754a1SNick Piggin 			/*
2840945754a1SNick Piggin 			 * Only after switching the pte to the new page may
2841945754a1SNick Piggin 			 * we remove the mapcount here. Otherwise another
2842945754a1SNick Piggin 			 * process may come and find the rmap count decremented
2843945754a1SNick Piggin 			 * before the pte is switched to the new page, and
2844945754a1SNick Piggin 			 * "reuse" the old page writing into it while our pte
2845945754a1SNick Piggin 			 * here still points into it and can be read by other
2846945754a1SNick Piggin 			 * threads.
2847945754a1SNick Piggin 			 *
2848945754a1SNick Piggin 			 * The critical issue is to order this
2849945754a1SNick Piggin 			 * page_remove_rmap with the ptp_clear_flush above.
2850945754a1SNick Piggin 			 * Those stores are ordered by (if nothing else,)
2851945754a1SNick Piggin 			 * the barrier present in the atomic_add_negative
2852945754a1SNick Piggin 			 * in page_remove_rmap.
2853945754a1SNick Piggin 			 *
2854945754a1SNick Piggin 			 * Then the TLB flush in ptep_clear_flush ensures that
2855945754a1SNick Piggin 			 * no process can access the old page before the
2856945754a1SNick Piggin 			 * decremented mapcount is visible. And the old page
2857945754a1SNick Piggin 			 * cannot be reused until after the decremented
2858945754a1SNick Piggin 			 * mapcount is visible. So transitively, TLBs to
2859945754a1SNick Piggin 			 * old page will be flushed before it can be reused.
2860945754a1SNick Piggin 			 */
2861edc315fdSHugh Dickins 			page_remove_rmap(old_page);
2862945754a1SNick Piggin 		}
2863945754a1SNick Piggin 
28641da177e4SLinus Torvalds 		/* Free the old page.. */
28651da177e4SLinus Torvalds 		new_page = old_page;
2866f33ea7f4SNick Piggin 		ret |= VM_FAULT_WRITE;
28678a9f3ccdSBalbir Singh 	} else
28688a9f3ccdSBalbir Singh 		mem_cgroup_uncharge_page(new_page);
28698a9f3ccdSBalbir Singh 
28706bdb913fSHaggai Eran 	if (new_page)
28716bdb913fSHaggai Eran 		page_cache_release(new_page);
287265500d23SHugh Dickins unlock:
28738f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
28741756954cSDavid Rientjes 	if (mmun_end > mmun_start)
28756bdb913fSHaggai Eran 		mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2876e15f8c01SMichel Lespinasse 	if (old_page) {
2877e15f8c01SMichel Lespinasse 		/*
2878e15f8c01SMichel Lespinasse 		 * Don't let another task, with possibly unlocked vma,
2879e15f8c01SMichel Lespinasse 		 * keep the mlocked page.
2880e15f8c01SMichel Lespinasse 		 */
2881e15f8c01SMichel Lespinasse 		if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) {
2882e15f8c01SMichel Lespinasse 			lock_page(old_page);	/* LRU manipulation */
2883e15f8c01SMichel Lespinasse 			munlock_vma_page(old_page);
2884e15f8c01SMichel Lespinasse 			unlock_page(old_page);
2885e15f8c01SMichel Lespinasse 		}
2886e15f8c01SMichel Lespinasse 		page_cache_release(old_page);
2887e15f8c01SMichel Lespinasse 	}
2888f33ea7f4SNick Piggin 	return ret;
28898a9f3ccdSBalbir Singh oom_free_new:
28906dbf6d3bSHugh Dickins 	page_cache_release(new_page);
289165500d23SHugh Dickins oom:
289266521d5aSDominik Dingel 	if (old_page)
28931da177e4SLinus Torvalds 		page_cache_release(old_page);
28941da177e4SLinus Torvalds 	return VM_FAULT_OOM;
28959637a5efSDavid Howells 
28969637a5efSDavid Howells unwritable_page:
28979637a5efSDavid Howells 	page_cache_release(old_page);
2898c2ec175cSNick Piggin 	return ret;
28991da177e4SLinus Torvalds }
29001da177e4SLinus Torvalds 
290197a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma,
29021da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
29031da177e4SLinus Torvalds 		struct zap_details *details)
29041da177e4SLinus Torvalds {
2905f5cc4eefSAl Viro 	zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
29061da177e4SLinus Torvalds }
29071da177e4SLinus Torvalds 
29086b2dbba8SMichel Lespinasse static inline void unmap_mapping_range_tree(struct rb_root *root,
29091da177e4SLinus Torvalds 					    struct zap_details *details)
29101da177e4SLinus Torvalds {
29111da177e4SLinus Torvalds 	struct vm_area_struct *vma;
29121da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
29131da177e4SLinus Torvalds 
29146b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, root,
29151da177e4SLinus Torvalds 			details->first_index, details->last_index) {
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
2918d6e93217SLibin 		vea = vba + vma_pages(vma) - 1;
29191da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
29201da177e4SLinus Torvalds 		zba = details->first_index;
29211da177e4SLinus Torvalds 		if (zba < vba)
29221da177e4SLinus Torvalds 			zba = vba;
29231da177e4SLinus Torvalds 		zea = details->last_index;
29241da177e4SLinus Torvalds 		if (zea > vea)
29251da177e4SLinus Torvalds 			zea = vea;
29261da177e4SLinus Torvalds 
292797a89413SPeter Zijlstra 		unmap_mapping_range_vma(vma,
29281da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
29291da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
293097a89413SPeter Zijlstra 				details);
29311da177e4SLinus Torvalds 	}
29321da177e4SLinus Torvalds }
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head,
29351da177e4SLinus Torvalds 					    struct zap_details *details)
29361da177e4SLinus Torvalds {
29371da177e4SLinus Torvalds 	struct vm_area_struct *vma;
29381da177e4SLinus Torvalds 
29391da177e4SLinus Torvalds 	/*
29401da177e4SLinus Torvalds 	 * In nonlinear VMAs there is no correspondence between virtual address
29411da177e4SLinus Torvalds 	 * offset and file offset.  So we must perform an exhaustive search
29421da177e4SLinus Torvalds 	 * across *all* the pages in each nonlinear VMA, not just the pages
29431da177e4SLinus Torvalds 	 * whose virtual address lies outside the file truncation point.
29441da177e4SLinus Torvalds 	 */
29456b2dbba8SMichel Lespinasse 	list_for_each_entry(vma, head, shared.nonlinear) {
29461da177e4SLinus Torvalds 		details->nonlinear_vma = vma;
294797a89413SPeter Zijlstra 		unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details);
29481da177e4SLinus Torvalds 	}
29491da177e4SLinus Torvalds }
29501da177e4SLinus Torvalds 
29511da177e4SLinus Torvalds /**
295272fd4a35SRobert 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.
29533d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
29541da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
29551da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
295625d9e2d1Snpiggin@suse.de  * boundary.  Note that this is different from truncate_pagecache(), which
29571da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
29581da177e4SLinus Torvalds  * partial pages.
29591da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
29601da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
29611da177e4SLinus Torvalds  * end of the file.
29621da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
29631da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
29641da177e4SLinus Torvalds  */
29651da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
29661da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
29671da177e4SLinus Torvalds {
29681da177e4SLinus Torvalds 	struct zap_details details;
29691da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
29701da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
29711da177e4SLinus Torvalds 
29721da177e4SLinus Torvalds 	/* Check for overflow. */
29731da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
29741da177e4SLinus Torvalds 		long long holeend =
29751da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
29761da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
29771da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
29781da177e4SLinus Torvalds 	}
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
29811da177e4SLinus Torvalds 	details.nonlinear_vma = NULL;
29821da177e4SLinus Torvalds 	details.first_index = hba;
29831da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
29841da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
29851da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
29861da177e4SLinus Torvalds 
298797a89413SPeter Zijlstra 
29883d48ae45SPeter Zijlstra 	mutex_lock(&mapping->i_mmap_mutex);
29896b2dbba8SMichel Lespinasse 	if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
29901da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
29911da177e4SLinus Torvalds 	if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
29921da177e4SLinus Torvalds 		unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
29933d48ae45SPeter Zijlstra 	mutex_unlock(&mapping->i_mmap_mutex);
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
29961da177e4SLinus Torvalds 
29971da177e4SLinus Torvalds /*
29988f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
29998f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
30008f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
30011da177e4SLinus Torvalds  */
300265500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
300365500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
300430c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
30051da177e4SLinus Torvalds {
30068f4e2101SHugh Dickins 	spinlock_t *ptl;
300756f31801SHugh Dickins 	struct page *page, *swapcache;
300865500d23SHugh Dickins 	swp_entry_t entry;
30091da177e4SLinus Torvalds 	pte_t pte;
3010d065bd81SMichel Lespinasse 	int locked;
301156039efaSKAMEZAWA Hiroyuki 	struct mem_cgroup *ptr;
3012ad8c2ee8SRik van Riel 	int exclusive = 0;
301383c54070SNick Piggin 	int ret = 0;
30141da177e4SLinus Torvalds 
30154c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
30168f4e2101SHugh Dickins 		goto out;
301765500d23SHugh Dickins 
301865500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
3019d1737fdbSAndi Kleen 	if (unlikely(non_swap_entry(entry))) {
30200697212aSChristoph Lameter 		if (is_migration_entry(entry)) {
30210697212aSChristoph Lameter 			migration_entry_wait(mm, pmd, address);
3022d1737fdbSAndi Kleen 		} else if (is_hwpoison_entry(entry)) {
3023d1737fdbSAndi Kleen 			ret = VM_FAULT_HWPOISON;
3024d1737fdbSAndi Kleen 		} else {
3025d1737fdbSAndi Kleen 			print_bad_pte(vma, address, orig_pte, NULL);
3026d99be1a8SHugh Dickins 			ret = VM_FAULT_SIGBUS;
3027d1737fdbSAndi Kleen 		}
30280697212aSChristoph Lameter 		goto out;
30290697212aSChristoph Lameter 	}
30300ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
30311da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
30321da177e4SLinus Torvalds 	if (!page) {
303302098feaSHugh Dickins 		page = swapin_readahead(entry,
303402098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, vma, address);
30351da177e4SLinus Torvalds 		if (!page) {
30361da177e4SLinus Torvalds 			/*
30378f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
30388f4e2101SHugh Dickins 			 * while we released the pte lock.
30391da177e4SLinus Torvalds 			 */
30408f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
30411da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
30421da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
30430ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
304465500d23SHugh Dickins 			goto unlock;
30451da177e4SLinus Torvalds 		}
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
30481da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
3049f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
3050456f998eSYing Han 		mem_cgroup_count_vm_event(mm, PGMAJFAULT);
3051d1737fdbSAndi Kleen 	} else if (PageHWPoison(page)) {
305271f72525SWu Fengguang 		/*
305371f72525SWu Fengguang 		 * hwpoisoned dirty swapcache pages are kept for killing
305471f72525SWu Fengguang 		 * owner processes (which may be unknown at hwpoison time)
305571f72525SWu Fengguang 		 */
3056d1737fdbSAndi Kleen 		ret = VM_FAULT_HWPOISON;
3057d1737fdbSAndi Kleen 		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
305856f31801SHugh Dickins 		swapcache = page;
30594779cb31SAndi Kleen 		goto out_release;
30601da177e4SLinus Torvalds 	}
30611da177e4SLinus Torvalds 
306256f31801SHugh Dickins 	swapcache = page;
3063d065bd81SMichel Lespinasse 	locked = lock_page_or_retry(page, mm, flags);
3064e709ffd6SRik van Riel 
306520a1022dSBalbir Singh 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
3066d065bd81SMichel Lespinasse 	if (!locked) {
3067d065bd81SMichel Lespinasse 		ret |= VM_FAULT_RETRY;
3068d065bd81SMichel Lespinasse 		goto out_release;
3069d065bd81SMichel Lespinasse 	}
30701da177e4SLinus Torvalds 
30714969c119SAndrea Arcangeli 	/*
307231c4a3d3SHugh Dickins 	 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
307331c4a3d3SHugh Dickins 	 * release the swapcache from under us.  The page pin, and pte_same
307431c4a3d3SHugh Dickins 	 * test below, are not enough to exclude that.  Even if it is still
307531c4a3d3SHugh Dickins 	 * swapcache, we need to check that the page's swap has not changed.
30764969c119SAndrea Arcangeli 	 */
307731c4a3d3SHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
30784969c119SAndrea Arcangeli 		goto out_page;
30794969c119SAndrea Arcangeli 
3080cbf86cfeSHugh Dickins 	page = ksm_might_need_to_copy(page, vma, address);
30814969c119SAndrea Arcangeli 	if (unlikely(!page)) {
30825ad64688SHugh Dickins 		ret = VM_FAULT_OOM;
30834969c119SAndrea Arcangeli 		page = swapcache;
30844969c119SAndrea Arcangeli 		goto out_page;
30854969c119SAndrea Arcangeli 	}
30865ad64688SHugh Dickins 
30872c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) {
3088073e587eSKAMEZAWA Hiroyuki 		ret = VM_FAULT_OOM;
3089bc43f75cSJohannes Weiner 		goto out_page;
3090073e587eSKAMEZAWA Hiroyuki 	}
3091073e587eSKAMEZAWA Hiroyuki 
30921da177e4SLinus Torvalds 	/*
30938f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
30941da177e4SLinus Torvalds 	 */
30958f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
30969e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
3097b8107480SKirill Korotaev 		goto out_nomap;
3098b8107480SKirill Korotaev 
3099b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
3100b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
3101b8107480SKirill Korotaev 		goto out_nomap;
31021da177e4SLinus Torvalds 	}
31031da177e4SLinus Torvalds 
31048c7c6e34SKAMEZAWA Hiroyuki 	/*
31058c7c6e34SKAMEZAWA Hiroyuki 	 * The page isn't present yet, go ahead with the fault.
31068c7c6e34SKAMEZAWA Hiroyuki 	 *
31078c7c6e34SKAMEZAWA Hiroyuki 	 * Be careful about the sequence of operations here.
31088c7c6e34SKAMEZAWA Hiroyuki 	 * To get its accounting right, reuse_swap_page() must be called
31098c7c6e34SKAMEZAWA Hiroyuki 	 * while the page is counted on swap but not yet in mapcount i.e.
31108c7c6e34SKAMEZAWA Hiroyuki 	 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
31118c7c6e34SKAMEZAWA Hiroyuki 	 * must be called after the swap_free(), or it will never succeed.
311203f3c433SKAMEZAWA Hiroyuki 	 * Because delete_from_swap_page() may be called by reuse_swap_page(),
311303f3c433SKAMEZAWA Hiroyuki 	 * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry
311403f3c433SKAMEZAWA Hiroyuki 	 * in page->private. In this case, a record in swap_cgroup  is silently
311503f3c433SKAMEZAWA Hiroyuki 	 * discarded at swap_free().
31168c7c6e34SKAMEZAWA Hiroyuki 	 */
31171da177e4SLinus Torvalds 
311834e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
3119b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter_fast(mm, MM_SWAPENTS);
31201da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
312130c9f3a9SLinus Torvalds 	if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
31221da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
312330c9f3a9SLinus Torvalds 		flags &= ~FAULT_FLAG_WRITE;
31249a5b489bSAndrea Arcangeli 		ret |= VM_FAULT_WRITE;
3125ad8c2ee8SRik van Riel 		exclusive = 1;
31261da177e4SLinus Torvalds 	}
31271da177e4SLinus Torvalds 	flush_icache_page(vma, page);
3128179ef71cSCyrill Gorcunov 	if (pte_swp_soft_dirty(orig_pte))
3129179ef71cSCyrill Gorcunov 		pte = pte_mksoft_dirty(pte);
31301da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
313156f31801SHugh Dickins 	if (page == swapcache)
3132ad8c2ee8SRik van Riel 		do_page_add_anon_rmap(page, vma, address, exclusive);
313356f31801SHugh Dickins 	else /* ksm created a completely new copy */
313456f31801SHugh Dickins 		page_add_new_anon_rmap(page, vma, address);
313503f3c433SKAMEZAWA Hiroyuki 	/* It's better to call commit-charge after rmap is established */
313603f3c433SKAMEZAWA Hiroyuki 	mem_cgroup_commit_charge_swapin(page, ptr);
31371da177e4SLinus Torvalds 
3138c475a8abSHugh Dickins 	swap_free(entry);
3139b291f000SNick Piggin 	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
3140a2c43eedSHugh Dickins 		try_to_free_swap(page);
3141c475a8abSHugh Dickins 	unlock_page(page);
314256f31801SHugh Dickins 	if (page != swapcache) {
31434969c119SAndrea Arcangeli 		/*
31444969c119SAndrea Arcangeli 		 * Hold the lock to avoid the swap entry to be reused
31454969c119SAndrea Arcangeli 		 * until we take the PT lock for the pte_same() check
31464969c119SAndrea Arcangeli 		 * (to avoid false positives from pte_same). For
31474969c119SAndrea Arcangeli 		 * further safety release the lock after the swap_free
31484969c119SAndrea Arcangeli 		 * so that the swap count won't change under a
31494969c119SAndrea Arcangeli 		 * parallel locked swapcache.
31504969c119SAndrea Arcangeli 		 */
31514969c119SAndrea Arcangeli 		unlock_page(swapcache);
31524969c119SAndrea Arcangeli 		page_cache_release(swapcache);
31534969c119SAndrea Arcangeli 	}
3154c475a8abSHugh Dickins 
315530c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
315661469f1dSHugh Dickins 		ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
315761469f1dSHugh Dickins 		if (ret & VM_FAULT_ERROR)
315861469f1dSHugh Dickins 			ret &= VM_FAULT_ERROR;
31591da177e4SLinus Torvalds 		goto out;
31601da177e4SLinus Torvalds 	}
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
31634b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
316465500d23SHugh Dickins unlock:
31658f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
31661da177e4SLinus Torvalds out:
31671da177e4SLinus Torvalds 	return ret;
3168b8107480SKirill Korotaev out_nomap:
31697a81b88cSKAMEZAWA Hiroyuki 	mem_cgroup_cancel_charge_swapin(ptr);
31708f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
3171bc43f75cSJohannes Weiner out_page:
3172b8107480SKirill Korotaev 	unlock_page(page);
31734779cb31SAndi Kleen out_release:
3174b8107480SKirill Korotaev 	page_cache_release(page);
317556f31801SHugh Dickins 	if (page != swapcache) {
31764969c119SAndrea Arcangeli 		unlock_page(swapcache);
31774969c119SAndrea Arcangeli 		page_cache_release(swapcache);
31784969c119SAndrea Arcangeli 	}
317965500d23SHugh Dickins 	return ret;
31801da177e4SLinus Torvalds }
31811da177e4SLinus Torvalds 
31821da177e4SLinus Torvalds /*
31838ca3eb08SLuck, Tony  * This is like a special single-page "expand_{down|up}wards()",
31848ca3eb08SLuck, Tony  * except we must first make sure that 'address{-|+}PAGE_SIZE'
3185320b2b8dSLinus Torvalds  * doesn't hit another vma.
3186320b2b8dSLinus Torvalds  */
3187320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
3188320b2b8dSLinus Torvalds {
3189320b2b8dSLinus Torvalds 	address &= PAGE_MASK;
3190320b2b8dSLinus Torvalds 	if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
31910e8e50e2SLinus Torvalds 		struct vm_area_struct *prev = vma->vm_prev;
3192320b2b8dSLinus Torvalds 
31930e8e50e2SLinus Torvalds 		/*
31940e8e50e2SLinus Torvalds 		 * Is there a mapping abutting this one below?
31950e8e50e2SLinus Torvalds 		 *
31960e8e50e2SLinus Torvalds 		 * That's only ok if it's the same stack mapping
31970e8e50e2SLinus Torvalds 		 * that has gotten split..
31980e8e50e2SLinus Torvalds 		 */
31990e8e50e2SLinus Torvalds 		if (prev && prev->vm_end == address)
32000e8e50e2SLinus Torvalds 			return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
32010e8e50e2SLinus Torvalds 
3202d05f3169SMichal Hocko 		expand_downwards(vma, address - PAGE_SIZE);
3203320b2b8dSLinus Torvalds 	}
32048ca3eb08SLuck, Tony 	if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
32058ca3eb08SLuck, Tony 		struct vm_area_struct *next = vma->vm_next;
32068ca3eb08SLuck, Tony 
32078ca3eb08SLuck, Tony 		/* As VM_GROWSDOWN but s/below/above/ */
32088ca3eb08SLuck, Tony 		if (next && next->vm_start == address + PAGE_SIZE)
32098ca3eb08SLuck, Tony 			return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
32108ca3eb08SLuck, Tony 
32118ca3eb08SLuck, Tony 		expand_upwards(vma, address + PAGE_SIZE);
32128ca3eb08SLuck, Tony 	}
3213320b2b8dSLinus Torvalds 	return 0;
3214320b2b8dSLinus Torvalds }
3215320b2b8dSLinus Torvalds 
3216320b2b8dSLinus Torvalds /*
32178f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
32188f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
32198f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
32201da177e4SLinus Torvalds  */
322165500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
322265500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
322330c9f3a9SLinus Torvalds 		unsigned int flags)
32241da177e4SLinus Torvalds {
32258f4e2101SHugh Dickins 	struct page *page;
32268f4e2101SHugh Dickins 	spinlock_t *ptl;
32271da177e4SLinus Torvalds 	pte_t entry;
32281da177e4SLinus Torvalds 
32295528f913SLinus Torvalds 	pte_unmap(page_table);
3230320b2b8dSLinus Torvalds 
323111ac5524SLinus Torvalds 	/* Check if we need to add a guard page to the stack */
323211ac5524SLinus Torvalds 	if (check_stack_guard_page(vma, address) < 0)
323311ac5524SLinus Torvalds 		return VM_FAULT_SIGBUS;
323411ac5524SLinus Torvalds 
323511ac5524SLinus Torvalds 	/* Use the zero-page for reads */
323662eede62SHugh Dickins 	if (!(flags & FAULT_FLAG_WRITE)) {
323762eede62SHugh Dickins 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
323862eede62SHugh Dickins 						vma->vm_page_prot));
323911ac5524SLinus Torvalds 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
3240a13ea5b7SHugh Dickins 		if (!pte_none(*page_table))
3241a13ea5b7SHugh Dickins 			goto unlock;
3242a13ea5b7SHugh Dickins 		goto setpte;
3243a13ea5b7SHugh Dickins 	}
3244a13ea5b7SHugh Dickins 
32451da177e4SLinus Torvalds 	/* Allocate our own private page. */
32461da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
324765500d23SHugh Dickins 		goto oom;
3248769848c0SMel Gorman 	page = alloc_zeroed_user_highpage_movable(vma, address);
32491da177e4SLinus Torvalds 	if (!page)
325065500d23SHugh Dickins 		goto oom;
325152f37629SMinchan Kim 	/*
325252f37629SMinchan Kim 	 * The memory barrier inside __SetPageUptodate makes sure that
325352f37629SMinchan Kim 	 * preceeding stores to the page contents become visible before
325452f37629SMinchan Kim 	 * the set_pte_at() write.
325552f37629SMinchan Kim 	 */
32560ed361deSNick Piggin 	__SetPageUptodate(page);
32571da177e4SLinus Torvalds 
32582c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))
32598a9f3ccdSBalbir Singh 		goto oom_free_page;
32608a9f3ccdSBalbir Singh 
326165500d23SHugh Dickins 	entry = mk_pte(page, vma->vm_page_prot);
32621ac0cb5dSHugh Dickins 	if (vma->vm_flags & VM_WRITE)
32631ac0cb5dSHugh Dickins 		entry = pte_mkwrite(pte_mkdirty(entry));
32648f4e2101SHugh Dickins 
32658f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
32661c2fb7a4SAndrea Arcangeli 	if (!pte_none(*page_table))
32678f4e2101SHugh Dickins 		goto release;
32689ba69294SHugh Dickins 
326934e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
32709617d95eSNick Piggin 	page_add_new_anon_rmap(page, vma, address);
3271a13ea5b7SHugh Dickins setpte:
327265500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
32754b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
327665500d23SHugh Dickins unlock:
32778f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
327883c54070SNick Piggin 	return 0;
32798f4e2101SHugh Dickins release:
32808a9f3ccdSBalbir Singh 	mem_cgroup_uncharge_page(page);
32818f4e2101SHugh Dickins 	page_cache_release(page);
32828f4e2101SHugh Dickins 	goto unlock;
32838a9f3ccdSBalbir Singh oom_free_page:
32846dbf6d3bSHugh Dickins 	page_cache_release(page);
328565500d23SHugh Dickins oom:
32861da177e4SLinus Torvalds 	return VM_FAULT_OOM;
32871da177e4SLinus Torvalds }
32881da177e4SLinus Torvalds 
32891da177e4SLinus Torvalds /*
329054cb8821SNick Piggin  * __do_fault() tries to create a new page mapping. It aggressively
32911da177e4SLinus Torvalds  * tries to share with existing pages, but makes a separate copy if
329254cb8821SNick Piggin  * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
329354cb8821SNick Piggin  * the next page fault.
32941da177e4SLinus Torvalds  *
32951da177e4SLinus Torvalds  * As this is called only for pages that do not currently exist, we
32961da177e4SLinus Torvalds  * do not need to flush old virtual caches or the TLB.
32971da177e4SLinus Torvalds  *
32988f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
329916abfa08SHugh Dickins  * but allow concurrent faults), and pte neither mapped nor locked.
33008f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
33011da177e4SLinus Torvalds  */
330254cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
330316abfa08SHugh Dickins 		unsigned long address, pmd_t *pmd,
330454cb8821SNick Piggin 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
33051da177e4SLinus Torvalds {
330616abfa08SHugh Dickins 	pte_t *page_table;
33078f4e2101SHugh Dickins 	spinlock_t *ptl;
3308d0217ac0SNick Piggin 	struct page *page;
33091d65f86dSKAMEZAWA Hiroyuki 	struct page *cow_page;
33101da177e4SLinus Torvalds 	pte_t entry;
33111da177e4SLinus Torvalds 	int anon = 0;
3312d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
3313d0217ac0SNick Piggin 	struct vm_fault vmf;
3314d0217ac0SNick Piggin 	int ret;
3315a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
331654cb8821SNick Piggin 
33171d65f86dSKAMEZAWA Hiroyuki 	/*
33181d65f86dSKAMEZAWA Hiroyuki 	 * If we do COW later, allocate page befor taking lock_page()
33191d65f86dSKAMEZAWA Hiroyuki 	 * on the file cache page. This will reduce lock holding time.
33201d65f86dSKAMEZAWA Hiroyuki 	 */
33211d65f86dSKAMEZAWA Hiroyuki 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
33221d65f86dSKAMEZAWA Hiroyuki 
33231d65f86dSKAMEZAWA Hiroyuki 		if (unlikely(anon_vma_prepare(vma)))
33241d65f86dSKAMEZAWA Hiroyuki 			return VM_FAULT_OOM;
33251d65f86dSKAMEZAWA Hiroyuki 
33261d65f86dSKAMEZAWA Hiroyuki 		cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
33271d65f86dSKAMEZAWA Hiroyuki 		if (!cow_page)
33281d65f86dSKAMEZAWA Hiroyuki 			return VM_FAULT_OOM;
33291d65f86dSKAMEZAWA Hiroyuki 
33301d65f86dSKAMEZAWA Hiroyuki 		if (mem_cgroup_newpage_charge(cow_page, mm, GFP_KERNEL)) {
33311d65f86dSKAMEZAWA Hiroyuki 			page_cache_release(cow_page);
33321d65f86dSKAMEZAWA Hiroyuki 			return VM_FAULT_OOM;
33331d65f86dSKAMEZAWA Hiroyuki 		}
33341d65f86dSKAMEZAWA Hiroyuki 	} else
33351d65f86dSKAMEZAWA Hiroyuki 		cow_page = NULL;
33361d65f86dSKAMEZAWA Hiroyuki 
3337d0217ac0SNick Piggin 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
3338d0217ac0SNick Piggin 	vmf.pgoff = pgoff;
3339d0217ac0SNick Piggin 	vmf.flags = flags;
3340d0217ac0SNick Piggin 	vmf.page = NULL;
33411da177e4SLinus Torvalds 
3342d0217ac0SNick Piggin 	ret = vma->vm_ops->fault(vma, &vmf);
3343d065bd81SMichel Lespinasse 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3344d065bd81SMichel Lespinasse 			    VM_FAULT_RETRY)))
33451d65f86dSKAMEZAWA Hiroyuki 		goto uncharge_out;
33461da177e4SLinus Torvalds 
3347a3b947eaSAndi Kleen 	if (unlikely(PageHWPoison(vmf.page))) {
3348a3b947eaSAndi Kleen 		if (ret & VM_FAULT_LOCKED)
3349a3b947eaSAndi Kleen 			unlock_page(vmf.page);
33501d65f86dSKAMEZAWA Hiroyuki 		ret = VM_FAULT_HWPOISON;
33511d65f86dSKAMEZAWA Hiroyuki 		goto uncharge_out;
3352a3b947eaSAndi Kleen 	}
3353a3b947eaSAndi Kleen 
3354d00806b1SNick Piggin 	/*
3355d0217ac0SNick Piggin 	 * For consistency in subsequent calls, make the faulted page always
3356d00806b1SNick Piggin 	 * locked.
3357d00806b1SNick Piggin 	 */
335883c54070SNick Piggin 	if (unlikely(!(ret & VM_FAULT_LOCKED)))
3359d0217ac0SNick Piggin 		lock_page(vmf.page);
336054cb8821SNick Piggin 	else
3361d0217ac0SNick Piggin 		VM_BUG_ON(!PageLocked(vmf.page));
3362d00806b1SNick Piggin 
33631da177e4SLinus Torvalds 	/*
33641da177e4SLinus Torvalds 	 * Should we do an early C-O-W break?
33651da177e4SLinus Torvalds 	 */
3366d0217ac0SNick Piggin 	page = vmf.page;
336754cb8821SNick Piggin 	if (flags & FAULT_FLAG_WRITE) {
33689637a5efSDavid Howells 		if (!(vma->vm_flags & VM_SHARED)) {
33691d65f86dSKAMEZAWA Hiroyuki 			page = cow_page;
337054cb8821SNick Piggin 			anon = 1;
3371d0217ac0SNick Piggin 			copy_user_highpage(page, vmf.page, address, vma);
33720ed361deSNick Piggin 			__SetPageUptodate(page);
33739637a5efSDavid Howells 		} else {
337454cb8821SNick Piggin 			/*
337554cb8821SNick Piggin 			 * If the page will be shareable, see if the backing
33769637a5efSDavid Howells 			 * address space wants to know that the page is about
337754cb8821SNick Piggin 			 * to become writable
337854cb8821SNick Piggin 			 */
337969676147SMark Fasheh 			if (vma->vm_ops->page_mkwrite) {
3380c2ec175cSNick Piggin 				int tmp;
3381c2ec175cSNick Piggin 
338269676147SMark Fasheh 				unlock_page(page);
3383b827e496SNick Piggin 				vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
3384c2ec175cSNick Piggin 				tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
3385c2ec175cSNick Piggin 				if (unlikely(tmp &
3386c2ec175cSNick Piggin 					  (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
3387c2ec175cSNick Piggin 					ret = tmp;
3388b827e496SNick Piggin 					goto unwritable_page;
338969676147SMark Fasheh 				}
3390b827e496SNick Piggin 				if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
339169676147SMark Fasheh 					lock_page(page);
3392d0217ac0SNick Piggin 					if (!page->mapping) {
3393b827e496SNick Piggin 						ret = 0; /* retry the fault */
3394b827e496SNick Piggin 						unlock_page(page);
3395b827e496SNick Piggin 						goto unwritable_page;
3396d0217ac0SNick Piggin 					}
3397b827e496SNick Piggin 				} else
3398b827e496SNick Piggin 					VM_BUG_ON(!PageLocked(page));
3399a200ee18SPeter Zijlstra 				page_mkwrite = 1;
34009637a5efSDavid Howells 			}
34019637a5efSDavid Howells 		}
340254cb8821SNick Piggin 
34031da177e4SLinus Torvalds 	}
34041da177e4SLinus Torvalds 
34058f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
34061da177e4SLinus Torvalds 
34071da177e4SLinus Torvalds 	/*
34081da177e4SLinus Torvalds 	 * This silly early PAGE_DIRTY setting removes a race
34091da177e4SLinus Torvalds 	 * due to the bad i386 page protection. But it's valid
34101da177e4SLinus Torvalds 	 * for other architectures too.
34111da177e4SLinus Torvalds 	 *
341230c9f3a9SLinus Torvalds 	 * Note that if FAULT_FLAG_WRITE is set, we either now have
34131da177e4SLinus Torvalds 	 * an exclusive copy of the page, or this is a shared mapping,
34141da177e4SLinus Torvalds 	 * so we can make it writable and dirty to avoid having to
34151da177e4SLinus Torvalds 	 * handle that later.
34161da177e4SLinus Torvalds 	 */
34171da177e4SLinus Torvalds 	/* Only go through if we didn't race with anybody else... */
34181c2fb7a4SAndrea Arcangeli 	if (likely(pte_same(*page_table, orig_pte))) {
3419d00806b1SNick Piggin 		flush_icache_page(vma, page);
3420d00806b1SNick Piggin 		entry = mk_pte(page, vma->vm_page_prot);
342154cb8821SNick Piggin 		if (flags & FAULT_FLAG_WRITE)
34221da177e4SLinus Torvalds 			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
342341bb3476SCyrill Gorcunov 		else if (pte_file(orig_pte) && pte_file_soft_dirty(orig_pte))
342441bb3476SCyrill Gorcunov 			pte_mksoft_dirty(entry);
34251da177e4SLinus Torvalds 		if (anon) {
342634e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
3427d00806b1SNick Piggin 			page_add_new_anon_rmap(page, vma, address);
3428f57e88a8SHugh Dickins 		} else {
342934e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_FILEPAGES);
3430d00806b1SNick Piggin 			page_add_file_rmap(page);
343154cb8821SNick Piggin 			if (flags & FAULT_FLAG_WRITE) {
3432d00806b1SNick Piggin 				dirty_page = page;
3433d08b3851SPeter Zijlstra 				get_page(dirty_page);
3434d08b3851SPeter Zijlstra 			}
34354294621fSHugh Dickins 		}
343664d6519dSLee Schermerhorn 		set_pte_at(mm, address, page_table, entry);
34371da177e4SLinus Torvalds 
3438d00806b1SNick Piggin 		/* no need to invalidate: a not-present page won't be cached */
34394b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
3440d00806b1SNick Piggin 	} else {
34411d65f86dSKAMEZAWA Hiroyuki 		if (cow_page)
34421d65f86dSKAMEZAWA Hiroyuki 			mem_cgroup_uncharge_page(cow_page);
3443d00806b1SNick Piggin 		if (anon)
3444d00806b1SNick Piggin 			page_cache_release(page);
3445d00806b1SNick Piggin 		else
344654cb8821SNick Piggin 			anon = 1; /* no anon but release faulted_page */
3447d00806b1SNick Piggin 	}
3448d00806b1SNick Piggin 
34498f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
3450d00806b1SNick Piggin 
3451b827e496SNick Piggin 	if (dirty_page) {
3452b827e496SNick Piggin 		struct address_space *mapping = page->mapping;
345341c4d25fSJan Kara 		int dirtied = 0;
34548f7b3d15SAnton Salikhmetov 
3455b827e496SNick Piggin 		if (set_page_dirty(dirty_page))
345641c4d25fSJan Kara 			dirtied = 1;
3457b827e496SNick Piggin 		unlock_page(dirty_page);
3458d08b3851SPeter Zijlstra 		put_page(dirty_page);
345941c4d25fSJan Kara 		if ((dirtied || page_mkwrite) && mapping) {
3460b827e496SNick Piggin 			/*
3461b827e496SNick Piggin 			 * Some device drivers do not set page.mapping but still
3462b827e496SNick Piggin 			 * dirty their pages
3463b827e496SNick Piggin 			 */
3464b827e496SNick Piggin 			balance_dirty_pages_ratelimited(mapping);
3465d08b3851SPeter Zijlstra 		}
3466d00806b1SNick Piggin 
3467b827e496SNick Piggin 		/* file_update_time outside page_lock */
346841c4d25fSJan Kara 		if (vma->vm_file && !page_mkwrite)
3469b827e496SNick Piggin 			file_update_time(vma->vm_file);
3470b827e496SNick Piggin 	} else {
3471b827e496SNick Piggin 		unlock_page(vmf.page);
3472b827e496SNick Piggin 		if (anon)
3473b827e496SNick Piggin 			page_cache_release(vmf.page);
3474b827e496SNick Piggin 	}
3475b827e496SNick Piggin 
3476b827e496SNick Piggin 	return ret;
3477b827e496SNick Piggin 
3478b827e496SNick Piggin unwritable_page:
3479b827e496SNick Piggin 	page_cache_release(page);
348083c54070SNick Piggin 	return ret;
34811d65f86dSKAMEZAWA Hiroyuki uncharge_out:
34821d65f86dSKAMEZAWA Hiroyuki 	/* fs's fault handler get error */
34831d65f86dSKAMEZAWA Hiroyuki 	if (cow_page) {
34841d65f86dSKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_page(cow_page);
34851d65f86dSKAMEZAWA Hiroyuki 		page_cache_release(cow_page);
34861d65f86dSKAMEZAWA Hiroyuki 	}
34871d65f86dSKAMEZAWA Hiroyuki 	return ret;
348854cb8821SNick Piggin }
3489d00806b1SNick Piggin 
349054cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
349154cb8821SNick Piggin 		unsigned long address, pte_t *page_table, pmd_t *pmd,
349230c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
349354cb8821SNick Piggin {
349454cb8821SNick Piggin 	pgoff_t pgoff = (((address & PAGE_MASK)
34950da7e01fSDean Nelson 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
349654cb8821SNick Piggin 
349716abfa08SHugh Dickins 	pte_unmap(page_table);
349816abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
349954cb8821SNick Piggin }
350054cb8821SNick Piggin 
3501f4b81804SJes Sorensen /*
35021da177e4SLinus Torvalds  * Fault of a previously existing named mapping. Repopulate the pte
35031da177e4SLinus Torvalds  * from the encoded file_pte if possible. This enables swappable
35041da177e4SLinus Torvalds  * nonlinear vmas.
35058f4e2101SHugh Dickins  *
35068f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
35078f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
35088f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
35091da177e4SLinus Torvalds  */
3510d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
351165500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
351230c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
35131da177e4SLinus Torvalds {
351465500d23SHugh Dickins 	pgoff_t pgoff;
35151da177e4SLinus Torvalds 
351630c9f3a9SLinus Torvalds 	flags |= FAULT_FLAG_NONLINEAR;
351730c9f3a9SLinus Torvalds 
35184c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
351983c54070SNick Piggin 		return 0;
35201da177e4SLinus Torvalds 
35212509ef26SHugh Dickins 	if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
352265500d23SHugh Dickins 		/*
352365500d23SHugh Dickins 		 * Page table corrupted: show pte and kill process.
352465500d23SHugh Dickins 		 */
35253dc14741SHugh Dickins 		print_bad_pte(vma, address, orig_pte, NULL);
3526d99be1a8SHugh Dickins 		return VM_FAULT_SIGBUS;
352765500d23SHugh Dickins 	}
352865500d23SHugh Dickins 
352965500d23SHugh Dickins 	pgoff = pte_to_pgoff(orig_pte);
353016abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
35311da177e4SLinus Torvalds }
35321da177e4SLinus Torvalds 
35339532fec1SMel Gorman int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
353404bb2f94SRik van Riel 				unsigned long addr, int page_nid,
353504bb2f94SRik van Riel 				int *flags)
35369532fec1SMel Gorman {
35379532fec1SMel Gorman 	get_page(page);
35389532fec1SMel Gorman 
35399532fec1SMel Gorman 	count_vm_numa_event(NUMA_HINT_FAULTS);
354004bb2f94SRik van Riel 	if (page_nid == numa_node_id()) {
35419532fec1SMel Gorman 		count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
354204bb2f94SRik van Riel 		*flags |= TNF_FAULT_LOCAL;
354304bb2f94SRik van Riel 	}
35449532fec1SMel Gorman 
35459532fec1SMel Gorman 	return mpol_misplaced(page, vma, addr);
35469532fec1SMel Gorman }
35479532fec1SMel Gorman 
3548d10e63f2SMel Gorman int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3549d10e63f2SMel Gorman 		   unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
3550d10e63f2SMel Gorman {
35514daae3b4SMel Gorman 	struct page *page = NULL;
3552d10e63f2SMel Gorman 	spinlock_t *ptl;
35538191acbdSMel Gorman 	int page_nid = -1;
355490572890SPeter Zijlstra 	int last_cpupid;
3555cbee9f88SPeter Zijlstra 	int target_nid;
3556b8593bfdSMel Gorman 	bool migrated = false;
35576688cc05SPeter Zijlstra 	int flags = 0;
3558d10e63f2SMel Gorman 
3559d10e63f2SMel Gorman 	/*
3560d10e63f2SMel Gorman 	* The "pte" at this point cannot be used safely without
3561d10e63f2SMel Gorman 	* validation through pte_unmap_same(). It's of NUMA type but
3562d10e63f2SMel Gorman 	* the pfn may be screwed if the read is non atomic.
3563d10e63f2SMel Gorman 	*
3564d10e63f2SMel Gorman 	* ptep_modify_prot_start is not called as this is clearing
3565d10e63f2SMel Gorman 	* the _PAGE_NUMA bit and it is not really expected that there
3566d10e63f2SMel Gorman 	* would be concurrent hardware modifications to the PTE.
3567d10e63f2SMel Gorman 	*/
3568d10e63f2SMel Gorman 	ptl = pte_lockptr(mm, pmd);
3569d10e63f2SMel Gorman 	spin_lock(ptl);
35704daae3b4SMel Gorman 	if (unlikely(!pte_same(*ptep, pte))) {
35714daae3b4SMel Gorman 		pte_unmap_unlock(ptep, ptl);
35724daae3b4SMel Gorman 		goto out;
35734daae3b4SMel Gorman 	}
35744daae3b4SMel Gorman 
3575d10e63f2SMel Gorman 	pte = pte_mknonnuma(pte);
3576d10e63f2SMel Gorman 	set_pte_at(mm, addr, ptep, pte);
3577d10e63f2SMel Gorman 	update_mmu_cache(vma, addr, ptep);
3578d10e63f2SMel Gorman 
3579d10e63f2SMel Gorman 	page = vm_normal_page(vma, addr, pte);
3580d10e63f2SMel Gorman 	if (!page) {
3581d10e63f2SMel Gorman 		pte_unmap_unlock(ptep, ptl);
3582d10e63f2SMel Gorman 		return 0;
3583d10e63f2SMel Gorman 	}
3584a1a46184SMel Gorman 	BUG_ON(is_zero_pfn(page_to_pfn(page)));
3585d10e63f2SMel Gorman 
35866688cc05SPeter Zijlstra 	/*
35876688cc05SPeter Zijlstra 	 * Avoid grouping on DSO/COW pages in specific and RO pages
35886688cc05SPeter Zijlstra 	 * in general, RO pages shouldn't hurt as much anyway since
35896688cc05SPeter Zijlstra 	 * they can be in shared cache state.
35906688cc05SPeter Zijlstra 	 */
35916688cc05SPeter Zijlstra 	if (!pte_write(pte))
35926688cc05SPeter Zijlstra 		flags |= TNF_NO_GROUP;
35936688cc05SPeter Zijlstra 
3594dabe1d99SRik van Riel 	/*
3595dabe1d99SRik van Riel 	 * Flag if the page is shared between multiple address spaces. This
3596dabe1d99SRik van Riel 	 * is later used when determining whether to group tasks together
3597dabe1d99SRik van Riel 	 */
3598dabe1d99SRik van Riel 	if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3599dabe1d99SRik van Riel 		flags |= TNF_SHARED;
3600dabe1d99SRik van Riel 
360190572890SPeter Zijlstra 	last_cpupid = page_cpupid_last(page);
36028191acbdSMel Gorman 	page_nid = page_to_nid(page);
360304bb2f94SRik van Riel 	target_nid = numa_migrate_prep(page, vma, addr, page_nid, &flags);
3604d10e63f2SMel Gorman 	pte_unmap_unlock(ptep, ptl);
36054daae3b4SMel Gorman 	if (target_nid == -1) {
36064daae3b4SMel Gorman 		put_page(page);
36074daae3b4SMel Gorman 		goto out;
36084daae3b4SMel Gorman 	}
36094daae3b4SMel Gorman 
36104daae3b4SMel Gorman 	/* Migrate to the requested node */
36111bc115d8SMel Gorman 	migrated = migrate_misplaced_page(page, vma, target_nid);
36126688cc05SPeter Zijlstra 	if (migrated) {
36138191acbdSMel Gorman 		page_nid = target_nid;
36146688cc05SPeter Zijlstra 		flags |= TNF_MIGRATED;
36156688cc05SPeter Zijlstra 	}
36164daae3b4SMel Gorman 
36174daae3b4SMel Gorman out:
36188191acbdSMel Gorman 	if (page_nid != -1)
36196688cc05SPeter Zijlstra 		task_numa_fault(last_cpupid, page_nid, 1, flags);
3620d10e63f2SMel Gorman 	return 0;
3621d10e63f2SMel Gorman }
3622d10e63f2SMel Gorman 
36231da177e4SLinus Torvalds /*
36241da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
36251da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
36261da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
36271da177e4SLinus Torvalds  *
36281da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
36291da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
36301da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
36311da177e4SLinus Torvalds  *
3632c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
3633c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
3634c74df32cSHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
36351da177e4SLinus Torvalds  */
3636c0292554SKirill A. Shutemov static int handle_pte_fault(struct mm_struct *mm,
36371da177e4SLinus Torvalds 		     struct vm_area_struct *vma, unsigned long address,
363830c9f3a9SLinus Torvalds 		     pte_t *pte, pmd_t *pmd, unsigned int flags)
36391da177e4SLinus Torvalds {
36401da177e4SLinus Torvalds 	pte_t entry;
36418f4e2101SHugh Dickins 	spinlock_t *ptl;
36421da177e4SLinus Torvalds 
36438dab5241SBenjamin Herrenschmidt 	entry = *pte;
36441da177e4SLinus Torvalds 	if (!pte_present(entry)) {
364565500d23SHugh Dickins 		if (pte_none(entry)) {
3646f4b81804SJes Sorensen 			if (vma->vm_ops) {
36473c18ddd1SNick Piggin 				if (likely(vma->vm_ops->fault))
364854cb8821SNick Piggin 					return do_linear_fault(mm, vma, address,
364930c9f3a9SLinus Torvalds 						pte, pmd, flags, entry);
3650f4b81804SJes Sorensen 			}
3651f4b81804SJes Sorensen 			return do_anonymous_page(mm, vma, address,
365230c9f3a9SLinus Torvalds 						 pte, pmd, flags);
365365500d23SHugh Dickins 		}
36541da177e4SLinus Torvalds 		if (pte_file(entry))
3655d0217ac0SNick Piggin 			return do_nonlinear_fault(mm, vma, address,
365630c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
365765500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
365830c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
36591da177e4SLinus Torvalds 	}
36601da177e4SLinus Torvalds 
3661d10e63f2SMel Gorman 	if (pte_numa(entry))
3662d10e63f2SMel Gorman 		return do_numa_page(mm, vma, address, entry, pte, pmd);
3663d10e63f2SMel Gorman 
36644c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
36658f4e2101SHugh Dickins 	spin_lock(ptl);
36668f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
36678f4e2101SHugh Dickins 		goto unlock;
366830c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
36691da177e4SLinus Torvalds 		if (!pte_write(entry))
36708f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
36718f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
36721da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
36731da177e4SLinus Torvalds 	}
36741da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
367530c9f3a9SLinus Torvalds 	if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
36764b3073e1SRussell King 		update_mmu_cache(vma, address, pte);
36771a44e149SAndrea Arcangeli 	} else {
36781a44e149SAndrea Arcangeli 		/*
36791a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
36801a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
36811a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
36821a44e149SAndrea Arcangeli 		 * with threads.
36831a44e149SAndrea Arcangeli 		 */
368430c9f3a9SLinus Torvalds 		if (flags & FAULT_FLAG_WRITE)
368561c77326SShaohua Li 			flush_tlb_fix_spurious_fault(vma, address);
36861a44e149SAndrea Arcangeli 	}
36878f4e2101SHugh Dickins unlock:
36888f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
368983c54070SNick Piggin 	return 0;
36901da177e4SLinus Torvalds }
36911da177e4SLinus Torvalds 
36921da177e4SLinus Torvalds /*
36931da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
36941da177e4SLinus Torvalds  */
3695519e5247SJohannes Weiner static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3696d06063ccSLinus Torvalds 			     unsigned long address, unsigned int flags)
36971da177e4SLinus Torvalds {
36981da177e4SLinus Torvalds 	pgd_t *pgd;
36991da177e4SLinus Torvalds 	pud_t *pud;
37001da177e4SLinus Torvalds 	pmd_t *pmd;
37011da177e4SLinus Torvalds 	pte_t *pte;
37021da177e4SLinus Torvalds 
3703ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
370430c9f3a9SLinus Torvalds 		return hugetlb_fault(mm, vma, address, flags);
37051da177e4SLinus Torvalds 
37061f1d06c3SDavid Rientjes retry:
37071da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
37081da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
37091da177e4SLinus Torvalds 	if (!pud)
3710c74df32cSHugh Dickins 		return VM_FAULT_OOM;
37111da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
37121da177e4SLinus Torvalds 	if (!pmd)
3713c74df32cSHugh Dickins 		return VM_FAULT_OOM;
371471e3aac0SAndrea Arcangeli 	if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
3715c0292554SKirill A. Shutemov 		int ret = VM_FAULT_FALLBACK;
371671e3aac0SAndrea Arcangeli 		if (!vma->vm_ops)
3717c0292554SKirill A. Shutemov 			ret = do_huge_pmd_anonymous_page(mm, vma, address,
371871e3aac0SAndrea Arcangeli 					pmd, flags);
3719c0292554SKirill A. Shutemov 		if (!(ret & VM_FAULT_FALLBACK))
3720c0292554SKirill A. Shutemov 			return ret;
372171e3aac0SAndrea Arcangeli 	} else {
372271e3aac0SAndrea Arcangeli 		pmd_t orig_pmd = *pmd;
37231f1d06c3SDavid Rientjes 		int ret;
37241f1d06c3SDavid Rientjes 
372571e3aac0SAndrea Arcangeli 		barrier();
372671e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(orig_pmd)) {
3727a1dd450bSWill Deacon 			unsigned int dirty = flags & FAULT_FLAG_WRITE;
3728a1dd450bSWill Deacon 
3729e53289c0SLinus Torvalds 			/*
3730e53289c0SLinus Torvalds 			 * If the pmd is splitting, return and retry the
3731e53289c0SLinus Torvalds 			 * the fault.  Alternative: wait until the split
3732e53289c0SLinus Torvalds 			 * is done, and goto retry.
3733e53289c0SLinus Torvalds 			 */
3734e53289c0SLinus Torvalds 			if (pmd_trans_splitting(orig_pmd))
3735e53289c0SLinus Torvalds 				return 0;
3736e53289c0SLinus Torvalds 
37373d59eebcSLinus Torvalds 			if (pmd_numa(orig_pmd))
37384daae3b4SMel Gorman 				return do_huge_pmd_numa_page(mm, vma, address,
3739d10e63f2SMel Gorman 							     orig_pmd, pmd);
3740d10e63f2SMel Gorman 
37413d59eebcSLinus Torvalds 			if (dirty && !pmd_write(orig_pmd)) {
37421f1d06c3SDavid Rientjes 				ret = do_huge_pmd_wp_page(mm, vma, address, pmd,
37431f1d06c3SDavid Rientjes 							  orig_pmd);
37441f1d06c3SDavid Rientjes 				/*
37451f1d06c3SDavid Rientjes 				 * If COW results in an oom, the huge pmd will
37461f1d06c3SDavid Rientjes 				 * have been split, so retry the fault on the
37471f1d06c3SDavid Rientjes 				 * pte for a smaller charge.
37481f1d06c3SDavid Rientjes 				 */
37491f1d06c3SDavid Rientjes 				if (unlikely(ret & VM_FAULT_OOM))
37501f1d06c3SDavid Rientjes 					goto retry;
37511f1d06c3SDavid Rientjes 				return ret;
3752a1dd450bSWill Deacon 			} else {
3753a1dd450bSWill Deacon 				huge_pmd_set_accessed(mm, vma, address, pmd,
3754a1dd450bSWill Deacon 						      orig_pmd, dirty);
37551f1d06c3SDavid Rientjes 			}
3756d10e63f2SMel Gorman 
375771e3aac0SAndrea Arcangeli 			return 0;
375871e3aac0SAndrea Arcangeli 		}
375971e3aac0SAndrea Arcangeli 	}
376071e3aac0SAndrea Arcangeli 
37610f19c179SMel Gorman 	/* THP should already have been handled */
37620f19c179SMel Gorman 	BUG_ON(pmd_numa(*pmd));
3763d10e63f2SMel Gorman 
376471e3aac0SAndrea Arcangeli 	/*
376571e3aac0SAndrea Arcangeli 	 * Use __pte_alloc instead of pte_alloc_map, because we can't
376671e3aac0SAndrea Arcangeli 	 * run pte_offset_map on the pmd, if an huge pmd could
376771e3aac0SAndrea Arcangeli 	 * materialize from under us from a different thread.
376871e3aac0SAndrea Arcangeli 	 */
37694fd01770SMel Gorman 	if (unlikely(pmd_none(*pmd)) &&
37704fd01770SMel Gorman 	    unlikely(__pte_alloc(mm, vma, pmd, address)))
3771c74df32cSHugh Dickins 		return VM_FAULT_OOM;
377271e3aac0SAndrea Arcangeli 	/* if an huge pmd materialized from under us just retry later */
377371e3aac0SAndrea Arcangeli 	if (unlikely(pmd_trans_huge(*pmd)))
377471e3aac0SAndrea Arcangeli 		return 0;
377571e3aac0SAndrea Arcangeli 	/*
377671e3aac0SAndrea Arcangeli 	 * A regular pmd is established and it can't morph into a huge pmd
377771e3aac0SAndrea Arcangeli 	 * from under us anymore at this point because we hold the mmap_sem
377871e3aac0SAndrea Arcangeli 	 * read mode and khugepaged takes it in write mode. So now it's
377971e3aac0SAndrea Arcangeli 	 * safe to run pte_offset_map().
378071e3aac0SAndrea Arcangeli 	 */
378171e3aac0SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
37821da177e4SLinus Torvalds 
378330c9f3a9SLinus Torvalds 	return handle_pte_fault(mm, vma, address, pte, pmd, flags);
37841da177e4SLinus Torvalds }
37851da177e4SLinus Torvalds 
3786519e5247SJohannes Weiner int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3787519e5247SJohannes Weiner 		    unsigned long address, unsigned int flags)
3788519e5247SJohannes Weiner {
3789519e5247SJohannes Weiner 	int ret;
3790519e5247SJohannes Weiner 
3791519e5247SJohannes Weiner 	__set_current_state(TASK_RUNNING);
3792519e5247SJohannes Weiner 
3793519e5247SJohannes Weiner 	count_vm_event(PGFAULT);
3794519e5247SJohannes Weiner 	mem_cgroup_count_vm_event(mm, PGFAULT);
3795519e5247SJohannes Weiner 
3796519e5247SJohannes Weiner 	/* do counter updates before entering really critical section. */
3797519e5247SJohannes Weiner 	check_sync_rss_stat(current);
3798519e5247SJohannes Weiner 
3799519e5247SJohannes Weiner 	/*
3800519e5247SJohannes Weiner 	 * Enable the memcg OOM handling for faults triggered in user
3801519e5247SJohannes Weiner 	 * space.  Kernel faults are handled more gracefully.
3802519e5247SJohannes Weiner 	 */
3803519e5247SJohannes Weiner 	if (flags & FAULT_FLAG_USER)
380449426420SJohannes Weiner 		mem_cgroup_oom_enable();
3805519e5247SJohannes Weiner 
3806519e5247SJohannes Weiner 	ret = __handle_mm_fault(mm, vma, address, flags);
3807519e5247SJohannes Weiner 
380849426420SJohannes Weiner 	if (flags & FAULT_FLAG_USER) {
380949426420SJohannes Weiner 		mem_cgroup_oom_disable();
381049426420SJohannes Weiner                 /*
381149426420SJohannes Weiner                  * The task may have entered a memcg OOM situation but
381249426420SJohannes Weiner                  * if the allocation error was handled gracefully (no
381349426420SJohannes Weiner                  * VM_FAULT_OOM), there is no need to kill anything.
381449426420SJohannes Weiner                  * Just clean up the OOM state peacefully.
381549426420SJohannes Weiner                  */
381649426420SJohannes Weiner                 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
381749426420SJohannes Weiner                         mem_cgroup_oom_synchronize(false);
381849426420SJohannes Weiner 	}
38193812c8c8SJohannes Weiner 
3820519e5247SJohannes Weiner 	return ret;
3821519e5247SJohannes Weiner }
3822519e5247SJohannes Weiner 
38231da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
38241da177e4SLinus Torvalds /*
38251da177e4SLinus Torvalds  * Allocate page upper directory.
3826872fec16SHugh Dickins  * We've already handled the fast-path in-line.
38271da177e4SLinus Torvalds  */
38281bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
38291da177e4SLinus Torvalds {
3830c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
3831c74df32cSHugh Dickins 	if (!new)
38321bb3630eSHugh Dickins 		return -ENOMEM;
38331da177e4SLinus Torvalds 
3834362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3835362a61adSNick Piggin 
3836872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
38371bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
38385e541973SBenjamin Herrenschmidt 		pud_free(mm, new);
38391bb3630eSHugh Dickins 	else
38401da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
3841872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
38421bb3630eSHugh Dickins 	return 0;
38431da177e4SLinus Torvalds }
38441da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
38451da177e4SLinus Torvalds 
38461da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
38471da177e4SLinus Torvalds /*
38481da177e4SLinus Torvalds  * Allocate page middle directory.
3849872fec16SHugh Dickins  * We've already handled the fast-path in-line.
38501da177e4SLinus Torvalds  */
38511bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
38521da177e4SLinus Torvalds {
3853c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
3854c74df32cSHugh Dickins 	if (!new)
38551bb3630eSHugh Dickins 		return -ENOMEM;
38561da177e4SLinus Torvalds 
3857362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3858362a61adSNick Piggin 
3859872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
38601da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
38611bb3630eSHugh Dickins 	if (pud_present(*pud))		/* Another has populated it */
38625e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
38631bb3630eSHugh Dickins 	else
38641da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
38651da177e4SLinus Torvalds #else
38661bb3630eSHugh Dickins 	if (pgd_present(*pud))		/* Another has populated it */
38675e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
38681bb3630eSHugh Dickins 	else
38691da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
38701da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
3871872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
38721bb3630eSHugh Dickins 	return 0;
38731da177e4SLinus Torvalds }
38741da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
38751da177e4SLinus Torvalds 
38761da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA)
38771da177e4SLinus Torvalds 
38781da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR)
38795ce7852cSAdrian Bunk static struct vm_area_struct gate_vma;
38801da177e4SLinus Torvalds 
38811da177e4SLinus Torvalds static int __init gate_vma_init(void)
38821da177e4SLinus Torvalds {
38831da177e4SLinus Torvalds 	gate_vma.vm_mm = NULL;
38841da177e4SLinus Torvalds 	gate_vma.vm_start = FIXADDR_USER_START;
38851da177e4SLinus Torvalds 	gate_vma.vm_end = FIXADDR_USER_END;
3886b6558c4aSRoland McGrath 	gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
3887b6558c4aSRoland McGrath 	gate_vma.vm_page_prot = __P101;
3888909af768SJason Baron 
38891da177e4SLinus Torvalds 	return 0;
38901da177e4SLinus Torvalds }
38911da177e4SLinus Torvalds __initcall(gate_vma_init);
38921da177e4SLinus Torvalds #endif
38931da177e4SLinus Torvalds 
389431db58b3SStephen Wilson struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
38951da177e4SLinus Torvalds {
38961da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
38971da177e4SLinus Torvalds 	return &gate_vma;
38981da177e4SLinus Torvalds #else
38991da177e4SLinus Torvalds 	return NULL;
39001da177e4SLinus Torvalds #endif
39011da177e4SLinus Torvalds }
39021da177e4SLinus Torvalds 
3903cae5d390SStephen Wilson int in_gate_area_no_mm(unsigned long addr)
39041da177e4SLinus Torvalds {
39051da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
39061da177e4SLinus Torvalds 	if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
39071da177e4SLinus Torvalds 		return 1;
39081da177e4SLinus Torvalds #endif
39091da177e4SLinus Torvalds 	return 0;
39101da177e4SLinus Torvalds }
39111da177e4SLinus Torvalds 
39121da177e4SLinus Torvalds #endif	/* __HAVE_ARCH_GATE_AREA */
39130ec76a11SDavid Howells 
39141b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address,
3915f8ad0f49SJohannes Weiner 		pte_t **ptepp, spinlock_t **ptlp)
3916f8ad0f49SJohannes Weiner {
3917f8ad0f49SJohannes Weiner 	pgd_t *pgd;
3918f8ad0f49SJohannes Weiner 	pud_t *pud;
3919f8ad0f49SJohannes Weiner 	pmd_t *pmd;
3920f8ad0f49SJohannes Weiner 	pte_t *ptep;
3921f8ad0f49SJohannes Weiner 
3922f8ad0f49SJohannes Weiner 	pgd = pgd_offset(mm, address);
3923f8ad0f49SJohannes Weiner 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3924f8ad0f49SJohannes Weiner 		goto out;
3925f8ad0f49SJohannes Weiner 
3926f8ad0f49SJohannes Weiner 	pud = pud_offset(pgd, address);
3927f8ad0f49SJohannes Weiner 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3928f8ad0f49SJohannes Weiner 		goto out;
3929f8ad0f49SJohannes Weiner 
3930f8ad0f49SJohannes Weiner 	pmd = pmd_offset(pud, address);
3931f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
3932f8ad0f49SJohannes Weiner 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3933f8ad0f49SJohannes Weiner 		goto out;
3934f8ad0f49SJohannes Weiner 
3935f8ad0f49SJohannes Weiner 	/* We cannot handle huge page PFN maps. Luckily they don't exist. */
3936f8ad0f49SJohannes Weiner 	if (pmd_huge(*pmd))
3937f8ad0f49SJohannes Weiner 		goto out;
3938f8ad0f49SJohannes Weiner 
3939f8ad0f49SJohannes Weiner 	ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3940f8ad0f49SJohannes Weiner 	if (!ptep)
3941f8ad0f49SJohannes Weiner 		goto out;
3942f8ad0f49SJohannes Weiner 	if (!pte_present(*ptep))
3943f8ad0f49SJohannes Weiner 		goto unlock;
3944f8ad0f49SJohannes Weiner 	*ptepp = ptep;
3945f8ad0f49SJohannes Weiner 	return 0;
3946f8ad0f49SJohannes Weiner unlock:
3947f8ad0f49SJohannes Weiner 	pte_unmap_unlock(ptep, *ptlp);
3948f8ad0f49SJohannes Weiner out:
3949f8ad0f49SJohannes Weiner 	return -EINVAL;
3950f8ad0f49SJohannes Weiner }
3951f8ad0f49SJohannes Weiner 
39521b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address,
39531b36ba81SNamhyung Kim 			     pte_t **ptepp, spinlock_t **ptlp)
39541b36ba81SNamhyung Kim {
39551b36ba81SNamhyung Kim 	int res;
39561b36ba81SNamhyung Kim 
39571b36ba81SNamhyung Kim 	/* (void) is needed to make gcc happy */
39581b36ba81SNamhyung Kim 	(void) __cond_lock(*ptlp,
39591b36ba81SNamhyung Kim 			   !(res = __follow_pte(mm, address, ptepp, ptlp)));
39601b36ba81SNamhyung Kim 	return res;
39611b36ba81SNamhyung Kim }
39621b36ba81SNamhyung Kim 
39633b6748e2SJohannes Weiner /**
39643b6748e2SJohannes Weiner  * follow_pfn - look up PFN at a user virtual address
39653b6748e2SJohannes Weiner  * @vma: memory mapping
39663b6748e2SJohannes Weiner  * @address: user virtual address
39673b6748e2SJohannes Weiner  * @pfn: location to store found PFN
39683b6748e2SJohannes Weiner  *
39693b6748e2SJohannes Weiner  * Only IO mappings and raw PFN mappings are allowed.
39703b6748e2SJohannes Weiner  *
39713b6748e2SJohannes Weiner  * Returns zero and the pfn at @pfn on success, -ve otherwise.
39723b6748e2SJohannes Weiner  */
39733b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address,
39743b6748e2SJohannes Weiner 	unsigned long *pfn)
39753b6748e2SJohannes Weiner {
39763b6748e2SJohannes Weiner 	int ret = -EINVAL;
39773b6748e2SJohannes Weiner 	spinlock_t *ptl;
39783b6748e2SJohannes Weiner 	pte_t *ptep;
39793b6748e2SJohannes Weiner 
39803b6748e2SJohannes Weiner 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
39813b6748e2SJohannes Weiner 		return ret;
39823b6748e2SJohannes Weiner 
39833b6748e2SJohannes Weiner 	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
39843b6748e2SJohannes Weiner 	if (ret)
39853b6748e2SJohannes Weiner 		return ret;
39863b6748e2SJohannes Weiner 	*pfn = pte_pfn(*ptep);
39873b6748e2SJohannes Weiner 	pte_unmap_unlock(ptep, ptl);
39883b6748e2SJohannes Weiner 	return 0;
39893b6748e2SJohannes Weiner }
39903b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn);
39913b6748e2SJohannes Weiner 
399228b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
3993d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma,
399428b2ee20SRik van Riel 		unsigned long address, unsigned int flags,
3995d87fe660Svenkatesh.pallipadi@intel.com 		unsigned long *prot, resource_size_t *phys)
399628b2ee20SRik van Riel {
399703668a4dSJohannes Weiner 	int ret = -EINVAL;
399828b2ee20SRik van Riel 	pte_t *ptep, pte;
399928b2ee20SRik van Riel 	spinlock_t *ptl;
400028b2ee20SRik van Riel 
4001d87fe660Svenkatesh.pallipadi@intel.com 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4002d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
400328b2ee20SRik van Riel 
400403668a4dSJohannes Weiner 	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
4005d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
400628b2ee20SRik van Riel 	pte = *ptep;
400703668a4dSJohannes Weiner 
400828b2ee20SRik van Riel 	if ((flags & FOLL_WRITE) && !pte_write(pte))
400928b2ee20SRik van Riel 		goto unlock;
401028b2ee20SRik van Riel 
401128b2ee20SRik van Riel 	*prot = pgprot_val(pte_pgprot(pte));
401203668a4dSJohannes Weiner 	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
401328b2ee20SRik van Riel 
401403668a4dSJohannes Weiner 	ret = 0;
401528b2ee20SRik van Riel unlock:
401628b2ee20SRik van Riel 	pte_unmap_unlock(ptep, ptl);
401728b2ee20SRik van Riel out:
4018d87fe660Svenkatesh.pallipadi@intel.com 	return ret;
401928b2ee20SRik van Riel }
402028b2ee20SRik van Riel 
402128b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
402228b2ee20SRik van Riel 			void *buf, int len, int write)
402328b2ee20SRik van Riel {
402428b2ee20SRik van Riel 	resource_size_t phys_addr;
402528b2ee20SRik van Riel 	unsigned long prot = 0;
40262bc7273bSKOSAKI Motohiro 	void __iomem *maddr;
402728b2ee20SRik van Riel 	int offset = addr & (PAGE_SIZE-1);
402828b2ee20SRik van Riel 
4029d87fe660Svenkatesh.pallipadi@intel.com 	if (follow_phys(vma, addr, write, &prot, &phys_addr))
403028b2ee20SRik van Riel 		return -EINVAL;
403128b2ee20SRik van Riel 
403228b2ee20SRik van Riel 	maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot);
403328b2ee20SRik van Riel 	if (write)
403428b2ee20SRik van Riel 		memcpy_toio(maddr + offset, buf, len);
403528b2ee20SRik van Riel 	else
403628b2ee20SRik van Riel 		memcpy_fromio(buf, maddr + offset, len);
403728b2ee20SRik van Riel 	iounmap(maddr);
403828b2ee20SRik van Riel 
403928b2ee20SRik van Riel 	return len;
404028b2ee20SRik van Riel }
40415a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys);
404228b2ee20SRik van Riel #endif
404328b2ee20SRik van Riel 
40440ec76a11SDavid Howells /*
4045206cb636SStephen Wilson  * Access another process' address space as given in mm.  If non-NULL, use the
4046206cb636SStephen Wilson  * given task for page fault accounting.
40470ec76a11SDavid Howells  */
4048206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
4049206cb636SStephen Wilson 		unsigned long addr, void *buf, int len, int write)
40500ec76a11SDavid Howells {
40510ec76a11SDavid Howells 	struct vm_area_struct *vma;
40520ec76a11SDavid Howells 	void *old_buf = buf;
40530ec76a11SDavid Howells 
40540ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
4055183ff22bSSimon Arlott 	/* ignore errors, just check how much was successfully transferred */
40560ec76a11SDavid Howells 	while (len) {
40570ec76a11SDavid Howells 		int bytes, ret, offset;
40580ec76a11SDavid Howells 		void *maddr;
405928b2ee20SRik van Riel 		struct page *page = NULL;
40600ec76a11SDavid Howells 
40610ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
40620ec76a11SDavid Howells 				write, 1, &page, &vma);
406328b2ee20SRik van Riel 		if (ret <= 0) {
406428b2ee20SRik van Riel 			/*
406528b2ee20SRik van Riel 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
406628b2ee20SRik van Riel 			 * we can access using slightly different code.
406728b2ee20SRik van Riel 			 */
406828b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
406928b2ee20SRik van Riel 			vma = find_vma(mm, addr);
4070fe936dfcSMichael Ellerman 			if (!vma || vma->vm_start > addr)
40710ec76a11SDavid Howells 				break;
407228b2ee20SRik van Riel 			if (vma->vm_ops && vma->vm_ops->access)
407328b2ee20SRik van Riel 				ret = vma->vm_ops->access(vma, addr, buf,
407428b2ee20SRik van Riel 							  len, write);
407528b2ee20SRik van Riel 			if (ret <= 0)
407628b2ee20SRik van Riel #endif
407728b2ee20SRik van Riel 				break;
407828b2ee20SRik van Riel 			bytes = ret;
407928b2ee20SRik van Riel 		} else {
40800ec76a11SDavid Howells 			bytes = len;
40810ec76a11SDavid Howells 			offset = addr & (PAGE_SIZE-1);
40820ec76a11SDavid Howells 			if (bytes > PAGE_SIZE-offset)
40830ec76a11SDavid Howells 				bytes = PAGE_SIZE-offset;
40840ec76a11SDavid Howells 
40850ec76a11SDavid Howells 			maddr = kmap(page);
40860ec76a11SDavid Howells 			if (write) {
40870ec76a11SDavid Howells 				copy_to_user_page(vma, page, addr,
40880ec76a11SDavid Howells 						  maddr + offset, buf, bytes);
40890ec76a11SDavid Howells 				set_page_dirty_lock(page);
40900ec76a11SDavid Howells 			} else {
40910ec76a11SDavid Howells 				copy_from_user_page(vma, page, addr,
40920ec76a11SDavid Howells 						    buf, maddr + offset, bytes);
40930ec76a11SDavid Howells 			}
40940ec76a11SDavid Howells 			kunmap(page);
40950ec76a11SDavid Howells 			page_cache_release(page);
409628b2ee20SRik van Riel 		}
40970ec76a11SDavid Howells 		len -= bytes;
40980ec76a11SDavid Howells 		buf += bytes;
40990ec76a11SDavid Howells 		addr += bytes;
41000ec76a11SDavid Howells 	}
41010ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
41020ec76a11SDavid Howells 
41030ec76a11SDavid Howells 	return buf - old_buf;
41040ec76a11SDavid Howells }
410503252919SAndi Kleen 
41065ddd36b9SStephen Wilson /**
4107ae91dbfcSRandy Dunlap  * access_remote_vm - access another process' address space
41085ddd36b9SStephen Wilson  * @mm:		the mm_struct of the target address space
41095ddd36b9SStephen Wilson  * @addr:	start address to access
41105ddd36b9SStephen Wilson  * @buf:	source or destination buffer
41115ddd36b9SStephen Wilson  * @len:	number of bytes to transfer
41125ddd36b9SStephen Wilson  * @write:	whether the access is a write
41135ddd36b9SStephen Wilson  *
41145ddd36b9SStephen Wilson  * The caller must hold a reference on @mm.
41155ddd36b9SStephen Wilson  */
41165ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr,
41175ddd36b9SStephen Wilson 		void *buf, int len, int write)
41185ddd36b9SStephen Wilson {
41195ddd36b9SStephen Wilson 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
41205ddd36b9SStephen Wilson }
41215ddd36b9SStephen Wilson 
412203252919SAndi Kleen /*
4123206cb636SStephen Wilson  * Access another process' address space.
4124206cb636SStephen Wilson  * Source/target buffer must be kernel space,
4125206cb636SStephen Wilson  * Do not walk the page table directly, use get_user_pages
4126206cb636SStephen Wilson  */
4127206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr,
4128206cb636SStephen Wilson 		void *buf, int len, int write)
4129206cb636SStephen Wilson {
4130206cb636SStephen Wilson 	struct mm_struct *mm;
4131206cb636SStephen Wilson 	int ret;
4132206cb636SStephen Wilson 
4133206cb636SStephen Wilson 	mm = get_task_mm(tsk);
4134206cb636SStephen Wilson 	if (!mm)
4135206cb636SStephen Wilson 		return 0;
4136206cb636SStephen Wilson 
4137206cb636SStephen Wilson 	ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
4138206cb636SStephen Wilson 	mmput(mm);
4139206cb636SStephen Wilson 
4140206cb636SStephen Wilson 	return ret;
4141206cb636SStephen Wilson }
4142206cb636SStephen Wilson 
414303252919SAndi Kleen /*
414403252919SAndi Kleen  * Print the name of a VMA.
414503252919SAndi Kleen  */
414603252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip)
414703252919SAndi Kleen {
414803252919SAndi Kleen 	struct mm_struct *mm = current->mm;
414903252919SAndi Kleen 	struct vm_area_struct *vma;
415003252919SAndi Kleen 
4151e8bff74aSIngo Molnar 	/*
4152e8bff74aSIngo Molnar 	 * Do not print if we are in atomic
4153e8bff74aSIngo Molnar 	 * contexts (in exception stacks, etc.):
4154e8bff74aSIngo Molnar 	 */
4155e8bff74aSIngo Molnar 	if (preempt_count())
4156e8bff74aSIngo Molnar 		return;
4157e8bff74aSIngo Molnar 
415803252919SAndi Kleen 	down_read(&mm->mmap_sem);
415903252919SAndi Kleen 	vma = find_vma(mm, ip);
416003252919SAndi Kleen 	if (vma && vma->vm_file) {
416103252919SAndi Kleen 		struct file *f = vma->vm_file;
416203252919SAndi Kleen 		char *buf = (char *)__get_free_page(GFP_KERNEL);
416303252919SAndi Kleen 		if (buf) {
41642fbc57c5SAndy Shevchenko 			char *p;
416503252919SAndi Kleen 
4166cf28b486SJan Blunck 			p = d_path(&f->f_path, buf, PAGE_SIZE);
416703252919SAndi Kleen 			if (IS_ERR(p))
416803252919SAndi Kleen 				p = "?";
41692fbc57c5SAndy Shevchenko 			printk("%s%s[%lx+%lx]", prefix, kbasename(p),
417003252919SAndi Kleen 					vma->vm_start,
417103252919SAndi Kleen 					vma->vm_end - vma->vm_start);
417203252919SAndi Kleen 			free_page((unsigned long)buf);
417303252919SAndi Kleen 		}
417403252919SAndi Kleen 	}
417551a07e50SJeff Liu 	up_read(&mm->mmap_sem);
417603252919SAndi Kleen }
41773ee1afa3SNick Piggin 
4178662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
41793ee1afa3SNick Piggin void might_fault(void)
41803ee1afa3SNick Piggin {
418195156f00SPeter Zijlstra 	/*
418295156f00SPeter Zijlstra 	 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
418395156f00SPeter Zijlstra 	 * holding the mmap_sem, this is safe because kernel memory doesn't
418495156f00SPeter Zijlstra 	 * get paged out, therefore we'll never actually fault, and the
418595156f00SPeter Zijlstra 	 * below annotations will generate false positives.
418695156f00SPeter Zijlstra 	 */
418795156f00SPeter Zijlstra 	if (segment_eq(get_fs(), KERNEL_DS))
418895156f00SPeter Zijlstra 		return;
418995156f00SPeter Zijlstra 
41903ee1afa3SNick Piggin 	/*
41913ee1afa3SNick Piggin 	 * it would be nicer only to annotate paths which are not under
41923ee1afa3SNick Piggin 	 * pagefault_disable, however that requires a larger audit and
41933ee1afa3SNick Piggin 	 * providing helpers like get_user_atomic.
41943ee1afa3SNick Piggin 	 */
4195662bbcb2SMichael S. Tsirkin 	if (in_atomic())
4196662bbcb2SMichael S. Tsirkin 		return;
4197662bbcb2SMichael S. Tsirkin 
4198662bbcb2SMichael S. Tsirkin 	__might_sleep(__FILE__, __LINE__, 0);
4199662bbcb2SMichael S. Tsirkin 
4200662bbcb2SMichael S. Tsirkin 	if (current->mm)
42013ee1afa3SNick Piggin 		might_lock_read(&current->mm->mmap_sem);
42023ee1afa3SNick Piggin }
42033ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault);
42043ee1afa3SNick Piggin #endif
420547ad8475SAndrea Arcangeli 
420647ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
420747ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page,
420847ad8475SAndrea Arcangeli 				unsigned long addr,
420947ad8475SAndrea Arcangeli 				unsigned int pages_per_huge_page)
421047ad8475SAndrea Arcangeli {
421147ad8475SAndrea Arcangeli 	int i;
421247ad8475SAndrea Arcangeli 	struct page *p = page;
421347ad8475SAndrea Arcangeli 
421447ad8475SAndrea Arcangeli 	might_sleep();
421547ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page;
421647ad8475SAndrea Arcangeli 	     i++, p = mem_map_next(p, page, i)) {
421747ad8475SAndrea Arcangeli 		cond_resched();
421847ad8475SAndrea Arcangeli 		clear_user_highpage(p, addr + i * PAGE_SIZE);
421947ad8475SAndrea Arcangeli 	}
422047ad8475SAndrea Arcangeli }
422147ad8475SAndrea Arcangeli void clear_huge_page(struct page *page,
422247ad8475SAndrea Arcangeli 		     unsigned long addr, unsigned int pages_per_huge_page)
422347ad8475SAndrea Arcangeli {
422447ad8475SAndrea Arcangeli 	int i;
422547ad8475SAndrea Arcangeli 
422647ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
422747ad8475SAndrea Arcangeli 		clear_gigantic_page(page, addr, pages_per_huge_page);
422847ad8475SAndrea Arcangeli 		return;
422947ad8475SAndrea Arcangeli 	}
423047ad8475SAndrea Arcangeli 
423147ad8475SAndrea Arcangeli 	might_sleep();
423247ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
423347ad8475SAndrea Arcangeli 		cond_resched();
423447ad8475SAndrea Arcangeli 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
423547ad8475SAndrea Arcangeli 	}
423647ad8475SAndrea Arcangeli }
423747ad8475SAndrea Arcangeli 
423847ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src,
423947ad8475SAndrea Arcangeli 				    unsigned long addr,
424047ad8475SAndrea Arcangeli 				    struct vm_area_struct *vma,
424147ad8475SAndrea Arcangeli 				    unsigned int pages_per_huge_page)
424247ad8475SAndrea Arcangeli {
424347ad8475SAndrea Arcangeli 	int i;
424447ad8475SAndrea Arcangeli 	struct page *dst_base = dst;
424547ad8475SAndrea Arcangeli 	struct page *src_base = src;
424647ad8475SAndrea Arcangeli 
424747ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; ) {
424847ad8475SAndrea Arcangeli 		cond_resched();
424947ad8475SAndrea Arcangeli 		copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
425047ad8475SAndrea Arcangeli 
425147ad8475SAndrea Arcangeli 		i++;
425247ad8475SAndrea Arcangeli 		dst = mem_map_next(dst, dst_base, i);
425347ad8475SAndrea Arcangeli 		src = mem_map_next(src, src_base, i);
425447ad8475SAndrea Arcangeli 	}
425547ad8475SAndrea Arcangeli }
425647ad8475SAndrea Arcangeli 
425747ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src,
425847ad8475SAndrea Arcangeli 			 unsigned long addr, struct vm_area_struct *vma,
425947ad8475SAndrea Arcangeli 			 unsigned int pages_per_huge_page)
426047ad8475SAndrea Arcangeli {
426147ad8475SAndrea Arcangeli 	int i;
426247ad8475SAndrea Arcangeli 
426347ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
426447ad8475SAndrea Arcangeli 		copy_user_gigantic_page(dst, src, addr, vma,
426547ad8475SAndrea Arcangeli 					pages_per_huge_page);
426647ad8475SAndrea Arcangeli 		return;
426747ad8475SAndrea Arcangeli 	}
426847ad8475SAndrea Arcangeli 
426947ad8475SAndrea Arcangeli 	might_sleep();
427047ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
427147ad8475SAndrea Arcangeli 		cond_resched();
427247ad8475SAndrea Arcangeli 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
427347ad8475SAndrea Arcangeli 	}
427447ad8475SAndrea Arcangeli }
427547ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
427649076ec2SKirill A. Shutemov 
427740b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
4278b35f1819SKirill A. Shutemov 
4279b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep;
4280b35f1819SKirill A. Shutemov 
4281b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void)
4282b35f1819SKirill A. Shutemov {
4283b35f1819SKirill A. Shutemov 	page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
4284b35f1819SKirill A. Shutemov 			SLAB_PANIC, NULL);
4285b35f1819SKirill A. Shutemov }
4286b35f1819SKirill A. Shutemov 
4287539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page)
428849076ec2SKirill A. Shutemov {
428949076ec2SKirill A. Shutemov 	spinlock_t *ptl;
429049076ec2SKirill A. Shutemov 
4291b35f1819SKirill A. Shutemov 	ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
429249076ec2SKirill A. Shutemov 	if (!ptl)
429349076ec2SKirill A. Shutemov 		return false;
4294539edb58SPeter Zijlstra 	page->ptl = ptl;
429549076ec2SKirill A. Shutemov 	return true;
429649076ec2SKirill A. Shutemov }
429749076ec2SKirill A. Shutemov 
4298539edb58SPeter Zijlstra void ptlock_free(struct page *page)
429949076ec2SKirill A. Shutemov {
4300b35f1819SKirill A. Shutemov 	kmem_cache_free(page_ptl_cachep, page->ptl);
430149076ec2SKirill A. Shutemov }
430249076ec2SKirill A. Shutemov #endif
4303