xref: /linux/mm/memory.c (revision 52f37629fd3c7b24e1e6c125e665454cd7ac1acb)
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>
621da177e4SLinus Torvalds 
636952b61dSAlexey Dobriyan #include <asm/io.h>
641da177e4SLinus Torvalds #include <asm/pgalloc.h>
651da177e4SLinus Torvalds #include <asm/uaccess.h>
661da177e4SLinus Torvalds #include <asm/tlb.h>
671da177e4SLinus Torvalds #include <asm/tlbflush.h>
681da177e4SLinus Torvalds #include <asm/pgtable.h>
691da177e4SLinus Torvalds 
7042b77728SJan Beulich #include "internal.h"
7142b77728SJan Beulich 
7275980e97SPeter Zijlstra #ifdef LAST_NID_NOT_IN_PAGE_FLAGS
7375980e97SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_nid.
7475980e97SPeter Zijlstra #endif
7575980e97SPeter Zijlstra 
76d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES
771da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */
781da177e4SLinus Torvalds unsigned long max_mapnr;
791da177e4SLinus Torvalds struct page *mem_map;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr);
821da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
831da177e4SLinus Torvalds #endif
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds unsigned long num_physpages;
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(num_physpages);
961da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory);
971da177e4SLinus Torvalds 
9832a93233SIngo Molnar /*
9932a93233SIngo Molnar  * Randomize the address space (stacks, mmaps, brk, etc.).
10032a93233SIngo Molnar  *
10132a93233SIngo Molnar  * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
10232a93233SIngo Molnar  *   as ancient (libc5 based) binaries can segfault. )
10332a93233SIngo Molnar  */
10432a93233SIngo Molnar int randomize_va_space __read_mostly =
10532a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK
10632a93233SIngo Molnar 					1;
10732a93233SIngo Molnar #else
10832a93233SIngo Molnar 					2;
10932a93233SIngo Molnar #endif
110a62eaf15SAndi Kleen 
111a62eaf15SAndi Kleen static int __init disable_randmaps(char *s)
112a62eaf15SAndi Kleen {
113a62eaf15SAndi Kleen 	randomize_va_space = 0;
1149b41046cSOGAWA Hirofumi 	return 1;
115a62eaf15SAndi Kleen }
116a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps);
117a62eaf15SAndi Kleen 
11862eede62SHugh Dickins unsigned long zero_pfn __read_mostly;
11903f6462aSHugh Dickins unsigned long highest_memmap_pfn __read_mostly;
120a13ea5b7SHugh Dickins 
121a13ea5b7SHugh Dickins /*
122a13ea5b7SHugh Dickins  * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
123a13ea5b7SHugh Dickins  */
124a13ea5b7SHugh Dickins static int __init init_zero_pfn(void)
125a13ea5b7SHugh Dickins {
126a13ea5b7SHugh Dickins 	zero_pfn = page_to_pfn(ZERO_PAGE(0));
127a13ea5b7SHugh Dickins 	return 0;
128a13ea5b7SHugh Dickins }
129a13ea5b7SHugh Dickins core_initcall(init_zero_pfn);
130a62eaf15SAndi Kleen 
131d559db08SKAMEZAWA Hiroyuki 
13234e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
13334e55232SKAMEZAWA Hiroyuki 
134ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm)
13534e55232SKAMEZAWA Hiroyuki {
13634e55232SKAMEZAWA Hiroyuki 	int i;
13734e55232SKAMEZAWA Hiroyuki 
13834e55232SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++) {
13905af2e10SDavid Rientjes 		if (current->rss_stat.count[i]) {
14005af2e10SDavid Rientjes 			add_mm_counter(mm, i, current->rss_stat.count[i]);
14105af2e10SDavid Rientjes 			current->rss_stat.count[i] = 0;
14234e55232SKAMEZAWA Hiroyuki 		}
14334e55232SKAMEZAWA Hiroyuki 	}
14405af2e10SDavid Rientjes 	current->rss_stat.events = 0;
14534e55232SKAMEZAWA Hiroyuki }
14634e55232SKAMEZAWA Hiroyuki 
14734e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
14834e55232SKAMEZAWA Hiroyuki {
14934e55232SKAMEZAWA Hiroyuki 	struct task_struct *task = current;
15034e55232SKAMEZAWA Hiroyuki 
15134e55232SKAMEZAWA Hiroyuki 	if (likely(task->mm == mm))
15234e55232SKAMEZAWA Hiroyuki 		task->rss_stat.count[member] += val;
15334e55232SKAMEZAWA Hiroyuki 	else
15434e55232SKAMEZAWA Hiroyuki 		add_mm_counter(mm, member, val);
15534e55232SKAMEZAWA Hiroyuki }
15634e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
15734e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
15834e55232SKAMEZAWA Hiroyuki 
15934e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */
16034e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH	(64)
16134e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
16234e55232SKAMEZAWA Hiroyuki {
16334e55232SKAMEZAWA Hiroyuki 	if (unlikely(task != current))
16434e55232SKAMEZAWA Hiroyuki 		return;
16534e55232SKAMEZAWA Hiroyuki 	if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
166ea48cf78SDavid Rientjes 		sync_mm_rss(task->mm);
16734e55232SKAMEZAWA Hiroyuki }
1689547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */
16934e55232SKAMEZAWA Hiroyuki 
17034e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
17134e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
17234e55232SKAMEZAWA Hiroyuki 
17334e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
17434e55232SKAMEZAWA Hiroyuki {
17534e55232SKAMEZAWA Hiroyuki }
17634e55232SKAMEZAWA Hiroyuki 
1779547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */
1789547d01bSPeter Zijlstra 
1799547d01bSPeter Zijlstra #ifdef HAVE_GENERIC_MMU_GATHER
1809547d01bSPeter Zijlstra 
1819547d01bSPeter Zijlstra static int tlb_next_batch(struct mmu_gather *tlb)
1829547d01bSPeter Zijlstra {
1839547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
1849547d01bSPeter Zijlstra 
1859547d01bSPeter Zijlstra 	batch = tlb->active;
1869547d01bSPeter Zijlstra 	if (batch->next) {
1879547d01bSPeter Zijlstra 		tlb->active = batch->next;
1889547d01bSPeter Zijlstra 		return 1;
1899547d01bSPeter Zijlstra 	}
1909547d01bSPeter Zijlstra 
19153a59fc6SMichal Hocko 	if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
19253a59fc6SMichal Hocko 		return 0;
19353a59fc6SMichal Hocko 
1949547d01bSPeter Zijlstra 	batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
1959547d01bSPeter Zijlstra 	if (!batch)
1969547d01bSPeter Zijlstra 		return 0;
1979547d01bSPeter Zijlstra 
19853a59fc6SMichal Hocko 	tlb->batch_count++;
1999547d01bSPeter Zijlstra 	batch->next = NULL;
2009547d01bSPeter Zijlstra 	batch->nr   = 0;
2019547d01bSPeter Zijlstra 	batch->max  = MAX_GATHER_BATCH;
2029547d01bSPeter Zijlstra 
2039547d01bSPeter Zijlstra 	tlb->active->next = batch;
2049547d01bSPeter Zijlstra 	tlb->active = batch;
2059547d01bSPeter Zijlstra 
2069547d01bSPeter Zijlstra 	return 1;
2079547d01bSPeter Zijlstra }
2089547d01bSPeter Zijlstra 
2099547d01bSPeter Zijlstra /* tlb_gather_mmu
2109547d01bSPeter Zijlstra  *	Called to initialize an (on-stack) mmu_gather structure for page-table
2119547d01bSPeter Zijlstra  *	tear-down from @mm. The @fullmm argument is used when @mm is without
2129547d01bSPeter Zijlstra  *	users and we're going to destroy the full address space (exit/execve).
2139547d01bSPeter Zijlstra  */
2149547d01bSPeter Zijlstra void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm)
2159547d01bSPeter Zijlstra {
2169547d01bSPeter Zijlstra 	tlb->mm = mm;
2179547d01bSPeter Zijlstra 
2189547d01bSPeter Zijlstra 	tlb->fullmm     = fullmm;
2191de14c3cSDave Hansen 	tlb->need_flush_all = 0;
220597e1c35SAlex Shi 	tlb->start	= -1UL;
221597e1c35SAlex Shi 	tlb->end	= 0;
2229547d01bSPeter Zijlstra 	tlb->need_flush = 0;
2239547d01bSPeter Zijlstra 	tlb->fast_mode  = (num_possible_cpus() == 1);
2249547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2259547d01bSPeter Zijlstra 	tlb->local.nr   = 0;
2269547d01bSPeter Zijlstra 	tlb->local.max  = ARRAY_SIZE(tlb->__pages);
2279547d01bSPeter Zijlstra 	tlb->active     = &tlb->local;
22853a59fc6SMichal Hocko 	tlb->batch_count = 0;
2299547d01bSPeter Zijlstra 
2309547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2319547d01bSPeter Zijlstra 	tlb->batch = NULL;
23234e55232SKAMEZAWA Hiroyuki #endif
2339547d01bSPeter Zijlstra }
2349547d01bSPeter Zijlstra 
2359547d01bSPeter Zijlstra void tlb_flush_mmu(struct mmu_gather *tlb)
2369547d01bSPeter Zijlstra {
2379547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
2389547d01bSPeter Zijlstra 
2399547d01bSPeter Zijlstra 	if (!tlb->need_flush)
2409547d01bSPeter Zijlstra 		return;
2419547d01bSPeter Zijlstra 	tlb->need_flush = 0;
2429547d01bSPeter Zijlstra 	tlb_flush(tlb);
2439547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2449547d01bSPeter Zijlstra 	tlb_table_flush(tlb);
2459547d01bSPeter Zijlstra #endif
2469547d01bSPeter Zijlstra 
2479547d01bSPeter Zijlstra 	if (tlb_fast_mode(tlb))
2489547d01bSPeter Zijlstra 		return;
2499547d01bSPeter Zijlstra 
2509547d01bSPeter Zijlstra 	for (batch = &tlb->local; batch; batch = batch->next) {
2519547d01bSPeter Zijlstra 		free_pages_and_swap_cache(batch->pages, batch->nr);
2529547d01bSPeter Zijlstra 		batch->nr = 0;
2539547d01bSPeter Zijlstra 	}
2549547d01bSPeter Zijlstra 	tlb->active = &tlb->local;
2559547d01bSPeter Zijlstra }
2569547d01bSPeter Zijlstra 
2579547d01bSPeter Zijlstra /* tlb_finish_mmu
2589547d01bSPeter Zijlstra  *	Called at the end of the shootdown operation to free up any resources
2599547d01bSPeter Zijlstra  *	that were required.
2609547d01bSPeter Zijlstra  */
2619547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
2629547d01bSPeter Zijlstra {
2639547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch, *next;
2649547d01bSPeter Zijlstra 
265597e1c35SAlex Shi 	tlb->start = start;
266597e1c35SAlex Shi 	tlb->end   = end;
2679547d01bSPeter Zijlstra 	tlb_flush_mmu(tlb);
2689547d01bSPeter Zijlstra 
2699547d01bSPeter Zijlstra 	/* keep the page table cache within bounds */
2709547d01bSPeter Zijlstra 	check_pgt_cache();
2719547d01bSPeter Zijlstra 
2729547d01bSPeter Zijlstra 	for (batch = tlb->local.next; batch; batch = next) {
2739547d01bSPeter Zijlstra 		next = batch->next;
2749547d01bSPeter Zijlstra 		free_pages((unsigned long)batch, 0);
2759547d01bSPeter Zijlstra 	}
2769547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2779547d01bSPeter Zijlstra }
2789547d01bSPeter Zijlstra 
2799547d01bSPeter Zijlstra /* __tlb_remove_page
2809547d01bSPeter Zijlstra  *	Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
2819547d01bSPeter Zijlstra  *	handling the additional races in SMP caused by other CPUs caching valid
2829547d01bSPeter Zijlstra  *	mappings in their TLBs. Returns the number of free page slots left.
2839547d01bSPeter Zijlstra  *	When out of page slots we must call tlb_flush_mmu().
2849547d01bSPeter Zijlstra  */
2859547d01bSPeter Zijlstra int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
2869547d01bSPeter Zijlstra {
2879547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
2889547d01bSPeter Zijlstra 
289f21760b1SShaohua Li 	VM_BUG_ON(!tlb->need_flush);
2909547d01bSPeter Zijlstra 
2919547d01bSPeter Zijlstra 	if (tlb_fast_mode(tlb)) {
2929547d01bSPeter Zijlstra 		free_page_and_swap_cache(page);
2939547d01bSPeter Zijlstra 		return 1; /* avoid calling tlb_flush_mmu() */
2949547d01bSPeter Zijlstra 	}
2959547d01bSPeter Zijlstra 
2969547d01bSPeter Zijlstra 	batch = tlb->active;
2979547d01bSPeter Zijlstra 	batch->pages[batch->nr++] = page;
2989547d01bSPeter Zijlstra 	if (batch->nr == batch->max) {
2999547d01bSPeter Zijlstra 		if (!tlb_next_batch(tlb))
3009547d01bSPeter Zijlstra 			return 0;
3010b43c3aaSShaohua Li 		batch = tlb->active;
3029547d01bSPeter Zijlstra 	}
3039547d01bSPeter Zijlstra 	VM_BUG_ON(batch->nr > batch->max);
3049547d01bSPeter Zijlstra 
3059547d01bSPeter Zijlstra 	return batch->max - batch->nr;
3069547d01bSPeter Zijlstra }
3079547d01bSPeter Zijlstra 
3089547d01bSPeter Zijlstra #endif /* HAVE_GENERIC_MMU_GATHER */
30934e55232SKAMEZAWA Hiroyuki 
31026723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
31126723911SPeter Zijlstra 
31226723911SPeter Zijlstra /*
31326723911SPeter Zijlstra  * See the comment near struct mmu_table_batch.
31426723911SPeter Zijlstra  */
31526723911SPeter Zijlstra 
31626723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg)
31726723911SPeter Zijlstra {
31826723911SPeter Zijlstra 	/* Simply deliver the interrupt */
31926723911SPeter Zijlstra }
32026723911SPeter Zijlstra 
32126723911SPeter Zijlstra static void tlb_remove_table_one(void *table)
32226723911SPeter Zijlstra {
32326723911SPeter Zijlstra 	/*
32426723911SPeter Zijlstra 	 * This isn't an RCU grace period and hence the page-tables cannot be
32526723911SPeter Zijlstra 	 * assumed to be actually RCU-freed.
32626723911SPeter Zijlstra 	 *
32726723911SPeter Zijlstra 	 * It is however sufficient for software page-table walkers that rely on
32826723911SPeter Zijlstra 	 * IRQ disabling. See the comment near struct mmu_table_batch.
32926723911SPeter Zijlstra 	 */
33026723911SPeter Zijlstra 	smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
33126723911SPeter Zijlstra 	__tlb_remove_table(table);
33226723911SPeter Zijlstra }
33326723911SPeter Zijlstra 
33426723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head)
33526723911SPeter Zijlstra {
33626723911SPeter Zijlstra 	struct mmu_table_batch *batch;
33726723911SPeter Zijlstra 	int i;
33826723911SPeter Zijlstra 
33926723911SPeter Zijlstra 	batch = container_of(head, struct mmu_table_batch, rcu);
34026723911SPeter Zijlstra 
34126723911SPeter Zijlstra 	for (i = 0; i < batch->nr; i++)
34226723911SPeter Zijlstra 		__tlb_remove_table(batch->tables[i]);
34326723911SPeter Zijlstra 
34426723911SPeter Zijlstra 	free_page((unsigned long)batch);
34526723911SPeter Zijlstra }
34626723911SPeter Zijlstra 
34726723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb)
34826723911SPeter Zijlstra {
34926723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
35026723911SPeter Zijlstra 
35126723911SPeter Zijlstra 	if (*batch) {
35226723911SPeter Zijlstra 		call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
35326723911SPeter Zijlstra 		*batch = NULL;
35426723911SPeter Zijlstra 	}
35526723911SPeter Zijlstra }
35626723911SPeter Zijlstra 
35726723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table)
35826723911SPeter Zijlstra {
35926723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
36026723911SPeter Zijlstra 
36126723911SPeter Zijlstra 	tlb->need_flush = 1;
36226723911SPeter Zijlstra 
36326723911SPeter Zijlstra 	/*
36426723911SPeter Zijlstra 	 * When there's less then two users of this mm there cannot be a
36526723911SPeter Zijlstra 	 * concurrent page-table walk.
36626723911SPeter Zijlstra 	 */
36726723911SPeter Zijlstra 	if (atomic_read(&tlb->mm->mm_users) < 2) {
36826723911SPeter Zijlstra 		__tlb_remove_table(table);
36926723911SPeter Zijlstra 		return;
37026723911SPeter Zijlstra 	}
37126723911SPeter Zijlstra 
37226723911SPeter Zijlstra 	if (*batch == NULL) {
37326723911SPeter Zijlstra 		*batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
37426723911SPeter Zijlstra 		if (*batch == NULL) {
37526723911SPeter Zijlstra 			tlb_remove_table_one(table);
37626723911SPeter Zijlstra 			return;
37726723911SPeter Zijlstra 		}
37826723911SPeter Zijlstra 		(*batch)->nr = 0;
37926723911SPeter Zijlstra 	}
38026723911SPeter Zijlstra 	(*batch)->tables[(*batch)->nr++] = table;
38126723911SPeter Zijlstra 	if ((*batch)->nr == MAX_TABLE_BATCH)
38226723911SPeter Zijlstra 		tlb_table_flush(tlb);
38326723911SPeter Zijlstra }
38426723911SPeter Zijlstra 
3859547d01bSPeter Zijlstra #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
38626723911SPeter Zijlstra 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  * If a p?d_bad entry is found while walking page tables, report
3891da177e4SLinus Torvalds  * the error, before resetting entry to p?d_none.  Usually (but
3901da177e4SLinus Torvalds  * very seldom) called out from the p?d_none_or_clear_bad macros.
3911da177e4SLinus Torvalds  */
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	pgd_ERROR(*pgd);
3961da177e4SLinus Torvalds 	pgd_clear(pgd);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	pud_ERROR(*pud);
4021da177e4SLinus Torvalds 	pud_clear(pud);
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	pmd_ERROR(*pmd);
4081da177e4SLinus Torvalds 	pmd_clear(pmd);
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds /*
4121da177e4SLinus Torvalds  * Note: this doesn't free the actual pages themselves. That
4131da177e4SLinus Torvalds  * has been handled earlier when unmapping all the memory regions.
4141da177e4SLinus Torvalds  */
4159e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
4169e1b32caSBenjamin Herrenschmidt 			   unsigned long addr)
4171da177e4SLinus Torvalds {
4182f569afdSMartin Schwidefsky 	pgtable_t token = pmd_pgtable(*pmd);
4191da177e4SLinus Torvalds 	pmd_clear(pmd);
4209e1b32caSBenjamin Herrenschmidt 	pte_free_tlb(tlb, token, addr);
4211da177e4SLinus Torvalds 	tlb->mm->nr_ptes--;
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
424e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
425e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
426e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	pmd_t *pmd;
4291da177e4SLinus Torvalds 	unsigned long next;
430e0da382cSHugh Dickins 	unsigned long start;
4311da177e4SLinus Torvalds 
432e0da382cSHugh Dickins 	start = addr;
4331da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
4341da177e4SLinus Torvalds 	do {
4351da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
4361da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
4371da177e4SLinus Torvalds 			continue;
4389e1b32caSBenjamin Herrenschmidt 		free_pte_range(tlb, pmd, addr);
4391da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
4401da177e4SLinus Torvalds 
441e0da382cSHugh Dickins 	start &= PUD_MASK;
442e0da382cSHugh Dickins 	if (start < floor)
443e0da382cSHugh Dickins 		return;
444e0da382cSHugh Dickins 	if (ceiling) {
445e0da382cSHugh Dickins 		ceiling &= PUD_MASK;
446e0da382cSHugh Dickins 		if (!ceiling)
447e0da382cSHugh Dickins 			return;
4481da177e4SLinus Torvalds 	}
449e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
450e0da382cSHugh Dickins 		return;
451e0da382cSHugh Dickins 
452e0da382cSHugh Dickins 	pmd = pmd_offset(pud, start);
453e0da382cSHugh Dickins 	pud_clear(pud);
4549e1b32caSBenjamin Herrenschmidt 	pmd_free_tlb(tlb, pmd, start);
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
457e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
458e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
459e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	pud_t *pud;
4621da177e4SLinus Torvalds 	unsigned long next;
463e0da382cSHugh Dickins 	unsigned long start;
4641da177e4SLinus Torvalds 
465e0da382cSHugh Dickins 	start = addr;
4661da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
4671da177e4SLinus Torvalds 	do {
4681da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
4691da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
4701da177e4SLinus Torvalds 			continue;
471e0da382cSHugh Dickins 		free_pmd_range(tlb, pud, addr, next, floor, ceiling);
4721da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
4731da177e4SLinus Torvalds 
474e0da382cSHugh Dickins 	start &= PGDIR_MASK;
475e0da382cSHugh Dickins 	if (start < floor)
476e0da382cSHugh Dickins 		return;
477e0da382cSHugh Dickins 	if (ceiling) {
478e0da382cSHugh Dickins 		ceiling &= PGDIR_MASK;
479e0da382cSHugh Dickins 		if (!ceiling)
480e0da382cSHugh Dickins 			return;
4811da177e4SLinus Torvalds 	}
482e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
483e0da382cSHugh Dickins 		return;
484e0da382cSHugh Dickins 
485e0da382cSHugh Dickins 	pud = pud_offset(pgd, start);
486e0da382cSHugh Dickins 	pgd_clear(pgd);
4879e1b32caSBenjamin Herrenschmidt 	pud_free_tlb(tlb, pud, start);
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds /*
491e0da382cSHugh Dickins  * This function frees user-level page tables of a process.
492e0da382cSHugh Dickins  *
4931da177e4SLinus Torvalds  * Must be called with pagetable lock held.
4941da177e4SLinus Torvalds  */
49542b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb,
496e0da382cSHugh Dickins 			unsigned long addr, unsigned long end,
497e0da382cSHugh Dickins 			unsigned long floor, unsigned long ceiling)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	pgd_t *pgd;
5001da177e4SLinus Torvalds 	unsigned long next;
5011da177e4SLinus Torvalds 
502e0da382cSHugh Dickins 	/*
503e0da382cSHugh Dickins 	 * The next few lines have given us lots of grief...
504e0da382cSHugh Dickins 	 *
505e0da382cSHugh Dickins 	 * Why are we testing PMD* at this top level?  Because often
506e0da382cSHugh Dickins 	 * there will be no work to do at all, and we'd prefer not to
507e0da382cSHugh Dickins 	 * go all the way down to the bottom just to discover that.
508e0da382cSHugh Dickins 	 *
509e0da382cSHugh Dickins 	 * Why all these "- 1"s?  Because 0 represents both the bottom
510e0da382cSHugh Dickins 	 * of the address space and the top of it (using -1 for the
511e0da382cSHugh Dickins 	 * top wouldn't help much: the masks would do the wrong thing).
512e0da382cSHugh Dickins 	 * The rule is that addr 0 and floor 0 refer to the bottom of
513e0da382cSHugh Dickins 	 * the address space, but end 0 and ceiling 0 refer to the top
514e0da382cSHugh Dickins 	 * Comparisons need to use "end - 1" and "ceiling - 1" (though
515e0da382cSHugh Dickins 	 * that end 0 case should be mythical).
516e0da382cSHugh Dickins 	 *
517e0da382cSHugh Dickins 	 * Wherever addr is brought up or ceiling brought down, we must
518e0da382cSHugh Dickins 	 * be careful to reject "the opposite 0" before it confuses the
519e0da382cSHugh Dickins 	 * subsequent tests.  But what about where end is brought down
520e0da382cSHugh Dickins 	 * by PMD_SIZE below? no, end can't go down to 0 there.
521e0da382cSHugh Dickins 	 *
522e0da382cSHugh Dickins 	 * Whereas we round start (addr) and ceiling down, by different
523e0da382cSHugh Dickins 	 * masks at different levels, in order to test whether a table
524e0da382cSHugh Dickins 	 * now has no other vmas using it, so can be freed, we don't
525e0da382cSHugh Dickins 	 * bother to round floor or end up - the tests don't need that.
526e0da382cSHugh Dickins 	 */
527e0da382cSHugh Dickins 
528e0da382cSHugh Dickins 	addr &= PMD_MASK;
529e0da382cSHugh Dickins 	if (addr < floor) {
530e0da382cSHugh Dickins 		addr += PMD_SIZE;
531e0da382cSHugh Dickins 		if (!addr)
532e0da382cSHugh Dickins 			return;
533e0da382cSHugh Dickins 	}
534e0da382cSHugh Dickins 	if (ceiling) {
535e0da382cSHugh Dickins 		ceiling &= PMD_MASK;
536e0da382cSHugh Dickins 		if (!ceiling)
537e0da382cSHugh Dickins 			return;
538e0da382cSHugh Dickins 	}
539e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
540e0da382cSHugh Dickins 		end -= PMD_SIZE;
541e0da382cSHugh Dickins 	if (addr > end - 1)
542e0da382cSHugh Dickins 		return;
543e0da382cSHugh Dickins 
54442b77728SJan Beulich 	pgd = pgd_offset(tlb->mm, addr);
5451da177e4SLinus Torvalds 	do {
5461da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
5471da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
5481da177e4SLinus Torvalds 			continue;
54942b77728SJan Beulich 		free_pud_range(tlb, pgd, addr, next, floor, ceiling);
5501da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
551e0da382cSHugh Dickins }
552e0da382cSHugh Dickins 
55342b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
554e0da382cSHugh Dickins 		unsigned long floor, unsigned long ceiling)
555e0da382cSHugh Dickins {
556e0da382cSHugh Dickins 	while (vma) {
557e0da382cSHugh Dickins 		struct vm_area_struct *next = vma->vm_next;
558e0da382cSHugh Dickins 		unsigned long addr = vma->vm_start;
559e0da382cSHugh Dickins 
5608f4f8c16SHugh Dickins 		/*
56125d9e2d1Snpiggin@suse.de 		 * Hide vma from rmap and truncate_pagecache before freeing
56225d9e2d1Snpiggin@suse.de 		 * pgtables
5638f4f8c16SHugh Dickins 		 */
5645beb4930SRik van Riel 		unlink_anon_vmas(vma);
5658f4f8c16SHugh Dickins 		unlink_file_vma(vma);
5668f4f8c16SHugh Dickins 
5679da61aefSDavid Gibson 		if (is_vm_hugetlb_page(vma)) {
5683bf5ee95SHugh Dickins 			hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
5693bf5ee95SHugh Dickins 				floor, next? next->vm_start: ceiling);
5703bf5ee95SHugh Dickins 		} else {
5713bf5ee95SHugh Dickins 			/*
5723bf5ee95SHugh Dickins 			 * Optimization: gather nearby vmas into one call down
5733bf5ee95SHugh Dickins 			 */
5743bf5ee95SHugh Dickins 			while (next && next->vm_start <= vma->vm_end + PMD_SIZE
5754866920bSDavid Gibson 			       && !is_vm_hugetlb_page(next)) {
576e0da382cSHugh Dickins 				vma = next;
577e0da382cSHugh Dickins 				next = vma->vm_next;
5785beb4930SRik van Riel 				unlink_anon_vmas(vma);
5798f4f8c16SHugh Dickins 				unlink_file_vma(vma);
580e0da382cSHugh Dickins 			}
5813bf5ee95SHugh Dickins 			free_pgd_range(tlb, addr, vma->vm_end,
582e0da382cSHugh Dickins 				floor, next? next->vm_start: ceiling);
5833bf5ee95SHugh Dickins 		}
584e0da382cSHugh Dickins 		vma = next;
585e0da382cSHugh Dickins 	}
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5888ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
5898ac1f832SAndrea Arcangeli 		pmd_t *pmd, unsigned long address)
5901da177e4SLinus Torvalds {
5912f569afdSMartin Schwidefsky 	pgtable_t new = pte_alloc_one(mm, address);
5928ac1f832SAndrea Arcangeli 	int wait_split_huge_page;
5931da177e4SLinus Torvalds 	if (!new)
5941bb3630eSHugh Dickins 		return -ENOMEM;
5951bb3630eSHugh Dickins 
596362a61adSNick Piggin 	/*
597362a61adSNick Piggin 	 * Ensure all pte setup (eg. pte page lock and page clearing) are
598362a61adSNick Piggin 	 * visible before the pte is made visible to other CPUs by being
599362a61adSNick Piggin 	 * put into page tables.
600362a61adSNick Piggin 	 *
601362a61adSNick Piggin 	 * The other side of the story is the pointer chasing in the page
602362a61adSNick Piggin 	 * table walking code (when walking the page table without locking;
603362a61adSNick Piggin 	 * ie. most of the time). Fortunately, these data accesses consist
604362a61adSNick Piggin 	 * of a chain of data-dependent loads, meaning most CPUs (alpha
605362a61adSNick Piggin 	 * being the notable exception) will already guarantee loads are
606362a61adSNick Piggin 	 * seen in-order. See the alpha page table accessors for the
607362a61adSNick Piggin 	 * smp_read_barrier_depends() barriers in page table walking code.
608362a61adSNick Piggin 	 */
609362a61adSNick Piggin 	smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
610362a61adSNick Piggin 
611c74df32cSHugh Dickins 	spin_lock(&mm->page_table_lock);
6128ac1f832SAndrea Arcangeli 	wait_split_huge_page = 0;
6138ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
6141da177e4SLinus Torvalds 		mm->nr_ptes++;
6151da177e4SLinus Torvalds 		pmd_populate(mm, pmd, new);
6162f569afdSMartin Schwidefsky 		new = NULL;
6178ac1f832SAndrea Arcangeli 	} else if (unlikely(pmd_trans_splitting(*pmd)))
6188ac1f832SAndrea Arcangeli 		wait_split_huge_page = 1;
619c74df32cSHugh Dickins 	spin_unlock(&mm->page_table_lock);
6202f569afdSMartin Schwidefsky 	if (new)
6212f569afdSMartin Schwidefsky 		pte_free(mm, new);
6228ac1f832SAndrea Arcangeli 	if (wait_split_huge_page)
6238ac1f832SAndrea Arcangeli 		wait_split_huge_page(vma->anon_vma, pmd);
6241bb3630eSHugh Dickins 	return 0;
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
6271bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
6281da177e4SLinus Torvalds {
6291bb3630eSHugh Dickins 	pte_t *new = pte_alloc_one_kernel(&init_mm, address);
6301da177e4SLinus Torvalds 	if (!new)
6311bb3630eSHugh Dickins 		return -ENOMEM;
6321da177e4SLinus Torvalds 
633362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
634362a61adSNick Piggin 
635872fec16SHugh Dickins 	spin_lock(&init_mm.page_table_lock);
6368ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
637872fec16SHugh Dickins 		pmd_populate_kernel(&init_mm, pmd, new);
6382f569afdSMartin Schwidefsky 		new = NULL;
6398ac1f832SAndrea Arcangeli 	} else
6408ac1f832SAndrea Arcangeli 		VM_BUG_ON(pmd_trans_splitting(*pmd));
641872fec16SHugh Dickins 	spin_unlock(&init_mm.page_table_lock);
6422f569afdSMartin Schwidefsky 	if (new)
6432f569afdSMartin Schwidefsky 		pte_free_kernel(&init_mm, new);
6441bb3630eSHugh Dickins 	return 0;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
647d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss)
648ae859762SHugh Dickins {
649d559db08SKAMEZAWA Hiroyuki 	memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
650d559db08SKAMEZAWA Hiroyuki }
651d559db08SKAMEZAWA Hiroyuki 
652d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
653d559db08SKAMEZAWA Hiroyuki {
654d559db08SKAMEZAWA Hiroyuki 	int i;
655d559db08SKAMEZAWA Hiroyuki 
65634e55232SKAMEZAWA Hiroyuki 	if (current->mm == mm)
65705af2e10SDavid Rientjes 		sync_mm_rss(mm);
658d559db08SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++)
659d559db08SKAMEZAWA Hiroyuki 		if (rss[i])
660d559db08SKAMEZAWA Hiroyuki 			add_mm_counter(mm, i, rss[i]);
661ae859762SHugh Dickins }
662ae859762SHugh Dickins 
6631da177e4SLinus Torvalds /*
6646aab341eSLinus Torvalds  * This function is called to print an error when a bad pte
6656aab341eSLinus Torvalds  * is found. For example, we might have a PFN-mapped pte in
6666aab341eSLinus Torvalds  * a region that doesn't allow it.
667b5810039SNick Piggin  *
668b5810039SNick Piggin  * The calling function must still handle the error.
669b5810039SNick Piggin  */
6703dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
6713dc14741SHugh Dickins 			  pte_t pte, struct page *page)
672b5810039SNick Piggin {
6733dc14741SHugh Dickins 	pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
6743dc14741SHugh Dickins 	pud_t *pud = pud_offset(pgd, addr);
6753dc14741SHugh Dickins 	pmd_t *pmd = pmd_offset(pud, addr);
6763dc14741SHugh Dickins 	struct address_space *mapping;
6773dc14741SHugh Dickins 	pgoff_t index;
678d936cf9bSHugh Dickins 	static unsigned long resume;
679d936cf9bSHugh Dickins 	static unsigned long nr_shown;
680d936cf9bSHugh Dickins 	static unsigned long nr_unshown;
681d936cf9bSHugh Dickins 
682d936cf9bSHugh Dickins 	/*
683d936cf9bSHugh Dickins 	 * Allow a burst of 60 reports, then keep quiet for that minute;
684d936cf9bSHugh Dickins 	 * or allow a steady drip of one report per second.
685d936cf9bSHugh Dickins 	 */
686d936cf9bSHugh Dickins 	if (nr_shown == 60) {
687d936cf9bSHugh Dickins 		if (time_before(jiffies, resume)) {
688d936cf9bSHugh Dickins 			nr_unshown++;
689d936cf9bSHugh Dickins 			return;
690d936cf9bSHugh Dickins 		}
691d936cf9bSHugh Dickins 		if (nr_unshown) {
6921e9e6365SHugh Dickins 			printk(KERN_ALERT
6931e9e6365SHugh Dickins 				"BUG: Bad page map: %lu messages suppressed\n",
694d936cf9bSHugh Dickins 				nr_unshown);
695d936cf9bSHugh Dickins 			nr_unshown = 0;
696d936cf9bSHugh Dickins 		}
697d936cf9bSHugh Dickins 		nr_shown = 0;
698d936cf9bSHugh Dickins 	}
699d936cf9bSHugh Dickins 	if (nr_shown++ == 0)
700d936cf9bSHugh Dickins 		resume = jiffies + 60 * HZ;
7013dc14741SHugh Dickins 
7023dc14741SHugh Dickins 	mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
7033dc14741SHugh Dickins 	index = linear_page_index(vma, addr);
7043dc14741SHugh Dickins 
7051e9e6365SHugh Dickins 	printk(KERN_ALERT
7061e9e6365SHugh Dickins 		"BUG: Bad page map in process %s  pte:%08llx pmd:%08llx\n",
7073dc14741SHugh Dickins 		current->comm,
7083dc14741SHugh Dickins 		(long long)pte_val(pte), (long long)pmd_val(*pmd));
709718a3821SWu Fengguang 	if (page)
710718a3821SWu Fengguang 		dump_page(page);
7111e9e6365SHugh Dickins 	printk(KERN_ALERT
7123dc14741SHugh Dickins 		"addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
7133dc14741SHugh Dickins 		(void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
7143dc14741SHugh Dickins 	/*
7153dc14741SHugh Dickins 	 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
7163dc14741SHugh Dickins 	 */
7173dc14741SHugh Dickins 	if (vma->vm_ops)
7181e9e6365SHugh Dickins 		print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n",
7193dc14741SHugh Dickins 				(unsigned long)vma->vm_ops->fault);
7203dc14741SHugh Dickins 	if (vma->vm_file && vma->vm_file->f_op)
7211e9e6365SHugh Dickins 		print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n",
7223dc14741SHugh Dickins 				(unsigned long)vma->vm_file->f_op->mmap);
723b5810039SNick Piggin 	dump_stack();
724373d4d09SRusty Russell 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
725b5810039SNick Piggin }
726b5810039SNick Piggin 
7272ec74c3eSSagi Grimberg static inline bool is_cow_mapping(vm_flags_t flags)
72867121172SLinus Torvalds {
72967121172SLinus Torvalds 	return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
73067121172SLinus Torvalds }
73167121172SLinus Torvalds 
732b5810039SNick Piggin /*
7337e675137SNick Piggin  * vm_normal_page -- This function gets the "struct page" associated with a pte.
7346aab341eSLinus Torvalds  *
7357e675137SNick Piggin  * "Special" mappings do not wish to be associated with a "struct page" (either
7367e675137SNick Piggin  * it doesn't exist, or it exists but they don't want to touch it). In this
7377e675137SNick Piggin  * case, NULL is returned here. "Normal" mappings do have a struct page.
738b379d790SJared Hulbert  *
7397e675137SNick Piggin  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
7407e675137SNick Piggin  * pte bit, in which case this function is trivial. Secondly, an architecture
7417e675137SNick Piggin  * may not have a spare pte bit, which requires a more complicated scheme,
7427e675137SNick Piggin  * described below.
7437e675137SNick Piggin  *
7447e675137SNick Piggin  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
7457e675137SNick Piggin  * special mapping (even if there are underlying and valid "struct pages").
7467e675137SNick Piggin  * COWed pages of a VM_PFNMAP are always normal.
7476aab341eSLinus Torvalds  *
748b379d790SJared Hulbert  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
749b379d790SJared Hulbert  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
7507e675137SNick Piggin  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
7517e675137SNick Piggin  * mapping will always honor the rule
7526aab341eSLinus Torvalds  *
7536aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
7546aab341eSLinus Torvalds  *
7557e675137SNick Piggin  * And for normal mappings this is false.
756b379d790SJared Hulbert  *
7577e675137SNick Piggin  * This restricts such mappings to be a linear translation from virtual address
7587e675137SNick Piggin  * to pfn. To get around this restriction, we allow arbitrary mappings so long
7597e675137SNick Piggin  * as the vma is not a COW mapping; in that case, we know that all ptes are
7607e675137SNick Piggin  * special (because none can have been COWed).
761b379d790SJared Hulbert  *
762b379d790SJared Hulbert  *
7637e675137SNick Piggin  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
7647e675137SNick Piggin  *
765b379d790SJared Hulbert  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
766b379d790SJared Hulbert  * page" backing, however the difference is that _all_ pages with a struct
767b379d790SJared Hulbert  * page (that is, those where pfn_valid is true) are refcounted and considered
768b379d790SJared Hulbert  * normal pages by the VM. The disadvantage is that pages are refcounted
769b379d790SJared Hulbert  * (which can be slower and simply not an option for some PFNMAP users). The
770b379d790SJared Hulbert  * advantage is that we don't have to follow the strict linearity rule of
771b379d790SJared Hulbert  * PFNMAP mappings in order to support COWable mappings.
772b379d790SJared Hulbert  *
773ee498ed7SHugh Dickins  */
7747e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL
7757e675137SNick Piggin # define HAVE_PTE_SPECIAL 1
7767e675137SNick Piggin #else
7777e675137SNick Piggin # define HAVE_PTE_SPECIAL 0
7787e675137SNick Piggin #endif
7797e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
7807e675137SNick Piggin 				pte_t pte)
781ee498ed7SHugh Dickins {
78222b31eecSHugh Dickins 	unsigned long pfn = pte_pfn(pte);
7837e675137SNick Piggin 
7847e675137SNick Piggin 	if (HAVE_PTE_SPECIAL) {
78522b31eecSHugh Dickins 		if (likely(!pte_special(pte)))
78622b31eecSHugh Dickins 			goto check_pfn;
787a13ea5b7SHugh Dickins 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
788a13ea5b7SHugh Dickins 			return NULL;
78962eede62SHugh Dickins 		if (!is_zero_pfn(pfn))
79022b31eecSHugh Dickins 			print_bad_pte(vma, addr, pte, NULL);
7917e675137SNick Piggin 		return NULL;
7927e675137SNick Piggin 	}
7937e675137SNick Piggin 
7947e675137SNick Piggin 	/* !HAVE_PTE_SPECIAL case follows: */
7957e675137SNick Piggin 
796b379d790SJared Hulbert 	if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
797b379d790SJared Hulbert 		if (vma->vm_flags & VM_MIXEDMAP) {
798b379d790SJared Hulbert 			if (!pfn_valid(pfn))
799b379d790SJared Hulbert 				return NULL;
800b379d790SJared Hulbert 			goto out;
801b379d790SJared Hulbert 		} else {
8027e675137SNick Piggin 			unsigned long off;
8037e675137SNick Piggin 			off = (addr - vma->vm_start) >> PAGE_SHIFT;
8046aab341eSLinus Torvalds 			if (pfn == vma->vm_pgoff + off)
8056aab341eSLinus Torvalds 				return NULL;
80667121172SLinus Torvalds 			if (!is_cow_mapping(vma->vm_flags))
807fb155c16SLinus Torvalds 				return NULL;
8086aab341eSLinus Torvalds 		}
809b379d790SJared Hulbert 	}
8106aab341eSLinus Torvalds 
81162eede62SHugh Dickins 	if (is_zero_pfn(pfn))
81262eede62SHugh Dickins 		return NULL;
81322b31eecSHugh Dickins check_pfn:
81422b31eecSHugh Dickins 	if (unlikely(pfn > highest_memmap_pfn)) {
81522b31eecSHugh Dickins 		print_bad_pte(vma, addr, pte, NULL);
81622b31eecSHugh Dickins 		return NULL;
81722b31eecSHugh Dickins 	}
8186aab341eSLinus Torvalds 
8196aab341eSLinus Torvalds 	/*
8207e675137SNick Piggin 	 * NOTE! We still have PageReserved() pages in the page tables.
8217e675137SNick Piggin 	 * eg. VDSO mappings can cause them to exist.
8226aab341eSLinus Torvalds 	 */
823b379d790SJared Hulbert out:
8246aab341eSLinus Torvalds 	return pfn_to_page(pfn);
825ee498ed7SHugh Dickins }
826ee498ed7SHugh Dickins 
827ee498ed7SHugh Dickins /*
8281da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
8291da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
8301da177e4SLinus Torvalds  * covered by this vma.
8311da177e4SLinus Torvalds  */
8321da177e4SLinus Torvalds 
833570a335bSHugh Dickins static inline unsigned long
8341da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
835b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
8368c103762SHugh Dickins 		unsigned long addr, int *rss)
8371da177e4SLinus Torvalds {
838b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
8391da177e4SLinus Torvalds 	pte_t pte = *src_pte;
8401da177e4SLinus Torvalds 	struct page *page;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
8431da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
8441da177e4SLinus Torvalds 		if (!pte_file(pte)) {
8450697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(pte);
8460697212aSChristoph Lameter 
847570a335bSHugh Dickins 			if (swap_duplicate(entry) < 0)
848570a335bSHugh Dickins 				return entry.val;
849570a335bSHugh Dickins 
8501da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
8511da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
8521da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
853f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
854f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
855f412ac08SHugh Dickins 						 &src_mm->mmlist);
8561da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
8571da177e4SLinus Torvalds 			}
858b084d435SKAMEZAWA Hiroyuki 			if (likely(!non_swap_entry(entry)))
859b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]++;
8609f9f1acdSKonstantin Khlebnikov 			else if (is_migration_entry(entry)) {
8619f9f1acdSKonstantin Khlebnikov 				page = migration_entry_to_page(entry);
8629f9f1acdSKonstantin Khlebnikov 
8639f9f1acdSKonstantin Khlebnikov 				if (PageAnon(page))
8649f9f1acdSKonstantin Khlebnikov 					rss[MM_ANONPAGES]++;
8659f9f1acdSKonstantin Khlebnikov 				else
8669f9f1acdSKonstantin Khlebnikov 					rss[MM_FILEPAGES]++;
8679f9f1acdSKonstantin Khlebnikov 
8689f9f1acdSKonstantin Khlebnikov 				if (is_write_migration_entry(entry) &&
8690697212aSChristoph Lameter 				    is_cow_mapping(vm_flags)) {
8700697212aSChristoph Lameter 					/*
8719f9f1acdSKonstantin Khlebnikov 					 * COW mappings require pages in both
8729f9f1acdSKonstantin Khlebnikov 					 * parent and child to be set to read.
8730697212aSChristoph Lameter 					 */
8740697212aSChristoph Lameter 					make_migration_entry_read(&entry);
8750697212aSChristoph Lameter 					pte = swp_entry_to_pte(entry);
8760697212aSChristoph Lameter 					set_pte_at(src_mm, addr, src_pte, pte);
8770697212aSChristoph Lameter 				}
8781da177e4SLinus Torvalds 			}
8799f9f1acdSKonstantin Khlebnikov 		}
880ae859762SHugh Dickins 		goto out_set_pte;
8811da177e4SLinus Torvalds 	}
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	/*
8841da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
8851da177e4SLinus Torvalds 	 * in the parent and the child
8861da177e4SLinus Torvalds 	 */
88767121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
8881da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
8893dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	/*
8931da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
8941da177e4SLinus Torvalds 	 * the child
8951da177e4SLinus Torvalds 	 */
8961da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
8971da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
8981da177e4SLinus Torvalds 	pte = pte_mkold(pte);
8996aab341eSLinus Torvalds 
9006aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
9016aab341eSLinus Torvalds 	if (page) {
9021da177e4SLinus Torvalds 		get_page(page);
90321333b2bSHugh Dickins 		page_dup_rmap(page);
904d559db08SKAMEZAWA Hiroyuki 		if (PageAnon(page))
905d559db08SKAMEZAWA Hiroyuki 			rss[MM_ANONPAGES]++;
906d559db08SKAMEZAWA Hiroyuki 		else
907d559db08SKAMEZAWA Hiroyuki 			rss[MM_FILEPAGES]++;
9086aab341eSLinus Torvalds 	}
909ae859762SHugh Dickins 
910ae859762SHugh Dickins out_set_pte:
911ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
912570a335bSHugh Dickins 	return 0;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
91571e3aac0SAndrea Arcangeli int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9161da177e4SLinus Torvalds 		   pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
9171da177e4SLinus Torvalds 		   unsigned long addr, unsigned long end)
9181da177e4SLinus Torvalds {
919c36987e2SDaisuke Nishimura 	pte_t *orig_src_pte, *orig_dst_pte;
9201da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
921c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
922e040f218SHugh Dickins 	int progress = 0;
923d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
924570a335bSHugh Dickins 	swp_entry_t entry = (swp_entry_t){0};
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds again:
927d559db08SKAMEZAWA Hiroyuki 	init_rss_vec(rss);
928d559db08SKAMEZAWA Hiroyuki 
929c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
9301da177e4SLinus Torvalds 	if (!dst_pte)
9311da177e4SLinus Torvalds 		return -ENOMEM;
932ece0e2b6SPeter Zijlstra 	src_pte = pte_offset_map(src_pmd, addr);
9334c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
934f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
935c36987e2SDaisuke Nishimura 	orig_src_pte = src_pte;
936c36987e2SDaisuke Nishimura 	orig_dst_pte = dst_pte;
9376606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	do {
9401da177e4SLinus Torvalds 		/*
9411da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
9421da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
9431da177e4SLinus Torvalds 		 */
944e040f218SHugh Dickins 		if (progress >= 32) {
945e040f218SHugh Dickins 			progress = 0;
946e040f218SHugh Dickins 			if (need_resched() ||
94795c354feSNick Piggin 			    spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
9481da177e4SLinus Torvalds 				break;
949e040f218SHugh Dickins 		}
9501da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
9511da177e4SLinus Torvalds 			progress++;
9521da177e4SLinus Torvalds 			continue;
9531da177e4SLinus Torvalds 		}
954570a335bSHugh Dickins 		entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
955570a335bSHugh Dickins 							vma, addr, rss);
956570a335bSHugh Dickins 		if (entry.val)
957570a335bSHugh Dickins 			break;
9581da177e4SLinus Torvalds 		progress += 8;
9591da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
9601da177e4SLinus Torvalds 
9616606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
962c74df32cSHugh Dickins 	spin_unlock(src_ptl);
963ece0e2b6SPeter Zijlstra 	pte_unmap(orig_src_pte);
964d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(dst_mm, rss);
965c36987e2SDaisuke Nishimura 	pte_unmap_unlock(orig_dst_pte, dst_ptl);
966c74df32cSHugh Dickins 	cond_resched();
967570a335bSHugh Dickins 
968570a335bSHugh Dickins 	if (entry.val) {
969570a335bSHugh Dickins 		if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
970570a335bSHugh Dickins 			return -ENOMEM;
971570a335bSHugh Dickins 		progress = 0;
972570a335bSHugh Dickins 	}
9731da177e4SLinus Torvalds 	if (addr != end)
9741da177e4SLinus Torvalds 		goto again;
9751da177e4SLinus Torvalds 	return 0;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9791da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
9801da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9811da177e4SLinus Torvalds {
9821da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
9831da177e4SLinus Torvalds 	unsigned long next;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
9861da177e4SLinus Torvalds 	if (!dst_pmd)
9871da177e4SLinus Torvalds 		return -ENOMEM;
9881da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
9891da177e4SLinus Torvalds 	do {
9901da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
99171e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*src_pmd)) {
99271e3aac0SAndrea Arcangeli 			int err;
99314d1a55cSAndrea Arcangeli 			VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
99471e3aac0SAndrea Arcangeli 			err = copy_huge_pmd(dst_mm, src_mm,
99571e3aac0SAndrea Arcangeli 					    dst_pmd, src_pmd, addr, vma);
99671e3aac0SAndrea Arcangeli 			if (err == -ENOMEM)
99771e3aac0SAndrea Arcangeli 				return -ENOMEM;
99871e3aac0SAndrea Arcangeli 			if (!err)
99971e3aac0SAndrea Arcangeli 				continue;
100071e3aac0SAndrea Arcangeli 			/* fall through */
100171e3aac0SAndrea Arcangeli 		}
10021da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
10031da177e4SLinus Torvalds 			continue;
10041da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
10051da177e4SLinus Torvalds 						vma, addr, next))
10061da177e4SLinus Torvalds 			return -ENOMEM;
10071da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
10081da177e4SLinus Torvalds 	return 0;
10091da177e4SLinus Torvalds }
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10121da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
10131da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
10161da177e4SLinus Torvalds 	unsigned long next;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
10191da177e4SLinus Torvalds 	if (!dst_pud)
10201da177e4SLinus Torvalds 		return -ENOMEM;
10211da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
10221da177e4SLinus Torvalds 	do {
10231da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
10241da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
10251da177e4SLinus Torvalds 			continue;
10261da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
10271da177e4SLinus Torvalds 						vma, addr, next))
10281da177e4SLinus Torvalds 			return -ENOMEM;
10291da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
10301da177e4SLinus Torvalds 	return 0;
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10341da177e4SLinus Torvalds 		struct vm_area_struct *vma)
10351da177e4SLinus Torvalds {
10361da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
10371da177e4SLinus Torvalds 	unsigned long next;
10381da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
10391da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
10402ec74c3eSSagi Grimberg 	unsigned long mmun_start;	/* For mmu_notifiers */
10412ec74c3eSSagi Grimberg 	unsigned long mmun_end;		/* For mmu_notifiers */
10422ec74c3eSSagi Grimberg 	bool is_cow;
1043cddb8a5cSAndrea Arcangeli 	int ret;
10441da177e4SLinus Torvalds 
1045d992895bSNick Piggin 	/*
1046d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
1047d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
1048d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
1049d992895bSNick Piggin 	 * efficient than faulting.
1050d992895bSNick Piggin 	 */
10514b6e1e37SKonstantin Khlebnikov 	if (!(vma->vm_flags & (VM_HUGETLB | VM_NONLINEAR |
10524b6e1e37SKonstantin Khlebnikov 			       VM_PFNMAP | VM_MIXEDMAP))) {
1053d992895bSNick Piggin 		if (!vma->anon_vma)
1054d992895bSNick Piggin 			return 0;
1055d992895bSNick Piggin 	}
1056d992895bSNick Piggin 
10571da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
10581da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
10591da177e4SLinus Torvalds 
1060b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP)) {
10612ab64037Svenkatesh.pallipadi@intel.com 		/*
10622ab64037Svenkatesh.pallipadi@intel.com 		 * We do not free on error cases below as remove_vma
10632ab64037Svenkatesh.pallipadi@intel.com 		 * gets called on error from higher level routine
10642ab64037Svenkatesh.pallipadi@intel.com 		 */
10655180da41SSuresh Siddha 		ret = track_pfn_copy(vma);
10662ab64037Svenkatesh.pallipadi@intel.com 		if (ret)
10672ab64037Svenkatesh.pallipadi@intel.com 			return ret;
10682ab64037Svenkatesh.pallipadi@intel.com 	}
10692ab64037Svenkatesh.pallipadi@intel.com 
1070cddb8a5cSAndrea Arcangeli 	/*
1071cddb8a5cSAndrea Arcangeli 	 * We need to invalidate the secondary MMU mappings only when
1072cddb8a5cSAndrea Arcangeli 	 * there could be a permission downgrade on the ptes of the
1073cddb8a5cSAndrea Arcangeli 	 * parent mm. And a permission downgrade will only happen if
1074cddb8a5cSAndrea Arcangeli 	 * is_cow_mapping() returns true.
1075cddb8a5cSAndrea Arcangeli 	 */
10762ec74c3eSSagi Grimberg 	is_cow = is_cow_mapping(vma->vm_flags);
10772ec74c3eSSagi Grimberg 	mmun_start = addr;
10782ec74c3eSSagi Grimberg 	mmun_end   = end;
10792ec74c3eSSagi Grimberg 	if (is_cow)
10802ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_start(src_mm, mmun_start,
10812ec74c3eSSagi Grimberg 						    mmun_end);
1082cddb8a5cSAndrea Arcangeli 
1083cddb8a5cSAndrea Arcangeli 	ret = 0;
10841da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
10851da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
10861da177e4SLinus Torvalds 	do {
10871da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
10881da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
10891da177e4SLinus Torvalds 			continue;
1090cddb8a5cSAndrea Arcangeli 		if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
1091cddb8a5cSAndrea Arcangeli 					    vma, addr, next))) {
1092cddb8a5cSAndrea Arcangeli 			ret = -ENOMEM;
1093cddb8a5cSAndrea Arcangeli 			break;
1094cddb8a5cSAndrea Arcangeli 		}
10951da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
1096cddb8a5cSAndrea Arcangeli 
10972ec74c3eSSagi Grimberg 	if (is_cow)
10982ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
1099cddb8a5cSAndrea Arcangeli 	return ret;
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
110251c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
1103b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
11041da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
110597a89413SPeter Zijlstra 				struct zap_details *details)
11061da177e4SLinus Torvalds {
1107b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
1108d16dfc55SPeter Zijlstra 	int force_flush = 0;
1109d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
111097a89413SPeter Zijlstra 	spinlock_t *ptl;
11115f1a1907SSteven Rostedt 	pte_t *start_pte;
111297a89413SPeter Zijlstra 	pte_t *pte;
1113d559db08SKAMEZAWA Hiroyuki 
1114d16dfc55SPeter Zijlstra again:
1115e303297eSPeter Zijlstra 	init_rss_vec(rss);
11165f1a1907SSteven Rostedt 	start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
11175f1a1907SSteven Rostedt 	pte = start_pte;
11186606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
11191da177e4SLinus Torvalds 	do {
11201da177e4SLinus Torvalds 		pte_t ptent = *pte;
112151c6f666SRobin Holt 		if (pte_none(ptent)) {
11221da177e4SLinus Torvalds 			continue;
112351c6f666SRobin Holt 		}
112451c6f666SRobin Holt 
11256f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
11266f5e6b9eSHugh Dickins 			struct page *page;
11276f5e6b9eSHugh Dickins 
11286aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
11291da177e4SLinus Torvalds 			if (unlikely(details) && page) {
11301da177e4SLinus Torvalds 				/*
11311da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
11321da177e4SLinus Torvalds 				 * invalidate cache without truncating:
11331da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
11341da177e4SLinus Torvalds 				 */
11351da177e4SLinus Torvalds 				if (details->check_mapping &&
11361da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
11371da177e4SLinus Torvalds 					continue;
11381da177e4SLinus Torvalds 				/*
11391da177e4SLinus Torvalds 				 * Each page->index must be checked when
11401da177e4SLinus Torvalds 				 * invalidating or truncating nonlinear.
11411da177e4SLinus Torvalds 				 */
11421da177e4SLinus Torvalds 				if (details->nonlinear_vma &&
11431da177e4SLinus Torvalds 				    (page->index < details->first_index ||
11441da177e4SLinus Torvalds 				     page->index > details->last_index))
11451da177e4SLinus Torvalds 					continue;
11461da177e4SLinus Torvalds 			}
1147b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
1148a600388dSZachary Amsden 							tlb->fullmm);
11491da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
11501da177e4SLinus Torvalds 			if (unlikely(!page))
11511da177e4SLinus Torvalds 				continue;
11521da177e4SLinus Torvalds 			if (unlikely(details) && details->nonlinear_vma
11531da177e4SLinus Torvalds 			    && linear_page_index(details->nonlinear_vma,
11541da177e4SLinus Torvalds 						addr) != page->index)
1155b5810039SNick Piggin 				set_pte_at(mm, addr, pte,
11561da177e4SLinus Torvalds 					   pgoff_to_pte(page->index));
11571da177e4SLinus Torvalds 			if (PageAnon(page))
1158d559db08SKAMEZAWA Hiroyuki 				rss[MM_ANONPAGES]--;
11596237bcd9SHugh Dickins 			else {
11606237bcd9SHugh Dickins 				if (pte_dirty(ptent))
11616237bcd9SHugh Dickins 					set_page_dirty(page);
11624917e5d0SJohannes Weiner 				if (pte_young(ptent) &&
11634917e5d0SJohannes Weiner 				    likely(!VM_SequentialReadHint(vma)))
1164bf3f3bc5SNick Piggin 					mark_page_accessed(page);
1165d559db08SKAMEZAWA Hiroyuki 				rss[MM_FILEPAGES]--;
11666237bcd9SHugh Dickins 			}
1167edc315fdSHugh Dickins 			page_remove_rmap(page);
11683dc14741SHugh Dickins 			if (unlikely(page_mapcount(page) < 0))
11693dc14741SHugh Dickins 				print_bad_pte(vma, addr, ptent, page);
1170d16dfc55SPeter Zijlstra 			force_flush = !__tlb_remove_page(tlb, page);
1171d16dfc55SPeter Zijlstra 			if (force_flush)
1172d16dfc55SPeter Zijlstra 				break;
11731da177e4SLinus Torvalds 			continue;
11741da177e4SLinus Torvalds 		}
11751da177e4SLinus Torvalds 		/*
11761da177e4SLinus Torvalds 		 * If details->check_mapping, we leave swap entries;
11771da177e4SLinus Torvalds 		 * if details->nonlinear_vma, we leave file entries.
11781da177e4SLinus Torvalds 		 */
11791da177e4SLinus Torvalds 		if (unlikely(details))
11801da177e4SLinus Torvalds 			continue;
11812509ef26SHugh Dickins 		if (pte_file(ptent)) {
11822509ef26SHugh Dickins 			if (unlikely(!(vma->vm_flags & VM_NONLINEAR)))
11832509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
1184b084d435SKAMEZAWA Hiroyuki 		} else {
1185b084d435SKAMEZAWA Hiroyuki 			swp_entry_t entry = pte_to_swp_entry(ptent);
1186b084d435SKAMEZAWA Hiroyuki 
1187b084d435SKAMEZAWA Hiroyuki 			if (!non_swap_entry(entry))
1188b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]--;
11899f9f1acdSKonstantin Khlebnikov 			else if (is_migration_entry(entry)) {
11909f9f1acdSKonstantin Khlebnikov 				struct page *page;
11919f9f1acdSKonstantin Khlebnikov 
11929f9f1acdSKonstantin Khlebnikov 				page = migration_entry_to_page(entry);
11939f9f1acdSKonstantin Khlebnikov 
11949f9f1acdSKonstantin Khlebnikov 				if (PageAnon(page))
11959f9f1acdSKonstantin Khlebnikov 					rss[MM_ANONPAGES]--;
11969f9f1acdSKonstantin Khlebnikov 				else
11979f9f1acdSKonstantin Khlebnikov 					rss[MM_FILEPAGES]--;
11989f9f1acdSKonstantin Khlebnikov 			}
1199b084d435SKAMEZAWA Hiroyuki 			if (unlikely(!free_swap_and_cache(entry)))
12002509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
1201b084d435SKAMEZAWA Hiroyuki 		}
12029888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
120397a89413SPeter Zijlstra 	} while (pte++, addr += PAGE_SIZE, addr != end);
1204ae859762SHugh Dickins 
1205d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(mm, rss);
12066606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
12075f1a1907SSteven Rostedt 	pte_unmap_unlock(start_pte, ptl);
120851c6f666SRobin Holt 
1209d16dfc55SPeter Zijlstra 	/*
1210d16dfc55SPeter Zijlstra 	 * mmu_gather ran out of room to batch pages, we break out of
1211d16dfc55SPeter Zijlstra 	 * the PTE lock to avoid doing the potential expensive TLB invalidate
1212d16dfc55SPeter Zijlstra 	 * and page-free while holding it.
1213d16dfc55SPeter Zijlstra 	 */
1214d16dfc55SPeter Zijlstra 	if (force_flush) {
1215d16dfc55SPeter Zijlstra 		force_flush = 0;
1216597e1c35SAlex Shi 
1217597e1c35SAlex Shi #ifdef HAVE_GENERIC_MMU_GATHER
1218597e1c35SAlex Shi 		tlb->start = addr;
1219597e1c35SAlex Shi 		tlb->end = end;
1220597e1c35SAlex Shi #endif
1221d16dfc55SPeter Zijlstra 		tlb_flush_mmu(tlb);
1222d16dfc55SPeter Zijlstra 		if (addr != end)
1223d16dfc55SPeter Zijlstra 			goto again;
1224d16dfc55SPeter Zijlstra 	}
1225d16dfc55SPeter Zijlstra 
122651c6f666SRobin Holt 	return addr;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
122951c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1230b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
12311da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
123297a89413SPeter Zijlstra 				struct zap_details *details)
12331da177e4SLinus Torvalds {
12341da177e4SLinus Torvalds 	pmd_t *pmd;
12351da177e4SLinus Torvalds 	unsigned long next;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
12381da177e4SLinus Torvalds 	do {
12391da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
124071e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*pmd)) {
124114d1a55cSAndrea Arcangeli 			if (next - addr != HPAGE_PMD_SIZE) {
1242e0897d75SDavid Rientjes #ifdef CONFIG_DEBUG_VM
1243e0897d75SDavid Rientjes 				if (!rwsem_is_locked(&tlb->mm->mmap_sem)) {
1244e0897d75SDavid 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",
1245e0897d75SDavid Rientjes 						__func__, addr, end,
1246e0897d75SDavid Rientjes 						vma->vm_start,
1247e0897d75SDavid Rientjes 						vma->vm_end);
1248e0897d75SDavid Rientjes 					BUG();
1249e0897d75SDavid Rientjes 				}
1250e0897d75SDavid Rientjes #endif
1251e180377fSKirill A. Shutemov 				split_huge_page_pmd(vma, addr, pmd);
1252f21760b1SShaohua Li 			} else if (zap_huge_pmd(tlb, vma, pmd, addr))
12531a5a9906SAndrea Arcangeli 				goto next;
125471e3aac0SAndrea Arcangeli 			/* fall through */
125571e3aac0SAndrea Arcangeli 		}
12561a5a9906SAndrea Arcangeli 		/*
12571a5a9906SAndrea Arcangeli 		 * Here there can be other concurrent MADV_DONTNEED or
12581a5a9906SAndrea Arcangeli 		 * trans huge page faults running, and if the pmd is
12591a5a9906SAndrea Arcangeli 		 * none or trans huge it can change under us. This is
12601a5a9906SAndrea Arcangeli 		 * because MADV_DONTNEED holds the mmap_sem in read
12611a5a9906SAndrea Arcangeli 		 * mode.
12621a5a9906SAndrea Arcangeli 		 */
12631a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
12641a5a9906SAndrea Arcangeli 			goto next;
126597a89413SPeter Zijlstra 		next = zap_pte_range(tlb, vma, pmd, addr, next, details);
12661a5a9906SAndrea Arcangeli next:
126797a89413SPeter Zijlstra 		cond_resched();
126897a89413SPeter Zijlstra 	} while (pmd++, addr = next, addr != end);
126951c6f666SRobin Holt 
127051c6f666SRobin Holt 	return addr;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
127351c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1274b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
12751da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
127697a89413SPeter Zijlstra 				struct zap_details *details)
12771da177e4SLinus Torvalds {
12781da177e4SLinus Torvalds 	pud_t *pud;
12791da177e4SLinus Torvalds 	unsigned long next;
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
12821da177e4SLinus Torvalds 	do {
12831da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
128497a89413SPeter Zijlstra 		if (pud_none_or_clear_bad(pud))
12851da177e4SLinus Torvalds 			continue;
128697a89413SPeter Zijlstra 		next = zap_pmd_range(tlb, vma, pud, addr, next, details);
128797a89413SPeter Zijlstra 	} while (pud++, addr = next, addr != end);
128851c6f666SRobin Holt 
128951c6f666SRobin Holt 	return addr;
12901da177e4SLinus Torvalds }
12911da177e4SLinus Torvalds 
1292038c7aa1SAl Viro static void unmap_page_range(struct mmu_gather *tlb,
129351c6f666SRobin Holt 			     struct vm_area_struct *vma,
12941da177e4SLinus Torvalds 			     unsigned long addr, unsigned long end,
129597a89413SPeter Zijlstra 			     struct zap_details *details)
12961da177e4SLinus Torvalds {
12971da177e4SLinus Torvalds 	pgd_t *pgd;
12981da177e4SLinus Torvalds 	unsigned long next;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	if (details && !details->check_mapping && !details->nonlinear_vma)
13011da177e4SLinus Torvalds 		details = NULL;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1304569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_start();
13051da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
13061da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
13071da177e4SLinus Torvalds 	do {
13081da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
130997a89413SPeter Zijlstra 		if (pgd_none_or_clear_bad(pgd))
13101da177e4SLinus Torvalds 			continue;
131197a89413SPeter Zijlstra 		next = zap_pud_range(tlb, vma, pgd, addr, next, details);
131297a89413SPeter Zijlstra 	} while (pgd++, addr = next, addr != end);
13131da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
1314569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_end();
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
1317f5cc4eefSAl Viro 
1318f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb,
13191da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
13204f74d2c8SLinus Torvalds 		unsigned long end_addr,
13211da177e4SLinus Torvalds 		struct zap_details *details)
13221da177e4SLinus Torvalds {
1323f5cc4eefSAl Viro 	unsigned long start = max(vma->vm_start, start_addr);
13241da177e4SLinus Torvalds 	unsigned long end;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	if (start >= vma->vm_end)
1327f5cc4eefSAl Viro 		return;
13281da177e4SLinus Torvalds 	end = min(vma->vm_end, end_addr);
13291da177e4SLinus Torvalds 	if (end <= vma->vm_start)
1330f5cc4eefSAl Viro 		return;
13311da177e4SLinus Torvalds 
1332cbc91f71SSrikar Dronamraju 	if (vma->vm_file)
1333cbc91f71SSrikar Dronamraju 		uprobe_munmap(vma, start, end);
1334cbc91f71SSrikar Dronamraju 
1335b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP))
13365180da41SSuresh Siddha 		untrack_pfn(vma, 0, 0);
13372ab64037Svenkatesh.pallipadi@intel.com 
13388b2a1238SAl Viro 	if (start != end) {
133951c6f666SRobin Holt 		if (unlikely(is_vm_hugetlb_page(vma))) {
1340a137e1ccSAndi Kleen 			/*
1341a137e1ccSAndi Kleen 			 * It is undesirable to test vma->vm_file as it
1342a137e1ccSAndi Kleen 			 * should be non-null for valid hugetlb area.
1343a137e1ccSAndi Kleen 			 * However, vm_file will be NULL in the error
1344a137e1ccSAndi Kleen 			 * cleanup path of do_mmap_pgoff. When
1345a137e1ccSAndi Kleen 			 * hugetlbfs ->mmap method fails,
1346a137e1ccSAndi Kleen 			 * do_mmap_pgoff() nullifies vma->vm_file
1347a137e1ccSAndi Kleen 			 * before calling this function to clean up.
1348a137e1ccSAndi Kleen 			 * Since no pte has actually been setup, it is
1349a137e1ccSAndi Kleen 			 * safe to do nothing in this case.
1350a137e1ccSAndi Kleen 			 */
135124669e58SAneesh Kumar K.V 			if (vma->vm_file) {
135224669e58SAneesh Kumar K.V 				mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
1353d833352aSMel Gorman 				__unmap_hugepage_range_final(tlb, vma, start, end, NULL);
135424669e58SAneesh Kumar K.V 				mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
135524669e58SAneesh Kumar K.V 			}
135651c6f666SRobin Holt 		} else
1357038c7aa1SAl Viro 			unmap_page_range(tlb, vma, start, end, details);
135897a89413SPeter Zijlstra 	}
135951c6f666SRobin Holt }
13601da177e4SLinus Torvalds 
1361f5cc4eefSAl Viro /**
1362f5cc4eefSAl Viro  * unmap_vmas - unmap a range of memory covered by a list of vma's
1363f5cc4eefSAl Viro  * @tlb: address of the caller's struct mmu_gather
1364f5cc4eefSAl Viro  * @vma: the starting vma
1365f5cc4eefSAl Viro  * @start_addr: virtual address at which to start unmapping
1366f5cc4eefSAl Viro  * @end_addr: virtual address at which to end unmapping
1367f5cc4eefSAl Viro  *
1368f5cc4eefSAl Viro  * Unmap all pages in the vma list.
1369f5cc4eefSAl Viro  *
1370f5cc4eefSAl Viro  * Only addresses between `start' and `end' will be unmapped.
1371f5cc4eefSAl Viro  *
1372f5cc4eefSAl Viro  * The VMA list must be sorted in ascending virtual address order.
1373f5cc4eefSAl Viro  *
1374f5cc4eefSAl Viro  * unmap_vmas() assumes that the caller will flush the whole unmapped address
1375f5cc4eefSAl Viro  * range after unmap_vmas() returns.  So the only responsibility here is to
1376f5cc4eefSAl Viro  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1377f5cc4eefSAl Viro  * drops the lock and schedules.
1378f5cc4eefSAl Viro  */
1379f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb,
1380f5cc4eefSAl Viro 		struct vm_area_struct *vma, unsigned long start_addr,
13814f74d2c8SLinus Torvalds 		unsigned long end_addr)
1382f5cc4eefSAl Viro {
1383f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1384f5cc4eefSAl Viro 
1385f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
1386f5cc4eefSAl Viro 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
13874f74d2c8SLinus Torvalds 		unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
1388cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
13891da177e4SLinus Torvalds }
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds /**
13921da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
13931da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
1394eb4546bbSRandy Dunlap  * @start: starting address of pages to zap
13951da177e4SLinus Torvalds  * @size: number of bytes to zap
13961da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
1397f5cc4eefSAl Viro  *
1398f5cc4eefSAl Viro  * Caller must protect the VMA list
13991da177e4SLinus Torvalds  */
14007e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start,
14011da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
14021da177e4SLinus Torvalds {
14031da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1404d16dfc55SPeter Zijlstra 	struct mmu_gather tlb;
14057e027b14SLinus Torvalds 	unsigned long end = start + size;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	lru_add_drain();
1408d16dfc55SPeter Zijlstra 	tlb_gather_mmu(&tlb, mm, 0);
1409365e9c87SHugh Dickins 	update_hiwater_rss(mm);
14107e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_start(mm, start, end);
14117e027b14SLinus Torvalds 	for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
14124f74d2c8SLinus Torvalds 		unmap_single_vma(&tlb, vma, start, end, details);
14137e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_end(mm, start, end);
14147e027b14SLinus Torvalds 	tlb_finish_mmu(&tlb, start, end);
14151da177e4SLinus Torvalds }
14161da177e4SLinus Torvalds 
1417c627f9ccSJack Steiner /**
1418f5cc4eefSAl Viro  * zap_page_range_single - remove user pages in a given range
1419f5cc4eefSAl Viro  * @vma: vm_area_struct holding the applicable pages
1420f5cc4eefSAl Viro  * @address: starting address of pages to zap
1421f5cc4eefSAl Viro  * @size: number of bytes to zap
1422f5cc4eefSAl Viro  * @details: details of nonlinear truncation or shared cache invalidation
1423f5cc4eefSAl Viro  *
1424f5cc4eefSAl Viro  * The range must fit into one VMA.
1425f5cc4eefSAl Viro  */
1426f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1427f5cc4eefSAl Viro 		unsigned long size, struct zap_details *details)
1428f5cc4eefSAl Viro {
1429f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1430f5cc4eefSAl Viro 	struct mmu_gather tlb;
1431f5cc4eefSAl Viro 	unsigned long end = address + size;
1432f5cc4eefSAl Viro 
1433f5cc4eefSAl Viro 	lru_add_drain();
1434f5cc4eefSAl Viro 	tlb_gather_mmu(&tlb, mm, 0);
1435f5cc4eefSAl Viro 	update_hiwater_rss(mm);
1436f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, address, end);
14374f74d2c8SLinus Torvalds 	unmap_single_vma(&tlb, vma, address, end, details);
1438f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_end(mm, address, end);
1439f5cc4eefSAl Viro 	tlb_finish_mmu(&tlb, address, end);
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
1442c627f9ccSJack Steiner /**
1443c627f9ccSJack Steiner  * zap_vma_ptes - remove ptes mapping the vma
1444c627f9ccSJack Steiner  * @vma: vm_area_struct holding ptes to be zapped
1445c627f9ccSJack Steiner  * @address: starting address of pages to zap
1446c627f9ccSJack Steiner  * @size: number of bytes to zap
1447c627f9ccSJack Steiner  *
1448c627f9ccSJack Steiner  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1449c627f9ccSJack Steiner  *
1450c627f9ccSJack Steiner  * The entire address range must be fully contained within the vma.
1451c627f9ccSJack Steiner  *
1452c627f9ccSJack Steiner  * Returns 0 if successful.
1453c627f9ccSJack Steiner  */
1454c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1455c627f9ccSJack Steiner 		unsigned long size)
1456c627f9ccSJack Steiner {
1457c627f9ccSJack Steiner 	if (address < vma->vm_start || address + size > vma->vm_end ||
1458c627f9ccSJack Steiner 	    		!(vma->vm_flags & VM_PFNMAP))
1459c627f9ccSJack Steiner 		return -1;
1460f5cc4eefSAl Viro 	zap_page_range_single(vma, address, size, NULL);
1461c627f9ccSJack Steiner 	return 0;
1462c627f9ccSJack Steiner }
1463c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes);
1464c627f9ccSJack Steiner 
1465142762bdSJohannes Weiner /**
1466240aadeeSMichel Lespinasse  * follow_page_mask - look up a page descriptor from a user-virtual address
1467142762bdSJohannes Weiner  * @vma: vm_area_struct mapping @address
1468142762bdSJohannes Weiner  * @address: virtual address to look up
1469142762bdSJohannes Weiner  * @flags: flags modifying lookup behaviour
1470240aadeeSMichel Lespinasse  * @page_mask: on output, *page_mask is set according to the size of the page
1471142762bdSJohannes Weiner  *
1472142762bdSJohannes Weiner  * @flags can have FOLL_ flags set, defined in <linux/mm.h>
1473142762bdSJohannes Weiner  *
1474142762bdSJohannes Weiner  * Returns the mapped (struct page *), %NULL if no mapping exists, or
1475142762bdSJohannes Weiner  * an error pointer if there is a mapping to something not represented
1476142762bdSJohannes Weiner  * by a page descriptor (see also vm_normal_page()).
14771da177e4SLinus Torvalds  */
1478240aadeeSMichel Lespinasse struct page *follow_page_mask(struct vm_area_struct *vma,
1479240aadeeSMichel Lespinasse 			      unsigned long address, unsigned int flags,
1480240aadeeSMichel Lespinasse 			      unsigned int *page_mask)
14811da177e4SLinus Torvalds {
14821da177e4SLinus Torvalds 	pgd_t *pgd;
14831da177e4SLinus Torvalds 	pud_t *pud;
14841da177e4SLinus Torvalds 	pmd_t *pmd;
14851da177e4SLinus Torvalds 	pte_t *ptep, pte;
1486deceb6cdSHugh Dickins 	spinlock_t *ptl;
14871da177e4SLinus Torvalds 	struct page *page;
14886aab341eSLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
14891da177e4SLinus Torvalds 
1490240aadeeSMichel Lespinasse 	*page_mask = 0;
1491240aadeeSMichel Lespinasse 
1492deceb6cdSHugh Dickins 	page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
1493deceb6cdSHugh Dickins 	if (!IS_ERR(page)) {
1494deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
1495deceb6cdSHugh Dickins 		goto out;
1496deceb6cdSHugh Dickins 	}
14971da177e4SLinus Torvalds 
1498deceb6cdSHugh Dickins 	page = NULL;
14991da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
15001da177e4SLinus Torvalds 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
1501deceb6cdSHugh Dickins 		goto no_page_table;
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	pud = pud_offset(pgd, address);
1504ceb86879SAndi Kleen 	if (pud_none(*pud))
1505ceb86879SAndi Kleen 		goto no_page_table;
15068a07651eSHugh Dickins 	if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
1507ceb86879SAndi Kleen 		BUG_ON(flags & FOLL_GET);
1508ceb86879SAndi Kleen 		page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
1509ceb86879SAndi Kleen 		goto out;
1510ceb86879SAndi Kleen 	}
1511ceb86879SAndi Kleen 	if (unlikely(pud_bad(*pud)))
1512deceb6cdSHugh Dickins 		goto no_page_table;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	pmd = pmd_offset(pud, address);
1515aeed5fceSHugh Dickins 	if (pmd_none(*pmd))
1516deceb6cdSHugh Dickins 		goto no_page_table;
151771e3aac0SAndrea Arcangeli 	if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
1518deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
1519deceb6cdSHugh Dickins 		page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
1520deceb6cdSHugh Dickins 		goto out;
1521deceb6cdSHugh Dickins 	}
15220b9d7052SAndrea Arcangeli 	if ((flags & FOLL_NUMA) && pmd_numa(*pmd))
15230b9d7052SAndrea Arcangeli 		goto no_page_table;
152471e3aac0SAndrea Arcangeli 	if (pmd_trans_huge(*pmd)) {
1525500d65d4SAndrea Arcangeli 		if (flags & FOLL_SPLIT) {
1526e180377fSKirill A. Shutemov 			split_huge_page_pmd(vma, address, pmd);
1527500d65d4SAndrea Arcangeli 			goto split_fallthrough;
1528500d65d4SAndrea Arcangeli 		}
152971e3aac0SAndrea Arcangeli 		spin_lock(&mm->page_table_lock);
153071e3aac0SAndrea Arcangeli 		if (likely(pmd_trans_huge(*pmd))) {
153171e3aac0SAndrea Arcangeli 			if (unlikely(pmd_trans_splitting(*pmd))) {
153271e3aac0SAndrea Arcangeli 				spin_unlock(&mm->page_table_lock);
153371e3aac0SAndrea Arcangeli 				wait_split_huge_page(vma->anon_vma, pmd);
153471e3aac0SAndrea Arcangeli 			} else {
1535b676b293SDavid Rientjes 				page = follow_trans_huge_pmd(vma, address,
153671e3aac0SAndrea Arcangeli 							     pmd, flags);
153771e3aac0SAndrea Arcangeli 				spin_unlock(&mm->page_table_lock);
1538240aadeeSMichel Lespinasse 				*page_mask = HPAGE_PMD_NR - 1;
153971e3aac0SAndrea Arcangeli 				goto out;
154071e3aac0SAndrea Arcangeli 			}
154171e3aac0SAndrea Arcangeli 		} else
154271e3aac0SAndrea Arcangeli 			spin_unlock(&mm->page_table_lock);
154371e3aac0SAndrea Arcangeli 		/* fall through */
154471e3aac0SAndrea Arcangeli 	}
1545500d65d4SAndrea Arcangeli split_fallthrough:
1546aeed5fceSHugh Dickins 	if (unlikely(pmd_bad(*pmd)))
1547aeed5fceSHugh Dickins 		goto no_page_table;
1548aeed5fceSHugh Dickins 
1549deceb6cdSHugh Dickins 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 	pte = *ptep;
15525117b3b8SHugh Dickins 	if (!pte_present(pte)) {
15535117b3b8SHugh Dickins 		swp_entry_t entry;
15545117b3b8SHugh Dickins 		/*
15555117b3b8SHugh Dickins 		 * KSM's break_ksm() relies upon recognizing a ksm page
15565117b3b8SHugh Dickins 		 * even while it is being migrated, so for that case we
15575117b3b8SHugh Dickins 		 * need migration_entry_wait().
15585117b3b8SHugh Dickins 		 */
15595117b3b8SHugh Dickins 		if (likely(!(flags & FOLL_MIGRATION)))
156089f5b7daSLinus Torvalds 			goto no_page;
15615117b3b8SHugh Dickins 		if (pte_none(pte) || pte_file(pte))
15625117b3b8SHugh Dickins 			goto no_page;
15635117b3b8SHugh Dickins 		entry = pte_to_swp_entry(pte);
15645117b3b8SHugh Dickins 		if (!is_migration_entry(entry))
15655117b3b8SHugh Dickins 			goto no_page;
15665117b3b8SHugh Dickins 		pte_unmap_unlock(ptep, ptl);
15675117b3b8SHugh Dickins 		migration_entry_wait(mm, pmd, address);
15685117b3b8SHugh Dickins 		goto split_fallthrough;
15695117b3b8SHugh Dickins 	}
15700b9d7052SAndrea Arcangeli 	if ((flags & FOLL_NUMA) && pte_numa(pte))
15710b9d7052SAndrea Arcangeli 		goto no_page;
1572deceb6cdSHugh Dickins 	if ((flags & FOLL_WRITE) && !pte_write(pte))
1573deceb6cdSHugh Dickins 		goto unlock;
1574a13ea5b7SHugh Dickins 
15756aab341eSLinus Torvalds 	page = vm_normal_page(vma, address, pte);
1576a13ea5b7SHugh Dickins 	if (unlikely(!page)) {
1577a13ea5b7SHugh Dickins 		if ((flags & FOLL_DUMP) ||
157862eede62SHugh Dickins 		    !is_zero_pfn(pte_pfn(pte)))
157989f5b7daSLinus Torvalds 			goto bad_page;
1580a13ea5b7SHugh Dickins 		page = pte_page(pte);
1581a13ea5b7SHugh Dickins 	}
1582deceb6cdSHugh Dickins 
1583deceb6cdSHugh Dickins 	if (flags & FOLL_GET)
158470b50f94SAndrea Arcangeli 		get_page_foll(page);
1585deceb6cdSHugh Dickins 	if (flags & FOLL_TOUCH) {
1586deceb6cdSHugh Dickins 		if ((flags & FOLL_WRITE) &&
1587deceb6cdSHugh Dickins 		    !pte_dirty(pte) && !PageDirty(page))
1588f33ea7f4SNick Piggin 			set_page_dirty(page);
1589bd775c42SKOSAKI Motohiro 		/*
1590bd775c42SKOSAKI Motohiro 		 * pte_mkyoung() would be more correct here, but atomic care
1591bd775c42SKOSAKI Motohiro 		 * is needed to avoid losing the dirty bit: it is easier to use
1592bd775c42SKOSAKI Motohiro 		 * mark_page_accessed().
1593bd775c42SKOSAKI Motohiro 		 */
15941da177e4SLinus Torvalds 		mark_page_accessed(page);
15951da177e4SLinus Torvalds 	}
1596a1fde08cSLinus Torvalds 	if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1597110d74a9SMichel Lespinasse 		/*
1598110d74a9SMichel Lespinasse 		 * The preliminary mapping check is mainly to avoid the
1599110d74a9SMichel Lespinasse 		 * pointless overhead of lock_page on the ZERO_PAGE
1600110d74a9SMichel Lespinasse 		 * which might bounce very badly if there is contention.
1601110d74a9SMichel Lespinasse 		 *
1602110d74a9SMichel Lespinasse 		 * If the page is already locked, we don't need to
1603110d74a9SMichel Lespinasse 		 * handle it now - vmscan will handle it later if and
1604110d74a9SMichel Lespinasse 		 * when it attempts to reclaim the page.
1605110d74a9SMichel Lespinasse 		 */
1606110d74a9SMichel Lespinasse 		if (page->mapping && trylock_page(page)) {
1607110d74a9SMichel Lespinasse 			lru_add_drain();  /* push cached pages to LRU */
1608110d74a9SMichel Lespinasse 			/*
1609e6c509f8SHugh Dickins 			 * Because we lock page here, and migration is
1610e6c509f8SHugh Dickins 			 * blocked by the pte's page reference, and we
1611e6c509f8SHugh Dickins 			 * know the page is still mapped, we don't even
1612e6c509f8SHugh Dickins 			 * need to check for file-cache page truncation.
1613110d74a9SMichel Lespinasse 			 */
1614110d74a9SMichel Lespinasse 			mlock_vma_page(page);
1615110d74a9SMichel Lespinasse 			unlock_page(page);
1616110d74a9SMichel Lespinasse 		}
1617110d74a9SMichel Lespinasse 	}
1618deceb6cdSHugh Dickins unlock:
1619deceb6cdSHugh Dickins 	pte_unmap_unlock(ptep, ptl);
16201da177e4SLinus Torvalds out:
1621deceb6cdSHugh Dickins 	return page;
1622deceb6cdSHugh Dickins 
162389f5b7daSLinus Torvalds bad_page:
162489f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
162589f5b7daSLinus Torvalds 	return ERR_PTR(-EFAULT);
162689f5b7daSLinus Torvalds 
162789f5b7daSLinus Torvalds no_page:
162889f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
162989f5b7daSLinus Torvalds 	if (!pte_none(pte))
163089f5b7daSLinus Torvalds 		return page;
16318e4b9a60SHugh Dickins 
1632deceb6cdSHugh Dickins no_page_table:
1633deceb6cdSHugh Dickins 	/*
1634deceb6cdSHugh Dickins 	 * When core dumping an enormous anonymous area that nobody
16358e4b9a60SHugh Dickins 	 * has touched so far, we don't want to allocate unnecessary pages or
16368e4b9a60SHugh Dickins 	 * page tables.  Return error instead of NULL to skip handle_mm_fault,
16378e4b9a60SHugh Dickins 	 * then get_dump_page() will return NULL to leave a hole in the dump.
16388e4b9a60SHugh Dickins 	 * But we can only make this optimization where a hole would surely
16398e4b9a60SHugh Dickins 	 * be zero-filled if handle_mm_fault() actually did handle it.
1640deceb6cdSHugh Dickins 	 */
16418e4b9a60SHugh Dickins 	if ((flags & FOLL_DUMP) &&
16428e4b9a60SHugh Dickins 	    (!vma->vm_ops || !vma->vm_ops->fault))
16438e4b9a60SHugh Dickins 		return ERR_PTR(-EFAULT);
1644deceb6cdSHugh Dickins 	return page;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
164795042f9eSLinus Torvalds static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
164895042f9eSLinus Torvalds {
1649a09a79f6SMikulas Patocka 	return stack_guard_page_start(vma, addr) ||
1650a09a79f6SMikulas Patocka 	       stack_guard_page_end(vma, addr+PAGE_SIZE);
165195042f9eSLinus Torvalds }
165295042f9eSLinus Torvalds 
16530014bd99SHuang Ying /**
16540014bd99SHuang Ying  * __get_user_pages() - pin user pages in memory
16550014bd99SHuang Ying  * @tsk:	task_struct of target task
16560014bd99SHuang Ying  * @mm:		mm_struct of target mm
16570014bd99SHuang Ying  * @start:	starting user address
16580014bd99SHuang Ying  * @nr_pages:	number of pages from start to pin
16590014bd99SHuang Ying  * @gup_flags:	flags modifying pin behaviour
16600014bd99SHuang Ying  * @pages:	array that receives pointers to the pages pinned.
16610014bd99SHuang Ying  *		Should be at least nr_pages long. Or NULL, if caller
16620014bd99SHuang Ying  *		only intends to ensure the pages are faulted in.
16630014bd99SHuang Ying  * @vmas:	array of pointers to vmas corresponding to each page.
16640014bd99SHuang Ying  *		Or NULL if the caller does not require them.
16650014bd99SHuang Ying  * @nonblocking: whether waiting for disk IO or mmap_sem contention
16660014bd99SHuang Ying  *
16670014bd99SHuang Ying  * Returns number of pages pinned. This may be fewer than the number
16680014bd99SHuang Ying  * requested. If nr_pages is 0 or negative, returns 0. If no pages
16690014bd99SHuang Ying  * were pinned, returns -errno. Each page returned must be released
16700014bd99SHuang Ying  * with a put_page() call when it is finished with. vmas will only
16710014bd99SHuang Ying  * remain valid while mmap_sem is held.
16720014bd99SHuang Ying  *
16730014bd99SHuang Ying  * Must be called with mmap_sem held for read or write.
16740014bd99SHuang Ying  *
16750014bd99SHuang Ying  * __get_user_pages walks a process's page tables and takes a reference to
16760014bd99SHuang Ying  * each struct page that each user address corresponds to at a given
16770014bd99SHuang Ying  * instant. That is, it takes the page that would be accessed if a user
16780014bd99SHuang Ying  * thread accesses the given user virtual address at that instant.
16790014bd99SHuang Ying  *
16800014bd99SHuang Ying  * This does not guarantee that the page exists in the user mappings when
16810014bd99SHuang Ying  * __get_user_pages returns, and there may even be a completely different
16820014bd99SHuang Ying  * page there in some cases (eg. if mmapped pagecache has been invalidated
16830014bd99SHuang Ying  * and subsequently re faulted). However it does guarantee that the page
16840014bd99SHuang Ying  * won't be freed completely. And mostly callers simply care that the page
16850014bd99SHuang Ying  * contains data that was valid *at some point in time*. Typically, an IO
16860014bd99SHuang Ying  * or similar operation cannot guarantee anything stronger anyway because
16870014bd99SHuang Ying  * locks can't be held over the syscall boundary.
16880014bd99SHuang Ying  *
16890014bd99SHuang Ying  * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
16900014bd99SHuang Ying  * the page is written to, set_page_dirty (or set_page_dirty_lock, as
16910014bd99SHuang Ying  * appropriate) must be called after the page is finished with, and
16920014bd99SHuang Ying  * before put_page is called.
16930014bd99SHuang Ying  *
16940014bd99SHuang Ying  * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
16950014bd99SHuang Ying  * or mmap_sem contention, and if waiting is needed to pin all pages,
16960014bd99SHuang Ying  * *@nonblocking will be set to 0.
16970014bd99SHuang Ying  *
16980014bd99SHuang Ying  * In most cases, get_user_pages or get_user_pages_fast should be used
16990014bd99SHuang Ying  * instead of __get_user_pages. __get_user_pages should be used only if
17000014bd99SHuang Ying  * you need some special @gup_flags.
17010014bd99SHuang Ying  */
170228a35716SMichel Lespinasse long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
170328a35716SMichel Lespinasse 		unsigned long start, unsigned long nr_pages,
170428a35716SMichel Lespinasse 		unsigned int gup_flags, struct page **pages,
170528a35716SMichel Lespinasse 		struct vm_area_struct **vmas, int *nonblocking)
17061da177e4SLinus Torvalds {
170728a35716SMichel Lespinasse 	long i;
170858fa879eSHugh Dickins 	unsigned long vm_flags;
1709240aadeeSMichel Lespinasse 	unsigned int page_mask;
17101da177e4SLinus Torvalds 
171128a35716SMichel Lespinasse 	if (!nr_pages)
1712900cf086SJonathan Corbet 		return 0;
171358fa879eSHugh Dickins 
171458fa879eSHugh Dickins 	VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
171558fa879eSHugh Dickins 
17161da177e4SLinus Torvalds 	/*
17171da177e4SLinus Torvalds 	 * Require read or write permissions.
171858fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
17191da177e4SLinus Torvalds 	 */
172058fa879eSHugh Dickins 	vm_flags  = (gup_flags & FOLL_WRITE) ?
172158fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
172258fa879eSHugh Dickins 	vm_flags &= (gup_flags & FOLL_FORCE) ?
172358fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
17240b9d7052SAndrea Arcangeli 
17250b9d7052SAndrea Arcangeli 	/*
17260b9d7052SAndrea Arcangeli 	 * If FOLL_FORCE and FOLL_NUMA are both set, handle_mm_fault
17270b9d7052SAndrea Arcangeli 	 * would be called on PROT_NONE ranges. We must never invoke
17280b9d7052SAndrea Arcangeli 	 * handle_mm_fault on PROT_NONE ranges or the NUMA hinting
17290b9d7052SAndrea Arcangeli 	 * page faults would unprotect the PROT_NONE ranges if
17300b9d7052SAndrea Arcangeli 	 * _PAGE_NUMA and _PAGE_PROTNONE are sharing the same pte/pmd
17310b9d7052SAndrea Arcangeli 	 * bitflag. So to avoid that, don't set FOLL_NUMA if
17320b9d7052SAndrea Arcangeli 	 * FOLL_FORCE is set.
17330b9d7052SAndrea Arcangeli 	 */
17340b9d7052SAndrea Arcangeli 	if (!(gup_flags & FOLL_FORCE))
17350b9d7052SAndrea Arcangeli 		gup_flags |= FOLL_NUMA;
17360b9d7052SAndrea Arcangeli 
17371da177e4SLinus Torvalds 	i = 0;
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds 	do {
17401da177e4SLinus Torvalds 		struct vm_area_struct *vma;
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds 		vma = find_extend_vma(mm, start);
1743e7f22e20SStephen Wilson 		if (!vma && in_gate_area(mm, start)) {
17441da177e4SLinus Torvalds 			unsigned long pg = start & PAGE_MASK;
17451da177e4SLinus Torvalds 			pgd_t *pgd;
17461da177e4SLinus Torvalds 			pud_t *pud;
17471da177e4SLinus Torvalds 			pmd_t *pmd;
17481da177e4SLinus Torvalds 			pte_t *pte;
1749b291f000SNick Piggin 
1750b291f000SNick Piggin 			/* user gate pages are read-only */
175158fa879eSHugh Dickins 			if (gup_flags & FOLL_WRITE)
17521da177e4SLinus Torvalds 				return i ? : -EFAULT;
17531da177e4SLinus Torvalds 			if (pg > TASK_SIZE)
17541da177e4SLinus Torvalds 				pgd = pgd_offset_k(pg);
17551da177e4SLinus Torvalds 			else
17561da177e4SLinus Torvalds 				pgd = pgd_offset_gate(mm, pg);
17571da177e4SLinus Torvalds 			BUG_ON(pgd_none(*pgd));
17581da177e4SLinus Torvalds 			pud = pud_offset(pgd, pg);
17591da177e4SLinus Torvalds 			BUG_ON(pud_none(*pud));
17601da177e4SLinus Torvalds 			pmd = pmd_offset(pud, pg);
1761690dbe1cSHugh Dickins 			if (pmd_none(*pmd))
1762690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1763f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
17641da177e4SLinus Torvalds 			pte = pte_offset_map(pmd, pg);
1765690dbe1cSHugh Dickins 			if (pte_none(*pte)) {
1766690dbe1cSHugh Dickins 				pte_unmap(pte);
1767690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1768690dbe1cSHugh Dickins 			}
176995042f9eSLinus Torvalds 			vma = get_gate_vma(mm);
17701da177e4SLinus Torvalds 			if (pages) {
1771de51257aSHugh Dickins 				struct page *page;
1772de51257aSHugh Dickins 
177395042f9eSLinus Torvalds 				page = vm_normal_page(vma, start, *pte);
1774de51257aSHugh Dickins 				if (!page) {
1775de51257aSHugh Dickins 					if (!(gup_flags & FOLL_DUMP) &&
1776de51257aSHugh Dickins 					     is_zero_pfn(pte_pfn(*pte)))
1777de51257aSHugh Dickins 						page = pte_page(*pte);
1778de51257aSHugh Dickins 					else {
1779de51257aSHugh Dickins 						pte_unmap(pte);
1780de51257aSHugh Dickins 						return i ? : -EFAULT;
1781de51257aSHugh Dickins 					}
1782de51257aSHugh Dickins 				}
17836aab341eSLinus Torvalds 				pages[i] = page;
17846aab341eSLinus Torvalds 				get_page(page);
17851da177e4SLinus Torvalds 			}
17861da177e4SLinus Torvalds 			pte_unmap(pte);
1787240aadeeSMichel Lespinasse 			page_mask = 0;
178895042f9eSLinus Torvalds 			goto next_page;
17891da177e4SLinus Torvalds 		}
17901da177e4SLinus Torvalds 
1791b291f000SNick Piggin 		if (!vma ||
1792b291f000SNick Piggin 		    (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
17931c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
17941da177e4SLinus Torvalds 			return i ? : -EFAULT;
17951da177e4SLinus Torvalds 
17962a15efc9SHugh Dickins 		if (is_vm_hugetlb_page(vma)) {
17972a15efc9SHugh Dickins 			i = follow_hugetlb_page(mm, vma, pages, vmas,
179858fa879eSHugh Dickins 					&start, &nr_pages, i, gup_flags);
17992a15efc9SHugh Dickins 			continue;
18002a15efc9SHugh Dickins 		}
1801deceb6cdSHugh Dickins 
18021da177e4SLinus Torvalds 		do {
180308ef4729SHugh Dickins 			struct page *page;
180458fa879eSHugh Dickins 			unsigned int foll_flags = gup_flags;
1805240aadeeSMichel Lespinasse 			unsigned int page_increm;
18061da177e4SLinus Torvalds 
1807462e00ccSEthan Solomita 			/*
18084779280dSYing Han 			 * If we have a pending SIGKILL, don't keep faulting
18091c3aff1cSHugh Dickins 			 * pages and potentially allocating memory.
1810462e00ccSEthan Solomita 			 */
18111c3aff1cSHugh Dickins 			if (unlikely(fatal_signal_pending(current)))
18124779280dSYing Han 				return i ? i : -ERESTARTSYS;
1813462e00ccSEthan Solomita 
1814deceb6cdSHugh Dickins 			cond_resched();
1815240aadeeSMichel Lespinasse 			while (!(page = follow_page_mask(vma, start,
1816240aadeeSMichel Lespinasse 						foll_flags, &page_mask))) {
1817a68d2ebcSLinus Torvalds 				int ret;
181853a7706dSMichel Lespinasse 				unsigned int fault_flags = 0;
181953a7706dSMichel Lespinasse 
1820a09a79f6SMikulas Patocka 				/* For mlock, just skip the stack guard page. */
1821a09a79f6SMikulas Patocka 				if (foll_flags & FOLL_MLOCK) {
1822a09a79f6SMikulas Patocka 					if (stack_guard_page(vma, start))
1823a09a79f6SMikulas Patocka 						goto next_page;
1824a09a79f6SMikulas Patocka 				}
182553a7706dSMichel Lespinasse 				if (foll_flags & FOLL_WRITE)
182653a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_WRITE;
182753a7706dSMichel Lespinasse 				if (nonblocking)
182853a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_ALLOW_RETRY;
1829318b275fSGleb Natapov 				if (foll_flags & FOLL_NOWAIT)
1830318b275fSGleb Natapov 					fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
1831d06063ccSLinus Torvalds 
1832d26ed650SHugh Dickins 				ret = handle_mm_fault(mm, vma, start,
183353a7706dSMichel Lespinasse 							fault_flags);
1834d26ed650SHugh Dickins 
183583c54070SNick Piggin 				if (ret & VM_FAULT_ERROR) {
183683c54070SNick Piggin 					if (ret & VM_FAULT_OOM)
183783c54070SNick Piggin 						return i ? i : -ENOMEM;
183869ebb83eSHuang Ying 					if (ret & (VM_FAULT_HWPOISON |
183969ebb83eSHuang Ying 						   VM_FAULT_HWPOISON_LARGE)) {
184069ebb83eSHuang Ying 						if (i)
184169ebb83eSHuang Ying 							return i;
184269ebb83eSHuang Ying 						else if (gup_flags & FOLL_HWPOISON)
184369ebb83eSHuang Ying 							return -EHWPOISON;
184469ebb83eSHuang Ying 						else
184569ebb83eSHuang Ying 							return -EFAULT;
184669ebb83eSHuang Ying 					}
184769ebb83eSHuang Ying 					if (ret & VM_FAULT_SIGBUS)
184883c54070SNick Piggin 						return i ? i : -EFAULT;
184983c54070SNick Piggin 					BUG();
185083c54070SNick Piggin 				}
1851e7f22e20SStephen Wilson 
1852e7f22e20SStephen Wilson 				if (tsk) {
185383c54070SNick Piggin 					if (ret & VM_FAULT_MAJOR)
185483c54070SNick Piggin 						tsk->maj_flt++;
185583c54070SNick Piggin 					else
185683c54070SNick Piggin 						tsk->min_flt++;
1857e7f22e20SStephen Wilson 				}
185883c54070SNick Piggin 
185953a7706dSMichel Lespinasse 				if (ret & VM_FAULT_RETRY) {
1860318b275fSGleb Natapov 					if (nonblocking)
186153a7706dSMichel Lespinasse 						*nonblocking = 0;
186253a7706dSMichel Lespinasse 					return i;
186353a7706dSMichel Lespinasse 				}
186453a7706dSMichel Lespinasse 
1865f33ea7f4SNick Piggin 				/*
186683c54070SNick Piggin 				 * The VM_FAULT_WRITE bit tells us that
186783c54070SNick Piggin 				 * do_wp_page has broken COW when necessary,
186883c54070SNick Piggin 				 * even if maybe_mkwrite decided not to set
186983c54070SNick Piggin 				 * pte_write. We can thus safely do subsequent
1870878b63acSHugh Dickins 				 * page lookups as if they were reads. But only
1871878b63acSHugh Dickins 				 * do so when looping for pte_write is futile:
1872878b63acSHugh Dickins 				 * in some cases userspace may also be wanting
1873878b63acSHugh Dickins 				 * to write to the gotten user page, which a
1874878b63acSHugh Dickins 				 * read fault here might prevent (a readonly
1875878b63acSHugh Dickins 				 * page might get reCOWed by userspace write).
1876f33ea7f4SNick Piggin 				 */
1877878b63acSHugh Dickins 				if ((ret & VM_FAULT_WRITE) &&
1878878b63acSHugh Dickins 				    !(vma->vm_flags & VM_WRITE))
1879deceb6cdSHugh Dickins 					foll_flags &= ~FOLL_WRITE;
1880a68d2ebcSLinus Torvalds 
18817f7bbbe5SBenjamin Herrenschmidt 				cond_resched();
18821da177e4SLinus Torvalds 			}
188389f5b7daSLinus Torvalds 			if (IS_ERR(page))
188489f5b7daSLinus Torvalds 				return i ? i : PTR_ERR(page);
18851da177e4SLinus Torvalds 			if (pages) {
188608ef4729SHugh Dickins 				pages[i] = page;
188703beb076SJames Bottomley 
1888a6f36be3SRussell King 				flush_anon_page(vma, page, start);
188908ef4729SHugh Dickins 				flush_dcache_page(page);
1890240aadeeSMichel Lespinasse 				page_mask = 0;
18911da177e4SLinus Torvalds 			}
189295042f9eSLinus Torvalds next_page:
1893240aadeeSMichel Lespinasse 			if (vmas) {
18941da177e4SLinus Torvalds 				vmas[i] = vma;
1895240aadeeSMichel Lespinasse 				page_mask = 0;
1896240aadeeSMichel Lespinasse 			}
1897240aadeeSMichel Lespinasse 			page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
1898240aadeeSMichel Lespinasse 			if (page_increm > nr_pages)
1899240aadeeSMichel Lespinasse 				page_increm = nr_pages;
1900240aadeeSMichel Lespinasse 			i += page_increm;
1901240aadeeSMichel Lespinasse 			start += page_increm * PAGE_SIZE;
1902240aadeeSMichel Lespinasse 			nr_pages -= page_increm;
19039d73777eSPeter Zijlstra 		} while (nr_pages && start < vma->vm_end);
19049d73777eSPeter Zijlstra 	} while (nr_pages);
19051da177e4SLinus Torvalds 	return i;
19061da177e4SLinus Torvalds }
19070014bd99SHuang Ying EXPORT_SYMBOL(__get_user_pages);
1908b291f000SNick Piggin 
19092efaca92SBenjamin Herrenschmidt /*
19102efaca92SBenjamin Herrenschmidt  * fixup_user_fault() - manually resolve a user page fault
19112efaca92SBenjamin Herrenschmidt  * @tsk:	the task_struct to use for page fault accounting, or
19122efaca92SBenjamin Herrenschmidt  *		NULL if faults are not to be recorded.
19132efaca92SBenjamin Herrenschmidt  * @mm:		mm_struct of target mm
19142efaca92SBenjamin Herrenschmidt  * @address:	user address
19152efaca92SBenjamin Herrenschmidt  * @fault_flags:flags to pass down to handle_mm_fault()
19162efaca92SBenjamin Herrenschmidt  *
19172efaca92SBenjamin Herrenschmidt  * This is meant to be called in the specific scenario where for locking reasons
19182efaca92SBenjamin Herrenschmidt  * we try to access user memory in atomic context (within a pagefault_disable()
19192efaca92SBenjamin Herrenschmidt  * section), this returns -EFAULT, and we want to resolve the user fault before
19202efaca92SBenjamin Herrenschmidt  * trying again.
19212efaca92SBenjamin Herrenschmidt  *
19222efaca92SBenjamin Herrenschmidt  * Typically this is meant to be used by the futex code.
19232efaca92SBenjamin Herrenschmidt  *
19242efaca92SBenjamin Herrenschmidt  * The main difference with get_user_pages() is that this function will
19252efaca92SBenjamin Herrenschmidt  * unconditionally call handle_mm_fault() which will in turn perform all the
19262efaca92SBenjamin Herrenschmidt  * necessary SW fixup of the dirty and young bits in the PTE, while
19272efaca92SBenjamin Herrenschmidt  * handle_mm_fault() only guarantees to update these in the struct page.
19282efaca92SBenjamin Herrenschmidt  *
19292efaca92SBenjamin Herrenschmidt  * This is important for some architectures where those bits also gate the
19302efaca92SBenjamin Herrenschmidt  * access permission to the page because they are maintained in software.  On
19312efaca92SBenjamin Herrenschmidt  * such architectures, gup() will not be enough to make a subsequent access
19322efaca92SBenjamin Herrenschmidt  * succeed.
19332efaca92SBenjamin Herrenschmidt  *
19342efaca92SBenjamin Herrenschmidt  * This should be called with the mm_sem held for read.
19352efaca92SBenjamin Herrenschmidt  */
19362efaca92SBenjamin Herrenschmidt int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
19372efaca92SBenjamin Herrenschmidt 		     unsigned long address, unsigned int fault_flags)
19382efaca92SBenjamin Herrenschmidt {
19392efaca92SBenjamin Herrenschmidt 	struct vm_area_struct *vma;
19402efaca92SBenjamin Herrenschmidt 	int ret;
19412efaca92SBenjamin Herrenschmidt 
19422efaca92SBenjamin Herrenschmidt 	vma = find_extend_vma(mm, address);
19432efaca92SBenjamin Herrenschmidt 	if (!vma || address < vma->vm_start)
19442efaca92SBenjamin Herrenschmidt 		return -EFAULT;
19452efaca92SBenjamin Herrenschmidt 
19462efaca92SBenjamin Herrenschmidt 	ret = handle_mm_fault(mm, vma, address, fault_flags);
19472efaca92SBenjamin Herrenschmidt 	if (ret & VM_FAULT_ERROR) {
19482efaca92SBenjamin Herrenschmidt 		if (ret & VM_FAULT_OOM)
19492efaca92SBenjamin Herrenschmidt 			return -ENOMEM;
19502efaca92SBenjamin Herrenschmidt 		if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
19512efaca92SBenjamin Herrenschmidt 			return -EHWPOISON;
19522efaca92SBenjamin Herrenschmidt 		if (ret & VM_FAULT_SIGBUS)
19532efaca92SBenjamin Herrenschmidt 			return -EFAULT;
19542efaca92SBenjamin Herrenschmidt 		BUG();
19552efaca92SBenjamin Herrenschmidt 	}
19562efaca92SBenjamin Herrenschmidt 	if (tsk) {
19572efaca92SBenjamin Herrenschmidt 		if (ret & VM_FAULT_MAJOR)
19582efaca92SBenjamin Herrenschmidt 			tsk->maj_flt++;
19592efaca92SBenjamin Herrenschmidt 		else
19602efaca92SBenjamin Herrenschmidt 			tsk->min_flt++;
19612efaca92SBenjamin Herrenschmidt 	}
19622efaca92SBenjamin Herrenschmidt 	return 0;
19632efaca92SBenjamin Herrenschmidt }
19642efaca92SBenjamin Herrenschmidt 
19652efaca92SBenjamin Herrenschmidt /*
1966d2bf6be8SNick Piggin  * get_user_pages() - pin user pages in memory
1967e7f22e20SStephen Wilson  * @tsk:	the task_struct to use for page fault accounting, or
1968e7f22e20SStephen Wilson  *		NULL if faults are not to be recorded.
1969d2bf6be8SNick Piggin  * @mm:		mm_struct of target mm
1970d2bf6be8SNick Piggin  * @start:	starting user address
19719d73777eSPeter Zijlstra  * @nr_pages:	number of pages from start to pin
1972d2bf6be8SNick Piggin  * @write:	whether pages will be written to by the caller
1973d2bf6be8SNick Piggin  * @force:	whether to force write access even if user mapping is
1974d2bf6be8SNick Piggin  *		readonly. This will result in the page being COWed even
1975d2bf6be8SNick Piggin  *		in MAP_SHARED mappings. You do not want this.
1976d2bf6be8SNick Piggin  * @pages:	array that receives pointers to the pages pinned.
1977d2bf6be8SNick Piggin  *		Should be at least nr_pages long. Or NULL, if caller
1978d2bf6be8SNick Piggin  *		only intends to ensure the pages are faulted in.
1979d2bf6be8SNick Piggin  * @vmas:	array of pointers to vmas corresponding to each page.
1980d2bf6be8SNick Piggin  *		Or NULL if the caller does not require them.
1981d2bf6be8SNick Piggin  *
1982d2bf6be8SNick Piggin  * Returns number of pages pinned. This may be fewer than the number
19839d73777eSPeter Zijlstra  * requested. If nr_pages is 0 or negative, returns 0. If no pages
1984d2bf6be8SNick Piggin  * were pinned, returns -errno. Each page returned must be released
1985d2bf6be8SNick Piggin  * with a put_page() call when it is finished with. vmas will only
1986d2bf6be8SNick Piggin  * remain valid while mmap_sem is held.
1987d2bf6be8SNick Piggin  *
1988d2bf6be8SNick Piggin  * Must be called with mmap_sem held for read or write.
1989d2bf6be8SNick Piggin  *
1990d2bf6be8SNick Piggin  * get_user_pages walks a process's page tables and takes a reference to
1991d2bf6be8SNick Piggin  * each struct page that each user address corresponds to at a given
1992d2bf6be8SNick Piggin  * instant. That is, it takes the page that would be accessed if a user
1993d2bf6be8SNick Piggin  * thread accesses the given user virtual address at that instant.
1994d2bf6be8SNick Piggin  *
1995d2bf6be8SNick Piggin  * This does not guarantee that the page exists in the user mappings when
1996d2bf6be8SNick Piggin  * get_user_pages returns, and there may even be a completely different
1997d2bf6be8SNick Piggin  * page there in some cases (eg. if mmapped pagecache has been invalidated
1998d2bf6be8SNick Piggin  * and subsequently re faulted). However it does guarantee that the page
1999d2bf6be8SNick Piggin  * won't be freed completely. And mostly callers simply care that the page
2000d2bf6be8SNick Piggin  * contains data that was valid *at some point in time*. Typically, an IO
2001d2bf6be8SNick Piggin  * or similar operation cannot guarantee anything stronger anyway because
2002d2bf6be8SNick Piggin  * locks can't be held over the syscall boundary.
2003d2bf6be8SNick Piggin  *
2004d2bf6be8SNick Piggin  * If write=0, the page must not be written to. If the page is written to,
2005d2bf6be8SNick Piggin  * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
2006d2bf6be8SNick Piggin  * after the page is finished with, and before put_page is called.
2007d2bf6be8SNick Piggin  *
2008d2bf6be8SNick Piggin  * get_user_pages is typically used for fewer-copy IO operations, to get a
2009d2bf6be8SNick Piggin  * handle on the memory by some means other than accesses via the user virtual
2010d2bf6be8SNick Piggin  * addresses. The pages may be submitted for DMA to devices or accessed via
2011d2bf6be8SNick Piggin  * their kernel linear mapping (via the kmap APIs). Care should be taken to
2012d2bf6be8SNick Piggin  * use the correct cache flushing APIs.
2013d2bf6be8SNick Piggin  *
2014d2bf6be8SNick Piggin  * See also get_user_pages_fast, for performance critical applications.
2015d2bf6be8SNick Piggin  */
201628a35716SMichel Lespinasse long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
201728a35716SMichel Lespinasse 		unsigned long start, unsigned long nr_pages, int write,
201828a35716SMichel Lespinasse 		int force, struct page **pages, struct vm_area_struct **vmas)
2019b291f000SNick Piggin {
202058fa879eSHugh Dickins 	int flags = FOLL_TOUCH;
2021b291f000SNick Piggin 
202258fa879eSHugh Dickins 	if (pages)
202358fa879eSHugh Dickins 		flags |= FOLL_GET;
2024b291f000SNick Piggin 	if (write)
202558fa879eSHugh Dickins 		flags |= FOLL_WRITE;
2026b291f000SNick Piggin 	if (force)
202758fa879eSHugh Dickins 		flags |= FOLL_FORCE;
2028b291f000SNick Piggin 
202953a7706dSMichel Lespinasse 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
203053a7706dSMichel Lespinasse 				NULL);
2031b291f000SNick Piggin }
20321da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages);
20331da177e4SLinus Torvalds 
2034f3e8fccdSHugh Dickins /**
2035f3e8fccdSHugh Dickins  * get_dump_page() - pin user page in memory while writing it to core dump
2036f3e8fccdSHugh Dickins  * @addr: user address
2037f3e8fccdSHugh Dickins  *
2038f3e8fccdSHugh Dickins  * Returns struct page pointer of user page pinned for dump,
2039f3e8fccdSHugh Dickins  * to be freed afterwards by page_cache_release() or put_page().
2040f3e8fccdSHugh Dickins  *
2041f3e8fccdSHugh Dickins  * Returns NULL on any kind of failure - a hole must then be inserted into
2042f3e8fccdSHugh Dickins  * the corefile, to preserve alignment with its headers; and also returns
2043f3e8fccdSHugh Dickins  * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
2044f3e8fccdSHugh Dickins  * allowing a hole to be left in the corefile to save diskspace.
2045f3e8fccdSHugh Dickins  *
2046f3e8fccdSHugh Dickins  * Called without mmap_sem, but after all other threads have been killed.
2047f3e8fccdSHugh Dickins  */
2048f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE
2049f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr)
2050f3e8fccdSHugh Dickins {
2051f3e8fccdSHugh Dickins 	struct vm_area_struct *vma;
2052f3e8fccdSHugh Dickins 	struct page *page;
2053f3e8fccdSHugh Dickins 
2054f3e8fccdSHugh Dickins 	if (__get_user_pages(current, current->mm, addr, 1,
205553a7706dSMichel Lespinasse 			     FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
205653a7706dSMichel Lespinasse 			     NULL) < 1)
2057f3e8fccdSHugh Dickins 		return NULL;
2058f3e8fccdSHugh Dickins 	flush_cache_page(vma, addr, page_to_pfn(page));
2059f3e8fccdSHugh Dickins 	return page;
2060f3e8fccdSHugh Dickins }
2061f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */
2062f3e8fccdSHugh Dickins 
206325ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
2064920c7a5dSHarvey Harrison 			spinlock_t **ptl)
2065c9cfcddfSLinus Torvalds {
2066c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
2067c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
2068c9cfcddfSLinus Torvalds 	if (pud) {
206949c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
2070f66055abSAndrea Arcangeli 		if (pmd) {
2071f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
2072c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
2073c9cfcddfSLinus Torvalds 		}
2074f66055abSAndrea Arcangeli 	}
2075c9cfcddfSLinus Torvalds 	return NULL;
2076c9cfcddfSLinus Torvalds }
2077c9cfcddfSLinus Torvalds 
20781da177e4SLinus Torvalds /*
2079238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
2080238f58d8SLinus Torvalds  *
2081238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
2082238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
2083238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
2084238f58d8SLinus Torvalds  */
2085423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr,
2086423bad60SNick Piggin 			struct page *page, pgprot_t prot)
2087238f58d8SLinus Torvalds {
2088423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
2089238f58d8SLinus Torvalds 	int retval;
2090238f58d8SLinus Torvalds 	pte_t *pte;
2091238f58d8SLinus Torvalds 	spinlock_t *ptl;
2092238f58d8SLinus Torvalds 
2093238f58d8SLinus Torvalds 	retval = -EINVAL;
2094a145dd41SLinus Torvalds 	if (PageAnon(page))
20955b4e655eSKAMEZAWA Hiroyuki 		goto out;
2096238f58d8SLinus Torvalds 	retval = -ENOMEM;
2097238f58d8SLinus Torvalds 	flush_dcache_page(page);
2098c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
2099238f58d8SLinus Torvalds 	if (!pte)
21005b4e655eSKAMEZAWA Hiroyuki 		goto out;
2101238f58d8SLinus Torvalds 	retval = -EBUSY;
2102238f58d8SLinus Torvalds 	if (!pte_none(*pte))
2103238f58d8SLinus Torvalds 		goto out_unlock;
2104238f58d8SLinus Torvalds 
2105238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
2106238f58d8SLinus Torvalds 	get_page(page);
210734e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_FILEPAGES);
2108238f58d8SLinus Torvalds 	page_add_file_rmap(page);
2109238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
2110238f58d8SLinus Torvalds 
2111238f58d8SLinus Torvalds 	retval = 0;
21128a9f3ccdSBalbir Singh 	pte_unmap_unlock(pte, ptl);
21138a9f3ccdSBalbir Singh 	return retval;
2114238f58d8SLinus Torvalds out_unlock:
2115238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
2116238f58d8SLinus Torvalds out:
2117238f58d8SLinus Torvalds 	return retval;
2118238f58d8SLinus Torvalds }
2119238f58d8SLinus Torvalds 
2120bfa5bf6dSRolf Eike Beer /**
2121bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
2122bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
2123bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
2124bfa5bf6dSRolf Eike Beer  * @page: source kernel page
2125bfa5bf6dSRolf Eike Beer  *
2126a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
2127a145dd41SLinus Torvalds  * into a user vma.
2128a145dd41SLinus Torvalds  *
2129a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
2130a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
2131a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
21328dfcc9baSNick Piggin  * (see split_page()).
2133a145dd41SLinus Torvalds  *
2134a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
2135a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
2136a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
2137a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
2138a145dd41SLinus Torvalds  * ask for a shared writable mapping!
2139a145dd41SLinus Torvalds  *
2140a145dd41SLinus Torvalds  * The page does not need to be reserved.
21414b6e1e37SKonstantin Khlebnikov  *
21424b6e1e37SKonstantin Khlebnikov  * Usually this function is called from f_op->mmap() handler
21434b6e1e37SKonstantin Khlebnikov  * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
21444b6e1e37SKonstantin Khlebnikov  * Caller must set VM_MIXEDMAP on vma if it wants to call this
21454b6e1e37SKonstantin Khlebnikov  * function from other places, for example from page-fault handler.
2146a145dd41SLinus Torvalds  */
2147423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
2148423bad60SNick Piggin 			struct page *page)
2149a145dd41SLinus Torvalds {
2150a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
2151a145dd41SLinus Torvalds 		return -EFAULT;
2152a145dd41SLinus Torvalds 	if (!page_count(page))
2153a145dd41SLinus Torvalds 		return -EINVAL;
21544b6e1e37SKonstantin Khlebnikov 	if (!(vma->vm_flags & VM_MIXEDMAP)) {
21554b6e1e37SKonstantin Khlebnikov 		BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
21564b6e1e37SKonstantin Khlebnikov 		BUG_ON(vma->vm_flags & VM_PFNMAP);
21574b6e1e37SKonstantin Khlebnikov 		vma->vm_flags |= VM_MIXEDMAP;
21584b6e1e37SKonstantin Khlebnikov 	}
2159423bad60SNick Piggin 	return insert_page(vma, addr, page, vma->vm_page_prot);
2160a145dd41SLinus Torvalds }
2161e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
2162a145dd41SLinus Torvalds 
2163423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2164423bad60SNick Piggin 			unsigned long pfn, pgprot_t prot)
2165423bad60SNick Piggin {
2166423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
2167423bad60SNick Piggin 	int retval;
2168423bad60SNick Piggin 	pte_t *pte, entry;
2169423bad60SNick Piggin 	spinlock_t *ptl;
2170423bad60SNick Piggin 
2171423bad60SNick Piggin 	retval = -ENOMEM;
2172423bad60SNick Piggin 	pte = get_locked_pte(mm, addr, &ptl);
2173423bad60SNick Piggin 	if (!pte)
2174423bad60SNick Piggin 		goto out;
2175423bad60SNick Piggin 	retval = -EBUSY;
2176423bad60SNick Piggin 	if (!pte_none(*pte))
2177423bad60SNick Piggin 		goto out_unlock;
2178423bad60SNick Piggin 
2179423bad60SNick Piggin 	/* Ok, finally just insert the thing.. */
2180423bad60SNick Piggin 	entry = pte_mkspecial(pfn_pte(pfn, prot));
2181423bad60SNick Piggin 	set_pte_at(mm, addr, pte, entry);
21824b3073e1SRussell King 	update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
2183423bad60SNick Piggin 
2184423bad60SNick Piggin 	retval = 0;
2185423bad60SNick Piggin out_unlock:
2186423bad60SNick Piggin 	pte_unmap_unlock(pte, ptl);
2187423bad60SNick Piggin out:
2188423bad60SNick Piggin 	return retval;
2189423bad60SNick Piggin }
2190423bad60SNick Piggin 
2191e0dc0d8fSNick Piggin /**
2192e0dc0d8fSNick Piggin  * vm_insert_pfn - insert single pfn into user vma
2193e0dc0d8fSNick Piggin  * @vma: user vma to map to
2194e0dc0d8fSNick Piggin  * @addr: target user address of this page
2195e0dc0d8fSNick Piggin  * @pfn: source kernel pfn
2196e0dc0d8fSNick Piggin  *
2197c462f179SRobert P. J. Day  * Similar to vm_insert_page, this allows drivers to insert individual pages
2198e0dc0d8fSNick Piggin  * they've allocated into a user vma. Same comments apply.
2199e0dc0d8fSNick Piggin  *
2200e0dc0d8fSNick Piggin  * This function should only be called from a vm_ops->fault handler, and
2201e0dc0d8fSNick Piggin  * in that case the handler should return NULL.
22020d71d10aSNick Piggin  *
22030d71d10aSNick Piggin  * vma cannot be a COW mapping.
22040d71d10aSNick Piggin  *
22050d71d10aSNick Piggin  * As this is called only for pages that do not currently exist, we
22060d71d10aSNick Piggin  * do not need to flush old virtual caches or the TLB.
2207e0dc0d8fSNick Piggin  */
2208e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2209e0dc0d8fSNick Piggin 			unsigned long pfn)
2210e0dc0d8fSNick Piggin {
22112ab64037Svenkatesh.pallipadi@intel.com 	int ret;
2212e4b866edSvenkatesh.pallipadi@intel.com 	pgprot_t pgprot = vma->vm_page_prot;
22137e675137SNick Piggin 	/*
22147e675137SNick Piggin 	 * Technically, architectures with pte_special can avoid all these
22157e675137SNick Piggin 	 * restrictions (same for remap_pfn_range).  However we would like
22167e675137SNick Piggin 	 * consistency in testing and feature parity among all, so we should
22177e675137SNick Piggin 	 * try to keep these invariants in place for everybody.
22187e675137SNick Piggin 	 */
2219b379d790SJared Hulbert 	BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
2220b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
2221b379d790SJared Hulbert 						(VM_PFNMAP|VM_MIXEDMAP));
2222b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
2223b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
2224e0dc0d8fSNick Piggin 
2225423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
2226423bad60SNick Piggin 		return -EFAULT;
22275180da41SSuresh Siddha 	if (track_pfn_insert(vma, &pgprot, pfn))
22282ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
22292ab64037Svenkatesh.pallipadi@intel.com 
2230e4b866edSvenkatesh.pallipadi@intel.com 	ret = insert_pfn(vma, addr, pfn, pgprot);
22312ab64037Svenkatesh.pallipadi@intel.com 
22322ab64037Svenkatesh.pallipadi@intel.com 	return ret;
2233e0dc0d8fSNick Piggin }
2234e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn);
2235e0dc0d8fSNick Piggin 
2236423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2237423bad60SNick Piggin 			unsigned long pfn)
2238423bad60SNick Piggin {
2239423bad60SNick Piggin 	BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
2240423bad60SNick Piggin 
2241423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
2242423bad60SNick Piggin 		return -EFAULT;
2243423bad60SNick Piggin 
2244423bad60SNick Piggin 	/*
2245423bad60SNick Piggin 	 * If we don't have pte special, then we have to use the pfn_valid()
2246423bad60SNick Piggin 	 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
2247423bad60SNick Piggin 	 * refcount the page if pfn_valid is true (hence insert_page rather
224862eede62SHugh Dickins 	 * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
224962eede62SHugh Dickins 	 * without pte special, it would there be refcounted as a normal page.
2250423bad60SNick Piggin 	 */
2251423bad60SNick Piggin 	if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
2252423bad60SNick Piggin 		struct page *page;
2253423bad60SNick Piggin 
2254423bad60SNick Piggin 		page = pfn_to_page(pfn);
2255423bad60SNick Piggin 		return insert_page(vma, addr, page, vma->vm_page_prot);
2256423bad60SNick Piggin 	}
2257423bad60SNick Piggin 	return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
2258423bad60SNick Piggin }
2259423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed);
2260423bad60SNick Piggin 
2261a145dd41SLinus Torvalds /*
22621da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
22631da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
22641da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
22651da177e4SLinus Torvalds  */
22661da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
22671da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
22681da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
22691da177e4SLinus Torvalds {
22701da177e4SLinus Torvalds 	pte_t *pte;
2271c74df32cSHugh Dickins 	spinlock_t *ptl;
22721da177e4SLinus Torvalds 
2273c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
22741da177e4SLinus Torvalds 	if (!pte)
22751da177e4SLinus Torvalds 		return -ENOMEM;
22766606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
22771da177e4SLinus Torvalds 	do {
22781da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
22797e675137SNick Piggin 		set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
22801da177e4SLinus Torvalds 		pfn++;
22811da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
22826606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
2283c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
22841da177e4SLinus Torvalds 	return 0;
22851da177e4SLinus Torvalds }
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
22881da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
22891da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
22901da177e4SLinus Torvalds {
22911da177e4SLinus Torvalds 	pmd_t *pmd;
22921da177e4SLinus Torvalds 	unsigned long next;
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
22951da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
22961da177e4SLinus Torvalds 	if (!pmd)
22971da177e4SLinus Torvalds 		return -ENOMEM;
2298f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
22991da177e4SLinus Torvalds 	do {
23001da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
23011da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
23021da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
23031da177e4SLinus Torvalds 			return -ENOMEM;
23041da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
23051da177e4SLinus Torvalds 	return 0;
23061da177e4SLinus Torvalds }
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
23091da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
23101da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
23111da177e4SLinus Torvalds {
23121da177e4SLinus Torvalds 	pud_t *pud;
23131da177e4SLinus Torvalds 	unsigned long next;
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
23161da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
23171da177e4SLinus Torvalds 	if (!pud)
23181da177e4SLinus Torvalds 		return -ENOMEM;
23191da177e4SLinus Torvalds 	do {
23201da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
23211da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
23221da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
23231da177e4SLinus Torvalds 			return -ENOMEM;
23241da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
23251da177e4SLinus Torvalds 	return 0;
23261da177e4SLinus Torvalds }
23271da177e4SLinus Torvalds 
2328bfa5bf6dSRolf Eike Beer /**
2329bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
2330bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
2331bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
2332bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
2333bfa5bf6dSRolf Eike Beer  * @size: size of map area
2334bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
2335bfa5bf6dSRolf Eike Beer  *
2336bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
2337bfa5bf6dSRolf Eike Beer  */
23381da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
23391da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
23401da177e4SLinus Torvalds {
23411da177e4SLinus Torvalds 	pgd_t *pgd;
23421da177e4SLinus Torvalds 	unsigned long next;
23432d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
23441da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
23451da177e4SLinus Torvalds 	int err;
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds 	/*
23481da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
23491da177e4SLinus Torvalds 	 * rest of the world about it:
23501da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
23511da177e4SLinus Torvalds 	 *	(accesses can have side effects).
23526aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
23536aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
23546aab341eSLinus Torvalds 	 *	with them.
2355314e51b9SKonstantin Khlebnikov 	 *   VM_DONTEXPAND
2356314e51b9SKonstantin Khlebnikov 	 *      Disable vma merging and expanding with mremap().
2357314e51b9SKonstantin Khlebnikov 	 *   VM_DONTDUMP
2358314e51b9SKonstantin Khlebnikov 	 *      Omit vma from core dump, even when VM_IO turned off.
2359fb155c16SLinus Torvalds 	 *
2360fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
2361fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
2362fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
2363b3b9c293SKonstantin Khlebnikov 	 * See vm_normal_page() for details.
23641da177e4SLinus Torvalds 	 */
2365b3b9c293SKonstantin Khlebnikov 	if (is_cow_mapping(vma->vm_flags)) {
2366b3b9c293SKonstantin Khlebnikov 		if (addr != vma->vm_start || end != vma->vm_end)
2367b3b9c293SKonstantin Khlebnikov 			return -EINVAL;
23686aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
2369b3b9c293SKonstantin Khlebnikov 	}
2370b3b9c293SKonstantin Khlebnikov 
2371b3b9c293SKonstantin Khlebnikov 	err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
2372b3b9c293SKonstantin Khlebnikov 	if (err)
23733c8bb73aSvenkatesh.pallipadi@intel.com 		return -EINVAL;
2374fb155c16SLinus Torvalds 
2375314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds 	BUG_ON(addr >= end);
23781da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
23791da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
23801da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
23811da177e4SLinus Torvalds 	do {
23821da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
23831da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
23841da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
23851da177e4SLinus Torvalds 		if (err)
23861da177e4SLinus Torvalds 			break;
23871da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
23882ab64037Svenkatesh.pallipadi@intel.com 
23892ab64037Svenkatesh.pallipadi@intel.com 	if (err)
23905180da41SSuresh Siddha 		untrack_pfn(vma, pfn, PAGE_ALIGN(size));
23912ab64037Svenkatesh.pallipadi@intel.com 
23921da177e4SLinus Torvalds 	return err;
23931da177e4SLinus Torvalds }
23941da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
23951da177e4SLinus Torvalds 
2396b4cbb197SLinus Torvalds /**
2397b4cbb197SLinus Torvalds  * vm_iomap_memory - remap memory to userspace
2398b4cbb197SLinus Torvalds  * @vma: user vma to map to
2399b4cbb197SLinus Torvalds  * @start: start of area
2400b4cbb197SLinus Torvalds  * @len: size of area
2401b4cbb197SLinus Torvalds  *
2402b4cbb197SLinus Torvalds  * This is a simplified io_remap_pfn_range() for common driver use. The
2403b4cbb197SLinus Torvalds  * driver just needs to give us the physical memory range to be mapped,
2404b4cbb197SLinus Torvalds  * we'll figure out the rest from the vma information.
2405b4cbb197SLinus Torvalds  *
2406b4cbb197SLinus Torvalds  * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2407b4cbb197SLinus Torvalds  * whatever write-combining details or similar.
2408b4cbb197SLinus Torvalds  */
2409b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2410b4cbb197SLinus Torvalds {
2411b4cbb197SLinus Torvalds 	unsigned long vm_len, pfn, pages;
2412b4cbb197SLinus Torvalds 
2413b4cbb197SLinus Torvalds 	/* Check that the physical memory area passed in looks valid */
2414b4cbb197SLinus Torvalds 	if (start + len < start)
2415b4cbb197SLinus Torvalds 		return -EINVAL;
2416b4cbb197SLinus Torvalds 	/*
2417b4cbb197SLinus Torvalds 	 * You *really* shouldn't map things that aren't page-aligned,
2418b4cbb197SLinus Torvalds 	 * but we've historically allowed it because IO memory might
2419b4cbb197SLinus Torvalds 	 * just have smaller alignment.
2420b4cbb197SLinus Torvalds 	 */
2421b4cbb197SLinus Torvalds 	len += start & ~PAGE_MASK;
2422b4cbb197SLinus Torvalds 	pfn = start >> PAGE_SHIFT;
2423b4cbb197SLinus Torvalds 	pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2424b4cbb197SLinus Torvalds 	if (pfn + pages < pfn)
2425b4cbb197SLinus Torvalds 		return -EINVAL;
2426b4cbb197SLinus Torvalds 
2427b4cbb197SLinus Torvalds 	/* We start the mapping 'vm_pgoff' pages into the area */
2428b4cbb197SLinus Torvalds 	if (vma->vm_pgoff > pages)
2429b4cbb197SLinus Torvalds 		return -EINVAL;
2430b4cbb197SLinus Torvalds 	pfn += vma->vm_pgoff;
2431b4cbb197SLinus Torvalds 	pages -= vma->vm_pgoff;
2432b4cbb197SLinus Torvalds 
2433b4cbb197SLinus Torvalds 	/* Can we fit all of the mapping? */
2434b4cbb197SLinus Torvalds 	vm_len = vma->vm_end - vma->vm_start;
2435b4cbb197SLinus Torvalds 	if (vm_len >> PAGE_SHIFT > pages)
2436b4cbb197SLinus Torvalds 		return -EINVAL;
2437b4cbb197SLinus Torvalds 
2438b4cbb197SLinus Torvalds 	/* Ok, let it rip */
2439b4cbb197SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2440b4cbb197SLinus Torvalds }
2441b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
2442b4cbb197SLinus Torvalds 
2443aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2444aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2445aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2446aee16b3cSJeremy Fitzhardinge {
2447aee16b3cSJeremy Fitzhardinge 	pte_t *pte;
2448aee16b3cSJeremy Fitzhardinge 	int err;
24492f569afdSMartin Schwidefsky 	pgtable_t token;
245094909914SBorislav Petkov 	spinlock_t *uninitialized_var(ptl);
2451aee16b3cSJeremy Fitzhardinge 
2452aee16b3cSJeremy Fitzhardinge 	pte = (mm == &init_mm) ?
2453aee16b3cSJeremy Fitzhardinge 		pte_alloc_kernel(pmd, addr) :
2454aee16b3cSJeremy Fitzhardinge 		pte_alloc_map_lock(mm, pmd, addr, &ptl);
2455aee16b3cSJeremy Fitzhardinge 	if (!pte)
2456aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2457aee16b3cSJeremy Fitzhardinge 
2458aee16b3cSJeremy Fitzhardinge 	BUG_ON(pmd_huge(*pmd));
2459aee16b3cSJeremy Fitzhardinge 
246038e0edb1SJeremy Fitzhardinge 	arch_enter_lazy_mmu_mode();
246138e0edb1SJeremy Fitzhardinge 
24622f569afdSMartin Schwidefsky 	token = pmd_pgtable(*pmd);
2463aee16b3cSJeremy Fitzhardinge 
2464aee16b3cSJeremy Fitzhardinge 	do {
2465c36987e2SDaisuke Nishimura 		err = fn(pte++, token, addr, data);
2466aee16b3cSJeremy Fitzhardinge 		if (err)
2467aee16b3cSJeremy Fitzhardinge 			break;
2468c36987e2SDaisuke Nishimura 	} while (addr += PAGE_SIZE, addr != end);
2469aee16b3cSJeremy Fitzhardinge 
247038e0edb1SJeremy Fitzhardinge 	arch_leave_lazy_mmu_mode();
247138e0edb1SJeremy Fitzhardinge 
2472aee16b3cSJeremy Fitzhardinge 	if (mm != &init_mm)
2473aee16b3cSJeremy Fitzhardinge 		pte_unmap_unlock(pte-1, ptl);
2474aee16b3cSJeremy Fitzhardinge 	return err;
2475aee16b3cSJeremy Fitzhardinge }
2476aee16b3cSJeremy Fitzhardinge 
2477aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2478aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2479aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2480aee16b3cSJeremy Fitzhardinge {
2481aee16b3cSJeremy Fitzhardinge 	pmd_t *pmd;
2482aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2483aee16b3cSJeremy Fitzhardinge 	int err;
2484aee16b3cSJeremy Fitzhardinge 
2485ceb86879SAndi Kleen 	BUG_ON(pud_huge(*pud));
2486ceb86879SAndi Kleen 
2487aee16b3cSJeremy Fitzhardinge 	pmd = pmd_alloc(mm, pud, addr);
2488aee16b3cSJeremy Fitzhardinge 	if (!pmd)
2489aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2490aee16b3cSJeremy Fitzhardinge 	do {
2491aee16b3cSJeremy Fitzhardinge 		next = pmd_addr_end(addr, end);
2492aee16b3cSJeremy Fitzhardinge 		err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2493aee16b3cSJeremy Fitzhardinge 		if (err)
2494aee16b3cSJeremy Fitzhardinge 			break;
2495aee16b3cSJeremy Fitzhardinge 	} while (pmd++, addr = next, addr != end);
2496aee16b3cSJeremy Fitzhardinge 	return err;
2497aee16b3cSJeremy Fitzhardinge }
2498aee16b3cSJeremy Fitzhardinge 
2499aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
2500aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2501aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2502aee16b3cSJeremy Fitzhardinge {
2503aee16b3cSJeremy Fitzhardinge 	pud_t *pud;
2504aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2505aee16b3cSJeremy Fitzhardinge 	int err;
2506aee16b3cSJeremy Fitzhardinge 
2507aee16b3cSJeremy Fitzhardinge 	pud = pud_alloc(mm, pgd, addr);
2508aee16b3cSJeremy Fitzhardinge 	if (!pud)
2509aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2510aee16b3cSJeremy Fitzhardinge 	do {
2511aee16b3cSJeremy Fitzhardinge 		next = pud_addr_end(addr, end);
2512aee16b3cSJeremy Fitzhardinge 		err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2513aee16b3cSJeremy Fitzhardinge 		if (err)
2514aee16b3cSJeremy Fitzhardinge 			break;
2515aee16b3cSJeremy Fitzhardinge 	} while (pud++, addr = next, addr != end);
2516aee16b3cSJeremy Fitzhardinge 	return err;
2517aee16b3cSJeremy Fitzhardinge }
2518aee16b3cSJeremy Fitzhardinge 
2519aee16b3cSJeremy Fitzhardinge /*
2520aee16b3cSJeremy Fitzhardinge  * Scan a region of virtual memory, filling in page tables as necessary
2521aee16b3cSJeremy Fitzhardinge  * and calling a provided function on each leaf page table.
2522aee16b3cSJeremy Fitzhardinge  */
2523aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2524aee16b3cSJeremy Fitzhardinge 			unsigned long size, pte_fn_t fn, void *data)
2525aee16b3cSJeremy Fitzhardinge {
2526aee16b3cSJeremy Fitzhardinge 	pgd_t *pgd;
2527aee16b3cSJeremy Fitzhardinge 	unsigned long next;
252857250a5bSJeremy Fitzhardinge 	unsigned long end = addr + size;
2529aee16b3cSJeremy Fitzhardinge 	int err;
2530aee16b3cSJeremy Fitzhardinge 
2531aee16b3cSJeremy Fitzhardinge 	BUG_ON(addr >= end);
2532aee16b3cSJeremy Fitzhardinge 	pgd = pgd_offset(mm, addr);
2533aee16b3cSJeremy Fitzhardinge 	do {
2534aee16b3cSJeremy Fitzhardinge 		next = pgd_addr_end(addr, end);
2535aee16b3cSJeremy Fitzhardinge 		err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
2536aee16b3cSJeremy Fitzhardinge 		if (err)
2537aee16b3cSJeremy Fitzhardinge 			break;
2538aee16b3cSJeremy Fitzhardinge 	} while (pgd++, addr = next, addr != end);
253957250a5bSJeremy Fitzhardinge 
2540aee16b3cSJeremy Fitzhardinge 	return err;
2541aee16b3cSJeremy Fitzhardinge }
2542aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range);
2543aee16b3cSJeremy Fitzhardinge 
25441da177e4SLinus Torvalds /*
25458f4e2101SHugh Dickins  * handle_pte_fault chooses page fault handler according to an entry
25468f4e2101SHugh Dickins  * which was read non-atomically.  Before making any commitment, on
25478f4e2101SHugh Dickins  * those architectures or configurations (e.g. i386 with PAE) which
2548a335b2e1SRyota Ozaki  * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault
25498f4e2101SHugh Dickins  * must check under lock before unmapping the pte and proceeding
25508f4e2101SHugh Dickins  * (but do_wp_page is only called after already making such a check;
2551a335b2e1SRyota Ozaki  * and do_anonymous_page can safely check later on).
25528f4e2101SHugh Dickins  */
25534c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
25548f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
25558f4e2101SHugh Dickins {
25568f4e2101SHugh Dickins 	int same = 1;
25578f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
25588f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
25594c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
25604c21e2f2SHugh Dickins 		spin_lock(ptl);
25618f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
25624c21e2f2SHugh Dickins 		spin_unlock(ptl);
25638f4e2101SHugh Dickins 	}
25648f4e2101SHugh Dickins #endif
25658f4e2101SHugh Dickins 	pte_unmap(page_table);
25668f4e2101SHugh Dickins 	return same;
25678f4e2101SHugh Dickins }
25688f4e2101SHugh Dickins 
25699de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
25706aab341eSLinus Torvalds {
25716aab341eSLinus Torvalds 	/*
25726aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
25736aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
25746aab341eSLinus Torvalds 	 * just copying from the original user address. If that
25756aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
25766aab341eSLinus Torvalds 	 */
25776aab341eSLinus Torvalds 	if (unlikely(!src)) {
25789b04c5feSCong Wang 		void *kaddr = kmap_atomic(dst);
25795d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
25805d2a2dbbSLinus Torvalds 
25815d2a2dbbSLinus Torvalds 		/*
25825d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
25835d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
25845d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
25855d2a2dbbSLinus Torvalds 		 * zeroes.
25865d2a2dbbSLinus Torvalds 		 */
25875d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
25883ecb01dfSJan Beulich 			clear_page(kaddr);
25899b04c5feSCong Wang 		kunmap_atomic(kaddr);
2590c4ec7b0dSDmitriy Monakhov 		flush_dcache_page(dst);
25910ed361deSNick Piggin 	} else
25929de455b2SAtsushi Nemoto 		copy_user_highpage(dst, src, va, vma);
25936aab341eSLinus Torvalds }
25946aab341eSLinus Torvalds 
25951da177e4SLinus Torvalds /*
25961da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
25971da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
25981da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
25991da177e4SLinus Torvalds  *
26001da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
26011da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
26021da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
26031da177e4SLinus Torvalds  * COW.
26041da177e4SLinus Torvalds  *
26051da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
26061da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
26071da177e4SLinus Torvalds  * and potentially makes it more efficient.
26081da177e4SLinus Torvalds  *
26098f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
26108f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
26118f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
26121da177e4SLinus Torvalds  */
26131da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
261465500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
26158f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
2616e6219ec8SNamhyung Kim 	__releases(ptl)
26171da177e4SLinus Torvalds {
26182ec74c3eSSagi Grimberg 	struct page *old_page, *new_page = NULL;
26191da177e4SLinus Torvalds 	pte_t entry;
2620b009c024SMichel Lespinasse 	int ret = 0;
2621a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
2622d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
26231756954cSDavid Rientjes 	unsigned long mmun_start = 0;	/* For mmu_notifiers */
26241756954cSDavid Rientjes 	unsigned long mmun_end = 0;	/* For mmu_notifiers */
26251da177e4SLinus Torvalds 
26266aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
2627251b97f5SPeter Zijlstra 	if (!old_page) {
2628251b97f5SPeter Zijlstra 		/*
2629251b97f5SPeter Zijlstra 		 * VM_MIXEDMAP !pfn_valid() case
2630251b97f5SPeter Zijlstra 		 *
2631251b97f5SPeter Zijlstra 		 * We should not cow pages in a shared writeable mapping.
2632251b97f5SPeter Zijlstra 		 * Just mark the pages writable as we can't do any dirty
2633251b97f5SPeter Zijlstra 		 * accounting on raw pfn maps.
2634251b97f5SPeter Zijlstra 		 */
2635251b97f5SPeter Zijlstra 		if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2636251b97f5SPeter Zijlstra 				     (VM_WRITE|VM_SHARED))
2637251b97f5SPeter Zijlstra 			goto reuse;
2638920fc356SHugh Dickins 		goto gotten;
2639251b97f5SPeter Zijlstra 	}
26401da177e4SLinus Torvalds 
2641d08b3851SPeter Zijlstra 	/*
2642ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
2643ee6a6457SPeter Zijlstra 	 * not dirty accountable.
2644d08b3851SPeter Zijlstra 	 */
26459a840895SHugh Dickins 	if (PageAnon(old_page) && !PageKsm(old_page)) {
2646ab967d86SHugh Dickins 		if (!trylock_page(old_page)) {
2647ab967d86SHugh Dickins 			page_cache_get(old_page);
2648ab967d86SHugh Dickins 			pte_unmap_unlock(page_table, ptl);
2649ab967d86SHugh Dickins 			lock_page(old_page);
2650ab967d86SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address,
2651ab967d86SHugh Dickins 							 &ptl);
2652ab967d86SHugh Dickins 			if (!pte_same(*page_table, orig_pte)) {
2653ab967d86SHugh Dickins 				unlock_page(old_page);
2654ab967d86SHugh Dickins 				goto unlock;
2655ab967d86SHugh Dickins 			}
2656ab967d86SHugh Dickins 			page_cache_release(old_page);
2657ab967d86SHugh Dickins 		}
2658b009c024SMichel Lespinasse 		if (reuse_swap_page(old_page)) {
2659c44b6743SRik van Riel 			/*
2660c44b6743SRik van Riel 			 * The page is all ours.  Move it to our anon_vma so
2661c44b6743SRik van Riel 			 * the rmap code will not search our parent or siblings.
2662c44b6743SRik van Riel 			 * Protected against the rmap code by the page lock.
2663c44b6743SRik van Riel 			 */
2664c44b6743SRik van Riel 			page_move_anon_rmap(old_page, vma, address);
2665ee6a6457SPeter Zijlstra 			unlock_page(old_page);
2666b009c024SMichel Lespinasse 			goto reuse;
2667b009c024SMichel Lespinasse 		}
2668b009c024SMichel Lespinasse 		unlock_page(old_page);
2669ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2670d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
2671ee6a6457SPeter Zijlstra 		/*
2672ee6a6457SPeter Zijlstra 		 * Only catch write-faults on shared writable pages,
2673ee6a6457SPeter Zijlstra 		 * read-only shared pages can get COWed by
2674ee6a6457SPeter Zijlstra 		 * get_user_pages(.write=1, .force=1).
2675ee6a6457SPeter Zijlstra 		 */
26769637a5efSDavid Howells 		if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2677c2ec175cSNick Piggin 			struct vm_fault vmf;
2678c2ec175cSNick Piggin 			int tmp;
2679c2ec175cSNick Piggin 
2680c2ec175cSNick Piggin 			vmf.virtual_address = (void __user *)(address &
2681c2ec175cSNick Piggin 								PAGE_MASK);
2682c2ec175cSNick Piggin 			vmf.pgoff = old_page->index;
2683c2ec175cSNick Piggin 			vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
2684c2ec175cSNick Piggin 			vmf.page = old_page;
2685c2ec175cSNick Piggin 
26869637a5efSDavid Howells 			/*
26879637a5efSDavid Howells 			 * Notify the address space that the page is about to
26889637a5efSDavid Howells 			 * become writable so that it can prohibit this or wait
26899637a5efSDavid Howells 			 * for the page to get into an appropriate state.
26909637a5efSDavid Howells 			 *
26919637a5efSDavid Howells 			 * We do this without the lock held, so that it can
26929637a5efSDavid Howells 			 * sleep if it needs to.
26939637a5efSDavid Howells 			 */
26949637a5efSDavid Howells 			page_cache_get(old_page);
26959637a5efSDavid Howells 			pte_unmap_unlock(page_table, ptl);
26969637a5efSDavid Howells 
2697c2ec175cSNick Piggin 			tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
2698c2ec175cSNick Piggin 			if (unlikely(tmp &
2699c2ec175cSNick Piggin 					(VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
2700c2ec175cSNick Piggin 				ret = tmp;
27019637a5efSDavid Howells 				goto unwritable_page;
2702c2ec175cSNick Piggin 			}
2703b827e496SNick Piggin 			if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
2704b827e496SNick Piggin 				lock_page(old_page);
2705b827e496SNick Piggin 				if (!old_page->mapping) {
2706b827e496SNick Piggin 					ret = 0; /* retry the fault */
2707b827e496SNick Piggin 					unlock_page(old_page);
2708b827e496SNick Piggin 					goto unwritable_page;
2709b827e496SNick Piggin 				}
2710b827e496SNick Piggin 			} else
2711b827e496SNick Piggin 				VM_BUG_ON(!PageLocked(old_page));
27129637a5efSDavid Howells 
27139637a5efSDavid Howells 			/*
27149637a5efSDavid Howells 			 * Since we dropped the lock we need to revalidate
27159637a5efSDavid Howells 			 * the PTE as someone else may have changed it.  If
27169637a5efSDavid Howells 			 * they did, we just return, as we can count on the
27179637a5efSDavid Howells 			 * MMU to tell us if they didn't also make it writable.
27189637a5efSDavid Howells 			 */
27199637a5efSDavid Howells 			page_table = pte_offset_map_lock(mm, pmd, address,
27209637a5efSDavid Howells 							 &ptl);
2721b827e496SNick Piggin 			if (!pte_same(*page_table, orig_pte)) {
2722b827e496SNick Piggin 				unlock_page(old_page);
27239637a5efSDavid Howells 				goto unlock;
2724b827e496SNick Piggin 			}
2725a200ee18SPeter Zijlstra 
2726a200ee18SPeter Zijlstra 			page_mkwrite = 1;
27279637a5efSDavid Howells 		}
2728d08b3851SPeter Zijlstra 		dirty_page = old_page;
2729d08b3851SPeter Zijlstra 		get_page(dirty_page);
27309637a5efSDavid Howells 
2731251b97f5SPeter Zijlstra reuse:
2732eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
273365500d23SHugh Dickins 		entry = pte_mkyoung(orig_pte);
273465500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2735954ffcb3SKAMEZAWA Hiroyuki 		if (ptep_set_access_flags(vma, address, page_table, entry,1))
27364b3073e1SRussell King 			update_mmu_cache(vma, address, page_table);
273772ddc8f7SMichel Lespinasse 		pte_unmap_unlock(page_table, ptl);
273865500d23SHugh Dickins 		ret |= VM_FAULT_WRITE;
273972ddc8f7SMichel Lespinasse 
274072ddc8f7SMichel Lespinasse 		if (!dirty_page)
274172ddc8f7SMichel Lespinasse 			return ret;
274272ddc8f7SMichel Lespinasse 
274372ddc8f7SMichel Lespinasse 		/*
274472ddc8f7SMichel Lespinasse 		 * Yes, Virginia, this is actually required to prevent a race
274572ddc8f7SMichel Lespinasse 		 * with clear_page_dirty_for_io() from clearing the page dirty
274672ddc8f7SMichel Lespinasse 		 * bit after it clear all dirty ptes, but before a racing
274772ddc8f7SMichel Lespinasse 		 * do_wp_page installs a dirty pte.
274872ddc8f7SMichel Lespinasse 		 *
2749a335b2e1SRyota Ozaki 		 * __do_fault is protected similarly.
275072ddc8f7SMichel Lespinasse 		 */
275172ddc8f7SMichel Lespinasse 		if (!page_mkwrite) {
275272ddc8f7SMichel Lespinasse 			wait_on_page_locked(dirty_page);
275372ddc8f7SMichel Lespinasse 			set_page_dirty_balance(dirty_page, page_mkwrite);
275441c4d25fSJan Kara 			/* file_update_time outside page_lock */
275541c4d25fSJan Kara 			if (vma->vm_file)
275641c4d25fSJan Kara 				file_update_time(vma->vm_file);
275772ddc8f7SMichel Lespinasse 		}
275872ddc8f7SMichel Lespinasse 		put_page(dirty_page);
275972ddc8f7SMichel Lespinasse 		if (page_mkwrite) {
276072ddc8f7SMichel Lespinasse 			struct address_space *mapping = dirty_page->mapping;
276172ddc8f7SMichel Lespinasse 
276272ddc8f7SMichel Lespinasse 			set_page_dirty(dirty_page);
276372ddc8f7SMichel Lespinasse 			unlock_page(dirty_page);
276472ddc8f7SMichel Lespinasse 			page_cache_release(dirty_page);
276572ddc8f7SMichel Lespinasse 			if (mapping)	{
276672ddc8f7SMichel Lespinasse 				/*
276772ddc8f7SMichel Lespinasse 				 * Some device drivers do not set page.mapping
276872ddc8f7SMichel Lespinasse 				 * but still dirty their pages
276972ddc8f7SMichel Lespinasse 				 */
277072ddc8f7SMichel Lespinasse 				balance_dirty_pages_ratelimited(mapping);
277172ddc8f7SMichel Lespinasse 			}
277272ddc8f7SMichel Lespinasse 		}
277372ddc8f7SMichel Lespinasse 
277472ddc8f7SMichel Lespinasse 		return ret;
27751da177e4SLinus Torvalds 	}
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds 	/*
27781da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
27791da177e4SLinus Torvalds 	 */
27801da177e4SLinus Torvalds 	page_cache_get(old_page);
2781920fc356SHugh Dickins gotten:
27828f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
278565500d23SHugh Dickins 		goto oom;
2786a13ea5b7SHugh Dickins 
278762eede62SHugh Dickins 	if (is_zero_pfn(pte_pfn(orig_pte))) {
2788a13ea5b7SHugh Dickins 		new_page = alloc_zeroed_user_highpage_movable(vma, address);
2789a13ea5b7SHugh Dickins 		if (!new_page)
2790a13ea5b7SHugh Dickins 			goto oom;
2791a13ea5b7SHugh Dickins 	} else {
2792769848c0SMel Gorman 		new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
27931da177e4SLinus Torvalds 		if (!new_page)
279465500d23SHugh Dickins 			goto oom;
2795a13ea5b7SHugh Dickins 		cow_user_page(new_page, old_page, address, vma);
2796a13ea5b7SHugh Dickins 	}
2797a13ea5b7SHugh Dickins 	__SetPageUptodate(new_page);
2798a13ea5b7SHugh Dickins 
27992c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))
28008a9f3ccdSBalbir Singh 		goto oom_free_new;
28018a9f3ccdSBalbir Singh 
28026bdb913fSHaggai Eran 	mmun_start  = address & PAGE_MASK;
28031756954cSDavid Rientjes 	mmun_end    = mmun_start + PAGE_SIZE;
28046bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
28056bdb913fSHaggai Eran 
28061da177e4SLinus Torvalds 	/*
28071da177e4SLinus Torvalds 	 * Re-check the pte - we dropped the lock
28081da177e4SLinus Torvalds 	 */
28098f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
281065500d23SHugh Dickins 	if (likely(pte_same(*page_table, orig_pte))) {
2811920fc356SHugh Dickins 		if (old_page) {
28124294621fSHugh Dickins 			if (!PageAnon(old_page)) {
281334e55232SKAMEZAWA Hiroyuki 				dec_mm_counter_fast(mm, MM_FILEPAGES);
281434e55232SKAMEZAWA Hiroyuki 				inc_mm_counter_fast(mm, MM_ANONPAGES);
28154294621fSHugh Dickins 			}
2816920fc356SHugh Dickins 		} else
281734e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
2818eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
281965500d23SHugh Dickins 		entry = mk_pte(new_page, vma->vm_page_prot);
282065500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
28214ce072f1SSiddha, Suresh B 		/*
28224ce072f1SSiddha, Suresh B 		 * Clear the pte entry and flush it first, before updating the
28234ce072f1SSiddha, Suresh B 		 * pte with the new entry. This will avoid a race condition
28244ce072f1SSiddha, Suresh B 		 * seen in the presence of one thread doing SMC and another
28254ce072f1SSiddha, Suresh B 		 * thread doing COW.
28264ce072f1SSiddha, Suresh B 		 */
2827828502d3SIzik Eidus 		ptep_clear_flush(vma, address, page_table);
28289617d95eSNick Piggin 		page_add_new_anon_rmap(new_page, vma, address);
2829828502d3SIzik Eidus 		/*
2830828502d3SIzik Eidus 		 * We call the notify macro here because, when using secondary
2831828502d3SIzik Eidus 		 * mmu page tables (such as kvm shadow page tables), we want the
2832828502d3SIzik Eidus 		 * new page to be mapped directly into the secondary page table.
2833828502d3SIzik Eidus 		 */
2834828502d3SIzik Eidus 		set_pte_at_notify(mm, address, page_table, entry);
28354b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
2836945754a1SNick Piggin 		if (old_page) {
2837945754a1SNick Piggin 			/*
2838945754a1SNick Piggin 			 * Only after switching the pte to the new page may
2839945754a1SNick Piggin 			 * we remove the mapcount here. Otherwise another
2840945754a1SNick Piggin 			 * process may come and find the rmap count decremented
2841945754a1SNick Piggin 			 * before the pte is switched to the new page, and
2842945754a1SNick Piggin 			 * "reuse" the old page writing into it while our pte
2843945754a1SNick Piggin 			 * here still points into it and can be read by other
2844945754a1SNick Piggin 			 * threads.
2845945754a1SNick Piggin 			 *
2846945754a1SNick Piggin 			 * The critical issue is to order this
2847945754a1SNick Piggin 			 * page_remove_rmap with the ptp_clear_flush above.
2848945754a1SNick Piggin 			 * Those stores are ordered by (if nothing else,)
2849945754a1SNick Piggin 			 * the barrier present in the atomic_add_negative
2850945754a1SNick Piggin 			 * in page_remove_rmap.
2851945754a1SNick Piggin 			 *
2852945754a1SNick Piggin 			 * Then the TLB flush in ptep_clear_flush ensures that
2853945754a1SNick Piggin 			 * no process can access the old page before the
2854945754a1SNick Piggin 			 * decremented mapcount is visible. And the old page
2855945754a1SNick Piggin 			 * cannot be reused until after the decremented
2856945754a1SNick Piggin 			 * mapcount is visible. So transitively, TLBs to
2857945754a1SNick Piggin 			 * old page will be flushed before it can be reused.
2858945754a1SNick Piggin 			 */
2859edc315fdSHugh Dickins 			page_remove_rmap(old_page);
2860945754a1SNick Piggin 		}
2861945754a1SNick Piggin 
28621da177e4SLinus Torvalds 		/* Free the old page.. */
28631da177e4SLinus Torvalds 		new_page = old_page;
2864f33ea7f4SNick Piggin 		ret |= VM_FAULT_WRITE;
28658a9f3ccdSBalbir Singh 	} else
28668a9f3ccdSBalbir Singh 		mem_cgroup_uncharge_page(new_page);
28678a9f3ccdSBalbir Singh 
28686bdb913fSHaggai Eran 	if (new_page)
28696bdb913fSHaggai Eran 		page_cache_release(new_page);
287065500d23SHugh Dickins unlock:
28718f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
28721756954cSDavid Rientjes 	if (mmun_end > mmun_start)
28736bdb913fSHaggai Eran 		mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2874e15f8c01SMichel Lespinasse 	if (old_page) {
2875e15f8c01SMichel Lespinasse 		/*
2876e15f8c01SMichel Lespinasse 		 * Don't let another task, with possibly unlocked vma,
2877e15f8c01SMichel Lespinasse 		 * keep the mlocked page.
2878e15f8c01SMichel Lespinasse 		 */
2879e15f8c01SMichel Lespinasse 		if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) {
2880e15f8c01SMichel Lespinasse 			lock_page(old_page);	/* LRU manipulation */
2881e15f8c01SMichel Lespinasse 			munlock_vma_page(old_page);
2882e15f8c01SMichel Lespinasse 			unlock_page(old_page);
2883e15f8c01SMichel Lespinasse 		}
2884e15f8c01SMichel Lespinasse 		page_cache_release(old_page);
2885e15f8c01SMichel Lespinasse 	}
2886f33ea7f4SNick Piggin 	return ret;
28878a9f3ccdSBalbir Singh oom_free_new:
28886dbf6d3bSHugh Dickins 	page_cache_release(new_page);
288965500d23SHugh Dickins oom:
289066521d5aSDominik Dingel 	if (old_page)
28911da177e4SLinus Torvalds 		page_cache_release(old_page);
28921da177e4SLinus Torvalds 	return VM_FAULT_OOM;
28939637a5efSDavid Howells 
28949637a5efSDavid Howells unwritable_page:
28959637a5efSDavid Howells 	page_cache_release(old_page);
2896c2ec175cSNick Piggin 	return ret;
28971da177e4SLinus Torvalds }
28981da177e4SLinus Torvalds 
289997a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma,
29001da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
29011da177e4SLinus Torvalds 		struct zap_details *details)
29021da177e4SLinus Torvalds {
2903f5cc4eefSAl Viro 	zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
29041da177e4SLinus Torvalds }
29051da177e4SLinus Torvalds 
29066b2dbba8SMichel Lespinasse static inline void unmap_mapping_range_tree(struct rb_root *root,
29071da177e4SLinus Torvalds 					    struct zap_details *details)
29081da177e4SLinus Torvalds {
29091da177e4SLinus Torvalds 	struct vm_area_struct *vma;
29101da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
29111da177e4SLinus Torvalds 
29126b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, root,
29131da177e4SLinus Torvalds 			details->first_index, details->last_index) {
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
29161da177e4SLinus Torvalds 		vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
29171da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
29181da177e4SLinus Torvalds 		zba = details->first_index;
29191da177e4SLinus Torvalds 		if (zba < vba)
29201da177e4SLinus Torvalds 			zba = vba;
29211da177e4SLinus Torvalds 		zea = details->last_index;
29221da177e4SLinus Torvalds 		if (zea > vea)
29231da177e4SLinus Torvalds 			zea = vea;
29241da177e4SLinus Torvalds 
292597a89413SPeter Zijlstra 		unmap_mapping_range_vma(vma,
29261da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
29271da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
292897a89413SPeter Zijlstra 				details);
29291da177e4SLinus Torvalds 	}
29301da177e4SLinus Torvalds }
29311da177e4SLinus Torvalds 
29321da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head,
29331da177e4SLinus Torvalds 					    struct zap_details *details)
29341da177e4SLinus Torvalds {
29351da177e4SLinus Torvalds 	struct vm_area_struct *vma;
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds 	/*
29381da177e4SLinus Torvalds 	 * In nonlinear VMAs there is no correspondence between virtual address
29391da177e4SLinus Torvalds 	 * offset and file offset.  So we must perform an exhaustive search
29401da177e4SLinus Torvalds 	 * across *all* the pages in each nonlinear VMA, not just the pages
29411da177e4SLinus Torvalds 	 * whose virtual address lies outside the file truncation point.
29421da177e4SLinus Torvalds 	 */
29436b2dbba8SMichel Lespinasse 	list_for_each_entry(vma, head, shared.nonlinear) {
29441da177e4SLinus Torvalds 		details->nonlinear_vma = vma;
294597a89413SPeter Zijlstra 		unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details);
29461da177e4SLinus Torvalds 	}
29471da177e4SLinus Torvalds }
29481da177e4SLinus Torvalds 
29491da177e4SLinus Torvalds /**
295072fd4a35SRobert 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.
29513d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
29521da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
29531da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
295425d9e2d1Snpiggin@suse.de  * boundary.  Note that this is different from truncate_pagecache(), which
29551da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
29561da177e4SLinus Torvalds  * partial pages.
29571da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
29581da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
29591da177e4SLinus Torvalds  * end of the file.
29601da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
29611da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
29621da177e4SLinus Torvalds  */
29631da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
29641da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
29651da177e4SLinus Torvalds {
29661da177e4SLinus Torvalds 	struct zap_details details;
29671da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
29681da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
29691da177e4SLinus Torvalds 
29701da177e4SLinus Torvalds 	/* Check for overflow. */
29711da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
29721da177e4SLinus Torvalds 		long long holeend =
29731da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
29741da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
29751da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
29761da177e4SLinus Torvalds 	}
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
29791da177e4SLinus Torvalds 	details.nonlinear_vma = NULL;
29801da177e4SLinus Torvalds 	details.first_index = hba;
29811da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
29821da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
29831da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
29841da177e4SLinus Torvalds 
298597a89413SPeter Zijlstra 
29863d48ae45SPeter Zijlstra 	mutex_lock(&mapping->i_mmap_mutex);
29876b2dbba8SMichel Lespinasse 	if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
29881da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
29891da177e4SLinus Torvalds 	if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
29901da177e4SLinus Torvalds 		unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
29913d48ae45SPeter Zijlstra 	mutex_unlock(&mapping->i_mmap_mutex);
29921da177e4SLinus Torvalds }
29931da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds /*
29968f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
29978f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
29988f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
29991da177e4SLinus Torvalds  */
300065500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
300165500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
300230c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
30031da177e4SLinus Torvalds {
30048f4e2101SHugh Dickins 	spinlock_t *ptl;
300556f31801SHugh Dickins 	struct page *page, *swapcache;
300665500d23SHugh Dickins 	swp_entry_t entry;
30071da177e4SLinus Torvalds 	pte_t pte;
3008d065bd81SMichel Lespinasse 	int locked;
300956039efaSKAMEZAWA Hiroyuki 	struct mem_cgroup *ptr;
3010ad8c2ee8SRik van Riel 	int exclusive = 0;
301183c54070SNick Piggin 	int ret = 0;
30121da177e4SLinus Torvalds 
30134c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
30148f4e2101SHugh Dickins 		goto out;
301565500d23SHugh Dickins 
301665500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
3017d1737fdbSAndi Kleen 	if (unlikely(non_swap_entry(entry))) {
30180697212aSChristoph Lameter 		if (is_migration_entry(entry)) {
30190697212aSChristoph Lameter 			migration_entry_wait(mm, pmd, address);
3020d1737fdbSAndi Kleen 		} else if (is_hwpoison_entry(entry)) {
3021d1737fdbSAndi Kleen 			ret = VM_FAULT_HWPOISON;
3022d1737fdbSAndi Kleen 		} else {
3023d1737fdbSAndi Kleen 			print_bad_pte(vma, address, orig_pte, NULL);
3024d99be1a8SHugh Dickins 			ret = VM_FAULT_SIGBUS;
3025d1737fdbSAndi Kleen 		}
30260697212aSChristoph Lameter 		goto out;
30270697212aSChristoph Lameter 	}
30280ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
30291da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
30301da177e4SLinus Torvalds 	if (!page) {
303102098feaSHugh Dickins 		page = swapin_readahead(entry,
303202098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, vma, address);
30331da177e4SLinus Torvalds 		if (!page) {
30341da177e4SLinus Torvalds 			/*
30358f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
30368f4e2101SHugh Dickins 			 * while we released the pte lock.
30371da177e4SLinus Torvalds 			 */
30388f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
30391da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
30401da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
30410ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
304265500d23SHugh Dickins 			goto unlock;
30431da177e4SLinus Torvalds 		}
30441da177e4SLinus Torvalds 
30451da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
30461da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
3047f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
3048456f998eSYing Han 		mem_cgroup_count_vm_event(mm, PGMAJFAULT);
3049d1737fdbSAndi Kleen 	} else if (PageHWPoison(page)) {
305071f72525SWu Fengguang 		/*
305171f72525SWu Fengguang 		 * hwpoisoned dirty swapcache pages are kept for killing
305271f72525SWu Fengguang 		 * owner processes (which may be unknown at hwpoison time)
305371f72525SWu Fengguang 		 */
3054d1737fdbSAndi Kleen 		ret = VM_FAULT_HWPOISON;
3055d1737fdbSAndi Kleen 		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
305656f31801SHugh Dickins 		swapcache = page;
30574779cb31SAndi Kleen 		goto out_release;
30581da177e4SLinus Torvalds 	}
30591da177e4SLinus Torvalds 
306056f31801SHugh Dickins 	swapcache = page;
3061d065bd81SMichel Lespinasse 	locked = lock_page_or_retry(page, mm, flags);
3062e709ffd6SRik van Riel 
306320a1022dSBalbir Singh 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
3064d065bd81SMichel Lespinasse 	if (!locked) {
3065d065bd81SMichel Lespinasse 		ret |= VM_FAULT_RETRY;
3066d065bd81SMichel Lespinasse 		goto out_release;
3067d065bd81SMichel Lespinasse 	}
30681da177e4SLinus Torvalds 
30694969c119SAndrea Arcangeli 	/*
307031c4a3d3SHugh Dickins 	 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
307131c4a3d3SHugh Dickins 	 * release the swapcache from under us.  The page pin, and pte_same
307231c4a3d3SHugh Dickins 	 * test below, are not enough to exclude that.  Even if it is still
307331c4a3d3SHugh Dickins 	 * swapcache, we need to check that the page's swap has not changed.
30744969c119SAndrea Arcangeli 	 */
307531c4a3d3SHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
30764969c119SAndrea Arcangeli 		goto out_page;
30774969c119SAndrea Arcangeli 
3078cbf86cfeSHugh Dickins 	page = ksm_might_need_to_copy(page, vma, address);
30794969c119SAndrea Arcangeli 	if (unlikely(!page)) {
30805ad64688SHugh Dickins 		ret = VM_FAULT_OOM;
30814969c119SAndrea Arcangeli 		page = swapcache;
30824969c119SAndrea Arcangeli 		goto out_page;
30834969c119SAndrea Arcangeli 	}
30845ad64688SHugh Dickins 
30852c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) {
3086073e587eSKAMEZAWA Hiroyuki 		ret = VM_FAULT_OOM;
3087bc43f75cSJohannes Weiner 		goto out_page;
3088073e587eSKAMEZAWA Hiroyuki 	}
3089073e587eSKAMEZAWA Hiroyuki 
30901da177e4SLinus Torvalds 	/*
30918f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
30921da177e4SLinus Torvalds 	 */
30938f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
30949e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
3095b8107480SKirill Korotaev 		goto out_nomap;
3096b8107480SKirill Korotaev 
3097b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
3098b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
3099b8107480SKirill Korotaev 		goto out_nomap;
31001da177e4SLinus Torvalds 	}
31011da177e4SLinus Torvalds 
31028c7c6e34SKAMEZAWA Hiroyuki 	/*
31038c7c6e34SKAMEZAWA Hiroyuki 	 * The page isn't present yet, go ahead with the fault.
31048c7c6e34SKAMEZAWA Hiroyuki 	 *
31058c7c6e34SKAMEZAWA Hiroyuki 	 * Be careful about the sequence of operations here.
31068c7c6e34SKAMEZAWA Hiroyuki 	 * To get its accounting right, reuse_swap_page() must be called
31078c7c6e34SKAMEZAWA Hiroyuki 	 * while the page is counted on swap but not yet in mapcount i.e.
31088c7c6e34SKAMEZAWA Hiroyuki 	 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
31098c7c6e34SKAMEZAWA Hiroyuki 	 * must be called after the swap_free(), or it will never succeed.
311003f3c433SKAMEZAWA Hiroyuki 	 * Because delete_from_swap_page() may be called by reuse_swap_page(),
311103f3c433SKAMEZAWA Hiroyuki 	 * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry
311203f3c433SKAMEZAWA Hiroyuki 	 * in page->private. In this case, a record in swap_cgroup  is silently
311303f3c433SKAMEZAWA Hiroyuki 	 * discarded at swap_free().
31148c7c6e34SKAMEZAWA Hiroyuki 	 */
31151da177e4SLinus Torvalds 
311634e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
3117b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter_fast(mm, MM_SWAPENTS);
31181da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
311930c9f3a9SLinus Torvalds 	if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
31201da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
312130c9f3a9SLinus Torvalds 		flags &= ~FAULT_FLAG_WRITE;
31229a5b489bSAndrea Arcangeli 		ret |= VM_FAULT_WRITE;
3123ad8c2ee8SRik van Riel 		exclusive = 1;
31241da177e4SLinus Torvalds 	}
31251da177e4SLinus Torvalds 	flush_icache_page(vma, page);
31261da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
312756f31801SHugh Dickins 	if (page == swapcache)
3128ad8c2ee8SRik van Riel 		do_page_add_anon_rmap(page, vma, address, exclusive);
312956f31801SHugh Dickins 	else /* ksm created a completely new copy */
313056f31801SHugh Dickins 		page_add_new_anon_rmap(page, vma, address);
313103f3c433SKAMEZAWA Hiroyuki 	/* It's better to call commit-charge after rmap is established */
313203f3c433SKAMEZAWA Hiroyuki 	mem_cgroup_commit_charge_swapin(page, ptr);
31331da177e4SLinus Torvalds 
3134c475a8abSHugh Dickins 	swap_free(entry);
3135b291f000SNick Piggin 	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
3136a2c43eedSHugh Dickins 		try_to_free_swap(page);
3137c475a8abSHugh Dickins 	unlock_page(page);
313856f31801SHugh Dickins 	if (page != swapcache) {
31394969c119SAndrea Arcangeli 		/*
31404969c119SAndrea Arcangeli 		 * Hold the lock to avoid the swap entry to be reused
31414969c119SAndrea Arcangeli 		 * until we take the PT lock for the pte_same() check
31424969c119SAndrea Arcangeli 		 * (to avoid false positives from pte_same). For
31434969c119SAndrea Arcangeli 		 * further safety release the lock after the swap_free
31444969c119SAndrea Arcangeli 		 * so that the swap count won't change under a
31454969c119SAndrea Arcangeli 		 * parallel locked swapcache.
31464969c119SAndrea Arcangeli 		 */
31474969c119SAndrea Arcangeli 		unlock_page(swapcache);
31484969c119SAndrea Arcangeli 		page_cache_release(swapcache);
31494969c119SAndrea Arcangeli 	}
3150c475a8abSHugh Dickins 
315130c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
315261469f1dSHugh Dickins 		ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
315361469f1dSHugh Dickins 		if (ret & VM_FAULT_ERROR)
315461469f1dSHugh Dickins 			ret &= VM_FAULT_ERROR;
31551da177e4SLinus Torvalds 		goto out;
31561da177e4SLinus Torvalds 	}
31571da177e4SLinus Torvalds 
31581da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
31594b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
316065500d23SHugh Dickins unlock:
31618f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
31621da177e4SLinus Torvalds out:
31631da177e4SLinus Torvalds 	return ret;
3164b8107480SKirill Korotaev out_nomap:
31657a81b88cSKAMEZAWA Hiroyuki 	mem_cgroup_cancel_charge_swapin(ptr);
31668f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
3167bc43f75cSJohannes Weiner out_page:
3168b8107480SKirill Korotaev 	unlock_page(page);
31694779cb31SAndi Kleen out_release:
3170b8107480SKirill Korotaev 	page_cache_release(page);
317156f31801SHugh Dickins 	if (page != swapcache) {
31724969c119SAndrea Arcangeli 		unlock_page(swapcache);
31734969c119SAndrea Arcangeli 		page_cache_release(swapcache);
31744969c119SAndrea Arcangeli 	}
317565500d23SHugh Dickins 	return ret;
31761da177e4SLinus Torvalds }
31771da177e4SLinus Torvalds 
31781da177e4SLinus Torvalds /*
31798ca3eb08SLuck, Tony  * This is like a special single-page "expand_{down|up}wards()",
31808ca3eb08SLuck, Tony  * except we must first make sure that 'address{-|+}PAGE_SIZE'
3181320b2b8dSLinus Torvalds  * doesn't hit another vma.
3182320b2b8dSLinus Torvalds  */
3183320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
3184320b2b8dSLinus Torvalds {
3185320b2b8dSLinus Torvalds 	address &= PAGE_MASK;
3186320b2b8dSLinus Torvalds 	if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
31870e8e50e2SLinus Torvalds 		struct vm_area_struct *prev = vma->vm_prev;
3188320b2b8dSLinus Torvalds 
31890e8e50e2SLinus Torvalds 		/*
31900e8e50e2SLinus Torvalds 		 * Is there a mapping abutting this one below?
31910e8e50e2SLinus Torvalds 		 *
31920e8e50e2SLinus Torvalds 		 * That's only ok if it's the same stack mapping
31930e8e50e2SLinus Torvalds 		 * that has gotten split..
31940e8e50e2SLinus Torvalds 		 */
31950e8e50e2SLinus Torvalds 		if (prev && prev->vm_end == address)
31960e8e50e2SLinus Torvalds 			return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
31970e8e50e2SLinus Torvalds 
3198d05f3169SMichal Hocko 		expand_downwards(vma, address - PAGE_SIZE);
3199320b2b8dSLinus Torvalds 	}
32008ca3eb08SLuck, Tony 	if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
32018ca3eb08SLuck, Tony 		struct vm_area_struct *next = vma->vm_next;
32028ca3eb08SLuck, Tony 
32038ca3eb08SLuck, Tony 		/* As VM_GROWSDOWN but s/below/above/ */
32048ca3eb08SLuck, Tony 		if (next && next->vm_start == address + PAGE_SIZE)
32058ca3eb08SLuck, Tony 			return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
32068ca3eb08SLuck, Tony 
32078ca3eb08SLuck, Tony 		expand_upwards(vma, address + PAGE_SIZE);
32088ca3eb08SLuck, Tony 	}
3209320b2b8dSLinus Torvalds 	return 0;
3210320b2b8dSLinus Torvalds }
3211320b2b8dSLinus Torvalds 
3212320b2b8dSLinus Torvalds /*
32138f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
32148f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
32158f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
32161da177e4SLinus Torvalds  */
321765500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
321865500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
321930c9f3a9SLinus Torvalds 		unsigned int flags)
32201da177e4SLinus Torvalds {
32218f4e2101SHugh Dickins 	struct page *page;
32228f4e2101SHugh Dickins 	spinlock_t *ptl;
32231da177e4SLinus Torvalds 	pte_t entry;
32241da177e4SLinus Torvalds 
32255528f913SLinus Torvalds 	pte_unmap(page_table);
3226320b2b8dSLinus Torvalds 
322711ac5524SLinus Torvalds 	/* Check if we need to add a guard page to the stack */
322811ac5524SLinus Torvalds 	if (check_stack_guard_page(vma, address) < 0)
322911ac5524SLinus Torvalds 		return VM_FAULT_SIGBUS;
323011ac5524SLinus Torvalds 
323111ac5524SLinus Torvalds 	/* Use the zero-page for reads */
323262eede62SHugh Dickins 	if (!(flags & FAULT_FLAG_WRITE)) {
323362eede62SHugh Dickins 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
323462eede62SHugh Dickins 						vma->vm_page_prot));
323511ac5524SLinus Torvalds 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
3236a13ea5b7SHugh Dickins 		if (!pte_none(*page_table))
3237a13ea5b7SHugh Dickins 			goto unlock;
3238a13ea5b7SHugh Dickins 		goto setpte;
3239a13ea5b7SHugh Dickins 	}
3240a13ea5b7SHugh Dickins 
32411da177e4SLinus Torvalds 	/* Allocate our own private page. */
32421da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
324365500d23SHugh Dickins 		goto oom;
3244769848c0SMel Gorman 	page = alloc_zeroed_user_highpage_movable(vma, address);
32451da177e4SLinus Torvalds 	if (!page)
324665500d23SHugh Dickins 		goto oom;
3247*52f37629SMinchan Kim 	/*
3248*52f37629SMinchan Kim 	 * The memory barrier inside __SetPageUptodate makes sure that
3249*52f37629SMinchan Kim 	 * preceeding stores to the page contents become visible before
3250*52f37629SMinchan Kim 	 * the set_pte_at() write.
3251*52f37629SMinchan Kim 	 */
32520ed361deSNick Piggin 	__SetPageUptodate(page);
32531da177e4SLinus Torvalds 
32542c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))
32558a9f3ccdSBalbir Singh 		goto oom_free_page;
32568a9f3ccdSBalbir Singh 
325765500d23SHugh Dickins 	entry = mk_pte(page, vma->vm_page_prot);
32581ac0cb5dSHugh Dickins 	if (vma->vm_flags & VM_WRITE)
32591ac0cb5dSHugh Dickins 		entry = pte_mkwrite(pte_mkdirty(entry));
32608f4e2101SHugh Dickins 
32618f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
32621c2fb7a4SAndrea Arcangeli 	if (!pte_none(*page_table))
32638f4e2101SHugh Dickins 		goto release;
32649ba69294SHugh Dickins 
326534e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
32669617d95eSNick Piggin 	page_add_new_anon_rmap(page, vma, address);
3267a13ea5b7SHugh Dickins setpte:
326865500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
32714b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
327265500d23SHugh Dickins unlock:
32738f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
327483c54070SNick Piggin 	return 0;
32758f4e2101SHugh Dickins release:
32768a9f3ccdSBalbir Singh 	mem_cgroup_uncharge_page(page);
32778f4e2101SHugh Dickins 	page_cache_release(page);
32788f4e2101SHugh Dickins 	goto unlock;
32798a9f3ccdSBalbir Singh oom_free_page:
32806dbf6d3bSHugh Dickins 	page_cache_release(page);
328165500d23SHugh Dickins oom:
32821da177e4SLinus Torvalds 	return VM_FAULT_OOM;
32831da177e4SLinus Torvalds }
32841da177e4SLinus Torvalds 
32851da177e4SLinus Torvalds /*
328654cb8821SNick Piggin  * __do_fault() tries to create a new page mapping. It aggressively
32871da177e4SLinus Torvalds  * tries to share with existing pages, but makes a separate copy if
328854cb8821SNick Piggin  * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
328954cb8821SNick Piggin  * the next page fault.
32901da177e4SLinus Torvalds  *
32911da177e4SLinus Torvalds  * As this is called only for pages that do not currently exist, we
32921da177e4SLinus Torvalds  * do not need to flush old virtual caches or the TLB.
32931da177e4SLinus Torvalds  *
32948f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
329516abfa08SHugh Dickins  * but allow concurrent faults), and pte neither mapped nor locked.
32968f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
32971da177e4SLinus Torvalds  */
329854cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
329916abfa08SHugh Dickins 		unsigned long address, pmd_t *pmd,
330054cb8821SNick Piggin 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
33011da177e4SLinus Torvalds {
330216abfa08SHugh Dickins 	pte_t *page_table;
33038f4e2101SHugh Dickins 	spinlock_t *ptl;
3304d0217ac0SNick Piggin 	struct page *page;
33051d65f86dSKAMEZAWA Hiroyuki 	struct page *cow_page;
33061da177e4SLinus Torvalds 	pte_t entry;
33071da177e4SLinus Torvalds 	int anon = 0;
3308d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
3309d0217ac0SNick Piggin 	struct vm_fault vmf;
3310d0217ac0SNick Piggin 	int ret;
3311a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
331254cb8821SNick Piggin 
33131d65f86dSKAMEZAWA Hiroyuki 	/*
33141d65f86dSKAMEZAWA Hiroyuki 	 * If we do COW later, allocate page befor taking lock_page()
33151d65f86dSKAMEZAWA Hiroyuki 	 * on the file cache page. This will reduce lock holding time.
33161d65f86dSKAMEZAWA Hiroyuki 	 */
33171d65f86dSKAMEZAWA Hiroyuki 	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
33181d65f86dSKAMEZAWA Hiroyuki 
33191d65f86dSKAMEZAWA Hiroyuki 		if (unlikely(anon_vma_prepare(vma)))
33201d65f86dSKAMEZAWA Hiroyuki 			return VM_FAULT_OOM;
33211d65f86dSKAMEZAWA Hiroyuki 
33221d65f86dSKAMEZAWA Hiroyuki 		cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
33231d65f86dSKAMEZAWA Hiroyuki 		if (!cow_page)
33241d65f86dSKAMEZAWA Hiroyuki 			return VM_FAULT_OOM;
33251d65f86dSKAMEZAWA Hiroyuki 
33261d65f86dSKAMEZAWA Hiroyuki 		if (mem_cgroup_newpage_charge(cow_page, mm, GFP_KERNEL)) {
33271d65f86dSKAMEZAWA Hiroyuki 			page_cache_release(cow_page);
33281d65f86dSKAMEZAWA Hiroyuki 			return VM_FAULT_OOM;
33291d65f86dSKAMEZAWA Hiroyuki 		}
33301d65f86dSKAMEZAWA Hiroyuki 	} else
33311d65f86dSKAMEZAWA Hiroyuki 		cow_page = NULL;
33321d65f86dSKAMEZAWA Hiroyuki 
3333d0217ac0SNick Piggin 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
3334d0217ac0SNick Piggin 	vmf.pgoff = pgoff;
3335d0217ac0SNick Piggin 	vmf.flags = flags;
3336d0217ac0SNick Piggin 	vmf.page = NULL;
33371da177e4SLinus Torvalds 
3338d0217ac0SNick Piggin 	ret = vma->vm_ops->fault(vma, &vmf);
3339d065bd81SMichel Lespinasse 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3340d065bd81SMichel Lespinasse 			    VM_FAULT_RETRY)))
33411d65f86dSKAMEZAWA Hiroyuki 		goto uncharge_out;
33421da177e4SLinus Torvalds 
3343a3b947eaSAndi Kleen 	if (unlikely(PageHWPoison(vmf.page))) {
3344a3b947eaSAndi Kleen 		if (ret & VM_FAULT_LOCKED)
3345a3b947eaSAndi Kleen 			unlock_page(vmf.page);
33461d65f86dSKAMEZAWA Hiroyuki 		ret = VM_FAULT_HWPOISON;
33471d65f86dSKAMEZAWA Hiroyuki 		goto uncharge_out;
3348a3b947eaSAndi Kleen 	}
3349a3b947eaSAndi Kleen 
3350d00806b1SNick Piggin 	/*
3351d0217ac0SNick Piggin 	 * For consistency in subsequent calls, make the faulted page always
3352d00806b1SNick Piggin 	 * locked.
3353d00806b1SNick Piggin 	 */
335483c54070SNick Piggin 	if (unlikely(!(ret & VM_FAULT_LOCKED)))
3355d0217ac0SNick Piggin 		lock_page(vmf.page);
335654cb8821SNick Piggin 	else
3357d0217ac0SNick Piggin 		VM_BUG_ON(!PageLocked(vmf.page));
3358d00806b1SNick Piggin 
33591da177e4SLinus Torvalds 	/*
33601da177e4SLinus Torvalds 	 * Should we do an early C-O-W break?
33611da177e4SLinus Torvalds 	 */
3362d0217ac0SNick Piggin 	page = vmf.page;
336354cb8821SNick Piggin 	if (flags & FAULT_FLAG_WRITE) {
33649637a5efSDavid Howells 		if (!(vma->vm_flags & VM_SHARED)) {
33651d65f86dSKAMEZAWA Hiroyuki 			page = cow_page;
336654cb8821SNick Piggin 			anon = 1;
3367d0217ac0SNick Piggin 			copy_user_highpage(page, vmf.page, address, vma);
33680ed361deSNick Piggin 			__SetPageUptodate(page);
33699637a5efSDavid Howells 		} else {
337054cb8821SNick Piggin 			/*
337154cb8821SNick Piggin 			 * If the page will be shareable, see if the backing
33729637a5efSDavid Howells 			 * address space wants to know that the page is about
337354cb8821SNick Piggin 			 * to become writable
337454cb8821SNick Piggin 			 */
337569676147SMark Fasheh 			if (vma->vm_ops->page_mkwrite) {
3376c2ec175cSNick Piggin 				int tmp;
3377c2ec175cSNick Piggin 
337869676147SMark Fasheh 				unlock_page(page);
3379b827e496SNick Piggin 				vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
3380c2ec175cSNick Piggin 				tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
3381c2ec175cSNick Piggin 				if (unlikely(tmp &
3382c2ec175cSNick Piggin 					  (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
3383c2ec175cSNick Piggin 					ret = tmp;
3384b827e496SNick Piggin 					goto unwritable_page;
338569676147SMark Fasheh 				}
3386b827e496SNick Piggin 				if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
338769676147SMark Fasheh 					lock_page(page);
3388d0217ac0SNick Piggin 					if (!page->mapping) {
3389b827e496SNick Piggin 						ret = 0; /* retry the fault */
3390b827e496SNick Piggin 						unlock_page(page);
3391b827e496SNick Piggin 						goto unwritable_page;
3392d0217ac0SNick Piggin 					}
3393b827e496SNick Piggin 				} else
3394b827e496SNick Piggin 					VM_BUG_ON(!PageLocked(page));
3395a200ee18SPeter Zijlstra 				page_mkwrite = 1;
33969637a5efSDavid Howells 			}
33979637a5efSDavid Howells 		}
339854cb8821SNick Piggin 
33991da177e4SLinus Torvalds 	}
34001da177e4SLinus Torvalds 
34018f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
34021da177e4SLinus Torvalds 
34031da177e4SLinus Torvalds 	/*
34041da177e4SLinus Torvalds 	 * This silly early PAGE_DIRTY setting removes a race
34051da177e4SLinus Torvalds 	 * due to the bad i386 page protection. But it's valid
34061da177e4SLinus Torvalds 	 * for other architectures too.
34071da177e4SLinus Torvalds 	 *
340830c9f3a9SLinus Torvalds 	 * Note that if FAULT_FLAG_WRITE is set, we either now have
34091da177e4SLinus Torvalds 	 * an exclusive copy of the page, or this is a shared mapping,
34101da177e4SLinus Torvalds 	 * so we can make it writable and dirty to avoid having to
34111da177e4SLinus Torvalds 	 * handle that later.
34121da177e4SLinus Torvalds 	 */
34131da177e4SLinus Torvalds 	/* Only go through if we didn't race with anybody else... */
34141c2fb7a4SAndrea Arcangeli 	if (likely(pte_same(*page_table, orig_pte))) {
3415d00806b1SNick Piggin 		flush_icache_page(vma, page);
3416d00806b1SNick Piggin 		entry = mk_pte(page, vma->vm_page_prot);
341754cb8821SNick Piggin 		if (flags & FAULT_FLAG_WRITE)
34181da177e4SLinus Torvalds 			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
34191da177e4SLinus Torvalds 		if (anon) {
342034e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
3421d00806b1SNick Piggin 			page_add_new_anon_rmap(page, vma, address);
3422f57e88a8SHugh Dickins 		} else {
342334e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_FILEPAGES);
3424d00806b1SNick Piggin 			page_add_file_rmap(page);
342554cb8821SNick Piggin 			if (flags & FAULT_FLAG_WRITE) {
3426d00806b1SNick Piggin 				dirty_page = page;
3427d08b3851SPeter Zijlstra 				get_page(dirty_page);
3428d08b3851SPeter Zijlstra 			}
34294294621fSHugh Dickins 		}
343064d6519dSLee Schermerhorn 		set_pte_at(mm, address, page_table, entry);
34311da177e4SLinus Torvalds 
3432d00806b1SNick Piggin 		/* no need to invalidate: a not-present page won't be cached */
34334b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
3434d00806b1SNick Piggin 	} else {
34351d65f86dSKAMEZAWA Hiroyuki 		if (cow_page)
34361d65f86dSKAMEZAWA Hiroyuki 			mem_cgroup_uncharge_page(cow_page);
3437d00806b1SNick Piggin 		if (anon)
3438d00806b1SNick Piggin 			page_cache_release(page);
3439d00806b1SNick Piggin 		else
344054cb8821SNick Piggin 			anon = 1; /* no anon but release faulted_page */
3441d00806b1SNick Piggin 	}
3442d00806b1SNick Piggin 
34438f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
3444d00806b1SNick Piggin 
3445b827e496SNick Piggin 	if (dirty_page) {
3446b827e496SNick Piggin 		struct address_space *mapping = page->mapping;
344741c4d25fSJan Kara 		int dirtied = 0;
34488f7b3d15SAnton Salikhmetov 
3449b827e496SNick Piggin 		if (set_page_dirty(dirty_page))
345041c4d25fSJan Kara 			dirtied = 1;
3451b827e496SNick Piggin 		unlock_page(dirty_page);
3452d08b3851SPeter Zijlstra 		put_page(dirty_page);
345341c4d25fSJan Kara 		if ((dirtied || page_mkwrite) && mapping) {
3454b827e496SNick Piggin 			/*
3455b827e496SNick Piggin 			 * Some device drivers do not set page.mapping but still
3456b827e496SNick Piggin 			 * dirty their pages
3457b827e496SNick Piggin 			 */
3458b827e496SNick Piggin 			balance_dirty_pages_ratelimited(mapping);
3459d08b3851SPeter Zijlstra 		}
3460d00806b1SNick Piggin 
3461b827e496SNick Piggin 		/* file_update_time outside page_lock */
346241c4d25fSJan Kara 		if (vma->vm_file && !page_mkwrite)
3463b827e496SNick Piggin 			file_update_time(vma->vm_file);
3464b827e496SNick Piggin 	} else {
3465b827e496SNick Piggin 		unlock_page(vmf.page);
3466b827e496SNick Piggin 		if (anon)
3467b827e496SNick Piggin 			page_cache_release(vmf.page);
3468b827e496SNick Piggin 	}
3469b827e496SNick Piggin 
3470b827e496SNick Piggin 	return ret;
3471b827e496SNick Piggin 
3472b827e496SNick Piggin unwritable_page:
3473b827e496SNick Piggin 	page_cache_release(page);
347483c54070SNick Piggin 	return ret;
34751d65f86dSKAMEZAWA Hiroyuki uncharge_out:
34761d65f86dSKAMEZAWA Hiroyuki 	/* fs's fault handler get error */
34771d65f86dSKAMEZAWA Hiroyuki 	if (cow_page) {
34781d65f86dSKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_page(cow_page);
34791d65f86dSKAMEZAWA Hiroyuki 		page_cache_release(cow_page);
34801d65f86dSKAMEZAWA Hiroyuki 	}
34811d65f86dSKAMEZAWA Hiroyuki 	return ret;
348254cb8821SNick Piggin }
3483d00806b1SNick Piggin 
348454cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
348554cb8821SNick Piggin 		unsigned long address, pte_t *page_table, pmd_t *pmd,
348630c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
348754cb8821SNick Piggin {
348854cb8821SNick Piggin 	pgoff_t pgoff = (((address & PAGE_MASK)
34890da7e01fSDean Nelson 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
349054cb8821SNick Piggin 
349116abfa08SHugh Dickins 	pte_unmap(page_table);
349216abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
349354cb8821SNick Piggin }
349454cb8821SNick Piggin 
3495f4b81804SJes Sorensen /*
34961da177e4SLinus Torvalds  * Fault of a previously existing named mapping. Repopulate the pte
34971da177e4SLinus Torvalds  * from the encoded file_pte if possible. This enables swappable
34981da177e4SLinus Torvalds  * nonlinear vmas.
34998f4e2101SHugh Dickins  *
35008f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
35018f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
35028f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
35031da177e4SLinus Torvalds  */
3504d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
350565500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
350630c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
35071da177e4SLinus Torvalds {
350865500d23SHugh Dickins 	pgoff_t pgoff;
35091da177e4SLinus Torvalds 
351030c9f3a9SLinus Torvalds 	flags |= FAULT_FLAG_NONLINEAR;
351130c9f3a9SLinus Torvalds 
35124c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
351383c54070SNick Piggin 		return 0;
35141da177e4SLinus Torvalds 
35152509ef26SHugh Dickins 	if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
351665500d23SHugh Dickins 		/*
351765500d23SHugh Dickins 		 * Page table corrupted: show pte and kill process.
351865500d23SHugh Dickins 		 */
35193dc14741SHugh Dickins 		print_bad_pte(vma, address, orig_pte, NULL);
3520d99be1a8SHugh Dickins 		return VM_FAULT_SIGBUS;
352165500d23SHugh Dickins 	}
352265500d23SHugh Dickins 
352365500d23SHugh Dickins 	pgoff = pte_to_pgoff(orig_pte);
352416abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
35251da177e4SLinus Torvalds }
35261da177e4SLinus Torvalds 
35279532fec1SMel Gorman int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
35289532fec1SMel Gorman 				unsigned long addr, int current_nid)
35299532fec1SMel Gorman {
35309532fec1SMel Gorman 	get_page(page);
35319532fec1SMel Gorman 
35329532fec1SMel Gorman 	count_vm_numa_event(NUMA_HINT_FAULTS);
35339532fec1SMel Gorman 	if (current_nid == numa_node_id())
35349532fec1SMel Gorman 		count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
35359532fec1SMel Gorman 
35369532fec1SMel Gorman 	return mpol_misplaced(page, vma, addr);
35379532fec1SMel Gorman }
35389532fec1SMel Gorman 
3539d10e63f2SMel Gorman int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3540d10e63f2SMel Gorman 		   unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
3541d10e63f2SMel Gorman {
35424daae3b4SMel Gorman 	struct page *page = NULL;
3543d10e63f2SMel Gorman 	spinlock_t *ptl;
3544cbee9f88SPeter Zijlstra 	int current_nid = -1;
3545cbee9f88SPeter Zijlstra 	int target_nid;
3546b8593bfdSMel Gorman 	bool migrated = false;
3547d10e63f2SMel Gorman 
3548d10e63f2SMel Gorman 	/*
3549d10e63f2SMel Gorman 	* The "pte" at this point cannot be used safely without
3550d10e63f2SMel Gorman 	* validation through pte_unmap_same(). It's of NUMA type but
3551d10e63f2SMel Gorman 	* the pfn may be screwed if the read is non atomic.
3552d10e63f2SMel Gorman 	*
3553d10e63f2SMel Gorman 	* ptep_modify_prot_start is not called as this is clearing
3554d10e63f2SMel Gorman 	* the _PAGE_NUMA bit and it is not really expected that there
3555d10e63f2SMel Gorman 	* would be concurrent hardware modifications to the PTE.
3556d10e63f2SMel Gorman 	*/
3557d10e63f2SMel Gorman 	ptl = pte_lockptr(mm, pmd);
3558d10e63f2SMel Gorman 	spin_lock(ptl);
35594daae3b4SMel Gorman 	if (unlikely(!pte_same(*ptep, pte))) {
35604daae3b4SMel Gorman 		pte_unmap_unlock(ptep, ptl);
35614daae3b4SMel Gorman 		goto out;
35624daae3b4SMel Gorman 	}
35634daae3b4SMel Gorman 
3564d10e63f2SMel Gorman 	pte = pte_mknonnuma(pte);
3565d10e63f2SMel Gorman 	set_pte_at(mm, addr, ptep, pte);
3566d10e63f2SMel Gorman 	update_mmu_cache(vma, addr, ptep);
3567d10e63f2SMel Gorman 
3568d10e63f2SMel Gorman 	page = vm_normal_page(vma, addr, pte);
3569d10e63f2SMel Gorman 	if (!page) {
3570d10e63f2SMel Gorman 		pte_unmap_unlock(ptep, ptl);
3571d10e63f2SMel Gorman 		return 0;
3572d10e63f2SMel Gorman 	}
3573d10e63f2SMel Gorman 
35744daae3b4SMel Gorman 	current_nid = page_to_nid(page);
35759532fec1SMel Gorman 	target_nid = numa_migrate_prep(page, vma, addr, current_nid);
3576d10e63f2SMel Gorman 	pte_unmap_unlock(ptep, ptl);
35774daae3b4SMel Gorman 	if (target_nid == -1) {
35784daae3b4SMel Gorman 		/*
35794daae3b4SMel Gorman 		 * Account for the fault against the current node if it not
35804daae3b4SMel Gorman 		 * being replaced regardless of where the page is located.
35814daae3b4SMel Gorman 		 */
35824daae3b4SMel Gorman 		current_nid = numa_node_id();
35834daae3b4SMel Gorman 		put_page(page);
35844daae3b4SMel Gorman 		goto out;
35854daae3b4SMel Gorman 	}
35864daae3b4SMel Gorman 
35874daae3b4SMel Gorman 	/* Migrate to the requested node */
3588b8593bfdSMel Gorman 	migrated = migrate_misplaced_page(page, target_nid);
3589b8593bfdSMel Gorman 	if (migrated)
35904daae3b4SMel Gorman 		current_nid = target_nid;
35914daae3b4SMel Gorman 
35924daae3b4SMel Gorman out:
35939532fec1SMel Gorman 	if (current_nid != -1)
3594b8593bfdSMel Gorman 		task_numa_fault(current_nid, 1, migrated);
3595d10e63f2SMel Gorman 	return 0;
3596d10e63f2SMel Gorman }
3597d10e63f2SMel Gorman 
3598d10e63f2SMel Gorman /* NUMA hinting page fault entry point for regular pmds */
3599d10e63f2SMel Gorman #ifdef CONFIG_NUMA_BALANCING
3600d10e63f2SMel Gorman static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3601d10e63f2SMel Gorman 		     unsigned long addr, pmd_t *pmdp)
3602d10e63f2SMel Gorman {
3603d10e63f2SMel Gorman 	pmd_t pmd;
3604d10e63f2SMel Gorman 	pte_t *pte, *orig_pte;
3605d10e63f2SMel Gorman 	unsigned long _addr = addr & PMD_MASK;
3606d10e63f2SMel Gorman 	unsigned long offset;
3607d10e63f2SMel Gorman 	spinlock_t *ptl;
3608d10e63f2SMel Gorman 	bool numa = false;
360903c5a6e1SMel Gorman 	int local_nid = numa_node_id();
3610d10e63f2SMel Gorman 
3611d10e63f2SMel Gorman 	spin_lock(&mm->page_table_lock);
3612d10e63f2SMel Gorman 	pmd = *pmdp;
3613d10e63f2SMel Gorman 	if (pmd_numa(pmd)) {
3614d10e63f2SMel Gorman 		set_pmd_at(mm, _addr, pmdp, pmd_mknonnuma(pmd));
3615d10e63f2SMel Gorman 		numa = true;
3616d10e63f2SMel Gorman 	}
3617d10e63f2SMel Gorman 	spin_unlock(&mm->page_table_lock);
3618d10e63f2SMel Gorman 
3619d10e63f2SMel Gorman 	if (!numa)
3620d10e63f2SMel Gorman 		return 0;
3621d10e63f2SMel Gorman 
3622d10e63f2SMel Gorman 	/* we're in a page fault so some vma must be in the range */
3623d10e63f2SMel Gorman 	BUG_ON(!vma);
3624d10e63f2SMel Gorman 	BUG_ON(vma->vm_start >= _addr + PMD_SIZE);
3625d10e63f2SMel Gorman 	offset = max(_addr, vma->vm_start) & ~PMD_MASK;
3626d10e63f2SMel Gorman 	VM_BUG_ON(offset >= PMD_SIZE);
3627d10e63f2SMel Gorman 	orig_pte = pte = pte_offset_map_lock(mm, pmdp, _addr, &ptl);
3628d10e63f2SMel Gorman 	pte += offset >> PAGE_SHIFT;
3629d10e63f2SMel Gorman 	for (addr = _addr + offset; addr < _addr + PMD_SIZE; pte++, addr += PAGE_SIZE) {
3630d10e63f2SMel Gorman 		pte_t pteval = *pte;
3631d10e63f2SMel Gorman 		struct page *page;
36329532fec1SMel Gorman 		int curr_nid = local_nid;
36339532fec1SMel Gorman 		int target_nid;
3634b8593bfdSMel Gorman 		bool migrated;
3635d10e63f2SMel Gorman 		if (!pte_present(pteval))
3636d10e63f2SMel Gorman 			continue;
3637d10e63f2SMel Gorman 		if (!pte_numa(pteval))
3638d10e63f2SMel Gorman 			continue;
3639d10e63f2SMel Gorman 		if (addr >= vma->vm_end) {
3640d10e63f2SMel Gorman 			vma = find_vma(mm, addr);
3641d10e63f2SMel Gorman 			/* there's a pte present so there must be a vma */
3642d10e63f2SMel Gorman 			BUG_ON(!vma);
3643d10e63f2SMel Gorman 			BUG_ON(addr < vma->vm_start);
3644d10e63f2SMel Gorman 		}
3645d10e63f2SMel Gorman 		if (pte_numa(pteval)) {
3646d10e63f2SMel Gorman 			pteval = pte_mknonnuma(pteval);
3647d10e63f2SMel Gorman 			set_pte_at(mm, addr, pte, pteval);
3648d10e63f2SMel Gorman 		}
3649d10e63f2SMel Gorman 		page = vm_normal_page(vma, addr, pteval);
3650d10e63f2SMel Gorman 		if (unlikely(!page))
3651d10e63f2SMel Gorman 			continue;
3652cbee9f88SPeter Zijlstra 		/* only check non-shared pages */
3653cbee9f88SPeter Zijlstra 		if (unlikely(page_mapcount(page) != 1))
3654cbee9f88SPeter Zijlstra 			continue;
36559532fec1SMel Gorman 
36569532fec1SMel Gorman 		/*
36579532fec1SMel Gorman 		 * Note that the NUMA fault is later accounted to either
36589532fec1SMel Gorman 		 * the node that is currently running or where the page is
36599532fec1SMel Gorman 		 * migrated to.
36609532fec1SMel Gorman 		 */
36619532fec1SMel Gorman 		curr_nid = local_nid;
36629532fec1SMel Gorman 		target_nid = numa_migrate_prep(page, vma, addr,
36639532fec1SMel Gorman 					       page_to_nid(page));
36649532fec1SMel Gorman 		if (target_nid == -1) {
36659532fec1SMel Gorman 			put_page(page);
36669532fec1SMel Gorman 			continue;
36679532fec1SMel Gorman 		}
36689532fec1SMel Gorman 
36699532fec1SMel Gorman 		/* Migrate to the requested node */
3670cbee9f88SPeter Zijlstra 		pte_unmap_unlock(pte, ptl);
3671b8593bfdSMel Gorman 		migrated = migrate_misplaced_page(page, target_nid);
3672b8593bfdSMel Gorman 		if (migrated)
36739532fec1SMel Gorman 			curr_nid = target_nid;
3674b8593bfdSMel Gorman 		task_numa_fault(curr_nid, 1, migrated);
3675cbee9f88SPeter Zijlstra 
3676cbee9f88SPeter Zijlstra 		pte = pte_offset_map_lock(mm, pmdp, addr, &ptl);
3677d10e63f2SMel Gorman 	}
3678d10e63f2SMel Gorman 	pte_unmap_unlock(orig_pte, ptl);
3679d10e63f2SMel Gorman 
3680d10e63f2SMel Gorman 	return 0;
3681d10e63f2SMel Gorman }
3682d10e63f2SMel Gorman #else
3683d10e63f2SMel Gorman static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3684d10e63f2SMel Gorman 		     unsigned long addr, pmd_t *pmdp)
3685d10e63f2SMel Gorman {
3686d10e63f2SMel Gorman 	BUG();
3687b3dd2070SAndrew Morton 	return 0;
3688d10e63f2SMel Gorman }
3689d10e63f2SMel Gorman #endif /* CONFIG_NUMA_BALANCING */
3690d10e63f2SMel Gorman 
36911da177e4SLinus Torvalds /*
36921da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
36931da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
36941da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
36951da177e4SLinus Torvalds  *
36961da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
36971da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
36981da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
36991da177e4SLinus Torvalds  *
3700c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
3701c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
3702c74df32cSHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
37031da177e4SLinus Torvalds  */
370471e3aac0SAndrea Arcangeli int handle_pte_fault(struct mm_struct *mm,
37051da177e4SLinus Torvalds 		     struct vm_area_struct *vma, unsigned long address,
370630c9f3a9SLinus Torvalds 		     pte_t *pte, pmd_t *pmd, unsigned int flags)
37071da177e4SLinus Torvalds {
37081da177e4SLinus Torvalds 	pte_t entry;
37098f4e2101SHugh Dickins 	spinlock_t *ptl;
37101da177e4SLinus Torvalds 
37118dab5241SBenjamin Herrenschmidt 	entry = *pte;
37121da177e4SLinus Torvalds 	if (!pte_present(entry)) {
371365500d23SHugh Dickins 		if (pte_none(entry)) {
3714f4b81804SJes Sorensen 			if (vma->vm_ops) {
37153c18ddd1SNick Piggin 				if (likely(vma->vm_ops->fault))
371654cb8821SNick Piggin 					return do_linear_fault(mm, vma, address,
371730c9f3a9SLinus Torvalds 						pte, pmd, flags, entry);
3718f4b81804SJes Sorensen 			}
3719f4b81804SJes Sorensen 			return do_anonymous_page(mm, vma, address,
372030c9f3a9SLinus Torvalds 						 pte, pmd, flags);
372165500d23SHugh Dickins 		}
37221da177e4SLinus Torvalds 		if (pte_file(entry))
3723d0217ac0SNick Piggin 			return do_nonlinear_fault(mm, vma, address,
372430c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
372565500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
372630c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
37271da177e4SLinus Torvalds 	}
37281da177e4SLinus Torvalds 
3729d10e63f2SMel Gorman 	if (pte_numa(entry))
3730d10e63f2SMel Gorman 		return do_numa_page(mm, vma, address, entry, pte, pmd);
3731d10e63f2SMel Gorman 
37324c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
37338f4e2101SHugh Dickins 	spin_lock(ptl);
37348f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
37358f4e2101SHugh Dickins 		goto unlock;
373630c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
37371da177e4SLinus Torvalds 		if (!pte_write(entry))
37388f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
37398f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
37401da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
37411da177e4SLinus Torvalds 	}
37421da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
374330c9f3a9SLinus Torvalds 	if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
37444b3073e1SRussell King 		update_mmu_cache(vma, address, pte);
37451a44e149SAndrea Arcangeli 	} else {
37461a44e149SAndrea Arcangeli 		/*
37471a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
37481a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
37491a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
37501a44e149SAndrea Arcangeli 		 * with threads.
37511a44e149SAndrea Arcangeli 		 */
375230c9f3a9SLinus Torvalds 		if (flags & FAULT_FLAG_WRITE)
375361c77326SShaohua Li 			flush_tlb_fix_spurious_fault(vma, address);
37541a44e149SAndrea Arcangeli 	}
37558f4e2101SHugh Dickins unlock:
37568f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
375783c54070SNick Piggin 	return 0;
37581da177e4SLinus Torvalds }
37591da177e4SLinus Torvalds 
37601da177e4SLinus Torvalds /*
37611da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
37621da177e4SLinus Torvalds  */
376383c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3764d06063ccSLinus Torvalds 		unsigned long address, unsigned int flags)
37651da177e4SLinus Torvalds {
37661da177e4SLinus Torvalds 	pgd_t *pgd;
37671da177e4SLinus Torvalds 	pud_t *pud;
37681da177e4SLinus Torvalds 	pmd_t *pmd;
37691da177e4SLinus Torvalds 	pte_t *pte;
37701da177e4SLinus Torvalds 
37711da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
37721da177e4SLinus Torvalds 
3773f8891e5eSChristoph Lameter 	count_vm_event(PGFAULT);
3774456f998eSYing Han 	mem_cgroup_count_vm_event(mm, PGFAULT);
37751da177e4SLinus Torvalds 
377634e55232SKAMEZAWA Hiroyuki 	/* do counter updates before entering really critical section. */
377734e55232SKAMEZAWA Hiroyuki 	check_sync_rss_stat(current);
377834e55232SKAMEZAWA Hiroyuki 
3779ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
378030c9f3a9SLinus Torvalds 		return hugetlb_fault(mm, vma, address, flags);
37811da177e4SLinus Torvalds 
37821f1d06c3SDavid Rientjes retry:
37831da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
37841da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
37851da177e4SLinus Torvalds 	if (!pud)
3786c74df32cSHugh Dickins 		return VM_FAULT_OOM;
37871da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
37881da177e4SLinus Torvalds 	if (!pmd)
3789c74df32cSHugh Dickins 		return VM_FAULT_OOM;
379071e3aac0SAndrea Arcangeli 	if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
379171e3aac0SAndrea Arcangeli 		if (!vma->vm_ops)
379271e3aac0SAndrea Arcangeli 			return do_huge_pmd_anonymous_page(mm, vma, address,
379371e3aac0SAndrea Arcangeli 							  pmd, flags);
379471e3aac0SAndrea Arcangeli 	} else {
379571e3aac0SAndrea Arcangeli 		pmd_t orig_pmd = *pmd;
37961f1d06c3SDavid Rientjes 		int ret;
37971f1d06c3SDavid Rientjes 
379871e3aac0SAndrea Arcangeli 		barrier();
379971e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(orig_pmd)) {
3800a1dd450bSWill Deacon 			unsigned int dirty = flags & FAULT_FLAG_WRITE;
3801a1dd450bSWill Deacon 
3802e53289c0SLinus Torvalds 			/*
3803e53289c0SLinus Torvalds 			 * If the pmd is splitting, return and retry the
3804e53289c0SLinus Torvalds 			 * the fault.  Alternative: wait until the split
3805e53289c0SLinus Torvalds 			 * is done, and goto retry.
3806e53289c0SLinus Torvalds 			 */
3807e53289c0SLinus Torvalds 			if (pmd_trans_splitting(orig_pmd))
3808e53289c0SLinus Torvalds 				return 0;
3809e53289c0SLinus Torvalds 
38103d59eebcSLinus Torvalds 			if (pmd_numa(orig_pmd))
38114daae3b4SMel Gorman 				return do_huge_pmd_numa_page(mm, vma, address,
3812d10e63f2SMel Gorman 							     orig_pmd, pmd);
3813d10e63f2SMel Gorman 
38143d59eebcSLinus Torvalds 			if (dirty && !pmd_write(orig_pmd)) {
38151f1d06c3SDavid Rientjes 				ret = do_huge_pmd_wp_page(mm, vma, address, pmd,
38161f1d06c3SDavid Rientjes 							  orig_pmd);
38171f1d06c3SDavid Rientjes 				/*
38181f1d06c3SDavid Rientjes 				 * If COW results in an oom, the huge pmd will
38191f1d06c3SDavid Rientjes 				 * have been split, so retry the fault on the
38201f1d06c3SDavid Rientjes 				 * pte for a smaller charge.
38211f1d06c3SDavid Rientjes 				 */
38221f1d06c3SDavid Rientjes 				if (unlikely(ret & VM_FAULT_OOM))
38231f1d06c3SDavid Rientjes 					goto retry;
38241f1d06c3SDavid Rientjes 				return ret;
3825a1dd450bSWill Deacon 			} else {
3826a1dd450bSWill Deacon 				huge_pmd_set_accessed(mm, vma, address, pmd,
3827a1dd450bSWill Deacon 						      orig_pmd, dirty);
38281f1d06c3SDavid Rientjes 			}
3829d10e63f2SMel Gorman 
383071e3aac0SAndrea Arcangeli 			return 0;
383171e3aac0SAndrea Arcangeli 		}
383271e3aac0SAndrea Arcangeli 	}
383371e3aac0SAndrea Arcangeli 
3834d10e63f2SMel Gorman 	if (pmd_numa(*pmd))
3835d10e63f2SMel Gorman 		return do_pmd_numa_page(mm, vma, address, pmd);
3836d10e63f2SMel Gorman 
383771e3aac0SAndrea Arcangeli 	/*
383871e3aac0SAndrea Arcangeli 	 * Use __pte_alloc instead of pte_alloc_map, because we can't
383971e3aac0SAndrea Arcangeli 	 * run pte_offset_map on the pmd, if an huge pmd could
384071e3aac0SAndrea Arcangeli 	 * materialize from under us from a different thread.
384171e3aac0SAndrea Arcangeli 	 */
38424fd01770SMel Gorman 	if (unlikely(pmd_none(*pmd)) &&
38434fd01770SMel Gorman 	    unlikely(__pte_alloc(mm, vma, pmd, address)))
3844c74df32cSHugh Dickins 		return VM_FAULT_OOM;
384571e3aac0SAndrea Arcangeli 	/* if an huge pmd materialized from under us just retry later */
384671e3aac0SAndrea Arcangeli 	if (unlikely(pmd_trans_huge(*pmd)))
384771e3aac0SAndrea Arcangeli 		return 0;
384871e3aac0SAndrea Arcangeli 	/*
384971e3aac0SAndrea Arcangeli 	 * A regular pmd is established and it can't morph into a huge pmd
385071e3aac0SAndrea Arcangeli 	 * from under us anymore at this point because we hold the mmap_sem
385171e3aac0SAndrea Arcangeli 	 * read mode and khugepaged takes it in write mode. So now it's
385271e3aac0SAndrea Arcangeli 	 * safe to run pte_offset_map().
385371e3aac0SAndrea Arcangeli 	 */
385471e3aac0SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
38551da177e4SLinus Torvalds 
385630c9f3a9SLinus Torvalds 	return handle_pte_fault(mm, vma, address, pte, pmd, flags);
38571da177e4SLinus Torvalds }
38581da177e4SLinus Torvalds 
38591da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
38601da177e4SLinus Torvalds /*
38611da177e4SLinus Torvalds  * Allocate page upper directory.
3862872fec16SHugh Dickins  * We've already handled the fast-path in-line.
38631da177e4SLinus Torvalds  */
38641bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
38651da177e4SLinus Torvalds {
3866c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
3867c74df32cSHugh Dickins 	if (!new)
38681bb3630eSHugh Dickins 		return -ENOMEM;
38691da177e4SLinus Torvalds 
3870362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3871362a61adSNick Piggin 
3872872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
38731bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
38745e541973SBenjamin Herrenschmidt 		pud_free(mm, new);
38751bb3630eSHugh Dickins 	else
38761da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
3877872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
38781bb3630eSHugh Dickins 	return 0;
38791da177e4SLinus Torvalds }
38801da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
38811da177e4SLinus Torvalds 
38821da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
38831da177e4SLinus Torvalds /*
38841da177e4SLinus Torvalds  * Allocate page middle directory.
3885872fec16SHugh Dickins  * We've already handled the fast-path in-line.
38861da177e4SLinus Torvalds  */
38871bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
38881da177e4SLinus Torvalds {
3889c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
3890c74df32cSHugh Dickins 	if (!new)
38911bb3630eSHugh Dickins 		return -ENOMEM;
38921da177e4SLinus Torvalds 
3893362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3894362a61adSNick Piggin 
3895872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
38961da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
38971bb3630eSHugh Dickins 	if (pud_present(*pud))		/* Another has populated it */
38985e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
38991bb3630eSHugh Dickins 	else
39001da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
39011da177e4SLinus Torvalds #else
39021bb3630eSHugh Dickins 	if (pgd_present(*pud))		/* Another has populated it */
39035e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
39041bb3630eSHugh Dickins 	else
39051da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
39061da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
3907872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
39081bb3630eSHugh Dickins 	return 0;
39091da177e4SLinus Torvalds }
39101da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
39111da177e4SLinus Torvalds 
39121da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA)
39131da177e4SLinus Torvalds 
39141da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR)
39155ce7852cSAdrian Bunk static struct vm_area_struct gate_vma;
39161da177e4SLinus Torvalds 
39171da177e4SLinus Torvalds static int __init gate_vma_init(void)
39181da177e4SLinus Torvalds {
39191da177e4SLinus Torvalds 	gate_vma.vm_mm = NULL;
39201da177e4SLinus Torvalds 	gate_vma.vm_start = FIXADDR_USER_START;
39211da177e4SLinus Torvalds 	gate_vma.vm_end = FIXADDR_USER_END;
3922b6558c4aSRoland McGrath 	gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
3923b6558c4aSRoland McGrath 	gate_vma.vm_page_prot = __P101;
3924909af768SJason Baron 
39251da177e4SLinus Torvalds 	return 0;
39261da177e4SLinus Torvalds }
39271da177e4SLinus Torvalds __initcall(gate_vma_init);
39281da177e4SLinus Torvalds #endif
39291da177e4SLinus Torvalds 
393031db58b3SStephen Wilson struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
39311da177e4SLinus Torvalds {
39321da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
39331da177e4SLinus Torvalds 	return &gate_vma;
39341da177e4SLinus Torvalds #else
39351da177e4SLinus Torvalds 	return NULL;
39361da177e4SLinus Torvalds #endif
39371da177e4SLinus Torvalds }
39381da177e4SLinus Torvalds 
3939cae5d390SStephen Wilson int in_gate_area_no_mm(unsigned long addr)
39401da177e4SLinus Torvalds {
39411da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
39421da177e4SLinus Torvalds 	if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
39431da177e4SLinus Torvalds 		return 1;
39441da177e4SLinus Torvalds #endif
39451da177e4SLinus Torvalds 	return 0;
39461da177e4SLinus Torvalds }
39471da177e4SLinus Torvalds 
39481da177e4SLinus Torvalds #endif	/* __HAVE_ARCH_GATE_AREA */
39490ec76a11SDavid Howells 
39501b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address,
3951f8ad0f49SJohannes Weiner 		pte_t **ptepp, spinlock_t **ptlp)
3952f8ad0f49SJohannes Weiner {
3953f8ad0f49SJohannes Weiner 	pgd_t *pgd;
3954f8ad0f49SJohannes Weiner 	pud_t *pud;
3955f8ad0f49SJohannes Weiner 	pmd_t *pmd;
3956f8ad0f49SJohannes Weiner 	pte_t *ptep;
3957f8ad0f49SJohannes Weiner 
3958f8ad0f49SJohannes Weiner 	pgd = pgd_offset(mm, address);
3959f8ad0f49SJohannes Weiner 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3960f8ad0f49SJohannes Weiner 		goto out;
3961f8ad0f49SJohannes Weiner 
3962f8ad0f49SJohannes Weiner 	pud = pud_offset(pgd, address);
3963f8ad0f49SJohannes Weiner 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3964f8ad0f49SJohannes Weiner 		goto out;
3965f8ad0f49SJohannes Weiner 
3966f8ad0f49SJohannes Weiner 	pmd = pmd_offset(pud, address);
3967f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
3968f8ad0f49SJohannes Weiner 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3969f8ad0f49SJohannes Weiner 		goto out;
3970f8ad0f49SJohannes Weiner 
3971f8ad0f49SJohannes Weiner 	/* We cannot handle huge page PFN maps. Luckily they don't exist. */
3972f8ad0f49SJohannes Weiner 	if (pmd_huge(*pmd))
3973f8ad0f49SJohannes Weiner 		goto out;
3974f8ad0f49SJohannes Weiner 
3975f8ad0f49SJohannes Weiner 	ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3976f8ad0f49SJohannes Weiner 	if (!ptep)
3977f8ad0f49SJohannes Weiner 		goto out;
3978f8ad0f49SJohannes Weiner 	if (!pte_present(*ptep))
3979f8ad0f49SJohannes Weiner 		goto unlock;
3980f8ad0f49SJohannes Weiner 	*ptepp = ptep;
3981f8ad0f49SJohannes Weiner 	return 0;
3982f8ad0f49SJohannes Weiner unlock:
3983f8ad0f49SJohannes Weiner 	pte_unmap_unlock(ptep, *ptlp);
3984f8ad0f49SJohannes Weiner out:
3985f8ad0f49SJohannes Weiner 	return -EINVAL;
3986f8ad0f49SJohannes Weiner }
3987f8ad0f49SJohannes Weiner 
39881b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address,
39891b36ba81SNamhyung Kim 			     pte_t **ptepp, spinlock_t **ptlp)
39901b36ba81SNamhyung Kim {
39911b36ba81SNamhyung Kim 	int res;
39921b36ba81SNamhyung Kim 
39931b36ba81SNamhyung Kim 	/* (void) is needed to make gcc happy */
39941b36ba81SNamhyung Kim 	(void) __cond_lock(*ptlp,
39951b36ba81SNamhyung Kim 			   !(res = __follow_pte(mm, address, ptepp, ptlp)));
39961b36ba81SNamhyung Kim 	return res;
39971b36ba81SNamhyung Kim }
39981b36ba81SNamhyung Kim 
39993b6748e2SJohannes Weiner /**
40003b6748e2SJohannes Weiner  * follow_pfn - look up PFN at a user virtual address
40013b6748e2SJohannes Weiner  * @vma: memory mapping
40023b6748e2SJohannes Weiner  * @address: user virtual address
40033b6748e2SJohannes Weiner  * @pfn: location to store found PFN
40043b6748e2SJohannes Weiner  *
40053b6748e2SJohannes Weiner  * Only IO mappings and raw PFN mappings are allowed.
40063b6748e2SJohannes Weiner  *
40073b6748e2SJohannes Weiner  * Returns zero and the pfn at @pfn on success, -ve otherwise.
40083b6748e2SJohannes Weiner  */
40093b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address,
40103b6748e2SJohannes Weiner 	unsigned long *pfn)
40113b6748e2SJohannes Weiner {
40123b6748e2SJohannes Weiner 	int ret = -EINVAL;
40133b6748e2SJohannes Weiner 	spinlock_t *ptl;
40143b6748e2SJohannes Weiner 	pte_t *ptep;
40153b6748e2SJohannes Weiner 
40163b6748e2SJohannes Weiner 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
40173b6748e2SJohannes Weiner 		return ret;
40183b6748e2SJohannes Weiner 
40193b6748e2SJohannes Weiner 	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
40203b6748e2SJohannes Weiner 	if (ret)
40213b6748e2SJohannes Weiner 		return ret;
40223b6748e2SJohannes Weiner 	*pfn = pte_pfn(*ptep);
40233b6748e2SJohannes Weiner 	pte_unmap_unlock(ptep, ptl);
40243b6748e2SJohannes Weiner 	return 0;
40253b6748e2SJohannes Weiner }
40263b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn);
40273b6748e2SJohannes Weiner 
402828b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
4029d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma,
403028b2ee20SRik van Riel 		unsigned long address, unsigned int flags,
4031d87fe660Svenkatesh.pallipadi@intel.com 		unsigned long *prot, resource_size_t *phys)
403228b2ee20SRik van Riel {
403303668a4dSJohannes Weiner 	int ret = -EINVAL;
403428b2ee20SRik van Riel 	pte_t *ptep, pte;
403528b2ee20SRik van Riel 	spinlock_t *ptl;
403628b2ee20SRik van Riel 
4037d87fe660Svenkatesh.pallipadi@intel.com 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4038d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
403928b2ee20SRik van Riel 
404003668a4dSJohannes Weiner 	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
4041d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
404228b2ee20SRik van Riel 	pte = *ptep;
404303668a4dSJohannes Weiner 
404428b2ee20SRik van Riel 	if ((flags & FOLL_WRITE) && !pte_write(pte))
404528b2ee20SRik van Riel 		goto unlock;
404628b2ee20SRik van Riel 
404728b2ee20SRik van Riel 	*prot = pgprot_val(pte_pgprot(pte));
404803668a4dSJohannes Weiner 	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
404928b2ee20SRik van Riel 
405003668a4dSJohannes Weiner 	ret = 0;
405128b2ee20SRik van Riel unlock:
405228b2ee20SRik van Riel 	pte_unmap_unlock(ptep, ptl);
405328b2ee20SRik van Riel out:
4054d87fe660Svenkatesh.pallipadi@intel.com 	return ret;
405528b2ee20SRik van Riel }
405628b2ee20SRik van Riel 
405728b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
405828b2ee20SRik van Riel 			void *buf, int len, int write)
405928b2ee20SRik van Riel {
406028b2ee20SRik van Riel 	resource_size_t phys_addr;
406128b2ee20SRik van Riel 	unsigned long prot = 0;
40622bc7273bSKOSAKI Motohiro 	void __iomem *maddr;
406328b2ee20SRik van Riel 	int offset = addr & (PAGE_SIZE-1);
406428b2ee20SRik van Riel 
4065d87fe660Svenkatesh.pallipadi@intel.com 	if (follow_phys(vma, addr, write, &prot, &phys_addr))
406628b2ee20SRik van Riel 		return -EINVAL;
406728b2ee20SRik van Riel 
406828b2ee20SRik van Riel 	maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot);
406928b2ee20SRik van Riel 	if (write)
407028b2ee20SRik van Riel 		memcpy_toio(maddr + offset, buf, len);
407128b2ee20SRik van Riel 	else
407228b2ee20SRik van Riel 		memcpy_fromio(buf, maddr + offset, len);
407328b2ee20SRik van Riel 	iounmap(maddr);
407428b2ee20SRik van Riel 
407528b2ee20SRik van Riel 	return len;
407628b2ee20SRik van Riel }
407728b2ee20SRik van Riel #endif
407828b2ee20SRik van Riel 
40790ec76a11SDavid Howells /*
4080206cb636SStephen Wilson  * Access another process' address space as given in mm.  If non-NULL, use the
4081206cb636SStephen Wilson  * given task for page fault accounting.
40820ec76a11SDavid Howells  */
4083206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
4084206cb636SStephen Wilson 		unsigned long addr, void *buf, int len, int write)
40850ec76a11SDavid Howells {
40860ec76a11SDavid Howells 	struct vm_area_struct *vma;
40870ec76a11SDavid Howells 	void *old_buf = buf;
40880ec76a11SDavid Howells 
40890ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
4090183ff22bSSimon Arlott 	/* ignore errors, just check how much was successfully transferred */
40910ec76a11SDavid Howells 	while (len) {
40920ec76a11SDavid Howells 		int bytes, ret, offset;
40930ec76a11SDavid Howells 		void *maddr;
409428b2ee20SRik van Riel 		struct page *page = NULL;
40950ec76a11SDavid Howells 
40960ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
40970ec76a11SDavid Howells 				write, 1, &page, &vma);
409828b2ee20SRik van Riel 		if (ret <= 0) {
409928b2ee20SRik van Riel 			/*
410028b2ee20SRik van Riel 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
410128b2ee20SRik van Riel 			 * we can access using slightly different code.
410228b2ee20SRik van Riel 			 */
410328b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
410428b2ee20SRik van Riel 			vma = find_vma(mm, addr);
4105fe936dfcSMichael Ellerman 			if (!vma || vma->vm_start > addr)
41060ec76a11SDavid Howells 				break;
410728b2ee20SRik van Riel 			if (vma->vm_ops && vma->vm_ops->access)
410828b2ee20SRik van Riel 				ret = vma->vm_ops->access(vma, addr, buf,
410928b2ee20SRik van Riel 							  len, write);
411028b2ee20SRik van Riel 			if (ret <= 0)
411128b2ee20SRik van Riel #endif
411228b2ee20SRik van Riel 				break;
411328b2ee20SRik van Riel 			bytes = ret;
411428b2ee20SRik van Riel 		} else {
41150ec76a11SDavid Howells 			bytes = len;
41160ec76a11SDavid Howells 			offset = addr & (PAGE_SIZE-1);
41170ec76a11SDavid Howells 			if (bytes > PAGE_SIZE-offset)
41180ec76a11SDavid Howells 				bytes = PAGE_SIZE-offset;
41190ec76a11SDavid Howells 
41200ec76a11SDavid Howells 			maddr = kmap(page);
41210ec76a11SDavid Howells 			if (write) {
41220ec76a11SDavid Howells 				copy_to_user_page(vma, page, addr,
41230ec76a11SDavid Howells 						  maddr + offset, buf, bytes);
41240ec76a11SDavid Howells 				set_page_dirty_lock(page);
41250ec76a11SDavid Howells 			} else {
41260ec76a11SDavid Howells 				copy_from_user_page(vma, page, addr,
41270ec76a11SDavid Howells 						    buf, maddr + offset, bytes);
41280ec76a11SDavid Howells 			}
41290ec76a11SDavid Howells 			kunmap(page);
41300ec76a11SDavid Howells 			page_cache_release(page);
413128b2ee20SRik van Riel 		}
41320ec76a11SDavid Howells 		len -= bytes;
41330ec76a11SDavid Howells 		buf += bytes;
41340ec76a11SDavid Howells 		addr += bytes;
41350ec76a11SDavid Howells 	}
41360ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
41370ec76a11SDavid Howells 
41380ec76a11SDavid Howells 	return buf - old_buf;
41390ec76a11SDavid Howells }
414003252919SAndi Kleen 
41415ddd36b9SStephen Wilson /**
4142ae91dbfcSRandy Dunlap  * access_remote_vm - access another process' address space
41435ddd36b9SStephen Wilson  * @mm:		the mm_struct of the target address space
41445ddd36b9SStephen Wilson  * @addr:	start address to access
41455ddd36b9SStephen Wilson  * @buf:	source or destination buffer
41465ddd36b9SStephen Wilson  * @len:	number of bytes to transfer
41475ddd36b9SStephen Wilson  * @write:	whether the access is a write
41485ddd36b9SStephen Wilson  *
41495ddd36b9SStephen Wilson  * The caller must hold a reference on @mm.
41505ddd36b9SStephen Wilson  */
41515ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr,
41525ddd36b9SStephen Wilson 		void *buf, int len, int write)
41535ddd36b9SStephen Wilson {
41545ddd36b9SStephen Wilson 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
41555ddd36b9SStephen Wilson }
41565ddd36b9SStephen Wilson 
415703252919SAndi Kleen /*
4158206cb636SStephen Wilson  * Access another process' address space.
4159206cb636SStephen Wilson  * Source/target buffer must be kernel space,
4160206cb636SStephen Wilson  * Do not walk the page table directly, use get_user_pages
4161206cb636SStephen Wilson  */
4162206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr,
4163206cb636SStephen Wilson 		void *buf, int len, int write)
4164206cb636SStephen Wilson {
4165206cb636SStephen Wilson 	struct mm_struct *mm;
4166206cb636SStephen Wilson 	int ret;
4167206cb636SStephen Wilson 
4168206cb636SStephen Wilson 	mm = get_task_mm(tsk);
4169206cb636SStephen Wilson 	if (!mm)
4170206cb636SStephen Wilson 		return 0;
4171206cb636SStephen Wilson 
4172206cb636SStephen Wilson 	ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
4173206cb636SStephen Wilson 	mmput(mm);
4174206cb636SStephen Wilson 
4175206cb636SStephen Wilson 	return ret;
4176206cb636SStephen Wilson }
4177206cb636SStephen Wilson 
417803252919SAndi Kleen /*
417903252919SAndi Kleen  * Print the name of a VMA.
418003252919SAndi Kleen  */
418103252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip)
418203252919SAndi Kleen {
418303252919SAndi Kleen 	struct mm_struct *mm = current->mm;
418403252919SAndi Kleen 	struct vm_area_struct *vma;
418503252919SAndi Kleen 
4186e8bff74aSIngo Molnar 	/*
4187e8bff74aSIngo Molnar 	 * Do not print if we are in atomic
4188e8bff74aSIngo Molnar 	 * contexts (in exception stacks, etc.):
4189e8bff74aSIngo Molnar 	 */
4190e8bff74aSIngo Molnar 	if (preempt_count())
4191e8bff74aSIngo Molnar 		return;
4192e8bff74aSIngo Molnar 
419303252919SAndi Kleen 	down_read(&mm->mmap_sem);
419403252919SAndi Kleen 	vma = find_vma(mm, ip);
419503252919SAndi Kleen 	if (vma && vma->vm_file) {
419603252919SAndi Kleen 		struct file *f = vma->vm_file;
419703252919SAndi Kleen 		char *buf = (char *)__get_free_page(GFP_KERNEL);
419803252919SAndi Kleen 		if (buf) {
41992fbc57c5SAndy Shevchenko 			char *p;
420003252919SAndi Kleen 
4201cf28b486SJan Blunck 			p = d_path(&f->f_path, buf, PAGE_SIZE);
420203252919SAndi Kleen 			if (IS_ERR(p))
420303252919SAndi Kleen 				p = "?";
42042fbc57c5SAndy Shevchenko 			printk("%s%s[%lx+%lx]", prefix, kbasename(p),
420503252919SAndi Kleen 					vma->vm_start,
420603252919SAndi Kleen 					vma->vm_end - vma->vm_start);
420703252919SAndi Kleen 			free_page((unsigned long)buf);
420803252919SAndi Kleen 		}
420903252919SAndi Kleen 	}
421051a07e50SJeff Liu 	up_read(&mm->mmap_sem);
421103252919SAndi Kleen }
42123ee1afa3SNick Piggin 
42133ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
42143ee1afa3SNick Piggin void might_fault(void)
42153ee1afa3SNick Piggin {
421695156f00SPeter Zijlstra 	/*
421795156f00SPeter Zijlstra 	 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
421895156f00SPeter Zijlstra 	 * holding the mmap_sem, this is safe because kernel memory doesn't
421995156f00SPeter Zijlstra 	 * get paged out, therefore we'll never actually fault, and the
422095156f00SPeter Zijlstra 	 * below annotations will generate false positives.
422195156f00SPeter Zijlstra 	 */
422295156f00SPeter Zijlstra 	if (segment_eq(get_fs(), KERNEL_DS))
422395156f00SPeter Zijlstra 		return;
422495156f00SPeter Zijlstra 
42253ee1afa3SNick Piggin 	might_sleep();
42263ee1afa3SNick Piggin 	/*
42273ee1afa3SNick Piggin 	 * it would be nicer only to annotate paths which are not under
42283ee1afa3SNick Piggin 	 * pagefault_disable, however that requires a larger audit and
42293ee1afa3SNick Piggin 	 * providing helpers like get_user_atomic.
42303ee1afa3SNick Piggin 	 */
42313ee1afa3SNick Piggin 	if (!in_atomic() && current->mm)
42323ee1afa3SNick Piggin 		might_lock_read(&current->mm->mmap_sem);
42333ee1afa3SNick Piggin }
42343ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault);
42353ee1afa3SNick Piggin #endif
423647ad8475SAndrea Arcangeli 
423747ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
423847ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page,
423947ad8475SAndrea Arcangeli 				unsigned long addr,
424047ad8475SAndrea Arcangeli 				unsigned int pages_per_huge_page)
424147ad8475SAndrea Arcangeli {
424247ad8475SAndrea Arcangeli 	int i;
424347ad8475SAndrea Arcangeli 	struct page *p = page;
424447ad8475SAndrea Arcangeli 
424547ad8475SAndrea Arcangeli 	might_sleep();
424647ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page;
424747ad8475SAndrea Arcangeli 	     i++, p = mem_map_next(p, page, i)) {
424847ad8475SAndrea Arcangeli 		cond_resched();
424947ad8475SAndrea Arcangeli 		clear_user_highpage(p, addr + i * PAGE_SIZE);
425047ad8475SAndrea Arcangeli 	}
425147ad8475SAndrea Arcangeli }
425247ad8475SAndrea Arcangeli void clear_huge_page(struct page *page,
425347ad8475SAndrea Arcangeli 		     unsigned long addr, unsigned int pages_per_huge_page)
425447ad8475SAndrea Arcangeli {
425547ad8475SAndrea Arcangeli 	int i;
425647ad8475SAndrea Arcangeli 
425747ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
425847ad8475SAndrea Arcangeli 		clear_gigantic_page(page, addr, pages_per_huge_page);
425947ad8475SAndrea Arcangeli 		return;
426047ad8475SAndrea Arcangeli 	}
426147ad8475SAndrea Arcangeli 
426247ad8475SAndrea Arcangeli 	might_sleep();
426347ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
426447ad8475SAndrea Arcangeli 		cond_resched();
426547ad8475SAndrea Arcangeli 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
426647ad8475SAndrea Arcangeli 	}
426747ad8475SAndrea Arcangeli }
426847ad8475SAndrea Arcangeli 
426947ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src,
427047ad8475SAndrea Arcangeli 				    unsigned long addr,
427147ad8475SAndrea Arcangeli 				    struct vm_area_struct *vma,
427247ad8475SAndrea Arcangeli 				    unsigned int pages_per_huge_page)
427347ad8475SAndrea Arcangeli {
427447ad8475SAndrea Arcangeli 	int i;
427547ad8475SAndrea Arcangeli 	struct page *dst_base = dst;
427647ad8475SAndrea Arcangeli 	struct page *src_base = src;
427747ad8475SAndrea Arcangeli 
427847ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; ) {
427947ad8475SAndrea Arcangeli 		cond_resched();
428047ad8475SAndrea Arcangeli 		copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
428147ad8475SAndrea Arcangeli 
428247ad8475SAndrea Arcangeli 		i++;
428347ad8475SAndrea Arcangeli 		dst = mem_map_next(dst, dst_base, i);
428447ad8475SAndrea Arcangeli 		src = mem_map_next(src, src_base, i);
428547ad8475SAndrea Arcangeli 	}
428647ad8475SAndrea Arcangeli }
428747ad8475SAndrea Arcangeli 
428847ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src,
428947ad8475SAndrea Arcangeli 			 unsigned long addr, struct vm_area_struct *vma,
429047ad8475SAndrea Arcangeli 			 unsigned int pages_per_huge_page)
429147ad8475SAndrea Arcangeli {
429247ad8475SAndrea Arcangeli 	int i;
429347ad8475SAndrea Arcangeli 
429447ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
429547ad8475SAndrea Arcangeli 		copy_user_gigantic_page(dst, src, addr, vma,
429647ad8475SAndrea Arcangeli 					pages_per_huge_page);
429747ad8475SAndrea Arcangeli 		return;
429847ad8475SAndrea Arcangeli 	}
429947ad8475SAndrea Arcangeli 
430047ad8475SAndrea Arcangeli 	might_sleep();
430147ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
430247ad8475SAndrea Arcangeli 		cond_resched();
430347ad8475SAndrea Arcangeli 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
430447ad8475SAndrea Arcangeli 	}
430547ad8475SAndrea Arcangeli }
430647ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
4307